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