xref: /aosp_15_r20/external/webrtc/rtc_base/rtc_certificate.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright 2015 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 #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 
Create(std::unique_ptr<SSLIdentity> identity)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 
RTCCertificate(SSLIdentity * identity)29 RTCCertificate::RTCCertificate(SSLIdentity* identity) : identity_(identity) {
30   RTC_DCHECK(identity_);
31 }
32 
33 RTCCertificate::~RTCCertificate() = default;
34 
Expires() const35 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 
HasExpired(uint64_t now) const43 bool RTCCertificate::HasExpired(uint64_t now) const {
44   return Expires() <= now;
45 }
46 
GetSSLCertificate() const47 const SSLCertificate& RTCCertificate::GetSSLCertificate() const {
48   return identity_->certificate();
49 }
50 
GetSSLCertificateChain() const51 const SSLCertChain& RTCCertificate::GetSSLCertificateChain() const {
52   return identity_->cert_chain();
53 }
54 
ToPEM() const55 RTCCertificatePEM RTCCertificate::ToPEM() const {
56   return RTCCertificatePEM(identity_->PrivateKeyToPEMString(),
57                            GetSSLCertificate().ToPEMString());
58 }
59 
FromPEM(const RTCCertificatePEM & pem)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 
operator ==(const RTCCertificate & certificate) const69 bool RTCCertificate::operator==(const RTCCertificate& certificate) const {
70   return *this->identity_ == *certificate.identity_;
71 }
72 
operator !=(const RTCCertificate & certificate) const73 bool RTCCertificate::operator!=(const RTCCertificate& certificate) const {
74   return !(*this == certificate);
75 }
76 
77 }  // namespace rtc
78