murgatroid99 | 749666e | 2015-01-12 18:25:58 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
murgatroid99 | 749666e | 2015-01-12 18:25:58 -0800 | [diff] [blame] | 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 | |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 44 | using v8::Exception; |
| 45 | using v8::External; |
| 46 | using v8::Function; |
| 47 | using v8::FunctionTemplate; |
| 48 | using v8::Handle; |
| 49 | using v8::HandleScope; |
| 50 | using v8::Integer; |
| 51 | using v8::Local; |
| 52 | using v8::Object; |
| 53 | using v8::ObjectTemplate; |
| 54 | using v8::Persistent; |
| 55 | using v8::Value; |
| 56 | |
murgatroid99 | f28066b | 2015-03-04 14:42:31 -0800 | [diff] [blame] | 57 | NanCallback *ServerCredentials::constructor; |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 58 | Persistent<FunctionTemplate> ServerCredentials::fun_tpl; |
| 59 | |
| 60 | ServerCredentials::ServerCredentials(grpc_server_credentials *credentials) |
Craig Tiller | e8e304e | 2015-01-13 14:41:29 -0800 | [diff] [blame] | 61 | : wrapped_credentials(credentials) {} |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 62 | |
| 63 | ServerCredentials::~ServerCredentials() { |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 64 | grpc_server_credentials_release(wrapped_credentials); |
| 65 | } |
| 66 | |
| 67 | void ServerCredentials::Init(Handle<Object> exports) { |
| 68 | NanScope(); |
murgatroid99 | f28066b | 2015-03-04 14:42:31 -0800 | [diff] [blame] | 69 | Local<FunctionTemplate> tpl = NanNew<FunctionTemplate>(New); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 70 | tpl->SetClassName(NanNew("ServerCredentials")); |
| 71 | tpl->InstanceTemplate()->SetInternalFieldCount(1); |
| 72 | NanAssignPersistent(fun_tpl, tpl); |
murgatroid99 | f28066b | 2015-03-04 14:42:31 -0800 | [diff] [blame] | 73 | Handle<Function> ctr = tpl->GetFunction(); |
| 74 | ctr->Set(NanNew("createSsl"), |
| 75 | NanNew<FunctionTemplate>(CreateSsl)->GetFunction()); |
| 76 | ctr->Set(NanNew("createFake"), |
| 77 | NanNew<FunctionTemplate>(CreateFake)->GetFunction()); |
| 78 | constructor = new NanCallback(ctr); |
| 79 | exports->Set(NanNew("ServerCredentials"), ctr); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | bool ServerCredentials::HasInstance(Handle<Value> val) { |
| 83 | NanScope(); |
| 84 | return NanHasInstance(fun_tpl, val); |
| 85 | } |
| 86 | |
| 87 | Handle<Value> ServerCredentials::WrapStruct( |
| 88 | grpc_server_credentials *credentials) { |
| 89 | NanEscapableScope(); |
| 90 | if (credentials == NULL) { |
| 91 | return NanEscapeScope(NanNull()); |
| 92 | } |
| 93 | const int argc = 1; |
| 94 | Handle<Value> argv[argc] = { |
murgatroid99 | f28066b | 2015-03-04 14:42:31 -0800 | [diff] [blame] | 95 | NanNew<External>(reinterpret_cast<void *>(credentials))}; |
| 96 | return NanEscapeScope(constructor->GetFunction()->NewInstance(argc, argv)); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | grpc_server_credentials *ServerCredentials::GetWrappedServerCredentials() { |
| 100 | return wrapped_credentials; |
| 101 | } |
| 102 | |
| 103 | NAN_METHOD(ServerCredentials::New) { |
| 104 | NanScope(); |
| 105 | |
| 106 | if (args.IsConstructCall()) { |
| 107 | if (!args[0]->IsExternal()) { |
| 108 | return NanThrowTypeError( |
| 109 | "ServerCredentials can only be created with the provide functions"); |
| 110 | } |
murgatroid99 | f28066b | 2015-03-04 14:42:31 -0800 | [diff] [blame] | 111 | Handle<External> ext = args[0].As<External>(); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 112 | grpc_server_credentials *creds_value = |
murgatroid99 | f28066b | 2015-03-04 14:42:31 -0800 | [diff] [blame] | 113 | reinterpret_cast<grpc_server_credentials *>(ext->Value()); |
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 | f28066b | 2015-03-04 14:42:31 -0800 | [diff] [blame] | 120 | NanReturnValue(constructor->GetFunction()->NewInstance(argc, argv)); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 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 | f28066b | 2015-03-04 14:42:31 -0800 | [diff] [blame] | 129 | if (::node::Buffer::HasInstance(args[0])) { |
| 130 | root_certs = ::node::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 | } |
murgatroid99 | f28066b | 2015-03-04 14:42:31 -0800 | [diff] [blame] | 135 | if (!::node::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 | } |
murgatroid99 | f28066b | 2015-03-04 14:42:31 -0800 | [diff] [blame] | 138 | key_cert_pair.private_key = ::node::Buffer::Data(args[1]); |
| 139 | if (!::node::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 | } |
murgatroid99 | f28066b | 2015-03-04 14:42:31 -0800 | [diff] [blame] | 142 | key_cert_pair.cert_chain = ::node::Buffer::Data(args[2]); |
Julien Boeuf | 5029b30 | 2015-07-21 23:02:16 -0700 | [diff] [blame^] | 143 | // TODO Add a force_client_auth parameter and pass it as the last parameter |
| 144 | // here. |
Julien Boeuf | 073d7b6 | 2015-01-21 11:13:43 -0800 | [diff] [blame] | 145 | NanReturnValue(WrapStruct( |
Julien Boeuf | 5029b30 | 2015-07-21 23:02:16 -0700 | [diff] [blame^] | 146 | grpc_ssl_server_credentials_create(root_certs, &key_cert_pair, 1, 0))); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | NAN_METHOD(ServerCredentials::CreateFake) { |
| 150 | NanScope(); |
Craig Tiller | e8e304e | 2015-01-13 14:41:29 -0800 | [diff] [blame] | 151 | NanReturnValue( |
| 152 | WrapStruct(grpc_fake_transport_security_server_credentials_create())); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | } // namespace node |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 156 | } // namespace grpc |