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