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