Henrik Boström | 41b3a38 | 2015-08-20 12:15:54 +0200 | [diff] [blame] | 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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #include "rtc_base/rtc_certificate.h" |
Henrik Boström | 41b3a38 | 2015-08-20 12:15:54 +0200 | [diff] [blame] | 12 | |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 13 | #include <memory> |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 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" |
Henrik Boström | 41b3a38 | 2015-08-20 12:15:54 +0200 | [diff] [blame] | 20 | |
| 21 | namespace rtc { |
| 22 | |
| 23 | scoped_refptr<RTCCertificate> RTCCertificate::Create( |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 24 | std::unique_ptr<SSLIdentity> identity) { |
Henrik Boström | 41b3a38 | 2015-08-20 12:15:54 +0200 | [diff] [blame] | 25 | return new RefCountedObject<RTCCertificate>(identity.release()); |
| 26 | } |
| 27 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 28 | RTCCertificate::RTCCertificate(SSLIdentity* identity) : identity_(identity) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 29 | RTC_DCHECK(identity_); |
Henrik Boström | 41b3a38 | 2015-08-20 12:15:54 +0200 | [diff] [blame] | 30 | } |
| 31 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 32 | RTCCertificate::~RTCCertificate() {} |
Henrik Boström | 41b3a38 | 2015-08-20 12:15:54 +0200 | [diff] [blame] | 33 | |
hbos | 3980d46 | 2015-12-09 05:26:49 -0800 | [diff] [blame] | 34 | uint64_t RTCCertificate::Expires() const { |
Benjamin Wright | 6c6c9df | 2018-10-25 01:16:26 -0700 | [diff] [blame] | 35 | int64_t expires = GetSSLCertificate().CertificateExpirationTime(); |
hbos | 3980d46 | 2015-12-09 05:26:49 -0800 | [diff] [blame] | 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 |
Henrik Boström | 41b3a38 | 2015-08-20 12:15:54 +0200 | [diff] [blame] | 40 | } |
| 41 | |
hbos | 3980d46 | 2015-12-09 05:26:49 -0800 | [diff] [blame] | 42 | bool RTCCertificate::HasExpired(uint64_t now) const { |
| 43 | return Expires() <= now; |
Henrik Boström | 41b3a38 | 2015-08-20 12:15:54 +0200 | [diff] [blame] | 44 | } |
| 45 | |
Benjamin Wright | 6c6c9df | 2018-10-25 01:16:26 -0700 | [diff] [blame] | 46 | const SSLCertificate& RTCCertificate::GetSSLCertificate() const { |
| 47 | return identity_->certificate(); |
| 48 | } |
| 49 | |
| 50 | // Deprecated: TODO(benwright) - Remove once chromium is updated. |
Henrik Boström | 41b3a38 | 2015-08-20 12:15:54 +0200 | [diff] [blame] | 51 | const SSLCertificate& RTCCertificate::ssl_certificate() const { |
| 52 | return identity_->certificate(); |
| 53 | } |
| 54 | |
Benjamin Wright | 6c6c9df | 2018-10-25 01:16:26 -0700 | [diff] [blame] | 55 | const SSLCertChain& RTCCertificate::GetSSLCertificateChain() const { |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 56 | return identity_->cert_chain(); |
| 57 | } |
| 58 | |
hbos | 6b470a9 | 2016-04-28 05:14:21 -0700 | [diff] [blame] | 59 | RTCCertificatePEM RTCCertificate::ToPEM() const { |
| 60 | return RTCCertificatePEM(identity_->PrivateKeyToPEMString(), |
Benjamin Wright | 6c6c9df | 2018-10-25 01:16:26 -0700 | [diff] [blame] | 61 | GetSSLCertificate().ToPEMString()); |
hbos | 6b470a9 | 2016-04-28 05:14:21 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | scoped_refptr<RTCCertificate> RTCCertificate::FromPEM( |
| 65 | const RTCCertificatePEM& pem) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 66 | std::unique_ptr<SSLIdentity> identity( |
| 67 | SSLIdentity::FromPEMStrings(pem.private_key(), pem.certificate())); |
jbroman | b9eaeba | 2016-10-20 10:27:21 -0700 | [diff] [blame] | 68 | if (!identity) |
| 69 | return nullptr; |
hbos | 6b470a9 | 2016-04-28 05:14:21 -0700 | [diff] [blame] | 70 | return new RefCountedObject<RTCCertificate>(identity.release()); |
| 71 | } |
| 72 | |
| 73 | bool RTCCertificate::operator==(const RTCCertificate& certificate) const { |
| 74 | return *this->identity_ == *certificate.identity_; |
| 75 | } |
| 76 | |
| 77 | bool RTCCertificate::operator!=(const RTCCertificate& certificate) const { |
| 78 | return !(*this == certificate); |
| 79 | } |
| 80 | |
Henrik Boström | 41b3a38 | 2015-08-20 12:15:54 +0200 | [diff] [blame] | 81 | } // namespace rtc |