1 /* $OpenBSD: ca.c,v 1.26 2018/02/07 05:47:55 jsing Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
59 /* The PPKI stuff has been donated by Jeff Barber <jeffb@issl.atl.hp.com> */
61 #include <sys/types.h>
72 #include <openssl/bio.h>
73 #include <openssl/bn.h>
74 #include <openssl/conf.h>
75 #include <openssl/err.h>
76 #include <openssl/evp.h>
77 #include <openssl/objects.h>
78 #include <openssl/ocsp.h>
79 #include <openssl/pem.h>
80 #include <openssl/txt_db.h>
81 #include <openssl/x509.h>
82 #include <openssl/x509v3.h>
84 #define BASE_SECTION "ca"
86 #define ENV_DEFAULT_CA "default_ca"
88 #define STRING_MASK "string_mask"
89 #define UTF8_IN "utf8"
91 #define ENV_NEW_CERTS_DIR "new_certs_dir"
92 #define ENV_CERTIFICATE "certificate"
93 #define ENV_SERIAL "serial"
94 #define ENV_CRLNUMBER "crlnumber"
95 #define ENV_PRIVATE_KEY "private_key"
96 #define ENV_DEFAULT_DAYS "default_days"
97 #define ENV_DEFAULT_STARTDATE "default_startdate"
98 #define ENV_DEFAULT_ENDDATE "default_enddate"
99 #define ENV_DEFAULT_CRL_DAYS "default_crl_days"
100 #define ENV_DEFAULT_CRL_HOURS "default_crl_hours"
101 #define ENV_DEFAULT_MD "default_md"
102 #define ENV_DEFAULT_EMAIL_DN "email_in_dn"
103 #define ENV_PRESERVE "preserve"
104 #define ENV_POLICY "policy"
105 #define ENV_EXTENSIONS "x509_extensions"
106 #define ENV_CRLEXT "crl_extensions"
107 #define ENV_MSIE_HACK "msie_hack"
108 #define ENV_NAMEOPT "name_opt"
109 #define ENV_CERTOPT "cert_opt"
110 #define ENV_EXTCOPY "copy_extensions"
111 #define ENV_UNIQUE_SUBJECT "unique_subject"
113 #define ENV_DATABASE "database"
115 /* Additional revocation information types */
117 #define REV_NONE 0 /* No addditional information */
118 #define REV_CRL_REASON 1 /* Value is CRL reason code */
119 #define REV_HOLD 2 /* Value is hold instruction */
120 #define REV_KEY_COMPROMISE 3 /* Value is cert key compromise time */
121 #define REV_CA_COMPROMISE 4 /* Value is CA key compromise time */
123 static const char *ca_usage
[] = {
126 " -verbose - Talk a lot while doing things\n",
127 " -config file - A config file\n",
128 " -name arg - The particular CA definition to use\n",
129 " -gencrl - Generate a new CRL\n",
130 " -crldays days - Days is when the next CRL is due\n",
131 " -crlhours hours - Hours is when the next CRL is due\n",
132 " -startdate YYMMDDHHMMSSZ - certificate validity notBefore\n",
133 " -enddate YYMMDDHHMMSSZ - certificate validity notAfter (overrides -days)\n",
134 " -days arg - number of days to certify the certificate for\n",
135 " -md arg - md to use, one of md5 or sha1\n",
136 " -policy arg - The CA 'policy' to support\n",
137 " -keyfile arg - private key file\n",
138 " -keyform arg - private key file format (PEM)\n",
139 " -key arg - key to decode the private key if it is encrypted\n",
140 " -cert file - The CA certificate\n",
141 " -selfsign - sign a certificate with the key associated with it\n",
142 " -in file - The input PEM encoded certificate request(s)\n",
143 " -out file - Where to put the output file(s)\n",
144 " -outdir dir - Where to put output certificates\n",
145 " -infiles .... - The last argument, requests to process\n",
146 " -spkac file - File contains DN and signed public key and challenge\n",
147 " -ss_cert file - File contains a self signed cert to sign\n",
148 " -preserveDN - Don't re-order the DN\n",
149 " -noemailDN - Don't add the EMAIL field into certificate' subject\n",
150 " -batch - Don't ask questions\n",
151 " -msie_hack - msie modifications to handle all those universal strings\n",
152 " -revoke file - Revoke a certificate (given in file)\n",
153 " -subj arg - Use arg instead of request's subject\n",
154 " -utf8 - input characters are UTF8 (default ASCII)\n",
155 " -multivalue-rdn - enable support for multivalued RDNs\n",
156 " -extensions .. - Extension section (override value in config file)\n",
157 " -extfile file - Configuration file with X509v3 extentions to add\n",
158 " -crlexts .. - CRL extension section (override value in config file)\n",
159 " -status serial - Shows certificate status given the serial number\n",
160 " -updatedb - Updates db for expired certificates\n",
164 static void lookup_fail(const char *name
, const char *tag
);
165 static int certify(X509
** xret
, char *infile
, EVP_PKEY
* pkey
, X509
* x509
,
166 const EVP_MD
* dgst
, STACK_OF(OPENSSL_STRING
) * sigopts
,
167 STACK_OF(CONF_VALUE
) * policy
, CA_DB
* db
, BIGNUM
* serial
, char *subj
,
168 unsigned long chtype
, int multirdn
, int email_dn
, char *startdate
,
169 char *enddate
, long days
, int batch
, char *ext_sect
, CONF
* conf
,
170 int verbose
, unsigned long certopt
, unsigned long nameopt
,
171 int default_op
, int ext_copy
, int selfsign
);
172 static int certify_cert(X509
** xret
, char *infile
, EVP_PKEY
* pkey
,
173 X509
* x509
, const EVP_MD
* dgst
, STACK_OF(OPENSSL_STRING
) * sigopts
,
174 STACK_OF(CONF_VALUE
) * policy
, CA_DB
* db
, BIGNUM
* serial
, char *subj
,
175 unsigned long chtype
, int multirdn
, int email_dn
, char *startdate
,
176 char *enddate
, long days
, int batch
, char *ext_sect
, CONF
* conf
,
177 int verbose
, unsigned long certopt
, unsigned long nameopt
, int default_op
,
179 static int certify_spkac(X509
** xret
, char *infile
, EVP_PKEY
* pkey
,
180 X509
* x509
, const EVP_MD
* dgst
, STACK_OF(OPENSSL_STRING
) * sigopts
,
181 STACK_OF(CONF_VALUE
) * policy
, CA_DB
* db
, BIGNUM
* serial
, char *subj
,
182 unsigned long chtype
, int multirdn
, int email_dn
, char *startdate
,
183 char *enddate
, long days
, char *ext_sect
, CONF
* conf
, int verbose
,
184 unsigned long certopt
, unsigned long nameopt
, int default_op
, int ext_copy
);
185 static void write_new_certificate(BIO
* bp
, X509
* x
, int output_der
,
187 static int do_body(X509
** xret
, EVP_PKEY
* pkey
, X509
* x509
,
188 const EVP_MD
* dgst
, STACK_OF(OPENSSL_STRING
) * sigopts
,
189 STACK_OF(CONF_VALUE
) * policy
, CA_DB
* db
, BIGNUM
* serial
, char *subj
,
190 unsigned long chtype
, int multirdn
, int email_dn
, char *startdate
,
191 char *enddate
, long days
, int batch
, int verbose
, X509_REQ
* req
,
192 char *ext_sect
, CONF
* conf
, unsigned long certopt
, unsigned long nameopt
,
193 int default_op
, int ext_copy
, int selfsign
);
194 static int do_revoke(X509
* x509
, CA_DB
* db
, int ext
, char *extval
);
195 static int get_certificate_status(const char *ser_status
, CA_DB
* db
);
196 static int do_updatedb(CA_DB
* db
);
197 static int check_time_format(const char *str
);
198 static char * bin2hex(unsigned char *, size_t);
199 char *make_revocation_str(int rev_type
, char *rev_arg
);
200 int make_revoked(X509_REVOKED
* rev
, const char *str
);
201 int old_entry_print(BIO
* bp
, ASN1_OBJECT
* obj
, ASN1_STRING
* str
);
202 static CONF
*conf
= NULL
;
203 static CONF
*extconf
= NULL
;
204 static char *section
= NULL
;
206 static int preserve
= 0;
207 static int msie_hack
= 0;
211 * Set a certificate time based on user provided input. Make sure
212 * what we put in the certificate is legit for RFC 5280. Returns
213 * 0 on success, -1 on an invalid time string. Strings must be
214 * YYYYMMDDHHMMSSZ for post 2050 dates. YYYYMMDDHHMMSSZ or
215 * YYMMDDHHMMSSZ is accepted for pre 2050 dates, and fixed up to
216 * be the correct format in the certificate.
219 setCertificateTime(ASN1_TIME
*x509time
, char *timestring
)
222 memset(&tm1
, 0, sizeof(tm1
));
223 if (ASN1_time_parse(timestring
, strlen(timestring
), &tm1
, 0) == -1)
225 if (!ASN1_TIME_set_tm(x509time
, &tm1
))
231 ca_main(int argc
, char **argv
)
233 char *key
= NULL
, *passargin
= NULL
;
250 char *configfile
= NULL
;
253 char *keyfile
= NULL
;
254 char *certfile
= NULL
;
255 int keyform
= FORMAT_PEM
;
257 char *spkac_file
= NULL
;
258 char *ss_cert_file
= NULL
;
259 char *ser_status
= NULL
;
260 EVP_PKEY
*pkey
= NULL
;
262 char *outfile
= NULL
;
264 char *serialfile
= NULL
;
265 char *crlnumberfile
= NULL
;
266 char *extensions
= NULL
;
267 char *extfile
= NULL
;
269 unsigned long chtype
= MBSTRING_ASC
;
271 char *tmp_email_dn
= NULL
;
272 char *crl_ext
= NULL
;
273 int rev_type
= REV_NONE
;
274 char *rev_arg
= NULL
;
275 BIGNUM
*serial
= NULL
;
276 BIGNUM
*crlnumber
= NULL
;
277 char *startdate
= NULL
;
278 char *enddate
= NULL
;
282 unsigned long nameopt
= 0, certopt
= 0;
284 int ext_copy
= EXT_COPY_NONE
;
286 X509
*x509
= NULL
, *x509p
= NULL
;
288 BIO
*in
= NULL
, *out
= NULL
, *Sout
= NULL
, *Cout
= NULL
;
291 X509_CRL
*crl
= NULL
;
292 X509_REVOKED
*r
= NULL
;
294 ASN1_INTEGER
*tmpser
;
299 const EVP_MD
*dgst
= NULL
;
300 STACK_OF(CONF_VALUE
) * attribs
= NULL
;
301 STACK_OF(X509
) * cert_sk
= NULL
;
302 STACK_OF(OPENSSL_STRING
) * sigopts
= NULL
;
304 const char *errstr
= NULL
;
307 if (single_execution
) {
308 if (pledge("stdio cpath wpath rpath tty", NULL
) == -1) {
324 if (strcmp(*argv
, "-verbose") == 0)
326 else if (strcmp(*argv
, "-config") == 0) {
329 configfile
= *(++argv
);
330 } else if (strcmp(*argv
, "-name") == 0) {
334 } else if (strcmp(*argv
, "-subj") == 0) {
339 } else if (strcmp(*argv
, "-utf8") == 0)
340 chtype
= MBSTRING_UTF8
;
341 else if (strcmp(*argv
, "-create_serial") == 0)
343 else if (strcmp(*argv
, "-multivalue-rdn") == 0)
345 else if (strcmp(*argv
, "-startdate") == 0) {
348 startdate
= *(++argv
);
349 } else if (strcmp(*argv
, "-enddate") == 0) {
353 } else if (strcmp(*argv
, "-days") == 0) {
356 days
= strtonum(*(++argv
), 0, LONG_MAX
, &errstr
);
359 } else if (strcmp(*argv
, "-md") == 0) {
363 } else if (strcmp(*argv
, "-policy") == 0) {
367 } else if (strcmp(*argv
, "-keyfile") == 0) {
371 } else if (strcmp(*argv
, "-keyform") == 0) {
374 keyform
= str2fmt(*(++argv
));
375 } else if (strcmp(*argv
, "-passin") == 0) {
378 passargin
= *(++argv
);
379 } else if (strcmp(*argv
, "-key") == 0) {
383 } else if (strcmp(*argv
, "-cert") == 0) {
386 certfile
= *(++argv
);
387 } else if (strcmp(*argv
, "-selfsign") == 0)
389 else if (strcmp(*argv
, "-in") == 0) {
394 } else if (strcmp(*argv
, "-out") == 0) {
398 } else if (strcmp(*argv
, "-outdir") == 0) {
402 } else if (strcmp(*argv
, "-sigopt") == 0) {
406 sigopts
= sk_OPENSSL_STRING_new_null();
408 !sk_OPENSSL_STRING_push(sigopts
, *(++argv
)))
410 } else if (strcmp(*argv
, "-notext") == 0)
412 else if (strcmp(*argv
, "-batch") == 0)
414 else if (strcmp(*argv
, "-preserveDN") == 0)
416 else if (strcmp(*argv
, "-noemailDN") == 0)
418 else if (strcmp(*argv
, "-gencrl") == 0)
420 else if (strcmp(*argv
, "-msie_hack") == 0)
422 else if (strcmp(*argv
, "-crldays") == 0) {
425 crldays
= strtonum(*(++argv
), 0, LONG_MAX
, &errstr
);
428 } else if (strcmp(*argv
, "-crlhours") == 0) {
431 crlhours
= strtonum(*(++argv
), 0, LONG_MAX
, &errstr
);
434 } else if (strcmp(*argv
, "-crlsec") == 0) {
437 crlsec
= strtonum(*(++argv
), 0, LONG_MAX
, &errstr
);
440 } else if (strcmp(*argv
, "-infiles") == 0) {
445 } else if (strcmp(*argv
, "-ss_cert") == 0) {
448 ss_cert_file
= *(++argv
);
450 } else if (strcmp(*argv
, "-spkac") == 0) {
453 spkac_file
= *(++argv
);
455 } else if (strcmp(*argv
, "-revoke") == 0) {
460 } else if (strcmp(*argv
, "-extensions") == 0) {
463 extensions
= *(++argv
);
464 } else if (strcmp(*argv
, "-extfile") == 0) {
468 } else if (strcmp(*argv
, "-status") == 0) {
471 ser_status
= *(++argv
);
472 } else if (strcmp(*argv
, "-updatedb") == 0) {
474 } else if (strcmp(*argv
, "-crlexts") == 0) {
478 } else if (strcmp(*argv
, "-crl_reason") == 0) {
482 rev_type
= REV_CRL_REASON
;
483 } else if (strcmp(*argv
, "-crl_hold") == 0) {
488 } else if (strcmp(*argv
, "-crl_compromise") == 0) {
492 rev_type
= REV_KEY_COMPROMISE
;
493 } else if (strcmp(*argv
, "-crl_CA_compromise") == 0) {
497 rev_type
= REV_CA_COMPROMISE
;
502 BIO_printf(bio_err
, "invalid argument %s: %s\n",
505 BIO_printf(bio_err
, "unknown option %s\n", *argv
);
516 for (pp2
= ca_usage
; (*pp2
!= NULL
); pp2
++)
517 BIO_printf(bio_err
, "%s", *pp2
);
521 /*****************************************************************/
523 if (configfile
== NULL
)
524 configfile
= getenv("OPENSSL_CONF");
525 if (configfile
== NULL
) {
526 if ((tofree
= make_config_name()) == NULL
) {
527 BIO_printf(bio_err
, "error making config file name\n");
532 BIO_printf(bio_err
, "Using configuration from %s\n", configfile
);
533 conf
= NCONF_new(NULL
);
534 if (NCONF_load(conf
, configfile
, &errorline
) <= 0) {
537 "error loading the config file '%s'\n",
541 "error on line %ld of config file '%s'\n",
542 errorline
, configfile
);
548 /* Lets get the config section we are using */
549 if (section
== NULL
) {
550 section
= NCONF_get_string(conf
, BASE_SECTION
, ENV_DEFAULT_CA
);
551 if (section
== NULL
) {
552 lookup_fail(BASE_SECTION
, ENV_DEFAULT_CA
);
557 p
= NCONF_get_string(conf
, NULL
, "oid_file");
563 oid_bio
= BIO_new_file(p
, "r");
564 if (oid_bio
== NULL
) {
567 "problems opening %s for extra oid's\n", p);
568 ERR_print_errors(bio_err);
572 OBJ_create_objects(oid_bio
);
576 if (!add_oid_section(bio_err
, conf
)) {
577 ERR_print_errors(bio_err
);
581 f
= NCONF_get_string(conf
, section
, STRING_MASK
);
585 if (f
&& !ASN1_STRING_set_default_mask_asc(f
)) {
587 "Invalid global string mask setting %s\n", f
);
590 if (chtype
!= MBSTRING_UTF8
) {
591 f
= NCONF_get_string(conf
, section
, UTF8_IN
);
594 else if (!strcmp(f
, "yes"))
595 chtype
= MBSTRING_UTF8
;
597 db_attr
.unique_subject
= 1;
598 p
= NCONF_get_string(conf
, section
, ENV_UNIQUE_SUBJECT
);
600 db_attr
.unique_subject
= parse_yesno(p
, 1);
604 in
= BIO_new(BIO_s_file());
605 out
= BIO_new(BIO_s_file());
606 Sout
= BIO_new(BIO_s_file());
607 Cout
= BIO_new(BIO_s_file());
608 if ((in
== NULL
) || (out
== NULL
) || (Sout
== NULL
) || (Cout
== NULL
)) {
609 ERR_print_errors(bio_err
);
612 /*****************************************************************/
613 /* report status of cert with serial number given on command line */
615 if ((dbfile
= NCONF_get_string(conf
, section
,
616 ENV_DATABASE
)) == NULL
) {
617 lookup_fail(section
, ENV_DATABASE
);
620 db
= load_index(dbfile
, &db_attr
);
624 if (!index_index(db
))
627 if (get_certificate_status(ser_status
, db
) != 1)
628 BIO_printf(bio_err
, "Error verifying serial %s!\n",
632 /*****************************************************************/
633 /* we definitely need a private key, so let's get it */
635 if ((keyfile
== NULL
) && ((keyfile
= NCONF_get_string(conf
,
636 section
, ENV_PRIVATE_KEY
)) == NULL
)) {
637 lookup_fail(section
, ENV_PRIVATE_KEY
);
642 if (!app_passwd(bio_err
, passargin
, NULL
, &key
, NULL
)) {
643 BIO_printf(bio_err
, "Error getting password\n");
647 pkey
= load_key(bio_err
, keyfile
, keyform
, 0, key
, "CA private key");
649 explicit_bzero(key
, strlen(key
));
651 /* load_key() has already printed an appropriate message */
654 /*****************************************************************/
655 /* we need a certificate */
656 if (!selfsign
|| spkac_file
|| ss_cert_file
|| gencrl
) {
657 if ((certfile
== NULL
) &&
658 ((certfile
= NCONF_get_string(conf
,
659 section
, ENV_CERTIFICATE
)) == NULL
)) {
660 lookup_fail(section
, ENV_CERTIFICATE
);
663 x509
= load_cert(bio_err
, certfile
, FORMAT_PEM
, NULL
,
668 if (!X509_check_private_key(x509
, pkey
)) {
670 "CA certificate and CA private key do not match\n");
677 f
= NCONF_get_string(conf
, BASE_SECTION
, ENV_PRESERVE
);
680 if ((f
!= NULL
) && ((*f
== 'y') || (*f
== 'Y')))
682 f
= NCONF_get_string(conf
, BASE_SECTION
, ENV_MSIE_HACK
);
685 if ((f
!= NULL
) && ((*f
== 'y') || (*f
== 'Y')))
688 f
= NCONF_get_string(conf
, section
, ENV_NAMEOPT
);
691 if (!set_name_ex(&nameopt
, f
)) {
693 "Invalid name options: \"%s\"\n", f
);
700 f
= NCONF_get_string(conf
, section
, ENV_CERTOPT
);
703 if (!set_cert_ex(&certopt
, f
)) {
705 "Invalid certificate options: \"%s\"\n", f
);
712 f
= NCONF_get_string(conf
, section
, ENV_EXTCOPY
);
715 if (!set_ext_copy(&ext_copy
, f
)) {
717 "Invalid extension copy option: \"%s\"\n", f
);
723 /*****************************************************************/
724 /* lookup where to write new certificates */
725 if (outdir
== NULL
&& req
) {
726 if ((outdir
= NCONF_get_string(conf
, section
,
727 ENV_NEW_CERTS_DIR
)) == NULL
) {
728 BIO_printf(bio_err
, "output directory %s not defined\n",
733 /*****************************************************************/
734 /* we need to load the database file */
735 if ((dbfile
= NCONF_get_string(conf
, section
, ENV_DATABASE
)) == NULL
) {
736 lookup_fail(section
, ENV_DATABASE
);
739 db
= load_index(dbfile
, &db_attr
);
743 /* Lets check some fields */
744 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(db
->db
->data
); i
++) {
745 pp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, i
);
746 if ((pp
[DB_type
][0] != DB_TYPE_REV
) &&
747 (pp
[DB_rev_date
][0] != '\0')) {
748 BIO_printf(bio_err
, "entry %d: not revoked yet, but has a revocation date\n", i
+ 1);
751 if ((pp
[DB_type
][0] == DB_TYPE_REV
) &&
752 !make_revoked(NULL
, pp
[DB_rev_date
])) {
753 BIO_printf(bio_err
, " in entry %d\n", i
+ 1);
756 if (!check_time_format((char *) pp
[DB_exp_date
])) {
757 BIO_printf(bio_err
, "entry %d: invalid expiry date\n",
767 if ((j
& 1) || (j
< 2)) {
769 "entry %d: bad serial number length (%d)\n",
774 if (!(((*p
>= '0') && (*p
<= '9')) ||
775 ((*p
>= 'A') && (*p
<= 'F')) ||
776 ((*p
>= 'a') && (*p
<= 'f')))) {
777 BIO_printf(bio_err
, "entry %d: bad serial number characters, char pos %ld, char is '%c'\n", i
+ 1, (long) (p
- pp
[DB_serial
]), *p
);
784 BIO_set_fp(out
, stdout
, BIO_NOCLOSE
| BIO_FP_TEXT
); /* cannot fail */
785 TXT_DB_write(out
, db
->db
);
786 BIO_printf(bio_err
, "%d entries loaded from the database\n",
787 sk_OPENSSL_PSTRING_num(db
->db
->data
));
788 BIO_printf(bio_err
, "generating index\n");
790 if (!index_index(db
))
793 /*****************************************************************/
794 /* Update the db file for expired certificates */
797 BIO_printf(bio_err
, "Updating %s ...\n", dbfile
);
801 BIO_printf(bio_err
, "Malloc failure\n");
806 "No entries found to mark expired\n");
808 if (!save_index(dbfile
, "new", db
))
811 if (!rotate_index(dbfile
, "new", "old"))
816 "Done. %d entries marked as expired\n", i
);
819 /*****************************************************************/
820 /* Read extentions config file */
822 extconf
= NCONF_new(NULL
);
823 if (NCONF_load(extconf
, extfile
, &errorline
) <= 0) {
826 "ERROR: loading the config file '%s'\n",
830 "ERROR: on line %ld of config file '%s'\n",
837 "Successfully loaded extensions file %s\n",
840 /* We can have sections in the ext file */
841 if (!extensions
&& !(extensions
= NCONF_get_string(extconf
,
842 "default", "extensions")))
843 extensions
= "default";
845 /*****************************************************************/
847 if (outfile
!= NULL
) {
848 if (BIO_write_filename(Sout
, outfile
) <= 0) {
853 BIO_set_fp(Sout
, stdout
, BIO_NOCLOSE
| BIO_FP_TEXT
);
856 if ((md
== NULL
) && ((md
= NCONF_get_string(conf
, section
,
857 ENV_DEFAULT_MD
)) == NULL
)) {
858 lookup_fail(section
, ENV_DEFAULT_MD
);
861 if (!strcmp(md
, "default")) {
863 if (EVP_PKEY_get_default_digest_nid(pkey
, &def_nid
) <= 0) {
864 BIO_puts(bio_err
, "no default digest\n");
867 md
= (char *) OBJ_nid2sn(def_nid
);
869 if ((dgst
= EVP_get_digestbyname(md
)) == NULL
) {
871 "%s is an unsupported message digest type\n", md
);
875 if ((email_dn
== 1) && ((tmp_email_dn
= NCONF_get_string(conf
,
876 section
, ENV_DEFAULT_EMAIL_DN
)) != NULL
)) {
877 if (strcmp(tmp_email_dn
, "no") == 0)
881 BIO_printf(bio_err
, "message digest is %s\n",
882 OBJ_nid2ln(dgst
->type
));
883 if ((policy
== NULL
) && ((policy
= NCONF_get_string(conf
,
884 section
, ENV_POLICY
)) == NULL
)) {
885 lookup_fail(section
, ENV_POLICY
);
889 BIO_printf(bio_err
, "policy is %s\n", policy
);
891 if ((serialfile
= NCONF_get_string(conf
, section
,
892 ENV_SERIAL
)) == NULL
) {
893 lookup_fail(section
, ENV_SERIAL
);
898 * no '-extfile' option, so we look for extensions in
899 * the main configuration file
902 extensions
= NCONF_get_string(conf
, section
,
908 /* Check syntax of file */
910 X509V3_set_ctx_test(&ctx
);
911 X509V3_set_nconf(&ctx
, conf
);
912 if (!X509V3_EXT_add_nconf(conf
, &ctx
,
915 "Error Loading extension section %s\n",
922 if (startdate
== NULL
) {
923 startdate
= NCONF_get_string(conf
, section
,
924 ENV_DEFAULT_STARTDATE
);
925 if (startdate
== NULL
)
928 if (startdate
== NULL
)
931 if (enddate
== NULL
) {
932 enddate
= NCONF_get_string(conf
, section
,
933 ENV_DEFAULT_ENDDATE
);
937 if (days
== 0 && enddate
== NULL
) {
938 if (!NCONF_get_number(conf
, section
,
939 ENV_DEFAULT_DAYS
, &days
))
942 if (enddate
== NULL
&& days
== 0) {
944 "cannot lookup how many days to certify for\n");
947 if ((serial
= load_serial(serialfile
, create_ser
, NULL
)) ==
950 "error while loading serial number\n");
954 if (BN_is_zero(serial
))
956 "next serial number is 00\n");
958 if ((f
= BN_bn2hex(serial
)) == NULL
)
961 "next serial number is %s\n", f
);
965 if ((attribs
= NCONF_get_section(conf
, policy
)) == NULL
) {
967 "unable to find 'section' for %s\n", policy
);
970 if ((cert_sk
= sk_X509_new_null()) == NULL
) {
971 BIO_printf(bio_err
, "Memory allocation failure\n");
974 if (spkac_file
!= NULL
) {
976 j
= certify_spkac(&x
, spkac_file
, pkey
, x509
, dgst
,
977 sigopts
, attribs
, db
, serial
, subj
, chtype
,
978 multirdn
, email_dn
, startdate
, enddate
, days
,
979 extensions
, conf
, verbose
, certopt
, nameopt
,
980 default_op
, ext_copy
);
985 BIO_printf(bio_err
, "\n");
986 if (!BN_add_word(serial
, 1))
988 if (!sk_X509_push(cert_sk
, x
)) {
990 "Memory allocation failure\n");
999 if (ss_cert_file
!= NULL
) {
1001 j
= certify_cert(&x
, ss_cert_file
, pkey
, x509
, dgst
,
1002 sigopts
, attribs
, db
, serial
, subj
, chtype
,
1003 multirdn
, email_dn
, startdate
, enddate
, days
, batch
,
1004 extensions
, conf
, verbose
, certopt
, nameopt
,
1005 default_op
, ext_copy
);
1010 BIO_printf(bio_err
, "\n");
1011 if (!BN_add_word(serial
, 1))
1013 if (!sk_X509_push(cert_sk
, x
)) {
1015 "Memory allocation failure\n");
1020 if (infile
!= NULL
) {
1022 j
= certify(&x
, infile
, pkey
, x509p
, dgst
, sigopts
,
1023 attribs
, db
, serial
, subj
, chtype
, multirdn
,
1024 email_dn
, startdate
, enddate
, days
, batch
,
1025 extensions
, conf
, verbose
, certopt
, nameopt
,
1026 default_op
, ext_copy
, selfsign
);
1031 BIO_printf(bio_err
, "\n");
1032 if (!BN_add_word(serial
, 1))
1034 if (!sk_X509_push(cert_sk
, x
)) {
1036 "Memory allocation failure\n");
1041 for (i
= 0; i
< argc
; i
++) {
1043 j
= certify(&x
, argv
[i
], pkey
, x509p
, dgst
, sigopts
,
1044 attribs
, db
, serial
, subj
, chtype
, multirdn
,
1045 email_dn
, startdate
, enddate
, days
, batch
,
1046 extensions
, conf
, verbose
, certopt
, nameopt
,
1047 default_op
, ext_copy
, selfsign
);
1052 BIO_printf(bio_err
, "\n");
1053 if (!BN_add_word(serial
, 1))
1055 if (!sk_X509_push(cert_sk
, x
)) {
1057 "Memory allocation failure\n");
1063 * we have a stack of newly certified certificates and a data
1064 * base and serial number that need updating
1067 if (sk_X509_num(cert_sk
) > 0) {
1071 BIO_printf(bio_err
, "\n%d out of %d certificate requests certified, commit? [y/n]", total_done
, total
);
1072 (void) BIO_flush(bio_err
);
1073 if (!fgets(answer
, sizeof answer
- 1, stdin
)) {
1074 BIO_printf(bio_err
, "CERTIFICATION CANCELED: I/O error\n");
1078 if ((answer
[0] != 'y') && (answer
[0] != 'Y')) {
1079 BIO_printf(bio_err
, "CERTIFICATION CANCELED\n");
1084 BIO_printf(bio_err
, "Write out database with %d new entries\n", sk_X509_num(cert_sk
));
1086 if (!save_serial(serialfile
, "new", serial
, NULL
))
1089 if (!save_index(dbfile
, "new", db
))
1093 BIO_printf(bio_err
, "writing new certificates\n");
1094 for (i
= 0; i
< sk_X509_num(cert_sk
); i
++) {
1097 unsigned char *data
;
1098 char pempath
[PATH_MAX
];
1100 x
= sk_X509_value(cert_sk
, i
);
1102 j
= x
->cert_info
->serialNumber
->length
;
1103 data
= (unsigned char *)x
->cert_info
->serialNumber
->data
;
1105 serialstr
= bin2hex(data
, j
);
1107 serialstr
= strdup("00");
1109 k
= snprintf(pempath
, sizeof(pempath
),
1110 "%s/%s.pem", outdir
, serialstr
);
1112 if (k
== -1 || k
>= sizeof(pempath
)) {
1114 "certificate file name too long\n");
1119 "memory allocation failed\n");
1123 BIO_printf(bio_err
, "writing %s\n", pempath
);
1125 if (BIO_write_filename(Cout
, pempath
) <= 0) {
1129 write_new_certificate(Cout
, x
, 0, notext
);
1130 write_new_certificate(Sout
, x
, output_der
, notext
);
1133 if (sk_X509_num(cert_sk
)) {
1134 /* Rename the database and the serial file */
1135 if (!rotate_serial(serialfile
, "new", "old"))
1138 if (!rotate_index(dbfile
, "new", "old"))
1141 BIO_printf(bio_err
, "Data Base Updated\n");
1144 /*****************************************************************/
1148 crl_ext
= NCONF_get_string(conf
, section
, ENV_CRLEXT
);
1153 /* Check syntax of file */
1155 X509V3_set_ctx_test(&ctx
);
1156 X509V3_set_nconf(&ctx
, conf
);
1157 if (!X509V3_EXT_add_nconf(conf
, &ctx
, crl_ext
, NULL
)) {
1159 "Error Loading CRL extension section %s\n",
1165 if ((crlnumberfile
= NCONF_get_string(conf
, section
,
1166 ENV_CRLNUMBER
)) != NULL
)
1167 if ((crlnumber
= load_serial(crlnumberfile
, 0,
1170 "error while loading CRL number\n");
1173 if (!crldays
&& !crlhours
&& !crlsec
) {
1174 if (!NCONF_get_number(conf
, section
,
1175 ENV_DEFAULT_CRL_DAYS
, &crldays
))
1177 if (!NCONF_get_number(conf
, section
,
1178 ENV_DEFAULT_CRL_HOURS
, &crlhours
))
1182 if ((crldays
== 0) && (crlhours
== 0) && (crlsec
== 0)) {
1183 BIO_printf(bio_err
, "cannot lookup how long until the next CRL is issued\n");
1187 BIO_printf(bio_err
, "making CRL\n");
1188 if ((crl
= X509_CRL_new()) == NULL
)
1190 if (!X509_CRL_set_issuer_name(crl
, X509_get_subject_name(x509
)))
1193 tmptm
= ASN1_TIME_new();
1196 X509_gmtime_adj(tmptm
, 0);
1197 X509_CRL_set_lastUpdate(crl
, tmptm
);
1198 if (!X509_time_adj_ex(tmptm
, crldays
,
1199 crlhours
* 60 * 60 + crlsec
, NULL
)) {
1200 BIO_puts(bio_err
, "error setting CRL nextUpdate\n");
1203 X509_CRL_set_nextUpdate(crl
, tmptm
);
1205 ASN1_TIME_free(tmptm
);
1207 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(db
->db
->data
); i
++) {
1208 pp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, i
);
1209 if (pp
[DB_type
][0] == DB_TYPE_REV
) {
1210 if ((r
= X509_REVOKED_new()) == NULL
)
1212 j
= make_revoked(r
, pp
[DB_rev_date
]);
1217 if (!BN_hex2bn(&serial
, pp
[DB_serial
]))
1219 tmpser
= BN_to_ASN1_INTEGER(serial
, NULL
);
1224 X509_REVOKED_set_serialNumber(r
, tmpser
);
1225 ASN1_INTEGER_free(tmpser
);
1226 X509_CRL_add0_revoked(crl
, r
);
1231 * sort the data so it will be written in serial number order
1235 /* we now have a CRL */
1237 BIO_printf(bio_err
, "signing CRL\n");
1239 /* Add any extensions asked for */
1241 if (crl_ext
|| crlnumberfile
!= NULL
) {
1243 X509V3_set_ctx(&crlctx
, x509
, NULL
, NULL
, crl
, 0);
1244 X509V3_set_nconf(&crlctx
, conf
);
1247 if (!X509V3_EXT_CRL_add_nconf(conf
, &crlctx
,
1250 if (crlnumberfile
!= NULL
) {
1251 tmpser
= BN_to_ASN1_INTEGER(crlnumber
, NULL
);
1254 X509_CRL_add1_ext_i2d(crl
, NID_crl_number
,
1256 ASN1_INTEGER_free(tmpser
);
1258 if (!BN_add_word(crlnumber
, 1))
1262 if (crl_ext
|| crl_v2
) {
1263 if (!X509_CRL_set_version(crl
, 1))
1264 goto err
; /* version 2 CRL */
1266 if (crlnumberfile
!= NULL
) /* we have a CRL number that
1268 if (!save_serial(crlnumberfile
, "new", crlnumber
, NULL
))
1275 if (!do_X509_CRL_sign(bio_err
, crl
, pkey
, dgst
, sigopts
))
1278 PEM_write_bio_X509_CRL(Sout
, crl
);
1280 if (crlnumberfile
!= NULL
) /* Rename the crlnumber file */
1281 if (!rotate_serial(crlnumberfile
, "new", "old"))
1285 /*****************************************************************/
1287 if (infile
== NULL
) {
1288 BIO_printf(bio_err
, "no input files\n");
1292 revcert
= load_cert(bio_err
, infile
, FORMAT_PEM
,
1294 if (revcert
== NULL
)
1296 j
= do_revoke(revcert
, db
, rev_type
, rev_arg
);
1301 if (!save_index(dbfile
, "new", db
))
1304 if (!rotate_index(dbfile
, "new", "old"))
1307 BIO_printf(bio_err
, "Data Base Updated\n");
1310 /*****************************************************************/
1322 sk_X509_pop_free(cert_sk
, X509_free
);
1325 ERR_print_errors(bio_err
);
1326 if (free_key
&& key
)
1332 sk_OPENSSL_STRING_free(sigopts
);
1333 EVP_PKEY_free(pkey
);
1338 NCONF_free(extconf
);
1345 lookup_fail(const char *name
, const char *tag
)
1347 BIO_printf(bio_err
, "variable lookup failed for %s::%s\n", name
, tag
);
1351 certify(X509
** xret
, char *infile
, EVP_PKEY
* pkey
, X509
* x509
,
1352 const EVP_MD
* dgst
, STACK_OF(OPENSSL_STRING
) * sigopts
,
1353 STACK_OF(CONF_VALUE
) * policy
, CA_DB
* db
, BIGNUM
* serial
, char *subj
,
1354 unsigned long chtype
, int multirdn
, int email_dn
, char *startdate
,
1355 char *enddate
, long days
, int batch
, char *ext_sect
, CONF
* lconf
,
1356 int verbose
, unsigned long certopt
, unsigned long nameopt
, int default_op
,
1357 int ext_copy
, int selfsign
)
1359 X509_REQ
*req
= NULL
;
1361 EVP_PKEY
*pktmp
= NULL
;
1364 in
= BIO_new(BIO_s_file());
1366 if (BIO_read_filename(in
, infile
) <= 0) {
1370 if ((req
= PEM_read_bio_X509_REQ(in
, NULL
, NULL
, NULL
)) == NULL
) {
1371 BIO_printf(bio_err
, "Error reading certificate request in %s\n",
1376 X509_REQ_print(bio_err
, req
);
1378 BIO_printf(bio_err
, "Check that the request matches the signature\n");
1380 if (selfsign
&& !X509_REQ_check_private_key(req
, pkey
)) {
1382 "Certificate request and CA private key do not match\n");
1386 if ((pktmp
= X509_REQ_get_pubkey(req
)) == NULL
) {
1387 BIO_printf(bio_err
, "error unpacking public key\n");
1390 i
= X509_REQ_verify(req
, pktmp
);
1391 EVP_PKEY_free(pktmp
);
1394 BIO_printf(bio_err
, "Signature verification problems....\n");
1400 "Signature did not match the certificate request\n");
1403 BIO_printf(bio_err
, "Signature ok\n");
1405 ok
= do_body(xret
, pkey
, x509
, dgst
, sigopts
, policy
, db
, serial
,
1406 subj
, chtype
, multirdn
, email_dn
, startdate
, enddate
, days
, batch
,
1407 verbose
, req
, ext_sect
, lconf
, certopt
, nameopt
, default_op
,
1408 ext_copy
, selfsign
);
1419 certify_cert(X509
** xret
, char *infile
, EVP_PKEY
* pkey
, X509
* x509
,
1420 const EVP_MD
* dgst
, STACK_OF(OPENSSL_STRING
) * sigopts
,
1421 STACK_OF(CONF_VALUE
) * policy
, CA_DB
* db
, BIGNUM
* serial
, char *subj
,
1422 unsigned long chtype
, int multirdn
, int email_dn
, char *startdate
,
1423 char *enddate
, long days
, int batch
, char *ext_sect
, CONF
* lconf
,
1424 int verbose
, unsigned long certopt
, unsigned long nameopt
, int default_op
,
1428 X509_REQ
*rreq
= NULL
;
1429 EVP_PKEY
*pktmp
= NULL
;
1432 if ((req
= load_cert(bio_err
, infile
, FORMAT_PEM
, NULL
,
1436 X509_print(bio_err
, req
);
1438 BIO_printf(bio_err
, "Check that the request matches the signature\n");
1440 if ((pktmp
= X509_get_pubkey(req
)) == NULL
) {
1441 BIO_printf(bio_err
, "error unpacking public key\n");
1444 i
= X509_verify(req
, pktmp
);
1445 EVP_PKEY_free(pktmp
);
1448 BIO_printf(bio_err
, "Signature verification problems....\n");
1454 "Signature did not match the certificate\n");
1457 BIO_printf(bio_err
, "Signature ok\n");
1459 if ((rreq
= X509_to_X509_REQ(req
, NULL
, EVP_md5())) == NULL
)
1462 ok
= do_body(xret
, pkey
, x509
, dgst
, sigopts
, policy
, db
, serial
,
1463 subj
, chtype
, multirdn
, email_dn
, startdate
, enddate
, days
, batch
,
1464 verbose
, rreq
, ext_sect
, lconf
, certopt
, nameopt
, default_op
,
1469 X509_REQ_free(rreq
);
1476 do_body(X509
** xret
, EVP_PKEY
* pkey
, X509
* x509
, const EVP_MD
* dgst
,
1477 STACK_OF(OPENSSL_STRING
) * sigopts
, STACK_OF(CONF_VALUE
) * policy
,
1478 CA_DB
* db
, BIGNUM
* serial
, char *subj
, unsigned long chtype
, int multirdn
,
1479 int email_dn
, char *startdate
, char *enddate
, long days
, int batch
,
1480 int verbose
, X509_REQ
* req
, char *ext_sect
, CONF
* lconf
,
1481 unsigned long certopt
, unsigned long nameopt
, int default_op
,
1482 int ext_copy
, int selfsign
)
1484 X509_NAME
*name
= NULL
, *CAname
= NULL
, *subject
= NULL
, *dn_subject
= NULL
;
1485 ASN1_UTCTIME
*tm
, *tmptm
;
1486 ASN1_STRING
*str
, *str2
;
1490 X509_NAME_ENTRY
*ne
;
1491 X509_NAME_ENTRY
*tne
, *push
;
1493 int ok
= -1, i
, j
, last
, nid
;
1496 OPENSSL_STRING row
[DB_NUMBER
];
1497 OPENSSL_STRING
*irow
= NULL
;
1498 OPENSSL_STRING
*rrow
= NULL
;
1500 tmptm
= ASN1_UTCTIME_new();
1501 if (tmptm
== NULL
) {
1502 BIO_printf(bio_err
, "malloc error\n");
1505 for (i
= 0; i
< DB_NUMBER
; i
++)
1509 X509_NAME
*n
= parse_name(subj
, chtype
, multirdn
);
1512 ERR_print_errors(bio_err
);
1515 X509_REQ_set_subject_name(req
, n
);
1516 req
->req_info
->enc
.modified
= 1;
1521 "The Subject's Distinguished Name is as follows\n");
1523 name
= X509_REQ_get_subject_name(req
);
1524 for (i
= 0; i
< X509_NAME_entry_count(name
); i
++) {
1525 ne
= X509_NAME_get_entry(name
, i
);
1526 str
= X509_NAME_ENTRY_get_data(ne
);
1527 obj
= X509_NAME_ENTRY_get_object(ne
);
1530 /* assume all type should be strings */
1531 nid
= OBJ_obj2nid(ne
->object
);
1533 if (str
->type
== V_ASN1_UNIVERSALSTRING
)
1534 ASN1_UNIVERSALSTRING_to_string(str
);
1536 if ((str
->type
== V_ASN1_IA5STRING
) &&
1537 (nid
!= NID_pkcs9_emailAddress
))
1538 str
->type
= V_ASN1_T61STRING
;
1540 if ((nid
== NID_pkcs9_emailAddress
) &&
1541 (str
->type
== V_ASN1_PRINTABLESTRING
))
1542 str
->type
= V_ASN1_IA5STRING
;
1544 /* If no EMAIL is wanted in the subject */
1545 if ((OBJ_obj2nid(obj
) == NID_pkcs9_emailAddress
) && (!email_dn
))
1548 /* check some things */
1549 if ((OBJ_obj2nid(obj
) == NID_pkcs9_emailAddress
) &&
1550 (str
->type
!= V_ASN1_IA5STRING
)) {
1551 BIO_printf(bio_err
, "\nemailAddress type needs to be of type IA5STRING\n");
1554 if ((str
->type
!= V_ASN1_BMPSTRING
) &&
1555 (str
->type
!= V_ASN1_UTF8STRING
)) {
1556 j
= ASN1_PRINTABLE_type(str
->data
, str
->length
);
1557 if (((j
== V_ASN1_T61STRING
) &&
1558 (str
->type
!= V_ASN1_T61STRING
)) ||
1559 ((j
== V_ASN1_IA5STRING
) &&
1560 (str
->type
== V_ASN1_PRINTABLESTRING
))) {
1561 BIO_printf(bio_err
, "\nThe string contains characters that are illegal for the ASN.1 type\n");
1566 old_entry_print(bio_err
, obj
, str
);
1569 /* Ok, now we check the 'policy' stuff. */
1570 if ((subject
= X509_NAME_new()) == NULL
) {
1571 BIO_printf(bio_err
, "Memory allocation failure\n");
1574 /* take a copy of the issuer name before we mess with it. */
1576 CAname
= X509_NAME_dup(name
);
1578 CAname
= X509_NAME_dup(x509
->cert_info
->subject
);
1583 for (i
= 0; i
< sk_CONF_VALUE_num(policy
); i
++) {
1584 cv
= sk_CONF_VALUE_value(policy
, i
); /* get the object id */
1585 if ((j
= OBJ_txt2nid(cv
->name
)) == NID_undef
) {
1586 BIO_printf(bio_err
, "%s:unknown object type in 'policy' configuration\n", cv
->name
);
1589 obj
= OBJ_nid2obj(j
);
1593 /* lookup the object in the supplied name list */
1594 j
= X509_NAME_get_index_by_OBJ(name
, obj
, last
);
1600 tne
= X509_NAME_get_entry(name
, j
);
1604 /* depending on the 'policy', decide what to do. */
1606 if (strcmp(cv
->value
, "optional") == 0) {
1609 } else if (strcmp(cv
->value
, "supplied") == 0) {
1611 BIO_printf(bio_err
, "The %s field needed to be supplied and was missing\n", cv
->name
);
1615 } else if (strcmp(cv
->value
, "match") == 0) {
1619 BIO_printf(bio_err
, "The mandatory %s field was missing\n", cv
->name
);
1625 j
= X509_NAME_get_index_by_OBJ(CAname
, obj
, last2
);
1626 if ((j
< 0) && (last2
== -1)) {
1627 BIO_printf(bio_err
, "The %s field does not exist in the CA certificate,\nthe 'policy' is misconfigured\n", cv
->name
);
1631 push
= X509_NAME_get_entry(CAname
, j
);
1632 str
= X509_NAME_ENTRY_get_data(tne
);
1633 str2
= X509_NAME_ENTRY_get_data(push
);
1635 if (ASN1_STRING_cmp(str
, str2
) != 0)
1639 BIO_printf(bio_err
, "The %s field needed to be the same in the\nCA certificate (%s) and the request (%s)\n", cv
->name
, ((str2
== NULL
) ? "NULL" : (char *) str2
->data
), ((str
== NULL
) ? "NULL" : (char *) str
->data
));
1643 BIO_printf(bio_err
, "%s:invalid type in 'policy' configuration\n", cv
->value
);
1648 if (!X509_NAME_add_entry(subject
, push
,
1651 X509_NAME_ENTRY_free(push
);
1653 "Memory allocation failure\n");
1663 X509_NAME_free(subject
);
1664 /* subject=X509_NAME_dup(X509_REQ_get_subject_name(req)); */
1665 subject
= X509_NAME_dup(name
);
1666 if (subject
== NULL
)
1670 BIO_printf(bio_err
, "The subject name appears to be ok, checking data base for clashes\n");
1672 /* Build the correct Subject if no email is wanted in the subject */
1674 * and add it later on because of the method extensions are added
1679 dn_subject
= subject
;
1681 X509_NAME_ENTRY
*tmpne
;
1683 * Its best to dup the subject DN and then delete any email
1684 * addresses because this retains its structure.
1686 if (!(dn_subject
= X509_NAME_dup(subject
))) {
1687 BIO_printf(bio_err
, "Memory allocation failure\n");
1690 while ((i
= X509_NAME_get_index_by_NID(dn_subject
,
1691 NID_pkcs9_emailAddress
, -1)) >= 0) {
1692 tmpne
= X509_NAME_get_entry(dn_subject
, i
);
1693 X509_NAME_delete_entry(dn_subject
, i
);
1694 X509_NAME_ENTRY_free(tmpne
);
1698 if (BN_is_zero(serial
))
1699 row
[DB_serial
] = strdup("00");
1701 row
[DB_serial
] = BN_bn2hex(serial
);
1702 if (row
[DB_serial
] == NULL
) {
1703 BIO_printf(bio_err
, "Memory allocation failure\n");
1706 if (db
->attributes
.unique_subject
) {
1707 OPENSSL_STRING
*crow
= row
;
1709 rrow
= TXT_DB_get_by_index(db
->db
, DB_name
, crow
);
1712 "ERROR:There is already a certificate for %s\n",
1717 rrow
= TXT_DB_get_by_index(db
->db
, DB_serial
, row
);
1720 "ERROR:Serial number %s has already been issued,\n",
1722 BIO_printf(bio_err
, " check the database/serial_file for corruption\n");
1727 "The matching entry has the following details\n");
1728 if (rrow
[DB_type
][0] == 'E')
1730 else if (rrow
[DB_type
][0] == 'R')
1732 else if (rrow
[DB_type
][0] == 'V')
1735 p
= "\ninvalid type, Data base error\n";
1736 BIO_printf(bio_err
, "Type :%s\n", p
);
1737 if (rrow
[DB_type
][0] == 'R') {
1738 p
= rrow
[DB_exp_date
];
1741 BIO_printf(bio_err
, "Was revoked on:%s\n", p
);
1743 p
= rrow
[DB_exp_date
];
1746 BIO_printf(bio_err
, "Expires on :%s\n", p
);
1747 p
= rrow
[DB_serial
];
1750 BIO_printf(bio_err
, "Serial Number :%s\n", p
);
1754 BIO_printf(bio_err
, "File name :%s\n", p
);
1758 BIO_printf(bio_err
, "Subject Name :%s\n", p
);
1759 ok
= -1; /* This is now a 'bad' error. */
1762 /* We are now totally happy, lets make and sign the certificate */
1764 BIO_printf(bio_err
, "Everything appears to be ok, creating and signing the certificate\n");
1766 if ((ret
= X509_new()) == NULL
)
1768 ci
= ret
->cert_info
;
1771 /* Make it an X509 v3 certificate. */
1772 if (!X509_set_version(ret
, 2))
1775 if (ci
->serialNumber
== NULL
)
1777 if (BN_to_ASN1_INTEGER(serial
, ci
->serialNumber
) == NULL
)
1780 if (!X509_set_issuer_name(ret
, subject
))
1783 if (!X509_set_issuer_name(ret
, X509_get_subject_name(x509
)))
1787 if (strcmp(startdate
, "today") == 0)
1788 X509_gmtime_adj(X509_get_notBefore(ret
), 0);
1789 else if (setCertificateTime(X509_get_notBefore(ret
), startdate
) == -1) {
1790 BIO_printf(bio_err
, "Invalid start date %s\n",
1795 if (enddate
== NULL
)
1796 X509_time_adj_ex(X509_get_notAfter(ret
), days
, 0, NULL
);
1797 else if (setCertificateTime(X509_get_notAfter(ret
), enddate
) == -1) {
1798 BIO_printf(bio_err
, "Invalid end date %s\n",
1803 if (!X509_set_subject_name(ret
, subject
))
1806 pktmp
= X509_REQ_get_pubkey(req
);
1807 i
= X509_set_pubkey(ret
, pktmp
);
1808 EVP_PKEY_free(pktmp
);
1812 /* Lets add the extensions, if there are any */
1815 if (ci
->version
== NULL
)
1816 if ((ci
->version
= ASN1_INTEGER_new()) == NULL
)
1818 ASN1_INTEGER_set(ci
->version
, 2); /* version 3 certificate */
1821 * Free the current entries if any, there should not be any I
1824 if (ci
->extensions
!= NULL
)
1825 sk_X509_EXTENSION_pop_free(ci
->extensions
,
1826 X509_EXTENSION_free
);
1828 ci
->extensions
= NULL
;
1830 /* Initialize the context structure */
1832 X509V3_set_ctx(&ctx
, ret
, ret
, req
, NULL
, 0);
1834 X509V3_set_ctx(&ctx
, x509
, ret
, req
, NULL
, 0);
1839 "Extra configuration file found\n");
1841 /* Use the extconf configuration db LHASH */
1842 X509V3_set_nconf(&ctx
, extconf
);
1844 /* Test the structure (needed?) */
1845 /* X509V3_set_ctx_test(&ctx); */
1847 /* Adds exts contained in the configuration file */
1848 if (!X509V3_EXT_add_nconf(extconf
, &ctx
,
1851 "ERROR: adding extensions in section %s\n",
1853 ERR_print_errors(bio_err
);
1857 BIO_printf(bio_err
, "Successfully added extensions from file.\n");
1858 } else if (ext_sect
) {
1859 /* We found extensions to be set from config file */
1860 X509V3_set_nconf(&ctx
, lconf
);
1862 if (!X509V3_EXT_add_nconf(lconf
, &ctx
, ext_sect
, ret
)) {
1864 "ERROR: adding extensions in section %s\n",
1866 ERR_print_errors(bio_err
);
1870 BIO_printf(bio_err
, "Successfully added extensions from config\n");
1873 /* Copy extensions from request (if any) */
1875 if (!copy_extensions(ret
, req
, ext_copy
)) {
1876 BIO_printf(bio_err
, "ERROR: adding extensions from request\n");
1877 ERR_print_errors(bio_err
);
1880 /* Set the right value for the noemailDN option */
1881 if (email_dn
== 0) {
1882 if (!X509_set_subject_name(ret
, dn_subject
))
1886 BIO_printf(bio_err
, "Certificate Details:\n");
1888 * Never print signature details because signature not
1891 certopt
|= X509_FLAG_NO_SIGDUMP
| X509_FLAG_NO_SIGNAME
;
1892 X509_print_ex(bio_err
, ret
, nameopt
, certopt
);
1894 BIO_printf(bio_err
, "Certificate is to be certified until ");
1895 ASN1_TIME_print(bio_err
, X509_get_notAfter(ret
));
1897 BIO_printf(bio_err
, " (%ld days)", days
);
1898 BIO_printf(bio_err
, "\n");
1903 BIO_printf(bio_err
, "Sign the certificate? [y/n]:");
1904 (void) BIO_flush(bio_err
);
1905 if (!fgets(answer
, sizeof(answer
) - 1, stdin
)) {
1907 "CERTIFICATE WILL NOT BE CERTIFIED: I/O error\n");
1911 if (!((answer
[0] == 'y') || (answer
[0] == 'Y'))) {
1913 "CERTIFICATE WILL NOT BE CERTIFIED\n");
1918 pktmp
= X509_get_pubkey(ret
);
1919 if (EVP_PKEY_missing_parameters(pktmp
) &&
1920 !EVP_PKEY_missing_parameters(pkey
))
1921 EVP_PKEY_copy_parameters(pktmp
, pkey
);
1922 EVP_PKEY_free(pktmp
);
1924 if (!do_X509_sign(bio_err
, ret
, pkey
, dgst
, sigopts
))
1927 /* We now just add it to the database */
1928 row
[DB_type
] = malloc(2);
1930 tm
= X509_get_notAfter(ret
);
1931 row
[DB_exp_date
] = malloc(tm
->length
+ 1);
1932 if (row
[DB_type
] == NULL
|| row
[DB_exp_date
] == NULL
) {
1933 BIO_printf(bio_err
, "Memory allocation failure\n");
1937 memcpy(row
[DB_exp_date
], tm
->data
, tm
->length
);
1938 row
[DB_exp_date
][tm
->length
] = '\0';
1940 row
[DB_rev_date
] = NULL
;
1942 /* row[DB_serial] done already */
1943 row
[DB_file
] = malloc(8);
1944 row
[DB_name
] = X509_NAME_oneline(X509_get_subject_name(ret
), NULL
, 0);
1946 if ((row
[DB_type
] == NULL
) || (row
[DB_file
] == NULL
) ||
1947 (row
[DB_name
] == NULL
)) {
1948 BIO_printf(bio_err
, "Memory allocation failure\n");
1951 (void) strlcpy(row
[DB_file
], "unknown", 8);
1952 row
[DB_type
][0] = 'V';
1953 row
[DB_type
][1] = '\0';
1955 if ((irow
= reallocarray(NULL
, DB_NUMBER
+ 1, sizeof(char *))) ==
1957 BIO_printf(bio_err
, "Memory allocation failure\n");
1960 for (i
= 0; i
< DB_NUMBER
; i
++) {
1964 irow
[DB_NUMBER
] = NULL
;
1966 if (!TXT_DB_insert(db
->db
, irow
)) {
1967 BIO_printf(bio_err
, "failed to update database\n");
1968 BIO_printf(bio_err
, "TXT_DB error number %ld\n", db
->db
->error
);
1973 for (i
= 0; i
< DB_NUMBER
; i
++)
1977 X509_NAME_free(CAname
);
1978 if (subject
!= NULL
)
1979 X509_NAME_free(subject
);
1980 if ((dn_subject
!= NULL
) && !email_dn
)
1981 X509_NAME_free(dn_subject
);
1983 ASN1_UTCTIME_free(tmptm
);
1994 write_new_certificate(BIO
* bp
, X509
* x
, int output_der
, int notext
)
1997 (void) i2d_X509_bio(bp
, x
);
2002 PEM_write_bio_X509(bp
, x
);
2006 certify_spkac(X509
** xret
, char *infile
, EVP_PKEY
* pkey
, X509
* x509
,
2007 const EVP_MD
* dgst
, STACK_OF(OPENSSL_STRING
) * sigopts
,
2008 STACK_OF(CONF_VALUE
) * policy
, CA_DB
* db
, BIGNUM
* serial
, char *subj
,
2009 unsigned long chtype
, int multirdn
, int email_dn
, char *startdate
,
2010 char *enddate
, long days
, char *ext_sect
, CONF
* lconf
, int verbose
,
2011 unsigned long certopt
, unsigned long nameopt
, int default_op
, int ext_copy
)
2013 STACK_OF(CONF_VALUE
) * sk
= NULL
;
2014 LHASH_OF(CONF_VALUE
) * parms
= NULL
;
2015 X509_REQ
*req
= NULL
;
2016 CONF_VALUE
*cv
= NULL
;
2017 NETSCAPE_SPKI
*spki
= NULL
;
2020 EVP_PKEY
*pktmp
= NULL
;
2021 X509_NAME
*n
= NULL
;
2027 * Load input file into a hash table. (This is just an easy
2028 * way to read and parse the file, then put it into a convenient
2031 parms
= CONF_load(NULL
, infile
, &errline
);
2032 if (parms
== NULL
) {
2033 BIO_printf(bio_err
, "error on line %ld of %s\n",
2035 ERR_print_errors(bio_err
);
2038 sk
= CONF_get_section(parms
, "default");
2039 if (sk_CONF_VALUE_num(sk
) == 0) {
2040 BIO_printf(bio_err
, "no name/value pairs found in %s\n",
2046 * Now create a dummy X509 request structure. We don't actually
2047 * have an X509 request, but we have many of the components
2048 * (a public key, various DN components). The idea is that we
2049 * put these components into the right X509 request structure
2050 * and we can use the same code as if you had a real X509 request.
2052 req
= X509_REQ_new();
2054 ERR_print_errors(bio_err
);
2058 * Build up the subject name set.
2064 if (sk_CONF_VALUE_num(sk
) <= i
)
2067 cv
= sk_CONF_VALUE_value(sk
, i
);
2070 * Skip past any leading X. X: X, etc to allow for multiple
2073 for (buf
= cv
->name
; *buf
; buf
++) {
2074 if ((*buf
== ':') || (*buf
== ',') || (*buf
== '.')) {
2083 if ((nid
= OBJ_txt2nid(type
)) == NID_undef
) {
2084 if (strcmp(type
, "SPKAC") == 0) {
2085 spki
= NETSCAPE_SPKI_b64_decode(cv
->value
, -1);
2087 BIO_printf(bio_err
, "unable to load Netscape SPKAC structure\n");
2088 ERR_print_errors(bio_err
);
2094 if (!X509_NAME_add_entry_by_NID(n
, nid
, chtype
,
2095 (unsigned char *)buf
, -1, -1, 0))
2100 "Netscape SPKAC structure not found in %s\n", infile
);
2104 * Now extract the key from the SPKI structure.
2108 "Check that the SPKAC request matches the signature\n");
2110 if ((pktmp
= NETSCAPE_SPKI_get_pubkey(spki
)) == NULL
) {
2111 BIO_printf(bio_err
, "error unpacking SPKAC public key\n");
2114 j
= NETSCAPE_SPKI_verify(spki
, pktmp
);
2117 "signature verification failed on SPKAC public key\n");
2120 BIO_printf(bio_err
, "Signature ok\n");
2122 X509_REQ_set_pubkey(req
, pktmp
);
2123 EVP_PKEY_free(pktmp
);
2124 ok
= do_body(xret
, pkey
, x509
, dgst
, sigopts
, policy
, db
, serial
,
2125 subj
, chtype
, multirdn
, email_dn
, startdate
, enddate
, days
, 1,
2126 verbose
, req
, ext_sect
, lconf
, certopt
, nameopt
, default_op
,
2135 NETSCAPE_SPKI_free(spki
);
2141 check_time_format(const char *str
)
2143 return ASN1_TIME_set_string(NULL
, str
);
2147 do_revoke(X509
* x509
, CA_DB
* db
, int type
, char *value
)
2149 ASN1_UTCTIME
*tm
= NULL
;
2150 char *row
[DB_NUMBER
], **rrow
, **irow
;
2151 char *rev_str
= NULL
;
2155 for (i
= 0; i
< DB_NUMBER
; i
++)
2157 row
[DB_name
] = X509_NAME_oneline(X509_get_subject_name(x509
), NULL
, 0);
2158 bn
= ASN1_INTEGER_to_BN(X509_get_serialNumber(x509
), NULL
);
2162 row
[DB_serial
] = strdup("00");
2164 row
[DB_serial
] = BN_bn2hex(bn
);
2166 if ((row
[DB_name
] == NULL
) || (row
[DB_serial
] == NULL
)) {
2167 BIO_printf(bio_err
, "Memory allocation failure\n");
2171 * We have to lookup by serial number because name lookup skips
2174 rrow
= TXT_DB_get_by_index(db
->db
, DB_serial
, row
);
2177 "Adding Entry with serial number %s to DB for %s\n",
2178 row
[DB_serial
], row
[DB_name
]);
2180 /* We now just add it to the database */
2181 row
[DB_type
] = malloc(2);
2183 tm
= X509_get_notAfter(x509
);
2184 row
[DB_exp_date
] = malloc(tm
->length
+ 1);
2185 if (row
[DB_type
] == NULL
|| row
[DB_exp_date
] == NULL
) {
2186 BIO_printf(bio_err
, "Memory allocation failure\n");
2189 memcpy(row
[DB_exp_date
], tm
->data
, tm
->length
);
2190 row
[DB_exp_date
][tm
->length
] = '\0';
2192 row
[DB_rev_date
] = NULL
;
2194 /* row[DB_serial] done already */
2195 row
[DB_file
] = malloc(8);
2197 /* row[DB_name] done already */
2199 if ((row
[DB_type
] == NULL
) || (row
[DB_file
] == NULL
)) {
2200 BIO_printf(bio_err
, "Memory allocation failure\n");
2203 (void) strlcpy(row
[DB_file
], "unknown", 8);
2204 row
[DB_type
][0] = 'V';
2205 row
[DB_type
][1] = '\0';
2207 if ((irow
= reallocarray(NULL
, sizeof(char *),
2208 (DB_NUMBER
+ 1))) == NULL
) {
2209 BIO_printf(bio_err
, "Memory allocation failure\n");
2212 for (i
= 0; i
< DB_NUMBER
; i
++) {
2216 irow
[DB_NUMBER
] = NULL
;
2218 if (!TXT_DB_insert(db
->db
, irow
)) {
2219 BIO_printf(bio_err
, "failed to update database\n");
2220 BIO_printf(bio_err
, "TXT_DB error number %ld\n",
2224 /* Revoke Certificate */
2225 ok
= do_revoke(x509
, db
, type
, value
);
2229 } else if (index_name_cmp_noconst(row
, rrow
)) {
2230 BIO_printf(bio_err
, "ERROR:name does not match %s\n",
2233 } else if (rrow
[DB_type
][0] == 'R') {
2234 BIO_printf(bio_err
, "ERROR:Already revoked, serial number %s\n",
2238 BIO_printf(bio_err
, "Revoking Certificate %s.\n",
2240 rev_str
= make_revocation_str(type
, value
);
2242 BIO_printf(bio_err
, "Error in revocation arguments\n");
2245 rrow
[DB_type
][0] = 'R';
2246 rrow
[DB_type
][1] = '\0';
2247 rrow
[DB_rev_date
] = rev_str
;
2252 for (i
= 0; i
< DB_NUMBER
; i
++)
2259 get_certificate_status(const char *serial
, CA_DB
* db
)
2261 char *row
[DB_NUMBER
], **rrow
;
2264 /* Free Resources */
2265 for (i
= 0; i
< DB_NUMBER
; i
++)
2268 /* Malloc needed char spaces */
2269 row
[DB_serial
] = malloc(strlen(serial
) + 2);
2270 if (row
[DB_serial
] == NULL
) {
2271 BIO_printf(bio_err
, "Malloc failure\n");
2274 if (strlen(serial
) % 2) {
2275 /* Set the first char to 0 */ ;
2276 row
[DB_serial
][0] = '0';
2278 /* Copy String from serial to row[DB_serial] */
2279 memcpy(row
[DB_serial
] + 1, serial
, strlen(serial
));
2280 row
[DB_serial
][strlen(serial
) + 1] = '\0';
2282 /* Copy String from serial to row[DB_serial] */
2283 memcpy(row
[DB_serial
], serial
, strlen(serial
));
2284 row
[DB_serial
][strlen(serial
)] = '\0';
2287 /* Make it Upper Case */
2288 for (i
= 0; row
[DB_serial
][i
] != '\0'; i
++)
2289 row
[DB_serial
][i
] = toupper((unsigned char) row
[DB_serial
][i
]);
2294 /* Search for the certificate */
2295 rrow
= TXT_DB_get_by_index(db
->db
, DB_serial
, row
);
2297 BIO_printf(bio_err
, "Serial %s not present in db.\n",
2301 } else if (rrow
[DB_type
][0] == 'V') {
2302 BIO_printf(bio_err
, "%s=Valid (%c)\n",
2303 row
[DB_serial
], rrow
[DB_type
][0]);
2305 } else if (rrow
[DB_type
][0] == 'R') {
2306 BIO_printf(bio_err
, "%s=Revoked (%c)\n",
2307 row
[DB_serial
], rrow
[DB_type
][0]);
2309 } else if (rrow
[DB_type
][0] == 'E') {
2310 BIO_printf(bio_err
, "%s=Expired (%c)\n",
2311 row
[DB_serial
], rrow
[DB_type
][0]);
2313 } else if (rrow
[DB_type
][0] == 'S') {
2314 BIO_printf(bio_err
, "%s=Suspended (%c)\n",
2315 row
[DB_serial
], rrow
[DB_type
][0]);
2318 BIO_printf(bio_err
, "%s=Unknown (%c).\n",
2319 row
[DB_serial
], rrow
[DB_type
][0]);
2324 for (i
= 0; i
< DB_NUMBER
; i
++)
2331 do_updatedb(CA_DB
* db
)
2333 ASN1_UTCTIME
*a_tm
= NULL
;
2335 int db_y2k
, a_y2k
; /* flags = 1 if y >= 2000 */
2336 char **rrow
, *a_tm_s
;
2338 a_tm
= ASN1_UTCTIME_new();
2340 /* get actual time and make a string */
2341 a_tm
= X509_gmtime_adj(a_tm
, 0);
2342 a_tm_s
= malloc(a_tm
->length
+ 1);
2343 if (a_tm_s
== NULL
) {
2347 memcpy(a_tm_s
, a_tm
->data
, a_tm
->length
);
2348 a_tm_s
[a_tm
->length
] = '\0';
2350 if (strncmp(a_tm_s
, "49", 2) <= 0)
2355 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(db
->db
->data
); i
++) {
2356 rrow
= sk_OPENSSL_PSTRING_value(db
->db
->data
, i
);
2358 if (rrow
[DB_type
][0] == 'V') {
2359 /* ignore entries that are not valid */
2360 if (strncmp(rrow
[DB_exp_date
], "49", 2) <= 0)
2365 if (db_y2k
== a_y2k
) {
2366 /* all on the same y2k side */
2367 if (strcmp(rrow
[DB_exp_date
], a_tm_s
) <= 0) {
2368 rrow
[DB_type
][0] = 'E';
2369 rrow
[DB_type
][1] = '\0';
2372 BIO_printf(bio_err
, "%s=Expired\n",
2375 } else if (db_y2k
< a_y2k
) {
2376 rrow
[DB_type
][0] = 'E';
2377 rrow
[DB_type
][1] = '\0';
2380 BIO_printf(bio_err
, "%s=Expired\n",
2387 ASN1_UTCTIME_free(a_tm
);
2393 static const char *crl_reasons
[] = {
2394 /* CRL reason strings */
2398 "affiliationChanged",
2400 "cessationOfOperation",
2403 /* Additional pseudo reasons */
2409 #define NUM_REASONS (sizeof(crl_reasons) / sizeof(char *))
2411 /* Given revocation information convert to a DB string.
2412 * The format of the string is:
2413 * revtime[,reason,extra]. Where 'revtime' is the
2414 * revocation time (the current time). 'reason' is the
2415 * optional CRL reason and 'extra' is any additional
2420 make_revocation_str(int rev_type
, char *rev_arg
)
2422 char *other
= NULL
, *str
;
2423 const char *reason
= NULL
;
2425 ASN1_UTCTIME
*revtm
= NULL
;
2431 case REV_CRL_REASON
:
2432 for (i
= 0; i
< 8; i
++) {
2433 if (!strcasecmp(rev_arg
, crl_reasons
[i
])) {
2434 reason
= crl_reasons
[i
];
2438 if (reason
== NULL
) {
2439 BIO_printf(bio_err
, "Unknown CRL reason %s\n", rev_arg
);
2445 /* Argument is an OID */
2447 otmp
= OBJ_txt2obj(rev_arg
, 0);
2448 ASN1_OBJECT_free(otmp
);
2452 "Invalid object identifier %s\n", rev_arg
);
2455 reason
= "holdInstruction";
2459 case REV_KEY_COMPROMISE
:
2460 case REV_CA_COMPROMISE
:
2462 /* Argument is the key compromise time */
2463 if (!ASN1_GENERALIZEDTIME_set_string(NULL
, rev_arg
)) {
2465 "Invalid time format %s. Need YYYYMMDDHHMMSSZ\n",
2470 if (rev_type
== REV_KEY_COMPROMISE
)
2473 reason
= "CAkeyTime";
2479 revtm
= X509_gmtime_adj(NULL
, 0);
2480 if (asprintf(&str
, "%s%s%s%s%s", revtm
->data
,
2481 reason
? "," : "", reason
? reason
: "",
2482 other
? "," : "", other
? other
: "") == -1)
2484 ASN1_UTCTIME_free(revtm
);
2488 /* Convert revocation field to X509_REVOKED entry
2492 * 2 OK and some extensions added (i.e. V2 CRL)
2496 make_revoked(X509_REVOKED
* rev
, const char *str
)
2499 int reason_code
= -1;
2501 ASN1_OBJECT
*hold
= NULL
;
2502 ASN1_GENERALIZEDTIME
*comp_time
= NULL
;
2503 ASN1_ENUMERATED
*rtmp
= NULL
;
2505 ASN1_TIME
*revDate
= NULL
;
2507 i
= unpack_revinfo(&revDate
, &reason_code
, &hold
, &comp_time
, str
);
2512 if (rev
&& !X509_REVOKED_set_revocationDate(rev
, revDate
))
2515 if (rev
&& (reason_code
!= OCSP_REVOKED_STATUS_NOSTATUS
)) {
2516 rtmp
= ASN1_ENUMERATED_new();
2517 if (!rtmp
|| !ASN1_ENUMERATED_set(rtmp
, reason_code
))
2519 if (!X509_REVOKED_add1_ext_i2d(rev
, NID_crl_reason
, rtmp
, 0, 0))
2522 if (rev
&& comp_time
) {
2523 if (!X509_REVOKED_add1_ext_i2d(rev
, NID_invalidity_date
,
2528 if (!X509_REVOKED_add1_ext_i2d(rev
, NID_hold_instruction_code
,
2532 if (reason_code
!= OCSP_REVOKED_STATUS_NOSTATUS
)
2540 ASN1_OBJECT_free(hold
);
2541 ASN1_GENERALIZEDTIME_free(comp_time
);
2542 ASN1_ENUMERATED_free(rtmp
);
2543 ASN1_TIME_free(revDate
);
2549 old_entry_print(BIO
* bp
, ASN1_OBJECT
* obj
, ASN1_STRING
* str
)
2551 char buf
[25], *pbuf
, *p
;
2554 j
= i2a_ASN1_OBJECT(bp
, obj
);
2556 for (j
= 22 - j
; j
> 0; j
--)
2562 if (str
->type
== V_ASN1_PRINTABLESTRING
)
2563 BIO_printf(bp
, "PRINTABLE:'");
2564 else if (str
->type
== V_ASN1_T61STRING
)
2565 BIO_printf(bp
, "T61STRING:'");
2566 else if (str
->type
== V_ASN1_IA5STRING
)
2567 BIO_printf(bp
, "IA5STRING:'");
2568 else if (str
->type
== V_ASN1_UNIVERSALSTRING
)
2569 BIO_printf(bp
, "UNIVERSALSTRING:'");
2571 BIO_printf(bp
, "ASN.1 %2d:'", str
->type
);
2573 p
= (char *) str
->data
;
2574 for (j
= str
->length
; j
> 0; j
--) {
2575 if ((*p
>= ' ') && (*p
<= '~'))
2576 BIO_printf(bp
, "%c", *p
);
2578 BIO_printf(bp
, "\\0x%02X", *p
);
2579 else if ((unsigned char) *p
== 0xf7)
2580 BIO_printf(bp
, "^?");
2582 BIO_printf(bp
, "^%c", *p
+ '@');
2585 BIO_printf(bp
, "'\n");
2590 unpack_revinfo(ASN1_TIME
** prevtm
, int *preason
, ASN1_OBJECT
** phold
,
2591 ASN1_GENERALIZEDTIME
** pinvtm
, const char *str
)
2594 char *rtime_str
, *reason_str
= NULL
, *arg_str
= NULL
, *p
;
2595 int reason_code
= -1;
2598 ASN1_OBJECT
*hold
= NULL
;
2599 ASN1_GENERALIZEDTIME
*comp_time
= NULL
;
2601 if ((tmp
= strdup(str
)) == NULL
) {
2602 BIO_printf(bio_err
, "malloc failed\n");
2605 p
= strchr(tmp
, ',');
2619 *prevtm
= ASN1_UTCTIME_new();
2620 if (!ASN1_UTCTIME_set_string(*prevtm
, rtime_str
)) {
2621 BIO_printf(bio_err
, "invalid revocation date %s\n",
2627 for (i
= 0; i
< NUM_REASONS
; i
++) {
2628 if (!strcasecmp(reason_str
, crl_reasons
[i
])) {
2633 if (reason_code
== OCSP_REVOKED_STATUS_NOSTATUS
) {
2634 BIO_printf(bio_err
, "invalid reason code %s\n",
2638 if (reason_code
== 7)
2639 reason_code
= OCSP_REVOKED_STATUS_REMOVEFROMCRL
;
2640 else if (reason_code
== 8) { /* Hold instruction */
2643 "missing hold instruction\n");
2646 reason_code
= OCSP_REVOKED_STATUS_CERTIFICATEHOLD
;
2647 hold
= OBJ_txt2obj(arg_str
, 0);
2651 "invalid object identifier %s\n", arg_str
);
2656 } else if ((reason_code
== 9) || (reason_code
== 10)) {
2659 "missing compromised time\n");
2662 comp_time
= ASN1_GENERALIZEDTIME_new();
2663 if (!ASN1_GENERALIZEDTIME_set_string(comp_time
,
2666 "invalid compromised time %s\n", arg_str
);
2669 if (reason_code
== 9)
2670 reason_code
= OCSP_REVOKED_STATUS_KEYCOMPROMISE
;
2672 reason_code
= OCSP_REVOKED_STATUS_CACOMPROMISE
;
2676 *preason
= reason_code
;
2678 *pinvtm
= comp_time
;
2680 ASN1_GENERALIZEDTIME_free(comp_time
);
2688 ASN1_OBJECT_free(hold
);
2690 ASN1_GENERALIZEDTIME_free(comp_time
);
2696 bin2hex(unsigned char * data
, size_t len
)
2699 char hex
[] = "0123456789ABCDEF";
2702 if ((ret
= malloc(len
* 2 + 1))) {
2703 for (i
= 0; i
< len
; i
++) {
2704 ret
[i
* 2 + 0] = hex
[data
[i
] >> 4];
2705 ret
[i
* 2 + 1] = hex
[data
[i
] & 0x0F];
2707 ret
[len
* 2] = '\0';