d65d049559ee5760b8f69202c69945c9b062f28a
[platform/upstream/grpc.git] / src / ruby / ext / grpc / rb_channel_credentials.c
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 static void grpc_rb_channel_credentials_free_internal(void* p) {
52   grpc_rb_channel_credentials* wrapper = NULL;
53   if (p == NULL) {
54     return;
55   };
56   wrapper = (grpc_rb_channel_credentials*)p;
57   grpc_channel_credentials_release(wrapper->wrapped);
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) {
65   grpc_rb_channel_credentials_free_internal(p);
66   grpc_ruby_shutdown();
67 }
68
69 /* Protects the mark object from GC */
70 static void grpc_rb_channel_credentials_mark(void* p) {
71   grpc_rb_channel_credentials* wrapper = NULL;
72   if (p == NULL) {
73     return;
74   }
75   wrapper = (grpc_rb_channel_credentials*)p;
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",
84     {grpc_rb_channel_credentials_mark,
85      grpc_rb_channel_credentials_free,
86      GRPC_RB_MEMSIZE_UNAVAILABLE,
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) {
98   grpc_ruby_init();
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,
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. */
109 VALUE grpc_rb_wrap_channel_credentials(grpc_channel_credentials* c,
110                                        VALUE mark) {
111   VALUE rb_wrapper;
112   grpc_rb_channel_credentials* wrapper;
113   if (c == NULL) {
114     return Qnil;
115   }
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;
121   return rb_wrapper;
122 }
123
124 /* The attribute used on the mark object to hold the pem_root_certs. */
125 static ID id_pem_root_certs;
126
127 /* The attribute used on the mark object to hold the pem_private_key. */
128 static ID id_pem_private_key;
129
130 /* The attribute used on the mark object to hold the pem_private_key. */
131 static ID id_pem_cert_chain;
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;
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);
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,
161                        &grpc_rb_channel_credentials_data_type, wrapper);
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) {
202   grpc_channel_credentials* creds;
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);
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);
216     if (prev != NULL) {
217       grpc_channel_credentials_release(prev);
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
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;
234   } else {
235     return GRPC_SSL_ROOTS_OVERRIDE_OK;
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
248 void Init_grpc_channel_credentials() {
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,
254                        grpc_rb_channel_credentials_alloc);
255
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);
266
267   grpc_set_ssl_roots_override_callback(get_ssl_roots_override);
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 */
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;
281 }
282
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);
286 }