henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 12 | #include <string> |
| 13 | |
| 14 | #include "webrtc/base/gunit.h" |
Torbjorn Granlund | 46c9cc0 | 2015-12-01 13:06:34 +0100 | [diff] [blame] | 15 | #include "webrtc/base/helpers.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 16 | #include "webrtc/base/ssladapter.h" |
| 17 | #include "webrtc/base/sslidentity.h" |
| 18 | |
| 19 | using rtc::SSLIdentity; |
| 20 | |
| 21 | const char kTestCertificate[] = "-----BEGIN CERTIFICATE-----\n" |
| 22 | "MIIB6TCCAVICAQYwDQYJKoZIhvcNAQEEBQAwWzELMAkGA1UEBhMCQVUxEzARBgNV\n" |
| 23 | "BAgTClF1ZWVuc2xhbmQxGjAYBgNVBAoTEUNyeXB0U29mdCBQdHkgTHRkMRswGQYD\n" |
| 24 | "VQQDExJUZXN0IENBICgxMDI0IGJpdCkwHhcNMDAxMDE2MjIzMTAzWhcNMDMwMTE0\n" |
| 25 | "MjIzMTAzWjBjMQswCQYDVQQGEwJBVTETMBEGA1UECBMKUXVlZW5zbGFuZDEaMBgG\n" |
| 26 | "A1UEChMRQ3J5cHRTb2Z0IFB0eSBMdGQxIzAhBgNVBAMTGlNlcnZlciB0ZXN0IGNl\n" |
| 27 | "cnQgKDUxMiBiaXQpMFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJ+zw4Qnlf8SMVIP\n" |
| 28 | "Fe9GEcStgOY2Ww/dgNdhjeD8ckUJNP5VZkVDTGiXav6ooKXfX3j/7tdkuD8Ey2//\n" |
| 29 | "Kv7+ue0CAwEAATANBgkqhkiG9w0BAQQFAAOBgQCT0grFQeZaqYb5EYfk20XixZV4\n" |
| 30 | "GmyAbXMftG1Eo7qGiMhYzRwGNWxEYojf5PZkYZXvSqZ/ZXHXa4g59jK/rJNnaVGM\n" |
| 31 | "k+xIX8mxQvlV0n5O9PIha5BX5teZnkHKgL8aKKLKW1BK7YTngsfSzzaeame5iKfz\n" |
| 32 | "itAE+OjGF+PFKbwX8Q==\n" |
| 33 | "-----END CERTIFICATE-----\n"; |
| 34 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 35 | const unsigned char kTestCertSha1[] = { |
| 36 | 0xA6, 0xC8, 0x59, 0xEA, 0xC3, 0x7E, 0x6D, 0x33, |
| 37 | 0xCF, 0xE2, 0x69, 0x9D, 0x74, 0xE6, 0xF6, 0x8A, |
| 38 | 0x9E, 0x47, 0xA7, 0xCA}; |
| 39 | const unsigned char kTestCertSha224[] = { |
| 40 | 0xd4, 0xce, 0xc6, 0xcf, 0x28, 0xcb, 0xe9, 0x77, |
| 41 | 0x38, 0x36, 0xcf, 0xb1, 0x3b, 0x4a, 0xd7, 0xbd, |
| 42 | 0xae, 0x24, 0x21, 0x08, 0xcf, 0x6a, 0x44, 0x0d, |
| 43 | 0x3f, 0x94, 0x2a, 0x5b}; |
| 44 | const unsigned char kTestCertSha256[] = { |
| 45 | 0x41, 0x6b, 0xb4, 0x93, 0x47, 0x79, 0x77, 0x24, |
| 46 | 0x77, 0x0b, 0x8b, 0x2e, 0xa6, 0x2b, 0xe0, 0xf9, |
| 47 | 0x0a, 0xed, 0x1f, 0x31, 0xa6, 0xf7, 0x5c, 0xa1, |
| 48 | 0x5a, 0xc4, 0xb0, 0xa2, 0xa4, 0x78, 0xb9, 0x76}; |
| 49 | const unsigned char kTestCertSha384[] = { |
| 50 | 0x42, 0x31, 0x9a, 0x79, 0x1d, 0xd6, 0x08, 0xbf, |
| 51 | 0x3b, 0xba, 0x36, 0xd8, 0x37, 0x4a, 0x9a, 0x75, |
| 52 | 0xd3, 0x25, 0x6e, 0x28, 0x92, 0xbe, 0x06, 0xb7, |
| 53 | 0xc5, 0xa0, 0x83, 0xe3, 0x86, 0xb1, 0x03, 0xfc, |
| 54 | 0x64, 0x47, 0xd6, 0xd8, 0xaa, 0xd9, 0x36, 0x60, |
| 55 | 0x04, 0xcc, 0xbe, 0x7d, 0x6a, 0xe8, 0x34, 0x49}; |
| 56 | const unsigned char kTestCertSha512[] = { |
| 57 | 0x51, 0x1d, 0xec, 0x02, 0x3d, 0x51, 0x45, 0xd3, |
| 58 | 0xd8, 0x1d, 0xa4, 0x9d, 0x43, 0xc9, 0xee, 0x32, |
| 59 | 0x6f, 0x4f, 0x37, 0xee, 0xab, 0x3f, 0x25, 0xdf, |
| 60 | 0x72, 0xfc, 0x61, 0x1a, 0xd5, 0x92, 0xff, 0x6b, |
| 61 | 0x28, 0x71, 0x58, 0xb3, 0xe1, 0x8a, 0x18, 0xcf, |
| 62 | 0x61, 0x33, 0x0e, 0x14, 0xc3, 0x04, 0xaa, 0x07, |
| 63 | 0xf6, 0xa5, 0xda, 0xdc, 0x42, 0x42, 0x22, 0x35, |
| 64 | 0xce, 0x26, 0x58, 0x4a, 0x33, 0x6d, 0xbc, 0xb6}; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 65 | |
| 66 | class SSLIdentityTest : public testing::Test { |
| 67 | public: |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 68 | SSLIdentityTest() {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 69 | |
| 70 | ~SSLIdentityTest() { |
| 71 | } |
| 72 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 73 | virtual void SetUp() { |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 74 | identity_rsa1_.reset(SSLIdentity::Generate("test1", rtc::KT_RSA)); |
| 75 | identity_rsa2_.reset(SSLIdentity::Generate("test2", rtc::KT_RSA)); |
| 76 | identity_ecdsa1_.reset(SSLIdentity::Generate("test3", rtc::KT_ECDSA)); |
| 77 | identity_ecdsa2_.reset(SSLIdentity::Generate("test4", rtc::KT_ECDSA)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 78 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 79 | ASSERT_TRUE(identity_rsa1_); |
| 80 | ASSERT_TRUE(identity_rsa2_); |
| 81 | ASSERT_TRUE(identity_ecdsa1_); |
| 82 | ASSERT_TRUE(identity_ecdsa2_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 83 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 84 | test_cert_.reset(rtc::SSLCertificate::FromPEMString(kTestCertificate)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 85 | ASSERT_TRUE(test_cert_); |
| 86 | } |
| 87 | |
| 88 | void TestGetSignatureDigestAlgorithm() { |
| 89 | std::string digest_algorithm; |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 90 | |
| 91 | ASSERT_TRUE(identity_rsa1_->certificate().GetSignatureDigestAlgorithm( |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 92 | &digest_algorithm)); |
Joachim Bauch | 1b794d5 | 2015-05-12 03:32:11 +0200 | [diff] [blame] | 93 | ASSERT_EQ(rtc::DIGEST_SHA_256, digest_algorithm); |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 94 | |
| 95 | ASSERT_TRUE(identity_rsa2_->certificate().GetSignatureDigestAlgorithm( |
| 96 | &digest_algorithm)); |
| 97 | ASSERT_EQ(rtc::DIGEST_SHA_256, digest_algorithm); |
| 98 | |
| 99 | ASSERT_TRUE(identity_ecdsa1_->certificate().GetSignatureDigestAlgorithm( |
| 100 | &digest_algorithm)); |
| 101 | ASSERT_EQ(rtc::DIGEST_SHA_256, digest_algorithm); |
| 102 | |
| 103 | ASSERT_TRUE(identity_ecdsa2_->certificate().GetSignatureDigestAlgorithm( |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 104 | &digest_algorithm)); |
Joachim Bauch | 1b794d5 | 2015-05-12 03:32:11 +0200 | [diff] [blame] | 105 | ASSERT_EQ(rtc::DIGEST_SHA_256, digest_algorithm); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 106 | |
| 107 | // The test certificate has an MD5-based signature. |
| 108 | ASSERT_TRUE(test_cert_->GetSignatureDigestAlgorithm(&digest_algorithm)); |
| 109 | ASSERT_EQ(rtc::DIGEST_MD5, digest_algorithm); |
| 110 | } |
| 111 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 112 | typedef unsigned char DigestType[rtc::MessageDigest::kMaxSize]; |
| 113 | |
| 114 | void TestDigestHelper(DigestType digest, |
| 115 | const SSLIdentity* identity, |
| 116 | const std::string& algorithm, |
| 117 | size_t expected_len) { |
| 118 | DigestType digest1; |
| 119 | size_t digest_len; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 120 | bool rv; |
| 121 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 122 | memset(digest, 0, expected_len); |
| 123 | rv = identity->certificate().ComputeDigest(algorithm, digest, |
| 124 | sizeof(DigestType), &digest_len); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 125 | EXPECT_TRUE(rv); |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 126 | EXPECT_EQ(expected_len, digest_len); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 127 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 128 | // Repeat digest computation for the identity as a sanity check. |
| 129 | memset(digest1, 0xff, expected_len); |
| 130 | rv = identity->certificate().ComputeDigest(algorithm, digest1, |
| 131 | sizeof(DigestType), &digest_len); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 132 | EXPECT_TRUE(rv); |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 133 | EXPECT_EQ(expected_len, digest_len); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 134 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 135 | EXPECT_EQ(0, memcmp(digest, digest1, expected_len)); |
| 136 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 137 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 138 | void TestDigestForGeneratedCert(const std::string& algorithm, |
| 139 | size_t expected_len) { |
| 140 | DigestType digest[4]; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 141 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 142 | ASSERT_TRUE(expected_len <= sizeof(DigestType)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 143 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 144 | TestDigestHelper(digest[0], identity_rsa1_.get(), algorithm, expected_len); |
| 145 | TestDigestHelper(digest[1], identity_rsa2_.get(), algorithm, expected_len); |
| 146 | TestDigestHelper(digest[2], identity_ecdsa1_.get(), algorithm, |
| 147 | expected_len); |
| 148 | TestDigestHelper(digest[3], identity_ecdsa2_.get(), algorithm, |
| 149 | expected_len); |
| 150 | |
| 151 | // Sanity check that all four digests are unique. This could theoretically |
| 152 | // fail, since cryptographic hash collisions have a non-zero probability. |
| 153 | for (int i = 0; i < 4; i++) { |
| 154 | for (int j = 0; j < 4; j++) { |
| 155 | if (i != j) |
| 156 | EXPECT_NE(0, memcmp(digest[i], digest[j], expected_len)); |
| 157 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 161 | void TestDigestForFixedCert(const std::string& algorithm, |
| 162 | size_t expected_len, |
| 163 | const unsigned char* expected_digest) { |
| 164 | bool rv; |
| 165 | DigestType digest; |
| 166 | size_t digest_len; |
| 167 | |
| 168 | ASSERT_TRUE(expected_len <= sizeof(DigestType)); |
| 169 | |
| 170 | rv = test_cert_->ComputeDigest(algorithm, digest, sizeof(digest), |
| 171 | &digest_len); |
| 172 | EXPECT_TRUE(rv); |
| 173 | EXPECT_EQ(expected_len, digest_len); |
| 174 | EXPECT_EQ(0, memcmp(digest, expected_digest, expected_len)); |
| 175 | } |
| 176 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 177 | private: |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame^] | 178 | std::unique_ptr<SSLIdentity> identity_rsa1_; |
| 179 | std::unique_ptr<SSLIdentity> identity_rsa2_; |
| 180 | std::unique_ptr<SSLIdentity> identity_ecdsa1_; |
| 181 | std::unique_ptr<SSLIdentity> identity_ecdsa2_; |
| 182 | std::unique_ptr<rtc::SSLCertificate> test_cert_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 183 | }; |
| 184 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 185 | TEST_F(SSLIdentityTest, FixedDigestSHA1) { |
| 186 | TestDigestForFixedCert(rtc::DIGEST_SHA_1, 20, kTestCertSha1); |
| 187 | } |
| 188 | |
| 189 | // HASH_AlgSHA224 is not supported in the chromium linux build. |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 190 | TEST_F(SSLIdentityTest, FixedDigestSHA224) { |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 191 | TestDigestForFixedCert(rtc::DIGEST_SHA_224, 28, kTestCertSha224); |
| 192 | } |
| 193 | |
| 194 | TEST_F(SSLIdentityTest, FixedDigestSHA256) { |
| 195 | TestDigestForFixedCert(rtc::DIGEST_SHA_256, 32, kTestCertSha256); |
| 196 | } |
| 197 | |
| 198 | TEST_F(SSLIdentityTest, FixedDigestSHA384) { |
| 199 | TestDigestForFixedCert(rtc::DIGEST_SHA_384, 48, kTestCertSha384); |
| 200 | } |
| 201 | |
| 202 | TEST_F(SSLIdentityTest, FixedDigestSHA512) { |
| 203 | TestDigestForFixedCert(rtc::DIGEST_SHA_512, 64, kTestCertSha512); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | // HASH_AlgSHA224 is not supported in the chromium linux build. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 207 | TEST_F(SSLIdentityTest, DigestSHA224) { |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 208 | TestDigestForGeneratedCert(rtc::DIGEST_SHA_224, 28); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | TEST_F(SSLIdentityTest, DigestSHA256) { |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 212 | TestDigestForGeneratedCert(rtc::DIGEST_SHA_256, 32); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | TEST_F(SSLIdentityTest, DigestSHA384) { |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 216 | TestDigestForGeneratedCert(rtc::DIGEST_SHA_384, 48); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | TEST_F(SSLIdentityTest, DigestSHA512) { |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 220 | TestDigestForGeneratedCert(rtc::DIGEST_SHA_512, 64); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 223 | TEST_F(SSLIdentityTest, FromPEMStringsRSA) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 224 | static const char kRSA_PRIVATE_KEY_PEM[] = |
| 225 | "-----BEGIN RSA PRIVATE KEY-----\n" |
| 226 | "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMYRkbhmI7kVA/rM\n" |
| 227 | "czsZ+6JDhDvnkF+vn6yCAGuRPV03zuRqZtDy4N4to7PZu9PjqrRl7nDMXrG3YG9y\n" |
| 228 | "rlIAZ72KjcKKFAJxQyAKLCIdawKRyp8RdK3LEySWEZb0AV58IadqPZDTNHHRX8dz\n" |
| 229 | "5aTSMsbbkZ+C/OzTnbiMqLL/vg6jAgMBAAECgYAvgOs4FJcgvp+TuREx7YtiYVsH\n" |
| 230 | "mwQPTum2z/8VzWGwR8BBHBvIpVe1MbD/Y4seyI2aco/7UaisatSgJhsU46/9Y4fq\n" |
| 231 | "2TwXH9QANf4at4d9n/R6rzwpAJOpgwZgKvdQjkfrKTtgLV+/dawvpxUYkRH4JZM1\n" |
| 232 | "CVGukMfKNrSVH4Ap4QJBAOJmGV1ASPnB4r4nc99at7JuIJmd7fmuVUwUgYi4XgaR\n" |
| 233 | "WhScBsgYwZ/JoywdyZJgnbcrTDuVcWG56B3vXbhdpMsCQQDf9zeJrjnPZ3Cqm79y\n" |
| 234 | "kdqANep0uwZciiNiWxsQrCHztywOvbFhdp8iYVFG9EK8DMY41Y5TxUwsHD+67zao\n" |
| 235 | "ZNqJAkEA1suLUP/GvL8IwuRneQd2tWDqqRQ/Td3qq03hP7e77XtF/buya3Ghclo5\n" |
| 236 | "54czUR89QyVfJEC6278nzA7n2h1uVQJAcG6mztNL6ja/dKZjYZye2CY44QjSlLo0\n" |
| 237 | "MTgTSjdfg/28fFn2Jjtqf9Pi/X+50LWI/RcYMC2no606wRk9kyOuIQJBAK6VSAim\n" |
| 238 | "1pOEjsYQn0X5KEIrz1G3bfCbB848Ime3U2/FWlCHMr6ch8kCZ5d1WUeJD3LbwMNG\n" |
| 239 | "UCXiYxSsu20QNVw=\n" |
| 240 | "-----END RSA PRIVATE KEY-----\n"; |
| 241 | |
| 242 | static const char kCERT_PEM[] = |
| 243 | "-----BEGIN CERTIFICATE-----\n" |
| 244 | "MIIBmTCCAQKgAwIBAgIEbzBSAjANBgkqhkiG9w0BAQsFADARMQ8wDQYDVQQDEwZX\n" |
| 245 | "ZWJSVEMwHhcNMTQwMTAyMTgyNDQ3WhcNMTQwMjAxMTgyNDQ3WjARMQ8wDQYDVQQD\n" |
| 246 | "EwZXZWJSVEMwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMYRkbhmI7kVA/rM\n" |
| 247 | "czsZ+6JDhDvnkF+vn6yCAGuRPV03zuRqZtDy4N4to7PZu9PjqrRl7nDMXrG3YG9y\n" |
| 248 | "rlIAZ72KjcKKFAJxQyAKLCIdawKRyp8RdK3LEySWEZb0AV58IadqPZDTNHHRX8dz\n" |
| 249 | "5aTSMsbbkZ+C/OzTnbiMqLL/vg6jAgMBAAEwDQYJKoZIhvcNAQELBQADgYEAUflI\n" |
| 250 | "VUe5Krqf5RVa5C3u/UTAOAUJBiDS3VANTCLBxjuMsvqOG0WvaYWP3HYPgrz0jXK2\n" |
| 251 | "LJE/mGw3MyFHEqi81jh95J+ypl6xKW6Rm8jKLR87gUvCaVYn/Z4/P3AqcQTB7wOv\n" |
| 252 | "UD0A8qfhfDM+LK6rPAnCsVN0NRDY3jvd6rzix9M=\n" |
| 253 | "-----END CERTIFICATE-----\n"; |
| 254 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame^] | 255 | std::unique_ptr<SSLIdentity> identity( |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 256 | SSLIdentity::FromPEMStrings(kRSA_PRIVATE_KEY_PEM, kCERT_PEM)); |
| 257 | EXPECT_TRUE(identity); |
| 258 | EXPECT_EQ(kCERT_PEM, identity->certificate().ToPEMString()); |
| 259 | } |
| 260 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 261 | TEST_F(SSLIdentityTest, FromPEMStringsEC) { |
| 262 | static const char kRSA_PRIVATE_KEY_PEM[] = |
| 263 | "-----BEGIN EC PRIVATE KEY-----\n" |
| 264 | "MHcCAQEEIKkIztWLPbs4Y2zWv7VW2Ov4is2ifleCuPgRB8fRv3IkoAoGCCqGSM49\n" |
| 265 | "AwEHoUQDQgAEDPV33NrhSdhg9cBRkUWUXnVMXc3h17i9ARbSmNgminKcBXb8/y8L\n" |
| 266 | "A76cMWQPPM0ybHO8OS7ZVg2U/m+TwE1M2g==\n" |
| 267 | "-----END EC PRIVATE KEY-----\n"; |
| 268 | static const char kCERT_PEM[] = |
| 269 | "-----BEGIN CERTIFICATE-----\n" |
| 270 | "MIIB0jCCAXmgAwIBAgIJAMCjpFt9t6LMMAoGCCqGSM49BAMCMEUxCzAJBgNVBAYT\n" |
| 271 | "AkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBXaWRn\n" |
| 272 | "aXRzIFB0eSBMdGQwIBcNMTUwNjMwMTMwMTIyWhgPMjI4OTA0MTMxMzAxMjJaMEUx\n" |
| 273 | "CzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRl\n" |
| 274 | "cm5ldCBXaWRnaXRzIFB0eSBMdGQwWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAAQM\n" |
| 275 | "9Xfc2uFJ2GD1wFGRRZRedUxdzeHXuL0BFtKY2CaKcpwFdvz/LwsDvpwxZA88zTJs\n" |
| 276 | "c7w5LtlWDZT+b5PATUzao1AwTjAdBgNVHQ4EFgQUYHq6nxNNIE832ZmaHc/noODO\n" |
| 277 | "rtAwHwYDVR0jBBgwFoAUYHq6nxNNIE832ZmaHc/noODOrtAwDAYDVR0TBAUwAwEB\n" |
| 278 | "/zAKBggqhkjOPQQDAgNHADBEAiAQRojsTyZG0BlKoU7gOt5h+yAMLl2cxmDtOIQr\n" |
| 279 | "GWP/PwIgJynB4AUDsPT0DWmethOXYijB5sY5UPd9DvgmiS/Mr6s=\n" |
| 280 | "-----END CERTIFICATE-----\n"; |
| 281 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame^] | 282 | std::unique_ptr<SSLIdentity> identity( |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 283 | SSLIdentity::FromPEMStrings(kRSA_PRIVATE_KEY_PEM, kCERT_PEM)); |
| 284 | EXPECT_TRUE(identity); |
| 285 | EXPECT_EQ(kCERT_PEM, identity->certificate().ToPEMString()); |
| 286 | } |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 287 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 288 | TEST_F(SSLIdentityTest, PemDerConversion) { |
| 289 | std::string der; |
| 290 | EXPECT_TRUE(SSLIdentity::PemToDer("CERTIFICATE", kTestCertificate, &der)); |
| 291 | |
| 292 | EXPECT_EQ(kTestCertificate, SSLIdentity::DerToPem( |
| 293 | "CERTIFICATE", |
| 294 | reinterpret_cast<const unsigned char*>(der.data()), der.length())); |
| 295 | } |
| 296 | |
| 297 | TEST_F(SSLIdentityTest, GetSignatureDigestAlgorithm) { |
| 298 | TestGetSignatureDigestAlgorithm(); |
| 299 | } |
Torbjorn Granlund | 46c9cc0 | 2015-12-01 13:06:34 +0100 | [diff] [blame] | 300 | |
| 301 | class SSLIdentityExpirationTest : public testing::Test { |
| 302 | public: |
| 303 | SSLIdentityExpirationTest() { |
| 304 | // Set use of the test RNG to get deterministic expiration timestamp. |
| 305 | rtc::SetRandomTestMode(true); |
| 306 | } |
| 307 | ~SSLIdentityExpirationTest() { |
| 308 | // Put it back for the next test. |
| 309 | rtc::SetRandomTestMode(false); |
| 310 | } |
| 311 | |
| 312 | void TestASN1TimeToSec() { |
| 313 | struct asn_example { |
| 314 | const char* string; |
| 315 | bool long_format; |
| 316 | int64_t want; |
| 317 | } static const data[] = { |
| 318 | // Valid examples. |
| 319 | {"19700101000000Z", true, 0}, |
| 320 | {"700101000000Z", false, 0}, |
| 321 | {"19700101000001Z", true, 1}, |
| 322 | {"700101000001Z", false, 1}, |
| 323 | {"19700101000100Z", true, 60}, |
| 324 | {"19700101000101Z", true, 61}, |
| 325 | {"19700101010000Z", true, 3600}, |
| 326 | {"19700101010001Z", true, 3601}, |
| 327 | {"19700101010100Z", true, 3660}, |
| 328 | {"19700101010101Z", true, 3661}, |
| 329 | {"710911012345Z", false, 53400225}, |
| 330 | {"20000101000000Z", true, 946684800}, |
| 331 | {"20000101000000Z", true, 946684800}, |
| 332 | {"20151130140156Z", true, 1448892116}, |
| 333 | {"151130140156Z", false, 1448892116}, |
| 334 | {"20491231235959Z", true, 2524607999}, |
| 335 | {"491231235959Z", false, 2524607999}, |
| 336 | {"20500101000000Z", true, 2524607999+1}, |
| 337 | {"20700101000000Z", true, 3155760000}, |
| 338 | {"21000101000000Z", true, 4102444800}, |
| 339 | {"24000101000000Z", true, 13569465600}, |
| 340 | |
| 341 | // Invalid examples. |
| 342 | {"19700101000000", true, -1}, // missing Z long format |
| 343 | {"19700101000000X", true, -1}, // X instead of Z long format |
| 344 | {"197001010000000", true, -1}, // 0 instead of Z long format |
| 345 | {"1970010100000000Z", true, -1}, // excess digits long format |
| 346 | {"700101000000", false, -1}, // missing Z short format |
| 347 | {"700101000000X", false, -1}, // X instead of Z short format |
| 348 | {"7001010000000", false, -1}, // 0 instead of Z short format |
| 349 | {"70010100000000Z", false, -1}, // excess digits short format |
| 350 | {":9700101000000Z", true, -1}, // invalid character |
| 351 | {"1:700101000001Z", true, -1}, // invalid character |
| 352 | {"19:00101000100Z", true, -1}, // invalid character |
| 353 | {"197:0101000101Z", true, -1}, // invalid character |
| 354 | {"1970:101010000Z", true, -1}, // invalid character |
| 355 | {"19700:01010001Z", true, -1}, // invalid character |
| 356 | {"197001:1010100Z", true, -1}, // invalid character |
| 357 | {"1970010:010101Z", true, -1}, // invalid character |
| 358 | {"70010100:000Z", false, -1}, // invalid character |
| 359 | {"700101000:01Z", false, -1}, // invalid character |
| 360 | {"2000010100:000Z", true, -1}, // invalid character |
| 361 | {"21000101000:00Z", true, -1}, // invalid character |
| 362 | {"240001010000:0Z", true, -1}, // invalid character |
| 363 | {"500101000000Z", false, -1}, // but too old for epoch |
| 364 | {"691231235959Z", false, -1}, // too old for epoch |
| 365 | {"19611118043000Z", false, -1}, // way too old for epoch |
| 366 | }; |
| 367 | |
| 368 | unsigned char buf[20]; |
| 369 | |
| 370 | // Run all examples and check for the expected result. |
| 371 | for (const auto& entry : data) { |
| 372 | size_t length = strlen(entry.string); |
| 373 | memcpy(buf, entry.string, length); // Copy the ASN1 string... |
| 374 | buf[length] = rtc::CreateRandomId(); // ...and terminate it with junk. |
| 375 | int64_t res = rtc::ASN1TimeToSec(buf, length, entry.long_format); |
| 376 | LOG(LS_VERBOSE) << entry.string; |
| 377 | ASSERT_EQ(entry.want, res); |
| 378 | } |
| 379 | // Run all examples again, but with an invalid length. |
| 380 | for (const auto& entry : data) { |
| 381 | size_t length = strlen(entry.string); |
| 382 | memcpy(buf, entry.string, length); // Copy the ASN1 string... |
| 383 | buf[length] = rtc::CreateRandomId(); // ...and terminate it with junk. |
| 384 | int64_t res = rtc::ASN1TimeToSec(buf, length - 1, entry.long_format); |
| 385 | LOG(LS_VERBOSE) << entry.string; |
| 386 | ASSERT_EQ(-1, res); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | void TestExpireTime(int times) { |
torbjorng | e8dc081 | 2016-02-15 09:35:54 -0800 | [diff] [blame] | 391 | // We test just ECDSA here since what we're out to exercise is the |
| 392 | // interfaces for expiration setting and reading. |
Torbjorn Granlund | 46c9cc0 | 2015-12-01 13:06:34 +0100 | [diff] [blame] | 393 | for (int i = 0; i < times; i++) { |
Torbjorn Granlund | 46c9cc0 | 2015-12-01 13:06:34 +0100 | [diff] [blame] | 394 | // We limit the time to < 2^31 here, i.e., we stay before 2038, since else |
| 395 | // we hit time offset limitations in OpenSSL on some 32-bit systems. |
Taylor Brandstetter | 88dec83 | 2016-03-15 09:57:55 -0700 | [diff] [blame] | 396 | time_t time_before_generation = time(nullptr); |
| 397 | time_t lifetime = |
| 398 | rtc::CreateRandomId() % (0x80000000 - time_before_generation); |
torbjorng | e8dc081 | 2016-02-15 09:35:54 -0800 | [diff] [blame] | 399 | rtc::KeyParams key_params = rtc::KeyParams::ECDSA(rtc::EC_NIST_P256); |
| 400 | SSLIdentity* identity = |
Torbjorn Granlund | 1d846b2 | 2016-03-31 16:21:04 +0200 | [diff] [blame] | 401 | rtc::SSLIdentity::GenerateWithExpiration("", key_params, lifetime); |
Taylor Brandstetter | 88dec83 | 2016-03-15 09:57:55 -0700 | [diff] [blame] | 402 | time_t time_after_generation = time(nullptr); |
| 403 | EXPECT_LE(time_before_generation + lifetime, |
| 404 | identity->certificate().CertificateExpirationTime()); |
| 405 | EXPECT_GE(time_after_generation + lifetime, |
Torbjorn Granlund | 46c9cc0 | 2015-12-01 13:06:34 +0100 | [diff] [blame] | 406 | identity->certificate().CertificateExpirationTime()); |
| 407 | delete identity; |
| 408 | } |
| 409 | } |
| 410 | }; |
| 411 | |
| 412 | TEST_F(SSLIdentityExpirationTest, TestASN1TimeToSec) { |
| 413 | TestASN1TimeToSec(); |
| 414 | } |
| 415 | |
| 416 | TEST_F(SSLIdentityExpirationTest, TestExpireTime) { |
| 417 | TestExpireTime(500); |
| 418 | } |