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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef RTC_BASE_OPENSSLIDENTITY_H_ |
| 12 | #define RTC_BASE_OPENSSLIDENTITY_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <openssl/ossl_typ.h> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 15 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 16 | #include <ctime> |
| 17 | #include <memory> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 18 | #include <string> |
| 19 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 20 | #include "rtc_base/checks.h" |
| 21 | #include "rtc_base/constructormagic.h" |
| 22 | #include "rtc_base/opensslcertificate.h" |
| 23 | #include "rtc_base/sslcertificate.h" |
| 24 | #include "rtc_base/sslidentity.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 25 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 26 | namespace rtc { |
| 27 | |
| 28 | // OpenSSLKeyPair encapsulates an OpenSSL EVP_PKEY* keypair object, |
| 29 | // which is reference counted inside the OpenSSL library. |
Benjamin Wright | 61c5cc8 | 2018-10-26 17:50:00 -0700 | [diff] [blame] | 30 | class OpenSSLKeyPair final { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 31 | public: |
| 32 | explicit OpenSSLKeyPair(EVP_PKEY* pkey) : pkey_(pkey) { |
| 33 | RTC_DCHECK(pkey_ != nullptr); |
| 34 | } |
| 35 | |
| 36 | static OpenSSLKeyPair* Generate(const KeyParams& key_params); |
| 37 | // Constructs a key pair from the private key PEM string. This must not result |
| 38 | // in missing public key parameters. Returns null on error. |
Jian Cui | 0a8798b | 2017-11-16 16:58:02 -0800 | [diff] [blame] | 39 | static OpenSSLKeyPair* FromPrivateKeyPEMString(const std::string& pem_string); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 40 | |
| 41 | virtual ~OpenSSLKeyPair(); |
| 42 | |
| 43 | virtual OpenSSLKeyPair* GetReference(); |
| 44 | |
| 45 | EVP_PKEY* pkey() const { return pkey_; } |
| 46 | std::string PrivateKeyToPEMString() const; |
| 47 | std::string PublicKeyToPEMString() const; |
| 48 | bool operator==(const OpenSSLKeyPair& other) const; |
| 49 | bool operator!=(const OpenSSLKeyPair& other) const; |
| 50 | |
| 51 | private: |
| 52 | void AddReference(); |
| 53 | |
| 54 | EVP_PKEY* pkey_; |
| 55 | |
| 56 | RTC_DISALLOW_COPY_AND_ASSIGN(OpenSSLKeyPair); |
| 57 | }; |
| 58 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 59 | // Holds a keypair and certificate together, and a method to generate |
| 60 | // them consistently. |
Benjamin Wright | 61c5cc8 | 2018-10-26 17:50:00 -0700 | [diff] [blame] | 61 | class OpenSSLIdentity final : public SSLIdentity { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 62 | public: |
| 63 | static OpenSSLIdentity* GenerateWithExpiration(const std::string& common_name, |
| 64 | const KeyParams& key_params, |
| 65 | time_t certificate_lifetime); |
| 66 | static OpenSSLIdentity* GenerateForTest(const SSLIdentityParams& params); |
| 67 | static SSLIdentity* FromPEMStrings(const std::string& private_key, |
| 68 | const std::string& certificate); |
Jian Cui | 0a8798b | 2017-11-16 16:58:02 -0800 | [diff] [blame] | 69 | static SSLIdentity* FromPEMChainStrings(const std::string& private_key, |
| 70 | const std::string& certificate_chain); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 71 | ~OpenSSLIdentity() override; |
| 72 | |
| 73 | const OpenSSLCertificate& certificate() const override; |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 74 | const SSLCertChain& cert_chain() const override; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 75 | OpenSSLIdentity* GetReference() const override; |
| 76 | |
| 77 | // Configure an SSL context object to use our key and certificate. |
| 78 | bool ConfigureIdentity(SSL_CTX* ctx); |
| 79 | |
| 80 | std::string PrivateKeyToPEMString() const override; |
| 81 | std::string PublicKeyToPEMString() const override; |
| 82 | bool operator==(const OpenSSLIdentity& other) const; |
| 83 | bool operator!=(const OpenSSLIdentity& other) const; |
| 84 | |
| 85 | private: |
Jian Cui | 0a8798b | 2017-11-16 16:58:02 -0800 | [diff] [blame] | 86 | OpenSSLIdentity(std::unique_ptr<OpenSSLKeyPair> key_pair, |
| 87 | std::unique_ptr<OpenSSLCertificate> certificate); |
| 88 | OpenSSLIdentity(std::unique_ptr<OpenSSLKeyPair> key_pair, |
| 89 | std::unique_ptr<SSLCertChain> cert_chain); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 90 | |
| 91 | static OpenSSLIdentity* GenerateInternal(const SSLIdentityParams& params); |
| 92 | |
| 93 | std::unique_ptr<OpenSSLKeyPair> key_pair_; |
Jian Cui | 0a8798b | 2017-11-16 16:58:02 -0800 | [diff] [blame] | 94 | std::unique_ptr<SSLCertChain> cert_chain_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 95 | |
| 96 | RTC_DISALLOW_COPY_AND_ASSIGN(OpenSSLIdentity); |
| 97 | }; |
| 98 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 99 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 100 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 101 | #endif // RTC_BASE_OPENSSLIDENTITY_H_ |