blob: 29c4db58ef56c80035e832cd242cad72e9dc8dd4 [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
15#ifndef RTC_BASE_SSLCERTIFICATE_H_
16#define RTC_BASE_SSLCERTIFICATE_H_
17
18#include <algorithm>
19#include <memory>
20#include <string>
21#include <vector>
22
23#include "rtc_base/buffer.h"
24#include "rtc_base/constructormagic.h"
25#include "rtc_base/messagedigest.h"
26#include "rtc_base/timeutils.h"
27
28namespace rtc {
29
30struct SSLCertificateStats {
31 SSLCertificateStats(std::string&& fingerprint,
32 std::string&& fingerprint_algorithm,
33 std::string&& base64_certificate,
34 std::unique_ptr<SSLCertificateStats>&& issuer);
35 ~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.)
50class SSLCertificate {
51 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.
57 // Caller is responsible for freeing the returned object.
58 static SSLCertificate* FromPEMString(const std::string& pem_string);
59 virtual ~SSLCertificate() {}
60
61 // Returns a new SSLCertificate object instance wrapping the same
62 // underlying certificate, including its chain if present. Caller is
63 // responsible for freeing the returned object. Use GetUniqueReference
64 // instead.
65 virtual SSLCertificate* GetReference() const = 0;
66
67 std::unique_ptr<SSLCertificate> GetUniqueReference() const;
68
69 // Returns a PEM encoded string representation of the certificate.
70 virtual std::string ToPEMString() const = 0;
71
72 // Provides a DER encoded binary representation of the certificate.
73 virtual void ToDER(Buffer* der_buffer) const = 0;
74
75 // Gets the name of the digest algorithm that was used to compute this
76 // certificate's signature.
77 virtual bool GetSignatureDigestAlgorithm(std::string* algorithm) const = 0;
78
79 // Compute the digest of the certificate given algorithm
80 virtual bool ComputeDigest(const std::string& algorithm,
81 unsigned char* digest,
82 size_t size,
83 size_t* length) const = 0;
84
85 // Returns the time in seconds relative to epoch, 1970-01-01T00:00:00Z (UTC),
86 // or -1 if an expiration time could not be retrieved.
87 virtual int64_t CertificateExpirationTime() const = 0;
88
89 // Gets information (fingerprint, etc.) about this certificate. This is used
90 // for certificate stats, see
91 // https://w3c.github.io/webrtc-stats/#certificatestats-dict*.
92 std::unique_ptr<SSLCertificateStats> GetStats() const;
93};
94
95// SSLCertChain is a simple wrapper for a vector of SSLCertificates. It serves
96// primarily to ensure proper memory management (especially deletion) of the
97// SSLCertificate pointers.
98class SSLCertChain {
99 public:
100 explicit SSLCertChain(std::vector<std::unique_ptr<SSLCertificate>> certs);
101 // These constructors copy the provided SSLCertificate(s), so the caller
102 // retains ownership.
103 explicit SSLCertChain(const std::vector<SSLCertificate*>& certs);
104 explicit SSLCertChain(const SSLCertificate* cert);
105 // Allow move semantics for the object.
106 SSLCertChain(SSLCertChain&&);
107 SSLCertChain& operator=(SSLCertChain&&);
108
109 ~SSLCertChain();
110
111 // Vector access methods.
112 size_t GetSize() const { return certs_.size(); }
113
114 // Returns a temporary reference, only valid until the chain is destroyed.
115 const SSLCertificate& Get(size_t pos) const { return *(certs_[pos]); }
116
117 // Returns a new SSLCertChain object instance wrapping the same underlying
118 // certificate chain. Caller is responsible for freeing the returned object.
119 SSLCertChain* Copy() const;
120 // Same as above, but returning a unique_ptr for convenience.
121 std::unique_ptr<SSLCertChain> UniqueCopy() const;
122
123 // Gets information (fingerprint, etc.) about this certificate chain. This is
124 // used for certificate stats, see
125 // https://w3c.github.io/webrtc-stats/#certificatestats-dict*.
126 std::unique_ptr<SSLCertificateStats> GetStats() const;
127
128 private:
129 std::vector<std::unique_ptr<SSLCertificate>> certs_;
130
131 RTC_DISALLOW_COPY_AND_ASSIGN(SSLCertChain);
132};
133
134// SSLCertificateVerifier provides a simple interface to allow third parties to
135// define their own certificate verification code. It is completely independent
136// from the underlying SSL implementation.
137class SSLCertificateVerifier {
138 public:
139 virtual ~SSLCertificateVerifier() = default;
140 // Returns true if the certificate is valid, else false. It is up to the
141 // implementer to define what a valid certificate looks like.
142 virtual bool Verify(const SSLCertificate& certificate) = 0;
143};
144
145} // namespace rtc
146
147#endif // RTC_BASE_SSLCERTIFICATE_H_