blob: 9c9a1b38a7bf33900e23521d62c95f1f7cb7e1e8 [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
44using ::node::Buffer;
45using v8::Arguments;
46using v8::Exception;
47using v8::External;
48using v8::Function;
49using v8::FunctionTemplate;
50using v8::Handle;
51using v8::HandleScope;
52using v8::Integer;
53using v8::Local;
54using v8::Object;
55using v8::ObjectTemplate;
56using v8::Persistent;
57using v8::Value;
58
59Persistent<Function> ServerCredentials::constructor;
60Persistent<FunctionTemplate> ServerCredentials::fun_tpl;
61
62ServerCredentials::ServerCredentials(grpc_server_credentials *credentials)
Craig Tillere8e304e2015-01-13 14:41:29 -080063 : wrapped_credentials(credentials) {}
murgatroid99e5061512015-01-12 18:14:35 -080064
65ServerCredentials::~ServerCredentials() {
murgatroid99e5061512015-01-12 18:14:35 -080066 grpc_server_credentials_release(wrapped_credentials);
67}
68
69void 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
83bool ServerCredentials::HasInstance(Handle<Value> val) {
84 NanScope();
85 return NanHasInstance(fun_tpl, val);
86}
87
88Handle<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 Tillere8e304e2015-01-13 14:41:29 -080096 External::New(reinterpret_cast<void *>(credentials))};
murgatroid99e5061512015-01-12 18:14:35 -080097 return NanEscapeScope(constructor->NewInstance(argc, argv));
98}
99
100grpc_server_credentials *ServerCredentials::GetWrappedServerCredentials() {
101 return wrapped_credentials;
102}
103
104NAN_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 Tillere8e304e2015-01-13 14:41:29 -0800113 reinterpret_cast<grpc_server_credentials *>(External::Unwrap(args[0]));
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]};
murgatroid99e5061512015-01-12 18:14:35 -0800120 NanReturnValue(constructor->NewInstance(argc, argv));
121 }
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;
murgatroid99e5061512015-01-12 18:14:35 -0800129 if (Buffer::HasInstance(args[0])) {
130 root_certs = 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 }
135 if (!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 }
Julien Boeuf073d7b62015-01-21 11:13:43 -0800138 key_cert_pair.private_key = Buffer::Data(args[1]);
murgatroid99e5061512015-01-12 18:14:35 -0800139 if (!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 }
Julien Boeuf073d7b62015-01-21 11:13:43 -0800142 key_cert_pair.cert_chain = Buffer::Data(args[2]);
143 NanReturnValue(WrapStruct(
144 grpc_ssl_server_credentials_create(root_certs, &key_cert_pair, 1)));
murgatroid99e5061512015-01-12 18:14:35 -0800145}
146
147NAN_METHOD(ServerCredentials::CreateFake) {
148 NanScope();
Craig Tillere8e304e2015-01-13 14:41:29 -0800149 NanReturnValue(
150 WrapStruct(grpc_fake_transport_security_server_credentials_create()));
murgatroid99e5061512015-01-12 18:14:35 -0800151}
152
153} // namespace node
Craig Tiller06059952015-02-18 08:34:56 -0800154} // namespace grpc