LCOV - code coverage report
Current view: top level - node/ext - server_credentials.cc (source / functions) Hit Total Coverage
Test: tmp.CaZ6RjdVn2 Lines: 72 75 96.0 %
Date: 2015-12-10 22:15:08 Functions: 12 12 100.0 %

          Line data    Source code
       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           1 : Persistent<FunctionTemplate> ServerCredentials::fun_tpl;
      67             : 
      68          22 : ServerCredentials::ServerCredentials(grpc_server_credentials *credentials)
      69          44 :     : wrapped_credentials(credentials) {}
      70             : 
      71          63 : ServerCredentials::~ServerCredentials() {
      72          21 :   grpc_server_credentials_release(wrapped_credentials);
      73          42 : }
      74             : 
      75           1 : void ServerCredentials::Init(Local<Object> exports) {
      76             :   Nan::HandleScope scope;
      77           1 :   Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
      78           1 :   tpl->SetClassName(Nan::New("ServerCredentials").ToLocalChecked());
      79           2 :   tpl->InstanceTemplate()->SetInternalFieldCount(1);
      80           1 :   Local<Function> ctr = tpl->GetFunction();
      81           1 :   Nan::Set(ctr, Nan::New("createSsl").ToLocalChecked(),
      82             :            Nan::GetFunction(
      83           3 :                Nan::New<FunctionTemplate>(CreateSsl)).ToLocalChecked());
      84           1 :   Nan::Set(ctr, Nan::New("createInsecure").ToLocalChecked(),
      85             :            Nan::GetFunction(
      86           3 :                Nan::New<FunctionTemplate>(CreateInsecure)).ToLocalChecked());
      87             :   fun_tpl.Reset(tpl);
      88           1 :   constructor = new Nan::Callback(ctr);
      89           2 :   Nan::Set(exports, Nan::New("ServerCredentials").ToLocalChecked(), ctr);
      90           1 : }
      91             : 
      92          30 : bool ServerCredentials::HasInstance(Local<Value> val) {
      93             :   Nan::HandleScope scope;
      94          90 :   return Nan::New(fun_tpl)->HasInstance(val);
      95             : }
      96             : 
      97          22 : Local<Value> ServerCredentials::WrapStruct(
      98             :     grpc_server_credentials *credentials) {
      99             :   Nan::EscapableHandleScope scope;
     100          22 :   const int argc = 1;
     101             :   Local<Value> argv[argc] = {
     102          22 :     Nan::New<External>(reinterpret_cast<void *>(credentials))};
     103             :   MaybeLocal<Object> maybe_instance = Nan::NewInstance(
     104          44 :       constructor->GetFunction(), argc, argv);
     105          22 :   if (maybe_instance.IsEmpty()) {
     106           0 :     return scope.Escape(Nan::Null());
     107             :   } else {
     108          22 :     return scope.Escape(maybe_instance.ToLocalChecked());
     109             :   }
     110             : }
     111             : 
     112          30 : grpc_server_credentials *ServerCredentials::GetWrappedServerCredentials() {
     113          30 :   return wrapped_credentials;
     114             : }
     115             : 
     116          22 : NAN_METHOD(ServerCredentials::New) {
     117          44 :   if (info.IsConstructCall()) {
     118          44 :     if (!info[0]->IsExternal()) {
     119             :       return Nan::ThrowTypeError(
     120             :           "ServerCredentials can only be created with the provided functions");
     121             :     }
     122          66 :     Local<External> ext = info[0].As<External>();
     123             :     grpc_server_credentials *creds_value =
     124          22 :         reinterpret_cast<grpc_server_credentials *>(ext->Value());
     125          22 :     ServerCredentials *credentials = new ServerCredentials(creds_value);
     126          44 :     credentials->Wrap(info.This());
     127          66 :     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          13 : NAN_METHOD(ServerCredentials::CreateSsl) {
     136             :   Nan::HandleScope scope;
     137          13 :   char *root_certs = NULL;
     138          26 :   if (::node::Buffer::HasInstance(info[0])) {
     139          10 :     root_certs = ::node::Buffer::Data(info[0]);
     140          26 :   } 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          24 :   if (!info[1]->IsArray()) {
     145             :     return Nan::ThrowTypeError(
     146             :         "createSsl's second argument must be a list of objects");
     147             :   }
     148          10 :   int force_client_auth = 0;
     149          20 :   if (info[2]->IsBoolean()) {
     150           2 :     force_client_auth = (int)Nan::To<bool>(info[2]).FromJust();
     151          29 :   } 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          18 :   Local<Array> pair_list = Local<Array>::Cast(info[1]);
     156           9 :   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           9 :       key_cert_pair_count];
     159             : 
     160           9 :   Local<String> key_key = Nan::New("private_key").ToLocalChecked();
     161           9 :   Local<String> cert_key = Nan::New("cert_chain").ToLocalChecked();
     162             : 
     163          30 :   for(uint32_t i = 0; i < key_cert_pair_count; i++) {
     164           8 :     Local<Value> pair_val = Nan::Get(pair_list, i).ToLocalChecked();
     165           8 :     if (!pair_val->IsObject()) {
     166           0 :       delete key_cert_pairs;
     167           2 :       return Nan::ThrowTypeError("Key/cert pairs must be objects");
     168             :     }
     169           8 :     Local<Object> pair_obj = Nan::To<Object>(pair_val).ToLocalChecked();
     170           8 :     Local<Value> maybe_key = Nan::Get(pair_obj, key_key).ToLocalChecked();
     171           8 :     Local<Value> maybe_cert = Nan::Get(pair_obj, cert_key).ToLocalChecked();
     172           8 :     if (!::node::Buffer::HasInstance(maybe_key)) {
     173           1 :       delete key_cert_pairs;
     174             :       return Nan::ThrowTypeError("private_key must be a Buffer");
     175             :     }
     176           7 :     if (!::node::Buffer::HasInstance(maybe_cert)) {
     177           1 :       delete key_cert_pairs;
     178             :       return Nan::ThrowTypeError("cert_chain must be a Buffer");
     179             :     }
     180           6 :     key_cert_pairs[i].private_key = ::node::Buffer::Data(maybe_key);
     181           6 :     key_cert_pairs[i].cert_chain = ::node::Buffer::Data(maybe_cert);
     182             :   }
     183             :   grpc_server_credentials *creds = grpc_ssl_server_credentials_create(
     184           7 :       root_certs, key_cert_pairs, key_cert_pair_count, force_client_auth, NULL);
     185           7 :   delete key_cert_pairs;
     186           7 :   if (creds == NULL) {
     187           0 :     info.GetReturnValue().SetNull();
     188             :   } else {
     189          21 :     info.GetReturnValue().Set(WrapStruct(creds));
     190             :   }
     191             : }
     192             : 
     193          15 : NAN_METHOD(ServerCredentials::CreateInsecure) {
     194          45 :   info.GetReturnValue().Set(WrapStruct(NULL));
     195          15 : }
     196             : 
     197             : }  // namespace node
     198           3 : }  // namespace grpc

Generated by: LCOV version 1.11