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 | |
hbos | 6b470a9 | 2016-04-28 05:14:21 -0700 | [diff] [blame^] | 177 | void TestCloningIdentity(const SSLIdentity& identity) { |
| 178 | // Convert |identity| to PEM strings and create a new identity by converting |
| 179 | // back from the string format. |
| 180 | std::string priv_pem = identity.PrivateKeyToPEMString(); |
| 181 | std::string publ_pem = identity.PublicKeyToPEMString(); |
| 182 | std::string cert_pem = identity.certificate().ToPEMString(); |
| 183 | std::unique_ptr<SSLIdentity> clone( |
| 184 | SSLIdentity::FromPEMStrings(priv_pem, cert_pem)); |
| 185 | EXPECT_TRUE(clone); |
| 186 | |
| 187 | // Make sure the clone is identical to the original. |
| 188 | EXPECT_TRUE(identity == *clone); |
| 189 | ASSERT_EQ(identity.certificate().CertificateExpirationTime(), |
| 190 | clone->certificate().CertificateExpirationTime()); |
| 191 | |
| 192 | // At this point we are confident that the identities are identical. To be |
| 193 | // extra sure, we compare PEM strings of the clone with the original. Note |
| 194 | // that the PEM strings of two identities are not strictly guaranteed to be |
| 195 | // equal (they describe structs whose members could be listed in a different |
| 196 | // order, for example). But because the same function is used to produce |
| 197 | // both PEMs, its a good enough bet that this comparison will work. If the |
| 198 | // assumption stops holding in the future we can always remove this from the |
| 199 | // unittest. |
| 200 | std::string clone_priv_pem = clone->PrivateKeyToPEMString(); |
| 201 | std::string clone_publ_pem = clone->PublicKeyToPEMString(); |
| 202 | std::string clone_cert_pem = clone->certificate().ToPEMString(); |
| 203 | ASSERT_EQ(priv_pem, clone_priv_pem); |
| 204 | ASSERT_EQ(publ_pem, clone_publ_pem); |
| 205 | ASSERT_EQ(cert_pem, clone_cert_pem); |
| 206 | } |
| 207 | |
| 208 | protected: |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 209 | std::unique_ptr<SSLIdentity> identity_rsa1_; |
| 210 | std::unique_ptr<SSLIdentity> identity_rsa2_; |
| 211 | std::unique_ptr<SSLIdentity> identity_ecdsa1_; |
| 212 | std::unique_ptr<SSLIdentity> identity_ecdsa2_; |
| 213 | std::unique_ptr<rtc::SSLCertificate> test_cert_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 214 | }; |
| 215 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 216 | TEST_F(SSLIdentityTest, FixedDigestSHA1) { |
| 217 | TestDigestForFixedCert(rtc::DIGEST_SHA_1, 20, kTestCertSha1); |
| 218 | } |
| 219 | |
| 220 | // HASH_AlgSHA224 is not supported in the chromium linux build. |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 221 | TEST_F(SSLIdentityTest, FixedDigestSHA224) { |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 222 | TestDigestForFixedCert(rtc::DIGEST_SHA_224, 28, kTestCertSha224); |
| 223 | } |
| 224 | |
| 225 | TEST_F(SSLIdentityTest, FixedDigestSHA256) { |
| 226 | TestDigestForFixedCert(rtc::DIGEST_SHA_256, 32, kTestCertSha256); |
| 227 | } |
| 228 | |
| 229 | TEST_F(SSLIdentityTest, FixedDigestSHA384) { |
| 230 | TestDigestForFixedCert(rtc::DIGEST_SHA_384, 48, kTestCertSha384); |
| 231 | } |
| 232 | |
| 233 | TEST_F(SSLIdentityTest, FixedDigestSHA512) { |
| 234 | TestDigestForFixedCert(rtc::DIGEST_SHA_512, 64, kTestCertSha512); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | // HASH_AlgSHA224 is not supported in the chromium linux build. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 238 | TEST_F(SSLIdentityTest, DigestSHA224) { |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 239 | TestDigestForGeneratedCert(rtc::DIGEST_SHA_224, 28); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | TEST_F(SSLIdentityTest, DigestSHA256) { |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 243 | TestDigestForGeneratedCert(rtc::DIGEST_SHA_256, 32); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | TEST_F(SSLIdentityTest, DigestSHA384) { |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 247 | TestDigestForGeneratedCert(rtc::DIGEST_SHA_384, 48); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | TEST_F(SSLIdentityTest, DigestSHA512) { |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 251 | TestDigestForGeneratedCert(rtc::DIGEST_SHA_512, 64); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 252 | } |
| 253 | |
hbos | 6b470a9 | 2016-04-28 05:14:21 -0700 | [diff] [blame^] | 254 | TEST_F(SSLIdentityTest, IdentityComparison) { |
| 255 | EXPECT_TRUE(*identity_rsa1_ == *identity_rsa1_); |
| 256 | EXPECT_FALSE(*identity_rsa1_ == *identity_rsa2_); |
| 257 | EXPECT_FALSE(*identity_rsa1_ == *identity_ecdsa1_); |
| 258 | EXPECT_FALSE(*identity_rsa1_ == *identity_ecdsa2_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 259 | |
hbos | 6b470a9 | 2016-04-28 05:14:21 -0700 | [diff] [blame^] | 260 | EXPECT_TRUE(*identity_rsa2_ == *identity_rsa2_); |
| 261 | EXPECT_FALSE(*identity_rsa2_ == *identity_ecdsa1_); |
| 262 | EXPECT_FALSE(*identity_rsa2_ == *identity_ecdsa2_); |
| 263 | |
| 264 | EXPECT_TRUE(*identity_ecdsa1_ == *identity_ecdsa1_); |
| 265 | EXPECT_FALSE(*identity_ecdsa1_ == *identity_ecdsa2_); |
| 266 | } |
| 267 | |
| 268 | TEST_F(SSLIdentityTest, FromPEMStringsRSA) { |
| 269 | // These PEM strings were created by generating an identity with |
| 270 | // |SSLIdentity::Generate| and invoking |identity->PrivateKeyToPEMString()|, |
| 271 | // |identity->PublicKeyToPEMString()| and |
| 272 | // |identity->certificate().ToPEMString()|. If the crypto library is updated, |
| 273 | // and the update changes the string form of the keys, these will have to be |
| 274 | // updated too. |
| 275 | static const char kRSA_PRIVATE_KEY_PEM[] = |
| 276 | "-----BEGIN PRIVATE KEY-----\n" |
| 277 | "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAMQPqDStRlYeDpkX\n" |
| 278 | "erRmv+a1naM8vSVSY0gG2plnrnofViWRW3MRqWC+020MsIj3hPZeSAnt/y/FL/nr\n" |
| 279 | "4Ea7NXcwdRo1/1xEK7U/f/cjSg1aunyvHCHwcFcMr31HLFvHr0ZgcFwbgIuFLNEl\n" |
| 280 | "7kK5HMO9APz1ntUjek8BmBj8yMl9AgMBAAECgYA8FWBC5GcNtSBcIinkZyigF0A7\n" |
| 281 | "6j081sa+J/uNz4xUuI257ZXM6biygUhhvuXK06/XoIULJfhyN0fAm1yb0HtNhiUs\n" |
| 282 | "kMOYeon6b8FqFaPjrQf7Gr9FMiIHXNK19uegTMKztXyPZoUWlX84X0iawY95x0Y3\n" |
| 283 | "73f6P2rN2UOjlVVjAQJBAOKy3l2w3Zj2w0oAJox0eMwl+RxBNt1C42SHrob2mFUT\n" |
| 284 | "rytpVVYOasr8CoDI0kjacjI94sLum+buJoXXX6YTGO0CQQDdZwlYIEkoS3ftfxPa\n" |
| 285 | "Ai0YTBzAWvHJg0r8Gk/TkHo6IM+LSsZ9ZYUv/vBe4BKLw1I4hZ+bQvBiq+f8ROtk\n" |
| 286 | "+TDRAkAPL3ghwoU1h+IRBO2QHwUwd6K2N9AbBi4BP+168O3HVSg4ujeTKigRLMzv\n" |
| 287 | "T4R2iNt5bhfQgvdCgtVlxcWMdF8JAkBwDCg3eEdt5BuyjwBt8XH+/O4ED0KUWCTH\n" |
| 288 | "x00k5dZlupsuhE5Fwe4QpzXg3gekwdnHjyCCQ/NCDHvgOMTkmhQxAkA9V03KRX9b\n" |
| 289 | "bhvEzY/fu8gEp+EzsER96/D79az5z1BaMGL5OPM2xHBPJATKlswnAa7Lp3QKGZGk\n" |
| 290 | "TxslfL18J71s\n" |
| 291 | "-----END PRIVATE KEY-----\n"; |
| 292 | static const char kRSA_PUBLIC_KEY_PEM[] = |
| 293 | "-----BEGIN PUBLIC KEY-----\n" |
| 294 | "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDED6g0rUZWHg6ZF3q0Zr/mtZ2j\n" |
| 295 | "PL0lUmNIBtqZZ656H1YlkVtzEalgvtNtDLCI94T2XkgJ7f8vxS/56+BGuzV3MHUa\n" |
| 296 | "Nf9cRCu1P3/3I0oNWrp8rxwh8HBXDK99Ryxbx69GYHBcG4CLhSzRJe5CuRzDvQD8\n" |
| 297 | "9Z7VI3pPAZgY/MjJfQIDAQAB\n" |
| 298 | "-----END PUBLIC KEY-----\n"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 299 | static const char kCERT_PEM[] = |
| 300 | "-----BEGIN CERTIFICATE-----\n" |
hbos | 6b470a9 | 2016-04-28 05:14:21 -0700 | [diff] [blame^] | 301 | "MIIBnDCCAQWgAwIBAgIJAOEHLgeWYwrpMA0GCSqGSIb3DQEBCwUAMBAxDjAMBgNV\n" |
| 302 | "BAMMBXRlc3QxMB4XDTE2MDQyNDE4MTAyMloXDTE2MDUyNTE4MTAyMlowEDEOMAwG\n" |
| 303 | "A1UEAwwFdGVzdDEwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMQPqDStRlYe\n" |
| 304 | "DpkXerRmv+a1naM8vSVSY0gG2plnrnofViWRW3MRqWC+020MsIj3hPZeSAnt/y/F\n" |
| 305 | "L/nr4Ea7NXcwdRo1/1xEK7U/f/cjSg1aunyvHCHwcFcMr31HLFvHr0ZgcFwbgIuF\n" |
| 306 | "LNEl7kK5HMO9APz1ntUjek8BmBj8yMl9AgMBAAEwDQYJKoZIhvcNAQELBQADgYEA\n" |
| 307 | "C3ehaZFl+oEYN069C2ht/gMzuC77L854RF/x7xRtNZzkcg9TVgXXdM3auUvJi8dx\n" |
| 308 | "yTpU3ixErjQvoZew5ngXTEvTY8BSQUijJEaLWh8n6NDKRbEGTdAk8nPAmq9hdCFq\n" |
| 309 | "e3UkexqNHm3g/VxG4NUC1Y+w29ai0/Rgh+VvgbDwK+Q=\n" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 310 | "-----END CERTIFICATE-----\n"; |
| 311 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 312 | std::unique_ptr<SSLIdentity> identity( |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 313 | SSLIdentity::FromPEMStrings(kRSA_PRIVATE_KEY_PEM, kCERT_PEM)); |
| 314 | EXPECT_TRUE(identity); |
hbos | 6b470a9 | 2016-04-28 05:14:21 -0700 | [diff] [blame^] | 315 | EXPECT_EQ(kRSA_PRIVATE_KEY_PEM, identity->PrivateKeyToPEMString()); |
| 316 | EXPECT_EQ(kRSA_PUBLIC_KEY_PEM, identity->PublicKeyToPEMString()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 317 | EXPECT_EQ(kCERT_PEM, identity->certificate().ToPEMString()); |
| 318 | } |
| 319 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 320 | TEST_F(SSLIdentityTest, FromPEMStringsEC) { |
hbos | 6b470a9 | 2016-04-28 05:14:21 -0700 | [diff] [blame^] | 321 | // These PEM strings were created by generating an identity with |
| 322 | // |SSLIdentity::Generate| and invoking |identity->PrivateKeyToPEMString()|, |
| 323 | // |identity->PublicKeyToPEMString()| and |
| 324 | // |identity->certificate().ToPEMString()|. If the crypto library is updated, |
| 325 | // and the update changes the string form of the keys, these will have to be |
| 326 | // updated too. |
| 327 | static const char kECDSA_PRIVATE_KEY_PEM[] = |
| 328 | "-----BEGIN PRIVATE KEY-----\n" |
| 329 | "MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg/AkEA2hklq7dQ2rN\n" |
| 330 | "ZxYL6hOUACL4pn7P4FYlA3ZQhIChRANCAAR7YgdO3utP/8IqVRq8G4VZKreMAxeN\n" |
| 331 | "rUa12twthv4uFjuHAHa9D9oyAjncmn+xvZZRyVmKrA56jRzENcEEHoAg\n" |
| 332 | "-----END PRIVATE KEY-----\n"; |
| 333 | static const char kECDSA_PUBLIC_KEY_PEM[] = |
| 334 | "-----BEGIN PUBLIC KEY-----\n" |
| 335 | "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEe2IHTt7rT//CKlUavBuFWSq3jAMX\n" |
| 336 | "ja1GtdrcLYb+LhY7hwB2vQ/aMgI53Jp/sb2WUclZiqwOeo0cxDXBBB6AIA==\n" |
| 337 | "-----END PUBLIC KEY-----\n"; |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 338 | static const char kCERT_PEM[] = |
| 339 | "-----BEGIN CERTIFICATE-----\n" |
hbos | 6b470a9 | 2016-04-28 05:14:21 -0700 | [diff] [blame^] | 340 | "MIIBFDCBu6ADAgECAgkArpkxjw62sW4wCgYIKoZIzj0EAwIwEDEOMAwGA1UEAwwF\n" |
| 341 | "dGVzdDMwHhcNMTYwNDI0MTgxNDM4WhcNMTYwNTI1MTgxNDM4WjAQMQ4wDAYDVQQD\n" |
| 342 | "DAV0ZXN0MzBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABHtiB07e60//wipVGrwb\n" |
| 343 | "hVkqt4wDF42tRrXa3C2G/i4WO4cAdr0P2jICOdyaf7G9llHJWYqsDnqNHMQ1wQQe\n" |
| 344 | "gCAwCgYIKoZIzj0EAwIDSAAwRQIhANyreQ/K5yuPPpirsd0e/4WGLHou6bIOSQks\n" |
| 345 | "DYzo56NmAiAKOr3u8ol3LmygbUCwEvtWrS8QcJDygxHPACo99hkekw==\n" |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 346 | "-----END CERTIFICATE-----\n"; |
| 347 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 348 | std::unique_ptr<SSLIdentity> identity( |
hbos | 6b470a9 | 2016-04-28 05:14:21 -0700 | [diff] [blame^] | 349 | SSLIdentity::FromPEMStrings(kECDSA_PRIVATE_KEY_PEM, kCERT_PEM)); |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 350 | EXPECT_TRUE(identity); |
hbos | 6b470a9 | 2016-04-28 05:14:21 -0700 | [diff] [blame^] | 351 | EXPECT_EQ(kECDSA_PRIVATE_KEY_PEM, identity->PrivateKeyToPEMString()); |
| 352 | EXPECT_EQ(kECDSA_PUBLIC_KEY_PEM, identity->PublicKeyToPEMString()); |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 353 | EXPECT_EQ(kCERT_PEM, identity->certificate().ToPEMString()); |
| 354 | } |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 355 | |
hbos | 6b470a9 | 2016-04-28 05:14:21 -0700 | [diff] [blame^] | 356 | TEST_F(SSLIdentityTest, CloneIdentityRSA) { |
| 357 | TestCloningIdentity(*identity_rsa1_); |
| 358 | TestCloningIdentity(*identity_rsa2_); |
| 359 | } |
| 360 | |
| 361 | TEST_F(SSLIdentityTest, CloneIdentityECDSA) { |
| 362 | TestCloningIdentity(*identity_ecdsa1_); |
| 363 | TestCloningIdentity(*identity_ecdsa2_); |
| 364 | } |
| 365 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 366 | TEST_F(SSLIdentityTest, PemDerConversion) { |
| 367 | std::string der; |
| 368 | EXPECT_TRUE(SSLIdentity::PemToDer("CERTIFICATE", kTestCertificate, &der)); |
| 369 | |
| 370 | EXPECT_EQ(kTestCertificate, SSLIdentity::DerToPem( |
| 371 | "CERTIFICATE", |
| 372 | reinterpret_cast<const unsigned char*>(der.data()), der.length())); |
| 373 | } |
| 374 | |
| 375 | TEST_F(SSLIdentityTest, GetSignatureDigestAlgorithm) { |
| 376 | TestGetSignatureDigestAlgorithm(); |
| 377 | } |
Torbjorn Granlund | 46c9cc0 | 2015-12-01 13:06:34 +0100 | [diff] [blame] | 378 | |
| 379 | class SSLIdentityExpirationTest : public testing::Test { |
| 380 | public: |
| 381 | SSLIdentityExpirationTest() { |
| 382 | // Set use of the test RNG to get deterministic expiration timestamp. |
| 383 | rtc::SetRandomTestMode(true); |
| 384 | } |
| 385 | ~SSLIdentityExpirationTest() { |
| 386 | // Put it back for the next test. |
| 387 | rtc::SetRandomTestMode(false); |
| 388 | } |
| 389 | |
| 390 | void TestASN1TimeToSec() { |
| 391 | struct asn_example { |
| 392 | const char* string; |
| 393 | bool long_format; |
| 394 | int64_t want; |
| 395 | } static const data[] = { |
| 396 | // Valid examples. |
| 397 | {"19700101000000Z", true, 0}, |
| 398 | {"700101000000Z", false, 0}, |
| 399 | {"19700101000001Z", true, 1}, |
| 400 | {"700101000001Z", false, 1}, |
| 401 | {"19700101000100Z", true, 60}, |
| 402 | {"19700101000101Z", true, 61}, |
| 403 | {"19700101010000Z", true, 3600}, |
| 404 | {"19700101010001Z", true, 3601}, |
| 405 | {"19700101010100Z", true, 3660}, |
| 406 | {"19700101010101Z", true, 3661}, |
| 407 | {"710911012345Z", false, 53400225}, |
| 408 | {"20000101000000Z", true, 946684800}, |
| 409 | {"20000101000000Z", true, 946684800}, |
| 410 | {"20151130140156Z", true, 1448892116}, |
| 411 | {"151130140156Z", false, 1448892116}, |
| 412 | {"20491231235959Z", true, 2524607999}, |
| 413 | {"491231235959Z", false, 2524607999}, |
| 414 | {"20500101000000Z", true, 2524607999+1}, |
| 415 | {"20700101000000Z", true, 3155760000}, |
| 416 | {"21000101000000Z", true, 4102444800}, |
| 417 | {"24000101000000Z", true, 13569465600}, |
| 418 | |
| 419 | // Invalid examples. |
| 420 | {"19700101000000", true, -1}, // missing Z long format |
| 421 | {"19700101000000X", true, -1}, // X instead of Z long format |
| 422 | {"197001010000000", true, -1}, // 0 instead of Z long format |
| 423 | {"1970010100000000Z", true, -1}, // excess digits long format |
| 424 | {"700101000000", false, -1}, // missing Z short format |
| 425 | {"700101000000X", false, -1}, // X instead of Z short format |
| 426 | {"7001010000000", false, -1}, // 0 instead of Z short format |
| 427 | {"70010100000000Z", false, -1}, // excess digits short format |
| 428 | {":9700101000000Z", true, -1}, // invalid character |
| 429 | {"1:700101000001Z", true, -1}, // invalid character |
| 430 | {"19:00101000100Z", true, -1}, // invalid character |
| 431 | {"197:0101000101Z", true, -1}, // invalid character |
| 432 | {"1970:101010000Z", true, -1}, // invalid character |
| 433 | {"19700:01010001Z", true, -1}, // invalid character |
| 434 | {"197001:1010100Z", true, -1}, // invalid character |
| 435 | {"1970010:010101Z", true, -1}, // invalid character |
| 436 | {"70010100:000Z", false, -1}, // invalid character |
| 437 | {"700101000:01Z", false, -1}, // invalid character |
| 438 | {"2000010100:000Z", true, -1}, // invalid character |
| 439 | {"21000101000:00Z", true, -1}, // invalid character |
| 440 | {"240001010000:0Z", true, -1}, // invalid character |
| 441 | {"500101000000Z", false, -1}, // but too old for epoch |
| 442 | {"691231235959Z", false, -1}, // too old for epoch |
| 443 | {"19611118043000Z", false, -1}, // way too old for epoch |
| 444 | }; |
| 445 | |
| 446 | unsigned char buf[20]; |
| 447 | |
| 448 | // Run all examples and check for the expected result. |
| 449 | for (const auto& entry : data) { |
| 450 | size_t length = strlen(entry.string); |
| 451 | memcpy(buf, entry.string, length); // Copy the ASN1 string... |
| 452 | buf[length] = rtc::CreateRandomId(); // ...and terminate it with junk. |
| 453 | int64_t res = rtc::ASN1TimeToSec(buf, length, entry.long_format); |
| 454 | LOG(LS_VERBOSE) << entry.string; |
| 455 | ASSERT_EQ(entry.want, res); |
| 456 | } |
| 457 | // Run all examples again, but with an invalid length. |
| 458 | for (const auto& entry : data) { |
| 459 | size_t length = strlen(entry.string); |
| 460 | memcpy(buf, entry.string, length); // Copy the ASN1 string... |
| 461 | buf[length] = rtc::CreateRandomId(); // ...and terminate it with junk. |
| 462 | int64_t res = rtc::ASN1TimeToSec(buf, length - 1, entry.long_format); |
| 463 | LOG(LS_VERBOSE) << entry.string; |
| 464 | ASSERT_EQ(-1, res); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | void TestExpireTime(int times) { |
torbjorng | e8dc081 | 2016-02-15 09:35:54 -0800 | [diff] [blame] | 469 | // We test just ECDSA here since what we're out to exercise is the |
| 470 | // interfaces for expiration setting and reading. |
Torbjorn Granlund | 46c9cc0 | 2015-12-01 13:06:34 +0100 | [diff] [blame] | 471 | for (int i = 0; i < times; i++) { |
Torbjorn Granlund | 46c9cc0 | 2015-12-01 13:06:34 +0100 | [diff] [blame] | 472 | // We limit the time to < 2^31 here, i.e., we stay before 2038, since else |
| 473 | // we hit time offset limitations in OpenSSL on some 32-bit systems. |
Taylor Brandstetter | 88dec83 | 2016-03-15 09:57:55 -0700 | [diff] [blame] | 474 | time_t time_before_generation = time(nullptr); |
| 475 | time_t lifetime = |
| 476 | rtc::CreateRandomId() % (0x80000000 - time_before_generation); |
torbjorng | e8dc081 | 2016-02-15 09:35:54 -0800 | [diff] [blame] | 477 | rtc::KeyParams key_params = rtc::KeyParams::ECDSA(rtc::EC_NIST_P256); |
| 478 | SSLIdentity* identity = |
Torbjorn Granlund | 1d846b2 | 2016-03-31 16:21:04 +0200 | [diff] [blame] | 479 | rtc::SSLIdentity::GenerateWithExpiration("", key_params, lifetime); |
Taylor Brandstetter | 88dec83 | 2016-03-15 09:57:55 -0700 | [diff] [blame] | 480 | time_t time_after_generation = time(nullptr); |
| 481 | EXPECT_LE(time_before_generation + lifetime, |
| 482 | identity->certificate().CertificateExpirationTime()); |
| 483 | EXPECT_GE(time_after_generation + lifetime, |
Torbjorn Granlund | 46c9cc0 | 2015-12-01 13:06:34 +0100 | [diff] [blame] | 484 | identity->certificate().CertificateExpirationTime()); |
| 485 | delete identity; |
| 486 | } |
| 487 | } |
| 488 | }; |
| 489 | |
| 490 | TEST_F(SSLIdentityExpirationTest, TestASN1TimeToSec) { |
| 491 | TestASN1TimeToSec(); |
| 492 | } |
| 493 | |
| 494 | TEST_F(SSLIdentityExpirationTest, TestExpireTime) { |
| 495 | TestExpireTime(500); |
| 496 | } |