blob: 63f46b374def52ac3d3d81247611569ad0525fe9 [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
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/openssl_certificate.h"
Taylor Brandstetter165c6182020-12-10 16:23:03 -080021#include "rtc_base/openssl_key_pair.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "rtc_base/ssl_certificate.h"
23#include "rtc_base/ssl_identity.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020024
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020025namespace rtc {
26
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020027// Holds a keypair and certificate together, and a method to generate
28// them consistently.
Benjamin Wright61c5cc82018-10-26 17:50:00 -070029class OpenSSLIdentity final : public SSLIdentity {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020030 public:
Harald Alvestrand8515d5a2020-03-20 22:51:32 +010031 static std::unique_ptr<OpenSSLIdentity> CreateWithExpiration(
32 const std::string& common_name,
33 const KeyParams& key_params,
34 time_t certificate_lifetime);
35 static std::unique_ptr<OpenSSLIdentity> CreateForTest(
36 const SSLIdentityParams& params);
37 static std::unique_ptr<SSLIdentity> CreateFromPEMStrings(
38 const std::string& private_key,
39 const std::string& certificate);
40 static std::unique_ptr<SSLIdentity> CreateFromPEMChainStrings(
41 const std::string& private_key,
42 const std::string& certificate_chain);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020043 ~OpenSSLIdentity() override;
44
Artem Titov6cae2d52022-01-26 15:01:10 +000045 OpenSSLIdentity(const OpenSSLIdentity&) = delete;
46 OpenSSLIdentity& operator=(const OpenSSLIdentity&) = delete;
47
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020048 const OpenSSLCertificate& certificate() const override;
Taylor Brandstetterc3928662018-02-23 13:04:51 -080049 const SSLCertChain& cert_chain() const override;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020050
51 // Configure an SSL context object to use our key and certificate.
52 bool ConfigureIdentity(SSL_CTX* ctx);
53
54 std::string PrivateKeyToPEMString() const override;
55 std::string PublicKeyToPEMString() const override;
56 bool operator==(const OpenSSLIdentity& other) const;
57 bool operator!=(const OpenSSLIdentity& other) const;
58
59 private:
Jian Cui0a8798b2017-11-16 16:58:02 -080060 OpenSSLIdentity(std::unique_ptr<OpenSSLKeyPair> key_pair,
61 std::unique_ptr<OpenSSLCertificate> certificate);
62 OpenSSLIdentity(std::unique_ptr<OpenSSLKeyPair> key_pair,
63 std::unique_ptr<SSLCertChain> cert_chain);
Harald Alvestrand8515d5a2020-03-20 22:51:32 +010064 std::unique_ptr<SSLIdentity> CloneInternal() const override;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020065
Harald Alvestrand8515d5a2020-03-20 22:51:32 +010066 static std::unique_ptr<OpenSSLIdentity> CreateInternal(
67 const SSLIdentityParams& params);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020068
69 std::unique_ptr<OpenSSLKeyPair> key_pair_;
Jian Cui0a8798b2017-11-16 16:58:02 -080070 std::unique_ptr<SSLCertChain> cert_chain_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020071};
72
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020073} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000074
Steve Anton10542f22019-01-11 09:11:00 -080075#endif // RTC_BASE_OPENSSL_IDENTITY_H_