xref: /aosp_15_r20/external/webrtc/rtc_base/openssl_identity.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 
11 #ifndef RTC_BASE_OPENSSL_IDENTITY_H_
12 #define RTC_BASE_OPENSSL_IDENTITY_H_
13 
14 #include <openssl/ossl_typ.h>
15 
16 #include <ctime>
17 #include <memory>
18 #include <string>
19 
20 #include "rtc_base/openssl_certificate.h"
21 #include "rtc_base/openssl_key_pair.h"
22 #include "rtc_base/ssl_certificate.h"
23 #include "rtc_base/ssl_identity.h"
24 
25 namespace rtc {
26 
27 // Holds a keypair and certificate together, and a method to generate
28 // them consistently.
29 class OpenSSLIdentity final : public SSLIdentity {
30  public:
31   static std::unique_ptr<OpenSSLIdentity> CreateWithExpiration(
32       absl::string_view common_name,
33       const KeyParams& key_params,
34       time_t certificate_lifetime);
35   static std::unique_ptr<OpenSSLIdentity> CreateForTest(
36       const SSLIdentityParams& params);
37   static std::unique_ptr<SSLIdentity> CreateFromPEMStrings(
38       absl::string_view private_key,
39       absl::string_view certificate);
40   static std::unique_ptr<SSLIdentity> CreateFromPEMChainStrings(
41       absl::string_view private_key,
42       absl::string_view certificate_chain);
43   ~OpenSSLIdentity() override;
44 
45   OpenSSLIdentity(const OpenSSLIdentity&) = delete;
46   OpenSSLIdentity& operator=(const OpenSSLIdentity&) = delete;
47 
48   const OpenSSLCertificate& certificate() const override;
49   const SSLCertChain& cert_chain() const override;
50 
51   // Configure an SSL context object to use our key and certificate.
52   bool ConfigureIdentity(SSL_CTX* ctx);
53 
54   std::string PrivateKeyToPEMString() const override;
55   std::string PublicKeyToPEMString() const override;
56   bool operator==(const OpenSSLIdentity& other) const;
57   bool operator!=(const OpenSSLIdentity& other) const;
58 
59  private:
60   OpenSSLIdentity(std::unique_ptr<OpenSSLKeyPair> key_pair,
61                   std::unique_ptr<OpenSSLCertificate> certificate);
62   OpenSSLIdentity(std::unique_ptr<OpenSSLKeyPair> key_pair,
63                   std::unique_ptr<SSLCertChain> cert_chain);
64   std::unique_ptr<SSLIdentity> CloneInternal() const override;
65 
66   static std::unique_ptr<OpenSSLIdentity> CreateInternal(
67       const SSLIdentityParams& params);
68 
69   std::unique_ptr<OpenSSLKeyPair> key_pair_;
70   std::unique_ptr<SSLCertChain> cert_chain_;
71 };
72 
73 }  // namespace rtc
74 
75 #endif  // RTC_BASE_OPENSSL_IDENTITY_H_
76