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