Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(159)

Side by Side Diff: webrtc/base/sslidentity_unittest.cc

Issue 2259283002: Refactor certificate stats collection, added SSLCertificateStats. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Another unittest for the case of certificate chains using fake certificates Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/base/sslidentity.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2011 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2011 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include <memory> 11 #include <memory>
12 #include <string> 12 #include <string>
13 13
14 #include "webrtc/base/fakesslidentity.h"
14 #include "webrtc/base/gunit.h" 15 #include "webrtc/base/gunit.h"
15 #include "webrtc/base/helpers.h" 16 #include "webrtc/base/helpers.h"
16 #include "webrtc/base/ssladapter.h" 17 #include "webrtc/base/ssladapter.h"
18 #include "webrtc/base/sslfingerprint.h"
17 #include "webrtc/base/sslidentity.h" 19 #include "webrtc/base/sslidentity.h"
20 #include "webrtc/base/stringutils.h"
18 21
19 using rtc::SSLIdentity; 22 using rtc::SSLIdentity;
20 23
21 const char kTestCertificate[] = "-----BEGIN CERTIFICATE-----\n" 24 const char kTestCertificate[] = "-----BEGIN CERTIFICATE-----\n"
22 "MIIB6TCCAVICAQYwDQYJKoZIhvcNAQEEBQAwWzELMAkGA1UEBhMCQVUxEzARBgNV\n" 25 "MIIB6TCCAVICAQYwDQYJKoZIhvcNAQEEBQAwWzELMAkGA1UEBhMCQVUxEzARBgNV\n"
23 "BAgTClF1ZWVuc2xhbmQxGjAYBgNVBAoTEUNyeXB0U29mdCBQdHkgTHRkMRswGQYD\n" 26 "BAgTClF1ZWVuc2xhbmQxGjAYBgNVBAoTEUNyeXB0U29mdCBQdHkgTHRkMRswGQYD\n"
24 "VQQDExJUZXN0IENBICgxMDI0IGJpdCkwHhcNMDAxMDE2MjIzMTAzWhcNMDMwMTE0\n" 27 "VQQDExJUZXN0IENBICgxMDI0IGJpdCkwHhcNMDAxMDE2MjIzMTAzWhcNMDMwMTE0\n"
25 "MjIzMTAzWjBjMQswCQYDVQQGEwJBVTETMBEGA1UECBMKUXVlZW5zbGFuZDEaMBgG\n" 28 "MjIzMTAzWjBjMQswCQYDVQQGEwJBVTETMBEGA1UECBMKUXVlZW5zbGFuZDEaMBgG\n"
26 "A1UEChMRQ3J5cHRTb2Z0IFB0eSBMdGQxIzAhBgNVBAMTGlNlcnZlciB0ZXN0IGNl\n" 29 "A1UEChMRQ3J5cHRTb2Z0IFB0eSBMdGQxIzAhBgNVBAMTGlNlcnZlciB0ZXN0IGNl\n"
27 "cnQgKDUxMiBiaXQpMFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJ+zw4Qnlf8SMVIP\n" 30 "cnQgKDUxMiBiaXQpMFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJ+zw4Qnlf8SMVIP\n"
(...skipping 28 matching lines...) Expand all
56 const unsigned char kTestCertSha512[] = { 59 const unsigned char kTestCertSha512[] = {
57 0x51, 0x1d, 0xec, 0x02, 0x3d, 0x51, 0x45, 0xd3, 60 0x51, 0x1d, 0xec, 0x02, 0x3d, 0x51, 0x45, 0xd3,
58 0xd8, 0x1d, 0xa4, 0x9d, 0x43, 0xc9, 0xee, 0x32, 61 0xd8, 0x1d, 0xa4, 0x9d, 0x43, 0xc9, 0xee, 0x32,
59 0x6f, 0x4f, 0x37, 0xee, 0xab, 0x3f, 0x25, 0xdf, 62 0x6f, 0x4f, 0x37, 0xee, 0xab, 0x3f, 0x25, 0xdf,
60 0x72, 0xfc, 0x61, 0x1a, 0xd5, 0x92, 0xff, 0x6b, 63 0x72, 0xfc, 0x61, 0x1a, 0xd5, 0x92, 0xff, 0x6b,
61 0x28, 0x71, 0x58, 0xb3, 0xe1, 0x8a, 0x18, 0xcf, 64 0x28, 0x71, 0x58, 0xb3, 0xe1, 0x8a, 0x18, 0xcf,
62 0x61, 0x33, 0x0e, 0x14, 0xc3, 0x04, 0xaa, 0x07, 65 0x61, 0x33, 0x0e, 0x14, 0xc3, 0x04, 0xaa, 0x07,
63 0xf6, 0xa5, 0xda, 0xdc, 0x42, 0x42, 0x22, 0x35, 66 0xf6, 0xa5, 0xda, 0xdc, 0x42, 0x42, 0x22, 0x35,
64 0xce, 0x26, 0x58, 0x4a, 0x33, 0x6d, 0xbc, 0xb6}; 67 0xce, 0x26, 0x58, 0x4a, 0x33, 0x6d, 0xbc, 0xb6};
65 68
69 // These PEM strings were created by generating an identity with
70 // |SSLIdentity::Generate| and invoking |identity->PrivateKeyToPEMString()|,
71 // |identity->PublicKeyToPEMString()| and
72 // |identity->certificate().ToPEMString()|. If the crypto library is updated,
73 // and the update changes the string form of the keys, these will have to be
74 // updated too. The fingerprint, fingerprint algorithm and base64 certificate
75 // were created by calling |identity->certificate().GetStats()|.
76 static const char kRSA_PRIVATE_KEY_PEM[] =
77 "-----BEGIN PRIVATE KEY-----\n"
78 "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAMQPqDStRlYeDpkX\n"
79 "erRmv+a1naM8vSVSY0gG2plnrnofViWRW3MRqWC+020MsIj3hPZeSAnt/y/FL/nr\n"
80 "4Ea7NXcwdRo1/1xEK7U/f/cjSg1aunyvHCHwcFcMr31HLFvHr0ZgcFwbgIuFLNEl\n"
81 "7kK5HMO9APz1ntUjek8BmBj8yMl9AgMBAAECgYA8FWBC5GcNtSBcIinkZyigF0A7\n"
82 "6j081sa+J/uNz4xUuI257ZXM6biygUhhvuXK06/XoIULJfhyN0fAm1yb0HtNhiUs\n"
83 "kMOYeon6b8FqFaPjrQf7Gr9FMiIHXNK19uegTMKztXyPZoUWlX84X0iawY95x0Y3\n"
84 "73f6P2rN2UOjlVVjAQJBAOKy3l2w3Zj2w0oAJox0eMwl+RxBNt1C42SHrob2mFUT\n"
85 "rytpVVYOasr8CoDI0kjacjI94sLum+buJoXXX6YTGO0CQQDdZwlYIEkoS3ftfxPa\n"
86 "Ai0YTBzAWvHJg0r8Gk/TkHo6IM+LSsZ9ZYUv/vBe4BKLw1I4hZ+bQvBiq+f8ROtk\n"
87 "+TDRAkAPL3ghwoU1h+IRBO2QHwUwd6K2N9AbBi4BP+168O3HVSg4ujeTKigRLMzv\n"
88 "T4R2iNt5bhfQgvdCgtVlxcWMdF8JAkBwDCg3eEdt5BuyjwBt8XH+/O4ED0KUWCTH\n"
89 "x00k5dZlupsuhE5Fwe4QpzXg3gekwdnHjyCCQ/NCDHvgOMTkmhQxAkA9V03KRX9b\n"
90 "bhvEzY/fu8gEp+EzsER96/D79az5z1BaMGL5OPM2xHBPJATKlswnAa7Lp3QKGZGk\n"
91 "TxslfL18J71s\n"
92 "-----END PRIVATE KEY-----\n";
93 static const char kRSA_PUBLIC_KEY_PEM[] =
94 "-----BEGIN PUBLIC KEY-----\n"
95 "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDED6g0rUZWHg6ZF3q0Zr/mtZ2j\n"
96 "PL0lUmNIBtqZZ656H1YlkVtzEalgvtNtDLCI94T2XkgJ7f8vxS/56+BGuzV3MHUa\n"
97 "Nf9cRCu1P3/3I0oNWrp8rxwh8HBXDK99Ryxbx69GYHBcG4CLhSzRJe5CuRzDvQD8\n"
98 "9Z7VI3pPAZgY/MjJfQIDAQAB\n"
99 "-----END PUBLIC KEY-----\n";
100 static const char kRSA_CERT_PEM[] =
101 "-----BEGIN CERTIFICATE-----\n"
102 "MIIBnDCCAQWgAwIBAgIJAOEHLgeWYwrpMA0GCSqGSIb3DQEBCwUAMBAxDjAMBgNV\n"
103 "BAMMBXRlc3QxMB4XDTE2MDQyNDE4MTAyMloXDTE2MDUyNTE4MTAyMlowEDEOMAwG\n"
104 "A1UEAwwFdGVzdDEwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMQPqDStRlYe\n"
105 "DpkXerRmv+a1naM8vSVSY0gG2plnrnofViWRW3MRqWC+020MsIj3hPZeSAnt/y/F\n"
106 "L/nr4Ea7NXcwdRo1/1xEK7U/f/cjSg1aunyvHCHwcFcMr31HLFvHr0ZgcFwbgIuF\n"
107 "LNEl7kK5HMO9APz1ntUjek8BmBj8yMl9AgMBAAEwDQYJKoZIhvcNAQELBQADgYEA\n"
108 "C3ehaZFl+oEYN069C2ht/gMzuC77L854RF/x7xRtNZzkcg9TVgXXdM3auUvJi8dx\n"
109 "yTpU3ixErjQvoZew5ngXTEvTY8BSQUijJEaLWh8n6NDKRbEGTdAk8nPAmq9hdCFq\n"
110 "e3UkexqNHm3g/VxG4NUC1Y+w29ai0/Rgh+VvgbDwK+Q=\n"
111 "-----END CERTIFICATE-----\n";
112 static const char kRSA_FINGERPRINT[] =
113 "3C:E8:B2:70:09:CF:A9:09:5A:F4:EF:8F:8D:8A:32:FF:EA:04:91:BA:6E:D4:17:78:16"
114 ":2A:EE:F9:9A:DD:E2:2B";
115 static const char kRSA_FINGERPRINT_ALGORITHM[] =
116 "sha-256";
117 static const char kRSA_BASE64_CERTIFICATE[] =
118 "MIIBnDCCAQWgAwIBAgIJAOEHLgeWYwrpMA0GCSqGSIb3DQEBCwUAMBAxDjAMBgNVBAMMBXRlc3"
119 "QxMB4XDTE2MDQyNDE4MTAyMloXDTE2MDUyNTE4MTAyMlowEDEOMAwGA1UEAwwFdGVzdDEwgZ8w"
120 "DQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMQPqDStRlYeDpkXerRmv+a1naM8vSVSY0gG2plnrn"
121 "ofViWRW3MRqWC+020MsIj3hPZeSAnt/y/FL/nr4Ea7NXcwdRo1/1xEK7U/f/cjSg1aunyvHCHw"
122 "cFcMr31HLFvHr0ZgcFwbgIuFLNEl7kK5HMO9APz1ntUjek8BmBj8yMl9AgMBAAEwDQYJKoZIhv"
123 "cNAQELBQADgYEAC3ehaZFl+oEYN069C2ht/gMzuC77L854RF/x7xRtNZzkcg9TVgXXdM3auUvJ"
124 "i8dxyTpU3ixErjQvoZew5ngXTEvTY8BSQUijJEaLWh8n6NDKRbEGTdAk8nPAmq9hdCFqe3Ukex"
125 "qNHm3g/VxG4NUC1Y+w29ai0/Rgh+VvgbDwK+Q=";
126
127 static const char kECDSA_PRIVATE_KEY_PEM[] =
128 "-----BEGIN PRIVATE KEY-----\n"
129 "MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg/AkEA2hklq7dQ2rN\n"
130 "ZxYL6hOUACL4pn7P4FYlA3ZQhIChRANCAAR7YgdO3utP/8IqVRq8G4VZKreMAxeN\n"
131 "rUa12twthv4uFjuHAHa9D9oyAjncmn+xvZZRyVmKrA56jRzENcEEHoAg\n"
132 "-----END PRIVATE KEY-----\n";
133 static const char kECDSA_PUBLIC_KEY_PEM[] =
134 "-----BEGIN PUBLIC KEY-----\n"
135 "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEe2IHTt7rT//CKlUavBuFWSq3jAMX\n"
136 "ja1GtdrcLYb+LhY7hwB2vQ/aMgI53Jp/sb2WUclZiqwOeo0cxDXBBB6AIA==\n"
137 "-----END PUBLIC KEY-----\n";
138 static const char kECDSA_CERT_PEM[] =
139 "-----BEGIN CERTIFICATE-----\n"
140 "MIIBFDCBu6ADAgECAgkArpkxjw62sW4wCgYIKoZIzj0EAwIwEDEOMAwGA1UEAwwF\n"
141 "dGVzdDMwHhcNMTYwNDI0MTgxNDM4WhcNMTYwNTI1MTgxNDM4WjAQMQ4wDAYDVQQD\n"
142 "DAV0ZXN0MzBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABHtiB07e60//wipVGrwb\n"
143 "hVkqt4wDF42tRrXa3C2G/i4WO4cAdr0P2jICOdyaf7G9llHJWYqsDnqNHMQ1wQQe\n"
144 "gCAwCgYIKoZIzj0EAwIDSAAwRQIhANyreQ/K5yuPPpirsd0e/4WGLHou6bIOSQks\n"
145 "DYzo56NmAiAKOr3u8ol3LmygbUCwEvtWrS8QcJDygxHPACo99hkekw==\n"
146 "-----END CERTIFICATE-----\n";
147 static const char kECDSA_FINGERPRINT[] =
148 "9F:47:FA:88:76:3D:18:B8:00:A0:59:9D:C3:5D:34:0B:1F:B8:99:9E:68:DA:F3:A5:DA"
149 ":50:33:A9:FF:4D:31:89";
150 static const char kECDSA_FINGERPRINT_ALGORITHM[] =
151 "sha-256";
152 static const char kECDSA_BASE64_CERTIFICATE[] =
153 "MIIBFDCBu6ADAgECAgkArpkxjw62sW4wCgYIKoZIzj0EAwIwEDEOMAwGA1UEAwwFdGVzdDMwHh"
154 "cNMTYwNDI0MTgxNDM4WhcNMTYwNTI1MTgxNDM4WjAQMQ4wDAYDVQQDDAV0ZXN0MzBZMBMGByqG"
155 "SM49AgEGCCqGSM49AwEHA0IABHtiB07e60//wipVGrwbhVkqt4wDF42tRrXa3C2G/i4WO4cAdr"
156 "0P2jICOdyaf7G9llHJWYqsDnqNHMQ1wQQegCAwCgYIKoZIzj0EAwIDSAAwRQIhANyreQ/K5yuP"
157 "Ppirsd0e/4WGLHou6bIOSQksDYzo56NmAiAKOr3u8ol3LmygbUCwEvtWrS8QcJDygxHPACo99h"
158 "kekw==";
159
160 struct IdentityAndInfo {
161 std::unique_ptr<rtc::SSLIdentity> identity;
162 std::vector<std::string> ders;
163 std::vector<std::string> pems;
164 std::vector<std::string> fingerprints;
165 };
166
167 IdentityAndInfo CreateFakeIdentityAndInfoFromDers(
168 const std::vector<std::string>& ders) {
169 RTC_CHECK(!ders.empty());
170 IdentityAndInfo info;
171 info.ders = ders;
172 for (const std::string& der : ders) {
173 info.pems.push_back(rtc::SSLIdentity::DerToPem(
174 "CERTIFICATE",
175 reinterpret_cast<const unsigned char*>(der.c_str()),
176 der.length()));
177 }
178 info.identity.reset(
179 new rtc::FakeSSLIdentity(rtc::FakeSSLCertificate(info.pems)));
180 // Strip header/footer and newline characters of PEM strings.
181 for (size_t i = 0; i < info.pems.size(); ++i) {
182 rtc::replace_substrs("-----BEGIN CERTIFICATE-----", 27,
183 "", 0, &info.pems[i]);
184 rtc::replace_substrs("-----END CERTIFICATE-----", 25,
185 "", 0, &info.pems[i]);
186 rtc::replace_substrs("\n", 1,
187 "", 0, &info.pems[i]);
188 }
189 // Fingerprint of leaf certificate.
190 std::unique_ptr<rtc::SSLFingerprint> fp(
191 rtc::SSLFingerprint::Create("sha-1", &info.identity->certificate()));
192 EXPECT_TRUE(fp);
193 info.fingerprints.push_back(fp->GetRfc4572Fingerprint());
194 // Fingerprints of the rest of the chain.
195 std::unique_ptr<rtc::SSLCertChain> chain =
196 info.identity->certificate().GetChain();
197 if (chain) {
198 for (size_t i = 0; i < chain->GetSize(); i++) {
199 fp.reset(rtc::SSLFingerprint::Create("sha-1", &chain->Get(i)));
200 EXPECT_TRUE(fp);
201 info.fingerprints.push_back(fp->GetRfc4572Fingerprint());
202 }
203 }
204 EXPECT_EQ(info.ders.size(), info.fingerprints.size());
205 return info;
206 }
207
66 class SSLIdentityTest : public testing::Test { 208 class SSLIdentityTest : public testing::Test {
67 public: 209 public:
68 SSLIdentityTest() {} 210 SSLIdentityTest() {}
69 211
70 ~SSLIdentityTest() { 212 ~SSLIdentityTest() {
71 } 213 }
72 214
73 virtual void SetUp() { 215 virtual void SetUp() {
74 identity_rsa1_.reset(SSLIdentity::Generate("test1", rtc::KT_RSA)); 216 identity_rsa1_.reset(SSLIdentity::Generate("test1", rtc::KT_RSA));
75 identity_rsa2_.reset(SSLIdentity::Generate("test2", rtc::KT_RSA)); 217 identity_rsa2_.reset(SSLIdentity::Generate("test2", rtc::KT_RSA));
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
259 401
260 EXPECT_TRUE(*identity_rsa2_ == *identity_rsa2_); 402 EXPECT_TRUE(*identity_rsa2_ == *identity_rsa2_);
261 EXPECT_FALSE(*identity_rsa2_ == *identity_ecdsa1_); 403 EXPECT_FALSE(*identity_rsa2_ == *identity_ecdsa1_);
262 EXPECT_FALSE(*identity_rsa2_ == *identity_ecdsa2_); 404 EXPECT_FALSE(*identity_rsa2_ == *identity_ecdsa2_);
263 405
264 EXPECT_TRUE(*identity_ecdsa1_ == *identity_ecdsa1_); 406 EXPECT_TRUE(*identity_ecdsa1_ == *identity_ecdsa1_);
265 EXPECT_FALSE(*identity_ecdsa1_ == *identity_ecdsa2_); 407 EXPECT_FALSE(*identity_ecdsa1_ == *identity_ecdsa2_);
266 } 408 }
267 409
268 TEST_F(SSLIdentityTest, FromPEMStringsRSA) { 410 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";
299 static const char kCERT_PEM[] =
300 "-----BEGIN CERTIFICATE-----\n"
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"
310 "-----END CERTIFICATE-----\n";
311
312 std::unique_ptr<SSLIdentity> identity( 411 std::unique_ptr<SSLIdentity> identity(
313 SSLIdentity::FromPEMStrings(kRSA_PRIVATE_KEY_PEM, kCERT_PEM)); 412 SSLIdentity::FromPEMStrings(kRSA_PRIVATE_KEY_PEM, kRSA_CERT_PEM));
314 EXPECT_TRUE(identity); 413 EXPECT_TRUE(identity);
315 EXPECT_EQ(kRSA_PRIVATE_KEY_PEM, identity->PrivateKeyToPEMString()); 414 EXPECT_EQ(kRSA_PRIVATE_KEY_PEM, identity->PrivateKeyToPEMString());
316 EXPECT_EQ(kRSA_PUBLIC_KEY_PEM, identity->PublicKeyToPEMString()); 415 EXPECT_EQ(kRSA_PUBLIC_KEY_PEM, identity->PublicKeyToPEMString());
317 EXPECT_EQ(kCERT_PEM, identity->certificate().ToPEMString()); 416 EXPECT_EQ(kRSA_CERT_PEM, identity->certificate().ToPEMString());
318 } 417 }
319 418
320 TEST_F(SSLIdentityTest, FromPEMStringsEC) { 419 TEST_F(SSLIdentityTest, FromPEMStringsEC) {
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";
338 static const char kCERT_PEM[] =
339 "-----BEGIN CERTIFICATE-----\n"
340 "MIIBFDCBu6ADAgECAgkArpkxjw62sW4wCgYIKoZIzj0EAwIwEDEOMAwGA1UEAwwF\n"
341 "dGVzdDMwHhcNMTYwNDI0MTgxNDM4WhcNMTYwNTI1MTgxNDM4WjAQMQ4wDAYDVQQD\n"
342 "DAV0ZXN0MzBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABHtiB07e60//wipVGrwb\n"
343 "hVkqt4wDF42tRrXa3C2G/i4WO4cAdr0P2jICOdyaf7G9llHJWYqsDnqNHMQ1wQQe\n"
344 "gCAwCgYIKoZIzj0EAwIDSAAwRQIhANyreQ/K5yuPPpirsd0e/4WGLHou6bIOSQks\n"
345 "DYzo56NmAiAKOr3u8ol3LmygbUCwEvtWrS8QcJDygxHPACo99hkekw==\n"
346 "-----END CERTIFICATE-----\n";
347
348 std::unique_ptr<SSLIdentity> identity( 420 std::unique_ptr<SSLIdentity> identity(
349 SSLIdentity::FromPEMStrings(kECDSA_PRIVATE_KEY_PEM, kCERT_PEM)); 421 SSLIdentity::FromPEMStrings(kECDSA_PRIVATE_KEY_PEM, kECDSA_CERT_PEM));
350 EXPECT_TRUE(identity); 422 EXPECT_TRUE(identity);
351 EXPECT_EQ(kECDSA_PRIVATE_KEY_PEM, identity->PrivateKeyToPEMString()); 423 EXPECT_EQ(kECDSA_PRIVATE_KEY_PEM, identity->PrivateKeyToPEMString());
352 EXPECT_EQ(kECDSA_PUBLIC_KEY_PEM, identity->PublicKeyToPEMString()); 424 EXPECT_EQ(kECDSA_PUBLIC_KEY_PEM, identity->PublicKeyToPEMString());
353 EXPECT_EQ(kCERT_PEM, identity->certificate().ToPEMString()); 425 EXPECT_EQ(kECDSA_CERT_PEM, identity->certificate().ToPEMString());
354 } 426 }
355 427
356 TEST_F(SSLIdentityTest, CloneIdentityRSA) { 428 TEST_F(SSLIdentityTest, CloneIdentityRSA) {
357 TestCloningIdentity(*identity_rsa1_); 429 TestCloningIdentity(*identity_rsa1_);
358 TestCloningIdentity(*identity_rsa2_); 430 TestCloningIdentity(*identity_rsa2_);
359 } 431 }
360 432
361 TEST_F(SSLIdentityTest, CloneIdentityECDSA) { 433 TEST_F(SSLIdentityTest, CloneIdentityECDSA) {
362 TestCloningIdentity(*identity_ecdsa1_); 434 TestCloningIdentity(*identity_ecdsa1_);
363 TestCloningIdentity(*identity_ecdsa2_); 435 TestCloningIdentity(*identity_ecdsa2_);
364 } 436 }
365 437
366 TEST_F(SSLIdentityTest, PemDerConversion) { 438 TEST_F(SSLIdentityTest, PemDerConversion) {
367 std::string der; 439 std::string der;
368 EXPECT_TRUE(SSLIdentity::PemToDer("CERTIFICATE", kTestCertificate, &der)); 440 EXPECT_TRUE(SSLIdentity::PemToDer("CERTIFICATE", kTestCertificate, &der));
369 441
370 EXPECT_EQ(kTestCertificate, SSLIdentity::DerToPem( 442 EXPECT_EQ(kTestCertificate, SSLIdentity::DerToPem(
371 "CERTIFICATE", 443 "CERTIFICATE",
372 reinterpret_cast<const unsigned char*>(der.data()), der.length())); 444 reinterpret_cast<const unsigned char*>(der.data()), der.length()));
373 } 445 }
374 446
375 TEST_F(SSLIdentityTest, GetSignatureDigestAlgorithm) { 447 TEST_F(SSLIdentityTest, GetSignatureDigestAlgorithm) {
376 TestGetSignatureDigestAlgorithm(); 448 TestGetSignatureDigestAlgorithm();
377 } 449 }
378 450
451 TEST_F(SSLIdentityTest, SSLCertificateGetStatsRSA) {
452 std::unique_ptr<SSLIdentity> identity(
453 SSLIdentity::FromPEMStrings(kRSA_PRIVATE_KEY_PEM, kRSA_CERT_PEM));
454 std::unique_ptr<rtc::SSLCertificateStats> stats =
455 identity->certificate().GetStats();
456 EXPECT_EQ(stats->fingerprint, kRSA_FINGERPRINT);
457 EXPECT_EQ(stats->fingerprint_algorithm, kRSA_FINGERPRINT_ALGORITHM);
458 EXPECT_EQ(stats->base64_certificate, kRSA_BASE64_CERTIFICATE);
459 EXPECT_FALSE(stats->issuer);
460 }
461
462 TEST_F(SSLIdentityTest, SSLCertificateGetStatsECDSA) {
463 std::unique_ptr<SSLIdentity> identity(
464 SSLIdentity::FromPEMStrings(kECDSA_PRIVATE_KEY_PEM, kECDSA_CERT_PEM));
465 std::unique_ptr<rtc::SSLCertificateStats> stats =
466 identity->certificate().GetStats();
467 EXPECT_EQ(stats->fingerprint, kECDSA_FINGERPRINT);
468 EXPECT_EQ(stats->fingerprint_algorithm, kECDSA_FINGERPRINT_ALGORITHM);
469 EXPECT_EQ(stats->base64_certificate, kECDSA_BASE64_CERTIFICATE);
470 EXPECT_FALSE(stats->issuer);
471 }
472
473 TEST_F(SSLIdentityTest, SSLCertificateGetStatsWithChain) {
474 std::vector<std::string> ders;
475 ders.push_back("every der results in");
476 ders.push_back("an identity + certificate");
477 ders.push_back("in a certificate chain");
478 IdentityAndInfo info = CreateFakeIdentityAndInfoFromDers(ders);
479 EXPECT_TRUE(info.identity);
480 EXPECT_EQ(info.ders, ders);
481 EXPECT_EQ(info.pems.size(), info.ders.size());
482 EXPECT_EQ(info.fingerprints.size(), info.ders.size());
483
484 std::unique_ptr<rtc::SSLCertificateStats> first_stats =
485 info.identity->certificate().GetStats();
486 rtc::SSLCertificateStats* cert_stats = first_stats.get();
487 for (size_t i = 0; i < info.ders.size(); ++i) {
hta-webrtc 2016/08/25 09:32:14 Do you want to add EXPECT_GE(1, info.ders.size() (
hbos 2016/08/25 10:46:33 Not necessary, I have already EXPECT checked that
488 EXPECT_EQ(cert_stats->fingerprint, info.fingerprints[i]);
489 EXPECT_EQ(cert_stats->fingerprint_algorithm, "sha-1");
490 EXPECT_EQ(cert_stats->base64_certificate, info.pems[i]);
491 cert_stats = cert_stats->issuer.get();
492 EXPECT_EQ(static_cast<bool>(cert_stats), i + 1 < info.ders.size());
493 }
494 }
495
379 class SSLIdentityExpirationTest : public testing::Test { 496 class SSLIdentityExpirationTest : public testing::Test {
380 public: 497 public:
381 SSLIdentityExpirationTest() { 498 SSLIdentityExpirationTest() {
382 // Set use of the test RNG to get deterministic expiration timestamp. 499 // Set use of the test RNG to get deterministic expiration timestamp.
383 rtc::SetRandomTestMode(true); 500 rtc::SetRandomTestMode(true);
384 } 501 }
385 ~SSLIdentityExpirationTest() { 502 ~SSLIdentityExpirationTest() {
386 // Put it back for the next test. 503 // Put it back for the next test.
387 rtc::SetRandomTestMode(false); 504 rtc::SetRandomTestMode(false);
388 } 505 }
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
487 } 604 }
488 }; 605 };
489 606
490 TEST_F(SSLIdentityExpirationTest, TestASN1TimeToSec) { 607 TEST_F(SSLIdentityExpirationTest, TestASN1TimeToSec) {
491 TestASN1TimeToSec(); 608 TestASN1TimeToSec();
492 } 609 }
493 610
494 TEST_F(SSLIdentityExpirationTest, TestExpireTime) { 611 TEST_F(SSLIdentityExpirationTest, TestExpireTime) {
495 TestExpireTime(500); 612 TestExpireTime(500);
496 } 613 }
OLDNEW
« no previous file with comments | « webrtc/base/sslidentity.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698