rb_server_credentials.c
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #include <ruby/ruby.h>
20 
21 #include "rb_server_credentials.h"
22 
23 #include "rb_grpc.h"
25 
26 #include <grpc/grpc.h>
27 #include <grpc/grpc_security.h>
28 #include <grpc/support/log.h>
29 
30 /* grpc_rb_cServerCredentials is the ruby class that proxies
31  grpc_server_credentials. */
32 static VALUE grpc_rb_cServerCredentials = Qnil;
33 
34 /* grpc_rb_server_credentials wraps a grpc_server_credentials. It provides a
35  peer ruby object, 'mark' to hold references to objects involved in
36  constructing the server credentials. */
38  /* Holder of ruby objects involved in constructing the server credentials */
39  VALUE mark;
40  /* The actual server credentials */
43 
44 /* Destroys the server credentials instances. */
47  if (p == NULL) {
48  return;
49  };
51 
52  /* Delete the wrapped object if the mark object is Qnil, which indicates that
53  no other object is the actual owner. */
54  if (wrapper->wrapped != NULL && wrapper->mark == Qnil) {
56  wrapper->wrapped = NULL;
57  }
58 
59  xfree(p);
60 }
61 
62 /* Destroys the server credentials instances. */
63 static void grpc_rb_server_credentials_free(void* p) {
66 }
67 
68 /* Protects the mark object from GC */
69 static void grpc_rb_server_credentials_mark(void* p) {
71  if (p == NULL) {
72  return;
73  }
75 
76  /* If it's not already cleaned up, mark the mark object */
77  if (wrapper->mark != Qnil) {
78  rb_gc_mark(wrapper->mark);
79  }
80 }
81 
82 static const rb_data_type_t grpc_rb_server_credentials_data_type = {
83  "grpc_server_credentials",
87  {NULL, NULL}},
88  NULL,
89  NULL,
90 #ifdef RUBY_TYPED_FREE_IMMEDIATELY
91  RUBY_TYPED_FREE_IMMEDIATELY
92 #endif
93 };
94 
95 /* Allocates ServerCredential instances.
96  Provides safe initial defaults for the instance fields. */
97 static VALUE grpc_rb_server_credentials_alloc(VALUE cls) {
100  wrapper->wrapped = NULL;
101  wrapper->mark = Qnil;
102  return TypedData_Wrap_Struct(cls, &grpc_rb_server_credentials_data_type,
103  wrapper);
104 }
105 
106 /* The attribute used on the mark object to preserve the pem_root_certs. */
108 
109 /* The attribute used on the mark object to preserve the pem_key_certs */
111 
112 /* The key used to access the pem cert in a key_cert pair hash */
113 static VALUE sym_cert_chain;
114 
115 /* The key used to access the pem private key in a key_cert pair hash */
116 static VALUE sym_private_key;
117 
118 /*
119  call-seq:
120  creds = ServerCredentials.new(nil,
121  [{private_key: <pem_private_key1>,
122  {cert_chain: <pem_cert_chain1>}],
123  force_client_auth)
124  creds = ServerCredentials.new(pem_root_certs,
125  [{private_key: <pem_private_key1>,
126  {cert_chain: <pem_cert_chain1>}],
127  force_client_auth)
128 
129  pem_root_certs: (optional) PEM encoding of the server root certificate
130  pem_private_key: (required) PEM encoding of the server's private keys
131  force_client_auth: indicatees
132 
133  Initializes ServerCredential instances. */
134 static VALUE grpc_rb_server_credentials_init(VALUE self, VALUE pem_root_certs,
135  VALUE pem_key_certs,
136  VALUE force_client_auth) {
138  grpc_server_credentials* creds = NULL;
139  grpc_ssl_pem_key_cert_pair* key_cert_pairs = NULL;
140  VALUE cert = Qnil;
141  VALUE key = Qnil;
142  VALUE key_cert = Qnil;
143  int auth_client = 0;
144  long num_key_certs = 0;
145  int i;
146 
147  if (NIL_P(force_client_auth) ||
148  !(force_client_auth == Qfalse || force_client_auth == Qtrue)) {
149  rb_raise(rb_eTypeError,
150  "bad force_client_auth: got:<%s> want: <True|False|nil>",
151  rb_obj_classname(force_client_auth));
152  return Qnil;
153  }
154  if (NIL_P(pem_key_certs) || TYPE(pem_key_certs) != T_ARRAY) {
155  rb_raise(rb_eTypeError, "bad pem_key_certs: got:<%s> want: <Array>",
156  rb_obj_classname(pem_key_certs));
157  return Qnil;
158  }
159  num_key_certs = RARRAY_LEN(pem_key_certs);
160  if (num_key_certs == 0) {
161  rb_raise(rb_eTypeError, "bad pem_key_certs: it had no elements");
162  return Qnil;
163  }
164  for (i = 0; i < num_key_certs; i++) {
165  key_cert = rb_ary_entry(pem_key_certs, i);
166  if (key_cert == Qnil) {
167  rb_raise(rb_eTypeError,
168  "could not create a server credential: nil key_cert");
169  return Qnil;
170  } else if (TYPE(key_cert) != T_HASH) {
171  rb_raise(rb_eTypeError,
172  "could not create a server credential: want <Hash>, got <%s>",
173  rb_obj_classname(key_cert));
174  return Qnil;
175  } else if (rb_hash_aref(key_cert, sym_private_key) == Qnil) {
176  rb_raise(rb_eTypeError,
177  "could not create a server credential: want nil private key");
178  return Qnil;
179  } else if (rb_hash_aref(key_cert, sym_cert_chain) == Qnil) {
180  rb_raise(rb_eTypeError,
181  "could not create a server credential: want nil cert chain");
182  return Qnil;
183  }
184  }
185 
186  auth_client = TYPE(force_client_auth) == T_TRUE
189  key_cert_pairs = ALLOC_N(grpc_ssl_pem_key_cert_pair, num_key_certs);
190  for (i = 0; i < num_key_certs; i++) {
191  key_cert = rb_ary_entry(pem_key_certs, i);
192  key = rb_hash_aref(key_cert, sym_private_key);
193  cert = rb_hash_aref(key_cert, sym_cert_chain);
194  key_cert_pairs[i].private_key = RSTRING_PTR(key);
195  key_cert_pairs[i].cert_chain = RSTRING_PTR(cert);
196  }
197 
198  TypedData_Get_Struct(self, grpc_rb_server_credentials,
200 
201  if (pem_root_certs == Qnil) {
203  NULL, key_cert_pairs, num_key_certs, auth_client, NULL);
204  } else {
206  key_cert_pairs, num_key_certs,
207  auth_client, NULL);
208  }
209  xfree(key_cert_pairs);
210  if (creds == NULL) {
211  rb_raise(rb_eRuntimeError,
212  "the call to grpc_ssl_server_credentials_create_ex() failed, "
213  "could not create a credentials, see "
214  "https://github.com/grpc/grpc/blob/master/TROUBLESHOOTING.md for "
215  "debugging tips");
216  return Qnil;
217  }
218  wrapper->wrapped = creds;
219 
220  /* Add the input objects as hidden fields to preserve them. */
221  rb_ivar_set(self, id_pem_key_certs, pem_key_certs);
222  rb_ivar_set(self, id_pem_root_certs, pem_root_certs);
223 
224  return self;
225 }
226 
229  rb_define_class_under(grpc_rb_mGrpcCore, "ServerCredentials", rb_cObject);
230 
231  /* Allocates an object managed by the ruby runtime */
232  rb_define_alloc_func(grpc_rb_cServerCredentials,
234 
235  /* Provides a ruby constructor and support for dup/clone. */
236  rb_define_method(grpc_rb_cServerCredentials, "initialize",
238  rb_define_method(grpc_rb_cServerCredentials, "initialize_copy",
240 
241  id_pem_key_certs = rb_intern("__pem_key_certs");
242  id_pem_root_certs = rb_intern("__pem_root_certs");
243  sym_private_key = ID2SYM(rb_intern("private_key"));
244  sym_cert_chain = ID2SYM(rb_intern("cert_chain"));
245 }
246 
247 /* Gets the wrapped grpc_server_credentials from the ruby wrapper */
250  Check_TypedStruct(v, &grpc_rb_server_credentials_data_type);
251  TypedData_Get_Struct(v, grpc_rb_server_credentials,
253  return wrapper->wrapped;
254 }
255 
256 /* Check if v is kind of ServerCredentials */
258  return rb_typeddata_is_kind_of(v, &grpc_rb_server_credentials_data_type);
259 }
log.h
rb_grpc_imports.generated.h
grpc_rb_server_credentials_free_internal
static void grpc_rb_server_credentials_free_internal(void *p)
Definition: rb_server_credentials.c:45
grpc_rb_server_credentials_mark
static void grpc_rb_server_credentials_mark(void *p)
Definition: rb_server_credentials.c:69
GRPC_RB_MEMSIZE_UNAVAILABLE
#define GRPC_RB_MEMSIZE_UNAVAILABLE
Definition: rb_grpc.h:57
grpc_ssl_pem_key_cert_pair::private_key
const char * private_key
Definition: grpc_security.h:176
grpc_ruby_shutdown
void grpc_ruby_shutdown()
Definition: rb_grpc.c:296
pem_root_certs
static char * pem_root_certs
Definition: rb_channel_credentials.c:38
ALLOC_N
#define ALLOC_N(class_name, n)
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:1488
xds_manager.p
p
Definition: xds_manager.py:60
grpc_security.h
grpc_rb_server_credentials
struct grpc_rb_server_credentials grpc_rb_server_credentials
grpc_ssl_pem_key_cert_pair::cert_chain
const char * cert_chain
Definition: grpc_security.h:180
_grpc_channel_wrapper::wrapped
grpc_channel * wrapped
Definition: src/php/ext/grpc/channel.h:35
grpc_rb_server_credentials_init
static VALUE grpc_rb_server_credentials_init(VALUE self, VALUE pem_root_certs, VALUE pem_key_certs, VALUE force_client_auth)
Definition: rb_server_credentials.c:134
grpc_ssl_server_credentials_create_ex
GRPCAPI grpc_server_credentials * grpc_ssl_server_credentials_create_ex(const char *pem_root_certs, grpc_ssl_pem_key_cert_pair *pem_key_cert_pairs, size_t num_key_cert_pairs, grpc_ssl_client_certificate_request_type client_certificate_request, void *reserved)
Definition: ssl_credentials.cc:330
grpc_server_credentials_release
GRPCAPI void grpc_server_credentials_release(grpc_server_credentials *creds)
Definition: credentials.cc:95
setup.v
v
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:42
grpc.h
grpc_rb_is_server_credentials
bool grpc_rb_is_server_credentials(VALUE v)
Definition: rb_server_credentials.c:257
wrapper
grpc_channel_wrapper * wrapper
Definition: src/php/ext/grpc/channel.h:48
rb_server_credentials.h
grpc_rb_server_credentials_data_type
static const rb_data_type_t grpc_rb_server_credentials_data_type
Definition: rb_server_credentials.c:82
grpc_rb_cServerCredentials
static VALUE grpc_rb_cServerCredentials
Definition: rb_server_credentials.c:32
grpc_server_credentials
Definition: src/core/lib/security/credentials/credentials.h:259
grpc_rb_server_credentials_free
static void grpc_rb_server_credentials_free(void *p)
Definition: rb_server_credentials.c:63
rb_grpc.h
grpc_rb_mGrpcCore
VALUE grpc_rb_mGrpcCore
Definition: rb_grpc.c:252
key
const char * key
Definition: hpack_parser_table.cc:164
ALLOC
#define ALLOC(class_name)
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:1486
grpc_rb_cannot_init_copy
VALUE grpc_rb_cannot_init_copy(VALUE copy, VALUE self)
Definition: rb_grpc.c:80
GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE
@ GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE
Definition: grpc_security_constants.h:84
grpc_rb_server_credentials::mark
VALUE mark
Definition: rb_server_credentials.c:39
grpc_rb_server_credentials_alloc
static VALUE grpc_rb_server_credentials_alloc(VALUE cls)
Definition: rb_server_credentials.c:97
TYPE
#define TYPE(u, l)
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/upb.c:8202
id_pem_key_certs
static ID id_pem_key_certs
Definition: rb_server_credentials.c:110
Init_grpc_server_credentials
void Init_grpc_server_credentials()
Definition: rb_server_credentials.c:227
GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY
@ GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY
Definition: grpc_security_constants.h:125
grpc_rb_server_credentials::wrapped
grpc_server_credentials * wrapped
Definition: rb_server_credentials.c:41
grpc_rb_get_wrapped_server_credentials
grpc_server_credentials * grpc_rb_get_wrapped_server_credentials(VALUE v)
Definition: rb_server_credentials.c:248
sym_private_key
static VALUE sym_private_key
Definition: rb_server_credentials.c:116
id_pem_root_certs
static ID id_pem_root_certs
Definition: rb_server_credentials.c:107
grpc_ssl_pem_key_cert_pair
Definition: grpc_security.h:173
grpc_rb_server_credentials
Definition: rb_server_credentials.c:37
sym_cert_chain
static VALUE sym_cert_chain
Definition: rb_server_credentials.c:113
grpc_ruby_init
void grpc_ruby_init()
Definition: rb_grpc.c:286
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:59