blob: a0119bb1c4f4b7573c7f950a821b426f691c6b02 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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 17:11:0013#ifndef RTC_BASE_SSL_IDENTITY_H_
14#define RTC_BASE_SSL_IDENTITY_H_
henrike@webrtc.orgf0488722014-05-13 18:00:2615
Yves Gerey988cc082018-10-23 10:03:0116#include <stdint.h>
Ali Tofigh7fa90572022-03-17 14:47:4917
Yves Gerey2e00abc2018-10-05 13:39:2418#include <ctime>
Harald Alvestrand8515d5a2020-03-20 21:51:3219#include <memory>
Henrik Kjellanderec78f1c2017-06-29 05:52:5020#include <string>
henrike@webrtc.orgf0488722014-05-13 18:00:2621
Ali Tofigh7fa90572022-03-17 14:47:4922#include "absl/strings/string_view.h"
Mirko Bonadei35214fc2019-09-23 12:54:2823#include "rtc_base/system/rtc_export.h"
24
Henrik Kjellanderec78f1c2017-06-29 05:52:5025namespace rtc {
26
Yves Gerey988cc082018-10-23 10:03:0127class SSLCertChain;
28class SSLCertificate;
29
Henrik Kjellanderec78f1c2017-06-29 05:52:5030// 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 12:54:2854class RTC_EXPORT KeyParams {
Henrik Kjellanderec78f1c2017-06-29 05:52:5055 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 14:03:1490// Parameters for generating a certificate. If `common_name` is non-empty, it
Henrik Kjellanderec78f1c2017-06-29 05:52:5091// 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 12:54:28103class RTC_EXPORT SSLIdentity {
Henrik Kjellanderec78f1c2017-06-29 05:52:50104 public:
105 // Generates an identity (keypair and self-signed certificate). If
Artem Titov96e3b992021-07-26 14:03:14106 // `common_name` is non-empty, it will be used for the certificate's subject
Henrik Kjellanderec78f1c2017-06-29 05:52:50107 // and issuer name, otherwise a random string will be used. The key type and
Artem Titov96e3b992021-07-26 14:03:14108 // 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 05:52:50110 // should be a non-negative number.
111 // Returns null on failure.
112 // Caller is responsible for freeing the returned object.
Ali Tofigh7fa90572022-03-17 14:47:49113 static std::unique_ptr<SSLIdentity> Create(absl::string_view common_name,
Henrik Kjellanderec78f1c2017-06-29 05:52:50114 const KeyParams& key_param,
115 time_t certificate_lifetime);
Ali Tofigh7fa90572022-03-17 14:47:49116 static std::unique_ptr<SSLIdentity> Create(absl::string_view common_name,
Harald Alvestrand8515d5a2020-03-20 21:51:32117 const KeyParams& key_param);
Ali Tofigh7fa90572022-03-17 14:47:49118 static std::unique_ptr<SSLIdentity> Create(absl::string_view common_name,
Harald Alvestrand8515d5a2020-03-20 21:51:32119 KeyType key_type);
Taylor Brandstetter4479a822020-04-14 23:36:29120
121 // Allows fine-grained control over expiration time.
Harald Alvestrand8515d5a2020-03-20 21:51:32122 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 14:47:49127 absl::string_view private_key,
128 absl::string_view certificate);
Harald Alvestrand8515d5a2020-03-20 21:51:32129
130 // Construct an identity from a private key and a certificate chain.
131 static std::unique_ptr<SSLIdentity> CreateFromPEMChainStrings(
Ali Tofigh7fa90572022-03-17 14:47:49132 absl::string_view private_key,
133 absl::string_view certificate_chain);
Harald Alvestrand8515d5a2020-03-20 21:51:32134
Henrik Kjellanderec78f1c2017-06-29 05:52:50135 virtual ~SSLIdentity() {}
136
137 // Returns a new SSLIdentity object instance wrapping the same
138 // identity information.
Harald Alvestrand8515d5a2020-03-20 21:51:32139 std::unique_ptr<SSLIdentity> Clone() const { return CloneInternal(); }
Henrik Kjellanderec78f1c2017-06-29 05:52:50140
Taylor Brandstetterc3928662018-02-23 21:04:51141 // Returns a temporary reference to the end-entity (leaf) certificate.
Henrik Kjellanderec78f1c2017-06-29 05:52:50142 virtual const SSLCertificate& certificate() const = 0;
Taylor Brandstetterc3928662018-02-23 21:04:51143 // Returns a temporary reference to the entire certificate chain.
144 virtual const SSLCertChain& cert_chain() const = 0;
Henrik Kjellanderec78f1c2017-06-29 05:52:50145 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 14:47:49149 static bool PemToDer(absl::string_view pem_type,
150 absl::string_view pem_string,
Henrik Kjellanderec78f1c2017-06-29 05:52:50151 std::string* der);
Ali Tofigh7fa90572022-03-17 14:47:49152 static std::string DerToPem(absl::string_view pem_type,
Henrik Kjellanderec78f1c2017-06-29 05:52:50153 const unsigned char* data,
154 size_t length);
Harald Alvestrand8515d5a2020-03-20 21:51:32155
156 protected:
157 virtual std::unique_ptr<SSLIdentity> CloneInternal() const = 0;
Henrik Kjellanderec78f1c2017-06-29 05:52:50158};
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 14:03:14165// `s` is not 0-terminated; its char count is defined by `length`.
Henrik Kjellanderec78f1c2017-06-29 05:52:50166int64_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:26173
Steve Anton10542f22019-01-11 17:11:00174#endif // RTC_BASE_SSL_IDENTITY_H_