-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeneralName.c
1994 lines (1755 loc) · 68.3 KB
/
GeneralName.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 <strings.h>
#include <stdlib.h>
#include "cert_check.h"
static char error_str[1000];
int is_letter(unsigned char c)
{
return(((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')));
}
int is_letter_or_digit(unsigned char c)
{
return(is_letter(c) || ((c >= '0') && (c <= '9')));
}
int is_hexdigit(unsigned char c)
{
return(((c >= '0') && (c <= '9')) || ((c >= 'a') && (c <= 'f')) || ((c >= 'A') && (c <= 'F')));
}
int validate_IA5String(unsigned char *DirectoryString, int DirectoryString_len, int max_len)
{
unsigned char *i;
if (DirectoryString_len > max_len) {
print_error("Warning: IA5String is too long.");
}
for(i=DirectoryString; i < DirectoryString + DirectoryString_len; i++) {
if (*i > 0x7F) {
sprintf(error_str, "Error: IA5String contains invalid character: %d.", *i);
print_error(error_str);
} else if ((*i < 0x20) || (*i == 0x7F)) {
sprintf(error_str, "Warning: IA5String contains a control character: %d.", *i);
print_error(error_str);
}
}
return(0);
}
int validate_domainlabel(unsigned char *domain_label, int domain_label_length)
{
unsigned char *i;
if (domain_label_length == 0) {
print_error("Error: domain label contains an empty string.");
}
if (domain_label_length > 63) {
print_error("Warning: domain label is longer than 63 octets.");
}
/* first character of a domain label must be a letter or digit */
if (!is_letter_or_digit(*domain_label)) {
if ((*domain_label >= 0x20) && (*domain_label <= 0x7E)) {
sprintf(error_str, "Error: First character of domain label is not a valid character: '%c'.", *domain_label);
} else {
sprintf(error_str, "Error: First character of domain label is not a valid character: %d.", *domain_label);
}
print_error(error_str);
}
if (domain_label_length > 1) {
/* final character of a domain label must be a letter or digit */
i = domain_label + domain_label_length - 1;
if (!is_letter_or_digit(*i)) {
if ((*i >= 0x20) && (*i <= 0x7E)) {
sprintf(error_str, "Error: Final character of domain label is not a valid character: '%c'.", *i);
} else {
sprintf(error_str, "Error: Final character of domain label is not a valid character: %d.", *i);
}
print_error(error_str);
}
}
/* All other characters in a domain label are limited to letters, digits, and the hyphen character */
for(i=domain_label + 1; i < domain_label + domain_label_length - 1; i++) {
if (!is_letter_or_digit(*i) && (*i != '-')) {
if ((*i >= 0x20) && (*i <= 0x7E)) {
sprintf(error_str, "Error: domain label contains an invalid character: '%c'.", *i);
} else {
sprintf(error_str, "Error: domain label contains an invalid character: %d.", *i);
}
print_error(error_str);
}
}
return(0);
}
int validate_domainComponent(int tag, unsigned char *domain_label, int domain_label_length)
{
if (tag != 22) {
print_error("Error: Incorrect tag. domainComponent must be encoded as IA5String.");
}
return(validate_domainlabel(domain_label, domain_label_length));
}
/* atext = ALPHA / DIGIT / ; Printable US-ASCII
* "!" / "#" / ; characters not including
* "$" / "%" / ; specials. Used for atoms.
* "&" / "'" /
* "*" / "+" /
* "-" / "/" /
* "=" / "?" /
* "^" / "_" /
* "`" / "{" /
* "|" / "}" /
* "~"
*/
static int is_atext(unsigned char c)
{
return(is_letter_or_digit(c) || (c == '!') || (c == '#') || (c == '$') || (c == '%') ||
(c == '&') || (c == '\'') || (c == '*') || (c == '+') || (c == '-') || (c == '/') ||
(c == '=') || (c == '?') || (c == '^') || (c == '_') || (c == '`') || (c == '{') ||
(c == '|') || (c == '}') || (c == '~'));
}
/*
* Local-part "@" ( Domain / address-literal )
*/
int validate_rfc822Name(unsigned char *rfc822Name, int rfc822Name_length)
{
unsigned char *i;
int domain_length;
/* rfc822Name must begin with a Local-part:
* Local-part = Dot-string / Quoted-string
*/
i = rfc822Name;
if (*i != '\"') {
/* Local-part = Dot-string = Atom *("." Atom)
* Atom = 1*atext */
while ((i < rfc822Name + rfc822Name_length) && (*i != '@')) {
while ((i < rfc822Name + rfc822Name_length) && (*i != '@') && (*i != '.')) {
if (!is_atext(*i)) {
if ((*i >= 0x20) && (*i <= 0x7E)) {
sprintf(error_str, "Error: Local-part of rfc822Name contains an invalid character: '%c'.", *i);
} else {
sprintf(error_str, "Error: Local-part of rfc822Name contains an invalid character: %d.", *i);
}
print_error(error_str);
}
i++;
}
if ((i < rfc822Name + rfc822Name_length) && (*i == '.')) {
/* A "." must be followed by another atom */
i++;
if (i == rfc822Name + rfc822Name_length) {
print_error("Error: rfc822Name does not contain an \"@\" or a domain portion.");
return(-1);
} else if (*i == '@') {
print_error("Error: Local-part of rfc822Name cannot end with a '.'.");
} else if (*i == '.') {
print_error("Error: Local-part of rfc822Name cannot contain two consequtive '.' characters.");
}
}
}
if (i == rfc822Name + rfc822Name_length) {
print_error("Error: rfc822Name does not contain an \"@\" or a domain portion.");
return(-1);
}
} else {
/* Local-part = Quoted-string = DQUOTE *QcontentSMTP DQUOTE
* QcontentSMTP = qtextSMTP / quoted-pairSMTP
* quoted-pairSMTP = %d92 %d32-126
* qtextSMTP = %d32-33 / %d35-91 / %d93-126
*/
i++;
while ((i < rfc822Name + rfc822Name_length) && (*i != '\"')) {
if (*i == 92) {
i++;
if (i == rfc822Name + rfc822Name_length) {
print_error("Error: rfc822Name ends in quoted part of Local-part with an escape character.");
return(-1);
} else if ((*i < 32) || (*i > 126)) {
sprintf(error_str, "Error: Local-part contains an illegal escaped character: %d.", *i);
print_error(error_str);
}
} else if ((*i < 32) || (*i > 126)) {
sprintf(error_str, "Error: Local-part contains an illegal character: %d.", *i);
print_error(error_str);
}
i++;
}
if (i == rfc822Name + rfc822Name_length) {
print_error("Error: rfc822Name does not contain an \"@\" or a domain portion.");
return(-1);
} else if (*i != '@') {
print_error("Error: Local-part of rfc822Name, encoded as a Quoted-string, is not followed by '@'.");
return(-1);
}
}
if (i - rfc822Name > 64) {
print_error("Error: Local-part of rfc822Name is longer than 64 octets.");
}
i++;
if (i == rfc822Name + rfc822Name_length) {
print_error("Error: rfc822Name does not contain a domain portion.");
}
domain_length = rfc822Name + rfc822Name_length - i;
if (domain_length > 255) {
print_error("Error: domain portion of rfc822Name is longer than 255 octets.");
}
if (*i != '[') {
/* Domain = sub-domain *("." sub-domain) */
validate_dNSName(i, domain_length);
} else {
/* address-literal = "[" ( IPv4-address-literal /
* IPv6-address-literal /
* General-address-literal ) "]"
*/
/* TODO */
}
return(0);
}
int validate_UUID(unsigned char *UUID, int UUID_length)
{
unsigned char *i;
if (UUID_length != 36) {
print_error("Error: The length of the UUID URN is incorrect.");
return(-1);
}
if ((*(UUID+8) != '-') || (*(UUID+13) != '-') || (*(UUID+18) != '-') || (*(UUID+23) != '-')) {
print_error("Error: Hyphen characters do not appear in the expected places in the UUID URN.");
return(-1);
}
for(i=UUID; i < UUID + UUID_length; i++) {
if ((i != UUID + 8) && (i != UUID + 13) && (i != UUID + 18) && (i != UUID + 23) && !is_hexdigit(*i)) {
if ((*i >= 0x20) && (*i <= 0x7E)) {
sprintf(error_str, "Error: Unexpected character in UUID URN: '%c'.", *i);
} else {
sprintf(error_str, "Error: Unexpected character in UUID URN: %d.", *i);
}
print_error(error_str);
return(-1);
}
}
return(0);
}
int validate_URN(unsigned char *URN, int URN_length)
{
int NID_length, error_found;
unsigned char *i;
char *NID;
if (URN_length == 0) {
print_error("Error: uniformResourceIdentifier contains a URN with no namespace identifier.");
return(-1);
}
i = URN;
/* URN must begin with a namespace identifier of the form <let-num> [ 1,31<let-num-hyp> ]
* followed by a ':'. */
if (!is_letter_or_digit(*i)) {
error_found = 1;
if ((*i >= 0x20) && (*i <= 0x7E)) {
sprintf(error_str, "Error: Namespace identifier portion of URN contains an invalid character: '%c'. First character of NID must be a letter or digit.", *i);
} else {
sprintf(error_str, "Error: Namespace identifier portion of URN contains an invalid character: %d. First character of NID must be a letter or digit.", *i);
}
print_error(error_str);
}
i++;
while ((i < URN + URN_length) && (*i != ':')) {
if (!is_letter_or_digit(*i) && (*i != '-')) {
error_found = 1;
if ((*i >= 0x20) && (*i <= 0x7E)) {
sprintf(error_str, "Error: Namespace identifier portion of URN contains an invalid character: '%c'.", *i);
} else {
sprintf(error_str, "Error: Namespace identifier portion of URN contains an invalid character: %d.", *i);
}
print_error(error_str);
}
i++;
}
if (i == URN + URN_length) {
print_error("Error: URN does not begin with: NID \":\".");
return(-1);
}
NID_length = i - URN;
if ((NID_length < 2) || (NID_length > 32)) {
print_error("Error: Invalid length for namespace identifier portion of URN.");
}
i++;
NID = (char *)malloc(NID_length + 1);
memcpy(NID, URN, NID_length);
NID[NID_length] = '\0';
if (i == URN + URN_length) {
sprintf(error_str, "Error: The Namespace Specific String (NSS) portion of the %s URN is empty.", NID);
print_error(error_str);
free(NID);
return(-1);
}
/* Perform namespace identifier validations */
if (strcasecmp(NID, "uuid") == 0) {
free(NID);
return(validate_UUID(URN + 5, URN_length - 5));
}
/* check that Namespace Specific String (NSS) portion of URN conforms to the generic RFC 2141 syntax */
while (i < URN + URN_length) {
if (*i == '%') {
/* it has already been verified that the '%' is followed by two hexadecimal digits */
if ( (*(i+1) == '0') && (*(i+2) == '0') ) {
sprintf(error_str, "Warning: The Namespace Specific String (NSS) portion of the %s URN contains a 0 octet (in %%-encoded form).", NID);
print_error(error_str);
}
i += 3;
} else if ((*i == '/') || (*i == '?') || (*i == '#')) {
sprintf(error_str, "Warning: The Namespace Specific String (NSS) portion of the %s URN contains a reserved character in unencoded form: '%c'.", NID, *i);
print_error(error_str);
i++;
} else {
if (!is_letter_or_digit(*i) && (*i != '(') && (*i != ')') && (*i != '+') && (*i != ',') &&
(*i != '-') && (*i != '.') && (*i != ':') && (*i != '=') && (*i != '@') && (*i != ';') &&
(*i != '$') && (*i != '_') && (*i != '!') && (*i != '*') && (*i != '\'') ) {
if ((*i >= 0x20) && (*i <= 0x7E)) {
sprintf(error_str, "Error: The Namespace Specific String (NSS) portion of the %s URN contains an illegal character: '%c'.", NID, *i);
} else {
sprintf(error_str, "Error: The Namespace Specific String (NSS) portion of the %s URN contains an illegal character: %d.", NID, *i);
}
print_error(error_str);
}
i++;
}
}
free(NID);
if (error_found) {
return(-1);
} else {
return(0);
}
}
/*
* gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
*/
static int is_URI_gen_delim(unsigned char c)
{
return((c == ':') || (c == '/') || (c == '?') || (c == '#') || (c == '[') || (c == ']') || (c == '@'));
}
/*
* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
* / "*" / "+" / "," / ";" / "="
*/
static int is_URI_sub_delim(unsigned char c)
{
return((c == '!') || (c == '$') || (c == '&') || (c == '\'') || (c == '(') || (c == ')') ||
(c == '*') || (c == '+') || (c == ',') || (c == ';') || (c == '='));
}
/*
* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
*/
static int is_URI_unreserved(unsigned char c)
{
return(is_letter_or_digit(c) || (c == '-') || (c == '.') || (c == '_') || (c == '~'));
}
/*
* userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
*/
static int validate_URI_userinfo(unsigned char *userinfo, int userinfo_length)
{
unsigned char *i;
for(i=userinfo; i < userinfo + userinfo_length; i++) {
if (!is_URI_unreserved(*i) && (*i != '%') && !is_URI_sub_delim(*i) && (*i != ':')) {
if ((*i >= 0x20) && (*i <= 0x7E)) {
sprintf(error_str, "Error: userinfo portion of uniformResourceIdentifier contains an illegal character: '%c'.", *i);
} else {
sprintf(error_str, "Error: userinfo portion of uniformResourceIdentifier contains an illegal character: %d.", *i);
}
print_error(error_str);
}
}
return(0);
}
/*
* host = IP-literal / IPv4address / reg-name
*
* reg-name = *( unreserved / pct-encoded / sub-delims )
*
* IP-literal = "[" ( IPv6address / IPvFuture ) "]"
*
* IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" )
*
* IPv6address = 6( h16 ":" ) ls32
* / "::" 5( h16 ":" ) ls32
* / [ h16 ] "::" 4( h16 ":" ) ls32
* / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
* / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
* / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
* / [ *4( h16 ":" ) h16 ] "::" ls32
* / [ *5( h16 ":" ) h16 ] "::" h16
* / [ *6( h16 ":" ) h16 ] "::"
*
* ls32 = ( h16 ":" h16 ) / IPv4address
* ; least-significant 32 bits of address
*
* h16 = 1*4HEXDIG
* ; 16 bits of address represented in hexadecimal
*
* NOTE: IPv4address satisifies syntax for reg-name
*/
static int validate_URI_host(unsigned char *host, int host_length)
{
unsigned char *i;
if (*host != '[') {
/* reg-name or IPv4address */
for(i=host; i < host + host_length; i++) {
if (!is_URI_unreserved(*i) && (*i != '%') && !is_URI_sub_delim(*i)) {
if ((*i >= 0x20) && (*i <= 0x7E)) {
sprintf(error_str, "Error: host portion of uniformResourceIdentifier contains an illegal character: '%c'.", *i);
} else {
sprintf(error_str, "Error: host portion of uniformResourceIdentifier contains an illegal character: %d.", *i);
}
print_error(error_str);
}
}
} else {
host++;
host_length -=2;
if ((*host != 'v') && (*host != 'V')) {
/* IPv6address */
/* TODO: For now, just check that contents are limited to hexadecimal digits and ':' */
for(i=host; i < host + host_length; i++) {
if (!is_hexdigit(*i) && (*i != ':')) {
if ((*i >= 0x20) && (*i <= 0x7E)) {
sprintf(error_str, "Error: IPv6address in host in uniformResourceIdentifier contains a character that is not a hexadecimal digit or ':': '%c'.", *i);
} else {
sprintf(error_str, "Error: IPv6address in host in uniformResourceIdentifier contains a character that is not a hexadecimal digit or ':': %d.", *i);
}
print_error(error_str);
}
}
} else {
/* IPvFuture */
i = host + 1;
while ((i < host + host_length) && (*i != '.')) {
if (!is_hexdigit(*i)) {
if ((*i >= 0x20) && (*i <= 0x7E)) {
sprintf(error_str, "Error: version number in IPvFuture host in uniformResourceIdentifier contains a character that is not a hexadecimal digit: '%c'.", *i);
} else {
sprintf(error_str, "Error: version number in IPvFuture host in uniformResourceIdentifier contains a character that is not a hexadecimal digit: %d.", *i);
}
print_error(error_str);
}
i++;
}
if (i == host + 1) {
print_error("Error: IPvFuture host name in uniformResourceIdentifier is missing version number.");
return(-1);
}
if (i + 1 >= host + host_length) {
print_error("Error: IPvFuture host name in uniformResourceIdentifier contains no value after version number.");
return(-1);
}
i++;
while (i < host + host_length) {
if (!is_URI_unreserved(*i) && !is_URI_sub_delim(*i) && (*i != ':')) {
if ((*i >= 0x20) && (*i <= 0x7E)) {
sprintf(error_str, "Error: host portion of uniformResourceIdentifier contains an illegal character: '%c'.", *i);
} else {
sprintf(error_str, "Error: host portion of uniformResourceIdentifier contains an illegal character: %d.", *i);
}
print_error(error_str);
}
i++;
}
}
}
return(0);
}
/*
* port = *DIGIT
*/
static int validate_URI_port(unsigned char *port, int port_length)
{
unsigned char *i;
for(i=port; i < port + port_length; i++) {
if ((*i < '0') || (*i > '9')) {
if ((*i >= 0x20) && (*i <= 0x7E)) {
sprintf(error_str, "Error: port number in authority portion of uniformResourceIdentifier contains an illegal character: '%c'.", *i);
} else {
sprintf(error_str, "Error: port number in authority portion of uniformResourceIdentifier contains an illegal character: %d.", *i);
}
print_error(error_str);
}
}
return(0);
}
int validate_URI(unsigned char *URI, int URI_length)
{
unsigned char *i, *j, *authority, *userinfo, *host, *port, *path, *query, *fragment;
char *scheme;
int scheme_length, authority_length, userinfo_length, host_length, port_length, path_length, query_length, fragment_length;
int error_found = 0;
int contains_authority = 0;
if (URI_length == 0) {
print_error("Error: uniformResourceIdentifier contains an empty string.");
return(-1);
}
i = URI;
/* URI must begin with a scheme of the form scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
* followed by a ':'. */
if (!is_letter(*i)) {
error_found = 1;
if ((*i >= 0x20) && (*i <= 0x7E)) {
sprintf(error_str, "Error: Scheme portion of uniformResourceIdentifier contains an invalid character: '%c'. First character of URI must be a letter.", *i);
} else {
sprintf(error_str, "Error: Scheme portion of uniformResourceIdentifier contains an invalid character: %d. First character of URI must be a letter.", *i);
}
print_error(error_str);
}
i++;
while ((i < URI + URI_length) && (*i != ':')) {
if (!is_letter_or_digit(*i) && (*i != '+') && (*i != '-') && (*i != '.')) {
error_found = 1;
if ((*i >= 0x20) && (*i <= 0x7E)) {
sprintf(error_str, "Error: Scheme portion of uniformResourceIdentifier contains an invalid character: '%c'.", *i);
} else {
sprintf(error_str, "Error: Scheme portion of uniformResourceIdentifier contains an invalid character: %d.", *i);
}
print_error(error_str);
}
i++;
}
if (i == URI + URI_length) {
print_error("Error: uniformResourceIdentifier does not begin with: scheme \":\".");
return(-1);
}
scheme_length = i - URI;
i++;
scheme = (char *)malloc(scheme_length + 1);
memcpy(scheme, URI, scheme_length);
scheme[scheme_length] = '\0';
j = i;
/* check for illegal characters in remainder of URI. */
while (j < URI + URI_length) {
if (*j == '%') {
if (j + 2 >= URI + URI_length) {
sprintf(error_str, "Error: %s uniformResourceIdentifier includes a '%%' character that is not followed by two hexadecimal digits.", scheme);
print_error(error_str);
error_found = 1;
j++;
} else if (!is_hexdigit(*(j+1))) {
sprintf(error_str, "Error: %s uniformResourceIdentifier includes a '%%' character that is not followed by two hexadecimal digits.", scheme);
print_error(error_str);
error_found = 1;
j++;
} else if (!is_hexdigit(*(j+2))) {
sprintf(error_str, "Error: %s uniformResourceIdentifier includes a '%%' character that is not followed by two hexadecimal digits.", scheme);
print_error(error_str);
error_found = 1;
j += 2;
} else {
j += 3;
}
} else {
if (!is_URI_unreserved(*j) && !is_URI_gen_delim(*j) && !is_URI_sub_delim(*j)) {
error_found = 1;
if ((*j >= 0x20) && (*j <= 0x7E)) {
sprintf(error_str, "Error: %s uniformResourceIdentifier contains an invalid character: '%c'.", scheme, *j);
} else {
sprintf(error_str, "Error: %s uniformResourceIdentifier contains an invalid character: %d.", scheme, *j);
}
print_error(error_str);
}
j++;
}
}
if (error_found) {
return(-1);
}
if (strcasecmp(scheme, "URN") == 0) {
free(scheme);
return(validate_URN(URI + 4, URI_length - 4));
}
/* If an authority component is present, then check it. */
if ((i + 1 < URI + URI_length) && (*i == '/') && (*(i+1) == '/')) {
contains_authority = 1;
i += 2;
authority = i;
/* next portion of URI must be an authority component */
/* The authority component is terminated by the next slash ("/"),
* question mark ("?"), or number sign ("#") character, or by the
* end of the URI.
*
* authority = [ userinfo "@" ] host [ ":" port ]
*/
while ((i < URI + URI_length) && (*i != '/') && (*i != '?') && (*i != '#')) {
i++;
}
authority_length = i - authority;
j = userinfo = authority;
while ((j < authority + authority_length) && (*j != '@')) {
j++;
}
if (*j == '@') {
userinfo_length = j - userinfo;
j++;
host = j;
} else {
userinfo_length = 0;
j = host = authority;
}
if (j == authority + authority_length) {
host_length = 0;
port_length = 0;
} else {
if (*j == '[') {
while ((j < authority + authority_length) && (*j != ']')) {
j++;
}
if (j == authority + authority_length) {
sprintf(error_str, "Error: host portion of %s uniformResourceIdentifier begins with ']' but there is no matching ']'.", scheme);
print_error(error_str);
free(scheme);
return(-1);
}
j++;
} else {
while ((j < authority + authority_length) && (*j != ':')) {
j++;
}
}
host_length = j - host;
if ((j < authority + authority_length) && (*j != ':')) {
sprintf(error_str, "Error: Unknown syntax error in authority component of %s uniformResourceIdentifier.", scheme);
print_error(error_str);
free(scheme);
return(-1);
}
if (j == authority + authority_length) {
port_length = 0;
} else {
j++;
port = j;
port_length = authority + authority_length - port;
}
}
if (userinfo_length > 0) {
if (strcasecmp(scheme, "ldap") == 0) {
sprintf(error_str, "Error: The authority component of %s URIs must not include a userinfo component [RFC 4516].", scheme);
print_error(error_str);
free(scheme);
return(-1);
}
validate_URI_userinfo(userinfo, userinfo_length);
}
if (host_length > 0) {
validate_URI_host(host, host_length);
}
if (port_length > 0) {
validate_URI_port(port, port_length);
}
} else if ((strcasecmp(scheme, "http") == 0) || (strcasecmp(scheme, "https") == 0)) {
sprintf(error_str, "Error: %s URIs must include an authority component [RFC 2616]", scheme);
print_error(error_str);
} else if (strcasecmp(scheme, "ldap") == 0) {
sprintf(error_str, "Error: %s URIs must include an authority component [RFC 4516]", scheme);
print_error(error_str);
} else if (strcasecmp(scheme, "rsync") == 0) {
sprintf(error_str, "Error: %s URIs must include an authority component [RFC 5781]", scheme);
print_error(error_str);
}
if (i == URI + URI_length) {
free(scheme);
return(0);
}
if (contains_authority && (*i != '/') && (*i != '?') && (*i != '#')) {
sprintf(error_str, "Error: %s uniformResourceIdentifier includes an authority component, but path is not empty and does not begin with a '/'.", scheme);
print_error(error_str);
}
/* The path is terminated by the first question mark ("?") or
* number sign ("#") character, or by the end of the URI.
*/
path = i;
while ((i < URI + URI_length) && (*i != '?') && (*i != '#')) {
i++;
}
path_length = i - path;
/* check path component of URI */
for(j = path; j < path + path_length; j++) {
if (!is_URI_unreserved(*j) && (*j != '%') && !is_URI_sub_delim(*j) &&
(*j != ':') && (*j != '@') && (*j != '/')) {
sprintf(error_str, "Error: Path component of %s URI contains an illegal character: '%c'.", scheme, *j);
print_error(error_str);
}
}
if (i == URI + URI_length) {
free(scheme);
return(0);
}
/* The query component is indicated by the first question
* mark ("?") character and terminated by a number sign ("#") character
* or by the end of the URI.
*/
if (*i == '?') {
i++;
query = i;
while ((i < URI + URI_length) && (*i != '#')) {
i++;
}
query_length = i - query;
/* check query component of URI */
for(j = query; j < query + query_length; j++) {
if (!is_URI_unreserved(*j) && (*j != '%') && !is_URI_sub_delim(*j) &&
(*j != ':') && (*j != '@') && (*j != '/') && (*j != '?')) {
sprintf(error_str, "Error: Query component of %s URI contains an illegal character: '%c'.", scheme, *j);
print_error(error_str);
}
}
}
if (i == URI + URI_length) {
free(scheme);
return(0);
}
/* skip over '#' */
i++;
fragment = i;
fragment_length = URI + URI_length - fragment;
/* check fragment component of URI */
for(j = fragment; j < fragment + fragment_length; j++) {
if (!is_URI_unreserved(*j) && (*j != '%') && !is_URI_sub_delim(*j) &&
(*j != ':') && (*j != '@') && (*j != '/') && (*j != '?')) {
sprintf(error_str, "Error: Fragment component of %s URI contains an illegal character: '%c'.", scheme, *j);
print_error(error_str);
}
}
free(scheme);
return(0);
}
int validate_emailAddress(int tag, unsigned char *emailAddress, int emailAddress_length)
{
if (tag != 22) {
print_error("Error: Incorrect tag. domainComponent must be encoded as IA5String.");
}
if (emailAddress_length == 0) {
print_error("Error: emailAddress contains an empty string.");
return(-1);
} else if (emailAddress_length > 255) {
print_error("Warning: emailAddress attribute is longer than 255 octets.");
}
return(validate_rfc822Name(emailAddress, emailAddress_length));
}
int validate_VisibleString(unsigned char *DirectoryString, int DirectoryString_len, int max_len)
{
unsigned char *i;
if (DirectoryString_len > max_len) {
print_error("Warning: VisibleString is too long.");
}
for(i=DirectoryString; i < DirectoryString + DirectoryString_len; i++) {
if ((*i < 0x20) || (*i >= 0x7F)) {
sprintf(error_str, "Error: VisibleString contains invalid character: %d.", *i);
print_error(error_str);
}
}
return(0);
}
int validate_PrintableString(unsigned char *DirectoryString, int DirectoryString_len, int max_len)
{
unsigned char *i;
if (DirectoryString_len > max_len) {
print_error("Warning: PrintableString is too long.");
}
for(i=DirectoryString; i < DirectoryString + DirectoryString_len; i++) {
if (!is_letter_or_digit(*i) && (*i != ' ') && (*i != '\'') && (*i != '(') && (*i != ')') && (*i != '+') &&
(*i != ',') && (*i != '-') && (*i != '.') && (*i != '/') && (*i != ':') && (*i != '=') && (*i != '?')) {
if ((*i >= 0x20) && (*i <= 0x7E)) {
sprintf(error_str, "Error: PrintableString contains invalid character: '%c'.", *i);
} else {
sprintf(error_str, "Error: PrintableString contains invalid character: %d.", *i);
}
print_error(error_str);
}
}
return(0);
}
/*
* Char. number range | UTF-8 octet sequence
* (hexadecimal) | (binary)
* --------------------+---------------------------------------------
* 0000 0000-0000 007F | 0xxxxxxx
* 0000 0080-0000 07FF | 110xxxxx 10xxxxxx
* 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
* 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
*/
int validate_UTF8String(unsigned char *DirectoryString, int DirectoryString_len, int max_len)
{
unsigned char *i;
char error_msg[100];
int length, c;
i = DirectoryString;
length = 0;
while (i < DirectoryString + DirectoryString_len) {
if ((*i & 0x80) == 0) {
/* if most significant bit is a zero, then character is
* encoded in a single octet: 0xxxxxxx */
c = *i;
i += 1;
} else if (((*i & 0xE0) == 0xC0) && (i < DirectoryString + DirectoryString_len - 1)) {
/* character is encoded as two octets: 110xxxxx 10xxxxxx */
c = (*i & 0x1F) << 6;
i += 1;
if ((*i & 0xC0) != 0x80) {
print_error("UTF8String contains an improperly formatted character.");
return(-1);
}
c += *i & 0x3F;
i += 1;
if (c < 128) {
print_error("UTF8String contains an improperly formatted character.");
}
} else if (((*i & 0xF0) == 0xE0) && (i < DirectoryString + DirectoryString_len - 2)) {
/* character is encoded as three octets: 1110xxxx 10xxxxxx 10xxxxxx */
c = (*i & 0x0F) << 12;
i += 1;
if ((*i & 0xC0) != 0x80) {
print_error("UTF8String contains an improperly formatted character.");
return(-1);
}
c += (*i & 0x3F) << 6;
i += 1;
if ((*i & 0xC0) != 0x80) {
print_error("UTF8String contains an improperly formatted character.");
return(-1);
}
c += *i & 0x3F;
i += 1;
if (c < 2048) {
print_error("UTF8String contains an improperly formatted character.");
}
} else if (((*i & 0xF8) != 0xF0) && (i < DirectoryString + DirectoryString_len - 3)) {
/* character is encoded as four octets: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
c = (*i & 0x07) << 18;
i += 1;
if ((*i & 0xC0) != 0x80) {
print_error("UTF8String contains an improperly formatted character.");
return(-1);
}
c += (*i & 0x3F) << 12;
i += 1;
if ((*i & 0xC0) != 0x80) {
print_error("UTF8String contains an improperly formatted character.");
return(-1);
}
c += (*i & 0x3F) << 6;
i += 1;
if ((*i & 0xC0) != 0x80) {
print_error("UTF8String contains an improperly formatted character.");
return(-1);
}
c += *i & 0x3F;
i += 1;
if (c < 65536) {
print_error("UTF8String contains an improperly formatted character.");
}
} else {
print_error("UTF8String contains an improperly formatted character.");
return(-1);
}
length++;
/* check that c is not a control character */
if (((c >= 0) && (c <= 0x1F)) || ((c >= 0x7F) && (c <= 0x9F))) {
sprintf(error_msg, "Warning: UTF8String contains control character %d.\n", c);
print_error(error_msg);
}
}
if (length > max_len) {
print_error("Warning: UTF8String is too long.");
}
return(0);
}
int validate_TeletexString(unsigned char *DirectoryString, int DirectoryString_len, int max_len)
{
unsigned char *i;
if (DirectoryString_len > max_len) {
print_error("Warning: TeletexString is too long.");
}
/* TODO: Check contents of string. Current text treats TeletexString like IA5String. */
for(i=DirectoryString; i < DirectoryString + DirectoryString_len; i++) {
if ((*i < 0x20) || (*i == 0x7F)) {