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