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