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
3 // found in the LICENSE file.
5 #include "net/cert/internal/verify_signed_data.h"
9 #include "base/base_paths.h"
10 #include "base/files/file_util.h"
11 #include "base/path_service.h"
12 #include "net/cert/internal/signature_algorithm.h"
13 #include "net/cert/pem_tokenizer.h"
14 #include "net/der/input.h"
15 #include "net/der/parse_values.h"
16 #include "net/der/parser.h"
17 #include "testing/gtest/include/gtest/gtest.h"
23 // Creates a der::Input from an std::string. The lifetimes are a bit subtle
24 // when using this function:
26 // The returned der::Input() is only valid so long as the input string is alive
27 // and is not mutated.
29 // Note that the input parameter has been made a pointer to prevent callers
30 // from accidentally passing an r-value.
31 der::Input
InputFromString(const std::string
* s
) {
32 return der::Input(reinterpret_cast<const uint8_t*>(s
->data()), s
->size());
35 // Reads a signature verification test file.
37 // The test file is a series of PEM blocks (PEM is just base64 data) with
40 // "PUBLIC KEY" - DER encoding of the SubjectPublicKeyInfo
41 // "ALGORITHM" - DER encoding of the AlgorithmIdentifier for the signature
42 // algorithm (signatureAlgorithm in X.509)
43 // "DATA" - The data that was signed (tbsCertificate in X.509)
44 // "SIGNATURE" - The result of signing DATA.
45 ::testing::AssertionResult
ParseTestDataFile(const std::string
& file_data
,
46 std::string
* public_key
,
47 std::string
* algorithm
,
48 std::string
* signed_data
,
49 std::string
* signature_value
) {
50 const char kPublicKeyBlock
[] = "PUBLIC KEY";
51 const char kAlgorithmBlock
[] = "ALGORITHM";
52 const char kSignedDataBlock
[] = "DATA";
53 const char kSignatureBlock
[] = "SIGNATURE";
55 std::vector
<std::string
> pem_headers
;
56 pem_headers
.push_back(kPublicKeyBlock
);
57 pem_headers
.push_back(kAlgorithmBlock
);
58 pem_headers
.push_back(kSignedDataBlock
);
59 pem_headers
.push_back(kSignatureBlock
);
61 // Keep track of which blocks have been encountered (by elimination).
62 std::set
<std::string
> remaining_blocks(pem_headers
.begin(),
65 PEMTokenizer
pem_tokenizer(file_data
, pem_headers
);
66 while (pem_tokenizer
.GetNext()) {
67 const std::string
& block_type
= pem_tokenizer
.block_type();
68 if (block_type
== kPublicKeyBlock
) {
69 public_key
->assign(pem_tokenizer
.data());
70 } else if (block_type
== kAlgorithmBlock
) {
71 algorithm
->assign(pem_tokenizer
.data());
72 } else if (block_type
== kSignedDataBlock
) {
73 signed_data
->assign(pem_tokenizer
.data());
74 } else if (block_type
== kSignatureBlock
) {
75 signature_value
->assign(pem_tokenizer
.data());
78 if (remaining_blocks
.erase(block_type
) != 1u) {
79 return ::testing::AssertionFailure()
80 << "PEM block defined multiple times: " << block_type
;
84 if (!remaining_blocks
.empty()) {
85 // Print one of the missing PEM blocks.
86 return ::testing::AssertionFailure() << "PEM block missing: "
87 << *remaining_blocks
.begin();
90 return ::testing::AssertionSuccess();
93 // Returns a path to the file |file_name| within the unittest data directory.
94 base::FilePath
GetTestFilePath(const char* file_name
) {
95 base::FilePath src_root
;
96 PathService::Get(base::DIR_SOURCE_ROOT
, &src_root
);
97 return src_root
.Append(
98 FILE_PATH_LITERAL("net/data/verify_signed_data_unittest"))
99 .AppendASCII(file_name
);
107 // Reads test data from |file_name| and runs VerifySignedData() over its inputs.
109 // If expected_result was SUCCESS then the test will only succeed if
110 // VerifySignedData() returns true.
112 // If expected_result was FAILURE then the test will only succeed if
113 // VerifySignedData() returns false.
114 void RunTestCase(VerifyResult expected_result
, const char* file_name
) {
115 #if !defined(USE_OPENSSL)
116 LOG(INFO
) << "Skipping test, only implemented for BoringSSL";
120 base::FilePath test_file_path
= GetTestFilePath(file_name
);
122 std::string file_data
;
123 ASSERT_TRUE(base::ReadFileToString(test_file_path
, &file_data
))
124 << "Couldn't read file: " << test_file_path
.value();
126 std::string public_key
;
127 std::string algorithm
;
128 std::string signed_data
;
129 std::string signature_value
;
131 ASSERT_TRUE(ParseTestDataFile(file_data
, &public_key
, &algorithm
,
132 &signed_data
, &signature_value
));
134 scoped_ptr
<SignatureAlgorithm
> signature_algorithm
=
135 SignatureAlgorithm::CreateFromDer(InputFromString(&algorithm
));
136 ASSERT_TRUE(signature_algorithm
);
138 der::BitString signature_value_bit_string
;
139 der::Parser
signature_value_parser(InputFromString(&signature_value
));
140 ASSERT_TRUE(signature_value_parser
.ReadBitString(&signature_value_bit_string
))
141 << "The signature value is not a valid BIT STRING";
143 bool expected_result_bool
= expected_result
== SUCCESS
;
145 EXPECT_EQ(expected_result_bool
,
147 *signature_algorithm
, InputFromString(&signed_data
),
148 signature_value_bit_string
, InputFromString(&public_key
)));
151 // Read the descriptions in the test files themselves for details on what is
154 TEST(VerifySignedDataTest
, RsaPkcs1Sha1
) {
155 RunTestCase(SUCCESS
, "rsa-pkcs1-sha1.pem");
158 TEST(VerifySignedDataTest
, RsaPkcs1Sha256
) {
159 RunTestCase(SUCCESS
, "rsa-pkcs1-sha256.pem");
162 TEST(VerifySignedDataTest
, RsaPkcs1Sha256KeyEncodedBer
) {
163 // TODO(eroman): This should fail! (SPKI should be DER-encoded).
164 RunTestCase(SUCCESS
, "rsa-pkcs1-sha256-key-encoded-ber.pem");
167 TEST(VerifySignedDataTest
, EcdsaSecp384r1Sha256
) {
168 RunTestCase(SUCCESS
, "ecdsa-secp384r1-sha256.pem");
171 TEST(VerifySignedDataTest
, EcdsaPrime256v1Sha512
) {
172 RunTestCase(SUCCESS
, "ecdsa-prime256v1-sha512.pem");
175 TEST(VerifySignedDataTest
, RsaPssSha1
) {
176 RunTestCase(SUCCESS
, "rsa-pss-sha1-salt20.pem");
179 TEST(VerifySignedDataTest
, RsaPssSha256Mgf1Sha512Salt33
) {
180 RunTestCase(SUCCESS
, "rsa-pss-sha256-mgf1-sha512-salt33.pem");
183 TEST(VerifySignedDataTest
, RsaPssSha256
) {
184 RunTestCase(SUCCESS
, "rsa-pss-sha256-salt10.pem");
187 TEST(VerifySignedDataTest
, RsaPssSha1WrongSalt
) {
188 RunTestCase(FAILURE
, "rsa-pss-sha1-wrong-salt.pem");
191 TEST(VerifySignedDataTest
, EcdsaSecp384r1Sha256CorruptedData
) {
192 RunTestCase(FAILURE
, "ecdsa-secp384r1-sha256-corrupted-data.pem");
195 TEST(VerifySignedDataTest
, RsaPkcs1Sha1WrongAlgorithm
) {
196 RunTestCase(FAILURE
, "rsa-pkcs1-sha1-wrong-algorithm.pem");
199 TEST(VerifySignedDataTest
, EcdsaPrime256v1Sha512WrongSignatureFormat
) {
200 RunTestCase(FAILURE
, "ecdsa-prime256v1-sha512-wrong-signature-format.pem");
203 TEST(VerifySignedDataTest
, EcdsaUsingRsaKey
) {
204 RunTestCase(FAILURE
, "ecdsa-using-rsa-key.pem");
207 TEST(VerifySignedDataTest
, RsaUsingEcKey
) {
208 RunTestCase(FAILURE
, "rsa-using-ec-key.pem");
211 TEST(VerifySignedDataTest
, RsaPkcs1Sha1BadKeyDerNull
) {
212 RunTestCase(FAILURE
, "rsa-pkcs1-sha1-bad-key-der-null.pem");
215 TEST(VerifySignedDataTest
, RsaPkcs1Sha1BadKeyDerLength
) {
216 RunTestCase(FAILURE
, "rsa-pkcs1-sha1-bad-key-der-length.pem");
219 TEST(VerifySignedDataTest
, RsaPkcs1Sha256UsingEcdsaAlgorithm
) {
220 RunTestCase(FAILURE
, "rsa-pkcs1-sha256-using-ecdsa-algorithm.pem");
223 TEST(VerifySignedDataTest
, EcdsaPrime256v1Sha512UsingRsaAlgorithm
) {
224 RunTestCase(FAILURE
, "ecdsa-prime256v1-sha512-using-rsa-algorithm.pem");
227 TEST(VerifySignedDataTest
, EcdsaPrime256v1Sha512UsingEcdhKey
) {
228 RunTestCase(FAILURE
, "ecdsa-prime256v1-sha512-using-ecdh-key.pem");
231 TEST(VerifySignedDataTest
, EcdsaPrime256v1Sha512UsingEcmqvKey
) {
232 RunTestCase(FAILURE
, "ecdsa-prime256v1-sha512-using-ecmqv-key.pem");
235 TEST(VerifySignedDataTest
, RsaPkcs1Sha1KeyParamsAbsent
) {
236 // TODO(eroman): This should fail! (key algoritm parsing is too permissive)
237 RunTestCase(SUCCESS
, "rsa-pkcs1-sha1-key-params-absent.pem");
240 TEST(VerifySignedDataTest
, RsaPssSha1Salt20UsingPssKeyNoParams
) {
241 // TODO(eroman): This should pass! (rsaPss not currently supported in key
243 RunTestCase(FAILURE
, "rsa-pss-sha1-salt20-using-pss-key-no-params.pem");
246 TEST(VerifySignedDataTest
, RsaPkcs1Sha1UsingPssKeyNoParams
) {
247 RunTestCase(FAILURE
, "rsa-pkcs1-sha1-using-pss-key-no-params.pem");
250 TEST(VerifySignedDataTest
, RsaPssSha256Salt10UsingPssKeyWithParams
) {
251 // TODO(eroman): This should pass! (rsaPss not currently supported in key
253 RunTestCase(FAILURE
, "rsa-pss-sha256-salt10-using-pss-key-with-params.pem");
256 TEST(VerifySignedDataTest
, RsaPssSha256Salt10UsingPssKeyWithWrongParams
) {
258 "rsa-pss-sha256-salt10-using-pss-key-with-wrong-params.pem");
261 TEST(VerifySignedDataTest
, RsaPssSha256Salt12UsingPssKeyWithNullParams
) {
263 "rsa-pss-sha1-salt20-using-pss-key-with-null-params.pem");
266 TEST(VerifySignedDataTest
, EcdsaPrime256v1Sha512SpkiParamsNull
) {
267 RunTestCase(FAILURE
, "ecdsa-prime256v1-sha512-spki-params-null.pem");
270 TEST(VerifySignedDataTest
, RsaPkcs1Sha256UsingIdEaRsa
) {
271 // TODO(eroman): This should fail! (shouldn't recognize this weird OID).
272 RunTestCase(SUCCESS
, "rsa-pkcs1-sha256-using-id-ea-rsa.pem");
275 TEST(VerifySignedDataTest
, RsaPkcs1Sha256SpkiNonNullParams
) {
276 // TODO(eroman): This should fail! (shouldn't recognize bogus params in rsa
278 RunTestCase(SUCCESS
, "rsa-pkcs1-sha256-spki-non-null-params.pem");
281 TEST(VerifySignedDataTest
, EcdsaPrime256v1Sha512UnusedBitsSignature
) {
282 RunTestCase(FAILURE
, "ecdsa-prime256v1-sha512-unused-bits-signature.pem");