You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.5 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2004 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef RTC_BASE_OPENSSL_IDENTITY_H_
  11. #define RTC_BASE_OPENSSL_IDENTITY_H_
  12. #include <openssl/ossl_typ.h>
  13. #include <ctime>
  14. #include <memory>
  15. #include <string>
  16. #include "rtc_base/checks.h"
  17. #include "rtc_base/constructor_magic.h"
  18. #include "rtc_base/openssl_certificate.h"
  19. #include "rtc_base/ssl_certificate.h"
  20. #include "rtc_base/ssl_identity.h"
  21. namespace rtc {
  22. // OpenSSLKeyPair encapsulates an OpenSSL EVP_PKEY* keypair object,
  23. // which is reference counted inside the OpenSSL library.
  24. class OpenSSLKeyPair final {
  25. public:
  26. explicit OpenSSLKeyPair(EVP_PKEY* pkey) : pkey_(pkey) {
  27. RTC_DCHECK(pkey_ != nullptr);
  28. }
  29. static OpenSSLKeyPair* Generate(const KeyParams& key_params);
  30. // Constructs a key pair from the private key PEM string. This must not result
  31. // in missing public key parameters. Returns null on error.
  32. static OpenSSLKeyPair* FromPrivateKeyPEMString(const std::string& pem_string);
  33. virtual ~OpenSSLKeyPair();
  34. virtual OpenSSLKeyPair* GetReference();
  35. EVP_PKEY* pkey() const { return pkey_; }
  36. std::string PrivateKeyToPEMString() const;
  37. std::string PublicKeyToPEMString() const;
  38. bool operator==(const OpenSSLKeyPair& other) const;
  39. bool operator!=(const OpenSSLKeyPair& other) const;
  40. private:
  41. void AddReference();
  42. EVP_PKEY* pkey_;
  43. RTC_DISALLOW_COPY_AND_ASSIGN(OpenSSLKeyPair);
  44. };
  45. // Holds a keypair and certificate together, and a method to generate
  46. // them consistently.
  47. class OpenSSLIdentity final : public SSLIdentity {
  48. public:
  49. static OpenSSLIdentity* GenerateWithExpiration(const std::string& common_name,
  50. const KeyParams& key_params,
  51. time_t certificate_lifetime);
  52. static OpenSSLIdentity* GenerateForTest(const SSLIdentityParams& params);
  53. static SSLIdentity* FromPEMStrings(const std::string& private_key,
  54. const std::string& certificate);
  55. static SSLIdentity* FromPEMChainStrings(const std::string& private_key,
  56. const std::string& certificate_chain);
  57. ~OpenSSLIdentity() override;
  58. const OpenSSLCertificate& certificate() const override;
  59. const SSLCertChain& cert_chain() const override;
  60. OpenSSLIdentity* GetReference() const override;
  61. // Configure an SSL context object to use our key and certificate.
  62. bool ConfigureIdentity(SSL_CTX* ctx);
  63. std::string PrivateKeyToPEMString() const override;
  64. std::string PublicKeyToPEMString() const override;
  65. bool operator==(const OpenSSLIdentity& other) const;
  66. bool operator!=(const OpenSSLIdentity& other) const;
  67. private:
  68. OpenSSLIdentity(std::unique_ptr<OpenSSLKeyPair> key_pair,
  69. std::unique_ptr<OpenSSLCertificate> certificate);
  70. OpenSSLIdentity(std::unique_ptr<OpenSSLKeyPair> key_pair,
  71. std::unique_ptr<SSLCertChain> cert_chain);
  72. static OpenSSLIdentity* GenerateInternal(const SSLIdentityParams& params);
  73. std::unique_ptr<OpenSSLKeyPair> key_pair_;
  74. std::unique_ptr<SSLCertChain> cert_chain_;
  75. RTC_DISALLOW_COPY_AND_ASSIGN(OpenSSLIdentity);
  76. };
  77. } // namespace rtc
  78. #endif // RTC_BASE_OPENSSL_IDENTITY_H_