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