xref: /aosp_15_r20/external/grpc-grpc/src/python/grpcio/grpc/_cython/_cygrpc/security.pyx.pxi (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1# Copyright 2016 gRPC authors.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15from libc.string cimport memcpy
16
17import pkgutil
18
19
20cdef grpc_ssl_roots_override_result ssl_roots_override_callback(
21    char **pem_root_certs) nogil:
22  with gil:
23    pkg = __name__
24    if pkg.endswith('.cygrpc'):
25      pkg = pkg[:-len('.cygrpc')]
26    temporary_pem_root_certs = pkgutil.get_data(pkg, '_credentials/roots.pem')
27    pem_root_certs[0] = <char *>gpr_malloc(len(temporary_pem_root_certs) + 1)
28    memcpy(
29        pem_root_certs[0], <char *>temporary_pem_root_certs,
30        len(temporary_pem_root_certs))
31    pem_root_certs[0][len(temporary_pem_root_certs)] = '\0'
32
33  return GRPC_SSL_ROOTS_OVERRIDE_OK
34
35
36def peer_identities(Call call):
37  cdef grpc_auth_context* auth_context
38  cdef grpc_auth_property_iterator properties
39  cdef const grpc_auth_property* property
40
41  auth_context = grpc_call_auth_context(call.c_call)
42  if auth_context == NULL:
43    return None
44  properties = grpc_auth_context_peer_identity(auth_context)
45  identities = []
46  while True:
47    property = grpc_auth_property_iterator_next(&properties)
48    if property == NULL:
49      break
50    if property.value != NULL:
51      identities.append(<bytes>(property.value))
52  grpc_auth_context_release(auth_context)
53  return identities if identities else None
54
55def peer_identity_key(Call call):
56  cdef grpc_auth_context* auth_context
57  cdef const char* c_key
58  auth_context = grpc_call_auth_context(call.c_call)
59  if auth_context == NULL:
60    return None
61  c_key = grpc_auth_context_peer_identity_property_name(auth_context)
62  if c_key == NULL:
63    key = None
64  else:
65    key = <bytes> grpc_auth_context_peer_identity_property_name(auth_context)
66  grpc_auth_context_release(auth_context)
67  return key
68
69def auth_context(Call call):
70  cdef grpc_auth_context* auth_context
71  cdef grpc_auth_property_iterator properties
72  cdef const grpc_auth_property* property
73
74  auth_context = grpc_call_auth_context(call.c_call)
75  if auth_context == NULL:
76    return {}
77  properties = grpc_auth_context_property_iterator(auth_context)
78  py_auth_context = {}
79  while True:
80    property = grpc_auth_property_iterator_next(&properties)
81    if property == NULL:
82      break
83    if property.name != NULL and property.value != NULL:
84      key = <bytes> property.name
85      if key in py_auth_context:
86        py_auth_context[key].append(<bytes>(property.value))
87      else:
88        py_auth_context[key] = [<bytes> property.value]
89  grpc_auth_context_release(auth_context)
90  return py_auth_context
91
92