blob: ae4dbea99b8fa53d986bd7ac2f01a79559477851 [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>
Yves Gerey2e00abc2018-10-05 15:39:24 +020017#include <ctime>
Harald Alvestrand8515d5a2020-03-20 22:51:32 +010018#include <memory>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020019#include <string>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000020
Harald Alvestrand8515d5a2020-03-20 22:51:32 +010021#include "rtc_base/deprecation.h"
Mirko Bonadei35214fc2019-09-23 14:54:28 +020022#include "rtc_base/system/rtc_export.h"
23
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020024namespace rtc {
25
Yves Gerey988cc082018-10-23 12:03:01 +020026class SSLCertChain;
27class SSLCertificate;
28
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020029// KT_LAST is intended for vector declarations and loops over all key types;
30// it does not represent any key type in itself.
31// KT_DEFAULT is used as the default KeyType for KeyParams.
32enum KeyType { KT_RSA, KT_ECDSA, KT_LAST, KT_DEFAULT = KT_ECDSA };
33
34static const int kRsaDefaultModSize = 1024;
35static const int kRsaDefaultExponent = 0x10001; // = 2^16+1 = 65537
36static const int kRsaMinModSize = 1024;
37static const int kRsaMaxModSize = 8192;
38
39// Certificate default validity lifetime.
40static const int kDefaultCertificateLifetimeInSeconds =
41 60 * 60 * 24 * 30; // 30 days
42// Certificate validity window.
43// This is to compensate for slightly incorrect system clocks.
44static const int kCertificateWindowInSeconds = -60 * 60 * 24;
45
46struct RSAParams {
47 unsigned int mod_size;
48 unsigned int pub_exp;
49};
50
51enum ECCurve { EC_NIST_P256, /* EC_FANCY, */ EC_LAST };
52
Mirko Bonadei35214fc2019-09-23 14:54:28 +020053class RTC_EXPORT KeyParams {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020054 public:
55 // Generate a KeyParams object from a simple KeyType, using default params.
56 explicit KeyParams(KeyType key_type = KT_DEFAULT);
57
58 // Generate a a KeyParams for RSA with explicit parameters.
59 static KeyParams RSA(int mod_size = kRsaDefaultModSize,
60 int pub_exp = kRsaDefaultExponent);
61
62 // Generate a a KeyParams for ECDSA specifying the curve.
63 static KeyParams ECDSA(ECCurve curve = EC_NIST_P256);
64
65 // Check validity of a KeyParams object. Since the factory functions have
66 // no way of returning errors, this function can be called after creation
67 // to make sure the parameters are OK.
68 bool IsValid() const;
69
70 RSAParams rsa_params() const;
71
72 ECCurve ec_curve() const;
73
74 KeyType type() const { return type_; }
75
76 private:
77 KeyType type_;
78 union {
79 RSAParams rsa;
80 ECCurve curve;
81 } params_;
82};
83
84// TODO(hbos): Remove once rtc::KeyType (to be modified) and
85// blink::WebRTCKeyType (to be landed) match. By using this function in Chromium
86// appropriately we can change KeyType enum -> class without breaking Chromium.
87KeyType IntKeyTypeFamilyToKeyType(int key_type_family);
88
89// Parameters for generating a certificate. If |common_name| is non-empty, it
90// will be used for the certificate's subject and issuer name, otherwise a
91// random string will be used.
92struct SSLIdentityParams {
93 std::string common_name;
94 time_t not_before; // Absolute time since epoch in seconds.
95 time_t not_after; // Absolute time since epoch in seconds.
96 KeyParams key_params;
97};
98
99// Our identity in an SSL negotiation: a keypair and certificate (both
100// with the same public key).
101// This too is pretty much immutable once created.
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200102class RTC_EXPORT SSLIdentity {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200103 public:
104 // Generates an identity (keypair and self-signed certificate). If
105 // |common_name| is non-empty, it will be used for the certificate's subject
106 // and issuer name, otherwise a random string will be used. The key type and
107 // parameters are defined in |key_param|. The certificate's lifetime in
108 // seconds from the current time is defined in |certificate_lifetime|; it
109 // should be a non-negative number.
110 // Returns null on failure.
111 // Caller is responsible for freeing the returned object.
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100112 static std::unique_ptr<SSLIdentity> Create(const std::string& common_name,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200113 const KeyParams& key_param,
114 time_t certificate_lifetime);
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100115 static std::unique_ptr<SSLIdentity> Create(const std::string& common_name,
116 const KeyParams& key_param);
117 static std::unique_ptr<SSLIdentity> Create(const std::string& common_name,
118 KeyType key_type);
119 static std::unique_ptr<SSLIdentity> CreateForTest(
120 const SSLIdentityParams& params);
121
122 // Construct an identity from a private key and a certificate.
123 static std::unique_ptr<SSLIdentity> CreateFromPEMStrings(
124 const std::string& private_key,
125 const std::string& certificate);
126
127 // Construct an identity from a private key and a certificate chain.
128 static std::unique_ptr<SSLIdentity> CreateFromPEMChainStrings(
129 const std::string& private_key,
130 const std::string& certificate_chain);
131
132 // Old versions of Create(). These return a pointer, but still require the
133 // caller to take ownership.
134 RTC_DEPRECATED static SSLIdentity* GenerateWithExpiration(
135 const std::string& common_name,
136 const KeyParams& key_param,
137 time_t certificate_lifetime);
138 RTC_DEPRECATED static SSLIdentity* Generate(const std::string& common_name,
139 const KeyParams& key_param);
140 RTC_DEPRECATED static SSLIdentity* Generate(const std::string& common_name,
141 KeyType key_type);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200142
143 // Generates an identity with the specified validity period.
144 // TODO(torbjorng): Now that Generate() accepts relevant params, make tests
145 // use that instead of this function.
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100146 RTC_DEPRECATED static SSLIdentity* GenerateForTest(
147 const SSLIdentityParams& params);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200148
149 // Construct an identity from a private key and a certificate.
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100150 RTC_DEPRECATED static SSLIdentity* FromPEMStrings(
151 const std::string& private_key,
152 const std::string& certificate);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200153
Jian Cui0a8798b2017-11-16 16:58:02 -0800154 // Construct an identity from a private key and a certificate chain.
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100155 RTC_DEPRECATED static SSLIdentity* FromPEMChainStrings(
156 const std::string& private_key,
157 const std::string& certificate_chain);
Jian Cui0a8798b2017-11-16 16:58:02 -0800158
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200159 virtual ~SSLIdentity() {}
160
161 // Returns a new SSLIdentity object instance wrapping the same
162 // identity information.
163 // Caller is responsible for freeing the returned object.
164 // TODO(hbos,torbjorng): Rename to a less confusing name.
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100165 RTC_DEPRECATED virtual SSLIdentity* GetReference() const = 0;
166 std::unique_ptr<SSLIdentity> Clone() const { return CloneInternal(); }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200167
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800168 // Returns a temporary reference to the end-entity (leaf) certificate.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200169 virtual const SSLCertificate& certificate() const = 0;
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800170 // Returns a temporary reference to the entire certificate chain.
171 virtual const SSLCertChain& cert_chain() const = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200172 virtual std::string PrivateKeyToPEMString() const = 0;
173 virtual std::string PublicKeyToPEMString() const = 0;
174
175 // Helpers for parsing converting between PEM and DER format.
176 static bool PemToDer(const std::string& pem_type,
177 const std::string& pem_string,
178 std::string* der);
179 static std::string DerToPem(const std::string& pem_type,
180 const unsigned char* data,
181 size_t length);
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100182
183 protected:
184 virtual std::unique_ptr<SSLIdentity> CloneInternal() const = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200185};
186
187bool operator==(const SSLIdentity& a, const SSLIdentity& b);
188bool operator!=(const SSLIdentity& a, const SSLIdentity& b);
189
190// Convert from ASN1 time as restricted by RFC 5280 to seconds from 1970-01-01
191// 00.00 ("epoch"). If the ASN1 time cannot be read, return -1. The data at
192// |s| is not 0-terminated; its char count is defined by |length|.
193int64_t ASN1TimeToSec(const unsigned char* s, size_t length, bool long_format);
194
195extern const char kPemTypeCertificate[];
196extern const char kPemTypeRsaPrivateKey[];
197extern const char kPemTypeEcPrivateKey[];
198
199} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000200
Steve Anton10542f22019-01-11 09:11:00 -0800201#endif // RTC_BASE_SSL_IDENTITY_H_