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

Side by Side Diff: net/cert/internal/signature_algorithm_unittest.cc

Issue 1218753002: Add DER parsing of AlgorithmId for signatures. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 5 years, 5 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
OLDNEW
(Empty)
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.
4
5 #include "net/cert/internal/signature_algorithm.h"
6
7 #include "base/files/file_util.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "net/base/test_data_directory.h"
10 #include "net/cert/pem_tokenizer.h"
11 #include "net/der/input.h"
12 #include "net/der/parser.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace net {
16
17 namespace {
18
19 // Parses a SignatureAlgorithm given an empty DER input.
20 TEST(SignatureAlgorithmTest, ParseDer_Empty) {
21 SignatureAlgorithm algorithm;
22 ASSERT_FALSE(algorithm.ParseDer(der::Input()));
23 }
24
25 // Parses a SignatureAlgorithm given invalid DER input.
26 TEST(SignatureAlgorithmTest, ParseDer_Bogus) {
27 const uint8_t kData[] = {0x00};
28 SignatureAlgorithm algorithm;
29 ASSERT_FALSE(algorithm.ParseDer(der::Input(kData)));
30 }
31
32 // Parses a sha-1WithRSAEncryption which contains no parameters field.
33 //
34 // SEQUENCE (1 elem)
35 // OBJECT IDENTIFIER 1.2.840.113549.1.1.5
36 TEST(SignatureAlgorithmTest, ParseDer_sha1WithRSAEncryption_NoParams) {
37 // clang-format off
38 const uint8_t kData[] = {
39 0x30, 0x0B, // SEQUENCE (11 bytes)
40 0x06, 0x09, // OBJECT IDENTIFIER (9 bytes)
41 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x05,
42 };
43 // clang-format on
44 SignatureAlgorithm algorithm;
45 ASSERT_TRUE(algorithm.ParseDer(der::Input(kData)));
46
47 EXPECT_EQ(SignatureAlgorithmId::RsaPkcs1, algorithm.algorithm());
48 EXPECT_EQ(DigestAlgorithm::Sha1, algorithm.digest());
49 }
50
51 // Parses a sha-1WithRSAEncryption which contains a NULL parameters field.
52 //
53 // SEQUENCE (2 elem)
54 // OBJECT IDENTIFIER 1.2.840.113549.1.1.5
55 // NULL
56 TEST(SignatureAlgorithmTest, ParseDer_sha1WithRSAEncryption_NullParams) {
57 // clang-format off
58 const uint8_t kData[] = {
59 0x30, 0x0D, // SEQUENCE (13 bytes)
60 0x06, 0x09, // OBJECT IDENTIFIER (9 bytes)
61 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x05,
62 0x05, 0x00, // NULL (0 bytes)
63 };
64 // clang-format on
65 SignatureAlgorithm algorithm;
66 ASSERT_TRUE(algorithm.ParseDer(der::Input(kData)));
67
68 EXPECT_EQ(SignatureAlgorithmId::RsaPkcs1, algorithm.algorithm());
69 EXPECT_EQ(DigestAlgorithm::Sha1, algorithm.digest());
70 }
71
72 // Parses a sha-1WithRSAEncryption which contains an unexpected parameters
73 // field. Instead of being NULL or omitted, it is an integer.
74 //
75 // SEQUENCE (2 elem)
76 // OBJECT IDENTIFIER 1.2.840.113549.1.1.5
77 // INTEGER 0
78 TEST(SignatureAlgorithmTest, ParseDer_sha1WithRSAEncryption_NonNullParams) {
79 // clang-format off
80 const uint8_t kData[] = {
81 0x30, 0x0E, // SEQUENCE (14 bytes)
82 0x06, 0x09, // OBJECT IDENTIFIER (9 bytes)
83 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x05,
84 0x02, 0x01, 0x00, // INTEGER (1 byte)
85 };
86 // clang-format on
87 SignatureAlgorithm algorithm;
88 ASSERT_FALSE(algorithm.ParseDer(der::Input(kData)));
89 }
90
91 // Parses a sha-1WithRSAEncryption which contains values after the sequence.
92 //
93 // SEQUENCE (1 elem)
94 // OBJECT IDENTIFIER 1.2.840.113549.1.1.5
95 // INTEGER 0
96 TEST(SignatureAlgorithmTest, ParseDer_DataAfterSequence) {
97 // clang-format off
98 const uint8_t kData[] = {
99 0x30, 0x0B, // SEQUENCE (11 bytes)
100 0x06, 0x09, // OBJECT IDENTIFIER (9 bytes)
101 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x05,
102 0x02, 0x01, 0x00, // INTEGER (1 byte)
103 };
104 // clang-format on
105 SignatureAlgorithm algorithm;
106 ASSERT_FALSE(algorithm.ParseDer(der::Input(kData)));
107 }
108
109 // Parses a sha-1WithRSAEncryption which contains a bad NULL parameters field.
110 // Normally NULL is encoded as {0x05, 0x00} (tag for NULL and length of 0). Here
111 // NULL is encoded as having a length of 1 instead, followed by data 0x09.
112 //
113 // SEQUENCE (2 elem)
114 // OBJECT IDENTIFIER 1.2.840.113549.1.1.5
115 // NULL
116 TEST(SignatureAlgorithmTest, ParseDer_sha1WithRSAEncryption_BadNullParams) {
117 // clang-format off
118 const uint8_t kData[] = {
119 0x30, 0x0E, // SEQUENCE (13 bytes)
120 0x06, 0x09, // OBJECT IDENTIFIER (9 bytes)
121 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x05,
122 0x05, 0x01, 0x09, // NULL (1 byte)
123 };
124 // clang-format on
125 SignatureAlgorithm algorithm;
126 ASSERT_FALSE(algorithm.ParseDer(der::Input(kData)));
127 }
128
129 // Parses a sha-1WithRSAEncryption which contains a NULL parameters field,
130 // followed by an integer.
131 //
132 // SEQUENCE (3 elem)
133 // OBJECT IDENTIFIER 1.2.840.113549.1.1.5
134 // NULL
135 // INTEGER 0
136 TEST(SignatureAlgorithmTest,
137 ParseDer_sha1WithRSAEncryption_NullParamsThenInteger) {
138 // clang-format off
139 const uint8_t kData[] = {
140 0x30, 0x10, // SEQUENCE (16 bytes)
141 0x06, 0x09, // OBJECT IDENTIFIER (9 bytes)
142 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x05,
143 0x05, 0x00, // NULL (0 bytes)
144 0x02, 0x01, 0x00, // INTEGER (1 byte)
145 };
146 // clang-format on
147 SignatureAlgorithm algorithm;
148 ASSERT_FALSE(algorithm.ParseDer(der::Input(kData)));
149 }
150
151 // Try parsing a SignatureAlgorithm given DER which does not encode a sequence.
152 //
153 // INTEGER 0
154 TEST(SignatureAlgorithmTest, ParseDer_NotASequence) {
155 // clang-format off
156 const uint8_t kData[] = {
157 0x02, 0x01, 0x00, // INTEGER (1 byte)
158 };
159 // clang-format on
160 SignatureAlgorithm algorithm;
161 ASSERT_FALSE(algorithm.ParseDer(der::Input(kData)));
162 }
163
164 // Parses a sha256WithRSAEncryption which contains no parameters field.
165 //
166 // SEQUENCE (1 elem)
167 // OBJECT IDENTIFIER 1.2.840.113549.1.1.11
168 TEST(SignatureAlgorithmTest, ParseDer_sha256WithRSAEncryption_NoParams) {
169 // clang-format off
170 const uint8_t kData[] = {
171 0x30, 0x0B, // SEQUENCE (11 bytes)
172 0x06, 0x09, // OBJECT IDENTIFIER (9 bytes)
173 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
174 };
175 // clang-format on
176 SignatureAlgorithm algorithm;
177 ASSERT_TRUE(algorithm.ParseDer(der::Input(kData)));
178
179 EXPECT_EQ(SignatureAlgorithmId::RsaPkcs1, algorithm.algorithm());
180 EXPECT_EQ(DigestAlgorithm::Sha256, algorithm.digest());
181 }
182
183 // Parses a sha384WithRSAEncryption which contains no parameters field.
184 //
185 // SEQUENCE (1 elem)
186 // OBJECT IDENTIFIER 1.2.840.113549.1.1.12
187 TEST(SignatureAlgorithmTest, ParseDer_sha384WithRSAEncryption_NoParams) {
188 // clang-format off
189 const uint8_t kData[] = {
190 0x30, 0x0B, // SEQUENCE (11 bytes)
191 0x06, 0x09, // OBJECT IDENTIFIER (9 bytes)
192 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0c,
193 };
194 // clang-format on
195 SignatureAlgorithm algorithm;
196 ASSERT_TRUE(algorithm.ParseDer(der::Input(kData)));
197
198 EXPECT_EQ(SignatureAlgorithmId::RsaPkcs1, algorithm.algorithm());
199 EXPECT_EQ(DigestAlgorithm::Sha384, algorithm.digest());
200 }
201
202 // Parses a sha512WithRSAEncryption which contains no parameters field.
203 //
204 // SEQUENCE (1 elem)
205 // OBJECT IDENTIFIER 1.2.840.113549.1.1.13
206 TEST(SignatureAlgorithmTest, ParseDer_sha512WithRSAEncryption_NoParams) {
207 // clang-format off
208 const uint8_t kData[] = {
209 0x30, 0x0B, // SEQUENCE (11 bytes)
210 0x06, 0x09, // OBJECT IDENTIFIER (9 bytes)
211 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0d,
212 };
213 // clang-format on
214 SignatureAlgorithm algorithm;
215 ASSERT_TRUE(algorithm.ParseDer(der::Input(kData)));
216
217 EXPECT_EQ(SignatureAlgorithmId::RsaPkcs1, algorithm.algorithm());
218 EXPECT_EQ(DigestAlgorithm::Sha512, algorithm.digest());
219 }
220
221 // Parses a sha224WithRSAEncryption which contains no parameters field. This
222 // fails because the parsing code does not enumerate this OID (even though it is
223 // in fact valid).
224 //
225 // SEQUENCE (1 elem)
226 // OBJECT IDENTIFIER 1.2.840.113549.1.1.14
227 TEST(SignatureAlgorithmTest, ParseDer_sha224WithRSAEncryption_NoParams) {
228 // clang-format off
229 const uint8_t kData[] = {
230 0x30, 0x0B, // SEQUENCE (11 bytes)
231 0x06, 0x09, // OBJECT IDENTIFIER (9 bytes)
232 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0e,
233 };
234 // clang-format on
235 SignatureAlgorithm algorithm;
236 ASSERT_FALSE(algorithm.ParseDer(der::Input(kData)));
237 }
238
239 // Parses a ecdsa-with-SHA1 which contains no parameters field.
240 //
241 // SEQUENCE (1 elem)
242 // OBJECT IDENTIFIER 1.2.840.10045.4.1
243 TEST(SignatureAlgorithmTest, ParseDer_ecdsaWithSHA1_NoParams) {
244 // clang-format off
245 const uint8_t kData[] = {
246 0x30, 0x09, // SEQUENCE (11 bytes)
247 0x06, 0x07, // OBJECT IDENTIFIER (7 bytes)
248 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x01,
249 };
250 // clang-format on
251 SignatureAlgorithm algorithm;
252 ASSERT_TRUE(algorithm.ParseDer(der::Input(kData)));
253
254 EXPECT_EQ(SignatureAlgorithmId::Ecdsa, algorithm.algorithm());
255 EXPECT_EQ(DigestAlgorithm::Sha1, algorithm.digest());
256 }
257
258 // Parses a ecdsa-with-SHA256 which contains no parameters field.
259 //
260 // SEQUENCE (1 elem)
261 // OBJECT IDENTIFIER 1.2.840.10045.4.3.2
262 TEST(SignatureAlgorithmTest, ParseDer_ecdsaWithSHA256_NoParams) {
263 // clang-format off
264 const uint8_t kData[] = {
265 0x30, 0x0A, // SEQUENCE (10 bytes)
266 0x06, 0x08, // OBJECT IDENTIFIER (8 bytes)
267 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02,
268 };
269 // clang-format on
270 SignatureAlgorithm algorithm;
271 ASSERT_TRUE(algorithm.ParseDer(der::Input(kData)));
272
273 EXPECT_EQ(SignatureAlgorithmId::Ecdsa, algorithm.algorithm());
274 EXPECT_EQ(DigestAlgorithm::Sha256, algorithm.digest());
275 }
276
277 // Parses a ecdsa-with-SHA384 which contains no parameters field.
278 //
279 // SEQUENCE (1 elem)
280 // OBJECT IDENTIFIER 1.2.840.10045.4.3.3
281
282 TEST(SignatureAlgorithmTest, ParseDer_ecdsaWithSHA384_NoParams) {
283 // clang-format off
284 const uint8_t kData[] = {
285 0x30, 0x0A, // SEQUENCE (10 bytes)
286 0x06, 0x08, // OBJECT IDENTIFIER (8 bytes)
287 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x03,
288 };
289 // clang-format on
290 SignatureAlgorithm algorithm;
291 ASSERT_TRUE(algorithm.ParseDer(der::Input(kData)));
292
293 EXPECT_EQ(SignatureAlgorithmId::Ecdsa, algorithm.algorithm());
294 EXPECT_EQ(DigestAlgorithm::Sha384, algorithm.digest());
295 }
296
297 // Parses a ecdsa-with-SHA512 which contains no parameters field.
298 //
299 // SEQUENCE (1 elem)
300 // OBJECT IDENTIFIER 1.2.840.10045.4.3.4
301 TEST(SignatureAlgorithmTest, ParseDer_ecdsaWithSHA512_NoParams) {
302 // clang-format off
303 const uint8_t kData[] = {
304 0x30, 0x0A, // SEQUENCE (10 bytes)
305 0x06, 0x08, // OBJECT IDENTIFIER (8 bytes)
306 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x04,
307 };
308 // clang-format on
309 SignatureAlgorithm algorithm;
310 ASSERT_TRUE(algorithm.ParseDer(der::Input(kData)));
311
312 EXPECT_EQ(SignatureAlgorithmId::Ecdsa, algorithm.algorithm());
313 EXPECT_EQ(DigestAlgorithm::Sha512, algorithm.digest());
314 }
315
316 // Tests that two RSA algorithms with different digests are not equal.
317 TEST(SignatureAlgorithmTest, Equals_RsaWithDifferentDigest) {
318 SignatureAlgorithm alg1;
319 alg1.AssignRsaPkcs1(DigestAlgorithm::Sha1);
320
321 SignatureAlgorithm alg2;
322 alg2.AssignRsaPkcs1(DigestAlgorithm::Sha256);
323
324 ASSERT_FALSE(alg1.Equals(alg2));
325 }
326
327 // Tests that two ECDSA algorithms with different digests are not equal.
328 TEST(SignatureAlgorithmTest, Equals_EcdsaWithDifferentDigest) {
329 SignatureAlgorithm alg1;
330 alg1.AssignEcdsa(DigestAlgorithm::Sha1);
331
332 SignatureAlgorithm alg2;
333 alg2.AssignEcdsa(DigestAlgorithm::Sha256);
334
335 ASSERT_FALSE(alg1.Equals(alg2));
336 }
337
338 // Tests that an ECDSA algorithm is not equal to an RSA algorithm (even though
339 // digests match).
340 TEST(SignatureAlgorithmTest, Equals_EcdsaNotEqualRsa) {
341 SignatureAlgorithm alg1;
342 alg1.AssignEcdsa(DigestAlgorithm::Sha256);
343
344 SignatureAlgorithm alg2;
345 alg2.AssignRsaPkcs1(DigestAlgorithm::Sha256);
346
347 ASSERT_FALSE(alg1.Equals(alg2));
348 }
349
350 // Tests that two identical ECDSA algorithms are equal - both use SHA-256.
351 TEST(SignatureAlgorithmTest, Equals_EcdsaMatch) {
352 SignatureAlgorithm alg1;
353 alg1.AssignEcdsa(DigestAlgorithm::Sha256);
354
355 SignatureAlgorithm alg2;
356 alg2.AssignEcdsa(DigestAlgorithm::Sha256);
357
358 ASSERT_TRUE(alg1.Equals(alg2));
359 }
360
361 // Tests that two identical RSA algorithms are equal - both use SHA-512
362 TEST(SignatureAlgorithmTest, Equals_RsaPkcs1Match) {
363 SignatureAlgorithm alg1;
364 alg1.AssignRsaPkcs1(DigestAlgorithm::Sha512);
365
366 SignatureAlgorithm alg2;
367 alg2.AssignRsaPkcs1(DigestAlgorithm::Sha512);
368
369 ASSERT_TRUE(alg1.Equals(alg2));
370 }
371
372 // Tests that two RSASSA-PSS algorithms are equal.
373 TEST(SignatureAlgorithmTest, Equals_RsaPssMatch) {
374 SignatureAlgorithm alg1;
375 alg1.AssignRsaPss(DigestAlgorithm::Sha256, DigestAlgorithm::Sha1, 21);
376
377 SignatureAlgorithm alg2;
378 alg2.AssignRsaPss(DigestAlgorithm::Sha256, DigestAlgorithm::Sha1, 21);
379
380 ASSERT_TRUE(alg1.Equals(alg2));
381 }
382
383 // Tests that two invalid algorithms are considered equal.
384 TEST(SignatureAlgorithmTest, Equals_BothInvalid) {
385 SignatureAlgorithm alg1;
386 SignatureAlgorithm alg2;
387
388 ASSERT_TRUE(alg1.Equals(alg2));
389 }
390
391 // Tests that an invalid algorith is not considered equal to a valid one.
392 TEST(SignatureAlgorithmTest, Equals_OneInvalid) {
393 SignatureAlgorithm alg1;
394 alg1.AssignEcdsa(DigestAlgorithm::Sha256);
395
396 SignatureAlgorithm alg2;
397
398 ASSERT_FALSE(alg1.Equals(alg2));
399 }
400
401 // Tests that two RSASSA-PSS algorithms with different hashes are not equal.
402 TEST(SignatureAlgorithmTest, Equals_RsaPssWithDifferentDigest) {
403 SignatureAlgorithm alg1;
404 alg1.AssignRsaPss(DigestAlgorithm::Sha1, DigestAlgorithm::Sha1, 20);
405
406 SignatureAlgorithm alg2;
407 alg1.AssignRsaPss(DigestAlgorithm::Sha256, DigestAlgorithm::Sha1, 20);
408
409 ASSERT_FALSE(alg1.Equals(alg2));
410 }
411
412 // Tests that two RSASSA-PSS algorithms with different mask gens are not equal.
413 TEST(SignatureAlgorithmTest, Equals_RsaPssWithDifferentMaskGen) {
414 SignatureAlgorithm alg1;
415 alg1.AssignRsaPss(DigestAlgorithm::Sha256, DigestAlgorithm::Sha1, 20);
416
417 SignatureAlgorithm alg2;
418 alg1.AssignRsaPss(DigestAlgorithm::Sha256, DigestAlgorithm::Sha256, 20);
419
420 ASSERT_FALSE(alg1.Equals(alg2));
421 }
422
423 // Tests that two RSASSA-PSS algorithms with different salts
424 TEST(SignatureAlgorithmTest, Equals_RsaPssWithDifferentSalt) {
425 SignatureAlgorithm alg1;
426 alg1.AssignRsaPss(DigestAlgorithm::Sha1, DigestAlgorithm::Sha1, 20);
427
428 SignatureAlgorithm alg2;
429 alg1.AssignRsaPss(DigestAlgorithm::Sha1, DigestAlgorithm::Sha1, 16);
430
431 ASSERT_FALSE(alg1.Equals(alg2));
432 }
433
434 // Tests that the parmeters returned for an ECDSA algorithm are null for
435 // non-ECDSA algorithms.
436 TEST(SignatureAlgorithmTest, ParamsAreNullForWrongType_Ecdsa) {
437 SignatureAlgorithm alg1;
438 alg1.AssignEcdsa(DigestAlgorithm::Sha1);
439
440 EXPECT_FALSE(alg1.ParamsForRsaPss());
441 }
442
443 // Tests that the parmeters returned for an RSA PKCS#1 v1.5 algorithm are null
444 // for non-RSA PKCS#1 v1.5 algorithms.
445 TEST(SignatureAlgorithmTest, ParamsAreNullForWrongType_RsaPkcs1) {
446 SignatureAlgorithm alg1;
447 alg1.AssignRsaPkcs1(DigestAlgorithm::Sha1);
448
449 EXPECT_FALSE(alg1.ParamsForRsaPss());
450 }
451
452 // Tests that the parmeters returned for an invalid algorithm are null.
453 TEST(SignatureAlgorithmTest, ParamsAreNullForWrongType_Invalid) {
454 SignatureAlgorithm alg1;
455
456 EXPECT_FALSE(alg1.ParamsForRsaPss());
457 }
458
459 } // namespace
460
461 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698