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