2 * Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
9 #include "internal/e_os.h"
15 #include <sys/types.h>
16 #include <openssl/conf.h>
17 #include <openssl/bio.h>
18 #include <openssl/err.h>
19 #include <openssl/bn.h>
20 #include <openssl/txt_db.h>
21 #include <openssl/evp.h>
22 #include <openssl/x509.h>
23 #include <openssl/x509v3.h>
24 #include <openssl/objects.h>
25 #include <openssl/ocsp.h>
26 #include <openssl/pem.h>
29 # ifdef OPENSSL_SYS_VMS
31 # elif !defined(OPENSSL_SYS_VXWORKS) && !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_TANDEM)
32 # include <sys/file.h>
46 # define PATH_MAX 4096
49 #define BASE_SECTION "ca"
51 #define ENV_DEFAULT_CA "default_ca"
53 #define STRING_MASK "string_mask"
54 #define UTF8_IN "utf8"
56 #define ENV_NEW_CERTS_DIR "new_certs_dir"
57 #define ENV_CERTIFICATE "certificate"
58 #define ENV_SERIAL "serial"
59 #define ENV_RAND_SERIAL "rand_serial"
60 #define ENV_CRLNUMBER "crlnumber"
61 #define ENV_PRIVATE_KEY "private_key"
62 #define ENV_DEFAULT_DAYS "default_days"
63 #define ENV_DEFAULT_STARTDATE "default_startdate"
64 #define ENV_DEFAULT_ENDDATE "default_enddate"
65 #define ENV_DEFAULT_CRL_DAYS "default_crl_days"
66 #define ENV_DEFAULT_CRL_HOURS "default_crl_hours"
67 #define ENV_DEFAULT_MD "default_md"
68 #define ENV_DEFAULT_EMAIL_DN "email_in_dn"
69 #define ENV_PRESERVE "preserve"
70 #define ENV_POLICY "policy"
71 #define ENV_EXTENSIONS "x509_extensions"
72 #define ENV_CRLEXT "crl_extensions"
73 #define ENV_MSIE_HACK "msie_hack"
74 #define ENV_NAMEOPT "name_opt"
75 #define ENV_CERTOPT "cert_opt"
76 #define ENV_EXTCOPY "copy_extensions"
77 #define ENV_UNIQUE_SUBJECT "unique_subject"
79 #define ENV_DATABASE "database"
81 /* Additional revocation information types */
83 REV_VALID
= -1, /* Valid (not-revoked) status */
84 REV_NONE
= 0, /* No additional information */
85 REV_CRL_REASON
= 1, /* Value is CRL reason code */
86 REV_HOLD
= 2, /* Value is hold instruction */
87 REV_KEY_COMPROMISE
= 3, /* Value is cert key compromise time */
88 REV_CA_COMPROMISE
= 4 /* Value is CA key compromise time */
91 static char *lookup_conf(const CONF
*conf
, const char *group
, const char *tag
);
93 static int certify(X509
**xret
, const char *infile
, int informat
,
94 EVP_PKEY
*pkey
, X509
*x509
,
96 STACK_OF(OPENSSL_STRING
) *sigopts
,
97 STACK_OF(OPENSSL_STRING
) *vfyopts
,
98 STACK_OF(CONF_VALUE
) *policy
, CA_DB
*db
,
99 BIGNUM
*serial
, const char *subj
, unsigned long chtype
,
100 int multirdn
, int email_dn
, const char *startdate
,
102 long days
, int batch
, const char *ext_sect
, CONF
*conf
,
103 int verbose
, unsigned long certopt
, unsigned long nameopt
,
104 int default_op
, int ext_copy
, int selfsign
, unsigned long dateopt
);
105 static int certify_cert(X509
**xret
, const char *infile
, int certformat
,
106 const char *passin
, EVP_PKEY
*pkey
, X509
*x509
,
108 STACK_OF(OPENSSL_STRING
) *sigopts
,
109 STACK_OF(OPENSSL_STRING
) *vfyopts
,
110 STACK_OF(CONF_VALUE
) *policy
, CA_DB
*db
,
111 BIGNUM
*serial
, const char *subj
, unsigned long chtype
,
112 int multirdn
, int email_dn
, const char *startdate
,
113 const char *enddate
, long days
, int batch
, const char *ext_sect
,
114 CONF
*conf
, int verbose
, unsigned long certopt
,
115 unsigned long nameopt
, int default_op
, int ext_copy
, unsigned long dateopt
);
116 static int certify_spkac(X509
**xret
, const char *infile
, EVP_PKEY
*pkey
,
117 X509
*x509
, const char *dgst
,
118 STACK_OF(OPENSSL_STRING
) *sigopts
,
119 STACK_OF(CONF_VALUE
) *policy
, CA_DB
*db
,
120 BIGNUM
*serial
, const char *subj
, unsigned long chtype
,
121 int multirdn
, int email_dn
, const char *startdate
,
122 const char *enddate
, long days
, const char *ext_sect
, CONF
*conf
,
123 int verbose
, unsigned long certopt
,
124 unsigned long nameopt
, int default_op
, int ext_copy
, unsigned long dateopt
);
125 static int do_body(X509
**xret
, EVP_PKEY
*pkey
, X509
*x509
,
126 const char *dgst
, STACK_OF(OPENSSL_STRING
) *sigopts
,
127 STACK_OF(CONF_VALUE
) *policy
, CA_DB
*db
, BIGNUM
*serial
,
128 const char *subj
, unsigned long chtype
, int multirdn
,
129 int email_dn
, const char *startdate
, const char *enddate
, long days
,
130 int batch
, int verbose
, X509_REQ
*req
, const char *ext_sect
,
131 CONF
*conf
, unsigned long certopt
, unsigned long nameopt
,
132 int default_op
, int ext_copy
, int selfsign
, unsigned long dateopt
);
133 static int get_certificate_status(const char *ser_status
, CA_DB
*db
);
134 static int check_time_format(const char *str
);
135 static int do_revoke(X509
*x509
, CA_DB
*db
, REVINFO_TYPE rev_type
,
137 static char *make_revocation_str(REVINFO_TYPE rev_type
, const char *rev_arg
);
138 static int make_revoked(X509_REVOKED
*rev
, const char *str
);
139 static int old_entry_print(const ASN1_OBJECT
*obj
, const ASN1_STRING
*str
);
140 static void write_new_certificate(BIO
*bp
, X509
*x
, int output_der
, int notext
);
142 static CONF
*extfile_conf
= NULL
;
143 static int preserve
= 0;
144 static int msie_hack
= 0;
146 typedef enum OPTION_choice
{
148 OPT_ENGINE
, OPT_VERBOSE
, OPT_CONFIG
, OPT_NAME
, OPT_SUBJ
, OPT_UTF8
,
149 OPT_CREATE_SERIAL
, OPT_MULTIVALUE_RDN
, OPT_STARTDATE
, OPT_ENDDATE
,
150 OPT_DAYS
, OPT_MD
, OPT_POLICY
, OPT_KEYFILE
, OPT_KEYFORM
, OPT_PASSIN
,
151 OPT_KEY
, OPT_CERT
, OPT_CERTFORM
, OPT_SELFSIGN
,
152 OPT_IN
, OPT_INFORM
, OPT_OUT
, OPT_DATEOPT
, OPT_OUTDIR
, OPT_VFYOPT
,
153 OPT_SIGOPT
, OPT_NOTEXT
, OPT_BATCH
, OPT_PRESERVEDN
, OPT_NOEMAILDN
,
154 OPT_GENCRL
, OPT_MSIE_HACK
, OPT_CRL_LASTUPDATE
, OPT_CRL_NEXTUPDATE
,
155 OPT_CRLDAYS
, OPT_CRLHOURS
, OPT_CRLSEC
, OPT_NOT_BEFORE
, OPT_NOT_AFTER
,
156 OPT_INFILES
, OPT_SS_CERT
, OPT_SPKAC
, OPT_REVOKE
, OPT_VALID
,
157 OPT_EXTENSIONS
, OPT_EXTFILE
, OPT_STATUS
, OPT_UPDATEDB
, OPT_CRLEXTS
,
158 OPT_RAND_SERIAL
, OPT_QUIET
,
159 OPT_R_ENUM
, OPT_PROV_ENUM
,
160 /* Do not change the order here; see related case statements below */
161 OPT_CRL_REASON
, OPT_CRL_HOLD
, OPT_CRL_COMPROMISE
, OPT_CRL_CA_COMPROMISE
164 const OPTIONS ca_options
[] = {
165 {OPT_HELP_STR
, 1, '-', "Usage: %s [options] [certreq...]\n"},
167 OPT_SECTION("General"),
168 {"help", OPT_HELP
, '-', "Display this summary"},
169 {"verbose", OPT_VERBOSE
, '-', "Verbose output during processing"},
170 {"quiet", OPT_QUIET
, '-', "Terse output during processing"},
171 {"outdir", OPT_OUTDIR
, '/', "Where to put output cert"},
172 {"in", OPT_IN
, '<', "The input cert request(s)"},
173 {"inform", OPT_INFORM
, 'F',
174 "CSR input format to use (PEM or DER; by default try PEM first)"},
175 {"infiles", OPT_INFILES
, '-', "The last argument, requests to process"},
176 {"out", OPT_OUT
, '>', "Where to put the output file(s)"},
177 {"dateopt", OPT_DATEOPT
, 's', "Datetime format used for printing. (rfc_822/iso_8601). Default is rfc_822."},
178 {"notext", OPT_NOTEXT
, '-', "Do not print the generated certificate"},
179 {"batch", OPT_BATCH
, '-', "Don't ask questions"},
180 {"msie_hack", OPT_MSIE_HACK
, '-',
181 "msie modifications to handle all Universal Strings"},
182 {"ss_cert", OPT_SS_CERT
, '<', "File contains a self signed cert to sign"},
183 {"spkac", OPT_SPKAC
, '<',
184 "File contains DN and signed public key and challenge"},
185 #ifndef OPENSSL_NO_ENGINE
186 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
189 OPT_SECTION("Configuration"),
190 {"config", OPT_CONFIG
, 's', "A config file"},
191 {"name", OPT_NAME
, 's', "The particular CA definition to use"},
192 {"section", OPT_NAME
, 's', "An alias for -name"},
193 {"policy", OPT_POLICY
, 's', "The CA 'policy' to support"},
195 OPT_SECTION("Certificate"),
196 {"subj", OPT_SUBJ
, 's', "Use arg instead of request's subject"},
197 {"utf8", OPT_UTF8
, '-', "Input characters are UTF8; default ASCII"},
198 {"create_serial", OPT_CREATE_SERIAL
, '-',
199 "If reading serial fails, create a new random serial"},
200 {"rand_serial", OPT_RAND_SERIAL
, '-',
201 "Always create a random serial; do not store it"},
202 {"multivalue-rdn", OPT_MULTIVALUE_RDN
, '-',
203 "Deprecated; multi-valued RDNs support is always on."},
204 {"startdate", OPT_STARTDATE
, 's',
205 "[CC]YYMMDDHHMMSSZ value for notBefore certificate field"},
206 {"not_before", OPT_NOT_BEFORE
, 's', "An alias for -startdate"},
207 {"enddate", OPT_ENDDATE
, 's',
208 "[CC]YYMMDDHHMMSSZ value for notAfter certificate field, overrides -days"},
209 {"not_after", OPT_NOT_AFTER
, 's', "An alias for -enddate"},
210 {"days", OPT_DAYS
, 'p', "Number of days from today to certify the cert for"},
211 {"extensions", OPT_EXTENSIONS
, 's',
212 "Extension section (override value in config file)"},
213 {"extfile", OPT_EXTFILE
, '<',
214 "Configuration file with X509v3 extensions to add"},
215 {"preserveDN", OPT_PRESERVEDN
, '-', "Don't re-order the DN"},
216 {"noemailDN", OPT_NOEMAILDN
, '-', "Don't add the EMAIL field to the DN"},
218 OPT_SECTION("Signing"),
219 {"md", OPT_MD
, 's', "Digest to use, such as sha256"},
220 {"keyfile", OPT_KEYFILE
, 's', "The CA private key"},
221 {"keyform", OPT_KEYFORM
, 'f',
222 "Private key file format (ENGINE, other values ignored)"},
223 {"passin", OPT_PASSIN
, 's', "Key and cert input file pass phrase source"},
224 {"key", OPT_KEY
, 's',
225 "Key to decrypt the private key or cert files if encrypted. Better use -passin"},
226 {"cert", OPT_CERT
, '<', "The CA cert"},
227 {"certform", OPT_CERTFORM
, 'F',
228 "Certificate input format (DER/PEM/P12); has no effect"},
229 {"selfsign", OPT_SELFSIGN
, '-',
230 "Sign a cert with the key associated with it"},
231 {"sigopt", OPT_SIGOPT
, 's', "Signature parameter in n:v form"},
232 {"vfyopt", OPT_VFYOPT
, 's', "Verification parameter in n:v form"},
234 OPT_SECTION("Revocation"),
235 {"gencrl", OPT_GENCRL
, '-', "Generate a new CRL"},
236 {"valid", OPT_VALID
, 's',
237 "Add a Valid(not-revoked) DB entry about a cert (given in file)"},
238 {"status", OPT_STATUS
, 's', "Shows cert status given the serial number"},
239 {"updatedb", OPT_UPDATEDB
, '-', "Updates db for expired cert"},
240 {"crlexts", OPT_CRLEXTS
, 's',
241 "CRL extension section (override value in config file)"},
242 {"crl_reason", OPT_CRL_REASON
, 's', "revocation reason"},
243 {"crl_hold", OPT_CRL_HOLD
, 's',
244 "the hold instruction, an OID. Sets revocation reason to certificateHold"},
245 {"crl_compromise", OPT_CRL_COMPROMISE
, 's',
246 "sets compromise time to val and the revocation reason to keyCompromise"},
247 {"crl_CA_compromise", OPT_CRL_CA_COMPROMISE
, 's',
248 "sets compromise time to val and the revocation reason to CACompromise"},
249 {"crl_lastupdate", OPT_CRL_LASTUPDATE
, 's',
250 "Sets the CRL lastUpdate time to val (YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ)"},
251 {"crl_nextupdate", OPT_CRL_NEXTUPDATE
, 's',
252 "Sets the CRL nextUpdate time to val (YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ)"},
253 {"crldays", OPT_CRLDAYS
, 'p', "Days until the next CRL is due"},
254 {"crlhours", OPT_CRLHOURS
, 'p', "Hours until the next CRL is due"},
255 {"crlsec", OPT_CRLSEC
, 'p', "Seconds until the next CRL is due"},
256 {"revoke", OPT_REVOKE
, '<', "Revoke a cert (given in file)"},
262 {"certreq", 0, 0, "Certificate requests to be signed (optional)"},
266 int ca_main(int argc
, char **argv
)
270 BIGNUM
*crlnumber
= NULL
, *serial
= NULL
;
271 EVP_PKEY
*pkey
= NULL
;
272 BIO
*in
= NULL
, *out
= NULL
, *Sout
= NULL
;
273 ASN1_INTEGER
*tmpser
;
276 STACK_OF(CONF_VALUE
) *attribs
= NULL
;
277 STACK_OF(OPENSSL_STRING
) *sigopts
= NULL
, *vfyopts
= NULL
;
278 STACK_OF(X509
) *cert_sk
= NULL
;
279 X509_CRL
*crl
= NULL
;
280 char *configfile
= default_config_file
, *section
= NULL
;
281 char def_dgst
[80] = "";
282 char *dgst
= NULL
, *policy
= NULL
, *keyfile
= NULL
;
283 char *certfile
= NULL
, *crl_ext
= NULL
, *crlnumberfile
= NULL
;
284 int certformat
= FORMAT_UNDEF
, informat
= FORMAT_UNDEF
;
285 unsigned long dateopt
= ASN1_DTFLGS_RFC822
;
286 const char *infile
= NULL
, *spkac_file
= NULL
, *ss_cert_file
= NULL
;
287 const char *extensions
= NULL
, *extfile
= NULL
, *passinarg
= NULL
;
289 char *outdir
= NULL
, *outfile
= NULL
, *rev_arg
= NULL
, *ser_status
= NULL
;
290 const char *serialfile
= NULL
, *subj
= NULL
;
291 char *prog
, *startdate
= NULL
, *enddate
= NULL
;
292 char *dbfile
= NULL
, *f
;
293 char new_cert
[PATH_MAX
];
294 char tmp
[10 + 1] = "\0";
297 size_t outdirlen
= 0;
298 int create_ser
= 0, free_passin
= 0, total
= 0, total_done
= 0;
299 int batch
= 0, default_op
= 1, doupdatedb
= 0, ext_copy
= EXT_COPY_NONE
;
300 int keyformat
= FORMAT_UNDEF
, multirdn
= 1, notext
= 0, output_der
= 0;
301 int ret
= 1, email_dn
= 1, req
= 0, verbose
= 0, gencrl
= 0, dorevoke
= 0;
302 int rand_ser
= 0, i
, j
, selfsign
= 0, def_ret
;
303 char *crl_lastupdate
= NULL
, *crl_nextupdate
= NULL
;
304 long crldays
= 0, crlhours
= 0, crlsec
= 0, days
= 0;
305 unsigned long chtype
= MBSTRING_ASC
, certopt
= 0;
306 X509
*x509
= NULL
, *x509p
= NULL
, *x
= NULL
;
307 REVINFO_TYPE rev_type
= REV_NONE
;
308 X509_REVOKED
*r
= NULL
;
311 prog
= opt_init(argc
, argv
, ca_options
);
312 while ((o
= opt_next()) != OPT_EOF
) {
317 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
320 opt_help(ca_options
);
328 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &informat
))
335 if (!set_dateopt(&dateopt
, opt_arg()))
345 configfile
= opt_arg();
355 chtype
= MBSTRING_UTF8
;
357 case OPT_RAND_SERIAL
:
360 case OPT_CREATE_SERIAL
:
363 case OPT_MULTIVALUE_RDN
:
368 startdate
= opt_arg();
375 days
= atoi(opt_arg());
387 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &keyformat
))
391 passinarg
= opt_arg();
398 if (!opt_provider(o
))
405 certfile
= opt_arg();
408 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &certformat
))
419 sigopts
= sk_OPENSSL_STRING_new_null();
420 if (sigopts
== NULL
|| !sk_OPENSSL_STRING_push(sigopts
, opt_arg()))
425 vfyopts
= sk_OPENSSL_STRING_new_null();
426 if (vfyopts
== NULL
|| !sk_OPENSSL_STRING_push(vfyopts
, opt_arg()))
447 case OPT_CRL_LASTUPDATE
:
448 crl_lastupdate
= opt_arg();
450 case OPT_CRL_NEXTUPDATE
:
451 crl_nextupdate
= opt_arg();
454 crldays
= atol(opt_arg());
457 crlhours
= atol(opt_arg());
460 crlsec
= atol(opt_arg());
466 ss_cert_file
= opt_arg();
470 spkac_file
= opt_arg();
482 extensions
= opt_arg();
488 ser_status
= opt_arg();
496 case OPT_CRL_REASON
: /* := REV_CRL_REASON */
498 case OPT_CRL_COMPROMISE
:
499 case OPT_CRL_CA_COMPROMISE
:
501 rev_type
= (o
- OPT_CRL_REASON
) + REV_CRL_REASON
;
504 e
= setup_engine(opt_arg(), 0);
510 /* Remaining args are files to certify. */
511 argc
= opt_num_rest();
514 if ((conf
= app_load_config_verbose(configfile
, 1)) == NULL
)
516 if (configfile
!= default_config_file
&& !app_load_modules(conf
))
519 /* Lets get the config section we are using */
521 && (section
= lookup_conf(conf
, BASE_SECTION
, ENV_DEFAULT_CA
)) == NULL
)
524 p
= app_conf_try_string(conf
, NULL
, "oid_file");
526 BIO
*oid_bio
= BIO_new_file(p
, "r");
528 if (oid_bio
== NULL
) {
531 OBJ_create_objects(oid_bio
);
535 if (!add_oid_section(conf
))
538 app_RAND_load_conf(conf
, BASE_SECTION
);
539 if (!app_RAND_load())
542 f
= app_conf_try_string(conf
, section
, STRING_MASK
);
543 if (f
!= NULL
&& !ASN1_STRING_set_default_mask_asc(f
)) {
544 BIO_printf(bio_err
, "Invalid global string mask setting %s\n", f
);
548 if (chtype
!= MBSTRING_UTF8
) {
549 f
= app_conf_try_string(conf
, section
, UTF8_IN
);
550 if (f
!= NULL
&& strcmp(f
, "yes") == 0)
551 chtype
= MBSTRING_UTF8
;
554 db_attr
.unique_subject
= 1;
555 p
= app_conf_try_string(conf
, section
, ENV_UNIQUE_SUBJECT
);
557 db_attr
.unique_subject
= parse_yesno(p
, 1);
559 /*****************************************************************/
560 /* report status of cert with serial number given on command line */
562 dbfile
= lookup_conf(conf
, section
, ENV_DATABASE
);
566 db
= load_index(dbfile
, &db_attr
);
568 BIO_printf(bio_err
, "Problem with index file: %s (could not load/parse file)\n", dbfile
);
572 if (index_index(db
) <= 0)
575 if (get_certificate_status(ser_status
, db
) != 1)
576 BIO_printf(bio_err
, "Error verifying serial %s!\n", ser_status
);
580 /*****************************************************************/
581 /* we definitely need a private key, so let's get it */
584 && (keyfile
= lookup_conf(conf
, section
, ENV_PRIVATE_KEY
)) == NULL
)
587 if (passin
== NULL
) {
589 if (!app_passwd(passinarg
, NULL
, &passin
, NULL
)) {
590 BIO_printf(bio_err
, "Error getting password\n");
594 pkey
= load_key(keyfile
, keyformat
, 0, passin
, e
, "CA private key");
597 /* load_key() has already printed an appropriate message */
600 /*****************************************************************/
601 /* we need a certificate */
602 if (!selfsign
|| spkac_file
|| ss_cert_file
|| gencrl
) {
604 && (certfile
= lookup_conf(conf
, section
, ENV_CERTIFICATE
)) == NULL
)
607 x509
= load_cert_pass(certfile
, certformat
, 1, passin
, "CA certificate");
611 if (!X509_check_private_key(x509
, pkey
)) {
613 "CA certificate and CA private key do not match\n");
620 f
= app_conf_try_string(conf
, BASE_SECTION
, ENV_PRESERVE
);
621 if (f
!= NULL
&& (*f
== 'y' || *f
== 'Y'))
623 f
= app_conf_try_string(conf
, BASE_SECTION
, ENV_MSIE_HACK
);
624 if (f
!= NULL
&& (*f
== 'y' || *f
== 'Y'))
627 f
= app_conf_try_string(conf
, section
, ENV_NAMEOPT
);
629 if (!set_nameopt(f
)) {
630 BIO_printf(bio_err
, "Invalid name options: \"%s\"\n", f
);
636 f
= app_conf_try_string(conf
, section
, ENV_CERTOPT
);
638 if (!set_cert_ex(&certopt
, f
)) {
639 BIO_printf(bio_err
, "Invalid certificate options: \"%s\"\n", f
);
645 f
= app_conf_try_string(conf
, section
, ENV_EXTCOPY
);
647 if (!set_ext_copy(&ext_copy
, f
)) {
648 BIO_printf(bio_err
, "Invalid extension copy option: \"%s\"\n", f
);
653 /*****************************************************************/
654 /* lookup where to write new certificates */
655 if ((outdir
== NULL
) && (req
)) {
657 outdir
= NCONF_get_string(conf
, section
, ENV_NEW_CERTS_DIR
);
658 if (outdir
== NULL
) {
660 "there needs to be defined a directory for new certificate to be placed in\n");
663 #ifndef OPENSSL_SYS_VMS
665 * outdir is a directory spec, but access() for VMS demands a
666 * filename. We could use the DEC C routine to convert the
667 * directory syntax to Unix, and give that to app_isdir,
668 * but for now the fopen will catch the error if it's not a
671 if (app_isdir(outdir
) <= 0) {
672 BIO_printf(bio_err
, "%s: %s is not a directory\n", prog
, outdir
);
679 /*****************************************************************/
680 /* we need to load the database file */
681 dbfile
= lookup_conf(conf
, section
, ENV_DATABASE
);
685 db
= load_index(dbfile
, &db_attr
);
687 BIO_printf(bio_err
, "Problem with index file: %s (could not load/parse file)\n", dbfile
);
691 /* Lets check some fields */
692 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(db
->db
->data
); i
++) {
693 pp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, i
);
694 if ((pp
[DB_type
][0] != DB_TYPE_REV
) && (pp
[DB_rev_date
][0] != '\0')) {
696 "entry %d: not revoked yet, but has a revocation date\n",
700 if ((pp
[DB_type
][0] == DB_TYPE_REV
) &&
701 !make_revoked(NULL
, pp
[DB_rev_date
])) {
702 BIO_printf(bio_err
, " in entry %d\n", i
+ 1);
705 if (!check_time_format((char *)pp
[DB_exp_date
])) {
706 BIO_printf(bio_err
, "entry %d: invalid expiry date\n", i
+ 1);
715 if ((j
& 1) || (j
< 2)) {
716 BIO_printf(bio_err
, "entry %d: bad serial number length (%d)\n",
721 if (!isxdigit(_UC(*p
))) {
723 "entry %d: bad char 0%o '%c' in serial number\n",
730 TXT_DB_write(bio_out
, db
->db
);
731 BIO_printf(bio_err
, "%d entries loaded from the database\n",
732 sk_OPENSSL_PSTRING_num(db
->db
->data
));
733 BIO_printf(bio_err
, "generating index\n");
736 if (index_index(db
) <= 0)
739 /*****************************************************************/
740 /* Update the db file for expired certificates */
743 BIO_printf(bio_err
, "Updating %s ...\n", dbfile
);
745 i
= do_updatedb(db
, NULL
);
747 BIO_printf(bio_err
, "Malloc failure\n");
751 BIO_printf(bio_err
, "No entries found to mark expired\n");
753 if (!save_index(dbfile
, "new", db
))
756 if (!rotate_index(dbfile
, "new", "old"))
760 BIO_printf(bio_err
, "Done. %d entries marked as expired\n", i
);
764 /*****************************************************************/
765 /* Read extensions config file */
767 if ((extfile_conf
= app_load_config(extfile
)) == NULL
) {
773 BIO_printf(bio_err
, "Successfully loaded extensions file %s\n",
776 /* We can have sections in the ext file */
777 if (extensions
== NULL
) {
779 app_conf_try_string(extfile_conf
, "default", "extensions");
780 if (extensions
== NULL
)
781 extensions
= "default";
785 /*****************************************************************/
787 if (spkac_file
!= NULL
&& outfile
!= NULL
) {
793 def_ret
= EVP_PKEY_get_default_digest_name(pkey
, def_dgst
, sizeof(def_dgst
));
795 * EVP_PKEY_get_default_digest_name() returns 2 if the digest is
796 * mandatory for this algorithm.
798 * That call may give back the name "UNDEF", which has these meanings:
800 * when def_ret == 2: the user MUST leave the digest unspecified
801 * when def_ret == 1: the user MAY leave the digest unspecified
803 if (def_ret
== 2 && strcmp(def_dgst
, "UNDEF") == 0) {
805 } else if (dgst
== NULL
806 && (dgst
= lookup_conf(conf
, section
, ENV_DEFAULT_MD
)) == NULL
807 && strcmp(def_dgst
, "UNDEF") != 0) {
810 if (strcmp(dgst
, "default") == 0 || strcmp(def_dgst
, "UNDEF") == 0) {
812 BIO_puts(bio_err
, "no default digest\n");
821 char *tmp_email_dn
= NULL
;
824 app_conf_try_string(conf
, section
, ENV_DEFAULT_EMAIL_DN
);
825 if (tmp_email_dn
!= NULL
&& strcmp(tmp_email_dn
, "no") == 0)
829 BIO_printf(bio_err
, "message digest is %s\n", dgst
);
831 && (policy
= lookup_conf(conf
, section
, ENV_POLICY
)) == NULL
)
835 BIO_printf(bio_err
, "policy is %s\n", policy
);
837 if (app_conf_try_string(conf
, section
, ENV_RAND_SERIAL
) != NULL
) {
840 serialfile
= lookup_conf(conf
, section
, ENV_SERIAL
);
841 if (serialfile
== NULL
)
845 if (extfile_conf
!= NULL
) {
846 /* Check syntax of extfile */
849 X509V3_set_ctx_test(&ctx
);
850 X509V3_set_nconf(&ctx
, extfile_conf
);
851 if (!X509V3_EXT_add_nconf(extfile_conf
, &ctx
, extensions
, NULL
)) {
853 "Error checking certificate extensions from extfile section %s\n",
860 * no '-extfile' option, so we look for extensions in the main
863 if (extensions
== NULL
)
864 extensions
= app_conf_try_string(conf
, section
, ENV_EXTENSIONS
);
865 if (extensions
!= NULL
) {
866 /* Check syntax of config file section */
869 X509V3_set_ctx_test(&ctx
);
870 X509V3_set_nconf(&ctx
, conf
);
871 if (!X509V3_EXT_add_nconf(conf
, &ctx
, extensions
, NULL
)) {
873 "Error checking certificate extension config section %s\n",
881 if (startdate
== NULL
)
883 app_conf_try_string(conf
, section
, ENV_DEFAULT_STARTDATE
);
885 enddate
= app_conf_try_string(conf
, section
, ENV_DEFAULT_ENDDATE
);
887 if (!app_conf_try_number(conf
, section
, ENV_DEFAULT_DAYS
, &days
))
890 if (enddate
== NULL
&& days
== 0) {
891 BIO_printf(bio_err
, "cannot lookup how many days to certify for\n");
894 if (days
!= 0 && enddate
!= NULL
)
896 "Warning: -enddate or -not_after option overriding -days option\n");
899 if ((serial
= BN_new()) == NULL
|| !rand_serial(serial
, NULL
)) {
900 BIO_printf(bio_err
, "error generating serial number\n");
904 serial
= load_serial(serialfile
, NULL
, create_ser
, NULL
);
905 if (serial
== NULL
) {
906 BIO_printf(bio_err
, "error while loading serial number\n");
910 if (BN_is_zero(serial
)) {
911 BIO_printf(bio_err
, "next serial number is 00\n");
913 if ((f
= BN_bn2hex(serial
)) == NULL
)
915 BIO_printf(bio_err
, "next serial number is %s\n", f
);
921 if ((attribs
= NCONF_get_section(conf
, policy
)) == NULL
) {
922 BIO_printf(bio_err
, "unable to find 'section' for %s\n", policy
);
926 if ((cert_sk
= sk_X509_new_null()) == NULL
) {
927 BIO_printf(bio_err
, "Memory allocation failure\n");
930 if (spkac_file
!= NULL
) {
932 j
= certify_spkac(&x
, spkac_file
, pkey
, x509
, dgst
, sigopts
,
933 attribs
, db
, serial
, subj
, chtype
, multirdn
,
934 email_dn
, startdate
, enddate
, days
, extensions
,
935 conf
, verbose
, certopt
, get_nameopt(), default_op
,
941 BIO_printf(bio_err
, "\n");
942 if (!BN_add_word(serial
, 1))
944 if (!sk_X509_push(cert_sk
, x
)) {
945 BIO_printf(bio_err
, "Memory allocation failure\n");
950 if (ss_cert_file
!= NULL
) {
952 j
= certify_cert(&x
, ss_cert_file
, certformat
, passin
, pkey
,
953 x509
, dgst
, sigopts
, vfyopts
, attribs
,
954 db
, serial
, subj
, chtype
, multirdn
, email_dn
,
955 startdate
, enddate
, days
, batch
, extensions
,
956 conf
, verbose
, certopt
, get_nameopt(), default_op
,
962 BIO_printf(bio_err
, "\n");
963 if (!BN_add_word(serial
, 1))
965 if (!sk_X509_push(cert_sk
, x
)) {
966 BIO_printf(bio_err
, "Memory allocation failure\n");
971 if (infile
!= NULL
) {
973 j
= certify(&x
, infile
, informat
, pkey
, x509p
, dgst
,
974 sigopts
, vfyopts
, attribs
, db
,
975 serial
, subj
, chtype
, multirdn
, email_dn
, startdate
,
976 enddate
, days
, batch
, extensions
, conf
, verbose
,
977 certopt
, get_nameopt(), default_op
, ext_copy
, selfsign
, dateopt
);
982 BIO_printf(bio_err
, "\n");
983 if (!BN_add_word(serial
, 1))
985 if (!sk_X509_push(cert_sk
, x
)) {
986 BIO_printf(bio_err
, "Memory allocation failure\n");
991 for (i
= 0; i
< argc
; i
++) {
993 j
= certify(&x
, argv
[i
], informat
, pkey
, x509p
, dgst
,
996 serial
, subj
, chtype
, multirdn
, email_dn
, startdate
,
997 enddate
, days
, batch
, extensions
, conf
, verbose
,
998 certopt
, get_nameopt(), default_op
, ext_copy
, selfsign
, dateopt
);
1003 BIO_printf(bio_err
, "\n");
1004 if (!BN_add_word(serial
, 1)) {
1008 if (!sk_X509_push(cert_sk
, x
)) {
1009 BIO_printf(bio_err
, "Memory allocation failure\n");
1016 * we have a stack of newly certified certificates and a database
1017 * and serial number that need updating
1020 if (sk_X509_num(cert_sk
) > 0) {
1023 "\n%d out of %d certificate requests certified, commit? [y/n]",
1025 (void)BIO_flush(bio_err
);
1027 if (fgets(tmp
, sizeof(tmp
), stdin
) == NULL
) {
1028 BIO_printf(bio_err
, "CERTIFICATION CANCELED: I/O error\n");
1032 if (tmp
[0] != 'y' && tmp
[0] != 'Y') {
1033 BIO_printf(bio_err
, "CERTIFICATION CANCELED\n");
1039 BIO_printf(bio_err
, "Write out database with %d new entries\n",
1040 sk_X509_num(cert_sk
));
1042 if (serialfile
!= NULL
1043 && !save_serial(serialfile
, "new", serial
, NULL
))
1046 if (!save_index(dbfile
, "new", db
))
1050 outdirlen
= OPENSSL_strlcpy(new_cert
, outdir
, sizeof(new_cert
));
1051 #ifndef OPENSSL_SYS_VMS
1052 outdirlen
= OPENSSL_strlcat(new_cert
, "/", sizeof(new_cert
));
1056 BIO_printf(bio_err
, "writing new certificates\n");
1058 for (i
= 0; i
< sk_X509_num(cert_sk
); i
++) {
1060 X509
*xi
= sk_X509_value(cert_sk
, i
);
1061 const ASN1_INTEGER
*serialNumber
= X509_get0_serialNumber(xi
);
1062 const unsigned char *psn
= ASN1_STRING_get0_data(serialNumber
);
1063 const int snl
= ASN1_STRING_length(serialNumber
);
1064 const int filen_len
= 2 * (snl
> 0 ? snl
: 1) + sizeof(".pem");
1065 char *n
= new_cert
+ outdirlen
;
1067 if (outdirlen
+ filen_len
> PATH_MAX
) {
1068 BIO_printf(bio_err
, "certificate file name too long\n");
1073 static const char HEX_DIGITS
[] = "0123456789ABCDEF";
1075 for (j
= 0; j
< snl
; j
++, psn
++) {
1076 *n
++ = HEX_DIGITS
[*psn
>> 4];
1077 *n
++ = HEX_DIGITS
[*psn
& 0x0F];
1087 *n
= '\0'; /* closing new_cert */
1089 BIO_printf(bio_err
, "writing %s\n", new_cert
);
1091 Sout
= bio_open_default(outfile
, 'w',
1092 output_der
? FORMAT_ASN1
: FORMAT_TEXT
);
1096 Cout
= BIO_new_file(new_cert
, "w");
1101 write_new_certificate(Cout
, xi
, 0, notext
);
1102 write_new_certificate(Sout
, xi
, output_der
, notext
);
1108 if (sk_X509_num(cert_sk
)) {
1109 /* Rename the database and the serial file */
1110 if (serialfile
!= NULL
1111 && !rotate_serial(serialfile
, "new", "old"))
1114 if (!rotate_index(dbfile
, "new", "old"))
1117 BIO_printf(bio_err
, "Database updated\n");
1121 /*****************************************************************/
1125 if (crl_ext
== NULL
)
1126 crl_ext
= app_conf_try_string(conf
, section
, ENV_CRLEXT
);
1127 if (crl_ext
!= NULL
) {
1128 /* Check syntax of file */
1131 X509V3_set_ctx_test(&ctx
);
1132 X509V3_set_nconf(&ctx
, conf
);
1133 if (!X509V3_EXT_add_nconf(conf
, &ctx
, crl_ext
, NULL
)) {
1135 "Error checking CRL extension section %s\n", crl_ext
);
1141 crlnumberfile
= app_conf_try_string(conf
, section
, ENV_CRLNUMBER
);
1142 if (crlnumberfile
!= NULL
) {
1143 if ((crlnumber
= load_serial(crlnumberfile
, NULL
, 0, NULL
))
1145 BIO_printf(bio_err
, "error while loading CRL number\n");
1150 if (!crldays
&& !crlhours
&& !crlsec
) {
1151 if (!app_conf_try_number(conf
, section
,
1152 ENV_DEFAULT_CRL_DAYS
, &crldays
))
1154 if (!app_conf_try_number(conf
, section
,
1155 ENV_DEFAULT_CRL_HOURS
, &crlhours
))
1158 if ((crl_nextupdate
== NULL
) &&
1159 (crldays
== 0) && (crlhours
== 0) && (crlsec
== 0)) {
1161 "cannot lookup how long until the next CRL is issued\n");
1166 BIO_printf(bio_err
, "making CRL\n");
1167 if ((crl
= X509_CRL_new_ex(app_get0_libctx(), app_get0_propq())) == NULL
)
1169 if (!X509_CRL_set_issuer_name(crl
, X509_get_subject_name(x509
)))
1172 if (!set_crl_lastupdate(crl
, crl_lastupdate
)) {
1173 BIO_puts(bio_err
, "error setting CRL lastUpdate\n");
1178 if (!set_crl_nextupdate(crl
, crl_nextupdate
,
1179 crldays
, crlhours
, crlsec
)) {
1180 BIO_puts(bio_err
, "error setting CRL nextUpdate\n");
1185 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(db
->db
->data
); i
++) {
1186 pp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, i
);
1187 if (pp
[DB_type
][0] == DB_TYPE_REV
) {
1188 if ((r
= X509_REVOKED_new()) == NULL
)
1190 j
= make_revoked(r
, pp
[DB_rev_date
]);
1195 if (!BN_hex2bn(&serial
, pp
[DB_serial
]))
1197 tmpser
= BN_to_ASN1_INTEGER(serial
, NULL
);
1202 X509_REVOKED_set_serialNumber(r
, tmpser
);
1203 ASN1_INTEGER_free(tmpser
);
1204 X509_CRL_add0_revoked(crl
, r
);
1209 * sort the data so it will be written in serial number order
1213 /* we now have a CRL */
1215 BIO_printf(bio_err
, "signing CRL\n");
1217 /* Add any extensions asked for */
1219 if (crl_ext
!= NULL
|| crlnumberfile
!= NULL
) {
1222 X509V3_set_ctx(&crlctx
, x509
, NULL
, NULL
, crl
, 0);
1223 X509V3_set_nconf(&crlctx
, conf
);
1225 if (crl_ext
!= NULL
)
1226 if (!X509V3_EXT_CRL_add_nconf(conf
, &crlctx
, crl_ext
, crl
)) {
1228 "Error adding CRL extensions from section %s\n", crl_ext
);
1231 if (crlnumberfile
!= NULL
) {
1232 tmpser
= BN_to_ASN1_INTEGER(crlnumber
, NULL
);
1235 X509_CRL_add1_ext_i2d(crl
, NID_crl_number
, tmpser
, 0, 0);
1236 ASN1_INTEGER_free(tmpser
);
1238 if (!BN_add_word(crlnumber
, 1))
1242 if (crl_ext
!= NULL
|| crl_v2
) {
1243 if (!X509_CRL_set_version(crl
, X509_CRL_VERSION_2
))
1247 /* we have a CRL number that need updating */
1248 if (crlnumberfile
!= NULL
1249 && !save_serial(crlnumberfile
, "new", crlnumber
, NULL
))
1255 if (!do_X509_CRL_sign(crl
, pkey
, dgst
, sigopts
))
1258 Sout
= bio_open_default(outfile
, 'w',
1259 output_der
? FORMAT_ASN1
: FORMAT_TEXT
);
1263 PEM_write_bio_X509_CRL(Sout
, crl
);
1265 /* Rename the crlnumber file */
1266 if (crlnumberfile
!= NULL
1267 && !rotate_serial(crlnumberfile
, "new", "old"))
1271 /*****************************************************************/
1273 if (infile
== NULL
) {
1274 BIO_printf(bio_err
, "no input files\n");
1279 revcert
= load_cert_pass(infile
, informat
, 1, passin
,
1280 "certificate to be revoked");
1281 if (revcert
== NULL
)
1284 rev_type
= REV_VALID
;
1285 j
= do_revoke(revcert
, db
, rev_type
, rev_arg
);
1290 if (!save_index(dbfile
, "new", db
))
1293 if (!rotate_index(dbfile
, "new", "old"))
1296 BIO_printf(bio_err
, "Database updated\n");
1303 ERR_print_errors(bio_err
);
1307 OSSL_STACK_OF_X509_free(cert_sk
);
1311 OPENSSL_free(passin
);
1315 sk_OPENSSL_STRING_free(sigopts
);
1316 sk_OPENSSL_STRING_free(vfyopts
);
1317 EVP_PKEY_free(pkey
);
1321 NCONF_free(extfile_conf
);
1326 static char *lookup_conf(const CONF
*conf
, const char *section
, const char *tag
)
1328 char *entry
= NCONF_get_string(conf
, section
, tag
);
1330 BIO_printf(bio_err
, "variable lookup failed for %s::%s\n", section
, tag
);
1334 static int certify(X509
**xret
, const char *infile
, int informat
,
1335 EVP_PKEY
*pkey
, X509
*x509
,
1337 STACK_OF(OPENSSL_STRING
) *sigopts
,
1338 STACK_OF(OPENSSL_STRING
) *vfyopts
,
1339 STACK_OF(CONF_VALUE
) *policy
, CA_DB
*db
,
1340 BIGNUM
*serial
, const char *subj
, unsigned long chtype
,
1341 int multirdn
, int email_dn
, const char *startdate
,
1342 const char *enddate
,
1343 long days
, int batch
, const char *ext_sect
, CONF
*lconf
,
1344 int verbose
, unsigned long certopt
, unsigned long nameopt
,
1345 int default_op
, int ext_copy
, int selfsign
, unsigned long dateopt
)
1347 X509_REQ
*req
= NULL
;
1348 EVP_PKEY
*pktmp
= NULL
;
1351 req
= load_csr_autofmt(infile
, informat
, vfyopts
, "certificate request");
1354 if ((pktmp
= X509_REQ_get0_pubkey(req
)) == NULL
) {
1355 BIO_printf(bio_err
, "Error unpacking public key\n");
1359 X509_REQ_print_ex(bio_err
, req
, nameopt
, X509_FLAG_COMPAT
);
1361 BIO_printf(bio_err
, "Check that the request matches the signature\n");
1364 if (selfsign
&& !X509_REQ_check_private_key(req
, pkey
)) {
1366 "Certificate request and CA private key do not match\n");
1369 i
= do_X509_REQ_verify(req
, pktmp
, vfyopts
);
1371 BIO_printf(bio_err
, "Signature verification problems...\n");
1376 "Signature did not match the certificate request\n");
1379 BIO_printf(bio_err
, "Signature ok\n");
1381 ok
= do_body(xret
, pkey
, x509
, dgst
, sigopts
, policy
, db
, serial
, subj
,
1382 chtype
, multirdn
, email_dn
, startdate
, enddate
, days
, batch
,
1383 verbose
, req
, ext_sect
, lconf
, certopt
, nameopt
, default_op
,
1384 ext_copy
, selfsign
, dateopt
);
1387 ERR_print_errors(bio_err
);
1392 static int certify_cert(X509
**xret
, const char *infile
, int certformat
,
1393 const char *passin
, EVP_PKEY
*pkey
, X509
*x509
,
1395 STACK_OF(OPENSSL_STRING
) *sigopts
,
1396 STACK_OF(OPENSSL_STRING
) *vfyopts
,
1397 STACK_OF(CONF_VALUE
) *policy
, CA_DB
*db
,
1398 BIGNUM
*serial
, const char *subj
, unsigned long chtype
,
1399 int multirdn
, int email_dn
, const char *startdate
,
1400 const char *enddate
, long days
, int batch
, const char *ext_sect
,
1401 CONF
*lconf
, int verbose
, unsigned long certopt
,
1402 unsigned long nameopt
, int default_op
, int ext_copy
, unsigned long dateopt
)
1404 X509
*template_cert
= NULL
;
1405 X509_REQ
*rreq
= NULL
;
1406 EVP_PKEY
*pktmp
= NULL
;
1409 if ((template_cert
= load_cert_pass(infile
, certformat
, 1, passin
,
1410 "template certificate")) == NULL
)
1413 X509_print(bio_err
, template_cert
);
1415 BIO_printf(bio_err
, "Check that the request matches the signature\n");
1417 if ((pktmp
= X509_get0_pubkey(template_cert
)) == NULL
) {
1418 BIO_printf(bio_err
, "error unpacking public key\n");
1421 i
= do_X509_verify(template_cert
, pktmp
, vfyopts
);
1424 BIO_printf(bio_err
, "Signature verification problems....\n");
1429 BIO_printf(bio_err
, "Signature did not match the certificate\n");
1432 BIO_printf(bio_err
, "Signature ok\n");
1435 if ((rreq
= X509_to_X509_REQ(template_cert
, NULL
, NULL
)) == NULL
)
1438 ok
= do_body(xret
, pkey
, x509
, dgst
, sigopts
, policy
, db
, serial
, subj
,
1439 chtype
, multirdn
, email_dn
, startdate
, enddate
, days
, batch
,
1440 verbose
, rreq
, ext_sect
, lconf
, certopt
, nameopt
, default_op
,
1441 ext_copy
, 0, dateopt
);
1444 X509_REQ_free(rreq
);
1445 X509_free(template_cert
);
1449 static int do_body(X509
**xret
, EVP_PKEY
*pkey
, X509
*x509
,
1450 const char *dgst
, STACK_OF(OPENSSL_STRING
) *sigopts
,
1451 STACK_OF(CONF_VALUE
) *policy
, CA_DB
*db
, BIGNUM
*serial
,
1452 const char *subj
, unsigned long chtype
, int multirdn
,
1453 int email_dn
, const char *startdate
, const char *enddate
, long days
,
1454 int batch
, int verbose
, X509_REQ
*req
, const char *ext_sect
,
1455 CONF
*lconf
, unsigned long certopt
, unsigned long nameopt
,
1456 int default_op
, int ext_copy
, int selfsign
, unsigned long dateopt
)
1458 const X509_NAME
*name
= NULL
;
1459 X509_NAME
*CAname
= NULL
, *subject
= NULL
;
1460 const ASN1_TIME
*tm
;
1461 ASN1_STRING
*str
, *str2
;
1464 X509_NAME_ENTRY
*ne
, *tne
;
1466 int ok
= -1, i
, j
, last
, nid
;
1469 OPENSSL_STRING row
[DB_NUMBER
];
1470 OPENSSL_STRING
*irow
= NULL
;
1471 OPENSSL_STRING
*rrow
= NULL
;
1475 for (i
= 0; i
< DB_NUMBER
; i
++)
1479 X509_NAME
*n
= parse_name(subj
, chtype
, multirdn
, "subject");
1483 X509_REQ_set_subject_name(req
, n
);
1488 BIO_printf(bio_err
, "The Subject's Distinguished Name is as follows\n");
1490 name
= X509_REQ_get_subject_name(req
);
1491 for (i
= 0; i
< X509_NAME_entry_count(name
); i
++) {
1492 ne
= X509_NAME_get_entry(name
, i
);
1493 str
= X509_NAME_ENTRY_get_data(ne
);
1494 obj
= X509_NAME_ENTRY_get_object(ne
);
1495 nid
= OBJ_obj2nid(obj
);
1498 /* assume all type should be strings */
1500 if (str
->type
== V_ASN1_UNIVERSALSTRING
)
1501 ASN1_UNIVERSALSTRING_to_string(str
);
1503 if (str
->type
== V_ASN1_IA5STRING
&& nid
!= NID_pkcs9_emailAddress
)
1504 str
->type
= V_ASN1_T61STRING
;
1506 if (nid
== NID_pkcs9_emailAddress
1507 && str
->type
== V_ASN1_PRINTABLESTRING
)
1508 str
->type
= V_ASN1_IA5STRING
;
1511 /* If no EMAIL is wanted in the subject */
1512 if (nid
== NID_pkcs9_emailAddress
&& !email_dn
)
1515 /* check some things */
1516 if (nid
== NID_pkcs9_emailAddress
&& str
->type
!= V_ASN1_IA5STRING
) {
1518 "\nemailAddress type needs to be of type IA5STRING\n");
1521 if (str
->type
!= V_ASN1_BMPSTRING
&& str
->type
!= V_ASN1_UTF8STRING
) {
1522 j
= ASN1_PRINTABLE_type(str
->data
, str
->length
);
1523 if ((j
== V_ASN1_T61STRING
&& str
->type
!= V_ASN1_T61STRING
) ||
1524 (j
== V_ASN1_IA5STRING
&& str
->type
== V_ASN1_PRINTABLESTRING
))
1527 "\nThe string contains characters that are illegal for the ASN.1 type\n");
1533 old_entry_print(obj
, str
);
1536 /* Ok, now we check the 'policy' stuff. */
1537 if ((subject
= X509_NAME_new()) == NULL
) {
1538 BIO_printf(bio_err
, "Memory allocation failure\n");
1542 /* take a copy of the issuer name before we mess with it. */
1544 CAname
= X509_NAME_dup(name
);
1546 CAname
= X509_NAME_dup(X509_get_subject_name(x509
));
1551 for (i
= 0; i
< sk_CONF_VALUE_num(policy
); i
++) {
1552 cv
= sk_CONF_VALUE_value(policy
, i
); /* get the object id */
1553 if ((j
= OBJ_txt2nid(cv
->name
)) == NID_undef
) {
1555 "%s:unknown object type in 'policy' configuration\n",
1559 obj
= OBJ_nid2obj(j
);
1563 X509_NAME_ENTRY
*push
= NULL
;
1565 /* lookup the object in the supplied name list */
1566 j
= X509_NAME_get_index_by_OBJ(name
, obj
, last
);
1572 tne
= X509_NAME_get_entry(name
, j
);
1576 /* depending on the 'policy', decide what to do. */
1577 if (strcmp(cv
->value
, "optional") == 0) {
1580 } else if (strcmp(cv
->value
, "supplied") == 0) {
1583 "The %s field needed to be supplied and was missing\n",
1589 } else if (strcmp(cv
->value
, "match") == 0) {
1594 "The mandatory %s field was missing\n",
1602 j
= X509_NAME_get_index_by_OBJ(CAname
, obj
, last2
);
1603 if ((j
< 0) && (last2
== -1)) {
1605 "The %s field does not exist in the CA certificate,\n"
1606 "the 'policy' is misconfigured\n", cv
->name
);
1610 push
= X509_NAME_get_entry(CAname
, j
);
1611 str
= X509_NAME_ENTRY_get_data(tne
);
1612 str2
= X509_NAME_ENTRY_get_data(push
);
1614 if (ASN1_STRING_cmp(str
, str2
) != 0)
1619 "The %s field is different between\n"
1620 "CA certificate (%s) and the request (%s)\n",
1622 ((str2
== NULL
) ? "NULL" : (char *)str2
->data
),
1623 ((str
== NULL
) ? "NULL" : (char *)str
->data
));
1628 "%s:invalid type in 'policy' configuration\n",
1634 if (!X509_NAME_add_entry(subject
, push
, -1, 0)) {
1635 BIO_printf(bio_err
, "Memory allocation failure\n");
1645 X509_NAME_free(subject
);
1646 /* subject=X509_NAME_dup(X509_REQ_get_subject_name(req)); */
1647 subject
= X509_NAME_dup(name
);
1648 if (subject
== NULL
)
1652 /* We are now totally happy, lets make and sign the certificate */
1655 "Everything appears to be ok, creating and signing the certificate\n");
1657 if ((ret
= X509_new_ex(app_get0_libctx(), app_get0_propq())) == NULL
)
1660 if (BN_to_ASN1_INTEGER(serial
, X509_get_serialNumber(ret
)) == NULL
)
1663 if (!X509_set_issuer_name(ret
, subject
))
1666 if (!X509_set_issuer_name(ret
, X509_get_subject_name(x509
)))
1670 if (!set_cert_times(ret
, startdate
, enddate
, days
, 0))
1673 if (enddate
!= NULL
) {
1676 if (!ASN1_TIME_diff(&tdays
, NULL
, NULL
, X509_get0_notAfter(ret
)))
1681 if (!X509_set_subject_name(ret
, subject
))
1684 pktmp
= X509_REQ_get0_pubkey(req
);
1685 i
= X509_set_pubkey(ret
, pktmp
);
1689 /* Initialize the context structure */
1690 X509V3_set_ctx(&ext_ctx
, selfsign
? ret
: x509
,
1691 ret
, NULL
/* no need to give req, needed info is in ret */,
1692 NULL
, X509V3_CTX_REPLACE
);
1693 /* prepare fallback for AKID, but only if issuer cert equals subject cert */
1695 if (!X509V3_set_issuer_pkey(&ext_ctx
, pkey
))
1697 if (!cert_matches_key(ret
, pkey
))
1699 "Warning: Signature key and public key of cert do not match\n");
1702 /* Lets add the extensions, if there are any */
1704 if (extfile_conf
!= NULL
) {
1706 BIO_printf(bio_err
, "Extra configuration file found\n");
1708 /* Use the extfile_conf configuration db LHASH */
1709 X509V3_set_nconf(&ext_ctx
, extfile_conf
);
1711 /* Adds exts contained in the configuration file */
1712 if (!X509V3_EXT_add_nconf(extfile_conf
, &ext_ctx
, ext_sect
, ret
)) {
1714 "Error adding certificate extensions from extfile section %s\n",
1720 "Successfully added extensions from file.\n");
1721 } else if (ext_sect
) {
1722 /* We found extensions to be set from config file */
1723 X509V3_set_nconf(&ext_ctx
, lconf
);
1725 if (!X509V3_EXT_add_nconf(lconf
, &ext_ctx
, ext_sect
, ret
)) {
1727 "Error adding certificate extensions from config section %s\n",
1734 "Successfully added extensions from config\n");
1738 /* Copy extensions from request (if any) */
1740 if (!copy_extensions(ret
, req
, ext_copy
)) {
1741 BIO_printf(bio_err
, "ERROR: adding extensions from request\n");
1747 "The subject name appears to be ok, checking database for clashes\n");
1749 /* Build the correct Subject if no e-mail is wanted in the subject. */
1751 X509_NAME_ENTRY
*tmpne
;
1752 X509_NAME
*dn_subject
;
1755 * Its best to dup the subject DN and then delete any email addresses
1756 * because this retains its structure.
1758 if ((dn_subject
= X509_NAME_dup(subject
)) == NULL
) {
1759 BIO_printf(bio_err
, "Memory allocation failure\n");
1763 while ((i
= X509_NAME_get_index_by_NID(dn_subject
,
1764 NID_pkcs9_emailAddress
,
1766 tmpne
= X509_NAME_delete_entry(dn_subject
, i
--);
1767 X509_NAME_ENTRY_free(tmpne
);
1770 if (!X509_set_subject_name(ret
, dn_subject
)) {
1771 X509_NAME_free(dn_subject
);
1774 X509_NAME_free(dn_subject
);
1777 row
[DB_name
] = X509_NAME_oneline(X509_get_subject_name(ret
), NULL
, 0);
1778 if (row
[DB_name
] == NULL
) {
1779 BIO_printf(bio_err
, "Memory allocation failure\n");
1783 if (BN_is_zero(serial
))
1784 row
[DB_serial
] = OPENSSL_strdup("00");
1786 row
[DB_serial
] = BN_bn2hex(serial
);
1787 if (row
[DB_serial
] == NULL
) {
1788 BIO_printf(bio_err
, "Memory allocation failure\n");
1792 if (row
[DB_name
][0] == '\0') {
1794 * An empty subject! We'll use the serial number instead. If
1795 * unique_subject is in use then we don't want different entries with
1796 * empty subjects matching each other.
1798 OPENSSL_free(row
[DB_name
]);
1799 row
[DB_name
] = OPENSSL_strdup(row
[DB_serial
]);
1800 if (row
[DB_name
] == NULL
) {
1801 BIO_printf(bio_err
, "Memory allocation failure\n");
1806 if (db
->attributes
.unique_subject
) {
1807 OPENSSL_STRING
*crow
= row
;
1809 rrow
= TXT_DB_get_by_index(db
->db
, DB_name
, crow
);
1812 "ERROR:There is already a certificate for %s\n",
1817 rrow
= TXT_DB_get_by_index(db
->db
, DB_serial
, row
);
1820 "ERROR:Serial number %s has already been issued,\n",
1823 " check the database/serial_file for corruption\n");
1828 BIO_printf(bio_err
, "The matching entry has the following details\n");
1829 if (rrow
[DB_type
][0] == DB_TYPE_EXP
)
1831 else if (rrow
[DB_type
][0] == DB_TYPE_REV
)
1833 else if (rrow
[DB_type
][0] == DB_TYPE_VAL
)
1836 p
= "\ninvalid type, Database error\n";
1837 BIO_printf(bio_err
, "Type :%s\n", p
);
1838 if (rrow
[DB_type
][0] == DB_TYPE_REV
) {
1839 p
= rrow
[DB_exp_date
];
1842 BIO_printf(bio_err
, "Was revoked on:%s\n", p
);
1844 p
= rrow
[DB_exp_date
];
1847 BIO_printf(bio_err
, "Expires on :%s\n", p
);
1848 p
= rrow
[DB_serial
];
1851 BIO_printf(bio_err
, "Serial Number :%s\n", p
);
1855 BIO_printf(bio_err
, "File name :%s\n", p
);
1859 BIO_printf(bio_err
, "Subject Name :%s\n", p
);
1860 ok
= -1; /* This is now a 'bad' error. */
1865 BIO_printf(bio_err
, "Certificate Details:\n");
1867 * Never print signature details because signature not present
1869 certopt
|= X509_FLAG_NO_SIGDUMP
| X509_FLAG_NO_SIGNAME
;
1870 X509_print_ex(bio_err
, ret
, nameopt
, certopt
);
1873 BIO_printf(bio_err
, "Certificate is to be certified until ");
1874 ASN1_TIME_print_ex(bio_err
, X509_get0_notAfter(ret
), dateopt
);
1876 BIO_printf(bio_err
, " (%ld days)", days
);
1877 BIO_printf(bio_err
, "\n");
1881 BIO_printf(bio_err
, "Sign the certificate? [y/n]:");
1882 (void)BIO_flush(bio_err
);
1884 if (fgets(buf
, sizeof(buf
), stdin
) == NULL
) {
1886 "CERTIFICATE WILL NOT BE CERTIFIED: I/O error\n");
1890 if (!(buf
[0] == 'y' || buf
[0] == 'Y')) {
1891 BIO_printf(bio_err
, "CERTIFICATE WILL NOT BE CERTIFIED\n");
1897 pktmp
= X509_get0_pubkey(ret
);
1898 if (EVP_PKEY_missing_parameters(pktmp
) &&
1899 !EVP_PKEY_missing_parameters(pkey
))
1900 EVP_PKEY_copy_parameters(pktmp
, pkey
);
1902 if (!do_X509_sign(ret
, 0, pkey
, dgst
, sigopts
, &ext_ctx
))
1905 /* We now just add it to the database as DB_TYPE_VAL('V') */
1906 row
[DB_type
] = OPENSSL_strdup("V");
1907 tm
= X509_get0_notAfter(ret
);
1908 row
[DB_exp_date
] = app_malloc(tm
->length
+ 1, "row expdate");
1909 memcpy(row
[DB_exp_date
], tm
->data
, tm
->length
);
1910 row
[DB_exp_date
][tm
->length
] = '\0';
1911 row
[DB_rev_date
] = NULL
;
1912 row
[DB_file
] = OPENSSL_strdup("unknown");
1913 if ((row
[DB_type
] == NULL
) || (row
[DB_file
] == NULL
)
1914 || (row
[DB_name
] == NULL
)) {
1915 BIO_printf(bio_err
, "Memory allocation failure\n");
1919 irow
= app_malloc(sizeof(*irow
) * (DB_NUMBER
+ 1), "row space");
1920 for (i
= 0; i
< DB_NUMBER
; i
++)
1922 irow
[DB_NUMBER
] = NULL
;
1924 if (!TXT_DB_insert(db
->db
, irow
)) {
1925 BIO_printf(bio_err
, "failed to update database\n");
1926 BIO_printf(bio_err
, "TXT_DB error number %ld\n", db
->db
->error
);
1933 for (i
= 0; i
< DB_NUMBER
; i
++)
1934 OPENSSL_free(row
[i
]);
1938 X509_NAME_free(CAname
);
1939 X509_NAME_free(subject
);
1947 static void write_new_certificate(BIO
*bp
, X509
*x
, int output_der
, int notext
)
1951 (void)i2d_X509_bio(bp
, x
);
1956 PEM_write_bio_X509(bp
, x
);
1959 static int certify_spkac(X509
**xret
, const char *infile
, EVP_PKEY
*pkey
,
1960 X509
*x509
, const char *dgst
,
1961 STACK_OF(OPENSSL_STRING
) *sigopts
,
1962 STACK_OF(CONF_VALUE
) *policy
, CA_DB
*db
,
1963 BIGNUM
*serial
, const char *subj
, unsigned long chtype
,
1964 int multirdn
, int email_dn
, const char *startdate
,
1965 const char *enddate
, long days
, const char *ext_sect
,
1966 CONF
*lconf
, int verbose
, unsigned long certopt
,
1967 unsigned long nameopt
, int default_op
, int ext_copy
, unsigned long dateopt
)
1969 STACK_OF(CONF_VALUE
) *sk
= NULL
;
1970 LHASH_OF(CONF_VALUE
) *parms
= NULL
;
1971 X509_REQ
*req
= NULL
;
1972 CONF_VALUE
*cv
= NULL
;
1973 NETSCAPE_SPKI
*spki
= NULL
;
1975 EVP_PKEY
*pktmp
= NULL
;
1976 X509_NAME
*n
= NULL
;
1977 X509_NAME_ENTRY
*ne
= NULL
;
1983 * Load input file into a hash table. (This is just an easy
1984 * way to read and parse the file, then put it into a convenient
1987 parms
= CONF_load(NULL
, infile
, &errline
);
1988 if (parms
== NULL
) {
1989 BIO_printf(bio_err
, "error on line %ld of %s\n", errline
, infile
);
1993 sk
= CONF_get_section(parms
, "default");
1994 if (sk_CONF_VALUE_num(sk
) == 0) {
1995 BIO_printf(bio_err
, "no name/value pairs found in %s\n", infile
);
2000 * Now create a dummy X509 request structure. We don't actually
2001 * have an X509 request, but we have many of the components
2002 * (a public key, various DN components). The idea is that we
2003 * put these components into the right X509 request structure
2004 * and we can use the same code as if you had a real X509 request.
2006 req
= X509_REQ_new();
2011 * Build up the subject name set.
2013 n
= X509_REQ_get_subject_name(req
);
2016 if (sk_CONF_VALUE_num(sk
) <= i
)
2019 cv
= sk_CONF_VALUE_value(sk
, i
);
2022 * Skip past any leading X. X: X, etc to allow for multiple instances
2024 for (buf
= cv
->name
; *buf
; buf
++)
2025 if ((*buf
== ':') || (*buf
== ',') || (*buf
== '.')) {
2033 if ((nid
= OBJ_txt2nid(type
)) == NID_undef
) {
2034 if (strcmp(type
, "SPKAC") == 0) {
2035 spki
= NETSCAPE_SPKI_b64_decode(cv
->value
, -1);
2038 "unable to load Netscape SPKAC structure\n");
2045 if (!X509_NAME_add_entry_by_NID(n
, nid
, chtype
,
2046 (unsigned char *)buf
, -1, -1, 0))
2050 BIO_printf(bio_err
, "Netscape SPKAC structure not found in %s\n",
2056 * Now extract the key from the SPKI structure.
2059 BIO_printf(bio_err
, "Check that the SPKAC request matches the signature\n");
2061 if ((pktmp
= NETSCAPE_SPKI_get_pubkey(spki
)) == NULL
) {
2062 BIO_printf(bio_err
, "error unpacking SPKAC public key\n");
2066 j
= NETSCAPE_SPKI_verify(spki
, pktmp
);
2068 EVP_PKEY_free(pktmp
);
2070 "signature verification failed on SPKAC public key\n");
2073 BIO_printf(bio_err
, "Signature ok\n");
2075 X509_REQ_set_pubkey(req
, pktmp
);
2076 EVP_PKEY_free(pktmp
);
2077 ok
= do_body(xret
, pkey
, x509
, dgst
, sigopts
, policy
, db
, serial
, subj
,
2078 chtype
, multirdn
, email_dn
, startdate
, enddate
, days
, 1,
2079 verbose
, req
, ext_sect
, lconf
, certopt
, nameopt
, default_op
,
2080 ext_copy
, 0, dateopt
);
2084 NETSCAPE_SPKI_free(spki
);
2085 X509_NAME_ENTRY_free(ne
);
2090 static int check_time_format(const char *str
)
2092 return ASN1_TIME_set_string(NULL
, str
);
2095 static int do_revoke(X509
*x509
, CA_DB
*db
, REVINFO_TYPE rev_type
,
2098 const ASN1_TIME
*tm
= NULL
;
2099 char *row
[DB_NUMBER
], **rrow
, **irow
;
2100 char *rev_str
= NULL
;
2104 for (i
= 0; i
< DB_NUMBER
; i
++)
2106 row
[DB_name
] = X509_NAME_oneline(X509_get_subject_name(x509
), NULL
, 0);
2107 bn
= ASN1_INTEGER_to_BN(X509_get0_serialNumber(x509
), NULL
);
2111 row
[DB_serial
] = OPENSSL_strdup("00");
2113 row
[DB_serial
] = BN_bn2hex(bn
);
2115 if (row
[DB_name
] != NULL
&& row
[DB_name
][0] == '\0') {
2116 /* Entries with empty Subjects actually use the serial number instead */
2117 OPENSSL_free(row
[DB_name
]);
2118 row
[DB_name
] = OPENSSL_strdup(row
[DB_serial
]);
2120 if ((row
[DB_name
] == NULL
) || (row
[DB_serial
] == NULL
)) {
2121 BIO_printf(bio_err
, "Memory allocation failure\n");
2125 * We have to lookup by serial number because name lookup skips revoked
2128 rrow
= TXT_DB_get_by_index(db
->db
, DB_serial
, row
);
2131 "Adding Entry with serial number %s to DB for %s\n",
2132 row
[DB_serial
], row
[DB_name
]);
2134 /* We now just add it to the database as DB_TYPE_REV('V') */
2135 row
[DB_type
] = OPENSSL_strdup("V");
2136 tm
= X509_get0_notAfter(x509
);
2137 row
[DB_exp_date
] = app_malloc(tm
->length
+ 1, "row exp_data");
2138 memcpy(row
[DB_exp_date
], tm
->data
, tm
->length
);
2139 row
[DB_exp_date
][tm
->length
] = '\0';
2140 row
[DB_rev_date
] = NULL
;
2141 row
[DB_file
] = OPENSSL_strdup("unknown");
2143 if (row
[DB_type
] == NULL
|| row
[DB_file
] == NULL
) {
2144 BIO_printf(bio_err
, "Memory allocation failure\n");
2148 irow
= app_malloc(sizeof(*irow
) * (DB_NUMBER
+ 1), "row ptr");
2149 for (i
= 0; i
< DB_NUMBER
; i
++)
2151 irow
[DB_NUMBER
] = NULL
;
2153 if (!TXT_DB_insert(db
->db
, irow
)) {
2154 BIO_printf(bio_err
, "failed to update database\n");
2155 BIO_printf(bio_err
, "TXT_DB error number %ld\n", db
->db
->error
);
2160 for (i
= 0; i
< DB_NUMBER
; i
++)
2163 /* Revoke Certificate */
2164 if (rev_type
== REV_VALID
)
2167 /* Retry revocation after DB insertion */
2168 ok
= do_revoke(x509
, db
, rev_type
, value
);
2172 } else if (index_name_cmp_noconst(row
, rrow
)) {
2173 BIO_printf(bio_err
, "ERROR:name does not match %s\n", row
[DB_name
]);
2175 } else if (rev_type
== REV_VALID
) {
2176 BIO_printf(bio_err
, "ERROR:Already present, serial number %s\n",
2179 } else if (rrow
[DB_type
][0] == DB_TYPE_REV
) {
2180 BIO_printf(bio_err
, "ERROR:Already revoked, serial number %s\n",
2184 BIO_printf(bio_err
, "Revoking Certificate %s.\n", rrow
[DB_serial
]);
2185 rev_str
= make_revocation_str(rev_type
, value
);
2187 BIO_printf(bio_err
, "Error in revocation arguments\n");
2190 rrow
[DB_type
][0] = DB_TYPE_REV
;
2191 rrow
[DB_type
][1] = '\0';
2192 rrow
[DB_rev_date
] = rev_str
;
2196 for (i
= 0; i
< DB_NUMBER
; i
++)
2197 OPENSSL_free(row
[i
]);
2201 static int get_certificate_status(const char *serial
, CA_DB
*db
)
2203 char *row
[DB_NUMBER
], **rrow
;
2205 size_t serial_len
= strlen(serial
);
2207 /* Free Resources */
2208 for (i
= 0; i
< DB_NUMBER
; i
++)
2211 /* Malloc needed char spaces */
2212 row
[DB_serial
] = app_malloc(serial_len
+ 2, "row serial#");
2214 if (serial_len
% 2) {
2216 * Set the first char to 0
2218 row
[DB_serial
][0] = '0';
2220 /* Copy String from serial to row[DB_serial] */
2221 memcpy(row
[DB_serial
] + 1, serial
, serial_len
);
2222 row
[DB_serial
][serial_len
+ 1] = '\0';
2224 /* Copy String from serial to row[DB_serial] */
2225 memcpy(row
[DB_serial
], serial
, serial_len
);
2226 row
[DB_serial
][serial_len
] = '\0';
2229 /* Make it Upper Case */
2230 make_uppercase(row
[DB_serial
]);
2234 /* Search for the certificate */
2235 rrow
= TXT_DB_get_by_index(db
->db
, DB_serial
, row
);
2237 BIO_printf(bio_err
, "Serial %s not present in db.\n", row
[DB_serial
]);
2240 } else if (rrow
[DB_type
][0] == DB_TYPE_VAL
) {
2241 BIO_printf(bio_err
, "%s=Valid (%c)\n",
2242 row
[DB_serial
], rrow
[DB_type
][0]);
2244 } else if (rrow
[DB_type
][0] == DB_TYPE_REV
) {
2245 BIO_printf(bio_err
, "%s=Revoked (%c)\n",
2246 row
[DB_serial
], rrow
[DB_type
][0]);
2248 } else if (rrow
[DB_type
][0] == DB_TYPE_EXP
) {
2249 BIO_printf(bio_err
, "%s=Expired (%c)\n",
2250 row
[DB_serial
], rrow
[DB_type
][0]);
2252 } else if (rrow
[DB_type
][0] == DB_TYPE_SUSP
) {
2253 BIO_printf(bio_err
, "%s=Suspended (%c)\n",
2254 row
[DB_serial
], rrow
[DB_type
][0]);
2257 BIO_printf(bio_err
, "%s=Unknown (%c).\n",
2258 row
[DB_serial
], rrow
[DB_type
][0]);
2262 for (i
= 0; i
< DB_NUMBER
; i
++) {
2263 OPENSSL_free(row
[i
]);
2268 int do_updatedb(CA_DB
*db
, time_t *now
)
2270 ASN1_TIME
*a_tm
= NULL
;
2274 a_tm
= ASN1_TIME_new();
2278 /* get actual time */
2279 if (X509_time_adj(a_tm
, 0, now
) == NULL
) {
2280 ASN1_TIME_free(a_tm
);
2284 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(db
->db
->data
); i
++) {
2285 rrow
= sk_OPENSSL_PSTRING_value(db
->db
->data
, i
);
2287 if (rrow
[DB_type
][0] == DB_TYPE_VAL
) {
2288 /* ignore entries that are not valid */
2289 ASN1_TIME
*exp_date
= NULL
;
2291 exp_date
= ASN1_TIME_new();
2292 if (exp_date
== NULL
) {
2293 ASN1_TIME_free(a_tm
);
2297 if (!ASN1_TIME_set_string(exp_date
, rrow
[DB_exp_date
])) {
2298 ASN1_TIME_free(a_tm
);
2299 ASN1_TIME_free(exp_date
);
2303 if (ASN1_TIME_compare(exp_date
, a_tm
) <= 0) {
2304 rrow
[DB_type
][0] = DB_TYPE_EXP
;
2305 rrow
[DB_type
][1] = '\0';
2308 BIO_printf(bio_err
, "%s=Expired\n", rrow
[DB_serial
]);
2310 ASN1_TIME_free(exp_date
);
2314 ASN1_TIME_free(a_tm
);
2318 static const char *crl_reasons
[] = {
2319 /* CRL reason strings */
2323 "affiliationChanged",
2325 "cessationOfOperation",
2328 /* Additional pseudo reasons */
2334 #define NUM_REASONS OSSL_NELEM(crl_reasons)
2337 * Given revocation information convert to a DB string. The format of the
2338 * string is: revtime[,reason,extra]. Where 'revtime' is the revocation time
2339 * (the current time). 'reason' is the optional CRL reason and 'extra' is any
2340 * additional argument
2343 static char *make_revocation_str(REVINFO_TYPE rev_type
, const char *rev_arg
)
2346 const char *reason
= NULL
, *other
= NULL
;
2348 ASN1_UTCTIME
*revtm
= NULL
;
2356 case REV_CRL_REASON
:
2357 for (i
= 0; i
< 8; i
++) {
2358 if (OPENSSL_strcasecmp(rev_arg
, crl_reasons
[i
]) == 0) {
2359 reason
= crl_reasons
[i
];
2363 if (reason
== NULL
) {
2364 BIO_printf(bio_err
, "Unknown CRL reason %s\n", rev_arg
);
2370 /* Argument is an OID */
2371 otmp
= OBJ_txt2obj(rev_arg
, 0);
2372 ASN1_OBJECT_free(otmp
);
2375 BIO_printf(bio_err
, "Invalid object identifier %s\n", rev_arg
);
2379 reason
= "holdInstruction";
2383 case REV_KEY_COMPROMISE
:
2384 case REV_CA_COMPROMISE
:
2385 /* Argument is the key compromise time */
2386 if (!ASN1_GENERALIZEDTIME_set_string(NULL
, rev_arg
)) {
2388 "Invalid time format %s. Need YYYYMMDDHHMMSSZ\n",
2393 if (rev_type
== REV_KEY_COMPROMISE
)
2396 reason
= "CAkeyTime";
2401 revtm
= X509_gmtime_adj(NULL
, 0);
2406 i
= revtm
->length
+ 1;
2409 i
+= (int)(strlen(reason
) + 1);
2411 i
+= (int)(strlen(other
) + 1);
2413 str
= app_malloc(i
, "revocation reason");
2414 OPENSSL_strlcpy(str
, (char *)revtm
->data
, i
);
2416 OPENSSL_strlcat(str
, ",", i
);
2417 OPENSSL_strlcat(str
, reason
, i
);
2420 OPENSSL_strlcat(str
, ",", i
);
2421 OPENSSL_strlcat(str
, other
, i
);
2423 ASN1_UTCTIME_free(revtm
);
2428 * Convert revocation field to X509_REVOKED entry
2432 * 2 OK and some extensions added (i.e. V2 CRL)
2435 static int make_revoked(X509_REVOKED
*rev
, const char *str
)
2438 int reason_code
= -1;
2440 ASN1_OBJECT
*hold
= NULL
;
2441 ASN1_GENERALIZEDTIME
*comp_time
= NULL
;
2442 ASN1_ENUMERATED
*rtmp
= NULL
;
2444 ASN1_TIME
*revDate
= NULL
;
2446 i
= unpack_revinfo(&revDate
, &reason_code
, &hold
, &comp_time
, str
);
2451 if (rev
&& !X509_REVOKED_set_revocationDate(rev
, revDate
))
2454 if (rev
&& (reason_code
!= OCSP_REVOKED_STATUS_NOSTATUS
)) {
2455 rtmp
= ASN1_ENUMERATED_new();
2456 if (rtmp
== NULL
|| !ASN1_ENUMERATED_set(rtmp
, reason_code
))
2458 if (X509_REVOKED_add1_ext_i2d(rev
, NID_crl_reason
, rtmp
, 0, 0) <= 0)
2462 if (rev
&& comp_time
) {
2463 if (X509_REVOKED_add1_ext_i2d
2464 (rev
, NID_invalidity_date
, comp_time
, 0, 0) <= 0)
2468 if (X509_REVOKED_add1_ext_i2d
2469 (rev
, NID_hold_instruction_code
, hold
, 0, 0) <= 0)
2473 if (reason_code
!= OCSP_REVOKED_STATUS_NOSTATUS
)
2481 ASN1_OBJECT_free(hold
);
2482 ASN1_GENERALIZEDTIME_free(comp_time
);
2483 ASN1_ENUMERATED_free(rtmp
);
2484 ASN1_TIME_free(revDate
);
2489 static int old_entry_print(const ASN1_OBJECT
*obj
, const ASN1_STRING
*str
)
2491 char buf
[25], *pbuf
;
2495 j
= i2a_ASN1_OBJECT(bio_err
, obj
);
2497 for (j
= 22 - j
; j
> 0; j
--)
2501 BIO_puts(bio_err
, buf
);
2503 if (str
->type
== V_ASN1_PRINTABLESTRING
)
2504 BIO_printf(bio_err
, "PRINTABLE:'");
2505 else if (str
->type
== V_ASN1_T61STRING
)
2506 BIO_printf(bio_err
, "T61STRING:'");
2507 else if (str
->type
== V_ASN1_IA5STRING
)
2508 BIO_printf(bio_err
, "IA5STRING:'");
2509 else if (str
->type
== V_ASN1_UNIVERSALSTRING
)
2510 BIO_printf(bio_err
, "UNIVERSALSTRING:'");
2512 BIO_printf(bio_err
, "ASN.1 %2d:'", str
->type
);
2514 p
= (const char *)str
->data
;
2515 for (j
= str
->length
; j
> 0; j
--) {
2516 if ((*p
>= ' ') && (*p
<= '~'))
2517 BIO_printf(bio_err
, "%c", *p
);
2519 BIO_printf(bio_err
, "\\0x%02X", *p
);
2520 else if ((unsigned char)*p
== 0xf7)
2521 BIO_printf(bio_err
, "^?");
2523 BIO_printf(bio_err
, "^%c", *p
+ '@');
2526 BIO_printf(bio_err
, "'\n");
2530 int unpack_revinfo(ASN1_TIME
**prevtm
, int *preason
, ASN1_OBJECT
**phold
,
2531 ASN1_GENERALIZEDTIME
**pinvtm
, const char *str
)
2534 char *rtime_str
, *reason_str
= NULL
, *arg_str
= NULL
, *p
;
2535 int reason_code
= -1;
2538 ASN1_OBJECT
*hold
= NULL
;
2539 ASN1_GENERALIZEDTIME
*comp_time
= NULL
;
2541 tmp
= OPENSSL_strdup(str
);
2543 BIO_printf(bio_err
, "memory allocation failure\n");
2547 p
= strchr(tmp
, ',');
2563 *prevtm
= ASN1_UTCTIME_new();
2564 if (*prevtm
== NULL
) {
2565 BIO_printf(bio_err
, "memory allocation failure\n");
2568 if (!ASN1_UTCTIME_set_string(*prevtm
, rtime_str
)) {
2569 BIO_printf(bio_err
, "invalid revocation date %s\n", rtime_str
);
2574 for (i
= 0; i
< NUM_REASONS
; i
++) {
2575 if (OPENSSL_strcasecmp(reason_str
, crl_reasons
[i
]) == 0) {
2580 if (reason_code
== OCSP_REVOKED_STATUS_NOSTATUS
) {
2581 BIO_printf(bio_err
, "invalid reason code %s\n", reason_str
);
2585 if (reason_code
== 7) {
2586 reason_code
= OCSP_REVOKED_STATUS_REMOVEFROMCRL
;
2587 } else if (reason_code
== 8) { /* Hold instruction */
2589 BIO_printf(bio_err
, "missing hold instruction\n");
2592 reason_code
= OCSP_REVOKED_STATUS_CERTIFICATEHOLD
;
2593 hold
= OBJ_txt2obj(arg_str
, 0);
2596 BIO_printf(bio_err
, "invalid object identifier %s\n", arg_str
);
2602 ASN1_OBJECT_free(hold
);
2603 } else if ((reason_code
== 9) || (reason_code
== 10)) {
2605 BIO_printf(bio_err
, "missing compromised time\n");
2608 comp_time
= ASN1_GENERALIZEDTIME_new();
2609 if (comp_time
== NULL
) {
2610 BIO_printf(bio_err
, "memory allocation failure\n");
2613 if (!ASN1_GENERALIZEDTIME_set_string(comp_time
, arg_str
)) {
2614 BIO_printf(bio_err
, "invalid compromised time %s\n", arg_str
);
2617 if (reason_code
== 9)
2618 reason_code
= OCSP_REVOKED_STATUS_KEYCOMPROMISE
;
2620 reason_code
= OCSP_REVOKED_STATUS_CACOMPROMISE
;
2625 *preason
= reason_code
;
2627 *pinvtm
= comp_time
;
2636 ASN1_GENERALIZEDTIME_free(comp_time
);