blob: 399fe9d474505217f3682ef4a80110369486625b [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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
jbauch555604a2016-04-26 03:13:22 -070011#include <memory>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000012#include <string>
13
14#include "webrtc/base/gunit.h"
Torbjorn Granlund46c9cc02015-12-01 13:06:34 +010015#include "webrtc/base/helpers.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000016#include "webrtc/base/ssladapter.h"
17#include "webrtc/base/sslidentity.h"
18
19using rtc::SSLIdentity;
20
21const 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 Granlundb6d4ec42015-08-17 14:08:59 +020035const 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};
39const 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};
44const 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};
49const 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};
56const 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.orgf0488722014-05-13 18:00:26 +000065
66class SSLIdentityTest : public testing::Test {
67 public:
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +020068 SSLIdentityTest() {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000069
70 ~SSLIdentityTest() {
71 }
72
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000073 virtual void SetUp() {
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +020074 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.orgf0488722014-05-13 18:00:26 +000078
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +020079 ASSERT_TRUE(identity_rsa1_);
80 ASSERT_TRUE(identity_rsa2_);
81 ASSERT_TRUE(identity_ecdsa1_);
82 ASSERT_TRUE(identity_ecdsa2_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000083
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +020084 test_cert_.reset(rtc::SSLCertificate::FromPEMString(kTestCertificate));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000085 ASSERT_TRUE(test_cert_);
86 }
87
88 void TestGetSignatureDigestAlgorithm() {
89 std::string digest_algorithm;
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +020090
91 ASSERT_TRUE(identity_rsa1_->certificate().GetSignatureDigestAlgorithm(
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000092 &digest_algorithm));
Joachim Bauch1b794d52015-05-12 03:32:11 +020093 ASSERT_EQ(rtc::DIGEST_SHA_256, digest_algorithm);
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +020094
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.orgf0488722014-05-13 18:00:26 +0000104 &digest_algorithm));
Joachim Bauch1b794d52015-05-12 03:32:11 +0200105 ASSERT_EQ(rtc::DIGEST_SHA_256, digest_algorithm);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000106
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 Granlundb6d4ec42015-08-17 14:08:59 +0200112 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.orgf0488722014-05-13 18:00:26 +0000120 bool rv;
121
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200122 memset(digest, 0, expected_len);
123 rv = identity->certificate().ComputeDigest(algorithm, digest,
124 sizeof(DigestType), &digest_len);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000125 EXPECT_TRUE(rv);
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200126 EXPECT_EQ(expected_len, digest_len);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000127
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200128 // 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.orgf0488722014-05-13 18:00:26 +0000132 EXPECT_TRUE(rv);
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200133 EXPECT_EQ(expected_len, digest_len);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000134
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200135 EXPECT_EQ(0, memcmp(digest, digest1, expected_len));
136 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000137
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200138 void TestDigestForGeneratedCert(const std::string& algorithm,
139 size_t expected_len) {
140 DigestType digest[4];
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000141
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200142 ASSERT_TRUE(expected_len <= sizeof(DigestType));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000143
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200144 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.orgf0488722014-05-13 18:00:26 +0000158 }
159 }
160
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200161 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
hbos6b470a92016-04-28 05:14:21 -0700177 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:
jbauch555604a2016-04-26 03:13:22 -0700209 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.orgf0488722014-05-13 18:00:26 +0000214};
215
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200216TEST_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 Granlundb6d4ec42015-08-17 14:08:59 +0200221TEST_F(SSLIdentityTest, FixedDigestSHA224) {
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200222 TestDigestForFixedCert(rtc::DIGEST_SHA_224, 28, kTestCertSha224);
223}
224
225TEST_F(SSLIdentityTest, FixedDigestSHA256) {
226 TestDigestForFixedCert(rtc::DIGEST_SHA_256, 32, kTestCertSha256);
227}
228
229TEST_F(SSLIdentityTest, FixedDigestSHA384) {
230 TestDigestForFixedCert(rtc::DIGEST_SHA_384, 48, kTestCertSha384);
231}
232
233TEST_F(SSLIdentityTest, FixedDigestSHA512) {
234 TestDigestForFixedCert(rtc::DIGEST_SHA_512, 64, kTestCertSha512);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000235}
236
237// HASH_AlgSHA224 is not supported in the chromium linux build.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000238TEST_F(SSLIdentityTest, DigestSHA224) {
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200239 TestDigestForGeneratedCert(rtc::DIGEST_SHA_224, 28);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000240}
241
242TEST_F(SSLIdentityTest, DigestSHA256) {
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200243 TestDigestForGeneratedCert(rtc::DIGEST_SHA_256, 32);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000244}
245
246TEST_F(SSLIdentityTest, DigestSHA384) {
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200247 TestDigestForGeneratedCert(rtc::DIGEST_SHA_384, 48);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000248}
249
250TEST_F(SSLIdentityTest, DigestSHA512) {
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200251 TestDigestForGeneratedCert(rtc::DIGEST_SHA_512, 64);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000252}
253
hbos6b470a92016-04-28 05:14:21 -0700254TEST_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.orgf0488722014-05-13 18:00:26 +0000259
hbos6b470a92016-04-28 05:14:21 -0700260 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
268TEST_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.orgf0488722014-05-13 18:00:26 +0000299 static const char kCERT_PEM[] =
300 "-----BEGIN CERTIFICATE-----\n"
hbos6b470a92016-04-28 05:14:21 -0700301 "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.orgf0488722014-05-13 18:00:26 +0000310 "-----END CERTIFICATE-----\n";
311
jbauch555604a2016-04-26 03:13:22 -0700312 std::unique_ptr<SSLIdentity> identity(
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000313 SSLIdentity::FromPEMStrings(kRSA_PRIVATE_KEY_PEM, kCERT_PEM));
314 EXPECT_TRUE(identity);
hbos6b470a92016-04-28 05:14:21 -0700315 EXPECT_EQ(kRSA_PRIVATE_KEY_PEM, identity->PrivateKeyToPEMString());
316 EXPECT_EQ(kRSA_PUBLIC_KEY_PEM, identity->PublicKeyToPEMString());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000317 EXPECT_EQ(kCERT_PEM, identity->certificate().ToPEMString());
318}
319
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200320TEST_F(SSLIdentityTest, FromPEMStringsEC) {
hbos6b470a92016-04-28 05:14:21 -0700321 // 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 Granlundb6d4ec42015-08-17 14:08:59 +0200338 static const char kCERT_PEM[] =
339 "-----BEGIN CERTIFICATE-----\n"
hbos6b470a92016-04-28 05:14:21 -0700340 "MIIBFDCBu6ADAgECAgkArpkxjw62sW4wCgYIKoZIzj0EAwIwEDEOMAwGA1UEAwwF\n"
341 "dGVzdDMwHhcNMTYwNDI0MTgxNDM4WhcNMTYwNTI1MTgxNDM4WjAQMQ4wDAYDVQQD\n"
342 "DAV0ZXN0MzBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABHtiB07e60//wipVGrwb\n"
343 "hVkqt4wDF42tRrXa3C2G/i4WO4cAdr0P2jICOdyaf7G9llHJWYqsDnqNHMQ1wQQe\n"
344 "gCAwCgYIKoZIzj0EAwIDSAAwRQIhANyreQ/K5yuPPpirsd0e/4WGLHou6bIOSQks\n"
345 "DYzo56NmAiAKOr3u8ol3LmygbUCwEvtWrS8QcJDygxHPACo99hkekw==\n"
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200346 "-----END CERTIFICATE-----\n";
347
jbauch555604a2016-04-26 03:13:22 -0700348 std::unique_ptr<SSLIdentity> identity(
hbos6b470a92016-04-28 05:14:21 -0700349 SSLIdentity::FromPEMStrings(kECDSA_PRIVATE_KEY_PEM, kCERT_PEM));
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200350 EXPECT_TRUE(identity);
hbos6b470a92016-04-28 05:14:21 -0700351 EXPECT_EQ(kECDSA_PRIVATE_KEY_PEM, identity->PrivateKeyToPEMString());
352 EXPECT_EQ(kECDSA_PUBLIC_KEY_PEM, identity->PublicKeyToPEMString());
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200353 EXPECT_EQ(kCERT_PEM, identity->certificate().ToPEMString());
354}
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200355
hbos6b470a92016-04-28 05:14:21 -0700356TEST_F(SSLIdentityTest, CloneIdentityRSA) {
357 TestCloningIdentity(*identity_rsa1_);
358 TestCloningIdentity(*identity_rsa2_);
359}
360
361TEST_F(SSLIdentityTest, CloneIdentityECDSA) {
362 TestCloningIdentity(*identity_ecdsa1_);
363 TestCloningIdentity(*identity_ecdsa2_);
364}
365
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000366TEST_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
375TEST_F(SSLIdentityTest, GetSignatureDigestAlgorithm) {
376 TestGetSignatureDigestAlgorithm();
377}
Torbjorn Granlund46c9cc02015-12-01 13:06:34 +0100378
379class 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) {
torbjornge8dc0812016-02-15 09:35:54 -0800469 // We test just ECDSA here since what we're out to exercise is the
470 // interfaces for expiration setting and reading.
Torbjorn Granlund46c9cc02015-12-01 13:06:34 +0100471 for (int i = 0; i < times; i++) {
Torbjorn Granlund46c9cc02015-12-01 13:06:34 +0100472 // 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 Brandstetter88dec832016-03-15 09:57:55 -0700474 time_t time_before_generation = time(nullptr);
475 time_t lifetime =
476 rtc::CreateRandomId() % (0x80000000 - time_before_generation);
torbjornge8dc0812016-02-15 09:35:54 -0800477 rtc::KeyParams key_params = rtc::KeyParams::ECDSA(rtc::EC_NIST_P256);
478 SSLIdentity* identity =
Torbjorn Granlund1d846b22016-03-31 16:21:04 +0200479 rtc::SSLIdentity::GenerateWithExpiration("", key_params, lifetime);
Taylor Brandstetter88dec832016-03-15 09:57:55 -0700480 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 Granlund46c9cc02015-12-01 13:06:34 +0100484 identity->certificate().CertificateExpirationTime());
485 delete identity;
486 }
487 }
488};
489
490TEST_F(SSLIdentityExpirationTest, TestASN1TimeToSec) {
491 TestASN1TimeToSec();
492}
493
494TEST_F(SSLIdentityExpirationTest, TestExpireTime) {
495 TestExpireTime(500);
496}