blob: b72a4c229436e6063c1cdd8d83fb20683ce4e917 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_OPENSSLIDENTITY_H_
12#define RTC_BASE_OPENSSLIDENTITY_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Yves Gerey2e00abc2018-10-05 15:39:24 +020014#include <openssl/base.h> // for EVP_PKEY, ssl_ctx_st
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000015
Yves Gerey2e00abc2018-10-05 15:39:24 +020016#include <ctime> // for time_t
17#include <memory> // for unique_ptr
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020018#include <string>
19
Yves Gerey2e00abc2018-10-05 15:39:24 +020020#include "rtc_base/checks.h" // for RTC_DCHECK
21#include "rtc_base/constructormagic.h" // for RTC_DISALLOW_COPY_AND_ASSIGN
22#include "rtc_base/opensslcertificate.h" // for OpenSSLCertificate
23#include "rtc_base/sslcertificate.h" // for SSLCertChain
24#include "rtc_base/sslidentity.h" // for SSLIdentity, KeyParams, SSL...
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020025
26typedef struct ssl_ctx_st SSL_CTX;
27
28namespace rtc {
29
30// OpenSSLKeyPair encapsulates an OpenSSL EVP_PKEY* keypair object,
31// which is reference counted inside the OpenSSL library.
32class OpenSSLKeyPair {
33 public:
34 explicit OpenSSLKeyPair(EVP_PKEY* pkey) : pkey_(pkey) {
35 RTC_DCHECK(pkey_ != nullptr);
36 }
37
38 static OpenSSLKeyPair* Generate(const KeyParams& key_params);
39 // Constructs a key pair from the private key PEM string. This must not result
40 // in missing public key parameters. Returns null on error.
Jian Cui0a8798b2017-11-16 16:58:02 -080041 static OpenSSLKeyPair* FromPrivateKeyPEMString(const std::string& pem_string);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020042
43 virtual ~OpenSSLKeyPair();
44
45 virtual OpenSSLKeyPair* GetReference();
46
47 EVP_PKEY* pkey() const { return pkey_; }
48 std::string PrivateKeyToPEMString() const;
49 std::string PublicKeyToPEMString() const;
50 bool operator==(const OpenSSLKeyPair& other) const;
51 bool operator!=(const OpenSSLKeyPair& other) const;
52
53 private:
54 void AddReference();
55
56 EVP_PKEY* pkey_;
57
58 RTC_DISALLOW_COPY_AND_ASSIGN(OpenSSLKeyPair);
59};
60
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020061// Holds a keypair and certificate together, and a method to generate
62// them consistently.
63class OpenSSLIdentity : public SSLIdentity {
64 public:
65 static OpenSSLIdentity* GenerateWithExpiration(const std::string& common_name,
66 const KeyParams& key_params,
67 time_t certificate_lifetime);
68 static OpenSSLIdentity* GenerateForTest(const SSLIdentityParams& params);
69 static SSLIdentity* FromPEMStrings(const std::string& private_key,
70 const std::string& certificate);
Jian Cui0a8798b2017-11-16 16:58:02 -080071 static SSLIdentity* FromPEMChainStrings(const std::string& private_key,
72 const std::string& certificate_chain);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020073 ~OpenSSLIdentity() override;
74
75 const OpenSSLCertificate& certificate() const override;
Taylor Brandstetterc3928662018-02-23 13:04:51 -080076 const SSLCertChain& cert_chain() const override;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020077 OpenSSLIdentity* GetReference() const override;
78
79 // Configure an SSL context object to use our key and certificate.
80 bool ConfigureIdentity(SSL_CTX* ctx);
81
82 std::string PrivateKeyToPEMString() const override;
83 std::string PublicKeyToPEMString() const override;
84 bool operator==(const OpenSSLIdentity& other) const;
85 bool operator!=(const OpenSSLIdentity& other) const;
86
87 private:
Jian Cui0a8798b2017-11-16 16:58:02 -080088 OpenSSLIdentity(std::unique_ptr<OpenSSLKeyPair> key_pair,
89 std::unique_ptr<OpenSSLCertificate> certificate);
90 OpenSSLIdentity(std::unique_ptr<OpenSSLKeyPair> key_pair,
91 std::unique_ptr<SSLCertChain> cert_chain);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020092
93 static OpenSSLIdentity* GenerateInternal(const SSLIdentityParams& params);
94
95 std::unique_ptr<OpenSSLKeyPair> key_pair_;
Jian Cui0a8798b2017-11-16 16:58:02 -080096 std::unique_ptr<SSLCertChain> cert_chain_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020097
98 RTC_DISALLOW_COPY_AND_ASSIGN(OpenSSLIdentity);
99};
100
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200101} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000102
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200103#endif // RTC_BASE_OPENSSLIDENTITY_H_