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