blob: 3b3f24fb91b0f046b63e8aa7b52a94aa32cb6fe3 [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>
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070020#include <memory>
21#include <string>
22#include <vector>
23
24#include "rtc_base/buffer.h"
Steve Anton10542f22019-01-11 09:11:00 -080025#include "rtc_base/constructor_magic.h"
Mirko Bonadei35214fc2019-09-23 14:54:28 +020026#include "rtc_base/system/rtc_export.h"
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070027
28namespace rtc {
29
Mirko Bonadei35214fc2019-09-23 14:54:28 +020030struct RTC_EXPORT SSLCertificateStats {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070031 SSLCertificateStats(std::string&& fingerprint,
32 std::string&& fingerprint_algorithm,
33 std::string&& base64_certificate,
Steve Antonf25303e2018-10-16 15:23:31 -070034 std::unique_ptr<SSLCertificateStats> issuer);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070035 ~SSLCertificateStats();
36 std::string fingerprint;
37 std::string fingerprint_algorithm;
38 std::string base64_certificate;
39 std::unique_ptr<SSLCertificateStats> issuer;
40};
41
42// Abstract interface overridden by SSL library specific
43// implementations.
44
45// A somewhat opaque type used to encapsulate a certificate.
46// Wraps the SSL library's notion of a certificate, with reference counting.
47// The SSLCertificate object is pretty much immutable once created.
48// (The OpenSSL implementation only does reference counting and
49// possibly caching of intermediate results.)
Mirko Bonadei35214fc2019-09-23 14:54:28 +020050class RTC_EXPORT SSLCertificate {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070051 public:
52 // Parses and builds a certificate from a PEM encoded string.
53 // Returns null on failure.
54 // The length of the string representation of the certificate is
55 // stored in *pem_length if it is non-null, and only if
56 // parsing was successful.
Steve Antonf25303e2018-10-16 15:23:31 -070057 static std::unique_ptr<SSLCertificate> FromPEMString(
58 const std::string& pem_string);
59 virtual ~SSLCertificate() = default;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070060
61 // Returns a new SSLCertificate object instance wrapping the same
Steve Antonf25303e2018-10-16 15:23:31 -070062 // underlying certificate, including its chain if present.
63 virtual std::unique_ptr<SSLCertificate> Clone() const = 0;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070064
65 // Returns a PEM encoded string representation of the certificate.
66 virtual std::string ToPEMString() const = 0;
67
68 // Provides a DER encoded binary representation of the certificate.
69 virtual void ToDER(Buffer* der_buffer) const = 0;
70
71 // Gets the name of the digest algorithm that was used to compute this
72 // certificate's signature.
73 virtual bool GetSignatureDigestAlgorithm(std::string* algorithm) const = 0;
74
75 // Compute the digest of the certificate given algorithm
76 virtual bool ComputeDigest(const std::string& algorithm,
77 unsigned char* digest,
78 size_t size,
79 size_t* length) const = 0;
80
81 // Returns the time in seconds relative to epoch, 1970-01-01T00:00:00Z (UTC),
82 // or -1 if an expiration time could not be retrieved.
83 virtual int64_t CertificateExpirationTime() const = 0;
84
85 // Gets information (fingerprint, etc.) about this certificate. This is used
86 // for certificate stats, see
87 // https://w3c.github.io/webrtc-stats/#certificatestats-dict*.
88 std::unique_ptr<SSLCertificateStats> GetStats() const;
89};
90
91// SSLCertChain is a simple wrapper for a vector of SSLCertificates. It serves
92// primarily to ensure proper memory management (especially deletion) of the
93// SSLCertificate pointers.
Mirko Bonadei35214fc2019-09-23 14:54:28 +020094class RTC_EXPORT SSLCertChain final {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070095 public:
Steve Antonf25303e2018-10-16 15:23:31 -070096 explicit SSLCertChain(std::unique_ptr<SSLCertificate> single_cert);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070097 explicit SSLCertChain(std::vector<std::unique_ptr<SSLCertificate>> certs);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070098 // Allow move semantics for the object.
99 SSLCertChain(SSLCertChain&&);
100 SSLCertChain& operator=(SSLCertChain&&);
101
102 ~SSLCertChain();
103
104 // Vector access methods.
105 size_t GetSize() const { return certs_.size(); }
106
107 // Returns a temporary reference, only valid until the chain is destroyed.
108 const SSLCertificate& Get(size_t pos) const { return *(certs_[pos]); }
109
110 // Returns a new SSLCertChain object instance wrapping the same underlying
Steve Antonf25303e2018-10-16 15:23:31 -0700111 // certificate chain.
112 std::unique_ptr<SSLCertChain> Clone() const;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700113
114 // Gets information (fingerprint, etc.) about this certificate chain. This is
115 // used for certificate stats, see
116 // https://w3c.github.io/webrtc-stats/#certificatestats-dict*.
117 std::unique_ptr<SSLCertificateStats> GetStats() const;
118
119 private:
120 std::vector<std::unique_ptr<SSLCertificate>> certs_;
121
122 RTC_DISALLOW_COPY_AND_ASSIGN(SSLCertChain);
123};
124
125// SSLCertificateVerifier provides a simple interface to allow third parties to
126// define their own certificate verification code. It is completely independent
127// from the underlying SSL implementation.
128class SSLCertificateVerifier {
129 public:
130 virtual ~SSLCertificateVerifier() = default;
131 // Returns true if the certificate is valid, else false. It is up to the
132 // implementer to define what a valid certificate looks like.
133 virtual bool Verify(const SSLCertificate& certificate) = 0;
134};
135
136} // namespace rtc
137
Steve Anton10542f22019-01-11 09:11:00 -0800138#endif // RTC_BASE_SSL_CERTIFICATE_H_