blob: 709105c72fce3f7158365c0007c9b51c975fb1b8 [file] [log] [blame]
murgatroid99749666e2015-01-12 18:25:58 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
murgatroid99749666e2015-01-12 18:25:58 -08004 * 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
murgatroid99e5061512015-01-12 18:14:35 -080034#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
41namespace grpc {
42namespace node {
43
murgatroid99e5061512015-01-12 18:14:35 -080044using v8::Exception;
45using v8::External;
46using v8::Function;
47using v8::FunctionTemplate;
48using v8::Handle;
49using v8::HandleScope;
50using v8::Integer;
51using v8::Local;
52using v8::Object;
53using v8::ObjectTemplate;
54using v8::Persistent;
55using v8::Value;
56
murgatroid99f28066b2015-03-04 14:42:31 -080057NanCallback *ServerCredentials::constructor;
murgatroid99e5061512015-01-12 18:14:35 -080058Persistent<FunctionTemplate> ServerCredentials::fun_tpl;
59
60ServerCredentials::ServerCredentials(grpc_server_credentials *credentials)
Craig Tillere8e304e2015-01-13 14:41:29 -080061 : wrapped_credentials(credentials) {}
murgatroid99e5061512015-01-12 18:14:35 -080062
63ServerCredentials::~ServerCredentials() {
murgatroid99e5061512015-01-12 18:14:35 -080064 grpc_server_credentials_release(wrapped_credentials);
65}
66
67void ServerCredentials::Init(Handle<Object> exports) {
68 NanScope();
murgatroid99f28066b2015-03-04 14:42:31 -080069 Local<FunctionTemplate> tpl = NanNew<FunctionTemplate>(New);
murgatroid99e5061512015-01-12 18:14:35 -080070 tpl->SetClassName(NanNew("ServerCredentials"));
71 tpl->InstanceTemplate()->SetInternalFieldCount(1);
72 NanAssignPersistent(fun_tpl, tpl);
murgatroid99f28066b2015-03-04 14:42:31 -080073 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);
murgatroid99e5061512015-01-12 18:14:35 -080080}
81
82bool ServerCredentials::HasInstance(Handle<Value> val) {
83 NanScope();
84 return NanHasInstance(fun_tpl, val);
85}
86
87Handle<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] = {
murgatroid99f28066b2015-03-04 14:42:31 -080095 NanNew<External>(reinterpret_cast<void *>(credentials))};
96 return NanEscapeScope(constructor->GetFunction()->NewInstance(argc, argv));
murgatroid99e5061512015-01-12 18:14:35 -080097}
98
99grpc_server_credentials *ServerCredentials::GetWrappedServerCredentials() {
100 return wrapped_credentials;
101}
102
103NAN_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 }
murgatroid99f28066b2015-03-04 14:42:31 -0800111 Handle<External> ext = args[0].As<External>();
murgatroid99e5061512015-01-12 18:14:35 -0800112 grpc_server_credentials *creds_value =
murgatroid99f28066b2015-03-04 14:42:31 -0800113 reinterpret_cast<grpc_server_credentials *>(ext->Value());
murgatroid99e5061512015-01-12 18:14:35 -0800114 ServerCredentials *credentials = new ServerCredentials(creds_value);
115 credentials->Wrap(args.This());
116 NanReturnValue(args.This());
117 } else {
118 const int argc = 1;
Craig Tillere8e304e2015-01-13 14:41:29 -0800119 Local<Value> argv[argc] = {args[0]};
murgatroid99f28066b2015-03-04 14:42:31 -0800120 NanReturnValue(constructor->GetFunction()->NewInstance(argc, argv));
murgatroid99e5061512015-01-12 18:14:35 -0800121 }
122}
123
124NAN_METHOD(ServerCredentials::CreateSsl) {
Julien Boeuf073d7b62015-01-21 11:13:43 -0800125 // TODO: have the node API support multiple key/cert pairs.
murgatroid99e5061512015-01-12 18:14:35 -0800126 NanScope();
127 char *root_certs = NULL;
Julien Boeuf073d7b62015-01-21 11:13:43 -0800128 grpc_ssl_pem_key_cert_pair key_cert_pair;
murgatroid99f28066b2015-03-04 14:42:31 -0800129 if (::node::Buffer::HasInstance(args[0])) {
130 root_certs = ::node::Buffer::Data(args[0]);
murgatroid99e5061512015-01-12 18:14:35 -0800131 } else if (!(args[0]->IsNull() || args[0]->IsUndefined())) {
132 return NanThrowTypeError(
133 "createSSl's first argument must be a Buffer if provided");
134 }
murgatroid99f28066b2015-03-04 14:42:31 -0800135 if (!::node::Buffer::HasInstance(args[1])) {
Craig Tillere8e304e2015-01-13 14:41:29 -0800136 return NanThrowTypeError("createSsl's second argument must be a Buffer");
murgatroid99e5061512015-01-12 18:14:35 -0800137 }
murgatroid99f28066b2015-03-04 14:42:31 -0800138 key_cert_pair.private_key = ::node::Buffer::Data(args[1]);
139 if (!::node::Buffer::HasInstance(args[2])) {
Craig Tillere8e304e2015-01-13 14:41:29 -0800140 return NanThrowTypeError("createSsl's third argument must be a Buffer");
murgatroid99e5061512015-01-12 18:14:35 -0800141 }
murgatroid99f28066b2015-03-04 14:42:31 -0800142 key_cert_pair.cert_chain = ::node::Buffer::Data(args[2]);
Julien Boeuf5029b302015-07-21 23:02:16 -0700143 // TODO Add a force_client_auth parameter and pass it as the last parameter
144 // here.
Julien Boeuf073d7b62015-01-21 11:13:43 -0800145 NanReturnValue(WrapStruct(
Julien Boeuf5029b302015-07-21 23:02:16 -0700146 grpc_ssl_server_credentials_create(root_certs, &key_cert_pair, 1, 0)));
murgatroid99e5061512015-01-12 18:14:35 -0800147}
148
149NAN_METHOD(ServerCredentials::CreateFake) {
150 NanScope();
Craig Tillere8e304e2015-01-13 14:41:29 -0800151 NanReturnValue(
152 WrapStruct(grpc_fake_transport_security_server_credentials_create()));
murgatroid99e5061512015-01-12 18:14:35 -0800153}
154
155} // namespace node
Craig Tiller190d3602015-02-18 09:23:38 -0800156} // namespace grpc