1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
58 /* The PPKI stuff has been donated by Jeff Barber <jeffb@issl.atl.hp.com> */
64 #include <sys/types.h>
65 #include <openssl/conf.h>
66 #include <openssl/bio.h>
67 #include <openssl/err.h>
68 #include <openssl/bn.h>
69 #include <openssl/txt_db.h>
70 #include <openssl/evp.h>
71 #include <openssl/x509.h>
72 #include <openssl/x509v3.h>
73 #include <openssl/objects.h>
74 #include <openssl/ocsp.h>
75 #include <openssl/pem.h>
78 # ifdef OPENSSL_SYS_VMS
84 # elif !defined(OPENSSL_SYS_VXWORKS) && !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_NETWARE)
85 # include <sys/file.h>
101 #define BASE_SECTION "ca"
102 #define CONFIG_FILE "openssl.cnf"
104 #define ENV_DEFAULT_CA "default_ca"
106 #define STRING_MASK "string_mask"
107 #define UTF8_IN "utf8"
109 #define ENV_DIR "dir"
110 #define ENV_CERTS "certs"
111 #define ENV_CRL_DIR "crl_dir"
112 #define ENV_CA_DB "CA_DB"
113 #define ENV_NEW_CERTS_DIR "new_certs_dir"
114 #define ENV_CERTIFICATE "certificate"
115 #define ENV_SERIAL "serial"
116 #define ENV_CRLNUMBER "crlnumber"
117 #define ENV_CRL "crl"
118 #define ENV_PRIVATE_KEY "private_key"
119 #define ENV_RANDFILE "RANDFILE"
120 #define ENV_DEFAULT_DAYS "default_days"
121 #define ENV_DEFAULT_STARTDATE "default_startdate"
122 #define ENV_DEFAULT_ENDDATE "default_enddate"
123 #define ENV_DEFAULT_CRL_DAYS "default_crl_days"
124 #define ENV_DEFAULT_CRL_HOURS "default_crl_hours"
125 #define ENV_DEFAULT_MD "default_md"
126 #define ENV_DEFAULT_EMAIL_DN "email_in_dn"
127 #define ENV_PRESERVE "preserve"
128 #define ENV_POLICY "policy"
129 #define ENV_EXTENSIONS "x509_extensions"
130 #define ENV_CRLEXT "crl_extensions"
131 #define ENV_MSIE_HACK "msie_hack"
132 #define ENV_NAMEOPT "name_opt"
133 #define ENV_CERTOPT "cert_opt"
134 #define ENV_EXTCOPY "copy_extensions"
135 #define ENV_UNIQUE_SUBJECT "unique_subject"
137 #define ENV_DATABASE "database"
139 /* Additional revocation information types */
141 #define REV_NONE 0 /* No addditional information */
142 #define REV_CRL_REASON 1 /* Value is CRL reason code */
143 #define REV_HOLD 2 /* Value is hold instruction */
144 #define REV_KEY_COMPROMISE 3 /* Value is cert key compromise time */
145 #define REV_CA_COMPROMISE 4 /* Value is CA key compromise time */
148 extern int EF_PROTECT_FREE;
149 extern int EF_PROTECT_BELOW;
150 extern int EF_ALIGNMENT;
153 static void lookup_fail(const char *name, const char *tag);
154 static int certify(X509 **xret, char *infile, EVP_PKEY *pkey, X509 *x509,
155 const EVP_MD *dgst, STACK_OF(OPENSSL_STRING) *sigopts,
156 STACK_OF(CONF_VALUE) *policy, CA_DB *db,
157 BIGNUM *serial, char *subj, unsigned long chtype,
158 int multirdn, int email_dn, char *startdate, char *enddate,
159 long days, int batch, char *ext_sect, CONF *conf,
160 int verbose, unsigned long certopt, unsigned long nameopt,
161 int default_op, int ext_copy, int selfsign);
162 static int certify_cert(X509 **xret, char *infile, EVP_PKEY *pkey, X509 *x509,
163 const EVP_MD *dgst, STACK_OF(OPENSSL_STRING) *sigopts,
164 STACK_OF(CONF_VALUE) *policy, CA_DB *db,
165 BIGNUM *serial, char *subj, unsigned long chtype,
166 int multirdn, int email_dn, char *startdate,
167 char *enddate, long days, int batch, char *ext_sect,
168 CONF *conf, int verbose, unsigned long certopt,
169 unsigned long nameopt, int default_op, int ext_copy,
171 static int certify_spkac(X509 **xret, char *infile, EVP_PKEY *pkey,
172 X509 *x509, const EVP_MD *dgst,
173 STACK_OF(OPENSSL_STRING) *sigopts,
174 STACK_OF(CONF_VALUE) *policy, CA_DB *db,
175 BIGNUM *serial, char *subj, unsigned long chtype,
176 int multirdn, int email_dn, char *startdate,
177 char *enddate, long days, char *ext_sect, CONF *conf,
178 int verbose, unsigned long certopt,
179 unsigned long nameopt, int default_op, int ext_copy);
180 static void write_new_certificate(BIO *bp, X509 *x, int output_der,
182 static int do_body(X509 **xret, EVP_PKEY *pkey, X509 *x509,
183 const EVP_MD *dgst, STACK_OF(OPENSSL_STRING) *sigopts,
184 STACK_OF(CONF_VALUE) *policy, CA_DB *db, BIGNUM *serial,
185 char *subj, unsigned long chtype, int multirdn,
186 int email_dn, char *startdate, char *enddate, long days,
187 int batch, int verbose, X509_REQ *req, char *ext_sect,
188 CONF *conf, unsigned long certopt, unsigned long nameopt,
189 int default_op, int ext_copy, int selfsign);
190 static int do_revoke(X509 *x509, CA_DB *db, int ext, char *extval);
191 static int get_certificate_status(const char *ser_status, CA_DB *db);
192 static int do_updatedb(CA_DB *db);
193 static int check_time_format(const char *str);
194 char *make_revocation_str(int rev_type, char *rev_arg);
195 int make_revoked(X509_REVOKED *rev, const char *str);
196 int old_entry_print(BIO *bp, ASN1_OBJECT *obj, ASN1_STRING *str);
198 static CONF *conf = NULL;
199 static CONF *extconf = NULL;
200 static char *section = NULL;
201 static int preserve = 0;
202 static int msie_hack = 0;
204 typedef enum OPTION_choice {
205 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
206 OPT_ENGINE, OPT_VERBOSE, OPT_CONFIG, OPT_NAME, OPT_SUBJ, OPT_UTF8,
207 OPT_CREATE_SERIAL, OPT_MULTIVALUE_RDN, OPT_STARTDATE, OPT_ENDDATE,
208 OPT_DAYS, OPT_MD, OPT_POLICY, OPT_KEYFILE, OPT_KEYFORM, OPT_PASSIN,
209 OPT_KEY, OPT_CERT, OPT_SELFSIGN, OPT_IN, OPT_OUT, OPT_OUTDIR,
210 OPT_SIGOPT, OPT_NOTEXT, OPT_BATCH, OPT_PRESERVEDN, OPT_NOEMAILDN,
211 OPT_GENCRL, OPT_MSIE_HACK, OPT_CRLDAYS, OPT_CRLHOURS, OPT_CRLSEC,
212 OPT_INFILES, OPT_SS_CERT, OPT_SPKAC, OPT_REVOKE, OPT_VALID,
213 OPT_EXTENSIONS, OPT_EXTFILE, OPT_STATUS, OPT_UPDATEDB, OPT_CRLEXTS,
214 OPT_CRL_REASON, OPT_CRL_HOLD, OPT_CRL_COMPROMISE,
215 OPT_CRL_CA_COMPROMISE
218 OPTIONS ca_options[] = {
219 {"help", OPT_HELP, '-', "Display this summary"},
220 {"verbose", OPT_VERBOSE, '-', "Verbose output during processing"},
221 {"config", OPT_CONFIG, 's', "A config file"},
222 {"name", OPT_NAME, 's', "The particular CA definition to use"},
223 {"subj", OPT_SUBJ, 's', "Use arg instead of request's subject"},
224 {"utf8", OPT_UTF8, '-', "Input characters are UTF8 (default ASCII)"},
225 {"create_serial", OPT_CREATE_SERIAL, '-'},
226 {"multivalue-rdn", OPT_MULTIVALUE_RDN, '-',
227 "Enable support for multivalued RDNs"},
228 {"startdate", OPT_STARTDATE, 's', "Cert notBefore, YYMMDDHHMMSSZ"},
229 {"enddate", OPT_ENDDATE, 's',
230 "YYMMDDHHMMSSZ cert notAfter (overrides -days)"},
231 {"days", OPT_DAYS, 'p', "Number of days to certify the cert for"},
232 {"md", OPT_MD, 's', "md to use; one of md2, md5, sha or sha1"},
233 {"policy", OPT_POLICY, 's', "The CA 'policy' to support"},
234 {"keyfile", OPT_KEYFILE, '<', "Private key file"},
235 {"keyform", OPT_KEYFORM, 'f', "Private key file format (PEM or ENGINE)"},
236 {"passin", OPT_PASSIN, 's'},
237 {"key", OPT_KEY, 's', "Key to decode the private key if it is encrypted"},
238 {"cert", OPT_CERT, '<', "The CA cert"},
239 {"selfsign", OPT_SELFSIGN, '-',
240 "Sign a cert with the key associated with it"},
241 {"in", OPT_IN, '<', "The input PEM encoded cert request(s)"},
242 {"out", OPT_OUT, '>', "Where to put the output file(s)"},
243 {"outdir", OPT_OUTDIR, '/', "Where to put output cert"},
244 {"sigopt", OPT_SIGOPT, 's'},
245 {"notext", OPT_NOTEXT, '-'},
246 {"batch", OPT_BATCH, '-', "Don't ask questions"},
247 {"preserveDN", OPT_PRESERVEDN, '-', "Don't re-order the DN"},
248 {"noemailDN", OPT_NOEMAILDN, '-', "Don't add the EMAIL field to the DN"},
249 {"gencrl", OPT_GENCRL, '-', "Generate a new CRL"},
250 {"msie_hack", OPT_MSIE_HACK, '-',
251 "msie modifications to handle all those universal strings"},
252 {"crldays", OPT_CRLDAYS, 'p', "Days is when the next CRL is due"},
253 {"crlhours", OPT_CRLHOURS, 'p', "Hours is when the next CRL is due"},
254 {"crlsec", OPT_CRLSEC, 'p'},
255 {"infiles", OPT_INFILES, '-', "The last argument, requests to process"},
256 {"ss_cert", OPT_SS_CERT, '<', "File contains a self signed cert to sign"},
257 {"spkac", OPT_SPKAC, '<',
258 "File contains DN and signed public key and challenge"},
259 {"revoke", OPT_REVOKE, '<', "Revoke a cert (given in file)"},
260 {"valid", OPT_VALID, 's'},
261 {"extensions", OPT_EXTENSIONS, 's',
262 "Extension section (override value in config file)"},
263 {"extfile", OPT_EXTFILE, '<',
264 "Configuration file with X509v3 extensions to add"},
265 {"status", OPT_STATUS, 's', "Shows cert status given the serial number"},
266 {"updatedb", OPT_UPDATEDB, '-', "Updates db for expired cert"},
267 {"crlexts", OPT_CRLEXTS, 's',
268 "CRL extension section (override value in config file)"},
269 {"crl_reason", OPT_CRL_REASON, 's'},
270 {"crl_hold", OPT_CRL_HOLD, 's'},
271 {"crl_compromise", OPT_CRL_COMPROMISE, 's'},
272 {"crl_CA_compromise", OPT_CRL_CA_COMPROMISE, 's'},
273 #ifndef OPENSSL_NO_ENGINE
274 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
279 int ca_main(int argc, char **argv)
282 BIGNUM *crlnumber = NULL, *serial = NULL;
283 EVP_PKEY *pkey = NULL;
284 BIO *in = NULL, *out = NULL, *Sout = NULL, *Cout = NULL;
285 ASN1_INTEGER *tmpser;
289 STACK_OF(CONF_VALUE) *attribs = NULL;
290 STACK_OF(OPENSSL_STRING) *sigopts = NULL;
291 STACK_OF(X509) *cert_sk = NULL;
292 X509_CRL *crl = NULL;
293 const EVP_MD *dgst = NULL;
294 char *configfile = NULL, *md = NULL, *policy = NULL, *keyfile = NULL;
295 char *certfile = NULL, *crl_ext = NULL, *crlnumberfile = NULL, *enddate =
297 char *infile = NULL, *spkac_file = NULL, *ss_cert_file = NULL;
298 char *extensions = NULL, *extfile = NULL, *key = NULL, *passinarg = NULL;
299 char *outdir = NULL, *outfile = NULL, *rev_arg = NULL, *ser_status = NULL;
300 char *serialfile = NULL, *startdate = NULL, *subj = NULL, *tmp_email_dn =
304 char *dbfile = NULL, *engine = NULL, *f, *randfile = NULL, *tofree = NULL;
307 int create_ser = 0, free_key = 0, total = 0, total_done = 0;
308 int batch = 0, default_op = 1, doupdatedb = 0, ext_copy = EXT_COPY_NONE;
309 int keyformat = FORMAT_PEM, multirdn = 0, notext = 0, output_der = 0;
310 int ret = 1, email_dn = 1, req = 0, verbose = 0, gencrl = 0, dorevoke = 0;
311 int i, j, rev_type = REV_NONE, selfsign = 0;
312 long crldays = 0, crlhours = 0, crlsec = 0, errorline = -1, days = 0;
313 unsigned long chtype = MBSTRING_ASC, nameopt = 0, certopt = 0;
314 X509 *x509 = NULL, *x509p = NULL, *x = NULL;
315 X509_REVOKED *r = NULL;
320 EF_PROTECT_BELOW = 1;
329 prog = opt_init(argc, argv, ca_options);
330 while ((o = opt_next()) != OPT_EOF) {
335 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
338 opt_help(ca_options);
351 configfile = opt_arg();
361 chtype = MBSTRING_UTF8;
363 case OPT_CREATE_SERIAL:
366 case OPT_MULTIVALUE_RDN:
370 startdate = opt_arg();
376 days = atoi(opt_arg());
388 if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyformat))
392 passinarg = opt_arg();
398 certfile = opt_arg();
408 sigopts = sk_OPENSSL_STRING_new_null();
410 || !sk_OPENSSL_STRING_push(sigopts, opt_arg()))
432 crldays = atol(opt_arg());
435 crlhours = atol(opt_arg());
438 crlsec = atol(opt_arg());
444 ss_cert_file = opt_arg();
448 spkac_file = opt_arg();
460 extensions = opt_arg();
466 ser_status = opt_arg();
476 rev_type = REV_CRL_REASON;
482 case OPT_CRL_COMPROMISE:
484 rev_type = REV_KEY_COMPROMISE;
486 case OPT_CRL_CA_COMPROMISE:
488 rev_type = REV_CA_COMPROMISE;
496 argc = opt_num_rest();
500 if (configfile == NULL)
501 configfile = getenv("OPENSSL_CONF");
502 if (configfile == NULL)
503 configfile = getenv("SSLEAY_CONF");
504 if (configfile == NULL) {
505 const char *s = X509_get_default_cert_area();
508 #ifdef OPENSSL_SYS_VMS
509 len = strlen(s) + sizeof(CONFIG_FILE);
510 tofree = OPENSSL_malloc(len);
512 BIO_printf(bio_err, "Out of memory\n");
517 len = strlen(s) + sizeof(CONFIG_FILE) + 1;
518 tofree = OPENSSL_malloc(len);
520 BIO_printf(bio_err, "Out of memory\n");
523 BUF_strlcpy(tofree, s, len);
524 BUF_strlcat(tofree, "/", len);
526 BUF_strlcat(tofree, CONFIG_FILE, len);
530 BIO_printf(bio_err, "Using configuration from %s\n", configfile);
531 conf = NCONF_new(NULL);
532 if (NCONF_load(conf, configfile, &errorline) <= 0) {
534 BIO_printf(bio_err, "error loading the config file '%s'\n",
537 BIO_printf(bio_err, "error on line %ld of config file '%s'\n",
538 errorline, configfile);
542 OPENSSL_free(tofree);
545 #ifndef OPENSSL_NO_ENGINE
546 e = setup_engine(engine, 0);
549 /* Lets get the config section we are using */
550 if (section == NULL) {
551 section = NCONF_get_string(conf, BASE_SECTION, ENV_DEFAULT_CA);
552 if (section == NULL) {
553 lookup_fail(BASE_SECTION, ENV_DEFAULT_CA);
559 p = NCONF_get_string(conf, NULL, "oid_file");
565 oid_bio = BIO_new_file(p, "r");
566 if (oid_bio == NULL) {
568 BIO_printf(bio_err,"problems opening %s for extra oid's\n",p);
569 ERR_print_errors(bio_err);
573 OBJ_create_objects(oid_bio);
577 if (!add_oid_section(conf)) {
578 ERR_print_errors(bio_err);
583 randfile = NCONF_get_string(conf, BASE_SECTION, "RANDFILE");
584 if (randfile == NULL)
586 app_RAND_load_file(randfile, 0);
588 f = NCONF_get_string(conf, section, STRING_MASK);
592 if (f && !ASN1_STRING_set_default_mask_asc(f)) {
593 BIO_printf(bio_err, "Invalid global string mask setting %s\n", f);
597 if (chtype != MBSTRING_UTF8) {
598 f = NCONF_get_string(conf, section, UTF8_IN);
601 else if (!strcmp(f, "yes"))
602 chtype = MBSTRING_UTF8;
605 db_attr.unique_subject = 1;
606 p = NCONF_get_string(conf, section, ENV_UNIQUE_SUBJECT);
608 db_attr.unique_subject = parse_yesno(p, 1);
612 /*****************************************************************/
613 /* report status of cert with serial number given on command line */
615 if ((dbfile = NCONF_get_string(conf, section, ENV_DATABASE)) == NULL) {
616 lookup_fail(section, ENV_DATABASE);
619 db = load_index(dbfile, &db_attr);
623 if (!index_index(db))
626 if (get_certificate_status(ser_status, db) != 1)
627 BIO_printf(bio_err, "Error verifying serial %s!\n", ser_status);
631 /*****************************************************************/
632 /* we definitely need a private key, so let's get it */
634 if ((keyfile == NULL) && ((keyfile = NCONF_get_string(conf,
638 lookup_fail(section, ENV_PRIVATE_KEY);
643 if (!app_passwd(passinarg, NULL, &key, NULL)) {
644 BIO_printf(bio_err, "Error getting password\n");
648 pkey = load_key(keyfile, keyformat, 0, key, e, "CA private key");
650 OPENSSL_cleanse(key, strlen(key));
652 /* load_key() has already printed an appropriate message */
656 /*****************************************************************/
657 /* we need a certificate */
658 if (!selfsign || spkac_file || ss_cert_file || gencrl) {
659 if ((certfile == NULL)
660 && ((certfile = NCONF_get_string(conf,
662 ENV_CERTIFICATE)) == NULL)) {
663 lookup_fail(section, ENV_CERTIFICATE);
666 x509 = load_cert(certfile, FORMAT_PEM, NULL, e, "CA certificate");
670 if (!X509_check_private_key(x509, pkey)) {
672 "CA certificate and CA private key do not match\n");
679 f = NCONF_get_string(conf, BASE_SECTION, ENV_PRESERVE);
682 if ((f != NULL) && ((*f == 'y') || (*f == 'Y')))
684 f = NCONF_get_string(conf, BASE_SECTION, ENV_MSIE_HACK);
687 if ((f != NULL) && ((*f == 'y') || (*f == 'Y')))
690 f = NCONF_get_string(conf, section, ENV_NAMEOPT);
693 if (!set_name_ex(&nameopt, f)) {
694 BIO_printf(bio_err, "Invalid name options: \"%s\"\n", f);
701 f = NCONF_get_string(conf, section, ENV_CERTOPT);
704 if (!set_cert_ex(&certopt, f)) {
705 BIO_printf(bio_err, "Invalid certificate options: \"%s\"\n", f);
712 f = NCONF_get_string(conf, section, ENV_EXTCOPY);
715 if (!set_ext_copy(&ext_copy, f)) {
716 BIO_printf(bio_err, "Invalid extension copy option: \"%s\"\n", f);
722 /*****************************************************************/
723 /* lookup where to write new certificates */
724 if ((outdir == NULL) && (req)) {
726 if ((outdir = NCONF_get_string(conf, section, ENV_NEW_CERTS_DIR))
729 "there needs to be defined a directory for new certificate to be placed in\n");
732 #ifndef OPENSSL_SYS_VMS
734 * outdir is a directory spec, but access() for VMS demands a
735 * filename. In any case, stat(), below, will catch the problem if
736 * outdir is not a directory spec, and the fopen() or open() will
737 * catch an error if there is no write access.
739 * Presumably, this problem could also be solved by using the DEC C
740 * routines to convert the directory syntax to Unixly, and give that
741 * to access(). However, time's too short to do that just now.
743 if (app_access(outdir, R_OK | W_OK | X_OK) != 0)
745 BIO_printf(bio_err, "I am unable to access the %s directory\n",
751 if (app_isdir(outdir) <= 0) {
752 BIO_printf(bio_err, "%s need to be a directory\n", outdir);
759 /*****************************************************************/
760 /* we need to load the database file */
761 if ((dbfile = NCONF_get_string(conf, section, ENV_DATABASE)) == NULL) {
762 lookup_fail(section, ENV_DATABASE);
765 db = load_index(dbfile, &db_attr);
769 /* Lets check some fields */
770 for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
771 pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
772 if ((pp[DB_type][0] != DB_TYPE_REV) && (pp[DB_rev_date][0] != '\0')) {
774 "entry %d: not revoked yet, but has a revocation date\n",
778 if ((pp[DB_type][0] == DB_TYPE_REV) &&
779 !make_revoked(NULL, pp[DB_rev_date])) {
780 BIO_printf(bio_err, " in entry %d\n", i + 1);
783 if (!check_time_format((char *)pp[DB_exp_date])) {
784 BIO_printf(bio_err, "entry %d: invalid expiry date\n", i + 1);
793 if ((j & 1) || (j < 2)) {
794 BIO_printf(bio_err, "entry %d: bad serial number length (%d)\n",
799 if (!(((*p >= '0') && (*p <= '9')) ||
800 ((*p >= 'A') && (*p <= 'F')) ||
801 ((*p >= 'a') && (*p <= 'f')))) {
803 "entry %d: bad serial number characters, char pos %ld, char is '%c'\n",
804 i + 1, (long)(p - pp[DB_serial]), *p);
811 TXT_DB_write(bio_out, db->db);
812 BIO_printf(bio_err, "%d entries loaded from the database\n",
813 sk_OPENSSL_PSTRING_num(db->db->data));
814 BIO_printf(bio_err, "generating index\n");
817 if (!index_index(db))
820 /*****************************************************************/
821 /* Update the db file for expired certificates */
824 BIO_printf(bio_err, "Updating %s ...\n", dbfile);
828 BIO_printf(bio_err, "Malloc failure\n");
832 BIO_printf(bio_err, "No entries found to mark expired\n");
834 if (!save_index(dbfile, "new", db))
837 if (!rotate_index(dbfile, "new", "old"))
842 "Done. %d entries marked as expired\n", i);
846 /*****************************************************************/
847 /* Read extensions config file */
849 extconf = NCONF_new(NULL);
850 if (NCONF_load(extconf, extfile, &errorline) <= 0) {
852 BIO_printf(bio_err, "ERROR: loading the config file '%s'\n",
856 "ERROR: on line %ld of config file '%s'\n",
863 BIO_printf(bio_err, "Successfully loaded extensions file %s\n",
866 /* We can have sections in the ext file */
869 NCONF_get_string(extconf, "default", "extensions")))
870 extensions = "default";
873 /*****************************************************************/
875 Sout = bio_open_default(outfile, "w");
880 if ((md == NULL) && ((md = NCONF_get_string(conf,
882 ENV_DEFAULT_MD)) == NULL)) {
883 lookup_fail(section, ENV_DEFAULT_MD);
887 if (!strcmp(md, "default")) {
889 if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) <= 0) {
890 BIO_puts(bio_err, "no default digest\n");
893 md = (char *)OBJ_nid2sn(def_nid);
896 if (!opt_md(md, &dgst)) {
901 if ((email_dn == 1) && ((tmp_email_dn = NCONF_get_string(conf,
903 ENV_DEFAULT_EMAIL_DN))
905 if (strcmp(tmp_email_dn, "no") == 0)
909 BIO_printf(bio_err, "message digest is %s\n",
910 OBJ_nid2ln(dgst->type));
911 if ((policy == NULL) && ((policy = NCONF_get_string(conf,
915 lookup_fail(section, ENV_POLICY);
919 BIO_printf(bio_err, "policy is %s\n", policy);
921 if ((serialfile = NCONF_get_string(conf, section, ENV_SERIAL))
923 lookup_fail(section, ENV_SERIAL);
929 * no '-extfile' option, so we look for extensions in the main
933 extensions = NCONF_get_string(conf, section, ENV_EXTENSIONS);
938 /* Check syntax of file */
940 X509V3_set_ctx_test(&ctx);
941 X509V3_set_nconf(&ctx, conf);
942 if (!X509V3_EXT_add_nconf(conf, &ctx, extensions, NULL)) {
944 "Error Loading extension section %s\n",
952 if (startdate == NULL) {
953 startdate = NCONF_get_string(conf, section,
954 ENV_DEFAULT_STARTDATE);
955 if (startdate == NULL)
958 if (startdate && !ASN1_TIME_set_string(NULL, startdate)) {
960 "start date is invalid, it should be YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ\n");
963 if (startdate == NULL)
966 if (enddate == NULL) {
967 enddate = NCONF_get_string(conf, section, ENV_DEFAULT_ENDDATE);
971 if (enddate && !ASN1_TIME_set_string(NULL, enddate)) {
973 "end date is invalid, it should be YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ\n");
978 if (!NCONF_get_number(conf, section, ENV_DEFAULT_DAYS, &days))
981 if (!enddate && (days == 0)) {
983 "cannot lookup how many days to certify for\n");
987 if ((serial = load_serial(serialfile, create_ser, NULL)) == NULL) {
988 BIO_printf(bio_err, "error while loading serial number\n");
992 if (BN_is_zero(serial))
993 BIO_printf(bio_err, "next serial number is 00\n");
995 if ((f = BN_bn2hex(serial)) == NULL)
997 BIO_printf(bio_err, "next serial number is %s\n", f);
1002 if ((attribs = NCONF_get_section(conf, policy)) == NULL) {
1003 BIO_printf(bio_err, "unable to find 'section' for %s\n", policy);
1007 if ((cert_sk = sk_X509_new_null()) == NULL) {
1008 BIO_printf(bio_err, "Memory allocation failure\n");
1011 if (spkac_file != NULL) {
1013 j = certify_spkac(&x, spkac_file, pkey, x509, dgst, sigopts,
1014 attribs, db, serial, subj, chtype, multirdn,
1015 email_dn, startdate, enddate, days, extensions,
1016 conf, verbose, certopt, nameopt, default_op,
1022 BIO_printf(bio_err, "\n");
1023 if (!BN_add_word(serial, 1))
1025 if (!sk_X509_push(cert_sk, x)) {
1026 BIO_printf(bio_err, "Memory allocation failure\n");
1035 if (ss_cert_file != NULL) {
1037 j = certify_cert(&x, ss_cert_file, pkey, x509, dgst, sigopts,
1039 db, serial, subj, chtype, multirdn, email_dn,
1040 startdate, enddate, days, batch, extensions,
1041 conf, verbose, certopt, nameopt, default_op,
1047 BIO_printf(bio_err, "\n");
1048 if (!BN_add_word(serial, 1))
1050 if (!sk_X509_push(cert_sk, x)) {
1051 BIO_printf(bio_err, "Memory allocation failure\n");
1056 if (infile != NULL) {
1058 j = certify(&x, infile, pkey, x509p, dgst, sigopts, attribs, db,
1059 serial, subj, chtype, multirdn, email_dn, startdate,
1060 enddate, days, batch, extensions, conf, verbose,
1061 certopt, nameopt, default_op, ext_copy, selfsign);
1066 BIO_printf(bio_err, "\n");
1067 if (!BN_add_word(serial, 1))
1069 if (!sk_X509_push(cert_sk, x)) {
1070 BIO_printf(bio_err, "Memory allocation failure\n");
1075 for (i = 0; i < argc; i++) {
1077 j = certify(&x, argv[i], pkey, x509p, dgst, sigopts, attribs, db,
1078 serial, subj, chtype, multirdn, email_dn, startdate,
1079 enddate, days, batch, extensions, conf, verbose,
1080 certopt, nameopt, default_op, ext_copy, selfsign);
1085 BIO_printf(bio_err, "\n");
1086 if (!BN_add_word(serial, 1))
1088 if (!sk_X509_push(cert_sk, x)) {
1089 BIO_printf(bio_err, "Memory allocation failure\n");
1095 * we have a stack of newly certified certificates and a data base
1096 * and serial number that need updating
1099 if (sk_X509_num(cert_sk) > 0) {
1102 "\n%d out of %d certificate requests certified, commit? [y/n]",
1104 (void)BIO_flush(bio_err);
1106 if (!fgets(buf[0], 10, stdin)) {
1108 "CERTIFICATION CANCELED: I/O error\n");
1112 if ((buf[0][0] != 'y') && (buf[0][0] != 'Y')) {
1113 BIO_printf(bio_err, "CERTIFICATION CANCELED\n");
1119 BIO_printf(bio_err, "Write out database with %d new entries\n",
1120 sk_X509_num(cert_sk));
1122 if (!save_serial(serialfile, "new", serial, NULL))
1125 if (!save_index(dbfile, "new", db))
1130 BIO_printf(bio_err, "writing new certificates\n");
1131 for (i = 0; i < sk_X509_num(cert_sk); i++) {
1135 x = sk_X509_value(cert_sk, i);
1137 j = x->cert_info->serialNumber->length;
1138 p = (const char *)x->cert_info->serialNumber->data;
1140 if (strlen(outdir) >= (size_t)(j ? BSIZE - j * 2 - 6 : BSIZE - 8)) {
1141 BIO_printf(bio_err, "certificate file name too long\n");
1145 strcpy(buf[2], outdir);
1147 #ifndef OPENSSL_SYS_VMS
1148 BUF_strlcat(buf[2], "/", sizeof(buf[2]));
1151 n = (char *)&(buf[2][strlen(buf[2])]);
1153 for (k = 0; k < j; k++) {
1154 if (n >= &(buf[2][sizeof(buf[2])]))
1157 &buf[2][0] + sizeof(buf[2]) - n,
1158 "%02X", (unsigned char)*(p++));
1171 BIO_printf(bio_err, "writing %s\n", buf[2]);
1173 Cout = BIO_new_file(buf[2], "w");
1178 write_new_certificate(Cout, x, 0, notext);
1179 write_new_certificate(Sout, x, output_der, notext);
1182 if (sk_X509_num(cert_sk)) {
1183 /* Rename the database and the serial file */
1184 if (!rotate_serial(serialfile, "new", "old"))
1187 if (!rotate_index(dbfile, "new", "old"))
1190 BIO_printf(bio_err, "Data Base Updated\n");
1194 /*****************************************************************/
1198 crl_ext = NCONF_get_string(conf, section, ENV_CRLEXT);
1203 /* Check syntax of file */
1205 X509V3_set_ctx_test(&ctx);
1206 X509V3_set_nconf(&ctx, conf);
1207 if (!X509V3_EXT_add_nconf(conf, &ctx, crl_ext, NULL)) {
1209 "Error Loading CRL extension section %s\n",
1216 if ((crlnumberfile = NCONF_get_string(conf, section, ENV_CRLNUMBER))
1218 if ((crlnumber = load_serial(crlnumberfile, 0, NULL)) == NULL) {
1219 BIO_printf(bio_err, "error while loading CRL number\n");
1223 if (!crldays && !crlhours && !crlsec) {
1224 if (!NCONF_get_number(conf, section,
1225 ENV_DEFAULT_CRL_DAYS, &crldays))
1227 if (!NCONF_get_number(conf, section,
1228 ENV_DEFAULT_CRL_HOURS, &crlhours))
1232 if ((crldays == 0) && (crlhours == 0) && (crlsec == 0)) {
1234 "cannot lookup how long until the next CRL is issued\n");
1239 BIO_printf(bio_err, "making CRL\n");
1240 if ((crl = X509_CRL_new()) == NULL)
1242 if (!X509_CRL_set_issuer_name(crl, X509_get_subject_name(x509)))
1245 tmptm = ASN1_TIME_new();
1248 X509_gmtime_adj(tmptm, 0);
1249 X509_CRL_set_lastUpdate(crl, tmptm);
1250 if (!X509_time_adj_ex(tmptm, crldays, crlhours * 60 * 60 + crlsec,
1252 BIO_puts(bio_err, "error setting CRL nextUpdate\n");
1255 X509_CRL_set_nextUpdate(crl, tmptm);
1257 ASN1_TIME_free(tmptm);
1259 for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
1260 pp = sk_OPENSSL_PSTRING_value(db->db->data, i);
1261 if (pp[DB_type][0] == DB_TYPE_REV) {
1262 if ((r = X509_REVOKED_new()) == NULL)
1264 j = make_revoked(r, pp[DB_rev_date]);
1269 if (!BN_hex2bn(&serial, pp[DB_serial]))
1271 tmpser = BN_to_ASN1_INTEGER(serial, NULL);
1276 X509_REVOKED_set_serialNumber(r, tmpser);
1277 ASN1_INTEGER_free(tmpser);
1278 X509_CRL_add0_revoked(crl, r);
1283 * sort the data so it will be written in serial number order
1287 /* we now have a CRL */
1289 BIO_printf(bio_err, "signing CRL\n");
1291 /* Add any extensions asked for */
1293 if (crl_ext || crlnumberfile != NULL) {
1295 X509V3_set_ctx(&crlctx, x509, NULL, NULL, crl, 0);
1296 X509V3_set_nconf(&crlctx, conf);
1299 if (!X509V3_EXT_CRL_add_nconf(conf, &crlctx, crl_ext, crl))
1301 if (crlnumberfile != NULL) {
1302 tmpser = BN_to_ASN1_INTEGER(crlnumber, NULL);
1305 X509_CRL_add1_ext_i2d(crl, NID_crl_number, tmpser, 0, 0);
1306 ASN1_INTEGER_free(tmpser);
1308 if (!BN_add_word(crlnumber, 1))
1312 if (crl_ext || crl_v2) {
1313 if (!X509_CRL_set_version(crl, 1))
1314 goto end; /* version 2 CRL */
1317 /* we have a CRL number that need updating */
1318 if (crlnumberfile != NULL)
1319 if (!save_serial(crlnumberfile, "new", crlnumber, NULL))
1327 if (!do_X509_CRL_sign(crl, pkey, dgst, sigopts))
1330 PEM_write_bio_X509_CRL(Sout, crl);
1332 if (crlnumberfile != NULL) /* Rename the crlnumber file */
1333 if (!rotate_serial(crlnumberfile, "new", "old"))
1337 /*****************************************************************/
1339 if (infile == NULL) {
1340 BIO_printf(bio_err, "no input files\n");
1344 revcert = load_cert(infile, FORMAT_PEM, NULL, e, infile);
1345 if (revcert == NULL)
1349 j = do_revoke(revcert, db, rev_type, rev_arg);
1354 if (!save_index(dbfile, "new", db))
1357 if (!rotate_index(dbfile, "new", "old"))
1360 BIO_printf(bio_err, "Data Base Updated\n");
1363 /*****************************************************************/
1367 OPENSSL_free(tofree);
1374 sk_X509_pop_free(cert_sk, X509_free);
1377 ERR_print_errors(bio_err);
1378 app_RAND_write_file(randfile);
1379 if (free_key && key)
1385 sk_OPENSSL_STRING_free(sigopts);
1386 EVP_PKEY_free(pkey);
1391 NCONF_free(extconf);
1396 static void lookup_fail(const char *name, const char *tag)
1398 BIO_printf(bio_err, "variable lookup failed for %s::%s\n", name, tag);
1401 static int certify(X509 **xret, char *infile, EVP_PKEY *pkey, X509 *x509,
1402 const EVP_MD *dgst, STACK_OF(OPENSSL_STRING) *sigopts,
1403 STACK_OF(CONF_VALUE) *policy, CA_DB *db,
1404 BIGNUM *serial, char *subj, unsigned long chtype,
1405 int multirdn, int email_dn, char *startdate, char *enddate,
1406 long days, int batch, char *ext_sect, CONF *lconf,
1407 int verbose, unsigned long certopt, unsigned long nameopt,
1408 int default_op, int ext_copy, int selfsign)
1410 X509_REQ *req = NULL;
1412 EVP_PKEY *pktmp = NULL;
1415 in = BIO_new_file(infile, "r");
1417 ERR_print_errors(bio_err);
1420 if ((req = PEM_read_bio_X509_REQ(in, NULL, NULL, NULL)) == NULL) {
1421 BIO_printf(bio_err, "Error reading certificate request in %s\n",
1426 X509_REQ_print(bio_err, req);
1428 BIO_printf(bio_err, "Check that the request matches the signature\n");
1430 if (selfsign && !X509_REQ_check_private_key(req, pkey)) {
1432 "Certificate request and CA private key do not match\n");
1436 if ((pktmp = X509_REQ_get_pubkey(req)) == NULL) {
1437 BIO_printf(bio_err, "error unpacking public key\n");
1440 i = X509_REQ_verify(req, pktmp);
1441 EVP_PKEY_free(pktmp);
1444 BIO_printf(bio_err, "Signature verification problems....\n");
1445 ERR_print_errors(bio_err);
1451 "Signature did not match the certificate request\n");
1452 ERR_print_errors(bio_err);
1455 BIO_printf(bio_err, "Signature ok\n");
1457 ok = do_body(xret, pkey, x509, dgst, sigopts, policy, db, serial, subj,
1458 chtype, multirdn, email_dn, startdate, enddate, days, batch,
1459 verbose, req, ext_sect, lconf, certopt, nameopt, default_op,
1460 ext_copy, selfsign);
1469 static int certify_cert(X509 **xret, char *infile, EVP_PKEY *pkey, X509 *x509,
1470 const EVP_MD *dgst, STACK_OF(OPENSSL_STRING) *sigopts,
1471 STACK_OF(CONF_VALUE) *policy, CA_DB *db,
1472 BIGNUM *serial, char *subj, unsigned long chtype,
1473 int multirdn, int email_dn, char *startdate,
1474 char *enddate, long days, int batch, char *ext_sect,
1475 CONF *lconf, int verbose, unsigned long certopt,
1476 unsigned long nameopt, int default_op, int ext_copy,
1480 X509_REQ *rreq = NULL;
1481 EVP_PKEY *pktmp = NULL;
1484 if ((req = load_cert(infile, FORMAT_PEM, NULL, e, infile)) == NULL)
1487 X509_print(bio_err, req);
1489 BIO_printf(bio_err, "Check that the request matches the signature\n");
1491 if ((pktmp = X509_get_pubkey(req)) == NULL) {
1492 BIO_printf(bio_err, "error unpacking public key\n");
1495 i = X509_verify(req, pktmp);
1496 EVP_PKEY_free(pktmp);
1499 BIO_printf(bio_err, "Signature verification problems....\n");
1504 BIO_printf(bio_err, "Signature did not match the certificate\n");
1507 BIO_printf(bio_err, "Signature ok\n");
1509 if ((rreq = X509_to_X509_REQ(req, NULL, EVP_md5())) == NULL)
1512 ok = do_body(xret, pkey, x509, dgst, sigopts, policy, db, serial, subj,
1513 chtype, multirdn, email_dn, startdate, enddate, days, batch,
1514 verbose, rreq, ext_sect, lconf, certopt, nameopt, default_op,
1519 X509_REQ_free(rreq);
1525 static int do_body(X509 **xret, EVP_PKEY *pkey, X509 *x509,
1526 const EVP_MD *dgst, STACK_OF(OPENSSL_STRING) *sigopts,
1527 STACK_OF(CONF_VALUE) *policy, CA_DB *db, BIGNUM *serial,
1528 char *subj, unsigned long chtype, int multirdn,
1529 int email_dn, char *startdate, char *enddate, long days,
1530 int batch, int verbose, X509_REQ *req, char *ext_sect,
1531 CONF *lconf, unsigned long certopt, unsigned long nameopt,
1532 int default_op, int ext_copy, int selfsign)
1534 X509_NAME *name = NULL, *CAname = NULL, *subject = NULL, *dn_subject =
1536 ASN1_UTCTIME *tm, *tmptm;
1537 ASN1_STRING *str, *str2;
1541 X509_NAME_ENTRY *ne;
1542 X509_NAME_ENTRY *tne, *push;
1544 int ok = -1, i, j, last, nid;
1547 OPENSSL_STRING row[DB_NUMBER];
1548 OPENSSL_STRING *irow = NULL;
1549 OPENSSL_STRING *rrow = NULL;
1552 tmptm = ASN1_UTCTIME_new();
1553 if (tmptm == NULL) {
1554 BIO_printf(bio_err, "malloc error\n");
1558 for (i = 0; i < DB_NUMBER; i++)
1562 X509_NAME *n = parse_name(subj, chtype, multirdn);
1565 ERR_print_errors(bio_err);
1568 X509_REQ_set_subject_name(req, n);
1569 req->req_info->enc.modified = 1;
1575 "The Subject's Distinguished Name is as follows\n");
1577 name = X509_REQ_get_subject_name(req);
1578 for (i = 0; i < X509_NAME_entry_count(name); i++) {
1579 ne = X509_NAME_get_entry(name, i);
1580 str = X509_NAME_ENTRY_get_data(ne);
1581 obj = X509_NAME_ENTRY_get_object(ne);
1584 /* assume all type should be strings */
1585 nid = OBJ_obj2nid(X509_NAME_ENTRY_get_object(ne));
1587 if (str->type == V_ASN1_UNIVERSALSTRING)
1588 ASN1_UNIVERSALSTRING_to_string(str);
1590 if ((str->type == V_ASN1_IA5STRING) &&
1591 (nid != NID_pkcs9_emailAddress))
1592 str->type = V_ASN1_T61STRING;
1594 if ((nid == NID_pkcs9_emailAddress) &&
1595 (str->type == V_ASN1_PRINTABLESTRING))
1596 str->type = V_ASN1_IA5STRING;
1599 /* If no EMAIL is wanted in the subject */
1600 if ((OBJ_obj2nid(obj) == NID_pkcs9_emailAddress) && (!email_dn))
1603 /* check some things */
1604 if ((OBJ_obj2nid(obj) == NID_pkcs9_emailAddress) &&
1605 (str->type != V_ASN1_IA5STRING)) {
1607 "\nemailAddress type needs to be of type IA5STRING\n");
1610 if ((str->type != V_ASN1_BMPSTRING)
1611 && (str->type != V_ASN1_UTF8STRING)) {
1612 j = ASN1_PRINTABLE_type(str->data, str->length);
1613 if (((j == V_ASN1_T61STRING) &&
1614 (str->type != V_ASN1_T61STRING)) ||
1615 ((j == V_ASN1_IA5STRING) &&
1616 (str->type == V_ASN1_PRINTABLESTRING))) {
1618 "\nThe string contains characters that are illegal for the ASN.1 type\n");
1624 old_entry_print(bio_err, obj, str);
1627 /* Ok, now we check the 'policy' stuff. */
1628 if ((subject = X509_NAME_new()) == NULL) {
1629 BIO_printf(bio_err, "Memory allocation failure\n");
1633 /* take a copy of the issuer name before we mess with it. */
1635 CAname = X509_NAME_dup(name);
1637 CAname = X509_NAME_dup(x509->cert_info->subject);
1642 for (i = 0; i < sk_CONF_VALUE_num(policy); i++) {
1643 cv = sk_CONF_VALUE_value(policy, i); /* get the object id */
1644 if ((j = OBJ_txt2nid(cv->name)) == NID_undef) {
1646 "%s:unknown object type in 'policy' configuration\n",
1650 obj = OBJ_nid2obj(j);
1654 /* lookup the object in the supplied name list */
1655 j = X509_NAME_get_index_by_OBJ(name, obj, last);
1661 tne = X509_NAME_get_entry(name, j);
1665 /* depending on the 'policy', decide what to do. */
1667 if (strcmp(cv->value, "optional") == 0) {
1670 } else if (strcmp(cv->value, "supplied") == 0) {
1673 "The %s field needed to be supplied and was missing\n",
1678 } else if (strcmp(cv->value, "match") == 0) {
1683 "The mandatory %s field was missing\n",
1691 j = X509_NAME_get_index_by_OBJ(CAname, obj, last2);
1692 if ((j < 0) && (last2 == -1)) {
1694 "The %s field does not exist in the CA certificate,\nthe 'policy' is misconfigured\n",
1699 push = X509_NAME_get_entry(CAname, j);
1700 str = X509_NAME_ENTRY_get_data(tne);
1701 str2 = X509_NAME_ENTRY_get_data(push);
1703 if (ASN1_STRING_cmp(str, str2) != 0)
1708 "The %s field needed to be the same in the\nCA certificate (%s) and the request (%s)\n",
1710 ((str2 == NULL) ? "NULL" : (char *)str2->data),
1711 ((str == NULL) ? "NULL" : (char *)str->data));
1716 "%s:invalid type in 'policy' configuration\n",
1722 if (!X509_NAME_add_entry(subject, push, -1, 0)) {
1724 X509_NAME_ENTRY_free(push);
1725 BIO_printf(bio_err, "Memory allocation failure\n");
1735 X509_NAME_free(subject);
1736 /* subject=X509_NAME_dup(X509_REQ_get_subject_name(req)); */
1737 subject = X509_NAME_dup(name);
1738 if (subject == NULL)
1744 "The subject name appears to be ok, checking data base for clashes\n");
1746 /* Build the correct Subject if no e-mail is wanted in the subject */
1748 * and add it later on because of the method extensions are added
1753 dn_subject = subject;
1755 X509_NAME_ENTRY *tmpne;
1757 * Its best to dup the subject DN and then delete any email addresses
1758 * because this retains its structure.
1760 if (!(dn_subject = X509_NAME_dup(subject))) {
1761 BIO_printf(bio_err, "Memory allocation failure\n");
1764 while ((i = X509_NAME_get_index_by_NID(dn_subject,
1765 NID_pkcs9_emailAddress,
1767 tmpne = X509_NAME_get_entry(dn_subject, i);
1768 X509_NAME_delete_entry(dn_subject, i);
1769 X509_NAME_ENTRY_free(tmpne);
1773 if (BN_is_zero(serial))
1774 row[DB_serial] = BUF_strdup("00");
1776 row[DB_serial] = BN_bn2hex(serial);
1777 if (row[DB_serial] == NULL) {
1778 BIO_printf(bio_err, "Memory allocation failure\n");
1782 if (db->attributes.unique_subject) {
1783 OPENSSL_STRING *crow = row;
1785 rrow = TXT_DB_get_by_index(db->db, DB_name, crow);
1788 "ERROR:There is already a certificate for %s\n",
1793 rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
1796 "ERROR:Serial number %s has already been issued,\n",
1799 " check the database/serial_file for corruption\n");
1804 BIO_printf(bio_err, "The matching entry has the following details\n");
1805 if (rrow[DB_type][0] == 'E')
1807 else if (rrow[DB_type][0] == 'R')
1809 else if (rrow[DB_type][0] == 'V')
1812 p = "\ninvalid type, Data base error\n";
1813 BIO_printf(bio_err, "Type :%s\n", p);;
1814 if (rrow[DB_type][0] == 'R') {
1815 p = rrow[DB_exp_date];
1818 BIO_printf(bio_err, "Was revoked on:%s\n", p);
1820 p = rrow[DB_exp_date];
1823 BIO_printf(bio_err, "Expires on :%s\n", p);
1824 p = rrow[DB_serial];
1827 BIO_printf(bio_err, "Serial Number :%s\n", p);
1831 BIO_printf(bio_err, "File name :%s\n", p);
1835 BIO_printf(bio_err, "Subject Name :%s\n", p);
1836 ok = -1; /* This is now a 'bad' error. */
1840 /* We are now totally happy, lets make and sign the certificate */
1843 "Everything appears to be ok, creating and signing the certificate\n");
1845 if ((ret = X509_new()) == NULL)
1847 ci = ret->cert_info;
1850 /* Make it an X509 v3 certificate. */
1851 if (!X509_set_version(ret, 2))
1855 if (BN_to_ASN1_INTEGER(serial, ci->serialNumber) == NULL)
1858 if (!X509_set_issuer_name(ret, subject))
1861 if (!X509_set_issuer_name(ret, X509_get_subject_name(x509)))
1865 if (strcmp(startdate, "today") == 0)
1866 X509_gmtime_adj(X509_get_notBefore(ret), 0);
1868 ASN1_TIME_set_string(X509_get_notBefore(ret), startdate);
1870 if (enddate == NULL)
1871 X509_time_adj_ex(X509_get_notAfter(ret), days, 0, NULL);
1874 ASN1_TIME_set_string(X509_get_notAfter(ret), enddate);
1875 ASN1_TIME_diff(&tdays, NULL, NULL, X509_get_notAfter(ret));
1879 if (!X509_set_subject_name(ret, subject))
1882 pktmp = X509_REQ_get_pubkey(req);
1883 i = X509_set_pubkey(ret, pktmp);
1884 EVP_PKEY_free(pktmp);
1888 /* Lets add the extensions, if there are any */
1891 if (ci->version == NULL)
1892 if ((ci->version = ASN1_INTEGER_new()) == NULL)
1894 ASN1_INTEGER_set(ci->version, 2); /* version 3 certificate */
1897 * Free the current entries if any, there should not be any I believe
1899 if (ci->extensions != NULL)
1900 sk_X509_EXTENSION_pop_free(ci->extensions, X509_EXTENSION_free);
1902 ci->extensions = NULL;
1904 /* Initialize the context structure */
1906 X509V3_set_ctx(&ctx, ret, ret, req, NULL, 0);
1908 X509V3_set_ctx(&ctx, x509, ret, req, NULL, 0);
1912 BIO_printf(bio_err, "Extra configuration file found\n");
1914 /* Use the extconf configuration db LHASH */
1915 X509V3_set_nconf(&ctx, extconf);
1917 /* Test the structure (needed?) */
1918 /* X509V3_set_ctx_test(&ctx); */
1920 /* Adds exts contained in the configuration file */
1921 if (!X509V3_EXT_add_nconf(extconf, &ctx, ext_sect, ret)) {
1923 "ERROR: adding extensions in section %s\n",
1925 ERR_print_errors(bio_err);
1930 "Successfully added extensions from file.\n");
1931 } else if (ext_sect) {
1932 /* We found extensions to be set from config file */
1933 X509V3_set_nconf(&ctx, lconf);
1935 if (!X509V3_EXT_add_nconf(lconf, &ctx, ext_sect, ret)) {
1937 "ERROR: adding extensions in section %s\n",
1939 ERR_print_errors(bio_err);
1945 "Successfully added extensions from config\n");
1949 /* Copy extensions from request (if any) */
1951 if (!copy_extensions(ret, req, ext_copy)) {
1952 BIO_printf(bio_err, "ERROR: adding extensions from request\n");
1953 ERR_print_errors(bio_err);
1957 /* Set the right value for the noemailDN option */
1958 if (email_dn == 0) {
1959 if (!X509_set_subject_name(ret, dn_subject))
1964 BIO_printf(bio_err, "Certificate Details:\n");
1966 * Never print signature details because signature not present
1968 certopt |= X509_FLAG_NO_SIGDUMP | X509_FLAG_NO_SIGNAME;
1969 X509_print_ex(bio_err, ret, nameopt, certopt);
1972 BIO_printf(bio_err, "Certificate is to be certified until ");
1973 ASN1_TIME_print(bio_err, X509_get_notAfter(ret));
1975 BIO_printf(bio_err, " (%ld days)", days);
1976 BIO_printf(bio_err, "\n");
1980 BIO_printf(bio_err, "Sign the certificate? [y/n]:");
1981 (void)BIO_flush(bio_err);
1983 if (!fgets(buf, sizeof(buf) - 1, stdin)) {
1985 "CERTIFICATE WILL NOT BE CERTIFIED: I/O error\n");
1989 if (!((buf[0] == 'y') || (buf[0] == 'Y'))) {
1990 BIO_printf(bio_err, "CERTIFICATE WILL NOT BE CERTIFIED\n");
1996 pktmp = X509_get_pubkey(ret);
1997 if (EVP_PKEY_missing_parameters(pktmp) &&
1998 !EVP_PKEY_missing_parameters(pkey))
1999 EVP_PKEY_copy_parameters(pktmp, pkey);
2000 EVP_PKEY_free(pktmp);
2002 if (!do_X509_sign(ret, pkey, dgst, sigopts))
2005 /* We now just add it to the database */
2006 row[DB_type] = (char *)OPENSSL_malloc(2);
2008 tm = X509_get_notAfter(ret);
2009 row[DB_exp_date] = (char *)OPENSSL_malloc(tm->length + 1);
2010 memcpy(row[DB_exp_date], tm->data, tm->length);
2011 row[DB_exp_date][tm->length] = '\0';
2013 row[DB_rev_date] = NULL;
2015 /* row[DB_serial] done already */
2016 row[DB_file] = (char *)OPENSSL_malloc(8);
2017 row[DB_name] = X509_NAME_oneline(X509_get_subject_name(ret), NULL, 0);
2019 if ((row[DB_type] == NULL) || (row[DB_exp_date] == NULL) ||
2020 (row[DB_file] == NULL) || (row[DB_name] == NULL)) {
2021 BIO_printf(bio_err, "Memory allocation failure\n");
2024 BUF_strlcpy(row[DB_file], "unknown", 8);
2025 row[DB_type][0] = 'V';
2026 row[DB_type][1] = '\0';
2029 (char **)OPENSSL_malloc(sizeof(char *) * (DB_NUMBER + 1))) == NULL) {
2030 BIO_printf(bio_err, "Memory allocation failure\n");
2034 for (i = 0; i < DB_NUMBER; i++) {
2038 irow[DB_NUMBER] = NULL;
2040 if (!TXT_DB_insert(db->db, irow)) {
2041 BIO_printf(bio_err, "failed to update database\n");
2042 BIO_printf(bio_err, "TXT_DB error number %ld\n", db->db->error);
2047 for (i = 0; i < DB_NUMBER; i++)
2049 OPENSSL_free(row[i]);
2052 X509_NAME_free(CAname);
2053 if (subject != NULL)
2054 X509_NAME_free(subject);
2055 if ((dn_subject != NULL) && !email_dn)
2056 X509_NAME_free(dn_subject);
2058 ASN1_UTCTIME_free(tmptm);
2068 static void write_new_certificate(BIO *bp, X509 *x, int output_der,
2073 (void)i2d_X509_bio(bp, x);
2078 PEM_write_bio_X509(bp, x);
2081 static int certify_spkac(X509 **xret, char *infile, EVP_PKEY *pkey,
2082 X509 *x509, const EVP_MD *dgst,
2083 STACK_OF(OPENSSL_STRING) *sigopts,
2084 STACK_OF(CONF_VALUE) *policy, CA_DB *db,
2085 BIGNUM *serial, char *subj, unsigned long chtype,
2086 int multirdn, int email_dn, char *startdate,
2087 char *enddate, long days, char *ext_sect,
2088 CONF *lconf, int verbose, unsigned long certopt,
2089 unsigned long nameopt, int default_op, int ext_copy)
2091 STACK_OF(CONF_VALUE) *sk = NULL;
2092 LHASH_OF(CONF_VALUE) *parms = NULL;
2093 X509_REQ *req = NULL;
2094 CONF_VALUE *cv = NULL;
2095 NETSCAPE_SPKI *spki = NULL;
2098 EVP_PKEY *pktmp = NULL;
2099 X509_NAME *n = NULL;
2100 X509_NAME_ENTRY *ne = NULL;
2106 * Load input file into a hash table. (This is just an easy
2107 * way to read and parse the file, then put it into a convenient
2110 parms = CONF_load(NULL, infile, &errline);
2111 if (parms == NULL) {
2112 BIO_printf(bio_err, "error on line %ld of %s\n", errline, infile);
2113 ERR_print_errors(bio_err);
2117 sk = CONF_get_section(parms, "default");
2118 if (sk_CONF_VALUE_num(sk) == 0) {
2119 BIO_printf(bio_err, "no name/value pairs found in %s\n", infile);
2125 * Now create a dummy X509 request structure. We don't actually
2126 * have an X509 request, but we have many of the components
2127 * (a public key, various DN components). The idea is that we
2128 * put these components into the right X509 request structure
2129 * and we can use the same code as if you had a real X509 request.
2131 req = X509_REQ_new();
2133 ERR_print_errors(bio_err);
2138 * Build up the subject name set.
2144 if (sk_CONF_VALUE_num(sk) <= i)
2147 cv = sk_CONF_VALUE_value(sk, i);
2150 * Skip past any leading X. X: X, etc to allow for multiple instances
2152 for (buf = cv->name; *buf; buf++)
2153 if ((*buf == ':') || (*buf == ',') || (*buf == '.')) {
2161 if ((nid = OBJ_txt2nid(type)) == NID_undef) {
2162 if (strcmp(type, "SPKAC") == 0) {
2163 spki = NETSCAPE_SPKI_b64_decode(cv->value, -1);
2166 "unable to load Netscape SPKAC structure\n");
2167 ERR_print_errors(bio_err);
2174 if (!X509_NAME_add_entry_by_NID(n, nid, chtype,
2175 (unsigned char *)buf, -1, -1, 0))
2179 BIO_printf(bio_err, "Netscape SPKAC structure not found in %s\n",
2185 * Now extract the key from the SPKI structure.
2189 "Check that the SPKAC request matches the signature\n");
2191 if ((pktmp = NETSCAPE_SPKI_get_pubkey(spki)) == NULL) {
2192 BIO_printf(bio_err, "error unpacking SPKAC public key\n");
2196 j = NETSCAPE_SPKI_verify(spki, pktmp);
2199 "signature verification failed on SPKAC public key\n");
2202 BIO_printf(bio_err, "Signature ok\n");
2204 X509_REQ_set_pubkey(req, pktmp);
2205 EVP_PKEY_free(pktmp);
2206 ok = do_body(xret, pkey, x509, dgst, sigopts, policy, db, serial, subj,
2207 chtype, multirdn, email_dn, startdate, enddate, days, 1,
2208 verbose, req, ext_sect, lconf, certopt, nameopt, default_op,
2216 NETSCAPE_SPKI_free(spki);
2218 X509_NAME_ENTRY_free(ne);
2223 static int check_time_format(const char *str)
2225 return ASN1_TIME_set_string(NULL, str);
2228 static int do_revoke(X509 *x509, CA_DB *db, int type, char *value)
2230 ASN1_UTCTIME *tm = NULL;
2231 char *row[DB_NUMBER], **rrow, **irow;
2232 char *rev_str = NULL;
2236 for (i = 0; i < DB_NUMBER; i++)
2238 row[DB_name] = X509_NAME_oneline(X509_get_subject_name(x509), NULL, 0);
2239 bn = ASN1_INTEGER_to_BN(X509_get_serialNumber(x509), NULL);
2243 row[DB_serial] = BUF_strdup("00");
2245 row[DB_serial] = BN_bn2hex(bn);
2247 if ((row[DB_name] == NULL) || (row[DB_serial] == NULL)) {
2248 BIO_printf(bio_err, "Memory allocation failure\n");
2252 * We have to lookup by serial number because name lookup skips revoked
2255 rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
2258 "Adding Entry with serial number %s to DB for %s\n",
2259 row[DB_serial], row[DB_name]);
2261 /* We now just add it to the database */
2262 row[DB_type] = (char *)OPENSSL_malloc(2);
2264 tm = X509_get_notAfter(x509);
2265 row[DB_exp_date] = (char *)OPENSSL_malloc(tm->length + 1);
2266 memcpy(row[DB_exp_date], tm->data, tm->length);
2267 row[DB_exp_date][tm->length] = '\0';
2269 row[DB_rev_date] = NULL;
2271 /* row[DB_serial] done already */
2272 row[DB_file] = (char *)OPENSSL_malloc(8);
2274 /* row[DB_name] done already */
2276 if ((row[DB_type] == NULL) || (row[DB_exp_date] == NULL) ||
2277 (row[DB_file] == NULL)) {
2278 BIO_printf(bio_err, "Memory allocation failure\n");
2281 BUF_strlcpy(row[DB_file], "unknown", 8);
2282 row[DB_type][0] = 'V';
2283 row[DB_type][1] = '\0';
2286 (char **)OPENSSL_malloc(sizeof(char *) * (DB_NUMBER + 1))) ==
2288 BIO_printf(bio_err, "Memory allocation failure\n");
2292 for (i = 0; i < DB_NUMBER; i++) {
2296 irow[DB_NUMBER] = NULL;
2298 if (!TXT_DB_insert(db->db, irow)) {
2299 BIO_printf(bio_err, "failed to update database\n");
2300 BIO_printf(bio_err, "TXT_DB error number %ld\n", db->db->error);
2304 /* Revoke Certificate */
2308 ok = do_revoke(x509, db, type, value);
2312 } else if (index_name_cmp_noconst(row, rrow)) {
2313 BIO_printf(bio_err, "ERROR:name does not match %s\n", row[DB_name]);
2315 } else if (type == -1) {
2316 BIO_printf(bio_err, "ERROR:Already present, serial number %s\n",
2319 } else if (rrow[DB_type][0] == 'R') {
2320 BIO_printf(bio_err, "ERROR:Already revoked, serial number %s\n",
2324 BIO_printf(bio_err, "Revoking Certificate %s.\n", rrow[DB_serial]);
2325 rev_str = make_revocation_str(type, value);
2327 BIO_printf(bio_err, "Error in revocation arguments\n");
2330 rrow[DB_type][0] = 'R';
2331 rrow[DB_type][1] = '\0';
2332 rrow[DB_rev_date] = rev_str;
2336 for (i = 0; i < DB_NUMBER; i++) {
2338 OPENSSL_free(row[i]);
2343 static int get_certificate_status(const char *serial, CA_DB *db)
2345 char *row[DB_NUMBER], **rrow;
2348 /* Free Resources */
2349 for (i = 0; i < DB_NUMBER; i++)
2352 /* Malloc needed char spaces */
2353 row[DB_serial] = OPENSSL_malloc(strlen(serial) + 2);
2354 if (row[DB_serial] == NULL) {
2355 BIO_printf(bio_err, "Malloc failure\n");
2359 if (strlen(serial) % 2) {
2361 * Set the first char to 0
2363 row[DB_serial][0] = '0';
2365 /* Copy String from serial to row[DB_serial] */
2366 memcpy(row[DB_serial] + 1, serial, strlen(serial));
2367 row[DB_serial][strlen(serial) + 1] = '\0';
2369 /* Copy String from serial to row[DB_serial] */
2370 memcpy(row[DB_serial], serial, strlen(serial));
2371 row[DB_serial][strlen(serial)] = '\0';
2374 /* Make it Upper Case */
2375 for (i = 0; row[DB_serial][i] != '\0'; i++)
2376 row[DB_serial][i] = toupper((unsigned char)row[DB_serial][i]);
2380 /* Search for the certificate */
2381 rrow = TXT_DB_get_by_index(db->db, DB_serial, row);
2383 BIO_printf(bio_err, "Serial %s not present in db.\n", row[DB_serial]);
2386 } else if (rrow[DB_type][0] == 'V') {
2387 BIO_printf(bio_err, "%s=Valid (%c)\n",
2388 row[DB_serial], rrow[DB_type][0]);
2390 } else if (rrow[DB_type][0] == 'R') {
2391 BIO_printf(bio_err, "%s=Revoked (%c)\n",
2392 row[DB_serial], rrow[DB_type][0]);
2394 } else if (rrow[DB_type][0] == 'E') {
2395 BIO_printf(bio_err, "%s=Expired (%c)\n",
2396 row[DB_serial], rrow[DB_type][0]);
2398 } else if (rrow[DB_type][0] == 'S') {
2399 BIO_printf(bio_err, "%s=Suspended (%c)\n",
2400 row[DB_serial], rrow[DB_type][0]);
2403 BIO_printf(bio_err, "%s=Unknown (%c).\n",
2404 row[DB_serial], rrow[DB_type][0]);
2408 for (i = 0; i < DB_NUMBER; i++) {
2410 OPENSSL_free(row[i]);
2415 static int do_updatedb(CA_DB *db)
2417 ASN1_UTCTIME *a_tm = NULL;
2419 int db_y2k, a_y2k; /* flags = 1 if y >= 2000 */
2420 char **rrow, *a_tm_s;
2422 a_tm = ASN1_UTCTIME_new();
2424 /* get actual time and make a string */
2425 a_tm = X509_gmtime_adj(a_tm, 0);
2426 a_tm_s = (char *)OPENSSL_malloc(a_tm->length + 1);
2427 if (a_tm_s == NULL) {
2432 memcpy(a_tm_s, a_tm->data, a_tm->length);
2433 a_tm_s[a_tm->length] = '\0';
2435 if (strncmp(a_tm_s, "49", 2) <= 0)
2440 for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) {
2441 rrow = sk_OPENSSL_PSTRING_value(db->db->data, i);
2443 if (rrow[DB_type][0] == 'V') {
2444 /* ignore entries that are not valid */
2445 if (strncmp(rrow[DB_exp_date], "49", 2) <= 0)
2450 if (db_y2k == a_y2k) {
2451 /* all on the same y2k side */
2452 if (strcmp(rrow[DB_exp_date], a_tm_s) <= 0) {
2453 rrow[DB_type][0] = 'E';
2454 rrow[DB_type][1] = '\0';
2457 BIO_printf(bio_err, "%s=Expired\n", rrow[DB_serial]);
2459 } else if (db_y2k < a_y2k) {
2460 rrow[DB_type][0] = 'E';
2461 rrow[DB_type][1] = '\0';
2464 BIO_printf(bio_err, "%s=Expired\n", rrow[DB_serial]);
2472 ASN1_UTCTIME_free(a_tm);
2473 OPENSSL_free(a_tm_s);
2478 static const char *crl_reasons[] = {
2479 /* CRL reason strings */
2483 "affiliationChanged",
2485 "cessationOfOperation",
2488 /* Additional pseudo reasons */
2494 #define NUM_REASONS (sizeof(crl_reasons) / sizeof(char *))
2497 * Given revocation information convert to a DB string. The format of the
2498 * string is: revtime[,reason,extra]. Where 'revtime' is the revocation time
2499 * (the current time). 'reason' is the optional CRL reason and 'extra' is any
2500 * additional argument
2503 char *make_revocation_str(int rev_type, char *rev_arg)
2505 char *other = NULL, *str;
2506 const char *reason = NULL;
2508 ASN1_UTCTIME *revtm = NULL;
2514 case REV_CRL_REASON:
2515 for (i = 0; i < 8; i++) {
2516 if (!strcasecmp(rev_arg, crl_reasons[i])) {
2517 reason = crl_reasons[i];
2521 if (reason == NULL) {
2522 BIO_printf(bio_err, "Unknown CRL reason %s\n", rev_arg);
2528 /* Argument is an OID */
2530 otmp = OBJ_txt2obj(rev_arg, 0);
2531 ASN1_OBJECT_free(otmp);
2534 BIO_printf(bio_err, "Invalid object identifier %s\n", rev_arg);
2538 reason = "holdInstruction";
2542 case REV_KEY_COMPROMISE:
2543 case REV_CA_COMPROMISE:
2545 /* Argument is the key compromise time */
2546 if (!ASN1_GENERALIZEDTIME_set_string(NULL, rev_arg)) {
2548 "Invalid time format %s. Need YYYYMMDDHHMMSSZ\n",
2553 if (rev_type == REV_KEY_COMPROMISE)
2556 reason = "CAkeyTime";
2562 revtm = X509_gmtime_adj(NULL, 0);
2567 i = revtm->length + 1;
2570 i += strlen(reason) + 1;
2572 i += strlen(other) + 1;
2574 str = OPENSSL_malloc(i);
2579 BUF_strlcpy(str, (char *)revtm->data, i);
2581 BUF_strlcat(str, ",", i);
2582 BUF_strlcat(str, reason, i);
2585 BUF_strlcat(str, ",", i);
2586 BUF_strlcat(str, other, i);
2588 ASN1_UTCTIME_free(revtm);
2593 * Convert revocation field to X509_REVOKED entry
2597 * 2 OK and some extensions added (i.e. V2 CRL)
2600 int make_revoked(X509_REVOKED *rev, const char *str)
2603 int reason_code = -1;
2605 ASN1_OBJECT *hold = NULL;
2606 ASN1_GENERALIZEDTIME *comp_time = NULL;
2607 ASN1_ENUMERATED *rtmp = NULL;
2609 ASN1_TIME *revDate = NULL;
2611 i = unpack_revinfo(&revDate, &reason_code, &hold, &comp_time, str);
2616 if (rev && !X509_REVOKED_set_revocationDate(rev, revDate))
2619 if (rev && (reason_code != OCSP_REVOKED_STATUS_NOSTATUS)) {
2620 rtmp = ASN1_ENUMERATED_new();
2621 if (!rtmp || !ASN1_ENUMERATED_set(rtmp, reason_code))
2623 if (!X509_REVOKED_add1_ext_i2d(rev, NID_crl_reason, rtmp, 0, 0))
2627 if (rev && comp_time) {
2628 if (!X509_REVOKED_add1_ext_i2d
2629 (rev, NID_invalidity_date, comp_time, 0, 0))
2633 if (!X509_REVOKED_add1_ext_i2d
2634 (rev, NID_hold_instruction_code, hold, 0, 0))
2638 if (reason_code != OCSP_REVOKED_STATUS_NOSTATUS)
2647 ASN1_OBJECT_free(hold);
2648 ASN1_GENERALIZEDTIME_free(comp_time);
2649 ASN1_ENUMERATED_free(rtmp);
2650 ASN1_TIME_free(revDate);
2655 int old_entry_print(BIO *bp, ASN1_OBJECT *obj, ASN1_STRING *str)
2657 char buf[25], *pbuf, *p;
2659 j = i2a_ASN1_OBJECT(bp, obj);
2661 for (j = 22 - j; j > 0; j--)
2667 if (str->type == V_ASN1_PRINTABLESTRING)
2668 BIO_printf(bp, "PRINTABLE:'");
2669 else if (str->type == V_ASN1_T61STRING)
2670 BIO_printf(bp, "T61STRING:'");
2671 else if (str->type == V_ASN1_IA5STRING)
2672 BIO_printf(bp, "IA5STRING:'");
2673 else if (str->type == V_ASN1_UNIVERSALSTRING)
2674 BIO_printf(bp, "UNIVERSALSTRING:'");
2676 BIO_printf(bp, "ASN.1 %2d:'", str->type);
2678 p = (char *)str->data;
2679 for (j = str->length; j > 0; j--) {
2680 if ((*p >= ' ') && (*p <= '~'))
2681 BIO_printf(bp, "%c", *p);
2683 BIO_printf(bp, "\\0x%02X", *p);
2684 else if ((unsigned char)*p == 0xf7)
2685 BIO_printf(bp, "^?");
2687 BIO_printf(bp, "^%c", *p + '@');
2690 BIO_printf(bp, "'\n");
2694 int unpack_revinfo(ASN1_TIME **prevtm, int *preason, ASN1_OBJECT **phold,
2695 ASN1_GENERALIZEDTIME **pinvtm, const char *str)
2698 char *rtime_str, *reason_str = NULL, *arg_str = NULL, *p;
2699 int reason_code = -1;
2702 ASN1_OBJECT *hold = NULL;
2703 ASN1_GENERALIZEDTIME *comp_time = NULL;
2705 tmp = BUF_strdup(str);
2707 BIO_printf(bio_err, "memory allocation failure\n");
2711 p = strchr(tmp, ',');
2727 *prevtm = ASN1_UTCTIME_new();
2729 BIO_printf(bio_err, "memory allocation failure\n");
2732 if (!ASN1_UTCTIME_set_string(*prevtm, rtime_str)) {
2733 BIO_printf(bio_err, "invalid revocation date %s\n", rtime_str);
2738 for (i = 0; i < NUM_REASONS; i++) {
2739 if (!strcasecmp(reason_str, crl_reasons[i])) {
2744 if (reason_code == OCSP_REVOKED_STATUS_NOSTATUS) {
2745 BIO_printf(bio_err, "invalid reason code %s\n", reason_str);
2749 if (reason_code == 7)
2750 reason_code = OCSP_REVOKED_STATUS_REMOVEFROMCRL;
2751 else if (reason_code == 8) { /* Hold instruction */
2753 BIO_printf(bio_err, "missing hold instruction\n");
2756 reason_code = OCSP_REVOKED_STATUS_CERTIFICATEHOLD;
2757 hold = OBJ_txt2obj(arg_str, 0);
2760 BIO_printf(bio_err, "invalid object identifier %s\n",
2766 } else if ((reason_code == 9) || (reason_code == 10)) {
2768 BIO_printf(bio_err, "missing compromised time\n");
2771 comp_time = ASN1_GENERALIZEDTIME_new();
2773 BIO_printf(bio_err, "memory allocation failure\n");
2776 if (!ASN1_GENERALIZEDTIME_set_string(comp_time, arg_str)) {
2777 BIO_printf(bio_err, "invalid compromised time %s\n", arg_str);
2780 if (reason_code == 9)
2781 reason_code = OCSP_REVOKED_STATUS_KEYCOMPROMISE;
2783 reason_code = OCSP_REVOKED_STATUS_CACOMPROMISE;
2788 *preason = reason_code;
2790 *pinvtm = comp_time;
2792 ASN1_GENERALIZEDTIME_free(comp_time);
2801 ASN1_OBJECT_free(hold);
2803 ASN1_GENERALIZEDTIME_free(comp_time);