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