• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/ref_counted_object.h"
17 #include "rtc_base/ssl_certificate.h"
18 #include "rtc_base/ssl_identity.h"
19 #include "rtc_base/time_utils.h"
20 
21 namespace rtc {
22 
Create(std::unique_ptr<SSLIdentity> identity)23 scoped_refptr<RTCCertificate> RTCCertificate::Create(
24     std::unique_ptr<SSLIdentity> identity) {
25   return new RefCountedObject<RTCCertificate>(identity.release());
26 }
27 
RTCCertificate(SSLIdentity * identity)28 RTCCertificate::RTCCertificate(SSLIdentity* identity) : identity_(identity) {
29   RTC_DCHECK(identity_);
30 }
31 
~RTCCertificate()32 RTCCertificate::~RTCCertificate() {}
33 
Expires() const34 uint64_t RTCCertificate::Expires() const {
35   int64_t expires = GetSSLCertificate().CertificateExpirationTime();
36   if (expires != -1)
37     return static_cast<uint64_t>(expires) * kNumMillisecsPerSec;
38   // If the expiration time could not be retrieved return an expired timestamp.
39   return 0;  // = 1970-01-01
40 }
41 
HasExpired(uint64_t now) const42 bool RTCCertificate::HasExpired(uint64_t now) const {
43   return Expires() <= now;
44 }
45 
GetSSLCertificate() const46 const SSLCertificate& RTCCertificate::GetSSLCertificate() const {
47   return identity_->certificate();
48 }
49 
50 // Deprecated: TODO(benwright) - Remove once chromium is updated.
ssl_certificate() const51 const SSLCertificate& RTCCertificate::ssl_certificate() const {
52   return identity_->certificate();
53 }
54 
GetSSLCertificateChain() const55 const SSLCertChain& RTCCertificate::GetSSLCertificateChain() const {
56   return identity_->cert_chain();
57 }
58 
ToPEM() const59 RTCCertificatePEM RTCCertificate::ToPEM() const {
60   return RTCCertificatePEM(identity_->PrivateKeyToPEMString(),
61                            GetSSLCertificate().ToPEMString());
62 }
63 
FromPEM(const RTCCertificatePEM & pem)64 scoped_refptr<RTCCertificate> RTCCertificate::FromPEM(
65     const RTCCertificatePEM& pem) {
66   std::unique_ptr<SSLIdentity> identity(
67       SSLIdentity::CreateFromPEMStrings(pem.private_key(), pem.certificate()));
68   if (!identity)
69     return nullptr;
70   return new RefCountedObject<RTCCertificate>(identity.release());
71 }
72 
operator ==(const RTCCertificate & certificate) const73 bool RTCCertificate::operator==(const RTCCertificate& certificate) const {
74   return *this->identity_ == *certificate.identity_;
75 }
76 
operator !=(const RTCCertificate & certificate) const77 bool RTCCertificate::operator!=(const RTCCertificate& certificate) const {
78   return !(*this == certificate);
79 }
80 
81 }  // namespace rtc
82