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