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.

140 lines
4.9 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 <time.h>
  12. #include <memory>
  13. #include <utility>
  14. #include "rtc_base/checks.h"
  15. #include "rtc_base/numerics/safe_conversions.h"
  16. #include "rtc_base/ssl_identity.h"
  17. #include "rtc_base/time_utils.h"
  18. #include "test/gtest.h"
  19. namespace rtc {
  20. namespace {
  21. static const char* kTestCertCommonName = "RTCCertificateTest's certificate";
  22. } // namespace
  23. class RTCCertificateTest : public ::testing::Test {
  24. protected:
  25. scoped_refptr<RTCCertificate> GenerateECDSA() {
  26. std::unique_ptr<SSLIdentity> identity(
  27. SSLIdentity::Generate(kTestCertCommonName, KeyParams::ECDSA()));
  28. RTC_CHECK(identity);
  29. return RTCCertificate::Create(std::move(identity));
  30. }
  31. // Timestamp note:
  32. // All timestamps in this unittest are expressed in number of seconds since
  33. // epoch, 1970-01-01T00:00:00Z (UTC). The RTCCertificate interface uses ms,
  34. // but only seconds-precision is supported by SSLCertificate. To make the
  35. // tests clearer we convert everything to seconds since the precision matters
  36. // when generating certificates or comparing timestamps.
  37. // As a result, ExpiresSeconds and HasExpiredSeconds are used instead of
  38. // RTCCertificate::Expires and ::HasExpired for ms -> s conversion.
  39. uint64_t NowSeconds() const { return TimeNanos() / kNumNanosecsPerSec; }
  40. uint64_t ExpiresSeconds(const scoped_refptr<RTCCertificate>& cert) const {
  41. uint64_t exp_ms = cert->Expires();
  42. uint64_t exp_s = exp_ms / kNumMillisecsPerSec;
  43. // Make sure this did not result in loss of precision.
  44. RTC_CHECK_EQ(exp_s * kNumMillisecsPerSec, exp_ms);
  45. return exp_s;
  46. }
  47. bool HasExpiredSeconds(const scoped_refptr<RTCCertificate>& cert,
  48. uint64_t now_s) const {
  49. return cert->HasExpired(now_s * kNumMillisecsPerSec);
  50. }
  51. // An RTC_CHECK ensures that |expires_s| this is in valid range of time_t as
  52. // is required by SSLIdentityParams. On some 32-bit systems time_t is limited
  53. // to < 2^31. On such systems this will fail for expiration times of year 2038
  54. // or later.
  55. scoped_refptr<RTCCertificate> GenerateCertificateWithExpires(
  56. uint64_t expires_s) const {
  57. RTC_CHECK(IsValueInRangeForNumericType<time_t>(expires_s));
  58. SSLIdentityParams params;
  59. params.common_name = kTestCertCommonName;
  60. params.not_before = 0;
  61. params.not_after = static_cast<time_t>(expires_s);
  62. // Certificate type does not matter for our purposes, using ECDSA because it
  63. // is fast to generate.
  64. params.key_params = KeyParams::ECDSA();
  65. std::unique_ptr<SSLIdentity> identity(SSLIdentity::GenerateForTest(params));
  66. return RTCCertificate::Create(std::move(identity));
  67. }
  68. };
  69. TEST_F(RTCCertificateTest, NewCertificateNotExpired) {
  70. // Generate a real certificate without specifying the expiration time.
  71. // Certificate type doesn't matter, using ECDSA because it's fast to generate.
  72. scoped_refptr<RTCCertificate> certificate = GenerateECDSA();
  73. uint64_t now = NowSeconds();
  74. EXPECT_FALSE(HasExpiredSeconds(certificate, now));
  75. // Even without specifying the expiration time we would expect it to be valid
  76. // for at least half an hour.
  77. EXPECT_FALSE(HasExpiredSeconds(certificate, now + 30 * 60));
  78. }
  79. TEST_F(RTCCertificateTest, UsesExpiresAskedFor) {
  80. uint64_t now = NowSeconds();
  81. scoped_refptr<RTCCertificate> certificate =
  82. GenerateCertificateWithExpires(now);
  83. EXPECT_EQ(now, ExpiresSeconds(certificate));
  84. }
  85. TEST_F(RTCCertificateTest, ExpiresInOneSecond) {
  86. // Generate a certificate that expires in 1s.
  87. uint64_t now = NowSeconds();
  88. scoped_refptr<RTCCertificate> certificate =
  89. GenerateCertificateWithExpires(now + 1);
  90. // Now it should not have expired.
  91. EXPECT_FALSE(HasExpiredSeconds(certificate, now));
  92. // In 2s it should have expired.
  93. EXPECT_TRUE(HasExpiredSeconds(certificate, now + 2));
  94. }
  95. TEST_F(RTCCertificateTest, DifferentCertificatesNotEqual) {
  96. scoped_refptr<RTCCertificate> a = GenerateECDSA();
  97. scoped_refptr<RTCCertificate> b = GenerateECDSA();
  98. EXPECT_TRUE(*a != *b);
  99. }
  100. TEST_F(RTCCertificateTest, CloneWithPEMSerialization) {
  101. scoped_refptr<RTCCertificate> orig = GenerateECDSA();
  102. // To PEM.
  103. RTCCertificatePEM orig_pem = orig->ToPEM();
  104. // Clone from PEM.
  105. scoped_refptr<RTCCertificate> clone = RTCCertificate::FromPEM(orig_pem);
  106. EXPECT_TRUE(clone);
  107. EXPECT_TRUE(*orig == *clone);
  108. EXPECT_EQ(orig->Expires(), clone->Expires());
  109. }
  110. TEST_F(RTCCertificateTest, FromPEMWithInvalidPEM) {
  111. RTCCertificatePEM pem("not a valid PEM", "not a valid PEM");
  112. scoped_refptr<RTCCertificate> certificate = RTCCertificate::FromPEM(pem);
  113. EXPECT_FALSE(certificate);
  114. }
  115. } // namespace rtc