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