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