credentials.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. *
  3. * Copyright 2014, 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 <string>
  34. #include <grpc/grpc_security.h>
  35. #include <grpc++/credentials.h>
  36. namespace grpc {
  37. Credentials::Credentials(grpc_credentials* c_creds) : creds_(c_creds) {}
  38. Credentials::~Credentials() { grpc_credentials_release(creds_); }
  39. grpc_credentials* Credentials::GetRawCreds() { return creds_; }
  40. std::unique_ptr<Credentials> CredentialsFactory::DefaultCredentials() {
  41. grpc_credentials* c_creds = grpc_default_credentials_create();
  42. std::unique_ptr<Credentials> cpp_creds(new Credentials(c_creds));
  43. return cpp_creds;
  44. }
  45. // Builds SSL Credentials given SSL specific options
  46. std::unique_ptr<Credentials> CredentialsFactory::SslCredentials(
  47. const SslCredentialsOptions& options) {
  48. const unsigned char* pem_root_certs =
  49. options.pem_root_certs.empty() ? nullptr
  50. : reinterpret_cast<const unsigned char*>(
  51. options.pem_root_certs.c_str());
  52. const unsigned char* pem_private_key =
  53. options.pem_private_key.empty() ? nullptr
  54. : reinterpret_cast<const unsigned char*>(
  55. options.pem_private_key.c_str());
  56. const unsigned char* pem_cert_chain =
  57. options.pem_cert_chain.empty() ? nullptr
  58. : reinterpret_cast<const unsigned char*>(
  59. options.pem_cert_chain.c_str());
  60. grpc_credentials* c_creds = grpc_ssl_credentials_create(
  61. pem_root_certs, options.pem_root_certs.size(), pem_private_key,
  62. options.pem_private_key.size(), pem_cert_chain,
  63. options.pem_cert_chain.size());
  64. std::unique_ptr<Credentials> cpp_creds(new Credentials(c_creds));
  65. return cpp_creds;
  66. }
  67. // Builds credentials for use when running in GCE
  68. std::unique_ptr<Credentials> CredentialsFactory::ComputeEngineCredentials() {
  69. grpc_credentials* c_creds = grpc_compute_engine_credentials_create();
  70. std::unique_ptr<Credentials> cpp_creds(new Credentials(c_creds));
  71. return cpp_creds;
  72. }
  73. // Combines two credentials objects into a composite credentials.
  74. std::unique_ptr<Credentials> CredentialsFactory::ComposeCredentials(
  75. const std::unique_ptr<Credentials>& creds1,
  76. const std::unique_ptr<Credentials>& creds2) {
  77. // Note that we are not saving unique_ptrs to the two credentials
  78. // passed in here. This is OK because the underlying C objects (i.e.,
  79. // creds1 and creds2) into grpc_composite_credentials_create will see their
  80. // refcounts incremented.
  81. grpc_credentials* c_creds = grpc_composite_credentials_create(
  82. creds1->GetRawCreds(), creds2->GetRawCreds());
  83. std::unique_ptr<Credentials> cpp_creds(new Credentials(c_creds));
  84. return cpp_creds;
  85. }
  86. } // namespace grpc