xref: /aosp_15_r20/external/webrtc/rtc_base/openssl_certificate.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
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_OPENSSL_CERTIFICATE_H_
12 #define RTC_BASE_OPENSSL_CERTIFICATE_H_
13 
14 #include <openssl/ossl_typ.h>
15 #include <stddef.h>
16 #include <stdint.h>
17 
18 #include <string>
19 
20 #include "rtc_base/buffer.h"
21 #include "rtc_base/ssl_certificate.h"
22 #include "rtc_base/ssl_identity.h"
23 
24 namespace rtc {
25 
26 class OpenSSLKeyPair;
27 
28 // OpenSSLCertificate encapsulates an OpenSSL X509* certificate object,
29 // which is also reference counted inside the OpenSSL library.
30 class OpenSSLCertificate final : public SSLCertificate {
31  public:
32   // X509 object has its reference count incremented. So the caller and
33   // OpenSSLCertificate share ownership.
34   explicit OpenSSLCertificate(X509* x509);
35 
36   static std::unique_ptr<OpenSSLCertificate> Generate(
37       OpenSSLKeyPair* key_pair,
38       const SSLIdentityParams& params);
39   static std::unique_ptr<OpenSSLCertificate> FromPEMString(
40       absl::string_view pem_string);
41 
42   ~OpenSSLCertificate() override;
43 
44   OpenSSLCertificate(const OpenSSLCertificate&) = delete;
45   OpenSSLCertificate& operator=(const OpenSSLCertificate&) = delete;
46 
47   std::unique_ptr<SSLCertificate> Clone() const override;
48 
x509()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(absl::string_view 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                             absl::string_view 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:
74   X509* x509_;  // NOT OWNED
75 };
76 
77 }  // namespace rtc
78 
79 #endif  // RTC_BASE_OPENSSL_CERTIFICATE_H_
80