henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 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 | // Handling of certificates and keypairs for SSLStreamAdapter's peer mode. |
| 12 | |
| 13 | #ifndef WEBRTC_BASE_SSLIDENTITY_H_ |
| 14 | #define WEBRTC_BASE_SSLIDENTITY_H_ |
| 15 | |
| 16 | #include <algorithm> |
| 17 | #include <string> |
| 18 | #include <vector> |
| 19 | |
| 20 | #include "webrtc/base/buffer.h" |
| 21 | #include "webrtc/base/messagedigest.h" |
| 22 | |
| 23 | namespace rtc { |
| 24 | |
Henrik Boström | 5b4ce33 | 2015-08-05 16:55:22 +0200 | [diff] [blame] | 25 | enum KeyType { KT_RSA, KT_ECDSA, KT_LAST, KT_DEFAULT = KT_RSA }; |
| 26 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 27 | // Forward declaration due to circular dependency with SSLCertificate. |
| 28 | class SSLCertChain; |
| 29 | |
| 30 | // Abstract interface overridden by SSL library specific |
| 31 | // implementations. |
| 32 | |
| 33 | // A somewhat opaque type used to encapsulate a certificate. |
| 34 | // Wraps the SSL library's notion of a certificate, with reference counting. |
| 35 | // The SSLCertificate object is pretty much immutable once created. |
| 36 | // (The OpenSSL implementation only does reference counting and |
| 37 | // possibly caching of intermediate results.) |
| 38 | class SSLCertificate { |
| 39 | public: |
| 40 | // Parses and build a certificate from a PEM encoded string. |
| 41 | // Returns NULL on failure. |
| 42 | // The length of the string representation of the certificate is |
| 43 | // stored in *pem_length if it is non-NULL, and only if |
| 44 | // parsing was successful. |
| 45 | // Caller is responsible for freeing the returned object. |
| 46 | static SSLCertificate* FromPEMString(const std::string& pem_string); |
| 47 | virtual ~SSLCertificate() {} |
| 48 | |
| 49 | // Returns a new SSLCertificate object instance wrapping the same |
| 50 | // underlying certificate, including its chain if present. |
| 51 | // Caller is responsible for freeing the returned object. |
| 52 | virtual SSLCertificate* GetReference() const = 0; |
| 53 | |
| 54 | // Provides the cert chain, or returns false. The caller owns the chain. |
| 55 | // The chain includes a copy of each certificate, excluding the leaf. |
| 56 | virtual bool GetChain(SSLCertChain** chain) const = 0; |
| 57 | |
| 58 | // Returns a PEM encoded string representation of the certificate. |
| 59 | virtual std::string ToPEMString() const = 0; |
| 60 | |
| 61 | // Provides a DER encoded binary representation of the certificate. |
| 62 | virtual void ToDER(Buffer* der_buffer) const = 0; |
| 63 | |
| 64 | // Gets the name of the digest algorithm that was used to compute this |
| 65 | // certificate's signature. |
| 66 | virtual bool GetSignatureDigestAlgorithm(std::string* algorithm) const = 0; |
| 67 | |
| 68 | // Compute the digest of the certificate given algorithm |
| 69 | virtual bool ComputeDigest(const std::string& algorithm, |
| 70 | unsigned char* digest, |
| 71 | size_t size, |
| 72 | size_t* length) const = 0; |
| 73 | }; |
| 74 | |
| 75 | // SSLCertChain is a simple wrapper for a vector of SSLCertificates. It serves |
| 76 | // primarily to ensure proper memory management (especially deletion) of the |
| 77 | // SSLCertificate pointers. |
| 78 | class SSLCertChain { |
| 79 | public: |
| 80 | // These constructors copy the provided SSLCertificate(s), so the caller |
| 81 | // retains ownership. |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 82 | explicit SSLCertChain(const std::vector<SSLCertificate*>& certs); |
| 83 | explicit SSLCertChain(const SSLCertificate* cert); |
| 84 | ~SSLCertChain(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 85 | |
| 86 | // Vector access methods. |
| 87 | size_t GetSize() const { return certs_.size(); } |
| 88 | |
| 89 | // Returns a temporary reference, only valid until the chain is destroyed. |
| 90 | const SSLCertificate& Get(size_t pos) const { return *(certs_[pos]); } |
| 91 | |
| 92 | // Returns a new SSLCertChain object instance wrapping the same underlying |
| 93 | // certificate chain. Caller is responsible for freeing the returned object. |
| 94 | SSLCertChain* Copy() const { |
| 95 | return new SSLCertChain(certs_); |
| 96 | } |
| 97 | |
| 98 | private: |
| 99 | // Helper function for duplicating a vector of certificates. |
| 100 | static SSLCertificate* DupCert(const SSLCertificate* cert) { |
| 101 | return cert->GetReference(); |
| 102 | } |
| 103 | |
| 104 | // Helper function for deleting a vector of certificates. |
| 105 | static void DeleteCert(SSLCertificate* cert) { delete cert; } |
| 106 | |
| 107 | std::vector<SSLCertificate*> certs_; |
| 108 | |
| 109 | DISALLOW_COPY_AND_ASSIGN(SSLCertChain); |
| 110 | }; |
| 111 | |
| 112 | // Parameters for generating an identity for testing. If common_name is |
| 113 | // non-empty, it will be used for the certificate's subject and issuer name, |
| 114 | // otherwise a random string will be used. |not_before| and |not_after| are |
| 115 | // offsets to the current time in number of seconds. |
| 116 | struct SSLIdentityParams { |
| 117 | std::string common_name; |
| 118 | int not_before; // in seconds. |
| 119 | int not_after; // in seconds. |
| 120 | }; |
| 121 | |
| 122 | // Our identity in an SSL negotiation: a keypair and certificate (both |
| 123 | // with the same public key). |
| 124 | // This too is pretty much immutable once created. |
| 125 | class SSLIdentity { |
| 126 | public: |
| 127 | // Generates an identity (keypair and self-signed certificate). If |
| 128 | // common_name is non-empty, it will be used for the certificate's |
| 129 | // subject and issuer name, otherwise a random string will be used. |
| 130 | // Returns NULL on failure. |
| 131 | // Caller is responsible for freeing the returned object. |
| 132 | static SSLIdentity* Generate(const std::string& common_name); |
| 133 | |
| 134 | // Generates an identity with the specified validity period. |
| 135 | static SSLIdentity* GenerateForTest(const SSLIdentityParams& params); |
| 136 | |
| 137 | // Construct an identity from a private key and a certificate. |
| 138 | static SSLIdentity* FromPEMStrings(const std::string& private_key, |
| 139 | const std::string& certificate); |
| 140 | |
| 141 | virtual ~SSLIdentity() {} |
| 142 | |
| 143 | // Returns a new SSLIdentity object instance wrapping the same |
| 144 | // identity information. |
| 145 | // Caller is responsible for freeing the returned object. |
| 146 | virtual SSLIdentity* GetReference() const = 0; |
| 147 | |
| 148 | // Returns a temporary reference to the certificate. |
| 149 | virtual const SSLCertificate& certificate() const = 0; |
| 150 | |
| 151 | // Helpers for parsing converting between PEM and DER format. |
| 152 | static bool PemToDer(const std::string& pem_type, |
| 153 | const std::string& pem_string, |
| 154 | std::string* der); |
| 155 | static std::string DerToPem(const std::string& pem_type, |
| 156 | const unsigned char* data, |
| 157 | size_t length); |
| 158 | }; |
| 159 | |
| 160 | extern const char kPemTypeCertificate[]; |
| 161 | extern const char kPemTypeRsaPrivateKey[]; |
| 162 | |
| 163 | } // namespace rtc |
| 164 | |
| 165 | #endif // WEBRTC_BASE_SSLIDENTITY_H_ |