server_credentials.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. #include <node.h>
  34. #include "grpc/grpc.h"
  35. #include "grpc/grpc_security.h"
  36. #include "grpc/support/log.h"
  37. #include "server_credentials.h"
  38. namespace grpc {
  39. namespace node {
  40. using v8::Exception;
  41. using v8::External;
  42. using v8::Function;
  43. using v8::FunctionTemplate;
  44. using v8::Handle;
  45. using v8::HandleScope;
  46. using v8::Integer;
  47. using v8::Local;
  48. using v8::Object;
  49. using v8::ObjectTemplate;
  50. using v8::Persistent;
  51. using v8::Value;
  52. NanCallback *ServerCredentials::constructor;
  53. Persistent<FunctionTemplate> ServerCredentials::fun_tpl;
  54. ServerCredentials::ServerCredentials(grpc_server_credentials *credentials)
  55. : wrapped_credentials(credentials) {}
  56. ServerCredentials::~ServerCredentials() {
  57. grpc_server_credentials_release(wrapped_credentials);
  58. }
  59. void ServerCredentials::Init(Handle<Object> exports) {
  60. NanScope();
  61. Local<FunctionTemplate> tpl = NanNew<FunctionTemplate>(New);
  62. tpl->SetClassName(NanNew("ServerCredentials"));
  63. tpl->InstanceTemplate()->SetInternalFieldCount(1);
  64. NanAssignPersistent(fun_tpl, tpl);
  65. Handle<Function> ctr = tpl->GetFunction();
  66. ctr->Set(NanNew("createSsl"),
  67. NanNew<FunctionTemplate>(CreateSsl)->GetFunction());
  68. ctr->Set(NanNew("createFake"),
  69. NanNew<FunctionTemplate>(CreateFake)->GetFunction());
  70. constructor = new NanCallback(ctr);
  71. exports->Set(NanNew("ServerCredentials"), ctr);
  72. }
  73. bool ServerCredentials::HasInstance(Handle<Value> val) {
  74. NanScope();
  75. return NanHasInstance(fun_tpl, val);
  76. }
  77. Handle<Value> ServerCredentials::WrapStruct(
  78. grpc_server_credentials *credentials) {
  79. NanEscapableScope();
  80. if (credentials == NULL) {
  81. return NanEscapeScope(NanNull());
  82. }
  83. const int argc = 1;
  84. Handle<Value> argv[argc] = {
  85. NanNew<External>(reinterpret_cast<void *>(credentials))};
  86. return NanEscapeScope(constructor->GetFunction()->NewInstance(argc, argv));
  87. }
  88. grpc_server_credentials *ServerCredentials::GetWrappedServerCredentials() {
  89. return wrapped_credentials;
  90. }
  91. NAN_METHOD(ServerCredentials::New) {
  92. NanScope();
  93. if (args.IsConstructCall()) {
  94. if (!args[0]->IsExternal()) {
  95. return NanThrowTypeError(
  96. "ServerCredentials can only be created with the provide functions");
  97. }
  98. Handle<External> ext = args[0].As<External>();
  99. grpc_server_credentials *creds_value =
  100. reinterpret_cast<grpc_server_credentials *>(ext->Value());
  101. ServerCredentials *credentials = new ServerCredentials(creds_value);
  102. credentials->Wrap(args.This());
  103. NanReturnValue(args.This());
  104. } else {
  105. const int argc = 1;
  106. Local<Value> argv[argc] = {args[0]};
  107. NanReturnValue(constructor->GetFunction()->NewInstance(argc, argv));
  108. }
  109. }
  110. NAN_METHOD(ServerCredentials::CreateSsl) {
  111. // TODO: have the node API support multiple key/cert pairs.
  112. NanScope();
  113. char *root_certs = NULL;
  114. grpc_ssl_pem_key_cert_pair key_cert_pair;
  115. if (::node::Buffer::HasInstance(args[0])) {
  116. root_certs = ::node::Buffer::Data(args[0]);
  117. } else if (!(args[0]->IsNull() || args[0]->IsUndefined())) {
  118. return NanThrowTypeError(
  119. "createSSl's first argument must be a Buffer if provided");
  120. }
  121. if (!::node::Buffer::HasInstance(args[1])) {
  122. return NanThrowTypeError("createSsl's second argument must be a Buffer");
  123. }
  124. key_cert_pair.private_key = ::node::Buffer::Data(args[1]);
  125. if (!::node::Buffer::HasInstance(args[2])) {
  126. return NanThrowTypeError("createSsl's third argument must be a Buffer");
  127. }
  128. key_cert_pair.cert_chain = ::node::Buffer::Data(args[2]);
  129. NanReturnValue(WrapStruct(
  130. grpc_ssl_server_credentials_create(root_certs, &key_cert_pair, 1)));
  131. }
  132. NAN_METHOD(ServerCredentials::CreateFake) {
  133. NanScope();
  134. NanReturnValue(
  135. WrapStruct(grpc_fake_transport_security_server_credentials_create()));
  136. }
  137. } // namespace node
  138. } // namespace grpc