blob: 823f5388ae24c340f36d1796e7abc37bc4ea2947 [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
20#include "webrtc/base/common.h"
kwiberg4485ffb2016-04-26 08:14:39 -070021#include "webrtc/base/constructormagic.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000022#include "webrtc/base/sslidentity.h"
23
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 ASSERT(pkey_ != NULL);
34 }
35
torbjorng4e572472015-10-08 09:42:49 -070036 static OpenSSLKeyPair* Generate(const KeyParams& key_params);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000037
38 virtual ~OpenSSLKeyPair();
39
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000040 virtual OpenSSLKeyPair* GetReference();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000041
42 EVP_PKEY* pkey() const { return pkey_; }
43
44 private:
45 void AddReference();
46
47 EVP_PKEY* pkey_;
48
henrikg3c089d72015-09-16 05:37:44 -070049 RTC_DISALLOW_COPY_AND_ASSIGN(OpenSSLKeyPair);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000050};
51
52// OpenSSLCertificate encapsulates an OpenSSL X509* certificate object,
53// which is also reference counted inside the OpenSSL library.
54class OpenSSLCertificate : public SSLCertificate {
55 public:
56 // Caller retains ownership of the X509 object.
57 explicit OpenSSLCertificate(X509* x509) : x509_(x509) {
58 AddReference();
59 }
60
61 static OpenSSLCertificate* Generate(OpenSSLKeyPair* key_pair,
62 const SSLIdentityParams& params);
63 static OpenSSLCertificate* FromPEMString(const std::string& pem_string);
64
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000065 ~OpenSSLCertificate() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000066
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000067 OpenSSLCertificate* GetReference() const override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000068
69 X509* x509() const { return x509_; }
70
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000071 std::string ToPEMString() const override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000072
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000073 void ToDER(Buffer* der_buffer) const override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000074
75 // Compute the digest of the certificate given algorithm
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000076 bool ComputeDigest(const std::string& algorithm,
77 unsigned char* digest,
78 size_t size,
79 size_t* length) const override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000080
81 // Compute the digest of a certificate as an X509 *
82 static bool ComputeDigest(const X509* x509,
83 const std::string& algorithm,
84 unsigned char* digest,
85 size_t size,
86 size_t* length);
87
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000088 bool GetSignatureDigestAlgorithm(std::string* algorithm) const override;
jbauch555604a2016-04-26 03:13:22 -070089 std::unique_ptr<SSLCertChain> GetChain() const override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000090
Torbjorn Granlund46c9cc02015-12-01 13:06:34 +010091 int64_t CertificateExpirationTime() const override;
92
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000093 private:
94 void AddReference() const;
95
96 X509* x509_;
97
henrikg3c089d72015-09-16 05:37:44 -070098 RTC_DISALLOW_COPY_AND_ASSIGN(OpenSSLCertificate);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000099};
100
101// Holds a keypair and certificate together, and a method to generate
102// them consistently.
103class OpenSSLIdentity : public SSLIdentity {
104 public:
Torbjorn Granlund1d846b22016-03-31 16:21:04 +0200105 static OpenSSLIdentity* GenerateWithExpiration(const std::string& common_name,
106 const KeyParams& key_params,
107 time_t certificate_lifetime);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000108 static OpenSSLIdentity* GenerateForTest(const SSLIdentityParams& params);
109 static SSLIdentity* FromPEMStrings(const std::string& private_key,
110 const std::string& certificate);
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000111 ~OpenSSLIdentity() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000112
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000113 const OpenSSLCertificate& certificate() const override;
114 OpenSSLIdentity* GetReference() const override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000115
116 // Configure an SSL context object to use our key and certificate.
117 bool ConfigureIdentity(SSL_CTX* ctx);
118
119 private:
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000120 OpenSSLIdentity(OpenSSLKeyPair* key_pair, OpenSSLCertificate* certificate);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000121
122 static OpenSSLIdentity* GenerateInternal(const SSLIdentityParams& params);
123
jbauch555604a2016-04-26 03:13:22 -0700124 std::unique_ptr<OpenSSLKeyPair> key_pair_;
125 std::unique_ptr<OpenSSLCertificate> certificate_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000126
henrikg3c089d72015-09-16 05:37:44 -0700127 RTC_DISALLOW_COPY_AND_ASSIGN(OpenSSLIdentity);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000128};
129
130
131} // namespace rtc
132
133#endif // WEBRTC_BASE_OPENSSLIDENTITY_H_