blob: 2e198800c45ee2577e41c36b27a1b121de205ea3 [file] [log] [blame]
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001/*
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
Steve Anton10542f22019-01-11 09:11:00 -080015#ifndef RTC_BASE_SSL_CERTIFICATE_H_
16#define RTC_BASE_SSL_CERTIFICATE_H_
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070017
Yves Gerey988cc082018-10-23 12:03:01 +020018#include <stddef.h>
19#include <stdint.h>
Byoungchan Lee14af7622022-01-12 05:24:58 +090020
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070021#include <memory>
22#include <string>
23#include <vector>
24
Ali Tofigh7fa90572022-03-17 15:47:49 +010025#include "absl/strings/string_view.h"
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070026#include "rtc_base/buffer.h"
Mirko Bonadei35214fc2019-09-23 14:54:28 +020027#include "rtc_base/system/rtc_export.h"
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070028
29namespace rtc {
30
Mirko Bonadei35214fc2019-09-23 14:54:28 +020031struct RTC_EXPORT SSLCertificateStats {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070032 SSLCertificateStats(std::string&& fingerprint,
33 std::string&& fingerprint_algorithm,
34 std::string&& base64_certificate,
Steve Antonf25303e2018-10-16 15:23:31 -070035 std::unique_ptr<SSLCertificateStats> issuer);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070036 ~SSLCertificateStats();
37 std::string fingerprint;
38 std::string fingerprint_algorithm;
39 std::string base64_certificate;
40 std::unique_ptr<SSLCertificateStats> issuer;
Henrik Boström69d23c92022-09-26 14:13:17 +020041
42 std::unique_ptr<SSLCertificateStats> Copy() const;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070043};
44
45// Abstract interface overridden by SSL library specific
46// implementations.
47
48// A somewhat opaque type used to encapsulate a certificate.
49// Wraps the SSL library's notion of a certificate, with reference counting.
50// The SSLCertificate object is pretty much immutable once created.
51// (The OpenSSL implementation only does reference counting and
52// possibly caching of intermediate results.)
Mirko Bonadei35214fc2019-09-23 14:54:28 +020053class RTC_EXPORT SSLCertificate {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070054 public:
55 // Parses and builds a certificate from a PEM encoded string.
56 // Returns null on failure.
57 // The length of the string representation of the certificate is
58 // stored in *pem_length if it is non-null, and only if
59 // parsing was successful.
Steve Antonf25303e2018-10-16 15:23:31 -070060 static std::unique_ptr<SSLCertificate> FromPEMString(
Ali Tofigh7fa90572022-03-17 15:47:49 +010061 absl::string_view pem_string);
Steve Antonf25303e2018-10-16 15:23:31 -070062 virtual ~SSLCertificate() = default;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070063
64 // Returns a new SSLCertificate object instance wrapping the same
Steve Antonf25303e2018-10-16 15:23:31 -070065 // underlying certificate, including its chain if present.
66 virtual std::unique_ptr<SSLCertificate> Clone() const = 0;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070067
68 // Returns a PEM encoded string representation of the certificate.
69 virtual std::string ToPEMString() const = 0;
70
71 // Provides a DER encoded binary representation of the certificate.
72 virtual void ToDER(Buffer* der_buffer) const = 0;
73
74 // Gets the name of the digest algorithm that was used to compute this
75 // certificate's signature.
76 virtual bool GetSignatureDigestAlgorithm(std::string* algorithm) const = 0;
77
78 // Compute the digest of the certificate given algorithm
Ali Tofigh7fa90572022-03-17 15:47:49 +010079 virtual bool ComputeDigest(absl::string_view algorithm,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070080 unsigned char* digest,
81 size_t size,
82 size_t* length) const = 0;
83
84 // Returns the time in seconds relative to epoch, 1970-01-01T00:00:00Z (UTC),
85 // or -1 if an expiration time could not be retrieved.
86 virtual int64_t CertificateExpirationTime() const = 0;
87
88 // Gets information (fingerprint, etc.) about this certificate. This is used
89 // for certificate stats, see
90 // https://w3c.github.io/webrtc-stats/#certificatestats-dict*.
91 std::unique_ptr<SSLCertificateStats> GetStats() const;
92};
93
94// SSLCertChain is a simple wrapper for a vector of SSLCertificates. It serves
95// primarily to ensure proper memory management (especially deletion) of the
96// SSLCertificate pointers.
Mirko Bonadei35214fc2019-09-23 14:54:28 +020097class RTC_EXPORT SSLCertChain final {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070098 public:
Steve Antonf25303e2018-10-16 15:23:31 -070099 explicit SSLCertChain(std::unique_ptr<SSLCertificate> single_cert);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700100 explicit SSLCertChain(std::vector<std::unique_ptr<SSLCertificate>> certs);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700101 // Allow move semantics for the object.
102 SSLCertChain(SSLCertChain&&);
103 SSLCertChain& operator=(SSLCertChain&&);
104
105 ~SSLCertChain();
106
Byoungchan Lee14af7622022-01-12 05:24:58 +0900107 SSLCertChain(const SSLCertChain&) = delete;
108 SSLCertChain& operator=(const SSLCertChain&) = delete;
109
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700110 // Vector access methods.
111 size_t GetSize() const { return certs_.size(); }
112
113 // Returns a temporary reference, only valid until the chain is destroyed.
114 const SSLCertificate& Get(size_t pos) const { return *(certs_[pos]); }
115
116 // Returns a new SSLCertChain object instance wrapping the same underlying
Steve Antonf25303e2018-10-16 15:23:31 -0700117 // certificate chain.
118 std::unique_ptr<SSLCertChain> Clone() const;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700119
120 // Gets information (fingerprint, etc.) about this certificate chain. This is
121 // used for certificate stats, see
122 // https://w3c.github.io/webrtc-stats/#certificatestats-dict*.
123 std::unique_ptr<SSLCertificateStats> GetStats() const;
124
125 private:
126 std::vector<std::unique_ptr<SSLCertificate>> certs_;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700127};
128
129// SSLCertificateVerifier provides a simple interface to allow third parties to
130// define their own certificate verification code. It is completely independent
131// from the underlying SSL implementation.
132class SSLCertificateVerifier {
133 public:
134 virtual ~SSLCertificateVerifier() = default;
135 // Returns true if the certificate is valid, else false. It is up to the
136 // implementer to define what a valid certificate looks like.
137 virtual bool Verify(const SSLCertificate& certificate) = 0;
138};
139
140} // namespace rtc
141
Steve Anton10542f22019-01-11 09:11:00 -0800142#endif // RTC_BASE_SSL_CERTIFICATE_H_