| 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 | #include "rtc_base/rtc_certificate.h" |
| 12 |
| 13 | #include <memory> |
| 14 |
| 15 | #include "rtc_base/checks.h" |
| 16 | #include "rtc_base/ssl_certificate.h" |
| 17 | #include "rtc_base/ssl_identity.h" |
| 18 | #include "rtc_base/time_utils.h" |
| 19 |
| 20 | namespace rtc { |
| 21 |
| 22 | scoped_refptr<RTCCertificate> RTCCertificate::Create( |
| 23 | std::unique_ptr<SSLIdentity> identity) { |
| 24 | // Explicit new to access proteced constructor. |
| 25 | return rtc::scoped_refptr<RTCCertificate>( |
| 26 | new RTCCertificate(identity.release())); |
| 27 | } |
| 28 |
| 29 | RTCCertificate::RTCCertificate(SSLIdentity* identity) : identity_(identity) { |
| 30 | RTC_DCHECK(identity_); |
| 31 | } |
| 32 |
| 33 | RTCCertificate::~RTCCertificate() = default; |
| 34 |
| 35 | uint64_t RTCCertificate::Expires() const { |
| 36 | int64_t expires = GetSSLCertificate().CertificateExpirationTime(); |
| 37 | if (expires != -1) |
| 38 | return static_cast<uint64_t>(expires) * kNumMillisecsPerSec; |
| 39 | // If the expiration time could not be retrieved return an expired timestamp. |
| 40 | return 0; // = 1970-01-01 |
| 41 | } |
| 42 |
| 43 | bool RTCCertificate::HasExpired(uint64_t now) const { |
| 44 | return Expires() <= now; |
| 45 | } |
| 46 |
| 47 | const SSLCertificate& RTCCertificate::GetSSLCertificate() const { |
| 48 | return identity_->certificate(); |
| 49 | } |
| 50 |
| 51 | const SSLCertChain& RTCCertificate::GetSSLCertificateChain() const { |
| 52 | return identity_->cert_chain(); |
| 53 | } |
| 54 |
| 55 | RTCCertificatePEM RTCCertificate::ToPEM() const { |
| 56 | return RTCCertificatePEM(identity_->PrivateKeyToPEMString(), |
| 57 | GetSSLCertificate().ToPEMString()); |
| 58 | } |
| 59 |
| 60 | scoped_refptr<RTCCertificate> RTCCertificate::FromPEM( |
| 61 | const RTCCertificatePEM& pem) { |
| 62 | std::unique_ptr<SSLIdentity> identity( |
| 63 | SSLIdentity::CreateFromPEMStrings(pem.private_key(), pem.certificate())); |
| 64 | if (!identity) |
| 65 | return nullptr; |
| 66 | return RTCCertificate::Create(std::move(identity)); |
| 67 | } |
| 68 |
| 69 | bool RTCCertificate::operator==(const RTCCertificate& certificate) const { |
| 70 | return *this->identity_ == *certificate.identity_; |
| 71 | } |
| 72 |
| 73 | bool RTCCertificate::operator!=(const RTCCertificate& certificate) const { |
| 74 | return !(*this == certificate); |
| 75 | } |
| 76 |
| 77 | } // namespace rtc |
| 78 |