blob: 77fbba3e9ec0fd0e441e103dd83c8778706e40a2 [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;
41};
42
43// Abstract interface overridden by SSL library specific
44// implementations.
45
46// A somewhat opaque type used to encapsulate a certificate.
47// Wraps the SSL library's notion of a certificate, with reference counting.
48// The SSLCertificate object is pretty much immutable once created.
49// (The OpenSSL implementation only does reference counting and
50// possibly caching of intermediate results.)
Mirko Bonadei35214fc2019-09-23 14:54:28 +020051class RTC_EXPORT SSLCertificate {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070052 public:
53 // Parses and builds a certificate from a PEM encoded string.
54 // Returns null on failure.
55 // The length of the string representation of the certificate is
56 // stored in *pem_length if it is non-null, and only if
57 // parsing was successful.
Steve Antonf25303e2018-10-16 15:23:31 -070058 static std::unique_ptr<SSLCertificate> FromPEMString(
Ali Tofigh7fa90572022-03-17 15:47:49 +010059 absl::string_view pem_string);
Steve Antonf25303e2018-10-16 15:23:31 -070060 virtual ~SSLCertificate() = default;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070061
62 // Returns a new SSLCertificate object instance wrapping the same
Steve Antonf25303e2018-10-16 15:23:31 -070063 // underlying certificate, including its chain if present.
64 virtual std::unique_ptr<SSLCertificate> Clone() const = 0;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070065
66 // Returns a PEM encoded string representation of the certificate.
67 virtual std::string ToPEMString() const = 0;
68
69 // Provides a DER encoded binary representation of the certificate.
70 virtual void ToDER(Buffer* der_buffer) const = 0;
71
72 // Gets the name of the digest algorithm that was used to compute this
73 // certificate's signature.
74 virtual bool GetSignatureDigestAlgorithm(std::string* algorithm) const = 0;
75
76 // Compute the digest of the certificate given algorithm
Ali Tofigh7fa90572022-03-17 15:47:49 +010077 virtual bool ComputeDigest(absl::string_view algorithm,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070078 unsigned char* digest,
79 size_t size,
80 size_t* length) const = 0;
81
82 // Returns the time in seconds relative to epoch, 1970-01-01T00:00:00Z (UTC),
83 // or -1 if an expiration time could not be retrieved.
84 virtual int64_t CertificateExpirationTime() const = 0;
85
86 // Gets information (fingerprint, etc.) about this certificate. This is used
87 // for certificate stats, see
88 // https://w3c.github.io/webrtc-stats/#certificatestats-dict*.
89 std::unique_ptr<SSLCertificateStats> GetStats() const;
90};
91
92// SSLCertChain is a simple wrapper for a vector of SSLCertificates. It serves
93// primarily to ensure proper memory management (especially deletion) of the
94// SSLCertificate pointers.
Mirko Bonadei35214fc2019-09-23 14:54:28 +020095class RTC_EXPORT SSLCertChain final {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070096 public:
Steve Antonf25303e2018-10-16 15:23:31 -070097 explicit SSLCertChain(std::unique_ptr<SSLCertificate> single_cert);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070098 explicit SSLCertChain(std::vector<std::unique_ptr<SSLCertificate>> certs);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070099 // Allow move semantics for the object.
100 SSLCertChain(SSLCertChain&&);
101 SSLCertChain& operator=(SSLCertChain&&);
102
103 ~SSLCertChain();
104
Byoungchan Lee14af7622022-01-12 05:24:58 +0900105 SSLCertChain(const SSLCertChain&) = delete;
106 SSLCertChain& operator=(const SSLCertChain&) = delete;
107
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700108 // Vector access methods.
109 size_t GetSize() const { return certs_.size(); }
110
111 // Returns a temporary reference, only valid until the chain is destroyed.
112 const SSLCertificate& Get(size_t pos) const { return *(certs_[pos]); }
113
114 // Returns a new SSLCertChain object instance wrapping the same underlying
Steve Antonf25303e2018-10-16 15:23:31 -0700115 // certificate chain.
116 std::unique_ptr<SSLCertChain> Clone() const;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700117
118 // Gets information (fingerprint, etc.) about this certificate chain. This is
119 // used for certificate stats, see
120 // https://w3c.github.io/webrtc-stats/#certificatestats-dict*.
121 std::unique_ptr<SSLCertificateStats> GetStats() const;
122
123 private:
124 std::vector<std::unique_ptr<SSLCertificate>> certs_;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700125};
126
127// SSLCertificateVerifier provides a simple interface to allow third parties to
128// define their own certificate verification code. It is completely independent
129// from the underlying SSL implementation.
130class SSLCertificateVerifier {
131 public:
132 virtual ~SSLCertificateVerifier() = default;
133 // Returns true if the certificate is valid, else false. It is up to the
134 // implementer to define what a valid certificate looks like.
135 virtual bool Verify(const SSLCertificate& certificate) = 0;
136};
137
138} // namespace rtc
139
Steve Anton10542f22019-01-11 09:11:00 -0800140#endif // RTC_BASE_SSL_CERTIFICATE_H_