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