blob: 1c3122b38e0c74ae616557bf50ddc891355b0e82 [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
11#ifndef WEBRTC_BASE_OPENSSLIDENTITY_H_
12#define WEBRTC_BASE_OPENSSLIDENTITY_H_
13
14#include <openssl/evp.h>
15#include <openssl/x509.h>
16
jbauch555604a2016-04-26 03:13:22 -070017#include <memory>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000018#include <string>
19
nisseede5da42017-01-12 05:15:36 -080020#include "webrtc/base/checks.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000021#include "webrtc/base/common.h"
kwiberg4485ffb2016-04-26 08:14:39 -070022#include "webrtc/base/constructormagic.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000023#include "webrtc/base/sslidentity.h"
24
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) {
nisseede5da42017-01-12 05:15:36 -080034 RTC_DCHECK(pkey_ != NULL);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000035 }
36
torbjorng4e572472015-10-08 09:42:49 -070037 static OpenSSLKeyPair* Generate(const KeyParams& key_params);
hbos6b470a92016-04-28 05:14:21 -070038 // Constructs a key pair from the private key PEM string. This must not result
39 // in missing public key parameters. Returns null on error.
40 static OpenSSLKeyPair* FromPrivateKeyPEMString(
41 const std::string& pem_string);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000042
43 virtual ~OpenSSLKeyPair();
44
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000045 virtual OpenSSLKeyPair* GetReference();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000046
47 EVP_PKEY* pkey() const { return pkey_; }
hbos6b470a92016-04-28 05:14:21 -070048 std::string PrivateKeyToPEMString() const;
49 std::string PublicKeyToPEMString() const;
50 bool operator==(const OpenSSLKeyPair& other) const;
51 bool operator!=(const OpenSSLKeyPair& other) const;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000052
53 private:
54 void AddReference();
55
56 EVP_PKEY* pkey_;
57
henrikg3c089d72015-09-16 05:37:44 -070058 RTC_DISALLOW_COPY_AND_ASSIGN(OpenSSLKeyPair);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000059};
60
61// OpenSSLCertificate encapsulates an OpenSSL X509* certificate object,
62// which is also reference counted inside the OpenSSL library.
63class OpenSSLCertificate : public SSLCertificate {
64 public:
65 // Caller retains ownership of the X509 object.
66 explicit OpenSSLCertificate(X509* x509) : x509_(x509) {
67 AddReference();
68 }
69
70 static OpenSSLCertificate* Generate(OpenSSLKeyPair* key_pair,
71 const SSLIdentityParams& params);
72 static OpenSSLCertificate* FromPEMString(const std::string& pem_string);
73
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000074 ~OpenSSLCertificate() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000075
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000076 OpenSSLCertificate* GetReference() const override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000077
78 X509* x509() const { return x509_; }
79
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000080 std::string ToPEMString() const override;
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000081 void ToDER(Buffer* der_buffer) const override;
hbos6b470a92016-04-28 05:14:21 -070082 bool operator==(const OpenSSLCertificate& other) const;
83 bool operator!=(const OpenSSLCertificate& other) const;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000084
85 // Compute the digest of the certificate given algorithm
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000086 bool ComputeDigest(const std::string& algorithm,
87 unsigned char* digest,
88 size_t size,
89 size_t* length) const override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000090
91 // Compute the digest of a certificate as an X509 *
92 static bool ComputeDigest(const X509* x509,
93 const std::string& algorithm,
94 unsigned char* digest,
95 size_t size,
96 size_t* length);
97
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000098 bool GetSignatureDigestAlgorithm(std::string* algorithm) const override;
jbauch555604a2016-04-26 03:13:22 -070099 std::unique_ptr<SSLCertChain> GetChain() const override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000100
Torbjorn Granlund46c9cc02015-12-01 13:06:34 +0100101 int64_t CertificateExpirationTime() const override;
102
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000103 private:
104 void AddReference() const;
105
106 X509* x509_;
107
henrikg3c089d72015-09-16 05:37:44 -0700108 RTC_DISALLOW_COPY_AND_ASSIGN(OpenSSLCertificate);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000109};
110
111// Holds a keypair and certificate together, and a method to generate
112// them consistently.
113class OpenSSLIdentity : public SSLIdentity {
114 public:
Torbjorn Granlund1d846b22016-03-31 16:21:04 +0200115 static OpenSSLIdentity* GenerateWithExpiration(const std::string& common_name,
116 const KeyParams& key_params,
117 time_t certificate_lifetime);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000118 static OpenSSLIdentity* GenerateForTest(const SSLIdentityParams& params);
119 static SSLIdentity* FromPEMStrings(const std::string& private_key,
120 const std::string& certificate);
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000121 ~OpenSSLIdentity() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000122
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000123 const OpenSSLCertificate& certificate() const override;
124 OpenSSLIdentity* GetReference() const override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000125
126 // Configure an SSL context object to use our key and certificate.
127 bool ConfigureIdentity(SSL_CTX* ctx);
128
hbos6b470a92016-04-28 05:14:21 -0700129 std::string PrivateKeyToPEMString() const override;
130 std::string PublicKeyToPEMString() const override;
131 bool operator==(const OpenSSLIdentity& other) const;
132 bool operator!=(const OpenSSLIdentity& other) const;
133
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000134 private:
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000135 OpenSSLIdentity(OpenSSLKeyPair* key_pair, OpenSSLCertificate* certificate);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000136
137 static OpenSSLIdentity* GenerateInternal(const SSLIdentityParams& params);
138
jbauch555604a2016-04-26 03:13:22 -0700139 std::unique_ptr<OpenSSLKeyPair> key_pair_;
140 std::unique_ptr<OpenSSLCertificate> certificate_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000141
henrikg3c089d72015-09-16 05:37:44 -0700142 RTC_DISALLOW_COPY_AND_ASSIGN(OpenSSLIdentity);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000143};
144
145
146} // namespace rtc
147
148#endif // WEBRTC_BASE_OPENSSLIDENTITY_H_