blob: a700a1d107c463faf54e23964d2c92d6b848a659 [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
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <openssl/evp.h>
15#include <openssl/x509.h>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000016
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020017#include <memory>
18#include <string>
19
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/checks.h"
21#include "rtc_base/constructormagic.h"
22#include "rtc_base/sslidentity.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020023
24typedef struct ssl_ctx_st SSL_CTX;
25
26namespace rtc {
27
28// OpenSSLKeyPair encapsulates an OpenSSL EVP_PKEY* keypair object,
29// which is reference counted inside the OpenSSL library.
30class OpenSSLKeyPair {
31 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
59// OpenSSLCertificate encapsulates an OpenSSL X509* certificate object,
60// which is also reference counted inside the OpenSSL library.
61class OpenSSLCertificate : public SSLCertificate {
62 public:
63 // Caller retains ownership of the X509 object.
Jian Cui0a8798b2017-11-16 16:58:02 -080064 explicit OpenSSLCertificate(X509* x509);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020065
66 static OpenSSLCertificate* Generate(OpenSSLKeyPair* key_pair,
67 const SSLIdentityParams& params);
68 static OpenSSLCertificate* FromPEMString(const std::string& pem_string);
69
70 ~OpenSSLCertificate() override;
71
72 OpenSSLCertificate* GetReference() const override;
73
74 X509* x509() const { return x509_; }
75
76 std::string ToPEMString() const override;
77 void ToDER(Buffer* der_buffer) const override;
78 bool operator==(const OpenSSLCertificate& other) const;
79 bool operator!=(const OpenSSLCertificate& other) const;
80
81 // Compute the digest of the certificate given algorithm
82 bool ComputeDigest(const std::string& algorithm,
83 unsigned char* digest,
84 size_t size,
85 size_t* length) const override;
86
87 // Compute the digest of a certificate as an X509 *
88 static bool ComputeDigest(const X509* x509,
89 const std::string& algorithm,
90 unsigned char* digest,
91 size_t size,
92 size_t* length);
93
94 bool GetSignatureDigestAlgorithm(std::string* algorithm) const override;
95 std::unique_ptr<SSLCertChain> GetChain() const override;
96
97 int64_t CertificateExpirationTime() const override;
98
99 private:
100 void AddReference() const;
101
102 X509* x509_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200103 RTC_DISALLOW_COPY_AND_ASSIGN(OpenSSLCertificate);
104};
105
106// Holds a keypair and certificate together, and a method to generate
107// them consistently.
108class OpenSSLIdentity : public SSLIdentity {
109 public:
110 static OpenSSLIdentity* GenerateWithExpiration(const std::string& common_name,
111 const KeyParams& key_params,
112 time_t certificate_lifetime);
113 static OpenSSLIdentity* GenerateForTest(const SSLIdentityParams& params);
114 static SSLIdentity* FromPEMStrings(const std::string& private_key,
115 const std::string& certificate);
Jian Cui0a8798b2017-11-16 16:58:02 -0800116 static SSLIdentity* FromPEMChainStrings(const std::string& private_key,
117 const std::string& certificate_chain);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200118 ~OpenSSLIdentity() override;
119
120 const OpenSSLCertificate& certificate() const override;
121 OpenSSLIdentity* GetReference() const override;
122
123 // Configure an SSL context object to use our key and certificate.
124 bool ConfigureIdentity(SSL_CTX* ctx);
125
126 std::string PrivateKeyToPEMString() const override;
127 std::string PublicKeyToPEMString() const override;
128 bool operator==(const OpenSSLIdentity& other) const;
129 bool operator!=(const OpenSSLIdentity& other) const;
130
131 private:
Jian Cui0a8798b2017-11-16 16:58:02 -0800132 OpenSSLIdentity(std::unique_ptr<OpenSSLKeyPair> key_pair,
133 std::unique_ptr<OpenSSLCertificate> certificate);
134 OpenSSLIdentity(std::unique_ptr<OpenSSLKeyPair> key_pair,
135 std::unique_ptr<SSLCertChain> cert_chain);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200136
137 static OpenSSLIdentity* GenerateInternal(const SSLIdentityParams& params);
138
139 std::unique_ptr<OpenSSLKeyPair> key_pair_;
Jian Cui0a8798b2017-11-16 16:58:02 -0800140 std::unique_ptr<SSLCertChain> cert_chain_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200141
142 RTC_DISALLOW_COPY_AND_ASSIGN(OpenSSLIdentity);
143};
144
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200145} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000146
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200147#endif // RTC_BASE_OPENSSLIDENTITY_H_