-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpkicore_input.c
1913 lines (1679 loc) · 43.2 KB
/
pkicore_input.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
/*
* Input control routines
*
* Copyright (c) 2011 Nick Kossifidis <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/stat.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <limits.h>
#include <regex.h>
#include <curl/curl.h>
#include "pkicore.h"
#include "pkicore_input.h"
/***************\
* Regexp checks *
\***************/
/**
* pki_is_singlestring - Checks if the given string is a single string
*
* Checks if the given string is a single string with no spaces
*
* @char* string - The string to check
*
* returns: 1 if it is 0 if it's not -1 on error
*/
static int
pki_is_singlestring(const char* string)
{
regex_t regex;
memset(®ex, 0 ,sizeof(regex_t));
int ret = 0;
ret = regcomp(®ex, "^"SINGLESTRING"$", REG_EXTENDED|REG_ICASE);
if (ret) {
pki_msg(0,"INPUT", "Invalid regexp7\n");
return -1;
}
ret = regexec(®ex, string, (size_t) 0, NULL, 0);
regfree(®ex);
return (!ret) ? 1 : 0;
}
/**
* pki_is_name - Checks if the given string is a name
*
* Checks if the given string is a name (firstname + surname)
*
* @char* string - The string to check
*
* returns: 1 if it is 0 if it's not -1 on error
*/
static int
pki_is_name(const char* string)
{
regex_t regex;
memset(®ex, 0 ,sizeof(regex_t));
int ret = 0;
ret = regcomp(®ex, "^"NAMESTRING"$", REG_EXTENDED);
if (ret) {
pki_msg(0,"INPUT", "Invalid regexp6\n");
return -1;
}
ret = regexec(®ex, string, (size_t) 0, NULL, 0);
regfree(®ex);
return (!ret) ? 1 : 0;
}
/**
* pki_is_oou - Checks if the given string is an Organization name
*
* Checks if the given string is an Organization/Organizational unit
* name.
*
* @char* string - The string to check
*
* returns: 1 if it is 0 if it's not -1 on error
*/
static int
pki_is_oou(const char* string)
{
regex_t regex;
memset(®ex, 0 ,sizeof(regex_t));
int ret = 0;
ret = regcomp(®ex, "^"OOU"$", REG_EXTENDED|REG_ICASE);
if (ret) {
pki_msg(0,"INPUT", "Invalid regexp6\n");
return -1;
}
ret = regexec(®ex, string, (size_t) 0, NULL, 0);
regfree(®ex);
return (!ret) ? 1 : 0;
}
/**
* pki_is_ipaddr - Checks if the given string is an IP address
*
* Checks if the given string is an IPv4 address.
* TODO: IPv6 checks + support
*
* @char* string - The string to check
*
* returns: 1 if it is 0 if it's not -1 on error
*/
static int
pki_is_ipaddr(const char* string)
{
regex_t regex;
memset(®ex, 0 ,sizeof(regex_t));
int ret = 0;
ret = regcomp(®ex, "^"IPADDR"$", REG_EXTENDED);
if (ret) {
pki_msg(0,"INPUT", "Invalid regexp5\n");
return -1;
}
ret = regexec(®ex, string, (size_t) 0, NULL, 0);
regfree(®ex);
return (!ret) ? 1 : 0;
}
/**
* pki_is_domain - Checks if the given string is a domain name
*
* Checks if the given string is a domain name
*
* @char* string - The string to check
*
* returns: 1 if it is 0 if it's not -1 on error
*/
static int
pki_is_domain(const char* string)
{
regex_t regex;
memset(®ex, 0 ,sizeof(regex_t));
int ret = 0;
ret = regcomp(®ex, "^"DOMAIN"$", REG_EXTENDED|REG_ICASE);
if (ret) {
pki_msg(0,"INPUT", "Invalid regexp4\n");
return -1;
}
ret = regexec(®ex, string, (size_t) 0, NULL, 0);
regfree(®ex);
return (!ret) ? 1 : 0;
}
/**
* pki_is_url - Checks if the given string is a URL
*
* Checks if the given string is an http/https URL
*
* @char* string - The string to check
*
* returns: 1 if it is 0 if it's not -1 on error
*/
static int
pki_is_url(const char* string)
{
regex_t regex;
memset(®ex, 0 ,sizeof(regex_t));
int ret = 0;
ret = regcomp(®ex, "^"FULLURL"$", REG_EXTENDED/*|REG_ICASE*/);
if (ret) {
pki_msg(0,"INPUT", "Invalid regexp3\n");
return -1;
}
ret = regexec(®ex, string, (size_t) 0, NULL, 0);
regfree(®ex);
return (!ret) ? 1 : 0;
}
#ifdef PKICORE_PKCS11
/**
* pki_is_pkcs11_url - Checks if a string is a PKCS#11 URL
*
* Checks if the given string is a PKCS#11 resource locator
*
* @string - The string to check
*
* returns 1 if it is, 0 if it's not, -1 on error
*/
static int
pki_is_pkcs11_url(const char* string)
{
regex_t regex;
memset(®ex, 0 ,sizeof(regex_t));
int ret = 0;
/* Regexp check */
ret = regcomp(®ex, "^"PKCS11URL"$", REG_EXTENDED|REG_ICASE);
if (ret) {
pki_msg(0,"INPUT", "Invalid regexp2\n");
return -1;
}
ret = regexec(®ex, string, (size_t) 0, NULL, 0);
regfree(®ex);
return (!ret) ? 1 : 0;
}
#endif
/**
* pki_is_email - Checks if the given string is an e-mail address
*
* Checks if the given string is an e-mail address
*
* @char* string - The string to check
*
* returns: 1 if it is 0 if it's not -1 on error
*/
static int
pki_is_email(const char* string)
{
regex_t regex;
memset(®ex, 0 ,sizeof(regex_t));
int ret = 0;
ret = regcomp(®ex, "^"EMAIL"$", REG_EXTENDED|REG_ICASE);
if (ret) {
pki_msg(0,"INPUT", "Invalid regexp1\n");
return -1;
}
ret = regexec(®ex, string, (size_t) 0, NULL, 0);
regfree(®ex);
return (!ret) ? 1 : 0;
}
/**
* pki_is_cn - Checks if the given string is a Common Name
*
* Checks if the given string is a Common Name
*
* @char* string - The string to check
*
* returns: 1 if it is 0 if it's not -1 on error
*/
static int
pki_is_cn(const char* string)
{
regex_t regex;
memset(®ex, 0 ,sizeof(regex_t));
int ret = 0;
ret = regcomp(®ex, "^"CN"$", REG_EXTENDED);
if (ret) {
pki_msg(0,"INPUT", "Invalid regexp0\n");
return -1;
}
ret = regexec(®ex, string, (size_t) 0, NULL, 0);
regfree(®ex);
return (!ret) ? 1 : 0;
}
/***************\
* Sanity checks *
\***************/
/**
* pki_check_dn - Checks if the given DN makes sense
*
* Perfoms regexp + sanity checks on the given DN.
*
* @struct pki_dn *dn - The DN to check
*
* returns: One of pki_error_codes
*/
int
pki_check_dn(struct pki_dn *dn)
{
int ret = PKI_OK;
if ((!dn->common_name) || (!dn->email)) {
pki_msg(0,"INPUT", "No common name or e-mail address given\n");
return PKI_INVALID_INPUT;
}
if (dn->country) {
ret = pki_is_singlestring(dn->country);
if (ret <= 0) {
pki_msg(0,"INPUT", "Invalid coutry name\n");
return PKI_INVALID_INPUT;
}
}
if (dn->state_province) {
ret = pki_is_singlestring(dn->state_province);
if (ret <= 0) {
pki_msg(0,"INPUT", "Invalid state name\n");
return PKI_INVALID_INPUT;
}
}
if (dn->locality) {
ret = pki_is_singlestring(dn->locality);
if (ret <= 0) {
pki_msg(0,"INPUT", "Invalid locality name\n");
return PKI_INVALID_INPUT;
}
}
if (dn->organization) {
ret = pki_is_oou(dn->organization);
if (ret <= 0) {
pki_msg(0,"INPUT", "Invalid organization name\n");
return PKI_INVALID_INPUT;
}
}
if (dn->organizational_unit) {
ret = pki_is_oou(dn->organizational_unit);
if (ret <= 0) {
pki_msg(0,"INPUT", "Invalid organizational unit name\n");
return PKI_INVALID_INPUT;
}
}
ret = pki_is_cn(dn->common_name);
if (ret <= 0) {
pki_msg(0,"INPUT", "Invalid common name\n");
return PKI_INVALID_INPUT;
}
ret = pki_is_email(dn->email);
if (ret <= 0) {
pki_msg(0,"INPUT", "Invalid e-mail address\n");
return PKI_INVALID_INPUT;
}
return PKI_OK;
}
/**
* pki_check_url - Check if provided string is a valid url
*
* Checks if the given string is a valid URL and tries to
* resolve it and -if requested- do a connection check.
*
* @char* string - String to check
* @int connect - Also check connection
*
* returns: 1 if it's a URL, 0 if not or -1 on failure
*/
int
pki_check_url(const char* string, int connect)
{
size_t len;
char *colon;
char *slash;
char *tmp;
char *hostname;
regex_t regex;
struct addrinfo *result = NULL;
CURL* curl_handle = NULL;
FILE* null_device = NULL;
int ret = 1;
/* Copy string to tmp
* for later use */
tmp = malloc(strlen(string) + 1);
if(!tmp)
return PKI_NOMEM_ERR;
ret = snprintf(tmp, strlen(string) + 1, "%s", string);
if(ret <= 0) {
ret = PKI_INVALID_INPUT;
goto cleanup;
}
/* Quick check for ":" */
colon = strchr(tmp, ':');
if(!colon) {
ret = 0;
goto cleanup;
}
/* Regexp check for generic url */
ret = regcomp(®ex, URL URLEXT "$", REG_EXTENDED|REG_ICASE);
if (ret) {
pki_msg(0,"INPUT", "Invalid regexp\n");
ret = -1;
goto cleanup;
}
ret = regexec(®ex, string, (size_t) 0, NULL, 0);
regfree(®ex);
if (ret) {
ret = 0;
goto cleanup;
} else
ret = 1;
/* TODO: Unicode support */
resolve:
/* Make sure it exists */
/* We need to get the hostname from URL
* and strip everything else. We already
* have the pointer to : so if we advance it
* by 3 (://) we'll get to the hostname. */
hostname = colon + 3;
/* If we have a / switch it to \0 so that
* we terminate the string there and skip
* anything that follows (we operate on tmp
* so no problem */
slash = strchr(hostname, '/');
if(slash) {
len = slash - hostname;
strcpy(slash, "\0");
}
ret = getaddrinfo(hostname, NULL, NULL, &result);
if (ret) {
pki_msg(0,"INPUT", "Unable to resolve URL: %s\n",hostname);
ret = -1;
goto cleanup;
} else {
freeaddrinfo(result);
ret = 1;
}
/* Try to connect and get the HTTP header
*
* TODO: The reason we do this test is to verify
* that we didn't get a reply from something like
* OpenDNS that even if the hostname is invalid
* it tries to fix it or redirects you to a
* search engine. So we need to add some tests
* here to parse these headers. Unfortunately
* CAs may also use redirection for their CRLs
* or certificates so just checking for redirection
* is not enough.
*/
if(!connect)
goto cleanup;
/* Initialize cURL
* Note: Don't let cURL re-initialize
* OpenSSL since we handle it already */
if(pki_get_global_flags() & PKI_CURL_OPENSSL)
curl_global_init(CURL_GLOBAL_ALL &~ CURL_GLOBAL_SSL);
/* Grab an easy handle */
curl_handle = curl_easy_init() ;
if (!curl_handle) {
pki_msg(0,"INPUT", "Couldn't init curl library !\n");
ret = -1;
goto cleanup;
}
null_device = fopen( "/dev/null", "w" ) ;
if (!null_device) {
pki_msg(0,"INPUT", "Couldn't open /dev/null for writing !\n");
ret = -1;
goto cleanup;
}
curl_easy_setopt(curl_handle, CURLOPT_HEADER, 1 ) ;
/* Skip body */
curl_easy_setopt(curl_handle, CURLOPT_NOBODY, 1 ) ;
/* Make sure the server we try to connect to is the expected
* one, if we get a cert with another hostname (e.g. in the cae
* of OpenDNS) abort */
curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, 1);
curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYHOST, 2);
/* Allow only one redirection */
curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1 ) ;
curl_easy_setopt(curl_handle, CURLOPT_MAXREDIRS, 1 ) ;
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, null_device ) ;
curl_easy_setopt(curl_handle, CURLOPT_URL, string) ;
ret = curl_easy_perform(curl_handle);
if (ret != CURLE_OK) {
pki_msg(0,"INPUT", "Couldn't connect to: %s\n", string);
ret = -1;
} else
ret = 1;
cleanup:
if (curl_handle)
curl_easy_cleanup(curl_handle);
free(tmp);
return ret;
}
/**
* pki_check_filename - Check if provided string is a valid filename
*
* Checks if the given string is a valid filename and tries to
* access it.
*
* @char* string - String to check
*
* returns: One of pki_error_codes
*/
static int
pki_check_filename(const char* string)
{
struct stat buffer;
if(stat(string, &buffer) == 0)
return PKI_OK;
else
return PKI_INVALID_INPUT;
}
/**
* pki_check_pass - Check if the given string is a valid password
*
* Checks if the given string is a valid password, that means it's
* within allowed password length and each character is a non-control/extended
* character.
*
* @char* string - String to check
*
* returns: One of pki_error_codes
*/
static int
pki_check_pass(const char* string)
{
int len = 0;
int i = 0;
len = strlen(string);
if((len >= PKI_PASS_MIN_CHARS) &&
(len <= PKI_PASS_MAX_CHARS)) {
for(i = 0; i < len; i++)
if((string[i] > 0x20) &&
(string[i] < 0x7f))
continue;
else {
pki_msg(0,"INPUT",
"Invalid password character (%c) !\n",
string[i]);
return PKI_INVALID_INPUT;
}
} else {
pki_msg(0,"INPUT",
"Invalid password length\n");
return PKI_INVALID_INPUT;
}
return PKI_OK;
}
/**
* pki_check_der_data - Check if provided data looks like valid DER encoded data
*
* Tries to check if the given data looks like DER encoded data, it basicaly
* checks the length and compares it to what user provided.
*
* @const char* data - Data to check
* @int len - Data length provided by user
*
* returns: One of pki_error_codes
*/
static int
pki_check_der_data(const char* data, int len)
{
int length_octets = 0;
int length = 0;
int i = 0;
/* Do a simple test to see if it looks like
* DER encoded data, first byte should be 0x30 */
if(data[0] == 0x30) {
/* Validate length to be sure */
if(data[1] & 0x80 && data[1] < 0x8F) {
/* Length in long format, check if
* it's indefinite */
if(data[1] & 0x7F) {
i = 2;
for(length_octets = data[1] & 0x7F;
length_octets > 0; length_octets--) {
length <<= 8;
length |= data[i];
i++;
}
/* Validate total length */
length_octets = data[1] & 0x7F;
length = length + length_octets + 2;
if(length != len) {
pki_msg(0, "INPUT",
"Invalid length for DER encoded data (malformed ?) !\n");
return PKI_INVALID_INPUT;
}
} else {
/* It's indefinite, try to find the
* end-of-content octets (4 zeroes) */
for(i = 2; i + 1 < len; i += 2)
if(data[i] == 0x00 &&
data[i+1] == 0x00)
break;
/* We reached the end without
* finding end-of-content octets */
if(i >= len) {
pki_msg(0, "INPUT",
"Could not determine DER encoded data length !\n");
return PKI_INVALID_INPUT;
}
/* Validate total length */
length = i + 2;
if(length != len) {
pki_msg(0, "INPUT",
"Invalid length for DER encoded data (malformed ?) !\n");
return PKI_INVALID_INPUT;
}
}
} else {
/* Length in short format, means it's shorter than 128
* bytes, seems too small for a known resource (CSR,
* Certificate or CRL) */
pki_msg(0, "INPUT",
"Invalid length for DER encoded data (malformed ?) !\n");
return PKI_INVALID_INPUT;
}
} else {
pki_msg(0, "INPUT",
"Data provided doesn't look like a known DER encoded resource !\n");
return PKI_INVALID_INPUT;
}
return PKI_OK;
}
/**
* pki_input_regexp_selftest() - Self-test function for the regexp checks
*
* Runs a self-test to check if regexp functions do what they are
* supposed to do
*
* returns: 0 on success, -1 on failure
*/
int
pki_input_regexp_selftest()
{
const char* filename = "tes-t123/tes_t321.123-test";
const char* url = "http://te-st123.321te_st.tes";
const char* fullurl = "http://te-st123.321te_st.tes/te-st/te_st.tes";
const char* email = "[email protected]";
const char* ip = "192.168.1.2";
const char* domain = "te-st.te_st.tes";
const char* name = "Nick Kossifidis";
const char* cacn = "FORTH Certificate Authority";
const char* single = "test";
int (*check)(const char*) = NULL;
int i = 0;
int failed = 0;
for (i = 0; i < 8; i++) {
if (i == 0) {
check = &pki_is_cn;
pki_msg(2,"INPUT",
"Testing is_cn...\n");
} else if (i == 1) {
check = &pki_is_email;
pki_msg(2,"INPUT",
"Testing is_email...\n");
} else if (i == 2) {
check = &pki_is_url;
pki_msg(2,"INPUT",
"Testing is_url...\n");
} else if (i == 3) {
check = &pki_is_domain;
pki_msg(2,"INPUT",
"Testing is_domain...\n");
} else if (i == 4) {
check = &pki_is_ipaddr;
pki_msg(2,"INPUT",
"Testing is_ipaddr...\n");
} else if (i == 5) {
check = &pki_is_name;
pki_msg(2,"INPUT",
"Testing is_name...\n");
} else if (i == 6) {
check = &pki_is_singlestring;
pki_msg(2,"INPUT",
"Testing is_singlestring...\n");
} else if (i == 7) {
check = &pki_is_oou;
pki_msg(2,"INPUT",
"Testing is_oou...\n");
}
if (check(filename)) {
pki_msg(2,"INPUT",
"HIT: %s\n", filename);
failed++;
}
if (check(url)) {
pki_msg(2,"INPUT",
"HIT: %s\n", url);
if(i != 2)
failed++;
}
if (check(fullurl)) {
pki_msg(2,"INPUT",
"HIT: %s\n", fullurl);
if(i != 2)
failed++;
}
if (check(email)) {
pki_msg(2,"INPUT",
"HIT: %s\n", email);
if(i != 1)
failed++;
}
if (check(ip)) {
pki_msg(2,"INPUT",
"HIT: %s\n", ip);
if(i != 4)
failed++;
}
if (check(domain)) {
pki_msg(2,"INPUT",
"HIT: %s\n", domain);
if(i != 3 && i != 0)
failed++;
}
if (check(name)) {
pki_msg(2,"INPUT",
"HIT: %s\n", name);
if(i != 5 && i != 0 && i != 7)
failed++;
}
if (check(cacn)) {
pki_msg(2,"INPUT",
"HIT: %s\n", cacn);
if(i != 0 && i != 7)
failed++;
}
if (check(single)) {
pki_msg(2,"INPUT",
"HIT: %s\n", single);
if(i != 6 && i != 0 && i != 3 && i != 7)
failed++;
}
}
pki_msg(0,"INPUT",
"Regexp self-test results...\n\tfailed tests: %i\n",failed);
return -1 ? failed > 0 : 0;
}
/*************************\
* Entry points from above *
\*************************/
/**
* pki_set_dn - Creates a DN structure
*
* Tries to create a DN structure based on the
* given attributes and puts it to the command
* structure.
*
* @struct pki_cmd *cmd - The command from above
* @char* country - The Country (C) attribute
* @char* state_province - The state/province (ST) attribute
* @char* locality - The locality (L) attribute
* @char* organization - The organization (O) attribute
* @char* organizational_unit - The organizational unit (OU) attribute
* @char* common_name - The common name (CN) attribute
* @char* email - The email (emailAddress) attribute
*
* returns: One of pki_error_codes
*/
int
pki_set_dn(struct pki_cmd *cmd,
const char* country,
const char* state_province,
const char* locality,
const char* organization,
const char* organizational_unit,
const char* common_name,
const char* email)
{
int ret = PKI_OK;
struct pki_dn *dn = malloc(sizeof(struct pki_dn));
if(!dn) {
pki_msg(0, "INPUT",
"Unable to allocate DN structure !\n");
return PKI_NOMEM_ERR;
}
dn = memset(dn, 0, sizeof(struct pki_dn));
if(!common_name || !email) {
pki_msg(0,"INPUT", "Missing common name or e-mail\n");
if(ret <= 0) {
ret = PKI_INVALID_INPUT;
goto cleanup;
}
}
/* Length and format checks */
if(country) {
dn->country = malloc(3);
memset(dn->country, 0, 3);
ret = snprintf(dn->country, 3, "%s", country);
if(ret <= 0) {
ret = PKI_INVALID_INPUT;
goto cleanup;
}
}
if(state_province) {
dn->state_province = malloc(PKI_MAX_DN_FIELD_LEN);
memset(dn->state_province, 0, PKI_MAX_DN_FIELD_LEN);
ret = snprintf(dn->state_province, PKI_MAX_DN_FIELD_LEN,
"%s", state_province);
if(ret <= 0) {
ret = PKI_INVALID_INPUT;
goto cleanup;
}
}
if(locality) {
dn->locality = malloc(PKI_MAX_DN_FIELD_LEN);
memset(dn->locality, 0, PKI_MAX_DN_FIELD_LEN);
ret = snprintf(dn->locality, PKI_MAX_DN_FIELD_LEN,
"%s", locality);
if(ret <= 0) {
ret = PKI_INVALID_INPUT;
goto cleanup;
}
}
if(organization) {
dn->organization = malloc(PKI_MAX_DN_FIELD_LEN);
memset(dn->organization, 0, PKI_MAX_DN_FIELD_LEN);
ret = snprintf(dn->organization, PKI_MAX_DN_FIELD_LEN,
"%s", organization);
if(ret <= 0) {
ret = PKI_INVALID_INPUT;
goto cleanup;
}
}
if(organizational_unit) {
dn->organizational_unit = malloc(PKI_MAX_DN_FIELD_LEN);
memset(dn->organizational_unit, 0, PKI_MAX_DN_FIELD_LEN);
ret = snprintf(dn->organizational_unit, PKI_MAX_DN_FIELD_LEN,
"%s", organizational_unit);
if(ret <= 0) {
ret = PKI_INVALID_INPUT;
goto cleanup;
}
}
dn->common_name = malloc(PKI_MAX_DN_FIELD_LEN);
memset(dn->common_name, 0, PKI_MAX_DN_FIELD_LEN);
ret = snprintf(dn->common_name, PKI_MAX_DN_FIELD_LEN,
"%s", common_name);
if(ret <= 0) {
ret = PKI_INVALID_INPUT;
goto cleanup;
}
dn->email = malloc(PKI_MAX_DN_FIELD_LEN);
memset(dn->email, 0, PKI_MAX_DN_FIELD_LEN);
ret = snprintf(dn->email, PKI_MAX_DN_FIELD_LEN,
"%s", email);
if(ret <= 0) {
ret = PKI_INVALID_INPUT;
goto cleanup;
}
/* Regexp checks */
ret = pki_check_dn(dn);
cmd->conf->dn = dn;
cleanup:
if(ret) {
if(dn->country)
free(dn->country);
if(dn->state_province)
free(dn->state_province);
if(dn->locality)
free(dn->locality);
if(dn->organization)
free(dn->organization);
if(dn->organizational_unit)
free(dn->organizational_unit);
if(dn->common_name)
free(dn->common_name);
free(dn);
}
return ret;
}
/** EXTENSIONS **\
/**
* pki_add_san - Adds a Subject Alternative Name to configuration
*
* Tries to add a SAN entry to the config struct so that
* it can later be processed by the library-specific code
*
* @struct pki_cmd *cmd - The command from above
* @char *string - The SAN name string
*
* returns: One of pki_error_codes
*/
int
pki_add_san(struct pki_cmd *cmd, const char* string)
{
struct pki_extensions* exts = NULL;
struct pki_config* conf = cmd->conf;
int i = 0;
int first_ext = 0;
int first_san = 0;
int ret = PKI_OK;
if(!conf->exts) {
conf->exts = malloc(sizeof(struct pki_extensions));
if(!conf->exts) {
pki_msg(0, "INPUT",
"Unable to allocate extensions !\n");
ret = PKI_NOMEM_ERR;
return ret;
}
first_ext = 1;
memset(conf->exts, 0,
sizeof(struct pki_extensions));
}
exts = conf->exts;
if(!exts->num_sans) {
exts->sans = (struct pki_gn**) malloc(PKI_SAN_MAXNUM *
sizeof(struct pki_gn *));
if(!exts->sans) {
pki_msg(0, "INPUT",
"Unable to allocate array of SANs !\n");
ret = PKI_NOMEM_ERR;
goto cleanup;
}
first_san = 1;
exts->sans[0] = (struct pki_gn*) malloc(PKI_SAN_MAXNUM *
sizeof(struct pki_gn));
if(!exts->sans[0]) {
pki_msg(0, "INPUT",
"Unable to allocate SANs !\n", i);
ret = PKI_NOMEM_ERR;