blob: cf9942637e480de935bdfaad3667f18555ddccc2 [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
13#ifndef WEBRTC_BASE_SSLIDENTITY_H_
14#define WEBRTC_BASE_SSLIDENTITY_H_
15
16#include <algorithm>
17#include <string>
18#include <vector>
19
20#include "webrtc/base/buffer.h"
21#include "webrtc/base/messagedigest.h"
22
23namespace rtc {
24
25// Forward declaration due to circular dependency with SSLCertificate.
26class SSLCertChain;
27
28// Abstract interface overridden by SSL library specific
29// implementations.
30
31// A somewhat opaque type used to encapsulate a certificate.
32// Wraps the SSL library's notion of a certificate, with reference counting.
33// The SSLCertificate object is pretty much immutable once created.
34// (The OpenSSL implementation only does reference counting and
35// possibly caching of intermediate results.)
36class SSLCertificate {
37 public:
38 // Parses and build a certificate from a PEM encoded string.
39 // Returns NULL on failure.
40 // The length of the string representation of the certificate is
41 // stored in *pem_length if it is non-NULL, and only if
42 // parsing was successful.
43 // Caller is responsible for freeing the returned object.
44 static SSLCertificate* FromPEMString(const std::string& pem_string);
45 virtual ~SSLCertificate() {}
46
47 // Returns a new SSLCertificate object instance wrapping the same
48 // underlying certificate, including its chain if present.
49 // Caller is responsible for freeing the returned object.
50 virtual SSLCertificate* GetReference() const = 0;
51
52 // Provides the cert chain, or returns false. The caller owns the chain.
53 // The chain includes a copy of each certificate, excluding the leaf.
54 virtual bool GetChain(SSLCertChain** chain) const = 0;
55
56 // Returns a PEM encoded string representation of the certificate.
57 virtual std::string ToPEMString() const = 0;
58
59 // Provides a DER encoded binary representation of the certificate.
60 virtual void ToDER(Buffer* der_buffer) const = 0;
61
62 // Gets the name of the digest algorithm that was used to compute this
63 // certificate's signature.
64 virtual bool GetSignatureDigestAlgorithm(std::string* algorithm) const = 0;
65
66 // Compute the digest of the certificate given algorithm
67 virtual bool ComputeDigest(const std::string& algorithm,
68 unsigned char* digest,
69 size_t size,
70 size_t* length) const = 0;
71};
72
73// SSLCertChain is a simple wrapper for a vector of SSLCertificates. It serves
74// primarily to ensure proper memory management (especially deletion) of the
75// SSLCertificate pointers.
76class SSLCertChain {
77 public:
78 // These constructors copy the provided SSLCertificate(s), so the caller
79 // retains ownership.
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000080 explicit SSLCertChain(const std::vector<SSLCertificate*>& certs);
81 explicit SSLCertChain(const SSLCertificate* cert);
82 ~SSLCertChain();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000083
84 // Vector access methods.
85 size_t GetSize() const { return certs_.size(); }
86
87 // Returns a temporary reference, only valid until the chain is destroyed.
88 const SSLCertificate& Get(size_t pos) const { return *(certs_[pos]); }
89
90 // Returns a new SSLCertChain object instance wrapping the same underlying
91 // certificate chain. Caller is responsible for freeing the returned object.
92 SSLCertChain* Copy() const {
93 return new SSLCertChain(certs_);
94 }
95
96 private:
97 // Helper function for duplicating a vector of certificates.
98 static SSLCertificate* DupCert(const SSLCertificate* cert) {
99 return cert->GetReference();
100 }
101
102 // Helper function for deleting a vector of certificates.
103 static void DeleteCert(SSLCertificate* cert) { delete cert; }
104
105 std::vector<SSLCertificate*> certs_;
106
henrikg3c089d72015-09-16 05:37:44 -0700107 RTC_DISALLOW_COPY_AND_ASSIGN(SSLCertChain);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000108};
109
torbjorng4e572472015-10-08 09:42:49 -0700110// KT_DEFAULT is currently an alias for KT_RSA. This is likely to change.
111// KT_LAST is intended for vector declarations and loops over all key types;
112// it does not represent any key type in itself.
Henrik Boström9b5476d2015-09-22 14:12:57 +0200113// TODO(hbos,torbjorng): Don't change KT_DEFAULT without first updating
114// PeerConnectionFactory_nativeCreatePeerConnection's certificate generation
115// code.
Henrik Boström5e56c592015-08-11 10:33:13 +0200116enum KeyType { KT_RSA, KT_ECDSA, KT_LAST, KT_DEFAULT = KT_RSA };
117
torbjorng4e572472015-10-08 09:42:49 -0700118static const int kRsaDefaultModSize = 1024;
119static const int kRsaDefaultExponent = 0x10001; // = 2^16+1 = 65537
120static const int kRsaMinModSize = 1024;
121static const int kRsaMaxModSize = 8192;
122
123struct RSAParams {
124 unsigned int mod_size;
125 unsigned int pub_exp;
126};
127
128enum ECCurve { EC_NIST_P256, /* EC_FANCY, */ EC_LAST };
129
130class KeyParams {
131 public:
132 // Generate a KeyParams object from a simple KeyType, using default params.
133 explicit KeyParams(KeyType key_type = KT_DEFAULT);
134
135 // Generate a a KeyParams for RSA with explicit parameters.
136 static KeyParams RSA(int mod_size = kRsaDefaultModSize,
137 int pub_exp = kRsaDefaultExponent);
138
139 // Generate a a KeyParams for ECDSA specifying the curve.
140 static KeyParams ECDSA(ECCurve curve = EC_NIST_P256);
141
142 // Check validity of a KeyParams object. Since the factory functions have
143 // no way of returning errors, this function can be called after creation
144 // to make sure the parameters are OK.
145 bool IsValid() const;
146
147 RSAParams rsa_params() const;
148
149 ECCurve ec_curve() const;
150
151 KeyType type() const { return type_; }
152
153 private:
154 KeyType type_;
155 union {
156 RSAParams rsa;
157 ECCurve curve;
158 } params_;
159};
160
Henrik Boström9b5476d2015-09-22 14:12:57 +0200161// TODO(hbos): Remove once rtc::KeyType (to be modified) and
162// blink::WebRTCKeyType (to be landed) match. By using this function in Chromium
163// appropriately we can change KeyType enum -> class without breaking Chromium.
164KeyType IntKeyTypeFamilyToKeyType(int key_type_family);
165
torbjorng4e572472015-10-08 09:42:49 -0700166// Parameters for generating a certificate. If |common_name| is non-empty, it
167// will be used for the certificate's subject and issuer name, otherwise a
168// random string will be used.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000169struct SSLIdentityParams {
170 std::string common_name;
torbjorng4e572472015-10-08 09:42:49 -0700171 int not_before; // offset from current time in seconds.
172 int not_after; // offset from current time in seconds.
173 KeyParams key_params;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000174};
175
176// Our identity in an SSL negotiation: a keypair and certificate (both
177// with the same public key).
178// This too is pretty much immutable once created.
179class SSLIdentity {
180 public:
181 // Generates an identity (keypair and self-signed certificate). If
182 // common_name is non-empty, it will be used for the certificate's
183 // subject and issuer name, otherwise a random string will be used.
184 // Returns NULL on failure.
185 // Caller is responsible for freeing the returned object.
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200186 static SSLIdentity* Generate(const std::string& common_name,
torbjorng4e572472015-10-08 09:42:49 -0700187 const KeyParams& key_param);
188 static SSLIdentity* Generate(const std::string& common_name,
189 KeyType key_type) {
190 return Generate(common_name, KeyParams(key_type));
191 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000192
193 // Generates an identity with the specified validity period.
194 static SSLIdentity* GenerateForTest(const SSLIdentityParams& params);
195
196 // Construct an identity from a private key and a certificate.
197 static SSLIdentity* FromPEMStrings(const std::string& private_key,
198 const std::string& certificate);
199
200 virtual ~SSLIdentity() {}
201
202 // Returns a new SSLIdentity object instance wrapping the same
203 // identity information.
204 // Caller is responsible for freeing the returned object.
Henrik Boström87713d02015-08-25 09:53:21 +0200205 // TODO(hbos,torbjorng): Rename to a less confusing name.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000206 virtual SSLIdentity* GetReference() const = 0;
207
208 // Returns a temporary reference to the certificate.
209 virtual const SSLCertificate& certificate() const = 0;
210
211 // Helpers for parsing converting between PEM and DER format.
212 static bool PemToDer(const std::string& pem_type,
213 const std::string& pem_string,
214 std::string* der);
215 static std::string DerToPem(const std::string& pem_type,
216 const unsigned char* data,
217 size_t length);
218};
219
220extern const char kPemTypeCertificate[];
221extern const char kPemTypeRsaPrivateKey[];
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200222extern const char kPemTypeEcPrivateKey[];
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000223
224} // namespace rtc
225
226#endif // WEBRTC_BASE_SSLIDENTITY_H_