blob: 029404cf3ed390956e5ae7a83e4b269bcb9973a1 [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,
Niklas Enbom82c71af2018-10-15 16:22:55 +000031 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.
Niklas Enbom82c71af2018-10-15 16:22:55 +000054 // Caller is responsible for freeing the returned object.
55 static SSLCertificate* FromPEMString(const std::string& pem_string);
56 virtual ~SSLCertificate() {}
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070057
58 // Returns a new SSLCertificate object instance wrapping the same
Niklas Enbom82c71af2018-10-15 16:22:55 +000059 // underlying certificate, including its chain if present. Caller is
60 // responsible for freeing the returned object. Use GetUniqueReference
61 // instead.
62 virtual SSLCertificate* GetReference() const = 0;
63
64 std::unique_ptr<SSLCertificate> GetUniqueReference() const;
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
77 virtual bool ComputeDigest(const std::string& algorithm,
78 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.
95class SSLCertChain {
96 public:
97 explicit SSLCertChain(std::vector<std::unique_ptr<SSLCertificate>> certs);
Niklas Enbom82c71af2018-10-15 16:22:55 +000098 // These constructors copy the provided SSLCertificate(s), so the caller
99 // retains ownership.
100 explicit SSLCertChain(const std::vector<SSLCertificate*>& certs);
101 explicit SSLCertChain(const SSLCertificate* cert);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700102 // Allow move semantics for the object.
103 SSLCertChain(SSLCertChain&&);
104 SSLCertChain& operator=(SSLCertChain&&);
105
106 ~SSLCertChain();
107
108 // 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
Niklas Enbom82c71af2018-10-15 16:22:55 +0000115 // certificate chain. Caller is responsible for freeing the returned object.
116 SSLCertChain* Copy() const;
117 // Same as above, but returning a unique_ptr for convenience.
118 std::unique_ptr<SSLCertChain> UniqueCopy() const;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700119
120 // Gets information (fingerprint, etc.) about this certificate chain. This is
121 // used for certificate stats, see
122 // https://w3c.github.io/webrtc-stats/#certificatestats-dict*.
123 std::unique_ptr<SSLCertificateStats> GetStats() const;
124
125 private:
126 std::vector<std::unique_ptr<SSLCertificate>> certs_;
127
128 RTC_DISALLOW_COPY_AND_ASSIGN(SSLCertChain);
129};
130
131// SSLCertificateVerifier provides a simple interface to allow third parties to
132// define their own certificate verification code. It is completely independent
133// from the underlying SSL implementation.
134class SSLCertificateVerifier {
135 public:
136 virtual ~SSLCertificateVerifier() = default;
137 // Returns true if the certificate is valid, else false. It is up to the
138 // implementer to define what a valid certificate looks like.
139 virtual bool Verify(const SSLCertificate& certificate) = 0;
140};
141
142} // namespace rtc
143
144#endif // RTC_BASE_SSLCERTIFICATE_H_