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