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