blob: fad1404a8f04f7983a857bff7de52a52faa08863 [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"
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070026
27namespace rtc {
28
29struct SSLCertificateStats {
30 SSLCertificateStats(std::string&& fingerprint,
31 std::string&& fingerprint_algorithm,
32 std::string&& base64_certificate,
Steve Antonf25303e2018-10-16 15:23:31 -070033 std::unique_ptr<SSLCertificateStats> issuer);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070034 ~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.)
49class 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 Antonf25303e2018-10-16 15:23:31 -070056 static std::unique_ptr<SSLCertificate> FromPEMString(
57 const std::string& pem_string);
58 virtual ~SSLCertificate() = default;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070059
60 // Returns a new SSLCertificate object instance wrapping the same
Steve Antonf25303e2018-10-16 15:23:31 -070061 // underlying certificate, including its chain if present.
62 virtual std::unique_ptr<SSLCertificate> Clone() const = 0;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070063
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 Wright26165942018-10-26 15:04:32 -070093class SSLCertChain final {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070094 public:
Steve Antonf25303e2018-10-16 15:23:31 -070095 explicit SSLCertChain(std::unique_ptr<SSLCertificate> single_cert);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070096 explicit SSLCertChain(std::vector<std::unique_ptr<SSLCertificate>> certs);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070097 // 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 Antonf25303e2018-10-16 15:23:31 -0700110 // certificate chain.
111 std::unique_ptr<SSLCertChain> Clone() const;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700112
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.
127class 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
Steve Anton10542f22019-01-11 09:11:00 -0800137#endif // RTC_BASE_SSL_CERTIFICATE_H_