You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.4 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "rtc_base/rtc_certificate.h"
  11. #include <memory>
  12. #include "rtc_base/checks.h"
  13. #include "rtc_base/ref_counted_object.h"
  14. #include "rtc_base/ssl_certificate.h"
  15. #include "rtc_base/ssl_identity.h"
  16. #include "rtc_base/time_utils.h"
  17. namespace rtc {
  18. scoped_refptr<RTCCertificate> RTCCertificate::Create(
  19. std::unique_ptr<SSLIdentity> identity) {
  20. return new RefCountedObject<RTCCertificate>(identity.release());
  21. }
  22. RTCCertificate::RTCCertificate(SSLIdentity* identity) : identity_(identity) {
  23. RTC_DCHECK(identity_);
  24. }
  25. RTCCertificate::~RTCCertificate() {}
  26. uint64_t RTCCertificate::Expires() const {
  27. int64_t expires = GetSSLCertificate().CertificateExpirationTime();
  28. if (expires != -1)
  29. return static_cast<uint64_t>(expires) * kNumMillisecsPerSec;
  30. // If the expiration time could not be retrieved return an expired timestamp.
  31. return 0; // = 1970-01-01
  32. }
  33. bool RTCCertificate::HasExpired(uint64_t now) const {
  34. return Expires() <= now;
  35. }
  36. const SSLCertificate& RTCCertificate::GetSSLCertificate() const {
  37. return identity_->certificate();
  38. }
  39. // Deprecated: TODO(benwright) - Remove once chromium is updated.
  40. const SSLCertificate& RTCCertificate::ssl_certificate() const {
  41. return identity_->certificate();
  42. }
  43. const SSLCertChain& RTCCertificate::GetSSLCertificateChain() const {
  44. return identity_->cert_chain();
  45. }
  46. RTCCertificatePEM RTCCertificate::ToPEM() const {
  47. return RTCCertificatePEM(identity_->PrivateKeyToPEMString(),
  48. GetSSLCertificate().ToPEMString());
  49. }
  50. scoped_refptr<RTCCertificate> RTCCertificate::FromPEM(
  51. const RTCCertificatePEM& pem) {
  52. std::unique_ptr<SSLIdentity> identity(
  53. SSLIdentity::FromPEMStrings(pem.private_key(), pem.certificate()));
  54. if (!identity)
  55. return nullptr;
  56. return new RefCountedObject<RTCCertificate>(identity.release());
  57. }
  58. bool RTCCertificate::operator==(const RTCCertificate& certificate) const {
  59. return *this->identity_ == *certificate.identity_;
  60. }
  61. bool RTCCertificate::operator!=(const RTCCertificate& certificate) const {
  62. return !(*this == certificate);
  63. }
  64. } // namespace rtc