1 /* $OpenBSD: ca.c,v 1.19 2015/10/17 15:00:11 doug 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
;
286 const char *errstr
= NULL
;
289 if (single_execution
) {
290 if (pledge("stdio rpath wpath cpath tty", NULL
) == -1) {
306 if (strcmp(*argv
, "-verbose") == 0)
308 else if (strcmp(*argv
, "-config") == 0) {
311 configfile
= *(++argv
);
312 } else if (strcmp(*argv
, "-name") == 0) {
316 } else if (strcmp(*argv
, "-subj") == 0) {
321 } else if (strcmp(*argv
, "-utf8") == 0)
322 chtype
= MBSTRING_UTF8
;
323 else if (strcmp(*argv
, "-create_serial") == 0)
325 else if (strcmp(*argv
, "-multivalue-rdn") == 0)
327 else if (strcmp(*argv
, "-startdate") == 0) {
330 startdate
= *(++argv
);
331 } else if (strcmp(*argv
, "-enddate") == 0) {
335 } else if (strcmp(*argv
, "-days") == 0) {
338 days
= strtonum(*(++argv
), 0, LONG_MAX
, &errstr
);
341 } else if (strcmp(*argv
, "-md") == 0) {
345 } else if (strcmp(*argv
, "-policy") == 0) {
349 } else if (strcmp(*argv
, "-keyfile") == 0) {
353 } else if (strcmp(*argv
, "-keyform") == 0) {
356 keyform
= str2fmt(*(++argv
));
357 } else if (strcmp(*argv
, "-passin") == 0) {
360 passargin
= *(++argv
);
361 } else if (strcmp(*argv
, "-key") == 0) {
365 } else if (strcmp(*argv
, "-cert") == 0) {
368 certfile
= *(++argv
);
369 } else if (strcmp(*argv
, "-selfsign") == 0)
371 else if (strcmp(*argv
, "-in") == 0) {
376 } else if (strcmp(*argv
, "-out") == 0) {
380 } else if (strcmp(*argv
, "-outdir") == 0) {
384 } else if (strcmp(*argv
, "-sigopt") == 0) {
388 sigopts
= sk_OPENSSL_STRING_new_null();
390 !sk_OPENSSL_STRING_push(sigopts
, *(++argv
)))
392 } else if (strcmp(*argv
, "-notext") == 0)
394 else if (strcmp(*argv
, "-batch") == 0)
396 else if (strcmp(*argv
, "-preserveDN") == 0)
398 else if (strcmp(*argv
, "-noemailDN") == 0)
400 else if (strcmp(*argv
, "-gencrl") == 0)
402 else if (strcmp(*argv
, "-msie_hack") == 0)
404 else if (strcmp(*argv
, "-crldays") == 0) {
407 crldays
= strtonum(*(++argv
), 0, LONG_MAX
, &errstr
);
410 } else if (strcmp(*argv
, "-crlhours") == 0) {
413 crlhours
= strtonum(*(++argv
), 0, LONG_MAX
, &errstr
);
416 } else if (strcmp(*argv
, "-crlsec") == 0) {
419 crlsec
= strtonum(*(++argv
), 0, LONG_MAX
, &errstr
);
422 } else if (strcmp(*argv
, "-infiles") == 0) {
427 } else if (strcmp(*argv
, "-ss_cert") == 0) {
430 ss_cert_file
= *(++argv
);
432 } else if (strcmp(*argv
, "-spkac") == 0) {
435 spkac_file
= *(++argv
);
437 } else if (strcmp(*argv
, "-revoke") == 0) {
442 } else if (strcmp(*argv
, "-extensions") == 0) {
445 extensions
= *(++argv
);
446 } else if (strcmp(*argv
, "-extfile") == 0) {
450 } else if (strcmp(*argv
, "-status") == 0) {
453 ser_status
= *(++argv
);
454 } else if (strcmp(*argv
, "-updatedb") == 0) {
456 } else if (strcmp(*argv
, "-crlexts") == 0) {
460 } else if (strcmp(*argv
, "-crl_reason") == 0) {
464 rev_type
= REV_CRL_REASON
;
465 } else if (strcmp(*argv
, "-crl_hold") == 0) {
470 } else if (strcmp(*argv
, "-crl_compromise") == 0) {
474 rev_type
= REV_KEY_COMPROMISE
;
475 } else if (strcmp(*argv
, "-crl_CA_compromise") == 0) {
479 rev_type
= REV_CA_COMPROMISE
;
484 BIO_printf(bio_err
, "invalid argument %s: %s\n",
487 BIO_printf(bio_err
, "unknown option %s\n", *argv
);
498 for (pp2
= ca_usage
; (*pp2
!= NULL
); pp2
++)
499 BIO_printf(bio_err
, "%s", *pp2
);
503 /*****************************************************************/
505 if (configfile
== NULL
)
506 configfile
= getenv("OPENSSL_CONF");
507 if (configfile
== NULL
) {
508 if ((tofree
= make_config_name()) == NULL
) {
509 BIO_printf(bio_err
, "error making config file name\n");
514 BIO_printf(bio_err
, "Using configuration from %s\n", configfile
);
515 conf
= NCONF_new(NULL
);
516 if (NCONF_load(conf
, configfile
, &errorline
) <= 0) {
519 "error loading the config file '%s'\n",
523 "error on line %ld of config file '%s'\n",
524 errorline
, configfile
);
530 /* Lets get the config section we are using */
531 if (section
== NULL
) {
532 section
= NCONF_get_string(conf
, BASE_SECTION
, ENV_DEFAULT_CA
);
533 if (section
== NULL
) {
534 lookup_fail(BASE_SECTION
, ENV_DEFAULT_CA
);
539 p
= NCONF_get_string(conf
, NULL
, "oid_file");
545 oid_bio
= BIO_new_file(p
, "r");
546 if (oid_bio
== NULL
) {
549 "problems opening %s for extra oid's\n", p);
550 ERR_print_errors(bio_err);
554 OBJ_create_objects(oid_bio
);
558 if (!add_oid_section(bio_err
, conf
)) {
559 ERR_print_errors(bio_err
);
563 f
= NCONF_get_string(conf
, section
, STRING_MASK
);
567 if (f
&& !ASN1_STRING_set_default_mask_asc(f
)) {
569 "Invalid global string mask setting %s\n", f
);
572 if (chtype
!= MBSTRING_UTF8
) {
573 f
= NCONF_get_string(conf
, section
, UTF8_IN
);
576 else if (!strcmp(f
, "yes"))
577 chtype
= MBSTRING_UTF8
;
579 db_attr
.unique_subject
= 1;
580 p
= NCONF_get_string(conf
, section
, ENV_UNIQUE_SUBJECT
);
582 db_attr
.unique_subject
= parse_yesno(p
, 1);
586 in
= BIO_new(BIO_s_file());
587 out
= BIO_new(BIO_s_file());
588 Sout
= BIO_new(BIO_s_file());
589 Cout
= BIO_new(BIO_s_file());
590 if ((in
== NULL
) || (out
== NULL
) || (Sout
== NULL
) || (Cout
== NULL
)) {
591 ERR_print_errors(bio_err
);
594 /*****************************************************************/
595 /* report status of cert with serial number given on command line */
597 if ((dbfile
= NCONF_get_string(conf
, section
,
598 ENV_DATABASE
)) == NULL
) {
599 lookup_fail(section
, ENV_DATABASE
);
602 db
= load_index(dbfile
, &db_attr
);
606 if (!index_index(db
))
609 if (get_certificate_status(ser_status
, db
) != 1)
610 BIO_printf(bio_err
, "Error verifying serial %s!\n",
614 /*****************************************************************/
615 /* we definitely need a private key, so let's get it */
617 if ((keyfile
== NULL
) && ((keyfile
= NCONF_get_string(conf
,
618 section
, ENV_PRIVATE_KEY
)) == NULL
)) {
619 lookup_fail(section
, ENV_PRIVATE_KEY
);
624 if (!app_passwd(bio_err
, passargin
, NULL
, &key
, NULL
)) {
625 BIO_printf(bio_err
, "Error getting password\n");
629 pkey
= load_key(bio_err
, keyfile
, keyform
, 0, key
, "CA private key");
631 explicit_bzero(key
, strlen(key
));
633 /* load_key() has already printed an appropriate message */
636 /*****************************************************************/
637 /* we need a certificate */
638 if (!selfsign
|| spkac_file
|| ss_cert_file
|| gencrl
) {
639 if ((certfile
== NULL
) &&
640 ((certfile
= NCONF_get_string(conf
,
641 section
, ENV_CERTIFICATE
)) == NULL
)) {
642 lookup_fail(section
, ENV_CERTIFICATE
);
645 x509
= load_cert(bio_err
, certfile
, FORMAT_PEM
, NULL
,
650 if (!X509_check_private_key(x509
, pkey
)) {
652 "CA certificate and CA private key do not match\n");
659 f
= NCONF_get_string(conf
, BASE_SECTION
, ENV_PRESERVE
);
662 if ((f
!= NULL
) && ((*f
== 'y') || (*f
== 'Y')))
664 f
= NCONF_get_string(conf
, BASE_SECTION
, ENV_MSIE_HACK
);
667 if ((f
!= NULL
) && ((*f
== 'y') || (*f
== 'Y')))
670 f
= NCONF_get_string(conf
, section
, ENV_NAMEOPT
);
673 if (!set_name_ex(&nameopt
, f
)) {
675 "Invalid name options: \"%s\"\n", f
);
682 f
= NCONF_get_string(conf
, section
, ENV_CERTOPT
);
685 if (!set_cert_ex(&certopt
, f
)) {
687 "Invalid certificate options: \"%s\"\n", f
);
694 f
= NCONF_get_string(conf
, section
, ENV_EXTCOPY
);
697 if (!set_ext_copy(&ext_copy
, f
)) {
699 "Invalid extension copy option: \"%s\"\n", f
);
705 /*****************************************************************/
706 /* lookup where to write new certificates */
707 if ((outdir
== NULL
) && (req
)) {
709 if ((outdir
= NCONF_get_string(conf
, section
,
710 ENV_NEW_CERTS_DIR
)) == NULL
) {
711 BIO_printf(bio_err
, "there needs to be defined a directory for new certificate to be placed in\n");
715 * outdir is a directory spec, but access() for VMS demands a
716 * filename. In any case, stat(), below, will catch the
717 * problem if outdir is not a directory spec, and the fopen()
718 * or open() will catch an error if there is no write access.
720 * Presumably, this problem could also be solved by using the
721 * DEC C routines to convert the directory syntax to Unixly,
722 * and give that to access(). However, time's too short to
725 if (access(outdir
, R_OK
| W_OK
| X_OK
) != 0) {
727 "I am unable to access the %s directory\n", outdir
);
731 if (app_isdir(outdir
) <= 0) {
733 "%s need to be a directory\n", outdir
);
738 /*****************************************************************/
739 /* we need to load the database file */
740 if ((dbfile
= NCONF_get_string(conf
, section
, ENV_DATABASE
)) == NULL
) {
741 lookup_fail(section
, ENV_DATABASE
);
744 db
= load_index(dbfile
, &db_attr
);
748 /* Lets check some fields */
749 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(db
->db
->data
); i
++) {
750 pp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, i
);
751 if ((pp
[DB_type
][0] != DB_TYPE_REV
) &&
752 (pp
[DB_rev_date
][0] != '\0')) {
753 BIO_printf(bio_err
, "entry %d: not revoked yet, but has a revocation date\n", i
+ 1);
756 if ((pp
[DB_type
][0] == DB_TYPE_REV
) &&
757 !make_revoked(NULL
, pp
[DB_rev_date
])) {
758 BIO_printf(bio_err
, " in entry %d\n", i
+ 1);
761 if (!check_time_format((char *) pp
[DB_exp_date
])) {
762 BIO_printf(bio_err
, "entry %d: invalid expiry date\n",
772 if ((j
& 1) || (j
< 2)) {
774 "entry %d: bad serial number length (%d)\n",
779 if (!(((*p
>= '0') && (*p
<= '9')) ||
780 ((*p
>= 'A') && (*p
<= 'F')) ||
781 ((*p
>= 'a') && (*p
<= 'f')))) {
782 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
);
789 BIO_set_fp(out
, stdout
, BIO_NOCLOSE
| BIO_FP_TEXT
); /* cannot fail */
790 TXT_DB_write(out
, db
->db
);
791 BIO_printf(bio_err
, "%d entries loaded from the database\n",
792 sk_OPENSSL_PSTRING_num(db
->db
->data
));
793 BIO_printf(bio_err
, "generating index\n");
795 if (!index_index(db
))
798 /*****************************************************************/
799 /* Update the db file for expired certificates */
802 BIO_printf(bio_err
, "Updating %s ...\n", dbfile
);
806 BIO_printf(bio_err
, "Malloc failure\n");
811 "No entries found to mark expired\n");
813 if (!save_index(dbfile
, "new", db
))
816 if (!rotate_index(dbfile
, "new", "old"))
821 "Done. %d entries marked as expired\n", i
);
824 /*****************************************************************/
825 /* Read extentions config file */
827 extconf
= NCONF_new(NULL
);
828 if (NCONF_load(extconf
, extfile
, &errorline
) <= 0) {
831 "ERROR: loading the config file '%s'\n",
835 "ERROR: on line %ld of config file '%s'\n",
842 "Successfully loaded extensions file %s\n",
845 /* We can have sections in the ext file */
846 if (!extensions
&& !(extensions
= NCONF_get_string(extconf
,
847 "default", "extensions")))
848 extensions
= "default";
850 /*****************************************************************/
852 if (outfile
!= NULL
) {
853 if (BIO_write_filename(Sout
, outfile
) <= 0) {
858 BIO_set_fp(Sout
, stdout
, BIO_NOCLOSE
| BIO_FP_TEXT
);
861 if ((md
== NULL
) && ((md
= NCONF_get_string(conf
, section
,
862 ENV_DEFAULT_MD
)) == NULL
)) {
863 lookup_fail(section
, ENV_DEFAULT_MD
);
866 if (!strcmp(md
, "default")) {
868 if (EVP_PKEY_get_default_digest_nid(pkey
, &def_nid
) <= 0) {
869 BIO_puts(bio_err
, "no default digest\n");
872 md
= (char *) OBJ_nid2sn(def_nid
);
874 if ((dgst
= EVP_get_digestbyname(md
)) == NULL
) {
876 "%s is an unsupported message digest type\n", md
);
880 if ((email_dn
== 1) && ((tmp_email_dn
= NCONF_get_string(conf
,
881 section
, ENV_DEFAULT_EMAIL_DN
)) != NULL
)) {
882 if (strcmp(tmp_email_dn
, "no") == 0)
886 BIO_printf(bio_err
, "message digest is %s\n",
887 OBJ_nid2ln(dgst
->type
));
888 if ((policy
== NULL
) && ((policy
= NCONF_get_string(conf
,
889 section
, ENV_POLICY
)) == NULL
)) {
890 lookup_fail(section
, ENV_POLICY
);
894 BIO_printf(bio_err
, "policy is %s\n", policy
);
896 if ((serialfile
= NCONF_get_string(conf
, section
,
897 ENV_SERIAL
)) == NULL
) {
898 lookup_fail(section
, ENV_SERIAL
);
903 * no '-extfile' option, so we look for extensions in
904 * the main configuration file
907 extensions
= NCONF_get_string(conf
, section
,
913 /* Check syntax of file */
915 X509V3_set_ctx_test(&ctx
);
916 X509V3_set_nconf(&ctx
, conf
);
917 if (!X509V3_EXT_add_nconf(conf
, &ctx
,
920 "Error Loading extension section %s\n",
927 if (startdate
== NULL
) {
928 startdate
= NCONF_get_string(conf
, section
,
929 ENV_DEFAULT_STARTDATE
);
930 if (startdate
== NULL
)
933 if (startdate
&& !ASN1_TIME_set_string(NULL
, startdate
)) {
934 BIO_printf(bio_err
, "start date is invalid, it should be YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ\n");
937 if (startdate
== NULL
)
940 if (enddate
== NULL
) {
941 enddate
= NCONF_get_string(conf
, section
,
942 ENV_DEFAULT_ENDDATE
);
946 if (enddate
&& !ASN1_TIME_set_string(NULL
, enddate
)) {
947 BIO_printf(bio_err
, "end date is invalid, it should be YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ\n");
951 if (!NCONF_get_number(conf
, section
,
952 ENV_DEFAULT_DAYS
, &days
))
955 if (!enddate
&& (days
== 0)) {
957 "cannot lookup how many days to certify for\n");
960 if ((serial
= load_serial(serialfile
, create_ser
, NULL
)) ==
963 "error while loading serial number\n");
967 if (BN_is_zero(serial
))
969 "next serial number is 00\n");
971 if ((f
= BN_bn2hex(serial
)) == NULL
)
974 "next serial number is %s\n", f
);
978 if ((attribs
= NCONF_get_section(conf
, policy
)) == NULL
) {
980 "unable to find 'section' for %s\n", policy
);
983 if ((cert_sk
= sk_X509_new_null()) == NULL
) {
984 BIO_printf(bio_err
, "Memory allocation failure\n");
987 if (spkac_file
!= NULL
) {
989 j
= certify_spkac(&x
, spkac_file
, pkey
, x509
, dgst
,
990 sigopts
, attribs
, db
, serial
, subj
, chtype
,
991 multirdn
, email_dn
, startdate
, enddate
, days
,
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");
1012 if (ss_cert_file
!= NULL
) {
1014 j
= certify_cert(&x
, ss_cert_file
, pkey
, x509
, dgst
,
1015 sigopts
, attribs
, db
, serial
, subj
, chtype
,
1016 multirdn
, email_dn
, startdate
, enddate
, days
, batch
,
1017 extensions
, conf
, verbose
, certopt
, nameopt
,
1018 default_op
, ext_copy
);
1023 BIO_printf(bio_err
, "\n");
1024 if (!BN_add_word(serial
, 1))
1026 if (!sk_X509_push(cert_sk
, x
)) {
1028 "Memory allocation failure\n");
1033 if (infile
!= NULL
) {
1035 j
= certify(&x
, infile
, pkey
, x509p
, dgst
, sigopts
,
1036 attribs
, db
, serial
, subj
, chtype
, multirdn
,
1037 email_dn
, startdate
, enddate
, days
, batch
,
1038 extensions
, conf
, verbose
, certopt
, nameopt
,
1039 default_op
, ext_copy
, selfsign
);
1044 BIO_printf(bio_err
, "\n");
1045 if (!BN_add_word(serial
, 1))
1047 if (!sk_X509_push(cert_sk
, x
)) {
1049 "Memory allocation failure\n");
1054 for (i
= 0; i
< argc
; i
++) {
1056 j
= certify(&x
, argv
[i
], pkey
, x509p
, dgst
, sigopts
,
1057 attribs
, db
, serial
, subj
, chtype
, multirdn
,
1058 email_dn
, startdate
, enddate
, days
, batch
,
1059 extensions
, conf
, verbose
, certopt
, nameopt
,
1060 default_op
, ext_copy
, selfsign
);
1065 BIO_printf(bio_err
, "\n");
1066 if (!BN_add_word(serial
, 1))
1068 if (!sk_X509_push(cert_sk
, x
)) {
1070 "Memory allocation failure\n");
1076 * we have a stack of newly certified certificates and a data
1077 * base and serial number that need updating
1080 if (sk_X509_num(cert_sk
) > 0) {
1082 BIO_printf(bio_err
, "\n%d out of %d certificate requests certified, commit? [y/n]", total_done
, total
);
1083 (void) BIO_flush(bio_err
);
1085 if (!fgets(buf
[0], 10, stdin
)) {
1086 BIO_printf(bio_err
, "CERTIFICATION CANCELED: I/O error\n");
1090 if ((buf
[0][0] != 'y') && (buf
[0][0] != 'Y')) {
1091 BIO_printf(bio_err
, "CERTIFICATION CANCELED\n");
1096 BIO_printf(bio_err
, "Write out database with %d new entries\n", sk_X509_num(cert_sk
));
1098 if (!save_serial(serialfile
, "new", serial
, NULL
))
1101 if (!save_index(dbfile
, "new", db
))
1105 BIO_printf(bio_err
, "writing new certificates\n");
1106 for (i
= 0; i
< sk_X509_num(cert_sk
); i
++) {
1109 unsigned char *data
;
1111 x
= sk_X509_value(cert_sk
, i
);
1113 j
= x
->cert_info
->serialNumber
->length
;
1114 data
= (unsigned char *)x
->cert_info
->serialNumber
->data
;
1116 serialstr
= bin2hex(data
, j
);
1118 serialstr
= strdup("00");
1120 k
= snprintf(buf
[2], sizeof(buf
[2]),
1121 "%s/%s.pem", outdir
, serialstr
);
1123 if (k
== -1 || k
>= sizeof(buf
[2])) {
1125 "certificate file name too long\n");
1130 "memory allocation failed\n");
1134 BIO_printf(bio_err
, "writing %s\n", buf
[2]);
1136 if (BIO_write_filename(Cout
, buf
[2]) <= 0) {
1140 write_new_certificate(Cout
, x
, 0, notext
);
1141 write_new_certificate(Sout
, x
, output_der
, notext
);
1144 if (sk_X509_num(cert_sk
)) {
1145 /* Rename the database and the serial file */
1146 if (!rotate_serial(serialfile
, "new", "old"))
1149 if (!rotate_index(dbfile
, "new", "old"))
1152 BIO_printf(bio_err
, "Data Base Updated\n");
1155 /*****************************************************************/
1159 crl_ext
= NCONF_get_string(conf
, section
, ENV_CRLEXT
);
1164 /* Check syntax of file */
1166 X509V3_set_ctx_test(&ctx
);
1167 X509V3_set_nconf(&ctx
, conf
);
1168 if (!X509V3_EXT_add_nconf(conf
, &ctx
, crl_ext
, NULL
)) {
1170 "Error Loading CRL extension section %s\n",
1176 if ((crlnumberfile
= NCONF_get_string(conf
, section
,
1177 ENV_CRLNUMBER
)) != NULL
)
1178 if ((crlnumber
= load_serial(crlnumberfile
, 0,
1181 "error while loading CRL number\n");
1184 if (!crldays
&& !crlhours
&& !crlsec
) {
1185 if (!NCONF_get_number(conf
, section
,
1186 ENV_DEFAULT_CRL_DAYS
, &crldays
))
1188 if (!NCONF_get_number(conf
, section
,
1189 ENV_DEFAULT_CRL_HOURS
, &crlhours
))
1193 if ((crldays
== 0) && (crlhours
== 0) && (crlsec
== 0)) {
1194 BIO_printf(bio_err
, "cannot lookup how long until the next CRL is issued\n");
1198 BIO_printf(bio_err
, "making CRL\n");
1199 if ((crl
= X509_CRL_new()) == NULL
)
1201 if (!X509_CRL_set_issuer_name(crl
, X509_get_subject_name(x509
)))
1204 tmptm
= ASN1_TIME_new();
1207 X509_gmtime_adj(tmptm
, 0);
1208 X509_CRL_set_lastUpdate(crl
, tmptm
);
1209 if (!X509_time_adj_ex(tmptm
, crldays
,
1210 crlhours
* 60 * 60 + crlsec
, NULL
)) {
1211 BIO_puts(bio_err
, "error setting CRL nextUpdate\n");
1214 X509_CRL_set_nextUpdate(crl
, tmptm
);
1216 ASN1_TIME_free(tmptm
);
1218 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(db
->db
->data
); i
++) {
1219 pp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, i
);
1220 if (pp
[DB_type
][0] == DB_TYPE_REV
) {
1221 if ((r
= X509_REVOKED_new()) == NULL
)
1223 j
= make_revoked(r
, pp
[DB_rev_date
]);
1228 if (!BN_hex2bn(&serial
, pp
[DB_serial
]))
1230 tmpser
= BN_to_ASN1_INTEGER(serial
, NULL
);
1235 X509_REVOKED_set_serialNumber(r
, tmpser
);
1236 ASN1_INTEGER_free(tmpser
);
1237 X509_CRL_add0_revoked(crl
, r
);
1242 * sort the data so it will be written in serial number order
1246 /* we now have a CRL */
1248 BIO_printf(bio_err
, "signing CRL\n");
1250 /* Add any extensions asked for */
1252 if (crl_ext
|| crlnumberfile
!= NULL
) {
1254 X509V3_set_ctx(&crlctx
, x509
, NULL
, NULL
, crl
, 0);
1255 X509V3_set_nconf(&crlctx
, conf
);
1258 if (!X509V3_EXT_CRL_add_nconf(conf
, &crlctx
,
1261 if (crlnumberfile
!= NULL
) {
1262 tmpser
= BN_to_ASN1_INTEGER(crlnumber
, NULL
);
1265 X509_CRL_add1_ext_i2d(crl
, NID_crl_number
,
1267 ASN1_INTEGER_free(tmpser
);
1269 if (!BN_add_word(crlnumber
, 1))
1273 if (crl_ext
|| crl_v2
) {
1274 if (!X509_CRL_set_version(crl
, 1))
1275 goto err
; /* version 2 CRL */
1277 if (crlnumberfile
!= NULL
) /* we have a CRL number that
1279 if (!save_serial(crlnumberfile
, "new", crlnumber
, NULL
))
1286 if (!do_X509_CRL_sign(bio_err
, crl
, pkey
, dgst
, sigopts
))
1289 PEM_write_bio_X509_CRL(Sout
, crl
);
1291 if (crlnumberfile
!= NULL
) /* Rename the crlnumber file */
1292 if (!rotate_serial(crlnumberfile
, "new", "old"))
1296 /*****************************************************************/
1298 if (infile
== NULL
) {
1299 BIO_printf(bio_err
, "no input files\n");
1303 revcert
= load_cert(bio_err
, infile
, FORMAT_PEM
,
1305 if (revcert
== NULL
)
1307 j
= do_revoke(revcert
, db
, rev_type
, rev_arg
);
1312 if (!save_index(dbfile
, "new", db
))
1315 if (!rotate_index(dbfile
, "new", "old"))
1318 BIO_printf(bio_err
, "Data Base Updated\n");
1321 /*****************************************************************/
1333 sk_X509_pop_free(cert_sk
, X509_free
);
1336 ERR_print_errors(bio_err
);
1337 if (free_key
&& key
)
1343 sk_OPENSSL_STRING_free(sigopts
);
1344 EVP_PKEY_free(pkey
);
1349 NCONF_free(extconf
);
1356 lookup_fail(const char *name
, const char *tag
)
1358 BIO_printf(bio_err
, "variable lookup failed for %s::%s\n", name
, tag
);
1362 certify(X509
** xret
, char *infile
, EVP_PKEY
* pkey
, X509
* x509
,
1363 const EVP_MD
* dgst
, STACK_OF(OPENSSL_STRING
) * sigopts
,
1364 STACK_OF(CONF_VALUE
) * policy
, CA_DB
* db
, BIGNUM
* serial
, char *subj
,
1365 unsigned long chtype
, int multirdn
, int email_dn
, char *startdate
,
1366 char *enddate
, long days
, int batch
, char *ext_sect
, CONF
* lconf
,
1367 int verbose
, unsigned long certopt
, unsigned long nameopt
, int default_op
,
1368 int ext_copy
, int selfsign
)
1370 X509_REQ
*req
= NULL
;
1372 EVP_PKEY
*pktmp
= NULL
;
1375 in
= BIO_new(BIO_s_file());
1377 if (BIO_read_filename(in
, infile
) <= 0) {
1381 if ((req
= PEM_read_bio_X509_REQ(in
, NULL
, NULL
, NULL
)) == NULL
) {
1382 BIO_printf(bio_err
, "Error reading certificate request in %s\n",
1387 X509_REQ_print(bio_err
, req
);
1389 BIO_printf(bio_err
, "Check that the request matches the signature\n");
1391 if (selfsign
&& !X509_REQ_check_private_key(req
, pkey
)) {
1393 "Certificate request and CA private key do not match\n");
1397 if ((pktmp
= X509_REQ_get_pubkey(req
)) == NULL
) {
1398 BIO_printf(bio_err
, "error unpacking public key\n");
1401 i
= X509_REQ_verify(req
, pktmp
);
1402 EVP_PKEY_free(pktmp
);
1405 BIO_printf(bio_err
, "Signature verification problems....\n");
1411 "Signature did not match the certificate request\n");
1414 BIO_printf(bio_err
, "Signature ok\n");
1416 ok
= do_body(xret
, pkey
, x509
, dgst
, sigopts
, policy
, db
, serial
,
1417 subj
, chtype
, multirdn
, email_dn
, startdate
, enddate
, days
, batch
,
1418 verbose
, req
, ext_sect
, lconf
, certopt
, nameopt
, default_op
,
1419 ext_copy
, selfsign
);
1430 certify_cert(X509
** xret
, char *infile
, EVP_PKEY
* pkey
, X509
* x509
,
1431 const EVP_MD
* dgst
, STACK_OF(OPENSSL_STRING
) * sigopts
,
1432 STACK_OF(CONF_VALUE
) * policy
, CA_DB
* db
, BIGNUM
* serial
, char *subj
,
1433 unsigned long chtype
, int multirdn
, int email_dn
, char *startdate
,
1434 char *enddate
, long days
, int batch
, char *ext_sect
, CONF
* lconf
,
1435 int verbose
, unsigned long certopt
, unsigned long nameopt
, int default_op
,
1439 X509_REQ
*rreq
= NULL
;
1440 EVP_PKEY
*pktmp
= NULL
;
1443 if ((req
= load_cert(bio_err
, infile
, FORMAT_PEM
, NULL
,
1447 X509_print(bio_err
, req
);
1449 BIO_printf(bio_err
, "Check that the request matches the signature\n");
1451 if ((pktmp
= X509_get_pubkey(req
)) == NULL
) {
1452 BIO_printf(bio_err
, "error unpacking public key\n");
1455 i
= X509_verify(req
, pktmp
);
1456 EVP_PKEY_free(pktmp
);
1459 BIO_printf(bio_err
, "Signature verification problems....\n");
1465 "Signature did not match the certificate\n");
1468 BIO_printf(bio_err
, "Signature ok\n");
1470 if ((rreq
= X509_to_X509_REQ(req
, NULL
, EVP_md5())) == NULL
)
1473 ok
= do_body(xret
, pkey
, x509
, dgst
, sigopts
, policy
, db
, serial
,
1474 subj
, chtype
, multirdn
, email_dn
, startdate
, enddate
, days
, batch
,
1475 verbose
, rreq
, ext_sect
, lconf
, certopt
, nameopt
, default_op
,
1480 X509_REQ_free(rreq
);
1487 do_body(X509
** xret
, EVP_PKEY
* pkey
, X509
* x509
, const EVP_MD
* dgst
,
1488 STACK_OF(OPENSSL_STRING
) * sigopts
, STACK_OF(CONF_VALUE
) * policy
,
1489 CA_DB
* db
, BIGNUM
* serial
, char *subj
, unsigned long chtype
, int multirdn
,
1490 int email_dn
, char *startdate
, char *enddate
, long days
, int batch
,
1491 int verbose
, X509_REQ
* req
, char *ext_sect
, CONF
* lconf
,
1492 unsigned long certopt
, unsigned long nameopt
, int default_op
,
1493 int ext_copy
, int selfsign
)
1495 X509_NAME
*name
= NULL
, *CAname
= NULL
, *subject
= NULL
, *dn_subject
= NULL
;
1496 ASN1_UTCTIME
*tm
, *tmptm
;
1497 ASN1_STRING
*str
, *str2
;
1501 X509_NAME_ENTRY
*ne
;
1502 X509_NAME_ENTRY
*tne
, *push
;
1504 int ok
= -1, i
, j
, last
, nid
;
1507 OPENSSL_STRING row
[DB_NUMBER
];
1508 OPENSSL_STRING
*irow
= NULL
;
1509 OPENSSL_STRING
*rrow
= NULL
;
1512 tmptm
= ASN1_UTCTIME_new();
1513 if (tmptm
== NULL
) {
1514 BIO_printf(bio_err
, "malloc error\n");
1517 for (i
= 0; i
< DB_NUMBER
; i
++)
1521 X509_NAME
*n
= parse_name(subj
, chtype
, multirdn
);
1524 ERR_print_errors(bio_err
);
1527 X509_REQ_set_subject_name(req
, n
);
1528 req
->req_info
->enc
.modified
= 1;
1533 "The Subject's Distinguished Name is as follows\n");
1535 name
= X509_REQ_get_subject_name(req
);
1536 for (i
= 0; i
< X509_NAME_entry_count(name
); i
++) {
1537 ne
= X509_NAME_get_entry(name
, i
);
1538 str
= X509_NAME_ENTRY_get_data(ne
);
1539 obj
= X509_NAME_ENTRY_get_object(ne
);
1542 /* assume all type should be strings */
1543 nid
= OBJ_obj2nid(ne
->object
);
1545 if (str
->type
== V_ASN1_UNIVERSALSTRING
)
1546 ASN1_UNIVERSALSTRING_to_string(str
);
1548 if ((str
->type
== V_ASN1_IA5STRING
) &&
1549 (nid
!= NID_pkcs9_emailAddress
))
1550 str
->type
= V_ASN1_T61STRING
;
1552 if ((nid
== NID_pkcs9_emailAddress
) &&
1553 (str
->type
== V_ASN1_PRINTABLESTRING
))
1554 str
->type
= V_ASN1_IA5STRING
;
1556 /* If no EMAIL is wanted in the subject */
1557 if ((OBJ_obj2nid(obj
) == NID_pkcs9_emailAddress
) && (!email_dn
))
1560 /* check some things */
1561 if ((OBJ_obj2nid(obj
) == NID_pkcs9_emailAddress
) &&
1562 (str
->type
!= V_ASN1_IA5STRING
)) {
1563 BIO_printf(bio_err
, "\nemailAddress type needs to be of type IA5STRING\n");
1566 if ((str
->type
!= V_ASN1_BMPSTRING
) &&
1567 (str
->type
!= V_ASN1_UTF8STRING
)) {
1568 j
= ASN1_PRINTABLE_type(str
->data
, str
->length
);
1569 if (((j
== V_ASN1_T61STRING
) &&
1570 (str
->type
!= V_ASN1_T61STRING
)) ||
1571 ((j
== V_ASN1_IA5STRING
) &&
1572 (str
->type
== V_ASN1_PRINTABLESTRING
))) {
1573 BIO_printf(bio_err
, "\nThe string contains characters that are illegal for the ASN.1 type\n");
1578 old_entry_print(bio_err
, obj
, str
);
1581 /* Ok, now we check the 'policy' stuff. */
1582 if ((subject
= X509_NAME_new()) == NULL
) {
1583 BIO_printf(bio_err
, "Memory allocation failure\n");
1586 /* take a copy of the issuer name before we mess with it. */
1588 CAname
= X509_NAME_dup(name
);
1590 CAname
= X509_NAME_dup(x509
->cert_info
->subject
);
1595 for (i
= 0; i
< sk_CONF_VALUE_num(policy
); i
++) {
1596 cv
= sk_CONF_VALUE_value(policy
, i
); /* get the object id */
1597 if ((j
= OBJ_txt2nid(cv
->name
)) == NID_undef
) {
1598 BIO_printf(bio_err
, "%s:unknown object type in 'policy' configuration\n", cv
->name
);
1601 obj
= OBJ_nid2obj(j
);
1605 /* lookup the object in the supplied name list */
1606 j
= X509_NAME_get_index_by_OBJ(name
, obj
, last
);
1612 tne
= X509_NAME_get_entry(name
, j
);
1616 /* depending on the 'policy', decide what to do. */
1618 if (strcmp(cv
->value
, "optional") == 0) {
1621 } else if (strcmp(cv
->value
, "supplied") == 0) {
1623 BIO_printf(bio_err
, "The %s field needed to be supplied and was missing\n", cv
->name
);
1627 } else if (strcmp(cv
->value
, "match") == 0) {
1631 BIO_printf(bio_err
, "The mandatory %s field was missing\n", cv
->name
);
1637 j
= X509_NAME_get_index_by_OBJ(CAname
, obj
, last2
);
1638 if ((j
< 0) && (last2
== -1)) {
1639 BIO_printf(bio_err
, "The %s field does not exist in the CA certificate,\nthe 'policy' is misconfigured\n", cv
->name
);
1643 push
= X509_NAME_get_entry(CAname
, j
);
1644 str
= X509_NAME_ENTRY_get_data(tne
);
1645 str2
= X509_NAME_ENTRY_get_data(push
);
1647 if (ASN1_STRING_cmp(str
, str2
) != 0)
1651 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
));
1655 BIO_printf(bio_err
, "%s:invalid type in 'policy' configuration\n", cv
->value
);
1660 if (!X509_NAME_add_entry(subject
, push
,
1663 X509_NAME_ENTRY_free(push
);
1665 "Memory allocation failure\n");
1675 X509_NAME_free(subject
);
1676 /* subject=X509_NAME_dup(X509_REQ_get_subject_name(req)); */
1677 subject
= X509_NAME_dup(name
);
1678 if (subject
== NULL
)
1682 BIO_printf(bio_err
, "The subject name appears to be ok, checking data base for clashes\n");
1684 /* Build the correct Subject if no email is wanted in the subject */
1686 * and add it later on because of the method extensions are added
1691 dn_subject
= subject
;
1693 X509_NAME_ENTRY
*tmpne
;
1695 * Its best to dup the subject DN and then delete any email
1696 * addresses because this retains its structure.
1698 if (!(dn_subject
= X509_NAME_dup(subject
))) {
1699 BIO_printf(bio_err
, "Memory allocation failure\n");
1702 while ((i
= X509_NAME_get_index_by_NID(dn_subject
,
1703 NID_pkcs9_emailAddress
, -1)) >= 0) {
1704 tmpne
= X509_NAME_get_entry(dn_subject
, i
);
1705 X509_NAME_delete_entry(dn_subject
, i
);
1706 X509_NAME_ENTRY_free(tmpne
);
1710 if (BN_is_zero(serial
))
1711 row
[DB_serial
] = strdup("00");
1713 row
[DB_serial
] = BN_bn2hex(serial
);
1714 if (row
[DB_serial
] == NULL
) {
1715 BIO_printf(bio_err
, "Memory allocation failure\n");
1718 if (db
->attributes
.unique_subject
) {
1719 OPENSSL_STRING
*crow
= row
;
1721 rrow
= TXT_DB_get_by_index(db
->db
, DB_name
, crow
);
1724 "ERROR:There is already a certificate for %s\n",
1729 rrow
= TXT_DB_get_by_index(db
->db
, DB_serial
, row
);
1732 "ERROR:Serial number %s has already been issued,\n",
1734 BIO_printf(bio_err
, " check the database/serial_file for corruption\n");
1739 "The matching entry has the following details\n");
1740 if (rrow
[DB_type
][0] == 'E')
1742 else if (rrow
[DB_type
][0] == 'R')
1744 else if (rrow
[DB_type
][0] == 'V')
1747 p
= "\ninvalid type, Data base error\n";
1748 BIO_printf(bio_err
, "Type :%s\n", p
);
1749 if (rrow
[DB_type
][0] == 'R') {
1750 p
= rrow
[DB_exp_date
];
1753 BIO_printf(bio_err
, "Was revoked on:%s\n", p
);
1755 p
= rrow
[DB_exp_date
];
1758 BIO_printf(bio_err
, "Expires on :%s\n", p
);
1759 p
= rrow
[DB_serial
];
1762 BIO_printf(bio_err
, "Serial Number :%s\n", p
);
1766 BIO_printf(bio_err
, "File name :%s\n", p
);
1770 BIO_printf(bio_err
, "Subject Name :%s\n", p
);
1771 ok
= -1; /* This is now a 'bad' error. */
1774 /* We are now totally happy, lets make and sign the certificate */
1776 BIO_printf(bio_err
, "Everything appears to be ok, creating and signing the certificate\n");
1778 if ((ret
= X509_new()) == NULL
)
1780 ci
= ret
->cert_info
;
1783 /* Make it an X509 v3 certificate. */
1784 if (!X509_set_version(ret
, 2))
1787 if (ci
->serialNumber
== NULL
)
1789 if (BN_to_ASN1_INTEGER(serial
, ci
->serialNumber
) == NULL
)
1792 if (!X509_set_issuer_name(ret
, subject
))
1795 if (!X509_set_issuer_name(ret
, X509_get_subject_name(x509
)))
1799 if (strcmp(startdate
, "today") == 0)
1800 X509_gmtime_adj(X509_get_notBefore(ret
), 0);
1802 ASN1_TIME_set_string(X509_get_notBefore(ret
), startdate
);
1804 if (enddate
== NULL
)
1805 X509_time_adj_ex(X509_get_notAfter(ret
), days
, 0, NULL
);
1807 ASN1_TIME_set_string(X509_get_notAfter(ret
), enddate
);
1809 if (!X509_set_subject_name(ret
, subject
))
1812 pktmp
= X509_REQ_get_pubkey(req
);
1813 i
= X509_set_pubkey(ret
, pktmp
);
1814 EVP_PKEY_free(pktmp
);
1818 /* Lets add the extensions, if there are any */
1821 if (ci
->version
== NULL
)
1822 if ((ci
->version
= ASN1_INTEGER_new()) == NULL
)
1824 ASN1_INTEGER_set(ci
->version
, 2); /* version 3 certificate */
1827 * Free the current entries if any, there should not be any I
1830 if (ci
->extensions
!= NULL
)
1831 sk_X509_EXTENSION_pop_free(ci
->extensions
,
1832 X509_EXTENSION_free
);
1834 ci
->extensions
= NULL
;
1836 /* Initialize the context structure */
1838 X509V3_set_ctx(&ctx
, ret
, ret
, req
, NULL
, 0);
1840 X509V3_set_ctx(&ctx
, x509
, ret
, req
, NULL
, 0);
1845 "Extra configuration file found\n");
1847 /* Use the extconf configuration db LHASH */
1848 X509V3_set_nconf(&ctx
, extconf
);
1850 /* Test the structure (needed?) */
1851 /* X509V3_set_ctx_test(&ctx); */
1853 /* Adds exts contained in the configuration file */
1854 if (!X509V3_EXT_add_nconf(extconf
, &ctx
,
1857 "ERROR: adding extensions in section %s\n",
1859 ERR_print_errors(bio_err
);
1863 BIO_printf(bio_err
, "Successfully added extensions from file.\n");
1864 } else if (ext_sect
) {
1865 /* We found extensions to be set from config file */
1866 X509V3_set_nconf(&ctx
, lconf
);
1868 if (!X509V3_EXT_add_nconf(lconf
, &ctx
, ext_sect
, ret
)) {
1870 "ERROR: adding extensions in section %s\n",
1872 ERR_print_errors(bio_err
);
1876 BIO_printf(bio_err
, "Successfully added extensions from config\n");
1879 /* Copy extensions from request (if any) */
1881 if (!copy_extensions(ret
, req
, ext_copy
)) {
1882 BIO_printf(bio_err
, "ERROR: adding extensions from request\n");
1883 ERR_print_errors(bio_err
);
1886 /* Set the right value for the noemailDN option */
1887 if (email_dn
== 0) {
1888 if (!X509_set_subject_name(ret
, dn_subject
))
1892 BIO_printf(bio_err
, "Certificate Details:\n");
1894 * Never print signature details because signature not
1897 certopt
|= X509_FLAG_NO_SIGDUMP
| X509_FLAG_NO_SIGNAME
;
1898 X509_print_ex(bio_err
, ret
, nameopt
, certopt
);
1900 BIO_printf(bio_err
, "Certificate is to be certified until ");
1901 ASN1_TIME_print(bio_err
, X509_get_notAfter(ret
));
1903 BIO_printf(bio_err
, " (%ld days)", days
);
1904 BIO_printf(bio_err
, "\n");
1908 BIO_printf(bio_err
, "Sign the certificate? [y/n]:");
1909 (void) BIO_flush(bio_err
);
1911 if (!fgets(buf
, sizeof(buf
) - 1, stdin
)) {
1913 "CERTIFICATE WILL NOT BE CERTIFIED: I/O error\n");
1917 if (!((buf
[0] == 'y') || (buf
[0] == 'Y'))) {
1919 "CERTIFICATE WILL NOT BE CERTIFIED\n");
1924 pktmp
= X509_get_pubkey(ret
);
1925 if (EVP_PKEY_missing_parameters(pktmp
) &&
1926 !EVP_PKEY_missing_parameters(pkey
))
1927 EVP_PKEY_copy_parameters(pktmp
, pkey
);
1928 EVP_PKEY_free(pktmp
);
1930 if (!do_X509_sign(bio_err
, ret
, pkey
, dgst
, sigopts
))
1933 /* We now just add it to the database */
1934 row
[DB_type
] = malloc(2);
1936 tm
= X509_get_notAfter(ret
);
1937 row
[DB_exp_date
] = malloc(tm
->length
+ 1);
1938 if (row
[DB_type
] == NULL
|| row
[DB_exp_date
] == NULL
) {
1939 BIO_printf(bio_err
, "Memory allocation failure\n");
1943 memcpy(row
[DB_exp_date
], tm
->data
, tm
->length
);
1944 row
[DB_exp_date
][tm
->length
] = '\0';
1946 row
[DB_rev_date
] = NULL
;
1948 /* row[DB_serial] done already */
1949 row
[DB_file
] = malloc(8);
1950 row
[DB_name
] = X509_NAME_oneline(X509_get_subject_name(ret
), NULL
, 0);
1952 if ((row
[DB_type
] == NULL
) || (row
[DB_file
] == NULL
) ||
1953 (row
[DB_name
] == NULL
)) {
1954 BIO_printf(bio_err
, "Memory allocation failure\n");
1957 (void) strlcpy(row
[DB_file
], "unknown", 8);
1958 row
[DB_type
][0] = 'V';
1959 row
[DB_type
][1] = '\0';
1961 if ((irow
= reallocarray(NULL
, DB_NUMBER
+ 1, sizeof(char *))) ==
1963 BIO_printf(bio_err
, "Memory allocation failure\n");
1966 for (i
= 0; i
< DB_NUMBER
; i
++) {
1970 irow
[DB_NUMBER
] = NULL
;
1972 if (!TXT_DB_insert(db
->db
, irow
)) {
1973 BIO_printf(bio_err
, "failed to update database\n");
1974 BIO_printf(bio_err
, "TXT_DB error number %ld\n", db
->db
->error
);
1979 for (i
= 0; i
< DB_NUMBER
; i
++)
1983 X509_NAME_free(CAname
);
1984 if (subject
!= NULL
)
1985 X509_NAME_free(subject
);
1986 if ((dn_subject
!= NULL
) && !email_dn
)
1987 X509_NAME_free(dn_subject
);
1989 ASN1_UTCTIME_free(tmptm
);
2000 write_new_certificate(BIO
* bp
, X509
* x
, int output_der
, int notext
)
2003 (void) i2d_X509_bio(bp
, x
);
2008 PEM_write_bio_X509(bp
, x
);
2012 certify_spkac(X509
** xret
, char *infile
, EVP_PKEY
* pkey
, X509
* x509
,
2013 const EVP_MD
* dgst
, STACK_OF(OPENSSL_STRING
) * sigopts
,
2014 STACK_OF(CONF_VALUE
) * policy
, CA_DB
* db
, BIGNUM
* serial
, char *subj
,
2015 unsigned long chtype
, int multirdn
, int email_dn
, char *startdate
,
2016 char *enddate
, long days
, char *ext_sect
, CONF
* lconf
, int verbose
,
2017 unsigned long certopt
, unsigned long nameopt
, int default_op
, int ext_copy
)
2019 STACK_OF(CONF_VALUE
) * sk
= NULL
;
2020 LHASH_OF(CONF_VALUE
) * parms
= NULL
;
2021 X509_REQ
*req
= NULL
;
2022 CONF_VALUE
*cv
= NULL
;
2023 NETSCAPE_SPKI
*spki
= NULL
;
2026 EVP_PKEY
*pktmp
= NULL
;
2027 X509_NAME
*n
= NULL
;
2033 * Load input file into a hash table. (This is just an easy
2034 * way to read and parse the file, then put it into a convenient
2037 parms
= CONF_load(NULL
, infile
, &errline
);
2038 if (parms
== NULL
) {
2039 BIO_printf(bio_err
, "error on line %ld of %s\n",
2041 ERR_print_errors(bio_err
);
2044 sk
= CONF_get_section(parms
, "default");
2045 if (sk_CONF_VALUE_num(sk
) == 0) {
2046 BIO_printf(bio_err
, "no name/value pairs found in %s\n",
2052 * Now create a dummy X509 request structure. We don't actually
2053 * have an X509 request, but we have many of the components
2054 * (a public key, various DN components). The idea is that we
2055 * put these components into the right X509 request structure
2056 * and we can use the same code as if you had a real X509 request.
2058 req
= X509_REQ_new();
2060 ERR_print_errors(bio_err
);
2064 * Build up the subject name set.
2070 if (sk_CONF_VALUE_num(sk
) <= i
)
2073 cv
= sk_CONF_VALUE_value(sk
, i
);
2076 * Skip past any leading X. X: X, etc to allow for multiple
2079 for (buf
= cv
->name
; *buf
; buf
++) {
2080 if ((*buf
== ':') || (*buf
== ',') || (*buf
== '.')) {
2089 if ((nid
= OBJ_txt2nid(type
)) == NID_undef
) {
2090 if (strcmp(type
, "SPKAC") == 0) {
2091 spki
= NETSCAPE_SPKI_b64_decode(cv
->value
, -1);
2093 BIO_printf(bio_err
, "unable to load Netscape SPKAC structure\n");
2094 ERR_print_errors(bio_err
);
2100 if (!X509_NAME_add_entry_by_NID(n
, nid
, chtype
,
2101 (unsigned char *)buf
, -1, -1, 0))
2106 "Netscape SPKAC structure not found in %s\n", infile
);
2110 * Now extract the key from the SPKI structure.
2114 "Check that the SPKAC request matches the signature\n");
2116 if ((pktmp
= NETSCAPE_SPKI_get_pubkey(spki
)) == NULL
) {
2117 BIO_printf(bio_err
, "error unpacking SPKAC public key\n");
2120 j
= NETSCAPE_SPKI_verify(spki
, pktmp
);
2123 "signature verification failed on SPKAC public key\n");
2126 BIO_printf(bio_err
, "Signature ok\n");
2128 X509_REQ_set_pubkey(req
, pktmp
);
2129 EVP_PKEY_free(pktmp
);
2130 ok
= do_body(xret
, pkey
, x509
, dgst
, sigopts
, policy
, db
, serial
,
2131 subj
, chtype
, multirdn
, email_dn
, startdate
, enddate
, days
, 1,
2132 verbose
, req
, ext_sect
, lconf
, certopt
, nameopt
, default_op
,
2141 NETSCAPE_SPKI_free(spki
);
2147 check_time_format(const char *str
)
2149 return ASN1_TIME_set_string(NULL
, str
);
2153 do_revoke(X509
* x509
, CA_DB
* db
, int type
, char *value
)
2155 ASN1_UTCTIME
*tm
= NULL
;
2156 char *row
[DB_NUMBER
], **rrow
, **irow
;
2157 char *rev_str
= NULL
;
2161 for (i
= 0; i
< DB_NUMBER
; i
++)
2163 row
[DB_name
] = X509_NAME_oneline(X509_get_subject_name(x509
), NULL
, 0);
2164 bn
= ASN1_INTEGER_to_BN(X509_get_serialNumber(x509
), NULL
);
2168 row
[DB_serial
] = strdup("00");
2170 row
[DB_serial
] = BN_bn2hex(bn
);
2172 if ((row
[DB_name
] == NULL
) || (row
[DB_serial
] == NULL
)) {
2173 BIO_printf(bio_err
, "Memory allocation failure\n");
2177 * We have to lookup by serial number because name lookup skips
2180 rrow
= TXT_DB_get_by_index(db
->db
, DB_serial
, row
);
2183 "Adding Entry with serial number %s to DB for %s\n",
2184 row
[DB_serial
], row
[DB_name
]);
2186 /* We now just add it to the database */
2187 row
[DB_type
] = malloc(2);
2189 tm
= X509_get_notAfter(x509
);
2190 row
[DB_exp_date
] = malloc(tm
->length
+ 1);
2191 if (row
[DB_type
] == NULL
|| row
[DB_exp_date
] == NULL
) {
2192 BIO_printf(bio_err
, "Memory allocation failure\n");
2195 memcpy(row
[DB_exp_date
], tm
->data
, tm
->length
);
2196 row
[DB_exp_date
][tm
->length
] = '\0';
2198 row
[DB_rev_date
] = NULL
;
2200 /* row[DB_serial] done already */
2201 row
[DB_file
] = malloc(8);
2203 /* row[DB_name] done already */
2205 if ((row
[DB_type
] == NULL
) || (row
[DB_file
] == NULL
)) {
2206 BIO_printf(bio_err
, "Memory allocation failure\n");
2209 (void) strlcpy(row
[DB_file
], "unknown", 8);
2210 row
[DB_type
][0] = 'V';
2211 row
[DB_type
][1] = '\0';
2213 if ((irow
= reallocarray(NULL
, sizeof(char *),
2214 (DB_NUMBER
+ 1))) == NULL
) {
2215 BIO_printf(bio_err
, "Memory allocation failure\n");
2218 for (i
= 0; i
< DB_NUMBER
; i
++) {
2222 irow
[DB_NUMBER
] = NULL
;
2224 if (!TXT_DB_insert(db
->db
, irow
)) {
2225 BIO_printf(bio_err
, "failed to update database\n");
2226 BIO_printf(bio_err
, "TXT_DB error number %ld\n",
2230 /* Revoke Certificate */
2231 ok
= do_revoke(x509
, db
, type
, value
);
2235 } else if (index_name_cmp_noconst(row
, rrow
)) {
2236 BIO_printf(bio_err
, "ERROR:name does not match %s\n",
2239 } else if (rrow
[DB_type
][0] == 'R') {
2240 BIO_printf(bio_err
, "ERROR:Already revoked, serial number %s\n",
2244 BIO_printf(bio_err
, "Revoking Certificate %s.\n",
2246 rev_str
= make_revocation_str(type
, value
);
2248 BIO_printf(bio_err
, "Error in revocation arguments\n");
2251 rrow
[DB_type
][0] = 'R';
2252 rrow
[DB_type
][1] = '\0';
2253 rrow
[DB_rev_date
] = rev_str
;
2258 for (i
= 0; i
< DB_NUMBER
; i
++)
2265 get_certificate_status(const char *serial
, CA_DB
* db
)
2267 char *row
[DB_NUMBER
], **rrow
;
2270 /* Free Resources */
2271 for (i
= 0; i
< DB_NUMBER
; i
++)
2274 /* Malloc needed char spaces */
2275 row
[DB_serial
] = malloc(strlen(serial
) + 2);
2276 if (row
[DB_serial
] == NULL
) {
2277 BIO_printf(bio_err
, "Malloc failure\n");
2280 if (strlen(serial
) % 2) {
2281 /* Set the first char to 0 */ ;
2282 row
[DB_serial
][0] = '0';
2284 /* Copy String from serial to row[DB_serial] */
2285 memcpy(row
[DB_serial
] + 1, serial
, strlen(serial
));
2286 row
[DB_serial
][strlen(serial
) + 1] = '\0';
2288 /* Copy String from serial to row[DB_serial] */
2289 memcpy(row
[DB_serial
], serial
, strlen(serial
));
2290 row
[DB_serial
][strlen(serial
)] = '\0';
2293 /* Make it Upper Case */
2294 for (i
= 0; row
[DB_serial
][i
] != '\0'; i
++)
2295 row
[DB_serial
][i
] = toupper((unsigned char) row
[DB_serial
][i
]);
2300 /* Search for the certificate */
2301 rrow
= TXT_DB_get_by_index(db
->db
, DB_serial
, row
);
2303 BIO_printf(bio_err
, "Serial %s not present in db.\n",
2307 } else if (rrow
[DB_type
][0] == 'V') {
2308 BIO_printf(bio_err
, "%s=Valid (%c)\n",
2309 row
[DB_serial
], rrow
[DB_type
][0]);
2311 } else if (rrow
[DB_type
][0] == 'R') {
2312 BIO_printf(bio_err
, "%s=Revoked (%c)\n",
2313 row
[DB_serial
], rrow
[DB_type
][0]);
2315 } else if (rrow
[DB_type
][0] == 'E') {
2316 BIO_printf(bio_err
, "%s=Expired (%c)\n",
2317 row
[DB_serial
], rrow
[DB_type
][0]);
2319 } else if (rrow
[DB_type
][0] == 'S') {
2320 BIO_printf(bio_err
, "%s=Suspended (%c)\n",
2321 row
[DB_serial
], rrow
[DB_type
][0]);
2324 BIO_printf(bio_err
, "%s=Unknown (%c).\n",
2325 row
[DB_serial
], rrow
[DB_type
][0]);
2330 for (i
= 0; i
< DB_NUMBER
; i
++)
2337 do_updatedb(CA_DB
* db
)
2339 ASN1_UTCTIME
*a_tm
= NULL
;
2341 int db_y2k
, a_y2k
; /* flags = 1 if y >= 2000 */
2342 char **rrow
, *a_tm_s
;
2344 a_tm
= ASN1_UTCTIME_new();
2346 /* get actual time and make a string */
2347 a_tm
= X509_gmtime_adj(a_tm
, 0);
2348 a_tm_s
= malloc(a_tm
->length
+ 1);
2349 if (a_tm_s
== NULL
) {
2353 memcpy(a_tm_s
, a_tm
->data
, a_tm
->length
);
2354 a_tm_s
[a_tm
->length
] = '\0';
2356 if (strncmp(a_tm_s
, "49", 2) <= 0)
2361 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(db
->db
->data
); i
++) {
2362 rrow
= sk_OPENSSL_PSTRING_value(db
->db
->data
, i
);
2364 if (rrow
[DB_type
][0] == 'V') {
2365 /* ignore entries that are not valid */
2366 if (strncmp(rrow
[DB_exp_date
], "49", 2) <= 0)
2371 if (db_y2k
== a_y2k
) {
2372 /* all on the same y2k side */
2373 if (strcmp(rrow
[DB_exp_date
], a_tm_s
) <= 0) {
2374 rrow
[DB_type
][0] = 'E';
2375 rrow
[DB_type
][1] = '\0';
2378 BIO_printf(bio_err
, "%s=Expired\n",
2381 } else if (db_y2k
< a_y2k
) {
2382 rrow
[DB_type
][0] = 'E';
2383 rrow
[DB_type
][1] = '\0';
2386 BIO_printf(bio_err
, "%s=Expired\n",
2393 ASN1_UTCTIME_free(a_tm
);
2399 static const char *crl_reasons
[] = {
2400 /* CRL reason strings */
2404 "affiliationChanged",
2406 "cessationOfOperation",
2409 /* Additional pseudo reasons */
2415 #define NUM_REASONS (sizeof(crl_reasons) / sizeof(char *))
2417 /* Given revocation information convert to a DB string.
2418 * The format of the string is:
2419 * revtime[,reason,extra]. Where 'revtime' is the
2420 * revocation time (the current time). 'reason' is the
2421 * optional CRL reason and 'extra' is any additional
2426 make_revocation_str(int rev_type
, char *rev_arg
)
2428 char *other
= NULL
, *str
;
2429 const char *reason
= NULL
;
2431 ASN1_UTCTIME
*revtm
= NULL
;
2437 case REV_CRL_REASON
:
2438 for (i
= 0; i
< 8; i
++) {
2439 if (!strcasecmp(rev_arg
, crl_reasons
[i
])) {
2440 reason
= crl_reasons
[i
];
2444 if (reason
== NULL
) {
2445 BIO_printf(bio_err
, "Unknown CRL reason %s\n", rev_arg
);
2451 /* Argument is an OID */
2453 otmp
= OBJ_txt2obj(rev_arg
, 0);
2454 ASN1_OBJECT_free(otmp
);
2458 "Invalid object identifier %s\n", rev_arg
);
2461 reason
= "holdInstruction";
2465 case REV_KEY_COMPROMISE
:
2466 case REV_CA_COMPROMISE
:
2468 /* Argument is the key compromise time */
2469 if (!ASN1_GENERALIZEDTIME_set_string(NULL
, rev_arg
)) {
2471 "Invalid time format %s. Need YYYYMMDDHHMMSSZ\n",
2476 if (rev_type
== REV_KEY_COMPROMISE
)
2479 reason
= "CAkeyTime";
2485 revtm
= X509_gmtime_adj(NULL
, 0);
2486 if (asprintf(&str
, "%s%s%s%s%s", revtm
->data
,
2487 reason
? "," : "", reason
? reason
: "",
2488 other
? "," : "", other
? other
: "") == -1)
2490 ASN1_UTCTIME_free(revtm
);
2494 /* Convert revocation field to X509_REVOKED entry
2498 * 2 OK and some extensions added (i.e. V2 CRL)
2502 make_revoked(X509_REVOKED
* rev
, const char *str
)
2505 int reason_code
= -1;
2507 ASN1_OBJECT
*hold
= NULL
;
2508 ASN1_GENERALIZEDTIME
*comp_time
= NULL
;
2509 ASN1_ENUMERATED
*rtmp
= NULL
;
2511 ASN1_TIME
*revDate
= NULL
;
2513 i
= unpack_revinfo(&revDate
, &reason_code
, &hold
, &comp_time
, str
);
2518 if (rev
&& !X509_REVOKED_set_revocationDate(rev
, revDate
))
2521 if (rev
&& (reason_code
!= OCSP_REVOKED_STATUS_NOSTATUS
)) {
2522 rtmp
= ASN1_ENUMERATED_new();
2523 if (!rtmp
|| !ASN1_ENUMERATED_set(rtmp
, reason_code
))
2525 if (!X509_REVOKED_add1_ext_i2d(rev
, NID_crl_reason
, rtmp
, 0, 0))
2528 if (rev
&& comp_time
) {
2529 if (!X509_REVOKED_add1_ext_i2d(rev
, NID_invalidity_date
,
2534 if (!X509_REVOKED_add1_ext_i2d(rev
, NID_hold_instruction_code
,
2538 if (reason_code
!= OCSP_REVOKED_STATUS_NOSTATUS
)
2546 ASN1_OBJECT_free(hold
);
2547 ASN1_GENERALIZEDTIME_free(comp_time
);
2548 ASN1_ENUMERATED_free(rtmp
);
2549 ASN1_TIME_free(revDate
);
2555 old_entry_print(BIO
* bp
, ASN1_OBJECT
* obj
, ASN1_STRING
* str
)
2557 char buf
[25], *pbuf
, *p
;
2560 j
= i2a_ASN1_OBJECT(bp
, obj
);
2562 for (j
= 22 - j
; j
> 0; j
--)
2568 if (str
->type
== V_ASN1_PRINTABLESTRING
)
2569 BIO_printf(bp
, "PRINTABLE:'");
2570 else if (str
->type
== V_ASN1_T61STRING
)
2571 BIO_printf(bp
, "T61STRING:'");
2572 else if (str
->type
== V_ASN1_IA5STRING
)
2573 BIO_printf(bp
, "IA5STRING:'");
2574 else if (str
->type
== V_ASN1_UNIVERSALSTRING
)
2575 BIO_printf(bp
, "UNIVERSALSTRING:'");
2577 BIO_printf(bp
, "ASN.1 %2d:'", str
->type
);
2579 p
= (char *) str
->data
;
2580 for (j
= str
->length
; j
> 0; j
--) {
2581 if ((*p
>= ' ') && (*p
<= '~'))
2582 BIO_printf(bp
, "%c", *p
);
2584 BIO_printf(bp
, "\\0x%02X", *p
);
2585 else if ((unsigned char) *p
== 0xf7)
2586 BIO_printf(bp
, "^?");
2588 BIO_printf(bp
, "^%c", *p
+ '@');
2591 BIO_printf(bp
, "'\n");
2596 unpack_revinfo(ASN1_TIME
** prevtm
, int *preason
, ASN1_OBJECT
** phold
,
2597 ASN1_GENERALIZEDTIME
** pinvtm
, const char *str
)
2600 char *rtime_str
, *reason_str
= NULL
, *arg_str
= NULL
, *p
;
2601 int reason_code
= -1;
2604 ASN1_OBJECT
*hold
= NULL
;
2605 ASN1_GENERALIZEDTIME
*comp_time
= NULL
;
2607 if ((tmp
= strdup(str
)) == NULL
) {
2608 BIO_printf(bio_err
, "malloc failed\n");
2611 p
= strchr(tmp
, ',');
2625 *prevtm
= ASN1_UTCTIME_new();
2626 if (!ASN1_UTCTIME_set_string(*prevtm
, rtime_str
)) {
2627 BIO_printf(bio_err
, "invalid revocation date %s\n",
2633 for (i
= 0; i
< NUM_REASONS
; i
++) {
2634 if (!strcasecmp(reason_str
, crl_reasons
[i
])) {
2639 if (reason_code
== OCSP_REVOKED_STATUS_NOSTATUS
) {
2640 BIO_printf(bio_err
, "invalid reason code %s\n",
2644 if (reason_code
== 7)
2645 reason_code
= OCSP_REVOKED_STATUS_REMOVEFROMCRL
;
2646 else if (reason_code
== 8) { /* Hold instruction */
2649 "missing hold instruction\n");
2652 reason_code
= OCSP_REVOKED_STATUS_CERTIFICATEHOLD
;
2653 hold
= OBJ_txt2obj(arg_str
, 0);
2657 "invalid object identifier %s\n", arg_str
);
2662 } else if ((reason_code
== 9) || (reason_code
== 10)) {
2665 "missing compromised time\n");
2668 comp_time
= ASN1_GENERALIZEDTIME_new();
2669 if (!ASN1_GENERALIZEDTIME_set_string(comp_time
,
2672 "invalid compromised time %s\n", arg_str
);
2675 if (reason_code
== 9)
2676 reason_code
= OCSP_REVOKED_STATUS_KEYCOMPROMISE
;
2678 reason_code
= OCSP_REVOKED_STATUS_CACOMPROMISE
;
2682 *preason
= reason_code
;
2684 *pinvtm
= comp_time
;
2686 ASN1_GENERALIZEDTIME_free(comp_time
);
2694 ASN1_OBJECT_free(hold
);
2696 ASN1_GENERALIZEDTIME_free(comp_time
);
2702 bin2hex(unsigned char * data
, size_t len
)
2705 char hex
[] = "0123456789ABCDEF";
2708 if ((ret
= malloc(len
* 2 + 1))) {
2709 for (i
= 0; i
< len
; i
++) {
2710 ret
[i
* 2 + 0] = hex
[data
[i
] >> 4];
2711 ret
[i
* 2 + 1] = hex
[data
[i
] & 0x0F];
2713 ret
[len
* 2] = '\0';