blob: c499b063326721ebf2f0da15f88dbb2e7d90ac7e [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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 Anton10542f22019-01-11 09:11:00 -080011#ifndef RTC_BASE_OPENSSL_IDENTITY_H_
12#define RTC_BASE_OPENSSL_IDENTITY_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <openssl/ossl_typ.h>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000015
Yves Gerey988cc082018-10-23 12:03:01 +020016#include <ctime>
17#include <memory>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020018#include <string>
19
Yves Gerey988cc082018-10-23 12:03:01 +020020#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "rtc_base/constructor_magic.h"
22#include "rtc_base/openssl_certificate.h"
23#include "rtc_base/ssl_certificate.h"
24#include "rtc_base/ssl_identity.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020025
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020026namespace rtc {
27
28// OpenSSLKeyPair encapsulates an OpenSSL EVP_PKEY* keypair object,
29// which is reference counted inside the OpenSSL library.
Benjamin Wright61c5cc82018-10-26 17:50:00 -070030class OpenSSLKeyPair final {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020031 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 Cui0a8798b2017-11-16 16:58:02 -080039 static OpenSSLKeyPair* FromPrivateKeyPEMString(const std::string& pem_string);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020040
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 Kjellanderec78f1c2017-06-29 07:52:50 +020059// Holds a keypair and certificate together, and a method to generate
60// them consistently.
Benjamin Wright61c5cc82018-10-26 17:50:00 -070061class OpenSSLIdentity final : public SSLIdentity {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020062 public:
Harald Alvestrand8515d5a2020-03-20 22:51:32 +010063 static std::unique_ptr<OpenSSLIdentity> CreateWithExpiration(
64 const std::string& common_name,
65 const KeyParams& key_params,
66 time_t certificate_lifetime);
67 static std::unique_ptr<OpenSSLIdentity> CreateForTest(
68 const SSLIdentityParams& params);
69 static std::unique_ptr<SSLIdentity> CreateFromPEMStrings(
70 const std::string& private_key,
71 const std::string& certificate);
72 static std::unique_ptr<SSLIdentity> CreateFromPEMChainStrings(
73 const std::string& private_key,
74 const std::string& certificate_chain);
75 // Deprecated versions
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020076 static OpenSSLIdentity* GenerateWithExpiration(const std::string& common_name,
77 const KeyParams& key_params,
78 time_t certificate_lifetime);
79 static OpenSSLIdentity* GenerateForTest(const SSLIdentityParams& params);
80 static SSLIdentity* FromPEMStrings(const std::string& private_key,
81 const std::string& certificate);
Jian Cui0a8798b2017-11-16 16:58:02 -080082 static SSLIdentity* FromPEMChainStrings(const std::string& private_key,
83 const std::string& certificate_chain);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020084 ~OpenSSLIdentity() override;
85
86 const OpenSSLCertificate& certificate() const override;
Taylor Brandstetterc3928662018-02-23 13:04:51 -080087 const SSLCertChain& cert_chain() const override;
Harald Alvestrand8515d5a2020-03-20 22:51:32 +010088 RTC_DEPRECATED OpenSSLIdentity* GetReference() const override;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020089
90 // Configure an SSL context object to use our key and certificate.
91 bool ConfigureIdentity(SSL_CTX* ctx);
92
93 std::string PrivateKeyToPEMString() const override;
94 std::string PublicKeyToPEMString() const override;
95 bool operator==(const OpenSSLIdentity& other) const;
96 bool operator!=(const OpenSSLIdentity& other) const;
97
98 private:
Jian Cui0a8798b2017-11-16 16:58:02 -080099 OpenSSLIdentity(std::unique_ptr<OpenSSLKeyPair> key_pair,
100 std::unique_ptr<OpenSSLCertificate> certificate);
101 OpenSSLIdentity(std::unique_ptr<OpenSSLKeyPair> key_pair,
102 std::unique_ptr<SSLCertChain> cert_chain);
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100103 std::unique_ptr<SSLIdentity> CloneInternal() const override;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200104
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100105 static std::unique_ptr<OpenSSLIdentity> CreateInternal(
106 const SSLIdentityParams& params);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200107
108 std::unique_ptr<OpenSSLKeyPair> key_pair_;
Jian Cui0a8798b2017-11-16 16:58:02 -0800109 std::unique_ptr<SSLCertChain> cert_chain_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200110
111 RTC_DISALLOW_COPY_AND_ASSIGN(OpenSSLIdentity);
112};
113
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200114} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000115
Steve Anton10542f22019-01-11 09:11:00 -0800116#endif // RTC_BASE_OPENSSL_IDENTITY_H_