xref: /aosp_15_r20/external/webrtc/rtc_base/ssl_certificate.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright 2018 The WebRTC Project Authors. All rights reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker // Generic interface for SSL Certificates, used in both the SSLAdapter
12*d9f75844SAndroid Build Coastguard Worker // for TLS TURN connections and the SSLStreamAdapter for DTLS Peer to Peer
13*d9f75844SAndroid Build Coastguard Worker // Connections for SRTP Key negotiation and SCTP encryption.
14*d9f75844SAndroid Build Coastguard Worker 
15*d9f75844SAndroid Build Coastguard Worker #ifndef RTC_BASE_SSL_CERTIFICATE_H_
16*d9f75844SAndroid Build Coastguard Worker #define RTC_BASE_SSL_CERTIFICATE_H_
17*d9f75844SAndroid Build Coastguard Worker 
18*d9f75844SAndroid Build Coastguard Worker #include <stddef.h>
19*d9f75844SAndroid Build Coastguard Worker #include <stdint.h>
20*d9f75844SAndroid Build Coastguard Worker 
21*d9f75844SAndroid Build Coastguard Worker #include <memory>
22*d9f75844SAndroid Build Coastguard Worker #include <string>
23*d9f75844SAndroid Build Coastguard Worker #include <vector>
24*d9f75844SAndroid Build Coastguard Worker 
25*d9f75844SAndroid Build Coastguard Worker #include "absl/strings/string_view.h"
26*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/buffer.h"
27*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/system/rtc_export.h"
28*d9f75844SAndroid Build Coastguard Worker 
29*d9f75844SAndroid Build Coastguard Worker namespace rtc {
30*d9f75844SAndroid Build Coastguard Worker 
31*d9f75844SAndroid Build Coastguard Worker struct RTC_EXPORT SSLCertificateStats {
32*d9f75844SAndroid Build Coastguard Worker   SSLCertificateStats(std::string&& fingerprint,
33*d9f75844SAndroid Build Coastguard Worker                       std::string&& fingerprint_algorithm,
34*d9f75844SAndroid Build Coastguard Worker                       std::string&& base64_certificate,
35*d9f75844SAndroid Build Coastguard Worker                       std::unique_ptr<SSLCertificateStats> issuer);
36*d9f75844SAndroid Build Coastguard Worker   ~SSLCertificateStats();
37*d9f75844SAndroid Build Coastguard Worker   std::string fingerprint;
38*d9f75844SAndroid Build Coastguard Worker   std::string fingerprint_algorithm;
39*d9f75844SAndroid Build Coastguard Worker   std::string base64_certificate;
40*d9f75844SAndroid Build Coastguard Worker   std::unique_ptr<SSLCertificateStats> issuer;
41*d9f75844SAndroid Build Coastguard Worker 
42*d9f75844SAndroid Build Coastguard Worker   std::unique_ptr<SSLCertificateStats> Copy() const;
43*d9f75844SAndroid Build Coastguard Worker };
44*d9f75844SAndroid Build Coastguard Worker 
45*d9f75844SAndroid Build Coastguard Worker // Abstract interface overridden by SSL library specific
46*d9f75844SAndroid Build Coastguard Worker // implementations.
47*d9f75844SAndroid Build Coastguard Worker 
48*d9f75844SAndroid Build Coastguard Worker // A somewhat opaque type used to encapsulate a certificate.
49*d9f75844SAndroid Build Coastguard Worker // Wraps the SSL library's notion of a certificate, with reference counting.
50*d9f75844SAndroid Build Coastguard Worker // The SSLCertificate object is pretty much immutable once created.
51*d9f75844SAndroid Build Coastguard Worker // (The OpenSSL implementation only does reference counting and
52*d9f75844SAndroid Build Coastguard Worker // possibly caching of intermediate results.)
53*d9f75844SAndroid Build Coastguard Worker class RTC_EXPORT SSLCertificate {
54*d9f75844SAndroid Build Coastguard Worker  public:
55*d9f75844SAndroid Build Coastguard Worker   // Parses and builds a certificate from a PEM encoded string.
56*d9f75844SAndroid Build Coastguard Worker   // Returns null on failure.
57*d9f75844SAndroid Build Coastguard Worker   // The length of the string representation of the certificate is
58*d9f75844SAndroid Build Coastguard Worker   // stored in *pem_length if it is non-null, and only if
59*d9f75844SAndroid Build Coastguard Worker   // parsing was successful.
60*d9f75844SAndroid Build Coastguard Worker   static std::unique_ptr<SSLCertificate> FromPEMString(
61*d9f75844SAndroid Build Coastguard Worker       absl::string_view pem_string);
62*d9f75844SAndroid Build Coastguard Worker   virtual ~SSLCertificate() = default;
63*d9f75844SAndroid Build Coastguard Worker 
64*d9f75844SAndroid Build Coastguard Worker   // Returns a new SSLCertificate object instance wrapping the same
65*d9f75844SAndroid Build Coastguard Worker   // underlying certificate, including its chain if present.
66*d9f75844SAndroid Build Coastguard Worker   virtual std::unique_ptr<SSLCertificate> Clone() const = 0;
67*d9f75844SAndroid Build Coastguard Worker 
68*d9f75844SAndroid Build Coastguard Worker   // Returns a PEM encoded string representation of the certificate.
69*d9f75844SAndroid Build Coastguard Worker   virtual std::string ToPEMString() const = 0;
70*d9f75844SAndroid Build Coastguard Worker 
71*d9f75844SAndroid Build Coastguard Worker   // Provides a DER encoded binary representation of the certificate.
72*d9f75844SAndroid Build Coastguard Worker   virtual void ToDER(Buffer* der_buffer) const = 0;
73*d9f75844SAndroid Build Coastguard Worker 
74*d9f75844SAndroid Build Coastguard Worker   // Gets the name of the digest algorithm that was used to compute this
75*d9f75844SAndroid Build Coastguard Worker   // certificate's signature.
76*d9f75844SAndroid Build Coastguard Worker   virtual bool GetSignatureDigestAlgorithm(std::string* algorithm) const = 0;
77*d9f75844SAndroid Build Coastguard Worker 
78*d9f75844SAndroid Build Coastguard Worker   // Compute the digest of the certificate given algorithm
79*d9f75844SAndroid Build Coastguard Worker   virtual bool ComputeDigest(absl::string_view algorithm,
80*d9f75844SAndroid Build Coastguard Worker                              unsigned char* digest,
81*d9f75844SAndroid Build Coastguard Worker                              size_t size,
82*d9f75844SAndroid Build Coastguard Worker                              size_t* length) const = 0;
83*d9f75844SAndroid Build Coastguard Worker 
84*d9f75844SAndroid Build Coastguard Worker   // Returns the time in seconds relative to epoch, 1970-01-01T00:00:00Z (UTC),
85*d9f75844SAndroid Build Coastguard Worker   // or -1 if an expiration time could not be retrieved.
86*d9f75844SAndroid Build Coastguard Worker   virtual int64_t CertificateExpirationTime() const = 0;
87*d9f75844SAndroid Build Coastguard Worker 
88*d9f75844SAndroid Build Coastguard Worker   // Gets information (fingerprint, etc.) about this certificate. This is used
89*d9f75844SAndroid Build Coastguard Worker   // for certificate stats, see
90*d9f75844SAndroid Build Coastguard Worker   // https://w3c.github.io/webrtc-stats/#certificatestats-dict*.
91*d9f75844SAndroid Build Coastguard Worker   std::unique_ptr<SSLCertificateStats> GetStats() const;
92*d9f75844SAndroid Build Coastguard Worker };
93*d9f75844SAndroid Build Coastguard Worker 
94*d9f75844SAndroid Build Coastguard Worker // SSLCertChain is a simple wrapper for a vector of SSLCertificates. It serves
95*d9f75844SAndroid Build Coastguard Worker // primarily to ensure proper memory management (especially deletion) of the
96*d9f75844SAndroid Build Coastguard Worker // SSLCertificate pointers.
97*d9f75844SAndroid Build Coastguard Worker class RTC_EXPORT SSLCertChain final {
98*d9f75844SAndroid Build Coastguard Worker  public:
99*d9f75844SAndroid Build Coastguard Worker   explicit SSLCertChain(std::unique_ptr<SSLCertificate> single_cert);
100*d9f75844SAndroid Build Coastguard Worker   explicit SSLCertChain(std::vector<std::unique_ptr<SSLCertificate>> certs);
101*d9f75844SAndroid Build Coastguard Worker   // Allow move semantics for the object.
102*d9f75844SAndroid Build Coastguard Worker   SSLCertChain(SSLCertChain&&);
103*d9f75844SAndroid Build Coastguard Worker   SSLCertChain& operator=(SSLCertChain&&);
104*d9f75844SAndroid Build Coastguard Worker 
105*d9f75844SAndroid Build Coastguard Worker   ~SSLCertChain();
106*d9f75844SAndroid Build Coastguard Worker 
107*d9f75844SAndroid Build Coastguard Worker   SSLCertChain(const SSLCertChain&) = delete;
108*d9f75844SAndroid Build Coastguard Worker   SSLCertChain& operator=(const SSLCertChain&) = delete;
109*d9f75844SAndroid Build Coastguard Worker 
110*d9f75844SAndroid Build Coastguard Worker   // Vector access methods.
GetSize()111*d9f75844SAndroid Build Coastguard Worker   size_t GetSize() const { return certs_.size(); }
112*d9f75844SAndroid Build Coastguard Worker 
113*d9f75844SAndroid Build Coastguard Worker   // Returns a temporary reference, only valid until the chain is destroyed.
Get(size_t pos)114*d9f75844SAndroid Build Coastguard Worker   const SSLCertificate& Get(size_t pos) const { return *(certs_[pos]); }
115*d9f75844SAndroid Build Coastguard Worker 
116*d9f75844SAndroid Build Coastguard Worker   // Returns a new SSLCertChain object instance wrapping the same underlying
117*d9f75844SAndroid Build Coastguard Worker   // certificate chain.
118*d9f75844SAndroid Build Coastguard Worker   std::unique_ptr<SSLCertChain> Clone() const;
119*d9f75844SAndroid Build Coastguard Worker 
120*d9f75844SAndroid Build Coastguard Worker   // Gets information (fingerprint, etc.) about this certificate chain. This is
121*d9f75844SAndroid Build Coastguard Worker   // used for certificate stats, see
122*d9f75844SAndroid Build Coastguard Worker   // https://w3c.github.io/webrtc-stats/#certificatestats-dict*.
123*d9f75844SAndroid Build Coastguard Worker   std::unique_ptr<SSLCertificateStats> GetStats() const;
124*d9f75844SAndroid Build Coastguard Worker 
125*d9f75844SAndroid Build Coastguard Worker  private:
126*d9f75844SAndroid Build Coastguard Worker   std::vector<std::unique_ptr<SSLCertificate>> certs_;
127*d9f75844SAndroid Build Coastguard Worker };
128*d9f75844SAndroid Build Coastguard Worker 
129*d9f75844SAndroid Build Coastguard Worker // SSLCertificateVerifier provides a simple interface to allow third parties to
130*d9f75844SAndroid Build Coastguard Worker // define their own certificate verification code. It is completely independent
131*d9f75844SAndroid Build Coastguard Worker // from the underlying SSL implementation.
132*d9f75844SAndroid Build Coastguard Worker class SSLCertificateVerifier {
133*d9f75844SAndroid Build Coastguard Worker  public:
134*d9f75844SAndroid Build Coastguard Worker   virtual ~SSLCertificateVerifier() = default;
135*d9f75844SAndroid Build Coastguard Worker   // Returns true if the certificate is valid, else false. It is up to the
136*d9f75844SAndroid Build Coastguard Worker   // implementer to define what a valid certificate looks like.
137*d9f75844SAndroid Build Coastguard Worker   virtual bool Verify(const SSLCertificate& certificate) = 0;
138*d9f75844SAndroid Build Coastguard Worker };
139*d9f75844SAndroid Build Coastguard Worker 
140*d9f75844SAndroid Build Coastguard Worker }  // namespace rtc
141*d9f75844SAndroid Build Coastguard Worker 
142*d9f75844SAndroid Build Coastguard Worker #endif  // RTC_BASE_SSL_CERTIFICATE_H_
143