1*d9f75844SAndroid Build Coastguard Worker /* 2*d9f75844SAndroid Build Coastguard Worker * Copyright 2015 The WebRTC Project Authors. All rights reserved. 3*d9f75844SAndroid Build Coastguard Worker * 4*d9f75844SAndroid Build Coastguard Worker * Use of this source code is governed by a BSD-style license 5*d9f75844SAndroid Build Coastguard Worker * that can be found in the LICENSE file in the root of the source 6*d9f75844SAndroid Build Coastguard Worker * tree. An additional intellectual property rights grant can be found 7*d9f75844SAndroid Build Coastguard Worker * in the file PATENTS. All contributing project authors may 8*d9f75844SAndroid Build Coastguard Worker * be found in the AUTHORS file in the root of the source tree. 9*d9f75844SAndroid Build Coastguard Worker */ 10*d9f75844SAndroid Build Coastguard Worker 11*d9f75844SAndroid Build Coastguard Worker #ifndef RTC_BASE_RTC_CERTIFICATE_H_ 12*d9f75844SAndroid Build Coastguard Worker #define RTC_BASE_RTC_CERTIFICATE_H_ 13*d9f75844SAndroid Build Coastguard Worker 14*d9f75844SAndroid Build Coastguard Worker #include <stdint.h> 15*d9f75844SAndroid Build Coastguard Worker 16*d9f75844SAndroid Build Coastguard Worker #include <memory> 17*d9f75844SAndroid Build Coastguard Worker #include <string> 18*d9f75844SAndroid Build Coastguard Worker 19*d9f75844SAndroid Build Coastguard Worker #include "absl/base/attributes.h" 20*d9f75844SAndroid Build Coastguard Worker #include "absl/strings/string_view.h" 21*d9f75844SAndroid Build Coastguard Worker #include "api/ref_counted_base.h" 22*d9f75844SAndroid Build Coastguard Worker #include "api/scoped_refptr.h" 23*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/system/rtc_export.h" 24*d9f75844SAndroid Build Coastguard Worker 25*d9f75844SAndroid Build Coastguard Worker namespace rtc { 26*d9f75844SAndroid Build Coastguard Worker 27*d9f75844SAndroid Build Coastguard Worker class SSLCertChain; 28*d9f75844SAndroid Build Coastguard Worker class SSLCertificate; 29*d9f75844SAndroid Build Coastguard Worker class SSLIdentity; 30*d9f75844SAndroid Build Coastguard Worker 31*d9f75844SAndroid Build Coastguard Worker // This class contains PEM strings of an RTCCertificate's private key and 32*d9f75844SAndroid Build Coastguard Worker // certificate and acts as a text representation of RTCCertificate. Certificates 33*d9f75844SAndroid Build Coastguard Worker // can be serialized and deserialized to and from this format, which allows for 34*d9f75844SAndroid Build Coastguard Worker // cloning and storing of certificates to disk. The PEM format is that of 35*d9f75844SAndroid Build Coastguard Worker // `SSLIdentity::PrivateKeyToPEMString` and `SSLCertificate::ToPEMString`, e.g. 36*d9f75844SAndroid Build Coastguard Worker // the string representations used by OpenSSL. 37*d9f75844SAndroid Build Coastguard Worker class RTCCertificatePEM { 38*d9f75844SAndroid Build Coastguard Worker public: RTCCertificatePEM(absl::string_view private_key,absl::string_view certificate)39*d9f75844SAndroid Build Coastguard Worker RTCCertificatePEM(absl::string_view private_key, 40*d9f75844SAndroid Build Coastguard Worker absl::string_view certificate) 41*d9f75844SAndroid Build Coastguard Worker : private_key_(private_key), certificate_(certificate) {} 42*d9f75844SAndroid Build Coastguard Worker private_key()43*d9f75844SAndroid Build Coastguard Worker const std::string& private_key() const { return private_key_; } certificate()44*d9f75844SAndroid Build Coastguard Worker const std::string& certificate() const { return certificate_; } 45*d9f75844SAndroid Build Coastguard Worker 46*d9f75844SAndroid Build Coastguard Worker private: 47*d9f75844SAndroid Build Coastguard Worker std::string private_key_; 48*d9f75844SAndroid Build Coastguard Worker std::string certificate_; 49*d9f75844SAndroid Build Coastguard Worker }; 50*d9f75844SAndroid Build Coastguard Worker 51*d9f75844SAndroid Build Coastguard Worker // A thin abstraction layer between "lower level crypto stuff" like 52*d9f75844SAndroid Build Coastguard Worker // SSLCertificate and WebRTC usage. Takes ownership of some lower level objects, 53*d9f75844SAndroid Build Coastguard Worker // reference counting protects these from premature destruction. 54*d9f75844SAndroid Build Coastguard Worker class RTC_EXPORT RTCCertificate final 55*d9f75844SAndroid Build Coastguard Worker : public RefCountedNonVirtual<RTCCertificate> { 56*d9f75844SAndroid Build Coastguard Worker public: 57*d9f75844SAndroid Build Coastguard Worker // Takes ownership of `identity`. 58*d9f75844SAndroid Build Coastguard Worker static scoped_refptr<RTCCertificate> Create( 59*d9f75844SAndroid Build Coastguard Worker std::unique_ptr<SSLIdentity> identity); 60*d9f75844SAndroid Build Coastguard Worker 61*d9f75844SAndroid Build Coastguard Worker // Returns the expiration time in ms relative to epoch, 1970-01-01T00:00:00Z. 62*d9f75844SAndroid Build Coastguard Worker uint64_t Expires() const; 63*d9f75844SAndroid Build Coastguard Worker // Checks if the certificate has expired, where `now` is expressed in ms 64*d9f75844SAndroid Build Coastguard Worker // relative to epoch, 1970-01-01T00:00:00Z. 65*d9f75844SAndroid Build Coastguard Worker bool HasExpired(uint64_t now) const; 66*d9f75844SAndroid Build Coastguard Worker 67*d9f75844SAndroid Build Coastguard Worker const SSLCertificate& GetSSLCertificate() const; 68*d9f75844SAndroid Build Coastguard Worker const SSLCertChain& GetSSLCertificateChain() const; 69*d9f75844SAndroid Build Coastguard Worker 70*d9f75844SAndroid Build Coastguard Worker // TODO(hbos): If possible, remove once RTCCertificate and its 71*d9f75844SAndroid Build Coastguard Worker // GetSSLCertificate() is used in all relevant places. Should not pass around 72*d9f75844SAndroid Build Coastguard Worker // raw SSLIdentity* for the sake of accessing SSLIdentity::certificate(). 73*d9f75844SAndroid Build Coastguard Worker // However, some places might need SSLIdentity* for its public/private key... identity()74*d9f75844SAndroid Build Coastguard Worker SSLIdentity* identity() const { return identity_.get(); } 75*d9f75844SAndroid Build Coastguard Worker 76*d9f75844SAndroid Build Coastguard Worker // To/from PEM, a text representation of the RTCCertificate. 77*d9f75844SAndroid Build Coastguard Worker RTCCertificatePEM ToPEM() const; 78*d9f75844SAndroid Build Coastguard Worker // Can return nullptr if the certificate is invalid. 79*d9f75844SAndroid Build Coastguard Worker static scoped_refptr<RTCCertificate> FromPEM(const RTCCertificatePEM& pem); 80*d9f75844SAndroid Build Coastguard Worker bool operator==(const RTCCertificate& certificate) const; 81*d9f75844SAndroid Build Coastguard Worker bool operator!=(const RTCCertificate& certificate) const; 82*d9f75844SAndroid Build Coastguard Worker 83*d9f75844SAndroid Build Coastguard Worker protected: 84*d9f75844SAndroid Build Coastguard Worker explicit RTCCertificate(SSLIdentity* identity); 85*d9f75844SAndroid Build Coastguard Worker 86*d9f75844SAndroid Build Coastguard Worker friend class RefCountedNonVirtual<RTCCertificate>; 87*d9f75844SAndroid Build Coastguard Worker ~RTCCertificate(); 88*d9f75844SAndroid Build Coastguard Worker 89*d9f75844SAndroid Build Coastguard Worker private: 90*d9f75844SAndroid Build Coastguard Worker // The SSLIdentity is the owner of the SSLCertificate. To protect our 91*d9f75844SAndroid Build Coastguard Worker // GetSSLCertificate() we take ownership of `identity_`. 92*d9f75844SAndroid Build Coastguard Worker const std::unique_ptr<SSLIdentity> identity_; 93*d9f75844SAndroid Build Coastguard Worker }; 94*d9f75844SAndroid Build Coastguard Worker 95*d9f75844SAndroid Build Coastguard Worker } // namespace rtc 96*d9f75844SAndroid Build Coastguard Worker 97*d9f75844SAndroid Build Coastguard Worker #endif // RTC_BASE_RTC_CERTIFICATE_H_ 98