blob: 59f87d07d217cce38eb3b88b62ec4d9518a7173a [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
107 DISALLOW_COPY_AND_ASSIGN(SSLCertChain);
108};
109
Henrik Boström5e56c592015-08-11 10:33:13 +0200110enum KeyType { KT_RSA, KT_ECDSA, KT_LAST, KT_DEFAULT = KT_RSA };
111
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000112// Parameters for generating an identity for testing. If common_name is
113// non-empty, it will be used for the certificate's subject and issuer name,
114// otherwise a random string will be used. |not_before| and |not_after| are
115// offsets to the current time in number of seconds.
116struct SSLIdentityParams {
117 std::string common_name;
118 int not_before; // in seconds.
119 int not_after; // in seconds.
120};
121
122// Our identity in an SSL negotiation: a keypair and certificate (both
123// with the same public key).
124// This too is pretty much immutable once created.
125class SSLIdentity {
126 public:
127 // Generates an identity (keypair and self-signed certificate). If
128 // common_name is non-empty, it will be used for the certificate's
129 // subject and issuer name, otherwise a random string will be used.
130 // Returns NULL on failure.
131 // Caller is responsible for freeing the returned object.
132 static SSLIdentity* Generate(const std::string& common_name);
133
134 // Generates an identity with the specified validity period.
135 static SSLIdentity* GenerateForTest(const SSLIdentityParams& params);
136
137 // Construct an identity from a private key and a certificate.
138 static SSLIdentity* FromPEMStrings(const std::string& private_key,
139 const std::string& certificate);
140
141 virtual ~SSLIdentity() {}
142
143 // Returns a new SSLIdentity object instance wrapping the same
144 // identity information.
145 // Caller is responsible for freeing the returned object.
146 virtual SSLIdentity* GetReference() const = 0;
147
148 // Returns a temporary reference to the certificate.
149 virtual const SSLCertificate& certificate() const = 0;
150
151 // Helpers for parsing converting between PEM and DER format.
152 static bool PemToDer(const std::string& pem_type,
153 const std::string& pem_string,
154 std::string* der);
155 static std::string DerToPem(const std::string& pem_type,
156 const unsigned char* data,
157 size_t length);
158};
159
160extern const char kPemTypeCertificate[];
161extern const char kPemTypeRsaPrivateKey[];
162
163} // namespace rtc
164
165#endif // WEBRTC_BASE_SSLIDENTITY_H_