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