OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015, Google Inc. |
| 4 * All rights reserved. |
| 5 * |
| 6 * Redistribution and use in source and binary forms, with or without |
| 7 * modification, are permitted provided that the following conditions are |
| 8 * met: |
| 9 * |
| 10 * * Redistributions of source code must retain the above copyright |
| 11 * notice, this list of conditions and the following disclaimer. |
| 12 * * Redistributions in binary form must reproduce the above |
| 13 * copyright notice, this list of conditions and the following disclaimer |
| 14 * in the documentation and/or other materials provided with the |
| 15 * distribution. |
| 16 * * Neither the name of Google Inc. nor the names of its |
| 17 * contributors may be used to endorse or promote products derived from |
| 18 * this software without specific prior written permission. |
| 19 * |
| 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 * |
| 32 */ |
| 33 |
| 34 #include <node.h> |
| 35 |
| 36 #include "grpc/grpc.h" |
| 37 #include "grpc/grpc_security.h" |
| 38 #include "grpc/support/log.h" |
| 39 #include "server_credentials.h" |
| 40 |
| 41 namespace grpc { |
| 42 namespace node { |
| 43 |
| 44 using Nan::Callback; |
| 45 using Nan::EscapableHandleScope; |
| 46 using Nan::HandleScope; |
| 47 using Nan::Maybe; |
| 48 using Nan::MaybeLocal; |
| 49 using Nan::ObjectWrap; |
| 50 using Nan::Persistent; |
| 51 using Nan::Utf8String; |
| 52 |
| 53 using v8::Array; |
| 54 using v8::Exception; |
| 55 using v8::External; |
| 56 using v8::Function; |
| 57 using v8::FunctionTemplate; |
| 58 using v8::Integer; |
| 59 using v8::Local; |
| 60 using v8::Object; |
| 61 using v8::ObjectTemplate; |
| 62 using v8::String; |
| 63 using v8::Value; |
| 64 |
| 65 Nan::Callback *ServerCredentials::constructor; |
| 66 Persistent<FunctionTemplate> ServerCredentials::fun_tpl; |
| 67 |
| 68 ServerCredentials::ServerCredentials(grpc_server_credentials *credentials) |
| 69 : wrapped_credentials(credentials) {} |
| 70 |
| 71 ServerCredentials::~ServerCredentials() { |
| 72 grpc_server_credentials_release(wrapped_credentials); |
| 73 } |
| 74 |
| 75 void ServerCredentials::Init(Local<Object> exports) { |
| 76 Nan::HandleScope scope; |
| 77 Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New); |
| 78 tpl->SetClassName(Nan::New("ServerCredentials").ToLocalChecked()); |
| 79 tpl->InstanceTemplate()->SetInternalFieldCount(1); |
| 80 Local<Function> ctr = tpl->GetFunction(); |
| 81 Nan::Set(ctr, Nan::New("createSsl").ToLocalChecked(), |
| 82 Nan::GetFunction( |
| 83 Nan::New<FunctionTemplate>(CreateSsl)).ToLocalChecked()); |
| 84 Nan::Set(ctr, Nan::New("createInsecure").ToLocalChecked(), |
| 85 Nan::GetFunction( |
| 86 Nan::New<FunctionTemplate>(CreateInsecure)).ToLocalChecked()); |
| 87 fun_tpl.Reset(tpl); |
| 88 constructor = new Nan::Callback(ctr); |
| 89 Nan::Set(exports, Nan::New("ServerCredentials").ToLocalChecked(), ctr); |
| 90 } |
| 91 |
| 92 bool ServerCredentials::HasInstance(Local<Value> val) { |
| 93 Nan::HandleScope scope; |
| 94 return Nan::New(fun_tpl)->HasInstance(val); |
| 95 } |
| 96 |
| 97 Local<Value> ServerCredentials::WrapStruct( |
| 98 grpc_server_credentials *credentials) { |
| 99 Nan::EscapableHandleScope scope; |
| 100 const int argc = 1; |
| 101 Local<Value> argv[argc] = { |
| 102 Nan::New<External>(reinterpret_cast<void *>(credentials))}; |
| 103 MaybeLocal<Object> maybe_instance = Nan::NewInstance( |
| 104 constructor->GetFunction(), argc, argv); |
| 105 if (maybe_instance.IsEmpty()) { |
| 106 return scope.Escape(Nan::Null()); |
| 107 } else { |
| 108 return scope.Escape(maybe_instance.ToLocalChecked()); |
| 109 } |
| 110 } |
| 111 |
| 112 grpc_server_credentials *ServerCredentials::GetWrappedServerCredentials() { |
| 113 return wrapped_credentials; |
| 114 } |
| 115 |
| 116 NAN_METHOD(ServerCredentials::New) { |
| 117 if (info.IsConstructCall()) { |
| 118 if (!info[0]->IsExternal()) { |
| 119 return Nan::ThrowTypeError( |
| 120 "ServerCredentials can only be created with the provided functions"); |
| 121 } |
| 122 Local<External> ext = info[0].As<External>(); |
| 123 grpc_server_credentials *creds_value = |
| 124 reinterpret_cast<grpc_server_credentials *>(ext->Value()); |
| 125 ServerCredentials *credentials = new ServerCredentials(creds_value); |
| 126 credentials->Wrap(info.This()); |
| 127 info.GetReturnValue().Set(info.This()); |
| 128 } else { |
| 129 // This should never be called directly |
| 130 return Nan::ThrowTypeError( |
| 131 "ServerCredentials can only be created with the provided functions"); |
| 132 } |
| 133 } |
| 134 |
| 135 NAN_METHOD(ServerCredentials::CreateSsl) { |
| 136 Nan::HandleScope scope; |
| 137 char *root_certs = NULL; |
| 138 if (::node::Buffer::HasInstance(info[0])) { |
| 139 root_certs = ::node::Buffer::Data(info[0]); |
| 140 } else if (!(info[0]->IsNull() || info[0]->IsUndefined())) { |
| 141 return Nan::ThrowTypeError( |
| 142 "createSSl's first argument must be a Buffer if provided"); |
| 143 } |
| 144 if (!info[1]->IsArray()) { |
| 145 return Nan::ThrowTypeError( |
| 146 "createSsl's second argument must be a list of objects"); |
| 147 } |
| 148 int force_client_auth = 0; |
| 149 if (info[2]->IsBoolean()) { |
| 150 force_client_auth = (int)Nan::To<bool>(info[2]).FromJust(); |
| 151 } else if (!(info[2]->IsUndefined() || info[2]->IsNull())) { |
| 152 return Nan::ThrowTypeError( |
| 153 "createSsl's third argument must be a boolean if provided"); |
| 154 } |
| 155 Local<Array> pair_list = Local<Array>::Cast(info[1]); |
| 156 uint32_t key_cert_pair_count = pair_list->Length(); |
| 157 grpc_ssl_pem_key_cert_pair *key_cert_pairs = new grpc_ssl_pem_key_cert_pair[ |
| 158 key_cert_pair_count]; |
| 159 |
| 160 Local<String> key_key = Nan::New("private_key").ToLocalChecked(); |
| 161 Local<String> cert_key = Nan::New("cert_chain").ToLocalChecked(); |
| 162 |
| 163 for(uint32_t i = 0; i < key_cert_pair_count; i++) { |
| 164 Local<Value> pair_val = Nan::Get(pair_list, i).ToLocalChecked(); |
| 165 if (!pair_val->IsObject()) { |
| 166 delete key_cert_pairs; |
| 167 return Nan::ThrowTypeError("Key/cert pairs must be objects"); |
| 168 } |
| 169 Local<Object> pair_obj = Nan::To<Object>(pair_val).ToLocalChecked(); |
| 170 Local<Value> maybe_key = Nan::Get(pair_obj, key_key).ToLocalChecked(); |
| 171 Local<Value> maybe_cert = Nan::Get(pair_obj, cert_key).ToLocalChecked(); |
| 172 if (!::node::Buffer::HasInstance(maybe_key)) { |
| 173 delete key_cert_pairs; |
| 174 return Nan::ThrowTypeError("private_key must be a Buffer"); |
| 175 } |
| 176 if (!::node::Buffer::HasInstance(maybe_cert)) { |
| 177 delete key_cert_pairs; |
| 178 return Nan::ThrowTypeError("cert_chain must be a Buffer"); |
| 179 } |
| 180 key_cert_pairs[i].private_key = ::node::Buffer::Data(maybe_key); |
| 181 key_cert_pairs[i].cert_chain = ::node::Buffer::Data(maybe_cert); |
| 182 } |
| 183 grpc_server_credentials *creds = grpc_ssl_server_credentials_create( |
| 184 root_certs, key_cert_pairs, key_cert_pair_count, force_client_auth, NULL); |
| 185 delete key_cert_pairs; |
| 186 if (creds == NULL) { |
| 187 info.GetReturnValue().SetNull(); |
| 188 } else { |
| 189 info.GetReturnValue().Set(WrapStruct(creds)); |
| 190 } |
| 191 } |
| 192 |
| 193 NAN_METHOD(ServerCredentials::CreateInsecure) { |
| 194 info.GetReturnValue().Set(WrapStruct(NULL)); |
| 195 } |
| 196 |
| 197 } // namespace node |
| 198 } // namespace grpc |
OLD | NEW |