2 * Copyright 1995-2018 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
13 #include <sys/types.h>
14 #include <openssl/conf.h>
15 #include <openssl/bio.h>
16 #include <openssl/err.h>
17 #include <openssl/bn.h>
18 #include <openssl/txt_db.h>
19 #include <openssl/evp.h>
20 #include <openssl/x509.h>
21 #include <openssl/x509v3.h>
22 #include <openssl/objects.h>
23 #include <openssl/ocsp.h>
24 #include <openssl/pem.h>
27 # ifdef OPENSSL_SYS_VMS
29 # elif !defined(OPENSSL_SYS_VXWORKS) && !defined(OPENSSL_SYS_WINDOWS)
30 # include <sys/file.h>
44 # define PATH_MAX 4096
47 #define BASE_SECTION "ca"
49 #define ENV_DEFAULT_CA "default_ca"
51 #define STRING_MASK "string_mask"
52 #define UTF8_IN "utf8"
54 #define ENV_NEW_CERTS_DIR "new_certs_dir"
55 #define ENV_CERTIFICATE "certificate"
56 #define ENV_SERIAL "serial"
57 #define ENV_RAND_SERIAL "rand_serial"
58 #define ENV_CRLNUMBER "crlnumber"
59 #define ENV_PRIVATE_KEY "private_key"
60 #define ENV_DEFAULT_DAYS "default_days"
61 #define ENV_DEFAULT_STARTDATE "default_startdate"
62 #define ENV_DEFAULT_ENDDATE "default_enddate"
63 #define ENV_DEFAULT_CRL_DAYS "default_crl_days"
64 #define ENV_DEFAULT_CRL_HOURS "default_crl_hours"
65 #define ENV_DEFAULT_MD "default_md"
66 #define ENV_DEFAULT_EMAIL_DN "email_in_dn"
67 #define ENV_PRESERVE "preserve"
68 #define ENV_POLICY "policy"
69 #define ENV_EXTENSIONS "x509_extensions"
70 #define ENV_CRLEXT "crl_extensions"
71 #define ENV_MSIE_HACK "msie_hack"
72 #define ENV_NAMEOPT "name_opt"
73 #define ENV_CERTOPT "cert_opt"
74 #define ENV_EXTCOPY "copy_extensions"
75 #define ENV_UNIQUE_SUBJECT "unique_subject"
77 #define ENV_DATABASE "database"
79 /* Additional revocation information types */
81 REV_VALID
= -1, /* Valid (not-revoked) status */
82 REV_NONE
= 0, /* No additional information */
83 REV_CRL_REASON
= 1, /* Value is CRL reason code */
84 REV_HOLD
= 2, /* Value is hold instruction */
85 REV_KEY_COMPROMISE
= 3, /* Value is cert key compromise time */
86 REV_CA_COMPROMISE
= 4 /* Value is CA key compromise time */
89 static char *lookup_conf(const CONF
*conf
, const char *group
, const char *tag
);
91 static int certify(X509
**xret
, const char *infile
, EVP_PKEY
*pkey
, X509
*x509
,
92 const EVP_MD
*dgst
, STACK_OF(OPENSSL_STRING
) *sigopts
,
93 STACK_OF(CONF_VALUE
) *policy
, CA_DB
*db
,
94 BIGNUM
*serial
, const char *subj
, unsigned long chtype
,
95 int multirdn
, int email_dn
, const char *startdate
,
97 long days
, int batch
, const char *ext_sect
, CONF
*conf
,
98 int verbose
, unsigned long certopt
, unsigned long nameopt
,
99 int default_op
, int ext_copy
, int selfsign
,
100 unsigned char *sm2_id
, size_t sm2idlen
);
101 static int certify_cert(X509
**xret
, const char *infile
, EVP_PKEY
*pkey
, X509
*x509
,
102 const EVP_MD
*dgst
, STACK_OF(OPENSSL_STRING
) *sigopts
,
103 STACK_OF(CONF_VALUE
) *policy
, CA_DB
*db
,
104 BIGNUM
*serial
, const char *subj
, unsigned long chtype
,
105 int multirdn
, int email_dn
, const char *startdate
,
106 const char *enddate
, long days
, int batch
, const char *ext_sect
,
107 CONF
*conf
, int verbose
, unsigned long certopt
,
108 unsigned long nameopt
, int default_op
, int ext_copy
);
109 static int certify_spkac(X509
**xret
, const char *infile
, EVP_PKEY
*pkey
,
110 X509
*x509
, const EVP_MD
*dgst
,
111 STACK_OF(OPENSSL_STRING
) *sigopts
,
112 STACK_OF(CONF_VALUE
) *policy
, CA_DB
*db
,
113 BIGNUM
*serial
, const char *subj
, unsigned long chtype
,
114 int multirdn
, int email_dn
, const char *startdate
,
115 const char *enddate
, long days
, const char *ext_sect
, CONF
*conf
,
116 int verbose
, unsigned long certopt
,
117 unsigned long nameopt
, int default_op
, int ext_copy
);
118 static int do_body(X509
**xret
, EVP_PKEY
*pkey
, X509
*x509
,
119 const EVP_MD
*dgst
, STACK_OF(OPENSSL_STRING
) *sigopts
,
120 STACK_OF(CONF_VALUE
) *policy
, CA_DB
*db
, BIGNUM
*serial
,
121 const char *subj
, unsigned long chtype
, int multirdn
,
122 int email_dn
, const char *startdate
, const char *enddate
, long days
,
123 int batch
, int verbose
, X509_REQ
*req
, const char *ext_sect
,
124 CONF
*conf
, unsigned long certopt
, unsigned long nameopt
,
125 int default_op
, int ext_copy
, int selfsign
);
126 static int get_certificate_status(const char *ser_status
, CA_DB
*db
);
127 static int do_updatedb(CA_DB
*db
);
128 static int check_time_format(const char *str
);
129 static int do_revoke(X509
*x509
, CA_DB
*db
, REVINFO_TYPE rev_type
,
131 static char *make_revocation_str(REVINFO_TYPE rev_type
, const char *rev_arg
);
132 static int make_revoked(X509_REVOKED
*rev
, const char *str
);
133 static int old_entry_print(const ASN1_OBJECT
*obj
, const ASN1_STRING
*str
);
134 static void write_new_certificate(BIO
*bp
, X509
*x
, int output_der
, int notext
);
136 static CONF
*extconf
= NULL
;
137 static int preserve
= 0;
138 static int msie_hack
= 0;
140 typedef enum OPTION_choice
{
141 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
142 OPT_ENGINE
, OPT_VERBOSE
, OPT_CONFIG
, OPT_NAME
, OPT_SUBJ
, OPT_UTF8
,
143 OPT_CREATE_SERIAL
, OPT_MULTIVALUE_RDN
, OPT_STARTDATE
, OPT_ENDDATE
,
144 OPT_DAYS
, OPT_MD
, OPT_POLICY
, OPT_KEYFILE
, OPT_KEYFORM
, OPT_PASSIN
,
145 OPT_KEY
, OPT_CERT
, OPT_SELFSIGN
, OPT_IN
, OPT_OUT
, OPT_OUTDIR
,
146 OPT_SIGOPT
, OPT_NOTEXT
, OPT_BATCH
, OPT_PRESERVEDN
, OPT_NOEMAILDN
,
147 OPT_GENCRL
, OPT_MSIE_HACK
, OPT_CRLDAYS
, OPT_CRLHOURS
, OPT_CRLSEC
,
148 OPT_INFILES
, OPT_SS_CERT
, OPT_SPKAC
, OPT_REVOKE
, OPT_VALID
,
149 OPT_EXTENSIONS
, OPT_EXTFILE
, OPT_STATUS
, OPT_UPDATEDB
, OPT_CRLEXTS
,
151 OPT_R_ENUM
, OPT_SM2ID
, OPT_SM2HEXID
, OPT_PROV_ENUM
,
152 /* Do not change the order here; see related case statements below */
153 OPT_CRL_REASON
, OPT_CRL_HOLD
, OPT_CRL_COMPROMISE
, OPT_CRL_CA_COMPROMISE
156 const OPTIONS ca_options
[] = {
157 {OPT_HELP_STR
, 1, '-', "Usage: %s [options] [certreq...]\n"},
159 OPT_SECTION("General"),
160 {"help", OPT_HELP
, '-', "Display this summary"},
161 {"verbose", OPT_VERBOSE
, '-', "Verbose output during processing"},
162 {"outdir", OPT_OUTDIR
, '/', "Where to put output cert"},
163 {"in", OPT_IN
, '<', "The input PEM encoded cert request(s)"},
164 {"infiles", OPT_INFILES
, '-', "The last argument, requests to process"},
165 {"out", OPT_OUT
, '>', "Where to put the output file(s)"},
166 {"notext", OPT_NOTEXT
, '-', "Do not print the generated certificate"},
167 {"batch", OPT_BATCH
, '-', "Don't ask questions"},
168 {"msie_hack", OPT_MSIE_HACK
, '-',
169 "msie modifications to handle all Universal Strings"},
170 {"ss_cert", OPT_SS_CERT
, '<', "File contains a self signed cert to sign"},
171 {"spkac", OPT_SPKAC
, '<',
172 "File contains DN and signed public key and challenge"},
173 #ifndef OPENSSL_NO_ENGINE
174 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
177 OPT_SECTION("Configuration"),
178 {"config", OPT_CONFIG
, 's', "A config file"},
179 {"name", OPT_NAME
, 's', "The particular CA definition to use"},
180 {"policy", OPT_POLICY
, 's', "The CA 'policy' to support"},
182 OPT_SECTION("Certificate"),
183 {"subj", OPT_SUBJ
, 's', "Use arg instead of request's subject"},
184 {"utf8", OPT_UTF8
, '-', "Input characters are UTF8 (default ASCII)"},
185 {"create_serial", OPT_CREATE_SERIAL
, '-',
186 "If reading serial fails, create a new random serial"},
187 {"rand_serial", OPT_RAND_SERIAL
, '-',
188 "Always create a random serial; do not store it"},
189 {"multivalue-rdn", OPT_MULTIVALUE_RDN
, '-',
190 "Enable support for multivalued RDNs"},
191 {"startdate", OPT_STARTDATE
, 's', "Cert notBefore, YYMMDDHHMMSSZ"},
192 {"enddate", OPT_ENDDATE
, 's',
193 "YYMMDDHHMMSSZ cert notAfter (overrides -days)"},
194 {"days", OPT_DAYS
, 'p', "Number of days to certify the cert for"},
195 {"extensions", OPT_EXTENSIONS
, 's',
196 "Extension section (override value in config file)"},
197 {"extfile", OPT_EXTFILE
, '<',
198 "Configuration file with X509v3 extensions to add"},
199 #ifndef OPENSSL_NO_SM2
200 {"sm2-id", OPT_SM2ID
, 's',
201 "Specify an ID string to verify an SM2 certificate request"},
202 {"sm2-hex-id", OPT_SM2HEXID
, 's',
203 "Specify a hex ID string to verify an SM2 certificate request"},
205 {"preserveDN", OPT_PRESERVEDN
, '-', "Don't re-order the DN"},
206 {"noemailDN", OPT_NOEMAILDN
, '-', "Don't add the EMAIL field to the DN"},
208 OPT_SECTION("Signing"),
209 {"md", OPT_MD
, 's', "md to use; one of md2, md5, sha or sha1"},
210 {"keyfile", OPT_KEYFILE
, 's', "Private key"},
211 {"keyform", OPT_KEYFORM
, 'f', "Private key file format (PEM or ENGINE)"},
212 {"passin", OPT_PASSIN
, 's', "Input file pass phrase source"},
213 {"key", OPT_KEY
, 's', "Key to decode the private key if it is encrypted"},
214 {"cert", OPT_CERT
, '<', "The CA cert"},
215 {"selfsign", OPT_SELFSIGN
, '-',
216 "Sign a cert with the key associated with it"},
217 {"sigopt", OPT_SIGOPT
, 's', "Signature parameter in n:v form"},
219 OPT_SECTION("Revocation"),
220 {"gencrl", OPT_GENCRL
, '-', "Generate a new CRL"},
221 {"valid", OPT_VALID
, 's',
222 "Add a Valid(not-revoked) DB entry about a cert (given in file)"},
223 {"status", OPT_STATUS
, 's', "Shows cert status given the serial number"},
224 {"updatedb", OPT_UPDATEDB
, '-', "Updates db for expired cert"},
225 {"crlexts", OPT_CRLEXTS
, 's',
226 "CRL extension section (override value in config file)"},
227 {"crl_reason", OPT_CRL_REASON
, 's', "revocation reason"},
228 {"crl_hold", OPT_CRL_HOLD
, 's',
229 "the hold instruction, an OID. Sets revocation reason to certificateHold"},
230 {"crl_compromise", OPT_CRL_COMPROMISE
, 's',
231 "sets compromise time to val and the revocation reason to keyCompromise"},
232 {"crl_CA_compromise", OPT_CRL_CA_COMPROMISE
, 's',
233 "sets compromise time to val and the revocation reason to CACompromise"},
234 {"crldays", OPT_CRLDAYS
, 'p', "Days until the next CRL is due"},
235 {"crlhours", OPT_CRLHOURS
, 'p', "Hours until the next CRL is due"},
236 {"crlsec", OPT_CRLSEC
, 'p', "Seconds until the next CRL is due"},
237 {"revoke", OPT_REVOKE
, '<', "Revoke a cert (given in file)"},
243 {"certreq", 0, 0, "Certificate requests to be signed (optional)"},
247 int ca_main(int argc
, char **argv
)
251 BIGNUM
*crlnumber
= NULL
, *serial
= NULL
;
252 EVP_PKEY
*pkey
= NULL
;
253 BIO
*in
= NULL
, *out
= NULL
, *Sout
= NULL
;
254 ASN1_INTEGER
*tmpser
;
258 STACK_OF(CONF_VALUE
) *attribs
= NULL
;
259 STACK_OF(OPENSSL_STRING
) *sigopts
= NULL
;
260 STACK_OF(X509
) *cert_sk
= NULL
;
261 X509_CRL
*crl
= NULL
;
262 const EVP_MD
*dgst
= NULL
;
263 char *configfile
= default_config_file
, *section
= NULL
;
264 char *md
= NULL
, *policy
= NULL
, *keyfile
= NULL
;
265 char *certfile
= NULL
, *crl_ext
= NULL
, *crlnumberfile
= NULL
, *key
= NULL
;
266 const char *infile
= NULL
, *spkac_file
= NULL
, *ss_cert_file
= NULL
;
267 const char *extensions
= NULL
, *extfile
= NULL
, *passinarg
= NULL
;
268 char *outdir
= NULL
, *outfile
= NULL
, *rev_arg
= NULL
, *ser_status
= NULL
;
269 const char *serialfile
= NULL
, *subj
= NULL
;
270 char *prog
, *startdate
= NULL
, *enddate
= NULL
;
271 char *dbfile
= NULL
, *f
;
272 char new_cert
[PATH_MAX
];
273 char tmp
[10 + 1] = "\0";
276 size_t outdirlen
= 0;
277 int create_ser
= 0, free_key
= 0, total
= 0, total_done
= 0;
278 int batch
= 0, default_op
= 1, doupdatedb
= 0, ext_copy
= EXT_COPY_NONE
;
279 int keyformat
= FORMAT_PEM
, multirdn
= 0, notext
= 0, output_der
= 0;
280 int ret
= 1, email_dn
= 1, req
= 0, verbose
= 0, gencrl
= 0, dorevoke
= 0;
281 int rand_ser
= 0, i
, j
, selfsign
= 0, def_nid
, def_ret
;
282 long crldays
= 0, crlhours
= 0, crlsec
= 0, days
= 0;
283 unsigned long chtype
= MBSTRING_ASC
, certopt
= 0;
284 X509
*x509
= NULL
, *x509p
= NULL
, *x
= NULL
;
285 REVINFO_TYPE rev_type
= REV_NONE
;
286 X509_REVOKED
*r
= NULL
;
288 unsigned char *sm2_id
= NULL
;
289 size_t sm2_idlen
= 0;
292 prog
= opt_init(argc
, argv
, ca_options
);
293 while ((o
= opt_next()) != OPT_EOF
) {
298 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
301 opt_help(ca_options
);
315 configfile
= opt_arg();
325 chtype
= MBSTRING_UTF8
;
327 case OPT_RAND_SERIAL
:
330 case OPT_CREATE_SERIAL
:
333 case OPT_MULTIVALUE_RDN
:
337 startdate
= opt_arg();
343 days
= atoi(opt_arg());
355 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &keyformat
))
359 passinarg
= opt_arg();
366 if (!opt_provider(o
))
373 certfile
= opt_arg();
383 sigopts
= sk_OPENSSL_STRING_new_null();
384 if (sigopts
== NULL
|| !sk_OPENSSL_STRING_push(sigopts
, opt_arg()))
406 crldays
= atol(opt_arg());
409 crlhours
= atol(opt_arg());
412 crlsec
= atol(opt_arg());
418 ss_cert_file
= opt_arg();
422 spkac_file
= opt_arg();
434 extensions
= opt_arg();
440 ser_status
= opt_arg();
448 case OPT_CRL_REASON
: /* := REV_CRL_REASON */
450 case OPT_CRL_COMPROMISE
:
451 case OPT_CRL_CA_COMPROMISE
:
453 rev_type
= (o
- OPT_CRL_REASON
) + REV_CRL_REASON
;
456 e
= setup_engine(opt_arg(), 0);
459 /* we assume the input is not a hex string */
460 if (sm2_id
!= NULL
) {
462 "Use one of the options 'sm2-hex-id' or 'sm2-id'\n");
465 sm2_id
= (unsigned char *)opt_arg();
466 sm2_idlen
= strlen((const char *)sm2_id
);
469 /* try to parse the input as hex string first */
470 if (sm2_id
!= NULL
) {
472 "Use one of the options 'sm2-hex-id' or 'sm2-id'\n");
476 sm2_id
= OPENSSL_hexstr2buf(opt_arg(), (long *)&sm2_idlen
);
477 if (sm2_id
== NULL
) {
478 BIO_printf(bio_err
, "Invalid hex string input\n");
485 argc
= opt_num_rest();
488 BIO_printf(bio_err
, "Using configuration from %s\n", configfile
);
490 if ((conf
= app_load_config(configfile
)) == NULL
)
492 if (configfile
!= default_config_file
&& !app_load_modules(conf
))
495 /* Lets get the config section we are using */
497 && (section
= lookup_conf(conf
, BASE_SECTION
, ENV_DEFAULT_CA
)) == NULL
)
500 p
= NCONF_get_string(conf
, NULL
, "oid_file");
504 BIO
*oid_bio
= BIO_new_file(p
, "r");
506 if (oid_bio
== NULL
) {
509 OBJ_create_objects(oid_bio
);
513 if (!add_oid_section(conf
)) {
514 ERR_print_errors(bio_err
);
518 app_RAND_load_conf(conf
, BASE_SECTION
);
520 f
= NCONF_get_string(conf
, section
, STRING_MASK
);
524 if (f
!= NULL
&& !ASN1_STRING_set_default_mask_asc(f
)) {
525 BIO_printf(bio_err
, "Invalid global string mask setting %s\n", f
);
529 if (chtype
!= MBSTRING_UTF8
) {
530 f
= NCONF_get_string(conf
, section
, UTF8_IN
);
533 else if (strcmp(f
, "yes") == 0)
534 chtype
= MBSTRING_UTF8
;
537 db_attr
.unique_subject
= 1;
538 p
= NCONF_get_string(conf
, section
, ENV_UNIQUE_SUBJECT
);
540 db_attr
.unique_subject
= parse_yesno(p
, 1);
544 /*****************************************************************/
545 /* report status of cert with serial number given on command line */
547 dbfile
= lookup_conf(conf
, section
, ENV_DATABASE
);
551 db
= load_index(dbfile
, &db_attr
);
555 if (index_index(db
) <= 0)
558 if (get_certificate_status(ser_status
, db
) != 1)
559 BIO_printf(bio_err
, "Error verifying serial %s!\n", ser_status
);
563 /*****************************************************************/
564 /* we definitely need a private key, so let's get it */
567 && (keyfile
= lookup_conf(conf
, section
, ENV_PRIVATE_KEY
)) == NULL
)
572 if (!app_passwd(passinarg
, NULL
, &key
, NULL
)) {
573 BIO_printf(bio_err
, "Error getting password\n");
577 pkey
= load_key(keyfile
, keyformat
, 0, key
, e
, "CA private key");
579 OPENSSL_cleanse(key
, strlen(key
));
581 /* load_key() has already printed an appropriate message */
584 /*****************************************************************/
585 /* we need a certificate */
586 if (!selfsign
|| spkac_file
|| ss_cert_file
|| gencrl
) {
588 && (certfile
= lookup_conf(conf
, section
, ENV_CERTIFICATE
)) == NULL
)
591 x509
= load_cert(certfile
, FORMAT_PEM
, "CA certificate");
595 if (!X509_check_private_key(x509
, pkey
)) {
597 "CA certificate and CA private key do not match\n");
604 f
= NCONF_get_string(conf
, BASE_SECTION
, ENV_PRESERVE
);
607 if ((f
!= NULL
) && ((*f
== 'y') || (*f
== 'Y')))
609 f
= NCONF_get_string(conf
, BASE_SECTION
, ENV_MSIE_HACK
);
612 if ((f
!= NULL
) && ((*f
== 'y') || (*f
== 'Y')))
615 f
= NCONF_get_string(conf
, section
, ENV_NAMEOPT
);
618 if (!set_nameopt(f
)) {
619 BIO_printf(bio_err
, "Invalid name options: \"%s\"\n", f
);
625 f
= NCONF_get_string(conf
, section
, ENV_CERTOPT
);
628 if (!set_cert_ex(&certopt
, f
)) {
629 BIO_printf(bio_err
, "Invalid certificate options: \"%s\"\n", f
);
637 f
= NCONF_get_string(conf
, section
, ENV_EXTCOPY
);
640 if (!set_ext_copy(&ext_copy
, f
)) {
641 BIO_printf(bio_err
, "Invalid extension copy option: \"%s\"\n", f
);
648 /*****************************************************************/
649 /* lookup where to write new certificates */
650 if ((outdir
== NULL
) && (req
)) {
652 outdir
= NCONF_get_string(conf
, section
, ENV_NEW_CERTS_DIR
);
653 if (outdir
== NULL
) {
655 "there needs to be defined a directory for new certificate to be placed in\n");
658 #ifndef OPENSSL_SYS_VMS
660 * outdir is a directory spec, but access() for VMS demands a
661 * filename. We could use the DEC C routine to convert the
662 * directory syntax to Unix, and give that to app_isdir,
663 * but for now the fopen will catch the error if it's not a
666 if (app_isdir(outdir
) <= 0) {
667 BIO_printf(bio_err
, "%s: %s is not a directory\n", prog
, outdir
);
674 /*****************************************************************/
675 /* we need to load the database file */
676 dbfile
= lookup_conf(conf
, section
, ENV_DATABASE
);
680 db
= load_index(dbfile
, &db_attr
);
684 /* Lets check some fields */
685 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(db
->db
->data
); i
++) {
686 pp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, i
);
687 if ((pp
[DB_type
][0] != DB_TYPE_REV
) && (pp
[DB_rev_date
][0] != '\0')) {
689 "entry %d: not revoked yet, but has a revocation date\n",
693 if ((pp
[DB_type
][0] == DB_TYPE_REV
) &&
694 !make_revoked(NULL
, pp
[DB_rev_date
])) {
695 BIO_printf(bio_err
, " in entry %d\n", i
+ 1);
698 if (!check_time_format((char *)pp
[DB_exp_date
])) {
699 BIO_printf(bio_err
, "entry %d: invalid expiry date\n", i
+ 1);
708 if ((j
& 1) || (j
< 2)) {
709 BIO_printf(bio_err
, "entry %d: bad serial number length (%d)\n",
714 if (!isxdigit(_UC(*p
))) {
716 "entry %d: bad char 0%o '%c' in serial number\n",
723 TXT_DB_write(bio_out
, db
->db
);
724 BIO_printf(bio_err
, "%d entries loaded from the database\n",
725 sk_OPENSSL_PSTRING_num(db
->db
->data
));
726 BIO_printf(bio_err
, "generating index\n");
729 if (index_index(db
) <= 0)
732 /*****************************************************************/
733 /* Update the db file for expired certificates */
736 BIO_printf(bio_err
, "Updating %s ...\n", dbfile
);
740 BIO_printf(bio_err
, "Malloc failure\n");
744 BIO_printf(bio_err
, "No entries found to mark expired\n");
746 if (!save_index(dbfile
, "new", db
))
749 if (!rotate_index(dbfile
, "new", "old"))
753 BIO_printf(bio_err
, "Done. %d entries marked as expired\n", i
);
757 /*****************************************************************/
758 /* Read extensions config file */
760 if ((extconf
= app_load_config(extfile
)) == NULL
) {
766 BIO_printf(bio_err
, "Successfully loaded extensions file %s\n",
769 /* We can have sections in the ext file */
770 if (extensions
== NULL
) {
771 extensions
= NCONF_get_string(extconf
, "default", "extensions");
772 if (extensions
== NULL
)
773 extensions
= "default";
777 /*****************************************************************/
779 if (spkac_file
!= NULL
&& outfile
!= NULL
) {
785 def_ret
= EVP_PKEY_get_default_digest_nid(pkey
, &def_nid
);
787 * EVP_PKEY_get_default_digest_nid() returns 2 if the digest is
788 * mandatory for this algorithm.
790 if (def_ret
== 2 && def_nid
== NID_undef
) {
791 /* The signing algorithm requires there to be no digest */
792 dgst
= EVP_md_null();
793 } else if (md
== NULL
794 && (md
= lookup_conf(conf
, section
, ENV_DEFAULT_MD
)) == NULL
) {
797 if (strcmp(md
, "default") == 0) {
799 BIO_puts(bio_err
, "no default digest\n");
802 md
= (char *)OBJ_nid2sn(def_nid
);
805 if (!opt_md(md
, &dgst
))
811 char *tmp_email_dn
= NULL
;
813 tmp_email_dn
= NCONF_get_string(conf
, section
, ENV_DEFAULT_EMAIL_DN
);
814 if (tmp_email_dn
!= NULL
&& strcmp(tmp_email_dn
, "no") == 0)
818 BIO_printf(bio_err
, "message digest is %s\n",
819 OBJ_nid2ln(EVP_MD_type(dgst
)));
821 && (policy
= lookup_conf(conf
, section
, ENV_POLICY
)) == NULL
)
825 BIO_printf(bio_err
, "policy is %s\n", policy
);
827 if (NCONF_get_string(conf
, section
, ENV_RAND_SERIAL
) != NULL
) {
830 serialfile
= lookup_conf(conf
, section
, ENV_SERIAL
);
831 if (serialfile
== NULL
)
835 if (extconf
== NULL
) {
837 * no '-extfile' option, so we look for extensions in the main
840 if (extensions
== NULL
) {
841 extensions
= NCONF_get_string(conf
, section
, ENV_EXTENSIONS
);
842 if (extensions
== NULL
)
845 if (extensions
!= NULL
) {
846 /* Check syntax of file */
848 X509V3_set_ctx_test(&ctx
);
849 X509V3_set_nconf(&ctx
, conf
);
850 if (!X509V3_EXT_add_nconf(conf
, &ctx
, extensions
, NULL
)) {
852 "Error Loading extension section %s\n",
860 if (startdate
== NULL
) {
861 startdate
= NCONF_get_string(conf
, section
, ENV_DEFAULT_STARTDATE
);
862 if (startdate
== NULL
)
865 if (startdate
!= NULL
&& !ASN1_TIME_set_string_X509(NULL
, startdate
)) {
867 "start date is invalid, it should be YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ\n");
870 if (startdate
== NULL
)
873 if (enddate
== NULL
) {
874 enddate
= NCONF_get_string(conf
, section
, ENV_DEFAULT_ENDDATE
);
878 if (enddate
!= NULL
&& !ASN1_TIME_set_string_X509(NULL
, enddate
)) {
880 "end date is invalid, it should be YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ\n");
885 if (!NCONF_get_number(conf
, section
, ENV_DEFAULT_DAYS
, &days
))
888 if (enddate
== NULL
&& days
== 0) {
889 BIO_printf(bio_err
, "cannot lookup how many days to certify for\n");
894 if ((serial
= BN_new()) == NULL
|| !rand_serial(serial
, NULL
)) {
895 BIO_printf(bio_err
, "error generating serial number\n");
899 if ((serial
= load_serial(serialfile
, create_ser
, NULL
)) == NULL
) {
900 BIO_printf(bio_err
, "error while loading serial number\n");
904 if (BN_is_zero(serial
)) {
905 BIO_printf(bio_err
, "next serial number is 00\n");
907 if ((f
= BN_bn2hex(serial
)) == NULL
)
909 BIO_printf(bio_err
, "next serial number is %s\n", f
);
915 if ((attribs
= NCONF_get_section(conf
, policy
)) == NULL
) {
916 BIO_printf(bio_err
, "unable to find 'section' for %s\n", policy
);
920 if ((cert_sk
= sk_X509_new_null()) == NULL
) {
921 BIO_printf(bio_err
, "Memory allocation failure\n");
924 if (spkac_file
!= NULL
) {
926 j
= certify_spkac(&x
, spkac_file
, pkey
, x509
, dgst
, sigopts
,
927 attribs
, db
, serial
, subj
, chtype
, multirdn
,
928 email_dn
, startdate
, enddate
, days
, extensions
,
929 conf
, verbose
, certopt
, get_nameopt(), default_op
,
935 BIO_printf(bio_err
, "\n");
936 if (!BN_add_word(serial
, 1))
938 if (!sk_X509_push(cert_sk
, x
)) {
939 BIO_printf(bio_err
, "Memory allocation failure\n");
944 if (ss_cert_file
!= NULL
) {
946 j
= certify_cert(&x
, ss_cert_file
, pkey
, x509
, dgst
, sigopts
,
948 db
, serial
, subj
, chtype
, multirdn
, email_dn
,
949 startdate
, enddate
, days
, batch
, extensions
,
950 conf
, verbose
, certopt
, get_nameopt(), default_op
,
956 BIO_printf(bio_err
, "\n");
957 if (!BN_add_word(serial
, 1))
959 if (!sk_X509_push(cert_sk
, x
)) {
960 BIO_printf(bio_err
, "Memory allocation failure\n");
965 if (infile
!= NULL
) {
967 j
= certify(&x
, infile
, pkey
, x509p
, dgst
, sigopts
, attribs
, db
,
968 serial
, subj
, chtype
, multirdn
, email_dn
, startdate
,
969 enddate
, days
, batch
, extensions
, conf
, verbose
,
970 certopt
, get_nameopt(), default_op
, ext_copy
, selfsign
,
976 BIO_printf(bio_err
, "\n");
977 if (!BN_add_word(serial
, 1))
979 if (!sk_X509_push(cert_sk
, x
)) {
980 BIO_printf(bio_err
, "Memory allocation failure\n");
985 for (i
= 0; i
< argc
; i
++) {
987 j
= certify(&x
, argv
[i
], pkey
, x509p
, dgst
, sigopts
, attribs
, db
,
988 serial
, subj
, chtype
, multirdn
, email_dn
, startdate
,
989 enddate
, days
, batch
, extensions
, conf
, verbose
,
990 certopt
, get_nameopt(), default_op
, ext_copy
, selfsign
,
996 BIO_printf(bio_err
, "\n");
997 if (!BN_add_word(serial
, 1)) {
1001 if (!sk_X509_push(cert_sk
, x
)) {
1002 BIO_printf(bio_err
, "Memory allocation failure\n");
1009 * we have a stack of newly certified certificates and a data base
1010 * and serial number that need updating
1013 if (sk_X509_num(cert_sk
) > 0) {
1016 "\n%d out of %d certificate requests certified, commit? [y/n]",
1018 (void)BIO_flush(bio_err
);
1020 if (fgets(tmp
, sizeof(tmp
), stdin
) == NULL
) {
1021 BIO_printf(bio_err
, "CERTIFICATION CANCELED: I/O error\n");
1025 if (tmp
[0] != 'y' && tmp
[0] != 'Y') {
1026 BIO_printf(bio_err
, "CERTIFICATION CANCELED\n");
1032 BIO_printf(bio_err
, "Write out database with %d new entries\n",
1033 sk_X509_num(cert_sk
));
1035 if (serialfile
!= NULL
1036 && !save_serial(serialfile
, "new", serial
, NULL
))
1039 if (!save_index(dbfile
, "new", db
))
1043 outdirlen
= OPENSSL_strlcpy(new_cert
, outdir
, sizeof(new_cert
));
1044 #ifndef OPENSSL_SYS_VMS
1045 outdirlen
= OPENSSL_strlcat(new_cert
, "/", sizeof(new_cert
));
1049 BIO_printf(bio_err
, "writing new certificates\n");
1051 for (i
= 0; i
< sk_X509_num(cert_sk
); i
++) {
1053 X509
*xi
= sk_X509_value(cert_sk
, i
);
1054 ASN1_INTEGER
*serialNumber
= X509_get_serialNumber(xi
);
1055 const unsigned char *psn
= ASN1_STRING_get0_data(serialNumber
);
1056 const int snl
= ASN1_STRING_length(serialNumber
);
1057 const int filen_len
= 2 * (snl
> 0 ? snl
: 1) + sizeof(".pem");
1058 char *n
= new_cert
+ outdirlen
;
1060 if (outdirlen
+ filen_len
> PATH_MAX
) {
1061 BIO_printf(bio_err
, "certificate file name too long\n");
1066 static const char HEX_DIGITS
[] = "0123456789ABCDEF";
1068 for (j
= 0; j
< snl
; j
++, psn
++) {
1069 *n
++ = HEX_DIGITS
[*psn
>> 4];
1070 *n
++ = HEX_DIGITS
[*psn
& 0x0F];
1080 *n
= '\0'; /* closing new_cert */
1082 BIO_printf(bio_err
, "writing %s\n", new_cert
);
1084 Sout
= bio_open_default(outfile
, 'w',
1085 output_der
? FORMAT_ASN1
: FORMAT_TEXT
);
1089 Cout
= BIO_new_file(new_cert
, "w");
1094 write_new_certificate(Cout
, xi
, 0, notext
);
1095 write_new_certificate(Sout
, xi
, output_der
, notext
);
1101 if (sk_X509_num(cert_sk
)) {
1102 /* Rename the database and the serial file */
1103 if (serialfile
!= NULL
1104 && !rotate_serial(serialfile
, "new", "old"))
1107 if (!rotate_index(dbfile
, "new", "old"))
1110 BIO_printf(bio_err
, "Data Base Updated\n");
1114 /*****************************************************************/
1117 if (crl_ext
== NULL
) {
1118 crl_ext
= NCONF_get_string(conf
, section
, ENV_CRLEXT
);
1119 if (crl_ext
== NULL
)
1122 if (crl_ext
!= NULL
) {
1123 /* Check syntax of file */
1125 X509V3_set_ctx_test(&ctx
);
1126 X509V3_set_nconf(&ctx
, conf
);
1127 if (!X509V3_EXT_add_nconf(conf
, &ctx
, crl_ext
, NULL
)) {
1129 "Error Loading CRL extension section %s\n", crl_ext
);
1135 if ((crlnumberfile
= NCONF_get_string(conf
, section
, ENV_CRLNUMBER
))
1137 if ((crlnumber
= load_serial(crlnumberfile
, 0, NULL
)) == NULL
) {
1138 BIO_printf(bio_err
, "error while loading CRL number\n");
1142 if (!crldays
&& !crlhours
&& !crlsec
) {
1143 if (!NCONF_get_number(conf
, section
,
1144 ENV_DEFAULT_CRL_DAYS
, &crldays
))
1146 if (!NCONF_get_number(conf
, section
,
1147 ENV_DEFAULT_CRL_HOURS
, &crlhours
))
1151 if ((crldays
== 0) && (crlhours
== 0) && (crlsec
== 0)) {
1153 "cannot lookup how long until the next CRL is issued\n");
1158 BIO_printf(bio_err
, "making CRL\n");
1159 if ((crl
= X509_CRL_new()) == NULL
)
1161 if (!X509_CRL_set_issuer_name(crl
, X509_get_subject_name(x509
)))
1164 tmptm
= ASN1_TIME_new();
1166 || X509_gmtime_adj(tmptm
, 0) == NULL
1167 || !X509_CRL_set1_lastUpdate(crl
, tmptm
)
1168 || X509_time_adj_ex(tmptm
, crldays
, crlhours
* 60 * 60 + crlsec
,
1170 BIO_puts(bio_err
, "error setting CRL nextUpdate\n");
1171 ASN1_TIME_free(tmptm
);
1174 X509_CRL_set1_nextUpdate(crl
, tmptm
);
1176 ASN1_TIME_free(tmptm
);
1178 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(db
->db
->data
); i
++) {
1179 pp
= sk_OPENSSL_PSTRING_value(db
->db
->data
, i
);
1180 if (pp
[DB_type
][0] == DB_TYPE_REV
) {
1181 if ((r
= X509_REVOKED_new()) == NULL
)
1183 j
= make_revoked(r
, pp
[DB_rev_date
]);
1188 if (!BN_hex2bn(&serial
, pp
[DB_serial
]))
1190 tmpser
= BN_to_ASN1_INTEGER(serial
, NULL
);
1195 X509_REVOKED_set_serialNumber(r
, tmpser
);
1196 ASN1_INTEGER_free(tmpser
);
1197 X509_CRL_add0_revoked(crl
, r
);
1202 * sort the data so it will be written in serial number order
1206 /* we now have a CRL */
1208 BIO_printf(bio_err
, "signing CRL\n");
1210 /* Add any extensions asked for */
1212 if (crl_ext
!= NULL
|| crlnumberfile
!= NULL
) {
1214 X509V3_set_ctx(&crlctx
, x509
, NULL
, NULL
, crl
, 0);
1215 X509V3_set_nconf(&crlctx
, conf
);
1217 if (crl_ext
!= NULL
)
1218 if (!X509V3_EXT_CRL_add_nconf(conf
, &crlctx
, crl_ext
, crl
))
1220 if (crlnumberfile
!= NULL
) {
1221 tmpser
= BN_to_ASN1_INTEGER(crlnumber
, NULL
);
1224 X509_CRL_add1_ext_i2d(crl
, NID_crl_number
, tmpser
, 0, 0);
1225 ASN1_INTEGER_free(tmpser
);
1227 if (!BN_add_word(crlnumber
, 1))
1231 if (crl_ext
!= NULL
|| crl_v2
) {
1232 if (!X509_CRL_set_version(crl
, 1))
1233 goto end
; /* version 2 CRL */
1236 /* we have a CRL number that need updating */
1237 if (crlnumberfile
!= NULL
1238 && !save_serial(crlnumberfile
, "new", crlnumber
, NULL
))
1244 if (!do_X509_CRL_sign(crl
, pkey
, dgst
, sigopts
))
1247 Sout
= bio_open_default(outfile
, 'w',
1248 output_der
? FORMAT_ASN1
: FORMAT_TEXT
);
1252 PEM_write_bio_X509_CRL(Sout
, crl
);
1254 /* Rename the crlnumber file */
1255 if (crlnumberfile
!= NULL
1256 && !rotate_serial(crlnumberfile
, "new", "old"))
1260 /*****************************************************************/
1262 if (infile
== NULL
) {
1263 BIO_printf(bio_err
, "no input files\n");
1267 revcert
= load_cert(infile
, FORMAT_PEM
, infile
);
1268 if (revcert
== NULL
)
1271 rev_type
= REV_VALID
;
1272 j
= do_revoke(revcert
, db
, rev_type
, rev_arg
);
1277 if (!save_index(dbfile
, "new", db
))
1280 if (!rotate_index(dbfile
, "new", "old"))
1283 BIO_printf(bio_err
, "Data Base Updated\n");
1290 OPENSSL_free(sm2_id
);
1292 ERR_print_errors(bio_err
);
1296 sk_X509_pop_free(cert_sk
, X509_free
);
1303 sk_OPENSSL_STRING_free(sigopts
);
1304 EVP_PKEY_free(pkey
);
1308 NCONF_free(extconf
);
1313 static char *lookup_conf(const CONF
*conf
, const char *section
, const char *tag
)
1315 char *entry
= NCONF_get_string(conf
, section
, tag
);
1317 BIO_printf(bio_err
, "variable lookup failed for %s::%s\n", section
, tag
);
1321 static int certify(X509
**xret
, const char *infile
, EVP_PKEY
*pkey
, X509
*x509
,
1322 const EVP_MD
*dgst
, STACK_OF(OPENSSL_STRING
) *sigopts
,
1323 STACK_OF(CONF_VALUE
) *policy
, CA_DB
*db
,
1324 BIGNUM
*serial
, const char *subj
, unsigned long chtype
,
1325 int multirdn
, int email_dn
, const char *startdate
,
1326 const char *enddate
,
1327 long days
, int batch
, const char *ext_sect
, CONF
*lconf
,
1328 int verbose
, unsigned long certopt
, unsigned long nameopt
,
1329 int default_op
, int ext_copy
, int selfsign
,
1330 unsigned char *sm2id
, size_t sm2idlen
)
1332 X509_REQ
*req
= NULL
;
1334 EVP_PKEY
*pktmp
= NULL
;
1337 in
= BIO_new_file(infile
, "r");
1339 ERR_print_errors(bio_err
);
1342 if ((req
= PEM_read_bio_X509_REQ(in
, NULL
, NULL
, NULL
)) == NULL
) {
1343 BIO_printf(bio_err
, "Error reading certificate request in %s\n",
1348 X509_REQ_print_ex(bio_err
, req
, nameopt
, X509_FLAG_COMPAT
);
1350 BIO_printf(bio_err
, "Check that the request matches the signature\n");
1352 if (selfsign
&& !X509_REQ_check_private_key(req
, pkey
)) {
1354 "Certificate request and CA private key do not match\n");
1358 if ((pktmp
= X509_REQ_get0_pubkey(req
)) == NULL
) {
1359 BIO_printf(bio_err
, "error unpacking public key\n");
1362 if (sm2id
!= NULL
) {
1363 #ifndef OPENSSL_NO_SM2
1364 ASN1_OCTET_STRING
*v
;
1366 v
= ASN1_OCTET_STRING_new();
1368 BIO_printf(bio_err
, "error: SM2 ID allocation failed\n");
1372 if (!ASN1_OCTET_STRING_set(v
, sm2id
, sm2idlen
)) {
1373 BIO_printf(bio_err
, "error: setting SM2 ID failed\n");
1374 ASN1_OCTET_STRING_free(v
);
1378 X509_REQ_set0_sm2_id(req
, v
);
1381 i
= X509_REQ_verify(req
, pktmp
);
1385 BIO_printf(bio_err
, "Signature verification problems....\n");
1386 ERR_print_errors(bio_err
);
1392 "Signature did not match the certificate request\n");
1393 ERR_print_errors(bio_err
);
1396 BIO_printf(bio_err
, "Signature ok\n");
1399 ok
= do_body(xret
, pkey
, x509
, dgst
, sigopts
, policy
, db
, serial
, subj
,
1400 chtype
, multirdn
, email_dn
, startdate
, enddate
, days
, batch
,
1401 verbose
, req
, ext_sect
, lconf
, certopt
, nameopt
, default_op
,
1402 ext_copy
, selfsign
);
1410 static int certify_cert(X509
**xret
, const char *infile
, EVP_PKEY
*pkey
, X509
*x509
,
1411 const EVP_MD
*dgst
, STACK_OF(OPENSSL_STRING
) *sigopts
,
1412 STACK_OF(CONF_VALUE
) *policy
, CA_DB
*db
,
1413 BIGNUM
*serial
, const char *subj
, unsigned long chtype
,
1414 int multirdn
, int email_dn
, const char *startdate
,
1415 const char *enddate
, long days
, int batch
, const char *ext_sect
,
1416 CONF
*lconf
, int verbose
, unsigned long certopt
,
1417 unsigned long nameopt
, int default_op
, int ext_copy
)
1420 X509_REQ
*rreq
= NULL
;
1421 EVP_PKEY
*pktmp
= NULL
;
1424 if ((req
= load_cert(infile
, FORMAT_PEM
, infile
)) == NULL
)
1427 X509_print(bio_err
, req
);
1429 BIO_printf(bio_err
, "Check that the request matches the signature\n");
1431 if ((pktmp
= X509_get0_pubkey(req
)) == NULL
) {
1432 BIO_printf(bio_err
, "error unpacking public key\n");
1435 i
= X509_verify(req
, pktmp
);
1438 BIO_printf(bio_err
, "Signature verification problems....\n");
1443 BIO_printf(bio_err
, "Signature did not match the certificate\n");
1446 BIO_printf(bio_err
, "Signature ok\n");
1449 if ((rreq
= X509_to_X509_REQ(req
, NULL
, NULL
)) == NULL
)
1452 ok
= do_body(xret
, pkey
, x509
, dgst
, sigopts
, policy
, db
, serial
, subj
,
1453 chtype
, multirdn
, email_dn
, startdate
, enddate
, days
, batch
,
1454 verbose
, rreq
, ext_sect
, lconf
, certopt
, nameopt
, default_op
,
1458 X509_REQ_free(rreq
);
1463 static int do_body(X509
**xret
, EVP_PKEY
*pkey
, X509
*x509
,
1464 const EVP_MD
*dgst
, STACK_OF(OPENSSL_STRING
) *sigopts
,
1465 STACK_OF(CONF_VALUE
) *policy
, CA_DB
*db
, BIGNUM
*serial
,
1466 const char *subj
, unsigned long chtype
, int multirdn
,
1467 int email_dn
, const char *startdate
, const char *enddate
, long days
,
1468 int batch
, int verbose
, X509_REQ
*req
, const char *ext_sect
,
1469 CONF
*lconf
, unsigned long certopt
, unsigned long nameopt
,
1470 int default_op
, int ext_copy
, int selfsign
)
1472 X509_NAME
*name
= NULL
, *CAname
= NULL
, *subject
= NULL
;
1473 const ASN1_TIME
*tm
;
1474 ASN1_STRING
*str
, *str2
;
1477 X509_NAME_ENTRY
*ne
, *tne
;
1479 int ok
= -1, i
, j
, last
, nid
;
1482 OPENSSL_STRING row
[DB_NUMBER
];
1483 OPENSSL_STRING
*irow
= NULL
;
1484 OPENSSL_STRING
*rrow
= NULL
;
1487 for (i
= 0; i
< DB_NUMBER
; i
++)
1491 X509_NAME
*n
= parse_name(subj
, chtype
, multirdn
);
1494 ERR_print_errors(bio_err
);
1497 X509_REQ_set_subject_name(req
, n
);
1502 BIO_printf(bio_err
, "The Subject's Distinguished Name is as follows\n");
1504 name
= X509_REQ_get_subject_name(req
);
1505 for (i
= 0; i
< X509_NAME_entry_count(name
); i
++) {
1506 ne
= X509_NAME_get_entry(name
, i
);
1507 str
= X509_NAME_ENTRY_get_data(ne
);
1508 obj
= X509_NAME_ENTRY_get_object(ne
);
1509 nid
= OBJ_obj2nid(obj
);
1512 /* assume all type should be strings */
1514 if (str
->type
== V_ASN1_UNIVERSALSTRING
)
1515 ASN1_UNIVERSALSTRING_to_string(str
);
1517 if (str
->type
== V_ASN1_IA5STRING
&& nid
!= NID_pkcs9_emailAddress
)
1518 str
->type
= V_ASN1_T61STRING
;
1520 if (nid
== NID_pkcs9_emailAddress
1521 && str
->type
== V_ASN1_PRINTABLESTRING
)
1522 str
->type
= V_ASN1_IA5STRING
;
1525 /* If no EMAIL is wanted in the subject */
1526 if (nid
== NID_pkcs9_emailAddress
&& !email_dn
)
1529 /* check some things */
1530 if (nid
== NID_pkcs9_emailAddress
&& str
->type
!= V_ASN1_IA5STRING
) {
1532 "\nemailAddress type needs to be of type IA5STRING\n");
1535 if (str
->type
!= V_ASN1_BMPSTRING
&& str
->type
!= V_ASN1_UTF8STRING
) {
1536 j
= ASN1_PRINTABLE_type(str
->data
, str
->length
);
1537 if ((j
== V_ASN1_T61STRING
&& str
->type
!= V_ASN1_T61STRING
) ||
1538 (j
== V_ASN1_IA5STRING
&& str
->type
== V_ASN1_PRINTABLESTRING
))
1541 "\nThe string contains characters that are illegal for the ASN.1 type\n");
1547 old_entry_print(obj
, str
);
1550 /* Ok, now we check the 'policy' stuff. */
1551 if ((subject
= X509_NAME_new()) == NULL
) {
1552 BIO_printf(bio_err
, "Memory allocation failure\n");
1556 /* take a copy of the issuer name before we mess with it. */
1558 CAname
= X509_NAME_dup(name
);
1560 CAname
= X509_NAME_dup(X509_get_subject_name(x509
));
1565 for (i
= 0; i
< sk_CONF_VALUE_num(policy
); i
++) {
1566 cv
= sk_CONF_VALUE_value(policy
, i
); /* get the object id */
1567 if ((j
= OBJ_txt2nid(cv
->name
)) == NID_undef
) {
1569 "%s:unknown object type in 'policy' configuration\n",
1573 obj
= OBJ_nid2obj(j
);
1577 X509_NAME_ENTRY
*push
= NULL
;
1579 /* lookup the object in the supplied name list */
1580 j
= X509_NAME_get_index_by_OBJ(name
, obj
, last
);
1586 tne
= X509_NAME_get_entry(name
, j
);
1590 /* depending on the 'policy', decide what to do. */
1591 if (strcmp(cv
->value
, "optional") == 0) {
1594 } else if (strcmp(cv
->value
, "supplied") == 0) {
1597 "The %s field needed to be supplied and was missing\n",
1603 } else if (strcmp(cv
->value
, "match") == 0) {
1608 "The mandatory %s field was missing\n",
1616 j
= X509_NAME_get_index_by_OBJ(CAname
, obj
, last2
);
1617 if ((j
< 0) && (last2
== -1)) {
1619 "The %s field does not exist in the CA certificate,\n"
1620 "the 'policy' is misconfigured\n", cv
->name
);
1624 push
= X509_NAME_get_entry(CAname
, j
);
1625 str
= X509_NAME_ENTRY_get_data(tne
);
1626 str2
= X509_NAME_ENTRY_get_data(push
);
1628 if (ASN1_STRING_cmp(str
, str2
) != 0)
1633 "The %s field is different between\n"
1634 "CA certificate (%s) and the request (%s)\n",
1636 ((str2
== NULL
) ? "NULL" : (char *)str2
->data
),
1637 ((str
== NULL
) ? "NULL" : (char *)str
->data
));
1642 "%s:invalid type in 'policy' configuration\n",
1648 if (!X509_NAME_add_entry(subject
, push
, -1, 0)) {
1649 BIO_printf(bio_err
, "Memory allocation failure\n");
1659 X509_NAME_free(subject
);
1660 /* subject=X509_NAME_dup(X509_REQ_get_subject_name(req)); */
1661 subject
= X509_NAME_dup(name
);
1662 if (subject
== NULL
)
1666 /* We are now totally happy, lets make and sign the certificate */
1669 "Everything appears to be ok, creating and signing the certificate\n");
1671 if ((ret
= X509_new()) == NULL
)
1675 /* Make it an X509 v3 certificate. */
1676 if (!X509_set_version(ret
, 2))
1680 if (BN_to_ASN1_INTEGER(serial
, X509_get_serialNumber(ret
)) == NULL
)
1683 if (!X509_set_issuer_name(ret
, subject
))
1686 if (!X509_set_issuer_name(ret
, X509_get_subject_name(x509
)))
1690 if (!set_cert_times(ret
, startdate
, enddate
, days
))
1693 if (enddate
!= NULL
) {
1696 if (!ASN1_TIME_diff(&tdays
, NULL
, NULL
, X509_get0_notAfter(ret
)))
1701 if (!X509_set_subject_name(ret
, subject
))
1704 pktmp
= X509_REQ_get0_pubkey(req
);
1705 i
= X509_set_pubkey(ret
, pktmp
);
1709 /* Lets add the extensions, if there are any */
1713 /* Initialize the context structure */
1715 X509V3_set_ctx(&ctx
, ret
, ret
, req
, NULL
, 0);
1717 X509V3_set_ctx(&ctx
, x509
, ret
, req
, NULL
, 0);
1719 if (extconf
!= NULL
) {
1721 BIO_printf(bio_err
, "Extra configuration file found\n");
1723 /* Use the extconf configuration db LHASH */
1724 X509V3_set_nconf(&ctx
, extconf
);
1726 /* Test the structure (needed?) */
1727 /* X509V3_set_ctx_test(&ctx); */
1729 /* Adds exts contained in the configuration file */
1730 if (!X509V3_EXT_add_nconf(extconf
, &ctx
, ext_sect
, ret
)) {
1732 "ERROR: adding extensions in section %s\n",
1734 ERR_print_errors(bio_err
);
1739 "Successfully added extensions from file.\n");
1740 } else if (ext_sect
) {
1741 /* We found extensions to be set from config file */
1742 X509V3_set_nconf(&ctx
, lconf
);
1744 if (!X509V3_EXT_add_nconf(lconf
, &ctx
, ext_sect
, ret
)) {
1746 "ERROR: adding extensions in section %s\n",
1748 ERR_print_errors(bio_err
);
1754 "Successfully added extensions from config\n");
1758 /* Copy extensions from request (if any) */
1760 if (!copy_extensions(ret
, req
, ext_copy
)) {
1761 BIO_printf(bio_err
, "ERROR: adding extensions from request\n");
1762 ERR_print_errors(bio_err
);
1767 const STACK_OF(X509_EXTENSION
) *exts
= X509_get0_extensions(ret
);
1769 if (exts
!= NULL
&& sk_X509_EXTENSION_num(exts
) > 0)
1770 /* Make it an X509 v3 certificate. */
1771 if (!X509_set_version(ret
, 2))
1777 "The subject name appears to be ok, checking data base for clashes\n");
1779 /* Build the correct Subject if no e-mail is wanted in the subject. */
1781 X509_NAME_ENTRY
*tmpne
;
1782 X509_NAME
*dn_subject
;
1785 * Its best to dup the subject DN and then delete any email addresses
1786 * because this retains its structure.
1788 if ((dn_subject
= X509_NAME_dup(subject
)) == NULL
) {
1789 BIO_printf(bio_err
, "Memory allocation failure\n");
1793 while ((i
= X509_NAME_get_index_by_NID(dn_subject
,
1794 NID_pkcs9_emailAddress
,
1796 tmpne
= X509_NAME_delete_entry(dn_subject
, i
--);
1797 X509_NAME_ENTRY_free(tmpne
);
1800 if (!X509_set_subject_name(ret
, dn_subject
)) {
1801 X509_NAME_free(dn_subject
);
1804 X509_NAME_free(dn_subject
);
1807 row
[DB_name
] = X509_NAME_oneline(X509_get_subject_name(ret
), NULL
, 0);
1808 if (row
[DB_name
] == NULL
) {
1809 BIO_printf(bio_err
, "Memory allocation failure\n");
1813 if (BN_is_zero(serial
))
1814 row
[DB_serial
] = OPENSSL_strdup("00");
1816 row
[DB_serial
] = BN_bn2hex(serial
);
1817 if (row
[DB_serial
] == NULL
) {
1818 BIO_printf(bio_err
, "Memory allocation failure\n");
1822 if (row
[DB_name
][0] == '\0') {
1824 * An empty subject! We'll use the serial number instead. If
1825 * unique_subject is in use then we don't want different entries with
1826 * empty subjects matching each other.
1828 OPENSSL_free(row
[DB_name
]);
1829 row
[DB_name
] = OPENSSL_strdup(row
[DB_serial
]);
1830 if (row
[DB_name
] == NULL
) {
1831 BIO_printf(bio_err
, "Memory allocation failure\n");
1836 if (db
->attributes
.unique_subject
) {
1837 OPENSSL_STRING
*crow
= row
;
1839 rrow
= TXT_DB_get_by_index(db
->db
, DB_name
, crow
);
1842 "ERROR:There is already a certificate for %s\n",
1847 rrow
= TXT_DB_get_by_index(db
->db
, DB_serial
, row
);
1850 "ERROR:Serial number %s has already been issued,\n",
1853 " check the database/serial_file for corruption\n");
1858 BIO_printf(bio_err
, "The matching entry has the following details\n");
1859 if (rrow
[DB_type
][0] == DB_TYPE_EXP
)
1861 else if (rrow
[DB_type
][0] == DB_TYPE_REV
)
1863 else if (rrow
[DB_type
][0] == DB_TYPE_VAL
)
1866 p
= "\ninvalid type, Data base error\n";
1867 BIO_printf(bio_err
, "Type :%s\n", p
);;
1868 if (rrow
[DB_type
][0] == DB_TYPE_REV
) {
1869 p
= rrow
[DB_exp_date
];
1872 BIO_printf(bio_err
, "Was revoked on:%s\n", p
);
1874 p
= rrow
[DB_exp_date
];
1877 BIO_printf(bio_err
, "Expires on :%s\n", p
);
1878 p
= rrow
[DB_serial
];
1881 BIO_printf(bio_err
, "Serial Number :%s\n", p
);
1885 BIO_printf(bio_err
, "File name :%s\n", p
);
1889 BIO_printf(bio_err
, "Subject Name :%s\n", p
);
1890 ok
= -1; /* This is now a 'bad' error. */
1895 BIO_printf(bio_err
, "Certificate Details:\n");
1897 * Never print signature details because signature not present
1899 certopt
|= X509_FLAG_NO_SIGDUMP
| X509_FLAG_NO_SIGNAME
;
1900 X509_print_ex(bio_err
, ret
, nameopt
, certopt
);
1903 BIO_printf(bio_err
, "Certificate is to be certified until ");
1904 ASN1_TIME_print(bio_err
, X509_get0_notAfter(ret
));
1906 BIO_printf(bio_err
, " (%ld days)", days
);
1907 BIO_printf(bio_err
, "\n");
1911 BIO_printf(bio_err
, "Sign the certificate? [y/n]:");
1912 (void)BIO_flush(bio_err
);
1914 if (fgets(buf
, sizeof(buf
), stdin
) == NULL
) {
1916 "CERTIFICATE WILL NOT BE CERTIFIED: I/O error\n");
1920 if (!(buf
[0] == 'y' || buf
[0] == 'Y')) {
1921 BIO_printf(bio_err
, "CERTIFICATE WILL NOT BE CERTIFIED\n");
1927 pktmp
= X509_get0_pubkey(ret
);
1928 if (EVP_PKEY_missing_parameters(pktmp
) &&
1929 !EVP_PKEY_missing_parameters(pkey
))
1930 EVP_PKEY_copy_parameters(pktmp
, pkey
);
1932 if (!do_X509_sign(ret
, pkey
, dgst
, sigopts
))
1935 /* We now just add it to the database as DB_TYPE_VAL('V') */
1936 row
[DB_type
] = OPENSSL_strdup("V");
1937 tm
= X509_get0_notAfter(ret
);
1938 row
[DB_exp_date
] = app_malloc(tm
->length
+ 1, "row expdate");
1939 memcpy(row
[DB_exp_date
], tm
->data
, tm
->length
);
1940 row
[DB_exp_date
][tm
->length
] = '\0';
1941 row
[DB_rev_date
] = NULL
;
1942 row
[DB_file
] = OPENSSL_strdup("unknown");
1943 if ((row
[DB_type
] == NULL
) || (row
[DB_exp_date
] == NULL
) ||
1944 (row
[DB_file
] == NULL
) || (row
[DB_name
] == NULL
)) {
1945 BIO_printf(bio_err
, "Memory allocation failure\n");
1949 irow
= app_malloc(sizeof(*irow
) * (DB_NUMBER
+ 1), "row space");
1950 for (i
= 0; i
< DB_NUMBER
; i
++)
1952 irow
[DB_NUMBER
] = NULL
;
1954 if (!TXT_DB_insert(db
->db
, irow
)) {
1955 BIO_printf(bio_err
, "failed to update database\n");
1956 BIO_printf(bio_err
, "TXT_DB error number %ld\n", db
->db
->error
);
1963 for (i
= 0; i
< DB_NUMBER
; i
++)
1964 OPENSSL_free(row
[i
]);
1968 X509_NAME_free(CAname
);
1969 X509_NAME_free(subject
);
1977 static void write_new_certificate(BIO
*bp
, X509
*x
, int output_der
, int notext
)
1981 (void)i2d_X509_bio(bp
, x
);
1986 PEM_write_bio_X509(bp
, x
);
1989 static int certify_spkac(X509
**xret
, const char *infile
, EVP_PKEY
*pkey
,
1990 X509
*x509
, const EVP_MD
*dgst
,
1991 STACK_OF(OPENSSL_STRING
) *sigopts
,
1992 STACK_OF(CONF_VALUE
) *policy
, CA_DB
*db
,
1993 BIGNUM
*serial
, const char *subj
, unsigned long chtype
,
1994 int multirdn
, int email_dn
, const char *startdate
,
1995 const char *enddate
, long days
, const char *ext_sect
,
1996 CONF
*lconf
, int verbose
, unsigned long certopt
,
1997 unsigned long nameopt
, int default_op
, int ext_copy
)
1999 STACK_OF(CONF_VALUE
) *sk
= NULL
;
2000 LHASH_OF(CONF_VALUE
) *parms
= NULL
;
2001 X509_REQ
*req
= NULL
;
2002 CONF_VALUE
*cv
= NULL
;
2003 NETSCAPE_SPKI
*spki
= NULL
;
2005 EVP_PKEY
*pktmp
= NULL
;
2006 X509_NAME
*n
= NULL
;
2007 X509_NAME_ENTRY
*ne
= NULL
;
2013 * Load input file into a hash table. (This is just an easy
2014 * way to read and parse the file, then put it into a convenient
2017 parms
= CONF_load(NULL
, infile
, &errline
);
2018 if (parms
== NULL
) {
2019 BIO_printf(bio_err
, "error on line %ld of %s\n", errline
, infile
);
2020 ERR_print_errors(bio_err
);
2024 sk
= CONF_get_section(parms
, "default");
2025 if (sk_CONF_VALUE_num(sk
) == 0) {
2026 BIO_printf(bio_err
, "no name/value pairs found in %s\n", infile
);
2031 * Now create a dummy X509 request structure. We don't actually
2032 * have an X509 request, but we have many of the components
2033 * (a public key, various DN components). The idea is that we
2034 * put these components into the right X509 request structure
2035 * and we can use the same code as if you had a real X509 request.
2037 req
= X509_REQ_new();
2039 ERR_print_errors(bio_err
);
2044 * Build up the subject name set.
2046 n
= X509_REQ_get_subject_name(req
);
2049 if (sk_CONF_VALUE_num(sk
) <= i
)
2052 cv
= sk_CONF_VALUE_value(sk
, i
);
2055 * Skip past any leading X. X: X, etc to allow for multiple instances
2057 for (buf
= cv
->name
; *buf
; buf
++)
2058 if ((*buf
== ':') || (*buf
== ',') || (*buf
== '.')) {
2066 if ((nid
= OBJ_txt2nid(type
)) == NID_undef
) {
2067 if (strcmp(type
, "SPKAC") == 0) {
2068 spki
= NETSCAPE_SPKI_b64_decode(cv
->value
, -1);
2071 "unable to load Netscape SPKAC structure\n");
2072 ERR_print_errors(bio_err
);
2079 if (!X509_NAME_add_entry_by_NID(n
, nid
, chtype
,
2080 (unsigned char *)buf
, -1, -1, 0))
2084 BIO_printf(bio_err
, "Netscape SPKAC structure not found in %s\n",
2090 * Now extract the key from the SPKI structure.
2093 BIO_printf(bio_err
, "Check that the SPKAC request matches the signature\n");
2095 if ((pktmp
= NETSCAPE_SPKI_get_pubkey(spki
)) == NULL
) {
2096 BIO_printf(bio_err
, "error unpacking SPKAC public key\n");
2100 j
= NETSCAPE_SPKI_verify(spki
, pktmp
);
2102 EVP_PKEY_free(pktmp
);
2104 "signature verification failed on SPKAC public key\n");
2107 BIO_printf(bio_err
, "Signature ok\n");
2109 X509_REQ_set_pubkey(req
, pktmp
);
2110 EVP_PKEY_free(pktmp
);
2111 ok
= do_body(xret
, pkey
, x509
, dgst
, sigopts
, policy
, db
, serial
, subj
,
2112 chtype
, multirdn
, email_dn
, startdate
, enddate
, days
, 1,
2113 verbose
, req
, ext_sect
, lconf
, certopt
, nameopt
, default_op
,
2118 NETSCAPE_SPKI_free(spki
);
2119 X509_NAME_ENTRY_free(ne
);
2124 static int check_time_format(const char *str
)
2126 return ASN1_TIME_set_string(NULL
, str
);
2129 static int do_revoke(X509
*x509
, CA_DB
*db
, REVINFO_TYPE rev_type
,
2132 const ASN1_TIME
*tm
= NULL
;
2133 char *row
[DB_NUMBER
], **rrow
, **irow
;
2134 char *rev_str
= NULL
;
2138 for (i
= 0; i
< DB_NUMBER
; i
++)
2140 row
[DB_name
] = X509_NAME_oneline(X509_get_subject_name(x509
), NULL
, 0);
2141 bn
= ASN1_INTEGER_to_BN(X509_get_serialNumber(x509
), NULL
);
2145 row
[DB_serial
] = OPENSSL_strdup("00");
2147 row
[DB_serial
] = BN_bn2hex(bn
);
2149 if (row
[DB_name
] != NULL
&& row
[DB_name
][0] == '\0') {
2150 /* Entries with empty Subjects actually use the serial number instead */
2151 OPENSSL_free(row
[DB_name
]);
2152 row
[DB_name
] = OPENSSL_strdup(row
[DB_serial
]);
2154 if ((row
[DB_name
] == NULL
) || (row
[DB_serial
] == NULL
)) {
2155 BIO_printf(bio_err
, "Memory allocation failure\n");
2159 * We have to lookup by serial number because name lookup skips revoked
2162 rrow
= TXT_DB_get_by_index(db
->db
, DB_serial
, row
);
2165 "Adding Entry with serial number %s to DB for %s\n",
2166 row
[DB_serial
], row
[DB_name
]);
2168 /* We now just add it to the database as DB_TYPE_REV('V') */
2169 row
[DB_type
] = OPENSSL_strdup("V");
2170 tm
= X509_get0_notAfter(x509
);
2171 row
[DB_exp_date
] = app_malloc(tm
->length
+ 1, "row exp_data");
2172 memcpy(row
[DB_exp_date
], tm
->data
, tm
->length
);
2173 row
[DB_exp_date
][tm
->length
] = '\0';
2174 row
[DB_rev_date
] = NULL
;
2175 row
[DB_file
] = OPENSSL_strdup("unknown");
2177 if (row
[DB_type
] == NULL
|| row
[DB_file
] == NULL
) {
2178 BIO_printf(bio_err
, "Memory allocation failure\n");
2182 irow
= app_malloc(sizeof(*irow
) * (DB_NUMBER
+ 1), "row ptr");
2183 for (i
= 0; i
< DB_NUMBER
; i
++)
2185 irow
[DB_NUMBER
] = NULL
;
2187 if (!TXT_DB_insert(db
->db
, irow
)) {
2188 BIO_printf(bio_err
, "failed to update database\n");
2189 BIO_printf(bio_err
, "TXT_DB error number %ld\n", db
->db
->error
);
2194 for (i
= 0; i
< DB_NUMBER
; i
++)
2197 /* Revoke Certificate */
2198 if (rev_type
== REV_VALID
)
2201 /* Retry revocation after DB insertion */
2202 ok
= do_revoke(x509
, db
, rev_type
, value
);
2206 } else if (index_name_cmp_noconst(row
, rrow
)) {
2207 BIO_printf(bio_err
, "ERROR:name does not match %s\n", row
[DB_name
]);
2209 } else if (rev_type
== REV_VALID
) {
2210 BIO_printf(bio_err
, "ERROR:Already present, serial number %s\n",
2213 } else if (rrow
[DB_type
][0] == DB_TYPE_REV
) {
2214 BIO_printf(bio_err
, "ERROR:Already revoked, serial number %s\n",
2218 BIO_printf(bio_err
, "Revoking Certificate %s.\n", rrow
[DB_serial
]);
2219 rev_str
= make_revocation_str(rev_type
, value
);
2221 BIO_printf(bio_err
, "Error in revocation arguments\n");
2224 rrow
[DB_type
][0] = DB_TYPE_REV
;
2225 rrow
[DB_type
][1] = '\0';
2226 rrow
[DB_rev_date
] = rev_str
;
2230 for (i
= 0; i
< DB_NUMBER
; i
++)
2231 OPENSSL_free(row
[i
]);
2235 static int get_certificate_status(const char *serial
, CA_DB
*db
)
2237 char *row
[DB_NUMBER
], **rrow
;
2239 size_t serial_len
= strlen(serial
);
2241 /* Free Resources */
2242 for (i
= 0; i
< DB_NUMBER
; i
++)
2245 /* Malloc needed char spaces */
2246 row
[DB_serial
] = app_malloc(serial_len
+ 2, "row serial#");
2248 if (serial_len
% 2) {
2250 * Set the first char to 0
2252 row
[DB_serial
][0] = '0';
2254 /* Copy String from serial to row[DB_serial] */
2255 memcpy(row
[DB_serial
] + 1, serial
, serial_len
);
2256 row
[DB_serial
][serial_len
+ 1] = '\0';
2258 /* Copy String from serial to row[DB_serial] */
2259 memcpy(row
[DB_serial
], serial
, serial_len
);
2260 row
[DB_serial
][serial_len
] = '\0';
2263 /* Make it Upper Case */
2264 make_uppercase(row
[DB_serial
]);
2268 /* Search for the certificate */
2269 rrow
= TXT_DB_get_by_index(db
->db
, DB_serial
, row
);
2271 BIO_printf(bio_err
, "Serial %s not present in db.\n", row
[DB_serial
]);
2274 } else if (rrow
[DB_type
][0] == DB_TYPE_VAL
) {
2275 BIO_printf(bio_err
, "%s=Valid (%c)\n",
2276 row
[DB_serial
], rrow
[DB_type
][0]);
2278 } else if (rrow
[DB_type
][0] == DB_TYPE_REV
) {
2279 BIO_printf(bio_err
, "%s=Revoked (%c)\n",
2280 row
[DB_serial
], rrow
[DB_type
][0]);
2282 } else if (rrow
[DB_type
][0] == DB_TYPE_EXP
) {
2283 BIO_printf(bio_err
, "%s=Expired (%c)\n",
2284 row
[DB_serial
], rrow
[DB_type
][0]);
2286 } else if (rrow
[DB_type
][0] == DB_TYPE_SUSP
) {
2287 BIO_printf(bio_err
, "%s=Suspended (%c)\n",
2288 row
[DB_serial
], rrow
[DB_type
][0]);
2291 BIO_printf(bio_err
, "%s=Unknown (%c).\n",
2292 row
[DB_serial
], rrow
[DB_type
][0]);
2296 for (i
= 0; i
< DB_NUMBER
; i
++) {
2297 OPENSSL_free(row
[i
]);
2302 static int do_updatedb(CA_DB
*db
)
2304 ASN1_UTCTIME
*a_tm
= NULL
;
2306 int db_y2k
, a_y2k
; /* flags = 1 if y >= 2000 */
2307 char **rrow
, *a_tm_s
;
2309 a_tm
= ASN1_UTCTIME_new();
2313 /* get actual time and make a string */
2314 if (X509_gmtime_adj(a_tm
, 0) == NULL
) {
2315 ASN1_UTCTIME_free(a_tm
);
2318 a_tm_s
= app_malloc(a_tm
->length
+ 1, "time string");
2320 memcpy(a_tm_s
, a_tm
->data
, a_tm
->length
);
2321 a_tm_s
[a_tm
->length
] = '\0';
2323 if (strncmp(a_tm_s
, "49", 2) <= 0)
2328 for (i
= 0; i
< sk_OPENSSL_PSTRING_num(db
->db
->data
); i
++) {
2329 rrow
= sk_OPENSSL_PSTRING_value(db
->db
->data
, i
);
2331 if (rrow
[DB_type
][0] == DB_TYPE_VAL
) {
2332 /* ignore entries that are not valid */
2333 if (strncmp(rrow
[DB_exp_date
], "49", 2) <= 0)
2338 if (db_y2k
== a_y2k
) {
2339 /* all on the same y2k side */
2340 if (strcmp(rrow
[DB_exp_date
], a_tm_s
) <= 0) {
2341 rrow
[DB_type
][0] = DB_TYPE_EXP
;
2342 rrow
[DB_type
][1] = '\0';
2345 BIO_printf(bio_err
, "%s=Expired\n", rrow
[DB_serial
]);
2347 } else if (db_y2k
< a_y2k
) {
2348 rrow
[DB_type
][0] = DB_TYPE_EXP
;
2349 rrow
[DB_type
][1] = '\0';
2352 BIO_printf(bio_err
, "%s=Expired\n", rrow
[DB_serial
]);
2358 ASN1_UTCTIME_free(a_tm
);
2359 OPENSSL_free(a_tm_s
);
2363 static const char *crl_reasons
[] = {
2364 /* CRL reason strings */
2368 "affiliationChanged",
2370 "cessationOfOperation",
2373 /* Additional pseudo reasons */
2379 #define NUM_REASONS OSSL_NELEM(crl_reasons)
2382 * Given revocation information convert to a DB string. The format of the
2383 * string is: revtime[,reason,extra]. Where 'revtime' is the revocation time
2384 * (the current time). 'reason' is the optional CRL reason and 'extra' is any
2385 * additional argument
2388 static char *make_revocation_str(REVINFO_TYPE rev_type
, const char *rev_arg
)
2391 const char *reason
= NULL
, *other
= NULL
;
2393 ASN1_UTCTIME
*revtm
= NULL
;
2401 case REV_CRL_REASON
:
2402 for (i
= 0; i
< 8; i
++) {
2403 if (strcasecmp(rev_arg
, crl_reasons
[i
]) == 0) {
2404 reason
= crl_reasons
[i
];
2408 if (reason
== NULL
) {
2409 BIO_printf(bio_err
, "Unknown CRL reason %s\n", rev_arg
);
2415 /* Argument is an OID */
2416 otmp
= OBJ_txt2obj(rev_arg
, 0);
2417 ASN1_OBJECT_free(otmp
);
2420 BIO_printf(bio_err
, "Invalid object identifier %s\n", rev_arg
);
2424 reason
= "holdInstruction";
2428 case REV_KEY_COMPROMISE
:
2429 case REV_CA_COMPROMISE
:
2430 /* Argument is the key compromise time */
2431 if (!ASN1_GENERALIZEDTIME_set_string(NULL
, rev_arg
)) {
2433 "Invalid time format %s. Need YYYYMMDDHHMMSSZ\n",
2438 if (rev_type
== REV_KEY_COMPROMISE
)
2441 reason
= "CAkeyTime";
2446 revtm
= X509_gmtime_adj(NULL
, 0);
2451 i
= revtm
->length
+ 1;
2454 i
+= strlen(reason
) + 1;
2456 i
+= strlen(other
) + 1;
2458 str
= app_malloc(i
, "revocation reason");
2459 OPENSSL_strlcpy(str
, (char *)revtm
->data
, i
);
2461 OPENSSL_strlcat(str
, ",", i
);
2462 OPENSSL_strlcat(str
, reason
, i
);
2465 OPENSSL_strlcat(str
, ",", i
);
2466 OPENSSL_strlcat(str
, other
, i
);
2468 ASN1_UTCTIME_free(revtm
);
2473 * Convert revocation field to X509_REVOKED entry
2477 * 2 OK and some extensions added (i.e. V2 CRL)
2480 static int make_revoked(X509_REVOKED
*rev
, const char *str
)
2483 int reason_code
= -1;
2485 ASN1_OBJECT
*hold
= NULL
;
2486 ASN1_GENERALIZEDTIME
*comp_time
= NULL
;
2487 ASN1_ENUMERATED
*rtmp
= NULL
;
2489 ASN1_TIME
*revDate
= NULL
;
2491 i
= unpack_revinfo(&revDate
, &reason_code
, &hold
, &comp_time
, str
);
2496 if (rev
&& !X509_REVOKED_set_revocationDate(rev
, revDate
))
2499 if (rev
&& (reason_code
!= OCSP_REVOKED_STATUS_NOSTATUS
)) {
2500 rtmp
= ASN1_ENUMERATED_new();
2501 if (rtmp
== NULL
|| !ASN1_ENUMERATED_set(rtmp
, reason_code
))
2503 if (!X509_REVOKED_add1_ext_i2d(rev
, NID_crl_reason
, rtmp
, 0, 0))
2507 if (rev
&& comp_time
) {
2508 if (!X509_REVOKED_add1_ext_i2d
2509 (rev
, NID_invalidity_date
, comp_time
, 0, 0))
2513 if (!X509_REVOKED_add1_ext_i2d
2514 (rev
, NID_hold_instruction_code
, hold
, 0, 0))
2518 if (reason_code
!= OCSP_REVOKED_STATUS_NOSTATUS
)
2526 ASN1_OBJECT_free(hold
);
2527 ASN1_GENERALIZEDTIME_free(comp_time
);
2528 ASN1_ENUMERATED_free(rtmp
);
2529 ASN1_TIME_free(revDate
);
2534 static int old_entry_print(const ASN1_OBJECT
*obj
, const ASN1_STRING
*str
)
2536 char buf
[25], *pbuf
;
2540 j
= i2a_ASN1_OBJECT(bio_err
, obj
);
2542 for (j
= 22 - j
; j
> 0; j
--)
2546 BIO_puts(bio_err
, buf
);
2548 if (str
->type
== V_ASN1_PRINTABLESTRING
)
2549 BIO_printf(bio_err
, "PRINTABLE:'");
2550 else if (str
->type
== V_ASN1_T61STRING
)
2551 BIO_printf(bio_err
, "T61STRING:'");
2552 else if (str
->type
== V_ASN1_IA5STRING
)
2553 BIO_printf(bio_err
, "IA5STRING:'");
2554 else if (str
->type
== V_ASN1_UNIVERSALSTRING
)
2555 BIO_printf(bio_err
, "UNIVERSALSTRING:'");
2557 BIO_printf(bio_err
, "ASN.1 %2d:'", str
->type
);
2559 p
= (const char *)str
->data
;
2560 for (j
= str
->length
; j
> 0; j
--) {
2561 if ((*p
>= ' ') && (*p
<= '~'))
2562 BIO_printf(bio_err
, "%c", *p
);
2564 BIO_printf(bio_err
, "\\0x%02X", *p
);
2565 else if ((unsigned char)*p
== 0xf7)
2566 BIO_printf(bio_err
, "^?");
2568 BIO_printf(bio_err
, "^%c", *p
+ '@');
2571 BIO_printf(bio_err
, "'\n");
2575 int unpack_revinfo(ASN1_TIME
**prevtm
, int *preason
, ASN1_OBJECT
**phold
,
2576 ASN1_GENERALIZEDTIME
**pinvtm
, const char *str
)
2579 char *rtime_str
, *reason_str
= NULL
, *arg_str
= NULL
, *p
;
2580 int reason_code
= -1;
2583 ASN1_OBJECT
*hold
= NULL
;
2584 ASN1_GENERALIZEDTIME
*comp_time
= NULL
;
2586 tmp
= OPENSSL_strdup(str
);
2588 BIO_printf(bio_err
, "memory allocation failure\n");
2592 p
= strchr(tmp
, ',');
2608 *prevtm
= ASN1_UTCTIME_new();
2609 if (*prevtm
== NULL
) {
2610 BIO_printf(bio_err
, "memory allocation failure\n");
2613 if (!ASN1_UTCTIME_set_string(*prevtm
, rtime_str
)) {
2614 BIO_printf(bio_err
, "invalid revocation date %s\n", rtime_str
);
2619 for (i
= 0; i
< NUM_REASONS
; i
++) {
2620 if (strcasecmp(reason_str
, crl_reasons
[i
]) == 0) {
2625 if (reason_code
== OCSP_REVOKED_STATUS_NOSTATUS
) {
2626 BIO_printf(bio_err
, "invalid reason code %s\n", reason_str
);
2630 if (reason_code
== 7) {
2631 reason_code
= OCSP_REVOKED_STATUS_REMOVEFROMCRL
;
2632 } else if (reason_code
== 8) { /* Hold instruction */
2634 BIO_printf(bio_err
, "missing hold instruction\n");
2637 reason_code
= OCSP_REVOKED_STATUS_CERTIFICATEHOLD
;
2638 hold
= OBJ_txt2obj(arg_str
, 0);
2641 BIO_printf(bio_err
, "invalid object identifier %s\n", arg_str
);
2647 ASN1_OBJECT_free(hold
);
2648 } else if ((reason_code
== 9) || (reason_code
== 10)) {
2650 BIO_printf(bio_err
, "missing compromised time\n");
2653 comp_time
= ASN1_GENERALIZEDTIME_new();
2654 if (comp_time
== NULL
) {
2655 BIO_printf(bio_err
, "memory allocation failure\n");
2658 if (!ASN1_GENERALIZEDTIME_set_string(comp_time
, arg_str
)) {
2659 BIO_printf(bio_err
, "invalid compromised time %s\n", arg_str
);
2662 if (reason_code
== 9)
2663 reason_code
= OCSP_REVOKED_STATUS_KEYCOMPROMISE
;
2665 reason_code
= OCSP_REVOKED_STATUS_CACOMPROMISE
;
2670 *preason
= reason_code
;
2672 *pinvtm
= comp_time
;
2681 ASN1_GENERALIZEDTIME_free(comp_time
);