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