2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
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
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>
27 # ifdef OPENSSL_SYS_VMS
29 # elif !defined(OPENSSL_SYS_VXWORKS) && !defined(OPENSSL_SYS_WINDOWS)
30 # include <sys/file.h>
44 # define PATH_MAX 4096
47 #define BASE_SECTION "ca"
49 #define ENV_DEFAULT_CA "default_ca"
51 #define STRING_MASK "string_mask"
52 #define UTF8_IN "utf8"
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"
77 #define ENV_DATABASE "database"
79 /* Additional revocation information types */
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 */
89 static char *lookup_conf(const CONF *conf, const char *group, const char *tag);
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,
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,
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);
136 static CONF *extconf = NULL;
137 static int preserve = 0;
138 static int msie_hack = 0;
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,
151 OPT_R_ENUM, OPT_SM2ID, OPT_SM2HEXID,
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
156 const OPTIONS ca_options[] = {
157 {OPT_HELP_STR, 1, '-', "Usage: %s [options] [certreq...]\n"},
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"},
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"},
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"},
205 {"preserveDN", OPT_PRESERVEDN, '-', "Don't re-order the DN"},
206 {"noemailDN", OPT_NOEMAILDN, '-', "Don't add the EMAIL field to the DN"},
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"},
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)"},
242 {"certreq", 0, 0, "Certificate requests to be signed (optional)"},
246 int ca_main(int argc, char **argv)
250 BIGNUM *crlnumber = NULL, *serial = NULL;
251 EVP_PKEY *pkey = NULL;
252 BIO *in = NULL, *out = NULL, *Sout = NULL;
253 ASN1_INTEGER *tmpser;
257 STACK_OF(CONF_VALUE) *attribs = NULL;
258 STACK_OF(OPENSSL_STRING) *sigopts = NULL;
259 STACK_OF(X509) *cert_sk = NULL;
260 X509_CRL *crl = NULL;
261 const EVP_MD *dgst = NULL;
262 char *configfile = default_config_file, *section = NULL;
263 char *md = NULL, *policy = NULL, *keyfile = NULL;
264 char *certfile = NULL, *crl_ext = NULL, *crlnumberfile = NULL, *key = NULL;
265 const char *infile = NULL, *spkac_file = NULL, *ss_cert_file = NULL;
266 const char *extensions = NULL, *extfile = NULL, *passinarg = NULL;
267 char *outdir = NULL, *outfile = NULL, *rev_arg = NULL, *ser_status = NULL;
268 const char *serialfile = NULL, *subj = NULL;
269 char *prog, *startdate = NULL, *enddate = NULL;
270 char *dbfile = NULL, *f;
271 char new_cert[PATH_MAX];
272 char tmp[10 + 1] = "\0";
275 size_t outdirlen = 0;
276 int create_ser = 0, free_key = 0, total = 0, total_done = 0;
277 int batch = 0, default_op = 1, doupdatedb = 0, ext_copy = EXT_COPY_NONE;
278 int keyformat = FORMAT_PEM, multirdn = 0, notext = 0, output_der = 0;
279 int ret = 1, email_dn = 1, req = 0, verbose = 0, gencrl = 0, dorevoke = 0;
280 int rand_ser = 0, i, j, selfsign = 0, def_nid, def_ret;
281 long crldays = 0, crlhours = 0, crlsec = 0, days = 0;
282 unsigned long chtype = MBSTRING_ASC, certopt = 0;
283 X509 *x509 = NULL, *x509p = NULL, *x = NULL;
284 REVINFO_TYPE rev_type = REV_NONE;
285 X509_REVOKED *r = NULL;
287 unsigned char *sm2_id = NULL;
288 size_t sm2_idlen = 0;
291 prog = opt_init(argc, argv, ca_options);
292 while ((o = opt_next()) != OPT_EOF) {
297 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
300 opt_help(ca_options);
314 configfile = opt_arg();
324 chtype = MBSTRING_UTF8;
326 case OPT_RAND_SERIAL:
329 case OPT_CREATE_SERIAL:
332 case OPT_MULTIVALUE_RDN:
336 startdate = opt_arg();
342 days = atoi(opt_arg());
354 if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyformat))
358 passinarg = opt_arg();
368 certfile = opt_arg();
378 sigopts = sk_OPENSSL_STRING_new_null();
379 if (sigopts == NULL || !sk_OPENSSL_STRING_push(sigopts, opt_arg()))
401 crldays = atol(opt_arg());
404 crlhours = atol(opt_arg());
407 crlsec = atol(opt_arg());
413 ss_cert_file = opt_arg();
417 spkac_file = opt_arg();
429 extensions = opt_arg();
435 ser_status = opt_arg();
443 case OPT_CRL_REASON: /* := REV_CRL_REASON */
445 case OPT_CRL_COMPROMISE:
446 case OPT_CRL_CA_COMPROMISE:
448 rev_type = (o - OPT_CRL_REASON) + REV_CRL_REASON;
451 e = setup_engine(opt_arg(), 0);
454 /* we assume the input is not a hex string */
455 if (sm2_id != NULL) {
457 "Use one of the options 'sm2-hex-id' or 'sm2-id'\n");
460 sm2_id = (unsigned char *)opt_arg();
461 sm2_idlen = strlen((const char *)sm2_id);
464 /* try to parse the input as hex string first */
465 if (sm2_id != NULL) {
467 "Use one of the options 'sm2-hex-id' or 'sm2-id'\n");
471 sm2_id = OPENSSL_hexstr2buf(opt_arg(), (long *)&sm2_idlen);
472 if (sm2_id == NULL) {
473 BIO_printf(bio_err, "Invalid hex string input\n");
480 argc = opt_num_rest();
483 BIO_printf(bio_err, "Using configuration from %s\n", configfile);
485 if ((conf = app_load_config(configfile)) == NULL)
487 if (configfile != default_config_file && !app_load_modules(conf))
490 /* Lets get the config section we are using */
492 && (section = lookup_conf(conf, BASE_SECTION, ENV_DEFAULT_CA)) == NULL)
495 p = NCONF_get_string(conf, NULL, "oid_file");
499 BIO *oid_bio = BIO_new_file(p, "r");
501 if (oid_bio == NULL) {
504 OBJ_create_objects(oid_bio);
508 if (!add_oid_section(conf)) {
509 ERR_print_errors(bio_err);
513 app_RAND_load_conf(conf, BASE_SECTION);
515 f = NCONF_get_string(conf, section, STRING_MASK);
519 if (f != NULL && !ASN1_STRING_set_default_mask_asc(f)) {
520 BIO_printf(bio_err, "Invalid global string mask setting %s\n", f);
524 if (chtype != MBSTRING_UTF8) {
525 f = NCONF_get_string(conf, section, UTF8_IN);
528 else if (strcmp(f, "yes") == 0)
529 chtype = MBSTRING_UTF8;
532 db_attr.unique_subject = 1;
533 p = NCONF_get_string(conf, section, ENV_UNIQUE_SUBJECT);
535 db_attr.unique_subject = parse_yesno(p, 1);
539 /*****************************************************************/
540 /* report status of cert with serial number given on command line */
542 dbfile = lookup_conf(conf, section, ENV_DATABASE);
546 db = load_index(dbfile, &db_attr);
550 if (index_index(db) <= 0)
553 if (get_certificate_status(ser_status, db) != 1)
554 BIO_printf(bio_err, "Error verifying serial %s!\n", ser_status);
558 /*****************************************************************/
559 /* we definitely need a private key, so let's get it */
562 && (keyfile = lookup_conf(conf, section, ENV_PRIVATE_KEY)) == NULL)
567 if (!app_passwd(passinarg, NULL, &key, NULL)) {
568 BIO_printf(bio_err, "Error getting password\n");
572 pkey = load_key(keyfile, keyformat, 0, key, e, "CA private key");
574 OPENSSL_cleanse(key, strlen(key));
576 /* load_key() has already printed an appropriate message */
579 /*****************************************************************/
580 /* we need a certificate */
581 if (!selfsign || spkac_file || ss_cert_file || gencrl) {
583 && (certfile = lookup_conf(conf, section, ENV_CERTIFICATE)) == NULL)
586 x509 = load_cert(certfile, FORMAT_PEM, "CA certificate");
590 if (!X509_check_private_key(x509, pkey)) {
592 "CA certificate and CA private key do not match\n");
599 f = NCONF_get_string(conf, BASE_SECTION, ENV_PRESERVE);
602 if ((f != NULL) && ((*f == 'y') || (*f == 'Y')))
604 f = NCONF_get_string(conf, BASE_SECTION, ENV_MSIE_HACK);
607 if ((f != NULL) && ((*f == 'y') || (*f == 'Y')))
610 f = NCONF_get_string(conf, section, ENV_NAMEOPT);
613 if (!set_nameopt(f)) {
614 BIO_printf(bio_err, "Invalid name options: \"%s\"\n", f);
620 f = NCONF_get_string(conf, section, ENV_CERTOPT);
623 if (!set_cert_ex(&certopt, f)) {
624 BIO_printf(bio_err, "Invalid certificate options: \"%s\"\n", f);
632 f = NCONF_get_string(conf, section, ENV_EXTCOPY);
635 if (!set_ext_copy(&ext_copy, f)) {
636 BIO_printf(bio_err, "Invalid extension copy option: \"%s\"\n", f);
643 /*****************************************************************/
644 /* lookup where to write new certificates */
645 if ((outdir == NULL) && (req)) {
647 outdir = NCONF_get_string(conf, section, ENV_NEW_CERTS_DIR);
648 if (outdir == NULL) {
650 "there needs to be defined a directory for new certificate to be placed in\n");
653 #ifndef OPENSSL_SYS_VMS
655 * outdir is a directory spec, but access() for VMS demands a
656 * filename. We could use the DEC C routine to convert the
657 * directory syntax to Unix, and give that to app_isdir,
658 * but for now the fopen will catch the error if it's not a
661 if (app_isdir(outdir) <= 0) {
662 BIO_printf(bio_err, "%s: %s is not a directory\n", prog, outdir);
669 /*****************************************************************/
670 /* we need to load the database file */
671 dbfile = lookup_conf(conf, section, ENV_DATABASE);
675 db = load_index(dbfile, &db_attr);
679 /* Lets check some fields */
680 for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
681 pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
682 if ((pp[DB_type][0] != DB_TYPE_REV) && (pp[DB_rev_date][0] != '\0')) {
684 "entry %d: not revoked yet, but has a revocation date\n",
688 if ((pp[DB_type][0] == DB_TYPE_REV) &&
689 !make_revoked(NULL, pp[DB_rev_date])) {
690 BIO_printf(bio_err, " in entry %d\n", i + 1);
693 if (!check_time_format((char *)pp[DB_exp_date])) {
694 BIO_printf(bio_err, "entry %d: invalid expiry date\n", i + 1);
703 if ((j & 1) || (j < 2)) {
704 BIO_printf(bio_err, "entry %d: bad serial number length (%d)\n",
709 if (!isxdigit(_UC(*p))) {
711 "entry %d: bad char 0%o '%c' in serial number\n",
718 TXT_DB_write(bio_out, db->db);
719 BIO_printf(bio_err, "%d entries loaded from the database\n",
720 sk_OPENSSL_PSTRING_num(db->db->data));
721 BIO_printf(bio_err, "generating index\n");
724 if (index_index(db) <= 0)
727 /*****************************************************************/
728 /* Update the db file for expired certificates */
731 BIO_printf(bio_err, "Updating %s ...\n", dbfile);
735 BIO_printf(bio_err, "Malloc failure\n");
739 BIO_printf(bio_err, "No entries found to mark expired\n");
741 if (!save_index(dbfile, "new", db))
744 if (!rotate_index(dbfile, "new", "old"))
748 BIO_printf(bio_err, "Done. %d entries marked as expired\n", i);
752 /*****************************************************************/
753 /* Read extensions config file */
755 if ((extconf = app_load_config(extfile)) == NULL) {
761 BIO_printf(bio_err, "Successfully loaded extensions file %s\n",
764 /* We can have sections in the ext file */
765 if (extensions == NULL) {
766 extensions = NCONF_get_string(extconf, "default", "extensions");
767 if (extensions == NULL)
768 extensions = "default";
772 /*****************************************************************/
774 if (spkac_file != NULL && outfile != NULL) {
780 def_ret = EVP_PKEY_get_default_digest_nid(pkey, &def_nid);
782 * EVP_PKEY_get_default_digest_nid() returns 2 if the digest is
783 * mandatory for this algorithm.
785 if (def_ret == 2 && def_nid == NID_undef) {
786 /* The signing algorithm requires there to be no digest */
787 dgst = EVP_md_null();
788 } else if (md == NULL
789 && (md = lookup_conf(conf, section, ENV_DEFAULT_MD)) == NULL) {
792 if (strcmp(md, "default") == 0) {
794 BIO_puts(bio_err, "no default digest\n");
797 md = (char *)OBJ_nid2sn(def_nid);
800 if (!opt_md(md, &dgst))
806 char *tmp_email_dn = NULL;
808 tmp_email_dn = NCONF_get_string(conf, section, ENV_DEFAULT_EMAIL_DN);
809 if (tmp_email_dn != NULL && strcmp(tmp_email_dn, "no") == 0)
813 BIO_printf(bio_err, "message digest is %s\n",
814 OBJ_nid2ln(EVP_MD_type(dgst)));
816 && (policy = lookup_conf(conf, section, ENV_POLICY)) == NULL)
820 BIO_printf(bio_err, "policy is %s\n", policy);
822 if (NCONF_get_string(conf, section, ENV_RAND_SERIAL) != NULL) {
825 serialfile = lookup_conf(conf, section, ENV_SERIAL);
826 if (serialfile == NULL)
830 if (extconf == NULL) {
832 * no '-extfile' option, so we look for extensions in the main
835 if (extensions == NULL) {
836 extensions = NCONF_get_string(conf, section, ENV_EXTENSIONS);
837 if (extensions == NULL)
840 if (extensions != NULL) {
841 /* Check syntax of file */
843 X509V3_set_ctx_test(&ctx);
844 X509V3_set_nconf(&ctx, conf);
845 if (!X509V3_EXT_add_nconf(conf, &ctx, extensions, NULL)) {
847 "Error Loading extension section %s\n",
855 if (startdate == NULL) {
856 startdate = NCONF_get_string(conf, section, ENV_DEFAULT_STARTDATE);
857 if (startdate == NULL)
860 if (startdate != NULL && !ASN1_TIME_set_string_X509(NULL, startdate)) {
862 "start date is invalid, it should be YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ\n");
865 if (startdate == NULL)
868 if (enddate == NULL) {
869 enddate = NCONF_get_string(conf, section, ENV_DEFAULT_ENDDATE);
873 if (enddate != NULL && !ASN1_TIME_set_string_X509(NULL, enddate)) {
875 "end date is invalid, it should be YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ\n");
880 if (!NCONF_get_number(conf, section, ENV_DEFAULT_DAYS, &days))
883 if (enddate == NULL && days == 0) {
884 BIO_printf(bio_err, "cannot lookup how many days to certify for\n");
889 if ((serial = BN_new()) == NULL || !rand_serial(serial, NULL)) {
890 BIO_printf(bio_err, "error generating serial number\n");
894 if ((serial = load_serial(serialfile, create_ser, NULL)) == NULL) {
895 BIO_printf(bio_err, "error while loading serial number\n");
899 if (BN_is_zero(serial)) {
900 BIO_printf(bio_err, "next serial number is 00\n");
902 if ((f = BN_bn2hex(serial)) == NULL)
904 BIO_printf(bio_err, "next serial number is %s\n", f);
910 if ((attribs = NCONF_get_section(conf, policy)) == NULL) {
911 BIO_printf(bio_err, "unable to find 'section' for %s\n", policy);
915 if ((cert_sk = sk_X509_new_null()) == NULL) {
916 BIO_printf(bio_err, "Memory allocation failure\n");
919 if (spkac_file != NULL) {
921 j = certify_spkac(&x, spkac_file, pkey, x509, dgst, sigopts,
922 attribs, db, serial, subj, chtype, multirdn,
923 email_dn, startdate, enddate, days, extensions,
924 conf, verbose, certopt, get_nameopt(), default_op,
930 BIO_printf(bio_err, "\n");
931 if (!BN_add_word(serial, 1))
933 if (!sk_X509_push(cert_sk, x)) {
934 BIO_printf(bio_err, "Memory allocation failure\n");
939 if (ss_cert_file != NULL) {
941 j = certify_cert(&x, ss_cert_file, pkey, x509, dgst, sigopts,
943 db, serial, subj, chtype, multirdn, email_dn,
944 startdate, enddate, days, batch, extensions,
945 conf, verbose, certopt, get_nameopt(), default_op,
951 BIO_printf(bio_err, "\n");
952 if (!BN_add_word(serial, 1))
954 if (!sk_X509_push(cert_sk, x)) {
955 BIO_printf(bio_err, "Memory allocation failure\n");
960 if (infile != NULL) {
962 j = certify(&x, infile, pkey, x509p, dgst, sigopts, attribs, db,
963 serial, subj, chtype, multirdn, email_dn, startdate,
964 enddate, days, batch, extensions, conf, verbose,
965 certopt, get_nameopt(), default_op, ext_copy, selfsign,
971 BIO_printf(bio_err, "\n");
972 if (!BN_add_word(serial, 1))
974 if (!sk_X509_push(cert_sk, x)) {
975 BIO_printf(bio_err, "Memory allocation failure\n");
980 for (i = 0; i < argc; i++) {
982 j = certify(&x, argv[i], pkey, x509p, dgst, sigopts, attribs, db,
983 serial, subj, chtype, multirdn, email_dn, startdate,
984 enddate, days, batch, extensions, conf, verbose,
985 certopt, get_nameopt(), default_op, ext_copy, selfsign,
991 BIO_printf(bio_err, "\n");
992 if (!BN_add_word(serial, 1)) {
996 if (!sk_X509_push(cert_sk, x)) {
997 BIO_printf(bio_err, "Memory allocation failure\n");
1004 * we have a stack of newly certified certificates and a data base
1005 * and serial number that need updating
1008 if (sk_X509_num(cert_sk) > 0) {
1011 "\n%d out of %d certificate requests certified, commit? [y/n]",
1013 (void)BIO_flush(bio_err);
1015 if (fgets(tmp, sizeof(tmp), stdin) == NULL) {
1016 BIO_printf(bio_err, "CERTIFICATION CANCELED: I/O error\n");
1020 if (tmp[0] != 'y' && tmp[0] != 'Y') {
1021 BIO_printf(bio_err, "CERTIFICATION CANCELED\n");
1027 BIO_printf(bio_err, "Write out database with %d new entries\n",
1028 sk_X509_num(cert_sk));
1030 if (serialfile != NULL
1031 && !save_serial(serialfile, "new", serial, NULL))
1034 if (!save_index(dbfile, "new", db))
1038 outdirlen = OPENSSL_strlcpy(new_cert, outdir, sizeof(new_cert));
1039 #ifndef OPENSSL_SYS_VMS
1040 outdirlen = OPENSSL_strlcat(new_cert, "/", sizeof(new_cert));
1044 BIO_printf(bio_err, "writing new certificates\n");
1046 for (i = 0; i < sk_X509_num(cert_sk); i++) {
1048 X509 *xi = sk_X509_value(cert_sk, i);
1049 ASN1_INTEGER *serialNumber = X509_get_serialNumber(xi);
1050 const unsigned char *psn = ASN1_STRING_get0_data(serialNumber);
1051 const int snl = ASN1_STRING_length(serialNumber);
1052 const int filen_len = 2 * (snl > 0 ? snl : 1) + sizeof(".pem");
1053 char *n = new_cert + outdirlen;
1055 if (outdirlen + filen_len > PATH_MAX) {
1056 BIO_printf(bio_err, "certificate file name too long\n");
1061 static const char HEX_DIGITS[] = "0123456789ABCDEF";
1063 for (j = 0; j < snl; j++, psn++) {
1064 *n++ = HEX_DIGITS[*psn >> 4];
1065 *n++ = HEX_DIGITS[*psn & 0x0F];
1075 *n = '\0'; /* closing new_cert */
1077 BIO_printf(bio_err, "writing %s\n", new_cert);
1079 Sout = bio_open_default(outfile, 'w',
1080 output_der ? FORMAT_ASN1 : FORMAT_TEXT);
1084 Cout = BIO_new_file(new_cert, "w");
1089 write_new_certificate(Cout, xi, 0, notext);
1090 write_new_certificate(Sout, xi, output_der, notext);
1096 if (sk_X509_num(cert_sk)) {
1097 /* Rename the database and the serial file */
1098 if (serialfile != NULL
1099 && !rotate_serial(serialfile, "new", "old"))
1102 if (!rotate_index(dbfile, "new", "old"))
1105 BIO_printf(bio_err, "Data Base Updated\n");
1109 /*****************************************************************/
1112 if (crl_ext == NULL) {
1113 crl_ext = NCONF_get_string(conf, section, ENV_CRLEXT);
1114 if (crl_ext == NULL)
1117 if (crl_ext != NULL) {
1118 /* Check syntax of file */
1120 X509V3_set_ctx_test(&ctx);
1121 X509V3_set_nconf(&ctx, conf);
1122 if (!X509V3_EXT_add_nconf(conf, &ctx, crl_ext, NULL)) {
1124 "Error Loading CRL extension section %s\n", crl_ext);
1130 if ((crlnumberfile = NCONF_get_string(conf, section, ENV_CRLNUMBER))
1132 if ((crlnumber = load_serial(crlnumberfile, 0, NULL)) == NULL) {
1133 BIO_printf(bio_err, "error while loading CRL number\n");
1137 if (!crldays && !crlhours && !crlsec) {
1138 if (!NCONF_get_number(conf, section,
1139 ENV_DEFAULT_CRL_DAYS, &crldays))
1141 if (!NCONF_get_number(conf, section,
1142 ENV_DEFAULT_CRL_HOURS, &crlhours))
1146 if ((crldays == 0) && (crlhours == 0) && (crlsec == 0)) {
1148 "cannot lookup how long until the next CRL is issued\n");
1153 BIO_printf(bio_err, "making CRL\n");
1154 if ((crl = X509_CRL_new()) == NULL)
1156 if (!X509_CRL_set_issuer_name(crl, X509_get_subject_name(x509)))
1159 tmptm = ASN1_TIME_new();
1161 || X509_gmtime_adj(tmptm, 0) == NULL
1162 || !X509_CRL_set1_lastUpdate(crl, tmptm)
1163 || X509_time_adj_ex(tmptm, crldays, crlhours * 60 * 60 + crlsec,
1165 BIO_puts(bio_err, "error setting CRL nextUpdate\n");
1166 ASN1_TIME_free(tmptm);
1169 X509_CRL_set1_nextUpdate(crl, tmptm);
1171 ASN1_TIME_free(tmptm);
1173 for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
1174 pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
1175 if (pp[DB_type][0] == DB_TYPE_REV) {
1176 if ((r = X509_REVOKED_new()) == NULL)
1178 j = make_revoked(r, pp[DB_rev_date]);
1183 if (!BN_hex2bn(&serial, pp[DB_serial]))
1185 tmpser = BN_to_ASN1_INTEGER(serial, NULL);
1190 X509_REVOKED_set_serialNumber(r, tmpser);
1191 ASN1_INTEGER_free(tmpser);
1192 X509_CRL_add0_revoked(crl, r);
1197 * sort the data so it will be written in serial number order
1201 /* we now have a CRL */
1203 BIO_printf(bio_err, "signing CRL\n");
1205 /* Add any extensions asked for */
1207 if (crl_ext != NULL || crlnumberfile != NULL) {
1209 X509V3_set_ctx(&crlctx, x509, NULL, NULL, crl, 0);
1210 X509V3_set_nconf(&crlctx, conf);
1212 if (crl_ext != NULL)
1213 if (!X509V3_EXT_CRL_add_nconf(conf, &crlctx, crl_ext, crl))
1215 if (crlnumberfile != NULL) {
1216 tmpser = BN_to_ASN1_INTEGER(crlnumber, NULL);
1219 X509_CRL_add1_ext_i2d(crl, NID_crl_number, tmpser, 0, 0);
1220 ASN1_INTEGER_free(tmpser);
1222 if (!BN_add_word(crlnumber, 1))
1226 if (crl_ext != NULL || crl_v2) {
1227 if (!X509_CRL_set_version(crl, 1))
1228 goto end; /* version 2 CRL */
1231 /* we have a CRL number that need updating */
1232 if (crlnumberfile != NULL
1233 && !save_serial(crlnumberfile, "new", crlnumber, NULL))
1239 if (!do_X509_CRL_sign(crl, pkey, dgst, sigopts))
1242 Sout = bio_open_default(outfile, 'w',
1243 output_der ? FORMAT_ASN1 : FORMAT_TEXT);
1247 PEM_write_bio_X509_CRL(Sout, crl);
1249 /* Rename the crlnumber file */
1250 if (crlnumberfile != NULL
1251 && !rotate_serial(crlnumberfile, "new", "old"))
1255 /*****************************************************************/
1257 if (infile == NULL) {
1258 BIO_printf(bio_err, "no input files\n");
1262 revcert = load_cert(infile, FORMAT_PEM, infile);
1263 if (revcert == NULL)
1266 rev_type = REV_VALID;
1267 j = do_revoke(revcert, db, rev_type, rev_arg);
1272 if (!save_index(dbfile, "new", db))
1275 if (!rotate_index(dbfile, "new", "old"))
1278 BIO_printf(bio_err, "Data Base Updated\n");
1285 OPENSSL_free(sm2_id);
1287 ERR_print_errors(bio_err);
1291 sk_X509_pop_free(cert_sk, X509_free);
1298 sk_OPENSSL_STRING_free(sigopts);
1299 EVP_PKEY_free(pkey);
1303 NCONF_free(extconf);
1308 static char *lookup_conf(const CONF *conf, const char *section, const char *tag)
1310 char *entry = NCONF_get_string(conf, section, tag);
1312 BIO_printf(bio_err, "variable lookup failed for %s::%s\n", section, tag);
1316 static int certify(X509 **xret, const char *infile, EVP_PKEY *pkey, X509 *x509,
1317 const EVP_MD *dgst, STACK_OF(OPENSSL_STRING) *sigopts,
1318 STACK_OF(CONF_VALUE) *policy, CA_DB *db,
1319 BIGNUM *serial, const char *subj, unsigned long chtype,
1320 int multirdn, int email_dn, const char *startdate,
1321 const char *enddate,
1322 long days, int batch, const char *ext_sect, CONF *lconf,
1323 int verbose, unsigned long certopt, unsigned long nameopt,
1324 int default_op, int ext_copy, int selfsign,
1325 unsigned char *sm2id, size_t sm2idlen)
1327 X509_REQ *req = NULL;
1329 EVP_PKEY *pktmp = NULL;
1332 in = BIO_new_file(infile, "r");
1334 ERR_print_errors(bio_err);
1337 if ((req = PEM_read_bio_X509_REQ(in, NULL, NULL, NULL)) == NULL) {
1338 BIO_printf(bio_err, "Error reading certificate request in %s\n",
1343 X509_REQ_print_ex(bio_err, req, nameopt, X509_FLAG_COMPAT);
1345 BIO_printf(bio_err, "Check that the request matches the signature\n");
1347 if (selfsign && !X509_REQ_check_private_key(req, pkey)) {
1349 "Certificate request and CA private key do not match\n");
1353 if ((pktmp = X509_REQ_get0_pubkey(req)) == NULL) {
1354 BIO_printf(bio_err, "error unpacking public key\n");
1357 if (sm2id != NULL) {
1358 #ifndef OPENSSL_NO_SM2
1359 ASN1_OCTET_STRING *v;
1361 v = ASN1_OCTET_STRING_new();
1363 BIO_printf(bio_err, "error: SM2 ID allocation failed\n");
1367 if (!ASN1_OCTET_STRING_set(v, sm2id, sm2idlen)) {
1368 BIO_printf(bio_err, "error: setting SM2 ID failed\n");
1369 ASN1_OCTET_STRING_free(v);
1373 X509_REQ_set0_sm2_id(req, v);
1376 i = X509_REQ_verify(req, pktmp);
1380 BIO_printf(bio_err, "Signature verification problems....\n");
1381 ERR_print_errors(bio_err);
1387 "Signature did not match the certificate request\n");
1388 ERR_print_errors(bio_err);
1391 BIO_printf(bio_err, "Signature ok\n");
1394 ok = do_body(xret, pkey, x509, dgst, sigopts, policy, db, serial, subj,
1395 chtype, multirdn, email_dn, startdate, enddate, days, batch,
1396 verbose, req, ext_sect, lconf, certopt, nameopt, default_op,
1397 ext_copy, selfsign);
1405 static int certify_cert(X509 **xret, const char *infile, EVP_PKEY *pkey, X509 *x509,
1406 const EVP_MD *dgst, STACK_OF(OPENSSL_STRING) *sigopts,
1407 STACK_OF(CONF_VALUE) *policy, CA_DB *db,
1408 BIGNUM *serial, const char *subj, unsigned long chtype,
1409 int multirdn, int email_dn, const char *startdate,
1410 const char *enddate, long days, int batch, const char *ext_sect,
1411 CONF *lconf, int verbose, unsigned long certopt,
1412 unsigned long nameopt, int default_op, int ext_copy)
1415 X509_REQ *rreq = NULL;
1416 EVP_PKEY *pktmp = NULL;
1419 if ((req = load_cert(infile, FORMAT_PEM, infile)) == NULL)
1422 X509_print(bio_err, req);
1424 BIO_printf(bio_err, "Check that the request matches the signature\n");
1426 if ((pktmp = X509_get0_pubkey(req)) == NULL) {
1427 BIO_printf(bio_err, "error unpacking public key\n");
1430 i = X509_verify(req, pktmp);
1433 BIO_printf(bio_err, "Signature verification problems....\n");
1438 BIO_printf(bio_err, "Signature did not match the certificate\n");
1441 BIO_printf(bio_err, "Signature ok\n");
1444 if ((rreq = X509_to_X509_REQ(req, NULL, NULL)) == NULL)
1447 ok = do_body(xret, pkey, x509, dgst, sigopts, policy, db, serial, subj,
1448 chtype, multirdn, email_dn, startdate, enddate, days, batch,
1449 verbose, rreq, ext_sect, lconf, certopt, nameopt, default_op,
1453 X509_REQ_free(rreq);
1458 static int do_body(X509 **xret, EVP_PKEY *pkey, X509 *x509,
1459 const EVP_MD *dgst, STACK_OF(OPENSSL_STRING) *sigopts,
1460 STACK_OF(CONF_VALUE) *policy, CA_DB *db, BIGNUM *serial,
1461 const char *subj, unsigned long chtype, int multirdn,
1462 int email_dn, const char *startdate, const char *enddate, long days,
1463 int batch, int verbose, X509_REQ *req, const char *ext_sect,
1464 CONF *lconf, unsigned long certopt, unsigned long nameopt,
1465 int default_op, int ext_copy, int selfsign)
1467 X509_NAME *name = NULL, *CAname = NULL, *subject = NULL;
1468 const ASN1_TIME *tm;
1469 ASN1_STRING *str, *str2;
1472 X509_NAME_ENTRY *ne, *tne;
1474 int ok = -1, i, j, last, nid;
1477 OPENSSL_STRING row[DB_NUMBER];
1478 OPENSSL_STRING *irow = NULL;
1479 OPENSSL_STRING *rrow = NULL;
1482 for (i = 0; i < DB_NUMBER; i++)
1486 X509_NAME *n = parse_name(subj, chtype, multirdn);
1489 ERR_print_errors(bio_err);
1492 X509_REQ_set_subject_name(req, n);
1497 BIO_printf(bio_err, "The Subject's Distinguished Name is as follows\n");
1499 name = X509_REQ_get_subject_name(req);
1500 for (i = 0; i < X509_NAME_entry_count(name); i++) {
1501 ne = X509_NAME_get_entry(name, i);
1502 str = X509_NAME_ENTRY_get_data(ne);
1503 obj = X509_NAME_ENTRY_get_object(ne);
1504 nid = OBJ_obj2nid(obj);
1507 /* assume all type should be strings */
1509 if (str->type == V_ASN1_UNIVERSALSTRING)
1510 ASN1_UNIVERSALSTRING_to_string(str);
1512 if (str->type == V_ASN1_IA5STRING && nid != NID_pkcs9_emailAddress)
1513 str->type = V_ASN1_T61STRING;
1515 if (nid == NID_pkcs9_emailAddress
1516 && str->type == V_ASN1_PRINTABLESTRING)
1517 str->type = V_ASN1_IA5STRING;
1520 /* If no EMAIL is wanted in the subject */
1521 if (nid == NID_pkcs9_emailAddress && !email_dn)
1524 /* check some things */
1525 if (nid == NID_pkcs9_emailAddress && str->type != V_ASN1_IA5STRING) {
1527 "\nemailAddress type needs to be of type IA5STRING\n");
1530 if (str->type != V_ASN1_BMPSTRING && str->type != V_ASN1_UTF8STRING) {
1531 j = ASN1_PRINTABLE_type(str->data, str->length);
1532 if ((j == V_ASN1_T61STRING && str->type != V_ASN1_T61STRING) ||
1533 (j == V_ASN1_IA5STRING && str->type == V_ASN1_PRINTABLESTRING))
1536 "\nThe string contains characters that are illegal for the ASN.1 type\n");
1542 old_entry_print(obj, str);
1545 /* Ok, now we check the 'policy' stuff. */
1546 if ((subject = X509_NAME_new()) == NULL) {
1547 BIO_printf(bio_err, "Memory allocation failure\n");
1551 /* take a copy of the issuer name before we mess with it. */
1553 CAname = X509_NAME_dup(name);
1555 CAname = X509_NAME_dup(X509_get_subject_name(x509));
1560 for (i = 0; i < sk_CONF_VALUE_num(policy); i++) {
1561 cv = sk_CONF_VALUE_value(policy, i); /* get the object id */
1562 if ((j = OBJ_txt2nid(cv->name)) == NID_undef) {
1564 "%s:unknown object type in 'policy' configuration\n",
1568 obj = OBJ_nid2obj(j);
1572 X509_NAME_ENTRY *push = NULL;
1574 /* lookup the object in the supplied name list */
1575 j = X509_NAME_get_index_by_OBJ(name, obj, last);
1581 tne = X509_NAME_get_entry(name, j);
1585 /* depending on the 'policy', decide what to do. */
1586 if (strcmp(cv->value, "optional") == 0) {
1589 } else if (strcmp(cv->value, "supplied") == 0) {
1592 "The %s field needed to be supplied and was missing\n",
1598 } else if (strcmp(cv->value, "match") == 0) {
1603 "The mandatory %s field was missing\n",
1611 j = X509_NAME_get_index_by_OBJ(CAname, obj, last2);
1612 if ((j < 0) && (last2 == -1)) {
1614 "The %s field does not exist in the CA certificate,\n"
1615 "the 'policy' is misconfigured\n", cv->name);
1619 push = X509_NAME_get_entry(CAname, j);
1620 str = X509_NAME_ENTRY_get_data(tne);
1621 str2 = X509_NAME_ENTRY_get_data(push);
1623 if (ASN1_STRING_cmp(str, str2) != 0)
1628 "The %s field is different between\n"
1629 "CA certificate (%s) and the request (%s)\n",
1631 ((str2 == NULL) ? "NULL" : (char *)str2->data),
1632 ((str == NULL) ? "NULL" : (char *)str->data));
1637 "%s:invalid type in 'policy' configuration\n",
1643 if (!X509_NAME_add_entry(subject, push, -1, 0)) {
1644 BIO_printf(bio_err, "Memory allocation failure\n");
1654 X509_NAME_free(subject);
1655 /* subject=X509_NAME_dup(X509_REQ_get_subject_name(req)); */
1656 subject = X509_NAME_dup(name);
1657 if (subject == NULL)
1661 /* We are now totally happy, lets make and sign the certificate */
1664 "Everything appears to be ok, creating and signing the certificate\n");
1666 if ((ret = X509_new()) == NULL)
1670 /* Make it an X509 v3 certificate. */
1671 if (!X509_set_version(ret, 2))
1675 if (BN_to_ASN1_INTEGER(serial, X509_get_serialNumber(ret)) == NULL)
1678 if (!X509_set_issuer_name(ret, subject))
1681 if (!X509_set_issuer_name(ret, X509_get_subject_name(x509)))
1685 if (!set_cert_times(ret, startdate, enddate, days))
1688 if (enddate != NULL) {
1691 if (!ASN1_TIME_diff(&tdays, NULL, NULL, X509_get0_notAfter(ret)))
1696 if (!X509_set_subject_name(ret, subject))
1699 pktmp = X509_REQ_get0_pubkey(req);
1700 i = X509_set_pubkey(ret, pktmp);
1704 /* Lets add the extensions, if there are any */
1708 /* Initialize the context structure */
1710 X509V3_set_ctx(&ctx, ret, ret, req, NULL, 0);
1712 X509V3_set_ctx(&ctx, x509, ret, req, NULL, 0);
1714 if (extconf != NULL) {
1716 BIO_printf(bio_err, "Extra configuration file found\n");
1718 /* Use the extconf configuration db LHASH */
1719 X509V3_set_nconf(&ctx, extconf);
1721 /* Test the structure (needed?) */
1722 /* X509V3_set_ctx_test(&ctx); */
1724 /* Adds exts contained in the configuration file */
1725 if (!X509V3_EXT_add_nconf(extconf, &ctx, ext_sect, ret)) {
1727 "ERROR: adding extensions in section %s\n",
1729 ERR_print_errors(bio_err);
1734 "Successfully added extensions from file.\n");
1735 } else if (ext_sect) {
1736 /* We found extensions to be set from config file */
1737 X509V3_set_nconf(&ctx, lconf);
1739 if (!X509V3_EXT_add_nconf(lconf, &ctx, ext_sect, ret)) {
1741 "ERROR: adding extensions in section %s\n",
1743 ERR_print_errors(bio_err);
1749 "Successfully added extensions from config\n");
1753 /* Copy extensions from request (if any) */
1755 if (!copy_extensions(ret, req, ext_copy)) {
1756 BIO_printf(bio_err, "ERROR: adding extensions from request\n");
1757 ERR_print_errors(bio_err);
1762 const STACK_OF(X509_EXTENSION) *exts = X509_get0_extensions(ret);
1764 if (exts != NULL && sk_X509_EXTENSION_num(exts) > 0)
1765 /* Make it an X509 v3 certificate. */
1766 if (!X509_set_version(ret, 2))
1772 "The subject name appears to be ok, checking data base for clashes\n");
1774 /* Build the correct Subject if no e-mail is wanted in the subject. */
1776 X509_NAME_ENTRY *tmpne;
1777 X509_NAME *dn_subject;
1780 * Its best to dup the subject DN and then delete any email addresses
1781 * because this retains its structure.
1783 if ((dn_subject = X509_NAME_dup(subject)) == NULL) {
1784 BIO_printf(bio_err, "Memory allocation failure\n");
1788 while ((i = X509_NAME_get_index_by_NID(dn_subject,
1789 NID_pkcs9_emailAddress,
1791 tmpne = X509_NAME_delete_entry(dn_subject, i--);
1792 X509_NAME_ENTRY_free(tmpne);
1795 if (!X509_set_subject_name(ret, dn_subject)) {
1796 X509_NAME_free(dn_subject);
1799 X509_NAME_free(dn_subject);
1802 row[DB_name] = X509_NAME_oneline(X509_get_subject_name(ret), NULL, 0);
1803 if (row[DB_name] == NULL) {
1804 BIO_printf(bio_err, "Memory allocation failure\n");
1808 if (BN_is_zero(serial))
1809 row[DB_serial] = OPENSSL_strdup("00");
1811 row[DB_serial] = BN_bn2hex(serial);
1812 if (row[DB_serial] == NULL) {
1813 BIO_printf(bio_err, "Memory allocation failure\n");
1817 if (row[DB_name][0] == '\0') {
1819 * An empty subject! We'll use the serial number instead. If
1820 * unique_subject is in use then we don't want different entries with
1821 * empty subjects matching each other.
1823 OPENSSL_free(row[DB_name]);
1824 row[DB_name] = OPENSSL_strdup(row[DB_serial]);
1825 if (row[DB_name] == NULL) {
1826 BIO_printf(bio_err, "Memory allocation failure\n");
1831 if (db->attributes.unique_subject) {
1832 OPENSSL_STRING *crow = row;
1834 rrow = TXT_DB_get_by_index(db->db, DB_name, crow);
1837 "ERROR:There is already a certificate for %s\n",
1842 rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
1845 "ERROR:Serial number %s has already been issued,\n",
1848 " check the database/serial_file for corruption\n");
1853 BIO_printf(bio_err, "The matching entry has the following details\n");
1854 if (rrow[DB_type][0] == DB_TYPE_EXP)
1856 else if (rrow[DB_type][0] == DB_TYPE_REV)
1858 else if (rrow[DB_type][0] == DB_TYPE_VAL)
1861 p = "\ninvalid type, Data base error\n";
1862 BIO_printf(bio_err, "Type :%s\n", p);;
1863 if (rrow[DB_type][0] == DB_TYPE_REV) {
1864 p = rrow[DB_exp_date];
1867 BIO_printf(bio_err, "Was revoked on:%s\n", p);
1869 p = rrow[DB_exp_date];
1872 BIO_printf(bio_err, "Expires on :%s\n", p);
1873 p = rrow[DB_serial];
1876 BIO_printf(bio_err, "Serial Number :%s\n", p);
1880 BIO_printf(bio_err, "File name :%s\n", p);
1884 BIO_printf(bio_err, "Subject Name :%s\n", p);
1885 ok = -1; /* This is now a 'bad' error. */
1890 BIO_printf(bio_err, "Certificate Details:\n");
1892 * Never print signature details because signature not present
1894 certopt |= X509_FLAG_NO_SIGDUMP | X509_FLAG_NO_SIGNAME;
1895 X509_print_ex(bio_err, ret, nameopt, certopt);
1898 BIO_printf(bio_err, "Certificate is to be certified until ");
1899 ASN1_TIME_print(bio_err, X509_get0_notAfter(ret));
1901 BIO_printf(bio_err, " (%ld days)", days);
1902 BIO_printf(bio_err, "\n");
1906 BIO_printf(bio_err, "Sign the certificate? [y/n]:");
1907 (void)BIO_flush(bio_err);
1909 if (fgets(buf, sizeof(buf), stdin) == NULL) {
1911 "CERTIFICATE WILL NOT BE CERTIFIED: I/O error\n");
1915 if (!(buf[0] == 'y' || buf[0] == 'Y')) {
1916 BIO_printf(bio_err, "CERTIFICATE WILL NOT BE CERTIFIED\n");
1922 pktmp = X509_get0_pubkey(ret);
1923 if (EVP_PKEY_missing_parameters(pktmp) &&
1924 !EVP_PKEY_missing_parameters(pkey))
1925 EVP_PKEY_copy_parameters(pktmp, pkey);
1927 if (!do_X509_sign(ret, pkey, dgst, sigopts))
1930 /* We now just add it to the database as DB_TYPE_VAL('V') */
1931 row[DB_type] = OPENSSL_strdup("V");
1932 tm = X509_get0_notAfter(ret);
1933 row[DB_exp_date] = app_malloc(tm->length + 1, "row expdate");
1934 memcpy(row[DB_exp_date], tm->data, tm->length);
1935 row[DB_exp_date][tm->length] = '\0';
1936 row[DB_rev_date] = NULL;
1937 row[DB_file] = OPENSSL_strdup("unknown");
1938 if ((row[DB_type] == NULL) || (row[DB_exp_date] == NULL) ||
1939 (row[DB_file] == NULL) || (row[DB_name] == NULL)) {
1940 BIO_printf(bio_err, "Memory allocation failure\n");
1944 irow = app_malloc(sizeof(*irow) * (DB_NUMBER + 1), "row space");
1945 for (i = 0; i < DB_NUMBER; i++)
1947 irow[DB_NUMBER] = NULL;
1949 if (!TXT_DB_insert(db->db, irow)) {
1950 BIO_printf(bio_err, "failed to update database\n");
1951 BIO_printf(bio_err, "TXT_DB error number %ld\n", db->db->error);
1958 for (i = 0; i < DB_NUMBER; i++)
1959 OPENSSL_free(row[i]);
1963 X509_NAME_free(CAname);
1964 X509_NAME_free(subject);
1972 static void write_new_certificate(BIO *bp, X509 *x, int output_der, int notext)
1976 (void)i2d_X509_bio(bp, x);
1981 PEM_write_bio_X509(bp, x);
1984 static int certify_spkac(X509 **xret, const char *infile, EVP_PKEY *pkey,
1985 X509 *x509, const EVP_MD *dgst,
1986 STACK_OF(OPENSSL_STRING) *sigopts,
1987 STACK_OF(CONF_VALUE) *policy, CA_DB *db,
1988 BIGNUM *serial, const char *subj, unsigned long chtype,
1989 int multirdn, int email_dn, const char *startdate,
1990 const char *enddate, long days, const char *ext_sect,
1991 CONF *lconf, int verbose, unsigned long certopt,
1992 unsigned long nameopt, int default_op, int ext_copy)
1994 STACK_OF(CONF_VALUE) *sk = NULL;
1995 LHASH_OF(CONF_VALUE) *parms = NULL;
1996 X509_REQ *req = NULL;
1997 CONF_VALUE *cv = NULL;
1998 NETSCAPE_SPKI *spki = NULL;
2000 EVP_PKEY *pktmp = NULL;
2001 X509_NAME *n = NULL;
2002 X509_NAME_ENTRY *ne = NULL;
2008 * Load input file into a hash table. (This is just an easy
2009 * way to read and parse the file, then put it into a convenient
2012 parms = CONF_load(NULL, infile, &errline);
2013 if (parms == NULL) {
2014 BIO_printf(bio_err, "error on line %ld of %s\n", errline, infile);
2015 ERR_print_errors(bio_err);
2019 sk = CONF_get_section(parms, "default");
2020 if (sk_CONF_VALUE_num(sk) == 0) {
2021 BIO_printf(bio_err, "no name/value pairs found in %s\n", infile);
2026 * Now create a dummy X509 request structure. We don't actually
2027 * have an X509 request, but we have many of the components
2028 * (a public key, various DN components). The idea is that we
2029 * put these components into the right X509 request structure
2030 * and we can use the same code as if you had a real X509 request.
2032 req = X509_REQ_new();
2034 ERR_print_errors(bio_err);
2039 * Build up the subject name set.
2041 n = X509_REQ_get_subject_name(req);
2044 if (sk_CONF_VALUE_num(sk) <= i)
2047 cv = sk_CONF_VALUE_value(sk, i);
2050 * Skip past any leading X. X: X, etc to allow for multiple instances
2052 for (buf = cv->name; *buf; buf++)
2053 if ((*buf == ':') || (*buf == ',') || (*buf == '.')) {
2061 if ((nid = OBJ_txt2nid(type)) == NID_undef) {
2062 if (strcmp(type, "SPKAC") == 0) {
2063 spki = NETSCAPE_SPKI_b64_decode(cv->value, -1);
2066 "unable to load Netscape SPKAC structure\n");
2067 ERR_print_errors(bio_err);
2074 if (!X509_NAME_add_entry_by_NID(n, nid, chtype,
2075 (unsigned char *)buf, -1, -1, 0))
2079 BIO_printf(bio_err, "Netscape SPKAC structure not found in %s\n",
2085 * Now extract the key from the SPKI structure.
2088 BIO_printf(bio_err, "Check that the SPKAC request matches the signature\n");
2090 if ((pktmp = NETSCAPE_SPKI_get_pubkey(spki)) == NULL) {
2091 BIO_printf(bio_err, "error unpacking SPKAC public key\n");
2095 j = NETSCAPE_SPKI_verify(spki, pktmp);
2097 EVP_PKEY_free(pktmp);
2099 "signature verification failed on SPKAC public key\n");
2102 BIO_printf(bio_err, "Signature ok\n");
2104 X509_REQ_set_pubkey(req, pktmp);
2105 EVP_PKEY_free(pktmp);
2106 ok = do_body(xret, pkey, x509, dgst, sigopts, policy, db, serial, subj,
2107 chtype, multirdn, email_dn, startdate, enddate, days, 1,
2108 verbose, req, ext_sect, lconf, certopt, nameopt, default_op,
2113 NETSCAPE_SPKI_free(spki);
2114 X509_NAME_ENTRY_free(ne);
2119 static int check_time_format(const char *str)
2121 return ASN1_TIME_set_string(NULL, str);
2124 static int do_revoke(X509 *x509, CA_DB *db, REVINFO_TYPE rev_type,
2127 const ASN1_TIME *tm = NULL;
2128 char *row[DB_NUMBER], **rrow, **irow;
2129 char *rev_str = NULL;
2133 for (i = 0; i < DB_NUMBER; i++)
2135 row[DB_name] = X509_NAME_oneline(X509_get_subject_name(x509), NULL, 0);
2136 bn = ASN1_INTEGER_to_BN(X509_get_serialNumber(x509), NULL);
2140 row[DB_serial] = OPENSSL_strdup("00");
2142 row[DB_serial] = BN_bn2hex(bn);
2144 if (row[DB_name] != NULL && row[DB_name][0] == '\0') {
2145 /* Entries with empty Subjects actually use the serial number instead */
2146 OPENSSL_free(row[DB_name]);
2147 row[DB_name] = OPENSSL_strdup(row[DB_serial]);
2149 if ((row[DB_name] == NULL) || (row[DB_serial] == NULL)) {
2150 BIO_printf(bio_err, "Memory allocation failure\n");
2154 * We have to lookup by serial number because name lookup skips revoked
2157 rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
2160 "Adding Entry with serial number %s to DB for %s\n",
2161 row[DB_serial], row[DB_name]);
2163 /* We now just add it to the database as DB_TYPE_REV('V') */
2164 row[DB_type] = OPENSSL_strdup("V");
2165 tm = X509_get0_notAfter(x509);
2166 row[DB_exp_date] = app_malloc(tm->length + 1, "row exp_data");
2167 memcpy(row[DB_exp_date], tm->data, tm->length);
2168 row[DB_exp_date][tm->length] = '\0';
2169 row[DB_rev_date] = NULL;
2170 row[DB_file] = OPENSSL_strdup("unknown");
2172 if (row[DB_type] == NULL || row[DB_file] == NULL) {
2173 BIO_printf(bio_err, "Memory allocation failure\n");
2177 irow = app_malloc(sizeof(*irow) * (DB_NUMBER + 1), "row ptr");
2178 for (i = 0; i < DB_NUMBER; i++)
2180 irow[DB_NUMBER] = NULL;
2182 if (!TXT_DB_insert(db->db, irow)) {
2183 BIO_printf(bio_err, "failed to update database\n");
2184 BIO_printf(bio_err, "TXT_DB error number %ld\n", db->db->error);
2189 for (i = 0; i < DB_NUMBER; i++)
2192 /* Revoke Certificate */
2193 if (rev_type == REV_VALID)
2196 /* Retry revocation after DB insertion */
2197 ok = do_revoke(x509, db, rev_type, value);
2201 } else if (index_name_cmp_noconst(row, rrow)) {
2202 BIO_printf(bio_err, "ERROR:name does not match %s\n", row[DB_name]);
2204 } else if (rev_type == REV_VALID) {
2205 BIO_printf(bio_err, "ERROR:Already present, serial number %s\n",
2208 } else if (rrow[DB_type][0] == DB_TYPE_REV) {
2209 BIO_printf(bio_err, "ERROR:Already revoked, serial number %s\n",
2213 BIO_printf(bio_err, "Revoking Certificate %s.\n", rrow[DB_serial]);
2214 rev_str = make_revocation_str(rev_type, value);
2216 BIO_printf(bio_err, "Error in revocation arguments\n");
2219 rrow[DB_type][0] = DB_TYPE_REV;
2220 rrow[DB_type][1] = '\0';
2221 rrow[DB_rev_date] = rev_str;
2225 for (i = 0; i < DB_NUMBER; i++)
2226 OPENSSL_free(row[i]);
2230 static int get_certificate_status(const char *serial, CA_DB *db)
2232 char *row[DB_NUMBER], **rrow;
2234 size_t serial_len = strlen(serial);
2236 /* Free Resources */
2237 for (i = 0; i < DB_NUMBER; i++)
2240 /* Malloc needed char spaces */
2241 row[DB_serial] = app_malloc(serial_len + 2, "row serial#");
2243 if (serial_len % 2) {
2245 * Set the first char to 0
2247 row[DB_serial][0] = '0';
2249 /* Copy String from serial to row[DB_serial] */
2250 memcpy(row[DB_serial] + 1, serial, serial_len);
2251 row[DB_serial][serial_len + 1] = '\0';
2253 /* Copy String from serial to row[DB_serial] */
2254 memcpy(row[DB_serial], serial, serial_len);
2255 row[DB_serial][serial_len] = '\0';
2258 /* Make it Upper Case */
2259 make_uppercase(row[DB_serial]);
2263 /* Search for the certificate */
2264 rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
2266 BIO_printf(bio_err, "Serial %s not present in db.\n", row[DB_serial]);
2269 } else if (rrow[DB_type][0] == DB_TYPE_VAL) {
2270 BIO_printf(bio_err, "%s=Valid (%c)\n",
2271 row[DB_serial], rrow[DB_type][0]);
2273 } else if (rrow[DB_type][0] == DB_TYPE_REV) {
2274 BIO_printf(bio_err, "%s=Revoked (%c)\n",
2275 row[DB_serial], rrow[DB_type][0]);
2277 } else if (rrow[DB_type][0] == DB_TYPE_EXP) {
2278 BIO_printf(bio_err, "%s=Expired (%c)\n",
2279 row[DB_serial], rrow[DB_type][0]);
2281 } else if (rrow[DB_type][0] == DB_TYPE_SUSP) {
2282 BIO_printf(bio_err, "%s=Suspended (%c)\n",
2283 row[DB_serial], rrow[DB_type][0]);
2286 BIO_printf(bio_err, "%s=Unknown (%c).\n",
2287 row[DB_serial], rrow[DB_type][0]);
2291 for (i = 0; i < DB_NUMBER; i++) {
2292 OPENSSL_free(row[i]);
2297 static int do_updatedb(CA_DB *db)
2299 ASN1_UTCTIME *a_tm = NULL;
2301 int db_y2k, a_y2k; /* flags = 1 if y >= 2000 */
2302 char **rrow, *a_tm_s;
2304 a_tm = ASN1_UTCTIME_new();
2308 /* get actual time and make a string */
2309 if (X509_gmtime_adj(a_tm, 0) == NULL) {
2310 ASN1_UTCTIME_free(a_tm);
2313 a_tm_s = app_malloc(a_tm->length + 1, "time string");
2315 memcpy(a_tm_s, a_tm->data, a_tm->length);
2316 a_tm_s[a_tm->length] = '\0';
2318 if (strncmp(a_tm_s, "49", 2) <= 0)
2323 for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
2324 rrow = sk_OPENSSL_PSTRING_value(db->db->data, i);
2326 if (rrow[DB_type][0] == DB_TYPE_VAL) {
2327 /* ignore entries that are not valid */
2328 if (strncmp(rrow[DB_exp_date], "49", 2) <= 0)
2333 if (db_y2k == a_y2k) {
2334 /* all on the same y2k side */
2335 if (strcmp(rrow[DB_exp_date], a_tm_s) <= 0) {
2336 rrow[DB_type][0] = DB_TYPE_EXP;
2337 rrow[DB_type][1] = '\0';
2340 BIO_printf(bio_err, "%s=Expired\n", rrow[DB_serial]);
2342 } else if (db_y2k < a_y2k) {
2343 rrow[DB_type][0] = DB_TYPE_EXP;
2344 rrow[DB_type][1] = '\0';
2347 BIO_printf(bio_err, "%s=Expired\n", rrow[DB_serial]);
2353 ASN1_UTCTIME_free(a_tm);
2354 OPENSSL_free(a_tm_s);
2358 static const char *crl_reasons[] = {
2359 /* CRL reason strings */
2363 "affiliationChanged",
2365 "cessationOfOperation",
2368 /* Additional pseudo reasons */
2374 #define NUM_REASONS OSSL_NELEM(crl_reasons)
2377 * Given revocation information convert to a DB string. The format of the
2378 * string is: revtime[,reason,extra]. Where 'revtime' is the revocation time
2379 * (the current time). 'reason' is the optional CRL reason and 'extra' is any
2380 * additional argument
2383 static char *make_revocation_str(REVINFO_TYPE rev_type, const char *rev_arg)
2386 const char *reason = NULL, *other = NULL;
2388 ASN1_UTCTIME *revtm = NULL;
2396 case REV_CRL_REASON:
2397 for (i = 0; i < 8; i++) {
2398 if (strcasecmp(rev_arg, crl_reasons[i]) == 0) {
2399 reason = crl_reasons[i];
2403 if (reason == NULL) {
2404 BIO_printf(bio_err, "Unknown CRL reason %s\n", rev_arg);
2410 /* Argument is an OID */
2411 otmp = OBJ_txt2obj(rev_arg, 0);
2412 ASN1_OBJECT_free(otmp);
2415 BIO_printf(bio_err, "Invalid object identifier %s\n", rev_arg);
2419 reason = "holdInstruction";
2423 case REV_KEY_COMPROMISE:
2424 case REV_CA_COMPROMISE:
2425 /* Argument is the key compromise time */
2426 if (!ASN1_GENERALIZEDTIME_set_string(NULL, rev_arg)) {
2428 "Invalid time format %s. Need YYYYMMDDHHMMSSZ\n",
2433 if (rev_type == REV_KEY_COMPROMISE)
2436 reason = "CAkeyTime";
2441 revtm = X509_gmtime_adj(NULL, 0);
2446 i = revtm->length + 1;
2449 i += strlen(reason) + 1;
2451 i += strlen(other) + 1;
2453 str = app_malloc(i, "revocation reason");
2454 OPENSSL_strlcpy(str, (char *)revtm->data, i);
2456 OPENSSL_strlcat(str, ",", i);
2457 OPENSSL_strlcat(str, reason, i);
2460 OPENSSL_strlcat(str, ",", i);
2461 OPENSSL_strlcat(str, other, i);
2463 ASN1_UTCTIME_free(revtm);
2468 * Convert revocation field to X509_REVOKED entry
2472 * 2 OK and some extensions added (i.e. V2 CRL)
2475 static int make_revoked(X509_REVOKED *rev, const char *str)
2478 int reason_code = -1;
2480 ASN1_OBJECT *hold = NULL;
2481 ASN1_GENERALIZEDTIME *comp_time = NULL;
2482 ASN1_ENUMERATED *rtmp = NULL;
2484 ASN1_TIME *revDate = NULL;
2486 i = unpack_revinfo(&revDate, &reason_code, &hold, &comp_time, str);
2491 if (rev && !X509_REVOKED_set_revocationDate(rev, revDate))
2494 if (rev && (reason_code != OCSP_REVOKED_STATUS_NOSTATUS)) {
2495 rtmp = ASN1_ENUMERATED_new();
2496 if (rtmp == NULL || !ASN1_ENUMERATED_set(rtmp, reason_code))
2498 if (!X509_REVOKED_add1_ext_i2d(rev, NID_crl_reason, rtmp, 0, 0))
2502 if (rev && comp_time) {
2503 if (!X509_REVOKED_add1_ext_i2d
2504 (rev, NID_invalidity_date, comp_time, 0, 0))
2508 if (!X509_REVOKED_add1_ext_i2d
2509 (rev, NID_hold_instruction_code, hold, 0, 0))
2513 if (reason_code != OCSP_REVOKED_STATUS_NOSTATUS)
2521 ASN1_OBJECT_free(hold);
2522 ASN1_GENERALIZEDTIME_free(comp_time);
2523 ASN1_ENUMERATED_free(rtmp);
2524 ASN1_TIME_free(revDate);
2529 static int old_entry_print(const ASN1_OBJECT *obj, const ASN1_STRING *str)
2531 char buf[25], *pbuf;
2535 j = i2a_ASN1_OBJECT(bio_err, obj);
2537 for (j = 22 - j; j > 0; j--)
2541 BIO_puts(bio_err, buf);
2543 if (str->type == V_ASN1_PRINTABLESTRING)
2544 BIO_printf(bio_err, "PRINTABLE:'");
2545 else if (str->type == V_ASN1_T61STRING)
2546 BIO_printf(bio_err, "T61STRING:'");
2547 else if (str->type == V_ASN1_IA5STRING)
2548 BIO_printf(bio_err, "IA5STRING:'");
2549 else if (str->type == V_ASN1_UNIVERSALSTRING)
2550 BIO_printf(bio_err, "UNIVERSALSTRING:'");
2552 BIO_printf(bio_err, "ASN.1 %2d:'", str->type);
2554 p = (const char *)str->data;
2555 for (j = str->length; j > 0; j--) {
2556 if ((*p >= ' ') && (*p <= '~'))
2557 BIO_printf(bio_err, "%c", *p);
2559 BIO_printf(bio_err, "\\0x%02X", *p);
2560 else if ((unsigned char)*p == 0xf7)
2561 BIO_printf(bio_err, "^?");
2563 BIO_printf(bio_err, "^%c", *p + '@');
2566 BIO_printf(bio_err, "'\n");
2570 int unpack_revinfo(ASN1_TIME **prevtm, int *preason, ASN1_OBJECT **phold,
2571 ASN1_GENERALIZEDTIME **pinvtm, const char *str)
2574 char *rtime_str, *reason_str = NULL, *arg_str = NULL, *p;
2575 int reason_code = -1;
2578 ASN1_OBJECT *hold = NULL;
2579 ASN1_GENERALIZEDTIME *comp_time = NULL;
2581 tmp = OPENSSL_strdup(str);
2583 BIO_printf(bio_err, "memory allocation failure\n");
2587 p = strchr(tmp, ',');
2603 *prevtm = ASN1_UTCTIME_new();
2604 if (*prevtm == NULL) {
2605 BIO_printf(bio_err, "memory allocation failure\n");
2608 if (!ASN1_UTCTIME_set_string(*prevtm, rtime_str)) {
2609 BIO_printf(bio_err, "invalid revocation date %s\n", rtime_str);
2614 for (i = 0; i < NUM_REASONS; i++) {
2615 if (strcasecmp(reason_str, crl_reasons[i]) == 0) {
2620 if (reason_code == OCSP_REVOKED_STATUS_NOSTATUS) {
2621 BIO_printf(bio_err, "invalid reason code %s\n", reason_str);
2625 if (reason_code == 7) {
2626 reason_code = OCSP_REVOKED_STATUS_REMOVEFROMCRL;
2627 } else if (reason_code == 8) { /* Hold instruction */
2629 BIO_printf(bio_err, "missing hold instruction\n");
2632 reason_code = OCSP_REVOKED_STATUS_CERTIFICATEHOLD;
2633 hold = OBJ_txt2obj(arg_str, 0);
2636 BIO_printf(bio_err, "invalid object identifier %s\n", arg_str);
2642 ASN1_OBJECT_free(hold);
2643 } else if ((reason_code == 9) || (reason_code == 10)) {
2645 BIO_printf(bio_err, "missing compromised time\n");
2648 comp_time = ASN1_GENERALIZEDTIME_new();
2649 if (comp_time == NULL) {
2650 BIO_printf(bio_err, "memory allocation failure\n");
2653 if (!ASN1_GENERALIZEDTIME_set_string(comp_time, arg_str)) {
2654 BIO_printf(bio_err, "invalid compromised time %s\n", arg_str);
2657 if (reason_code == 9)
2658 reason_code = OCSP_REVOKED_STATUS_KEYCOMPROMISE;
2660 reason_code = OCSP_REVOKED_STATUS_CACOMPROMISE;
2665 *preason = reason_code;
2667 *pinvtm = comp_time;
2676 ASN1_GENERALIZEDTIME_free(comp_time);