-
Notifications
You must be signed in to change notification settings - Fork 0
/
extensions.c
4544 lines (3775 loc) · 134 KB
/
extensions.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.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "cert_check.h"
static char error_str[1000];
struct KeyUsage_BITSTRING {
int digitalSignature;
int nonRepudiation;
int keyEncipherment;
int dataEncipherment;
int keyAgreement;
int keyCertSign;
int cRLSign;
int encipherOnly;
int decipherOnly;
};
typedef struct KeyUsage_BITSTRING KeyUsage_BITSTRING;
static int isKeyUsagePresent = 0;
static KeyUsage_BITSTRING keyUsage_value;
static int isBasicConstraintsPresent = 0;
static int BasicConstraints_criticality;
static int BasicConstraints_cA = 0;
static int iscertificatePoliciesPresent = 0;
static char *certificatePolicies[100];
static int ispolicyMappingsPresent = 0;
static char *issuerDomainPolicies[100];
static int issubjectAltNamePresent = 0;
/*
* KeyUsage ::= BIT STRING {
* digitalSignature (0),
* nonRepudiation (1), -- recent editions of X.509 have
* -- renamed this bit to contentCommitment
* keyEncipherment (2),
* dataEncipherment (3),
* keyAgreement (4),
* keyCertSign (5),
* cRLSign (6),
* encipherOnly (7),
* decipherOnly (8) }
*/
int validate_keyUsage(unsigned char *input, int input_len, int criticality)
{
int tag, length;
push_error_stack("keyUsage");
if (criticality == 0) {
print_error("Warning: keyUsage should be marked as critical.");
}
if ((tag = get_tag(&input, &input_len)) == -1) {
pop_error_stack();
return(-1);
}
if (tag != 0x03) {
print_error("Error: keyUsage is not encoded as a BIT STRING.");
pop_error_stack();
return(-1);
}
if ((length = get_length(&input, &input_len)) == -1) {
pop_error_stack();
return(-1);
}
if (length != input_len) {
print_error("Encoding of keyUsage extension contains extraneous data.");
pop_error_stack();
return(-1);
}
if (validate_BITSTRING(length, input, 1) == -1) {
pop_error_stack();
return(-1);
}
if (length == 1) {
print_error("Error: keyUsage extension has all bits set to 0.");
pop_error_stack();
return(-1);
}
/* keyUsage BIT STRING can be at most 9 bits long. If it is longer than 8
* bits, the number of unused bits must be 7. */
if ((length > 3) || ((length == 3) && (*input != 7))) {
print_error("Error: BIT STRING in keyUsage extension is too long.");
pop_error_stack();
return(-1);
}
input++;
keyUsage_value.digitalSignature = (*input & 0x80) ? 1 : 0;
keyUsage_value.nonRepudiation = (*input & 0x40) ? 1 : 0;
keyUsage_value.keyEncipherment = (*input & 0x20) ? 1 : 0;
keyUsage_value.dataEncipherment = (*input & 0x10) ? 1 : 0;
keyUsage_value.keyAgreement = (*input & 0x08) ? 1 : 0;
keyUsage_value.keyCertSign = (*input & 0x04) ? 1 : 0;
keyUsage_value.cRLSign = (*input & 0x02) ? 1 : 0;
keyUsage_value.encipherOnly = (*input & 0x01) ? 1 : 0;
if (length == 2)
keyUsage_value.decipherOnly = 0;
else
keyUsage_value.decipherOnly = (*(input+1) & 0x80) ? 1 : 0;
if (subjectPublicKeyType == SPK_RSA) {
/* keyAgreement, encipherOnly, and decipherOnly are not consistent with an RSA public key */
if (keyUsage_value.keyAgreement) {
print_error("Warning: Assertion of the keyAgreement bit is not consistent with an RSA public key.");
}
if (keyUsage_value.encipherOnly) {
print_error("Warning: Assertion of the encipherOnly bit is not consistent with an RSA public key.");
}
if (keyUsage_value.decipherOnly) {
print_error("Warning: Assertion of the decipherOnly bit is not consistent with an RSA public key.");
}
} else if (subjectPublicKeyType == SPK_ECC) {
/* keyEncipherment and dataEncipherment are not consistent with an elliptic curve public key */
if (keyUsage_value.keyEncipherment) {
print_error("Warning: Assertion of the keyEncipherment bit is not consistent with an elliptic curve public key.");
}
if (keyUsage_value.dataEncipherment) {
print_error("Warning: Assertion of the dataEncipherment bit is not consistent with an elliptic curve public key.");
}
if (keyUsage_value.keyAgreement == 0) {
if (keyUsage_value.encipherOnly) {
print_error("Warning: The encipherOnly bit may only be set if the keyAgreement bit is also set.");
}
if (keyUsage_value.decipherOnly) {
print_error("Warning: The decipherOnly bit may only be set if the keyAgreement bit is also set.");
}
}
if (keyUsage_value.encipherOnly && keyUsage_value.decipherOnly) {
print_error("Warning: The encipherOnly and decipherOnly bits must not both be set in the same certificate.");
}
} else if ((subjectPublicKeyType == SPK_ECDH) || (subjectPublicKeyType == SPK_ECMQV) || (subjectPublicKeyType == SPK_X25519) || (subjectPublicKeyType == SPK_X448)) {
/* keyAgreement must be asserted and encipherOnly and decipherOnly may be asserted */
if (!keyUsage_value.keyAgreement) {
print_error("Error: The keyAgreement bit must be asserted when the subject public key is id-ecDH, id-ecMQV, id-X25519, or id-X448.");
}
if (keyUsage_value.digitalSignature) {
print_error("Warning: Assertion of the digitalSignature bit is not consistent with an id-ecDH, id-ecMQV, id-X25519, or id-X448 public key.");
}
if (keyUsage_value.nonRepudiation) {
print_error("Warning: Assertion of the nonRepudiation bit is not consistent with an id-ecDH, id-ecMQV, id-X25519, or id-X448 public key.");
}
if (keyUsage_value.keyEncipherment) {
print_error("Warning: Assertion of the keyEncipherment bit is not consistent with an id-ecDH, id-ecMQV, id-X25519, or id-X448 public key.");
}
if (keyUsage_value.dataEncipherment) {
print_error("Warning: Assertion of the dataEncipherment bit is not consistent with an id-ecDH, id-ecMQV, id-X25519, or id-X448 public key.");
}
if (keyUsage_value.keyCertSign) {
print_error("Warning: Assertion of the keyCertSign bit is not consistent with an id-ecDH, id-ecMQV, id-X25519, or id-X448 public key.");
}
if (keyUsage_value.cRLSign) {
print_error("Warning: Assertion of the cRLSign bit is not consistent with an id-ecDH, id-ecMQV, id-X25519, or id-X448 public key.");
}
if (keyUsage_value.encipherOnly && keyUsage_value.decipherOnly) {
print_error("Warning: The encipherOnly and decipherOnly bits must not both be set in the same certificate.");
}
} else if ((subjectPublicKeyType == SPK_DSA) || (subjectPublicKeyType == SPK_ED25519) || (subjectPublicKeyType == SPK_ED448)) {
/* keyEncipherment, dataEncipherment, keyAgreement, encipherOnly, and decipherOnly are not consistent with a DSA public key */
if (keyUsage_value.keyEncipherment) {
print_error("Warning: Assertion of the keyEncipherment bit is not consistent with a DSA or EdDSA public key.");
}
if (keyUsage_value.dataEncipherment) {
print_error("Warning: Assertion of the dataEncipherment bit is not consistent with a DSA or EdDSA public key.");
}
if (keyUsage_value.keyAgreement) {
print_error("Warning: Assertion of the keyAgreement bit is not consistent with a DSA or EdDSA public key.");
}
if (keyUsage_value.encipherOnly) {
print_error("Warning: Assertion of the encipherOnly bit is not consistent with a DSA or EdDSA public key.");
}
if (keyUsage_value.decipherOnly) {
print_error("Warning: Assertion of the decipherOnly bit is not consistent with a DSA or EdDSA public key.");
}
} else {
print_error("Warning: Unknown subject public key type. Not checking consistency keyUsage extension with key type.");
}
isKeyUsagePresent = 1;
pop_error_stack();
return(0);
}
/*
* BasicConstraints ::= SEQUENCE {
* cA BOOLEAN DEFAULT FALSE,
* pathLenConstraint INTEGER (0..MAX) OPTIONAL }
*/
int validate_basicConstraints(unsigned char *input, int input_len, int criticality)
{
int tag, length, pathLenConstraint_length;
push_error_stack("BasicConstraints");
if ((tag = get_tag(&input, &input_len)) == -1) {
pop_error_stack();
return(-1);
}
if (tag != 0x30) {
print_error("Error: BasicConstraints 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("Encoding of BasicConstraints extension contains extraneous data.");
pop_error_stack();
return(-1);
}
if (length == 0) {
/* cA is FALSE and pathLenConstraint is absent */
pop_error_stack();
return(0);
}
/* If BasicConstraints is not an empty SEQUENCE then cA must be TRUE,
* since pathLenConstraint must be absent is cA is FALSE. */
BasicConstraints_cA = 0;
if (*input == 1) {
if (get_BOOLEAN(&input, &input_len, &BasicConstraints_cA) == -1) {
pop_error_stack();
return(-1);
}
if (BasicConstraints_cA == 0) {
print_error("Error: cA value of FALSE not DER encoded. Default value must not appear in encoding.");
}
}
isBasicConstraintsPresent = 1;
BasicConstraints_criticality = criticality;
if (input_len == 0) {
pop_error_stack();
return(0);
}
if ((input_len > 0) & (BasicConstraints_cA == 0)) {
print_error("Error: pathLenConstraint must be absent is cA is FALSE.");
}
if ((tag = get_tag(&input, &input_len)) == -1) {
pop_error_stack();
return(-1);
}
if (tag != 0x02) {
print_error("Error: pathLenConstraint is not encoded as an INTEGER.");
pop_error_stack();
return(-1);
}
if ((pathLenConstraint_length = get_length(&input, &input_len)) == -1) {
pop_error_stack();
return(-1);
}
if (pathLenConstraint_length != input_len) {
print_error("Error: Encoding of BasicConstraints extension contains extraneous data.");
pop_error_stack();
return(-1);
}
if (pathLenConstraint_length == 0) {
print_error("Error: The pathLenConstraint is encoded as an INTEGER of zero octets.");
pop_error_stack();
return(-1);
}
if ((*input & 0x80) != 0) {
print_error("Error: pathLenConstraint is a negative number.");
pop_error_stack();
return(-1);
}
if ((pathLenConstraint_length > 1) && (*input == 0) && (*(input + 1) == 0)) {
print_error("Error: pathLenConstraint is not DER encoded.");
pop_error_stack();
return(-1);
}
if ((pathLenConstraint_length > 1) && (*input > 20)) {
print_error("Warning: pathLenConstraint is an unusually larger number.");
}
pop_error_stack();
return(0);
}
/*
* SubjectKeyIdentifier ::= KeyIdentifier
*
* KeyIdentifier ::= OCTET STRING
*/
int validate_subjectKeyIdentifier(unsigned char *input, int input_len, int criticality)
{
int tag, length;
push_error_stack("subjectKeyIdentifier");
if (criticality) {
print_error("Error: subjectKeyIdentifier extension must be marked as non-critical.");
}
if ((tag = get_tag(&input, &input_len)) == -1) {
pop_error_stack();
return(-1);
}
if (tag != 0x04) {
print_error("Error: subjectKeyIdentifier is not encoded as an OCTET STRING.");
pop_error_stack();
return(-1);
}
if ((length = get_length(&input, &input_len)) == -1) {
pop_error_stack();
return(-1);
}
if (length != input_len) {
print_error("Encoding of subjectKeyIdentifier extension contains extraneous data.");
pop_error_stack();
return(-1);
}
pop_error_stack();
return(0);
}
/* AuthorityKeyIdentifier ::= SEQUENCE {
* keyIdentifier [0] KeyIdentifier OPTIONAL,
* authorityCertIssuer [1] GeneralNames OPTIONAL,
* authorityCertSerialNumber [2] CertificateSerialNumber OPTIONAL }
*
* KeyIdentifier ::= OCTET STRING
*
* CertificateSerialNumber ::= INTEGER
*/
int validate_authorityKeyIdentifier(unsigned char *input, int input_len, int criticality)
{
int tag, length;
unsigned char *authorityCertIssuer_tag;
push_error_stack("authorityKeyIdentifier");
if (criticality) {
print_error("Error: authorityKeyIdentifier extension must be marked as non-critical.");
}
if ((tag = get_tag(&input, &input_len)) == -1) {
pop_error_stack();
return(-1);
}
if (tag != 0x30) {
print_error("Error: authorityKeyIdentifier 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 authorityKeyIdentifier extension contains extraneous data.");
pop_error_stack();
return(-1);
}
if (input_len == 0) {
print_error("Error: authorityKeyIdentifier extension is an empty sequence.");
pop_error_stack();
return(-1);
}
if (*input == 0x80) {
/* This appears to be the keyIdentifier */
if ((tag = get_tag(&input, &input_len)) == -1) {
pop_error_stack();
return(-1);
}
if ((length = get_length(&input, &input_len)) == -1) {
pop_error_stack();
return(-1);
}
input += length;
input_len -= length;
if (input_len == 0) {
pop_error_stack();
return(0);
}
} else {
print_error("Warning: The PKIX profile requires the presence of the keyIdentifier field.");
}
if (*input == 0x82) {
print_error("Error: authorityCertSerialNumber cannot be present unless authorityCertIssuer is also present.");
} else if (*input != 0xA1) {
print_error("Error: Incorrect tag in authorityKeyIdentifier SEQUENCE.");
pop_error_stack();
return(-1);
} else {
authorityCertIssuer_tag = input;
*authorityCertIssuer_tag = 0x30;
push_error_stack("authorityCertIssuer");
if (validate_GeneralNames(&input, &input_len) == -1) {
pop_error_stack();
pop_error_stack();
return(-1);
}
pop_error_stack();
*authorityCertIssuer_tag = 0xA1;
}
if (input_len == 0) {
print_error("Error: authorityCertIssuer cannot be present unless authorityCertSerialNumber is also present.");
}
if ((tag = get_tag(&input, &input_len)) == -1) {
pop_error_stack();
return(-1);
}
if (tag != 0x82) {
print_error("Error: Incorrect tag in authorityKeyIdentifier 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 authorityKeyIdentifier extension contains extraneous data.");
pop_error_stack();
return(-1);
}
if (length == 0) {
print_error("Error: authorityCertSerialNumber number has a length of zero octets.");
pop_error_stack();
return(-1);
}
if (length > 20) {
print_error("Warning: authorityCertSerialNumber 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.");
}
pop_error_stack();
return(0);
}
/*
* ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
*
* KeyPurposeId ::= OBJECT IDENTIFIER
*/
int validate_extKeyUsage(unsigned char *input, int input_len, int criticality)
{
int tag, length;
char *KeyPurposeId;
push_error_stack("extended key usage");
if ((tag = get_tag(&input, &input_len)) == -1) {
pop_error_stack();
return(-1);
}
if (tag != 0x30) {
print_error("Error: extended key usage 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 extended key usage extension contains extraneous data.");
pop_error_stack();
return(-1);
}
if (input_len == 0) {
print_error("Error: extended key usage extension is an empty sequence.");
}
while (input_len > 0) {
if (get_OID(&input, &input_len, &KeyPurposeId) == -1) {
pop_error_stack();
return(-1);
}
if ((strcmp(KeyPurposeId, "2.5.29.37.0") == 0) && criticality) {
/* anyExtendedKeyUsage is present and extension is marked critical*/
print_error("Warning: The extended key usage extension should not be marked as critical if the anyExtendedKeyUsage KeyPurposeId is present.");
}
free(KeyPurposeId);
}
pop_error_stack();
return(0);
}
/*
* UserNotice ::= SEQUENCE {
* noticeRef NoticeReference OPTIONAL,
* explicitText DisplayText OPTIONAL }
*
* NoticeReference ::= SEQUENCE {
* organization DisplayText,
* noticeNumbers SEQUENCE OF INTEGER }
*/
int validate_UserNotice(unsigned char *input, int input_len)
{
int tag, UserNotice_length, NoticeReference_length, noticeNumbers_length;
push_error_stack("UserNotice");
if ((tag = get_tag(&input, &input_len)) == -1) {
pop_error_stack();
return(-1);
}
if (tag != 0x30) {
print_error("Error: UserNotice is not encoded as a SEQUENCE.");
pop_error_stack();
return(-1);
}
if ((UserNotice_length = get_length(&input, &input_len)) == -1) {
pop_error_stack();
return(-1);
}
if (UserNotice_length != input_len) {
print_error("Error: Encoding of UserNotice contains extraneous data.");
pop_error_stack();
return(-1);
}
if (UserNotice_length == 0) {
print_error("Warning: userNotice qualifier present with both noticeRef and explicitText absent.");
pop_error_stack();
return(0);
}
if (*input == 0x30) {
push_error_stack("noticeRef");
print_error("Warning: CAs conforming to the PKIX Certificate and CRL Profile should not use the noticeRef option.");
if ((tag = get_tag(&input, &UserNotice_length)) == -1) {
pop_error_stack();
pop_error_stack();
return(-1);
}
if ((NoticeReference_length = get_length(&input, &UserNotice_length)) == -1) {
pop_error_stack();
pop_error_stack();
return(-1);
}
UserNotice_length -= NoticeReference_length;
push_error_stack("organization");
if (validate_DisplayText(&input, &NoticeReference_length) == -1) {
pop_error_stack();
pop_error_stack();
pop_error_stack();
return(-1);
}
pop_error_stack();
if (NoticeReference_length > 0) {
if ((tag = get_tag(&input, &NoticeReference_length)) == -1) {
pop_error_stack();
pop_error_stack();
return(-1);
}
if (tag != 0x30) {
print_error("Error: Expecting SEQUENCE tag for noticeNumbers.");
pop_error_stack();
pop_error_stack();
return(-1);
}
if ((noticeNumbers_length = get_length(&input, &NoticeReference_length)) == -1) {
pop_error_stack();
pop_error_stack();
return(-1);
}
if (noticeNumbers_length != NoticeReference_length) {
print_error("Error: Extraneous data in NoticeReference SEQUENCE.");
pop_error_stack();
pop_error_stack();
return(-1);
}
push_error_stack("noticeNumbers");
while (noticeNumbers_length > 0) {
if (validate_INTEGER(&input, ¬iceNumbers_length, 0) == -1) {
pop_error_stack();
pop_error_stack();
pop_error_stack();
return(-1);
}
}
pop_error_stack();
} else {
print_error("Warning: noticeRef doesn't include any noticeNumbers.");
}
pop_error_stack();
}
if (UserNotice_length == 0) {
/* explicitText is absent */
pop_error_stack();
return(0);
}
push_error_stack("explicitText");
if (validate_DisplayText(&input, &UserNotice_length) == -1) {
pop_error_stack();
pop_error_stack();
return(-1);
}
pop_error_stack();
if (UserNotice_length > 0) {
print_error("Error: Extraneous data in UserNotice SEQUENCE.");
pop_error_stack();
return(-1);
}
pop_error_stack();
return(0);
}
/*
* policyQualifiers SEQUENCE SIZE (1..MAX) OF PolicyQualifierInfo
*
* PolicyQualifierInfo ::= SEQUENCE {
* policyQualifierId PolicyQualifierId,
* qualifier ANY DEFINED BY policyQualifierId }
*
* PolicyQualifierId ::= OBJECT IDENTIFIER
*/
int validate_policyQualifiers(unsigned char *input, int input_len, int is_anyPolicy)
{
int tag, length, PolicyQualifierInfo_length, qualifier_length;
char *policyQualifierId;
unsigned char *userNotice;
push_error_stack("policyQualifiers");
if ((tag = get_tag(&input, &input_len)) == -1) {
pop_error_stack();
return(-1);
}
if (tag != 0x30) {
print_error("Error: policyQualifiers 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 policyQualifiers field contains extraneous data.");
pop_error_stack();
return(-1);
}
if (input_len == 0) {
print_error("Error: policyQualifiers field is an empty sequence.");
}
while (input_len > 0) {
if ((tag = get_tag(&input, &input_len)) == -1) {
pop_error_stack();
return(-1);
}
if (tag != 0x30) {
print_error("Error: PolicyQualifierInfo is not encoded as a SEQUENCE.");
pop_error_stack();
return(-1);
}
if ((PolicyQualifierInfo_length = get_length(&input, &input_len)) == -1) {
pop_error_stack();
return(-1);
}
input_len -= PolicyQualifierInfo_length;
push_error_stack("PolicyQualifierInfo");
if (get_OID(&input, &PolicyQualifierInfo_length, &policyQualifierId) == -1) {
pop_error_stack();
pop_error_stack();
return(-1);
}
if (strcmp(policyQualifierId, "1.3.6.1.5.5.7.2.1") == 0) {
/* cPSuri */
push_error_stack("cPSuri");
if ((tag = get_tag(&input, &PolicyQualifierInfo_length)) == -1) {
pop_error_stack();
pop_error_stack();
pop_error_stack();
free(policyQualifierId);
return(-1);
}
if (tag != 22) {
print_error("Error: cPSuri is not encoded as an IA5String.");
pop_error_stack();
pop_error_stack();
pop_error_stack();
free(policyQualifierId);
return(-1);
}
if ((qualifier_length = get_length(&input, &PolicyQualifierInfo_length)) == -1) {
pop_error_stack();
pop_error_stack();
pop_error_stack();
free(policyQualifierId);
return(-1);
}
if (qualifier_length != PolicyQualifierInfo_length) {
print_error("Error: Encoding of PolicyQualifierInfo contains extraneous data.");
pop_error_stack();
pop_error_stack();
pop_error_stack();
free(policyQualifierId);
return(-1);
}
validate_URI(input, qualifier_length);
input += qualifier_length;
pop_error_stack();
} else if (strcmp(policyQualifierId, "1.3.6.1.5.5.7.2.2") == 0) {
/* userNotice */
userNotice = input;
input += PolicyQualifierInfo_length;
if (validate_UserNotice(userNotice, PolicyQualifierInfo_length) == -1) {
pop_error_stack();
pop_error_stack();
free(policyQualifierId);
return(-1);
}
} else {
sprintf(error_str, "Warning: Certificate contains a policy qualifier other than cPSuri or userNotice: %s.", policyQualifierId);
print_error(error_str);
if (is_anyPolicy) {
print_error("Warning: The PKIX Certificate and CRL Profile forbids associating any policy qualifiers with anyPolicy other than cPSuri and userNotice.");
}
if ((tag = get_tag(&input, &PolicyQualifierInfo_length)) == -1) {
pop_error_stack();
pop_error_stack();
free(policyQualifierId);
return(-1);
}
if ((qualifier_length = get_length(&input, &PolicyQualifierInfo_length)) == -1) {
pop_error_stack();
pop_error_stack();
free(policyQualifierId);
return(-1);
}
if (qualifier_length != PolicyQualifierInfo_length) {
print_error("Error: Encoding of PolicyQualifierInfo contains extraneous data.");
pop_error_stack();
pop_error_stack();
free(policyQualifierId);
return(-1);
}
input += qualifier_length;
}
free(policyQualifierId);
pop_error_stack();
}
pop_error_stack();
return(0);
}
/* certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
*
* PolicyInformation ::= SEQUENCE {
* policyIdentifier CertPolicyId,
* policyQualifiers SEQUENCE SIZE (1..MAX) OF PolicyQualifierInfo OPTIONAL }
*
* CertPolicyId ::= OBJECT IDENTIFIER
*/
int validate_certificatePolicies(unsigned char *input, int input_len)
{
int tag, length, PolicyInformation_length, is_anyPolicy, i, num_policies;
unsigned char *policyQualifiers;
push_error_stack("certificatePolicies");
for(i = 0; i < 100; i++)
certificatePolicies[i] = NULL;
if ((tag = get_tag(&input, &input_len)) == -1) {
pop_error_stack();
return(-1);
}
if (tag != 0x30) {
print_error("Error: certificatePolicies 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 certificatePolicies extension contains extraneous data.");
pop_error_stack();
return(-1);
}
if (input_len == 0) {
print_error("Error: certificatePolicies extension is an empty sequence.");
}
num_policies = 0;
while (input_len > 0) {
if (num_policies == 100) {
print_error("Error: certificatePolicies extension lists more than 100 policy OIDs.");
pop_error_stack();
return(-1);
}
if ((tag = get_tag(&input, &input_len)) == -1) {
pop_error_stack();
return(-1);
}
if (tag != 0x30) {
print_error("Error: PolicyInformation is not encoded as a SEQUENCE.");
pop_error_stack();
return(-1);
}
if ((PolicyInformation_length = get_length(&input, &input_len)) == -1) {
pop_error_stack();
return(-1);
}
input_len -= PolicyInformation_length;
push_error_stack("PolicyInformation");
if (get_OID(&input, &PolicyInformation_length, &certificatePolicies[num_policies]) == -1) {
pop_error_stack();
pop_error_stack();
return(-1);
}
pop_error_stack();
sprintf(error_str, "PolicyInformation: %s", certificatePolicies[num_policies]);
push_error_stack(error_str);
is_anyPolicy = (strcmp(certificatePolicies[num_policies], "2.5.29.32.0") == 0);
/* Check that the same policy OID does not appear more than once in the extension. */
i = 0;
while (i < num_policies) {
if (strcmp(certificatePolicies[num_policies], certificatePolicies[i]) == 0) {
sprintf(error_str, "Error: policy OID %s appears in the certificatePolicies more than once.", certificatePolicies[num_policies]);
print_error(error_str);
free(certificatePolicies[num_policies]);
num_policies--;
i = num_policies;
} else {
i++;
}
}
num_policies++;
if (PolicyInformation_length > 0) {
policyQualifiers = input;
input += PolicyInformation_length;
if (validate_policyQualifiers(policyQualifiers, PolicyInformation_length, is_anyPolicy) == -1) {
pop_error_stack();
pop_error_stack();
return(-1);
}
}
pop_error_stack();
}
iscertificatePoliciesPresent = 1;
pop_error_stack();
return(0);
}
/*
* PolicyMappings ::= SEQUENCE SIZE (1..MAX) OF SEQUENCE {
* issuerDomainPolicy CertPolicyId,
* subjectDomainPolicy CertPolicyId }
*
* CertPolicyId ::= OBJECT IDENTIFIER
*/
int validate_policyMappings(unsigned char *input, int input_len, int criticality)
{
int tag, length, PolicyMapping_length, num_mappings, i;