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