blob: 9b61eee8eadd6fa575438b9606c0ae70f7bf26af [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
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef RTC_BASE_OPENSSL_CERTIFICATE_H_
12#define RTC_BASE_OPENSSL_CERTIFICATE_H_
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070013
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <openssl/ossl_typ.h>
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070015
Yves Gerey988cc082018-10-23 12:03:01 +020016#include <stddef.h>
17#include <stdint.h>
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070018#include <string>
19
Yves Gerey988cc082018-10-23 12:03:01 +020020#include "rtc_base/buffer.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "rtc_base/constructor_magic.h"
22#include "rtc_base/ssl_certificate.h"
23#include "rtc_base/ssl_identity.h"
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070024
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070025namespace rtc {
26
27class OpenSSLKeyPair;
28
29// OpenSSLCertificate encapsulates an OpenSSL X509* certificate object,
30// which is also reference counted inside the OpenSSL library.
Benjamin Wright61c5cc82018-10-26 17:50:00 -070031class OpenSSLCertificate final : public SSLCertificate {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070032 public:
33 // X509 object has its reference count incremented. So the caller and
34 // OpenSSLCertificate share ownership.
35 explicit OpenSSLCertificate(X509* x509);
36
Steve Antonf25303e2018-10-16 15:23:31 -070037 static std::unique_ptr<OpenSSLCertificate> Generate(
38 OpenSSLKeyPair* key_pair,
39 const SSLIdentityParams& params);
40 static std::unique_ptr<OpenSSLCertificate> FromPEMString(
41 const std::string& pem_string);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070042
43 ~OpenSSLCertificate() override;
44
Steve Antonf25303e2018-10-16 15:23:31 -070045 std::unique_ptr<SSLCertificate> Clone() const override;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070046
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:
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070072 X509* x509_; // NOT OWNED
73 RTC_DISALLOW_COPY_AND_ASSIGN(OpenSSLCertificate);
74};
75
76} // namespace rtc
77
Steve Anton10542f22019-01-11 09:11:00 -080078#endif // RTC_BASE_OPENSSL_CERTIFICATE_H_