blob: 345691c3982a17d20b5b6b44dafbe926ef7f4969 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2004, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28// Handling of certificates and keypairs for SSLStreamAdapter's peer mode.
29
30#ifndef TALK_BASE_SSLIDENTITY_H_
31#define TALK_BASE_SSLIDENTITY_H_
32
wu@webrtc.org4551b792013-10-09 15:37:36 +000033#include <algorithm>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034#include <string>
wu@webrtc.org4551b792013-10-09 15:37:36 +000035#include <vector>
36
37#include "talk/base/buffer.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000038#include "talk/base/messagedigest.h"
39
40namespace talk_base {
41
wu@webrtc.org4551b792013-10-09 15:37:36 +000042// Forward declaration due to circular dependency with SSLCertificate.
43class SSLCertChain;
44
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045// Abstract interface overridden by SSL library specific
46// implementations.
47
48// A somewhat opaque type used to encapsulate a certificate.
49// Wraps the SSL library's notion of a certificate, with reference counting.
50// The SSLCertificate object is pretty much immutable once created.
51// (The OpenSSL implementation only does reference counting and
52// possibly caching of intermediate results.)
53class SSLCertificate {
54 public:
55 // Parses and build a certificate from a PEM encoded string.
56 // Returns NULL on failure.
57 // The length of the string representation of the certificate is
58 // stored in *pem_length if it is non-NULL, and only if
59 // parsing was successful.
60 // Caller is responsible for freeing the returned object.
61 static SSLCertificate* FromPEMString(const std::string& pem_string);
62 virtual ~SSLCertificate() {}
63
64 // Returns a new SSLCertificate object instance wrapping the same
wu@webrtc.org4551b792013-10-09 15:37:36 +000065 // underlying certificate, including its chain if present.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066 // Caller is responsible for freeing the returned object.
67 virtual SSLCertificate* GetReference() const = 0;
68
wu@webrtc.org4551b792013-10-09 15:37:36 +000069 // Provides the cert chain, or returns false. The caller owns the chain.
70 // The chain includes a copy of each certificate, excluding the leaf.
71 virtual bool GetChain(SSLCertChain** chain) const = 0;
72
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073 // Returns a PEM encoded string representation of the certificate.
74 virtual std::string ToPEMString() const = 0;
75
wu@webrtc.org4551b792013-10-09 15:37:36 +000076 // Provides a DER encoded binary representation of the certificate.
77 virtual void ToDER(Buffer* der_buffer) const = 0;
78
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079 // Compute the digest of the certificate given algorithm
80 virtual bool ComputeDigest(const std::string &algorithm,
wu@webrtc.org91053e72013-08-10 07:18:04 +000081 unsigned char* digest, std::size_t size,
82 std::size_t* length) const = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083};
84
wu@webrtc.org4551b792013-10-09 15:37:36 +000085// SSLCertChain is a simple wrapper for a vector of SSLCertificates. It serves
86// primarily to ensure proper memory management (especially deletion) of the
87// SSLCertificate pointers.
88class SSLCertChain {
89 public:
90 // These constructors copy the provided SSLCertificate(s), so the caller
91 // retains ownership.
92 explicit SSLCertChain(const std::vector<SSLCertificate*>& certs) {
93 ASSERT(!certs.empty());
94 certs_.resize(certs.size());
95 std::transform(certs.begin(), certs.end(), certs_.begin(), DupCert);
96 }
97 explicit SSLCertChain(const SSLCertificate* cert) {
98 certs_.push_back(cert->GetReference());
99 }
100
101 ~SSLCertChain() {
102 std::for_each(certs_.begin(), certs_.end(), DeleteCert);
103 }
104
105 // Vector access methods.
106 size_t GetSize() const { return certs_.size(); }
107
108 // Returns a temporary reference, only valid until the chain is destroyed.
109 const SSLCertificate& Get(size_t pos) const { return *(certs_[pos]); }
110
111 // Returns a new SSLCertChain object instance wrapping the same underlying
112 // certificate chain. Caller is responsible for freeing the returned object.
113 SSLCertChain* Copy() const {
114 return new SSLCertChain(certs_);
115 }
116
117 private:
118 // Helper function for duplicating a vector of certificates.
119 static SSLCertificate* DupCert(const SSLCertificate* cert) {
120 return cert->GetReference();
121 }
122
123 // Helper function for deleting a vector of certificates.
124 static void DeleteCert(SSLCertificate* cert) { delete cert; }
125
126 std::vector<SSLCertificate*> certs_;
127
128 DISALLOW_COPY_AND_ASSIGN(SSLCertChain);
129};
130
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000131// 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.
141 static SSLIdentity* Generate(const std::string& common_name);
142
143 // Construct an identity from a private key and a certificate.
144 static SSLIdentity* FromPEMStrings(const std::string& private_key,
145 const std::string& certificate);
146
147 virtual ~SSLIdentity() {}
148
149 // Returns a new SSLIdentity object instance wrapping the same
150 // identity information.
151 // Caller is responsible for freeing the returned object.
152 virtual SSLIdentity* GetReference() const = 0;
153
154 // Returns a temporary reference to the certificate.
155 virtual const SSLCertificate& certificate() const = 0;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000156
157 // Helpers for parsing converting between PEM and DER format.
158 static bool PemToDer(const std::string& pem_type,
159 const std::string& pem_string,
160 std::string* der);
161 static std::string DerToPem(const std::string& pem_type,
162 const unsigned char* data,
163 size_t length);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000164};
165
wu@webrtc.org91053e72013-08-10 07:18:04 +0000166extern const char kPemTypeCertificate[];
167extern const char kPemTypeRsaPrivateKey[];
168
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169} // namespace talk_base
170
wu@webrtc.org4551b792013-10-09 15:37:36 +0000171#endif // TALK_BASE_SSLIDENTITY_H_