henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2004--2008, 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 | #ifndef TALK_BASE_OPENSSLIDENTITY_H__ |
| 29 | #define TALK_BASE_OPENSSLIDENTITY_H__ |
| 30 | |
| 31 | #include <openssl/evp.h> |
| 32 | #include <openssl/x509.h> |
| 33 | |
| 34 | #include <string> |
| 35 | |
| 36 | #include "talk/base/common.h" |
| 37 | #include "talk/base/scoped_ptr.h" |
| 38 | #include "talk/base/sslidentity.h" |
| 39 | |
| 40 | typedef struct ssl_ctx_st SSL_CTX; |
| 41 | |
| 42 | namespace talk_base { |
| 43 | |
| 44 | // OpenSSLKeyPair encapsulates an OpenSSL EVP_PKEY* keypair object, |
| 45 | // which is reference counted inside the OpenSSL library. |
| 46 | class OpenSSLKeyPair { |
| 47 | public: |
| 48 | explicit OpenSSLKeyPair(EVP_PKEY* pkey) : pkey_(pkey) { |
| 49 | ASSERT(pkey_ != NULL); |
| 50 | } |
| 51 | |
| 52 | static OpenSSLKeyPair* Generate(); |
| 53 | |
| 54 | virtual ~OpenSSLKeyPair(); |
| 55 | |
| 56 | virtual OpenSSLKeyPair* GetReference() { |
| 57 | AddReference(); |
| 58 | return new OpenSSLKeyPair(pkey_); |
| 59 | } |
| 60 | |
| 61 | EVP_PKEY* pkey() const { return pkey_; } |
| 62 | |
| 63 | private: |
| 64 | void AddReference(); |
| 65 | |
| 66 | EVP_PKEY* pkey_; |
| 67 | |
| 68 | DISALLOW_EVIL_CONSTRUCTORS(OpenSSLKeyPair); |
| 69 | }; |
| 70 | |
| 71 | // OpenSSLCertificate encapsulates an OpenSSL X509* certificate object, |
| 72 | // which is also reference counted inside the OpenSSL library. |
| 73 | class OpenSSLCertificate : public SSLCertificate { |
| 74 | public: |
| 75 | static OpenSSLCertificate* Generate(OpenSSLKeyPair* key_pair, |
| 76 | const std::string& common_name); |
| 77 | static OpenSSLCertificate* FromPEMString(const std::string& pem_string); |
| 78 | |
| 79 | virtual ~OpenSSLCertificate(); |
| 80 | |
| 81 | virtual OpenSSLCertificate* GetReference() const { |
| 82 | AddReference(); |
| 83 | return new OpenSSLCertificate(x509_); |
| 84 | } |
| 85 | |
| 86 | X509* x509() const { return x509_; } |
| 87 | |
| 88 | virtual std::string ToPEMString() const; |
| 89 | |
| 90 | // Compute the digest of the certificate given algorithm |
| 91 | virtual bool ComputeDigest(const std::string &algorithm, |
| 92 | unsigned char *digest, std::size_t size, |
| 93 | std::size_t *length) const; |
| 94 | |
| 95 | // Compute the digest of a certificate as an X509 * |
| 96 | static bool ComputeDigest(const X509 *x509, |
| 97 | const std::string &algorithm, |
| 98 | unsigned char *digest, |
| 99 | std::size_t size, |
| 100 | std::size_t *length); |
| 101 | |
| 102 | private: |
| 103 | explicit OpenSSLCertificate(X509* x509) : x509_(x509) { |
| 104 | ASSERT(x509_ != NULL); |
| 105 | } |
| 106 | void AddReference() const; |
| 107 | |
| 108 | X509* x509_; |
| 109 | |
| 110 | DISALLOW_EVIL_CONSTRUCTORS(OpenSSLCertificate); |
| 111 | }; |
| 112 | |
| 113 | // Holds a keypair and certificate together, and a method to generate |
| 114 | // them consistently. |
| 115 | class OpenSSLIdentity : public SSLIdentity { |
| 116 | public: |
| 117 | static OpenSSLIdentity* Generate(const std::string& common_name); |
| 118 | static SSLIdentity* FromPEMStrings(const std::string& private_key, |
| 119 | const std::string& certificate); |
| 120 | virtual ~OpenSSLIdentity() { } |
| 121 | |
| 122 | virtual const OpenSSLCertificate& certificate() const { |
| 123 | return *certificate_; |
| 124 | } |
| 125 | |
| 126 | virtual OpenSSLIdentity* GetReference() const { |
| 127 | return new OpenSSLIdentity(key_pair_->GetReference(), |
| 128 | certificate_->GetReference()); |
| 129 | } |
| 130 | |
| 131 | // Configure an SSL context object to use our key and certificate. |
| 132 | bool ConfigureIdentity(SSL_CTX* ctx); |
| 133 | |
| 134 | private: |
| 135 | OpenSSLIdentity(OpenSSLKeyPair* key_pair, |
| 136 | OpenSSLCertificate* certificate) |
| 137 | : key_pair_(key_pair), certificate_(certificate) { |
| 138 | ASSERT(key_pair != NULL); |
| 139 | ASSERT(certificate != NULL); |
| 140 | } |
| 141 | |
| 142 | scoped_ptr<OpenSSLKeyPair> key_pair_; |
| 143 | scoped_ptr<OpenSSLCertificate> certificate_; |
| 144 | |
| 145 | DISALLOW_EVIL_CONSTRUCTORS(OpenSSLIdentity); |
| 146 | }; |
| 147 | |
| 148 | |
| 149 | } // namespace talk_base |
| 150 | |
| 151 | #endif // TALK_BASE_OPENSSLIDENTITY_H__ |