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