blob: 3f1b8c82f93a2216f1c7c4f5c880b43f8346fe86 [file] [log] [blame]
Benjamin Wrightd6f86e82018-05-08 20:12:251/*
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 17:11:0011#ifndef RTC_BASE_OPENSSL_CERTIFICATE_H_
12#define RTC_BASE_OPENSSL_CERTIFICATE_H_
Benjamin Wrightd6f86e82018-05-08 20:12:2513
Yves Gerey988cc082018-10-23 10:03:0114#include <openssl/ossl_typ.h>
Yves Gerey988cc082018-10-23 10:03:0115#include <stddef.h>
16#include <stdint.h>
Jonas Olssona4d87372019-07-05 17:08:3317
Benjamin Wrightd6f86e82018-05-08 20:12:2518#include <string>
19
Yves Gerey988cc082018-10-23 10:03:0120#include "rtc_base/buffer.h"
Steve Anton10542f22019-01-11 17:11:0021#include "rtc_base/ssl_certificate.h"
22#include "rtc_base/ssl_identity.h"
Benjamin Wrightd6f86e82018-05-08 20:12:2523
Benjamin Wrightd6f86e82018-05-08 20:12:2524namespace rtc {
25
26class OpenSSLKeyPair;
27
28// OpenSSLCertificate encapsulates an OpenSSL X509* certificate object,
29// which is also reference counted inside the OpenSSL library.
Benjamin Wright61c5cc82018-10-27 00:50:0030class OpenSSLCertificate final : public SSLCertificate {
Benjamin Wrightd6f86e82018-05-08 20:12:2531 public:
32 // X509 object has its reference count incremented. So the caller and
33 // OpenSSLCertificate share ownership.
34 explicit OpenSSLCertificate(X509* x509);
35
Steve Antonf25303e2018-10-16 22:23:3136 static std::unique_ptr<OpenSSLCertificate> Generate(
37 OpenSSLKeyPair* key_pair,
38 const SSLIdentityParams& params);
39 static std::unique_ptr<OpenSSLCertificate> FromPEMString(
Ali Tofigh58d861c2022-03-24 23:51:2040 absl::string_view pem_string);
Benjamin Wrightd6f86e82018-05-08 20:12:2541
42 ~OpenSSLCertificate() override;
43
Artem Titov6cae2d52022-01-26 15:01:1044 OpenSSLCertificate(const OpenSSLCertificate&) = delete;
45 OpenSSLCertificate& operator=(const OpenSSLCertificate&) = delete;
46
Steve Antonf25303e2018-10-16 22:23:3147 std::unique_ptr<SSLCertificate> Clone() const override;
Benjamin Wrightd6f86e82018-05-08 20:12:2548
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
Ali Tofigh58d861c2022-03-24 23:51:2057 bool ComputeDigest(absl::string_view algorithm,
Benjamin Wrightd6f86e82018-05-08 20:12:2558 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,
Ali Tofigh58d861c2022-03-24 23:51:2064 absl::string_view algorithm,
Benjamin Wrightd6f86e82018-05-08 20:12:2565 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 20:12:2574 X509* x509_; // NOT OWNED
Benjamin Wrightd6f86e82018-05-08 20:12:2575};
76
77} // namespace rtc
78
Steve Anton10542f22019-01-11 17:11:0079#endif // RTC_BASE_OPENSSL_CERTIFICATE_H_