xref: /aosp_15_r20/external/webrtc/rtc_base/fake_ssl_identity.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright 2012 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_FAKE_SSL_IDENTITY_H_
12 #define RTC_BASE_FAKE_SSL_IDENTITY_H_
13 
14 #include <memory>
15 #include <vector>
16 
17 #include "absl/strings/string_view.h"
18 #include "rtc_base/ssl_certificate.h"
19 #include "rtc_base/ssl_identity.h"
20 
21 namespace rtc {
22 
23 class FakeSSLCertificate : public SSLCertificate {
24  public:
25   // SHA-1 is the default digest algorithm because it is available in all build
26   // configurations used for unit testing.
27   explicit FakeSSLCertificate(absl::string_view pem_string);
28 
29   FakeSSLCertificate(const FakeSSLCertificate&);
30   ~FakeSSLCertificate() override;
31 
32   // SSLCertificate implementation.
33   std::unique_ptr<SSLCertificate> Clone() const override;
34   std::string ToPEMString() const override;
35   void ToDER(Buffer* der_buffer) const override;
36   int64_t CertificateExpirationTime() const override;
37   bool GetSignatureDigestAlgorithm(std::string* algorithm) const override;
38   bool ComputeDigest(absl::string_view algorithm,
39                      unsigned char* digest,
40                      size_t size,
41                      size_t* length) const override;
42 
43   void SetCertificateExpirationTime(int64_t expiration_time);
44 
45   void set_digest_algorithm(absl::string_view algorithm);
46 
47  private:
48   std::string pem_string_;
49   std::string digest_algorithm_;
50   // Expiration time in seconds relative to epoch, 1970-01-01T00:00:00Z (UTC).
51   int64_t expiration_time_;
52 };
53 
54 class FakeSSLIdentity : public SSLIdentity {
55  public:
56   explicit FakeSSLIdentity(absl::string_view pem_string);
57   // For a certificate chain.
58   explicit FakeSSLIdentity(const std::vector<std::string>& pem_strings);
59   explicit FakeSSLIdentity(const FakeSSLCertificate& cert);
60 
61   explicit FakeSSLIdentity(const FakeSSLIdentity& o);
62 
63   ~FakeSSLIdentity() override;
64 
65   // SSLIdentity implementation.
66   const SSLCertificate& certificate() const override;
67   const SSLCertChain& cert_chain() const override;
68   // Not implemented.
69   std::string PrivateKeyToPEMString() const override;
70   // Not implemented.
71   std::string PublicKeyToPEMString() const override;
72   // Not implemented.
73   virtual bool operator==(const SSLIdentity& other) const;
74 
75  private:
76   std::unique_ptr<SSLIdentity> CloneInternal() const override;
77 
78   std::unique_ptr<SSLCertChain> cert_chain_;
79 };
80 
81 }  // namespace rtc
82 
83 #endif  // RTC_BASE_FAKE_SSL_IDENTITY_H_
84