blob: c730ffd0dc9e769c6bd0f97d4ee1e7bdc5429ff2 [file] [log] [blame]
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001/*
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 RTC_BASE_OPENSSLCERTIFICATE_H_
12#define RTC_BASE_OPENSSLCERTIFICATE_H_
13
14#include <openssl/evp.h>
15#include <openssl/x509.h>
16
17#include <memory>
18#include <string>
19
20#include "rtc_base/checks.h"
21#include "rtc_base/constructormagic.h"
22#include "rtc_base/sslcertificate.h"
23#include "rtc_base/sslidentity.h"
24
25typedef struct ssl_ctx_st SSL_CTX;
26
27namespace rtc {
28
29class OpenSSLKeyPair;
30
31// OpenSSLCertificate encapsulates an OpenSSL X509* certificate object,
32// which is also reference counted inside the OpenSSL library.
33class OpenSSLCertificate : public SSLCertificate {
34 public:
35 // X509 object has its reference count incremented. So the caller and
36 // OpenSSLCertificate share ownership.
37 explicit OpenSSLCertificate(X509* x509);
38
39 static OpenSSLCertificate* Generate(OpenSSLKeyPair* key_pair,
40 const SSLIdentityParams& params);
41 static OpenSSLCertificate* FromPEMString(const std::string& pem_string);
42
43 ~OpenSSLCertificate() override;
44
45 OpenSSLCertificate* GetReference() const override;
46
47 X509* x509() const { return x509_; }
48
49 std::string ToPEMString() const override;
50 void ToDER(Buffer* der_buffer) const override;
51 bool operator==(const OpenSSLCertificate& other) const;
52 bool operator!=(const OpenSSLCertificate& other) const;
53
54 // Compute the digest of the certificate given algorithm
55 bool ComputeDigest(const std::string& algorithm,
56 unsigned char* digest,
57 size_t size,
58 size_t* length) const override;
59
60 // Compute the digest of a certificate as an X509 *
61 static bool ComputeDigest(const X509* x509,
62 const std::string& algorithm,
63 unsigned char* digest,
64 size_t size,
65 size_t* length);
66
67 bool GetSignatureDigestAlgorithm(std::string* algorithm) const override;
68
69 int64_t CertificateExpirationTime() const override;
70
71 private:
72 void AddReference() const;
73
74 X509* x509_; // NOT OWNED
75 RTC_DISALLOW_COPY_AND_ASSIGN(OpenSSLCertificate);
76};
77
78} // namespace rtc
79
80#endif // RTC_BASE_OPENSSLCERTIFICATE_H_