• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 The Chromium Authors
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 "signature_algorithm.h"
6 
7 #include <memory>
8 
9 #include "fillins/file_util.h"
10 #include "input.h"
11 #include "parser.h"
12 #include <gtest/gtest.h>
13 
14 namespace bssl {
15 
16 namespace {
17 
18 // Parses a SignatureAlgorithm given an empty DER input.
TEST(SignatureAlgorithmTest,ParseDerEmpty)19 TEST(SignatureAlgorithmTest, ParseDerEmpty) {
20   EXPECT_FALSE(ParseSignatureAlgorithm(der::Input()));
21 }
22 
23 // Parses a SignatureAlgorithm given invalid DER input.
TEST(SignatureAlgorithmTest,ParseDerBogus)24 TEST(SignatureAlgorithmTest, ParseDerBogus) {
25   const uint8_t kData[] = {0x00};
26   EXPECT_FALSE(ParseSignatureAlgorithm(der::Input(kData)));
27 }
28 
29 // Parses a SignatureAlgorithm with an unsupported algorithm OID.
30 //
31 //   SEQUENCE (2 elem)
32 //       OBJECT IDENTIFIER 66 (bogus)
TEST(SignatureAlgorithmTest,ParseDerRsaPssUnsupportedAlgorithmOid)33 TEST(SignatureAlgorithmTest, ParseDerRsaPssUnsupportedAlgorithmOid) {
34   // clang-format off
35   const uint8_t kData[] = {
36       0x30, 0x03,  // SEQUENCE (3 bytes)
37       0x06, 0x01,  // OBJECT IDENTIFIER (1 bytes)
38       0x42,
39   };
40   // clang-format on
41   EXPECT_FALSE(ParseSignatureAlgorithm(der::Input(kData)));
42 }
43 
44 // Parses a sha1WithRSAEncryption which contains a NULL parameters field.
45 //
46 //   SEQUENCE (2 elem)
47 //       OBJECT IDENTIFIER  1.2.840.113549.1.1.5
48 //       NULL
TEST(SignatureAlgorithmTest,ParseDerSha1WithRSAEncryptionNullParams)49 TEST(SignatureAlgorithmTest, ParseDerSha1WithRSAEncryptionNullParams) {
50   // clang-format off
51   const uint8_t kData[] = {
52       0x30, 0x0D,  // SEQUENCE (13 bytes)
53       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
54       0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x05,
55       0x05, 0x00,  // NULL (0 bytes)
56   };
57   // clang-format on
58   EXPECT_EQ(ParseSignatureAlgorithm(der::Input(kData)),
59             SignatureAlgorithm::kRsaPkcs1Sha1);
60 }
61 
62 // Parses a sha1WithRSAEncryption which contains no parameters field.
63 //
64 //   SEQUENCE (1 elem)
65 //       OBJECT IDENTIFIER  1.2.840.113549.1.1.5
TEST(SignatureAlgorithmTest,ParseDerSha1WithRSAEncryptionNoParams)66 TEST(SignatureAlgorithmTest, ParseDerSha1WithRSAEncryptionNoParams) {
67   // clang-format off
68   const uint8_t kData[] = {
69       0x30, 0x0B,  // SEQUENCE (11 bytes)
70       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
71       0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x05,
72   };
73   // clang-format on
74   EXPECT_EQ(ParseSignatureAlgorithm(der::Input(kData)),
75             SignatureAlgorithm::kRsaPkcs1Sha1);
76 }
77 
78 // Parses a sha1WithRSAEncryption which contains an unexpected parameters
79 // field. Instead of being NULL it is an integer.
80 //
81 //   SEQUENCE (2 elem)
82 //       OBJECT IDENTIFIER  1.2.840.113549.1.1.5
83 //       INTEGER  0
TEST(SignatureAlgorithmTest,ParseDerSha1WithRSAEncryptionNonNullParams)84 TEST(SignatureAlgorithmTest, ParseDerSha1WithRSAEncryptionNonNullParams) {
85   // clang-format off
86   const uint8_t kData[] = {
87       0x30, 0x0E,  // SEQUENCE (14 bytes)
88       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
89       0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x05,
90       0x02, 0x01, 0x00,  // INTEGER (1 byte)
91   };
92   // clang-format on
93   EXPECT_FALSE(ParseSignatureAlgorithm(der::Input(kData)));
94 }
95 
96 // Parses a sha1WithRSASignature which contains a NULL parameters field.
97 //
98 //   SEQUENCE (2 elem)
99 //       OBJECT IDENTIFIER  1.3.14.3.2.29
100 //       NULL
TEST(SignatureAlgorithmTest,ParseDerSha1WithRSASignatureNullParams)101 TEST(SignatureAlgorithmTest, ParseDerSha1WithRSASignatureNullParams) {
102   // clang-format off
103   const uint8_t kData[] = {
104       0x30, 0x09,  // SEQUENCE (9 bytes)
105       0x06, 0x05,  // OBJECT IDENTIFIER (5 bytes)
106       0x2b, 0x0e, 0x03, 0x02, 0x1d,
107       0x05, 0x00,  // NULL (0 bytes)
108   };
109   // clang-format on
110   EXPECT_EQ(ParseSignatureAlgorithm(der::Input(kData)),
111             SignatureAlgorithm::kRsaPkcs1Sha1);
112 }
113 
114 // Parses a sha1WithRSASignature which contains no parameters field.
115 //
116 //   SEQUENCE (1 elem)
117 //       OBJECT IDENTIFIER  1.3.14.3.2.29
TEST(SignatureAlgorithmTest,ParseDerSha1WithRSASignatureNoParams)118 TEST(SignatureAlgorithmTest, ParseDerSha1WithRSASignatureNoParams) {
119   // clang-format off
120   const uint8_t kData[] = {
121       0x30, 0x07,  // SEQUENCE (7 bytes)
122       0x06, 0x05,  // OBJECT IDENTIFIER (5 bytes)
123       0x2b, 0x0e, 0x03, 0x02, 0x1d,
124   };
125   // clang-format on
126   EXPECT_EQ(ParseSignatureAlgorithm(der::Input(kData)),
127             SignatureAlgorithm::kRsaPkcs1Sha1);
128 }
129 
130 // Parses a sha1WithRSAEncryption which contains values after the sequence.
131 //
132 //   SEQUENCE (2 elem)
133 //       OBJECT IDENTIFIER  1.2.840.113549.1.1.5
134 //       NULL
135 //   INTEGER  0
TEST(SignatureAlgorithmTest,ParseDerSha1WithRsaEncryptionDataAfterSequence)136 TEST(SignatureAlgorithmTest, ParseDerSha1WithRsaEncryptionDataAfterSequence) {
137   // clang-format off
138   const uint8_t kData[] = {
139       0x30, 0x0D,  // SEQUENCE (13 bytes)
140       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
141       0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x05,
142       0x05, 0x00,  // NULL (0 bytes)
143       0x02, 0x01, 0x00,  // INTEGER (1 byte)
144   };
145   // clang-format on
146   EXPECT_FALSE(ParseSignatureAlgorithm(der::Input(kData)));
147 }
148 
149 // Parses a sha1WithRSAEncryption which contains a bad NULL parameters field.
150 // Normally NULL is encoded as {0x05, 0x00} (tag for NULL and length of 0). Here
151 // NULL is encoded as having a length of 1 instead, followed by data 0x09.
152 //
153 //   SEQUENCE (2 elem)
154 //       OBJECT IDENTIFIER  1.2.840.113549.1.1.5
155 //       NULL
TEST(SignatureAlgorithmTest,ParseDerSha1WithRSAEncryptionBadNullParams)156 TEST(SignatureAlgorithmTest, ParseDerSha1WithRSAEncryptionBadNullParams) {
157   // clang-format off
158   const uint8_t kData[] = {
159       0x30, 0x0E,  // SEQUENCE (13 bytes)
160       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
161       0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x05,
162       0x05, 0x01, 0x09,  // NULL (1 byte)
163   };
164   // clang-format on
165   EXPECT_FALSE(ParseSignatureAlgorithm(der::Input(kData)));
166 }
167 
168 // Parses a sha1WithRSAEncryption which contains a NULL parameters field,
169 // followed by an integer.
170 //
171 //   SEQUENCE (3 elem)
172 //       OBJECT IDENTIFIER  1.2.840.113549.1.1.5
173 //       NULL
174 //       INTEGER  0
TEST(SignatureAlgorithmTest,ParseDerSha1WithRSAEncryptionNullParamsThenInteger)175 TEST(SignatureAlgorithmTest,
176      ParseDerSha1WithRSAEncryptionNullParamsThenInteger) {
177   // clang-format off
178   const uint8_t kData[] = {
179       0x30, 0x10,  // SEQUENCE (16 bytes)
180       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
181       0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x05,
182       0x05, 0x00,  // NULL (0 bytes)
183       0x02, 0x01, 0x00,  // INTEGER (1 byte)
184   };
185   // clang-format on
186   EXPECT_FALSE(ParseSignatureAlgorithm(der::Input(kData)));
187 }
188 
189 // Parses a SignatureAlgorithm given DER which does not encode a sequence.
190 //
191 //   INTEGER 0
TEST(SignatureAlgorithmTest,ParseDerNotASequence)192 TEST(SignatureAlgorithmTest, ParseDerNotASequence) {
193   // clang-format off
194   const uint8_t kData[] = {
195       0x02, 0x01, 0x00,  // INTEGER (1 byte)
196   };
197   // clang-format on
198   EXPECT_FALSE(ParseSignatureAlgorithm(der::Input(kData)));
199 }
200 
201 // Parses a sha256WithRSAEncryption which contains a NULL parameters field.
202 //
203 //   SEQUENCE (2 elem)
204 //       OBJECT IDENTIFIER  1.2.840.113549.1.1.11
205 //       NULL
TEST(SignatureAlgorithmTest,ParseDerSha256WithRSAEncryptionNullParams)206 TEST(SignatureAlgorithmTest, ParseDerSha256WithRSAEncryptionNullParams) {
207   // clang-format off
208   const uint8_t kData[] = {
209       0x30, 0x0D,  // SEQUENCE (13 bytes)
210       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
211       0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
212       0x05, 0x00,  // NULL (0 bytes)
213   };
214   // clang-format on
215   EXPECT_EQ(ParseSignatureAlgorithm(der::Input(kData)),
216             SignatureAlgorithm::kRsaPkcs1Sha256);
217 }
218 
219 // Parses a sha256WithRSAEncryption which contains no parameters field.
220 //
221 //   SEQUENCE (1 elem)
222 //       OBJECT IDENTIFIER  1.2.840.113549.1.1.11
TEST(SignatureAlgorithmTest,ParseDerSha256WithRSAEncryptionNoParams)223 TEST(SignatureAlgorithmTest, ParseDerSha256WithRSAEncryptionNoParams) {
224   // clang-format off
225   const uint8_t kData[] = {
226       0x30, 0x0B,  // SEQUENCE (11 bytes)
227       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
228       0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
229   };
230   // clang-format on
231   EXPECT_EQ(ParseSignatureAlgorithm(der::Input(kData)),
232             SignatureAlgorithm::kRsaPkcs1Sha256);
233 }
234 
235 // Parses a sha384WithRSAEncryption which contains a NULL parameters field.
236 //
237 //   SEQUENCE (2 elem)
238 //       OBJECT IDENTIFIER  1.2.840.113549.1.1.12
239 //       NULL
TEST(SignatureAlgorithmTest,ParseDerSha384WithRSAEncryptionNullParams)240 TEST(SignatureAlgorithmTest, ParseDerSha384WithRSAEncryptionNullParams) {
241   // clang-format off
242   const uint8_t kData[] = {
243       0x30, 0x0D,  // SEQUENCE (13 bytes)
244       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
245       0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0c,
246       0x05, 0x00,  // NULL (0 bytes)
247   };
248   // clang-format on
249   EXPECT_EQ(ParseSignatureAlgorithm(der::Input(kData)),
250             SignatureAlgorithm::kRsaPkcs1Sha384);
251 }
252 
253 // Parses a sha384WithRSAEncryption which contains no parameters field.
254 //
255 //   SEQUENCE (1 elem)
256 //       OBJECT IDENTIFIER  1.2.840.113549.1.1.12
TEST(SignatureAlgorithmTest,ParseDerSha384WithRSAEncryptionNoParams)257 TEST(SignatureAlgorithmTest, ParseDerSha384WithRSAEncryptionNoParams) {
258   // clang-format off
259   const uint8_t kData[] = {
260       0x30, 0x0B,  // SEQUENCE (11 bytes)
261       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
262       0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0c,
263   };
264   // clang-format on
265   EXPECT_EQ(ParseSignatureAlgorithm(der::Input(kData)),
266             SignatureAlgorithm::kRsaPkcs1Sha384);
267 }
268 
269 // Parses a sha512WithRSAEncryption which contains a NULL parameters field.
270 //
271 //   SEQUENCE (2 elem)
272 //       OBJECT IDENTIFIER  1.2.840.113549.1.1.13
273 //       NULL
TEST(SignatureAlgorithmTest,ParseDerSha512WithRSAEncryptionNullParams)274 TEST(SignatureAlgorithmTest, ParseDerSha512WithRSAEncryptionNullParams) {
275   // clang-format off
276   const uint8_t kData[] = {
277       0x30, 0x0D,  // SEQUENCE (13 bytes)
278       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
279       0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0d,
280       0x05, 0x00,  // NULL (0 bytes)
281   };
282   // clang-format on
283   EXPECT_EQ(ParseSignatureAlgorithm(der::Input(kData)),
284             SignatureAlgorithm::kRsaPkcs1Sha512);
285 }
286 
287 // Parses a sha512WithRSAEncryption which contains no parameters field.
288 //
289 //   SEQUENCE (1 elem)
290 //       OBJECT IDENTIFIER  1.2.840.113549.1.1.13
TEST(SignatureAlgorithmTest,ParseDerSha512WithRSAEncryptionNoParams)291 TEST(SignatureAlgorithmTest, ParseDerSha512WithRSAEncryptionNoParams) {
292   // clang-format off
293   const uint8_t kData[] = {
294       0x30, 0x0B,  // SEQUENCE (11 bytes)
295       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
296       0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0d,
297   };
298   // clang-format on
299   EXPECT_EQ(ParseSignatureAlgorithm(der::Input(kData)),
300             SignatureAlgorithm::kRsaPkcs1Sha512);
301 }
302 
303 // Parses a sha224WithRSAEncryption which contains a NULL parameters field.
304 // This fails because the parsing code does not enumerate this OID (even though
305 // it is in fact valid).
306 //
307 //   SEQUENCE (2 elem)
308 //       OBJECT IDENTIFIER  1.2.840.113549.1.1.14
309 //       NULL
TEST(SignatureAlgorithmTest,ParseDerSha224WithRSAEncryptionNullParams)310 TEST(SignatureAlgorithmTest, ParseDerSha224WithRSAEncryptionNullParams) {
311   // clang-format off
312   const uint8_t kData[] = {
313       0x30, 0x0D,  // SEQUENCE (13 bytes)
314       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
315       0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0e,
316       0x05, 0x00,  // NULL (0 bytes)
317   };
318   // clang-format on
319   EXPECT_FALSE(ParseSignatureAlgorithm(der::Input(kData)));
320 }
321 
322 // Parses a ecdsa-with-SHA1 which contains no parameters field.
323 //
324 //   SEQUENCE (1 elem)
325 //       OBJECT IDENTIFIER  1.2.840.10045.4.1
TEST(SignatureAlgorithmTest,ParseDerEcdsaWithSHA1NoParams)326 TEST(SignatureAlgorithmTest, ParseDerEcdsaWithSHA1NoParams) {
327   // clang-format off
328   const uint8_t kData[] = {
329       0x30, 0x09,  // SEQUENCE (9 bytes)
330       0x06, 0x07,  // OBJECT IDENTIFIER (7 bytes)
331       0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x01,
332   };
333   // clang-format on
334   EXPECT_EQ(ParseSignatureAlgorithm(der::Input(kData)),
335             SignatureAlgorithm::kEcdsaSha1);
336 }
337 
338 // Parses a ecdsa-with-SHA1 which contains a NULL parameters field.
339 //
340 //   SEQUENCE (2 elem)
341 //       OBJECT IDENTIFIER  1.2.840.10045.4.1
342 //       NULL
TEST(SignatureAlgorithmTest,ParseDerEcdsaWithSHA1NullParams)343 TEST(SignatureAlgorithmTest, ParseDerEcdsaWithSHA1NullParams) {
344   // clang-format off
345   const uint8_t kData[] = {
346       0x30, 0x0B,  // SEQUENCE (11 bytes)
347       0x06, 0x07,  // OBJECT IDENTIFIER (7 bytes)
348       0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x01,
349       0x05, 0x00,  // NULL (0 bytes)
350   };
351   // clang-format on
352   EXPECT_FALSE(ParseSignatureAlgorithm(der::Input(kData)));
353 }
354 
355 // Parses a ecdsa-with-SHA256 which contains no parameters field.
356 //
357 //   SEQUENCE (1 elem)
358 //       OBJECT IDENTIFIER  1.2.840.10045.4.3.2
TEST(SignatureAlgorithmTest,ParseDerEcdsaWithSHA256NoParams)359 TEST(SignatureAlgorithmTest, ParseDerEcdsaWithSHA256NoParams) {
360   // clang-format off
361   const uint8_t kData[] = {
362       0x30, 0x0A,  // SEQUENCE (10 bytes)
363       0x06, 0x08,  // OBJECT IDENTIFIER (8 bytes)
364       0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02,
365   };
366   // clang-format on
367   EXPECT_EQ(ParseSignatureAlgorithm(der::Input(kData)),
368             SignatureAlgorithm::kEcdsaSha256);
369 }
370 
371 // Parses a ecdsa-with-SHA256 which contains a NULL parameters field.
372 //
373 //   SEQUENCE (2 elem)
374 //       OBJECT IDENTIFIER  1.2.840.10045.4.3.2
375 //       NULL
TEST(SignatureAlgorithmTest,ParseDerEcdsaWithSHA256NullParams)376 TEST(SignatureAlgorithmTest, ParseDerEcdsaWithSHA256NullParams) {
377   // clang-format off
378   const uint8_t kData[] = {
379       0x30, 0x0C,  // SEQUENCE (12 bytes)
380       0x06, 0x08,  // OBJECT IDENTIFIER (8 bytes)
381       0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02,
382       0x05, 0x00,  // NULL (0 bytes)
383   };
384   // clang-format on
385   EXPECT_FALSE(ParseSignatureAlgorithm(der::Input(kData)));
386 }
387 
388 // Parses a ecdsa-with-SHA384 which contains no parameters field.
389 //
390 //   SEQUENCE (1 elem)
391 //       OBJECT IDENTIFIER  1.2.840.10045.4.3.3
TEST(SignatureAlgorithmTest,ParseDerEcdsaWithSHA384NoParams)392 TEST(SignatureAlgorithmTest, ParseDerEcdsaWithSHA384NoParams) {
393   // clang-format off
394   const uint8_t kData[] = {
395       0x30, 0x0A,  // SEQUENCE (10 bytes)
396       0x06, 0x08,  // OBJECT IDENTIFIER (8 bytes)
397       0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x03,
398   };
399   // clang-format on
400   EXPECT_EQ(ParseSignatureAlgorithm(der::Input(kData)),
401             SignatureAlgorithm::kEcdsaSha384);
402 }
403 
404 // Parses a ecdsa-with-SHA384 which contains a NULL parameters field.
405 //
406 //   SEQUENCE (2 elem)
407 //       OBJECT IDENTIFIER  1.2.840.10045.4.3.3
408 //       NULL
TEST(SignatureAlgorithmTest,ParseDerEcdsaWithSHA384NullParams)409 TEST(SignatureAlgorithmTest, ParseDerEcdsaWithSHA384NullParams) {
410   // clang-format off
411   const uint8_t kData[] = {
412       0x30, 0x0C,  // SEQUENCE (12 bytes)
413       0x06, 0x08,  // OBJECT IDENTIFIER (8 bytes)
414       0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x03,
415       0x05, 0x00,  // NULL (0 bytes)
416   };
417   // clang-format on
418   EXPECT_FALSE(ParseSignatureAlgorithm(der::Input(kData)));
419 }
420 
421 // Parses a ecdsa-with-SHA512 which contains no parameters field.
422 //
423 //   SEQUENCE (1 elem)
424 //       OBJECT IDENTIFIER  1.2.840.10045.4.3.4
TEST(SignatureAlgorithmTest,ParseDerEcdsaWithSHA512NoParams)425 TEST(SignatureAlgorithmTest, ParseDerEcdsaWithSHA512NoParams) {
426   // clang-format off
427   const uint8_t kData[] = {
428       0x30, 0x0A,  // SEQUENCE (10 bytes)
429       0x06, 0x08,  // OBJECT IDENTIFIER (8 bytes)
430       0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x04,
431   };
432   // clang-format on
433   EXPECT_EQ(ParseSignatureAlgorithm(der::Input(kData)),
434             SignatureAlgorithm::kEcdsaSha512);
435 }
436 
437 // Parses a ecdsa-with-SHA512 which contains a NULL parameters field.
438 //
439 //   SEQUENCE (2 elem)
440 //       OBJECT IDENTIFIER  1.2.840.10045.4.3.4
441 //       NULL
TEST(SignatureAlgorithmTest,ParseDerEcdsaWithSHA512NullParams)442 TEST(SignatureAlgorithmTest, ParseDerEcdsaWithSHA512NullParams) {
443   // clang-format off
444   const uint8_t kData[] = {
445       0x30, 0x0C,  // SEQUENCE (12 bytes)
446       0x06, 0x08,  // OBJECT IDENTIFIER (8 bytes)
447       0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x04,
448       0x05, 0x00,  // NULL (0 bytes)
449   };
450   // clang-format on
451   EXPECT_FALSE(ParseSignatureAlgorithm(der::Input(kData)));
452 }
453 
454 // Parses a rsaPss algorithm that uses SHA256 and a salt length of 32.
455 //
456 //   SEQUENCE (2 elem)
457 //       OBJECT IDENTIFIER  1.2.840.113549.1.1.10
458 //       SEQUENCE (4 elem)
459 //           [0] (1 elem)
460 //               SEQUENCE (2 elem)
461 //                   OBJECT IDENTIFIER 2.16.840.1.101.3.4.2.1
462 //                   NULL
463 //           [1] (1 elem)
464 //               SEQUENCE (2 elem)
465 //                   OBJECT IDENTIFIER  1.2.840.113549.1.1.8
466 //                   SEQUENCE (2 elem)
467 //                       OBJECT IDENTIFIER 2.16.840.1.101.3.4.2.1
468 //                       NULL
469 //           [2] (1 elem)
470 //               INTEGER  32
TEST(SignatureAlgorithmTest,ParseDerRsaPss)471 TEST(SignatureAlgorithmTest, ParseDerRsaPss) {
472   // clang-format off
473   const uint8_t kData[] = {
474       0x30, 0x41,  // SEQUENCE (65 bytes)
475       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
476       0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0A,
477       0x30, 0x34,  // SEQUENCE (52 bytes)
478       0xA0, 0x0F,  // [0] (15 bytes)
479       0x30, 0x0D,  // SEQUENCE (13 bytes)
480       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
481       0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
482       0x05, 0x00,  // NULL (0 bytes)
483       0xA1, 0x1C,  // [1] (28 bytes)
484       0x30, 0x1A,  // SEQUENCE (26 bytes)
485       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
486       0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x08,
487       0x30, 0x0D,  // SEQUENCE (13 bytes)
488       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
489       0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
490       0x05, 0x00,  // NULL (0 bytes)
491       0xA2, 0x03,  // [2] (3 bytes)
492       0x02, 0x01,  // INTEGER (1 byte)
493       0x20,
494 
495   };
496   // clang-format on
497   EXPECT_EQ(ParseSignatureAlgorithm(der::Input(kData)),
498             SignatureAlgorithm::kRsaPssSha256);
499 }
500 
501 // Parses a rsaPss algorithm that has an empty parameters. This encodes the
502 // default, SHA-1, which we do not support.
503 //
504 //   SEQUENCE (2 elem)
505 //       OBJECT IDENTIFIER  1.2.840.113549.1.1.10
506 //       SEQUENCE (0 elem)
TEST(SignatureAlgorithmTest,ParseDerRsaPssEmptyParams)507 TEST(SignatureAlgorithmTest, ParseDerRsaPssEmptyParams) {
508   // clang-format off
509   const uint8_t kData[] = {
510       0x30, 0x0D,  // SEQUENCE (13 bytes)
511       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
512       0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0A,
513       0x30, 0x00,  // SEQUENCE (0 bytes)
514   };
515   // clang-format on
516   EXPECT_FALSE(ParseSignatureAlgorithm(der::Input(kData)));
517 }
518 
519 // Parses a rsaPss algorithm that has NULL parameters. This fails.
520 //
521 //   SEQUENCE (2 elem)
522 //       OBJECT IDENTIFIER  1.2.840.113549.1.1.10
523 //       NULL
TEST(SignatureAlgorithmTest,ParseDerRsaPssNullParams)524 TEST(SignatureAlgorithmTest, ParseDerRsaPssNullParams) {
525   // clang-format off
526   const uint8_t kData[] = {
527       0x30, 0x0D,  // SEQUENCE (13 bytes)
528       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
529       0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0A,
530       0x05, 0x00,  // NULL (0 bytes)
531   };
532   // clang-format on
533   EXPECT_FALSE(ParseSignatureAlgorithm(der::Input(kData)));
534 }
535 
536 // Parses a rsaPss algorithm that has no parameters. This fails.
537 //
538 //   SEQUENCE (1 elem)
539 //       OBJECT IDENTIFIER  1.2.840.113549.1.1.10
TEST(SignatureAlgorithmTest,ParseDerRsaPssNoParams)540 TEST(SignatureAlgorithmTest, ParseDerRsaPssNoParams) {
541   // clang-format off
542   const uint8_t kData[] = {
543       0x30, 0x0B,  // SEQUENCE (11 bytes)
544       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
545       0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0A,
546   };
547   // clang-format on
548   EXPECT_FALSE(ParseSignatureAlgorithm(der::Input(kData)));
549 }
550 
551 // Parses a rsaPss algorithm that has data after the parameters sequence.
552 //
553 //   SEQUENCE (3 elem)
554 //       OBJECT IDENTIFIER  1.2.840.113549.1.1.10
555 //       SEQUENCE (0 elem)
556 //       NULL
TEST(SignatureAlgorithmTest,ParseDerRsaPssDataAfterParams)557 TEST(SignatureAlgorithmTest, ParseDerRsaPssDataAfterParams) {
558   // clang-format off
559   const uint8_t kData[] = {
560       0x30, 0x0F,  // SEQUENCE (15 bytes)
561       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
562       0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0A,
563       0x30, 0x00,  // SEQUENCE (0 bytes)
564       0x05, 0x00,  // NULL (0 bytes)
565   };
566   // clang-format on
567   EXPECT_FALSE(ParseSignatureAlgorithm(der::Input(kData)));
568 }
569 
570 // Parses a rsaPss algorithm that has unrecognized data (NULL) within the
571 // parameters sequence.
572 //
573 //   SEQUENCE (2 elem)
574 //       OBJECT IDENTIFIER  1.2.840.113549.1.1.10
575 //       SEQUENCE (2 elem)
576 //           [2] (1 elem)
577 //               INTEGER  23
578 //           NULL
TEST(SignatureAlgorithmTest,ParseDerRsaPssNullInsideParams)579 TEST(SignatureAlgorithmTest, ParseDerRsaPssNullInsideParams) {
580   // clang-format off
581   const uint8_t kData[] = {
582       0x30, 0x14,  // SEQUENCE (62 bytes)
583       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
584       0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0A,
585       0x30, 0x07,  // SEQUENCE (5 bytes)
586       0xA2, 0x03,  // [2] (3 bytes)
587       0x02, 0x01,  // INTEGER (1 byte)
588       0x17,
589       0x05, 0x00,  // NULL (0 bytes)
590   };
591   // clang-format on
592   EXPECT_FALSE(ParseSignatureAlgorithm(der::Input(kData)));
593 }
594 
595 // Parses a rsaPss algorithm that has an unsupported trailer value (2). Only
596 // trailer values of 1 are allowed by RFC 4055.
597 //
598 //   SEQUENCE (2 elem)
599 //       OBJECT IDENTIFIER  1.2.840.113549.1.1.10
600 //       SEQUENCE (1 elem)
601 //           [3] (1 elem)
602 //               INTEGER  2
TEST(SignatureAlgorithmTest,ParseDerRsaPssUnsupportedTrailer)603 TEST(SignatureAlgorithmTest, ParseDerRsaPssUnsupportedTrailer) {
604   // clang-format off
605   const uint8_t kData[] = {
606       0x30, 0x12,  // SEQUENCE (18 bytes)
607       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
608       0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0A,
609       0x30, 0x05,  // SEQUENCE (5 bytes)
610       0xA3, 0x03,  // [3] (3 bytes)
611       0x02, 0x01,  // INTEGER (1 byte)
612       0x02,
613   };
614   // clang-format on
615   EXPECT_FALSE(ParseSignatureAlgorithm(der::Input(kData)));
616 }
617 
618 // Parses a rsaPss algorithm that has extra data appearing after the trailer in
619 // the [3] section.
620 //
621 //   SEQUENCE (2 elem)
622 //       OBJECT IDENTIFIER  1.2.840.113549.1.1.10
623 //       SEQUENCE (1 elem)
624 //           [3] (2 elem)
625 //               INTEGER  1
626 //               NULL
TEST(SignatureAlgorithmTest,ParseDerRsaPssBadTrailer)627 TEST(SignatureAlgorithmTest, ParseDerRsaPssBadTrailer) {
628   // clang-format off
629   const uint8_t kData[] = {
630       0x30, 0x14,  // SEQUENCE (20 bytes)
631       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
632       0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0A,
633       0x30, 0x07,  // SEQUENCE (7 bytes)
634       0xA3, 0x05,  // [3] (5 bytes)
635       0x02, 0x01,  // INTEGER (1 byte)
636       0x01,
637       0x05, 0x00,  // NULL (0 bytes)
638   };
639   // clang-format on
640   EXPECT_FALSE(ParseSignatureAlgorithm(der::Input(kData)));
641 }
642 
643 // Parses a rsaPss algorithm that uses SHA384 for the hash, and leaves the rest
644 // as defaults, specifying a SHA-1 MGF-1 hash. This fails because we require
645 // the hashes match.
646 //
647 //   SEQUENCE (2 elem)
648 //       OBJECT IDENTIFIER  1.2.840.113549.1.1.10
649 //       SEQUENCE (1 elem)
650 //           [0] (1 elem)
651 //               SEQUENCE (2 elem)
652 //                   OBJECT IDENTIFIER  2.16.840.1.101.3.4.2.2
653 //                   NULL
TEST(SignatureAlgorithmTest,ParseDerRsaPssNonDefaultHash)654 TEST(SignatureAlgorithmTest, ParseDerRsaPssNonDefaultHash) {
655   // clang-format off
656   const uint8_t kData[] = {
657       0x30, 0x1E,  // SEQUENCE (30 bytes)
658       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
659       0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0A,
660       0x30, 0x11,  // SEQUENCE (17 bytes)
661       0xA0, 0x0F,  // [0] (15 bytes)
662       0x30, 0x0D,  // SEQUENCE (13 bytes)
663       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
664       0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02,
665       0x05, 0x00,  // NULL (0 bytes)
666   };
667   // clang-format on
668   EXPECT_FALSE(ParseSignatureAlgorithm(der::Input(kData)));
669 }
670 
671 // Parses a rsaPss algorithm that uses an invalid hash algorithm (twiddled the
672 // bytes for the SHA-384 OID a bit).
673 //
674 //   SEQUENCE (2 elem)
675 //       OBJECT IDENTIFIER  1.2.840.113549.1.1.10
676 //       SEQUENCE (1 elem)
677 //           [0] (1 elem)
678 //               SEQUENCE (1 elem)
679 //                   OBJECT IDENTIFIER  2.16.840.2.103.19.4.2.2
TEST(SignatureAlgorithmTest,ParseDerRsaPssUnsupportedHashOid)680 TEST(SignatureAlgorithmTest, ParseDerRsaPssUnsupportedHashOid) {
681   // clang-format off
682   const uint8_t kData[] = {
683       0x30, 0x1C,  // SEQUENCE (28 bytes)
684       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
685       0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0A,
686       0x30, 0x0F,  // SEQUENCE (15 bytes)
687       0xA0, 0x0D,  // [0] (13 bytes)
688       0x30, 0x0B,  // SEQUENCE (11 bytes)
689       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
690       0x60, 0x86, 0x48, 0x02, 0x67, 0x13, 0x04, 0x02, 0x02,
691   };
692   // clang-format on
693   EXPECT_FALSE(ParseSignatureAlgorithm(der::Input(kData)));
694 }
695 
696 // Parses a rsaPss algorithm that uses SHA512 MGF1 for the mask gen, and
697 // defaults (SHA-1) for the rest. This fails because we require the hashes
698 // match.
699 //
700 //   SEQUENCE (2 elem)
701 //       OBJECT IDENTIFIER  1.2.840.113549.1.1.10
702 //       SEQUENCE (1 elem)
703 //           [1] (1 elem)
704 //               SEQUENCE (2 elem)
705 //                   OBJECT IDENTIFIER  1.2.840.113549.1.1.8
706 //                   SEQUENCE (2 elem)
707 //                       OBJECT IDENTIFIER  2.16.840.1.101.3.4.2.3
708 //                       NULL
TEST(SignatureAlgorithmTest,ParseDerRsaPssNonDefaultMaskGen)709 TEST(SignatureAlgorithmTest, ParseDerRsaPssNonDefaultMaskGen) {
710   // clang-format off
711   const uint8_t kData[] = {
712       0x30, 0x2B,  // SEQUENCE (43 bytes)
713       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
714       0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0A,
715       0x30, 0x1E,  // SEQUENCE (30 bytes)
716       0xA1, 0x1C,  // [1] (28 bytes)
717       0x30, 0x1A,  // SEQUENCE (26 bytes)
718       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
719       0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x08,
720       0x30, 0x0D,  // SEQUENCE (13 bytes)
721       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
722       0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03,
723       0x05, 0x00,  // NULL (0 bytes)
724   };
725   // clang-format on
726   EXPECT_FALSE(ParseSignatureAlgorithm(der::Input(kData)));
727 }
728 
729 // Parses a rsaPss algorithm that uses a mask gen with an unrecognized OID
730 // (twiddled some of the bits).
731 //
732 //   SEQUENCE (2 elem)
733 //       OBJECT IDENTIFIER  1.2.840.113549.1.1.10
734 //       SEQUENCE (1 elem)
735 //           [1] (1 elem)
736 //               SEQUENCE (2 elem)
737 //                   OBJECT IDENTIFIER  1.2.840.113618.1.2.8
738 //                   SEQUENCE (2 elem)
739 //                       OBJECT IDENTIFIER  2.16.840.1.101.3.4.2.3
740 //                       NULL
TEST(SignatureAlgorithmTest,ParseDerRsaPssUnsupportedMaskGen)741 TEST(SignatureAlgorithmTest, ParseDerRsaPssUnsupportedMaskGen) {
742   // clang-format off
743   const uint8_t kData[] = {
744       0x30, 0x2B,  // SEQUENCE (43 bytes)
745       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
746       0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0A,
747       0x30, 0x1E,  // SEQUENCE (30 bytes)
748       0xA1, 0x1C,  // [1] (28 bytes)
749       0x30, 0x1A,  // SEQUENCE (26 bytes)
750       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
751       0x2A, 0x86, 0x48, 0x86, 0xF7, 0x52, 0x01, 0x02, 0x08,
752       0x30, 0x0D,  // SEQUENCE (13 bytes)
753       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
754       0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03,
755       0x05, 0x00,  // NULL (0 bytes)
756   };
757   // clang-format on
758   EXPECT_FALSE(ParseSignatureAlgorithm(der::Input(kData)));
759 }
760 
761 // Parses a rsaPss algorithm that uses SHA256 for the hash, and SHA512 for the
762 // MGF1. This fails because we require the hashes match.
763 //
764 //   SEQUENCE (2 elem)
765 //       OBJECT IDENTIFIER  1.2.840.113549.1.1.10
766 //       SEQUENCE (2 elem)
767 //           [0] (1 elem)
768 //               SEQUENCE (2 elem)
769 //                   OBJECT IDENTIFIER 2.16.840.1.101.3.4.2.1
770 //                   NULL
771 //           [1] (1 elem)
772 //               SEQUENCE (2 elem)
773 //                   OBJECT IDENTIFIER  1.2.840.113549.1.1.8
774 //                   SEQUENCE (2 elem)
775 //                       OBJECT IDENTIFIER  2.16.840.1.101.3.4.2.3
776 //                       NULL
TEST(SignatureAlgorithmTest,ParseDerRsaPssNonDefaultHashAndMaskGen)777 TEST(SignatureAlgorithmTest, ParseDerRsaPssNonDefaultHashAndMaskGen) {
778   // clang-format off
779   const uint8_t kData[] = {
780       0x30, 0x3C,  // SEQUENCE (60 bytes)
781       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
782       0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0A,
783       0x30, 0x2F,  // SEQUENCE (47 bytes)
784       0xA0, 0x0F,  // [0] (15 bytes)
785       0x30, 0x0D,  // SEQUENCE (13 bytes)
786       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
787       0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
788       0x05, 0x00,  // NULL (0 bytes)
789       0xA1, 0x1C,  // [1] (28 bytes)
790       0x30, 0x1A,  // SEQUENCE (26 bytes)
791       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
792       0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x08,
793       0x30, 0x0D,  // SEQUENCE (13 bytes)
794       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
795       0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03,
796       0x05, 0x00,  // NULL (0 bytes)
797   };
798   // clang-format on
799   EXPECT_FALSE(ParseSignatureAlgorithm(der::Input(kData)));
800 }
801 
802 // Parses a rsaPss algorithm that uses SHA256 for the hash, and SHA256 for the
803 // MGF1, and a salt length of 10. This fails because we require a standard salt
804 // length.
805 //
806 //   SEQUENCE (2 elem)
807 //       OBJECT IDENTIFIER  1.2.840.113549.1.1.10
808 //       SEQUENCE (3 elem)
809 //           [0] (1 elem)
810 //               SEQUENCE (2 elem)
811 //                   OBJECT IDENTIFIER 2.16.840.1.101.3.4.2.1
812 //                   NULL
813 //           [1] (1 elem)
814 //               SEQUENCE (2 elem)
815 //                   OBJECT IDENTIFIER  1.2.840.113549.1.1.8
816 //                   SEQUENCE (2 elem)
817 //                       OBJECT IDENTIFIER  2.16.840.1.101.3.4.2.1
818 //                       NULL
819 //           [2] (1 elem)
820 //               INTEGER  10
TEST(SignatureAlgorithmTest,ParseDerRsaPssNonDefaultHashAndMaskGenAndSalt)821 TEST(SignatureAlgorithmTest, ParseDerRsaPssNonDefaultHashAndMaskGenAndSalt) {
822   // clang-format off
823   const uint8_t kData[] = {
824       0x30, 0x41,  // SEQUENCE (65 bytes)
825       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
826       0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0A,
827       0x30, 0x34,  // SEQUENCE (52 bytes)
828       0xA0, 0x0F,  // [0] (15 bytes)
829       0x30, 0x0D,  // SEQUENCE (13 bytes)
830       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
831       0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
832       0x05, 0x00,  // NULL (0 bytes)
833       0xA1, 0x1C,  // [1] (28 bytes)
834       0x30, 0x1A,  // SEQUENCE (26 bytes)
835       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
836       0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x08,
837       0x30, 0x0D,  // SEQUENCE (13 bytes)
838       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
839       0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
840       0x05, 0x00,  // NULL (0 bytes)
841       0xA2, 0x03,  // [2] (3 bytes)
842       0x02, 0x01,  // INTEGER (1 byte)
843       0x0A,
844   };
845   // clang-format on
846   EXPECT_FALSE(ParseSignatureAlgorithm(der::Input(kData)));
847 }
848 
849 // Parses a rsaPss algorithm that specifies default hash (SHA1).
850 // It is invalid to specify the default.
851 //
852 //   SEQUENCE (2 elem)
853 //       OBJECT IDENTIFIER  1.2.840.113549.1.1.10
854 //       SEQUENCE (1 elem)
855 //           [0] (1 elem)
856 //               SEQUENCE (2 elem)
857 //                   OBJECT IDENTIFIER  1.3.14.3.2.26
858 //                   NULL
TEST(SignatureAlgorithmTest,ParseDerRsaPssSpecifiedDefaultHash)859 TEST(SignatureAlgorithmTest, ParseDerRsaPssSpecifiedDefaultHash) {
860   // clang-format off
861   const uint8_t kData[] = {
862       0x30, 0x1A,  // SEQUENCE (26 bytes)
863       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
864       0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0A,
865       0x30, 0x0D,  // SEQUENCE (13 bytes)
866       0xA0, 0x0B,  // [0] (11 bytes)
867       0x30, 0x09,  // SEQUENCE (9 bytes)
868       0x06, 0x05,  // OBJECT IDENTIFIER (5 bytes)
869       0x2B, 0x0E, 0x03, 0x02, 0x1A,
870       0x05, 0x00,  // NULL (0 bytes)
871   };
872   // clang-format on
873   EXPECT_FALSE(ParseSignatureAlgorithm(der::Input(kData)));
874 }
875 
876 // Parses a rsaPss algorithm that specifies default mask gen algorithm (SHA1).
877 // It is invalid to specify the default.
878 //
879 //   SEQUENCE (2 elem)
880 //       OBJECT IDENTIFIER  1.2.840.113549.1.1.10
881 //       SEQUENCE (1 elem)
882 //           [1] (1 elem)
883 //               SEQUENCE (2 elem)
884 //                   OBJECT IDENTIFIER  1.2.840.113549.1.1.8
885 //                   SEQUENCE (2 elem)
886 //                       OBJECT IDENTIFIER  1.3.14.3.2.26
887 //                       NULL
TEST(SignatureAlgorithmTest,ParseDerRsaPssSpecifiedDefaultMaskGen)888 TEST(SignatureAlgorithmTest, ParseDerRsaPssSpecifiedDefaultMaskGen) {
889   // clang-format off
890   const uint8_t kData[] = {
891       0x30, 0x27,  // SEQUENCE (39 bytes)
892       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
893       0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0A,
894       0x30, 0x1A,  // SEQUENCE (26 bytes)
895       0xA1, 0x18,  // [1] (24 bytes)
896       0x30, 0x16,  // SEQUENCE (22 bytes)
897       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
898       0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x08,
899       0x30, 0x09,  // SEQUENCE (9 bytes)
900       0x06, 0x05,  // OBJECT IDENTIFIER (5 bytes)
901       0x2B, 0x0E, 0x03, 0x02, 0x1A,
902       0x05, 0x00,  // NULL (0 bytes)
903   };
904   // clang-format on
905   EXPECT_FALSE(ParseSignatureAlgorithm(der::Input(kData)));
906 }
907 
908 // Parses a rsaPss algorithm that specifies default salt length.
909 // It is invalid to specify the default.
910 //
911 //   SEQUENCE (2 elem)
912 //       OBJECT IDENTIFIER  1.2.840.113549.1.1.10
913 //       SEQUENCE (1 elem)
914 //           [2] (1 elem)
915 //               INTEGER  20
TEST(SignatureAlgorithmTest,ParseDerRsaPssSpecifiedDefaultSaltLength)916 TEST(SignatureAlgorithmTest, ParseDerRsaPssSpecifiedDefaultSaltLength) {
917   // clang-format off
918   const uint8_t kData[] = {
919       0x30, 0x12,  // SEQUENCE (18 bytes)
920       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
921       0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0A,
922       0x30, 0x05,  // SEQUENCE (5 bytes)
923       0xA2, 0x03,  // [2] (3 bytes)
924       0x02, 0x01,  // INTEGER (1 byte)
925       0x14,
926   };
927   // clang-format on
928   EXPECT_FALSE(ParseSignatureAlgorithm(der::Input(kData)));
929 }
930 
931 // Parses a rsaPss algorithm that specifies default trailer field.
932 // It is invalid to specify the default.
TEST(SignatureAlgorithmTest,ParseDerRsaPssSpecifiedDefaultTrailerField)933 TEST(SignatureAlgorithmTest, ParseDerRsaPssSpecifiedDefaultTrailerField) {
934   // SEQUENCE {
935   //   # rsassa-pss
936   //   OBJECT_IDENTIFIER { 1.2.840.113549.1.1.10 }
937   //   SEQUENCE {
938   //     [0] {
939   //       SEQUENCE {
940   //         # sha256
941   //         OBJECT_IDENTIFIER { 2.16.840.1.101.3.4.2.1 }
942   //         NULL {}
943   //       }
944   //     }
945   //     [1] {
946   //       SEQUENCE {
947   //         # mgf1
948   //         OBJECT_IDENTIFIER { 1.2.840.113549.1.1.8 }
949   //         SEQUENCE {
950   //           # sha256
951   //           OBJECT_IDENTIFIER { 2.16.840.1.101.3.4.2.1 }
952   //           NULL {}
953   //         }
954   //       }
955   //     }
956   //     [2] {
957   //       INTEGER { 32 }
958   //     }
959   //     [3] {
960   //       INTEGER { 1 }
961   //     }
962   //   }
963   // }
964   const uint8_t kData[] = {
965       0x30, 0x46, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
966       0x0a, 0x30, 0x39, 0xa0, 0x0f, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
967       0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0xa1, 0x1c, 0x30, 0x1a,
968       0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x08, 0x30,
969       0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
970       0x05, 0x00, 0xa2, 0x03, 0x02, 0x01, 0x20, 0xa3, 0x03, 0x02, 0x01, 0x01};
971   EXPECT_FALSE(ParseSignatureAlgorithm(der::Input(kData)));
972 }
973 
974 // Parses a rsaPss algorithm that specifies multiple default parameter values.
975 // It is invalid to specify a default value.
976 //
977 //   SEQUENCE (2 elem)
978 //       OBJECT IDENTIFIER  1.2.840.113549.1.1.10
979 //       SEQUENCE (3 elem)
980 //           [0] (1 elem)
981 //               SEQUENCE (2 elem)
982 //                   OBJECT IDENTIFIER  1.3.14.3.2.26
983 //                   NULL
984 //           [1] (1 elem)
985 //               SEQUENCE (2 elem)
986 //                   OBJECT IDENTIFIER  1.2.840.113549.1.1.8
987 //                   SEQUENCE (2 elem)
988 //                       OBJECT IDENTIFIER  1.3.14.3.2.26
989 //                       NULL
990 //           [2] (1 elem)
991 //               INTEGER  20
992 //           [3] (1 elem)
993 //               INTEGER  1
TEST(SignatureAlgorithmTest,ParseDerRsaPssMultipleDefaultParameterValues)994 TEST(SignatureAlgorithmTest, ParseDerRsaPssMultipleDefaultParameterValues) {
995   // clang-format off
996   const uint8_t kData[] = {
997       0x30, 0x3E,  // SEQUENCE (62 bytes)
998       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
999       0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0A,
1000       0x30, 0x31,  // SEQUENCE (49 bytes)
1001       0xA0, 0x0B,  // [0] (11 bytes)
1002       0x30, 0x09,  // SEQUENCE (9 bytes)
1003       0x06, 0x05,  // OBJECT IDENTIFIER (5 bytes)
1004       0x2B, 0x0E, 0x03, 0x02, 0x1A,
1005       0x05, 0x00,  // NULL (0 bytes)
1006       0xA1, 0x18,  // [1] (24 bytes)
1007       0x30, 0x16,  // SEQUENCE (22 bytes)
1008       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
1009       0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x08,
1010       0x30, 0x09,  // SEQUENCE (9 bytes)
1011       0x06, 0x05,  // OBJECT IDENTIFIER (5 bytes)
1012       0x2B, 0x0E, 0x03, 0x02, 0x1A,
1013       0x05, 0x00,  // NULL (0 bytes)
1014       0xA2, 0x03,  // [2] (3 bytes)
1015       0x02, 0x01,  // INTEGER (1 byte)
1016       0x14,
1017       0xA3, 0x03,  // [3] (3 bytes)
1018       0x02, 0x01,  // INTEGER (1 byte)
1019       0x01,
1020   };
1021   // clang-format on
1022   EXPECT_FALSE(ParseSignatureAlgorithm(der::Input(kData)));
1023 }
1024 
TEST(SignatureAlgorithmTest,ParseRsaPss)1025 TEST(SignatureAlgorithmTest, ParseRsaPss) {
1026   // Test data generated with https://github.com/google/der-ascii.
1027   struct {
1028     std::vector<uint8_t> data;
1029     SignatureAlgorithm expected;
1030   } kValidTests[] = {
1031       // SEQUENCE {
1032       //   # rsassa-pss
1033       //   OBJECT_IDENTIFIER { 1.2.840.113549.1.1.10 }
1034       //   SEQUENCE {
1035       //     [0] {
1036       //       SEQUENCE {
1037       //         # sha256
1038       //         OBJECT_IDENTIFIER { 2.16.840.1.101.3.4.2.1 }
1039       //         NULL {}
1040       //       }
1041       //     }
1042       //     [1] {
1043       //       SEQUENCE {
1044       //         # mgf1
1045       //         OBJECT_IDENTIFIER { 1.2.840.113549.1.1.8 }
1046       //         SEQUENCE {
1047       //           # sha256
1048       //           OBJECT_IDENTIFIER { 2.16.840.1.101.3.4.2.1 }
1049       //           NULL {}
1050       //         }
1051       //       }
1052       //     }
1053       //     [2] {
1054       //       INTEGER { 32 }
1055       //     }
1056       //   }
1057       // }
1058       {{0x30, 0x41, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
1059         0x0a, 0x30, 0x34, 0xa0, 0x0f, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
1060         0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0xa1, 0x1c, 0x30, 0x1a,
1061         0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x08, 0x30,
1062         0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
1063         0x05, 0x00, 0xa2, 0x03, 0x02, 0x01, 0x20},
1064        SignatureAlgorithm::kRsaPssSha256},
1065       // SEQUENCE {
1066       //   # rsassa-pss
1067       //   OBJECT_IDENTIFIER { 1.2.840.113549.1.1.10 }
1068       //   SEQUENCE {
1069       //     [0] {
1070       //       SEQUENCE {
1071       //         # sha384
1072       //         OBJECT_IDENTIFIER { 2.16.840.1.101.3.4.2.2 }
1073       //         NULL {}
1074       //       }
1075       //     }
1076       //     [1] {
1077       //       SEQUENCE {
1078       //         # mgf1
1079       //         OBJECT_IDENTIFIER { 1.2.840.113549.1.1.8 }
1080       //         SEQUENCE {
1081       //           # sha384
1082       //           OBJECT_IDENTIFIER { 2.16.840.1.101.3.4.2.2 }
1083       //           NULL {}
1084       //         }
1085       //       }
1086       //     }
1087       //     [2] {
1088       //       INTEGER { 48 }
1089       //     }
1090       //   }
1091       // }
1092       {{0x30, 0x41, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
1093         0x0a, 0x30, 0x34, 0xa0, 0x0f, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
1094         0x01, 0x65, 0x03, 0x04, 0x02, 0x02, 0x05, 0x00, 0xa1, 0x1c, 0x30, 0x1a,
1095         0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x08, 0x30,
1096         0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02,
1097         0x05, 0x00, 0xa2, 0x03, 0x02, 0x01, 0x30},
1098        SignatureAlgorithm::kRsaPssSha384},
1099       // SEQUENCE {
1100       //   # rsassa-pss
1101       //   OBJECT_IDENTIFIER { 1.2.840.113549.1.1.10 }
1102       //   SEQUENCE {
1103       //     [0] {
1104       //       SEQUENCE {
1105       //         # sha512
1106       //         OBJECT_IDENTIFIER { 2.16.840.1.101.3.4.2.3 }
1107       //         NULL {}
1108       //       }
1109       //     }
1110       //     [1] {
1111       //       SEQUENCE {
1112       //         # mgf1
1113       //         OBJECT_IDENTIFIER { 1.2.840.113549.1.1.8 }
1114       //         SEQUENCE {
1115       //           # sha512
1116       //           OBJECT_IDENTIFIER { 2.16.840.1.101.3.4.2.3 }
1117       //           NULL {}
1118       //         }
1119       //       }
1120       //     }
1121       //     [2] {
1122       //       INTEGER { 64 }
1123       //     }
1124       //   }
1125       // }
1126       {{0x30, 0x41, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
1127         0x0a, 0x30, 0x34, 0xa0, 0x0f, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
1128         0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0xa1, 0x1c, 0x30, 0x1a,
1129         0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x08, 0x30,
1130         0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03,
1131         0x05, 0x00, 0xa2, 0x03, 0x02, 0x01, 0x40},
1132        SignatureAlgorithm::kRsaPssSha512},
1133 
1134       // The same inputs as above, but the NULLs in the digest algorithms are
1135       // omitted.
1136       {{0x30, 0x3d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01,
1137         0x01, 0x0a, 0x30, 0x30, 0xa0, 0x0d, 0x30, 0x0b, 0x06, 0x09, 0x60,
1138         0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0xa1, 0x1a, 0x30,
1139         0x18, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
1140         0x08, 0x30, 0x0b, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
1141         0x04, 0x02, 0x01, 0xa2, 0x03, 0x02, 0x01, 0x20},
1142        SignatureAlgorithm::kRsaPssSha256},
1143       {{0x30, 0x3d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01,
1144         0x01, 0x0a, 0x30, 0x30, 0xa0, 0x0d, 0x30, 0x0b, 0x06, 0x09, 0x60,
1145         0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, 0xa1, 0x1a, 0x30,
1146         0x18, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
1147         0x08, 0x30, 0x0b, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
1148         0x04, 0x02, 0x02, 0xa2, 0x03, 0x02, 0x01, 0x30},
1149        SignatureAlgorithm::kRsaPssSha384},
1150       {{0x30, 0x3d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01,
1151         0x01, 0x0a, 0x30, 0x30, 0xa0, 0x0d, 0x30, 0x0b, 0x06, 0x09, 0x60,
1152         0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0xa1, 0x1a, 0x30,
1153         0x18, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
1154         0x08, 0x30, 0x0b, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
1155         0x04, 0x02, 0x03, 0xa2, 0x03, 0x02, 0x01, 0x40},
1156        SignatureAlgorithm::kRsaPssSha512}};
1157   for (const auto& t : kValidTests) {
1158     EXPECT_EQ(ParseSignatureAlgorithm(der::Input(t.data)), t.expected);
1159   }
1160 
1161   struct {
1162     std::vector<uint8_t> data;
1163   } kInvalidTests[] = {
1164       // SEQUENCE {
1165       //   # rsassa-pss
1166       //   OBJECT_IDENTIFIER { 1.2.840.113549.1.1.10 }
1167       //   SEQUENCE {
1168       //     [0] {
1169       //       SEQUENCE {
1170       //         # sha256
1171       //         OBJECT_IDENTIFIER { 2.16.840.1.101.3.4.2.1 }
1172       //         NULL {}
1173       //       }
1174       //     }
1175       //     [1] {
1176       //       SEQUENCE {
1177       //         # mgf1
1178       //         OBJECT_IDENTIFIER { 1.2.840.113549.1.1.8 }
1179       //         SEQUENCE {
1180       //           # sha384
1181       //           OBJECT_IDENTIFIER { 2.16.840.1.101.3.4.2.2 }
1182       //           NULL {}
1183       //         }
1184       //       }
1185       //     }
1186       //   }
1187       // }
1188       {{0x30, 0x3c, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01,
1189         0x01, 0x0a, 0x30, 0x2f, 0xa0, 0x0f, 0x30, 0x0d, 0x06, 0x09, 0x60,
1190         0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0xa1,
1191         0x1c, 0x30, 0x1a, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
1192         0x01, 0x01, 0x08, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
1193         0x65, 0x03, 0x04, 0x02, 0x02, 0x05, 0x00}},
1194       // SEQUENCE {
1195       //   # rsassa-pss
1196       //   OBJECT_IDENTIFIER { 1.2.840.113549.1.1.10 }
1197       //   SEQUENCE {
1198       //     [0] {
1199       //       SEQUENCE {
1200       //         # md5
1201       //         OBJECT_IDENTIFIER { 1.2.840.113549.2.5 }
1202       //         NULL {}
1203       //       }
1204       //     }
1205       //     [1] {
1206       //       SEQUENCE {
1207       //         # mgf1
1208       //         OBJECT_IDENTIFIER { 1.2.840.113549.1.1.8 }
1209       //         SEQUENCE {
1210       //           # md5
1211       //           OBJECT_IDENTIFIER { 1.2.840.113549.2.5 }
1212       //           NULL {}
1213       //         }
1214       //       }
1215       //     }
1216       //     [2] {
1217       //       INTEGER { 16 }
1218       //     }
1219       //   }
1220       // }
1221       {{0x30, 0x3f, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01,
1222         0x01, 0x0a, 0x30, 0x32, 0xa0, 0x0e, 0x30, 0x0c, 0x06, 0x08, 0x2a,
1223         0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0xa1, 0x1b,
1224         0x30, 0x19, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01,
1225         0x01, 0x08, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48, 0x86, 0xf7,
1226         0x0d, 0x02, 0x05, 0x05, 0x00, 0xa2, 0x03, 0x02, 0x01, 0x10}},
1227       // SEQUENCE {
1228       //   # rsassa-pss
1229       //   OBJECT_IDENTIFIER { 1.2.840.113549.1.1.10 }
1230       //   # SHA-1 with salt length 20 is the default.
1231       //   SEQUENCE {}
1232       // }
1233       {{0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
1234         0x0a, 0x30, 0x00}},
1235       // SEQUENCE {
1236       //   # rsassa-pss
1237       //   OBJECT_IDENTIFIER { 1.2.840.113549.1.1.10 }
1238       //   SEQUENCE {
1239       //     [2] {
1240       //       INTEGER { 21 }
1241       //     }
1242       //   }
1243       // }
1244       {{0x30, 0x12, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
1245         0x01, 0x01, 0x0a, 0x30, 0x05, 0xa2, 0x03, 0x02, 0x01, 0x15}},
1246       // SEQUENCE {
1247       //   # rsassa-pss
1248       //   OBJECT_IDENTIFIER { 1.2.840.113549.1.1.10 }
1249       //   SEQUENCE {
1250       //     [0] {
1251       //       SEQUENCE {
1252       //         # sha256
1253       //         OBJECT_IDENTIFIER { 2.16.840.1.101.3.4.2.1 }
1254       //         NULL {}
1255       //       }
1256       //     }
1257       //     [1] {
1258       //       SEQUENCE {
1259       //         # mgf1
1260       //         OBJECT_IDENTIFIER { 1.2.840.113549.1.1.8 }
1261       //         SEQUENCE {
1262       //           # sha256
1263       //           OBJECT_IDENTIFIER { 2.16.840.1.101.3.4.2.1 }
1264       //           NULL {}
1265       //         }
1266       //       }
1267       //     }
1268       //     [2] {
1269       //       INTEGER { 33 }
1270       //     }
1271       //   }
1272       // }
1273       {{0x30, 0x41, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
1274         0x0a, 0x30, 0x34, 0xa0, 0x0f, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
1275         0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0xa1, 0x1c, 0x30, 0x1a,
1276         0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x08, 0x30,
1277         0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
1278         0x05, 0x00, 0xa2, 0x03, 0x02, 0x01, 0x21}},
1279       // SEQUENCE {
1280       //   # rsassa-pss
1281       //   OBJECT_IDENTIFIER { 1.2.840.113549.1.1.10 }
1282       //   SEQUENCE {
1283       //     [0] {
1284       //       SEQUENCE {
1285       //         # sha384
1286       //         OBJECT_IDENTIFIER { 2.16.840.1.101.3.4.2.2 }
1287       //         NULL {}
1288       //       }
1289       //     }
1290       //     [1] {
1291       //       SEQUENCE {
1292       //         # mgf1
1293       //         OBJECT_IDENTIFIER { 1.2.840.113549.1.1.8 }
1294       //         SEQUENCE {
1295       //           # sha384
1296       //           OBJECT_IDENTIFIER { 2.16.840.1.101.3.4.2.2 }
1297       //           NULL {}
1298       //         }
1299       //       }
1300       //     }
1301       //     [2] {
1302       //       INTEGER { 49 }
1303       //     }
1304       //   }
1305       // }
1306       {{0x30, 0x41, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
1307         0x0a, 0x30, 0x34, 0xa0, 0x0f, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
1308         0x01, 0x65, 0x03, 0x04, 0x02, 0x02, 0x05, 0x00, 0xa1, 0x1c, 0x30, 0x1a,
1309         0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x08, 0x30,
1310         0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02,
1311         0x05, 0x00, 0xa2, 0x03, 0x02, 0x01, 0x31}},
1312 
1313       // SEQUENCE {
1314       //   # rsassa-pss
1315       //   OBJECT_IDENTIFIER { 1.2.840.113549.1.1.10 }
1316       //   SEQUENCE {
1317       //     [0] {
1318       //       SEQUENCE {
1319       //         # sha512
1320       //         OBJECT_IDENTIFIER { 2.16.840.1.101.3.4.2.3 }
1321       //         NULL {}
1322       //       }
1323       //     }
1324       //     [1] {
1325       //       SEQUENCE {
1326       //         # mgf1
1327       //         OBJECT_IDENTIFIER { 1.2.840.113549.1.1.8 }
1328       //         SEQUENCE {
1329       //           # sha512
1330       //           OBJECT_IDENTIFIER { 2.16.840.1.101.3.4.2.3 }
1331       //           NULL {}
1332       //         }
1333       //       }
1334       //     }
1335       //     [2] {
1336       //       INTEGER { 65 }
1337       //     }
1338       //   }
1339       // }
1340       {{0x30, 0x41, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
1341         0x0a, 0x30, 0x34, 0xa0, 0x0f, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
1342         0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0xa1, 0x1c, 0x30, 0x1a,
1343         0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x08, 0x30,
1344         0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03,
1345         0x05, 0x00, 0xa2, 0x03, 0x02, 0x01, 0x41}},
1346   };
1347   for (const auto& t : kInvalidTests) {
1348     EXPECT_FALSE(ParseSignatureAlgorithm(der::Input(t.data)));
1349   }
1350 }
1351 
1352 // Parses a md5WithRSAEncryption which contains a NULL parameters field.
1353 //
1354 //   SEQUENCE (2 elem)
1355 //       OBJECT IDENTIFIER  1.2.840.113549.1.1.4
1356 //       NULL
TEST(SignatureAlgorithmTest,ParseDerMd5WithRsaEncryptionNullParams)1357 TEST(SignatureAlgorithmTest, ParseDerMd5WithRsaEncryptionNullParams) {
1358   // clang-format off
1359   const uint8_t kData[] = {
1360       0x30, 0x0D,  // SEQUENCE (13 bytes)
1361       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
1362       0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x04,
1363       0x05, 0x00,  // NULL (0 bytes)
1364   };
1365   // clang-format on
1366   EXPECT_FALSE(ParseSignatureAlgorithm(der::Input(kData)));
1367 }
1368 
1369 // Parses a md4WithRSAEncryption which contains a NULL parameters field.
1370 //
1371 //   SEQUENCE (2 elem)
1372 //       OBJECT IDENTIFIER  1.2.840.113549.1.1.3
1373 //       NULL
TEST(SignatureAlgorithmTest,ParseDerMd4WithRsaEncryptionNullParams)1374 TEST(SignatureAlgorithmTest, ParseDerMd4WithRsaEncryptionNullParams) {
1375   // clang-format off
1376   const uint8_t kData[] = {
1377       0x30, 0x0D,  // SEQUENCE (13 bytes)
1378       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
1379       0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x03,
1380       0x05, 0x00,  // NULL (0 bytes)
1381   };
1382   // clang-format on
1383   EXPECT_FALSE(ParseSignatureAlgorithm(der::Input(kData)));
1384 }
1385 
1386 // Parses a md2WithRSAEncryption which contains a NULL parameters field.
1387 //
1388 //   SEQUENCE (2 elem)
1389 //       OBJECT IDENTIFIER  1.2.840.113549.1.1.2
1390 //       NULL
TEST(SignatureAlgorithmTest,ParseDerMd2WithRsaEncryptionNullParams)1391 TEST(SignatureAlgorithmTest, ParseDerMd2WithRsaEncryptionNullParams) {
1392   // clang-format off
1393   const uint8_t kData[] = {
1394       0x30, 0x0D,  // SEQUENCE (13 bytes)
1395       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
1396       0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x02,
1397       0x05, 0x00,  // NULL (0 bytes)
1398   };
1399   // clang-format on
1400   EXPECT_FALSE(ParseSignatureAlgorithm(der::Input(kData)));
1401 }
1402 
1403 // Parses a dsaWithSha1 which contains no parameters field.
1404 //
1405 //   SEQUENCE (1 elem)
1406 //       OBJECT IDENTIFIER  1.2.840.10040.4.3
TEST(SignatureAlgorithmTest,ParseDerDsaWithSha1NoParams)1407 TEST(SignatureAlgorithmTest, ParseDerDsaWithSha1NoParams) {
1408   // clang-format off
1409   const uint8_t kData[] = {
1410       0x30, 0x09,  // SEQUENCE (9 bytes)
1411       0x06, 0x07,  // OBJECT IDENTIFIER (7 bytes)
1412       0x2a, 0x86, 0x48, 0xce, 0x38, 0x04, 0x03,
1413   };
1414   // clang-format on
1415   EXPECT_FALSE(ParseSignatureAlgorithm(der::Input(kData)));
1416 }
1417 
1418 // Parses a dsaWithSha1 which contains a NULL parameters field.
1419 //
1420 //   SEQUENCE (2 elem)
1421 //       OBJECT IDENTIFIER  1.2.840.10040.4.3
1422 //       NULL
TEST(SignatureAlgorithmTest,ParseDerDsaWithSha1NullParams)1423 TEST(SignatureAlgorithmTest, ParseDerDsaWithSha1NullParams) {
1424   // clang-format off
1425   const uint8_t kData[] = {
1426       0x30, 0x0B,  // SEQUENCE (9 bytes)
1427       0x06, 0x07,  // OBJECT IDENTIFIER (7 bytes)
1428       0x2a, 0x86, 0x48, 0xce, 0x38, 0x04, 0x03,
1429       0x05, 0x00,  // NULL (0 bytes)
1430   };
1431   // clang-format on
1432   EXPECT_FALSE(ParseSignatureAlgorithm(der::Input(kData)));
1433 }
1434 
1435 // Parses a dsaWithSha256 which contains no parameters field.
1436 //
1437 //   SEQUENCE (1 elem)
1438 //       OBJECT IDENTIFIER  2.16.840.1.101.3.4.3.2
TEST(SignatureAlgorithmTest,ParseDerDsaWithSha256NoParams)1439 TEST(SignatureAlgorithmTest, ParseDerDsaWithSha256NoParams) {
1440   // clang-format off
1441   const uint8_t kData[] = {
1442       0x30, 0x0B,  // SEQUENCE (11 bytes)
1443       0x06, 0x09,  // OBJECT IDENTIFIER (9 bytes)
1444       0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x03, 0x02
1445   };
1446   // clang-format on
1447   EXPECT_FALSE(ParseSignatureAlgorithm(der::Input(kData)));
1448 }
1449 
1450 }  // namespace
1451 
1452 }  // namespace net
1453