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/constructor_magic.h" 22 #include "rtc_base/ssl_certificate.h" 23 #include "rtc_base/ssl_identity.h" 24 25 namespace rtc { 26 27 class OpenSSLKeyPair; 28 29 // OpenSSLCertificate encapsulates an OpenSSL X509* certificate object, 30 // which is also reference counted inside the OpenSSL library. 31 class OpenSSLCertificate final : public SSLCertificate { 32 public: 33 // X509 object has its reference count incremented. So the caller and 34 // OpenSSLCertificate share ownership. 35 explicit OpenSSLCertificate(X509* x509); 36 37 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); 42 43 ~OpenSSLCertificate() override; 44 45 std::unique_ptr<SSLCertificate> Clone() const override; 46 x509()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: 72 X509* x509_; // NOT OWNED 73 RTC_DISALLOW_COPY_AND_ASSIGN(OpenSSLCertificate); 74 }; 75 76 } // namespace rtc 77 78 #endif // RTC_BASE_OPENSSL_CERTIFICATE_H_ 79