3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <ruby/ruby.h>
23 #include "rb_channel_credentials.h"
24 #include "rb_grpc_imports.generated.h"
26 #include <grpc/grpc.h>
27 #include <grpc/grpc_security.h>
28 #include <grpc/support/alloc.h>
29 #include <grpc/support/log.h>
31 #include "rb_call_credentials.h"
34 /* grpc_rb_cChannelCredentials is the ruby class that proxies
35 grpc_channel_credentials. */
36 static VALUE grpc_rb_cChannelCredentials = Qnil;
38 static char* pem_root_certs = NULL;
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
43 typedef struct grpc_rb_channel_credentials {
44 /* Holder of ruby objects involved in constructing the credentials */
47 /* The actual credentials */
48 grpc_channel_credentials* wrapped;
49 } grpc_rb_channel_credentials;
51 static void grpc_rb_channel_credentials_free_internal(void* p) {
52 grpc_rb_channel_credentials* wrapper = NULL;
56 wrapper = (grpc_rb_channel_credentials*)p;
57 grpc_channel_credentials_release(wrapper->wrapped);
58 wrapper->wrapped = NULL;
63 /* Destroys the credentials instances. */
64 static void grpc_rb_channel_credentials_free(void* p) {
65 grpc_rb_channel_credentials_free_internal(p);
69 /* Protects the mark object from GC */
70 static void grpc_rb_channel_credentials_mark(void* p) {
71 grpc_rb_channel_credentials* wrapper = NULL;
75 wrapper = (grpc_rb_channel_credentials*)p;
77 if (wrapper->mark != Qnil) {
78 rb_gc_mark(wrapper->mark);
82 static rb_data_type_t grpc_rb_channel_credentials_data_type = {
83 "grpc_channel_credentials",
84 {grpc_rb_channel_credentials_mark,
85 grpc_rb_channel_credentials_free,
86 GRPC_RB_MEMSIZE_UNAVAILABLE,
90 #ifdef RUBY_TYPED_FREE_IMMEDIATELY
91 RUBY_TYPED_FREE_IMMEDIATELY
95 /* Allocates ChannelCredential instances.
96 Provides safe initial defaults for the instance fields. */
97 static VALUE grpc_rb_channel_credentials_alloc(VALUE cls) {
99 grpc_rb_channel_credentials* wrapper = ALLOC(grpc_rb_channel_credentials);
100 wrapper->wrapped = NULL;
101 wrapper->mark = Qnil;
102 return TypedData_Wrap_Struct(cls, &grpc_rb_channel_credentials_data_type,
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. */
109 VALUE grpc_rb_wrap_channel_credentials(grpc_channel_credentials* c,
112 grpc_rb_channel_credentials* wrapper;
116 rb_wrapper = grpc_rb_channel_credentials_alloc(grpc_rb_cChannelCredentials);
117 TypedData_Get_Struct(rb_wrapper, grpc_rb_channel_credentials,
118 &grpc_rb_channel_credentials_data_type, wrapper);
119 wrapper->wrapped = c;
120 wrapper->mark = mark;
124 /* The attribute used on the mark object to hold the pem_root_certs. */
125 static ID id_pem_root_certs;
127 /* The attribute used on the mark object to hold the pem_private_key. */
128 static ID id_pem_private_key;
130 /* The attribute used on the mark object to hold the pem_private_key. */
131 static ID id_pem_cert_chain;
135 creds1 = Credentials.new()
137 creds2 = Credentials.new(pem_root_certs)
139 creds3 = Credentials.new(pem_root_certs, pem_private_key,
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,
147 VALUE pem_root_certs = Qnil;
148 VALUE pem_private_key = Qnil;
149 VALUE pem_cert_chain = Qnil;
150 grpc_rb_channel_credentials* wrapper = NULL;
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);
156 /* "03" == no mandatory arg, 3 optional */
157 rb_scan_args(argc, argv, "03", &pem_root_certs, &pem_private_key,
160 TypedData_Get_Struct(self, grpc_rb_channel_credentials,
161 &grpc_rb_channel_credentials_data_type, wrapper);
162 if (pem_root_certs != Qnil) {
163 pem_root_certs_cstr = RSTRING_PTR(pem_root_certs);
165 if (pem_private_key == Qnil && pem_cert_chain == Qnil) {
166 creds = grpc_ssl_credentials_create(pem_root_certs_cstr, NULL, NULL, NULL);
168 if (pem_private_key == Qnil) {
171 "could not create a credentials because pem_private_key is NULL");
173 if (pem_cert_chain == Qnil) {
174 rb_raise(rb_eRuntimeError,
175 "could not create a credentials because pem_cert_chain is NULL");
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,
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 "
190 wrapper->wrapped = creds;
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);
200 static VALUE grpc_rb_channel_credentials_compose(int argc, VALUE* argv,
202 grpc_channel_credentials* creds;
203 grpc_call_credentials* other;
204 grpc_channel_credentials* prev = NULL;
210 rb_ary_push(mark, self);
211 creds = grpc_rb_get_wrapped_channel_credentials(self);
212 for (int i = 0; i < argc; i++) {
213 rb_ary_push(mark, argv[i]);
214 other = grpc_rb_get_wrapped_call_credentials(argv[i]);
215 creds = grpc_composite_channel_credentials_create(creds, other, NULL);
217 grpc_channel_credentials_release(prev);
222 rb_raise(rb_eRuntimeError,
223 "Failed to compose channel and call credentials");
226 return grpc_rb_wrap_channel_credentials(creds, mark);
229 static grpc_ssl_roots_override_result get_ssl_roots_override(
230 char** pem_root_certs_ptr) {
231 *pem_root_certs_ptr = pem_root_certs;
232 if (pem_root_certs == NULL) {
233 return GRPC_SSL_ROOTS_OVERRIDE_FAIL;
235 return GRPC_SSL_ROOTS_OVERRIDE_OK;
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);
243 pem_root_certs = gpr_malloc((length + 1) * sizeof(char));
244 memcpy(pem_root_certs, roots_ptr, length + 1);
248 void Init_grpc_channel_credentials() {
249 grpc_rb_cChannelCredentials = rb_define_class_under(
250 grpc_rb_mGrpcCore, "ChannelCredentials", rb_cObject);
252 /* Allocates an object managed by the ruby runtime */
253 rb_define_alloc_func(grpc_rb_cChannelCredentials,
254 grpc_rb_channel_credentials_alloc);
256 /* Provides a ruby constructor and support for dup/clone. */
257 rb_define_method(grpc_rb_cChannelCredentials, "initialize",
258 grpc_rb_channel_credentials_init, -1);
259 rb_define_method(grpc_rb_cChannelCredentials, "initialize_copy",
260 grpc_rb_cannot_init_copy, 1);
261 rb_define_method(grpc_rb_cChannelCredentials, "compose",
262 grpc_rb_channel_credentials_compose, -1);
263 rb_define_module_function(grpc_rb_cChannelCredentials,
264 "set_default_roots_pem",
265 grpc_rb_set_default_roots_pem, 1);
267 grpc_set_ssl_roots_override_callback(get_ssl_roots_override);
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");
274 /* Gets the wrapped grpc_channel_credentials from the ruby wrapper */
275 grpc_channel_credentials* grpc_rb_get_wrapped_channel_credentials(VALUE v) {
276 grpc_rb_channel_credentials* wrapper = NULL;
277 Check_TypedStruct(v, &grpc_rb_channel_credentials_data_type);
278 TypedData_Get_Struct(v, grpc_rb_channel_credentials,
279 &grpc_rb_channel_credentials_data_type, wrapper);
280 return wrapper->wrapped;
283 /* Check if v is kind of ChannelCredentials */
284 bool grpc_rb_is_channel_credentials(VALUE v) {
285 return rb_typeddata_is_kind_of(v, &grpc_rb_channel_credentials_data_type);