blob: 34044276ca7659bdd785545bd9bef11b8caf9c92 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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 04:47:3111#ifndef RTC_BASE_OPENSSLIDENTITY_H_
12#define RTC_BASE_OPENSSLIDENTITY_H_
henrike@webrtc.orgf0488722014-05-13 18:00:2613
Henrik Kjellanderec78f1c2017-06-29 05:52:5014#include <openssl/evp.h>
15#include <openssl/x509.h>
henrike@webrtc.orgf0488722014-05-13 18:00:2616
Henrik Kjellanderec78f1c2017-06-29 05:52:5017#include <memory>
18#include <string>
19
Mirko Bonadei92ea95e2017-09-15 04:47:3120#include "rtc_base/checks.h"
21#include "rtc_base/constructormagic.h"
Benjamin Wrightd6f86e82018-05-08 20:12:2522#include "rtc_base/opensslcertificate.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3123#include "rtc_base/sslidentity.h"
Henrik Kjellanderec78f1c2017-06-29 05:52:5024
25typedef struct ssl_ctx_st SSL_CTX;
26
27namespace rtc {
28
29// OpenSSLKeyPair encapsulates an OpenSSL EVP_PKEY* keypair object,
30// which is reference counted inside the OpenSSL library.
31class 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 Cui0a8798b2017-11-17 00:58:0240 static OpenSSLKeyPair* FromPrivateKeyPEMString(const std::string& pem_string);
Henrik Kjellanderec78f1c2017-06-29 05:52:5041
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 Kjellanderec78f1c2017-06-29 05:52:5060// Holds a keypair and certificate together, and a method to generate
61// them consistently.
62class 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 Cui0a8798b2017-11-17 00:58:0270 static SSLIdentity* FromPEMChainStrings(const std::string& private_key,
71 const std::string& certificate_chain);
Henrik Kjellanderec78f1c2017-06-29 05:52:5072 ~OpenSSLIdentity() override;
73
74 const OpenSSLCertificate& certificate() const override;
Taylor Brandstetterc3928662018-02-23 21:04:5175 const SSLCertChain& cert_chain() const override;
Henrik Kjellanderec78f1c2017-06-29 05:52:5076 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 Cui0a8798b2017-11-17 00:58:0287 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 Kjellanderec78f1c2017-06-29 05:52:5091
92 static OpenSSLIdentity* GenerateInternal(const SSLIdentityParams& params);
93
94 std::unique_ptr<OpenSSLKeyPair> key_pair_;
Jian Cui0a8798b2017-11-17 00:58:0295 std::unique_ptr<SSLCertChain> cert_chain_;
Henrik Kjellanderec78f1c2017-06-29 05:52:5096
97 RTC_DISALLOW_COPY_AND_ASSIGN(OpenSSLIdentity);
98};
99
Henrik Kjellanderec78f1c2017-06-29 05:52:50100} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26101
Mirko Bonadei92ea95e2017-09-15 04:47:31102#endif // RTC_BASE_OPENSSLIDENTITY_H_