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