/
config.c
1217 lines (923 loc) · 28.5 KB
/
config.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
/*
* OCSP responder
* by Massimiliano Pala (madwolf@openca.org)
* OpenCA project 2001
*
* Copyright (c) 2001 The OpenCA Project. All rights reserved.
*
* ====================================================================
*
* This product includes cryptographic software written by Eric Young
* (eay@cryptsoft.com). This product includes software written by Tim
* Hudson (tjh@cryptsoft.com).
*
*/
#include "general.h"
/* External imported variables */
extern OCSPD_CONFIG * ocspd_conf;
/* Functions */
OCSPD_CONFIG * OCSPD_load_config(char *configfile)
{
OCSPD_CONFIG *h = NULL;
PKI_CONFIG *cnf = NULL;
PKI_CONFIG_STACK *ca_config_stack = NULL;
char *tmp_s = NULL;
char *tmp_s2 = NULL;
int i;
/* Check for the environment variable PRQP_CONF */
if (configfile == NULL) configfile = getenv("OCSPD_CONF");
/* If not, check for the default CONFIG_FILE */
if (configfile == NULL) configfile = CONFIG_FILE;
if( !configfile ) {
/* No config file is available */
PKI_log(PKI_LOG_ERR, "No config file provided!");
return (NULL);
}
/* Load the config file */
if(( cnf = PKI_CONFIG_load ( configfile )) == NULL ) {
PKI_log( PKI_LOG_ERR, "Can not load config file [%s]!",
configfile );
return (NULL);
}
if(( h = (OCSPD_CONFIG *)PKI_Malloc(sizeof(OCSPD_CONFIG))) == NULL) {
PKI_ERROR(PKI_ERR_MEMORY_ALLOC, NULL);
goto err;
}
/* Set the group and user string to NULL */
h->user = NULL;
h->group = NULL;
/* Set the PRQPD verbose status */
h->verbose = 0;
h->debug = 0;
h->nthreads = 5;
h->http_proto = "1.0";
h->max_timeout_secs = 5;
h->crl_auto_reload = 3600;
h->crl_reload_expired = 1;
h->crl_check_validity = 600;
/* Copy the config filename so that it could be re-loaded on SIGHUP */
h->cnf_filename = strdup( configfile );
/* Initialize the COND variables and MUTEXES */
for( i = 0; i < sizeof ( h->mutexes ) / sizeof( PKI_MUTEX ); i++ )
{
PKI_MUTEX_init ( &h->mutexes[i] );
}
for( i = 0; i < sizeof ( h->condVars ) / sizeof( PKI_COND ); i++)
{
PKI_COND_init ( &h->condVars[i] );
}
PKI_RWLOCK_init ( &h->crl_lock );
/* Token Initialization */
if (( tmp_s = PKI_CONFIG_get_value( cnf, "/serverConfig/general/pkiConfigDir")) == NULL)
{
PKI_log_err("Missing pkiConfigDir in configuration!");
return NULL;
}
else
{
if ((tmp_s2 = PKI_CONFIG_get_value( cnf, "/serverConfig/general/token" )) != NULL)
{
h->token_name = strdup( tmp_s2 );
h->token_config_dir = strdup ( tmp_s );
if ((h->token = PKI_TOKEN_new_null()) == NULL)
{
PKI_log( PKI_LOG_ERR, "Memory error for new token");
exit(1);
}
PKI_Free(tmp_s2);
}
else
{
PKI_log_err("No General Token provided in configuration.");
PKI_Free(tmp_s);
return NULL;
}
PKI_Free(tmp_s);
}
/* Thread configuration */
if((tmp_s = PKI_CONFIG_get_value(cnf, "/serverConfig/general/spawnThreads")) != NULL)
{
int t = 0;
if((t = atoi( tmp_s )) > 0 ) h->nthreads = t;
PKI_Free(tmp_s);
}
if((tmp_s = PKI_CONFIG_get_value( cnf, "/serverConfig/general/caConfigDir")) != NULL)
{
h->ca_config_dir = strdup(tmp_s);
ca_config_stack = PKI_CONFIG_load_dir(h->ca_config_dir, NULL);
if (ca_config_stack == NULL)
{
PKI_log( PKI_LOG_ERR, "Can't load caConfigDir (%s)", h->ca_config_dir);
PKI_Free(tmp_s);
goto err;
}
PKI_Free(tmp_s);
}
else
{
PKI_log( PKI_LOG_ERR, "/serverConfig/general/caConfigDir needed in conf!\n");
goto err;
}
/* Pid File */
if((tmp_s = PKI_CONFIG_get_value( cnf, "/serverConfig/general/pidFile")) != NULL )
{
h->pidfile = strdup(tmp_s);
PKI_Free(tmp_s);
}
/* AutoReload timeout */
if((tmp_s = PKI_CONFIG_get_value( cnf,
"/serverConfig/general/crlAutoReload")) != NULL)
{
h->crl_auto_reload = atoi(tmp_s);
if( h->crl_auto_reload <= 0 )
{
h->crl_auto_reload = 0;
PKI_log(PKI_LOG_INFO, "Auto Reload Disabled");
}
PKI_Free(tmp_s);
}
/* CRL validity check timeout */
if((tmp_s = PKI_CONFIG_get_value( cnf,
"/serverConfig/general/crlCheckValidity")) != NULL )
{
h->crl_check_validity = atoi(tmp_s);
if ( h->crl_check_validity <= 0 )
{
h->crl_check_validity = 0;
PKI_log(PKI_LOG_INFO, "CRL check validity disabled");
}
PKI_Free(tmp_s);
}
/* AutoReload timeout */
if ((tmp_s = PKI_CONFIG_get_value( cnf,
"/serverConfig/general/crlReloadExpired")) != NULL )
{
if (strncmp_nocase(tmp_s, "n", 1) == 0)
{
h->crl_reload_expired = 0;
PKI_log(PKI_LOG_INFO, "Expired CRLs Reload Disabled");
}
PKI_Free(tmp_s);
}
/* Server Privileges */
if ((tmp_s = PKI_CONFIG_get_value(cnf, "/serverConfig/security/user")) != NULL)
{
h->user = strdup(tmp_s);
PKI_Free(tmp_s);
}
if ((tmp_s = PKI_CONFIG_get_value( cnf, "/serverConfig/security/group" )) != NULL)
{
h->group = strdup(tmp_s);
PKI_Free(tmp_s);
}
if ((tmp_s = PKI_CONFIG_get_value( cnf, "/serverConfig/security/chrootDir" )) != NULL )
{
h->chroot_dir = strdup(tmp_s);
PKI_Free(tmp_s);
}
/* Bind Address */
if((tmp_s = PKI_CONFIG_get_value( cnf, "/serverConfig/network/bindAddress" )) == NULL)
{
// If not bindAddress, let's use the universal one
tmp_s = strdup("http://0.0.0.0:2560");
}
if ((h->bindUrl = URL_new( tmp_s )) == NULL)
{
PKI_log( PKI_LOG_ERR, "Can't parse bindAddress (%s)", tmp_s );
PKI_Free(tmp_s);
goto err;
}
// We need to free the tmp_s
PKI_Free(tmp_s);
/* HTTP Version */
if((tmp_s = PKI_CONFIG_get_value( cnf, "/serverConfig/network/httpProtocol")) != NULL)
{
h->http_proto = strdup(tmp_s);
PKI_Free(tmp_s);
}
/* Timeout for incoming connections */
if((tmp_s = PKI_CONFIG_get_value( cnf, "/serverConfig/network/timeOut")) != NULL )
{
long t = 0;
if ((t = atol( tmp_s )) > 0) h->max_timeout_secs = (unsigned int) t;
PKI_Free(tmp_s);
}
/* Maximum Request Size */
if((tmp_s = PKI_CONFIG_get_value( cnf,
"/serverConfig/response/maxReqSize" )) != NULL ) {
int t = 0;
if((t = atoi( tmp_s )) > 0 ) {
h->max_req_size = t;
}
PKI_Free(tmp_s);
}
// Default
h->digest = PKI_DIGEST_ALG_SHA1;
/* Digest Algorithm to be used */
if ((tmp_s = PKI_CONFIG_get_value(cnf, "/serverConfig/response/digestAlgorithm" )) != NULL)
{
h->digest = PKI_DIGEST_ALG_get_by_name( tmp_s );
if (!h->digest)
{
PKI_log_err("Can not parse response digest algorithm: %s", tmp_s);
exit(1);
}
else PKI_log_debug("Selected response digest algorithm: %s", tmp_s);
PKI_Free(tmp_s);
}
/* Signing Digest Algorithm to be used */
if((tmp_s = PKI_CONFIG_get_value( cnf,
"/serverConfig/response/signatureDigestAlgorithm" )) == NULL)
{
PKI_log_debug("No specific signature digest algorithm selected.");
h->sigDigest = NULL;
}
else
{
h->sigDigest = PKI_DIGEST_ALG_get_by_name( tmp_s );
if (!h->sigDigest)
{
PKI_log_err("Can not parse signing digest algorithm: %s", tmp_s);
exit(1);
}
else PKI_log_debug("Selected signature digest algorithm: %s", tmp_s);
PKI_Free(tmp_s);
}
/* Now Parse the PRQP Response Section */
if ((tmp_s = PKI_CONFIG_get_value( cnf, "/serverConfig/response/validity/days" )) != NULL)
{
h->ndays = atoi(tmp_s);
PKI_Free(tmp_s);
}
if ((tmp_s = PKI_CONFIG_get_value( cnf, "/serverConfig/response/validity/mins" )) != NULL)
{
h->nmin = atoi(tmp_s);
PKI_Free(tmp_s);
}
h->set_nextUpdate = h->ndays * 3600 + h->nmin * 60;
/* Database Options */
if ((tmp_s = PKI_CONFIG_get_value( cnf, "/serverConfig/general/dbUrl")) != NULL)
{
if ((h->db_url = URL_new ( tmp_s )) == NULL)
{
PKI_log_err ( "Database Url not parsable (%s)", tmp_s );
PKI_Free(tmp_s);
goto err;
}
PKI_Free(tmp_s);
}
/* Database Persistant */
if ((tmp_s = PKI_CONFIG_get_value( cnf, "/serverConfig/general/dbPersistant")) != NULL)
{
if (strncmp_nocase ( "n", tmp_s, 1 ) == 0 )
h->db_persistant = 0;
else
h->db_persistant = 1;
PKI_Free(tmp_s);
}
/* Now we should load the CA configuration files and generate the
CERT_ID for the different CAs */
if ((OCSPD_build_ca_list( h, ca_config_stack )) == PKI_ERR )
{
PKI_log(PKI_LOG_ERR, "Can not build CA list!");
if (ca_config_stack) PKI_STACK_CONFIG_free ( ca_config_stack );
goto err;
}
if (ca_config_stack) PKI_STACK_CONFIG_free ( ca_config_stack );
return ( h );
err:
if( ca_config_stack ) PKI_STACK_CONFIG_free ( ca_config_stack );
if( cnf ) PKI_CONFIG_free ( cnf );
if( h ) PKI_Free ( h );
return( NULL );
}
int OCSPD_build_ca_list ( OCSPD_CONFIG *handler,
PKI_CONFIG_STACK *ca_conf_sk) {
int i = 0;
PKI_STACK *ca_list = NULL;
PKI_log_debug("Building CA List");
if ( !ca_conf_sk ) {
PKI_log( PKI_LOG_ERR, "No stack of ca configs!");
return ( PKI_ERR );
}
if((ca_list = PKI_STACK_new((void (*))CA_LIST_ENTRY_free)) == NULL ) {
PKI_log_err ( "Memory Error");
return ( PKI_ERR );
}
for (i = 0; i < PKI_STACK_CONFIG_elements( ca_conf_sk ); i++)
{
char *tmp_s = NULL;
URL *tmp_url = NULL;
PKI_X509_CERT *tmp_cert = NULL;
CA_LIST_ENTRY *ca = NULL;
PKI_CONFIG *cnf = NULL;
char * tmp_id = NULL;
// Get the current Configureation file
cnf = PKI_STACK_CONFIG_get_num(ca_conf_sk, i);
if (!cnf) continue;
// Get the name of the CA config
if (tmp_id) PKI_Free(tmp_id);
if ((tmp_id = PKI_CONFIG_get_value( cnf, "/caConfig/name" )) == NULL) {
tmp_id = strdup("<ca-config-no-name-set>");
}
// Some Useful Info
PKI_log(PKI_LOG_ALWAYS, "Processing Configuration for [CA: %s]", tmp_id);
/* Get the CA cert from the cfg file itself */
if ((tmp_s = PKI_CONFIG_get_value(cnf, "/caConfig/caCertValue")) == NULL)
{
char *subTmp_s = NULL;
/* Gets the URL for the CA cert */
if ((subTmp_s = PKI_CONFIG_get_value(cnf,"/caConfig/caCertUrl")) == NULL) {
// Error, no URL for the CA certificate
PKI_log_err("Can not get /caConfig/caCertUrl");
// Free the memory
PKI_Free(tmp_s);
PKI_Free(tmp_id);
// Proceed to the entry
continue;
}
// Get the CA parsed url
if((tmp_url = URL_new(subTmp_s)) == NULL )
{
// Error, can not parse url data
PKI_log_err("Can not parse CA cert url [CA: %s, URL: %s]",
tmp_id, PKI_CONFIG_get_value(cnf, "/caConfig/caCertUrl"));
// Free the memory
PKI_Free(tmp_s);
PKI_Free(tmp_id);
PKI_Free(subTmp_s);
// Proceed to the entry
continue;
}
// Free the memory
PKI_Free(subTmp_s);
subTmp_s = NULL;
// Retrieves the CA cert
if ((tmp_cert = PKI_X509_CERT_get_url(tmp_url, NULL, NULL ))== NULL)
{
// Error, can not get the CA certificate from the
// provided URL in the configuration
PKI_log_err("Can not get CA cert [CA: %s, URL: %s]",
tmp_url->url_s, tmp_id);
// Free the memory
PKI_Free(tmp_s);
PKI_Free(tmp_id);
URL_free (tmp_url);
// Proceed to the next entry
continue;
}
} else {
PKI_X509_CERT_STACK *cc_sk = NULL;
PKI_MEM *mm = NULL;
// Allocates a new PKI_MEM strcuture
if((mm = PKI_MEM_new_data(strlen(tmp_s), tmp_s)) == NULL ) {
// Reports the Error
PKI_ERROR(PKI_ERR_MEMORY_ALLOC, NULL);
// Free Memory
PKI_Free(tmp_s);
PKI_Free(tmp_id);
// Proceed to the next entry
continue;
}
// Parses and get the stack of X509_CERT from the PKI_MEM data
if ((cc_sk = PKI_X509_CERT_STACK_get_mem(mm, NULL)) == NULL) {
// Error, can not get the stack of certs from the CA cert value
PKI_log_err("Can not parse cert from /caConfig/caCertValue [CA: %s]",
tmp_id);
// Free the Memory
PKI_Free(tmp_s);
PKI_Free(tmp_id);
// Proceed to the next entry
continue;
}
// Gets the certificate from the stack (although there should be
// only one, the interface allows for multiple to be stored there
// concatenated, we just get the first one)
if ((tmp_cert = PKI_STACK_X509_CERT_pop( cc_sk )) == NULL) {
// Error, can not get the CA cert from the stack
PKI_log_err("No elements on stack from /caConfig/caCertValue [CA: %s]",
tmp_id);
// Free the remaining elements (if any) and data structures
PKI_STACK_X509_CERT_free_all(cc_sk);
// Free the memory
PKI_Free(tmp_s);
PKI_Free(tmp_id);
// Continue to the next element
continue;
}
// Free the memory
PKI_STACK_X509_CERT_free_all(cc_sk);
PKI_Free(tmp_s);
}
/* OCSPD create the CA entry */
if ((ca = CA_LIST_ENTRY_new()) == NULL )
{
PKI_log_err ( "CA List structure init error");
/* remember to do THIS!!!! */
if( tmp_url ) URL_free ( tmp_url );
if( tmp_cert ) PKI_X509_CERT_free ( tmp_cert );
continue;
}
// Assigns the ownership of the CA Cert to the CA Entry
ca->ca_cert = tmp_cert;
tmp_cert = NULL;
// Assignes the ownership of the CA URL to the CA Entry
ca->ca_url = tmp_url;
tmp_url = NULL;
// Copies the CA Identifier to the CA Entry
ca->ca_id = tmp_id;
// Generates a new CERTID entry
ca->cid = CA_ENTRY_CERTID_new(ca->ca_cert, handler->digest);
/* Get the CRL URL and the CRL itself */
if ((tmp_s = PKI_CONFIG_get_value(cnf, "/caConfig/crlUrl")) == NULL)
{
PKI_STACK *cdp_sk = NULL;
/* Now let's get it from PRQP */
/* Now from the Certificate */
if ((cdp_sk = PKI_X509_CERT_get_cdp (ca->ca_cert)) == NULL) {
// No source for the CRL Distribution Point
PKI_log_err("No URL for CRLs Download, disabling CA [CA: %s]",
ca->ca_id );
// Free Memory
CA_LIST_ENTRY_free ( ca );
// Continue
continue;
}
// Cycles through the stack of CDP retrieved
while ((tmp_s = PKI_STACK_pop ( cdp_sk )) != NULL)
{
// If we can not parse the URL, it skips it
if ((ca->crl_url = URL_new ( tmp_s )) == NULL) {
// Error, Can not parse the URL
PKI_log_err( "CDP URL not in the right format [CA: %s, URL: %s",
ca->ca_id, (tmp_s != NULL ? tmp_s : "<null>"));
// Free Memory
PKI_Free(tmp_s);
CA_LIST_ENTRY_free(ca);
// Continue
continue;
}
// Frees the Memory
if( tmp_s ) PKI_Free ( tmp_s );
// Exits from the cycle (we just record the first one)
break;
}
} else {
// Some logging info
PKI_log_debug("CRL Downloading Process Started [CA: %s, URL: %s]",
ca->ca_id, tmp_s );
// Gets the data from the CRL URL
if ((ca->crl_url = URL_new(tmp_s)) == NULL) {
// Error, can not get a new URL
PKI_log_err("Cannot parse CRL's URL [CA: %s, URL: %s]",
ca->ca_id, tmp_s);
// Free Memory
CA_LIST_ENTRY_free ( ca );
PKI_Free(tmp_s);
// Take it from the top
continue;
}
// Free Memory
PKI_Free(tmp_s);
tmp_s = NULL; // Safety
}
// Loads the CRL from the parsed URL
// Failure loading the CRL might not be a fatal error,
// we let the OCSP continue since the loading error
// might be temporary
if (PKI_OK != OCSPD_load_crl(ca, handler) == PKI_ERR) {
// Error Output
PKI_log_err("Can not load CRL [CA: %s]", ca->ca_id);
// Free Memory
CA_LIST_ENTRY_free ( ca );
// Continue
continue;
// TODO: Provide options to set SSL configuration for
// outgoing connections
/*
if (ca->crl_url->ssl == 1) {
int idx = 0;
// Index
PKI_SSL * ssl = NULL;
// PKI_SSL structure for creds
PKI_X509_CERT_STACK *sk = NULL;
// Stack of Trused Certificates
// Allocates the PKI_CRED structure
if ((ca->creds = PKI_CRED_new()) == NULL) {
PKI_ERROR(PKI_ERR_MEMORY_ALLOC, NULL);
return -1;
}
// Allocates the PKI_SSL structure
if ((ssl = PKI_SSL_new(NULL)) == NULL) {
PKI_ERROR(PKI_ERR_MEMORY_ALLOC, NULL);
CA_LIST_ENTRY_free(ca);
continue;
}
if ((sk = PKI_X509_CERT_STACK_get(trusted_certs, NULL, NULL)) == 0) {
PKI_log_err("Can't load Trusted Certs from %s", trusted_certs);
}
if (PKI_SSL_set_trusted(ssl, sk) != PKI_OK) {
PKI_log_err("Can't set the Trusted Certs for TLS connections.");
}
// Set the verify options for TLS
PKI_SSL_set_verify(ssl, PKI_SSL_VERIFY_NONE);
// Sets the Cipher
PKI_SSL_set_cipher(ssl, );
// Sets the SSL flags
PKI_SSL_set_flags(ssl, );
// Set the SSL object for the credentials
PKI_CRED_set_ssl(ca->creds, ssl);
}
*/
}
// If the Server has a Token to be used with this CA, let's
// load it
if ((tmp_s = PKI_CONFIG_get_value(cnf, "/caConfig/serverToken")) == NULL) {
// No token in config, let's see if a specific cert
// is configured
ca->token = NULL;
if ((tmp_s = PKI_CONFIG_get_value(cnf, "/caConfig/serverCertUrl")) == NULL) {
// No cert is configured, we will use the defaults
ca->server_cert = NULL;
}
else
{
// The Server's cert URL is found, let's load the certificate
if ((tmp_cert = PKI_X509_CERT_get(tmp_s, NULL, NULL)) == NULL) {
// Error, can not get the certificate from the URL
PKI_log_err("Can not get server's cert [CA: %s, URL: %s]",
ca->ca_id, tmp_s );
// Free Memory
CA_LIST_ENTRY_free ( ca );
PKI_Free(tmp_s);
// Next Entry please...
continue;
} else {
// Transfer ownership of the cert to the CA Entry
ca->server_cert = tmp_cert;
}
// Free Memory
PKI_Free(tmp_s);
tmp_s = NULL; // Safety
}
} else {
// A Token for this CA is found - we do not load
// it to avoid problems with Thread Initialization
ca->server_cert = NULL;
ca->token_name = tmp_s;
ca->token = PKI_TOKEN_new_null();
// Gets the configuration directory
if ((tmp_s = PKI_CONFIG_get_value(cnf,
"/caConfig/pkiConfigDir")) != NULL) {
// Save the directory in the CA Entry
ca->token_config_dir = strdup(tmp_s);
// Free the Memory
PKI_Free(tmp_s);
} else {
// No config directory was given, let's duplicate the
// handler's one
ca->token_config_dir = strdup(handler->token_config_dir);
}
}
// Checks if the CA is marked as compromised
if ((tmp_s = PKI_CONFIG_get_value(cnf,
"/caConfig/caCompromised")) != NULL) {
// CA is marked as Compromised
ca->compromised = atoi(tmp_s);
// Free the memory
PKI_Free(tmp_s);
} else {
// CA is NOT marked as compromised
ca->compromised = 0;
}
// Responder Id Type
if ((tmp_s = PKI_CONFIG_get_value(cnf, "/caConfig/responderIdType")) != NULL)
{
if (strncmp_nocase(tmp_s, "keyid", 5) == 0)
{
ca->response_id_type = PKI_X509_OCSP_RESPID_TYPE_BY_KEYID;
}
else if (strncmp_nocase(tmp_s, "name", 4) == 0)
{
ca->response_id_type = PKI_X509_OCSP_RESPID_TYPE_BY_NAME;
}
else
{
PKI_log_err("Can not parse responderIdType: %s (allowed 'keyid' or 'name')", tmp_s);
exit(1);
}
PKI_Free(tmp_s);
}
else
{
// Default Value
ca->response_id_type = PKI_X509_OCSP_RESPID_TYPE_BY_KEYID;
}
// Now let's add the CA_LIST_ENTRY to the list of configured CAs
PKI_STACK_push ( ca_list, ca );
}
// Saves the CA Entries list in the main Handler
handler->ca_list = ca_list;
// All Done
return PKI_OK;
}
int OCSPD_load_crl ( CA_LIST_ENTRY *ca, OCSPD_CONFIG *conf ) {
int ret = 0;
char * x_name = NULL;
// Input Checks
if (!ca) return PKI_ERR;
// Checks we have a good URL to process
if (!ca->crl_url) {
PKI_log_err ("Can not load CRL because its URL is empty [CA: %s]",
ca->ca_id );
return PKI_ERR;
}
// Free any existing CRLs
if (ca->crl) PKI_X509_CRL_free(ca->crl);
// Load the new CRL
if (( ca->crl = PKI_X509_CRL_get_url(ca->crl_url,
NULL, NULL )) == NULL) {
// Error, can not get the CRL from the URL
PKI_log_err("Failed loading CRL for [CA: %s, URL: %s]",
ca->ca_id, ca->crl_url->url_s );
// Failed
return PKI_ERR;
} else {
// Some debugging Information
PKI_log_debug("CRL loaded successfully [URL: %s]", ca->crl_url->url_s);
}
// Let's check the CRL against the CA certificate
if( (ret = check_crl(ca->crl, ca->ca_cert, conf)) < 1 ) {
// Error, the check for the CRL failed
PKI_log_err( "CRL/CA check error [CA: %s, Code: %d ]",
ca->ca_id, ret );
// Failed
return PKI_ERR;
} else {
// Some debugging Information
PKI_log_debug("CRL Signature verified successfully [URL: %s]",
ca->crl_url->url_s);
}
// Now we copy the lastUpdate and nextUpdate fields
ca->lastUpdate = PKI_TIME_dup(
PKI_X509_CRL_get_data (ca->crl,
PKI_X509_DATA_LASTUPDATE));
ca->nextUpdate = PKI_TIME_dup (
PKI_X509_CRL_get_data (ca->crl,
PKI_X509_DATA_NEXTUPDATE ));
if ((ca->crl_status = check_crl_validity(ca, conf )) == CRL_OK ) {
// Some debugging info
PKI_log(PKI_LOG_INFO, "CRL Validity check PASSED [CA: %s]",
ca->ca_id);
} else {
// Error, can not verify the CRL validity
PKI_log_err ( "CRL Validity check FAILED [CA: %s, Code: %d]", ca->ca_id,
ca->crl_status );
}
// Let's get the CRLs entries, if any
if( ocspd_build_crl_entries_list ( ca, ca->crl ) == NULL ) {
// Info, reports the fact that there are no entries in the CRL
PKI_log(PKI_LOG_INFO, "CRL has 0 (Zero) Entries [CA: %s]", ca->ca_id );
}
// Some Info
PKI_log(PKI_LOG_ALWAYS, "CRL loaded and activated successfully [CA: %s]",
ca->ca_id );
// Success
return PKI_OK;
}
int ocspd_reload_all_ca ( OCSPD_CONFIG *conf ) {
int i=0;
CA_LIST_ENTRY *ca = NULL;
// Goes through all the configured CA Entries
for( i = 0; i < PKI_STACK_elements(conf->ca_list); i++) {
// Gets the Entry in the CA list
ca = PKI_STACK_get_num(conf->ca_list, i);
// If this CA cert was from a URL, let's reload it
if (ca->ca_url) {
// Free the old CA Certificate
if (ca->ca_cert) PKI_X509_CERT_free(ca->ca_cert);
// Get the CA certificate
if ((ca->ca_cert = PKI_X509_CERT_get_url(ca->ca_url,
NULL, NULL )) == NULL) {
// Can not get the CA Cert from the URL
PKI_log_err("Can not load CA cert [CA: %s, URL: %s]",
ca->ca_id, ca->ca_url->url_s);
// Next Entry
continue;
}
} else {
// Missing the CA certificate's URL
PKI_log_err("Can not load CA cert [CA: %s]",
ca->ca_id);
// Next Entry
continue;
}
// Some Logging Info
PKI_log(PKI_LOG_INFO, "CA Certificate loaded successfully [CA: %s]",
ca->ca_id );
// Free existing memory
if (ca->cid) CA_ENTRY_CERTID_free(ca->cid);
ca->cid = NULL;
// Generates anew CA Entry
if ((ca->cid = CA_ENTRY_CERTID_new(ca->ca_cert,
conf->digest)) == NULL ) {
PKI_log_err( "CA List structure init error (Can not create a new CERTID) [CA: %s]",
ca->ca_id);
continue;
}
}
// Success
return 1;
}
STACK_OF(X509_REVOKED) *ocspd_build_crl_entries_list(CA_LIST_ENTRY *ca,
PKI_X509_CRL *crl) {
long rev_num = 0;
STACK_OF(X509_REVOKED) *ret = NULL;
PKI_X509_CRL_VALUE *crl_val = NULL;
// Input Checks
if (!ca || !crl || !crl->value) {
// Input Error
PKI_ERROR(PKI_ERR_PARAM_NULL, NULL);
// Error
return NULL;
}
// Gets the reference to the internal CRL value
crl_val = crl->value;
// Gets the reference to the revoked stack
ret = X509_CRL_get_REVOKED(crl_val);
// Gets the number of revoked entries in the CRL
rev_num = sk_X509_REVOKED_num(ret);
// Some debugging info
PKI_log_debug("CRL Entries are %ld [CA: %s]",
(rev_num > 0 ? rev_num : 0), ca->ca_id );
// Saves the returned reference in the CA Entry
ca->entries_num = (unsigned long) rev_num;
ca->crl_list = ret;
// Checks that we have entries if the number of
// X509_REVOKED is non-negative
if ((rev_num > -1) && (ca->crl_list == NULL)) {
// Some internal memory error
PKI_ERROR(PKI_ERR_MEMORY_ALLOC, NULL);
// Error
return NULL;
}
// Sorts the X509_REVOKED list (faster lookup)
sk_X509_REVOKED_sort(ca->crl_list);