Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 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 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #ifndef RTC_BASE_OPENSSL_CERTIFICATE_H_ |
| 12 | #define RTC_BASE_OPENSSL_CERTIFICATE_H_ |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 14 | #include <openssl/ossl_typ.h> |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 15 | #include <stddef.h> |
| 16 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 17 | |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 18 | #include <string> |
| 19 | |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 20 | #include "rtc_base/buffer.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 21 | #include "rtc_base/ssl_certificate.h" |
| 22 | #include "rtc_base/ssl_identity.h" |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 23 | |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 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. |
Benjamin Wright | 61c5cc8 | 2018-10-27 00:50:00 | [diff] [blame] | 30 | class OpenSSLCertificate final : public SSLCertificate { |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 31 | public: |
| 32 | // X509 object has its reference count incremented. So the caller and |
| 33 | // OpenSSLCertificate share ownership. |
| 34 | explicit OpenSSLCertificate(X509* x509); |
| 35 | |
Steve Anton | f25303e | 2018-10-16 22:23:31 | [diff] [blame] | 36 | static std::unique_ptr<OpenSSLCertificate> Generate( |
| 37 | OpenSSLKeyPair* key_pair, |
| 38 | const SSLIdentityParams& params); |
| 39 | static std::unique_ptr<OpenSSLCertificate> FromPEMString( |
Ali Tofigh | 58d861c | 2022-03-24 23:51:20 | [diff] [blame] | 40 | absl::string_view pem_string); |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 41 | |
| 42 | ~OpenSSLCertificate() override; |
| 43 | |
Artem Titov | 6cae2d5 | 2022-01-26 15:01:10 | [diff] [blame] | 44 | OpenSSLCertificate(const OpenSSLCertificate&) = delete; |
| 45 | OpenSSLCertificate& operator=(const OpenSSLCertificate&) = delete; |
| 46 | |
Steve Anton | f25303e | 2018-10-16 22:23:31 | [diff] [blame] | 47 | std::unique_ptr<SSLCertificate> Clone() const override; |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 48 | |
| 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 Tofigh | 58d861c | 2022-03-24 23:51:20 | [diff] [blame] | 57 | bool ComputeDigest(absl::string_view algorithm, |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 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, |
Ali Tofigh | 58d861c | 2022-03-24 23:51:20 | [diff] [blame] | 64 | absl::string_view algorithm, |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 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 Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 74 | X509* x509_; // NOT OWNED |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | } // namespace rtc |
| 78 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 79 | #endif // RTC_BASE_OPENSSL_CERTIFICATE_H_ |