mirror of
https://github.com/mollyim/webrtc.git
synced 2025-05-14 06:10:40 +01:00

This extends the API surface so that custom certificates can be provided by an API user in both the standalone and factory creation paths for the OpenSSLAdapter. Prior to this change the SSL roots were hardcoded in a header file and directly included into openssladapter.cc. This forces the 100 kilobytes of certificates to always be compiled into the library. This is undesirable in certain linking cases where these certificates can be shared from another binary that already has an equivalent set of trusted roots hard coded into the binary. Support for removing the hard coded SSL roots has also been added through a new build flag. By default the hard coded SSL roots will be included and will be used if no other trusted root certificates are provided. The main goal of this CL is to reduce total binary size requirements of WebRTC by about 100kb in certain applications where adding these certificates is redundant. Change-Id: Ifd36d92b5cb32d1b3098a61ddfc244d76df8f30f Bug: chromium:526260 Change-Id: Ifd36d92b5cb32d1b3098a61ddfc244d76df8f30f Reviewed-on: https://webrtc-review.googlesource.com/64841 Commit-Queue: Benjamin Wright <benwright@webrtc.org> Reviewed-by: Karl Wiberg <kwiberg@webrtc.org> Reviewed-by: Taylor Brandstetter <deadbeef@webrtc.org> Cr-Commit-Position: refs/heads/master@{#23180}
147 lines
5.6 KiB
C++
147 lines
5.6 KiB
C++
/*
|
|
* Copyright 2018 The WebRTC Project Authors. All rights reserved.
|
|
*
|
|
* Use of this source code is governed by a BSD-style license
|
|
* that can be found in the LICENSE file in the root of the source
|
|
* tree. An additional intellectual property rights grant can be found
|
|
* in the file PATENTS. All contributing project authors may
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
*/
|
|
|
|
// Generic interface for SSL Certificates, used in both the SSLAdapter
|
|
// for TLS TURN connections and the SSLStreamAdapter for DTLS Peer to Peer
|
|
// Connections for SRTP Key negotiation and SCTP encryption.
|
|
|
|
#ifndef RTC_BASE_SSLCERTIFICATE_H_
|
|
#define RTC_BASE_SSLCERTIFICATE_H_
|
|
|
|
#include <algorithm>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "rtc_base/buffer.h"
|
|
#include "rtc_base/constructormagic.h"
|
|
#include "rtc_base/messagedigest.h"
|
|
#include "rtc_base/timeutils.h"
|
|
|
|
namespace rtc {
|
|
|
|
struct SSLCertificateStats {
|
|
SSLCertificateStats(std::string&& fingerprint,
|
|
std::string&& fingerprint_algorithm,
|
|
std::string&& base64_certificate,
|
|
std::unique_ptr<SSLCertificateStats>&& issuer);
|
|
~SSLCertificateStats();
|
|
std::string fingerprint;
|
|
std::string fingerprint_algorithm;
|
|
std::string base64_certificate;
|
|
std::unique_ptr<SSLCertificateStats> issuer;
|
|
};
|
|
|
|
// Abstract interface overridden by SSL library specific
|
|
// implementations.
|
|
|
|
// A somewhat opaque type used to encapsulate a certificate.
|
|
// Wraps the SSL library's notion of a certificate, with reference counting.
|
|
// The SSLCertificate object is pretty much immutable once created.
|
|
// (The OpenSSL implementation only does reference counting and
|
|
// possibly caching of intermediate results.)
|
|
class SSLCertificate {
|
|
public:
|
|
// Parses and builds a certificate from a PEM encoded string.
|
|
// Returns null on failure.
|
|
// The length of the string representation of the certificate is
|
|
// stored in *pem_length if it is non-null, and only if
|
|
// parsing was successful.
|
|
// Caller is responsible for freeing the returned object.
|
|
static SSLCertificate* FromPEMString(const std::string& pem_string);
|
|
virtual ~SSLCertificate() {}
|
|
|
|
// Returns a new SSLCertificate object instance wrapping the same
|
|
// underlying certificate, including its chain if present. Caller is
|
|
// responsible for freeing the returned object. Use GetUniqueReference
|
|
// instead.
|
|
virtual SSLCertificate* GetReference() const = 0;
|
|
|
|
std::unique_ptr<SSLCertificate> GetUniqueReference() const;
|
|
|
|
// Returns a PEM encoded string representation of the certificate.
|
|
virtual std::string ToPEMString() const = 0;
|
|
|
|
// Provides a DER encoded binary representation of the certificate.
|
|
virtual void ToDER(Buffer* der_buffer) const = 0;
|
|
|
|
// Gets the name of the digest algorithm that was used to compute this
|
|
// certificate's signature.
|
|
virtual bool GetSignatureDigestAlgorithm(std::string* algorithm) const = 0;
|
|
|
|
// Compute the digest of the certificate given algorithm
|
|
virtual bool ComputeDigest(const std::string& algorithm,
|
|
unsigned char* digest,
|
|
size_t size,
|
|
size_t* length) const = 0;
|
|
|
|
// Returns the time in seconds relative to epoch, 1970-01-01T00:00:00Z (UTC),
|
|
// or -1 if an expiration time could not be retrieved.
|
|
virtual int64_t CertificateExpirationTime() const = 0;
|
|
|
|
// Gets information (fingerprint, etc.) about this certificate. This is used
|
|
// for certificate stats, see
|
|
// https://w3c.github.io/webrtc-stats/#certificatestats-dict*.
|
|
std::unique_ptr<SSLCertificateStats> GetStats() const;
|
|
};
|
|
|
|
// SSLCertChain is a simple wrapper for a vector of SSLCertificates. It serves
|
|
// primarily to ensure proper memory management (especially deletion) of the
|
|
// SSLCertificate pointers.
|
|
class SSLCertChain {
|
|
public:
|
|
explicit SSLCertChain(std::vector<std::unique_ptr<SSLCertificate>> certs);
|
|
// These constructors copy the provided SSLCertificate(s), so the caller
|
|
// retains ownership.
|
|
explicit SSLCertChain(const std::vector<SSLCertificate*>& certs);
|
|
explicit SSLCertChain(const SSLCertificate* cert);
|
|
// Allow move semantics for the object.
|
|
SSLCertChain(SSLCertChain&&);
|
|
SSLCertChain& operator=(SSLCertChain&&);
|
|
|
|
~SSLCertChain();
|
|
|
|
// Vector access methods.
|
|
size_t GetSize() const { return certs_.size(); }
|
|
|
|
// Returns a temporary reference, only valid until the chain is destroyed.
|
|
const SSLCertificate& Get(size_t pos) const { return *(certs_[pos]); }
|
|
|
|
// Returns a new SSLCertChain object instance wrapping the same underlying
|
|
// certificate chain. Caller is responsible for freeing the returned object.
|
|
SSLCertChain* Copy() const;
|
|
// Same as above, but returning a unique_ptr for convenience.
|
|
std::unique_ptr<SSLCertChain> UniqueCopy() const;
|
|
|
|
// Gets information (fingerprint, etc.) about this certificate chain. This is
|
|
// used for certificate stats, see
|
|
// https://w3c.github.io/webrtc-stats/#certificatestats-dict*.
|
|
std::unique_ptr<SSLCertificateStats> GetStats() const;
|
|
|
|
private:
|
|
std::vector<std::unique_ptr<SSLCertificate>> certs_;
|
|
|
|
RTC_DISALLOW_COPY_AND_ASSIGN(SSLCertChain);
|
|
};
|
|
|
|
// SSLCertificateVerifier provides a simple interface to allow third parties to
|
|
// define their own certificate verification code. It is completely independent
|
|
// from the underlying SSL implementation.
|
|
class SSLCertificateVerifier {
|
|
public:
|
|
virtual ~SSLCertificateVerifier() = default;
|
|
// Returns true if the certificate is valid, else false. It is up to the
|
|
// implementer to define what a valid certificate looks like.
|
|
virtual bool Verify(const SSLCertificate& certificate) = 0;
|
|
};
|
|
|
|
} // namespace rtc
|
|
|
|
#endif // RTC_BASE_SSLCERTIFICATE_H_
|