-
Notifications
You must be signed in to change notification settings - Fork 2
/
pkicore_openssl_extensions.c
1030 lines (891 loc) · 24.5 KB
/
pkicore_openssl_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
/*
* X509v3 Extension handling routines - OpenSSL specific
*
* Copyright (c) 2011 Nick Kossifidis <mickflemm@gmail.com>
*
* 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 <stdio.h>
#include "pkicore.h"
#include "pkicore_openssl.h"
#include "pkicore_openssl_extensions.h"
/* WARNING, UGLY STUFF FOLLOWS !!!:
*
* OpenSSL is designed around a configuration file approach.
* That means that there are no functions to easily let you
* get/set extensions on a certificate/certificate request.
* Instead you should re-create the configuration file entries
* as strings and pass them to X509V3_EXT_conf_nid. That's for
* the entries that can be represented as one line in the
* config file. For more complex objects that need to be represented
* with multiple values (like CRL distribution points) things
* are even more ugly.
*
* The other solution is to DER-encode them and pass them directly to
* OpenSSL but this is too much of an overhead and I don't want
* to turn the code to a mess just because OpenSSL has a bad
* design. There is no middle ground (at least I couldn't find it),
* maybe the only thing we could do is to pass OIDs instead of
* aliases but again it'll be a string anyway and after all creating
* or signing certificates is not a time critical task.
*/
/* Note:
* There are no CSR extensions or CA extensions, all extensions
* can be applied on certificates and certificate requests BUT
* it doesn't make sense to put a distribution point or an authority
* information access extension on a CSR because these extensions are
* put there normaly by the Certificate Authority. Leting someone
* put a distribution point or aia on a CSR might introduce a security
* risk by giving the oportunity to bypass Certificate Authority's
* revocation mechanisms. That's why I 've marked distribution points
* and aiaps as "CA extensions" here and the rest as "CSR extensions",
* Note that we put CSR extensions also on signed certificates on
* pki_ossl_add_ca_extensions if we have some from above and we
* are not generating a self-signed certificate.
*/
/* Bit count functions used to cound how many
* key usage flags are set from above, in order
* to allocate that many "string slots" when
* generating the configuration string */
#ifdef __GNUC__
/* CPU's native bitcount */
static int bitcount(unsigned int num) {
return __builtin_popcount(num);
}
#else
/* Hamming Weight */
static int bitcount(unsigned int num) {
num = num - ((num >> 1) & 0x55555555);
num = (num & 0x33333333) + ((num >> 2) & 0x33333333);
return ((num + (num >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
}
#endif
/* Mapping between our key usage flags and their aliases */
static const struct pki_ossl_ku_mapping key_usage_map[] = {
{PKI_KEY_USAGE_SIGN, "digitalSignature"},
{PKI_KEY_USAGE_NONREP, "nonRepudiation"},
{PKI_KEY_USAGE_KEYENC, "keyEncipherment"},
{PKI_KEY_USAGE_DATAENC, "dataEncipherment"},
{PKI_KEY_USAGE_KEYAGREEMENT, "keyAgreement"},
{PKI_KEY_USAGE_KEYCERTSIGN, "keyCertSign"},
{PKI_KEY_USAGE_CRLSIGN, "cRLSign"},
{PKI_KEY_USAGE_ENCONLY, "encipherOnly"},
{PKI_KEY_USAGE_DECONLY, "decipherOnly"}
};
/* Same for extended key usage
* Note: Some OIDs are not included in OpenSSL's objects.h
* but on obj_mac.h (duplicates etc) */
static const struct pki_ossl_ku_mapping ext_key_usage_map[] = {
{PKI_EXT_KEY_USAGE_SERVERAUTH, "serverAuth"},
{PKI_EXT_KEY_USAGE_CLIENTAUTH, "clientAuth"},
{PKI_EXT_KEY_USAGE_CODESIGN, "codeSigning"},
{PKI_EXT_KEY_USAGE_EMAILPROTECT, "emailProtection"},
{PKI_EXT_KEY_USAGE_IPSECENDSYS, "ipsecEndSystem"},
{PKI_EXT_KEY_USAGE_IPSECTUN, "ipsecTunnel"},
{PKI_EXT_KEY_USAGE_IPSECUSR, "ipsecUser"},
{PKI_EXT_KEY_USAGE_TIMESTAMP, "timeStamping"},
{PKI_EXT_KEY_USAGE_OCSPSIGN, "OCSPSigning"}
};
/*********\
* Helpers *
\*********/
/**
* pki_ossl_convert_gen_name - Convert a general name to OpenSSL's format
*
* Converts the given general name structure to OpenSSL's
* internal GENERAL_NAME structure. This is used when generating
* CRL distribution points.
*
* @struct pki_gn *gn - Our general name structure
*
* returns: A GENERAL_NAME structure or NULL + ret code
*/
static GENERAL_NAME*
pki_ossl_convert_gen_name(struct pki_gn *gn)
{
GENERAL_NAME *gen = NULL;
int type =0;
int ret = PKI_OK;
switch(gn->type) {
case PKI_SAN_TYPE_EMAIL:
type = GEN_EMAIL;
break;
case PKI_SAN_TYPE_DNS:
type = GEN_DNS;
break;
case PKI_SAN_TYPE_IP:
type = GEN_IPADD;
break;
case PKI_SAN_TYPE_URI:
type = GEN_URI;
break;
default:
pki_msg(0, "EXT",
"Invalid general name type !\n");
ret = PKI_INVALID_INPUT;
goto cleanup;
}
gen = GENERAL_NAME_new();
if(!gen) {
pki_msg(0, "EXT",
"Unable to allocate a general name structure !\n");
ret = PKI_NOMEM_ERR;
goto cleanup;
}
gen->type = type;
if(type == GEN_IPADD) {
gen->d.ip = a2i_IPADDRESS(gn->value);
} else {
gen->d.ia5 = NULL;
gen->d.ia5 = M_ASN1_IA5STRING_new();
if(!gen->d.ia5) {
pki_msg(0, "EXT",
"Could not convert gen name to ia5 string !\n");
ret = PKI_OPENSSL_ERR;
goto cleanup;
}
ret = ASN1_STRING_set(gen->d.ia5,
(unsigned char*)gn->value,
strlen(gn->value));
if(!ret) {
pki_msg(0, "EXT",
"Could not set gen name !\n");
ret = PKI_OPENSSL_ERR;
goto cleanup;
} else
ret = PKI_OK;
}
cleanup:
if(ret) {
if(gen)
free(gen);
gen = NULL;
pki_set_ret_code(ret);
}
return gen;
}
/**
* pki_ossl_copy_extensions - Copy CSR extensions to a Certificate
*
* Tries to copy extensions from the given CSR to the
* given certificate. If some extensions are already present
* on certificate (put there by the CA) we keep them instead
* and skip CSR's version for security reasons (we always trust
* the CA more than the one that generated the CSR).
*
* @X509_REQ *csr - The source CSR to copy extensions from
* @X509 *cert - The target Certificate to copy extensions to
*
* returns: One of pki_error_codes
*/
static int
pki_ossl_copy_extensions(X509_REQ *csr, X509 *cert)
{
STACK_OF(X509_EXTENSION) *exts = NULL;
X509_EXTENSION *ext = NULL;
ASN1_OBJECT *obj = NULL;
int ret = PKI_OK;
int idx = 0;
int i = 0;
/* If no extensions exist on csr, it's OK */
exts = X509_REQ_get_extensions(csr);
if(!exts) {
pki_msg(1, "EXT",
"No extensions found on CSR\n");
ret = PKI_OK;
goto cleanup;
}
for(i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
ext = sk_X509_EXTENSION_value(exts, i);
/* Check if extension already exists on
* certificate (previously added by CA) */
obj = X509_EXTENSION_get_object(ext);
idx = X509_get_ext_by_OBJ(cert, obj, -1);
/* If it does skip it and keep CA's
* version for security reasons */
if (idx != -1)
continue;
/* Copy to certificate */
ret = X509_add_ext(cert, ext, -1);
if(!ret) {
pki_msg(0, "EXT",
"Unable to copy extension (idx: %i) !\n",
idx);
ret = PKI_OPENSSL_ERR;
goto cleanup;
}
}
ret = PKI_OK;
cleanup:
if(exts)
sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
return ret;
}
/****************\
* CSR extensions *
\****************/
/**
* pki_ossl_add_key_usage - Adds the key usage extension on the given stack
*
* Tries to create the key usage extension based on given configuration
* and put it on the given stack.
*
* @struct pki_config *conf - The configuration struct
* @STACK_OF(X509_EXTENSION) *exts - The extension stack
*
* returns: One of pki_error_codes
*/
static int
pki_ossl_add_key_usage(struct pki_config *conf,
STACK_OF(X509_EXTENSION) *exts_sk)
{
struct pki_extensions* exts = conf->exts;
unsigned int ku_flags = exts->key_usage;
const struct pki_ossl_ku_mapping *ku_map = key_usage_map;
char* keyusage = NULL;
X509_EXTENSION *ext = NULL;
int size = ARRAY_SIZE(key_usage_map);
int num_ku_flags = bitcount(ku_flags);
int i = 0;
int c = 0;
int ret = PKI_OK;
c = num_ku_flags;
/* Include coma(s) */
keyusage = malloc(num_ku_flags * (MAX_KEY_USAGE_STRING_LENGTH + 1));
if (!keyusage)
return PKI_NOMEM_ERR;
memset(keyusage, 0, sizeof(keyusage));
for (i = 0; i < size; i++) {
if (ku_flags & ku_map[i].ku_flag) {
strncat(keyusage, ku_map[i].str,
strlen(ku_map[i].str));
c--;
if ((c < num_ku_flags) && (c > 0))
strncat(keyusage, ",", strlen(","));
}
}
/* Create extension */
ext = X509V3_EXT_conf_nid(NULL, NULL, NID_key_usage, keyusage);
if (!ext) {
pki_msg(0,"EXT",
"Unable to create key usage extension !\n");
ret = PKI_OPENSSL_ERR;
goto cleanup;
}
/* Push it to the stack */
ret = sk_X509_EXTENSION_push(exts_sk, ext);
if(!ret) {
pki_msg(0,"EXT",
"Unable to add key usage extension !\n");
ret = PKI_OPENSSL_ERR;
goto cleanup;
} else
ret = PKI_OK;
cleanup:
if(keyusage)
free(keyusage);
return ret;
}
/**
* pki_ossl_add_ext_key_usage - Adds the extended key usage extension
* on the given stack
*
* Tries to create the extended key usage extension based on given
* configuration and put it on the given stack.
*
* @struct pki_config *conf - The configuration struct
* @STACK_OF(X509_EXTENSION) *exts - The extension stack
*
* returns: One of pki_error_codes
*/
static int
pki_ossl_add_ext_key_usage(struct pki_config *conf,
STACK_OF(X509_EXTENSION) *exts_sk)
{
struct pki_extensions* exts = conf->exts;
unsigned int ku_flags = exts->ext_key_usage;
const struct pki_ossl_ku_mapping *ku_map = ext_key_usage_map;
char* extkeyusage = NULL;
X509_EXTENSION *ext = NULL;
int size = ARRAY_SIZE(ext_key_usage_map);
int num_ku_flags = bitcount(ku_flags);
int i = 0;
int c = 0;
int ret = PKI_OK;
c = num_ku_flags;
/* Include coma(s) */
extkeyusage = malloc(num_ku_flags * (MAX_KEY_USAGE_STRING_LENGTH + 1));
if (!extkeyusage)
return PKI_NOMEM_ERR;
memset(extkeyusage, 0, sizeof(extkeyusage));
for (i = 0; i < size; i++) {
if (ku_map[i].ku_flag & ku_flags) {
strncat(extkeyusage, ku_map[i].str,
strlen(ku_map[i].str));
c--;
if ((c < num_ku_flags) && (c > 0))
strncat(extkeyusage, ",", strlen(","));
}
}
/* Create extension */
ext = X509V3_EXT_conf_nid(NULL, NULL, NID_ext_key_usage, extkeyusage);
if (!ext) {
pki_msg(0,"EXT",
"Unable to create extended key usage extension !\n");
ret = PKI_OPENSSL_ERR;
goto cleanup;
}
/* Push it to the stack */
ret = sk_X509_EXTENSION_push(exts_sk, ext);
if(!ret) {
pki_msg(0,"EXT",
"Unable to add extended key usage extension !\n");
ret = PKI_OPENSSL_ERR;
goto cleanup;
} else
ret = PKI_OK;
cleanup:
if(extkeyusage)
free(extkeyusage);
return PKI_OK;
}
/**
* pki_ossl_add_sans - Adds the key Subject Alternative Names extension
* on the given stack
*
* Tries to create the Subject Alternative Names extension based on given
* configuration and put it on the given stack.
*
* @struct pki_config *conf - The configuration struct
* @STACK_OF(X509_EXTENSION) *exts - The extension stack
*
* returns: One of pki_error_codes
*/
static int
pki_ossl_add_sans(struct pki_config *conf, STACK_OF(X509_EXTENSION) *exts_sk)
{
char* prefix = NULL;
char* entry = NULL;
char* sanlist = NULL;
X509_EXTENSION *ext = NULL;
struct pki_extensions* exts = conf->exts;
int i = 0;
int ret = PKI_OK;
if(!exts->num_sans)
return PKI_INVALID_INPUT;
/* We add 7 chars to each entry
* because we'll add the alias as a prefix
* and 2 more characters:
* max alias -> "email" (5) + ":" (1) + "," (1)*/
entry = malloc(PKI_MAX_RES_LEN + 7);
sanlist = malloc(exts->num_sans * (PKI_MAX_RES_LEN + 7));
if (!sanlist)
return PKI_NOMEM_ERR;
memset(sanlist, 0, exts->num_sans * (PKI_MAX_RES_LEN + 7));
for (i = 0; i < exts->num_sans; i++) {
if(exts->sans[i]->type == PKI_SAN_TYPE_EMAIL)
prefix = "email";
else if(exts->sans[i]->type == PKI_SAN_TYPE_DNS)
prefix = "DNS";
else if(exts->sans[i]->type == PKI_SAN_TYPE_IP)
prefix = "IP";
else if(exts->sans[i]->type == PKI_SAN_TYPE_URI)
prefix = "URI";
else {
pki_msg(0,"EXT",
"Unknown SAN type !\n");
ret = PKI_INVALID_INPUT;
goto cleanup;
}
memset(entry, 0, PKI_MAX_RES_LEN + 7);
if(i == exts->num_sans -1)
snprintf(entry, PKI_MAX_RES_LEN, "%s:%s",
prefix, exts->sans[i]->value);
else
snprintf(entry, PKI_MAX_RES_LEN, "%s:%s,",
prefix, exts->sans[i]->value);
if(i == 0)
strncpy(sanlist, entry, strlen(entry));
else
strncat(sanlist, entry, strlen(entry));
}
/* Create extension */
ext = X509V3_EXT_conf_nid(NULL, NULL, NID_subject_alt_name,
sanlist);
if (!ext) {
pki_msg(0,"EXT",
"Unable to create SANS extension !\n");
ret = PKI_OPENSSL_ERR;
goto cleanup;
}
/* Push it to the stack */
ret = sk_X509_EXTENSION_push(exts_sk, ext);
if(!ret) {
pki_msg(0,"EXT",
"Unable to add SANS extension !\n");
ret = PKI_OPENSSL_ERR;
goto cleanup;
} else
ret = PKI_OK;
cleanup:
if(entry)
free(entry);
if(sanlist)
free(sanlist);
return ret;
}
/**
* pki_ossl_add_bc - Adds the Basic Constraints extension on the given stack
*
* Tries to create the Basic Constraints extension based on given configuration
* and put it on the given stack.
*
* @struct pki_config *conf - The configuration struct
* @STACK_OF(X509_EXTENSION) *exts - The extension stack
*
* returns: One of pki_error_codes
*/
static int
pki_ossl_add_bc(struct pki_config *conf, STACK_OF(X509_EXTENSION) *exts_sk)
{
char* bc = NULL;
X509_EXTENSION *ext = NULL;
struct pki_extensions* exts = conf->exts;
int ret = PKI_OK;
bc = malloc(MAX_BC_STRING_LENGTH);
if (!bc)
return PKI_NOMEM_ERR;
memset(bc, 0, sizeof(bc));
if (exts->bc->ca)
strncat(bc, "CA:TRUE", strlen("CA:TRUE"));
else
strncat(bc, "CA:FALSE", strlen("CA:FALSE"));
/* Add pathlen if present */
if (exts->bc->ca && exts->bc->pathlen) {
char* tmp = malloc(strlen(",pathlen:xxx"));
if(!tmp) {
pki_msg(0,"EXT",
"Unable to allocate tmp string !\n");
ret = PKI_NOMEM_ERR;
goto cleanup;
}
memset(tmp, 0, strlen(",pathlen:xxx"));
snprintf(tmp, strlen(",pathlen:xxx"), ",pathlen:%i,",
exts->bc->pathlen);
strncat(bc, tmp, strlen(tmp));
free(tmp);
}
/* Create extension */
ext = X509V3_EXT_conf_nid(NULL, NULL, NID_basic_constraints,
bc);
if (!ext) {
pki_msg(0,"EXT",
"Unable to create basic constraints extension !\n");
ret = PKI_OPENSSL_ERR;
goto cleanup;
}
/* Push it to the stack */
ret = sk_X509_EXTENSION_push(exts_sk, ext);
if(!ret) {
pki_msg(0,"EXT",
"Unable to add basic constraints extension !\n");
ret = PKI_OPENSSL_ERR;
goto cleanup;
} else
ret = PKI_OK;
cleanup:
if(bc)
free(bc);
return ret;
}
/***************\
* CA Extensions *
\***************/
/**
* pki_ossl_add_aiaps - Adds the Authority Information Access extension
* on the given certificate
*
* Tries to create the Authority Information Access extension based on
* given configuration and put it on the given certificate.
*
* @struct pki_config *conf - The configuration struct
* @X509 *cert - The target certificate
*
* returns: One of pki_error_codes
*/
static int
pki_ossl_add_aiaps(struct pki_config *conf, X509 *cert)
{
const char* prefix = NULL;
char* entry = NULL;
char* aialist = NULL;
X509_EXTENSION *ext = NULL;
struct pki_extensions* exts = conf->exts;
int i = 0;
int ret = PKI_OK;
/* max alias -> "caIssuers" (9) + ";" + "URI" (3) + ":" (1) + "," (1)*/
entry = malloc(PKI_MAX_RES_LEN + 14);
aialist = malloc(exts->num_aiaps * (PKI_MAX_RES_LEN + 14));
if (!aialist)
return PKI_NOMEM_ERR;
memset(aialist, 0, exts->num_aiaps * (PKI_MAX_RES_LEN + 14));
for (i = 0; i < exts->num_aiaps; i++) {
if(exts->aiaps[i]->type == PKI_AIA_TYPE_OCSP)
prefix = "OCSP";
else if(exts->aiaps[i]->type == PKI_AIA_TYPE_CAISSUERS)
prefix = "caIssuers";
else {
pki_msg(0,"EXT",
"Unknown AIA type !\n");
ret = PKI_INVALID_INPUT;
goto cleanup;
}
memset(entry, 0, (PKI_MAX_RES_LEN + 14));
if(i == exts->num_aiaps -1)
snprintf(entry, PKI_MAX_RES_LEN, "%s;URI:%s",
prefix, exts->aiaps[i]->loc->value);
else
snprintf(entry, PKI_MAX_RES_LEN, "%s;URI:%s,",
prefix, exts->aiaps[i]->loc->value);
if(i == 0)
strncpy(aialist, entry, strlen(entry));
else
strncat(aialist, entry, strlen(entry));
}
ext = X509V3_EXT_conf_nid(NULL, NULL, NID_info_access, aialist);
if(!ext) {
pki_msg(0, "EXT",
"Unable to create AIA extension !\n");
ret = PKI_OPENSSL_ERR;
goto cleanup;
}
ret = X509_add_ext(cert, ext, -1);
if(!ret) {
pki_msg(0, "EXT",
"Unable to add AIA extension (ret:%i) !\n", ret);
ret = PKI_OPENSSL_ERR;
goto cleanup;
} else
ret = PKI_OK;
cleanup:
if(aialist)
free(aialist);
if(entry)
free(entry);
if(ext)
X509_EXTENSION_free(ext);
return ret;
}
/**
* pki_ossl_add_dp - Adds a Distribution point to the given certificate
*
* Tries to create a CRL distribution point and put it on the given
* certificate. Note: The actual extension is created after we put all
* distribution points (check out pki_ossl_add_ca_extensions).
*
* @struct struct pki_dp *dp - The distribution point
* @X509 *cert - The target certificate
*
* returns: One of pki_error_codes
*/
static int
pki_ossl_add_dp(struct pki_dp *dp, X509 *cert)
{
DIST_POINT *point = NULL;
DIST_POINT_NAME *dpname = NULL;
GENERAL_NAME *fullname = NULL;
GENERAL_NAMES *fullname_sk = NULL;
GENERAL_NAME *issuer = NULL;
GENERAL_NAMES *issuer_sk = NULL;
X509_NAME *issuer_dn = NULL;
int type = 0;
int i = 0;
int res = 0;
int ret = PKI_OK;
point = DIST_POINT_new();
if(!point) {
pki_msg(0, "EXT",
"Unable to allocate a new distpoint !\n");
ret = PKI_NOMEM_ERR;
goto cleanup;
}
/* Set distpoint name */
fullname_sk = sk_GENERAL_NAME_new_null();
if(!fullname_sk) {
pki_msg(0, "EXT",
"Unable to allocate a stack of fullnames !\n");
ret = PKI_NOMEM_ERR;
goto cleanup;
}
fullname = pki_ossl_convert_gen_name(dp->fullname);
if(!fullname) {
ret = pki_get_ret_code();
goto cleanup;
}
ret = sk_GENERAL_NAME_push(fullname_sk, fullname);
if(!ret) {
pki_msg(0, "EXT",
"Unable to push fullname to stack !\n");
ret = PKI_OPENSSL_ERR;
goto cleanup;
} else
ret = PKI_OK;
dpname = DIST_POINT_NAME_new();
if(!dpname) {
pki_msg(0, "EXT",
"Could not allocate distpoint name !\n");
ret = PKI_NOMEM_ERR;
goto cleanup;
}
dpname->type = 0;
dpname->name.fullname = fullname_sk;
point->distpoint = dpname;
/* Set reasons */
point->reasons = NULL;
point->reasons = ASN1_BIT_STRING_new();
if(!point->reasons) {
pki_msg(0, "EXT",
"Unable to allocate dist point reasons bitstring !\n");
ret = PKI_NOMEM_ERR;
goto cleanup;
}
if (dp->reasons) {
for(i = 0; i <= PKI_CRL_REASON_BITS; i++) {
res = dp->reasons & (1 << i);
if(res)
ASN1_BIT_STRING_set_bit(point->reasons, i, 1);
}
if (point->reasons->length > 0)
point->dp_reasons = point->reasons->data[0];
if (point->reasons->length > 1)
point->dp_reasons |= (point->reasons->data[1] << 8);
point->dp_reasons &= CRLDP_ALL_REASONS;
} else
point->dp_reasons = CRLDP_ALL_REASONS;
/* Set issuer name */
issuer_sk = sk_GENERAL_NAME_new_null();
if(!issuer_sk) {
pki_msg(0, "EXT",
"Unable to allocate a stack of issuer names !\n");
ret = PKI_NOMEM_ERR;
goto cleanup;
}
issuer = pki_ossl_convert_gen_name(dp->issuer);
if(!issuer) {
ret = pki_get_ret_code();
goto cleanup;
}
ret = sk_GENERAL_NAME_push(issuer_sk, issuer);
if(!ret) {
pki_msg(0, "EXT",
"Unable to push issuer name to stack !\n");
ret = PKI_OPENSSL_ERR;
goto cleanup;
} else
ret = PKI_OK;
point->CRLissuer = issuer_sk;
issuer_dn = X509_get_issuer_name(cert);
if(!issuer_dn) {
pki_msg(0, "EXT",
"Unable to get certificate's issuer name !\n");
ret = PKI_OPENSSL_ERR;
goto cleanup;
}
DIST_POINT_set_dpname(point->distpoint, issuer_dn);
/* Push to stack */
if(!cert->crldp)
cert->crldp = sk_DIST_POINT_new_null();
ret = sk_DIST_POINT_push(cert->crldp, point);
if(!ret) {
pki_msg(0, "EXT",
"Unable to push dist point to the stack !\n");
ret = PKI_OPENSSL_ERR;
goto cleanup;
} else
ret = PKI_OK;
cleanup:
if(ret) {
if(fullname_sk)
sk_GENERAL_NAME_pop_free(fullname_sk,
GENERAL_NAME_free);
else if(fullname)
GENERAL_NAME_free(fullname);
if(dpname) {
dpname->name.fullname = NULL;
DIST_POINT_NAME_free(dpname);
}
if(point->reasons) {
ASN1_BIT_STRING_free(point->reasons);
point->reasons = NULL;
}
if(issuer_sk)
sk_GENERAL_NAME_pop_free(issuer_sk,
GENERAL_NAME_free);
else if(issuer)
GENERAL_NAME_free(issuer);
if(point)
DIST_POINT_free(point);
}
return ret;
}
/**************\
* Entry points *
\**************/
/**
* pki_ossl_add_csr_extensions - Adds extensions to the given CSR
*
* Tries to add extensions provided from above to the given
* Certificate Signing Request.
*
* @struct pki_cmd *cmd - The command structure from above
* @X509_REQ *csr - The CSR
*
* returns: One of pki_error_codes
*/
int
pki_ossl_add_csr_extensions(struct pki_cmd *cmd, X509_REQ *csr)
{
int ret = PKI_OK;
STACK_OF(X509_EXTENSION) *exts = NULL;
struct pki_config *conf = cmd->conf;
if(!conf->exts)
return PKI_OK;
exts = sk_X509_EXTENSION_new_null();
ret = X509_REQ_set_version(csr, 2);
if(!ret) {
pki_msg(0, "EXT",
"Unable to set CSR version !\n");
ret = PKI_OPENSSL_ERR;
goto cleanup;
} else
ret = PKI_OK;
if(conf->exts->sans) {
ret = pki_ossl_add_sans(conf, exts);
if(ret)
goto cleanup;
}
if(conf->exts->bc) {
ret = pki_ossl_add_bc(conf, exts);
if(ret)
goto cleanup;
}
if(conf->exts->key_usage) {
ret = pki_ossl_add_key_usage(conf, exts);
if(ret)
goto cleanup;
}
if(conf->exts->ext_key_usage) {
ret = pki_ossl_add_ext_key_usage(conf, exts);
if(ret)
goto cleanup;
}
ret = X509_REQ_add_extensions(csr, exts);
if(!ret) {
pki_msg(0, "EXT",
"Unable to add extensions to CSR !\n");
ret = PKI_OPENSSL_ERR;
goto cleanup;
} else
ret = PKI_OK;
pki_msg(2, "EXT",
"Successfully Added extensions to CSR\n");
cleanup:
if(exts)
sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
return ret;
}
/**
* pki_ossl_add_cert_extensions - Adds extensions to the given certificate
*
* Tries to add CA and normal extensions provided from above to the given
* Certificate before signing it and also tries to copy extensions
* from the CSR if present.
*
* @struct pki_cmd *cmd - The command structure from above
* @X509 *cacert - CA's root certificate
* @X509 *cert - The Certificate
* @X509_REQ *csr - The CSR
*
* returns: One of pki_error_codes
*/
int
pki_ossl_add_cert_extensions(struct pki_cmd *cmd, X509 *cacert, X509* cert,
X509_REQ *csr)
{
int ret = PKI_OK;
int i = 0;
X509V3_CTX ctx;
X509_CINF *ci = cert->cert_info;
struct pki_config *conf = cmd->conf;
if(conf->exts) {
if (ci->extensions != NULL)
sk_X509_EXTENSION_pop_free(ci->extensions,
X509_EXTENSION_free);
ci->extensions = NULL;
X509V3_set_ctx(&ctx, cacert, cert, csr, NULL, 0);
/* Add CRL distribution points */
if(conf->exts->num_dps) {
/* Add them to the internal stack */
for(i = 0; i < conf->exts->num_dps; i++) {
pki_ossl_add_dp(conf->exts->dps[i], cert);
}
/* DER encode them */
ret = X509_add1_ext_i2d(cert,
NID_crl_distribution_points,
(void *)cert->crldp, 0,
X509V3_ADD_APPEND);
}
/* Add AIA points */
if(conf->exts->num_aiaps)
pki_ossl_add_aiaps(conf, cert);
/* Add standard extensions */
if(conf->exts->sans) {
ret = pki_ossl_add_sans(conf, ci->extensions);
if(ret)
goto cleanup;
}
if(conf->exts->bc) {
ret = pki_ossl_add_bc(conf, ci->extensions);
if(ret)
goto cleanup;
}
if(conf->exts->key_usage) {
ret = pki_ossl_add_key_usage(conf, ci->extensions);
if(ret)
goto cleanup;