blob: 00d6c7492285484fb4416ffa5a906f3601568997 [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
Steve Anton10542f22019-01-11 17:11:0011#ifndef RTC_BASE_OPENSSL_IDENTITY_H_
12#define RTC_BASE_OPENSSL_IDENTITY_H_
henrike@webrtc.orgf0488722014-05-13 18:00:2613
Yves Gerey988cc082018-10-23 10:03:0114#include <openssl/ossl_typ.h>
henrike@webrtc.orgf0488722014-05-13 18:00:2615
Yves Gerey988cc082018-10-23 10:03:0116#include <ctime>
17#include <memory>
Henrik Kjellanderec78f1c2017-06-29 05:52:5018#include <string>
19
Steve Anton10542f22019-01-11 17:11:0020#include "rtc_base/constructor_magic.h"
21#include "rtc_base/openssl_certificate.h"
Taylor Brandstetter165c6182020-12-11 00:23:0322#include "rtc_base/openssl_key_pair.h"
Steve Anton10542f22019-01-11 17:11:0023#include "rtc_base/ssl_certificate.h"
24#include "rtc_base/ssl_identity.h"
Henrik Kjellanderec78f1c2017-06-29 05:52:5025
Henrik Kjellanderec78f1c2017-06-29 05:52:5026namespace rtc {
27
Henrik Kjellanderec78f1c2017-06-29 05:52:5028// Holds a keypair and certificate together, and a method to generate
29// them consistently.
Benjamin Wright61c5cc82018-10-27 00:50:0030class OpenSSLIdentity final : public SSLIdentity {
Henrik Kjellanderec78f1c2017-06-29 05:52:5031 public:
Harald Alvestrand8515d5a2020-03-20 21:51:3232 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 Kjellanderec78f1c2017-06-29 05:52:5044 ~OpenSSLIdentity() override;
45
46 const OpenSSLCertificate& certificate() const override;
Taylor Brandstetterc3928662018-02-23 21:04:5147 const SSLCertChain& cert_chain() const override;
Henrik Kjellanderec78f1c2017-06-29 05:52:5048
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 Cui0a8798b2017-11-17 00:58:0258 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 Alvestrand8515d5a2020-03-20 21:51:3262 std::unique_ptr<SSLIdentity> CloneInternal() const override;
Henrik Kjellanderec78f1c2017-06-29 05:52:5063
Harald Alvestrand8515d5a2020-03-20 21:51:3264 static std::unique_ptr<OpenSSLIdentity> CreateInternal(
65 const SSLIdentityParams& params);
Henrik Kjellanderec78f1c2017-06-29 05:52:5066
67 std::unique_ptr<OpenSSLKeyPair> key_pair_;
Jian Cui0a8798b2017-11-17 00:58:0268 std::unique_ptr<SSLCertChain> cert_chain_;
Henrik Kjellanderec78f1c2017-06-29 05:52:5069
70 RTC_DISALLOW_COPY_AND_ASSIGN(OpenSSLIdentity);
71};
72
Henrik Kjellanderec78f1c2017-06-29 05:52:5073} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:2674
Steve Anton10542f22019-01-11 17:11:0075#endif // RTC_BASE_OPENSSL_IDENTITY_H_