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