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 <string.h>
22
23 #include "rb_channel_credentials.h"
24 #include "rb_grpc_imports.generated.h"
25
26 #include <grpc/grpc.h>
27 #include <grpc/grpc_security.h>
28 #include <grpc/support/alloc.h>
29 #include <grpc/support/log.h>
30
31 #include "rb_call_credentials.h"
32 #include "rb_grpc.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. */
43 typedef struct grpc_rb_channel_credentials {
44 /* Holder of ruby objects involved in constructing the credentials */
45 VALUE mark;
46
47 /* The actual credentials */
48 grpc_channel_credentials* wrapped;
49 } grpc_rb_channel_credentials;
50
51 /* Destroys the credentials instances. */
grpc_rb_channel_credentials_free(void * p)52 static void grpc_rb_channel_credentials_free(void* p) {
53 grpc_rb_channel_credentials* wrapper = NULL;
54 if (p == NULL) {
55 return;
56 };
57 wrapper = (grpc_rb_channel_credentials*)p;
58 grpc_channel_credentials_release(wrapper->wrapped);
59 wrapper->wrapped = NULL;
60
61 xfree(p);
62 }
63
64 /* Protects the mark object from GC */
grpc_rb_channel_credentials_mark(void * p)65 static void grpc_rb_channel_credentials_mark(void* p) {
66 grpc_rb_channel_credentials* wrapper = NULL;
67 if (p == NULL) {
68 return;
69 }
70 wrapper = (grpc_rb_channel_credentials*)p;
71
72 if (wrapper->mark != Qnil) {
73 rb_gc_mark(wrapper->mark);
74 }
75 }
76
77 static rb_data_type_t grpc_rb_channel_credentials_data_type = {
78 "grpc_channel_credentials",
79 {grpc_rb_channel_credentials_mark,
80 grpc_rb_channel_credentials_free,
81 GRPC_RB_MEMSIZE_UNAVAILABLE,
82 {NULL, NULL}},
83 NULL,
84 NULL,
85 #ifdef RUBY_TYPED_FREE_IMMEDIATELY
86 RUBY_TYPED_FREE_IMMEDIATELY
87 #endif
88 };
89
90 /* Allocates ChannelCredential instances.
91 Provides safe initial defaults for the instance fields. */
grpc_rb_channel_credentials_alloc(VALUE cls)92 static VALUE grpc_rb_channel_credentials_alloc(VALUE cls) {
93 grpc_rb_channel_credentials* wrapper = ALLOC(grpc_rb_channel_credentials);
94 wrapper->wrapped = NULL;
95 wrapper->mark = Qnil;
96 return TypedData_Wrap_Struct(cls, &grpc_rb_channel_credentials_data_type,
97 wrapper);
98 }
99
100 /* Creates a wrapping object for a given channel credentials. This should only
101 * be called with grpc_channel_credentials objects that are not already
102 * associated with any Ruby object. */
grpc_rb_wrap_channel_credentials(grpc_channel_credentials * c,VALUE mark)103 VALUE grpc_rb_wrap_channel_credentials(grpc_channel_credentials* c,
104 VALUE mark) {
105 VALUE rb_wrapper;
106 grpc_rb_channel_credentials* wrapper;
107 if (c == NULL) {
108 return Qnil;
109 }
110 rb_wrapper = grpc_rb_channel_credentials_alloc(grpc_rb_cChannelCredentials);
111 TypedData_Get_Struct(rb_wrapper, grpc_rb_channel_credentials,
112 &grpc_rb_channel_credentials_data_type, wrapper);
113 wrapper->wrapped = c;
114 wrapper->mark = mark;
115 return rb_wrapper;
116 }
117
118 /* The attribute used on the mark object to hold the pem_root_certs. */
119 static ID id_pem_root_certs;
120
121 /* The attribute used on the mark object to hold the pem_private_key. */
122 static ID id_pem_private_key;
123
124 /* The attribute used on the mark object to hold the pem_private_key. */
125 static ID id_pem_cert_chain;
126
127 /*
128 call-seq:
129 creds1 = Credentials.new()
130 ...
131 creds2 = Credentials.new(pem_root_certs)
132 ...
133 creds3 = Credentials.new(pem_root_certs, pem_private_key,
134 pem_cert_chain)
135 pem_root_certs: (optional) PEM encoding of the server root certificate
136 pem_private_key: (optional) PEM encoding of the client's private key
137 pem_cert_chain: (optional) PEM encoding of the client's cert chain
138 Initializes Credential instances. */
grpc_rb_channel_credentials_init(int argc,VALUE * argv,VALUE self)139 static VALUE grpc_rb_channel_credentials_init(int argc, VALUE* argv,
140 VALUE self) {
141 VALUE pem_root_certs = Qnil;
142 VALUE pem_private_key = Qnil;
143 VALUE pem_cert_chain = Qnil;
144 grpc_rb_channel_credentials* wrapper = NULL;
145 grpc_channel_credentials* creds = NULL;
146 grpc_ssl_pem_key_cert_pair key_cert_pair;
147 const char* pem_root_certs_cstr = NULL;
148 MEMZERO(&key_cert_pair, grpc_ssl_pem_key_cert_pair, 1);
149
150 grpc_ruby_once_init();
151
152 /* "03" == no mandatory arg, 3 optional */
153 rb_scan_args(argc, argv, "03", &pem_root_certs, &pem_private_key,
154 &pem_cert_chain);
155
156 TypedData_Get_Struct(self, grpc_rb_channel_credentials,
157 &grpc_rb_channel_credentials_data_type, wrapper);
158 if (pem_root_certs != Qnil) {
159 pem_root_certs_cstr = RSTRING_PTR(pem_root_certs);
160 }
161 if (pem_private_key == Qnil && pem_cert_chain == Qnil) {
162 creds = grpc_ssl_credentials_create(pem_root_certs_cstr, NULL, NULL, NULL);
163 } else {
164 key_cert_pair.private_key = RSTRING_PTR(pem_private_key);
165 key_cert_pair.cert_chain = RSTRING_PTR(pem_cert_chain);
166 creds = grpc_ssl_credentials_create(pem_root_certs_cstr, &key_cert_pair,
167 NULL, NULL);
168 }
169 if (creds == NULL) {
170 rb_raise(rb_eRuntimeError, "could not create a credentials, not sure why");
171 return Qnil;
172 }
173 wrapper->wrapped = creds;
174
175 /* Add the input objects as hidden fields to preserve them. */
176 rb_ivar_set(self, id_pem_cert_chain, pem_cert_chain);
177 rb_ivar_set(self, id_pem_private_key, pem_private_key);
178 rb_ivar_set(self, id_pem_root_certs, pem_root_certs);
179
180 return self;
181 }
182
grpc_rb_channel_credentials_compose(int argc,VALUE * argv,VALUE self)183 static VALUE grpc_rb_channel_credentials_compose(int argc, VALUE* argv,
184 VALUE self) {
185 grpc_channel_credentials* creds;
186 grpc_call_credentials* other;
187 grpc_channel_credentials* prev = NULL;
188 VALUE mark;
189 if (argc == 0) {
190 return self;
191 }
192 mark = rb_ary_new();
193 rb_ary_push(mark, self);
194 creds = grpc_rb_get_wrapped_channel_credentials(self);
195 for (int i = 0; i < argc; i++) {
196 rb_ary_push(mark, argv[i]);
197 other = grpc_rb_get_wrapped_call_credentials(argv[i]);
198 creds = grpc_composite_channel_credentials_create(creds, other, NULL);
199 if (prev != NULL) {
200 grpc_channel_credentials_release(prev);
201 }
202 prev = creds;
203
204 if (creds == NULL) {
205 rb_raise(rb_eRuntimeError,
206 "Failed to compose channel and call credentials");
207 }
208 }
209 return grpc_rb_wrap_channel_credentials(creds, mark);
210 }
211
get_ssl_roots_override(char ** pem_root_certs_ptr)212 static grpc_ssl_roots_override_result get_ssl_roots_override(
213 char** pem_root_certs_ptr) {
214 *pem_root_certs_ptr = pem_root_certs;
215 if (pem_root_certs == NULL) {
216 return GRPC_SSL_ROOTS_OVERRIDE_FAIL;
217 } else {
218 return GRPC_SSL_ROOTS_OVERRIDE_OK;
219 }
220 }
221
grpc_rb_set_default_roots_pem(VALUE self,VALUE roots)222 static VALUE grpc_rb_set_default_roots_pem(VALUE self, VALUE roots) {
223 char* roots_ptr = StringValueCStr(roots);
224 size_t length = strlen(roots_ptr);
225 (void)self;
226 pem_root_certs = gpr_malloc((length + 1) * sizeof(char));
227 memcpy(pem_root_certs, roots_ptr, length + 1);
228 return Qnil;
229 }
230
Init_grpc_channel_credentials()231 void Init_grpc_channel_credentials() {
232 grpc_rb_cChannelCredentials = rb_define_class_under(
233 grpc_rb_mGrpcCore, "ChannelCredentials", rb_cObject);
234
235 /* Allocates an object managed by the ruby runtime */
236 rb_define_alloc_func(grpc_rb_cChannelCredentials,
237 grpc_rb_channel_credentials_alloc);
238
239 /* Provides a ruby constructor and support for dup/clone. */
240 rb_define_method(grpc_rb_cChannelCredentials, "initialize",
241 grpc_rb_channel_credentials_init, -1);
242 rb_define_method(grpc_rb_cChannelCredentials, "initialize_copy",
243 grpc_rb_cannot_init_copy, 1);
244 rb_define_method(grpc_rb_cChannelCredentials, "compose",
245 grpc_rb_channel_credentials_compose, -1);
246 rb_define_module_function(grpc_rb_cChannelCredentials,
247 "set_default_roots_pem",
248 grpc_rb_set_default_roots_pem, 1);
249
250 grpc_set_ssl_roots_override_callback(get_ssl_roots_override);
251
252 id_pem_cert_chain = rb_intern("__pem_cert_chain");
253 id_pem_private_key = rb_intern("__pem_private_key");
254 id_pem_root_certs = rb_intern("__pem_root_certs");
255 }
256
257 /* Gets the wrapped grpc_channel_credentials from the ruby wrapper */
grpc_rb_get_wrapped_channel_credentials(VALUE v)258 grpc_channel_credentials* grpc_rb_get_wrapped_channel_credentials(VALUE v) {
259 grpc_rb_channel_credentials* wrapper = NULL;
260 TypedData_Get_Struct(v, grpc_rb_channel_credentials,
261 &grpc_rb_channel_credentials_data_type, wrapper);
262 return wrapper->wrapped;
263 }
264