blob: d8ba138f625484fddca35a38ff3f449eeea86832 [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
11#ifndef WEBRTC_BASE_OPENSSLIDENTITY_H_
12#define WEBRTC_BASE_OPENSSLIDENTITY_H_
13
14#include <openssl/evp.h>
15#include <openssl/x509.h>
16
17#include <string>
18
19#include "webrtc/base/common.h"
20#include "webrtc/base/scoped_ptr.h"
21#include "webrtc/base/sslidentity.h"
22
23typedef struct ssl_ctx_st SSL_CTX;
24
25namespace rtc {
26
27// OpenSSLKeyPair encapsulates an OpenSSL EVP_PKEY* keypair object,
28// which is reference counted inside the OpenSSL library.
29class OpenSSLKeyPair {
30 public:
31 explicit OpenSSLKeyPair(EVP_PKEY* pkey) : pkey_(pkey) {
32 ASSERT(pkey_ != NULL);
33 }
34
Torbjorn Granlundb6d4ec42015-08-17 12:08:5935 static OpenSSLKeyPair* Generate(KeyType key_type);
henrike@webrtc.orgf0488722014-05-13 18:00:2636
37 virtual ~OpenSSLKeyPair();
38
kwiberg@webrtc.org67186fe2015-03-09 22:21:5339 virtual OpenSSLKeyPair* GetReference();
henrike@webrtc.orgf0488722014-05-13 18:00:2640
41 EVP_PKEY* pkey() const { return pkey_; }
42
43 private:
44 void AddReference();
45
46 EVP_PKEY* pkey_;
47
henrikg3c089d72015-09-16 12:37:4448 RTC_DISALLOW_COPY_AND_ASSIGN(OpenSSLKeyPair);
henrike@webrtc.orgf0488722014-05-13 18:00:2649};
50
51// OpenSSLCertificate encapsulates an OpenSSL X509* certificate object,
52// which is also reference counted inside the OpenSSL library.
53class OpenSSLCertificate : public SSLCertificate {
54 public:
55 // Caller retains ownership of the X509 object.
56 explicit OpenSSLCertificate(X509* x509) : x509_(x509) {
57 AddReference();
58 }
59
60 static OpenSSLCertificate* Generate(OpenSSLKeyPair* key_pair,
61 const SSLIdentityParams& params);
62 static OpenSSLCertificate* FromPEMString(const std::string& pem_string);
63
kwiberg@webrtc.org67186fe2015-03-09 22:21:5364 ~OpenSSLCertificate() override;
henrike@webrtc.orgf0488722014-05-13 18:00:2665
kwiberg@webrtc.org67186fe2015-03-09 22:21:5366 OpenSSLCertificate* GetReference() const override;
henrike@webrtc.orgf0488722014-05-13 18:00:2667
68 X509* x509() const { return x509_; }
69
kwiberg@webrtc.org67186fe2015-03-09 22:21:5370 std::string ToPEMString() const override;
henrike@webrtc.orgf0488722014-05-13 18:00:2671
kwiberg@webrtc.org67186fe2015-03-09 22:21:5372 void ToDER(Buffer* der_buffer) const override;
henrike@webrtc.orgf0488722014-05-13 18:00:2673
74 // Compute the digest of the certificate given algorithm
kwiberg@webrtc.org67186fe2015-03-09 22:21:5375 bool ComputeDigest(const std::string& algorithm,
76 unsigned char* digest,
77 size_t size,
78 size_t* length) const override;
henrike@webrtc.orgf0488722014-05-13 18:00:2679
80 // Compute the digest of a certificate as an X509 *
81 static bool ComputeDigest(const X509* x509,
82 const std::string& algorithm,
83 unsigned char* digest,
84 size_t size,
85 size_t* length);
86
kwiberg@webrtc.org67186fe2015-03-09 22:21:5387 bool GetSignatureDigestAlgorithm(std::string* algorithm) const override;
88 bool GetChain(SSLCertChain** chain) const override;
henrike@webrtc.orgf0488722014-05-13 18:00:2689
90 private:
91 void AddReference() const;
92
93 X509* x509_;
94
henrikg3c089d72015-09-16 12:37:4495 RTC_DISALLOW_COPY_AND_ASSIGN(OpenSSLCertificate);
henrike@webrtc.orgf0488722014-05-13 18:00:2696};
97
98// Holds a keypair and certificate together, and a method to generate
99// them consistently.
100class OpenSSLIdentity : public SSLIdentity {
101 public:
Torbjorn Granlundb6d4ec42015-08-17 12:08:59102 static OpenSSLIdentity* Generate(const std::string& common_name,
103 KeyType key_type);
henrike@webrtc.orgf0488722014-05-13 18:00:26104 static OpenSSLIdentity* GenerateForTest(const SSLIdentityParams& params);
105 static SSLIdentity* FromPEMStrings(const std::string& private_key,
106 const std::string& certificate);
kwiberg@webrtc.org67186fe2015-03-09 22:21:53107 ~OpenSSLIdentity() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26108
kwiberg@webrtc.org67186fe2015-03-09 22:21:53109 const OpenSSLCertificate& certificate() const override;
110 OpenSSLIdentity* GetReference() const override;
henrike@webrtc.orgf0488722014-05-13 18:00:26111
112 // Configure an SSL context object to use our key and certificate.
113 bool ConfigureIdentity(SSL_CTX* ctx);
114
115 private:
kwiberg@webrtc.org67186fe2015-03-09 22:21:53116 OpenSSLIdentity(OpenSSLKeyPair* key_pair, OpenSSLCertificate* certificate);
henrike@webrtc.orgf0488722014-05-13 18:00:26117
118 static OpenSSLIdentity* GenerateInternal(const SSLIdentityParams& params);
119
120 scoped_ptr<OpenSSLKeyPair> key_pair_;
121 scoped_ptr<OpenSSLCertificate> certificate_;
122
henrikg3c089d72015-09-16 12:37:44123 RTC_DISALLOW_COPY_AND_ASSIGN(OpenSSLIdentity);
henrike@webrtc.orgf0488722014-05-13 18:00:26124};
125
126
127} // namespace rtc
128
129#endif // WEBRTC_BASE_OPENSSLIDENTITY_H_