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 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #ifndef RTC_BASE_OPENSSL_IDENTITY_H_ |
| 12 | #define RTC_BASE_OPENSSL_IDENTITY_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 14 | #include <openssl/ossl_typ.h> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 15 | |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 16 | #include <ctime> |
| 17 | #include <memory> |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 18 | #include <string> |
| 19 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 20 | #include "rtc_base/constructor_magic.h" |
| 21 | #include "rtc_base/openssl_certificate.h" |
Taylor Brandstetter | 165c618 | 2020-12-11 00:23:03 | [diff] [blame] | 22 | #include "rtc_base/openssl_key_pair.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 23 | #include "rtc_base/ssl_certificate.h" |
| 24 | #include "rtc_base/ssl_identity.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 25 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 26 | namespace rtc { |
| 27 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 28 | // Holds a keypair and certificate together, and a method to generate |
| 29 | // them consistently. |
Benjamin Wright | 61c5cc8 | 2018-10-27 00:50:00 | [diff] [blame] | 30 | class OpenSSLIdentity final : public SSLIdentity { |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 31 | public: |
Harald Alvestrand | 8515d5a | 2020-03-20 21:51:32 | [diff] [blame] | 32 | static std::unique_ptr<OpenSSLIdentity> CreateWithExpiration( |
| 33 | const std::string& common_name, |
| 34 | const KeyParams& key_params, |
| 35 | time_t certificate_lifetime); |
| 36 | static std::unique_ptr<OpenSSLIdentity> CreateForTest( |
| 37 | const SSLIdentityParams& params); |
| 38 | static std::unique_ptr<SSLIdentity> CreateFromPEMStrings( |
| 39 | const std::string& private_key, |
| 40 | const std::string& certificate); |
| 41 | static std::unique_ptr<SSLIdentity> CreateFromPEMChainStrings( |
| 42 | const std::string& private_key, |
| 43 | const std::string& certificate_chain); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 44 | ~OpenSSLIdentity() override; |
| 45 | |
| 46 | const OpenSSLCertificate& certificate() const override; |
Taylor Brandstetter | c392866 | 2018-02-23 21:04:51 | [diff] [blame] | 47 | const SSLCertChain& cert_chain() const override; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 48 | |
| 49 | // Configure an SSL context object to use our key and certificate. |
| 50 | bool ConfigureIdentity(SSL_CTX* ctx); |
| 51 | |
| 52 | std::string PrivateKeyToPEMString() const override; |
| 53 | std::string PublicKeyToPEMString() const override; |
| 54 | bool operator==(const OpenSSLIdentity& other) const; |
| 55 | bool operator!=(const OpenSSLIdentity& other) const; |
| 56 | |
| 57 | private: |
Jian Cui | 0a8798b | 2017-11-17 00:58:02 | [diff] [blame] | 58 | OpenSSLIdentity(std::unique_ptr<OpenSSLKeyPair> key_pair, |
| 59 | std::unique_ptr<OpenSSLCertificate> certificate); |
| 60 | OpenSSLIdentity(std::unique_ptr<OpenSSLKeyPair> key_pair, |
| 61 | std::unique_ptr<SSLCertChain> cert_chain); |
Harald Alvestrand | 8515d5a | 2020-03-20 21:51:32 | [diff] [blame] | 62 | std::unique_ptr<SSLIdentity> CloneInternal() const override; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 63 | |
Harald Alvestrand | 8515d5a | 2020-03-20 21:51:32 | [diff] [blame] | 64 | static std::unique_ptr<OpenSSLIdentity> CreateInternal( |
| 65 | const SSLIdentityParams& params); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 66 | |
| 67 | std::unique_ptr<OpenSSLKeyPair> key_pair_; |
Jian Cui | 0a8798b | 2017-11-17 00:58:02 | [diff] [blame] | 68 | std::unique_ptr<SSLCertChain> cert_chain_; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 69 | |
| 70 | RTC_DISALLOW_COPY_AND_ASSIGN(OpenSSLIdentity); |
| 71 | }; |
| 72 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 73 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 74 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 75 | #endif // RTC_BASE_OPENSSL_IDENTITY_H_ |