2015-08-20 12:15:54 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright 2015 The WebRTC Project Authors. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "rtc_base/rtc_certificate.h"
|
2015-08-20 12:15:54 +02:00
|
|
|
|
2016-04-26 03:13:22 -07:00
|
|
|
#include <memory>
|
2019-07-05 19:08:33 +02:00
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/checks.h"
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "rtc_base/ssl_certificate.h"
|
|
|
|
|
#include "rtc_base/ssl_identity.h"
|
|
|
|
|
#include "rtc_base/time_utils.h"
|
2015-08-20 12:15:54 +02:00
|
|
|
|
|
|
|
|
namespace rtc {
|
|
|
|
|
|
|
|
|
|
scoped_refptr<RTCCertificate> RTCCertificate::Create(
|
2016-04-26 03:13:22 -07:00
|
|
|
std::unique_ptr<SSLIdentity> identity) {
|
2024-08-09 11:56:33 -07:00
|
|
|
// Explicit new to access protected constructor.
|
2022-01-13 11:00:05 +01:00
|
|
|
return rtc::scoped_refptr<RTCCertificate>(
|
|
|
|
|
new RTCCertificate(identity.release()));
|
2015-08-20 12:15:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RTCCertificate::RTCCertificate(SSLIdentity* identity) : identity_(identity) {
|
2015-09-17 00:24:34 -07:00
|
|
|
RTC_DCHECK(identity_);
|
2015-08-20 12:15:54 +02:00
|
|
|
}
|
|
|
|
|
|
2021-04-20 16:58:01 +02:00
|
|
|
RTCCertificate::~RTCCertificate() = default;
|
2015-08-20 12:15:54 +02:00
|
|
|
|
2015-12-09 05:26:49 -08:00
|
|
|
uint64_t RTCCertificate::Expires() const {
|
2018-10-25 01:16:26 -07:00
|
|
|
int64_t expires = GetSSLCertificate().CertificateExpirationTime();
|
2015-12-09 05:26:49 -08:00
|
|
|
if (expires != -1)
|
|
|
|
|
return static_cast<uint64_t>(expires) * kNumMillisecsPerSec;
|
|
|
|
|
// If the expiration time could not be retrieved return an expired timestamp.
|
|
|
|
|
return 0; // = 1970-01-01
|
2015-08-20 12:15:54 +02:00
|
|
|
}
|
|
|
|
|
|
2015-12-09 05:26:49 -08:00
|
|
|
bool RTCCertificate::HasExpired(uint64_t now) const {
|
|
|
|
|
return Expires() <= now;
|
2015-08-20 12:15:54 +02:00
|
|
|
}
|
|
|
|
|
|
2018-10-25 01:16:26 -07:00
|
|
|
const SSLCertificate& RTCCertificate::GetSSLCertificate() const {
|
|
|
|
|
return identity_->certificate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const SSLCertChain& RTCCertificate::GetSSLCertificateChain() const {
|
2018-02-23 13:04:51 -08:00
|
|
|
return identity_->cert_chain();
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-28 05:14:21 -07:00
|
|
|
RTCCertificatePEM RTCCertificate::ToPEM() const {
|
|
|
|
|
return RTCCertificatePEM(identity_->PrivateKeyToPEMString(),
|
2018-10-25 01:16:26 -07:00
|
|
|
GetSSLCertificate().ToPEMString());
|
2016-04-28 05:14:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scoped_refptr<RTCCertificate> RTCCertificate::FromPEM(
|
|
|
|
|
const RTCCertificatePEM& pem) {
|
|
|
|
|
std::unique_ptr<SSLIdentity> identity(
|
2020-03-20 22:51:32 +01:00
|
|
|
SSLIdentity::CreateFromPEMStrings(pem.private_key(), pem.certificate()));
|
2016-10-20 10:27:21 -07:00
|
|
|
if (!identity)
|
|
|
|
|
return nullptr;
|
2022-01-13 11:00:05 +01:00
|
|
|
return RTCCertificate::Create(std::move(identity));
|
2016-04-28 05:14:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool RTCCertificate::operator==(const RTCCertificate& certificate) const {
|
|
|
|
|
return *this->identity_ == *certificate.identity_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool RTCCertificate::operator!=(const RTCCertificate& certificate) const {
|
|
|
|
|
return !(*this == certificate);
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-20 12:15:54 +02:00
|
|
|
} // namespace rtc
|