blob: 3a1bbd08563bf5d58faaaf184633220fecb45709 [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
Henrik Boström9b5476d2015-09-22 14:12:57 +0200110// TODO(hbos,torbjorng): Don't change KT_DEFAULT without first updating
111// PeerConnectionFactory_nativeCreatePeerConnection's certificate generation
112// code.
Henrik Boström5e56c592015-08-11 10:33:13 +0200113enum KeyType { KT_RSA, KT_ECDSA, KT_LAST, KT_DEFAULT = KT_RSA };
114
Henrik Boström9b5476d2015-09-22 14:12:57 +0200115// TODO(hbos): Remove once rtc::KeyType (to be modified) and
116// blink::WebRTCKeyType (to be landed) match. By using this function in Chromium
117// appropriately we can change KeyType enum -> class without breaking Chromium.
118KeyType IntKeyTypeFamilyToKeyType(int key_type_family);
119
torbjorng335204c2015-10-08 02:30:14 -0700120// Parameters for generating an identity for testing. If common_name is
121// non-empty, it will be used for the certificate's subject and issuer name,
122// otherwise a random string will be used. |not_before| and |not_after| are
123// offsets to the current time in number of seconds.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000124struct SSLIdentityParams {
125 std::string common_name;
torbjorng335204c2015-10-08 02:30:14 -0700126 int not_before; // in seconds.
127 int not_after; // in seconds.
128 KeyType key_type;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000129};
130
131// Our identity in an SSL negotiation: a keypair and certificate (both
132// with the same public key).
133// This too is pretty much immutable once created.
134class SSLIdentity {
135 public:
136 // Generates an identity (keypair and self-signed certificate). If
137 // common_name is non-empty, it will be used for the certificate's
138 // subject and issuer name, otherwise a random string will be used.
139 // Returns NULL on failure.
140 // Caller is responsible for freeing the returned object.
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200141 static SSLIdentity* Generate(const std::string& common_name,
torbjorng335204c2015-10-08 02:30:14 -0700142 KeyType key_type);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000143
144 // Generates an identity with the specified validity period.
145 static SSLIdentity* GenerateForTest(const SSLIdentityParams& params);
146
147 // Construct an identity from a private key and a certificate.
148 static SSLIdentity* FromPEMStrings(const std::string& private_key,
149 const std::string& certificate);
150
151 virtual ~SSLIdentity() {}
152
153 // Returns a new SSLIdentity object instance wrapping the same
154 // identity information.
155 // Caller is responsible for freeing the returned object.
Henrik Boström87713d02015-08-25 09:53:21 +0200156 // TODO(hbos,torbjorng): Rename to a less confusing name.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000157 virtual SSLIdentity* GetReference() const = 0;
158
159 // Returns a temporary reference to the certificate.
160 virtual const SSLCertificate& certificate() const = 0;
161
162 // Helpers for parsing converting between PEM and DER format.
163 static bool PemToDer(const std::string& pem_type,
164 const std::string& pem_string,
165 std::string* der);
166 static std::string DerToPem(const std::string& pem_type,
167 const unsigned char* data,
168 size_t length);
169};
170
171extern const char kPemTypeCertificate[];
172extern const char kPemTypeRsaPrivateKey[];
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200173extern const char kPemTypeEcPrivateKey[];
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000174
175} // namespace rtc
176
177#endif // WEBRTC_BASE_SSLIDENTITY_H_