-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcert_check.c
1973 lines (1719 loc) · 65 KB
/
cert_check.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* This software was developed by employees of the National Institute of
* Standards and Technology (NIST), an agency of the Federal Government and
* is being made available as a public service. Pursuant to title 17 United
* States Code Section 105, works of NIST employees are not subject to
* copyright protection in the United States. This software may be subject to
* foreign copyright. Permission in the United States and in foreign
* countries, to the extent that NIST may hold copyright, to use, copy,
* modify, create derivative works, and distribute this software and its
* documentation without fee is hereby granted on a non-exclusive basis,
* provided that this notice and disclaimer of warranty appears in all copies.
*
* THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
* EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY
* WARRANTY THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
* FREEDOM FROM INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL
* CONFORM TO THE SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR
* FREE. IN NO EVENT SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT
* LIMITED TO, DIRECT, INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING
* OUT OF, RESULTING FROM, OR IN ANY WAY CONNECTED WITH THIS SOFTWARE,
* WHETHER OR NOT BASED UPON WARRANTY, CONTRACT, TORT, OR OTHERWISE, WHETHER
* OR NOT INJURY WAS SUSTAINED BY PERSONS OR PROPERTY OR OTHERWISE, AND
* WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT OF THE RESULTS OF, OR
* USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
*/
/* This program takes as input a file that contains a DER encoded
* certificate and checks that the certificate is correctly
* encoded according to X.509 and that it was issued in
* conformance with RFC 5280.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "cert_check.h"
#define MAX_INPUT 100000 /* maximum length of the input file */
/* indicates whether the subject field contains an empty SEQUENCE */
int is_subject_empty;
/* signature_oid holds the value of the OID
* in the signature field of TBSCertificate and signature_parameters and
* signature_parameters_length hold the value and length of the parameters
* field the signature field of TBSCertificate. These are to be
* compared against the same fields in the signatureAlgoritm field of Certificate.
*/
char *signature_oid;
unsigned char *signature_parameters;
int signature_parameters_length;
int subjectPublicKeyType;
/* Validate parameters field for RSA-SSAPSS:
* RSASSA-PSS-params ::= SEQUENCE {
* hashAlgorithm [0] HashAlgorithm DEFAULT
* sha1Identifier,
* maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT
* mgf1SHA1Identifier,
* saltLength [2] INTEGER DEFAULT 20,
* trailerField [3] INTEGER DEFAULT 1 }
*/
int validate_RSA_SSAPSS_parameters(unsigned char *parameters, int parameters_len)
{
int tag, length, hashAlgorithm_tag, hashAlgorithm_length, maskGenAlgorithm_tag, maskGenAlgorithm_length, saltLength_value;
unsigned char *hashAlgorithm = NULL;
unsigned char *maskGenAlgorithm = NULL;
unsigned char *saltLength;
char *hashAlgorithm_OID = NULL;
char *maskGenAlgorithm_OID = NULL;
char *maskGenAlgorithm_hashAlgorithm_OID = NULL;
push_error_stack("RSASSA-PSS parameters");
if ((tag = get_tag(¶meters, ¶meters_len)) == -1) {
pop_error_stack();
return(-1);
}
if (tag != 0x30) {
print_error("Error: RSASSA-PSS-params is not encoded as a SEQUENCE.");
pop_error_stack();
return(-1);
}
if ((length = get_length(¶meters, ¶meters_len)) == -1) {
pop_error_stack();
return(-1);
}
if (length != parameters_len) {
print_error("Error: parameters field contains extraneous data.");
pop_error_stack();
return(-1);
}
if (length == 0) {
/* All parameter fields set to DEFAULT values */
pop_error_stack();
return(0);
}
if (*parameters == 0xA0) {
/* hashAlgorithm ::= SEQUENCE { OBJECT IDENTIFIER, parameters } */
if ((tag = get_tag(¶meters, ¶meters_len)) == -1) {
pop_error_stack();
return(-1);
}
if ((length = get_length(¶meters, ¶meters_len)) == -1) {
pop_error_stack();
return(-1);
}
hashAlgorithm = parameters;
parameters += length;
parameters_len -= length;
if ((hashAlgorithm_tag = get_tag(&hashAlgorithm, &length)) == -1) {
pop_error_stack();
return(-1);
}
if ((hashAlgorithm_length = get_length(&hashAlgorithm, &length)) == -1) {
pop_error_stack();
return(-1);
}
if (hashAlgorithm_tag != 0x30) {
print_error("Error: hashAlgorithm field of RSASSA-PSS-params is not encoded as a SEQUENCE.");
pop_error_stack();
return(-1);
}
if (hashAlgorithm_length != length) {
print_error("Error: Encoding of hashAlgorithm field of RSASSA-PSS-params contains extraneous data.");
pop_error_stack();
return(-1);
}
if (get_OID(&hashAlgorithm, &hashAlgorithm_length, &hashAlgorithm_OID) == -1) {
pop_error_stack();
return(-1);
}
if ((strcmp(hashAlgorithm_OID, "1.3.14.3.2.26") == 0) ||
(strcmp(hashAlgorithm_OID, "2.16.840.1.101.3.4.2.1") == 0) ||
(strcmp(hashAlgorithm_OID, "2.16.840.1.101.3.4.2.2") == 0) ||
(strcmp(hashAlgorithm_OID, "2.16.840.1.101.3.4.2.3") == 0) ||
(strcmp(hashAlgorithm_OID, "2.16.840.1.101.3.4.2.4") == 0)) {
/* hashAlgorithm is SHA-1, SHA-224, SHA-256, SHA-384, or SHA-512 */
if (strcmp(hashAlgorithm_OID, "1.3.14.3.2.26") == 0) {
print_error("Error: RSASSA-PSS-params are not DER encoded. DEFAULT value for hashAlgorithm appears in encoding.");
}
if (hashAlgorithm_length == 0) {
/* This is the correct encoding. Parameters should be absent. */
;
} else if ((hashAlgorithm_length == 2) && (*hashAlgorithm == 0x05) && (*(hashAlgorithm+1) == 0x00)) {
print_error("Warning: Parameters field for hashAlgorithm in RSASSA-PSS-params should be absent.");
} else {
print_error("Error: Incorrect value for parameters field for hashAlgorithm in RSASSA-PSS-params.");
pop_error_stack();
return(-1);
}
} else {
print_error("Warning: hashAlgorithm in RSASSA-PSS-params is not recognized.");
}
}
if (hashAlgorithm_OID == NULL) {
hashAlgorithm_OID = (char *)malloc(14);
if (hashAlgorithm_OID == NULL) {
pop_error_stack();
return(-1);
}
strcpy(hashAlgorithm_OID, "1.3.14.3.2.26");
}
if (parameters_len == 0) {
if (strcmp(hashAlgorithm_OID, "1.3.14.3.2.26") != 0) {
print_error("Warning: RFC 4055 recommends using same hash algorithm in both maskGenAlgorithm and hashAlgorithm and saltLength that is same as length of hash value generated by hashAlgorithm.");
}
free(hashAlgorithm_OID);
pop_error_stack();
return(0);
}
if (*parameters == 0xA1) {
if ((tag = get_tag(¶meters, ¶meters_len)) == -1) {
pop_error_stack();
return(-1);
}
if ((length = get_length(¶meters, ¶meters_len)) == -1) {
pop_error_stack();
return(-1);
}
maskGenAlgorithm = parameters;
parameters += length;
parameters_len -= length;
if ((maskGenAlgorithm_tag = get_tag(&maskGenAlgorithm, &length)) == -1) {
pop_error_stack();
return(-1);
}
if ((maskGenAlgorithm_length = get_length(&maskGenAlgorithm, &length)) == -1) {
pop_error_stack();
return(-1);
}
if (maskGenAlgorithm_tag != 0x30) {
print_error("Error: maskGenAlgorithm field of RSASSA-PSS-params is not encoded as a SEQUENCE.");
pop_error_stack();
return(-1);
}
if (maskGenAlgorithm_length != length) {
print_error("Error: Encoding of maskGenAlgorithm field of RSASSA-PSS-params contains extraneous data.");
pop_error_stack();
return(-1);
}
if (get_OID(&maskGenAlgorithm, &maskGenAlgorithm_length, &maskGenAlgorithm_OID) == -1) {
pop_error_stack();
return(-1);
}
if (strcmp(maskGenAlgorithm_OID, "1.2.840.113549.1.1.8") != 0) {
print_error("Warning: Unknown mask generation algorithm.");
} else {
/* mask generation algorithm is MGF1. Parameters field must be a hash function */
if ((tag = get_tag(&maskGenAlgorithm, &maskGenAlgorithm_length)) == -1) {
pop_error_stack();
return(-1);
}
if ((length = get_length(&maskGenAlgorithm, &maskGenAlgorithm_length)) == -1) {
pop_error_stack();
return(-1);
}
if (tag != 0x30) {
print_error("Error: parameters field of maskGenAlgorithm field of RSASSA-PSS-params is not encoded as a SEQUENCE.");
pop_error_stack();
return(-1);
}
if (length != maskGenAlgorithm_length) {
print_error("Error: Encoding of &maskGenAlgorithm field of RSASSA-PSS-params contains extraneous data.");
pop_error_stack();
return(-1);
}
if (get_OID(&maskGenAlgorithm, &maskGenAlgorithm_length, &maskGenAlgorithm_hashAlgorithm_OID) == -1) {
pop_error_stack();
return(-1);
}
if ((strcmp(maskGenAlgorithm_hashAlgorithm_OID, "1.3.14.3.2.26") == 0) ||
(strcmp(maskGenAlgorithm_hashAlgorithm_OID, "2.16.840.1.101.3.4.2.1") == 0) ||
(strcmp(maskGenAlgorithm_hashAlgorithm_OID, "2.16.840.1.101.3.4.2.2") == 0) ||
(strcmp(maskGenAlgorithm_hashAlgorithm_OID, "2.16.840.1.101.3.4.2.3") == 0) ||
(strcmp(maskGenAlgorithm_hashAlgorithm_OID, "2.16.840.1.101.3.4.2.4") == 0)) {
/* hashAlgorithm is SHA-1, SHA-224, SHA-256, SHA-384, or SHA-512 */
if (strcmp(maskGenAlgorithm_hashAlgorithm_OID, "1.3.14.3.2.26") == 0) {
print_error("Error: RSASSA-PSS-params are not DER encoded. DEFAULT value for maskGenAlgorithm appears in encoding.");
}
if (maskGenAlgorithm_length == 0) {
/* This is the correct encoding. Parameters should be absent. */
;
} else if ((maskGenAlgorithm_length == 2) && (*maskGenAlgorithm == 0x05) && (*(maskGenAlgorithm+1) == 0x00)) {
print_error("Warning: Parameters field for hashAlgorithm in maskGenAlgorithm in RSASSA-PSS-params should be absent.");
} else {
print_error("Error: Incorrect value for parameters field for hashAlgorithm in maskGenAlgorithm in RSASSA-PSS-params.");
pop_error_stack();
return(-1);
}
} else {
print_error("Warning: hashAlgorithm in maskGenAlgorithm in RSASSA-PSS-params is not recognized.");
}
if (strcmp(maskGenAlgorithm_hashAlgorithm_OID, hashAlgorithm_OID) != 0) {
print_error("Warning: RFC 4055 recommends using the same hash algorihm for maskGenAlgorithm and hashAlgorithm in RSASSA-PSS-params.");
}
}
}
if (parameters_len == 0) {
if (strcmp(hashAlgorithm_OID, "1.3.14.3.2.26") != 0) {
print_error("Warning: RFC 4055 recommends using a saltLength that is same as length of hash value generated by hashAlgorithm.");
}
if (hashAlgorithm_OID != NULL) free(hashAlgorithm_OID);
if (maskGenAlgorithm_OID != NULL) free(maskGenAlgorithm_OID);
if (maskGenAlgorithm_hashAlgorithm_OID != NULL) free(maskGenAlgorithm_hashAlgorithm_OID);
pop_error_stack();
return(0);
}
if (*parameters == 0xA2) {
if ((tag = get_tag(¶meters, ¶meters_len)) == -1) {
pop_error_stack();
return(-1);
}
if ((length = get_length(¶meters, ¶meters_len)) == -1) {
pop_error_stack();
return(-1);
}
saltLength = parameters;
parameters += length;
parameters_len -= length;
if ((length != 3) || (*saltLength != 0x02) || (*(saltLength+1) != 0x01) || ((*(saltLength+2) & 0x80) != 0)) {
push_error_stack("saltLength");
if (validate_INTEGER(&saltLength, &length, 1) == -1) {
pop_error_stack();
pop_error_stack();
return(-1);
} else {
print_error("Warning: RFC 4055 recommends using a saltLength that is same as length of hash value generated by hashAlgorithm.");
}
pop_error_stack();
} else {
saltLength_value = *(saltLength+2);
length = 0;
if (saltLength_value == 20) {
print_error("Error: RSASSA-PSS-params are not DER encoded. DEFAULT value for saltLength appears in encoding.");
}
if (((strcmp(hashAlgorithm_OID, "1.3.14.3.2.26") == 0) && (saltLength_value != 20)) ||
((strcmp(hashAlgorithm_OID, "2.16.840.1.101.3.4.2.1") == 0) && (saltLength_value != 32)) ||
((strcmp(hashAlgorithm_OID, "2.16.840.1.101.3.4.2.2") == 0) && (saltLength_value != 48)) ||
((strcmp(hashAlgorithm_OID, "2.16.840.1.101.3.4.2.3") == 0) && (saltLength_value != 64)) ||
((strcmp(hashAlgorithm_OID, "2.16.840.1.101.3.4.2.4") == 0) && (saltLength_value != 28))) {
print_error("Warning: RFC 4055 recommends using a saltLength that is same as length of hash value generated by hashAlgorithm.");
}
}
if (length != 0) {
print_error("Error: Encoding of saltLength field of RSASSA-PSS-params contains extraneous data.");
pop_error_stack();
return(-1);
}
}
if (parameters_len == 0) {
if (hashAlgorithm_OID != NULL) free(hashAlgorithm_OID);
if (maskGenAlgorithm_OID != NULL) free(maskGenAlgorithm_OID);
if (maskGenAlgorithm_hashAlgorithm_OID != NULL) free(maskGenAlgorithm_hashAlgorithm_OID);
pop_error_stack();
return(0);
}
/* trailerField */
if ((tag = get_tag(¶meters, ¶meters_len)) == -1) {
pop_error_stack();
return(-1);
}
if ((length = get_length(¶meters, ¶meters_len)) == -1) {
pop_error_stack();
return(-1);
}
if (tag != 0xA4) {
print_error("Error: Unexpected tag in RSASSA-PSS-params.");
pop_error_stack();
return(-1);
}
if (length != parameters_len) {
print_error("Error: Encoding of RSASSA-PSS-params contains extraneous data.");
pop_error_stack();
return(-1);
}
if ((length == 3) && (*parameters == 0x02) && (*(parameters+1) == 0x01) && (*(parameters+2) == 0x01)) {
print_error("Error: RSASSA-PSS-params are not DER encoded. DEFAULT value for trailerField appears in encoding.");
} else {
push_error_stack("trailerField");
if (validate_INTEGER(¶meters, ¶meters_len, 1) == -1) {
pop_error_stack();
pop_error_stack();
return(-1);
}
print_error("Warning: RFC 4055 requires trailerField to have a value of 1.");
pop_error_stack();
}
if (hashAlgorithm_OID != NULL) free(hashAlgorithm_OID);
if (maskGenAlgorithm_OID != NULL) free(maskGenAlgorithm_OID);
if (maskGenAlgorithm_hashAlgorithm_OID != NULL) free(maskGenAlgorithm_hashAlgorithm_OID);
pop_error_stack();
return(0);
}
/* Validate signature (or signatureAlgorithm) field, which
* is of type:
* AlgorithmIdentifier ::= SEQUENCE {
* algorithm OBJECT IDENTIFIER,
* parameters ANY DEFINED BY algorithm OPTIONAL }
*
* where algorithm contains the OID of a signature algorithm and
* parameters contains the corresponding parameters, if any.
* Return -1 if there is an error.
*/
int validate_signatureAlgorithm(unsigned char **input, int *input_len, char **oid, unsigned char **parameters, int *parameters_len)
{
int tag, AlgorithmIdentifier_length;
if ((tag = get_tag(input, input_len)) == -1) {
return(-1);
}
if (tag != 0x30) {
print_error("Error: AlgorithmIdentifier is not encoded as a SEQUENCE.");
return(-1);
}
if ((AlgorithmIdentifier_length = get_length(input, input_len)) == -1) {
return(-1);
}
*input_len -= AlgorithmIdentifier_length;
if (get_OID(input, &AlgorithmIdentifier_length, oid) == -1) {
return(-1);
}
/* get parameters */
if (AlgorithmIdentifier_length == 0) {
*parameters_len = 0;
*parameters = NULL;
} else {
/* Assume what's left are the parameters */
*parameters_len = AlgorithmIdentifier_length;
*parameters = (unsigned char *)malloc(AlgorithmIdentifier_length);
memcpy(*parameters, *input, AlgorithmIdentifier_length);
/* Perform basic sanity check on parameters and verify there is no extraneous data after parameters field. */
if (validate_generic_DER(input, &AlgorithmIdentifier_length) == -1) {
print_error("Error: parameters field of AlgorithmIdentifier is not DER encoded.");
free(*parameters);
return(-1);
}
if (AlgorithmIdentifier_length > 0) {
print_error("Error: AlgorithmIdentifier SEQUENCE contains extraneous data.");
return(-1);
}
}
/* Check that the OID and parameters are a valid combination */
if ((strcmp(*oid, "1.2.840.113549.1.1.2") == 0) ||
(strcmp(*oid, "1.2.840.113549.1.1.3") == 0) ||
(strcmp(*oid, "1.2.840.113549.1.1.4") == 0) ||
(strcmp(*oid, "1.2.840.113549.1.1.5") == 0) ||
(strcmp(*oid, "1.2.840.113549.1.1.11") == 0) ||
(strcmp(*oid, "1.2.840.113549.1.1.12") == 0) ||
(strcmp(*oid, "1.2.840.113549.1.1.13") == 0) ||
(strcmp(*oid, "1.2.840.113549.1.1.14") == 0)) {
/* RSA with MD2, MD4, MD5, SHA-1, SHA-224, SHA-256, SHA-384, or SHA-512 */
/* parameters field must contain NULL */
if ((*parameters_len != 2) || ((*parameters)[0] != 0x05) || ((*parameters)[1] != 0x00)) {
print_error("Error: Parameters field does not contain NULL.");
}
} else if (strcmp(*oid, "1.2.840.113549.1.1.10") == 0) {
/* RSA-SSAPSS */
validate_RSA_SSAPSS_parameters(*parameters, *parameters_len);
} else if ((strcmp(*oid, "1.2.840.10045.4.1") == 0) ||
(strcmp(*oid, "1.2.840.10045.4.3.1") == 0) ||
(strcmp(*oid, "1.2.840.10045.4.3.2") == 0) ||
(strcmp(*oid, "1.2.840.10045.4.3.3") == 0) ||
(strcmp(*oid, "1.2.840.10045.4.3.4") == 0) ||
(strcmp(*oid, "1.2.840.10040.4.3") == 0) ||
(strcmp(*oid, "2.16.840.1.101.3.4.3.1") == 0) ||
(strcmp(*oid, "2.16.840.1.101.3.4.3.2") == 0)) {
/* ECDSA with SHA-1, SHA-224, SHA-256, SHA-384, or SHA-512 or
* DSA with SHA-1, SHA-224, or SHA-256 */
if (*parameters_len != 0) {
print_error("Error: Parameters field must be absent for DSA or ECDSA.");
}
} else if ((strcmp(*oid, "1.3.101.112") == 0) ||
(strcmp(*oid, "1.3.101.113") == 0)) {
/* EdDSA */
if (*parameters_len != 0) {
print_error("Error: Parameters field must be absent for EdDSA.");
}
} else {
print_error("Warning: Unrecognized signature algorithm. Cannot check parameters field.");
}
return(0);
}
/*
* Dss-Parms ::= SEQUENCE {
* p INTEGER,
* q INTEGER,
* g INTEGER }
*/
int validate_DSA_parameters(unsigned char *input, int input_len)
{
int tag, pqg_length;
if ((tag = get_tag(&input, &input_len)) == -1) {
return(-1);
}
if (tag != 0x30) {
print_error("Error: DSA parameters are not encoded as a SEQUENCE.");
return(-1);
}
if ((pqg_length = get_length(&input, &input_len)) == -1) {
return(-1);
}
if (pqg_length != input_len) {
print_error("Error: Length of DSA parameters SEQUENCE is incorrect.");
return(-1);
}
if (validate_INTEGER(&input, &input_len, 1) == -1) {
return(-1);
}
if (validate_INTEGER(&input, &input_len, 1) == -1) {
return(-1);
}
if (validate_INTEGER(&input, &input_len, 1) == -1) {
return(-1);
}
if (input_len != 0) {
print_error("Error: parameters field contains extraneous data.");
return(-1);
}
return(0);
}
/*
* RSAPublicKey ::= SEQUENCE {
* modulus INTEGER, -- n
* publicExponent INTEGER } -- e
*/
int validate_RSAPublicKey(unsigned char *input, int input_len)
{
int tag, length;
push_error_stack("RSAPublicKey");
if ((tag = get_tag(&input, &input_len)) == -1) {
pop_error_stack();
return(-1);
}
if (tag != 0x30) {
print_error("RSAPublicKey is not encoded as a SEQUENCE.");
pop_error_stack();
return(-1);
}
if ((length = get_length(&input, &input_len)) == -1) {
pop_error_stack();
return(-1);
}
if (length != input_len) {
print_error("Error: Encoding of subjectPublicKey BITSTRING contains extraneous data.");
pop_error_stack();
return(-1);
}
if (validate_INTEGER(&input, &input_len, 1) == -1) {
pop_error_stack();
return(-1);
}
if (validate_INTEGER(&input, &input_len, 1) == -1) {
pop_error_stack();
return(-1);
}
if (input_len > 0) {
print_error("Error: Encoding of RSAPublicKey SEQUENCE contains extraneous data.");
pop_error_stack();
return(-1);
}
pop_error_stack();
return(0);
}
/*
* SubjectPublicKeyInfo ::= SEQUENCE {
* algorithm AlgorithmIdentifier,
* subjectPublicKey BIT STRING }
*/
int validate_subjectPublicKeyInfo(unsigned char **input, int *input_len)
{
int tag, length, AlgorithmIdentifier_length, subjectPublicKeyInfo_length, parameters_len, subjectPublicKey_length;
unsigned char *parameters, *parameters_contents;
char *oid, *namedCurve_oid;
if ((tag = get_tag(input, input_len)) == -1) {
return(-1);
}
if (tag != 0x30) {
print_error("AlgorithmIdentifier is not encoded as a SEQUENCE.");
return(-1);
}
if ((subjectPublicKeyInfo_length = get_length(input, input_len)) == -1) {
return(-1);
}
*input_len -= subjectPublicKeyInfo_length;
/* algorithm is a AlgorithmIdentifier */
if ((tag = get_tag(input, &subjectPublicKeyInfo_length)) == -1) {
return(-1);
}
if (tag != 0x30) {
print_error("AlgorithmIdentifier is not encoded as a SEQUENCE.");
}
if ((AlgorithmIdentifier_length = get_length(input, &subjectPublicKeyInfo_length)) == -1) {
return(-1);
}
subjectPublicKeyInfo_length -= AlgorithmIdentifier_length;
if (get_OID(input, &AlgorithmIdentifier_length, &oid) == -1) {
return(-1);
}
/* get parameters */
if (AlgorithmIdentifier_length == 0) {
parameters_len = 0;
parameters = NULL;
} else {
/* Assume what's left are the parameters */
parameters_len = AlgorithmIdentifier_length;
parameters = (unsigned char *)malloc(AlgorithmIdentifier_length);
memcpy(parameters, *input, AlgorithmIdentifier_length);
if ((tag = get_tag(input, &AlgorithmIdentifier_length)) == -1) {
return(-1);
}
if ((length = get_length(input, &AlgorithmIdentifier_length)) == -1) {
return(-1);
}
if (length != AlgorithmIdentifier_length) {
print_error("Error: AlgorithmIdentifier SEQUENCE contains extraneous data.");
return(-1);
}
*input += length;
}
if (strcmp(oid, "1.2.840.113549.1.1.1") == 0) {
subjectPublicKeyType = SPK_RSA;
} else if (strcmp(oid, "1.2.840.10045.2.1") == 0) {
subjectPublicKeyType = SPK_ECC;
} else if (strcmp(oid, "1.3.132.1.12") == 0) {
subjectPublicKeyType = SPK_ECDH;
} else if (strcmp(oid, "1.3.132.1.13") == 0) {
subjectPublicKeyType = SPK_ECMQV;
} else if (strcmp(oid, "1.2.840.10040.4.1") == 0) {
subjectPublicKeyType = SPK_DSA;
} else if (strcmp(oid, "1.3.101.110") == 0) {
subjectPublicKeyType = SPK_X25519;
} else if (strcmp(oid, "1.3.101.111") == 0) {
subjectPublicKeyType = SPK_X448;
} else if (strcmp(oid, "1.3.101.112") == 0) {
subjectPublicKeyType = SPK_ED25519;
} else if (strcmp(oid, "1.3.101.113") == 0) {
subjectPublicKeyType = SPK_ED448;
} else {
subjectPublicKeyType = SPK_UNKNOWN;
}
/* Check that the OID and parameters are a valid combination */
if (subjectPublicKeyType == SPK_RSA) {
/* RSA subject public key. Parameters must be NULL */
if ((parameters_len != 2) || (parameters[0] != 0x05) || (parameters[1] != 0x00)) {
print_error("Incorrect or missing parameters for RSA subject public key.");
return(-1);
}
} else if ((subjectPublicKeyType == SPK_ECC) || (subjectPublicKeyType == SPK_ECDH) ||
(subjectPublicKeyType == SPK_ECMQV)) {
/* Elliptic curve subject public key - id-ecPublicKey, id-id-ecDH, or id-ecMQV */
if (parameters_len == 0) {
print_error("ERROR: Parameters field absent for Elliptic Curve subject public key.");
return(-1);
}
tag = *parameters;
if (tag == 0x06) {
/* namedCurve */
parameters_contents = parameters;
if (get_OID(¶meters_contents, ¶meters_len, &namedCurve_oid) == -1) {
print_error("ERROR: Parameters field contains invalid value.");
return(-1);
}
free(namedCurve_oid);
} else if (tag == 0x05) {
/* implicitCurve */
if (parameters_len != 2) {
print_error("ERROR: Parameters field contains invalid value.");
return(-1);
}
if (*(parameters + 1) == 0)
print_error("Warning: CAs conforming to PKIX profile MUST NOT encode parameters as implicitCurve.");
else
print_error("ERROR: Parameters field contains invalid value.");
} else if (tag == 0x30) {
/* specifiedCurve */
print_error("Warning: CAs conforming to PKIX profile MUST NOT encode parameters as specifiedCurve. Contents of parameters field not checked.");
parameters_contents = parameters;
if (validate_generic_DER(¶meters_contents, ¶meters_len) == -1) {
return(-1);
}
}
} else if (subjectPublicKeyType == SPK_DSA) {
/* DSA */
if (parameters_len != 0) {
/* parameters are not inherited */
push_error_stack("DSA parameters");
validate_DSA_parameters(parameters, parameters_len);
pop_error_stack();
}
} else if ( (subjectPublicKeyType == SPK_X25519) || (subjectPublicKeyType == SPK_X448) ||
(subjectPublicKeyType == SPK_ED25519) || (subjectPublicKeyType == SPK_ED448) ) {
if (parameters_len != 0) {
print_error("ERROR: Parameters field must be absent for X25519, X448, ED25519, and ED448.");
return(-1);
}
} else {
print_error("Warning: Unrecognized subject public key algorithm. Cannot check parameters field.");
parameters_contents = parameters;
if ((parameters_len != 0 ) && (validate_generic_DER(¶meters_contents, ¶meters_len) == -1)) {
return(-1);
}
}
free(oid);
free(parameters);
push_error_stack("subjectPublicKey");
if ((tag = get_tag(input, &subjectPublicKeyInfo_length)) == -1) {
pop_error_stack();
return(-1);
}
if (tag != 0x03) {
print_error("subjectPublicKey is not encoded as a BIT STRING.");
}
if ((subjectPublicKey_length = get_length(input, &subjectPublicKeyInfo_length)) == -1) {
pop_error_stack();
return(-1);
}
if (subjectPublicKey_length != subjectPublicKeyInfo_length) {
print_error("Error: subjectPublicKeyInfo contains extraneous data.");
pop_error_stack();
return(-1);
}
if (validate_BITSTRING(subjectPublicKey_length, *input, 0) == -1) {
pop_error_stack();
return(-1);
}
/* TODO: Check contents of subjectPublicKey. Just skip over for now. */
if (subjectPublicKeyType == SPK_RSA) {
if (**input != 0) {
print_error("Error: Number of unused bits in subjectPublicKey BIT STRING must be 0.");
pop_error_stack();
return(-1);
}
*input += 1;
subjectPublicKey_length -= 1;
if (validate_RSAPublicKey(*input, subjectPublicKey_length) == -1) {
pop_error_stack();
return(-1);
}
} else if (subjectPublicKeyType == SPK_DSA) {
if (**input != 0) {
print_error("Error: Number of unused bits in subjectPublicKey BIT STRING must be 0.");
pop_error_stack();
return(-1);
}
*input += 1;
subjectPublicKey_length -= 1;
if (validate_INTEGER(input, &subjectPublicKey_length, 1) == -1) {
pop_error_stack();
return(-1);
}
if (subjectPublicKey_length > 0) {
print_error("Error: subjectPublicKey BIT STRING contains extraneous data.");
pop_error_stack();
return(-1);
}
} else if ((subjectPublicKeyType == SPK_ECC) || (subjectPublicKeyType == SPK_ECDH) || (subjectPublicKeyType == SPK_ECMQV)) {
if (**input != 0) {
print_error("Error: Number of unused bits in subjectPublicKey BIT STRING must be 0.");
pop_error_stack();
return(-1);
}
*input += 1;
subjectPublicKey_length -= 1;
if ((**input != 0x02) && (**input != 0x03) && (**input != 0x04)) {
print_error("Error: The first octet of the ECC subjectPublicKey must be 0x02, 0x03, or 0x04 (RFC 5480, Section 2.2).");
pop_error_stack();
return(-1);
}
} else if ((subjectPublicKeyType == SPK_X25519) || (subjectPublicKeyType == SPK_X448) ||
(subjectPublicKeyType == SPK_ED25519) || (subjectPublicKeyType == SPK_ED448)) {
if (**input != 0) {
print_error("Error: Number of unused bits in subjectPublicKey BIT STRING must be 0.");
pop_error_stack();
return(-1);
}
}
*input += subjectPublicKey_length;
pop_error_stack();
return(0);
}
/*
* TBSCertificate ::= SEQUENCE {
* version [0] EXPLICIT Version DEFAULT v1,
* serialNumber CertificateSerialNumber,
* signature AlgorithmIdentifier,
* issuer Name,
* validity Validity,
* subject Name,
* subjectPublicKeyInfo SubjectPublicKeyInfo,
* issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
* -- If present, version MUST be v2 or v3
* subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
* -- If present, version MUST be v2 or v3
* extensions [3] EXPLICIT Extensions OPTIONAL
* -- If present, version MUST be v3
* }
*/
int validate_TBSCertificate(unsigned char **input, int *input_len)
{
int tag, TBSCertificate_length, validity_length, length, version, UniqueID_length;
int extensions_length, is_issuer_empty;
push_error_stack("TBSCertificate");
if ((tag = get_tag(input, input_len)) == -1) {
pop_error_stack();
return(-1);
}
if (tag != 0x30) {
print_error("TBSCertificate is not encoded as a SEQUENCE.");
}
if ((TBSCertificate_length = get_length(input, input_len)) == -1) {
pop_error_stack();
return(-1);
}
*input_len -= TBSCertificate_length;
if (TBSCertificate_length == 0) {
print_error("ERROR: TBSCertificate is empty.");
return(-1);
}
/* version */
if (**input == 0xA0) {
if ((tag = get_tag(input, &TBSCertificate_length)) == -1) {
pop_error_stack();
return(-1);
}
if (tag != 0xA0) {
print_error("Incorrect tag encountered where either Version (tag A0) or serialNumber (tag 02) should be.");
pop_error_stack();
return(-1);
}
/* parse version */
if ((length = get_length(input, &TBSCertificate_length)) == -1) {
pop_error_stack();
return(-1);
}
if (length != 3) {
print_error("Incorrect length for encoding of version after tag [1].");
pop_error_stack();
return(-1);
}
/* next should be the tag for INTEGER */
if ((tag = get_tag(input, &TBSCertificate_length)) == -1) {
pop_error_stack();
return(-1);
}
if (tag != 0x02) {
print_error("Version not encoded as an INTEGER.");
pop_error_stack();
return(-1);
}
if ((length = get_length(input, &TBSCertificate_length)) == -1) {
pop_error_stack();
return(-1);
}
if (length != 1) {
print_error("Version should be encoded as an INTEGER of length 1.");
pop_error_stack();
return(-1);
}
if (**input == 0) {
print_error("For Version, default value of v1 MUST NOT appear in DER encoding.");
pop_error_stack();
return(-1);
} else if (**input > 2) {
print_error("Invalid value for Version.");
pop_error_stack();
return(-1);
}
version = **input + 1;
*input += 1;
TBSCertificate_length -= 1;
} else {
/* assume this is a version 1 certificate. */
version = 1;
}
/* serialNumber */
if ((tag = get_tag(input, &TBSCertificate_length)) == -1) {
pop_error_stack();
return(-1);
}
if (tag != 0x02) {
print_error("Incorrect tag encountered where either Version (tag A0) or serialNumber (tag 02) should be.");
pop_error_stack();
return(-1);
}
if ((length = get_length(input, &TBSCertificate_length)) == -1) {
pop_error_stack();
return(-1);
}
if (length == 0) {
print_error("Error: serial number has a length of zero octets.");
pop_error_stack();
return(-1);
}
if (length > 20) {
print_error("Warning: Serial number longer than 20 octets.");
}
if ((**input & 0x80) != 0) {
print_error("Warning: Serial number is negative.");
}
if ((length > 1) && (**input == 0) && (*(*input + 1) == 0)) {
print_error("Error: serial number is not DER encoded.");
}
*input += length;
TBSCertificate_length -= length;
/* signature */
push_error_stack("signature");
if (validate_signatureAlgorithm(input, &TBSCertificate_length, &signature_oid, &signature_parameters, &signature_parameters_length) == -1) {
pop_error_stack();
return(-1);
}
pop_error_stack();
/* issuer */
push_error_stack("issuer");
if (validate_directoryName(input, &TBSCertificate_length, &is_issuer_empty) == -1) {
pop_error_stack();
return(-1);
}
if (is_issuer_empty) {
print_error("Error: issuer field contains an empty SEQUENCE.");
}
pop_error_stack();