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 | |
| 11 | #include <string> |
| 12 | |
| 13 | #include "webrtc/base/gunit.h" |
| 14 | #include "webrtc/base/ssladapter.h" |
| 15 | #include "webrtc/base/sslidentity.h" |
| 16 | |
| 17 | using rtc::SSLIdentity; |
| 18 | |
| 19 | const char kTestCertificate[] = "-----BEGIN CERTIFICATE-----\n" |
| 20 | "MIIB6TCCAVICAQYwDQYJKoZIhvcNAQEEBQAwWzELMAkGA1UEBhMCQVUxEzARBgNV\n" |
| 21 | "BAgTClF1ZWVuc2xhbmQxGjAYBgNVBAoTEUNyeXB0U29mdCBQdHkgTHRkMRswGQYD\n" |
| 22 | "VQQDExJUZXN0IENBICgxMDI0IGJpdCkwHhcNMDAxMDE2MjIzMTAzWhcNMDMwMTE0\n" |
| 23 | "MjIzMTAzWjBjMQswCQYDVQQGEwJBVTETMBEGA1UECBMKUXVlZW5zbGFuZDEaMBgG\n" |
| 24 | "A1UEChMRQ3J5cHRTb2Z0IFB0eSBMdGQxIzAhBgNVBAMTGlNlcnZlciB0ZXN0IGNl\n" |
| 25 | "cnQgKDUxMiBiaXQpMFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJ+zw4Qnlf8SMVIP\n" |
| 26 | "Fe9GEcStgOY2Ww/dgNdhjeD8ckUJNP5VZkVDTGiXav6ooKXfX3j/7tdkuD8Ey2//\n" |
| 27 | "Kv7+ue0CAwEAATANBgkqhkiG9w0BAQQFAAOBgQCT0grFQeZaqYb5EYfk20XixZV4\n" |
| 28 | "GmyAbXMftG1Eo7qGiMhYzRwGNWxEYojf5PZkYZXvSqZ/ZXHXa4g59jK/rJNnaVGM\n" |
| 29 | "k+xIX8mxQvlV0n5O9PIha5BX5teZnkHKgL8aKKLKW1BK7YTngsfSzzaeame5iKfz\n" |
| 30 | "itAE+OjGF+PFKbwX8Q==\n" |
| 31 | "-----END CERTIFICATE-----\n"; |
| 32 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame^] | 33 | const unsigned char kTestCertSha1[] = { |
| 34 | 0xA6, 0xC8, 0x59, 0xEA, 0xC3, 0x7E, 0x6D, 0x33, |
| 35 | 0xCF, 0xE2, 0x69, 0x9D, 0x74, 0xE6, 0xF6, 0x8A, |
| 36 | 0x9E, 0x47, 0xA7, 0xCA}; |
| 37 | const unsigned char kTestCertSha224[] = { |
| 38 | 0xd4, 0xce, 0xc6, 0xcf, 0x28, 0xcb, 0xe9, 0x77, |
| 39 | 0x38, 0x36, 0xcf, 0xb1, 0x3b, 0x4a, 0xd7, 0xbd, |
| 40 | 0xae, 0x24, 0x21, 0x08, 0xcf, 0x6a, 0x44, 0x0d, |
| 41 | 0x3f, 0x94, 0x2a, 0x5b}; |
| 42 | const unsigned char kTestCertSha256[] = { |
| 43 | 0x41, 0x6b, 0xb4, 0x93, 0x47, 0x79, 0x77, 0x24, |
| 44 | 0x77, 0x0b, 0x8b, 0x2e, 0xa6, 0x2b, 0xe0, 0xf9, |
| 45 | 0x0a, 0xed, 0x1f, 0x31, 0xa6, 0xf7, 0x5c, 0xa1, |
| 46 | 0x5a, 0xc4, 0xb0, 0xa2, 0xa4, 0x78, 0xb9, 0x76}; |
| 47 | const unsigned char kTestCertSha384[] = { |
| 48 | 0x42, 0x31, 0x9a, 0x79, 0x1d, 0xd6, 0x08, 0xbf, |
| 49 | 0x3b, 0xba, 0x36, 0xd8, 0x37, 0x4a, 0x9a, 0x75, |
| 50 | 0xd3, 0x25, 0x6e, 0x28, 0x92, 0xbe, 0x06, 0xb7, |
| 51 | 0xc5, 0xa0, 0x83, 0xe3, 0x86, 0xb1, 0x03, 0xfc, |
| 52 | 0x64, 0x47, 0xd6, 0xd8, 0xaa, 0xd9, 0x36, 0x60, |
| 53 | 0x04, 0xcc, 0xbe, 0x7d, 0x6a, 0xe8, 0x34, 0x49}; |
| 54 | const unsigned char kTestCertSha512[] = { |
| 55 | 0x51, 0x1d, 0xec, 0x02, 0x3d, 0x51, 0x45, 0xd3, |
| 56 | 0xd8, 0x1d, 0xa4, 0x9d, 0x43, 0xc9, 0xee, 0x32, |
| 57 | 0x6f, 0x4f, 0x37, 0xee, 0xab, 0x3f, 0x25, 0xdf, |
| 58 | 0x72, 0xfc, 0x61, 0x1a, 0xd5, 0x92, 0xff, 0x6b, |
| 59 | 0x28, 0x71, 0x58, 0xb3, 0xe1, 0x8a, 0x18, 0xcf, |
| 60 | 0x61, 0x33, 0x0e, 0x14, 0xc3, 0x04, 0xaa, 0x07, |
| 61 | 0xf6, 0xa5, 0xda, 0xdc, 0x42, 0x42, 0x22, 0x35, |
| 62 | 0xce, 0x26, 0x58, 0x4a, 0x33, 0x6d, 0xbc, 0xb6}; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 63 | |
| 64 | class SSLIdentityTest : public testing::Test { |
| 65 | public: |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame^] | 66 | SSLIdentityTest() {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 67 | |
| 68 | ~SSLIdentityTest() { |
| 69 | } |
| 70 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 71 | virtual void SetUp() { |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame^] | 72 | identity_rsa1_.reset(SSLIdentity::Generate("test1", rtc::KT_RSA)); |
| 73 | identity_rsa2_.reset(SSLIdentity::Generate("test2", rtc::KT_RSA)); |
| 74 | identity_ecdsa1_.reset(SSLIdentity::Generate("test3", rtc::KT_ECDSA)); |
| 75 | identity_ecdsa2_.reset(SSLIdentity::Generate("test4", rtc::KT_ECDSA)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 76 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame^] | 77 | ASSERT_TRUE(identity_rsa1_); |
| 78 | ASSERT_TRUE(identity_rsa2_); |
| 79 | ASSERT_TRUE(identity_ecdsa1_); |
| 80 | ASSERT_TRUE(identity_ecdsa2_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 81 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame^] | 82 | test_cert_.reset(rtc::SSLCertificate::FromPEMString(kTestCertificate)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 83 | ASSERT_TRUE(test_cert_); |
| 84 | } |
| 85 | |
| 86 | void TestGetSignatureDigestAlgorithm() { |
| 87 | std::string digest_algorithm; |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame^] | 88 | |
| 89 | ASSERT_TRUE(identity_rsa1_->certificate().GetSignatureDigestAlgorithm( |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 90 | &digest_algorithm)); |
Joachim Bauch | 1b794d5 | 2015-05-12 03:32:11 +0200 | [diff] [blame] | 91 | ASSERT_EQ(rtc::DIGEST_SHA_256, digest_algorithm); |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame^] | 92 | |
| 93 | ASSERT_TRUE(identity_rsa2_->certificate().GetSignatureDigestAlgorithm( |
| 94 | &digest_algorithm)); |
| 95 | ASSERT_EQ(rtc::DIGEST_SHA_256, digest_algorithm); |
| 96 | |
| 97 | ASSERT_TRUE(identity_ecdsa1_->certificate().GetSignatureDigestAlgorithm( |
| 98 | &digest_algorithm)); |
| 99 | ASSERT_EQ(rtc::DIGEST_SHA_256, digest_algorithm); |
| 100 | |
| 101 | ASSERT_TRUE(identity_ecdsa2_->certificate().GetSignatureDigestAlgorithm( |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 102 | &digest_algorithm)); |
Joachim Bauch | 1b794d5 | 2015-05-12 03:32:11 +0200 | [diff] [blame] | 103 | ASSERT_EQ(rtc::DIGEST_SHA_256, digest_algorithm); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 104 | |
| 105 | // The test certificate has an MD5-based signature. |
| 106 | ASSERT_TRUE(test_cert_->GetSignatureDigestAlgorithm(&digest_algorithm)); |
| 107 | ASSERT_EQ(rtc::DIGEST_MD5, digest_algorithm); |
| 108 | } |
| 109 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame^] | 110 | typedef unsigned char DigestType[rtc::MessageDigest::kMaxSize]; |
| 111 | |
| 112 | void TestDigestHelper(DigestType digest, |
| 113 | const SSLIdentity* identity, |
| 114 | const std::string& algorithm, |
| 115 | size_t expected_len) { |
| 116 | DigestType digest1; |
| 117 | size_t digest_len; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 118 | bool rv; |
| 119 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame^] | 120 | memset(digest, 0, expected_len); |
| 121 | rv = identity->certificate().ComputeDigest(algorithm, digest, |
| 122 | sizeof(DigestType), &digest_len); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 123 | EXPECT_TRUE(rv); |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame^] | 124 | EXPECT_EQ(expected_len, digest_len); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 125 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame^] | 126 | // Repeat digest computation for the identity as a sanity check. |
| 127 | memset(digest1, 0xff, expected_len); |
| 128 | rv = identity->certificate().ComputeDigest(algorithm, digest1, |
| 129 | sizeof(DigestType), &digest_len); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 130 | EXPECT_TRUE(rv); |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame^] | 131 | EXPECT_EQ(expected_len, digest_len); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 132 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame^] | 133 | EXPECT_EQ(0, memcmp(digest, digest1, expected_len)); |
| 134 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 135 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame^] | 136 | void TestDigestForGeneratedCert(const std::string& algorithm, |
| 137 | size_t expected_len) { |
| 138 | DigestType digest[4]; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 139 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame^] | 140 | ASSERT_TRUE(expected_len <= sizeof(DigestType)); |
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 | TestDigestHelper(digest[0], identity_rsa1_.get(), algorithm, expected_len); |
| 143 | TestDigestHelper(digest[1], identity_rsa2_.get(), algorithm, expected_len); |
| 144 | TestDigestHelper(digest[2], identity_ecdsa1_.get(), algorithm, |
| 145 | expected_len); |
| 146 | TestDigestHelper(digest[3], identity_ecdsa2_.get(), algorithm, |
| 147 | expected_len); |
| 148 | |
| 149 | // Sanity check that all four digests are unique. This could theoretically |
| 150 | // fail, since cryptographic hash collisions have a non-zero probability. |
| 151 | for (int i = 0; i < 4; i++) { |
| 152 | for (int j = 0; j < 4; j++) { |
| 153 | if (i != j) |
| 154 | EXPECT_NE(0, memcmp(digest[i], digest[j], expected_len)); |
| 155 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame^] | 159 | void TestDigestForFixedCert(const std::string& algorithm, |
| 160 | size_t expected_len, |
| 161 | const unsigned char* expected_digest) { |
| 162 | bool rv; |
| 163 | DigestType digest; |
| 164 | size_t digest_len; |
| 165 | |
| 166 | ASSERT_TRUE(expected_len <= sizeof(DigestType)); |
| 167 | |
| 168 | rv = test_cert_->ComputeDigest(algorithm, digest, sizeof(digest), |
| 169 | &digest_len); |
| 170 | EXPECT_TRUE(rv); |
| 171 | EXPECT_EQ(expected_len, digest_len); |
| 172 | EXPECT_EQ(0, memcmp(digest, expected_digest, expected_len)); |
| 173 | } |
| 174 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 175 | private: |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame^] | 176 | rtc::scoped_ptr<SSLIdentity> identity_rsa1_; |
| 177 | rtc::scoped_ptr<SSLIdentity> identity_rsa2_; |
| 178 | rtc::scoped_ptr<SSLIdentity> identity_ecdsa1_; |
| 179 | rtc::scoped_ptr<SSLIdentity> identity_ecdsa2_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 180 | rtc::scoped_ptr<rtc::SSLCertificate> test_cert_; |
| 181 | }; |
| 182 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame^] | 183 | TEST_F(SSLIdentityTest, FixedDigestSHA1) { |
| 184 | TestDigestForFixedCert(rtc::DIGEST_SHA_1, 20, kTestCertSha1); |
| 185 | } |
| 186 | |
| 187 | // HASH_AlgSHA224 is not supported in the chromium linux build. |
| 188 | #if SSL_USE_NSS |
| 189 | TEST_F(SSLIdentityTest, DISABLED_FixedDigestSHA224) { |
| 190 | #else |
| 191 | TEST_F(SSLIdentityTest, FixedDigestSHA224) { |
| 192 | #endif |
| 193 | TestDigestForFixedCert(rtc::DIGEST_SHA_224, 28, kTestCertSha224); |
| 194 | } |
| 195 | |
| 196 | TEST_F(SSLIdentityTest, FixedDigestSHA256) { |
| 197 | TestDigestForFixedCert(rtc::DIGEST_SHA_256, 32, kTestCertSha256); |
| 198 | } |
| 199 | |
| 200 | TEST_F(SSLIdentityTest, FixedDigestSHA384) { |
| 201 | TestDigestForFixedCert(rtc::DIGEST_SHA_384, 48, kTestCertSha384); |
| 202 | } |
| 203 | |
| 204 | TEST_F(SSLIdentityTest, FixedDigestSHA512) { |
| 205 | TestDigestForFixedCert(rtc::DIGEST_SHA_512, 64, kTestCertSha512); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | // HASH_AlgSHA224 is not supported in the chromium linux build. |
| 209 | #if SSL_USE_NSS |
| 210 | TEST_F(SSLIdentityTest, DISABLED_DigestSHA224) { |
| 211 | #else |
| 212 | TEST_F(SSLIdentityTest, DigestSHA224) { |
| 213 | #endif |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame^] | 214 | TestDigestForGeneratedCert(rtc::DIGEST_SHA_224, 28); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | TEST_F(SSLIdentityTest, DigestSHA256) { |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame^] | 218 | TestDigestForGeneratedCert(rtc::DIGEST_SHA_256, 32); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | TEST_F(SSLIdentityTest, DigestSHA384) { |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame^] | 222 | TestDigestForGeneratedCert(rtc::DIGEST_SHA_384, 48); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | TEST_F(SSLIdentityTest, DigestSHA512) { |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame^] | 226 | TestDigestForGeneratedCert(rtc::DIGEST_SHA_512, 64); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame^] | 229 | TEST_F(SSLIdentityTest, FromPEMStringsRSA) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 230 | static const char kRSA_PRIVATE_KEY_PEM[] = |
| 231 | "-----BEGIN RSA PRIVATE KEY-----\n" |
| 232 | "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMYRkbhmI7kVA/rM\n" |
| 233 | "czsZ+6JDhDvnkF+vn6yCAGuRPV03zuRqZtDy4N4to7PZu9PjqrRl7nDMXrG3YG9y\n" |
| 234 | "rlIAZ72KjcKKFAJxQyAKLCIdawKRyp8RdK3LEySWEZb0AV58IadqPZDTNHHRX8dz\n" |
| 235 | "5aTSMsbbkZ+C/OzTnbiMqLL/vg6jAgMBAAECgYAvgOs4FJcgvp+TuREx7YtiYVsH\n" |
| 236 | "mwQPTum2z/8VzWGwR8BBHBvIpVe1MbD/Y4seyI2aco/7UaisatSgJhsU46/9Y4fq\n" |
| 237 | "2TwXH9QANf4at4d9n/R6rzwpAJOpgwZgKvdQjkfrKTtgLV+/dawvpxUYkRH4JZM1\n" |
| 238 | "CVGukMfKNrSVH4Ap4QJBAOJmGV1ASPnB4r4nc99at7JuIJmd7fmuVUwUgYi4XgaR\n" |
| 239 | "WhScBsgYwZ/JoywdyZJgnbcrTDuVcWG56B3vXbhdpMsCQQDf9zeJrjnPZ3Cqm79y\n" |
| 240 | "kdqANep0uwZciiNiWxsQrCHztywOvbFhdp8iYVFG9EK8DMY41Y5TxUwsHD+67zao\n" |
| 241 | "ZNqJAkEA1suLUP/GvL8IwuRneQd2tWDqqRQ/Td3qq03hP7e77XtF/buya3Ghclo5\n" |
| 242 | "54czUR89QyVfJEC6278nzA7n2h1uVQJAcG6mztNL6ja/dKZjYZye2CY44QjSlLo0\n" |
| 243 | "MTgTSjdfg/28fFn2Jjtqf9Pi/X+50LWI/RcYMC2no606wRk9kyOuIQJBAK6VSAim\n" |
| 244 | "1pOEjsYQn0X5KEIrz1G3bfCbB848Ime3U2/FWlCHMr6ch8kCZ5d1WUeJD3LbwMNG\n" |
| 245 | "UCXiYxSsu20QNVw=\n" |
| 246 | "-----END RSA PRIVATE KEY-----\n"; |
| 247 | |
| 248 | static const char kCERT_PEM[] = |
| 249 | "-----BEGIN CERTIFICATE-----\n" |
| 250 | "MIIBmTCCAQKgAwIBAgIEbzBSAjANBgkqhkiG9w0BAQsFADARMQ8wDQYDVQQDEwZX\n" |
| 251 | "ZWJSVEMwHhcNMTQwMTAyMTgyNDQ3WhcNMTQwMjAxMTgyNDQ3WjARMQ8wDQYDVQQD\n" |
| 252 | "EwZXZWJSVEMwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMYRkbhmI7kVA/rM\n" |
| 253 | "czsZ+6JDhDvnkF+vn6yCAGuRPV03zuRqZtDy4N4to7PZu9PjqrRl7nDMXrG3YG9y\n" |
| 254 | "rlIAZ72KjcKKFAJxQyAKLCIdawKRyp8RdK3LEySWEZb0AV58IadqPZDTNHHRX8dz\n" |
| 255 | "5aTSMsbbkZ+C/OzTnbiMqLL/vg6jAgMBAAEwDQYJKoZIhvcNAQELBQADgYEAUflI\n" |
| 256 | "VUe5Krqf5RVa5C3u/UTAOAUJBiDS3VANTCLBxjuMsvqOG0WvaYWP3HYPgrz0jXK2\n" |
| 257 | "LJE/mGw3MyFHEqi81jh95J+ypl6xKW6Rm8jKLR87gUvCaVYn/Z4/P3AqcQTB7wOv\n" |
| 258 | "UD0A8qfhfDM+LK6rPAnCsVN0NRDY3jvd6rzix9M=\n" |
| 259 | "-----END CERTIFICATE-----\n"; |
| 260 | |
| 261 | rtc::scoped_ptr<SSLIdentity> identity( |
| 262 | SSLIdentity::FromPEMStrings(kRSA_PRIVATE_KEY_PEM, kCERT_PEM)); |
| 263 | EXPECT_TRUE(identity); |
| 264 | EXPECT_EQ(kCERT_PEM, identity->certificate().ToPEMString()); |
| 265 | } |
| 266 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame^] | 267 | #if SSL_USE_OPENSSL |
| 268 | // This will not work on NSS as PK11_ImportDERPrivateKeyInfoAndReturnKey is not |
| 269 | // ready for EC keys. Furthermore, NSSIdentity::FromPEMStrings is currently |
| 270 | // hardwired for RSA (the header matching via kPemTypeRsaPrivateKey needs |
| 271 | // trivial generalization). |
| 272 | TEST_F(SSLIdentityTest, FromPEMStringsEC) { |
| 273 | static const char kRSA_PRIVATE_KEY_PEM[] = |
| 274 | "-----BEGIN EC PRIVATE KEY-----\n" |
| 275 | "MHcCAQEEIKkIztWLPbs4Y2zWv7VW2Ov4is2ifleCuPgRB8fRv3IkoAoGCCqGSM49\n" |
| 276 | "AwEHoUQDQgAEDPV33NrhSdhg9cBRkUWUXnVMXc3h17i9ARbSmNgminKcBXb8/y8L\n" |
| 277 | "A76cMWQPPM0ybHO8OS7ZVg2U/m+TwE1M2g==\n" |
| 278 | "-----END EC PRIVATE KEY-----\n"; |
| 279 | static const char kCERT_PEM[] = |
| 280 | "-----BEGIN CERTIFICATE-----\n" |
| 281 | "MIIB0jCCAXmgAwIBAgIJAMCjpFt9t6LMMAoGCCqGSM49BAMCMEUxCzAJBgNVBAYT\n" |
| 282 | "AkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBXaWRn\n" |
| 283 | "aXRzIFB0eSBMdGQwIBcNMTUwNjMwMTMwMTIyWhgPMjI4OTA0MTMxMzAxMjJaMEUx\n" |
| 284 | "CzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRl\n" |
| 285 | "cm5ldCBXaWRnaXRzIFB0eSBMdGQwWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAAQM\n" |
| 286 | "9Xfc2uFJ2GD1wFGRRZRedUxdzeHXuL0BFtKY2CaKcpwFdvz/LwsDvpwxZA88zTJs\n" |
| 287 | "c7w5LtlWDZT+b5PATUzao1AwTjAdBgNVHQ4EFgQUYHq6nxNNIE832ZmaHc/noODO\n" |
| 288 | "rtAwHwYDVR0jBBgwFoAUYHq6nxNNIE832ZmaHc/noODOrtAwDAYDVR0TBAUwAwEB\n" |
| 289 | "/zAKBggqhkjOPQQDAgNHADBEAiAQRojsTyZG0BlKoU7gOt5h+yAMLl2cxmDtOIQr\n" |
| 290 | "GWP/PwIgJynB4AUDsPT0DWmethOXYijB5sY5UPd9DvgmiS/Mr6s=\n" |
| 291 | "-----END CERTIFICATE-----\n"; |
| 292 | |
| 293 | rtc::scoped_ptr<SSLIdentity> identity( |
| 294 | SSLIdentity::FromPEMStrings(kRSA_PRIVATE_KEY_PEM, kCERT_PEM)); |
| 295 | EXPECT_TRUE(identity); |
| 296 | EXPECT_EQ(kCERT_PEM, identity->certificate().ToPEMString()); |
| 297 | } |
| 298 | #endif |
| 299 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 300 | TEST_F(SSLIdentityTest, PemDerConversion) { |
| 301 | std::string der; |
| 302 | EXPECT_TRUE(SSLIdentity::PemToDer("CERTIFICATE", kTestCertificate, &der)); |
| 303 | |
| 304 | EXPECT_EQ(kTestCertificate, SSLIdentity::DerToPem( |
| 305 | "CERTIFICATE", |
| 306 | reinterpret_cast<const unsigned char*>(der.data()), der.length())); |
| 307 | } |
| 308 | |
| 309 | TEST_F(SSLIdentityTest, GetSignatureDigestAlgorithm) { |
| 310 | TestGetSignatureDigestAlgorithm(); |
| 311 | } |