| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/cert/internal/verify_signed_data.h" | 5 #include "net/cert/internal/verify_signed_data.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/base_paths.h" | 9 #include "base/base_paths.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| 11 #include "base/path_service.h" | 11 #include "base/path_service.h" |
| 12 #include "net/cert/internal/signature_algorithm.h" | 12 #include "net/cert/internal/signature_algorithm.h" |
| 13 #include "net/cert/internal/signature_policy.h" |
| 13 #include "net/cert/pem_tokenizer.h" | 14 #include "net/cert/pem_tokenizer.h" |
| 14 #include "net/der/input.h" | 15 #include "net/der/input.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 17 |
| 18 #if defined(USE_OPENSSL) |
| 19 #include <openssl/obj_mac.h> |
| 20 #endif |
| 21 |
| 17 namespace net { | 22 namespace net { |
| 18 | 23 |
| 19 namespace { | 24 namespace { |
| 20 | 25 |
| 21 // Creates a der::Input from an std::string. The lifetimes are a bit subtle | 26 // Creates a der::Input from an std::string. The lifetimes are a bit subtle |
| 22 // when using this function: | 27 // when using this function: |
| 23 // | 28 // |
| 24 // The returned der::Input() is only valid so long as the input string is alive | 29 // The returned der::Input() is only valid so long as the input string is alive |
| 25 // and is not mutated. | 30 // and is not mutated. |
| 26 // | 31 // |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 return src_root.Append( | 100 return src_root.Append( |
| 96 FILE_PATH_LITERAL("net/data/verify_signed_data_unittest")) | 101 FILE_PATH_LITERAL("net/data/verify_signed_data_unittest")) |
| 97 .AppendASCII(file_name); | 102 .AppendASCII(file_name); |
| 98 } | 103 } |
| 99 | 104 |
| 100 enum VerifyResult { | 105 enum VerifyResult { |
| 101 SUCCESS, | 106 SUCCESS, |
| 102 FAILURE, | 107 FAILURE, |
| 103 }; | 108 }; |
| 104 | 109 |
| 105 // Reads test data from |file_name| and runs VerifySignedData() over its inputs. | 110 // Reads test data from |file_name| and runs VerifySignedData() over its |
| 111 // inputs, using |policy|. |
| 106 // | 112 // |
| 107 // If expected_result was SUCCESS then the test will only succeed if | 113 // If expected_result was SUCCESS then the test will only succeed if |
| 108 // VerifySignedData() returns true. | 114 // VerifySignedData() returns true. |
| 109 // | 115 // |
| 110 // If expected_result was FAILURE then the test will only succeed if | 116 // If expected_result was FAILURE then the test will only succeed if |
| 111 // VerifySignedData() returns false. | 117 // VerifySignedData() returns false. |
| 112 void RunTestCase(VerifyResult expected_result, const char* file_name) { | 118 void RunTestCaseUsingPolicy(VerifyResult expected_result, |
| 119 const char* file_name, |
| 120 const SignaturePolicy* policy) { |
| 113 #if !defined(USE_OPENSSL) | 121 #if !defined(USE_OPENSSL) |
| 114 LOG(INFO) << "Skipping test, only implemented for BoringSSL"; | 122 LOG(INFO) << "Skipping test, only implemented for BoringSSL"; |
| 115 return; | 123 return; |
| 116 #endif | 124 #endif |
| 117 | 125 |
| 118 base::FilePath test_file_path = GetTestFilePath(file_name); | 126 base::FilePath test_file_path = GetTestFilePath(file_name); |
| 119 | 127 |
| 120 std::string file_data; | 128 std::string file_data; |
| 121 ASSERT_TRUE(base::ReadFileToString(test_file_path, &file_data)) | 129 ASSERT_TRUE(base::ReadFileToString(test_file_path, &file_data)) |
| 122 << "Couldn't read file: " << test_file_path.value(); | 130 << "Couldn't read file: " << test_file_path.value(); |
| 123 | 131 |
| 124 std::string public_key; | 132 std::string public_key; |
| 125 std::string algorithm; | 133 std::string algorithm; |
| 126 std::string signed_data; | 134 std::string signed_data; |
| 127 std::string signature_value; | 135 std::string signature_value; |
| 128 | 136 |
| 129 ASSERT_TRUE(ParseTestDataFile(file_data, &public_key, &algorithm, | 137 ASSERT_TRUE(ParseTestDataFile(file_data, &public_key, &algorithm, |
| 130 &signed_data, &signature_value)); | 138 &signed_data, &signature_value)); |
| 131 | 139 |
| 132 scoped_ptr<SignatureAlgorithm> signature_algorithm = | 140 scoped_ptr<SignatureAlgorithm> signature_algorithm = |
| 133 SignatureAlgorithm::CreateFromDer(InputFromString(&algorithm)); | 141 SignatureAlgorithm::CreateFromDer(InputFromString(&algorithm)); |
| 134 ASSERT_TRUE(signature_algorithm); | 142 ASSERT_TRUE(signature_algorithm); |
| 135 | 143 |
| 136 bool expected_result_bool = expected_result == SUCCESS; | 144 bool expected_result_bool = expected_result == SUCCESS; |
| 137 | 145 |
| 138 EXPECT_EQ( | 146 EXPECT_EQ( |
| 139 expected_result_bool, | 147 expected_result_bool, |
| 140 VerifySignedData(*signature_algorithm, InputFromString(&signed_data), | 148 VerifySignedData(*signature_algorithm, InputFromString(&signed_data), |
| 141 InputFromString(&signature_value), | 149 InputFromString(&signature_value), |
| 142 InputFromString(&public_key))); | 150 InputFromString(&public_key), policy)); |
| 151 } |
| 152 |
| 153 // RunTestCase() is the same as RunTestCaseUsingPolicy(), only it uses a |
| 154 // default policy. This policy will accept a basic profile of signature |
| 155 // algorithms (including ANY sized RSA key >= 1024). |
| 156 void RunTestCase(VerifyResult expected_result, const char* file_name) { |
| 157 SimpleSignaturePolicy policy(1024); |
| 158 return RunTestCaseUsingPolicy(expected_result, file_name, &policy); |
| 143 } | 159 } |
| 144 | 160 |
| 145 // Read the descriptions in the test files themselves for details on what is | 161 // Read the descriptions in the test files themselves for details on what is |
| 146 // being tested. | 162 // being tested. |
| 147 | 163 |
| 148 TEST(VerifySignedDataTest, RsaPkcs1Sha1) { | 164 TEST(VerifySignedDataTest, RsaPkcs1Sha1) { |
| 149 RunTestCase(SUCCESS, "rsa-pkcs1-sha1.pem"); | 165 RunTestCase(SUCCESS, "rsa-pkcs1-sha1.pem"); |
| 150 } | 166 } |
| 151 | 167 |
| 152 TEST(VerifySignedDataTest, RsaPkcs1Sha256) { | 168 TEST(VerifySignedDataTest, RsaPkcs1Sha256) { |
| 153 RunTestCase(SUCCESS, "rsa-pkcs1-sha256.pem"); | 169 RunTestCase(SUCCESS, "rsa-pkcs1-sha256.pem"); |
| 154 } | 170 } |
| 155 | 171 |
| 172 TEST(VerifySignedDataTest, Rsa2048Pkcs1Sha512) { |
| 173 RunTestCase(SUCCESS, "rsa2048-pkcs1-sha512.pem"); |
| 174 } |
| 175 |
| 156 TEST(VerifySignedDataTest, RsaPkcs1Sha256KeyEncodedBer) { | 176 TEST(VerifySignedDataTest, RsaPkcs1Sha256KeyEncodedBer) { |
| 157 // TODO(eroman): This should fail! (SPKI should be DER-encoded). | 177 // TODO(eroman): This should fail! (SPKI should be DER-encoded). |
| 158 RunTestCase(SUCCESS, "rsa-pkcs1-sha256-key-encoded-ber.pem"); | 178 RunTestCase(SUCCESS, "rsa-pkcs1-sha256-key-encoded-ber.pem"); |
| 159 } | 179 } |
| 160 | 180 |
| 161 TEST(VerifySignedDataTest, EcdsaSecp384r1Sha256) { | 181 TEST(VerifySignedDataTest, EcdsaSecp384r1Sha256) { |
| 162 RunTestCase(SUCCESS, "ecdsa-secp384r1-sha256.pem"); | 182 RunTestCase(SUCCESS, "ecdsa-secp384r1-sha256.pem"); |
| 163 } | 183 } |
| 164 | 184 |
| 165 TEST(VerifySignedDataTest, EcdsaPrime256v1Sha512) { | 185 TEST(VerifySignedDataTest, EcdsaPrime256v1Sha512) { |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 TEST(VerifySignedDataTest, RsaPkcs1Sha256SpkiNonNullParams) { | 289 TEST(VerifySignedDataTest, RsaPkcs1Sha256SpkiNonNullParams) { |
| 270 // TODO(eroman): This should fail! (shouldn't recognize bogus params in rsa | 290 // TODO(eroman): This should fail! (shouldn't recognize bogus params in rsa |
| 271 // SPKI). | 291 // SPKI). |
| 272 RunTestCase(SUCCESS, "rsa-pkcs1-sha256-spki-non-null-params.pem"); | 292 RunTestCase(SUCCESS, "rsa-pkcs1-sha256-spki-non-null-params.pem"); |
| 273 } | 293 } |
| 274 | 294 |
| 275 TEST(VerifySignedDataTest, EcdsaPrime256v1Sha512SignatureNotBitString) { | 295 TEST(VerifySignedDataTest, EcdsaPrime256v1Sha512SignatureNotBitString) { |
| 276 RunTestCase(FAILURE, "ecdsa-prime256v1-sha512-signature-not-bitstring.pem"); | 296 RunTestCase(FAILURE, "ecdsa-prime256v1-sha512-signature-not-bitstring.pem"); |
| 277 } | 297 } |
| 278 | 298 |
| 299 // This policy rejects specifically secp384r1 curves. |
| 300 class RejectSecp384r1Policy : public SignaturePolicy { |
| 301 public: |
| 302 bool IsAcceptableCurveForEcdsa(int curve_nid) const override { |
| 303 #if defined(USE_OPENSSL) |
| 304 if (curve_nid == NID_secp384r1) |
| 305 return false; |
| 306 #endif |
| 307 return true; |
| 308 } |
| 309 }; |
| 310 |
| 311 TEST(VerifySignedDataTest, PolicyIsAcceptableCurveForEcdsa) { |
| 312 RejectSecp384r1Policy policy; |
| 313 |
| 314 // Using the regular policy both secp384r1 and secp256r1 should be accepted. |
| 315 RunTestCase(SUCCESS, "ecdsa-secp384r1-sha256.pem"); |
| 316 RunTestCase(SUCCESS, "ecdsa-prime256v1-sha512.pem"); |
| 317 |
| 318 // However when using a policy that specifically rejects secp384r1, only |
| 319 // prime256v1 should be accepted. |
| 320 RunTestCaseUsingPolicy(FAILURE, "ecdsa-secp384r1-sha256.pem", &policy); |
| 321 RunTestCaseUsingPolicy(SUCCESS, "ecdsa-prime256v1-sha512.pem", &policy); |
| 322 } |
| 323 |
| 324 TEST(VerifySignedDataTest, PolicyIsAcceptableModulusLengthForRsa) { |
| 325 SimpleSignaturePolicy policy_1024(1024); |
| 326 SimpleSignaturePolicy policy_2048(2048); |
| 327 |
| 328 // Using the regular policy both 1024-bit and 2048-bit RSA keys should be |
| 329 // accepted. |
| 330 RunTestCaseUsingPolicy(SUCCESS, "rsa-pkcs1-sha256.pem", &policy_1024); |
| 331 RunTestCaseUsingPolicy(SUCCESS, "rsa2048-pkcs1-sha512.pem", &policy_1024); |
| 332 |
| 333 // However when using a policy that rejects any keys less than 2048-bits, only |
| 334 // one of the tests will pass. |
| 335 RunTestCaseUsingPolicy(FAILURE, "rsa-pkcs1-sha256.pem", &policy_2048); |
| 336 RunTestCaseUsingPolicy(SUCCESS, "rsa2048-pkcs1-sha512.pem", &policy_2048); |
| 337 } |
| 338 |
| 339 // This policy rejects the use of SHA-512. |
| 340 class RejectSha512 : public SignaturePolicy { |
| 341 public: |
| 342 RejectSha512() : SignaturePolicy() {} |
| 343 |
| 344 bool IsAcceptableSignatureAlgorithm( |
| 345 const SignatureAlgorithm& algorithm) const override { |
| 346 if (algorithm.ParamsForRsaPss() && |
| 347 algorithm.ParamsForRsaPss()->mgf1_hash() == DigestAlgorithm::Sha512) { |
| 348 return false; |
| 349 } |
| 350 |
| 351 return algorithm.digest() != DigestAlgorithm::Sha512; |
| 352 } |
| 353 |
| 354 bool IsAcceptableModulusLengthForRsa( |
| 355 size_t modulus_length_bits) const override { |
| 356 return true; |
| 357 } |
| 358 }; |
| 359 |
| 360 TEST(VerifySignedDataTest, PolicyIsAcceptableDigestAlgorithm) { |
| 361 RejectSha512 policy; |
| 362 |
| 363 // Using the regular policy use of either SHA256 or SHA512 should work |
| 364 // (whether as the main digest, or the MGF1 for RSASSA-PSS) |
| 365 RunTestCase(SUCCESS, "rsa2048-pkcs1-sha512.pem"); |
| 366 RunTestCase(SUCCESS, "ecdsa-prime256v1-sha512.pem"); |
| 367 RunTestCase(SUCCESS, "ecdsa-secp384r1-sha256.pem"); |
| 368 RunTestCase(SUCCESS, "rsa-pkcs1-sha256.pem"); |
| 369 RunTestCase(SUCCESS, "rsa-pss-sha256-salt10.pem"); |
| 370 // This one uses both SHA256 and SHA512 |
| 371 RunTestCase(SUCCESS, "rsa-pss-sha256-mgf1-sha512-salt33.pem"); |
| 372 |
| 373 // However when using a policy that rejects SHA512, only the tests using |
| 374 // exclusively SHA256 should pass). |
| 375 RunTestCaseUsingPolicy(FAILURE, "rsa2048-pkcs1-sha512.pem", &policy); |
| 376 RunTestCaseUsingPolicy(FAILURE, "ecdsa-prime256v1-sha512.pem", &policy); |
| 377 RunTestCaseUsingPolicy(SUCCESS, "ecdsa-secp384r1-sha256.pem", &policy); |
| 378 RunTestCaseUsingPolicy(SUCCESS, "rsa-pkcs1-sha256.pem", &policy); |
| 379 RunTestCaseUsingPolicy(SUCCESS, "rsa-pss-sha256-salt10.pem", &policy); |
| 380 RunTestCaseUsingPolicy(FAILURE, "rsa-pss-sha256-mgf1-sha512-salt33.pem", |
| 381 &policy); |
| 382 } |
| 383 |
| 279 } // namespace | 384 } // namespace |
| 280 | 385 |
| 281 } // namespace net | 386 } // namespace net |
| OLD | NEW |