rb_channel_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_channel_credentials.h"
22 
23 #include <string.h>
24 
25 #include "rb_call_credentials.h"
26 #include "rb_grpc.h"
28 
29 #include <grpc/grpc.h>
30 #include <grpc/grpc_security.h>
31 #include <grpc/support/alloc.h>
32 #include <grpc/support/log.h>
33 
34 /* grpc_rb_cChannelCredentials is the ruby class that proxies
35  grpc_channel_credentials. */
36 static VALUE grpc_rb_cChannelCredentials = Qnil;
37 
38 static char* pem_root_certs = NULL;
39 
40 /* grpc_rb_channel_credentials wraps a grpc_channel_credentials. It provides a
41  * mark object that is used to hold references to any objects used to create
42  * the credentials. */
44  /* Holder of ruby objects involved in constructing the credentials */
45  VALUE mark;
46 
47  /* The actual credentials */
50 
53  if (p == NULL) {
54  return;
55  };
58  wrapper->wrapped = NULL;
59 
60  xfree(p);
61 }
62 
63 /* Destroys the credentials instances. */
64 static void grpc_rb_channel_credentials_free(void* p) {
67 }
68 
69 /* Protects the mark object from GC */
70 static void grpc_rb_channel_credentials_mark(void* p) {
72  if (p == NULL) {
73  return;
74  }
76 
77  if (wrapper->mark != Qnil) {
78  rb_gc_mark(wrapper->mark);
79  }
80 }
81 
82 static rb_data_type_t grpc_rb_channel_credentials_data_type = {
83  "grpc_channel_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 ChannelCredential instances.
96  Provides safe initial defaults for the instance fields. */
97 static VALUE grpc_rb_channel_credentials_alloc(VALUE cls) {
100  wrapper->wrapped = NULL;
101  wrapper->mark = Qnil;
102  return TypedData_Wrap_Struct(cls, &grpc_rb_channel_credentials_data_type,
103  wrapper);
104 }
105 
106 /* Creates a wrapping object for a given channel credentials. This should only
107  * be called with grpc_channel_credentials objects that are not already
108  * associated with any Ruby object. */
110  VALUE mark) {
111  VALUE rb_wrapper;
113  if (c == NULL) {
114  return Qnil;
115  }
117  TypedData_Get_Struct(rb_wrapper, grpc_rb_channel_credentials,
119  wrapper->wrapped = c;
120  wrapper->mark = mark;
121  return rb_wrapper;
122 }
123 
124 /* The attribute used on the mark object to hold the pem_root_certs. */
126 
127 /* The attribute used on the mark object to hold the pem_private_key. */
129 
130 /* The attribute used on the mark object to hold the pem_private_key. */
132 
133 /*
134  call-seq:
135  creds1 = Credentials.new()
136  ...
137  creds2 = Credentials.new(pem_root_certs)
138  ...
139  creds3 = Credentials.new(pem_root_certs, pem_private_key,
140  pem_cert_chain)
141  pem_root_certs: (optional) PEM encoding of the server root certificate
142  pem_private_key: (optional) PEM encoding of the client's private key
143  pem_cert_chain: (optional) PEM encoding of the client's cert chain
144  Initializes Credential instances. */
145 static VALUE grpc_rb_channel_credentials_init(int argc, VALUE* argv,
146  VALUE self) {
147  VALUE pem_root_certs = Qnil;
148  VALUE pem_private_key = Qnil;
149  VALUE pem_cert_chain = Qnil;
151  grpc_channel_credentials* creds = NULL;
152  grpc_ssl_pem_key_cert_pair key_cert_pair;
153  const char* pem_root_certs_cstr = NULL;
154  MEMZERO(&key_cert_pair, grpc_ssl_pem_key_cert_pair, 1);
155 
156  /* "03" == no mandatory arg, 3 optional */
157  rb_scan_args(argc, argv, "03", &pem_root_certs, &pem_private_key,
158  &pem_cert_chain);
159 
160  TypedData_Get_Struct(self, grpc_rb_channel_credentials,
162  if (pem_root_certs != Qnil) {
163  pem_root_certs_cstr = RSTRING_PTR(pem_root_certs);
164  }
165  if (pem_private_key == Qnil && pem_cert_chain == Qnil) {
166  creds = grpc_ssl_credentials_create(pem_root_certs_cstr, NULL, NULL, NULL);
167  } else {
168  if (pem_private_key == Qnil) {
169  rb_raise(
170  rb_eRuntimeError,
171  "could not create a credentials because pem_private_key is NULL");
172  }
173  if (pem_cert_chain == Qnil) {
174  rb_raise(rb_eRuntimeError,
175  "could not create a credentials because pem_cert_chain is NULL");
176  }
177  key_cert_pair.private_key = RSTRING_PTR(pem_private_key);
178  key_cert_pair.cert_chain = RSTRING_PTR(pem_cert_chain);
179  creds = grpc_ssl_credentials_create(pem_root_certs_cstr, &key_cert_pair,
180  NULL, NULL);
181  }
182  if (creds == NULL) {
183  rb_raise(rb_eRuntimeError,
184  "the call to grpc_ssl_credentials_create() failed, could not "
185  "create a credentials, see "
186  "https://github.com/grpc/grpc/blob/master/TROUBLESHOOTING.md for "
187  "debugging tips");
188  return Qnil;
189  }
190  wrapper->wrapped = creds;
191 
192  /* Add the input objects as hidden fields to preserve them. */
193  rb_ivar_set(self, id_pem_cert_chain, pem_cert_chain);
194  rb_ivar_set(self, id_pem_private_key, pem_private_key);
195  rb_ivar_set(self, id_pem_root_certs, pem_root_certs);
196 
197  return self;
198 }
199 
200 static VALUE grpc_rb_channel_credentials_compose(int argc, VALUE* argv,
201  VALUE self) {
203  grpc_call_credentials* other;
204  grpc_channel_credentials* prev = NULL;
205  VALUE mark;
206  if (argc == 0) {
207  return self;
208  }
209  mark = rb_ary_new();
210  rb_ary_push(mark, self);
212  for (int i = 0; i < argc; i++) {
213  rb_ary_push(mark, argv[i]);
215  creds = grpc_composite_channel_credentials_create(creds, other, NULL);
216  if (prev != NULL) {
218  }
219  prev = creds;
220 
221  if (creds == NULL) {
222  rb_raise(rb_eRuntimeError,
223  "Failed to compose channel and call credentials");
224  }
225  }
226  return grpc_rb_wrap_channel_credentials(creds, mark);
227 }
228 
230  char** pem_root_certs_ptr) {
231  *pem_root_certs_ptr = pem_root_certs;
232  if (pem_root_certs == NULL) {
234  } else {
236  }
237 }
238 
239 static VALUE grpc_rb_set_default_roots_pem(VALUE self, VALUE roots) {
240  char* roots_ptr = StringValueCStr(roots);
241  size_t length = strlen(roots_ptr);
242  (void)self;
243  pem_root_certs = gpr_malloc((length + 1) * sizeof(char));
244  memcpy(pem_root_certs, roots_ptr, length + 1);
245  return Qnil;
246 }
247 
249  grpc_rb_cChannelCredentials = rb_define_class_under(
250  grpc_rb_mGrpcCore, "ChannelCredentials", rb_cObject);
251 
252  /* Allocates an object managed by the ruby runtime */
253  rb_define_alloc_func(grpc_rb_cChannelCredentials,
255 
256  /* Provides a ruby constructor and support for dup/clone. */
257  rb_define_method(grpc_rb_cChannelCredentials, "initialize",
259  rb_define_method(grpc_rb_cChannelCredentials, "initialize_copy",
261  rb_define_method(grpc_rb_cChannelCredentials, "compose",
263  rb_define_module_function(grpc_rb_cChannelCredentials,
264  "set_default_roots_pem",
266 
268 
269  id_pem_cert_chain = rb_intern("__pem_cert_chain");
270  id_pem_private_key = rb_intern("__pem_private_key");
271  id_pem_root_certs = rb_intern("__pem_root_certs");
272 }
273 
274 /* Gets the wrapped grpc_channel_credentials from the ruby wrapper */
277  Check_TypedStruct(v, &grpc_rb_channel_credentials_data_type);
278  TypedData_Get_Struct(v, grpc_rb_channel_credentials,
280  return wrapper->wrapped;
281 }
282 
283 /* Check if v is kind of ChannelCredentials */
285  return rb_typeddata_is_kind_of(v, &grpc_rb_channel_credentials_data_type);
286 }
grpc_rb_channel_credentials
Definition: rb_channel_credentials.c:43
log.h
rb_grpc_imports.generated.h
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
grpc_rb_channel_credentials_free
static void grpc_rb_channel_credentials_free(void *p)
Definition: rb_channel_credentials.c:64
string.h
gpr_malloc
GPRAPI void * gpr_malloc(size_t size)
Definition: alloc.cc:29
grpc_call_credentials
Definition: src/core/lib/security/credentials/credentials.h:189
xds_manager.p
p
Definition: xds_manager.py:60
grpc_security.h
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_ssl_credentials_create
GRPCAPI grpc_channel_credentials * grpc_ssl_credentials_create(const char *pem_root_certs, grpc_ssl_pem_key_cert_pair *pem_key_cert_pair, const verify_peer_options *verify_options, void *reserved)
Definition: ssl_credentials.cc:132
grpc_rb_wrap_channel_credentials
VALUE grpc_rb_wrap_channel_credentials(grpc_channel_credentials *c, VALUE mark)
Definition: rb_channel_credentials.c:109
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
grpc_rb_get_wrapped_channel_credentials
grpc_channel_credentials * grpc_rb_get_wrapped_channel_credentials(VALUE v)
Definition: rb_channel_credentials.c:275
c
void c(T a)
Definition: miscompile_with_no_unique_address_test.cc:40
grpc_rb_cChannelCredentials
static VALUE grpc_rb_cChannelCredentials
Definition: rb_channel_credentials.c:36
GRPC_SSL_ROOTS_OVERRIDE_FAIL
@ GRPC_SSL_ROOTS_OVERRIDE_FAIL
Definition: grpc_security_constants.h:69
grpc_rb_get_wrapped_call_credentials
grpc_call_credentials * grpc_rb_get_wrapped_call_credentials(VALUE v)
Definition: rb_call_credentials.c:336
grpc_rb_channel_credentials::wrapped
grpc_channel_credentials * wrapped
Definition: rb_channel_credentials.c:48
setup.v
v
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:42
grpc_rb_is_channel_credentials
bool grpc_rb_is_channel_credentials(VALUE v)
Definition: rb_channel_credentials.c:284
grpc.h
grpc_ssl_roots_override_result
grpc_ssl_roots_override_result
Definition: grpc_security_constants.h:66
GRPC_SSL_ROOTS_OVERRIDE_OK
@ GRPC_SSL_ROOTS_OVERRIDE_OK
Definition: grpc_security_constants.h:67
grpc_rb_channel_credentials_data_type
static rb_data_type_t grpc_rb_channel_credentials_data_type
Definition: rb_channel_credentials.c:82
grpc_composite_channel_credentials_create
GRPCAPI grpc_channel_credentials * grpc_composite_channel_credentials_create(grpc_channel_credentials *channel_creds, grpc_call_credentials *call_creds, void *reserved)
Definition: composite_credentials.cc:164
wrapper
grpc_channel_wrapper * wrapper
Definition: src/php/ext/grpc/channel.h:48
Init_grpc_channel_credentials
void Init_grpc_channel_credentials()
Definition: rb_channel_credentials.c:248
grpc_rb_set_default_roots_pem
static VALUE grpc_rb_set_default_roots_pem(VALUE self, VALUE roots)
Definition: rb_channel_credentials.c:239
grpc_rb_channel_credentials_alloc
static VALUE grpc_rb_channel_credentials_alloc(VALUE cls)
Definition: rb_channel_credentials.c:97
id_pem_root_certs
static ID id_pem_root_certs
Definition: rb_channel_credentials.c:125
rb_call_credentials.h
grpc_rb_channel_credentials::mark
VALUE mark
Definition: rb_channel_credentials.c:45
grpc_channel_credentials_release
GRPCAPI void grpc_channel_credentials_release(grpc_channel_credentials *creds)
Definition: credentials.cc:36
rb_grpc.h
grpc_rb_mGrpcCore
VALUE grpc_rb_mGrpcCore
Definition: rb_grpc.c:252
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
alloc.h
grpc_rb_channel_credentials
struct grpc_rb_channel_credentials grpc_rb_channel_credentials
grpc_set_ssl_roots_override_callback
GRPCAPI void grpc_set_ssl_roots_override_callback(grpc_ssl_roots_override_callback cb)
Definition: ssl_utils.cc:71
rb_channel_credentials.h
get_ssl_roots_override
static grpc_ssl_roots_override_result get_ssl_roots_override(char **pem_root_certs_ptr)
Definition: rb_channel_credentials.c:229
grpc_rb_channel_credentials_mark
static void grpc_rb_channel_credentials_mark(void *p)
Definition: rb_channel_credentials.c:70
grpc_rb_channel_credentials_compose
static VALUE grpc_rb_channel_credentials_compose(int argc, VALUE *argv, VALUE self)
Definition: rb_channel_credentials.c:200
grpc_rb_channel_credentials_init
static VALUE grpc_rb_channel_credentials_init(int argc, VALUE *argv, VALUE self)
Definition: rb_channel_credentials.c:145
id_pem_cert_chain
static ID id_pem_cert_chain
Definition: rb_channel_credentials.c:131
length
std::size_t length
Definition: abseil-cpp/absl/time/internal/test_util.cc:57
id_pem_private_key
static ID id_pem_private_key
Definition: rb_channel_credentials.c:128
grpc_ssl_pem_key_cert_pair
Definition: grpc_security.h:173
grpc_channel_credentials
Definition: src/core/lib/security/credentials/credentials.h:96
grpc_rb_channel_credentials_free_internal
static void grpc_rb_channel_credentials_free_internal(void *p)
Definition: rb_channel_credentials.c:51
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