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