blob: 3b49f93ef52c8738a85b31eaf9a2bcb9632bb8b7 [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
Yves Gerey2e00abc2018-10-05 15:39:24 +020014#include <openssl/base.h> // for X509, ssl_ctx_st
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070015
Yves Gerey2e00abc2018-10-05 15:39:24 +020016#include <stddef.h> // for size_t
17#include <stdint.h> // for int64_t
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070018#include <string>
19
Yves Gerey2e00abc2018-10-05 15:39:24 +020020#include "rtc_base/buffer.h" // for Buffer
21#include "rtc_base/constructormagic.h" // for RTC_DISALLOW_COPY_AND_ASSIGN
22#include "rtc_base/sslcertificate.h" // for SSLCertificate
23#include "rtc_base/sslidentity.h" // for SSLIdentityParams
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070024
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
Steve Anton55cd3ac2018-10-11 13:32:35 -070039 static std::unique_ptr<OpenSSLCertificate> Generate(
40 OpenSSLKeyPair* key_pair,
41 const SSLIdentityParams& params);
42 static std::unique_ptr<OpenSSLCertificate> FromPEMString(
43 const std::string& pem_string);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070044
45 ~OpenSSLCertificate() override;
46
Steve Anton55cd3ac2018-10-11 13:32:35 -070047 std::unique_ptr<SSLCertificate> Clone() const override;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070048
49 X509* x509() const { return x509_; }
50
51 std::string ToPEMString() const override;
52 void ToDER(Buffer* der_buffer) const override;
53 bool operator==(const OpenSSLCertificate& other) const;
54 bool operator!=(const OpenSSLCertificate& other) const;
55
56 // Compute the digest of the certificate given algorithm
57 bool ComputeDigest(const std::string& algorithm,
58 unsigned char* digest,
59 size_t size,
60 size_t* length) const override;
61
62 // Compute the digest of a certificate as an X509 *
63 static bool ComputeDigest(const X509* x509,
64 const std::string& algorithm,
65 unsigned char* digest,
66 size_t size,
67 size_t* length);
68
69 bool GetSignatureDigestAlgorithm(std::string* algorithm) const override;
70
71 int64_t CertificateExpirationTime() const override;
72
73 private:
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070074 X509* x509_; // NOT OWNED
75 RTC_DISALLOW_COPY_AND_ASSIGN(OpenSSLCertificate);
76};
77
78} // namespace rtc
79
80#endif // RTC_BASE_OPENSSLCERTIFICATE_H_