murgatroid99 | 749666e | 2015-01-12 18:25:58 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2014, 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 | |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 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 ::node::Buffer; |
| 45 | using v8::Arguments; |
| 46 | using v8::Exception; |
| 47 | using v8::External; |
| 48 | using v8::Function; |
| 49 | using v8::FunctionTemplate; |
| 50 | using v8::Handle; |
| 51 | using v8::HandleScope; |
| 52 | using v8::Integer; |
| 53 | using v8::Local; |
| 54 | using v8::Object; |
| 55 | using v8::ObjectTemplate; |
| 56 | using v8::Persistent; |
| 57 | using v8::Value; |
| 58 | |
| 59 | Persistent<Function> ServerCredentials::constructor; |
| 60 | Persistent<FunctionTemplate> ServerCredentials::fun_tpl; |
| 61 | |
| 62 | ServerCredentials::ServerCredentials(grpc_server_credentials *credentials) |
Craig Tiller | e8e304e | 2015-01-13 14:41:29 -0800 | [diff] [blame] | 63 | : wrapped_credentials(credentials) {} |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 64 | |
| 65 | ServerCredentials::~ServerCredentials() { |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 66 | grpc_server_credentials_release(wrapped_credentials); |
| 67 | } |
| 68 | |
| 69 | void ServerCredentials::Init(Handle<Object> exports) { |
| 70 | NanScope(); |
| 71 | Local<FunctionTemplate> tpl = FunctionTemplate::New(New); |
| 72 | tpl->SetClassName(NanNew("ServerCredentials")); |
| 73 | tpl->InstanceTemplate()->SetInternalFieldCount(1); |
| 74 | NanAssignPersistent(fun_tpl, tpl); |
| 75 | NanAssignPersistent(constructor, tpl->GetFunction()); |
| 76 | constructor->Set(NanNew("createSsl"), |
| 77 | FunctionTemplate::New(CreateSsl)->GetFunction()); |
| 78 | constructor->Set(NanNew("createFake"), |
| 79 | FunctionTemplate::New(CreateFake)->GetFunction()); |
| 80 | exports->Set(NanNew("ServerCredentials"), constructor); |
| 81 | } |
| 82 | |
| 83 | bool ServerCredentials::HasInstance(Handle<Value> val) { |
| 84 | NanScope(); |
| 85 | return NanHasInstance(fun_tpl, val); |
| 86 | } |
| 87 | |
| 88 | Handle<Value> ServerCredentials::WrapStruct( |
| 89 | grpc_server_credentials *credentials) { |
| 90 | NanEscapableScope(); |
| 91 | if (credentials == NULL) { |
| 92 | return NanEscapeScope(NanNull()); |
| 93 | } |
| 94 | const int argc = 1; |
| 95 | Handle<Value> argv[argc] = { |
Craig Tiller | e8e304e | 2015-01-13 14:41:29 -0800 | [diff] [blame] | 96 | External::New(reinterpret_cast<void *>(credentials))}; |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 97 | return NanEscapeScope(constructor->NewInstance(argc, argv)); |
| 98 | } |
| 99 | |
| 100 | grpc_server_credentials *ServerCredentials::GetWrappedServerCredentials() { |
| 101 | return wrapped_credentials; |
| 102 | } |
| 103 | |
| 104 | NAN_METHOD(ServerCredentials::New) { |
| 105 | NanScope(); |
| 106 | |
| 107 | if (args.IsConstructCall()) { |
| 108 | if (!args[0]->IsExternal()) { |
| 109 | return NanThrowTypeError( |
| 110 | "ServerCredentials can only be created with the provide functions"); |
| 111 | } |
| 112 | grpc_server_credentials *creds_value = |
Craig Tiller | e8e304e | 2015-01-13 14:41:29 -0800 | [diff] [blame] | 113 | reinterpret_cast<grpc_server_credentials *>(External::Unwrap(args[0])); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 114 | ServerCredentials *credentials = new ServerCredentials(creds_value); |
| 115 | credentials->Wrap(args.This()); |
| 116 | NanReturnValue(args.This()); |
| 117 | } else { |
| 118 | const int argc = 1; |
Craig Tiller | e8e304e | 2015-01-13 14:41:29 -0800 | [diff] [blame] | 119 | Local<Value> argv[argc] = {args[0]}; |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 120 | NanReturnValue(constructor->NewInstance(argc, argv)); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | NAN_METHOD(ServerCredentials::CreateSsl) { |
Julien Boeuf | 073d7b6 | 2015-01-21 11:13:43 -0800 | [diff] [blame] | 125 | // TODO: have the node API support multiple key/cert pairs. |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 126 | NanScope(); |
| 127 | char *root_certs = NULL; |
Julien Boeuf | 073d7b6 | 2015-01-21 11:13:43 -0800 | [diff] [blame] | 128 | grpc_ssl_pem_key_cert_pair key_cert_pair; |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 129 | if (Buffer::HasInstance(args[0])) { |
| 130 | root_certs = Buffer::Data(args[0]); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 131 | } else if (!(args[0]->IsNull() || args[0]->IsUndefined())) { |
| 132 | return NanThrowTypeError( |
| 133 | "createSSl's first argument must be a Buffer if provided"); |
| 134 | } |
| 135 | if (!Buffer::HasInstance(args[1])) { |
Craig Tiller | e8e304e | 2015-01-13 14:41:29 -0800 | [diff] [blame] | 136 | return NanThrowTypeError("createSsl's second argument must be a Buffer"); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 137 | } |
Julien Boeuf | 073d7b6 | 2015-01-21 11:13:43 -0800 | [diff] [blame] | 138 | key_cert_pair.private_key = Buffer::Data(args[1]); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 139 | if (!Buffer::HasInstance(args[2])) { |
Craig Tiller | e8e304e | 2015-01-13 14:41:29 -0800 | [diff] [blame] | 140 | return NanThrowTypeError("createSsl's third argument must be a Buffer"); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 141 | } |
Julien Boeuf | 073d7b6 | 2015-01-21 11:13:43 -0800 | [diff] [blame] | 142 | key_cert_pair.cert_chain = Buffer::Data(args[2]); |
| 143 | NanReturnValue(WrapStruct( |
| 144 | grpc_ssl_server_credentials_create(root_certs, &key_cert_pair, 1))); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | NAN_METHOD(ServerCredentials::CreateFake) { |
| 148 | NanScope(); |
Craig Tiller | e8e304e | 2015-01-13 14:41:29 -0800 | [diff] [blame] | 149 | NanReturnValue( |
| 150 | WrapStruct(grpc_fake_transport_security_server_credentials_create())); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | } // namespace node |
| 154 | } // namespace grpc |