1 /* $OpenBSD: ca.c,v 1.60 2024/07/08 05:56:17 tb Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59 /* The PPKI stuff has been donated by Jeff Barber <jeffb@issl.atl.hp.com> */
60
61 #include <sys/types.h>
62
63 #include <ctype.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <limits.h>
67 #include <string.h>
68 #include <unistd.h>
69
70 #include "apps.h"
71
72 #include <openssl/bio.h>
73 #include <openssl/bn.h>
74 #include <openssl/conf.h>
75 #include <openssl/err.h>
76 #include <openssl/evp.h>
77 #include <openssl/objects.h>
78 #include <openssl/ocsp.h>
79 #include <openssl/pem.h>
80 #include <openssl/txt_db.h>
81 #include <openssl/x509.h>
82 #include <openssl/x509v3.h>
83
84 #define BASE_SECTION "ca"
85
86 #define ENV_DEFAULT_CA "default_ca"
87
88 #define STRING_MASK "string_mask"
89 #define UTF8_IN "utf8"
90
91 #define ENV_NEW_CERTS_DIR "new_certs_dir"
92 #define ENV_CERTIFICATE "certificate"
93 #define ENV_SERIAL "serial"
94 #define ENV_CRLNUMBER "crlnumber"
95 #define ENV_PRIVATE_KEY "private_key"
96 #define ENV_DEFAULT_DAYS "default_days"
97 #define ENV_DEFAULT_STARTDATE "default_startdate"
98 #define ENV_DEFAULT_ENDDATE "default_enddate"
99 #define ENV_DEFAULT_CRL_DAYS "default_crl_days"
100 #define ENV_DEFAULT_CRL_HOURS "default_crl_hours"
101 #define ENV_DEFAULT_MD "default_md"
102 #define ENV_DEFAULT_EMAIL_DN "email_in_dn"
103 #define ENV_PRESERVE "preserve"
104 #define ENV_POLICY "policy"
105 #define ENV_EXTENSIONS "x509_extensions"
106 #define ENV_CRLEXT "crl_extensions"
107 #define ENV_MSIE_HACK "msie_hack"
108 #define ENV_NAMEOPT "name_opt"
109 #define ENV_CERTOPT "cert_opt"
110 #define ENV_EXTCOPY "copy_extensions"
111 #define ENV_UNIQUE_SUBJECT "unique_subject"
112
113 #define ENV_DATABASE "database"
114
115 /* Additional revocation information types */
116
117 #define REV_NONE 0 /* No addditional information */
118 #define REV_CRL_REASON 1 /* Value is CRL reason code */
119 #define REV_HOLD 2 /* Value is hold instruction */
120 #define REV_KEY_COMPROMISE 3 /* Value is cert key compromise time */
121 #define REV_CA_COMPROMISE 4 /* Value is CA key compromise time */
122
123 static void lookup_fail(const char *name, const char *tag);
124 static int certify(X509 **xret, char *infile, EVP_PKEY *pkey, X509 *x509,
125 const EVP_MD *dgst, STACK_OF(OPENSSL_STRING) *sigopts,
126 STACK_OF(CONF_VALUE) *policy, CA_DB *db, BIGNUM *serial, char *subj,
127 unsigned long chtype, int multirdn, int email_dn, char *startdate,
128 char *enddate, long days, int batch, char *ext_sect, CONF *conf,
129 int verbose, unsigned long certopt, unsigned long nameopt,
130 int default_op, int ext_copy, int selfsign);
131 static int certify_cert(X509 **xret, char *infile, EVP_PKEY *pkey,
132 X509 *x509, const EVP_MD *dgst, STACK_OF(OPENSSL_STRING) *sigopts,
133 STACK_OF(CONF_VALUE) *policy, CA_DB *db, BIGNUM *serial, char *subj,
134 unsigned long chtype, int multirdn, int email_dn, char *startdate,
135 char *enddate, long days, int batch, char *ext_sect, CONF *conf,
136 int verbose, unsigned long certopt, unsigned long nameopt, int default_op,
137 int ext_copy);
138 static int write_new_certificate(BIO *bp, X509 *x, int output_der,
139 int notext);
140 static int do_body(X509 **xret, EVP_PKEY *pkey, X509 *x509,
141 const EVP_MD *dgst, STACK_OF(OPENSSL_STRING) *sigopts,
142 STACK_OF(CONF_VALUE) *policy, CA_DB *db, BIGNUM *serial, char *subj,
143 unsigned long chtype, int multirdn, int email_dn, char *startdate,
144 char *enddate, long days, int batch, int verbose, X509_REQ *req,
145 char *ext_sect, CONF *conf, unsigned long certopt, unsigned long nameopt,
146 int default_op, int ext_copy, int selfsign);
147 static int do_revoke(X509 *x509, CA_DB *db, int ext, char *extval);
148 static int get_certificate_status(const char *serial, CA_DB *db);
149 static int do_updatedb(CA_DB *db);
150 static int check_time_format(const char *str);
151 static char *bin2hex(unsigned char *, size_t);
152 char *make_revocation_str(int rev_type, char *rev_arg);
153 int make_revoked(X509_REVOKED *rev, const char *str);
154 int old_entry_print(BIO *bp, ASN1_OBJECT *obj, ASN1_STRING *str);
155
156 static CONF *conf = NULL;
157 static CONF *extconf = NULL;
158
159 static struct {
160 int batch;
161 char *certfile;
162 unsigned long chtype;
163 char *configfile;
164 int create_serial;
165 char *crl_ext;
166 long crldays;
167 long crlhours;
168 long crlsec;
169 long days;
170 int dorevoke;
171 int doupdatedb;
172 int email_dn;
173 char *enddate;
174 char *extensions;
175 char *extfile;
176 int gencrl;
177 char *infile;
178 char **infiles;
179 int infiles_num;
180 char *key;
181 char *keyfile;
182 int keyform;
183 char *md;
184 int multirdn;
185 int msie_hack;
186 int notext;
187 char *outdir;
188 char *outfile;
189 char *passargin;
190 char *policy;
191 int preserve;
192 int req;
193 char *rev_arg;
194 int rev_type;
195 char *serial_status;
196 char *section;
197 int selfsign;
198 STACK_OF(OPENSSL_STRING) *sigopts;
199 char *ss_cert_file;
200 char *startdate;
201 char *subj;
202 int verbose;
203 } cfg;
204
205 static int
ca_opt_chtype_utf8(void)206 ca_opt_chtype_utf8(void)
207 {
208 cfg.chtype = MBSTRING_UTF8;
209 return (0);
210 }
211
212 static int
ca_opt_crl_ca_compromise(char * arg)213 ca_opt_crl_ca_compromise(char *arg)
214 {
215 cfg.rev_arg = arg;
216 cfg.rev_type = REV_CA_COMPROMISE;
217 return (0);
218 }
219
220 static int
ca_opt_crl_compromise(char * arg)221 ca_opt_crl_compromise(char *arg)
222 {
223 cfg.rev_arg = arg;
224 cfg.rev_type = REV_KEY_COMPROMISE;
225 return (0);
226 }
227
228 static int
ca_opt_crl_hold(char * arg)229 ca_opt_crl_hold(char *arg)
230 {
231 cfg.rev_arg = arg;
232 cfg.rev_type = REV_HOLD;
233 return (0);
234 }
235
236 static int
ca_opt_crl_reason(char * arg)237 ca_opt_crl_reason(char *arg)
238 {
239 cfg.rev_arg = arg;
240 cfg.rev_type = REV_CRL_REASON;
241 return (0);
242 }
243
244 static int
ca_opt_in(char * arg)245 ca_opt_in(char *arg)
246 {
247 cfg.infile = arg;
248 cfg.req = 1;
249 return (0);
250 }
251
252 static int
ca_opt_infiles(int argc,char ** argv,int * argsused)253 ca_opt_infiles(int argc, char **argv, int *argsused)
254 {
255 cfg.infiles_num = argc - 1;
256 if (cfg.infiles_num < 1)
257 return (1);
258 cfg.infiles = argv + 1;
259 cfg.req = 1;
260 *argsused = argc;
261 return (0);
262 }
263
264 static int
ca_opt_revoke(char * arg)265 ca_opt_revoke(char *arg)
266 {
267 cfg.infile = arg;
268 cfg.dorevoke = 1;
269 return (0);
270 }
271
272 static int
ca_opt_sigopt(char * arg)273 ca_opt_sigopt(char *arg)
274 {
275 if (cfg.sigopts == NULL)
276 cfg.sigopts = sk_OPENSSL_STRING_new_null();
277 if (cfg.sigopts == NULL)
278 return (1);
279 if (!sk_OPENSSL_STRING_push(cfg.sigopts, arg))
280 return (1);
281 return (0);
282 }
283
284 static int
ca_opt_ss_cert(char * arg)285 ca_opt_ss_cert(char *arg)
286 {
287 cfg.ss_cert_file = arg;
288 cfg.req = 1;
289 return (0);
290 }
291
292 static const struct option ca_options[] = {
293 {
294 .name = "batch",
295 .desc = "Operate in batch mode",
296 .type = OPTION_FLAG,
297 .opt.flag = &cfg.batch,
298 },
299 {
300 .name = "cert",
301 .argname = "file",
302 .desc = "File containing the CA certificate",
303 .type = OPTION_ARG,
304 .opt.arg = &cfg.certfile,
305 },
306 {
307 .name = "config",
308 .argname = "file",
309 .desc = "Specify an alternative configuration file",
310 .type = OPTION_ARG,
311 .opt.arg = &cfg.configfile,
312 },
313 {
314 .name = "create_serial",
315 .desc = "If reading serial fails, create a new random serial",
316 .type = OPTION_FLAG,
317 .opt.flag = &cfg.create_serial,
318 },
319 {
320 .name = "crl_CA_compromise",
321 .argname = "time",
322 .desc = "Set the compromise time and the revocation reason to\n"
323 "CACompromise",
324 .type = OPTION_ARG_FUNC,
325 .opt.argfunc = ca_opt_crl_ca_compromise,
326 },
327 {
328 .name = "crl_compromise",
329 .argname = "time",
330 .desc = "Set the compromise time and the revocation reason to\n"
331 "keyCompromise",
332 .type = OPTION_ARG_FUNC,
333 .opt.argfunc = ca_opt_crl_compromise,
334 },
335 {
336 .name = "crl_hold",
337 .argname = "instruction",
338 .desc = "Set the hold instruction and the revocation reason to\n"
339 "certificateHold",
340 .type = OPTION_ARG_FUNC,
341 .opt.argfunc = ca_opt_crl_hold,
342 },
343 {
344 .name = "crl_reason",
345 .argname = "reason",
346 .desc = "Revocation reason",
347 .type = OPTION_ARG_FUNC,
348 .opt.argfunc = ca_opt_crl_reason,
349 },
350 {
351 .name = "crldays",
352 .argname = "days",
353 .desc = "Number of days before the next CRL is due",
354 .type = OPTION_ARG_LONG,
355 .opt.lvalue = &cfg.crldays,
356 },
357 {
358 .name = "crlexts",
359 .argname = "section",
360 .desc = "CRL extension section (override value in config file)",
361 .type = OPTION_ARG,
362 .opt.arg = &cfg.crl_ext,
363 },
364 {
365 .name = "crlhours",
366 .argname = "hours",
367 .desc = "Number of hours before the next CRL is due",
368 .type = OPTION_ARG_LONG,
369 .opt.lvalue = &cfg.crlhours,
370 },
371 {
372 .name = "crlsec",
373 .argname = "seconds",
374 .desc = "Number of seconds before the next CRL is due",
375 .type = OPTION_ARG_LONG,
376 .opt.lvalue = &cfg.crlsec,
377 },
378 {
379 .name = "days",
380 .argname = "arg",
381 .desc = "Number of days to certify the certificate for",
382 .type = OPTION_ARG_LONG,
383 .opt.lvalue = &cfg.days,
384 },
385 {
386 .name = "enddate",
387 .argname = "YYMMDDHHMMSSZ",
388 .desc = "Certificate validity notAfter (overrides -days)",
389 .type = OPTION_ARG,
390 .opt.arg = &cfg.enddate,
391 },
392 {
393 .name = "extensions",
394 .argname = "section",
395 .desc = "Extension section (override value in config file)",
396 .type = OPTION_ARG,
397 .opt.arg = &cfg.extensions,
398 },
399 {
400 .name = "extfile",
401 .argname = "file",
402 .desc = "Configuration file with X509v3 extentions to add",
403 .type = OPTION_ARG,
404 .opt.arg = &cfg.extfile,
405 },
406 {
407 .name = "gencrl",
408 .desc = "Generate a new CRL",
409 .type = OPTION_FLAG,
410 .opt.flag = &cfg.gencrl,
411 },
412 {
413 .name = "in",
414 .argname = "file",
415 .desc = "Input file containing a single certificate request",
416 .type = OPTION_ARG_FUNC,
417 .opt.argfunc = ca_opt_in,
418 },
419 {
420 .name = "infiles",
421 .argname = "...",
422 .desc = "The last argument, certificate requests to process",
423 .type = OPTION_ARGV_FUNC,
424 .opt.argvfunc = ca_opt_infiles,
425 },
426 {
427 .name = "key",
428 .argname = "password",
429 .desc = "Key to decode the private key if it is encrypted",
430 .type = OPTION_ARG,
431 .opt.arg = &cfg.key,
432 },
433 {
434 .name = "keyfile",
435 .argname = "file",
436 .desc = "Private key file",
437 .type = OPTION_ARG,
438 .opt.arg = &cfg.keyfile,
439 },
440 {
441 .name = "keyform",
442 .argname = "fmt",
443 .desc = "Private key file format (DER or PEM (default))",
444 .type = OPTION_ARG_FORMAT,
445 .opt.value = &cfg.keyform,
446 },
447 {
448 .name = "md",
449 .argname = "alg",
450 .desc = "Message digest to use",
451 .type = OPTION_ARG,
452 .opt.arg = &cfg.md,
453 },
454 {
455 .name = "msie_hack",
456 .type = OPTION_FLAG,
457 .opt.flag = &cfg.msie_hack,
458 },
459 {
460 .name = "multivalue-rdn",
461 .desc = "Enable support for multivalued RDNs",
462 .type = OPTION_FLAG,
463 .opt.flag = &cfg.multirdn,
464 },
465 {
466 .name = "name",
467 .argname = "section",
468 .desc = "Specifies the configuration file section to use",
469 .type = OPTION_ARG,
470 .opt.arg = &cfg.section,
471 },
472 {
473 .name = "noemailDN",
474 .desc = "Do not add the EMAIL field to the DN",
475 .type = OPTION_VALUE,
476 .opt.value = &cfg.email_dn,
477 .value = 0,
478 },
479 {
480 .name = "notext",
481 .desc = "Do not print the generated certificate",
482 .type = OPTION_FLAG,
483 .opt.flag = &cfg.notext,
484 },
485 {
486 .name = "out",
487 .argname = "file",
488 .desc = "Output file (default stdout)",
489 .type = OPTION_ARG,
490 .opt.arg = &cfg.outfile,
491 },
492 {
493 .name = "outdir",
494 .argname = "directory",
495 .desc = " Directory to output certificates to",
496 .type = OPTION_ARG,
497 .opt.arg = &cfg.outdir,
498 },
499 {
500 .name = "passin",
501 .argname = "src",
502 .desc = "Private key input password source",
503 .type = OPTION_ARG,
504 .opt.arg = &cfg.passargin,
505 },
506 {
507 .name = "policy",
508 .argname = "name",
509 .desc = "The CA 'policy' to support",
510 .type = OPTION_ARG,
511 .opt.arg = &cfg.policy,
512 },
513 {
514 .name = "preserveDN",
515 .desc = "Do not re-order the DN",
516 .type = OPTION_FLAG,
517 .opt.flag = &cfg.preserve,
518 },
519 {
520 .name = "revoke",
521 .argname = "file",
522 .desc = "Revoke a certificate (given in file)",
523 .type = OPTION_ARG_FUNC,
524 .opt.argfunc = ca_opt_revoke,
525 },
526 {
527 .name = "selfsign",
528 .desc = "Sign a certificate using the key associated with it",
529 .type = OPTION_FLAG,
530 .opt.flag = &cfg.selfsign,
531 },
532 {
533 .name = "sigopt",
534 .argname = "nm:v",
535 .desc = "Signature parameter in nm:v form",
536 .type = OPTION_ARG_FUNC,
537 .opt.argfunc = ca_opt_sigopt,
538 },
539 {
540 .name = "ss_cert",
541 .argname = "file",
542 .desc = "File contains a self signed certificate to sign",
543 .type = OPTION_ARG_FUNC,
544 .opt.argfunc = ca_opt_ss_cert,
545 },
546 {
547 .name = "startdate",
548 .argname = "YYMMDDHHMMSSZ",
549 .desc = "Certificate validity notBefore",
550 .type = OPTION_ARG,
551 .opt.arg = &cfg.startdate,
552 },
553 {
554 .name = "status",
555 .argname = "serial",
556 .desc = "Shows certificate status given the serial number",
557 .type = OPTION_ARG,
558 .opt.arg = &cfg.serial_status,
559 },
560 {
561 .name = "subj",
562 .argname = "arg",
563 .desc = "Use arg instead of request's subject",
564 .type = OPTION_ARG,
565 .opt.arg = &cfg.subj,
566 },
567 {
568 .name = "updatedb",
569 .desc = "Updates db for expired certificates",
570 .type = OPTION_FLAG,
571 .opt.flag = &cfg.doupdatedb,
572 },
573 {
574 .name = "utf8",
575 .desc = "Input characters are in UTF-8 (default ASCII)",
576 .type = OPTION_FUNC,
577 .opt.func = ca_opt_chtype_utf8,
578 },
579 {
580 .name = "verbose",
581 .desc = "Verbose output during processing",
582 .type = OPTION_FLAG,
583 .opt.flag = &cfg.verbose,
584 },
585 { NULL },
586 };
587
588 static void
ca_usage(void)589 ca_usage(void)
590 {
591 fprintf(stderr,
592 "usage: ca [-batch] [-cert file] [-config file] [-create_serial]\n"
593 " [-crl_CA_compromise time] [-crl_compromise time]\n"
594 " [-crl_hold instruction] [-crl_reason reason] [-crldays days]\n"
595 " [-crlexts section] [-crlhours hours] [-crlsec seconds]\n"
596 " [-days arg] [-enddate date] [-extensions section]\n"
597 " [-extfile file] [-gencrl] [-in file] [-infiles]\n"
598 " [-key password] [-keyfile file] [-keyform pem | der]\n"
599 " [-md alg] [-multivalue-rdn] [-name section]\n"
600 " [-noemailDN] [-notext] [-out file] [-outdir directory]\n"
601 " [-passin arg] [-policy name] [-preserveDN] [-revoke file]\n"
602 " [-selfsign] [-sigopt nm:v] [-ss_cert file]\n"
603 " [-startdate date] [-status serial] [-subj arg] [-updatedb]\n"
604 " [-utf8] [-verbose]\n\n");
605 options_usage(ca_options);
606 fprintf(stderr, "\n");
607 }
608
609 int
ca_main(int argc,char ** argv)610 ca_main(int argc, char **argv)
611 {
612 int free_key = 0;
613 int total = 0;
614 int total_done = 0;
615 long errorline = -1;
616 EVP_PKEY *pkey = NULL;
617 int output_der = 0;
618 char *serialfile = NULL;
619 char *crlnumberfile = NULL;
620 char *tmp_email_dn = NULL;
621 BIGNUM *serial = NULL;
622 BIGNUM *crlnumber = NULL;
623 unsigned long nameopt = 0, certopt = 0;
624 int default_op = 1;
625 int ext_copy = EXT_COPY_NONE;
626 X509 *x509 = NULL, *x509p = NULL;
627 X509 *x = NULL;
628 BIO *in = NULL, *out = NULL, *Sout = NULL, *Cout = NULL;
629 char *dbfile = NULL;
630 CA_DB *db = NULL;
631 X509_CRL *crl = NULL;
632 X509_REVOKED *r = NULL;
633 ASN1_TIME *tmptm = NULL;
634 ASN1_INTEGER *tmpserial;
635 char *f;
636 const char *p;
637 char *const *pp;
638 int i, j;
639 const EVP_MD *dgst = NULL;
640 STACK_OF(CONF_VALUE) *attribs = NULL;
641 STACK_OF(X509) *cert_sk = NULL;
642 char *tofree = NULL;
643 DB_ATTR db_attr;
644 int default_nid, rv;
645 int ret = 1;
646
647 if (pledge("stdio cpath wpath rpath tty", NULL) == -1) {
648 perror("pledge");
649 exit(1);
650 }
651
652 memset(&cfg, 0, sizeof(cfg));
653 cfg.email_dn = 1;
654 cfg.keyform = FORMAT_PEM;
655 cfg.chtype = MBSTRING_ASC;
656 cfg.rev_type = REV_NONE;
657
658 conf = NULL;
659
660 if (options_parse(argc, argv, ca_options, NULL, NULL) != 0) {
661 ca_usage();
662 goto err;
663 }
664
665 /*****************************************************************/
666 tofree = NULL;
667 if (cfg.configfile == NULL)
668 cfg.configfile = getenv("OPENSSL_CONF");
669 if (cfg.configfile == NULL) {
670 if ((tofree = make_config_name()) == NULL) {
671 BIO_printf(bio_err, "error making config file name\n");
672 goto err;
673 }
674 cfg.configfile = tofree;
675 }
676 BIO_printf(bio_err, "Using configuration from %s\n",
677 cfg.configfile);
678 conf = NCONF_new(NULL);
679 if (NCONF_load(conf, cfg.configfile, &errorline) <= 0) {
680 if (errorline <= 0)
681 BIO_printf(bio_err,
682 "error loading the config file '%s'\n",
683 cfg.configfile);
684 else
685 BIO_printf(bio_err,
686 "error on line %ld of config file '%s'\n",
687 errorline, cfg.configfile);
688 goto err;
689 }
690 free(tofree);
691 tofree = NULL;
692
693 /* Lets get the config section we are using */
694 if (cfg.section == NULL) {
695 cfg.section = NCONF_get_string(conf, BASE_SECTION,
696 ENV_DEFAULT_CA);
697 if (cfg.section == NULL) {
698 lookup_fail(BASE_SECTION, ENV_DEFAULT_CA);
699 goto err;
700 }
701 }
702 if (conf != NULL) {
703 p = NCONF_get_string(conf, NULL, "oid_file");
704 if (p == NULL)
705 ERR_clear_error();
706 if (p != NULL) {
707 BIO *oid_bio;
708
709 oid_bio = BIO_new_file(p, "r");
710 if (oid_bio == NULL) {
711 /*
712 BIO_printf(bio_err,
713 "problems opening %s for extra oid's\n", p);
714 ERR_print_errors(bio_err);
715 */
716 ERR_clear_error();
717 } else {
718 OBJ_create_objects(oid_bio);
719 BIO_free(oid_bio);
720 }
721 }
722 if (!add_oid_section(bio_err, conf)) {
723 ERR_print_errors(bio_err);
724 goto err;
725 }
726 }
727 f = NCONF_get_string(conf, cfg.section, STRING_MASK);
728 if (f == NULL)
729 ERR_clear_error();
730
731 if (f != NULL && !ASN1_STRING_set_default_mask_asc(f)) {
732 BIO_printf(bio_err,
733 "Invalid global string mask setting %s\n", f);
734 goto err;
735 }
736 if (cfg.chtype != MBSTRING_UTF8) {
737 f = NCONF_get_string(conf, cfg.section, UTF8_IN);
738 if (f == NULL)
739 ERR_clear_error();
740 else if (strcmp(f, "yes") == 0)
741 cfg.chtype = MBSTRING_UTF8;
742 }
743 db_attr.unique_subject = 1;
744 p = NCONF_get_string(conf, cfg.section, ENV_UNIQUE_SUBJECT);
745 if (p != NULL) {
746 db_attr.unique_subject = parse_yesno(p, 1);
747 } else
748 ERR_clear_error();
749
750 in = BIO_new(BIO_s_file());
751 out = BIO_new(BIO_s_file());
752 Sout = BIO_new(BIO_s_file());
753 Cout = BIO_new(BIO_s_file());
754 if ((in == NULL) || (out == NULL) || (Sout == NULL) || (Cout == NULL)) {
755 ERR_print_errors(bio_err);
756 goto err;
757 }
758 /*****************************************************************/
759 /* report status of cert with serial number given on command line */
760 if (cfg.serial_status) {
761 if ((dbfile = NCONF_get_string(conf, cfg.section,
762 ENV_DATABASE)) == NULL) {
763 lookup_fail(cfg.section, ENV_DATABASE);
764 goto err;
765 }
766 db = load_index(dbfile, &db_attr);
767 if (db == NULL)
768 goto err;
769
770 if (!index_index(db))
771 goto err;
772
773 if (get_certificate_status(cfg.serial_status, db) != 1)
774 BIO_printf(bio_err, "Error verifying serial %s!\n",
775 cfg.serial_status);
776 goto err;
777 }
778 /*****************************************************************/
779 /* we definitely need a private key, so let's get it */
780
781 if ((cfg.keyfile == NULL) &&
782 ((cfg.keyfile = NCONF_get_string(conf, cfg.section,
783 ENV_PRIVATE_KEY)) == NULL)) {
784 lookup_fail(cfg.section, ENV_PRIVATE_KEY);
785 goto err;
786 }
787 if (cfg.key == NULL) {
788 free_key = 1;
789 if (!app_passwd(bio_err, cfg.passargin, NULL,
790 &cfg.key, NULL)) {
791 BIO_printf(bio_err, "Error getting password\n");
792 goto err;
793 }
794 }
795 pkey = load_key(bio_err, cfg.keyfile, cfg.keyform, 0,
796 cfg.key, "CA private key");
797 if (cfg.key != NULL)
798 explicit_bzero(cfg.key, strlen(cfg.key));
799 if (pkey == NULL) {
800 /* load_key() has already printed an appropriate message */
801 goto err;
802 }
803 /*****************************************************************/
804 /* we need a certificate */
805 if (!cfg.selfsign || cfg.ss_cert_file != NULL || cfg.gencrl) {
806 if ((cfg.certfile == NULL) &&
807 ((cfg.certfile = NCONF_get_string(conf,
808 cfg.section, ENV_CERTIFICATE)) == NULL)) {
809 lookup_fail(cfg.section, ENV_CERTIFICATE);
810 goto err;
811 }
812 x509 = load_cert(bio_err, cfg.certfile, FORMAT_PEM, NULL,
813 "CA certificate");
814 if (x509 == NULL)
815 goto err;
816
817 if (!X509_check_private_key(x509, pkey)) {
818 BIO_printf(bio_err,
819 "CA certificate and CA private key do not match\n");
820 goto err;
821 }
822 }
823 if (!cfg.selfsign)
824 x509p = x509;
825
826 f = NCONF_get_string(conf, BASE_SECTION, ENV_PRESERVE);
827 if (f == NULL)
828 ERR_clear_error();
829 if ((f != NULL) && ((*f == 'y') || (*f == 'Y')))
830 cfg.preserve = 1;
831 f = NCONF_get_string(conf, BASE_SECTION, ENV_MSIE_HACK);
832 if (f == NULL)
833 ERR_clear_error();
834 if ((f != NULL) && ((*f == 'y') || (*f == 'Y')))
835 cfg.msie_hack = 1;
836
837 f = NCONF_get_string(conf, cfg.section, ENV_NAMEOPT);
838
839 if (f != NULL) {
840 if (!set_name_ex(&nameopt, f)) {
841 BIO_printf(bio_err,
842 "Invalid name options: \"%s\"\n", f);
843 goto err;
844 }
845 default_op = 0;
846 } else
847 ERR_clear_error();
848
849 f = NCONF_get_string(conf, cfg.section, ENV_CERTOPT);
850
851 if (f != NULL) {
852 if (!set_cert_ex(&certopt, f)) {
853 BIO_printf(bio_err,
854 "Invalid certificate options: \"%s\"\n", f);
855 goto err;
856 }
857 default_op = 0;
858 } else
859 ERR_clear_error();
860
861 f = NCONF_get_string(conf, cfg.section, ENV_EXTCOPY);
862
863 if (f != NULL) {
864 if (!set_ext_copy(&ext_copy, f)) {
865 BIO_printf(bio_err,
866 "Invalid extension copy option: \"%s\"\n", f);
867 goto err;
868 }
869 } else
870 ERR_clear_error();
871
872 /*****************************************************************/
873 /* lookup where to write new certificates */
874 if (cfg.outdir == NULL && cfg.req) {
875 if ((cfg.outdir = NCONF_get_string(conf,
876 cfg.section, ENV_NEW_CERTS_DIR)) == NULL) {
877 BIO_printf(bio_err, "output directory %s not defined\n",
878 ENV_NEW_CERTS_DIR);
879 goto err;
880 }
881 }
882 /*****************************************************************/
883 /* we need to load the database file */
884 if ((dbfile = NCONF_get_string(conf, cfg.section,
885 ENV_DATABASE)) == NULL) {
886 lookup_fail(cfg.section, ENV_DATABASE);
887 goto err;
888 }
889 db = load_index(dbfile, &db_attr);
890 if (db == NULL)
891 goto err;
892
893 /* Lets check some fields */
894 for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
895 pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
896 if ((pp[DB_type][0] != DB_TYPE_REV) &&
897 (pp[DB_rev_date][0] != '\0')) {
898 BIO_printf(bio_err,
899 "entry %d: not revoked yet, but has a revocation date\n",
900 i + 1);
901 goto err;
902 }
903 if ((pp[DB_type][0] == DB_TYPE_REV) &&
904 !make_revoked(NULL, pp[DB_rev_date])) {
905 BIO_printf(bio_err, " in entry %d\n", i + 1);
906 goto err;
907 }
908 if (!check_time_format((char *) pp[DB_exp_date])) {
909 BIO_printf(bio_err, "entry %d: invalid expiry date\n",
910 i + 1);
911 goto err;
912 }
913 p = pp[DB_serial];
914 j = strlen(p);
915 if (*p == '-') {
916 p++;
917 j--;
918 }
919 if ((j & 1) || (j < 2)) {
920 BIO_printf(bio_err,
921 "entry %d: bad serial number length (%d)\n",
922 i + 1, j);
923 goto err;
924 }
925 while (*p) {
926 if (!(((*p >= '0') && (*p <= '9')) ||
927 ((*p >= 'A') && (*p <= 'F')) ||
928 ((*p >= 'a') && (*p <= 'f')))) {
929 BIO_printf(bio_err,
930 "entry %d: bad serial number characters, char pos %ld, char is '%c'\n",
931 i + 1, (long) (p - pp[DB_serial]), *p);
932 goto err;
933 }
934 p++;
935 }
936 }
937 if (cfg.verbose) {
938 BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
939 TXT_DB_write(out, db->db);
940 BIO_printf(bio_err, "%d entries loaded from the database\n",
941 sk_OPENSSL_PSTRING_num(db->db->data));
942 BIO_printf(bio_err, "generating index\n");
943 }
944 if (!index_index(db))
945 goto err;
946
947 /*****************************************************************/
948 /* Update the db file for expired certificates */
949 if (cfg.doupdatedb) {
950 if (cfg.verbose)
951 BIO_printf(bio_err, "Updating %s ...\n", dbfile);
952
953 i = do_updatedb(db);
954 if (i == -1) {
955 BIO_printf(bio_err, "Malloc failure\n");
956 goto err;
957 } else if (i == 0) {
958 if (cfg.verbose)
959 BIO_printf(bio_err,
960 "No entries found to mark expired\n");
961 } else {
962 if (!save_index(dbfile, "new", db))
963 goto err;
964
965 if (!rotate_index(dbfile, "new", "old"))
966 goto err;
967
968 if (cfg.verbose)
969 BIO_printf(bio_err,
970 "Done. %d entries marked as expired\n", i);
971 }
972 }
973 /*****************************************************************/
974 /* Read extentions config file */
975 if (cfg.extfile != NULL) {
976 extconf = NCONF_new(NULL);
977 if (NCONF_load(extconf, cfg.extfile, &errorline) <= 0) {
978 if (errorline <= 0)
979 BIO_printf(bio_err,
980 "ERROR: loading the config file '%s'\n",
981 cfg.extfile);
982 else
983 BIO_printf(bio_err,
984 "ERROR: on line %ld of config file '%s'\n",
985 errorline, cfg.extfile);
986 ret = 1;
987 goto err;
988 }
989 if (cfg.verbose)
990 BIO_printf(bio_err,
991 "Successfully loaded extensions file %s\n",
992 cfg.extfile);
993
994 /* We can have sections in the ext file */
995 if (cfg.extensions == NULL &&
996 (cfg.extensions = NCONF_get_string(extconf, "default",
997 "extensions")) == NULL)
998 cfg.extensions = "default";
999 }
1000 /*****************************************************************/
1001 if (cfg.req || cfg.gencrl) {
1002 if (cfg.outfile != NULL) {
1003 if (BIO_write_filename(Sout, cfg.outfile) <= 0) {
1004 perror(cfg.outfile);
1005 goto err;
1006 }
1007 } else {
1008 BIO_set_fp(Sout, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
1009 }
1010 }
1011
1012 rv = EVP_PKEY_get_default_digest_nid(pkey, &default_nid);
1013 if (rv == 2 && default_nid == NID_undef) {
1014 /* The digest is required to be EVP_md_null() (EdDSA). */
1015 dgst = EVP_md_null();
1016 } else {
1017 /* Ignore rv unless we need a valid default_nid. */
1018 if (cfg.md == NULL)
1019 cfg.md = NCONF_get_string(conf, cfg.section,
1020 ENV_DEFAULT_MD);
1021 if (cfg.md == NULL) {
1022 lookup_fail(cfg.section, ENV_DEFAULT_MD);
1023 goto err;
1024 }
1025 if (strcmp(cfg.md, "default") == 0) {
1026 if (rv <= 0) {
1027 BIO_puts(bio_err, "no default digest\n");
1028 goto err;
1029 }
1030 cfg.md = (char *)OBJ_nid2sn(default_nid);
1031 }
1032 if (cfg.md == NULL)
1033 goto err;
1034 if ((dgst = EVP_get_digestbyname(cfg.md)) == NULL) {
1035 BIO_printf(bio_err, "%s is an unsupported "
1036 "message digest type\n", cfg.md);
1037 goto err;
1038 }
1039 }
1040 if (cfg.req) {
1041 if ((cfg.email_dn == 1) &&
1042 ((tmp_email_dn = NCONF_get_string(conf, cfg.section,
1043 ENV_DEFAULT_EMAIL_DN)) != NULL)) {
1044 if (strcmp(tmp_email_dn, "no") == 0)
1045 cfg.email_dn = 0;
1046 }
1047 if (cfg.verbose)
1048 BIO_printf(bio_err, "message digest is %s\n",
1049 OBJ_nid2ln(EVP_MD_type(dgst)));
1050 if ((cfg.policy == NULL) &&
1051 ((cfg.policy = NCONF_get_string(conf,
1052 cfg.section, ENV_POLICY)) == NULL)) {
1053 lookup_fail(cfg.section, ENV_POLICY);
1054 goto err;
1055 }
1056 if (cfg.verbose)
1057 BIO_printf(bio_err, "policy is %s\n", cfg.policy);
1058
1059 if ((serialfile = NCONF_get_string(conf, cfg.section,
1060 ENV_SERIAL)) == NULL) {
1061 lookup_fail(cfg.section, ENV_SERIAL);
1062 goto err;
1063 }
1064 if (extconf == NULL) {
1065 /*
1066 * no '-extfile' option, so we look for extensions in
1067 * the main configuration file
1068 */
1069 if (cfg.extensions == NULL) {
1070 cfg.extensions = NCONF_get_string(conf,
1071 cfg.section, ENV_EXTENSIONS);
1072 if (cfg.extensions == NULL)
1073 ERR_clear_error();
1074 }
1075 if (cfg.extensions != NULL) {
1076 /* Check syntax of file */
1077 X509V3_CTX ctx;
1078 X509V3_set_ctx_test(&ctx);
1079 X509V3_set_nconf(&ctx, conf);
1080 if (!X509V3_EXT_add_nconf(conf, &ctx,
1081 cfg.extensions, NULL)) {
1082 BIO_printf(bio_err,
1083 "Error Loading extension section %s\n",
1084 cfg.extensions);
1085 ret = 1;
1086 goto err;
1087 }
1088 }
1089 }
1090 if (cfg.startdate == NULL) {
1091 cfg.startdate = NCONF_get_string(conf,
1092 cfg.section, ENV_DEFAULT_STARTDATE);
1093 if (cfg.startdate == NULL)
1094 ERR_clear_error();
1095 }
1096 if (cfg.startdate == NULL)
1097 cfg.startdate = "today";
1098
1099 if (cfg.enddate == NULL) {
1100 cfg.enddate = NCONF_get_string(conf,
1101 cfg.section, ENV_DEFAULT_ENDDATE);
1102 if (cfg.enddate == NULL)
1103 ERR_clear_error();
1104 }
1105 if (cfg.days == 0 && cfg.enddate == NULL) {
1106 if (!NCONF_get_number(conf, cfg.section,
1107 ENV_DEFAULT_DAYS, &cfg.days))
1108 cfg.days = 0;
1109 }
1110 if (cfg.enddate == NULL && cfg.days == 0) {
1111 BIO_printf(bio_err,
1112 "cannot lookup how many days to certify for\n");
1113 goto err;
1114 }
1115 if ((serial = load_serial(serialfile, cfg.create_serial,
1116 NULL)) == NULL) {
1117 BIO_printf(bio_err,
1118 "error while loading serial number\n");
1119 goto err;
1120 }
1121 if (cfg.verbose) {
1122 if (BN_is_zero(serial))
1123 BIO_printf(bio_err,
1124 "next serial number is 00\n");
1125 else {
1126 if ((f = BN_bn2hex(serial)) == NULL)
1127 goto err;
1128 BIO_printf(bio_err,
1129 "next serial number is %s\n", f);
1130 free(f);
1131 }
1132 }
1133 if ((attribs = NCONF_get_section(conf, cfg.policy)) ==
1134 NULL) {
1135 BIO_printf(bio_err, "unable to find 'section' for %s\n",
1136 cfg.policy);
1137 goto err;
1138 }
1139 if ((cert_sk = sk_X509_new_null()) == NULL) {
1140 BIO_printf(bio_err, "Memory allocation failure\n");
1141 goto err;
1142 }
1143 if (cfg.ss_cert_file != NULL) {
1144 total++;
1145 j = certify_cert(&x, cfg.ss_cert_file, pkey, x509,
1146 dgst, cfg.sigopts, attribs, db, serial,
1147 cfg.subj, cfg.chtype,
1148 cfg.multirdn, cfg.email_dn,
1149 cfg.startdate, cfg.enddate,
1150 cfg.days, cfg.batch,
1151 cfg.extensions, conf, cfg.verbose,
1152 certopt, nameopt, default_op, ext_copy);
1153 if (j < 0)
1154 goto err;
1155 if (j > 0) {
1156 total_done++;
1157 BIO_printf(bio_err, "\n");
1158 if (!BN_add_word(serial, 1))
1159 goto err;
1160 if (!sk_X509_push(cert_sk, x)) {
1161 BIO_printf(bio_err,
1162 "Memory allocation failure\n");
1163 goto err;
1164 }
1165 }
1166 }
1167 if (cfg.infile != NULL) {
1168 total++;
1169 j = certify(&x, cfg.infile, pkey, x509p, dgst,
1170 cfg.sigopts, attribs, db, serial,
1171 cfg.subj, cfg.chtype,
1172 cfg.multirdn, cfg.email_dn,
1173 cfg.startdate, cfg.enddate,
1174 cfg.days, cfg.batch,
1175 cfg.extensions, conf, cfg.verbose,
1176 certopt, nameopt, default_op, ext_copy,
1177 cfg.selfsign);
1178 if (j < 0)
1179 goto err;
1180 if (j > 0) {
1181 total_done++;
1182 BIO_printf(bio_err, "\n");
1183 if (!BN_add_word(serial, 1))
1184 goto err;
1185 if (!sk_X509_push(cert_sk, x)) {
1186 BIO_printf(bio_err,
1187 "Memory allocation failure\n");
1188 goto err;
1189 }
1190 }
1191 }
1192 for (i = 0; i < cfg.infiles_num; i++) {
1193 total++;
1194 j = certify(&x, cfg.infiles[i], pkey, x509p, dgst,
1195 cfg.sigopts, attribs, db, serial,
1196 cfg.subj, cfg.chtype,
1197 cfg.multirdn, cfg.email_dn,
1198 cfg.startdate, cfg.enddate,
1199 cfg.days, cfg.batch,
1200 cfg.extensions, conf, cfg.verbose,
1201 certopt, nameopt, default_op, ext_copy,
1202 cfg.selfsign);
1203 if (j < 0)
1204 goto err;
1205 if (j > 0) {
1206 total_done++;
1207 BIO_printf(bio_err, "\n");
1208 if (!BN_add_word(serial, 1))
1209 goto err;
1210 if (!sk_X509_push(cert_sk, x)) {
1211 BIO_printf(bio_err,
1212 "Memory allocation failure\n");
1213 goto err;
1214 }
1215 }
1216 }
1217 /*
1218 * we have a stack of newly certified certificates and a data
1219 * base and serial number that need updating
1220 */
1221
1222 if (sk_X509_num(cert_sk) > 0) {
1223 if (!cfg.batch) {
1224 char answer[10];
1225
1226 BIO_printf(bio_err,
1227 "\n%d out of %d certificate requests certified, commit? [y/n]",
1228 total_done, total);
1229 (void) BIO_flush(bio_err);
1230 if (fgets(answer, sizeof answer - 1, stdin) ==
1231 NULL) {
1232 BIO_printf(bio_err,
1233 "CERTIFICATION CANCELED: I/O error\n");
1234 ret = 0;
1235 goto err;
1236 }
1237 if ((answer[0] != 'y') && (answer[0] != 'Y')) {
1238 BIO_printf(bio_err,
1239 "CERTIFICATION CANCELED\n");
1240 ret = 0;
1241 goto err;
1242 }
1243 }
1244 BIO_printf(bio_err,
1245 "Write out database with %d new entries\n",
1246 sk_X509_num(cert_sk));
1247
1248 if (!save_serial(serialfile, "new", serial, NULL))
1249 goto err;
1250
1251 if (!save_index(dbfile, "new", db))
1252 goto err;
1253 }
1254 if (cfg.verbose)
1255 BIO_printf(bio_err, "writing new certificates\n");
1256 for (i = 0; i < sk_X509_num(cert_sk); i++) {
1257 ASN1_INTEGER *serialNumber;
1258 int k;
1259 char *serialstr;
1260 unsigned char *data;
1261 char pempath[PATH_MAX];
1262
1263 x = sk_X509_value(cert_sk, i);
1264
1265 serialNumber = X509_get_serialNumber(x);
1266 j = ASN1_STRING_length(serialNumber);
1267 data = ASN1_STRING_data(serialNumber);
1268
1269 if (j > 0)
1270 serialstr = bin2hex(data, j);
1271 else
1272 serialstr = strdup("00");
1273 if (serialstr != NULL) {
1274 k = snprintf(pempath, sizeof(pempath),
1275 "%s/%s.pem", cfg.outdir, serialstr);
1276 free(serialstr);
1277 if (k < 0 || k >= sizeof(pempath)) {
1278 BIO_printf(bio_err,
1279 "certificate file name too long\n");
1280 goto err;
1281 }
1282 } else {
1283 BIO_printf(bio_err,
1284 "memory allocation failed\n");
1285 goto err;
1286 }
1287 if (cfg.verbose)
1288 BIO_printf(bio_err, "writing %s\n", pempath);
1289
1290 if (BIO_write_filename(Cout, pempath) <= 0) {
1291 perror(pempath);
1292 goto err;
1293 }
1294 if (!write_new_certificate(Cout, x, 0,
1295 cfg.notext))
1296 goto err;
1297 if (!write_new_certificate(Sout, x, output_der,
1298 cfg.notext))
1299 goto err;
1300 }
1301
1302 if (sk_X509_num(cert_sk)) {
1303 /* Rename the database and the serial file */
1304 if (!rotate_serial(serialfile, "new", "old"))
1305 goto err;
1306
1307 if (!rotate_index(dbfile, "new", "old"))
1308 goto err;
1309
1310 BIO_printf(bio_err, "Data Base Updated\n");
1311 }
1312 }
1313 /*****************************************************************/
1314 if (cfg.gencrl) {
1315 int crl_v2 = 0;
1316 if (cfg.crl_ext == NULL) {
1317 cfg.crl_ext = NCONF_get_string(conf,
1318 cfg.section, ENV_CRLEXT);
1319 if (cfg.crl_ext == NULL)
1320 ERR_clear_error();
1321 }
1322 if (cfg.crl_ext != NULL) {
1323 /* Check syntax of file */
1324 X509V3_CTX ctx;
1325 X509V3_set_ctx_test(&ctx);
1326 X509V3_set_nconf(&ctx, conf);
1327 if (!X509V3_EXT_add_nconf(conf, &ctx, cfg.crl_ext,
1328 NULL)) {
1329 BIO_printf(bio_err,
1330 "Error Loading CRL extension section %s\n",
1331 cfg.crl_ext);
1332 ret = 1;
1333 goto err;
1334 }
1335 }
1336 if ((crlnumberfile = NCONF_get_string(conf, cfg.section,
1337 ENV_CRLNUMBER)) != NULL)
1338 if ((crlnumber = load_serial(crlnumberfile, 0,
1339 NULL)) == NULL) {
1340 BIO_printf(bio_err,
1341 "error while loading CRL number\n");
1342 goto err;
1343 }
1344 if (!cfg.crldays && !cfg.crlhours &&
1345 !cfg.crlsec) {
1346 if (!NCONF_get_number(conf, cfg.section,
1347 ENV_DEFAULT_CRL_DAYS, &cfg.crldays))
1348 cfg.crldays = 0;
1349 if (!NCONF_get_number(conf, cfg.section,
1350 ENV_DEFAULT_CRL_HOURS, &cfg.crlhours))
1351 cfg.crlhours = 0;
1352 ERR_clear_error();
1353 }
1354 if ((cfg.crldays == 0) && (cfg.crlhours == 0) &&
1355 (cfg.crlsec == 0)) {
1356 BIO_printf(bio_err,
1357 "cannot lookup how long until the next CRL is issued\n");
1358 goto err;
1359 }
1360 if (cfg.verbose)
1361 BIO_printf(bio_err, "making CRL\n");
1362 if ((crl = X509_CRL_new()) == NULL)
1363 goto err;
1364 if (!X509_CRL_set_issuer_name(crl, X509_get_subject_name(x509)))
1365 goto err;
1366
1367 if ((tmptm = X509_gmtime_adj(NULL, 0)) == NULL)
1368 goto err;
1369 if (!X509_CRL_set_lastUpdate(crl, tmptm))
1370 goto err;
1371 if (X509_time_adj_ex(tmptm, cfg.crldays,
1372 cfg.crlhours * 60 * 60 + cfg.crlsec, NULL) ==
1373 NULL) {
1374 BIO_puts(bio_err, "error setting CRL nextUpdate\n");
1375 goto err;
1376 }
1377 if (!X509_CRL_set_nextUpdate(crl, tmptm))
1378 goto err;
1379 ASN1_TIME_free(tmptm);
1380 tmptm = NULL;
1381
1382 for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
1383 pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
1384 if (pp[DB_type][0] == DB_TYPE_REV) {
1385 if ((r = X509_REVOKED_new()) == NULL)
1386 goto err;
1387 j = make_revoked(r, pp[DB_rev_date]);
1388 if (!j)
1389 goto err;
1390 if (j == 2)
1391 crl_v2 = 1;
1392 if (!BN_hex2bn(&serial, pp[DB_serial]))
1393 goto err;
1394 tmpserial = BN_to_ASN1_INTEGER(serial, NULL);
1395 BN_free(serial);
1396 serial = NULL;
1397 if (tmpserial == NULL)
1398 goto err;
1399 if (!X509_REVOKED_set_serialNumber(r, tmpserial)) {
1400 ASN1_INTEGER_free(tmpserial);
1401 goto err;
1402 }
1403 ASN1_INTEGER_free(tmpserial);
1404 if (!X509_CRL_add0_revoked(crl, r))
1405 goto err;
1406 r = NULL;
1407 }
1408 }
1409
1410 /*
1411 * sort the data so it will be written in serial number order
1412 */
1413 X509_CRL_sort(crl);
1414
1415 /* we now have a CRL */
1416 if (cfg.verbose)
1417 BIO_printf(bio_err, "signing CRL\n");
1418
1419 /* Add any extensions asked for */
1420
1421 if (cfg.crl_ext != NULL || crlnumberfile != NULL) {
1422 X509V3_CTX crlctx;
1423 X509V3_set_ctx(&crlctx, x509, NULL, NULL, crl, 0);
1424 X509V3_set_nconf(&crlctx, conf);
1425
1426 if (cfg.crl_ext != NULL)
1427 if (!X509V3_EXT_CRL_add_nconf(conf, &crlctx,
1428 cfg.crl_ext, crl))
1429 goto err;
1430 if (crlnumberfile != NULL) {
1431 tmpserial = BN_to_ASN1_INTEGER(crlnumber, NULL);
1432 if (tmpserial == NULL)
1433 goto err;
1434 if (!X509_CRL_add1_ext_i2d(crl, NID_crl_number,
1435 tmpserial, 0, 0)) {
1436 ASN1_INTEGER_free(tmpserial);
1437 goto err;
1438 }
1439 ASN1_INTEGER_free(tmpserial);
1440 crl_v2 = 1;
1441 if (!BN_add_word(crlnumber, 1))
1442 goto err;
1443 }
1444 }
1445 if (cfg.crl_ext != NULL || crl_v2) {
1446 if (!X509_CRL_set_version(crl, 1))
1447 goto err; /* version 2 CRL */
1448 }
1449 if (crlnumberfile != NULL) /* we have a CRL number that
1450 * need updating */
1451 if (!save_serial(crlnumberfile, "new", crlnumber, NULL))
1452 goto err;
1453
1454 BN_free(crlnumber);
1455 crlnumber = NULL;
1456
1457 if (!do_X509_CRL_sign(bio_err, crl, pkey, dgst,
1458 cfg.sigopts))
1459 goto err;
1460
1461 if (!PEM_write_bio_X509_CRL(Sout, crl))
1462 goto err;
1463
1464 if (crlnumberfile != NULL) /* Rename the crlnumber file */
1465 if (!rotate_serial(crlnumberfile, "new", "old"))
1466 goto err;
1467
1468 }
1469 /*****************************************************************/
1470 if (cfg.dorevoke) {
1471 if (cfg.infile == NULL) {
1472 BIO_printf(bio_err, "no input files\n");
1473 goto err;
1474 } else {
1475 X509 *revcert;
1476 revcert = load_cert(bio_err, cfg.infile,
1477 FORMAT_PEM, NULL, cfg.infile);
1478 if (revcert == NULL)
1479 goto err;
1480 j = do_revoke(revcert, db, cfg.rev_type,
1481 cfg.rev_arg);
1482 if (j <= 0)
1483 goto err;
1484 X509_free(revcert);
1485
1486 if (!save_index(dbfile, "new", db))
1487 goto err;
1488
1489 if (!rotate_index(dbfile, "new", "old"))
1490 goto err;
1491
1492 BIO_printf(bio_err, "Data Base Updated\n");
1493 }
1494 }
1495 /*****************************************************************/
1496 ret = 0;
1497
1498 err:
1499 free(tofree);
1500
1501 BIO_free_all(Cout);
1502 BIO_free_all(Sout);
1503 BIO_free_all(out);
1504 BIO_free_all(in);
1505
1506 sk_X509_pop_free(cert_sk, X509_free);
1507
1508 if (ret)
1509 ERR_print_errors(bio_err);
1510 if (free_key)
1511 free(cfg.key);
1512 BN_free(serial);
1513 BN_free(crlnumber);
1514 free_index(db);
1515 sk_OPENSSL_STRING_free(cfg.sigopts);
1516 EVP_PKEY_free(pkey);
1517 X509_free(x509);
1518 X509_CRL_free(crl);
1519 X509_REVOKED_free(r);
1520 ASN1_TIME_free(tmptm);
1521 NCONF_free(conf);
1522 NCONF_free(extconf);
1523 OBJ_cleanup();
1524
1525 return (ret);
1526 }
1527
1528 static void
lookup_fail(const char * name,const char * tag)1529 lookup_fail(const char *name, const char *tag)
1530 {
1531 BIO_printf(bio_err, "variable lookup failed for %s::%s\n", name, tag);
1532 }
1533
1534 static int
certify(X509 ** xret,char * infile,EVP_PKEY * pkey,X509 * x509,const EVP_MD * dgst,STACK_OF (OPENSSL_STRING)* sigopts,STACK_OF (CONF_VALUE)* policy,CA_DB * db,BIGNUM * serial,char * subj,unsigned long chtype,int multirdn,int email_dn,char * startdate,char * enddate,long days,int batch,char * ext_sect,CONF * lconf,int verbose,unsigned long certopt,unsigned long nameopt,int default_op,int ext_copy,int selfsign)1535 certify(X509 **xret, char *infile, EVP_PKEY *pkey, X509 *x509,
1536 const EVP_MD *dgst, STACK_OF(OPENSSL_STRING) *sigopts,
1537 STACK_OF(CONF_VALUE) *policy, CA_DB *db, BIGNUM *serial, char *subj,
1538 unsigned long chtype, int multirdn, int email_dn, char *startdate,
1539 char *enddate, long days, int batch, char *ext_sect, CONF *lconf,
1540 int verbose, unsigned long certopt, unsigned long nameopt, int default_op,
1541 int ext_copy, int selfsign)
1542 {
1543 X509_REQ *req = NULL;
1544 BIO *in = NULL;
1545 EVP_PKEY *pktmp = NULL;
1546 int ok = -1, i;
1547
1548 in = BIO_new(BIO_s_file());
1549
1550 if (BIO_read_filename(in, infile) <= 0) {
1551 perror(infile);
1552 goto err;
1553 }
1554 if ((req = PEM_read_bio_X509_REQ(in, NULL, NULL, NULL)) == NULL) {
1555 BIO_printf(bio_err, "Error reading certificate request in %s\n",
1556 infile);
1557 goto err;
1558 }
1559 if (verbose) {
1560 if (!X509_REQ_print(bio_err, req))
1561 goto err;
1562 }
1563
1564 BIO_printf(bio_err, "Check that the request matches the signature\n");
1565
1566 if (selfsign && !X509_REQ_check_private_key(req, pkey)) {
1567 BIO_printf(bio_err,
1568 "Certificate request and CA private key do not match\n");
1569 ok = 0;
1570 goto err;
1571 }
1572 if ((pktmp = X509_REQ_get0_pubkey(req)) == NULL) {
1573 BIO_printf(bio_err, "error unpacking public key\n");
1574 goto err;
1575 }
1576 i = X509_REQ_verify(req, pktmp);
1577 if (i < 0) {
1578 ok = 0;
1579 BIO_printf(bio_err, "Signature verification problems....\n");
1580 goto err;
1581 }
1582 if (i == 0) {
1583 ok = 0;
1584 BIO_printf(bio_err,
1585 "Signature did not match the certificate request\n");
1586 goto err;
1587 } else
1588 BIO_printf(bio_err, "Signature ok\n");
1589
1590 ok = do_body(xret, pkey, x509, dgst, sigopts, policy, db, serial,
1591 subj, chtype, multirdn, email_dn, startdate, enddate, days, batch,
1592 verbose, req, ext_sect, lconf, certopt, nameopt, default_op,
1593 ext_copy, selfsign);
1594
1595 err:
1596 X509_REQ_free(req);
1597 BIO_free(in);
1598
1599 return (ok);
1600 }
1601
1602 static int
certify_cert(X509 ** xret,char * infile,EVP_PKEY * pkey,X509 * x509,const EVP_MD * dgst,STACK_OF (OPENSSL_STRING)* sigopts,STACK_OF (CONF_VALUE)* policy,CA_DB * db,BIGNUM * serial,char * subj,unsigned long chtype,int multirdn,int email_dn,char * startdate,char * enddate,long days,int batch,char * ext_sect,CONF * lconf,int verbose,unsigned long certopt,unsigned long nameopt,int default_op,int ext_copy)1603 certify_cert(X509 **xret, char *infile, EVP_PKEY *pkey, X509 *x509,
1604 const EVP_MD *dgst, STACK_OF(OPENSSL_STRING) *sigopts,
1605 STACK_OF(CONF_VALUE) *policy, CA_DB *db, BIGNUM *serial, char *subj,
1606 unsigned long chtype, int multirdn, int email_dn, char *startdate,
1607 char *enddate, long days, int batch, char *ext_sect, CONF *lconf,
1608 int verbose, unsigned long certopt, unsigned long nameopt, int default_op,
1609 int ext_copy)
1610 {
1611 X509 *req = NULL;
1612 X509_REQ *rreq = NULL;
1613 EVP_PKEY *pktmp = NULL;
1614 int ok = -1, i;
1615
1616 if ((req = load_cert(bio_err, infile, FORMAT_PEM, NULL,
1617 infile)) == NULL)
1618 goto err;
1619 if (verbose) {
1620 if (!X509_print(bio_err, req))
1621 goto err;
1622 }
1623
1624 BIO_printf(bio_err, "Check that the request matches the signature\n");
1625
1626 if ((pktmp = X509_get0_pubkey(req)) == NULL) {
1627 BIO_printf(bio_err, "error unpacking public key\n");
1628 goto err;
1629 }
1630 i = X509_verify(req, pktmp);
1631 if (i < 0) {
1632 ok = 0;
1633 BIO_printf(bio_err, "Signature verification problems....\n");
1634 goto err;
1635 }
1636 if (i == 0) {
1637 ok = 0;
1638 BIO_printf(bio_err,
1639 "Signature did not match the certificate\n");
1640 goto err;
1641 } else
1642 BIO_printf(bio_err, "Signature ok\n");
1643
1644 if ((rreq = X509_to_X509_REQ(req, NULL, EVP_md5())) == NULL)
1645 goto err;
1646
1647 ok = do_body(xret, pkey, x509, dgst, sigopts, policy, db, serial,
1648 subj, chtype, multirdn, email_dn, startdate, enddate, days, batch,
1649 verbose, rreq, ext_sect, lconf, certopt, nameopt, default_op,
1650 ext_copy, 0);
1651
1652 err:
1653 X509_REQ_free(rreq);
1654 X509_free(req);
1655
1656 return (ok);
1657 }
1658
1659 static int
do_body(X509 ** xret,EVP_PKEY * pkey,X509 * x509,const EVP_MD * dgst,STACK_OF (OPENSSL_STRING)* sigopts,STACK_OF (CONF_VALUE)* policy,CA_DB * db,BIGNUM * serial,char * subj,unsigned long chtype,int multirdn,int email_dn,char * startdate,char * enddate,long days,int batch,int verbose,X509_REQ * req,char * ext_sect,CONF * lconf,unsigned long certopt,unsigned long nameopt,int default_op,int ext_copy,int selfsign)1660 do_body(X509 **xret, EVP_PKEY *pkey, X509 *x509, const EVP_MD *dgst,
1661 STACK_OF(OPENSSL_STRING) *sigopts, STACK_OF(CONF_VALUE) *policy,
1662 CA_DB *db, BIGNUM *serial, char *subj, unsigned long chtype, int multirdn,
1663 int email_dn, char *startdate, char *enddate, long days, int batch,
1664 int verbose, X509_REQ *req, char *ext_sect, CONF *lconf,
1665 unsigned long certopt, unsigned long nameopt, int default_op,
1666 int ext_copy, int selfsign)
1667 {
1668 X509_NAME *name = NULL, *CAname = NULL;
1669 X509_NAME *subject = NULL, *dn_subject = NULL;
1670 ASN1_UTCTIME *tm;
1671 ASN1_STRING *str, *str2;
1672 ASN1_OBJECT *obj;
1673 X509 *ret = NULL;
1674 X509_NAME_ENTRY *ne;
1675 X509_NAME_ENTRY *tne, *push;
1676 EVP_PKEY *pktmp;
1677 int ok = -1, i, j, last, nid;
1678 const char *p;
1679 CONF_VALUE *cv;
1680 OPENSSL_STRING row[DB_NUMBER];
1681 OPENSSL_STRING *irow = NULL;
1682 OPENSSL_STRING *rrow = NULL;
1683 const STACK_OF(X509_EXTENSION) *exts;
1684
1685 *xret = NULL;
1686
1687 for (i = 0; i < DB_NUMBER; i++)
1688 row[i] = NULL;
1689
1690 if (subj != NULL) {
1691 X509_NAME *n = parse_name(subj, chtype, multirdn);
1692
1693 if (n == NULL) {
1694 ERR_print_errors(bio_err);
1695 goto err;
1696 }
1697 if (!X509_REQ_set_subject_name(req, n)) {
1698 X509_NAME_free(n);
1699 goto err;
1700 }
1701 X509_NAME_free(n);
1702 }
1703 if (default_op)
1704 BIO_printf(bio_err,
1705 "The Subject's Distinguished Name is as follows\n");
1706
1707 name = X509_REQ_get_subject_name(req);
1708 for (i = 0; i < X509_NAME_entry_count(name); i++) {
1709 ne = X509_NAME_get_entry(name, i);
1710 if (ne == NULL)
1711 goto err;
1712 str = X509_NAME_ENTRY_get_data(ne);
1713 if (str == NULL)
1714 goto err;
1715 obj = X509_NAME_ENTRY_get_object(ne);
1716 if (obj == NULL)
1717 goto err;
1718
1719 if (cfg.msie_hack) {
1720 /* assume all type should be strings */
1721 nid = OBJ_obj2nid(X509_NAME_ENTRY_get_object(ne));
1722 if (nid == NID_undef)
1723 goto err;
1724
1725 if (str->type == V_ASN1_UNIVERSALSTRING)
1726 ASN1_UNIVERSALSTRING_to_string(str);
1727
1728 if ((str->type == V_ASN1_IA5STRING) &&
1729 (nid != NID_pkcs9_emailAddress))
1730 str->type = V_ASN1_T61STRING;
1731
1732 if ((nid == NID_pkcs9_emailAddress) &&
1733 (str->type == V_ASN1_PRINTABLESTRING))
1734 str->type = V_ASN1_IA5STRING;
1735 }
1736 /* If no EMAIL is wanted in the subject */
1737 if ((OBJ_obj2nid(obj) == NID_pkcs9_emailAddress) && (!email_dn))
1738 continue;
1739
1740 /* check some things */
1741 if ((OBJ_obj2nid(obj) == NID_pkcs9_emailAddress) &&
1742 (str->type != V_ASN1_IA5STRING)) {
1743 BIO_printf(bio_err,
1744 "\nemailAddress type needs to be of type IA5STRING\n");
1745 goto err;
1746 }
1747 if ((str->type != V_ASN1_BMPSTRING) &&
1748 (str->type != V_ASN1_UTF8STRING)) {
1749 j = ASN1_PRINTABLE_type(str->data, str->length);
1750 if (((j == V_ASN1_T61STRING) &&
1751 (str->type != V_ASN1_T61STRING)) ||
1752 ((j == V_ASN1_IA5STRING) &&
1753 (str->type == V_ASN1_PRINTABLESTRING))) {
1754 BIO_printf(bio_err,
1755 "\nThe string contains characters that are illegal for the ASN.1 type\n");
1756 goto err;
1757 }
1758 }
1759 if (default_op)
1760 old_entry_print(bio_err, obj, str);
1761 }
1762
1763 /* Ok, now we check the 'policy' stuff. */
1764 if ((subject = X509_NAME_new()) == NULL) {
1765 BIO_printf(bio_err, "Memory allocation failure\n");
1766 goto err;
1767 }
1768 /* take a copy of the issuer name before we mess with it. */
1769 if (selfsign)
1770 CAname = X509_NAME_dup(name);
1771 else
1772 CAname = X509_NAME_dup(X509_get_subject_name(x509));
1773 if (CAname == NULL)
1774 goto err;
1775 str = str2 = NULL;
1776
1777 for (i = 0; i < sk_CONF_VALUE_num(policy); i++) {
1778 cv = sk_CONF_VALUE_value(policy, i); /* get the object id */
1779 if ((j = OBJ_txt2nid(cv->name)) == NID_undef) {
1780 BIO_printf(bio_err,
1781 "%s:unknown object type in 'policy' configuration\n",
1782 cv->name);
1783 goto err;
1784 }
1785 obj = OBJ_nid2obj(j);
1786 if (obj == NULL)
1787 goto err;
1788
1789 last = -1;
1790 for (;;) {
1791 /* lookup the object in the supplied name list */
1792 j = X509_NAME_get_index_by_OBJ(name, obj, last);
1793 if (j < 0) {
1794 if (last != -1)
1795 break;
1796 tne = NULL;
1797 } else {
1798 tne = X509_NAME_get_entry(name, j);
1799 if (tne == NULL)
1800 goto err;
1801 }
1802 last = j;
1803
1804 /* depending on the 'policy', decide what to do. */
1805 push = NULL;
1806 if (strcmp(cv->value, "optional") == 0) {
1807 if (tne != NULL)
1808 push = tne;
1809 } else if (strcmp(cv->value, "supplied") == 0) {
1810 if (tne == NULL) {
1811 BIO_printf(bio_err,
1812 "The %s field needed to be supplied and was missing\n",
1813 cv->name);
1814 goto err;
1815 } else
1816 push = tne;
1817 } else if (strcmp(cv->value, "match") == 0) {
1818 int last2;
1819
1820 if (tne == NULL) {
1821 BIO_printf(bio_err,
1822 "The mandatory %s field was missing\n",
1823 cv->name);
1824 goto err;
1825 }
1826 last2 = -1;
1827
1828 again2:
1829 j = X509_NAME_get_index_by_OBJ(CAname, obj,
1830 last2);
1831 if ((j < 0) && (last2 == -1)) {
1832 BIO_printf(bio_err,
1833 "The %s field does not exist in the CA certificate,\nthe 'policy' is misconfigured\n",
1834 cv->name);
1835 goto err;
1836 }
1837 if (j >= 0) {
1838 push = X509_NAME_get_entry(CAname, j);
1839 if (push == NULL)
1840 goto err;
1841 str = X509_NAME_ENTRY_get_data(tne);
1842 if (str == NULL)
1843 goto err;
1844 str2 = X509_NAME_ENTRY_get_data(push);
1845 if (str2 == NULL)
1846 goto err;
1847 last2 = j;
1848 if (ASN1_STRING_cmp(str, str2) != 0)
1849 goto again2;
1850 }
1851 if (j < 0) {
1852 BIO_printf(bio_err,
1853 "The %s field needed to be the same in the\nCA certificate (%s) and the request (%s)\n",
1854 cv->name, ((str2 == NULL) ?
1855 "NULL" : (char *) str2->data),
1856 ((str == NULL) ?
1857 "NULL" : (char *) str->data));
1858 goto err;
1859 }
1860 } else {
1861 BIO_printf(bio_err,
1862 "%s:invalid type in 'policy' configuration\n",
1863 cv->value);
1864 goto err;
1865 }
1866
1867 if (push != NULL) {
1868 if (!X509_NAME_add_entry(subject, push,
1869 -1, 0)) {
1870 X509_NAME_ENTRY_free(push);
1871 BIO_printf(bio_err,
1872 "Memory allocation failure\n");
1873 goto err;
1874 }
1875 }
1876 if (j < 0)
1877 break;
1878 }
1879 }
1880
1881 if (cfg.preserve) {
1882 X509_NAME_free(subject);
1883 /* subject=X509_NAME_dup(X509_REQ_get_subject_name(req)); */
1884 subject = X509_NAME_dup(name);
1885 if (subject == NULL)
1886 goto err;
1887 }
1888
1889 /* We are now totally happy, lets make and sign the certificate */
1890 if (verbose)
1891 BIO_printf(bio_err,
1892 "Everything appears to be ok, creating and signing the certificate\n");
1893
1894 if ((ret = X509_new()) == NULL)
1895 goto err;
1896
1897 #ifdef X509_V3
1898 /* Make it an X509 v3 certificate. */
1899 if (!X509_set_version(ret, 2))
1900 goto err;
1901 #endif
1902 if (X509_get_serialNumber(ret) == NULL)
1903 goto err;
1904 if (BN_to_ASN1_INTEGER(serial, X509_get_serialNumber(ret)) == NULL)
1905 goto err;
1906 if (selfsign) {
1907 if (!X509_set_issuer_name(ret, subject))
1908 goto err;
1909 } else {
1910 if (!X509_set_issuer_name(ret, X509_get_subject_name(x509)))
1911 goto err;
1912 }
1913
1914 if (strcmp(startdate, "today") == 0) {
1915 if (X509_gmtime_adj(X509_get_notBefore(ret), 0) == NULL)
1916 goto err;
1917 } else if (!ASN1_TIME_set_string_X509(X509_get_notBefore(ret), startdate)) {
1918 BIO_printf(bio_err, "Invalid start date %s\n", startdate);
1919 goto err;
1920 }
1921
1922 if (enddate == NULL) {
1923 if (X509_time_adj_ex(X509_get_notAfter(ret), days, 0,
1924 NULL) == NULL)
1925 goto err;
1926 } else if (!ASN1_TIME_set_string_X509(X509_get_notAfter(ret), enddate)) {
1927 BIO_printf(bio_err, "Invalid end date %s\n", enddate);
1928 goto err;
1929 }
1930
1931 if (!X509_set_subject_name(ret, subject))
1932 goto err;
1933
1934 if ((pktmp = X509_REQ_get0_pubkey(req)) == NULL)
1935 goto err;
1936
1937 if (!X509_set_pubkey(ret, pktmp))
1938 goto err;
1939
1940 /* Lets add the extensions, if there are any */
1941 if (ext_sect != NULL) {
1942 X509V3_CTX ctx;
1943
1944 /* Initialize the context structure */
1945 if (selfsign)
1946 X509V3_set_ctx(&ctx, ret, ret, req, NULL, 0);
1947 else
1948 X509V3_set_ctx(&ctx, x509, ret, req, NULL, 0);
1949
1950 if (extconf != NULL) {
1951 if (verbose)
1952 BIO_printf(bio_err,
1953 "Extra configuration file found\n");
1954
1955 /* Use the extconf configuration db LHASH */
1956 X509V3_set_nconf(&ctx, extconf);
1957
1958 /* Test the structure (needed?) */
1959 /* X509V3_set_ctx_test(&ctx); */
1960
1961 /* Adds exts contained in the configuration file */
1962 if (!X509V3_EXT_add_nconf(extconf, &ctx,
1963 ext_sect, ret)) {
1964 BIO_printf(bio_err,
1965 "ERROR: adding extensions in section %s\n",
1966 ext_sect);
1967 ERR_print_errors(bio_err);
1968 goto err;
1969 }
1970 if (verbose)
1971 BIO_printf(bio_err,
1972 "Successfully added extensions from file.\n");
1973 } else if (ext_sect != NULL) {
1974 /* We found extensions to be set from config file */
1975 X509V3_set_nconf(&ctx, lconf);
1976
1977 if (!X509V3_EXT_add_nconf(lconf, &ctx, ext_sect, ret)) {
1978 BIO_printf(bio_err,
1979 "ERROR: adding extensions in section %s\n",
1980 ext_sect);
1981 ERR_print_errors(bio_err);
1982 goto err;
1983 }
1984 if (verbose)
1985 BIO_printf(bio_err,
1986 "Successfully added extensions from config\n");
1987 }
1988 }
1989
1990 /* Copy extensions from request (if any) */
1991 if (!copy_extensions(ret, req, ext_copy)) {
1992 BIO_printf(bio_err, "ERROR: adding extensions from request\n");
1993 ERR_print_errors(bio_err);
1994 goto err;
1995 }
1996
1997 exts = X509_get0_extensions(ret);
1998 if (exts != NULL && sk_X509_EXTENSION_num(exts) > 0) {
1999 /* Make it an X509 v3 certificate. */
2000 if (!X509_set_version(ret, 2))
2001 goto err;
2002 }
2003
2004 if (verbose)
2005 BIO_printf(bio_err,
2006 "The subject name appears to be ok, checking data base for clashes\n");
2007
2008 /* Build the correct Subject if no email is wanted in the subject */
2009 if (!email_dn) {
2010 X509_NAME_ENTRY *tmpne;
2011 /*
2012 * Its best to dup the subject DN and then delete any email
2013 * addresses because this retains its structure.
2014 */
2015 if ((dn_subject = X509_NAME_dup(subject)) == NULL) {
2016 BIO_printf(bio_err, "Memory allocation failure\n");
2017 goto err;
2018 }
2019 while ((i = X509_NAME_get_index_by_NID(dn_subject,
2020 NID_pkcs9_emailAddress, -1)) >= 0) {
2021 tmpne = X509_NAME_get_entry(dn_subject, i);
2022 if (tmpne == NULL)
2023 goto err;
2024 if (X509_NAME_delete_entry(dn_subject, i) == NULL) {
2025 X509_NAME_ENTRY_free(tmpne);
2026 goto err;
2027 }
2028 X509_NAME_ENTRY_free(tmpne);
2029 }
2030
2031 if (!X509_set_subject_name(ret, dn_subject))
2032 goto err;
2033
2034 X509_NAME_free(dn_subject);
2035 dn_subject = NULL;
2036 }
2037
2038 row[DB_name] = X509_NAME_oneline(X509_get_subject_name(ret), NULL, 0);
2039 if (row[DB_name] == NULL) {
2040 BIO_printf(bio_err, "Memory allocation failure\n");
2041 goto err;
2042 }
2043
2044 if (BN_is_zero(serial))
2045 row[DB_serial] = strdup("00");
2046 else
2047 row[DB_serial] = BN_bn2hex(serial);
2048 if (row[DB_serial] == NULL) {
2049 BIO_printf(bio_err, "Memory allocation failure\n");
2050 goto err;
2051 }
2052
2053 if (row[DB_name][0] == '\0') {
2054 /*
2055 * An empty subject! We'll use the serial number instead. If
2056 * unique_subject is in use then we don't want different
2057 * entries with empty subjects matching each other.
2058 */
2059 free(row[DB_name]);
2060 row[DB_name] = strdup(row[DB_serial]);
2061 if (row[DB_name] == NULL) {
2062 BIO_printf(bio_err, "Memory allocation failure\n");
2063 goto err;
2064 }
2065 }
2066
2067 if (db->attributes.unique_subject) {
2068 OPENSSL_STRING *crow = row;
2069
2070 rrow = TXT_DB_get_by_index(db->db, DB_name, crow);
2071 if (rrow != NULL) {
2072 BIO_printf(bio_err,
2073 "ERROR:There is already a certificate for %s\n",
2074 row[DB_name]);
2075 }
2076 }
2077 if (rrow == NULL) {
2078 rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
2079 if (rrow != NULL) {
2080 BIO_printf(bio_err,
2081 "ERROR:Serial number %s has already been issued,\n",
2082 row[DB_serial]);
2083 BIO_printf(bio_err,
2084 " check the database/serial_file for corruption\n");
2085 }
2086 }
2087 if (rrow != NULL) {
2088 BIO_printf(bio_err,
2089 "The matching entry has the following details\n");
2090 if (rrow[DB_type][0] == DB_TYPE_EXP)
2091 p = "Expired";
2092 else if (rrow[DB_type][0] == DB_TYPE_REV)
2093 p = "Revoked";
2094 else if (rrow[DB_type][0] == DB_TYPE_VAL)
2095 p = "Valid";
2096 else
2097 p = "\ninvalid type, Data base error\n";
2098 BIO_printf(bio_err, "Type :%s\n", p);
2099 if (rrow[DB_type][0] == DB_TYPE_REV) {
2100 p = rrow[DB_exp_date];
2101 if (p == NULL)
2102 p = "undef";
2103 BIO_printf(bio_err, "Was revoked on:%s\n", p);
2104 }
2105 p = rrow[DB_exp_date];
2106 if (p == NULL)
2107 p = "undef";
2108 BIO_printf(bio_err, "Expires on :%s\n", p);
2109 p = rrow[DB_serial];
2110 if (p == NULL)
2111 p = "undef";
2112 BIO_printf(bio_err, "Serial Number :%s\n", p);
2113 p = rrow[DB_file];
2114 if (p == NULL)
2115 p = "undef";
2116 BIO_printf(bio_err, "File name :%s\n", p);
2117 p = rrow[DB_name];
2118 if (p == NULL)
2119 p = "undef";
2120 BIO_printf(bio_err, "Subject Name :%s\n", p);
2121 ok = -1; /* This is now a 'bad' error. */
2122 goto err;
2123 }
2124
2125 if (!default_op) {
2126 BIO_printf(bio_err, "Certificate Details:\n");
2127 /*
2128 * Never print signature details because signature not
2129 * present
2130 */
2131 certopt |= X509_FLAG_NO_SIGDUMP | X509_FLAG_NO_SIGNAME;
2132 if (!X509_print_ex(bio_err, ret, nameopt, certopt))
2133 goto err;
2134 }
2135 BIO_printf(bio_err, "Certificate is to be certified until ");
2136 ASN1_TIME_print(bio_err, X509_get_notAfter(ret));
2137 if (days)
2138 BIO_printf(bio_err, " (%ld days)", days);
2139 BIO_printf(bio_err, "\n");
2140
2141 if (!batch) {
2142 char answer[25];
2143
2144 BIO_printf(bio_err, "Sign the certificate? [y/n]:");
2145 (void) BIO_flush(bio_err);
2146 if (!fgets(answer, sizeof(answer) - 1, stdin)) {
2147 BIO_printf(bio_err,
2148 "CERTIFICATE WILL NOT BE CERTIFIED: I/O error\n");
2149 ok = 0;
2150 goto err;
2151 }
2152 if (!((answer[0] == 'y') || (answer[0] == 'Y'))) {
2153 BIO_printf(bio_err,
2154 "CERTIFICATE WILL NOT BE CERTIFIED\n");
2155 ok = 0;
2156 goto err;
2157 }
2158 }
2159
2160 if ((pktmp = X509_get0_pubkey(ret)) == NULL)
2161 goto err;
2162
2163 if (EVP_PKEY_missing_parameters(pktmp) &&
2164 !EVP_PKEY_missing_parameters(pkey)) {
2165 if (!EVP_PKEY_copy_parameters(pktmp, pkey)) {
2166 goto err;
2167 }
2168 }
2169
2170 if (!do_X509_sign(bio_err, ret, pkey, dgst, sigopts))
2171 goto err;
2172
2173 /* We now just add it to the database */
2174 row[DB_type] = malloc(2);
2175
2176 if ((tm = X509_get_notAfter(ret)) == NULL)
2177 goto err;
2178 row[DB_exp_date] = strndup(tm->data, tm->length);
2179 if (row[DB_type] == NULL || row[DB_exp_date] == NULL) {
2180 BIO_printf(bio_err, "Memory allocation failure\n");
2181 goto err;
2182 }
2183
2184 row[DB_rev_date] = NULL;
2185
2186 /* row[DB_serial] done already */
2187 row[DB_file] = malloc(8);
2188
2189 if ((row[DB_type] == NULL) || (row[DB_file] == NULL) ||
2190 (row[DB_name] == NULL)) {
2191 BIO_printf(bio_err, "Memory allocation failure\n");
2192 goto err;
2193 }
2194 (void) strlcpy(row[DB_file], "unknown", 8);
2195 row[DB_type][0] = DB_TYPE_VAL;
2196 row[DB_type][1] = '\0';
2197
2198 if ((irow = reallocarray(NULL, DB_NUMBER + 1, sizeof(char *))) ==
2199 NULL) {
2200 BIO_printf(bio_err, "Memory allocation failure\n");
2201 goto err;
2202 }
2203 for (i = 0; i < DB_NUMBER; i++) {
2204 irow[i] = row[i];
2205 row[i] = NULL;
2206 }
2207 irow[DB_NUMBER] = NULL;
2208
2209 if (!TXT_DB_insert(db->db, irow)) {
2210 BIO_printf(bio_err, "failed to update database\n");
2211 BIO_printf(bio_err, "TXT_DB error number %ld\n", db->db->error);
2212 goto err;
2213 }
2214
2215 *xret = ret;
2216 ret = NULL;
2217 ok = 1;
2218
2219 err:
2220 for (i = 0; i < DB_NUMBER; i++)
2221 free(row[i]);
2222
2223 X509_NAME_free(CAname);
2224 X509_NAME_free(subject);
2225 X509_NAME_free(dn_subject);
2226 X509_free(ret);
2227
2228 return (ok);
2229 }
2230
2231 static int
write_new_certificate(BIO * bp,X509 * x,int output_der,int notext)2232 write_new_certificate(BIO *bp, X509 *x, int output_der, int notext)
2233 {
2234 if (output_der) {
2235 if (!i2d_X509_bio(bp, x))
2236 return (0);
2237 }
2238 if (!notext) {
2239 if (!X509_print(bp, x))
2240 return (0);
2241 }
2242
2243 return PEM_write_bio_X509(bp, x);
2244 }
2245
2246 static int
check_time_format(const char * str)2247 check_time_format(const char *str)
2248 {
2249 return ASN1_TIME_set_string(NULL, str);
2250 }
2251
2252 static int
do_revoke(X509 * x509,CA_DB * db,int type,char * value)2253 do_revoke(X509 *x509, CA_DB *db, int type, char *value)
2254 {
2255 ASN1_UTCTIME *tm = NULL;
2256 char *row[DB_NUMBER], **rrow, **irow;
2257 char *rev_str = NULL;
2258 BIGNUM *bn = NULL;
2259 int ok = -1, i;
2260
2261 for (i = 0; i < DB_NUMBER; i++)
2262 row[i] = NULL;
2263 row[DB_name] = X509_NAME_oneline(X509_get_subject_name(x509), NULL, 0);
2264 bn = ASN1_INTEGER_to_BN(X509_get_serialNumber(x509), NULL);
2265 if (bn == NULL)
2266 goto err;
2267 if (BN_is_zero(bn))
2268 row[DB_serial] = strdup("00");
2269 else
2270 row[DB_serial] = BN_bn2hex(bn);
2271 BN_free(bn);
2272
2273 if (row[DB_name] != NULL && row[DB_name][0] == '\0') {
2274 /*
2275 * Entries with empty Subjects actually use the serial number
2276 * instead
2277 */
2278 free(row[DB_name]);
2279 row[DB_name] = strdup(row[DB_serial]);
2280 if (row[DB_name] == NULL) {
2281 BIO_printf(bio_err, "Memory allocation failure\n");
2282 goto err;
2283 }
2284 }
2285
2286 if ((row[DB_name] == NULL) || (row[DB_serial] == NULL)) {
2287 BIO_printf(bio_err, "Memory allocation failure\n");
2288 goto err;
2289 }
2290 /*
2291 * We have to lookup by serial number because name lookup skips
2292 * revoked certs
2293 */
2294 rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
2295 if (rrow == NULL) {
2296 BIO_printf(bio_err,
2297 "Adding Entry with serial number %s to DB for %s\n",
2298 row[DB_serial], row[DB_name]);
2299
2300 /* We now just add it to the database */
2301 row[DB_type] = malloc(2);
2302
2303 if ((tm = X509_get_notAfter(x509)) == NULL)
2304 goto err;
2305 row[DB_exp_date] = strndup(tm->data, tm->length);
2306 if (row[DB_type] == NULL || row[DB_exp_date] == NULL) {
2307 BIO_printf(bio_err, "Memory allocation failure\n");
2308 goto err;
2309 }
2310
2311 row[DB_rev_date] = NULL;
2312
2313 /* row[DB_serial] done already */
2314 row[DB_file] = malloc(8);
2315
2316 /* row[DB_name] done already */
2317
2318 if ((row[DB_type] == NULL) || (row[DB_file] == NULL)) {
2319 BIO_printf(bio_err, "Memory allocation failure\n");
2320 goto err;
2321 }
2322 (void) strlcpy(row[DB_file], "unknown", 8);
2323 row[DB_type][0] = DB_TYPE_VAL;
2324 row[DB_type][1] = '\0';
2325
2326 if ((irow = reallocarray(NULL, sizeof(char *),
2327 (DB_NUMBER + 1))) == NULL) {
2328 BIO_printf(bio_err, "Memory allocation failure\n");
2329 goto err;
2330 }
2331 for (i = 0; i < DB_NUMBER; i++) {
2332 irow[i] = row[i];
2333 row[i] = NULL;
2334 }
2335 irow[DB_NUMBER] = NULL;
2336
2337 if (!TXT_DB_insert(db->db, irow)) {
2338 BIO_printf(bio_err, "failed to update database\n");
2339 BIO_printf(bio_err, "TXT_DB error number %ld\n",
2340 db->db->error);
2341 goto err;
2342 }
2343 /* Revoke Certificate */
2344 ok = do_revoke(x509, db, type, value);
2345
2346 goto err;
2347
2348 } else if (index_name_cmp_noconst(row, rrow)) {
2349 BIO_printf(bio_err, "ERROR:name does not match %s\n",
2350 row[DB_name]);
2351 goto err;
2352 } else if (rrow[DB_type][0] == DB_TYPE_REV) {
2353 BIO_printf(bio_err, "ERROR:Already revoked, serial number %s\n",
2354 row[DB_serial]);
2355 goto err;
2356 } else {
2357 BIO_printf(bio_err, "Revoking Certificate %s.\n",
2358 rrow[DB_serial]);
2359 rev_str = make_revocation_str(type, value);
2360 if (rev_str == NULL) {
2361 BIO_printf(bio_err, "Error in revocation arguments\n");
2362 goto err;
2363 }
2364 rrow[DB_type][0] = DB_TYPE_REV;
2365 rrow[DB_type][1] = '\0';
2366 rrow[DB_rev_date] = rev_str;
2367 }
2368 ok = 1;
2369
2370 err:
2371 for (i = 0; i < DB_NUMBER; i++)
2372 free(row[i]);
2373
2374 return (ok);
2375 }
2376
2377 static int
get_certificate_status(const char * serial,CA_DB * db)2378 get_certificate_status(const char *serial, CA_DB *db)
2379 {
2380 char *row[DB_NUMBER], **rrow;
2381 int ok = -1, i;
2382
2383 /* Free Resources */
2384 for (i = 0; i < DB_NUMBER; i++)
2385 row[i] = NULL;
2386
2387 /* Malloc needed char spaces */
2388 row[DB_serial] = malloc(strlen(serial) + 2);
2389 if (row[DB_serial] == NULL) {
2390 BIO_printf(bio_err, "Malloc failure\n");
2391 goto err;
2392 }
2393 if (strlen(serial) % 2) {
2394 row[DB_serial][0] = '0';
2395
2396 /* Copy String from serial to row[DB_serial] */
2397 memcpy(row[DB_serial] + 1, serial, strlen(serial));
2398 row[DB_serial][strlen(serial) + 1] = '\0';
2399 } else {
2400 /* Copy String from serial to row[DB_serial] */
2401 memcpy(row[DB_serial], serial, strlen(serial));
2402 row[DB_serial][strlen(serial)] = '\0';
2403 }
2404
2405 /* Make it Upper Case */
2406 for (i = 0; row[DB_serial][i] != '\0'; i++)
2407 row[DB_serial][i] = toupper((unsigned char) row[DB_serial][i]);
2408
2409
2410 ok = 1;
2411
2412 /* Search for the certificate */
2413 rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
2414 if (rrow == NULL) {
2415 BIO_printf(bio_err, "Serial %s not present in db.\n",
2416 row[DB_serial]);
2417 ok = -1;
2418 goto err;
2419 } else if (rrow[DB_type][0] == DB_TYPE_VAL) {
2420 BIO_printf(bio_err, "%s=Valid (%c)\n",
2421 row[DB_serial], rrow[DB_type][0]);
2422 goto err;
2423 } else if (rrow[DB_type][0] == DB_TYPE_REV) {
2424 BIO_printf(bio_err, "%s=Revoked (%c)\n",
2425 row[DB_serial], rrow[DB_type][0]);
2426 goto err;
2427 } else if (rrow[DB_type][0] == DB_TYPE_EXP) {
2428 BIO_printf(bio_err, "%s=Expired (%c)\n",
2429 row[DB_serial], rrow[DB_type][0]);
2430 goto err;
2431 } else if (rrow[DB_type][0] == DB_TYPE_SUSP) {
2432 BIO_printf(bio_err, "%s=Suspended (%c)\n",
2433 row[DB_serial], rrow[DB_type][0]);
2434 goto err;
2435 } else {
2436 BIO_printf(bio_err, "%s=Unknown (%c).\n",
2437 row[DB_serial], rrow[DB_type][0]);
2438 ok = -1;
2439 }
2440
2441 err:
2442 for (i = 0; i < DB_NUMBER; i++)
2443 free(row[i]);
2444
2445 return (ok);
2446 }
2447
2448 static int
do_updatedb(CA_DB * db)2449 do_updatedb(CA_DB *db)
2450 {
2451 ASN1_UTCTIME *a_tm = NULL;
2452 int i, cnt = 0;
2453 int db_y2k, a_y2k; /* flags = 1 if y >= 2000 */
2454 char **rrow, *a_tm_s = NULL;
2455
2456 a_tm = ASN1_UTCTIME_new();
2457 if (a_tm == NULL) {
2458 cnt = -1;
2459 goto err;
2460 }
2461
2462 /* get actual time and make a string */
2463 a_tm = X509_gmtime_adj(a_tm, 0);
2464 if (a_tm == NULL) {
2465 cnt = -1;
2466 goto err;
2467 }
2468 a_tm_s = strndup(a_tm->data, a_tm->length);
2469 if (a_tm_s == NULL) {
2470 cnt = -1;
2471 goto err;
2472 }
2473
2474 if (strncmp(a_tm_s, "49", 2) <= 0)
2475 a_y2k = 1;
2476 else
2477 a_y2k = 0;
2478
2479 for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
2480 rrow = sk_OPENSSL_PSTRING_value(db->db->data, i);
2481
2482 if (rrow[DB_type][0] == DB_TYPE_VAL) {
2483 /* ignore entries that are not valid */
2484 if (strncmp(rrow[DB_exp_date], "49", 2) <= 0)
2485 db_y2k = 1;
2486 else
2487 db_y2k = 0;
2488
2489 if (db_y2k == a_y2k) {
2490 /* all on the same y2k side */
2491 if (strcmp(rrow[DB_exp_date], a_tm_s) <= 0) {
2492 rrow[DB_type][0] = DB_TYPE_EXP;
2493 rrow[DB_type][1] = '\0';
2494 cnt++;
2495
2496 BIO_printf(bio_err, "%s=Expired\n",
2497 rrow[DB_serial]);
2498 }
2499 } else if (db_y2k < a_y2k) {
2500 rrow[DB_type][0] = DB_TYPE_EXP;
2501 rrow[DB_type][1] = '\0';
2502 cnt++;
2503
2504 BIO_printf(bio_err, "%s=Expired\n",
2505 rrow[DB_serial]);
2506 }
2507 }
2508 }
2509
2510 err:
2511 ASN1_UTCTIME_free(a_tm);
2512 free(a_tm_s);
2513
2514 return (cnt);
2515 }
2516
2517 static const char *crl_reasons[] = {
2518 /* CRL reason strings */
2519 "unspecified",
2520 "keyCompromise",
2521 "CACompromise",
2522 "affiliationChanged",
2523 "superseded",
2524 "cessationOfOperation",
2525 "certificateHold",
2526 "removeFromCRL",
2527 /* Additional pseudo reasons */
2528 "holdInstruction",
2529 "keyTime",
2530 "CAkeyTime"
2531 };
2532
2533 #define NUM_REASONS (sizeof(crl_reasons) / sizeof(char *))
2534
2535 /* Given revocation information convert to a DB string.
2536 * The format of the string is:
2537 * revtime[,reason,extra]. Where 'revtime' is the
2538 * revocation time (the current time). 'reason' is the
2539 * optional CRL reason and 'extra' is any additional
2540 * argument
2541 */
2542
2543 char *
make_revocation_str(int rev_type,char * rev_arg)2544 make_revocation_str(int rev_type, char *rev_arg)
2545 {
2546 char *other = NULL, *str;
2547 const char *reason = NULL;
2548 ASN1_OBJECT *otmp;
2549 ASN1_UTCTIME *revtm = NULL;
2550 int i;
2551 switch (rev_type) {
2552 case REV_NONE:
2553 break;
2554
2555 case REV_CRL_REASON:
2556 for (i = 0; i < 8; i++) {
2557 if (strcasecmp(rev_arg, crl_reasons[i]) == 0) {
2558 reason = crl_reasons[i];
2559 break;
2560 }
2561 }
2562 if (reason == NULL) {
2563 BIO_printf(bio_err, "Unknown CRL reason %s\n", rev_arg);
2564 return NULL;
2565 }
2566 break;
2567
2568 case REV_HOLD:
2569 /* Argument is an OID */
2570 otmp = OBJ_txt2obj(rev_arg, 0);
2571 ASN1_OBJECT_free(otmp);
2572
2573 if (otmp == NULL) {
2574 BIO_printf(bio_err,
2575 "Invalid object identifier %s\n", rev_arg);
2576 return NULL;
2577 }
2578 reason = "holdInstruction";
2579 other = rev_arg;
2580 break;
2581
2582 case REV_KEY_COMPROMISE:
2583 case REV_CA_COMPROMISE:
2584 /* Argument is the key compromise time */
2585 if (!ASN1_GENERALIZEDTIME_set_string(NULL, rev_arg)) {
2586 BIO_printf(bio_err,
2587 "Invalid time format %s. Need YYYYMMDDHHMMSSZ\n",
2588 rev_arg);
2589 return NULL;
2590 }
2591 other = rev_arg;
2592 if (rev_type == REV_KEY_COMPROMISE)
2593 reason = "keyTime";
2594 else
2595 reason = "CAkeyTime";
2596
2597 break;
2598 }
2599
2600 revtm = X509_gmtime_adj(NULL, 0);
2601 if (revtm == NULL)
2602 return NULL;
2603
2604 if (asprintf(&str, "%s%s%s%s%s", revtm->data,
2605 reason ? "," : "", reason ? reason : "",
2606 other ? "," : "", other ? other : "") == -1)
2607 str = NULL;
2608
2609 ASN1_UTCTIME_free(revtm);
2610
2611 return str;
2612 }
2613
2614 /* Convert revocation field to X509_REVOKED entry
2615 * return code:
2616 * 0 error
2617 * 1 OK
2618 * 2 OK and some extensions added (i.e. V2 CRL)
2619 */
2620
2621 int
make_revoked(X509_REVOKED * rev,const char * str)2622 make_revoked(X509_REVOKED *rev, const char *str)
2623 {
2624 char *tmp = NULL;
2625 int reason_code = -1;
2626 int i, ret = 0;
2627 ASN1_OBJECT *hold = NULL;
2628 ASN1_GENERALIZEDTIME *comp_time = NULL;
2629 ASN1_ENUMERATED *rtmp = NULL;
2630
2631 ASN1_TIME *revDate = NULL;
2632
2633 i = unpack_revinfo(&revDate, &reason_code, &hold, &comp_time, str);
2634
2635 if (i == 0)
2636 goto err;
2637
2638 if (rev != NULL && !X509_REVOKED_set_revocationDate(rev, revDate))
2639 goto err;
2640
2641 if (rev != NULL && (reason_code != OCSP_REVOKED_STATUS_NOSTATUS)) {
2642 rtmp = ASN1_ENUMERATED_new();
2643 if (rtmp == NULL || !ASN1_ENUMERATED_set(rtmp, reason_code))
2644 goto err;
2645 if (!X509_REVOKED_add1_ext_i2d(rev, NID_crl_reason, rtmp, 0, 0))
2646 goto err;
2647 }
2648 if (rev != NULL && comp_time != NULL) {
2649 if (!X509_REVOKED_add1_ext_i2d(rev, NID_invalidity_date,
2650 comp_time, 0, 0))
2651 goto err;
2652 }
2653 if (rev != NULL && hold != NULL) {
2654 if (!X509_REVOKED_add1_ext_i2d(rev, NID_hold_instruction_code,
2655 hold, 0, 0))
2656 goto err;
2657 }
2658 if (reason_code != OCSP_REVOKED_STATUS_NOSTATUS)
2659 ret = 2;
2660 else
2661 ret = 1;
2662
2663 err:
2664 free(tmp);
2665
2666 ASN1_OBJECT_free(hold);
2667 ASN1_GENERALIZEDTIME_free(comp_time);
2668 ASN1_ENUMERATED_free(rtmp);
2669 ASN1_TIME_free(revDate);
2670
2671 return ret;
2672 }
2673
2674 int
old_entry_print(BIO * bp,ASN1_OBJECT * obj,ASN1_STRING * str)2675 old_entry_print(BIO *bp, ASN1_OBJECT *obj, ASN1_STRING *str)
2676 {
2677 char buf[25], *pbuf, *p;
2678 int j;
2679
2680 j = i2a_ASN1_OBJECT(bp, obj);
2681 pbuf = buf;
2682 for (j = 22 - j; j > 0; j--)
2683 *(pbuf++) = ' ';
2684 *(pbuf++) = ':';
2685 *(pbuf++) = '\0';
2686 BIO_puts(bp, buf);
2687
2688 if (str->type == V_ASN1_PRINTABLESTRING)
2689 BIO_printf(bp, "PRINTABLE:'");
2690 else if (str->type == V_ASN1_T61STRING)
2691 BIO_printf(bp, "T61STRING:'");
2692 else if (str->type == V_ASN1_IA5STRING)
2693 BIO_printf(bp, "IA5STRING:'");
2694 else if (str->type == V_ASN1_UNIVERSALSTRING)
2695 BIO_printf(bp, "UNIVERSALSTRING:'");
2696 else
2697 BIO_printf(bp, "ASN.1 %2d:'", str->type);
2698
2699 p = (char *) str->data;
2700 for (j = str->length; j > 0; j--) {
2701 if ((*p >= ' ') && (*p <= '~'))
2702 BIO_printf(bp, "%c", *p);
2703 else if (*p & 0x80)
2704 BIO_printf(bp, "\\0x%02X", *p);
2705 else if ((unsigned char) *p == 0xf7)
2706 BIO_printf(bp, "^?");
2707 else
2708 BIO_printf(bp, "^%c", *p + '@');
2709 p++;
2710 }
2711 BIO_printf(bp, "'\n");
2712 return 1;
2713 }
2714
2715 int
unpack_revinfo(ASN1_TIME ** prevtm,int * preason,ASN1_OBJECT ** phold,ASN1_GENERALIZEDTIME ** pinvtm,const char * str)2716 unpack_revinfo(ASN1_TIME **prevtm, int *preason, ASN1_OBJECT **phold,
2717 ASN1_GENERALIZEDTIME **pinvtm, const char *str)
2718 {
2719 char *tmp = NULL;
2720 char *rtime_str, *reason_str = NULL, *arg_str = NULL, *p;
2721 int reason_code = -1;
2722 int ret = 0;
2723 unsigned int i;
2724 ASN1_OBJECT *hold = NULL;
2725 ASN1_GENERALIZEDTIME *comp_time = NULL;
2726
2727 if ((tmp = strdup(str)) == NULL) {
2728 BIO_printf(bio_err, "malloc failed\n");
2729 goto err;
2730 }
2731 p = strchr(tmp, ',');
2732 rtime_str = tmp;
2733
2734 if (p != NULL) {
2735 *p = '\0';
2736 p++;
2737 reason_str = p;
2738 p = strchr(p, ',');
2739 if (p != NULL) {
2740 *p = '\0';
2741 arg_str = p + 1;
2742 }
2743 }
2744 if (prevtm != NULL) {
2745 *prevtm = ASN1_UTCTIME_new();
2746 if (!ASN1_UTCTIME_set_string(*prevtm, rtime_str)) {
2747 BIO_printf(bio_err, "invalid revocation date %s\n",
2748 rtime_str);
2749 goto err;
2750 }
2751 }
2752 if (reason_str != NULL) {
2753 for (i = 0; i < NUM_REASONS; i++) {
2754 if (strcasecmp(reason_str, crl_reasons[i]) == 0) {
2755 reason_code = i;
2756 break;
2757 }
2758 }
2759 if (reason_code == OCSP_REVOKED_STATUS_NOSTATUS) {
2760 BIO_printf(bio_err, "invalid reason code %s\n",
2761 reason_str);
2762 goto err;
2763 }
2764 if (reason_code == 7)
2765 reason_code = OCSP_REVOKED_STATUS_REMOVEFROMCRL;
2766 else if (reason_code == 8) { /* Hold instruction */
2767 if (arg_str == NULL) {
2768 BIO_printf(bio_err,
2769 "missing hold instruction\n");
2770 goto err;
2771 }
2772 reason_code = OCSP_REVOKED_STATUS_CERTIFICATEHOLD;
2773 hold = OBJ_txt2obj(arg_str, 0);
2774
2775 if (hold == NULL) {
2776 BIO_printf(bio_err,
2777 "invalid object identifier %s\n", arg_str);
2778 goto err;
2779 }
2780 if (phold != NULL)
2781 *phold = hold;
2782 } else if ((reason_code == 9) || (reason_code == 10)) {
2783 if (arg_str == NULL) {
2784 BIO_printf(bio_err,
2785 "missing compromised time\n");
2786 goto err;
2787 }
2788 comp_time = ASN1_GENERALIZEDTIME_new();
2789 if (!ASN1_GENERALIZEDTIME_set_string(comp_time,
2790 arg_str)) {
2791 BIO_printf(bio_err,
2792 "invalid compromised time %s\n", arg_str);
2793 goto err;
2794 }
2795 if (reason_code == 9)
2796 reason_code = OCSP_REVOKED_STATUS_KEYCOMPROMISE;
2797 else
2798 reason_code = OCSP_REVOKED_STATUS_CACOMPROMISE;
2799 }
2800 }
2801 if (preason != NULL)
2802 *preason = reason_code;
2803 if (pinvtm != NULL)
2804 *pinvtm = comp_time;
2805 else
2806 ASN1_GENERALIZEDTIME_free(comp_time);
2807
2808 ret = 1;
2809
2810 err:
2811 free(tmp);
2812
2813 if (phold == NULL)
2814 ASN1_OBJECT_free(hold);
2815 if (pinvtm == NULL)
2816 ASN1_GENERALIZEDTIME_free(comp_time);
2817
2818 return ret;
2819 }
2820
2821 static char *
bin2hex(unsigned char * data,size_t len)2822 bin2hex(unsigned char *data, size_t len)
2823 {
2824 char *ret = NULL;
2825 char hex[] = "0123456789ABCDEF";
2826 int i;
2827
2828 if ((ret = malloc(len * 2 + 1)) != NULL) {
2829 for (i = 0; i < len; i++) {
2830 ret[i * 2 + 0] = hex[data[i] >> 4];
2831 ret[i * 2 + 1] = hex[data[i] & 0x0F];
2832 }
2833 ret[len * 2] = '\0';
2834 }
2835 return ret;
2836 }
2837