henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 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 | #ifndef WEBRTC_BASE_OPENSSLIDENTITY_H_ |
| 12 | #define WEBRTC_BASE_OPENSSLIDENTITY_H_ |
| 13 | |
| 14 | #include <openssl/evp.h> |
| 15 | #include <openssl/x509.h> |
| 16 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 17 | #include <memory> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 18 | #include <string> |
| 19 | |
| 20 | #include "webrtc/base/common.h" |
kwiberg | 4485ffb | 2016-04-26 08:14:39 -0700 | [diff] [blame] | 21 | #include "webrtc/base/constructormagic.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 22 | #include "webrtc/base/sslidentity.h" |
| 23 | |
| 24 | typedef struct ssl_ctx_st SSL_CTX; |
| 25 | |
| 26 | namespace rtc { |
| 27 | |
| 28 | // OpenSSLKeyPair encapsulates an OpenSSL EVP_PKEY* keypair object, |
| 29 | // which is reference counted inside the OpenSSL library. |
| 30 | class OpenSSLKeyPair { |
| 31 | public: |
| 32 | explicit OpenSSLKeyPair(EVP_PKEY* pkey) : pkey_(pkey) { |
| 33 | ASSERT(pkey_ != NULL); |
| 34 | } |
| 35 | |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame] | 36 | static OpenSSLKeyPair* Generate(const KeyParams& key_params); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 37 | |
| 38 | virtual ~OpenSSLKeyPair(); |
| 39 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 40 | virtual OpenSSLKeyPair* GetReference(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 41 | |
| 42 | EVP_PKEY* pkey() const { return pkey_; } |
| 43 | |
| 44 | private: |
| 45 | void AddReference(); |
| 46 | |
| 47 | EVP_PKEY* pkey_; |
| 48 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 49 | RTC_DISALLOW_COPY_AND_ASSIGN(OpenSSLKeyPair); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | // OpenSSLCertificate encapsulates an OpenSSL X509* certificate object, |
| 53 | // which is also reference counted inside the OpenSSL library. |
| 54 | class OpenSSLCertificate : public SSLCertificate { |
| 55 | public: |
| 56 | // Caller retains ownership of the X509 object. |
| 57 | explicit OpenSSLCertificate(X509* x509) : x509_(x509) { |
| 58 | AddReference(); |
| 59 | } |
| 60 | |
| 61 | static OpenSSLCertificate* Generate(OpenSSLKeyPair* key_pair, |
| 62 | const SSLIdentityParams& params); |
| 63 | static OpenSSLCertificate* FromPEMString(const std::string& pem_string); |
| 64 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 65 | ~OpenSSLCertificate() override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 66 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 67 | OpenSSLCertificate* GetReference() const override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 68 | |
| 69 | X509* x509() const { return x509_; } |
| 70 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 71 | std::string ToPEMString() const override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 72 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 73 | void ToDER(Buffer* der_buffer) const override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 74 | |
| 75 | // Compute the digest of the certificate given algorithm |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 76 | bool ComputeDigest(const std::string& algorithm, |
| 77 | unsigned char* digest, |
| 78 | size_t size, |
| 79 | size_t* length) const override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 80 | |
| 81 | // Compute the digest of a certificate as an X509 * |
| 82 | static bool ComputeDigest(const X509* x509, |
| 83 | const std::string& algorithm, |
| 84 | unsigned char* digest, |
| 85 | size_t size, |
| 86 | size_t* length); |
| 87 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 88 | bool GetSignatureDigestAlgorithm(std::string* algorithm) const override; |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 89 | std::unique_ptr<SSLCertChain> GetChain() const override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 90 | |
Torbjorn Granlund | 46c9cc0 | 2015-12-01 13:06:34 +0100 | [diff] [blame] | 91 | int64_t CertificateExpirationTime() const override; |
| 92 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 93 | private: |
| 94 | void AddReference() const; |
| 95 | |
| 96 | X509* x509_; |
| 97 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 98 | RTC_DISALLOW_COPY_AND_ASSIGN(OpenSSLCertificate); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | // Holds a keypair and certificate together, and a method to generate |
| 102 | // them consistently. |
| 103 | class OpenSSLIdentity : public SSLIdentity { |
| 104 | public: |
Torbjorn Granlund | 1d846b2 | 2016-03-31 16:21:04 +0200 | [diff] [blame] | 105 | static OpenSSLIdentity* GenerateWithExpiration(const std::string& common_name, |
| 106 | const KeyParams& key_params, |
| 107 | time_t certificate_lifetime); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 108 | static OpenSSLIdentity* GenerateForTest(const SSLIdentityParams& params); |
| 109 | static SSLIdentity* FromPEMStrings(const std::string& private_key, |
| 110 | const std::string& certificate); |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 111 | ~OpenSSLIdentity() override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 112 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 113 | const OpenSSLCertificate& certificate() const override; |
| 114 | OpenSSLIdentity* GetReference() const override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 115 | |
| 116 | // Configure an SSL context object to use our key and certificate. |
| 117 | bool ConfigureIdentity(SSL_CTX* ctx); |
| 118 | |
| 119 | private: |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 120 | OpenSSLIdentity(OpenSSLKeyPair* key_pair, OpenSSLCertificate* certificate); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 121 | |
| 122 | static OpenSSLIdentity* GenerateInternal(const SSLIdentityParams& params); |
| 123 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 124 | std::unique_ptr<OpenSSLKeyPair> key_pair_; |
| 125 | std::unique_ptr<OpenSSLCertificate> certificate_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 126 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 127 | RTC_DISALLOW_COPY_AND_ASSIGN(OpenSSLIdentity); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | |
| 131 | } // namespace rtc |
| 132 | |
| 133 | #endif // WEBRTC_BASE_OPENSSLIDENTITY_H_ |