1 /* $OpenBSD: ca.c,v 1.23 2017/01/20 08:57:11 deraadt 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 ca_main(int argc
, char **argv
)
213 char *key
= NULL
, *passargin
= NULL
;
230 char *configfile
= NULL
;
233 char *keyfile
= NULL
;
234 char *certfile
= NULL
;
235 int keyform
= FORMAT_PEM
;
237 char *spkac_file
= NULL
;
238 char *ss_cert_file
= NULL
;
239 char *ser_status
= NULL
;
240 EVP_PKEY
*pkey
= NULL
;
242 char *outfile
= NULL
;
244 char *serialfile
= NULL
;
245 char *crlnumberfile
= NULL
;
246 char *extensions
= NULL
;
247 char *extfile
= NULL
;
249 unsigned long chtype
= MBSTRING_ASC
;
251 char *tmp_email_dn
= NULL
;
252 char *crl_ext
= NULL
;
253 int rev_type
= REV_NONE
;
254 char *rev_arg
= NULL
;
255 BIGNUM
*serial
= NULL
;
256 BIGNUM
*crlnumber
= NULL
;
257 char *startdate
= NULL
;
258 char *enddate
= NULL
;
262 unsigned long nameopt
= 0, certopt
= 0;
264 int ext_copy
= EXT_COPY_NONE
;
266 X509
*x509
= NULL
, *x509p
= NULL
;
268 BIO
*in
= NULL
, *out
= NULL
, *Sout
= NULL
, *Cout
= NULL
;
271 X509_CRL
*crl
= NULL
;
272 X509_REVOKED
*r
= NULL
;
274 ASN1_INTEGER
*tmpser
;
279 const EVP_MD
*dgst
= NULL
;
280 STACK_OF(CONF_VALUE
) * attribs
= NULL
;
281 STACK_OF(X509
) * cert_sk
= NULL
;
282 STACK_OF(OPENSSL_STRING
) * sigopts
= NULL
;
284 const char *errstr
= NULL
;
287 if (single_execution
) {
288 if (pledge("stdio cpath wpath rpath tty", NULL
) == -1) {
304 if (strcmp(*argv
, "-verbose") == 0)
306 else if (strcmp(*argv
, "-config") == 0) {
309 configfile
= *(++argv
);
310 } else if (strcmp(*argv
, "-name") == 0) {
314 } else if (strcmp(*argv
, "-subj") == 0) {
319 } else if (strcmp(*argv
, "-utf8") == 0)
320 chtype
= MBSTRING_UTF8
;
321 else if (strcmp(*argv
, "-create_serial") == 0)
323 else if (strcmp(*argv
, "-multivalue-rdn") == 0)
325 else if (strcmp(*argv
, "-startdate") == 0) {
328 startdate
= *(++argv
);
329 } else if (strcmp(*argv
, "-enddate") == 0) {
333 } else if (strcmp(*argv
, "-days") == 0) {
336 days
= strtonum(*(++argv
), 0, LONG_MAX
, &errstr
);
339 } else if (strcmp(*argv
, "-md") == 0) {
343 } else if (strcmp(*argv
, "-policy") == 0) {
347 } else if (strcmp(*argv
, "-keyfile") == 0) {
351 } else if (strcmp(*argv
, "-keyform") == 0) {
354 keyform
= str2fmt(*(++argv
));
355 } else if (strcmp(*argv
, "-passin") == 0) {
358 passargin
= *(++argv
);
359 } else if (strcmp(*argv
, "-key") == 0) {
363 } else if (strcmp(*argv
, "-cert") == 0) {
366 certfile
= *(++argv
);
367 } else if (strcmp(*argv
, "-selfsign") == 0)
369 else if (strcmp(*argv
, "-in") == 0) {
374 } else if (strcmp(*argv
, "-out") == 0) {
378 } else if (strcmp(*argv
, "-outdir") == 0) {
382 } else if (strcmp(*argv
, "-sigopt") == 0) {
386 sigopts
= sk_OPENSSL_STRING_new_null();
388 !sk_OPENSSL_STRING_push(sigopts
, *(++argv
)))
390 } else if (strcmp(*argv
, "-notext") == 0)
392 else if (strcmp(*argv
, "-batch") == 0)
394 else if (strcmp(*argv
, "-preserveDN") == 0)
396 else if (strcmp(*argv
, "-noemailDN") == 0)
398 else if (strcmp(*argv
, "-gencrl") == 0)
400 else if (strcmp(*argv
, "-msie_hack") == 0)
402 else if (strcmp(*argv
, "-crldays") == 0) {
405 crldays
= strtonum(*(++argv
), 0, LONG_MAX
, &errstr
);
408 } else if (strcmp(*argv
, "-crlhours") == 0) {
411 crlhours
= strtonum(*(++argv
), 0, LONG_MAX
, &errstr
);
414 } else if (strcmp(*argv
, "-crlsec") == 0) {
417 crlsec
= strtonum(*(++argv
), 0, LONG_MAX
, &errstr
);
420 } else if (strcmp(*argv
, "-infiles") == 0) {
425 } else if (strcmp(*argv
, "-ss_cert") == 0) {
428 ss_cert_file
= *(++argv
);
430 } else if (strcmp(*argv
, "-spkac") == 0) {
433 spkac_file
= *(++argv
);
435 } else if (strcmp(*argv
, "-revoke") == 0) {
440 } else if (strcmp(*argv
, "-extensions") == 0) {
443 extensions
= *(++argv
);
444 } else if (strcmp(*argv
, "-extfile") == 0) {
448 } else if (strcmp(*argv
, "-status") == 0) {
451 ser_status
= *(++argv
);
452 } else if (strcmp(*argv
, "-updatedb") == 0) {
454 } else if (strcmp(*argv
, "-crlexts") == 0) {
458 } else if (strcmp(*argv
, "-crl_reason") == 0) {
462 rev_type
= REV_CRL_REASON
;
463 } else if (strcmp(*argv
, "-crl_hold") == 0) {
468 } else if (strcmp(*argv
, "-crl_compromise") == 0) {
472 rev_type
= REV_KEY_COMPROMISE
;
473 } else if (strcmp(*argv
, "-crl_CA_compromise") == 0) {
477 rev_type
= REV_CA_COMPROMISE
;
482 BIO_printf(bio_err
, "invalid argument %s: %s\n",
485 BIO_printf(bio_err
, "unknown option %s\n", *argv
);
496 for (pp2
= ca_usage
; (*pp2
!= NULL
); pp2
++)
497 BIO_printf(bio_err
, "%s", *pp2
);
501 /*****************************************************************/
503 if (configfile
== NULL
)
504 configfile
= getenv("OPENSSL_CONF");
505 if (configfile
== NULL
) {
506 if ((tofree
= make_config_name()) == NULL
) {
507 BIO_printf(bio_err
, "error making config file name\n");
512 BIO_printf(bio_err
, "Using configuration from %s\n", configfile
);
513 conf
= NCONF_new(NULL
);
514 if (NCONF_load(conf
, configfile
, &errorline
) <= 0) {
517 "error loading the config file '%s'\n",
521 "error on line %ld of config file '%s'\n",
522 errorline
, configfile
);
528 /* Lets get the config section we are using */
529 if (section
== NULL
) {
530 section
= NCONF_get_string(conf
, BASE_SECTION
, ENV_DEFAULT_CA
);
531 if (section
== NULL
) {
532 lookup_fail(BASE_SECTION
, ENV_DEFAULT_CA
);
537 p
= NCONF_get_string(conf
, NULL
, "oid_file");
543 oid_bio
= BIO_new_file(p
, "r");
544 if (oid_bio
== NULL
) {
547 "problems opening %s for extra oid's\n", p);
548 ERR_print_errors(bio_err);
552 OBJ_create_objects(oid_bio
);
556 if (!add_oid_section(bio_err
, conf
)) {
557 ERR_print_errors(bio_err
);
561 f
= NCONF_get_string(conf
, section
, STRING_MASK
);
565 if (f
&& !ASN1_STRING_set_default_mask_asc(f
)) {
567 "Invalid global string mask setting %s\n", f
);
570 if (chtype
!= MBSTRING_UTF8
) {
571 f
= NCONF_get_string(conf
, section
, UTF8_IN
);
574 else if (!strcmp(f
, "yes"))
575 chtype
= MBSTRING_UTF8
;
577 db_attr
.unique_subject
= 1;
578 p
= NCONF_get_string(conf
, section
, ENV_UNIQUE_SUBJECT
);
580 db_attr
.unique_subject
= parse_yesno(p
, 1);
584 in
= BIO_new(BIO_s_file());
585 out
= BIO_new(BIO_s_file());
586 Sout
= BIO_new(BIO_s_file());
587 Cout
= BIO_new(BIO_s_file());
588 if ((in
== NULL
) || (out
== NULL
) || (Sout
== NULL
) || (Cout
== NULL
)) {
589 ERR_print_errors(bio_err
);
592 /*****************************************************************/
593 /* report status of cert with serial number given on command line */
595 if ((dbfile
= NCONF_get_string(conf
, section
,
596 ENV_DATABASE
)) == NULL
) {
597 lookup_fail(section
, ENV_DATABASE
);
600 db
= load_index(dbfile
, &db_attr
);
604 if (!index_index(db
))
607 if (get_certificate_status(ser_status
, db
) != 1)
608 BIO_printf(bio_err
, "Error verifying serial %s!\n",
612 /*****************************************************************/
613 /* we definitely need a private key, so let's get it */
615 if ((keyfile
== NULL
) && ((keyfile
= NCONF_get_string(conf
,
616 section
, ENV_PRIVATE_KEY
)) == NULL
)) {
617 lookup_fail(section
, ENV_PRIVATE_KEY
);
622 if (!app_passwd(bio_err
, passargin
, NULL
, &key
, NULL
)) {
623 BIO_printf(bio_err
, "Error getting password\n");
627 pkey
= load_key(bio_err
, keyfile
, keyform
, 0, key
, "CA private key");
629 explicit_bzero(key
, strlen(key
));
631 /* load_key() has already printed an appropriate message */
634 /*****************************************************************/
635 /* we need a certificate */
636 if (!selfsign
|| spkac_file
|| ss_cert_file
|| gencrl
) {
637 if ((certfile
== NULL
) &&
638 ((certfile
= NCONF_get_string(conf
,
639 section
, ENV_CERTIFICATE
)) == NULL
)) {
640 lookup_fail(section
, ENV_CERTIFICATE
);
643 x509
= load_cert(bio_err
, certfile
, FORMAT_PEM
, NULL
,
648 if (!X509_check_private_key(x509
, pkey
)) {
650 "CA certificate and CA private key do not match\n");
657 f
= NCONF_get_string(conf
, BASE_SECTION
, ENV_PRESERVE
);
660 if ((f
!= NULL
) && ((*f
== 'y') || (*f
== 'Y')))
662 f
= NCONF_get_string(conf
, BASE_SECTION
, ENV_MSIE_HACK
);
665 if ((f
!= NULL
) && ((*f
== 'y') || (*f
== 'Y')))
668 f
= NCONF_get_string(conf
, section
, ENV_NAMEOPT
);
671 if (!set_name_ex(&nameopt
, f
)) {
673 "Invalid name options: \"%s\"\n", f
);
680 f
= NCONF_get_string(conf
, section
, ENV_CERTOPT
);
683 if (!set_cert_ex(&certopt
, f
)) {
685 "Invalid certificate options: \"%s\"\n", f
);
692 f
= NCONF_get_string(conf
, section
, ENV_EXTCOPY
);
695 if (!set_ext_copy(&ext_copy
, f
)) {
697 "Invalid extension copy option: \"%s\"\n", f
);
703 /*****************************************************************/
704 /* lookup where to write new certificates */
705 if (outdir
== NULL
&& req
) {
706 if ((outdir
= NCONF_get_string(conf
, section
,
707 ENV_NEW_CERTS_DIR
)) == NULL
) {
708 BIO_printf(bio_err
, "output directory %s not defined\n",
713 /*****************************************************************/
714 /* we need to load the database file */
715 if ((dbfile
= NCONF_get_string(conf
, section
, ENV_DATABASE
)) == NULL
) {
716 lookup_fail(section
, ENV_DATABASE
);
719 db
= load_index(dbfile
, &db_attr
);
723 /* Lets check some fields */
724 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(db
->db
->data
); i
++) {
725 pp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, i
);
726 if ((pp
[DB_type
][0] != DB_TYPE_REV
) &&
727 (pp
[DB_rev_date
][0] != '\0')) {
728 BIO_printf(bio_err
, "entry %d: not revoked yet, but has a revocation date\n", i
+ 1);
731 if ((pp
[DB_type
][0] == DB_TYPE_REV
) &&
732 !make_revoked(NULL
, pp
[DB_rev_date
])) {
733 BIO_printf(bio_err
, " in entry %d\n", i
+ 1);
736 if (!check_time_format((char *) pp
[DB_exp_date
])) {
737 BIO_printf(bio_err
, "entry %d: invalid expiry date\n",
747 if ((j
& 1) || (j
< 2)) {
749 "entry %d: bad serial number length (%d)\n",
754 if (!(((*p
>= '0') && (*p
<= '9')) ||
755 ((*p
>= 'A') && (*p
<= 'F')) ||
756 ((*p
>= 'a') && (*p
<= 'f')))) {
757 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
);
764 BIO_set_fp(out
, stdout
, BIO_NOCLOSE
| BIO_FP_TEXT
); /* cannot fail */
765 TXT_DB_write(out
, db
->db
);
766 BIO_printf(bio_err
, "%d entries loaded from the database\n",
767 sk_OPENSSL_PSTRING_num(db
->db
->data
));
768 BIO_printf(bio_err
, "generating index\n");
770 if (!index_index(db
))
773 /*****************************************************************/
774 /* Update the db file for expired certificates */
777 BIO_printf(bio_err
, "Updating %s ...\n", dbfile
);
781 BIO_printf(bio_err
, "Malloc failure\n");
786 "No entries found to mark expired\n");
788 if (!save_index(dbfile
, "new", db
))
791 if (!rotate_index(dbfile
, "new", "old"))
796 "Done. %d entries marked as expired\n", i
);
799 /*****************************************************************/
800 /* Read extentions config file */
802 extconf
= NCONF_new(NULL
);
803 if (NCONF_load(extconf
, extfile
, &errorline
) <= 0) {
806 "ERROR: loading the config file '%s'\n",
810 "ERROR: on line %ld of config file '%s'\n",
817 "Successfully loaded extensions file %s\n",
820 /* We can have sections in the ext file */
821 if (!extensions
&& !(extensions
= NCONF_get_string(extconf
,
822 "default", "extensions")))
823 extensions
= "default";
825 /*****************************************************************/
827 if (outfile
!= NULL
) {
828 if (BIO_write_filename(Sout
, outfile
) <= 0) {
833 BIO_set_fp(Sout
, stdout
, BIO_NOCLOSE
| BIO_FP_TEXT
);
836 if ((md
== NULL
) && ((md
= NCONF_get_string(conf
, section
,
837 ENV_DEFAULT_MD
)) == NULL
)) {
838 lookup_fail(section
, ENV_DEFAULT_MD
);
841 if (!strcmp(md
, "default")) {
843 if (EVP_PKEY_get_default_digest_nid(pkey
, &def_nid
) <= 0) {
844 BIO_puts(bio_err
, "no default digest\n");
847 md
= (char *) OBJ_nid2sn(def_nid
);
849 if ((dgst
= EVP_get_digestbyname(md
)) == NULL
) {
851 "%s is an unsupported message digest type\n", md
);
855 if ((email_dn
== 1) && ((tmp_email_dn
= NCONF_get_string(conf
,
856 section
, ENV_DEFAULT_EMAIL_DN
)) != NULL
)) {
857 if (strcmp(tmp_email_dn
, "no") == 0)
861 BIO_printf(bio_err
, "message digest is %s\n",
862 OBJ_nid2ln(dgst
->type
));
863 if ((policy
== NULL
) && ((policy
= NCONF_get_string(conf
,
864 section
, ENV_POLICY
)) == NULL
)) {
865 lookup_fail(section
, ENV_POLICY
);
869 BIO_printf(bio_err
, "policy is %s\n", policy
);
871 if ((serialfile
= NCONF_get_string(conf
, section
,
872 ENV_SERIAL
)) == NULL
) {
873 lookup_fail(section
, ENV_SERIAL
);
878 * no '-extfile' option, so we look for extensions in
879 * the main configuration file
882 extensions
= NCONF_get_string(conf
, section
,
888 /* Check syntax of file */
890 X509V3_set_ctx_test(&ctx
);
891 X509V3_set_nconf(&ctx
, conf
);
892 if (!X509V3_EXT_add_nconf(conf
, &ctx
,
895 "Error Loading extension section %s\n",
902 if (startdate
== NULL
) {
903 startdate
= NCONF_get_string(conf
, section
,
904 ENV_DEFAULT_STARTDATE
);
905 if (startdate
== NULL
)
908 if (startdate
&& !ASN1_TIME_set_string(NULL
, startdate
)) {
909 BIO_printf(bio_err
, "start date is invalid, it should be YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ\n");
912 if (startdate
== NULL
)
915 if (enddate
== NULL
) {
916 enddate
= NCONF_get_string(conf
, section
,
917 ENV_DEFAULT_ENDDATE
);
921 if (enddate
&& !ASN1_TIME_set_string(NULL
, enddate
)) {
922 BIO_printf(bio_err
, "end date is invalid, it should be YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ\n");
926 if (!NCONF_get_number(conf
, section
,
927 ENV_DEFAULT_DAYS
, &days
))
930 if (!enddate
&& (days
== 0)) {
932 "cannot lookup how many days to certify for\n");
935 if ((serial
= load_serial(serialfile
, create_ser
, NULL
)) ==
938 "error while loading serial number\n");
942 if (BN_is_zero(serial
))
944 "next serial number is 00\n");
946 if ((f
= BN_bn2hex(serial
)) == NULL
)
949 "next serial number is %s\n", f
);
953 if ((attribs
= NCONF_get_section(conf
, policy
)) == NULL
) {
955 "unable to find 'section' for %s\n", policy
);
958 if ((cert_sk
= sk_X509_new_null()) == NULL
) {
959 BIO_printf(bio_err
, "Memory allocation failure\n");
962 if (spkac_file
!= NULL
) {
964 j
= certify_spkac(&x
, spkac_file
, pkey
, x509
, dgst
,
965 sigopts
, attribs
, db
, serial
, subj
, chtype
,
966 multirdn
, email_dn
, startdate
, enddate
, days
,
967 extensions
, conf
, verbose
, certopt
, nameopt
,
968 default_op
, ext_copy
);
973 BIO_printf(bio_err
, "\n");
974 if (!BN_add_word(serial
, 1))
976 if (!sk_X509_push(cert_sk
, x
)) {
978 "Memory allocation failure\n");
987 if (ss_cert_file
!= NULL
) {
989 j
= certify_cert(&x
, ss_cert_file
, pkey
, x509
, dgst
,
990 sigopts
, attribs
, db
, serial
, subj
, chtype
,
991 multirdn
, email_dn
, startdate
, enddate
, days
, batch
,
992 extensions
, conf
, verbose
, certopt
, nameopt
,
993 default_op
, ext_copy
);
998 BIO_printf(bio_err
, "\n");
999 if (!BN_add_word(serial
, 1))
1001 if (!sk_X509_push(cert_sk
, x
)) {
1003 "Memory allocation failure\n");
1008 if (infile
!= NULL
) {
1010 j
= certify(&x
, infile
, pkey
, x509p
, dgst
, sigopts
,
1011 attribs
, db
, serial
, subj
, chtype
, multirdn
,
1012 email_dn
, startdate
, enddate
, days
, batch
,
1013 extensions
, conf
, verbose
, certopt
, nameopt
,
1014 default_op
, ext_copy
, selfsign
);
1019 BIO_printf(bio_err
, "\n");
1020 if (!BN_add_word(serial
, 1))
1022 if (!sk_X509_push(cert_sk
, x
)) {
1024 "Memory allocation failure\n");
1029 for (i
= 0; i
< argc
; i
++) {
1031 j
= certify(&x
, argv
[i
], pkey
, x509p
, dgst
, sigopts
,
1032 attribs
, db
, serial
, subj
, chtype
, multirdn
,
1033 email_dn
, startdate
, enddate
, days
, batch
,
1034 extensions
, conf
, verbose
, certopt
, nameopt
,
1035 default_op
, ext_copy
, selfsign
);
1040 BIO_printf(bio_err
, "\n");
1041 if (!BN_add_word(serial
, 1))
1043 if (!sk_X509_push(cert_sk
, x
)) {
1045 "Memory allocation failure\n");
1051 * we have a stack of newly certified certificates and a data
1052 * base and serial number that need updating
1055 if (sk_X509_num(cert_sk
) > 0) {
1059 BIO_printf(bio_err
, "\n%d out of %d certificate requests certified, commit? [y/n]", total_done
, total
);
1060 (void) BIO_flush(bio_err
);
1061 if (!fgets(answer
, sizeof answer
- 1, stdin
)) {
1062 BIO_printf(bio_err
, "CERTIFICATION CANCELED: I/O error\n");
1066 if ((answer
[0] != 'y') && (answer
[0] != 'Y')) {
1067 BIO_printf(bio_err
, "CERTIFICATION CANCELED\n");
1072 BIO_printf(bio_err
, "Write out database with %d new entries\n", sk_X509_num(cert_sk
));
1074 if (!save_serial(serialfile
, "new", serial
, NULL
))
1077 if (!save_index(dbfile
, "new", db
))
1081 BIO_printf(bio_err
, "writing new certificates\n");
1082 for (i
= 0; i
< sk_X509_num(cert_sk
); i
++) {
1085 unsigned char *data
;
1086 char pempath
[PATH_MAX
];
1088 x
= sk_X509_value(cert_sk
, i
);
1090 j
= x
->cert_info
->serialNumber
->length
;
1091 data
= (unsigned char *)x
->cert_info
->serialNumber
->data
;
1093 serialstr
= bin2hex(data
, j
);
1095 serialstr
= strdup("00");
1097 k
= snprintf(pempath
, sizeof(pempath
),
1098 "%s/%s.pem", outdir
, serialstr
);
1100 if (k
== -1 || k
>= sizeof(pempath
)) {
1102 "certificate file name too long\n");
1107 "memory allocation failed\n");
1111 BIO_printf(bio_err
, "writing %s\n", pempath
);
1113 if (BIO_write_filename(Cout
, pempath
) <= 0) {
1117 write_new_certificate(Cout
, x
, 0, notext
);
1118 write_new_certificate(Sout
, x
, output_der
, notext
);
1121 if (sk_X509_num(cert_sk
)) {
1122 /* Rename the database and the serial file */
1123 if (!rotate_serial(serialfile
, "new", "old"))
1126 if (!rotate_index(dbfile
, "new", "old"))
1129 BIO_printf(bio_err
, "Data Base Updated\n");
1132 /*****************************************************************/
1136 crl_ext
= NCONF_get_string(conf
, section
, ENV_CRLEXT
);
1141 /* Check syntax of file */
1143 X509V3_set_ctx_test(&ctx
);
1144 X509V3_set_nconf(&ctx
, conf
);
1145 if (!X509V3_EXT_add_nconf(conf
, &ctx
, crl_ext
, NULL
)) {
1147 "Error Loading CRL extension section %s\n",
1153 if ((crlnumberfile
= NCONF_get_string(conf
, section
,
1154 ENV_CRLNUMBER
)) != NULL
)
1155 if ((crlnumber
= load_serial(crlnumberfile
, 0,
1158 "error while loading CRL number\n");
1161 if (!crldays
&& !crlhours
&& !crlsec
) {
1162 if (!NCONF_get_number(conf
, section
,
1163 ENV_DEFAULT_CRL_DAYS
, &crldays
))
1165 if (!NCONF_get_number(conf
, section
,
1166 ENV_DEFAULT_CRL_HOURS
, &crlhours
))
1170 if ((crldays
== 0) && (crlhours
== 0) && (crlsec
== 0)) {
1171 BIO_printf(bio_err
, "cannot lookup how long until the next CRL is issued\n");
1175 BIO_printf(bio_err
, "making CRL\n");
1176 if ((crl
= X509_CRL_new()) == NULL
)
1178 if (!X509_CRL_set_issuer_name(crl
, X509_get_subject_name(x509
)))
1181 tmptm
= ASN1_TIME_new();
1184 X509_gmtime_adj(tmptm
, 0);
1185 X509_CRL_set_lastUpdate(crl
, tmptm
);
1186 if (!X509_time_adj_ex(tmptm
, crldays
,
1187 crlhours
* 60 * 60 + crlsec
, NULL
)) {
1188 BIO_puts(bio_err
, "error setting CRL nextUpdate\n");
1191 X509_CRL_set_nextUpdate(crl
, tmptm
);
1193 ASN1_TIME_free(tmptm
);
1195 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(db
->db
->data
); i
++) {
1196 pp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, i
);
1197 if (pp
[DB_type
][0] == DB_TYPE_REV
) {
1198 if ((r
= X509_REVOKED_new()) == NULL
)
1200 j
= make_revoked(r
, pp
[DB_rev_date
]);
1205 if (!BN_hex2bn(&serial
, pp
[DB_serial
]))
1207 tmpser
= BN_to_ASN1_INTEGER(serial
, NULL
);
1212 X509_REVOKED_set_serialNumber(r
, tmpser
);
1213 ASN1_INTEGER_free(tmpser
);
1214 X509_CRL_add0_revoked(crl
, r
);
1219 * sort the data so it will be written in serial number order
1223 /* we now have a CRL */
1225 BIO_printf(bio_err
, "signing CRL\n");
1227 /* Add any extensions asked for */
1229 if (crl_ext
|| crlnumberfile
!= NULL
) {
1231 X509V3_set_ctx(&crlctx
, x509
, NULL
, NULL
, crl
, 0);
1232 X509V3_set_nconf(&crlctx
, conf
);
1235 if (!X509V3_EXT_CRL_add_nconf(conf
, &crlctx
,
1238 if (crlnumberfile
!= NULL
) {
1239 tmpser
= BN_to_ASN1_INTEGER(crlnumber
, NULL
);
1242 X509_CRL_add1_ext_i2d(crl
, NID_crl_number
,
1244 ASN1_INTEGER_free(tmpser
);
1246 if (!BN_add_word(crlnumber
, 1))
1250 if (crl_ext
|| crl_v2
) {
1251 if (!X509_CRL_set_version(crl
, 1))
1252 goto err
; /* version 2 CRL */
1254 if (crlnumberfile
!= NULL
) /* we have a CRL number that
1256 if (!save_serial(crlnumberfile
, "new", crlnumber
, NULL
))
1263 if (!do_X509_CRL_sign(bio_err
, crl
, pkey
, dgst
, sigopts
))
1266 PEM_write_bio_X509_CRL(Sout
, crl
);
1268 if (crlnumberfile
!= NULL
) /* Rename the crlnumber file */
1269 if (!rotate_serial(crlnumberfile
, "new", "old"))
1273 /*****************************************************************/
1275 if (infile
== NULL
) {
1276 BIO_printf(bio_err
, "no input files\n");
1280 revcert
= load_cert(bio_err
, infile
, FORMAT_PEM
,
1282 if (revcert
== NULL
)
1284 j
= do_revoke(revcert
, db
, rev_type
, rev_arg
);
1289 if (!save_index(dbfile
, "new", db
))
1292 if (!rotate_index(dbfile
, "new", "old"))
1295 BIO_printf(bio_err
, "Data Base Updated\n");
1298 /*****************************************************************/
1310 sk_X509_pop_free(cert_sk
, X509_free
);
1313 ERR_print_errors(bio_err
);
1314 if (free_key
&& key
)
1320 sk_OPENSSL_STRING_free(sigopts
);
1321 EVP_PKEY_free(pkey
);
1326 NCONF_free(extconf
);
1333 lookup_fail(const char *name
, const char *tag
)
1335 BIO_printf(bio_err
, "variable lookup failed for %s::%s\n", name
, tag
);
1339 certify(X509
** xret
, char *infile
, EVP_PKEY
* pkey
, X509
* x509
,
1340 const EVP_MD
* dgst
, STACK_OF(OPENSSL_STRING
) * sigopts
,
1341 STACK_OF(CONF_VALUE
) * policy
, CA_DB
* db
, BIGNUM
* serial
, char *subj
,
1342 unsigned long chtype
, int multirdn
, int email_dn
, char *startdate
,
1343 char *enddate
, long days
, int batch
, char *ext_sect
, CONF
* lconf
,
1344 int verbose
, unsigned long certopt
, unsigned long nameopt
, int default_op
,
1345 int ext_copy
, int selfsign
)
1347 X509_REQ
*req
= NULL
;
1349 EVP_PKEY
*pktmp
= NULL
;
1352 in
= BIO_new(BIO_s_file());
1354 if (BIO_read_filename(in
, infile
) <= 0) {
1358 if ((req
= PEM_read_bio_X509_REQ(in
, NULL
, NULL
, NULL
)) == NULL
) {
1359 BIO_printf(bio_err
, "Error reading certificate request in %s\n",
1364 X509_REQ_print(bio_err
, req
);
1366 BIO_printf(bio_err
, "Check that the request matches the signature\n");
1368 if (selfsign
&& !X509_REQ_check_private_key(req
, pkey
)) {
1370 "Certificate request and CA private key do not match\n");
1374 if ((pktmp
= X509_REQ_get_pubkey(req
)) == NULL
) {
1375 BIO_printf(bio_err
, "error unpacking public key\n");
1378 i
= X509_REQ_verify(req
, pktmp
);
1379 EVP_PKEY_free(pktmp
);
1382 BIO_printf(bio_err
, "Signature verification problems....\n");
1388 "Signature did not match the certificate request\n");
1391 BIO_printf(bio_err
, "Signature ok\n");
1393 ok
= do_body(xret
, pkey
, x509
, dgst
, sigopts
, policy
, db
, serial
,
1394 subj
, chtype
, multirdn
, email_dn
, startdate
, enddate
, days
, batch
,
1395 verbose
, req
, ext_sect
, lconf
, certopt
, nameopt
, default_op
,
1396 ext_copy
, selfsign
);
1407 certify_cert(X509
** xret
, char *infile
, EVP_PKEY
* pkey
, X509
* x509
,
1408 const EVP_MD
* dgst
, STACK_OF(OPENSSL_STRING
) * sigopts
,
1409 STACK_OF(CONF_VALUE
) * policy
, CA_DB
* db
, BIGNUM
* serial
, char *subj
,
1410 unsigned long chtype
, int multirdn
, int email_dn
, char *startdate
,
1411 char *enddate
, long days
, int batch
, char *ext_sect
, CONF
* lconf
,
1412 int verbose
, unsigned long certopt
, unsigned long nameopt
, int default_op
,
1416 X509_REQ
*rreq
= NULL
;
1417 EVP_PKEY
*pktmp
= NULL
;
1420 if ((req
= load_cert(bio_err
, infile
, FORMAT_PEM
, NULL
,
1424 X509_print(bio_err
, req
);
1426 BIO_printf(bio_err
, "Check that the request matches the signature\n");
1428 if ((pktmp
= X509_get_pubkey(req
)) == NULL
) {
1429 BIO_printf(bio_err
, "error unpacking public key\n");
1432 i
= X509_verify(req
, pktmp
);
1433 EVP_PKEY_free(pktmp
);
1436 BIO_printf(bio_err
, "Signature verification problems....\n");
1442 "Signature did not match the certificate\n");
1445 BIO_printf(bio_err
, "Signature ok\n");
1447 if ((rreq
= X509_to_X509_REQ(req
, NULL
, EVP_md5())) == NULL
)
1450 ok
= do_body(xret
, pkey
, x509
, dgst
, sigopts
, policy
, db
, serial
,
1451 subj
, chtype
, multirdn
, email_dn
, startdate
, enddate
, days
, batch
,
1452 verbose
, rreq
, ext_sect
, lconf
, certopt
, nameopt
, default_op
,
1457 X509_REQ_free(rreq
);
1464 do_body(X509
** xret
, EVP_PKEY
* pkey
, X509
* x509
, const EVP_MD
* dgst
,
1465 STACK_OF(OPENSSL_STRING
) * sigopts
, STACK_OF(CONF_VALUE
) * policy
,
1466 CA_DB
* db
, BIGNUM
* serial
, char *subj
, unsigned long chtype
, int multirdn
,
1467 int email_dn
, char *startdate
, char *enddate
, long days
, int batch
,
1468 int verbose
, X509_REQ
* req
, char *ext_sect
, CONF
* lconf
,
1469 unsigned long certopt
, unsigned long nameopt
, int default_op
,
1470 int ext_copy
, int selfsign
)
1472 X509_NAME
*name
= NULL
, *CAname
= NULL
, *subject
= NULL
, *dn_subject
= NULL
;
1473 ASN1_UTCTIME
*tm
, *tmptm
;
1474 ASN1_STRING
*str
, *str2
;
1478 X509_NAME_ENTRY
*ne
;
1479 X509_NAME_ENTRY
*tne
, *push
;
1481 int ok
= -1, i
, j
, last
, nid
;
1484 OPENSSL_STRING row
[DB_NUMBER
];
1485 OPENSSL_STRING
*irow
= NULL
;
1486 OPENSSL_STRING
*rrow
= NULL
;
1488 tmptm
= ASN1_UTCTIME_new();
1489 if (tmptm
== NULL
) {
1490 BIO_printf(bio_err
, "malloc error\n");
1493 for (i
= 0; i
< DB_NUMBER
; i
++)
1497 X509_NAME
*n
= parse_name(subj
, chtype
, multirdn
);
1500 ERR_print_errors(bio_err
);
1503 X509_REQ_set_subject_name(req
, n
);
1504 req
->req_info
->enc
.modified
= 1;
1509 "The Subject's Distinguished Name is as follows\n");
1511 name
= X509_REQ_get_subject_name(req
);
1512 for (i
= 0; i
< X509_NAME_entry_count(name
); i
++) {
1513 ne
= X509_NAME_get_entry(name
, i
);
1514 str
= X509_NAME_ENTRY_get_data(ne
);
1515 obj
= X509_NAME_ENTRY_get_object(ne
);
1518 /* assume all type should be strings */
1519 nid
= OBJ_obj2nid(ne
->object
);
1521 if (str
->type
== V_ASN1_UNIVERSALSTRING
)
1522 ASN1_UNIVERSALSTRING_to_string(str
);
1524 if ((str
->type
== V_ASN1_IA5STRING
) &&
1525 (nid
!= NID_pkcs9_emailAddress
))
1526 str
->type
= V_ASN1_T61STRING
;
1528 if ((nid
== NID_pkcs9_emailAddress
) &&
1529 (str
->type
== V_ASN1_PRINTABLESTRING
))
1530 str
->type
= V_ASN1_IA5STRING
;
1532 /* If no EMAIL is wanted in the subject */
1533 if ((OBJ_obj2nid(obj
) == NID_pkcs9_emailAddress
) && (!email_dn
))
1536 /* check some things */
1537 if ((OBJ_obj2nid(obj
) == NID_pkcs9_emailAddress
) &&
1538 (str
->type
!= V_ASN1_IA5STRING
)) {
1539 BIO_printf(bio_err
, "\nemailAddress type needs to be of type IA5STRING\n");
1542 if ((str
->type
!= V_ASN1_BMPSTRING
) &&
1543 (str
->type
!= V_ASN1_UTF8STRING
)) {
1544 j
= ASN1_PRINTABLE_type(str
->data
, str
->length
);
1545 if (((j
== V_ASN1_T61STRING
) &&
1546 (str
->type
!= V_ASN1_T61STRING
)) ||
1547 ((j
== V_ASN1_IA5STRING
) &&
1548 (str
->type
== V_ASN1_PRINTABLESTRING
))) {
1549 BIO_printf(bio_err
, "\nThe string contains characters that are illegal for the ASN.1 type\n");
1554 old_entry_print(bio_err
, obj
, str
);
1557 /* Ok, now we check the 'policy' stuff. */
1558 if ((subject
= X509_NAME_new()) == NULL
) {
1559 BIO_printf(bio_err
, "Memory allocation failure\n");
1562 /* take a copy of the issuer name before we mess with it. */
1564 CAname
= X509_NAME_dup(name
);
1566 CAname
= X509_NAME_dup(x509
->cert_info
->subject
);
1571 for (i
= 0; i
< sk_CONF_VALUE_num(policy
); i
++) {
1572 cv
= sk_CONF_VALUE_value(policy
, i
); /* get the object id */
1573 if ((j
= OBJ_txt2nid(cv
->name
)) == NID_undef
) {
1574 BIO_printf(bio_err
, "%s:unknown object type in 'policy' configuration\n", cv
->name
);
1577 obj
= OBJ_nid2obj(j
);
1581 /* lookup the object in the supplied name list */
1582 j
= X509_NAME_get_index_by_OBJ(name
, obj
, last
);
1588 tne
= X509_NAME_get_entry(name
, j
);
1592 /* depending on the 'policy', decide what to do. */
1594 if (strcmp(cv
->value
, "optional") == 0) {
1597 } else if (strcmp(cv
->value
, "supplied") == 0) {
1599 BIO_printf(bio_err
, "The %s field needed to be supplied and was missing\n", cv
->name
);
1603 } else if (strcmp(cv
->value
, "match") == 0) {
1607 BIO_printf(bio_err
, "The mandatory %s field was missing\n", cv
->name
);
1613 j
= X509_NAME_get_index_by_OBJ(CAname
, obj
, last2
);
1614 if ((j
< 0) && (last2
== -1)) {
1615 BIO_printf(bio_err
, "The %s field does not exist in the CA certificate,\nthe 'policy' is misconfigured\n", cv
->name
);
1619 push
= X509_NAME_get_entry(CAname
, j
);
1620 str
= X509_NAME_ENTRY_get_data(tne
);
1621 str2
= X509_NAME_ENTRY_get_data(push
);
1623 if (ASN1_STRING_cmp(str
, str2
) != 0)
1627 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
));
1631 BIO_printf(bio_err
, "%s:invalid type in 'policy' configuration\n", cv
->value
);
1636 if (!X509_NAME_add_entry(subject
, push
,
1639 X509_NAME_ENTRY_free(push
);
1641 "Memory allocation failure\n");
1651 X509_NAME_free(subject
);
1652 /* subject=X509_NAME_dup(X509_REQ_get_subject_name(req)); */
1653 subject
= X509_NAME_dup(name
);
1654 if (subject
== NULL
)
1658 BIO_printf(bio_err
, "The subject name appears to be ok, checking data base for clashes\n");
1660 /* Build the correct Subject if no email is wanted in the subject */
1662 * and add it later on because of the method extensions are added
1667 dn_subject
= subject
;
1669 X509_NAME_ENTRY
*tmpne
;
1671 * Its best to dup the subject DN and then delete any email
1672 * addresses because this retains its structure.
1674 if (!(dn_subject
= X509_NAME_dup(subject
))) {
1675 BIO_printf(bio_err
, "Memory allocation failure\n");
1678 while ((i
= X509_NAME_get_index_by_NID(dn_subject
,
1679 NID_pkcs9_emailAddress
, -1)) >= 0) {
1680 tmpne
= X509_NAME_get_entry(dn_subject
, i
);
1681 X509_NAME_delete_entry(dn_subject
, i
);
1682 X509_NAME_ENTRY_free(tmpne
);
1686 if (BN_is_zero(serial
))
1687 row
[DB_serial
] = strdup("00");
1689 row
[DB_serial
] = BN_bn2hex(serial
);
1690 if (row
[DB_serial
] == NULL
) {
1691 BIO_printf(bio_err
, "Memory allocation failure\n");
1694 if (db
->attributes
.unique_subject
) {
1695 OPENSSL_STRING
*crow
= row
;
1697 rrow
= TXT_DB_get_by_index(db
->db
, DB_name
, crow
);
1700 "ERROR:There is already a certificate for %s\n",
1705 rrow
= TXT_DB_get_by_index(db
->db
, DB_serial
, row
);
1708 "ERROR:Serial number %s has already been issued,\n",
1710 BIO_printf(bio_err
, " check the database/serial_file for corruption\n");
1715 "The matching entry has the following details\n");
1716 if (rrow
[DB_type
][0] == 'E')
1718 else if (rrow
[DB_type
][0] == 'R')
1720 else if (rrow
[DB_type
][0] == 'V')
1723 p
= "\ninvalid type, Data base error\n";
1724 BIO_printf(bio_err
, "Type :%s\n", p
);
1725 if (rrow
[DB_type
][0] == 'R') {
1726 p
= rrow
[DB_exp_date
];
1729 BIO_printf(bio_err
, "Was revoked on:%s\n", p
);
1731 p
= rrow
[DB_exp_date
];
1734 BIO_printf(bio_err
, "Expires on :%s\n", p
);
1735 p
= rrow
[DB_serial
];
1738 BIO_printf(bio_err
, "Serial Number :%s\n", p
);
1742 BIO_printf(bio_err
, "File name :%s\n", p
);
1746 BIO_printf(bio_err
, "Subject Name :%s\n", p
);
1747 ok
= -1; /* This is now a 'bad' error. */
1750 /* We are now totally happy, lets make and sign the certificate */
1752 BIO_printf(bio_err
, "Everything appears to be ok, creating and signing the certificate\n");
1754 if ((ret
= X509_new()) == NULL
)
1756 ci
= ret
->cert_info
;
1759 /* Make it an X509 v3 certificate. */
1760 if (!X509_set_version(ret
, 2))
1763 if (ci
->serialNumber
== NULL
)
1765 if (BN_to_ASN1_INTEGER(serial
, ci
->serialNumber
) == NULL
)
1768 if (!X509_set_issuer_name(ret
, subject
))
1771 if (!X509_set_issuer_name(ret
, X509_get_subject_name(x509
)))
1775 if (strcmp(startdate
, "today") == 0)
1776 X509_gmtime_adj(X509_get_notBefore(ret
), 0);
1778 ASN1_TIME_set_string(X509_get_notBefore(ret
), startdate
);
1780 if (enddate
== NULL
)
1781 X509_time_adj_ex(X509_get_notAfter(ret
), days
, 0, NULL
);
1783 ASN1_TIME_set_string(X509_get_notAfter(ret
), enddate
);
1785 if (!X509_set_subject_name(ret
, subject
))
1788 pktmp
= X509_REQ_get_pubkey(req
);
1789 i
= X509_set_pubkey(ret
, pktmp
);
1790 EVP_PKEY_free(pktmp
);
1794 /* Lets add the extensions, if there are any */
1797 if (ci
->version
== NULL
)
1798 if ((ci
->version
= ASN1_INTEGER_new()) == NULL
)
1800 ASN1_INTEGER_set(ci
->version
, 2); /* version 3 certificate */
1803 * Free the current entries if any, there should not be any I
1806 if (ci
->extensions
!= NULL
)
1807 sk_X509_EXTENSION_pop_free(ci
->extensions
,
1808 X509_EXTENSION_free
);
1810 ci
->extensions
= NULL
;
1812 /* Initialize the context structure */
1814 X509V3_set_ctx(&ctx
, ret
, ret
, req
, NULL
, 0);
1816 X509V3_set_ctx(&ctx
, x509
, ret
, req
, NULL
, 0);
1821 "Extra configuration file found\n");
1823 /* Use the extconf configuration db LHASH */
1824 X509V3_set_nconf(&ctx
, extconf
);
1826 /* Test the structure (needed?) */
1827 /* X509V3_set_ctx_test(&ctx); */
1829 /* Adds exts contained in the configuration file */
1830 if (!X509V3_EXT_add_nconf(extconf
, &ctx
,
1833 "ERROR: adding extensions in section %s\n",
1835 ERR_print_errors(bio_err
);
1839 BIO_printf(bio_err
, "Successfully added extensions from file.\n");
1840 } else if (ext_sect
) {
1841 /* We found extensions to be set from config file */
1842 X509V3_set_nconf(&ctx
, lconf
);
1844 if (!X509V3_EXT_add_nconf(lconf
, &ctx
, ext_sect
, ret
)) {
1846 "ERROR: adding extensions in section %s\n",
1848 ERR_print_errors(bio_err
);
1852 BIO_printf(bio_err
, "Successfully added extensions from config\n");
1855 /* Copy extensions from request (if any) */
1857 if (!copy_extensions(ret
, req
, ext_copy
)) {
1858 BIO_printf(bio_err
, "ERROR: adding extensions from request\n");
1859 ERR_print_errors(bio_err
);
1862 /* Set the right value for the noemailDN option */
1863 if (email_dn
== 0) {
1864 if (!X509_set_subject_name(ret
, dn_subject
))
1868 BIO_printf(bio_err
, "Certificate Details:\n");
1870 * Never print signature details because signature not
1873 certopt
|= X509_FLAG_NO_SIGDUMP
| X509_FLAG_NO_SIGNAME
;
1874 X509_print_ex(bio_err
, ret
, nameopt
, certopt
);
1876 BIO_printf(bio_err
, "Certificate is to be certified until ");
1877 ASN1_TIME_print(bio_err
, X509_get_notAfter(ret
));
1879 BIO_printf(bio_err
, " (%ld days)", days
);
1880 BIO_printf(bio_err
, "\n");
1885 BIO_printf(bio_err
, "Sign the certificate? [y/n]:");
1886 (void) BIO_flush(bio_err
);
1887 if (!fgets(answer
, sizeof(answer
) - 1, stdin
)) {
1889 "CERTIFICATE WILL NOT BE CERTIFIED: I/O error\n");
1893 if (!((answer
[0] == 'y') || (answer
[0] == 'Y'))) {
1895 "CERTIFICATE WILL NOT BE CERTIFIED\n");
1900 pktmp
= X509_get_pubkey(ret
);
1901 if (EVP_PKEY_missing_parameters(pktmp
) &&
1902 !EVP_PKEY_missing_parameters(pkey
))
1903 EVP_PKEY_copy_parameters(pktmp
, pkey
);
1904 EVP_PKEY_free(pktmp
);
1906 if (!do_X509_sign(bio_err
, ret
, pkey
, dgst
, sigopts
))
1909 /* We now just add it to the database */
1910 row
[DB_type
] = malloc(2);
1912 tm
= X509_get_notAfter(ret
);
1913 row
[DB_exp_date
] = malloc(tm
->length
+ 1);
1914 if (row
[DB_type
] == NULL
|| row
[DB_exp_date
] == NULL
) {
1915 BIO_printf(bio_err
, "Memory allocation failure\n");
1919 memcpy(row
[DB_exp_date
], tm
->data
, tm
->length
);
1920 row
[DB_exp_date
][tm
->length
] = '\0';
1922 row
[DB_rev_date
] = NULL
;
1924 /* row[DB_serial] done already */
1925 row
[DB_file
] = malloc(8);
1926 row
[DB_name
] = X509_NAME_oneline(X509_get_subject_name(ret
), NULL
, 0);
1928 if ((row
[DB_type
] == NULL
) || (row
[DB_file
] == NULL
) ||
1929 (row
[DB_name
] == NULL
)) {
1930 BIO_printf(bio_err
, "Memory allocation failure\n");
1933 (void) strlcpy(row
[DB_file
], "unknown", 8);
1934 row
[DB_type
][0] = 'V';
1935 row
[DB_type
][1] = '\0';
1937 if ((irow
= reallocarray(NULL
, DB_NUMBER
+ 1, sizeof(char *))) ==
1939 BIO_printf(bio_err
, "Memory allocation failure\n");
1942 for (i
= 0; i
< DB_NUMBER
; i
++) {
1946 irow
[DB_NUMBER
] = NULL
;
1948 if (!TXT_DB_insert(db
->db
, irow
)) {
1949 BIO_printf(bio_err
, "failed to update database\n");
1950 BIO_printf(bio_err
, "TXT_DB error number %ld\n", db
->db
->error
);
1955 for (i
= 0; i
< DB_NUMBER
; i
++)
1959 X509_NAME_free(CAname
);
1960 if (subject
!= NULL
)
1961 X509_NAME_free(subject
);
1962 if ((dn_subject
!= NULL
) && !email_dn
)
1963 X509_NAME_free(dn_subject
);
1965 ASN1_UTCTIME_free(tmptm
);
1976 write_new_certificate(BIO
* bp
, X509
* x
, int output_der
, int notext
)
1979 (void) i2d_X509_bio(bp
, x
);
1984 PEM_write_bio_X509(bp
, x
);
1988 certify_spkac(X509
** xret
, char *infile
, EVP_PKEY
* pkey
, X509
* x509
,
1989 const EVP_MD
* dgst
, STACK_OF(OPENSSL_STRING
) * sigopts
,
1990 STACK_OF(CONF_VALUE
) * policy
, CA_DB
* db
, BIGNUM
* serial
, char *subj
,
1991 unsigned long chtype
, int multirdn
, int email_dn
, char *startdate
,
1992 char *enddate
, long days
, char *ext_sect
, CONF
* lconf
, int verbose
,
1993 unsigned long certopt
, unsigned long nameopt
, int default_op
, int ext_copy
)
1995 STACK_OF(CONF_VALUE
) * sk
= NULL
;
1996 LHASH_OF(CONF_VALUE
) * parms
= NULL
;
1997 X509_REQ
*req
= NULL
;
1998 CONF_VALUE
*cv
= NULL
;
1999 NETSCAPE_SPKI
*spki
= NULL
;
2002 EVP_PKEY
*pktmp
= NULL
;
2003 X509_NAME
*n
= NULL
;
2009 * Load input file into a hash table. (This is just an easy
2010 * way to read and parse the file, then put it into a convenient
2013 parms
= CONF_load(NULL
, infile
, &errline
);
2014 if (parms
== NULL
) {
2015 BIO_printf(bio_err
, "error on line %ld of %s\n",
2017 ERR_print_errors(bio_err
);
2020 sk
= CONF_get_section(parms
, "default");
2021 if (sk_CONF_VALUE_num(sk
) == 0) {
2022 BIO_printf(bio_err
, "no name/value pairs found in %s\n",
2028 * Now create a dummy X509 request structure. We don't actually
2029 * have an X509 request, but we have many of the components
2030 * (a public key, various DN components). The idea is that we
2031 * put these components into the right X509 request structure
2032 * and we can use the same code as if you had a real X509 request.
2034 req
= X509_REQ_new();
2036 ERR_print_errors(bio_err
);
2040 * Build up the subject name set.
2046 if (sk_CONF_VALUE_num(sk
) <= i
)
2049 cv
= sk_CONF_VALUE_value(sk
, i
);
2052 * Skip past any leading X. X: X, etc to allow for multiple
2055 for (buf
= cv
->name
; *buf
; buf
++) {
2056 if ((*buf
== ':') || (*buf
== ',') || (*buf
== '.')) {
2065 if ((nid
= OBJ_txt2nid(type
)) == NID_undef
) {
2066 if (strcmp(type
, "SPKAC") == 0) {
2067 spki
= NETSCAPE_SPKI_b64_decode(cv
->value
, -1);
2069 BIO_printf(bio_err
, "unable to load Netscape SPKAC structure\n");
2070 ERR_print_errors(bio_err
);
2076 if (!X509_NAME_add_entry_by_NID(n
, nid
, chtype
,
2077 (unsigned char *)buf
, -1, -1, 0))
2082 "Netscape SPKAC structure not found in %s\n", infile
);
2086 * Now extract the key from the SPKI structure.
2090 "Check that the SPKAC request matches the signature\n");
2092 if ((pktmp
= NETSCAPE_SPKI_get_pubkey(spki
)) == NULL
) {
2093 BIO_printf(bio_err
, "error unpacking SPKAC public key\n");
2096 j
= NETSCAPE_SPKI_verify(spki
, pktmp
);
2099 "signature verification failed on SPKAC public key\n");
2102 BIO_printf(bio_err
, "Signature ok\n");
2104 X509_REQ_set_pubkey(req
, pktmp
);
2105 EVP_PKEY_free(pktmp
);
2106 ok
= do_body(xret
, pkey
, x509
, dgst
, sigopts
, policy
, db
, serial
,
2107 subj
, chtype
, multirdn
, email_dn
, startdate
, enddate
, days
, 1,
2108 verbose
, req
, ext_sect
, lconf
, certopt
, nameopt
, default_op
,
2117 NETSCAPE_SPKI_free(spki
);
2123 check_time_format(const char *str
)
2125 return ASN1_TIME_set_string(NULL
, str
);
2129 do_revoke(X509
* x509
, CA_DB
* db
, int type
, char *value
)
2131 ASN1_UTCTIME
*tm
= NULL
;
2132 char *row
[DB_NUMBER
], **rrow
, **irow
;
2133 char *rev_str
= NULL
;
2137 for (i
= 0; i
< DB_NUMBER
; i
++)
2139 row
[DB_name
] = X509_NAME_oneline(X509_get_subject_name(x509
), NULL
, 0);
2140 bn
= ASN1_INTEGER_to_BN(X509_get_serialNumber(x509
), NULL
);
2144 row
[DB_serial
] = strdup("00");
2146 row
[DB_serial
] = BN_bn2hex(bn
);
2148 if ((row
[DB_name
] == NULL
) || (row
[DB_serial
] == NULL
)) {
2149 BIO_printf(bio_err
, "Memory allocation failure\n");
2153 * We have to lookup by serial number because name lookup skips
2156 rrow
= TXT_DB_get_by_index(db
->db
, DB_serial
, row
);
2159 "Adding Entry with serial number %s to DB for %s\n",
2160 row
[DB_serial
], row
[DB_name
]);
2162 /* We now just add it to the database */
2163 row
[DB_type
] = malloc(2);
2165 tm
= X509_get_notAfter(x509
);
2166 row
[DB_exp_date
] = malloc(tm
->length
+ 1);
2167 if (row
[DB_type
] == NULL
|| row
[DB_exp_date
] == NULL
) {
2168 BIO_printf(bio_err
, "Memory allocation failure\n");
2171 memcpy(row
[DB_exp_date
], tm
->data
, tm
->length
);
2172 row
[DB_exp_date
][tm
->length
] = '\0';
2174 row
[DB_rev_date
] = NULL
;
2176 /* row[DB_serial] done already */
2177 row
[DB_file
] = malloc(8);
2179 /* row[DB_name] done already */
2181 if ((row
[DB_type
] == NULL
) || (row
[DB_file
] == NULL
)) {
2182 BIO_printf(bio_err
, "Memory allocation failure\n");
2185 (void) strlcpy(row
[DB_file
], "unknown", 8);
2186 row
[DB_type
][0] = 'V';
2187 row
[DB_type
][1] = '\0';
2189 if ((irow
= reallocarray(NULL
, sizeof(char *),
2190 (DB_NUMBER
+ 1))) == NULL
) {
2191 BIO_printf(bio_err
, "Memory allocation failure\n");
2194 for (i
= 0; i
< DB_NUMBER
; i
++) {
2198 irow
[DB_NUMBER
] = NULL
;
2200 if (!TXT_DB_insert(db
->db
, irow
)) {
2201 BIO_printf(bio_err
, "failed to update database\n");
2202 BIO_printf(bio_err
, "TXT_DB error number %ld\n",
2206 /* Revoke Certificate */
2207 ok
= do_revoke(x509
, db
, type
, value
);
2211 } else if (index_name_cmp_noconst(row
, rrow
)) {
2212 BIO_printf(bio_err
, "ERROR:name does not match %s\n",
2215 } else if (rrow
[DB_type
][0] == 'R') {
2216 BIO_printf(bio_err
, "ERROR:Already revoked, serial number %s\n",
2220 BIO_printf(bio_err
, "Revoking Certificate %s.\n",
2222 rev_str
= make_revocation_str(type
, value
);
2224 BIO_printf(bio_err
, "Error in revocation arguments\n");
2227 rrow
[DB_type
][0] = 'R';
2228 rrow
[DB_type
][1] = '\0';
2229 rrow
[DB_rev_date
] = rev_str
;
2234 for (i
= 0; i
< DB_NUMBER
; i
++)
2241 get_certificate_status(const char *serial
, CA_DB
* db
)
2243 char *row
[DB_NUMBER
], **rrow
;
2246 /* Free Resources */
2247 for (i
= 0; i
< DB_NUMBER
; i
++)
2250 /* Malloc needed char spaces */
2251 row
[DB_serial
] = malloc(strlen(serial
) + 2);
2252 if (row
[DB_serial
] == NULL
) {
2253 BIO_printf(bio_err
, "Malloc failure\n");
2256 if (strlen(serial
) % 2) {
2257 /* Set the first char to 0 */ ;
2258 row
[DB_serial
][0] = '0';
2260 /* Copy String from serial to row[DB_serial] */
2261 memcpy(row
[DB_serial
] + 1, serial
, strlen(serial
));
2262 row
[DB_serial
][strlen(serial
) + 1] = '\0';
2264 /* Copy String from serial to row[DB_serial] */
2265 memcpy(row
[DB_serial
], serial
, strlen(serial
));
2266 row
[DB_serial
][strlen(serial
)] = '\0';
2269 /* Make it Upper Case */
2270 for (i
= 0; row
[DB_serial
][i
] != '\0'; i
++)
2271 row
[DB_serial
][i
] = toupper((unsigned char) row
[DB_serial
][i
]);
2276 /* Search for the certificate */
2277 rrow
= TXT_DB_get_by_index(db
->db
, DB_serial
, row
);
2279 BIO_printf(bio_err
, "Serial %s not present in db.\n",
2283 } else if (rrow
[DB_type
][0] == 'V') {
2284 BIO_printf(bio_err
, "%s=Valid (%c)\n",
2285 row
[DB_serial
], rrow
[DB_type
][0]);
2287 } else if (rrow
[DB_type
][0] == 'R') {
2288 BIO_printf(bio_err
, "%s=Revoked (%c)\n",
2289 row
[DB_serial
], rrow
[DB_type
][0]);
2291 } else if (rrow
[DB_type
][0] == 'E') {
2292 BIO_printf(bio_err
, "%s=Expired (%c)\n",
2293 row
[DB_serial
], rrow
[DB_type
][0]);
2295 } else if (rrow
[DB_type
][0] == 'S') {
2296 BIO_printf(bio_err
, "%s=Suspended (%c)\n",
2297 row
[DB_serial
], rrow
[DB_type
][0]);
2300 BIO_printf(bio_err
, "%s=Unknown (%c).\n",
2301 row
[DB_serial
], rrow
[DB_type
][0]);
2306 for (i
= 0; i
< DB_NUMBER
; i
++)
2313 do_updatedb(CA_DB
* db
)
2315 ASN1_UTCTIME
*a_tm
= NULL
;
2317 int db_y2k
, a_y2k
; /* flags = 1 if y >= 2000 */
2318 char **rrow
, *a_tm_s
;
2320 a_tm
= ASN1_UTCTIME_new();
2322 /* get actual time and make a string */
2323 a_tm
= X509_gmtime_adj(a_tm
, 0);
2324 a_tm_s
= malloc(a_tm
->length
+ 1);
2325 if (a_tm_s
== NULL
) {
2329 memcpy(a_tm_s
, a_tm
->data
, a_tm
->length
);
2330 a_tm_s
[a_tm
->length
] = '\0';
2332 if (strncmp(a_tm_s
, "49", 2) <= 0)
2337 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(db
->db
->data
); i
++) {
2338 rrow
= sk_OPENSSL_PSTRING_value(db
->db
->data
, i
);
2340 if (rrow
[DB_type
][0] == 'V') {
2341 /* ignore entries that are not valid */
2342 if (strncmp(rrow
[DB_exp_date
], "49", 2) <= 0)
2347 if (db_y2k
== a_y2k
) {
2348 /* all on the same y2k side */
2349 if (strcmp(rrow
[DB_exp_date
], a_tm_s
) <= 0) {
2350 rrow
[DB_type
][0] = 'E';
2351 rrow
[DB_type
][1] = '\0';
2354 BIO_printf(bio_err
, "%s=Expired\n",
2357 } else if (db_y2k
< a_y2k
) {
2358 rrow
[DB_type
][0] = 'E';
2359 rrow
[DB_type
][1] = '\0';
2362 BIO_printf(bio_err
, "%s=Expired\n",
2369 ASN1_UTCTIME_free(a_tm
);
2375 static const char *crl_reasons
[] = {
2376 /* CRL reason strings */
2380 "affiliationChanged",
2382 "cessationOfOperation",
2385 /* Additional pseudo reasons */
2391 #define NUM_REASONS (sizeof(crl_reasons) / sizeof(char *))
2393 /* Given revocation information convert to a DB string.
2394 * The format of the string is:
2395 * revtime[,reason,extra]. Where 'revtime' is the
2396 * revocation time (the current time). 'reason' is the
2397 * optional CRL reason and 'extra' is any additional
2402 make_revocation_str(int rev_type
, char *rev_arg
)
2404 char *other
= NULL
, *str
;
2405 const char *reason
= NULL
;
2407 ASN1_UTCTIME
*revtm
= NULL
;
2413 case REV_CRL_REASON
:
2414 for (i
= 0; i
< 8; i
++) {
2415 if (!strcasecmp(rev_arg
, crl_reasons
[i
])) {
2416 reason
= crl_reasons
[i
];
2420 if (reason
== NULL
) {
2421 BIO_printf(bio_err
, "Unknown CRL reason %s\n", rev_arg
);
2427 /* Argument is an OID */
2429 otmp
= OBJ_txt2obj(rev_arg
, 0);
2430 ASN1_OBJECT_free(otmp
);
2434 "Invalid object identifier %s\n", rev_arg
);
2437 reason
= "holdInstruction";
2441 case REV_KEY_COMPROMISE
:
2442 case REV_CA_COMPROMISE
:
2444 /* Argument is the key compromise time */
2445 if (!ASN1_GENERALIZEDTIME_set_string(NULL
, rev_arg
)) {
2447 "Invalid time format %s. Need YYYYMMDDHHMMSSZ\n",
2452 if (rev_type
== REV_KEY_COMPROMISE
)
2455 reason
= "CAkeyTime";
2461 revtm
= X509_gmtime_adj(NULL
, 0);
2462 if (asprintf(&str
, "%s%s%s%s%s", revtm
->data
,
2463 reason
? "," : "", reason
? reason
: "",
2464 other
? "," : "", other
? other
: "") == -1)
2466 ASN1_UTCTIME_free(revtm
);
2470 /* Convert revocation field to X509_REVOKED entry
2474 * 2 OK and some extensions added (i.e. V2 CRL)
2478 make_revoked(X509_REVOKED
* rev
, const char *str
)
2481 int reason_code
= -1;
2483 ASN1_OBJECT
*hold
= NULL
;
2484 ASN1_GENERALIZEDTIME
*comp_time
= NULL
;
2485 ASN1_ENUMERATED
*rtmp
= NULL
;
2487 ASN1_TIME
*revDate
= NULL
;
2489 i
= unpack_revinfo(&revDate
, &reason_code
, &hold
, &comp_time
, str
);
2494 if (rev
&& !X509_REVOKED_set_revocationDate(rev
, revDate
))
2497 if (rev
&& (reason_code
!= OCSP_REVOKED_STATUS_NOSTATUS
)) {
2498 rtmp
= ASN1_ENUMERATED_new();
2499 if (!rtmp
|| !ASN1_ENUMERATED_set(rtmp
, reason_code
))
2501 if (!X509_REVOKED_add1_ext_i2d(rev
, NID_crl_reason
, rtmp
, 0, 0))
2504 if (rev
&& comp_time
) {
2505 if (!X509_REVOKED_add1_ext_i2d(rev
, NID_invalidity_date
,
2510 if (!X509_REVOKED_add1_ext_i2d(rev
, NID_hold_instruction_code
,
2514 if (reason_code
!= OCSP_REVOKED_STATUS_NOSTATUS
)
2522 ASN1_OBJECT_free(hold
);
2523 ASN1_GENERALIZEDTIME_free(comp_time
);
2524 ASN1_ENUMERATED_free(rtmp
);
2525 ASN1_TIME_free(revDate
);
2531 old_entry_print(BIO
* bp
, ASN1_OBJECT
* obj
, ASN1_STRING
* str
)
2533 char buf
[25], *pbuf
, *p
;
2536 j
= i2a_ASN1_OBJECT(bp
, obj
);
2538 for (j
= 22 - j
; j
> 0; j
--)
2544 if (str
->type
== V_ASN1_PRINTABLESTRING
)
2545 BIO_printf(bp
, "PRINTABLE:'");
2546 else if (str
->type
== V_ASN1_T61STRING
)
2547 BIO_printf(bp
, "T61STRING:'");
2548 else if (str
->type
== V_ASN1_IA5STRING
)
2549 BIO_printf(bp
, "IA5STRING:'");
2550 else if (str
->type
== V_ASN1_UNIVERSALSTRING
)
2551 BIO_printf(bp
, "UNIVERSALSTRING:'");
2553 BIO_printf(bp
, "ASN.1 %2d:'", str
->type
);
2555 p
= (char *) str
->data
;
2556 for (j
= str
->length
; j
> 0; j
--) {
2557 if ((*p
>= ' ') && (*p
<= '~'))
2558 BIO_printf(bp
, "%c", *p
);
2560 BIO_printf(bp
, "\\0x%02X", *p
);
2561 else if ((unsigned char) *p
== 0xf7)
2562 BIO_printf(bp
, "^?");
2564 BIO_printf(bp
, "^%c", *p
+ '@');
2567 BIO_printf(bp
, "'\n");
2572 unpack_revinfo(ASN1_TIME
** prevtm
, int *preason
, ASN1_OBJECT
** phold
,
2573 ASN1_GENERALIZEDTIME
** pinvtm
, const char *str
)
2576 char *rtime_str
, *reason_str
= NULL
, *arg_str
= NULL
, *p
;
2577 int reason_code
= -1;
2580 ASN1_OBJECT
*hold
= NULL
;
2581 ASN1_GENERALIZEDTIME
*comp_time
= NULL
;
2583 if ((tmp
= strdup(str
)) == NULL
) {
2584 BIO_printf(bio_err
, "malloc failed\n");
2587 p
= strchr(tmp
, ',');
2601 *prevtm
= ASN1_UTCTIME_new();
2602 if (!ASN1_UTCTIME_set_string(*prevtm
, rtime_str
)) {
2603 BIO_printf(bio_err
, "invalid revocation date %s\n",
2609 for (i
= 0; i
< NUM_REASONS
; i
++) {
2610 if (!strcasecmp(reason_str
, crl_reasons
[i
])) {
2615 if (reason_code
== OCSP_REVOKED_STATUS_NOSTATUS
) {
2616 BIO_printf(bio_err
, "invalid reason code %s\n",
2620 if (reason_code
== 7)
2621 reason_code
= OCSP_REVOKED_STATUS_REMOVEFROMCRL
;
2622 else if (reason_code
== 8) { /* Hold instruction */
2625 "missing hold instruction\n");
2628 reason_code
= OCSP_REVOKED_STATUS_CERTIFICATEHOLD
;
2629 hold
= OBJ_txt2obj(arg_str
, 0);
2633 "invalid object identifier %s\n", arg_str
);
2638 } else if ((reason_code
== 9) || (reason_code
== 10)) {
2641 "missing compromised time\n");
2644 comp_time
= ASN1_GENERALIZEDTIME_new();
2645 if (!ASN1_GENERALIZEDTIME_set_string(comp_time
,
2648 "invalid compromised time %s\n", arg_str
);
2651 if (reason_code
== 9)
2652 reason_code
= OCSP_REVOKED_STATUS_KEYCOMPROMISE
;
2654 reason_code
= OCSP_REVOKED_STATUS_CACOMPROMISE
;
2658 *preason
= reason_code
;
2660 *pinvtm
= comp_time
;
2662 ASN1_GENERALIZEDTIME_free(comp_time
);
2670 ASN1_OBJECT_free(hold
);
2672 ASN1_GENERALIZEDTIME_free(comp_time
);
2678 bin2hex(unsigned char * data
, size_t len
)
2681 char hex
[] = "0123456789ABCDEF";
2684 if ((ret
= malloc(len
* 2 + 1))) {
2685 for (i
= 0; i
< len
; i
++) {
2686 ret
[i
* 2 + 0] = hex
[data
[i
] >> 4];
2687 ret
[i
* 2 + 1] = hex
[data
[i
] & 0x0F];
2689 ret
[len
* 2] = '\0';