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 */
147 static void lookup_fail(const char *name
, const char *tag
);
148 static int certify(X509
**xret
, char *infile
, EVP_PKEY
*pkey
, X509
*x509
,
149 const EVP_MD
*dgst
, STACK_OF(OPENSSL_STRING
) *sigopts
,
150 STACK_OF(CONF_VALUE
) *policy
, CA_DB
*db
,
151 BIGNUM
*serial
, char *subj
, unsigned long chtype
,
152 int multirdn
, int email_dn
, char *startdate
, char *enddate
,
153 long days
, int batch
, char *ext_sect
, CONF
*conf
,
154 int verbose
, unsigned long certopt
, unsigned long nameopt
,
155 int default_op
, int ext_copy
, int selfsign
);
156 static int certify_cert(X509
**xret
, char *infile
, EVP_PKEY
*pkey
, X509
*x509
,
157 const EVP_MD
*dgst
, STACK_OF(OPENSSL_STRING
) *sigopts
,
158 STACK_OF(CONF_VALUE
) *policy
, CA_DB
*db
,
159 BIGNUM
*serial
, char *subj
, unsigned long chtype
,
160 int multirdn
, int email_dn
, char *startdate
,
161 char *enddate
, long days
, int batch
, char *ext_sect
,
162 CONF
*conf
, int verbose
, unsigned long certopt
,
163 unsigned long nameopt
, int default_op
, int ext_copy
,
165 static int certify_spkac(X509
**xret
, char *infile
, EVP_PKEY
*pkey
,
166 X509
*x509
, const EVP_MD
*dgst
,
167 STACK_OF(OPENSSL_STRING
) *sigopts
,
168 STACK_OF(CONF_VALUE
) *policy
, CA_DB
*db
,
169 BIGNUM
*serial
, char *subj
, unsigned long chtype
,
170 int multirdn
, int email_dn
, char *startdate
,
171 char *enddate
, long days
, char *ext_sect
, CONF
*conf
,
172 int verbose
, unsigned long certopt
,
173 unsigned long nameopt
, int default_op
, int ext_copy
);
174 static void write_new_certificate(BIO
*bp
, X509
*x
, int output_der
,
176 static int do_body(X509
**xret
, EVP_PKEY
*pkey
, X509
*x509
,
177 const EVP_MD
*dgst
, STACK_OF(OPENSSL_STRING
) *sigopts
,
178 STACK_OF(CONF_VALUE
) *policy
, CA_DB
*db
, BIGNUM
*serial
,
179 char *subj
, unsigned long chtype
, int multirdn
,
180 int email_dn
, char *startdate
, char *enddate
, long days
,
181 int batch
, int verbose
, X509_REQ
*req
, char *ext_sect
,
182 CONF
*conf
, unsigned long certopt
, unsigned long nameopt
,
183 int default_op
, int ext_copy
, int selfsign
);
184 static int do_revoke(X509
*x509
, CA_DB
*db
, int ext
, char *extval
);
185 static int get_certificate_status(const char *ser_status
, CA_DB
*db
);
186 static int do_updatedb(CA_DB
*db
);
187 static int check_time_format(const char *str
);
188 char *make_revocation_str(int rev_type
, char *rev_arg
);
189 int make_revoked(X509_REVOKED
*rev
, const char *str
);
190 static int old_entry_print(ASN1_OBJECT
*obj
, ASN1_STRING
*str
);
192 static CONF
*conf
= NULL
;
193 static CONF
*extconf
= NULL
;
194 static char *section
= NULL
;
195 static int preserve
= 0;
196 static int msie_hack
= 0;
198 typedef enum OPTION_choice
{
199 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
200 OPT_ENGINE
, OPT_VERBOSE
, OPT_CONFIG
, OPT_NAME
, OPT_SUBJ
, OPT_UTF8
,
201 OPT_CREATE_SERIAL
, OPT_MULTIVALUE_RDN
, OPT_STARTDATE
, OPT_ENDDATE
,
202 OPT_DAYS
, OPT_MD
, OPT_POLICY
, OPT_KEYFILE
, OPT_KEYFORM
, OPT_PASSIN
,
203 OPT_KEY
, OPT_CERT
, OPT_SELFSIGN
, OPT_IN
, OPT_OUT
, OPT_OUTDIR
,
204 OPT_SIGOPT
, OPT_NOTEXT
, OPT_BATCH
, OPT_PRESERVEDN
, OPT_NOEMAILDN
,
205 OPT_GENCRL
, OPT_MSIE_HACK
, OPT_CRLDAYS
, OPT_CRLHOURS
, OPT_CRLSEC
,
206 OPT_INFILES
, OPT_SS_CERT
, OPT_SPKAC
, OPT_REVOKE
, OPT_VALID
,
207 OPT_EXTENSIONS
, OPT_EXTFILE
, OPT_STATUS
, OPT_UPDATEDB
, OPT_CRLEXTS
,
208 OPT_CRL_REASON
, OPT_CRL_HOLD
, OPT_CRL_COMPROMISE
,
209 OPT_CRL_CA_COMPROMISE
212 OPTIONS ca_options
[] = {
213 {"help", OPT_HELP
, '-', "Display this summary"},
214 {"verbose", OPT_VERBOSE
, '-', "Verbose output during processing"},
215 {"config", OPT_CONFIG
, 's', "A config file"},
216 {"name", OPT_NAME
, 's', "The particular CA definition to use"},
217 {"subj", OPT_SUBJ
, 's', "Use arg instead of request's subject"},
218 {"utf8", OPT_UTF8
, '-', "Input characters are UTF8 (default ASCII)"},
219 {"create_serial", OPT_CREATE_SERIAL
, '-'},
220 {"multivalue-rdn", OPT_MULTIVALUE_RDN
, '-',
221 "Enable support for multivalued RDNs"},
222 {"startdate", OPT_STARTDATE
, 's', "Cert notBefore, YYMMDDHHMMSSZ"},
223 {"enddate", OPT_ENDDATE
, 's',
224 "YYMMDDHHMMSSZ cert notAfter (overrides -days)"},
225 {"days", OPT_DAYS
, 'p', "Number of days to certify the cert for"},
226 {"md", OPT_MD
, 's', "md to use; one of md2, md5, sha or sha1"},
227 {"policy", OPT_POLICY
, 's', "The CA 'policy' to support"},
228 {"keyfile", OPT_KEYFILE
, '<', "Private key file"},
229 {"keyform", OPT_KEYFORM
, 'f', "Private key file format (PEM or ENGINE)"},
230 {"passin", OPT_PASSIN
, 's'},
231 {"key", OPT_KEY
, 's', "Key to decode the private key if it is encrypted"},
232 {"cert", OPT_CERT
, '<', "The CA cert"},
233 {"selfsign", OPT_SELFSIGN
, '-',
234 "Sign a cert with the key associated with it"},
235 {"in", OPT_IN
, '<', "The input PEM encoded cert request(s)"},
236 {"out", OPT_OUT
, '>', "Where to put the output file(s)"},
237 {"outdir", OPT_OUTDIR
, '/', "Where to put output cert"},
238 {"sigopt", OPT_SIGOPT
, 's'},
239 {"notext", OPT_NOTEXT
, '-'},
240 {"batch", OPT_BATCH
, '-', "Don't ask questions"},
241 {"preserveDN", OPT_PRESERVEDN
, '-', "Don't re-order the DN"},
242 {"noemailDN", OPT_NOEMAILDN
, '-', "Don't add the EMAIL field to the DN"},
243 {"gencrl", OPT_GENCRL
, '-', "Generate a new CRL"},
244 {"msie_hack", OPT_MSIE_HACK
, '-',
245 "msie modifications to handle all those universal strings"},
246 {"crldays", OPT_CRLDAYS
, 'p', "Days is when the next CRL is due"},
247 {"crlhours", OPT_CRLHOURS
, 'p', "Hours is when the next CRL is due"},
248 {"crlsec", OPT_CRLSEC
, 'p'},
249 {"infiles", OPT_INFILES
, '-', "The last argument, requests to process"},
250 {"ss_cert", OPT_SS_CERT
, '<', "File contains a self signed cert to sign"},
251 {"spkac", OPT_SPKAC
, '<',
252 "File contains DN and signed public key and challenge"},
253 {"revoke", OPT_REVOKE
, '<', "Revoke a cert (given in file)"},
254 {"valid", OPT_VALID
, 's'},
255 {"extensions", OPT_EXTENSIONS
, 's',
256 "Extension section (override value in config file)"},
257 {"extfile", OPT_EXTFILE
, '<',
258 "Configuration file with X509v3 extensions to add"},
259 {"status", OPT_STATUS
, 's', "Shows cert status given the serial number"},
260 {"updatedb", OPT_UPDATEDB
, '-', "Updates db for expired cert"},
261 {"crlexts", OPT_CRLEXTS
, 's',
262 "CRL extension section (override value in config file)"},
263 {"crl_reason", OPT_CRL_REASON
, 's'},
264 {"crl_hold", OPT_CRL_HOLD
, 's'},
265 {"crl_compromise", OPT_CRL_COMPROMISE
, 's'},
266 {"crl_CA_compromise", OPT_CRL_CA_COMPROMISE
, 's'},
267 #ifndef OPENSSL_NO_ENGINE
268 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
273 int ca_main(int argc
, char **argv
)
276 BIGNUM
*crlnumber
= NULL
, *serial
= NULL
;
277 EVP_PKEY
*pkey
= NULL
;
278 BIO
*in
= NULL
, *out
= NULL
, *Sout
= NULL
, *Cout
= NULL
;
279 ASN1_INTEGER
*tmpser
;
283 STACK_OF(CONF_VALUE
) *attribs
= NULL
;
284 STACK_OF(OPENSSL_STRING
) *sigopts
= NULL
;
285 STACK_OF(X509
) *cert_sk
= NULL
;
286 X509_CRL
*crl
= NULL
;
287 const EVP_MD
*dgst
= NULL
;
288 char *configfile
= NULL
, *md
= NULL
, *policy
= NULL
, *keyfile
= NULL
;
289 char *certfile
= NULL
, *crl_ext
= NULL
, *crlnumberfile
= NULL
;
290 char *infile
= NULL
, *spkac_file
= NULL
, *ss_cert_file
= NULL
;
291 char *extensions
= NULL
, *extfile
= NULL
, *key
= NULL
, *passinarg
= NULL
;
292 char *outdir
= NULL
, *outfile
= NULL
, *rev_arg
= NULL
, *ser_status
= NULL
;
293 char *serialfile
= NULL
, *startdate
= NULL
, *subj
= NULL
;
294 char *prog
, *enddate
= NULL
, *tmp_email_dn
= NULL
;
295 char *dbfile
= NULL
, *f
, *randfile
= NULL
, *tofree
= NULL
;
299 int create_ser
= 0, free_key
= 0, total
= 0, total_done
= 0;
300 int batch
= 0, default_op
= 1, doupdatedb
= 0, ext_copy
= EXT_COPY_NONE
;
301 int keyformat
= FORMAT_PEM
, multirdn
= 0, notext
= 0, output_der
= 0;
302 int ret
= 1, email_dn
= 1, req
= 0, verbose
= 0, gencrl
= 0, dorevoke
= 0;
303 int i
, j
, rev_type
= REV_NONE
, selfsign
= 0;
304 long crldays
= 0, crlhours
= 0, crlsec
= 0, errorline
= -1, days
= 0;
305 unsigned long chtype
= MBSTRING_ASC
, nameopt
= 0, certopt
= 0;
306 X509
*x509
= NULL
, *x509p
= NULL
, *x
= NULL
;
307 X509_REVOKED
*r
= NULL
;
315 prog
= opt_init(argc
, argv
, ca_options
);
316 while ((o
= opt_next()) != OPT_EOF
) {
321 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
324 opt_help(ca_options
);
337 configfile
= opt_arg();
347 chtype
= MBSTRING_UTF8
;
349 case OPT_CREATE_SERIAL
:
352 case OPT_MULTIVALUE_RDN
:
356 startdate
= opt_arg();
362 days
= atoi(opt_arg());
374 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &keyformat
))
378 passinarg
= opt_arg();
384 certfile
= opt_arg();
394 sigopts
= sk_OPENSSL_STRING_new_null();
396 || !sk_OPENSSL_STRING_push(sigopts
, opt_arg()))
418 crldays
= atol(opt_arg());
421 crlhours
= atol(opt_arg());
424 crlsec
= atol(opt_arg());
430 ss_cert_file
= opt_arg();
434 spkac_file
= opt_arg();
446 extensions
= opt_arg();
452 ser_status
= opt_arg();
462 rev_type
= REV_CRL_REASON
;
468 case OPT_CRL_COMPROMISE
:
470 rev_type
= REV_KEY_COMPROMISE
;
472 case OPT_CRL_CA_COMPROMISE
:
474 rev_type
= REV_CA_COMPROMISE
;
477 e
= setup_engine(opt_arg(), 0);
482 argc
= opt_num_rest();
486 if (configfile
== NULL
)
487 configfile
= getenv("OPENSSL_CONF");
488 if (configfile
== NULL
)
489 configfile
= getenv("SSLEAY_CONF");
490 if (configfile
== NULL
) {
491 const char *s
= X509_get_default_cert_area();
494 #ifdef OPENSSL_SYS_VMS
495 len
= strlen(s
) + sizeof(CONFIG_FILE
);
496 tofree
= OPENSSL_malloc(len
);
498 BIO_printf(bio_err
, "Out of memory\n");
503 len
= strlen(s
) + sizeof(CONFIG_FILE
) + 1;
504 tofree
= OPENSSL_malloc(len
);
506 BIO_printf(bio_err
, "Out of memory\n");
509 BUF_strlcpy(tofree
, s
, len
);
510 BUF_strlcat(tofree
, "/", len
);
512 BUF_strlcat(tofree
, CONFIG_FILE
, len
);
516 BIO_printf(bio_err
, "Using configuration from %s\n", configfile
);
517 conf
= NCONF_new(NULL
);
518 if (NCONF_load(conf
, configfile
, &errorline
) <= 0) {
520 BIO_printf(bio_err
, "error loading the config file '%s'\n",
523 BIO_printf(bio_err
, "error on line %ld of config file '%s'\n",
524 errorline
, configfile
);
528 OPENSSL_free(tofree
);
532 /* Lets get the config section we are using */
533 if (section
== NULL
) {
534 section
= NCONF_get_string(conf
, BASE_SECTION
, ENV_DEFAULT_CA
);
535 if (section
== NULL
) {
536 lookup_fail(BASE_SECTION
, ENV_DEFAULT_CA
);
542 p
= NCONF_get_string(conf
, NULL
, "oid_file");
548 oid_bio
= BIO_new_file(p
, "r");
549 if (oid_bio
== NULL
) {
551 BIO_printf(bio_err,"problems opening %s for extra oid's\n",p);
552 ERR_print_errors(bio_err);
556 OBJ_create_objects(oid_bio
);
560 if (!add_oid_section(conf
)) {
561 ERR_print_errors(bio_err
);
566 randfile
= NCONF_get_string(conf
, BASE_SECTION
, "RANDFILE");
567 if (randfile
== NULL
)
569 app_RAND_load_file(randfile
, 0);
571 f
= NCONF_get_string(conf
, section
, STRING_MASK
);
575 if (f
&& !ASN1_STRING_set_default_mask_asc(f
)) {
576 BIO_printf(bio_err
, "Invalid global string mask setting %s\n", f
);
580 if (chtype
!= MBSTRING_UTF8
) {
581 f
= NCONF_get_string(conf
, section
, UTF8_IN
);
584 else if (!strcmp(f
, "yes"))
585 chtype
= MBSTRING_UTF8
;
588 db_attr
.unique_subject
= 1;
589 p
= NCONF_get_string(conf
, section
, ENV_UNIQUE_SUBJECT
);
591 db_attr
.unique_subject
= parse_yesno(p
, 1);
595 /*****************************************************************/
596 /* report status of cert with serial number given on command line */
598 if ((dbfile
= NCONF_get_string(conf
, section
, ENV_DATABASE
)) == NULL
) {
599 lookup_fail(section
, ENV_DATABASE
);
602 db
= load_index(dbfile
, &db_attr
);
606 if (!index_index(db
))
609 if (get_certificate_status(ser_status
, db
) != 1)
610 BIO_printf(bio_err
, "Error verifying serial %s!\n", ser_status
);
614 /*****************************************************************/
615 /* we definitely need a private key, so let's get it */
617 if ((keyfile
== NULL
) && ((keyfile
= NCONF_get_string(conf
,
621 lookup_fail(section
, ENV_PRIVATE_KEY
);
626 if (!app_passwd(passinarg
, NULL
, &key
, NULL
)) {
627 BIO_printf(bio_err
, "Error getting password\n");
631 pkey
= load_key(keyfile
, keyformat
, 0, key
, e
, "CA private key");
633 OPENSSL_cleanse(key
, strlen(key
));
635 /* load_key() has already printed an appropriate message */
639 /*****************************************************************/
640 /* we need a certificate */
641 if (!selfsign
|| spkac_file
|| ss_cert_file
|| gencrl
) {
642 if ((certfile
== NULL
)
643 && ((certfile
= NCONF_get_string(conf
,
645 ENV_CERTIFICATE
)) == NULL
)) {
646 lookup_fail(section
, ENV_CERTIFICATE
);
649 x509
= load_cert(certfile
, FORMAT_PEM
, NULL
, e
, "CA certificate");
653 if (!X509_check_private_key(x509
, pkey
)) {
655 "CA certificate and CA private key do not match\n");
662 f
= NCONF_get_string(conf
, BASE_SECTION
, ENV_PRESERVE
);
665 if ((f
!= NULL
) && ((*f
== 'y') || (*f
== 'Y')))
667 f
= NCONF_get_string(conf
, BASE_SECTION
, ENV_MSIE_HACK
);
670 if ((f
!= NULL
) && ((*f
== 'y') || (*f
== 'Y')))
673 f
= NCONF_get_string(conf
, section
, ENV_NAMEOPT
);
676 if (!set_name_ex(&nameopt
, f
)) {
677 BIO_printf(bio_err
, "Invalid name options: \"%s\"\n", f
);
684 f
= NCONF_get_string(conf
, section
, ENV_CERTOPT
);
687 if (!set_cert_ex(&certopt
, f
)) {
688 BIO_printf(bio_err
, "Invalid certificate options: \"%s\"\n", f
);
695 f
= NCONF_get_string(conf
, section
, ENV_EXTCOPY
);
698 if (!set_ext_copy(&ext_copy
, f
)) {
699 BIO_printf(bio_err
, "Invalid extension copy option: \"%s\"\n", f
);
705 /*****************************************************************/
706 /* lookup where to write new certificates */
707 if ((outdir
== NULL
) && (req
)) {
709 if ((outdir
= NCONF_get_string(conf
, section
, ENV_NEW_CERTS_DIR
))
712 "there needs to be defined a directory for new certificate to be placed in\n");
715 #ifndef OPENSSL_SYS_VMS
717 * outdir is a directory spec, but access() for VMS demands a
718 * filename. In any case, stat(), below, will catch the problem if
719 * outdir is not a directory spec, and the fopen() or open() will
720 * catch an error if there is no write access.
722 * Presumably, this problem could also be solved by using the DEC C
723 * routines to convert the directory syntax to Unixly, and give that
724 * to access(). However, time's too short to do that just now.
726 if (app_access(outdir
, R_OK
| W_OK
| X_OK
) != 0)
728 BIO_printf(bio_err
, "I am unable to access the %s directory\n",
734 if (app_isdir(outdir
) <= 0) {
735 BIO_printf(bio_err
, "%s need to be a directory\n", outdir
);
742 /*****************************************************************/
743 /* we need to load the database file */
744 if ((dbfile
= NCONF_get_string(conf
, section
, ENV_DATABASE
)) == NULL
) {
745 lookup_fail(section
, ENV_DATABASE
);
748 db
= load_index(dbfile
, &db_attr
);
752 /* Lets check some fields */
753 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(db
->db
->data
); i
++) {
754 pp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, i
);
755 if ((pp
[DB_type
][0] != DB_TYPE_REV
) && (pp
[DB_rev_date
][0] != '\0')) {
757 "entry %d: not revoked yet, but has a revocation date\n",
761 if ((pp
[DB_type
][0] == DB_TYPE_REV
) &&
762 !make_revoked(NULL
, pp
[DB_rev_date
])) {
763 BIO_printf(bio_err
, " in entry %d\n", i
+ 1);
766 if (!check_time_format((char *)pp
[DB_exp_date
])) {
767 BIO_printf(bio_err
, "entry %d: invalid expiry date\n", i
+ 1);
776 if ((j
& 1) || (j
< 2)) {
777 BIO_printf(bio_err
, "entry %d: bad serial number length (%d)\n",
784 "entry %d: bad char 0%o '%c' in serial number\n",
791 TXT_DB_write(bio_out
, db
->db
);
792 BIO_printf(bio_err
, "%d entries loaded from the database\n",
793 sk_OPENSSL_PSTRING_num(db
->db
->data
));
794 BIO_printf(bio_err
, "generating index\n");
797 if (!index_index(db
))
800 /*****************************************************************/
801 /* Update the db file for expired certificates */
804 BIO_printf(bio_err
, "Updating %s ...\n", dbfile
);
808 BIO_printf(bio_err
, "Malloc failure\n");
812 BIO_printf(bio_err
, "No entries found to mark expired\n");
814 if (!save_index(dbfile
, "new", db
))
817 if (!rotate_index(dbfile
, "new", "old"))
822 "Done. %d entries marked as expired\n", i
);
826 /*****************************************************************/
827 /* Read extensions config file */
829 extconf
= NCONF_new(NULL
);
830 if (NCONF_load(extconf
, extfile
, &errorline
) <= 0) {
832 BIO_printf(bio_err
, "ERROR: loading the config file '%s'\n",
836 "ERROR: on line %ld of config file '%s'\n",
843 BIO_printf(bio_err
, "Successfully loaded extensions file %s\n",
846 /* We can have sections in the ext file */
849 NCONF_get_string(extconf
, "default", "extensions")))
850 extensions
= "default";
853 /*****************************************************************/
855 Sout
= bio_open_default(outfile
, "w");
860 if ((md
== NULL
) && ((md
= NCONF_get_string(conf
,
862 ENV_DEFAULT_MD
)) == NULL
)) {
863 lookup_fail(section
, ENV_DEFAULT_MD
);
867 if (!strcmp(md
, "default")) {
869 if (EVP_PKEY_get_default_digest_nid(pkey
, &def_nid
) <= 0) {
870 BIO_puts(bio_err
, "no default digest\n");
873 md
= (char *)OBJ_nid2sn(def_nid
);
876 if (!opt_md(md
, &dgst
)) {
881 if ((email_dn
== 1) && ((tmp_email_dn
= NCONF_get_string(conf
,
883 ENV_DEFAULT_EMAIL_DN
))
885 if (strcmp(tmp_email_dn
, "no") == 0)
889 BIO_printf(bio_err
, "message digest is %s\n",
890 OBJ_nid2ln(dgst
->type
));
891 if ((policy
== NULL
) && ((policy
= NCONF_get_string(conf
,
895 lookup_fail(section
, ENV_POLICY
);
899 BIO_printf(bio_err
, "policy is %s\n", policy
);
901 if ((serialfile
= NCONF_get_string(conf
, section
, ENV_SERIAL
))
903 lookup_fail(section
, ENV_SERIAL
);
909 * no '-extfile' option, so we look for extensions in the main
913 extensions
= NCONF_get_string(conf
, section
, ENV_EXTENSIONS
);
918 /* Check syntax of file */
920 X509V3_set_ctx_test(&ctx
);
921 X509V3_set_nconf(&ctx
, conf
);
922 if (!X509V3_EXT_add_nconf(conf
, &ctx
, extensions
, NULL
)) {
924 "Error Loading extension section %s\n",
932 if (startdate
== NULL
) {
933 startdate
= NCONF_get_string(conf
, section
,
934 ENV_DEFAULT_STARTDATE
);
935 if (startdate
== NULL
)
938 if (startdate
&& !ASN1_TIME_set_string(NULL
, startdate
)) {
940 "start date is invalid, it should be YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ\n");
943 if (startdate
== NULL
)
946 if (enddate
== NULL
) {
947 enddate
= NCONF_get_string(conf
, section
, ENV_DEFAULT_ENDDATE
);
951 if (enddate
&& !ASN1_TIME_set_string(NULL
, enddate
)) {
953 "end date is invalid, it should be YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ\n");
958 if (!NCONF_get_number(conf
, section
, ENV_DEFAULT_DAYS
, &days
))
961 if (!enddate
&& (days
== 0)) {
963 "cannot lookup how many days to certify for\n");
967 if ((serial
= load_serial(serialfile
, create_ser
, NULL
)) == NULL
) {
968 BIO_printf(bio_err
, "error while loading serial number\n");
972 if (BN_is_zero(serial
))
973 BIO_printf(bio_err
, "next serial number is 00\n");
975 if ((f
= BN_bn2hex(serial
)) == NULL
)
977 BIO_printf(bio_err
, "next serial number is %s\n", f
);
982 if ((attribs
= NCONF_get_section(conf
, policy
)) == NULL
) {
983 BIO_printf(bio_err
, "unable to find 'section' for %s\n", policy
);
987 if ((cert_sk
= sk_X509_new_null()) == NULL
) {
988 BIO_printf(bio_err
, "Memory allocation failure\n");
991 if (spkac_file
!= NULL
) {
993 j
= certify_spkac(&x
, spkac_file
, pkey
, x509
, dgst
, sigopts
,
994 attribs
, db
, serial
, subj
, chtype
, multirdn
,
995 email_dn
, startdate
, enddate
, days
, extensions
,
996 conf
, verbose
, certopt
, nameopt
, default_op
,
1002 BIO_printf(bio_err
, "\n");
1003 if (!BN_add_word(serial
, 1))
1005 if (!sk_X509_push(cert_sk
, x
)) {
1006 BIO_printf(bio_err
, "Memory allocation failure\n");
1015 if (ss_cert_file
!= NULL
) {
1017 j
= certify_cert(&x
, ss_cert_file
, pkey
, x509
, dgst
, sigopts
,
1019 db
, serial
, subj
, chtype
, multirdn
, email_dn
,
1020 startdate
, enddate
, days
, batch
, extensions
,
1021 conf
, verbose
, certopt
, nameopt
, default_op
,
1027 BIO_printf(bio_err
, "\n");
1028 if (!BN_add_word(serial
, 1))
1030 if (!sk_X509_push(cert_sk
, x
)) {
1031 BIO_printf(bio_err
, "Memory allocation failure\n");
1036 if (infile
!= NULL
) {
1038 j
= certify(&x
, infile
, pkey
, x509p
, dgst
, sigopts
, attribs
, db
,
1039 serial
, subj
, chtype
, multirdn
, email_dn
, startdate
,
1040 enddate
, days
, batch
, extensions
, conf
, verbose
,
1041 certopt
, nameopt
, default_op
, ext_copy
, selfsign
);
1046 BIO_printf(bio_err
, "\n");
1047 if (!BN_add_word(serial
, 1))
1049 if (!sk_X509_push(cert_sk
, x
)) {
1050 BIO_printf(bio_err
, "Memory allocation failure\n");
1055 for (i
= 0; i
< argc
; i
++) {
1057 j
= certify(&x
, argv
[i
], pkey
, x509p
, dgst
, sigopts
, attribs
, db
,
1058 serial
, subj
, chtype
, multirdn
, email_dn
, startdate
,
1059 enddate
, days
, batch
, extensions
, conf
, verbose
,
1060 certopt
, nameopt
, default_op
, ext_copy
, selfsign
);
1065 BIO_printf(bio_err
, "\n");
1066 if (!BN_add_word(serial
, 1))
1068 if (!sk_X509_push(cert_sk
, x
)) {
1069 BIO_printf(bio_err
, "Memory allocation failure\n");
1075 * we have a stack of newly certified certificates and a data base
1076 * and serial number that need updating
1079 if (sk_X509_num(cert_sk
) > 0) {
1082 "\n%d out of %d certificate requests certified, commit? [y/n]",
1084 (void)BIO_flush(bio_err
);
1086 if (!fgets(buf
[0], 10, stdin
)) {
1088 "CERTIFICATION CANCELED: I/O error\n");
1092 if ((buf
[0][0] != 'y') && (buf
[0][0] != 'Y')) {
1093 BIO_printf(bio_err
, "CERTIFICATION CANCELED\n");
1099 BIO_printf(bio_err
, "Write out database with %d new entries\n",
1100 sk_X509_num(cert_sk
));
1102 if (!save_serial(serialfile
, "new", serial
, NULL
))
1105 if (!save_index(dbfile
, "new", db
))
1110 BIO_printf(bio_err
, "writing new certificates\n");
1111 for (i
= 0; i
< sk_X509_num(cert_sk
); i
++) {
1115 x
= sk_X509_value(cert_sk
, i
);
1117 j
= x
->cert_info
->serialNumber
->length
;
1118 p
= (const char *)x
->cert_info
->serialNumber
->data
;
1120 if (strlen(outdir
) >= (size_t)(j
? BSIZE
- j
* 2 - 6 : BSIZE
- 8)) {
1121 BIO_printf(bio_err
, "certificate file name too long\n");
1125 strcpy(buf
[2], outdir
);
1127 #ifndef OPENSSL_SYS_VMS
1128 BUF_strlcat(buf
[2], "/", sizeof(buf
[2]));
1131 n
= (char *)&(buf
[2][strlen(buf
[2])]);
1133 for (k
= 0; k
< j
; k
++) {
1134 if (n
>= &(buf
[2][sizeof(buf
[2])]))
1137 &buf
[2][0] + sizeof(buf
[2]) - n
,
1138 "%02X", (unsigned char)*(p
++));
1151 BIO_printf(bio_err
, "writing %s\n", buf
[2]);
1153 Cout
= BIO_new_file(buf
[2], "w");
1158 write_new_certificate(Cout
, x
, 0, notext
);
1159 write_new_certificate(Sout
, x
, output_der
, notext
);
1162 if (sk_X509_num(cert_sk
)) {
1163 /* Rename the database and the serial file */
1164 if (!rotate_serial(serialfile
, "new", "old"))
1167 if (!rotate_index(dbfile
, "new", "old"))
1170 BIO_printf(bio_err
, "Data Base Updated\n");
1174 /*****************************************************************/
1178 crl_ext
= NCONF_get_string(conf
, section
, ENV_CRLEXT
);
1183 /* Check syntax of file */
1185 X509V3_set_ctx_test(&ctx
);
1186 X509V3_set_nconf(&ctx
, conf
);
1187 if (!X509V3_EXT_add_nconf(conf
, &ctx
, crl_ext
, NULL
)) {
1189 "Error Loading CRL extension section %s\n",
1196 if ((crlnumberfile
= NCONF_get_string(conf
, section
, ENV_CRLNUMBER
))
1198 if ((crlnumber
= load_serial(crlnumberfile
, 0, NULL
)) == NULL
) {
1199 BIO_printf(bio_err
, "error while loading CRL number\n");
1203 if (!crldays
&& !crlhours
&& !crlsec
) {
1204 if (!NCONF_get_number(conf
, section
,
1205 ENV_DEFAULT_CRL_DAYS
, &crldays
))
1207 if (!NCONF_get_number(conf
, section
,
1208 ENV_DEFAULT_CRL_HOURS
, &crlhours
))
1212 if ((crldays
== 0) && (crlhours
== 0) && (crlsec
== 0)) {
1214 "cannot lookup how long until the next CRL is issued\n");
1219 BIO_printf(bio_err
, "making CRL\n");
1220 if ((crl
= X509_CRL_new()) == NULL
)
1222 if (!X509_CRL_set_issuer_name(crl
, X509_get_subject_name(x509
)))
1225 tmptm
= ASN1_TIME_new();
1228 X509_gmtime_adj(tmptm
, 0);
1229 X509_CRL_set_lastUpdate(crl
, tmptm
);
1230 if (!X509_time_adj_ex(tmptm
, crldays
, crlhours
* 60 * 60 + crlsec
,
1232 BIO_puts(bio_err
, "error setting CRL nextUpdate\n");
1235 X509_CRL_set_nextUpdate(crl
, tmptm
);
1237 ASN1_TIME_free(tmptm
);
1239 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(db
->db
->data
); i
++) {
1240 pp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, i
);
1241 if (pp
[DB_type
][0] == DB_TYPE_REV
) {
1242 if ((r
= X509_REVOKED_new()) == NULL
)
1244 j
= make_revoked(r
, pp
[DB_rev_date
]);
1249 if (!BN_hex2bn(&serial
, pp
[DB_serial
]))
1251 tmpser
= BN_to_ASN1_INTEGER(serial
, NULL
);
1256 X509_REVOKED_set_serialNumber(r
, tmpser
);
1257 ASN1_INTEGER_free(tmpser
);
1258 X509_CRL_add0_revoked(crl
, r
);
1263 * sort the data so it will be written in serial number order
1267 /* we now have a CRL */
1269 BIO_printf(bio_err
, "signing CRL\n");
1271 /* Add any extensions asked for */
1273 if (crl_ext
|| crlnumberfile
!= NULL
) {
1275 X509V3_set_ctx(&crlctx
, x509
, NULL
, NULL
, crl
, 0);
1276 X509V3_set_nconf(&crlctx
, conf
);
1279 if (!X509V3_EXT_CRL_add_nconf(conf
, &crlctx
, crl_ext
, crl
))
1281 if (crlnumberfile
!= NULL
) {
1282 tmpser
= BN_to_ASN1_INTEGER(crlnumber
, NULL
);
1285 X509_CRL_add1_ext_i2d(crl
, NID_crl_number
, tmpser
, 0, 0);
1286 ASN1_INTEGER_free(tmpser
);
1288 if (!BN_add_word(crlnumber
, 1))
1292 if (crl_ext
|| crl_v2
) {
1293 if (!X509_CRL_set_version(crl
, 1))
1294 goto end
; /* version 2 CRL */
1297 /* we have a CRL number that need updating */
1298 if (crlnumberfile
!= NULL
)
1299 if (!save_serial(crlnumberfile
, "new", crlnumber
, NULL
))
1307 if (!do_X509_CRL_sign(crl
, pkey
, dgst
, sigopts
))
1310 PEM_write_bio_X509_CRL(Sout
, crl
);
1312 if (crlnumberfile
!= NULL
) /* Rename the crlnumber file */
1313 if (!rotate_serial(crlnumberfile
, "new", "old"))
1317 /*****************************************************************/
1319 if (infile
== NULL
) {
1320 BIO_printf(bio_err
, "no input files\n");
1324 revcert
= load_cert(infile
, FORMAT_PEM
, NULL
, e
, infile
);
1325 if (revcert
== NULL
)
1329 j
= do_revoke(revcert
, db
, rev_type
, rev_arg
);
1334 if (!save_index(dbfile
, "new", db
))
1337 if (!rotate_index(dbfile
, "new", "old"))
1340 BIO_printf(bio_err
, "Data Base Updated\n");
1343 /*****************************************************************/
1347 OPENSSL_free(tofree
);
1352 sk_X509_pop_free(cert_sk
, X509_free
);
1355 ERR_print_errors(bio_err
);
1356 app_RAND_write_file(randfile
);
1357 if (free_key
&& key
)
1363 sk_OPENSSL_STRING_free(sigopts
);
1364 EVP_PKEY_free(pkey
);
1368 NCONF_free(extconf
);
1373 static void lookup_fail(const char *name
, const char *tag
)
1375 BIO_printf(bio_err
, "variable lookup failed for %s::%s\n", name
, tag
);
1378 static int certify(X509
**xret
, char *infile
, EVP_PKEY
*pkey
, X509
*x509
,
1379 const EVP_MD
*dgst
, STACK_OF(OPENSSL_STRING
) *sigopts
,
1380 STACK_OF(CONF_VALUE
) *policy
, CA_DB
*db
,
1381 BIGNUM
*serial
, char *subj
, unsigned long chtype
,
1382 int multirdn
, int email_dn
, char *startdate
, char *enddate
,
1383 long days
, int batch
, char *ext_sect
, CONF
*lconf
,
1384 int verbose
, unsigned long certopt
, unsigned long nameopt
,
1385 int default_op
, int ext_copy
, int selfsign
)
1387 X509_REQ
*req
= NULL
;
1389 EVP_PKEY
*pktmp
= NULL
;
1392 in
= BIO_new_file(infile
, "r");
1394 ERR_print_errors(bio_err
);
1397 if ((req
= PEM_read_bio_X509_REQ(in
, NULL
, NULL
, NULL
)) == NULL
) {
1398 BIO_printf(bio_err
, "Error reading certificate request in %s\n",
1403 X509_REQ_print(bio_err
, req
);
1405 BIO_printf(bio_err
, "Check that the request matches the signature\n");
1407 if (selfsign
&& !X509_REQ_check_private_key(req
, pkey
)) {
1409 "Certificate request and CA private key do not match\n");
1413 if ((pktmp
= X509_REQ_get_pubkey(req
)) == NULL
) {
1414 BIO_printf(bio_err
, "error unpacking public key\n");
1417 i
= X509_REQ_verify(req
, pktmp
);
1418 EVP_PKEY_free(pktmp
);
1421 BIO_printf(bio_err
, "Signature verification problems....\n");
1422 ERR_print_errors(bio_err
);
1428 "Signature did not match the certificate request\n");
1429 ERR_print_errors(bio_err
);
1432 BIO_printf(bio_err
, "Signature ok\n");
1434 ok
= do_body(xret
, pkey
, x509
, dgst
, sigopts
, policy
, db
, serial
, subj
,
1435 chtype
, multirdn
, email_dn
, startdate
, enddate
, days
, batch
,
1436 verbose
, req
, ext_sect
, lconf
, certopt
, nameopt
, default_op
,
1437 ext_copy
, selfsign
);
1445 static int certify_cert(X509
**xret
, char *infile
, EVP_PKEY
*pkey
, X509
*x509
,
1446 const EVP_MD
*dgst
, STACK_OF(OPENSSL_STRING
) *sigopts
,
1447 STACK_OF(CONF_VALUE
) *policy
, CA_DB
*db
,
1448 BIGNUM
*serial
, char *subj
, unsigned long chtype
,
1449 int multirdn
, int email_dn
, char *startdate
,
1450 char *enddate
, long days
, int batch
, char *ext_sect
,
1451 CONF
*lconf
, int verbose
, unsigned long certopt
,
1452 unsigned long nameopt
, int default_op
, int ext_copy
,
1456 X509_REQ
*rreq
= NULL
;
1457 EVP_PKEY
*pktmp
= NULL
;
1460 if ((req
= load_cert(infile
, FORMAT_PEM
, NULL
, e
, infile
)) == NULL
)
1463 X509_print(bio_err
, req
);
1465 BIO_printf(bio_err
, "Check that the request matches the signature\n");
1467 if ((pktmp
= X509_get_pubkey(req
)) == NULL
) {
1468 BIO_printf(bio_err
, "error unpacking public key\n");
1471 i
= X509_verify(req
, pktmp
);
1472 EVP_PKEY_free(pktmp
);
1475 BIO_printf(bio_err
, "Signature verification problems....\n");
1480 BIO_printf(bio_err
, "Signature did not match the certificate\n");
1483 BIO_printf(bio_err
, "Signature ok\n");
1485 if ((rreq
= X509_to_X509_REQ(req
, NULL
, EVP_md5())) == NULL
)
1488 ok
= do_body(xret
, pkey
, x509
, dgst
, sigopts
, policy
, db
, serial
, subj
,
1489 chtype
, multirdn
, email_dn
, startdate
, enddate
, days
, batch
,
1490 verbose
, rreq
, ext_sect
, lconf
, certopt
, nameopt
, default_op
,
1494 X509_REQ_free(rreq
);
1499 static int do_body(X509
**xret
, EVP_PKEY
*pkey
, X509
*x509
,
1500 const EVP_MD
*dgst
, STACK_OF(OPENSSL_STRING
) *sigopts
,
1501 STACK_OF(CONF_VALUE
) *policy
, CA_DB
*db
, BIGNUM
*serial
,
1502 char *subj
, unsigned long chtype
, int multirdn
,
1503 int email_dn
, char *startdate
, char *enddate
, long days
,
1504 int batch
, int verbose
, X509_REQ
*req
, char *ext_sect
,
1505 CONF
*lconf
, unsigned long certopt
, unsigned long nameopt
,
1506 int default_op
, int ext_copy
, int selfsign
)
1508 X509_NAME
*name
= NULL
, *CAname
= NULL
, *subject
= NULL
, *dn_subject
=
1510 ASN1_UTCTIME
*tm
, *tmptm
;
1511 ASN1_STRING
*str
, *str2
;
1515 X509_NAME_ENTRY
*ne
;
1516 X509_NAME_ENTRY
*tne
, *push
;
1518 int ok
= -1, i
, j
, last
, nid
;
1521 OPENSSL_STRING row
[DB_NUMBER
];
1522 OPENSSL_STRING
*irow
= NULL
;
1523 OPENSSL_STRING
*rrow
= NULL
;
1526 tmptm
= ASN1_UTCTIME_new();
1527 if (tmptm
== NULL
) {
1528 BIO_printf(bio_err
, "malloc error\n");
1532 for (i
= 0; i
< DB_NUMBER
; i
++)
1536 X509_NAME
*n
= parse_name(subj
, chtype
, multirdn
);
1539 ERR_print_errors(bio_err
);
1542 X509_REQ_set_subject_name(req
, n
);
1543 req
->req_info
->enc
.modified
= 1;
1549 "The Subject's Distinguished Name is as follows\n");
1551 name
= X509_REQ_get_subject_name(req
);
1552 for (i
= 0; i
< X509_NAME_entry_count(name
); i
++) {
1553 ne
= X509_NAME_get_entry(name
, i
);
1554 str
= X509_NAME_ENTRY_get_data(ne
);
1555 obj
= X509_NAME_ENTRY_get_object(ne
);
1558 /* assume all type should be strings */
1559 nid
= OBJ_obj2nid(X509_NAME_ENTRY_get_object(ne
));
1561 if (str
->type
== V_ASN1_UNIVERSALSTRING
)
1562 ASN1_UNIVERSALSTRING_to_string(str
);
1564 if ((str
->type
== V_ASN1_IA5STRING
) &&
1565 (nid
!= NID_pkcs9_emailAddress
))
1566 str
->type
= V_ASN1_T61STRING
;
1568 if ((nid
== NID_pkcs9_emailAddress
) &&
1569 (str
->type
== V_ASN1_PRINTABLESTRING
))
1570 str
->type
= V_ASN1_IA5STRING
;
1573 /* If no EMAIL is wanted in the subject */
1574 if ((OBJ_obj2nid(obj
) == NID_pkcs9_emailAddress
) && (!email_dn
))
1577 /* check some things */
1578 if ((OBJ_obj2nid(obj
) == NID_pkcs9_emailAddress
) &&
1579 (str
->type
!= V_ASN1_IA5STRING
)) {
1581 "\nemailAddress type needs to be of type IA5STRING\n");
1584 if ((str
->type
!= V_ASN1_BMPSTRING
)
1585 && (str
->type
!= V_ASN1_UTF8STRING
)) {
1586 j
= ASN1_PRINTABLE_type(str
->data
, str
->length
);
1587 if (((j
== V_ASN1_T61STRING
) &&
1588 (str
->type
!= V_ASN1_T61STRING
)) ||
1589 ((j
== V_ASN1_IA5STRING
) &&
1590 (str
->type
== V_ASN1_PRINTABLESTRING
))) {
1592 "\nThe string contains characters that are illegal for the ASN.1 type\n");
1598 old_entry_print(obj
, str
);
1601 /* Ok, now we check the 'policy' stuff. */
1602 if ((subject
= X509_NAME_new()) == NULL
) {
1603 BIO_printf(bio_err
, "Memory allocation failure\n");
1607 /* take a copy of the issuer name before we mess with it. */
1609 CAname
= X509_NAME_dup(name
);
1611 CAname
= X509_NAME_dup(x509
->cert_info
->subject
);
1616 for (i
= 0; i
< sk_CONF_VALUE_num(policy
); i
++) {
1617 cv
= sk_CONF_VALUE_value(policy
, i
); /* get the object id */
1618 if ((j
= OBJ_txt2nid(cv
->name
)) == NID_undef
) {
1620 "%s:unknown object type in 'policy' configuration\n",
1624 obj
= OBJ_nid2obj(j
);
1628 /* lookup the object in the supplied name list */
1629 j
= X509_NAME_get_index_by_OBJ(name
, obj
, last
);
1635 tne
= X509_NAME_get_entry(name
, j
);
1639 /* depending on the 'policy', decide what to do. */
1641 if (strcmp(cv
->value
, "optional") == 0) {
1644 } else if (strcmp(cv
->value
, "supplied") == 0) {
1647 "The %s field needed to be supplied and was missing\n",
1652 } else if (strcmp(cv
->value
, "match") == 0) {
1657 "The mandatory %s field was missing\n",
1665 j
= X509_NAME_get_index_by_OBJ(CAname
, obj
, last2
);
1666 if ((j
< 0) && (last2
== -1)) {
1668 "The %s field does not exist in the CA certificate,\nthe 'policy' is misconfigured\n",
1673 push
= X509_NAME_get_entry(CAname
, j
);
1674 str
= X509_NAME_ENTRY_get_data(tne
);
1675 str2
= X509_NAME_ENTRY_get_data(push
);
1677 if (ASN1_STRING_cmp(str
, str2
) != 0)
1682 "The %s field needed to be the same in the\nCA certificate (%s) and the request (%s)\n",
1684 ((str2
== NULL
) ? "NULL" : (char *)str2
->data
),
1685 ((str
== NULL
) ? "NULL" : (char *)str
->data
));
1690 "%s:invalid type in 'policy' configuration\n",
1696 if (!X509_NAME_add_entry(subject
, push
, -1, 0)) {
1697 X509_NAME_ENTRY_free(push
);
1698 BIO_printf(bio_err
, "Memory allocation failure\n");
1708 X509_NAME_free(subject
);
1709 /* subject=X509_NAME_dup(X509_REQ_get_subject_name(req)); */
1710 subject
= X509_NAME_dup(name
);
1711 if (subject
== NULL
)
1717 "The subject name appears to be ok, checking data base for clashes\n");
1719 /* Build the correct Subject if no e-mail is wanted in the subject */
1721 * and add it later on because of the method extensions are added
1726 dn_subject
= subject
;
1728 X509_NAME_ENTRY
*tmpne
;
1730 * Its best to dup the subject DN and then delete any email addresses
1731 * because this retains its structure.
1733 if (!(dn_subject
= X509_NAME_dup(subject
))) {
1734 BIO_printf(bio_err
, "Memory allocation failure\n");
1737 while ((i
= X509_NAME_get_index_by_NID(dn_subject
,
1738 NID_pkcs9_emailAddress
,
1740 tmpne
= X509_NAME_get_entry(dn_subject
, i
);
1741 X509_NAME_delete_entry(dn_subject
, i
);
1742 X509_NAME_ENTRY_free(tmpne
);
1746 if (BN_is_zero(serial
))
1747 row
[DB_serial
] = BUF_strdup("00");
1749 row
[DB_serial
] = BN_bn2hex(serial
);
1750 if (row
[DB_serial
] == NULL
) {
1751 BIO_printf(bio_err
, "Memory allocation failure\n");
1755 if (db
->attributes
.unique_subject
) {
1756 OPENSSL_STRING
*crow
= row
;
1758 rrow
= TXT_DB_get_by_index(db
->db
, DB_name
, crow
);
1761 "ERROR:There is already a certificate for %s\n",
1766 rrow
= TXT_DB_get_by_index(db
->db
, DB_serial
, row
);
1769 "ERROR:Serial number %s has already been issued,\n",
1772 " check the database/serial_file for corruption\n");
1777 BIO_printf(bio_err
, "The matching entry has the following details\n");
1778 if (rrow
[DB_type
][0] == 'E')
1780 else if (rrow
[DB_type
][0] == 'R')
1782 else if (rrow
[DB_type
][0] == 'V')
1785 p
= "\ninvalid type, Data base error\n";
1786 BIO_printf(bio_err
, "Type :%s\n", p
);;
1787 if (rrow
[DB_type
][0] == 'R') {
1788 p
= rrow
[DB_exp_date
];
1791 BIO_printf(bio_err
, "Was revoked on:%s\n", p
);
1793 p
= rrow
[DB_exp_date
];
1796 BIO_printf(bio_err
, "Expires on :%s\n", p
);
1797 p
= rrow
[DB_serial
];
1800 BIO_printf(bio_err
, "Serial Number :%s\n", p
);
1804 BIO_printf(bio_err
, "File name :%s\n", p
);
1808 BIO_printf(bio_err
, "Subject Name :%s\n", p
);
1809 ok
= -1; /* This is now a 'bad' error. */
1813 /* We are now totally happy, lets make and sign the certificate */
1816 "Everything appears to be ok, creating and signing the certificate\n");
1818 if ((ret
= X509_new()) == NULL
)
1820 ci
= ret
->cert_info
;
1823 /* Make it an X509 v3 certificate. */
1824 if (!X509_set_version(ret
, 2))
1828 if (BN_to_ASN1_INTEGER(serial
, ci
->serialNumber
) == NULL
)
1831 if (!X509_set_issuer_name(ret
, subject
))
1834 if (!X509_set_issuer_name(ret
, X509_get_subject_name(x509
)))
1838 if (strcmp(startdate
, "today") == 0)
1839 X509_gmtime_adj(X509_get_notBefore(ret
), 0);
1841 ASN1_TIME_set_string(X509_get_notBefore(ret
), startdate
);
1843 if (enddate
== NULL
)
1844 X509_time_adj_ex(X509_get_notAfter(ret
), days
, 0, NULL
);
1847 ASN1_TIME_set_string(X509_get_notAfter(ret
), enddate
);
1848 ASN1_TIME_diff(&tdays
, NULL
, NULL
, X509_get_notAfter(ret
));
1852 if (!X509_set_subject_name(ret
, subject
))
1855 pktmp
= X509_REQ_get_pubkey(req
);
1856 i
= X509_set_pubkey(ret
, pktmp
);
1857 EVP_PKEY_free(pktmp
);
1861 /* Lets add the extensions, if there are any */
1864 if (ci
->version
== NULL
)
1865 if ((ci
->version
= ASN1_INTEGER_new()) == NULL
)
1867 ASN1_INTEGER_set(ci
->version
, 2); /* version 3 certificate */
1870 * Free the current entries if any, there should not be any I believe
1872 sk_X509_EXTENSION_pop_free(ci
->extensions
, X509_EXTENSION_free
);
1874 ci
->extensions
= NULL
;
1876 /* Initialize the context structure */
1878 X509V3_set_ctx(&ctx
, ret
, ret
, req
, NULL
, 0);
1880 X509V3_set_ctx(&ctx
, x509
, ret
, req
, NULL
, 0);
1884 BIO_printf(bio_err
, "Extra configuration file found\n");
1886 /* Use the extconf configuration db LHASH */
1887 X509V3_set_nconf(&ctx
, extconf
);
1889 /* Test the structure (needed?) */
1890 /* X509V3_set_ctx_test(&ctx); */
1892 /* Adds exts contained in the configuration file */
1893 if (!X509V3_EXT_add_nconf(extconf
, &ctx
, ext_sect
, ret
)) {
1895 "ERROR: adding extensions in section %s\n",
1897 ERR_print_errors(bio_err
);
1902 "Successfully added extensions from file.\n");
1903 } else if (ext_sect
) {
1904 /* We found extensions to be set from config file */
1905 X509V3_set_nconf(&ctx
, lconf
);
1907 if (!X509V3_EXT_add_nconf(lconf
, &ctx
, ext_sect
, ret
)) {
1909 "ERROR: adding extensions in section %s\n",
1911 ERR_print_errors(bio_err
);
1917 "Successfully added extensions from config\n");
1921 /* Copy extensions from request (if any) */
1923 if (!copy_extensions(ret
, req
, ext_copy
)) {
1924 BIO_printf(bio_err
, "ERROR: adding extensions from request\n");
1925 ERR_print_errors(bio_err
);
1929 /* Set the right value for the noemailDN option */
1930 if (email_dn
== 0) {
1931 if (!X509_set_subject_name(ret
, dn_subject
))
1936 BIO_printf(bio_err
, "Certificate Details:\n");
1938 * Never print signature details because signature not present
1940 certopt
|= X509_FLAG_NO_SIGDUMP
| X509_FLAG_NO_SIGNAME
;
1941 X509_print_ex(bio_err
, ret
, nameopt
, certopt
);
1944 BIO_printf(bio_err
, "Certificate is to be certified until ");
1945 ASN1_TIME_print(bio_err
, X509_get_notAfter(ret
));
1947 BIO_printf(bio_err
, " (%ld days)", days
);
1948 BIO_printf(bio_err
, "\n");
1952 BIO_printf(bio_err
, "Sign the certificate? [y/n]:");
1953 (void)BIO_flush(bio_err
);
1955 if (!fgets(buf
, sizeof(buf
) - 1, stdin
)) {
1957 "CERTIFICATE WILL NOT BE CERTIFIED: I/O error\n");
1961 if (!((buf
[0] == 'y') || (buf
[0] == 'Y'))) {
1962 BIO_printf(bio_err
, "CERTIFICATE WILL NOT BE CERTIFIED\n");
1968 pktmp
= X509_get_pubkey(ret
);
1969 if (EVP_PKEY_missing_parameters(pktmp
) &&
1970 !EVP_PKEY_missing_parameters(pkey
))
1971 EVP_PKEY_copy_parameters(pktmp
, pkey
);
1972 EVP_PKEY_free(pktmp
);
1974 if (!do_X509_sign(ret
, pkey
, dgst
, sigopts
))
1977 /* We now just add it to the database */
1978 row
[DB_type
] = OPENSSL_malloc(2);
1980 tm
= X509_get_notAfter(ret
);
1981 row
[DB_exp_date
] = OPENSSL_malloc(tm
->length
+ 1);
1982 memcpy(row
[DB_exp_date
], tm
->data
, tm
->length
);
1983 row
[DB_exp_date
][tm
->length
] = '\0';
1985 row
[DB_rev_date
] = NULL
;
1987 /* row[DB_serial] done already */
1988 row
[DB_file
] = OPENSSL_malloc(8);
1989 row
[DB_name
] = X509_NAME_oneline(X509_get_subject_name(ret
), NULL
, 0);
1991 if ((row
[DB_type
] == NULL
) || (row
[DB_exp_date
] == NULL
) ||
1992 (row
[DB_file
] == NULL
) || (row
[DB_name
] == NULL
)) {
1993 BIO_printf(bio_err
, "Memory allocation failure\n");
1996 BUF_strlcpy(row
[DB_file
], "unknown", 8);
1997 row
[DB_type
][0] = 'V';
1998 row
[DB_type
][1] = '\0';
2000 if ((irow
= OPENSSL_malloc(sizeof(char *) * (DB_NUMBER
+ 1))) == NULL
) {
2001 BIO_printf(bio_err
, "Memory allocation failure\n");
2005 for (i
= 0; i
< DB_NUMBER
; i
++) {
2009 irow
[DB_NUMBER
] = NULL
;
2011 if (!TXT_DB_insert(db
->db
, irow
)) {
2012 BIO_printf(bio_err
, "failed to update database\n");
2013 BIO_printf(bio_err
, "TXT_DB error number %ld\n", db
->db
->error
);
2018 for (i
= 0; i
< DB_NUMBER
; i
++)
2020 OPENSSL_free(row
[i
]);
2022 X509_NAME_free(CAname
);
2023 X509_NAME_free(subject
);
2024 if (dn_subject
!= subject
)
2025 X509_NAME_free(dn_subject
);
2026 ASN1_UTCTIME_free(tmptm
);
2034 static void write_new_certificate(BIO
*bp
, X509
*x
, int output_der
,
2039 (void)i2d_X509_bio(bp
, x
);
2044 PEM_write_bio_X509(bp
, x
);
2047 static int certify_spkac(X509
**xret
, char *infile
, EVP_PKEY
*pkey
,
2048 X509
*x509
, const EVP_MD
*dgst
,
2049 STACK_OF(OPENSSL_STRING
) *sigopts
,
2050 STACK_OF(CONF_VALUE
) *policy
, CA_DB
*db
,
2051 BIGNUM
*serial
, char *subj
, unsigned long chtype
,
2052 int multirdn
, int email_dn
, char *startdate
,
2053 char *enddate
, long days
, char *ext_sect
,
2054 CONF
*lconf
, int verbose
, unsigned long certopt
,
2055 unsigned long nameopt
, int default_op
, int ext_copy
)
2057 STACK_OF(CONF_VALUE
) *sk
= NULL
;
2058 LHASH_OF(CONF_VALUE
) *parms
= NULL
;
2059 X509_REQ
*req
= NULL
;
2060 CONF_VALUE
*cv
= NULL
;
2061 NETSCAPE_SPKI
*spki
= NULL
;
2064 EVP_PKEY
*pktmp
= NULL
;
2065 X509_NAME
*n
= NULL
;
2066 X509_NAME_ENTRY
*ne
= NULL
;
2072 * Load input file into a hash table. (This is just an easy
2073 * way to read and parse the file, then put it into a convenient
2076 parms
= CONF_load(NULL
, infile
, &errline
);
2077 if (parms
== NULL
) {
2078 BIO_printf(bio_err
, "error on line %ld of %s\n", errline
, infile
);
2079 ERR_print_errors(bio_err
);
2083 sk
= CONF_get_section(parms
, "default");
2084 if (sk_CONF_VALUE_num(sk
) == 0) {
2085 BIO_printf(bio_err
, "no name/value pairs found in %s\n", infile
);
2091 * Now create a dummy X509 request structure. We don't actually
2092 * have an X509 request, but we have many of the components
2093 * (a public key, various DN components). The idea is that we
2094 * put these components into the right X509 request structure
2095 * and we can use the same code as if you had a real X509 request.
2097 req
= X509_REQ_new();
2099 ERR_print_errors(bio_err
);
2104 * Build up the subject name set.
2110 if (sk_CONF_VALUE_num(sk
) <= i
)
2113 cv
= sk_CONF_VALUE_value(sk
, i
);
2116 * Skip past any leading X. X: X, etc to allow for multiple instances
2118 for (buf
= cv
->name
; *buf
; buf
++)
2119 if ((*buf
== ':') || (*buf
== ',') || (*buf
== '.')) {
2127 if ((nid
= OBJ_txt2nid(type
)) == NID_undef
) {
2128 if (strcmp(type
, "SPKAC") == 0) {
2129 spki
= NETSCAPE_SPKI_b64_decode(cv
->value
, -1);
2132 "unable to load Netscape SPKAC structure\n");
2133 ERR_print_errors(bio_err
);
2140 if (!X509_NAME_add_entry_by_NID(n
, nid
, chtype
,
2141 (unsigned char *)buf
, -1, -1, 0))
2145 BIO_printf(bio_err
, "Netscape SPKAC structure not found in %s\n",
2151 * Now extract the key from the SPKI structure.
2155 "Check that the SPKAC request matches the signature\n");
2157 if ((pktmp
= NETSCAPE_SPKI_get_pubkey(spki
)) == NULL
) {
2158 BIO_printf(bio_err
, "error unpacking SPKAC public key\n");
2162 j
= NETSCAPE_SPKI_verify(spki
, pktmp
);
2165 "signature verification failed on SPKAC public key\n");
2168 BIO_printf(bio_err
, "Signature ok\n");
2170 X509_REQ_set_pubkey(req
, pktmp
);
2171 EVP_PKEY_free(pktmp
);
2172 ok
= do_body(xret
, pkey
, x509
, dgst
, sigopts
, policy
, db
, serial
, subj
,
2173 chtype
, multirdn
, email_dn
, startdate
, enddate
, days
, 1,
2174 verbose
, req
, ext_sect
, lconf
, certopt
, nameopt
, default_op
,
2181 NETSCAPE_SPKI_free(spki
);
2182 X509_NAME_ENTRY_free(ne
);
2187 static int check_time_format(const char *str
)
2189 return ASN1_TIME_set_string(NULL
, str
);
2192 static int do_revoke(X509
*x509
, CA_DB
*db
, int type
, char *value
)
2194 ASN1_UTCTIME
*tm
= NULL
;
2195 char *row
[DB_NUMBER
], **rrow
, **irow
;
2196 char *rev_str
= NULL
;
2200 for (i
= 0; i
< DB_NUMBER
; i
++)
2202 row
[DB_name
] = X509_NAME_oneline(X509_get_subject_name(x509
), NULL
, 0);
2203 bn
= ASN1_INTEGER_to_BN(X509_get_serialNumber(x509
), NULL
);
2207 row
[DB_serial
] = BUF_strdup("00");
2209 row
[DB_serial
] = BN_bn2hex(bn
);
2211 if ((row
[DB_name
] == NULL
) || (row
[DB_serial
] == NULL
)) {
2212 BIO_printf(bio_err
, "Memory allocation failure\n");
2216 * We have to lookup by serial number because name lookup skips revoked
2219 rrow
= TXT_DB_get_by_index(db
->db
, DB_serial
, row
);
2222 "Adding Entry with serial number %s to DB for %s\n",
2223 row
[DB_serial
], row
[DB_name
]);
2225 /* We now just add it to the database */
2226 row
[DB_type
] = OPENSSL_malloc(2);
2228 tm
= X509_get_notAfter(x509
);
2229 row
[DB_exp_date
] = OPENSSL_malloc(tm
->length
+ 1);
2230 memcpy(row
[DB_exp_date
], tm
->data
, tm
->length
);
2231 row
[DB_exp_date
][tm
->length
] = '\0';
2233 row
[DB_rev_date
] = NULL
;
2235 /* row[DB_serial] done already */
2236 row
[DB_file
] = OPENSSL_malloc(8);
2238 /* row[DB_name] done already */
2240 if ((row
[DB_type
] == NULL
) || (row
[DB_exp_date
] == NULL
) ||
2241 (row
[DB_file
] == NULL
)) {
2242 BIO_printf(bio_err
, "Memory allocation failure\n");
2245 BUF_strlcpy(row
[DB_file
], "unknown", 8);
2246 row
[DB_type
][0] = 'V';
2247 row
[DB_type
][1] = '\0';
2249 if ((irow
= OPENSSL_malloc(sizeof(char *) * (DB_NUMBER
+ 1))) == NULL
) {
2250 BIO_printf(bio_err
, "Memory allocation failure\n");
2254 for (i
= 0; i
< DB_NUMBER
; i
++) {
2258 irow
[DB_NUMBER
] = NULL
;
2260 if (!TXT_DB_insert(db
->db
, irow
)) {
2261 BIO_printf(bio_err
, "failed to update database\n");
2262 BIO_printf(bio_err
, "TXT_DB error number %ld\n", db
->db
->error
);
2266 /* Revoke Certificate */
2270 ok
= do_revoke(x509
, db
, type
, value
);
2274 } else if (index_name_cmp_noconst(row
, rrow
)) {
2275 BIO_printf(bio_err
, "ERROR:name does not match %s\n", row
[DB_name
]);
2277 } else if (type
== -1) {
2278 BIO_printf(bio_err
, "ERROR:Already present, serial number %s\n",
2281 } else if (rrow
[DB_type
][0] == 'R') {
2282 BIO_printf(bio_err
, "ERROR:Already revoked, serial number %s\n",
2286 BIO_printf(bio_err
, "Revoking Certificate %s.\n", rrow
[DB_serial
]);
2287 rev_str
= make_revocation_str(type
, value
);
2289 BIO_printf(bio_err
, "Error in revocation arguments\n");
2292 rrow
[DB_type
][0] = 'R';
2293 rrow
[DB_type
][1] = '\0';
2294 rrow
[DB_rev_date
] = rev_str
;
2298 for (i
= 0; i
< DB_NUMBER
; i
++) {
2300 OPENSSL_free(row
[i
]);
2305 static int get_certificate_status(const char *serial
, CA_DB
*db
)
2307 char *row
[DB_NUMBER
], **rrow
;
2310 /* Free Resources */
2311 for (i
= 0; i
< DB_NUMBER
; i
++)
2314 /* Malloc needed char spaces */
2315 row
[DB_serial
] = OPENSSL_malloc(strlen(serial
) + 2);
2316 if (row
[DB_serial
] == NULL
) {
2317 BIO_printf(bio_err
, "Malloc failure\n");
2321 if (strlen(serial
) % 2) {
2323 * Set the first char to 0
2325 row
[DB_serial
][0] = '0';
2327 /* Copy String from serial to row[DB_serial] */
2328 memcpy(row
[DB_serial
] + 1, serial
, strlen(serial
));
2329 row
[DB_serial
][strlen(serial
) + 1] = '\0';
2331 /* Copy String from serial to row[DB_serial] */
2332 memcpy(row
[DB_serial
], serial
, strlen(serial
));
2333 row
[DB_serial
][strlen(serial
)] = '\0';
2336 /* Make it Upper Case */
2337 for (i
= 0; row
[DB_serial
][i
] != '\0'; i
++)
2338 row
[DB_serial
][i
] = toupper((unsigned char)row
[DB_serial
][i
]);
2342 /* Search for the certificate */
2343 rrow
= TXT_DB_get_by_index(db
->db
, DB_serial
, row
);
2345 BIO_printf(bio_err
, "Serial %s not present in db.\n", row
[DB_serial
]);
2348 } else if (rrow
[DB_type
][0] == 'V') {
2349 BIO_printf(bio_err
, "%s=Valid (%c)\n",
2350 row
[DB_serial
], rrow
[DB_type
][0]);
2352 } else if (rrow
[DB_type
][0] == 'R') {
2353 BIO_printf(bio_err
, "%s=Revoked (%c)\n",
2354 row
[DB_serial
], rrow
[DB_type
][0]);
2356 } else if (rrow
[DB_type
][0] == 'E') {
2357 BIO_printf(bio_err
, "%s=Expired (%c)\n",
2358 row
[DB_serial
], rrow
[DB_type
][0]);
2360 } else if (rrow
[DB_type
][0] == 'S') {
2361 BIO_printf(bio_err
, "%s=Suspended (%c)\n",
2362 row
[DB_serial
], rrow
[DB_type
][0]);
2365 BIO_printf(bio_err
, "%s=Unknown (%c).\n",
2366 row
[DB_serial
], rrow
[DB_type
][0]);
2370 for (i
= 0; i
< DB_NUMBER
; i
++) {
2372 OPENSSL_free(row
[i
]);
2377 static int do_updatedb(CA_DB
*db
)
2379 ASN1_UTCTIME
*a_tm
= NULL
;
2381 int db_y2k
, a_y2k
; /* flags = 1 if y >= 2000 */
2382 char **rrow
, *a_tm_s
;
2384 a_tm
= ASN1_UTCTIME_new();
2386 /* get actual time and make a string */
2387 a_tm
= X509_gmtime_adj(a_tm
, 0);
2388 a_tm_s
= OPENSSL_malloc(a_tm
->length
+ 1);
2389 if (a_tm_s
== NULL
) {
2394 memcpy(a_tm_s
, a_tm
->data
, a_tm
->length
);
2395 a_tm_s
[a_tm
->length
] = '\0';
2397 if (strncmp(a_tm_s
, "49", 2) <= 0)
2402 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(db
->db
->data
); i
++) {
2403 rrow
= sk_OPENSSL_PSTRING_value(db
->db
->data
, i
);
2405 if (rrow
[DB_type
][0] == 'V') {
2406 /* ignore entries that are not valid */
2407 if (strncmp(rrow
[DB_exp_date
], "49", 2) <= 0)
2412 if (db_y2k
== a_y2k
) {
2413 /* all on the same y2k side */
2414 if (strcmp(rrow
[DB_exp_date
], a_tm_s
) <= 0) {
2415 rrow
[DB_type
][0] = 'E';
2416 rrow
[DB_type
][1] = '\0';
2419 BIO_printf(bio_err
, "%s=Expired\n", rrow
[DB_serial
]);
2421 } else if (db_y2k
< a_y2k
) {
2422 rrow
[DB_type
][0] = 'E';
2423 rrow
[DB_type
][1] = '\0';
2426 BIO_printf(bio_err
, "%s=Expired\n", rrow
[DB_serial
]);
2434 ASN1_UTCTIME_free(a_tm
);
2435 OPENSSL_free(a_tm_s
);
2440 static const char *crl_reasons
[] = {
2441 /* CRL reason strings */
2445 "affiliationChanged",
2447 "cessationOfOperation",
2450 /* Additional pseudo reasons */
2456 #define NUM_REASONS (sizeof(crl_reasons) / sizeof(char *))
2459 * Given revocation information convert to a DB string. The format of the
2460 * string is: revtime[,reason,extra]. Where 'revtime' is the revocation time
2461 * (the current time). 'reason' is the optional CRL reason and 'extra' is any
2462 * additional argument
2465 char *make_revocation_str(int rev_type
, char *rev_arg
)
2467 char *other
= NULL
, *str
;
2468 const char *reason
= NULL
;
2470 ASN1_UTCTIME
*revtm
= NULL
;
2476 case REV_CRL_REASON
:
2477 for (i
= 0; i
< 8; i
++) {
2478 if (!strcasecmp(rev_arg
, crl_reasons
[i
])) {
2479 reason
= crl_reasons
[i
];
2483 if (reason
== NULL
) {
2484 BIO_printf(bio_err
, "Unknown CRL reason %s\n", rev_arg
);
2490 /* Argument is an OID */
2492 otmp
= OBJ_txt2obj(rev_arg
, 0);
2493 ASN1_OBJECT_free(otmp
);
2496 BIO_printf(bio_err
, "Invalid object identifier %s\n", rev_arg
);
2500 reason
= "holdInstruction";
2504 case REV_KEY_COMPROMISE
:
2505 case REV_CA_COMPROMISE
:
2507 /* Argument is the key compromise time */
2508 if (!ASN1_GENERALIZEDTIME_set_string(NULL
, rev_arg
)) {
2510 "Invalid time format %s. Need YYYYMMDDHHMMSSZ\n",
2515 if (rev_type
== REV_KEY_COMPROMISE
)
2518 reason
= "CAkeyTime";
2524 revtm
= X509_gmtime_adj(NULL
, 0);
2529 i
= revtm
->length
+ 1;
2532 i
+= strlen(reason
) + 1;
2534 i
+= strlen(other
) + 1;
2536 str
= OPENSSL_malloc(i
);
2541 BUF_strlcpy(str
, (char *)revtm
->data
, i
);
2543 BUF_strlcat(str
, ",", i
);
2544 BUF_strlcat(str
, reason
, i
);
2547 BUF_strlcat(str
, ",", i
);
2548 BUF_strlcat(str
, other
, i
);
2550 ASN1_UTCTIME_free(revtm
);
2555 * Convert revocation field to X509_REVOKED entry
2559 * 2 OK and some extensions added (i.e. V2 CRL)
2562 int make_revoked(X509_REVOKED
*rev
, const char *str
)
2565 int reason_code
= -1;
2567 ASN1_OBJECT
*hold
= NULL
;
2568 ASN1_GENERALIZEDTIME
*comp_time
= NULL
;
2569 ASN1_ENUMERATED
*rtmp
= NULL
;
2571 ASN1_TIME
*revDate
= NULL
;
2573 i
= unpack_revinfo(&revDate
, &reason_code
, &hold
, &comp_time
, str
);
2578 if (rev
&& !X509_REVOKED_set_revocationDate(rev
, revDate
))
2581 if (rev
&& (reason_code
!= OCSP_REVOKED_STATUS_NOSTATUS
)) {
2582 rtmp
= ASN1_ENUMERATED_new();
2583 if (!rtmp
|| !ASN1_ENUMERATED_set(rtmp
, reason_code
))
2585 if (!X509_REVOKED_add1_ext_i2d(rev
, NID_crl_reason
, rtmp
, 0, 0))
2589 if (rev
&& comp_time
) {
2590 if (!X509_REVOKED_add1_ext_i2d
2591 (rev
, NID_invalidity_date
, comp_time
, 0, 0))
2595 if (!X509_REVOKED_add1_ext_i2d
2596 (rev
, NID_hold_instruction_code
, hold
, 0, 0))
2600 if (reason_code
!= OCSP_REVOKED_STATUS_NOSTATUS
)
2609 ASN1_OBJECT_free(hold
);
2610 ASN1_GENERALIZEDTIME_free(comp_time
);
2611 ASN1_ENUMERATED_free(rtmp
);
2612 ASN1_TIME_free(revDate
);
2617 static int old_entry_print(ASN1_OBJECT
*obj
, ASN1_STRING
*str
)
2619 char buf
[25], *pbuf
, *p
;
2621 j
= i2a_ASN1_OBJECT(bio_err
, obj
);
2623 for (j
= 22 - j
; j
> 0; j
--)
2627 BIO_puts(bio_err
, buf
);
2629 if (str
->type
== V_ASN1_PRINTABLESTRING
)
2630 BIO_printf(bio_err
, "PRINTABLE:'");
2631 else if (str
->type
== V_ASN1_T61STRING
)
2632 BIO_printf(bio_err
, "T61STRING:'");
2633 else if (str
->type
== V_ASN1_IA5STRING
)
2634 BIO_printf(bio_err
, "IA5STRING:'");
2635 else if (str
->type
== V_ASN1_UNIVERSALSTRING
)
2636 BIO_printf(bio_err
, "UNIVERSALSTRING:'");
2638 BIO_printf(bio_err
, "ASN.1 %2d:'", str
->type
);
2640 p
= (char *)str
->data
;
2641 for (j
= str
->length
; j
> 0; j
--) {
2642 if ((*p
>= ' ') && (*p
<= '~'))
2643 BIO_printf(bio_err
, "%c", *p
);
2645 BIO_printf(bio_err
, "\\0x%02X", *p
);
2646 else if ((unsigned char)*p
== 0xf7)
2647 BIO_printf(bio_err
, "^?");
2649 BIO_printf(bio_err
, "^%c", *p
+ '@');
2652 BIO_printf(bio_err
, "'\n");
2656 int unpack_revinfo(ASN1_TIME
**prevtm
, int *preason
, ASN1_OBJECT
**phold
,
2657 ASN1_GENERALIZEDTIME
**pinvtm
, const char *str
)
2660 char *rtime_str
, *reason_str
= NULL
, *arg_str
= NULL
, *p
;
2661 int reason_code
= -1;
2664 ASN1_OBJECT
*hold
= NULL
;
2665 ASN1_GENERALIZEDTIME
*comp_time
= NULL
;
2667 tmp
= BUF_strdup(str
);
2669 BIO_printf(bio_err
, "memory allocation failure\n");
2673 p
= strchr(tmp
, ',');
2689 *prevtm
= ASN1_UTCTIME_new();
2691 BIO_printf(bio_err
, "memory allocation failure\n");
2694 if (!ASN1_UTCTIME_set_string(*prevtm
, rtime_str
)) {
2695 BIO_printf(bio_err
, "invalid revocation date %s\n", rtime_str
);
2700 for (i
= 0; i
< NUM_REASONS
; i
++) {
2701 if (!strcasecmp(reason_str
, crl_reasons
[i
])) {
2706 if (reason_code
== OCSP_REVOKED_STATUS_NOSTATUS
) {
2707 BIO_printf(bio_err
, "invalid reason code %s\n", reason_str
);
2711 if (reason_code
== 7)
2712 reason_code
= OCSP_REVOKED_STATUS_REMOVEFROMCRL
;
2713 else if (reason_code
== 8) { /* Hold instruction */
2715 BIO_printf(bio_err
, "missing hold instruction\n");
2718 reason_code
= OCSP_REVOKED_STATUS_CERTIFICATEHOLD
;
2719 hold
= OBJ_txt2obj(arg_str
, 0);
2722 BIO_printf(bio_err
, "invalid object identifier %s\n",
2729 ASN1_OBJECT_free(hold
);
2730 } else if ((reason_code
== 9) || (reason_code
== 10)) {
2732 BIO_printf(bio_err
, "missing compromised time\n");
2735 comp_time
= ASN1_GENERALIZEDTIME_new();
2737 BIO_printf(bio_err
, "memory allocation failure\n");
2740 if (!ASN1_GENERALIZEDTIME_set_string(comp_time
, arg_str
)) {
2741 BIO_printf(bio_err
, "invalid compromised time %s\n", arg_str
);
2744 if (reason_code
== 9)
2745 reason_code
= OCSP_REVOKED_STATUS_KEYCOMPROMISE
;
2747 reason_code
= OCSP_REVOKED_STATUS_CACOMPROMISE
;
2752 *preason
= reason_code
;
2754 *pinvtm
= comp_time
;
2764 ASN1_GENERALIZEDTIME_free(comp_time
);