blob: a0119bb1c4f4b7573c7f950a821b426f691c6b02 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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 Anton10542f22019-01-11 09:11:00 -080013#ifndef RTC_BASE_SSL_IDENTITY_H_
14#define RTC_BASE_SSL_IDENTITY_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000015
Yves Gerey988cc082018-10-23 12:03:01 +020016#include <stdint.h>
Ali Tofigh7fa90572022-03-17 15:47:49 +010017
Yves Gerey2e00abc2018-10-05 15:39:24 +020018#include <ctime>
Harald Alvestrand8515d5a2020-03-20 22:51:32 +010019#include <memory>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020020#include <string>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000021
Ali Tofigh7fa90572022-03-17 15:47:49 +010022#include "absl/strings/string_view.h"
Mirko Bonadei35214fc2019-09-23 14:54:28 +020023#include "rtc_base/system/rtc_export.h"
24
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020025namespace rtc {
26
Yves Gerey988cc082018-10-23 12:03:01 +020027class SSLCertChain;
28class SSLCertificate;
29
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020030// KT_LAST is intended for vector declarations and loops over all key types;
31// it does not represent any key type in itself.
32// KT_DEFAULT is used as the default KeyType for KeyParams.
33enum KeyType { KT_RSA, KT_ECDSA, KT_LAST, KT_DEFAULT = KT_ECDSA };
34
35static const int kRsaDefaultModSize = 1024;
36static const int kRsaDefaultExponent = 0x10001; // = 2^16+1 = 65537
37static const int kRsaMinModSize = 1024;
38static const int kRsaMaxModSize = 8192;
39
40// Certificate default validity lifetime.
41static const int kDefaultCertificateLifetimeInSeconds =
42 60 * 60 * 24 * 30; // 30 days
43// Certificate validity window.
44// This is to compensate for slightly incorrect system clocks.
45static const int kCertificateWindowInSeconds = -60 * 60 * 24;
46
47struct RSAParams {
48 unsigned int mod_size;
49 unsigned int pub_exp;
50};
51
52enum ECCurve { EC_NIST_P256, /* EC_FANCY, */ EC_LAST };
53
Mirko Bonadei35214fc2019-09-23 14:54:28 +020054class RTC_EXPORT KeyParams {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020055 public:
56 // Generate a KeyParams object from a simple KeyType, using default params.
57 explicit KeyParams(KeyType key_type = KT_DEFAULT);
58
59 // Generate a a KeyParams for RSA with explicit parameters.
60 static KeyParams RSA(int mod_size = kRsaDefaultModSize,
61 int pub_exp = kRsaDefaultExponent);
62
63 // Generate a a KeyParams for ECDSA specifying the curve.
64 static KeyParams ECDSA(ECCurve curve = EC_NIST_P256);
65
66 // Check validity of a KeyParams object. Since the factory functions have
67 // no way of returning errors, this function can be called after creation
68 // to make sure the parameters are OK.
69 bool IsValid() const;
70
71 RSAParams rsa_params() const;
72
73 ECCurve ec_curve() const;
74
75 KeyType type() const { return type_; }
76
77 private:
78 KeyType type_;
79 union {
80 RSAParams rsa;
81 ECCurve curve;
82 } params_;
83};
84
85// TODO(hbos): Remove once rtc::KeyType (to be modified) and
86// blink::WebRTCKeyType (to be landed) match. By using this function in Chromium
87// appropriately we can change KeyType enum -> class without breaking Chromium.
88KeyType IntKeyTypeFamilyToKeyType(int key_type_family);
89
Artem Titov96e3b992021-07-26 16:03:14 +020090// Parameters for generating a certificate. If `common_name` is non-empty, it
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020091// will be used for the certificate's subject and issuer name, otherwise a
92// random string will be used.
93struct SSLIdentityParams {
94 std::string common_name;
95 time_t not_before; // Absolute time since epoch in seconds.
96 time_t not_after; // Absolute time since epoch in seconds.
97 KeyParams key_params;
98};
99
100// Our identity in an SSL negotiation: a keypair and certificate (both
101// with the same public key).
102// This too is pretty much immutable once created.
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200103class RTC_EXPORT SSLIdentity {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200104 public:
105 // Generates an identity (keypair and self-signed certificate). If
Artem Titov96e3b992021-07-26 16:03:14 +0200106 // `common_name` is non-empty, it will be used for the certificate's subject
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200107 // and issuer name, otherwise a random string will be used. The key type and
Artem Titov96e3b992021-07-26 16:03:14 +0200108 // parameters are defined in `key_param`. The certificate's lifetime in
109 // seconds from the current time is defined in `certificate_lifetime`; it
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200110 // should be a non-negative number.
111 // Returns null on failure.
112 // Caller is responsible for freeing the returned object.
Ali Tofigh7fa90572022-03-17 15:47:49 +0100113 static std::unique_ptr<SSLIdentity> Create(absl::string_view common_name,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200114 const KeyParams& key_param,
115 time_t certificate_lifetime);
Ali Tofigh7fa90572022-03-17 15:47:49 +0100116 static std::unique_ptr<SSLIdentity> Create(absl::string_view common_name,
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100117 const KeyParams& key_param);
Ali Tofigh7fa90572022-03-17 15:47:49 +0100118 static std::unique_ptr<SSLIdentity> Create(absl::string_view common_name,
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100119 KeyType key_type);
Taylor Brandstetter4479a822020-04-14 16:36:29 -0700120
121 // Allows fine-grained control over expiration time.
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100122 static std::unique_ptr<SSLIdentity> CreateForTest(
123 const SSLIdentityParams& params);
124
125 // Construct an identity from a private key and a certificate.
126 static std::unique_ptr<SSLIdentity> CreateFromPEMStrings(
Ali Tofigh7fa90572022-03-17 15:47:49 +0100127 absl::string_view private_key,
128 absl::string_view certificate);
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100129
130 // Construct an identity from a private key and a certificate chain.
131 static std::unique_ptr<SSLIdentity> CreateFromPEMChainStrings(
Ali Tofigh7fa90572022-03-17 15:47:49 +0100132 absl::string_view private_key,
133 absl::string_view certificate_chain);
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100134
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200135 virtual ~SSLIdentity() {}
136
137 // Returns a new SSLIdentity object instance wrapping the same
138 // identity information.
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100139 std::unique_ptr<SSLIdentity> Clone() const { return CloneInternal(); }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200140
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800141 // Returns a temporary reference to the end-entity (leaf) certificate.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200142 virtual const SSLCertificate& certificate() const = 0;
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800143 // Returns a temporary reference to the entire certificate chain.
144 virtual const SSLCertChain& cert_chain() const = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200145 virtual std::string PrivateKeyToPEMString() const = 0;
146 virtual std::string PublicKeyToPEMString() const = 0;
147
148 // Helpers for parsing converting between PEM and DER format.
Ali Tofigh7fa90572022-03-17 15:47:49 +0100149 static bool PemToDer(absl::string_view pem_type,
150 absl::string_view pem_string,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200151 std::string* der);
Ali Tofigh7fa90572022-03-17 15:47:49 +0100152 static std::string DerToPem(absl::string_view pem_type,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200153 const unsigned char* data,
154 size_t length);
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100155
156 protected:
157 virtual std::unique_ptr<SSLIdentity> CloneInternal() const = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200158};
159
160bool operator==(const SSLIdentity& a, const SSLIdentity& b);
161bool operator!=(const SSLIdentity& a, const SSLIdentity& b);
162
163// Convert from ASN1 time as restricted by RFC 5280 to seconds from 1970-01-01
164// 00.00 ("epoch"). If the ASN1 time cannot be read, return -1. The data at
Artem Titov96e3b992021-07-26 16:03:14 +0200165// `s` is not 0-terminated; its char count is defined by `length`.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200166int64_t ASN1TimeToSec(const unsigned char* s, size_t length, bool long_format);
167
168extern const char kPemTypeCertificate[];
169extern const char kPemTypeRsaPrivateKey[];
170extern const char kPemTypeEcPrivateKey[];
171
172} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000173
Steve Anton10542f22019-01-11 09:11:00 -0800174#endif // RTC_BASE_SSL_IDENTITY_H_