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/ssl_certificate.h" |
| 17 | #include "rtc_base/ssl_identity.h" |
| 18 | #include "rtc_base/time_utils.h" |
Henrik Boström | 41b3a38 | 2015-08-20 12:15:54 +0200 | [diff] [blame] | 19 | |
| 20 | namespace rtc { |
| 21 | |
| 22 | scoped_refptr<RTCCertificate> RTCCertificate::Create( |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 23 | std::unique_ptr<SSLIdentity> identity) { |
Tommi | 86ee89f | 2021-04-20 16:58:01 +0200 | [diff] [blame] | 24 | return new RTCCertificate(identity.release()); |
Henrik Boström | 41b3a38 | 2015-08-20 12:15:54 +0200 | [diff] [blame] | 25 | } |
| 26 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 27 | RTCCertificate::RTCCertificate(SSLIdentity* identity) : identity_(identity) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 28 | RTC_DCHECK(identity_); |
Henrik Boström | 41b3a38 | 2015-08-20 12:15:54 +0200 | [diff] [blame] | 29 | } |
| 30 | |
Tommi | 86ee89f | 2021-04-20 16:58:01 +0200 | [diff] [blame] | 31 | RTCCertificate::~RTCCertificate() = default; |
Henrik Boström | 41b3a38 | 2015-08-20 12:15:54 +0200 | [diff] [blame] | 32 | |
hbos | 3980d46 | 2015-12-09 05:26:49 -0800 | [diff] [blame] | 33 | uint64_t RTCCertificate::Expires() const { |
Benjamin Wright | 6c6c9df | 2018-10-25 01:16:26 -0700 | [diff] [blame] | 34 | int64_t expires = GetSSLCertificate().CertificateExpirationTime(); |
hbos | 3980d46 | 2015-12-09 05:26:49 -0800 | [diff] [blame] | 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 |
Henrik Boström | 41b3a38 | 2015-08-20 12:15:54 +0200 | [diff] [blame] | 39 | } |
| 40 | |
hbos | 3980d46 | 2015-12-09 05:26:49 -0800 | [diff] [blame] | 41 | bool RTCCertificate::HasExpired(uint64_t now) const { |
| 42 | return Expires() <= now; |
Henrik Boström | 41b3a38 | 2015-08-20 12:15:54 +0200 | [diff] [blame] | 43 | } |
| 44 | |
Benjamin Wright | 6c6c9df | 2018-10-25 01:16:26 -0700 | [diff] [blame] | 45 | const SSLCertificate& RTCCertificate::GetSSLCertificate() const { |
| 46 | return identity_->certificate(); |
| 47 | } |
| 48 | |
Benjamin Wright | 6c6c9df | 2018-10-25 01:16:26 -0700 | [diff] [blame] | 49 | const SSLCertChain& RTCCertificate::GetSSLCertificateChain() const { |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 50 | return identity_->cert_chain(); |
| 51 | } |
| 52 | |
hbos | 6b470a9 | 2016-04-28 05:14:21 -0700 | [diff] [blame] | 53 | RTCCertificatePEM RTCCertificate::ToPEM() const { |
| 54 | return RTCCertificatePEM(identity_->PrivateKeyToPEMString(), |
Benjamin Wright | 6c6c9df | 2018-10-25 01:16:26 -0700 | [diff] [blame] | 55 | GetSSLCertificate().ToPEMString()); |
hbos | 6b470a9 | 2016-04-28 05:14:21 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | scoped_refptr<RTCCertificate> RTCCertificate::FromPEM( |
| 59 | const RTCCertificatePEM& pem) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 60 | std::unique_ptr<SSLIdentity> identity( |
Harald Alvestrand | 8515d5a | 2020-03-20 22:51:32 +0100 | [diff] [blame] | 61 | SSLIdentity::CreateFromPEMStrings(pem.private_key(), pem.certificate())); |
jbroman | b9eaeba | 2016-10-20 10:27:21 -0700 | [diff] [blame] | 62 | if (!identity) |
| 63 | return nullptr; |
Tommi | 86ee89f | 2021-04-20 16:58:01 +0200 | [diff] [blame] | 64 | return new RTCCertificate(identity.release()); |
hbos | 6b470a9 | 2016-04-28 05:14:21 -0700 | [diff] [blame] | 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 | |
Henrik Boström | 41b3a38 | 2015-08-20 12:15:54 +0200 | [diff] [blame] | 75 | } // namespace rtc |