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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 13 | #ifndef RTC_BASE_SSL_IDENTITY_H_ |
| 14 | #define RTC_BASE_SSL_IDENTITY_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 15 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 16 | #include <stdint.h> |
Yves Gerey | 2e00abc | 2018-10-05 15:39:24 +0200 | [diff] [blame] | 17 | #include <ctime> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 18 | #include <string> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 19 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 20 | namespace rtc { |
| 21 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 22 | class SSLCertChain; |
| 23 | class SSLCertificate; |
| 24 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 25 | // KT_LAST is intended for vector declarations and loops over all key types; |
| 26 | // it does not represent any key type in itself. |
| 27 | // KT_DEFAULT is used as the default KeyType for KeyParams. |
| 28 | enum KeyType { KT_RSA, KT_ECDSA, KT_LAST, KT_DEFAULT = KT_ECDSA }; |
| 29 | |
| 30 | static const int kRsaDefaultModSize = 1024; |
| 31 | static const int kRsaDefaultExponent = 0x10001; // = 2^16+1 = 65537 |
| 32 | static const int kRsaMinModSize = 1024; |
| 33 | static const int kRsaMaxModSize = 8192; |
| 34 | |
| 35 | // Certificate default validity lifetime. |
| 36 | static const int kDefaultCertificateLifetimeInSeconds = |
| 37 | 60 * 60 * 24 * 30; // 30 days |
| 38 | // Certificate validity window. |
| 39 | // This is to compensate for slightly incorrect system clocks. |
| 40 | static const int kCertificateWindowInSeconds = -60 * 60 * 24; |
| 41 | |
| 42 | struct RSAParams { |
| 43 | unsigned int mod_size; |
| 44 | unsigned int pub_exp; |
| 45 | }; |
| 46 | |
| 47 | enum ECCurve { EC_NIST_P256, /* EC_FANCY, */ EC_LAST }; |
| 48 | |
| 49 | class KeyParams { |
| 50 | public: |
| 51 | // Generate a KeyParams object from a simple KeyType, using default params. |
| 52 | explicit KeyParams(KeyType key_type = KT_DEFAULT); |
| 53 | |
| 54 | // Generate a a KeyParams for RSA with explicit parameters. |
| 55 | static KeyParams RSA(int mod_size = kRsaDefaultModSize, |
| 56 | int pub_exp = kRsaDefaultExponent); |
| 57 | |
| 58 | // Generate a a KeyParams for ECDSA specifying the curve. |
| 59 | static KeyParams ECDSA(ECCurve curve = EC_NIST_P256); |
| 60 | |
| 61 | // Check validity of a KeyParams object. Since the factory functions have |
| 62 | // no way of returning errors, this function can be called after creation |
| 63 | // to make sure the parameters are OK. |
| 64 | bool IsValid() const; |
| 65 | |
| 66 | RSAParams rsa_params() const; |
| 67 | |
| 68 | ECCurve ec_curve() const; |
| 69 | |
| 70 | KeyType type() const { return type_; } |
| 71 | |
| 72 | private: |
| 73 | KeyType type_; |
| 74 | union { |
| 75 | RSAParams rsa; |
| 76 | ECCurve curve; |
| 77 | } params_; |
| 78 | }; |
| 79 | |
| 80 | // TODO(hbos): Remove once rtc::KeyType (to be modified) and |
| 81 | // blink::WebRTCKeyType (to be landed) match. By using this function in Chromium |
| 82 | // appropriately we can change KeyType enum -> class without breaking Chromium. |
| 83 | KeyType IntKeyTypeFamilyToKeyType(int key_type_family); |
| 84 | |
| 85 | // Parameters for generating a certificate. If |common_name| is non-empty, it |
| 86 | // will be used for the certificate's subject and issuer name, otherwise a |
| 87 | // random string will be used. |
| 88 | struct SSLIdentityParams { |
| 89 | std::string common_name; |
| 90 | time_t not_before; // Absolute time since epoch in seconds. |
| 91 | time_t not_after; // Absolute time since epoch in seconds. |
| 92 | KeyParams key_params; |
| 93 | }; |
| 94 | |
| 95 | // Our identity in an SSL negotiation: a keypair and certificate (both |
| 96 | // with the same public key). |
| 97 | // This too is pretty much immutable once created. |
| 98 | class SSLIdentity { |
| 99 | public: |
| 100 | // Generates an identity (keypair and self-signed certificate). If |
| 101 | // |common_name| is non-empty, it will be used for the certificate's subject |
| 102 | // and issuer name, otherwise a random string will be used. The key type and |
| 103 | // parameters are defined in |key_param|. The certificate's lifetime in |
| 104 | // seconds from the current time is defined in |certificate_lifetime|; it |
| 105 | // should be a non-negative number. |
| 106 | // Returns null on failure. |
| 107 | // Caller is responsible for freeing the returned object. |
| 108 | static SSLIdentity* GenerateWithExpiration(const std::string& common_name, |
| 109 | const KeyParams& key_param, |
| 110 | time_t certificate_lifetime); |
| 111 | static SSLIdentity* Generate(const std::string& common_name, |
| 112 | const KeyParams& key_param); |
| 113 | static SSLIdentity* Generate(const std::string& common_name, |
| 114 | KeyType key_type); |
| 115 | |
| 116 | // Generates an identity with the specified validity period. |
| 117 | // TODO(torbjorng): Now that Generate() accepts relevant params, make tests |
| 118 | // use that instead of this function. |
| 119 | static SSLIdentity* GenerateForTest(const SSLIdentityParams& params); |
| 120 | |
| 121 | // Construct an identity from a private key and a certificate. |
| 122 | static SSLIdentity* FromPEMStrings(const std::string& private_key, |
| 123 | const std::string& certificate); |
| 124 | |
Jian Cui | 0a8798b | 2017-11-16 16:58:02 -0800 | [diff] [blame] | 125 | // Construct an identity from a private key and a certificate chain. |
| 126 | static SSLIdentity* FromPEMChainStrings(const std::string& private_key, |
| 127 | const std::string& certificate_chain); |
| 128 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 129 | virtual ~SSLIdentity() {} |
| 130 | |
| 131 | // Returns a new SSLIdentity object instance wrapping the same |
| 132 | // identity information. |
| 133 | // Caller is responsible for freeing the returned object. |
| 134 | // TODO(hbos,torbjorng): Rename to a less confusing name. |
| 135 | virtual SSLIdentity* GetReference() const = 0; |
| 136 | |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 137 | // Returns a temporary reference to the end-entity (leaf) certificate. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 138 | virtual const SSLCertificate& certificate() const = 0; |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 139 | // Returns a temporary reference to the entire certificate chain. |
| 140 | virtual const SSLCertChain& cert_chain() const = 0; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 141 | virtual std::string PrivateKeyToPEMString() const = 0; |
| 142 | virtual std::string PublicKeyToPEMString() const = 0; |
| 143 | |
| 144 | // Helpers for parsing converting between PEM and DER format. |
| 145 | static bool PemToDer(const std::string& pem_type, |
| 146 | const std::string& pem_string, |
| 147 | std::string* der); |
| 148 | static std::string DerToPem(const std::string& pem_type, |
| 149 | const unsigned char* data, |
| 150 | size_t length); |
| 151 | }; |
| 152 | |
| 153 | bool operator==(const SSLIdentity& a, const SSLIdentity& b); |
| 154 | bool operator!=(const SSLIdentity& a, const SSLIdentity& b); |
| 155 | |
| 156 | // Convert from ASN1 time as restricted by RFC 5280 to seconds from 1970-01-01 |
| 157 | // 00.00 ("epoch"). If the ASN1 time cannot be read, return -1. The data at |
| 158 | // |s| is not 0-terminated; its char count is defined by |length|. |
| 159 | int64_t ASN1TimeToSec(const unsigned char* s, size_t length, bool long_format); |
| 160 | |
| 161 | extern const char kPemTypeCertificate[]; |
| 162 | extern const char kPemTypeRsaPrivateKey[]; |
| 163 | extern const char kPemTypeEcPrivateKey[]; |
| 164 | |
| 165 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 166 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 167 | #endif // RTC_BASE_SSL_IDENTITY_H_ |