]>
Commit | Line | Data |
---|---|---|
846e33c7 | 1 | /* |
6738bf14 | 2 | * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. |
d02b48c6 | 3 | * |
dffa7520 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
846e33c7 RS |
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 | |
d02b48c6 | 8 | */ |
d02b48c6 RE |
9 | #include <stdio.h> |
10 | #include <stdlib.h> | |
11 | #include <string.h> | |
c67cdb50 | 12 | #include <ctype.h> |
d02b48c6 | 13 | #include <sys/types.h> |
ec577822 BM |
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> | |
a6b7ffdd | 23 | #include <openssl/ocsp.h> |
ec577822 | 24 | #include <openssl/pem.h> |
d02b48c6 RE |
25 | |
26 | #ifndef W_OK | |
0f113f3e | 27 | # ifdef OPENSSL_SYS_VMS |
e627a13c | 28 | # include <unistd.h> |
1fbab1dc | 29 | # elif !defined(OPENSSL_SYS_VXWORKS) && !defined(OPENSSL_SYS_WINDOWS) |
0f113f3e MC |
30 | # include <sys/file.h> |
31 | # endif | |
7d7d2cbc UM |
32 | #endif |
33 | ||
d610d27f | 34 | #include "apps.h" |
dab2cd68 | 35 | #include "progs.h" |
d610d27f | 36 | |
7d7d2cbc | 37 | #ifndef W_OK |
0f113f3e | 38 | # define F_OK 0 |
0f113f3e MC |
39 | # define W_OK 2 |
40 | # define R_OK 4 | |
d02b48c6 RE |
41 | #endif |
42 | ||
dfc3ffe5 F |
43 | #ifndef PATH_MAX |
44 | # define PATH_MAX 4096 | |
45 | #endif | |
d02b48c6 | 46 | |
070c2332 | 47 | #define BASE_SECTION "ca" |
d02b48c6 | 48 | |
0f113f3e MC |
49 | #define ENV_DEFAULT_CA "default_ca" |
50 | ||
070c2332 | 51 | #define STRING_MASK "string_mask" |
0f113f3e MC |
52 | #define UTF8_IN "utf8" |
53 | ||
0f113f3e | 54 | #define ENV_NEW_CERTS_DIR "new_certs_dir" |
070c2332 | 55 | #define ENV_CERTIFICATE "certificate" |
0f113f3e | 56 | #define ENV_SERIAL "serial" |
ffb46830 | 57 | #define ENV_RAND_SERIAL "rand_serial" |
0f113f3e | 58 | #define ENV_CRLNUMBER "crlnumber" |
0f113f3e | 59 | #define ENV_PRIVATE_KEY "private_key" |
0f113f3e MC |
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" | |
76 | ||
77 | #define ENV_DATABASE "database" | |
d02b48c6 | 78 | |
a6b7ffdd | 79 | /* Additional revocation information types */ |
06a79af2 F |
80 | typedef enum { |
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 */ | |
87 | } REVINFO_TYPE; | |
0f113f3e | 88 | |
edbff8da | 89 | static char *lookup_conf(const CONF *conf, const char *group, const char *tag); |
cc696296 F |
90 | |
91 | static int certify(X509 **xret, const char *infile, EVP_PKEY *pkey, X509 *x509, | |
0f113f3e MC |
92 | const EVP_MD *dgst, STACK_OF(OPENSSL_STRING) *sigopts, |
93 | STACK_OF(CONF_VALUE) *policy, CA_DB *db, | |
cc696296 F |
94 | BIGNUM *serial, const char *subj, unsigned long chtype, |
95 | int multirdn, int email_dn, const char *startdate, | |
96 | const char *enddate, | |
97 | long days, int batch, const char *ext_sect, CONF *conf, | |
0f113f3e | 98 | int verbose, unsigned long certopt, unsigned long nameopt, |
bc42bd62 PY |
99 | int default_op, int ext_copy, int selfsign, |
100 | unsigned char *sm2_id, size_t sm2idlen); | |
cc696296 | 101 | static int certify_cert(X509 **xret, const char *infile, EVP_PKEY *pkey, X509 *x509, |
0f113f3e MC |
102 | const EVP_MD *dgst, STACK_OF(OPENSSL_STRING) *sigopts, |
103 | STACK_OF(CONF_VALUE) *policy, CA_DB *db, | |
cc696296 F |
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, | |
0f113f3e | 107 | CONF *conf, int verbose, unsigned long certopt, |
a773b52a | 108 | unsigned long nameopt, int default_op, int ext_copy); |
cc696296 | 109 | static int certify_spkac(X509 **xret, const char *infile, EVP_PKEY *pkey, |
0f113f3e MC |
110 | X509 *x509, const EVP_MD *dgst, |
111 | STACK_OF(OPENSSL_STRING) *sigopts, | |
112 | STACK_OF(CONF_VALUE) *policy, CA_DB *db, | |
cc696296 F |
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, | |
0f113f3e MC |
116 | int verbose, unsigned long certopt, |
117 | unsigned long nameopt, int default_op, int ext_copy); | |
0f113f3e MC |
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, | |
cc696296 F |
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, | |
0f113f3e MC |
124 | CONF *conf, unsigned long certopt, unsigned long nameopt, |
125 | int default_op, int ext_copy, int selfsign); | |
f85b68cd RL |
126 | static int get_certificate_status(const char *ser_status, CA_DB *db); |
127 | static int do_updatedb(CA_DB *db); | |
7c337e00 | 128 | static int check_time_format(const char *str); |
06a79af2 F |
129 | static int do_revoke(X509 *x509, CA_DB *db, REVINFO_TYPE rev_type, |
130 | const char *extval); | |
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); | |
69b017f6 | 133 | static int old_entry_print(const ASN1_OBJECT *obj, const ASN1_STRING *str); |
06a79af2 | 134 | static void write_new_certificate(BIO *bp, X509 *x, int output_der, int notext); |
7e1b7485 | 135 | |
0f113f3e | 136 | static CONF *extconf = NULL; |
0f113f3e MC |
137 | static int preserve = 0; |
138 | static int msie_hack = 0; | |
35a99b63 | 139 | |
7e1b7485 RS |
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, | |
ffb46830 | 150 | OPT_RAND_SERIAL, |
6bd4e3f2 | 151 | OPT_R_ENUM, OPT_SM2ID, OPT_SM2HEXID, OPT_PROV_ENUM, |
06a79af2 F |
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 | |
7e1b7485 RS |
154 | } OPTION_CHOICE; |
155 | ||
44c83ebd | 156 | const OPTIONS ca_options[] = { |
92de469f RS |
157 | {OPT_HELP_STR, 1, '-', "Usage: %s [options] [certreq...]\n"}, |
158 | ||
5388f986 | 159 | OPT_SECTION("General"), |
7e1b7485 RS |
160 | {"help", OPT_HELP, '-', "Display this summary"}, |
161 | {"verbose", OPT_VERBOSE, '-', "Verbose output during processing"}, | |
5388f986 RS |
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"}, | |
175 | #endif | |
176 | ||
177 | OPT_SECTION("Configuration"), | |
7e1b7485 RS |
178 | {"config", OPT_CONFIG, 's', "A config file"}, |
179 | {"name", OPT_NAME, 's', "The particular CA definition to use"}, | |
5388f986 RS |
180 | {"policy", OPT_POLICY, 's', "The CA 'policy' to support"}, |
181 | ||
182 | OPT_SECTION("Certificate"), | |
7e1b7485 RS |
183 | {"subj", OPT_SUBJ, 's', "Use arg instead of request's subject"}, |
184 | {"utf8", OPT_UTF8, '-', "Input characters are UTF8 (default ASCII)"}, | |
6755ff11 | 185 | {"create_serial", OPT_CREATE_SERIAL, '-', |
d6073e27 | 186 | "If reading serial fails, create a new random serial"}, |
ffb46830 RS |
187 | {"rand_serial", OPT_RAND_SERIAL, '-', |
188 | "Always create a random serial; do not store it"}, | |
7e1b7485 RS |
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"}, | |
5388f986 RS |
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"}, | |
204 | #endif | |
205 | {"preserveDN", OPT_PRESERVEDN, '-', "Don't re-order the DN"}, | |
206 | {"noemailDN", OPT_NOEMAILDN, '-', "Don't add the EMAIL field to the DN"}, | |
207 | ||
208 | OPT_SECTION("Signing"), | |
7e1b7485 | 209 | {"md", OPT_MD, 's', "md to use; one of md2, md5, sha or sha1"}, |
dd958974 | 210 | {"keyfile", OPT_KEYFILE, 's', "Private key"}, |
7e1b7485 | 211 | {"keyform", OPT_KEYFORM, 'f', "Private key file format (PEM or ENGINE)"}, |
16e1b281 | 212 | {"passin", OPT_PASSIN, 's', "Input file pass phrase source"}, |
7e1b7485 RS |
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"}, | |
16e1b281 | 217 | {"sigopt", OPT_SIGOPT, 's', "Signature parameter in n:v form"}, |
5388f986 RS |
218 | |
219 | OPT_SECTION("Revocation"), | |
7e1b7485 | 220 | {"gencrl", OPT_GENCRL, '-', "Generate a new CRL"}, |
b4b42ce6 F |
221 | {"valid", OPT_VALID, 's', |
222 | "Add a Valid(not-revoked) DB entry about a cert (given in file)"}, | |
7e1b7485 RS |
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)"}, | |
6755ff11 MR |
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"}, | |
5388f986 RS |
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)"}, | |
238 | ||
3ee1eac2 | 239 | OPT_R_OPTIONS, |
6bd4e3f2 | 240 | OPT_PROV_OPTIONS, |
92de469f RS |
241 | |
242 | OPT_PARAMETERS(), | |
243 | {"certreq", 0, 0, "Certificate requests to be signed (optional)"}, | |
7e1b7485 RS |
244 | {NULL} |
245 | }; | |
667ac4ec | 246 | |
7e1b7485 | 247 | int ca_main(int argc, char **argv) |
0f113f3e | 248 | { |
fba13663 | 249 | CONF *conf = NULL; |
0f113f3e | 250 | ENGINE *e = NULL; |
7e1b7485 | 251 | BIGNUM *crlnumber = NULL, *serial = NULL; |
0f113f3e | 252 | EVP_PKEY *pkey = NULL; |
2d87ee68 | 253 | BIO *in = NULL, *out = NULL, *Sout = NULL; |
0f113f3e | 254 | ASN1_INTEGER *tmpser; |
7e1b7485 RS |
255 | ASN1_TIME *tmptm; |
256 | CA_DB *db = NULL; | |
257 | DB_ATTR db_attr; | |
0f113f3e | 258 | STACK_OF(CONF_VALUE) *attribs = NULL; |
0f113f3e | 259 | STACK_OF(OPENSSL_STRING) *sigopts = NULL; |
7e1b7485 RS |
260 | STACK_OF(X509) *cert_sk = NULL; |
261 | X509_CRL *crl = NULL; | |
262 | const EVP_MD *dgst = NULL; | |
fba13663 | 263 | char *configfile = default_config_file, *section = NULL; |
cc01d217 | 264 | char *md = NULL, *policy = NULL, *keyfile = NULL; |
cc696296 F |
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; | |
7e1b7485 | 268 | char *outdir = NULL, *outfile = NULL, *rev_arg = NULL, *ser_status = NULL; |
cc696296 F |
269 | const char *serialfile = NULL, *subj = NULL; |
270 | char *prog, *startdate = NULL, *enddate = NULL; | |
3ee1eac2 | 271 | char *dbfile = NULL, *f; |
b5895815 | 272 | char new_cert[PATH_MAX]; |
3a19b22a | 273 | char tmp[10 + 1] = "\0"; |
333b070e | 274 | char *const *pp; |
7e1b7485 | 275 | const char *p; |
b5895815 | 276 | size_t outdirlen = 0; |
7e1b7485 RS |
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; | |
aabbc24e | 281 | int rand_ser = 0, i, j, selfsign = 0, def_nid, def_ret; |
cc01d217 | 282 | long crldays = 0, crlhours = 0, crlsec = 0, days = 0; |
b5c4209b | 283 | unsigned long chtype = MBSTRING_ASC, certopt = 0; |
7e1b7485 | 284 | X509 *x509 = NULL, *x509p = NULL, *x = NULL; |
06a79af2 | 285 | REVINFO_TYPE rev_type = REV_NONE; |
7e1b7485 RS |
286 | X509_REVOKED *r = NULL; |
287 | OPTION_CHOICE o; | |
bc42bd62 PY |
288 | unsigned char *sm2_id = NULL; |
289 | size_t sm2_idlen = 0; | |
290 | int sm2_free = 0; | |
d02b48c6 | 291 | |
7e1b7485 RS |
292 | prog = opt_init(argc, argv, ca_options); |
293 | while ((o = opt_next()) != OPT_EOF) { | |
294 | switch (o) { | |
8bf78043 F |
295 | case OPT_EOF: |
296 | case OPT_ERR: | |
7e1b7485 | 297 | opthelp: |
8bf78043 F |
298 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); |
299 | goto end; | |
300 | case OPT_HELP: | |
301 | opt_help(ca_options); | |
302 | ret = 0; | |
303 | goto end; | |
304 | case OPT_IN: | |
305 | req = 1; | |
306 | infile = opt_arg(); | |
307 | break; | |
308 | case OPT_OUT: | |
309 | outfile = opt_arg(); | |
310 | break; | |
311 | case OPT_VERBOSE: | |
312 | verbose = 1; | |
313 | break; | |
314 | case OPT_CONFIG: | |
315 | configfile = opt_arg(); | |
316 | break; | |
317 | case OPT_NAME: | |
318 | section = opt_arg(); | |
319 | break; | |
320 | case OPT_SUBJ: | |
321 | subj = opt_arg(); | |
322 | /* preserve=1; */ | |
323 | break; | |
324 | case OPT_UTF8: | |
325 | chtype = MBSTRING_UTF8; | |
326 | break; | |
ffb46830 RS |
327 | case OPT_RAND_SERIAL: |
328 | rand_ser = 1; | |
329 | break; | |
7e1b7485 | 330 | case OPT_CREATE_SERIAL: |
0f113f3e | 331 | create_ser = 1; |
7e1b7485 RS |
332 | break; |
333 | case OPT_MULTIVALUE_RDN: | |
0f113f3e | 334 | multirdn = 1; |
7e1b7485 RS |
335 | break; |
336 | case OPT_STARTDATE: | |
337 | startdate = opt_arg(); | |
338 | break; | |
339 | case OPT_ENDDATE: | |
340 | enddate = opt_arg(); | |
341 | break; | |
342 | case OPT_DAYS: | |
343 | days = atoi(opt_arg()); | |
344 | break; | |
345 | case OPT_MD: | |
346 | md = opt_arg(); | |
347 | break; | |
348 | case OPT_POLICY: | |
349 | policy = opt_arg(); | |
350 | break; | |
351 | case OPT_KEYFILE: | |
352 | keyfile = opt_arg(); | |
353 | break; | |
354 | case OPT_KEYFORM: | |
355 | if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyformat)) | |
356 | goto opthelp; | |
357 | break; | |
358 | case OPT_PASSIN: | |
359 | passinarg = opt_arg(); | |
360 | break; | |
3ee1eac2 RS |
361 | case OPT_R_CASES: |
362 | if (!opt_rand(o)) | |
363 | goto end; | |
364 | break; | |
6bd4e3f2 P |
365 | case OPT_PROV_CASES: |
366 | if (!opt_provider(o)) | |
367 | goto end; | |
368 | break; | |
7e1b7485 RS |
369 | case OPT_KEY: |
370 | key = opt_arg(); | |
371 | break; | |
372 | case OPT_CERT: | |
373 | certfile = opt_arg(); | |
374 | break; | |
375 | case OPT_SELFSIGN: | |
0f113f3e | 376 | selfsign = 1; |
7e1b7485 RS |
377 | break; |
378 | case OPT_OUTDIR: | |
379 | outdir = opt_arg(); | |
380 | break; | |
381 | case OPT_SIGOPT: | |
382 | if (sigopts == NULL) | |
0f113f3e | 383 | sigopts = sk_OPENSSL_STRING_new_null(); |
b5895815 | 384 | if (sigopts == NULL || !sk_OPENSSL_STRING_push(sigopts, opt_arg())) |
7e1b7485 RS |
385 | goto end; |
386 | break; | |
387 | case OPT_NOTEXT: | |
0f113f3e | 388 | notext = 1; |
7e1b7485 RS |
389 | break; |
390 | case OPT_BATCH: | |
0f113f3e | 391 | batch = 1; |
7e1b7485 RS |
392 | break; |
393 | case OPT_PRESERVEDN: | |
0f113f3e | 394 | preserve = 1; |
7e1b7485 RS |
395 | break; |
396 | case OPT_NOEMAILDN: | |
0f113f3e | 397 | email_dn = 0; |
7e1b7485 RS |
398 | break; |
399 | case OPT_GENCRL: | |
0f113f3e | 400 | gencrl = 1; |
7e1b7485 RS |
401 | break; |
402 | case OPT_MSIE_HACK: | |
0f113f3e | 403 | msie_hack = 1; |
0f113f3e | 404 | break; |
7e1b7485 RS |
405 | case OPT_CRLDAYS: |
406 | crldays = atol(opt_arg()); | |
407 | break; | |
408 | case OPT_CRLHOURS: | |
409 | crlhours = atol(opt_arg()); | |
410 | break; | |
411 | case OPT_CRLSEC: | |
412 | crlsec = atol(opt_arg()); | |
413 | break; | |
414 | case OPT_INFILES: | |
0f113f3e | 415 | req = 1; |
7e1b7485 RS |
416 | goto end_of_options; |
417 | case OPT_SS_CERT: | |
418 | ss_cert_file = opt_arg(); | |
0f113f3e | 419 | req = 1; |
7e1b7485 RS |
420 | break; |
421 | case OPT_SPKAC: | |
422 | spkac_file = opt_arg(); | |
423 | req = 1; | |
424 | break; | |
425 | case OPT_REVOKE: | |
426 | infile = opt_arg(); | |
0f113f3e | 427 | dorevoke = 1; |
7e1b7485 RS |
428 | break; |
429 | case OPT_VALID: | |
430 | infile = opt_arg(); | |
0f113f3e | 431 | dorevoke = 2; |
7e1b7485 RS |
432 | break; |
433 | case OPT_EXTENSIONS: | |
434 | extensions = opt_arg(); | |
435 | break; | |
436 | case OPT_EXTFILE: | |
437 | extfile = opt_arg(); | |
438 | break; | |
439 | case OPT_STATUS: | |
440 | ser_status = opt_arg(); | |
441 | break; | |
442 | case OPT_UPDATEDB: | |
0f113f3e | 443 | doupdatedb = 1; |
7e1b7485 RS |
444 | break; |
445 | case OPT_CRLEXTS: | |
446 | crl_ext = opt_arg(); | |
447 | break; | |
b5895815 | 448 | case OPT_CRL_REASON: /* := REV_CRL_REASON */ |
7e1b7485 | 449 | case OPT_CRL_HOLD: |
7e1b7485 | 450 | case OPT_CRL_COMPROMISE: |
7e1b7485 RS |
451 | case OPT_CRL_CA_COMPROMISE: |
452 | rev_arg = opt_arg(); | |
06a79af2 | 453 | rev_type = (o - OPT_CRL_REASON) + REV_CRL_REASON; |
7e1b7485 RS |
454 | break; |
455 | case OPT_ENGINE: | |
333b070e | 456 | e = setup_engine(opt_arg(), 0); |
0f113f3e | 457 | break; |
bc42bd62 PY |
458 | case OPT_SM2ID: |
459 | /* we assume the input is not a hex string */ | |
460 | if (sm2_id != NULL) { | |
461 | BIO_printf(bio_err, | |
462 | "Use one of the options 'sm2-hex-id' or 'sm2-id'\n"); | |
463 | goto end; | |
464 | } | |
465 | sm2_id = (unsigned char *)opt_arg(); | |
466 | sm2_idlen = strlen((const char *)sm2_id); | |
467 | break; | |
468 | case OPT_SM2HEXID: | |
469 | /* try to parse the input as hex string first */ | |
470 | if (sm2_id != NULL) { | |
471 | BIO_printf(bio_err, | |
472 | "Use one of the options 'sm2-hex-id' or 'sm2-id'\n"); | |
473 | goto end; | |
474 | } | |
475 | sm2_free = 1; | |
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"); | |
479 | goto end; | |
480 | } | |
481 | break; | |
0f113f3e | 482 | } |
0f113f3e | 483 | } |
7e1b7485 RS |
484 | end_of_options: |
485 | argc = opt_num_rest(); | |
486 | argv = opt_rest(); | |
0f113f3e | 487 | |
0f113f3e | 488 | BIO_printf(bio_err, "Using configuration from %s\n", configfile); |
2e66d3d6 HP |
489 | |
490 | if ((conf = app_load_config(configfile)) == NULL) | |
491 | goto end; | |
c821defc | 492 | if (configfile != default_config_file && !app_load_modules(conf)) |
2e66d3d6 | 493 | goto end; |
b5a93e22 | 494 | |
0f113f3e | 495 | /* Lets get the config section we are using */ |
edbff8da F |
496 | if (section == NULL |
497 | && (section = lookup_conf(conf, BASE_SECTION, ENV_DEFAULT_CA)) == NULL) | |
498 | goto end; | |
0f113f3e | 499 | |
df364297 F |
500 | p = NCONF_get_string(conf, NULL, "oid_file"); |
501 | if (p == NULL) | |
502 | ERR_clear_error(); | |
503 | if (p != NULL) { | |
504 | BIO *oid_bio = BIO_new_file(p, "r"); | |
0f113f3e | 505 | |
df364297 F |
506 | if (oid_bio == NULL) { |
507 | ERR_clear_error(); | |
508 | } else { | |
509 | OBJ_create_objects(oid_bio); | |
510 | BIO_free(oid_bio); | |
0f113f3e MC |
511 | } |
512 | } | |
df364297 F |
513 | if (!add_oid_section(conf)) { |
514 | ERR_print_errors(bio_err); | |
515 | goto end; | |
516 | } | |
0f113f3e | 517 | |
3ee1eac2 | 518 | app_RAND_load_conf(conf, BASE_SECTION); |
0f113f3e MC |
519 | |
520 | f = NCONF_get_string(conf, section, STRING_MASK); | |
2234212c | 521 | if (f == NULL) |
0f113f3e MC |
522 | ERR_clear_error(); |
523 | ||
2234212c | 524 | if (f != NULL && !ASN1_STRING_set_default_mask_asc(f)) { |
0f113f3e | 525 | BIO_printf(bio_err, "Invalid global string mask setting %s\n", f); |
7e1b7485 | 526 | goto end; |
0f113f3e MC |
527 | } |
528 | ||
529 | if (chtype != MBSTRING_UTF8) { | |
530 | f = NCONF_get_string(conf, section, UTF8_IN); | |
2234212c | 531 | if (f == NULL) |
0f113f3e | 532 | ERR_clear_error(); |
86885c28 | 533 | else if (strcmp(f, "yes") == 0) |
0f113f3e MC |
534 | chtype = MBSTRING_UTF8; |
535 | } | |
536 | ||
537 | db_attr.unique_subject = 1; | |
538 | p = NCONF_get_string(conf, section, ENV_UNIQUE_SUBJECT); | |
2234212c | 539 | if (p != NULL) |
0f113f3e | 540 | db_attr.unique_subject = parse_yesno(p, 1); |
2234212c | 541 | else |
0f113f3e | 542 | ERR_clear_error(); |
0f113f3e | 543 | |
8bf78043 | 544 | /*****************************************************************/ |
0f113f3e MC |
545 | /* report status of cert with serial number given on command line */ |
546 | if (ser_status) { | |
edbff8da | 547 | dbfile = lookup_conf(conf, section, ENV_DATABASE); |
d6073e27 | 548 | if (dbfile == NULL) |
7e1b7485 | 549 | goto end; |
edbff8da | 550 | |
0f113f3e MC |
551 | db = load_index(dbfile, &db_attr); |
552 | if (db == NULL) | |
7e1b7485 | 553 | goto end; |
0f113f3e | 554 | |
a4107d73 | 555 | if (index_index(db) <= 0) |
7e1b7485 | 556 | goto end; |
0f113f3e MC |
557 | |
558 | if (get_certificate_status(ser_status, db) != 1) | |
559 | BIO_printf(bio_err, "Error verifying serial %s!\n", ser_status); | |
7e1b7485 | 560 | goto end; |
0f113f3e MC |
561 | } |
562 | ||
8bf78043 | 563 | /*****************************************************************/ |
0f113f3e MC |
564 | /* we definitely need a private key, so let's get it */ |
565 | ||
edbff8da F |
566 | if (keyfile == NULL |
567 | && (keyfile = lookup_conf(conf, section, ENV_PRIVATE_KEY)) == NULL) | |
7e1b7485 | 568 | goto end; |
edbff8da | 569 | |
2234212c | 570 | if (key == NULL) { |
0f113f3e | 571 | free_key = 1; |
7e1b7485 | 572 | if (!app_passwd(passinarg, NULL, &key, NULL)) { |
0f113f3e | 573 | BIO_printf(bio_err, "Error getting password\n"); |
7e1b7485 | 574 | goto end; |
0f113f3e MC |
575 | } |
576 | } | |
7e1b7485 | 577 | pkey = load_key(keyfile, keyformat, 0, key, e, "CA private key"); |
2234212c | 578 | if (key != NULL) |
0f113f3e | 579 | OPENSSL_cleanse(key, strlen(key)); |
2234212c | 580 | if (pkey == NULL) |
0f113f3e | 581 | /* load_key() has already printed an appropriate message */ |
7e1b7485 | 582 | goto end; |
0f113f3e | 583 | |
8bf78043 | 584 | /*****************************************************************/ |
0f113f3e MC |
585 | /* we need a certificate */ |
586 | if (!selfsign || spkac_file || ss_cert_file || gencrl) { | |
edbff8da F |
587 | if (certfile == NULL |
588 | && (certfile = lookup_conf(conf, section, ENV_CERTIFICATE)) == NULL) | |
7e1b7485 | 589 | goto end; |
edbff8da | 590 | |
a773b52a | 591 | x509 = load_cert(certfile, FORMAT_PEM, "CA certificate"); |
0f113f3e | 592 | if (x509 == NULL) |
7e1b7485 | 593 | goto end; |
0f113f3e MC |
594 | |
595 | if (!X509_check_private_key(x509, pkey)) { | |
596 | BIO_printf(bio_err, | |
597 | "CA certificate and CA private key do not match\n"); | |
7e1b7485 | 598 | goto end; |
0f113f3e MC |
599 | } |
600 | } | |
601 | if (!selfsign) | |
602 | x509p = x509; | |
603 | ||
604 | f = NCONF_get_string(conf, BASE_SECTION, ENV_PRESERVE); | |
605 | if (f == NULL) | |
606 | ERR_clear_error(); | |
607 | if ((f != NULL) && ((*f == 'y') || (*f == 'Y'))) | |
608 | preserve = 1; | |
609 | f = NCONF_get_string(conf, BASE_SECTION, ENV_MSIE_HACK); | |
610 | if (f == NULL) | |
611 | ERR_clear_error(); | |
612 | if ((f != NULL) && ((*f == 'y') || (*f == 'Y'))) | |
613 | msie_hack = 1; | |
614 | ||
615 | f = NCONF_get_string(conf, section, ENV_NAMEOPT); | |
616 | ||
2234212c | 617 | if (f != NULL) { |
b5c4209b | 618 | if (!set_nameopt(f)) { |
0f113f3e | 619 | BIO_printf(bio_err, "Invalid name options: \"%s\"\n", f); |
7e1b7485 | 620 | goto end; |
0f113f3e MC |
621 | } |
622 | default_op = 0; | |
f1cece55 | 623 | } |
0f113f3e MC |
624 | |
625 | f = NCONF_get_string(conf, section, ENV_CERTOPT); | |
626 | ||
2234212c | 627 | if (f != NULL) { |
0f113f3e MC |
628 | if (!set_cert_ex(&certopt, f)) { |
629 | BIO_printf(bio_err, "Invalid certificate options: \"%s\"\n", f); | |
7e1b7485 | 630 | goto end; |
0f113f3e MC |
631 | } |
632 | default_op = 0; | |
2234212c | 633 | } else { |
0f113f3e | 634 | ERR_clear_error(); |
2234212c | 635 | } |
0f113f3e MC |
636 | |
637 | f = NCONF_get_string(conf, section, ENV_EXTCOPY); | |
638 | ||
2234212c | 639 | if (f != NULL) { |
0f113f3e MC |
640 | if (!set_ext_copy(&ext_copy, f)) { |
641 | BIO_printf(bio_err, "Invalid extension copy option: \"%s\"\n", f); | |
7e1b7485 | 642 | goto end; |
0f113f3e | 643 | } |
2234212c | 644 | } else { |
0f113f3e | 645 | ERR_clear_error(); |
2234212c | 646 | } |
0f113f3e | 647 | |
8bf78043 | 648 | /*****************************************************************/ |
0f113f3e MC |
649 | /* lookup where to write new certificates */ |
650 | if ((outdir == NULL) && (req)) { | |
651 | ||
edbff8da F |
652 | outdir = NCONF_get_string(conf, section, ENV_NEW_CERTS_DIR); |
653 | if (outdir == NULL) { | |
0f113f3e MC |
654 | BIO_printf(bio_err, |
655 | "there needs to be defined a directory for new certificate to be placed in\n"); | |
7e1b7485 | 656 | goto end; |
0f113f3e | 657 | } |
bc36ee62 | 658 | #ifndef OPENSSL_SYS_VMS |
0f113f3e MC |
659 | /* |
660 | * outdir is a directory spec, but access() for VMS demands a | |
b6d3cb54 | 661 | * filename. We could use the DEC C routine to convert the |
46d08509 | 662 | * directory syntax to Unix, and give that to app_isdir, |
b6d3cb54 RS |
663 | * but for now the fopen will catch the error if it's not a |
664 | * directory | |
0f113f3e | 665 | */ |
0f113f3e | 666 | if (app_isdir(outdir) <= 0) { |
b6d3cb54 | 667 | BIO_printf(bio_err, "%s: %s is not a directory\n", prog, outdir); |
0f113f3e | 668 | perror(outdir); |
7e1b7485 | 669 | goto end; |
0f113f3e | 670 | } |
79875776 | 671 | #endif |
0f113f3e MC |
672 | } |
673 | ||
8bf78043 | 674 | /*****************************************************************/ |
0f113f3e | 675 | /* we need to load the database file */ |
edbff8da F |
676 | dbfile = lookup_conf(conf, section, ENV_DATABASE); |
677 | if (dbfile == NULL) | |
7e1b7485 | 678 | goto end; |
edbff8da | 679 | |
0f113f3e MC |
680 | db = load_index(dbfile, &db_attr); |
681 | if (db == NULL) | |
7e1b7485 | 682 | goto end; |
0f113f3e MC |
683 | |
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')) { | |
688 | BIO_printf(bio_err, | |
689 | "entry %d: not revoked yet, but has a revocation date\n", | |
690 | i + 1); | |
7e1b7485 | 691 | goto end; |
0f113f3e MC |
692 | } |
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); | |
7e1b7485 | 696 | goto end; |
0f113f3e MC |
697 | } |
698 | if (!check_time_format((char *)pp[DB_exp_date])) { | |
699 | BIO_printf(bio_err, "entry %d: invalid expiry date\n", i + 1); | |
7e1b7485 | 700 | goto end; |
0f113f3e MC |
701 | } |
702 | p = pp[DB_serial]; | |
703 | j = strlen(p); | |
704 | if (*p == '-') { | |
705 | p++; | |
706 | j--; | |
707 | } | |
708 | if ((j & 1) || (j < 2)) { | |
709 | BIO_printf(bio_err, "entry %d: bad serial number length (%d)\n", | |
710 | i + 1, j); | |
7e1b7485 | 711 | goto end; |
0f113f3e | 712 | } |
2fa45e6e | 713 | for ( ; *p; p++) { |
18295f0c | 714 | if (!isxdigit(_UC(*p))) { |
0f113f3e | 715 | BIO_printf(bio_err, |
2fa45e6e RS |
716 | "entry %d: bad char 0%o '%c' in serial number\n", |
717 | i + 1, *p, *p); | |
7e1b7485 | 718 | goto end; |
0f113f3e | 719 | } |
0f113f3e MC |
720 | } |
721 | } | |
722 | if (verbose) { | |
7e1b7485 | 723 | TXT_DB_write(bio_out, db->db); |
0f113f3e MC |
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"); | |
727 | } | |
728 | ||
a4107d73 | 729 | if (index_index(db) <= 0) |
7e1b7485 | 730 | goto end; |
0f113f3e | 731 | |
8bf78043 | 732 | /*****************************************************************/ |
0f113f3e MC |
733 | /* Update the db file for expired certificates */ |
734 | if (doupdatedb) { | |
735 | if (verbose) | |
736 | BIO_printf(bio_err, "Updating %s ...\n", dbfile); | |
737 | ||
738 | i = do_updatedb(db); | |
739 | if (i == -1) { | |
740 | BIO_printf(bio_err, "Malloc failure\n"); | |
7e1b7485 | 741 | goto end; |
0f113f3e MC |
742 | } else if (i == 0) { |
743 | if (verbose) | |
744 | BIO_printf(bio_err, "No entries found to mark expired\n"); | |
745 | } else { | |
746 | if (!save_index(dbfile, "new", db)) | |
7e1b7485 | 747 | goto end; |
0f113f3e MC |
748 | |
749 | if (!rotate_index(dbfile, "new", "old")) | |
7e1b7485 | 750 | goto end; |
0f113f3e MC |
751 | |
752 | if (verbose) | |
b5895815 | 753 | BIO_printf(bio_err, "Done. %d entries marked as expired\n", i); |
0f113f3e MC |
754 | } |
755 | } | |
756 | ||
cc01d217 | 757 | /*****************************************************************/ |
0f113f3e MC |
758 | /* Read extensions config file */ |
759 | if (extfile) { | |
cc01d217 | 760 | if ((extconf = app_load_config(extfile)) == NULL) { |
0f113f3e | 761 | ret = 1; |
7e1b7485 | 762 | goto end; |
0f113f3e MC |
763 | } |
764 | ||
765 | if (verbose) | |
766 | BIO_printf(bio_err, "Successfully loaded extensions file %s\n", | |
767 | extfile); | |
768 | ||
769 | /* We can have sections in the ext file */ | |
edbff8da F |
770 | if (extensions == NULL) { |
771 | extensions = NCONF_get_string(extconf, "default", "extensions"); | |
772 | if (extensions == NULL) | |
773 | extensions = "default"; | |
774 | } | |
0f113f3e MC |
775 | } |
776 | ||
d303b9d8 | 777 | /*****************************************************************/ |
0f113f3e | 778 | if (req || gencrl) { |
69f6b3ce | 779 | if (spkac_file != NULL && outfile != NULL) { |
25642ad2 RL |
780 | output_der = 1; |
781 | batch = 1; | |
782 | } | |
0f113f3e MC |
783 | } |
784 | ||
aabbc24e MC |
785 | def_ret = EVP_PKEY_get_default_digest_nid(pkey, &def_nid); |
786 | /* | |
787 | * EVP_PKEY_get_default_digest_nid() returns 2 if the digest is | |
788 | * mandatory for this algorithm. | |
789 | */ | |
790 | if (def_ret == 2 && def_nid == NID_undef) { | |
791 | /* The signing algorithm requires there to be no digest */ | |
f3021aca | 792 | dgst = EVP_md_null(); |
aabbc24e MC |
793 | } else if (md == NULL |
794 | && (md = lookup_conf(conf, section, ENV_DEFAULT_MD)) == NULL) { | |
795 | goto end; | |
f3021aca MC |
796 | } else { |
797 | if (strcmp(md, "default") == 0) { | |
aabbc24e | 798 | if (def_ret <= 0) { |
f3021aca MC |
799 | BIO_puts(bio_err, "no default digest\n"); |
800 | goto end; | |
801 | } | |
802 | md = (char *)OBJ_nid2sn(def_nid); | |
0f113f3e | 803 | } |
0f113f3e | 804 | |
aabbc24e | 805 | if (!opt_md(md, &dgst)) |
f3021aca | 806 | goto end; |
0f113f3e MC |
807 | } |
808 | ||
809 | if (req) { | |
cdd202f2 F |
810 | if (email_dn == 1) { |
811 | char *tmp_email_dn = NULL; | |
812 | ||
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) | |
0f113f3e MC |
815 | email_dn = 0; |
816 | } | |
817 | if (verbose) | |
818 | BIO_printf(bio_err, "message digest is %s\n", | |
cc9d6655 | 819 | OBJ_nid2ln(EVP_MD_type(dgst))); |
edbff8da F |
820 | if (policy == NULL |
821 | && (policy = lookup_conf(conf, section, ENV_POLICY)) == NULL) | |
7e1b7485 | 822 | goto end; |
edbff8da | 823 | |
0f113f3e MC |
824 | if (verbose) |
825 | BIO_printf(bio_err, "policy is %s\n", policy); | |
826 | ||
ffb46830 RS |
827 | if (NCONF_get_string(conf, section, ENV_RAND_SERIAL) != NULL) { |
828 | rand_ser = 1; | |
829 | } else { | |
830 | serialfile = lookup_conf(conf, section, ENV_SERIAL); | |
831 | if (serialfile == NULL) | |
832 | goto end; | |
833 | } | |
0f113f3e | 834 | |
2234212c | 835 | if (extconf == NULL) { |
0f113f3e MC |
836 | /* |
837 | * no '-extfile' option, so we look for extensions in the main | |
838 | * configuration file | |
839 | */ | |
2234212c | 840 | if (extensions == NULL) { |
0f113f3e | 841 | extensions = NCONF_get_string(conf, section, ENV_EXTENSIONS); |
2234212c | 842 | if (extensions == NULL) |
0f113f3e MC |
843 | ERR_clear_error(); |
844 | } | |
2234212c | 845 | if (extensions != NULL) { |
0f113f3e MC |
846 | /* Check syntax of file */ |
847 | X509V3_CTX ctx; | |
848 | X509V3_set_ctx_test(&ctx); | |
849 | X509V3_set_nconf(&ctx, conf); | |
850 | if (!X509V3_EXT_add_nconf(conf, &ctx, extensions, NULL)) { | |
851 | BIO_printf(bio_err, | |
852 | "Error Loading extension section %s\n", | |
853 | extensions); | |
854 | ret = 1; | |
7e1b7485 | 855 | goto end; |
0f113f3e MC |
856 | } |
857 | } | |
858 | } | |
859 | ||
860 | if (startdate == NULL) { | |
b5895815 | 861 | startdate = NCONF_get_string(conf, section, ENV_DEFAULT_STARTDATE); |
0f113f3e MC |
862 | if (startdate == NULL) |
863 | ERR_clear_error(); | |
864 | } | |
2234212c | 865 | if (startdate != NULL && !ASN1_TIME_set_string_X509(NULL, startdate)) { |
0f113f3e MC |
866 | BIO_printf(bio_err, |
867 | "start date is invalid, it should be YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ\n"); | |
7e1b7485 | 868 | goto end; |
0f113f3e MC |
869 | } |
870 | if (startdate == NULL) | |
871 | startdate = "today"; | |
872 | ||
873 | if (enddate == NULL) { | |
874 | enddate = NCONF_get_string(conf, section, ENV_DEFAULT_ENDDATE); | |
875 | if (enddate == NULL) | |
876 | ERR_clear_error(); | |
877 | } | |
2234212c | 878 | if (enddate != NULL && !ASN1_TIME_set_string_X509(NULL, enddate)) { |
0f113f3e MC |
879 | BIO_printf(bio_err, |
880 | "end date is invalid, it should be YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ\n"); | |
7e1b7485 | 881 | goto end; |
0f113f3e MC |
882 | } |
883 | ||
884 | if (days == 0) { | |
885 | if (!NCONF_get_number(conf, section, ENV_DEFAULT_DAYS, &days)) | |
886 | days = 0; | |
887 | } | |
b5895815 F |
888 | if (enddate == NULL && days == 0) { |
889 | BIO_printf(bio_err, "cannot lookup how many days to certify for\n"); | |
7e1b7485 | 890 | goto end; |
0f113f3e MC |
891 | } |
892 | ||
ffb46830 RS |
893 | if (rand_ser) { |
894 | if ((serial = BN_new()) == NULL || !rand_serial(serial, NULL)) { | |
895 | BIO_printf(bio_err, "error generating serial number\n"); | |
896 | goto end; | |
897 | } | |
898 | } else { | |
899 | if ((serial = load_serial(serialfile, create_ser, NULL)) == NULL) { | |
900 | BIO_printf(bio_err, "error while loading serial number\n"); | |
901 | goto end; | |
902 | } | |
903 | if (verbose) { | |
904 | if (BN_is_zero(serial)) { | |
905 | BIO_printf(bio_err, "next serial number is 00\n"); | |
906 | } else { | |
907 | if ((f = BN_bn2hex(serial)) == NULL) | |
908 | goto end; | |
909 | BIO_printf(bio_err, "next serial number is %s\n", f); | |
910 | OPENSSL_free(f); | |
911 | } | |
0f113f3e MC |
912 | } |
913 | } | |
914 | ||
915 | if ((attribs = NCONF_get_section(conf, policy)) == NULL) { | |
916 | BIO_printf(bio_err, "unable to find 'section' for %s\n", policy); | |
7e1b7485 | 917 | goto end; |
0f113f3e MC |
918 | } |
919 | ||
920 | if ((cert_sk = sk_X509_new_null()) == NULL) { | |
921 | BIO_printf(bio_err, "Memory allocation failure\n"); | |
7e1b7485 | 922 | goto end; |
0f113f3e MC |
923 | } |
924 | if (spkac_file != NULL) { | |
925 | total++; | |
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, | |
b5c4209b | 929 | conf, verbose, certopt, get_nameopt(), default_op, |
0f113f3e MC |
930 | ext_copy); |
931 | if (j < 0) | |
7e1b7485 | 932 | goto end; |
0f113f3e MC |
933 | if (j > 0) { |
934 | total_done++; | |
935 | BIO_printf(bio_err, "\n"); | |
936 | if (!BN_add_word(serial, 1)) | |
7e1b7485 | 937 | goto end; |
0f113f3e MC |
938 | if (!sk_X509_push(cert_sk, x)) { |
939 | BIO_printf(bio_err, "Memory allocation failure\n"); | |
7e1b7485 | 940 | goto end; |
0f113f3e | 941 | } |
0f113f3e MC |
942 | } |
943 | } | |
944 | if (ss_cert_file != NULL) { | |
945 | total++; | |
946 | j = certify_cert(&x, ss_cert_file, pkey, x509, dgst, sigopts, | |
947 | attribs, | |
948 | db, serial, subj, chtype, multirdn, email_dn, | |
949 | startdate, enddate, days, batch, extensions, | |
b5c4209b | 950 | conf, verbose, certopt, get_nameopt(), default_op, |
a773b52a | 951 | ext_copy); |
0f113f3e | 952 | if (j < 0) |
7e1b7485 | 953 | goto end; |
0f113f3e MC |
954 | if (j > 0) { |
955 | total_done++; | |
956 | BIO_printf(bio_err, "\n"); | |
957 | if (!BN_add_word(serial, 1)) | |
7e1b7485 | 958 | goto end; |
0f113f3e MC |
959 | if (!sk_X509_push(cert_sk, x)) { |
960 | BIO_printf(bio_err, "Memory allocation failure\n"); | |
7e1b7485 | 961 | goto end; |
0f113f3e MC |
962 | } |
963 | } | |
964 | } | |
965 | if (infile != NULL) { | |
966 | total++; | |
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, | |
bc42bd62 PY |
970 | certopt, get_nameopt(), default_op, ext_copy, selfsign, |
971 | sm2_id, sm2_idlen); | |
0f113f3e | 972 | if (j < 0) |
7e1b7485 | 973 | goto end; |
0f113f3e MC |
974 | if (j > 0) { |
975 | total_done++; | |
976 | BIO_printf(bio_err, "\n"); | |
977 | if (!BN_add_word(serial, 1)) | |
7e1b7485 | 978 | goto end; |
0f113f3e MC |
979 | if (!sk_X509_push(cert_sk, x)) { |
980 | BIO_printf(bio_err, "Memory allocation failure\n"); | |
7e1b7485 | 981 | goto end; |
0f113f3e MC |
982 | } |
983 | } | |
984 | } | |
985 | for (i = 0; i < argc; i++) { | |
986 | total++; | |
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, | |
bc42bd62 PY |
990 | certopt, get_nameopt(), default_op, ext_copy, selfsign, |
991 | sm2_id, sm2_idlen); | |
0f113f3e | 992 | if (j < 0) |
7e1b7485 | 993 | goto end; |
0f113f3e MC |
994 | if (j > 0) { |
995 | total_done++; | |
996 | BIO_printf(bio_err, "\n"); | |
aebd0e5c PK |
997 | if (!BN_add_word(serial, 1)) { |
998 | X509_free(x); | |
7e1b7485 | 999 | goto end; |
aebd0e5c | 1000 | } |
0f113f3e MC |
1001 | if (!sk_X509_push(cert_sk, x)) { |
1002 | BIO_printf(bio_err, "Memory allocation failure\n"); | |
aebd0e5c | 1003 | X509_free(x); |
7e1b7485 | 1004 | goto end; |
0f113f3e MC |
1005 | } |
1006 | } | |
1007 | } | |
1008 | /* | |
1009 | * we have a stack of newly certified certificates and a data base | |
1010 | * and serial number that need updating | |
1011 | */ | |
1012 | ||
1013 | if (sk_X509_num(cert_sk) > 0) { | |
1014 | if (!batch) { | |
1015 | BIO_printf(bio_err, | |
1016 | "\n%d out of %d certificate requests certified, commit? [y/n]", | |
1017 | total_done, total); | |
1018 | (void)BIO_flush(bio_err); | |
3a19b22a F |
1019 | tmp[0] = '\0'; |
1020 | if (fgets(tmp, sizeof(tmp), stdin) == NULL) { | |
b5895815 | 1021 | BIO_printf(bio_err, "CERTIFICATION CANCELED: I/O error\n"); |
0f113f3e | 1022 | ret = 0; |
7e1b7485 | 1023 | goto end; |
0f113f3e | 1024 | } |
3a19b22a | 1025 | if (tmp[0] != 'y' && tmp[0] != 'Y') { |
0f113f3e MC |
1026 | BIO_printf(bio_err, "CERTIFICATION CANCELED\n"); |
1027 | ret = 0; | |
7e1b7485 | 1028 | goto end; |
0f113f3e MC |
1029 | } |
1030 | } | |
1031 | ||
1032 | BIO_printf(bio_err, "Write out database with %d new entries\n", | |
1033 | sk_X509_num(cert_sk)); | |
1034 | ||
aeec793b | 1035 | if (serialfile != NULL |
ffb46830 | 1036 | && !save_serial(serialfile, "new", serial, NULL)) |
7e1b7485 | 1037 | goto end; |
0f113f3e MC |
1038 | |
1039 | if (!save_index(dbfile, "new", db)) | |
7e1b7485 | 1040 | goto end; |
0f113f3e MC |
1041 | } |
1042 | ||
b5895815 F |
1043 | outdirlen = OPENSSL_strlcpy(new_cert, outdir, sizeof(new_cert)); |
1044 | #ifndef OPENSSL_SYS_VMS | |
1045 | outdirlen = OPENSSL_strlcat(new_cert, "/", sizeof(new_cert)); | |
1046 | #endif | |
1047 | ||
0f113f3e MC |
1048 | if (verbose) |
1049 | BIO_printf(bio_err, "writing new certificates\n"); | |
b5895815 | 1050 | |
0f113f3e | 1051 | for (i = 0; i < sk_X509_num(cert_sk); i++) { |
2d87ee68 | 1052 | BIO *Cout = NULL; |
0db1fb3f F |
1053 | X509 *xi = sk_X509_value(cert_sk, i); |
1054 | ASN1_INTEGER *serialNumber = X509_get_serialNumber(xi); | |
b5895815 F |
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; | |
0f113f3e | 1059 | |
b5895815 | 1060 | if (outdirlen + filen_len > PATH_MAX) { |
0f113f3e | 1061 | BIO_printf(bio_err, "certificate file name too long\n"); |
7e1b7485 | 1062 | goto end; |
0f113f3e MC |
1063 | } |
1064 | ||
b5895815 F |
1065 | if (snl > 0) { |
1066 | static const char HEX_DIGITS[] = "0123456789ABCDEF"; | |
7d7d2cbc | 1067 | |
b5895815 F |
1068 | for (j = 0; j < snl; j++, psn++) { |
1069 | *n++ = HEX_DIGITS[*psn >> 4]; | |
1070 | *n++ = HEX_DIGITS[*psn & 0x0F]; | |
0f113f3e MC |
1071 | } |
1072 | } else { | |
1073 | *(n++) = '0'; | |
1074 | *(n++) = '0'; | |
1075 | } | |
1076 | *(n++) = '.'; | |
1077 | *(n++) = 'p'; | |
1078 | *(n++) = 'e'; | |
1079 | *(n++) = 'm'; | |
b5895815 | 1080 | *n = '\0'; /* closing new_cert */ |
0f113f3e | 1081 | if (verbose) |
3a19b22a | 1082 | BIO_printf(bio_err, "writing %s\n", new_cert); |
0f113f3e | 1083 | |
63871d9f RL |
1084 | Sout = bio_open_default(outfile, 'w', |
1085 | output_der ? FORMAT_ASN1 : FORMAT_TEXT); | |
1086 | if (Sout == NULL) | |
1087 | goto end; | |
1088 | ||
3a19b22a | 1089 | Cout = BIO_new_file(new_cert, "w"); |
7e1b7485 | 1090 | if (Cout == NULL) { |
3a19b22a | 1091 | perror(new_cert); |
7e1b7485 | 1092 | goto end; |
0f113f3e | 1093 | } |
0db1fb3f F |
1094 | write_new_certificate(Cout, xi, 0, notext); |
1095 | write_new_certificate(Sout, xi, output_der, notext); | |
2d87ee68 | 1096 | BIO_free_all(Cout); |
63871d9f RL |
1097 | BIO_free_all(Sout); |
1098 | Sout = NULL; | |
0f113f3e MC |
1099 | } |
1100 | ||
1101 | if (sk_X509_num(cert_sk)) { | |
1102 | /* Rename the database and the serial file */ | |
aeec793b DMSP |
1103 | if (serialfile != NULL |
1104 | && !rotate_serial(serialfile, "new", "old")) | |
7e1b7485 | 1105 | goto end; |
0f113f3e MC |
1106 | |
1107 | if (!rotate_index(dbfile, "new", "old")) | |
7e1b7485 | 1108 | goto end; |
0f113f3e MC |
1109 | |
1110 | BIO_printf(bio_err, "Data Base Updated\n"); | |
1111 | } | |
1112 | } | |
1113 | ||
8bf78043 | 1114 | /*****************************************************************/ |
0f113f3e MC |
1115 | if (gencrl) { |
1116 | int crl_v2 = 0; | |
2234212c | 1117 | if (crl_ext == NULL) { |
0f113f3e | 1118 | crl_ext = NCONF_get_string(conf, section, ENV_CRLEXT); |
2234212c | 1119 | if (crl_ext == NULL) |
0f113f3e MC |
1120 | ERR_clear_error(); |
1121 | } | |
2234212c | 1122 | if (crl_ext != NULL) { |
0f113f3e MC |
1123 | /* Check syntax of file */ |
1124 | X509V3_CTX ctx; | |
1125 | X509V3_set_ctx_test(&ctx); | |
1126 | X509V3_set_nconf(&ctx, conf); | |
1127 | if (!X509V3_EXT_add_nconf(conf, &ctx, crl_ext, NULL)) { | |
1128 | BIO_printf(bio_err, | |
b5895815 | 1129 | "Error Loading CRL extension section %s\n", crl_ext); |
0f113f3e | 1130 | ret = 1; |
7e1b7485 | 1131 | goto end; |
0f113f3e MC |
1132 | } |
1133 | } | |
1134 | ||
1135 | if ((crlnumberfile = NCONF_get_string(conf, section, ENV_CRLNUMBER)) | |
1136 | != NULL) | |
1137 | if ((crlnumber = load_serial(crlnumberfile, 0, NULL)) == NULL) { | |
1138 | BIO_printf(bio_err, "error while loading CRL number\n"); | |
7e1b7485 | 1139 | goto end; |
0f113f3e MC |
1140 | } |
1141 | ||
1142 | if (!crldays && !crlhours && !crlsec) { | |
1143 | if (!NCONF_get_number(conf, section, | |
1144 | ENV_DEFAULT_CRL_DAYS, &crldays)) | |
1145 | crldays = 0; | |
1146 | if (!NCONF_get_number(conf, section, | |
1147 | ENV_DEFAULT_CRL_HOURS, &crlhours)) | |
1148 | crlhours = 0; | |
1149 | ERR_clear_error(); | |
1150 | } | |
1151 | if ((crldays == 0) && (crlhours == 0) && (crlsec == 0)) { | |
1152 | BIO_printf(bio_err, | |
1153 | "cannot lookup how long until the next CRL is issued\n"); | |
7e1b7485 | 1154 | goto end; |
0f113f3e MC |
1155 | } |
1156 | ||
1157 | if (verbose) | |
1158 | BIO_printf(bio_err, "making CRL\n"); | |
1159 | if ((crl = X509_CRL_new()) == NULL) | |
7e1b7485 | 1160 | goto end; |
0f113f3e | 1161 | if (!X509_CRL_set_issuer_name(crl, X509_get_subject_name(x509))) |
7e1b7485 | 1162 | goto end; |
0f113f3e MC |
1163 | |
1164 | tmptm = ASN1_TIME_new(); | |
bc2a0dd2 MC |
1165 | if (tmptm == NULL |
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, | |
1169 | NULL) == NULL) { | |
0f113f3e | 1170 | BIO_puts(bio_err, "error setting CRL nextUpdate\n"); |
bc2a0dd2 | 1171 | ASN1_TIME_free(tmptm); |
7e1b7485 | 1172 | goto end; |
0f113f3e | 1173 | } |
568ce3a5 | 1174 | X509_CRL_set1_nextUpdate(crl, tmptm); |
0f113f3e MC |
1175 | |
1176 | ASN1_TIME_free(tmptm); | |
1177 | ||
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) | |
7e1b7485 | 1182 | goto end; |
0f113f3e MC |
1183 | j = make_revoked(r, pp[DB_rev_date]); |
1184 | if (!j) | |
7e1b7485 | 1185 | goto end; |
0f113f3e MC |
1186 | if (j == 2) |
1187 | crl_v2 = 1; | |
1188 | if (!BN_hex2bn(&serial, pp[DB_serial])) | |
7e1b7485 | 1189 | goto end; |
0f113f3e MC |
1190 | tmpser = BN_to_ASN1_INTEGER(serial, NULL); |
1191 | BN_free(serial); | |
1192 | serial = NULL; | |
1193 | if (!tmpser) | |
7e1b7485 | 1194 | goto end; |
0f113f3e MC |
1195 | X509_REVOKED_set_serialNumber(r, tmpser); |
1196 | ASN1_INTEGER_free(tmpser); | |
1197 | X509_CRL_add0_revoked(crl, r); | |
1198 | } | |
1199 | } | |
1200 | ||
1201 | /* | |
1202 | * sort the data so it will be written in serial number order | |
1203 | */ | |
1204 | X509_CRL_sort(crl); | |
1205 | ||
1206 | /* we now have a CRL */ | |
1207 | if (verbose) | |
1208 | BIO_printf(bio_err, "signing CRL\n"); | |
1209 | ||
1210 | /* Add any extensions asked for */ | |
1211 | ||
2234212c | 1212 | if (crl_ext != NULL || crlnumberfile != NULL) { |
0f113f3e MC |
1213 | X509V3_CTX crlctx; |
1214 | X509V3_set_ctx(&crlctx, x509, NULL, NULL, crl, 0); | |
1215 | X509V3_set_nconf(&crlctx, conf); | |
1216 | ||
2234212c | 1217 | if (crl_ext != NULL) |
0f113f3e | 1218 | if (!X509V3_EXT_CRL_add_nconf(conf, &crlctx, crl_ext, crl)) |
7e1b7485 | 1219 | goto end; |
0f113f3e MC |
1220 | if (crlnumberfile != NULL) { |
1221 | tmpser = BN_to_ASN1_INTEGER(crlnumber, NULL); | |
1222 | if (!tmpser) | |
7e1b7485 | 1223 | goto end; |
0f113f3e MC |
1224 | X509_CRL_add1_ext_i2d(crl, NID_crl_number, tmpser, 0, 0); |
1225 | ASN1_INTEGER_free(tmpser); | |
1226 | crl_v2 = 1; | |
1227 | if (!BN_add_word(crlnumber, 1)) | |
7e1b7485 | 1228 | goto end; |
0f113f3e MC |
1229 | } |
1230 | } | |
2234212c | 1231 | if (crl_ext != NULL || crl_v2) { |
0f113f3e | 1232 | if (!X509_CRL_set_version(crl, 1)) |
7e1b7485 | 1233 | goto end; /* version 2 CRL */ |
0f113f3e MC |
1234 | } |
1235 | ||
1236 | /* we have a CRL number that need updating */ | |
aeec793b DMSP |
1237 | if (crlnumberfile != NULL |
1238 | && !save_serial(crlnumberfile, "new", crlnumber, NULL)) | |
1239 | goto end; | |
0f113f3e | 1240 | |
23a1d5e9 RS |
1241 | BN_free(crlnumber); |
1242 | crlnumber = NULL; | |
0f113f3e | 1243 | |
7e1b7485 RS |
1244 | if (!do_X509_CRL_sign(crl, pkey, dgst, sigopts)) |
1245 | goto end; | |
0f113f3e | 1246 | |
63871d9f RL |
1247 | Sout = bio_open_default(outfile, 'w', |
1248 | output_der ? FORMAT_ASN1 : FORMAT_TEXT); | |
1249 | if (Sout == NULL) | |
1250 | goto end; | |
1251 | ||
0f113f3e MC |
1252 | PEM_write_bio_X509_CRL(Sout, crl); |
1253 | ||
aeec793b DMSP |
1254 | /* Rename the crlnumber file */ |
1255 | if (crlnumberfile != NULL | |
1256 | && !rotate_serial(crlnumberfile, "new", "old")) | |
1257 | goto end; | |
0f113f3e MC |
1258 | |
1259 | } | |
8bf78043 | 1260 | /*****************************************************************/ |
0f113f3e MC |
1261 | if (dorevoke) { |
1262 | if (infile == NULL) { | |
1263 | BIO_printf(bio_err, "no input files\n"); | |
7e1b7485 | 1264 | goto end; |
0f113f3e MC |
1265 | } else { |
1266 | X509 *revcert; | |
a773b52a | 1267 | revcert = load_cert(infile, FORMAT_PEM, infile); |
0f113f3e | 1268 | if (revcert == NULL) |
7e1b7485 | 1269 | goto end; |
0f113f3e | 1270 | if (dorevoke == 2) |
06a79af2 | 1271 | rev_type = REV_VALID; |
0f113f3e MC |
1272 | j = do_revoke(revcert, db, rev_type, rev_arg); |
1273 | if (j <= 0) | |
7e1b7485 | 1274 | goto end; |
0f113f3e MC |
1275 | X509_free(revcert); |
1276 | ||
1277 | if (!save_index(dbfile, "new", db)) | |
7e1b7485 | 1278 | goto end; |
0f113f3e MC |
1279 | |
1280 | if (!rotate_index(dbfile, "new", "old")) | |
7e1b7485 | 1281 | goto end; |
0f113f3e MC |
1282 | |
1283 | BIO_printf(bio_err, "Data Base Updated\n"); | |
1284 | } | |
1285 | } | |
0f113f3e | 1286 | ret = 0; |
ffb46830 | 1287 | |
7e1b7485 | 1288 | end: |
bc42bd62 PY |
1289 | if (sm2_free) |
1290 | OPENSSL_free(sm2_id); | |
ffb46830 RS |
1291 | if (ret) |
1292 | ERR_print_errors(bio_err); | |
0f113f3e MC |
1293 | BIO_free_all(Sout); |
1294 | BIO_free_all(out); | |
1295 | BIO_free_all(in); | |
222561fe | 1296 | sk_X509_pop_free(cert_sk, X509_free); |
0f113f3e | 1297 | |
25aaa98a | 1298 | if (free_key) |
0f113f3e MC |
1299 | OPENSSL_free(key); |
1300 | BN_free(serial); | |
1301 | BN_free(crlnumber); | |
1302 | free_index(db); | |
25aaa98a | 1303 | sk_OPENSSL_STRING_free(sigopts); |
0f113f3e | 1304 | EVP_PKEY_free(pkey); |
222561fe | 1305 | X509_free(x509); |
0f113f3e MC |
1306 | X509_CRL_free(crl); |
1307 | NCONF_free(conf); | |
1308 | NCONF_free(extconf); | |
dd1abd44 | 1309 | release_engine(e); |
26a7d938 | 1310 | return ret; |
0f113f3e | 1311 | } |
d02b48c6 | 1312 | |
edbff8da | 1313 | static char *lookup_conf(const CONF *conf, const char *section, const char *tag) |
0f113f3e | 1314 | { |
edbff8da F |
1315 | char *entry = NCONF_get_string(conf, section, tag); |
1316 | if (entry == NULL) | |
1317 | BIO_printf(bio_err, "variable lookup failed for %s::%s\n", section, tag); | |
1318 | return entry; | |
0f113f3e | 1319 | } |
d02b48c6 | 1320 | |
cc696296 | 1321 | static int certify(X509 **xret, const char *infile, EVP_PKEY *pkey, X509 *x509, |
0f113f3e MC |
1322 | const EVP_MD *dgst, STACK_OF(OPENSSL_STRING) *sigopts, |
1323 | STACK_OF(CONF_VALUE) *policy, CA_DB *db, | |
cc696296 F |
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, | |
0f113f3e | 1328 | int verbose, unsigned long certopt, unsigned long nameopt, |
bc42bd62 PY |
1329 | int default_op, int ext_copy, int selfsign, |
1330 | unsigned char *sm2id, size_t sm2idlen) | |
0f113f3e MC |
1331 | { |
1332 | X509_REQ *req = NULL; | |
1333 | BIO *in = NULL; | |
1334 | EVP_PKEY *pktmp = NULL; | |
1335 | int ok = -1, i; | |
1336 | ||
7e1b7485 RS |
1337 | in = BIO_new_file(infile, "r"); |
1338 | if (in == NULL) { | |
1339 | ERR_print_errors(bio_err); | |
1340 | goto end; | |
0f113f3e MC |
1341 | } |
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", | |
1344 | infile); | |
7e1b7485 | 1345 | goto end; |
0f113f3e MC |
1346 | } |
1347 | if (verbose) | |
b5c4209b | 1348 | X509_REQ_print_ex(bio_err, req, nameopt, X509_FLAG_COMPAT); |
0f113f3e MC |
1349 | |
1350 | BIO_printf(bio_err, "Check that the request matches the signature\n"); | |
1351 | ||
1352 | if (selfsign && !X509_REQ_check_private_key(req, pkey)) { | |
1353 | BIO_printf(bio_err, | |
1354 | "Certificate request and CA private key do not match\n"); | |
1355 | ok = 0; | |
7e1b7485 | 1356 | goto end; |
0f113f3e | 1357 | } |
c5137473 | 1358 | if ((pktmp = X509_REQ_get0_pubkey(req)) == NULL) { |
0f113f3e | 1359 | BIO_printf(bio_err, "error unpacking public key\n"); |
7e1b7485 | 1360 | goto end; |
0f113f3e | 1361 | } |
bc42bd62 PY |
1362 | if (sm2id != NULL) { |
1363 | #ifndef OPENSSL_NO_SM2 | |
1364 | ASN1_OCTET_STRING *v; | |
1365 | ||
1366 | v = ASN1_OCTET_STRING_new(); | |
1367 | if (v == NULL) { | |
1368 | BIO_printf(bio_err, "error: SM2 ID allocation failed\n"); | |
1369 | goto end; | |
1370 | } | |
1371 | ||
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); | |
1375 | goto end; | |
1376 | } | |
1377 | ||
1378 | X509_REQ_set0_sm2_id(req, v); | |
1379 | #endif | |
1380 | } | |
0f113f3e | 1381 | i = X509_REQ_verify(req, pktmp); |
c5137473 | 1382 | pktmp = NULL; |
0f113f3e MC |
1383 | if (i < 0) { |
1384 | ok = 0; | |
1385 | BIO_printf(bio_err, "Signature verification problems....\n"); | |
1386 | ERR_print_errors(bio_err); | |
7e1b7485 | 1387 | goto end; |
0f113f3e MC |
1388 | } |
1389 | if (i == 0) { | |
1390 | ok = 0; | |
1391 | BIO_printf(bio_err, | |
1392 | "Signature did not match the certificate request\n"); | |
1393 | ERR_print_errors(bio_err); | |
7e1b7485 | 1394 | goto end; |
2234212c | 1395 | } else { |
0f113f3e | 1396 | BIO_printf(bio_err, "Signature ok\n"); |
2234212c | 1397 | } |
0f113f3e MC |
1398 | |
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); | |
1403 | ||
7e1b7485 | 1404 | end: |
222561fe | 1405 | X509_REQ_free(req); |
ca3a82c3 | 1406 | BIO_free(in); |
26a7d938 | 1407 | return ok; |
0f113f3e | 1408 | } |
d02b48c6 | 1409 | |
cc696296 | 1410 | static int certify_cert(X509 **xret, const char *infile, EVP_PKEY *pkey, X509 *x509, |
0f113f3e MC |
1411 | const EVP_MD *dgst, STACK_OF(OPENSSL_STRING) *sigopts, |
1412 | STACK_OF(CONF_VALUE) *policy, CA_DB *db, | |
cc696296 F |
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, | |
0f113f3e | 1416 | CONF *lconf, int verbose, unsigned long certopt, |
a773b52a | 1417 | unsigned long nameopt, int default_op, int ext_copy) |
0f113f3e MC |
1418 | { |
1419 | X509 *req = NULL; | |
1420 | X509_REQ *rreq = NULL; | |
1421 | EVP_PKEY *pktmp = NULL; | |
1422 | int ok = -1, i; | |
1423 | ||
a773b52a | 1424 | if ((req = load_cert(infile, FORMAT_PEM, infile)) == NULL) |
7e1b7485 | 1425 | goto end; |
0f113f3e MC |
1426 | if (verbose) |
1427 | X509_print(bio_err, req); | |
1428 | ||
1429 | BIO_printf(bio_err, "Check that the request matches the signature\n"); | |
1430 | ||
c01ff880 | 1431 | if ((pktmp = X509_get0_pubkey(req)) == NULL) { |
0f113f3e | 1432 | BIO_printf(bio_err, "error unpacking public key\n"); |
7e1b7485 | 1433 | goto end; |
0f113f3e MC |
1434 | } |
1435 | i = X509_verify(req, pktmp); | |
0f113f3e MC |
1436 | if (i < 0) { |
1437 | ok = 0; | |
1438 | BIO_printf(bio_err, "Signature verification problems....\n"); | |
7e1b7485 | 1439 | goto end; |
0f113f3e MC |
1440 | } |
1441 | if (i == 0) { | |
1442 | ok = 0; | |
1443 | BIO_printf(bio_err, "Signature did not match the certificate\n"); | |
7e1b7485 | 1444 | goto end; |
2234212c | 1445 | } else { |
0f113f3e | 1446 | BIO_printf(bio_err, "Signature ok\n"); |
2234212c | 1447 | } |
0f113f3e | 1448 | |
f8547f62 | 1449 | if ((rreq = X509_to_X509_REQ(req, NULL, NULL)) == NULL) |
7e1b7485 | 1450 | goto end; |
0f113f3e MC |
1451 | |
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, | |
1455 | ext_copy, 0); | |
1456 | ||
7e1b7485 | 1457 | end: |
222561fe RS |
1458 | X509_REQ_free(rreq); |
1459 | X509_free(req); | |
26a7d938 | 1460 | return ok; |
0f113f3e MC |
1461 | } |
1462 | ||
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, | |
cc696296 F |
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, | |
0f113f3e MC |
1469 | CONF *lconf, unsigned long certopt, unsigned long nameopt, |
1470 | int default_op, int ext_copy, int selfsign) | |
1471 | { | |
2cedf794 | 1472 | X509_NAME *name = NULL, *CAname = NULL, *subject = NULL; |
568ce3a5 | 1473 | const ASN1_TIME *tm; |
0f113f3e MC |
1474 | ASN1_STRING *str, *str2; |
1475 | ASN1_OBJECT *obj; | |
1476 | X509 *ret = NULL; | |
07a38fd2 | 1477 | X509_NAME_ENTRY *ne, *tne; |
0f113f3e MC |
1478 | EVP_PKEY *pktmp; |
1479 | int ok = -1, i, j, last, nid; | |
1480 | const char *p; | |
1481 | CONF_VALUE *cv; | |
1482 | OPENSSL_STRING row[DB_NUMBER]; | |
1483 | OPENSSL_STRING *irow = NULL; | |
1484 | OPENSSL_STRING *rrow = NULL; | |
1485 | char buf[25]; | |
1486 | ||
0f113f3e MC |
1487 | for (i = 0; i < DB_NUMBER; i++) |
1488 | row[i] = NULL; | |
1489 | ||
1490 | if (subj) { | |
1491 | X509_NAME *n = parse_name(subj, chtype, multirdn); | |
1492 | ||
1493 | if (!n) { | |
1494 | ERR_print_errors(bio_err); | |
7e1b7485 | 1495 | goto end; |
0f113f3e MC |
1496 | } |
1497 | X509_REQ_set_subject_name(req, n); | |
0f113f3e MC |
1498 | X509_NAME_free(n); |
1499 | } | |
1500 | ||
1501 | if (default_op) | |
b5895815 | 1502 | BIO_printf(bio_err, "The Subject's Distinguished Name is as follows\n"); |
0f113f3e MC |
1503 | |
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); | |
07a38fd2 | 1509 | nid = OBJ_obj2nid(obj); |
0f113f3e MC |
1510 | |
1511 | if (msie_hack) { | |
1512 | /* assume all type should be strings */ | |
0f113f3e MC |
1513 | |
1514 | if (str->type == V_ASN1_UNIVERSALSTRING) | |
1515 | ASN1_UNIVERSALSTRING_to_string(str); | |
1516 | ||
07a38fd2 | 1517 | if (str->type == V_ASN1_IA5STRING && nid != NID_pkcs9_emailAddress) |
0f113f3e MC |
1518 | str->type = V_ASN1_T61STRING; |
1519 | ||
07a38fd2 F |
1520 | if (nid == NID_pkcs9_emailAddress |
1521 | && str->type == V_ASN1_PRINTABLESTRING) | |
0f113f3e MC |
1522 | str->type = V_ASN1_IA5STRING; |
1523 | } | |
1524 | ||
1525 | /* If no EMAIL is wanted in the subject */ | |
07a38fd2 | 1526 | if (nid == NID_pkcs9_emailAddress && !email_dn) |
0f113f3e MC |
1527 | continue; |
1528 | ||
1529 | /* check some things */ | |
07a38fd2 | 1530 | if (nid == NID_pkcs9_emailAddress && str->type != V_ASN1_IA5STRING) { |
0f113f3e MC |
1531 | BIO_printf(bio_err, |
1532 | "\nemailAddress type needs to be of type IA5STRING\n"); | |
7e1b7485 | 1533 | goto end; |
0f113f3e | 1534 | } |
07a38fd2 | 1535 | if (str->type != V_ASN1_BMPSTRING && str->type != V_ASN1_UTF8STRING) { |
0f113f3e | 1536 | j = ASN1_PRINTABLE_type(str->data, str->length); |
07a38fd2 F |
1537 | if ((j == V_ASN1_T61STRING && str->type != V_ASN1_T61STRING) || |
1538 | (j == V_ASN1_IA5STRING && str->type == V_ASN1_PRINTABLESTRING)) | |
1539 | { | |
0f113f3e MC |
1540 | BIO_printf(bio_err, |
1541 | "\nThe string contains characters that are illegal for the ASN.1 type\n"); | |
7e1b7485 | 1542 | goto end; |
0f113f3e MC |
1543 | } |
1544 | } | |
1545 | ||
1546 | if (default_op) | |
ecf3a1fb | 1547 | old_entry_print(obj, str); |
0f113f3e MC |
1548 | } |
1549 | ||
1550 | /* Ok, now we check the 'policy' stuff. */ | |
1551 | if ((subject = X509_NAME_new()) == NULL) { | |
1552 | BIO_printf(bio_err, "Memory allocation failure\n"); | |
7e1b7485 | 1553 | goto end; |
0f113f3e MC |
1554 | } |
1555 | ||
1556 | /* take a copy of the issuer name before we mess with it. */ | |
1557 | if (selfsign) | |
1558 | CAname = X509_NAME_dup(name); | |
1559 | else | |
a8d8e06b | 1560 | CAname = X509_NAME_dup(X509_get_subject_name(x509)); |
0f113f3e | 1561 | if (CAname == NULL) |
7e1b7485 | 1562 | goto end; |
0f113f3e MC |
1563 | str = str2 = NULL; |
1564 | ||
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) { | |
1568 | BIO_printf(bio_err, | |
1569 | "%s:unknown object type in 'policy' configuration\n", | |
1570 | cv->name); | |
7e1b7485 | 1571 | goto end; |
0f113f3e MC |
1572 | } |
1573 | obj = OBJ_nid2obj(j); | |
1574 | ||
1575 | last = -1; | |
1576 | for (;;) { | |
07a38fd2 F |
1577 | X509_NAME_ENTRY *push = NULL; |
1578 | ||
0f113f3e MC |
1579 | /* lookup the object in the supplied name list */ |
1580 | j = X509_NAME_get_index_by_OBJ(name, obj, last); | |
1581 | if (j < 0) { | |
1582 | if (last != -1) | |
1583 | break; | |
1584 | tne = NULL; | |
1585 | } else { | |
1586 | tne = X509_NAME_get_entry(name, j); | |
1587 | } | |
1588 | last = j; | |
1589 | ||
1590 | /* depending on the 'policy', decide what to do. */ | |
0f113f3e MC |
1591 | if (strcmp(cv->value, "optional") == 0) { |
1592 | if (tne != NULL) | |
1593 | push = tne; | |
1594 | } else if (strcmp(cv->value, "supplied") == 0) { | |
1595 | if (tne == NULL) { | |
1596 | BIO_printf(bio_err, | |
1597 | "The %s field needed to be supplied and was missing\n", | |
1598 | cv->name); | |
7e1b7485 | 1599 | goto end; |
2234212c | 1600 | } else { |
0f113f3e | 1601 | push = tne; |
2234212c | 1602 | } |
0f113f3e MC |
1603 | } else if (strcmp(cv->value, "match") == 0) { |
1604 | int last2; | |
1605 | ||
1606 | if (tne == NULL) { | |
1607 | BIO_printf(bio_err, | |
1608 | "The mandatory %s field was missing\n", | |
1609 | cv->name); | |
7e1b7485 | 1610 | goto end; |
0f113f3e MC |
1611 | } |
1612 | ||
1613 | last2 = -1; | |
1614 | ||
1615 | again2: | |
1616 | j = X509_NAME_get_index_by_OBJ(CAname, obj, last2); | |
1617 | if ((j < 0) && (last2 == -1)) { | |
1618 | BIO_printf(bio_err, | |
fefa4d55 | 1619 | "The %s field does not exist in the CA certificate,\n" |
b5895815 | 1620 | "the 'policy' is misconfigured\n", cv->name); |
7e1b7485 | 1621 | goto end; |
0f113f3e MC |
1622 | } |
1623 | if (j >= 0) { | |
1624 | push = X509_NAME_get_entry(CAname, j); | |
1625 | str = X509_NAME_ENTRY_get_data(tne); | |
1626 | str2 = X509_NAME_ENTRY_get_data(push); | |
1627 | last2 = j; | |
1628 | if (ASN1_STRING_cmp(str, str2) != 0) | |
1629 | goto again2; | |
1630 | } | |
1631 | if (j < 0) { | |
1632 | BIO_printf(bio_err, | |
fefa4d55 F |
1633 | "The %s field is different between\n" |
1634 | "CA certificate (%s) and the request (%s)\n", | |
0f113f3e MC |
1635 | cv->name, |
1636 | ((str2 == NULL) ? "NULL" : (char *)str2->data), | |
1637 | ((str == NULL) ? "NULL" : (char *)str->data)); | |
7e1b7485 | 1638 | goto end; |
0f113f3e MC |
1639 | } |
1640 | } else { | |
1641 | BIO_printf(bio_err, | |
1642 | "%s:invalid type in 'policy' configuration\n", | |
1643 | cv->value); | |
7e1b7485 | 1644 | goto end; |
0f113f3e MC |
1645 | } |
1646 | ||
1647 | if (push != NULL) { | |
1648 | if (!X509_NAME_add_entry(subject, push, -1, 0)) { | |
0f113f3e | 1649 | BIO_printf(bio_err, "Memory allocation failure\n"); |
7e1b7485 | 1650 | goto end; |
0f113f3e MC |
1651 | } |
1652 | } | |
1653 | if (j < 0) | |
1654 | break; | |
1655 | } | |
1656 | } | |
1657 | ||
1658 | if (preserve) { | |
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) | |
7e1b7485 | 1663 | goto end; |
0f113f3e MC |
1664 | } |
1665 | ||
0f113f3e MC |
1666 | /* We are now totally happy, lets make and sign the certificate */ |
1667 | if (verbose) | |
1668 | BIO_printf(bio_err, | |
1669 | "Everything appears to be ok, creating and signing the certificate\n"); | |
1670 | ||
1671 | if ((ret = X509_new()) == NULL) | |
7e1b7485 | 1672 | goto end; |
d02b48c6 RE |
1673 | |
1674 | #ifdef X509_V3 | |
0f113f3e MC |
1675 | /* Make it an X509 v3 certificate. */ |
1676 | if (!X509_set_version(ret, 2)) | |
7e1b7485 | 1677 | goto end; |
d02b48c6 RE |
1678 | #endif |
1679 | ||
22293ea1 | 1680 | if (BN_to_ASN1_INTEGER(serial, X509_get_serialNumber(ret)) == NULL) |
7e1b7485 | 1681 | goto end; |
0f113f3e MC |
1682 | if (selfsign) { |
1683 | if (!X509_set_issuer_name(ret, subject)) | |
7e1b7485 | 1684 | goto end; |
0f113f3e MC |
1685 | } else { |
1686 | if (!X509_set_issuer_name(ret, X509_get_subject_name(x509))) | |
7e1b7485 | 1687 | goto end; |
0f113f3e MC |
1688 | } |
1689 | ||
dc047d31 DSH |
1690 | if (!set_cert_times(ret, startdate, enddate, days)) |
1691 | goto end; | |
0f113f3e | 1692 | |
dc047d31 | 1693 | if (enddate != NULL) { |
0f113f3e | 1694 | int tdays; |
0d502c35 MC |
1695 | |
1696 | if (!ASN1_TIME_diff(&tdays, NULL, NULL, X509_get0_notAfter(ret))) | |
1697 | goto end; | |
0f113f3e MC |
1698 | days = tdays; |
1699 | } | |
1700 | ||
1701 | if (!X509_set_subject_name(ret, subject)) | |
7e1b7485 | 1702 | goto end; |
0f113f3e | 1703 | |
6e863f07 | 1704 | pktmp = X509_REQ_get0_pubkey(req); |
0f113f3e | 1705 | i = X509_set_pubkey(ret, pktmp); |
0f113f3e | 1706 | if (!i) |
7e1b7485 | 1707 | goto end; |
0f113f3e MC |
1708 | |
1709 | /* Lets add the extensions, if there are any */ | |
1710 | if (ext_sect) { | |
1711 | X509V3_CTX ctx; | |
0f113f3e MC |
1712 | |
1713 | /* Initialize the context structure */ | |
1714 | if (selfsign) | |
1715 | X509V3_set_ctx(&ctx, ret, ret, req, NULL, 0); | |
1716 | else | |
1717 | X509V3_set_ctx(&ctx, x509, ret, req, NULL, 0); | |
1718 | ||
2234212c | 1719 | if (extconf != NULL) { |
0f113f3e MC |
1720 | if (verbose) |
1721 | BIO_printf(bio_err, "Extra configuration file found\n"); | |
1722 | ||
1723 | /* Use the extconf configuration db LHASH */ | |
1724 | X509V3_set_nconf(&ctx, extconf); | |
1725 | ||
1726 | /* Test the structure (needed?) */ | |
1727 | /* X509V3_set_ctx_test(&ctx); */ | |
1728 | ||
1729 | /* Adds exts contained in the configuration file */ | |
1730 | if (!X509V3_EXT_add_nconf(extconf, &ctx, ext_sect, ret)) { | |
1731 | BIO_printf(bio_err, | |
1732 | "ERROR: adding extensions in section %s\n", | |
1733 | ext_sect); | |
1734 | ERR_print_errors(bio_err); | |
7e1b7485 | 1735 | goto end; |
0f113f3e MC |
1736 | } |
1737 | if (verbose) | |
1738 | BIO_printf(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); | |
1743 | ||
1744 | if (!X509V3_EXT_add_nconf(lconf, &ctx, ext_sect, ret)) { | |
1745 | BIO_printf(bio_err, | |
1746 | "ERROR: adding extensions in section %s\n", | |
1747 | ext_sect); | |
1748 | ERR_print_errors(bio_err); | |
7e1b7485 | 1749 | goto end; |
0f113f3e MC |
1750 | } |
1751 | ||
1752 | if (verbose) | |
1753 | BIO_printf(bio_err, | |
1754 | "Successfully added extensions from config\n"); | |
1755 | } | |
1756 | } | |
1757 | ||
1758 | /* Copy extensions from request (if any) */ | |
1759 | ||
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); | |
7e1b7485 | 1763 | goto end; |
0f113f3e MC |
1764 | } |
1765 | ||
4881d849 RL |
1766 | { |
1767 | const STACK_OF(X509_EXTENSION) *exts = X509_get0_extensions(ret); | |
1768 | ||
1769 | if (exts != NULL && sk_X509_EXTENSION_num(exts) > 0) | |
1770 | /* Make it an X509 v3 certificate. */ | |
1771 | if (!X509_set_version(ret, 2)) | |
1772 | goto end; | |
1773 | } | |
1774 | ||
2cedf794 MC |
1775 | if (verbose) |
1776 | BIO_printf(bio_err, | |
1777 | "The subject name appears to be ok, checking data base for clashes\n"); | |
1778 | ||
1779 | /* Build the correct Subject if no e-mail is wanted in the subject. */ | |
1780 | if (!email_dn) { | |
1781 | X509_NAME_ENTRY *tmpne; | |
1782 | X509_NAME *dn_subject; | |
1783 | ||
1784 | /* | |
1785 | * Its best to dup the subject DN and then delete any email addresses | |
1786 | * because this retains its structure. | |
1787 | */ | |
1788 | if ((dn_subject = X509_NAME_dup(subject)) == NULL) { | |
1789 | BIO_printf(bio_err, "Memory allocation failure\n"); | |
1790 | goto end; | |
1791 | } | |
991f0355 | 1792 | i = -1; |
2cedf794 MC |
1793 | while ((i = X509_NAME_get_index_by_NID(dn_subject, |
1794 | NID_pkcs9_emailAddress, | |
991f0355 F |
1795 | i)) >= 0) { |
1796 | tmpne = X509_NAME_delete_entry(dn_subject, i--); | |
2cedf794 MC |
1797 | X509_NAME_ENTRY_free(tmpne); |
1798 | } | |
1799 | ||
1800 | if (!X509_set_subject_name(ret, dn_subject)) { | |
1801 | X509_NAME_free(dn_subject); | |
7e1b7485 | 1802 | goto end; |
2cedf794 MC |
1803 | } |
1804 | X509_NAME_free(dn_subject); | |
1805 | } | |
1806 | ||
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"); | |
1810 | goto end; | |
1811 | } | |
1812 | ||
1813 | if (BN_is_zero(serial)) | |
1814 | row[DB_serial] = OPENSSL_strdup("00"); | |
1815 | else | |
1816 | row[DB_serial] = BN_bn2hex(serial); | |
1817 | if (row[DB_serial] == NULL) { | |
1818 | BIO_printf(bio_err, "Memory allocation failure\n"); | |
1819 | goto end; | |
1820 | } | |
1821 | ||
5af88441 MC |
1822 | if (row[DB_name][0] == '\0') { |
1823 | /* | |
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. | |
1827 | */ | |
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"); | |
1832 | goto end; | |
1833 | } | |
1834 | } | |
1835 | ||
2cedf794 MC |
1836 | if (db->attributes.unique_subject) { |
1837 | OPENSSL_STRING *crow = row; | |
1838 | ||
1839 | rrow = TXT_DB_get_by_index(db->db, DB_name, crow); | |
1840 | if (rrow != NULL) { | |
1841 | BIO_printf(bio_err, | |
1842 | "ERROR:There is already a certificate for %s\n", | |
1843 | row[DB_name]); | |
1844 | } | |
1845 | } | |
1846 | if (rrow == NULL) { | |
1847 | rrow = TXT_DB_get_by_index(db->db, DB_serial, row); | |
1848 | if (rrow != NULL) { | |
1849 | BIO_printf(bio_err, | |
1850 | "ERROR:Serial number %s has already been issued,\n", | |
1851 | row[DB_serial]); | |
1852 | BIO_printf(bio_err, | |
1853 | " check the database/serial_file for corruption\n"); | |
1854 | } | |
1855 | } | |
1856 | ||
1857 | if (rrow != NULL) { | |
1858 | BIO_printf(bio_err, "The matching entry has the following details\n"); | |
1859 | if (rrow[DB_type][0] == DB_TYPE_EXP) | |
1860 | p = "Expired"; | |
1861 | else if (rrow[DB_type][0] == DB_TYPE_REV) | |
1862 | p = "Revoked"; | |
1863 | else if (rrow[DB_type][0] == DB_TYPE_VAL) | |
1864 | p = "Valid"; | |
1865 | else | |
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]; | |
1870 | if (p == NULL) | |
1871 | p = "undef"; | |
1872 | BIO_printf(bio_err, "Was revoked on:%s\n", p); | |
1873 | } | |
1874 | p = rrow[DB_exp_date]; | |
1875 | if (p == NULL) | |
1876 | p = "undef"; | |
1877 | BIO_printf(bio_err, "Expires on :%s\n", p); | |
1878 | p = rrow[DB_serial]; | |
1879 | if (p == NULL) | |
1880 | p = "undef"; | |
1881 | BIO_printf(bio_err, "Serial Number :%s\n", p); | |
1882 | p = rrow[DB_file]; | |
1883 | if (p == NULL) | |
1884 | p = "undef"; | |
1885 | BIO_printf(bio_err, "File name :%s\n", p); | |
1886 | p = rrow[DB_name]; | |
1887 | if (p == NULL) | |
1888 | p = "undef"; | |
1889 | BIO_printf(bio_err, "Subject Name :%s\n", p); | |
1890 | ok = -1; /* This is now a 'bad' error. */ | |
1891 | goto end; | |
0f113f3e MC |
1892 | } |
1893 | ||
1894 | if (!default_op) { | |
1895 | BIO_printf(bio_err, "Certificate Details:\n"); | |
1896 | /* | |
1897 | * Never print signature details because signature not present | |
1898 | */ | |
1899 | certopt |= X509_FLAG_NO_SIGDUMP | X509_FLAG_NO_SIGNAME; | |
1900 | X509_print_ex(bio_err, ret, nameopt, certopt); | |
1901 | } | |
1902 | ||
1903 | BIO_printf(bio_err, "Certificate is to be certified until "); | |
568ce3a5 | 1904 | ASN1_TIME_print(bio_err, X509_get0_notAfter(ret)); |
0f113f3e MC |
1905 | if (days) |
1906 | BIO_printf(bio_err, " (%ld days)", days); | |
1907 | BIO_printf(bio_err, "\n"); | |
1908 | ||
1909 | if (!batch) { | |
1910 | ||
1911 | BIO_printf(bio_err, "Sign the certificate? [y/n]:"); | |
1912 | (void)BIO_flush(bio_err); | |
1913 | buf[0] = '\0'; | |
3a19b22a | 1914 | if (fgets(buf, sizeof(buf), stdin) == NULL) { |
0f113f3e MC |
1915 | BIO_printf(bio_err, |
1916 | "CERTIFICATE WILL NOT BE CERTIFIED: I/O error\n"); | |
1917 | ok = 0; | |
7e1b7485 | 1918 | goto end; |
0f113f3e | 1919 | } |
3a19b22a | 1920 | if (!(buf[0] == 'y' || buf[0] == 'Y')) { |
0f113f3e MC |
1921 | BIO_printf(bio_err, "CERTIFICATE WILL NOT BE CERTIFIED\n"); |
1922 | ok = 0; | |
7e1b7485 | 1923 | goto end; |
0f113f3e MC |
1924 | } |
1925 | } | |
1926 | ||
c01ff880 | 1927 | pktmp = X509_get0_pubkey(ret); |
0f113f3e MC |
1928 | if (EVP_PKEY_missing_parameters(pktmp) && |
1929 | !EVP_PKEY_missing_parameters(pkey)) | |
1930 | EVP_PKEY_copy_parameters(pktmp, pkey); | |
0f113f3e | 1931 | |
7e1b7485 RS |
1932 | if (!do_X509_sign(ret, pkey, dgst, sigopts)) |
1933 | goto end; | |
0f113f3e | 1934 | |
06a79af2 | 1935 | /* We now just add it to the database as DB_TYPE_VAL('V') */ |
7644a9ae | 1936 | row[DB_type] = OPENSSL_strdup("V"); |
568ce3a5 | 1937 | tm = X509_get0_notAfter(ret); |
68dc6824 | 1938 | row[DB_exp_date] = app_malloc(tm->length + 1, "row expdate"); |
0f113f3e MC |
1939 | memcpy(row[DB_exp_date], tm->data, tm->length); |
1940 | row[DB_exp_date][tm->length] = '\0'; | |
0f113f3e | 1941 | row[DB_rev_date] = NULL; |
7644a9ae | 1942 | row[DB_file] = OPENSSL_strdup("unknown"); |
0f113f3e MC |
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"); | |
7e1b7485 | 1946 | goto end; |
0f113f3e | 1947 | } |
0f113f3e | 1948 | |
b4faea50 | 1949 | irow = app_malloc(sizeof(*irow) * (DB_NUMBER + 1), "row space"); |
9ad52c56 | 1950 | for (i = 0; i < DB_NUMBER; i++) |
0f113f3e | 1951 | irow[i] = row[i]; |
0f113f3e MC |
1952 | irow[DB_NUMBER] = NULL; |
1953 | ||
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); | |
7e1b7485 | 1957 | goto end; |
0f113f3e | 1958 | } |
9ad52c56 | 1959 | irow = NULL; |
0f113f3e | 1960 | ok = 1; |
7e1b7485 | 1961 | end: |
01985122 | 1962 | if (ok != 1) { |
9ad52c56 BE |
1963 | for (i = 0; i < DB_NUMBER; i++) |
1964 | OPENSSL_free(row[i]); | |
9ad52c56 | 1965 | } |
01985122 | 1966 | OPENSSL_free(irow); |
0f113f3e | 1967 | |
222561fe RS |
1968 | X509_NAME_free(CAname); |
1969 | X509_NAME_free(subject); | |
222561fe RS |
1970 | if (ok <= 0) |
1971 | X509_free(ret); | |
1972 | else | |
0f113f3e | 1973 | *xret = ret; |
26a7d938 | 1974 | return ok; |
0f113f3e MC |
1975 | } |
1976 | ||
b5895815 | 1977 | static void write_new_certificate(BIO *bp, X509 *x, int output_der, int notext) |
0f113f3e MC |
1978 | { |
1979 | ||
1980 | if (output_der) { | |
1981 | (void)i2d_X509_bio(bp, x); | |
1982 | return; | |
1983 | } | |
0f113f3e MC |
1984 | if (!notext) |
1985 | X509_print(bp, x); | |
1986 | PEM_write_bio_X509(bp, x); | |
1987 | } | |
1988 | ||
cc696296 | 1989 | static int certify_spkac(X509 **xret, const char *infile, EVP_PKEY *pkey, |
0f113f3e MC |
1990 | X509 *x509, const EVP_MD *dgst, |
1991 | STACK_OF(OPENSSL_STRING) *sigopts, | |
1992 | STACK_OF(CONF_VALUE) *policy, CA_DB *db, | |
cc696296 F |
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, | |
0f113f3e MC |
1996 | CONF *lconf, int verbose, unsigned long certopt, |
1997 | unsigned long nameopt, int default_op, int ext_copy) | |
1998 | { | |
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; | |
0f113f3e MC |
2004 | char *type, *buf; |
2005 | EVP_PKEY *pktmp = NULL; | |
2006 | X509_NAME *n = NULL; | |
2007 | X509_NAME_ENTRY *ne = NULL; | |
2008 | int ok = -1, i, j; | |
2009 | long errline; | |
2010 | int nid; | |
2011 | ||
2012 | /* | |
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 | |
2015 | * STACK format). | |
2016 | */ | |
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); | |
7e1b7485 | 2021 | goto end; |
0f113f3e MC |
2022 | } |
2023 | ||
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); | |
7e1b7485 | 2027 | goto end; |
0f113f3e MC |
2028 | } |
2029 | ||
2030 | /* | |
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. | |
2036 | */ | |
2037 | req = X509_REQ_new(); | |
2038 | if (req == NULL) { | |
2039 | ERR_print_errors(bio_err); | |
7e1b7485 | 2040 | goto end; |
0f113f3e MC |
2041 | } |
2042 | ||
2043 | /* | |
2044 | * Build up the subject name set. | |
2045 | */ | |
124055a9 | 2046 | n = X509_REQ_get_subject_name(req); |
0f113f3e MC |
2047 | |
2048 | for (i = 0;; i++) { | |
2049 | if (sk_CONF_VALUE_num(sk) <= i) | |
2050 | break; | |
2051 | ||
2052 | cv = sk_CONF_VALUE_value(sk, i); | |
2053 | type = cv->name; | |
2054 | /* | |
2055 | * Skip past any leading X. X: X, etc to allow for multiple instances | |
2056 | */ | |
2057 | for (buf = cv->name; *buf; buf++) | |
2058 | if ((*buf == ':') || (*buf == ',') || (*buf == '.')) { | |
2059 | buf++; | |
2060 | if (*buf) | |
2061 | type = buf; | |
2062 | break; | |
2063 | } | |
2064 | ||
2065 | buf = cv->value; | |
2066 | if ((nid = OBJ_txt2nid(type)) == NID_undef) { | |
2067 | if (strcmp(type, "SPKAC") == 0) { | |
2068 | spki = NETSCAPE_SPKI_b64_decode(cv->value, -1); | |
2069 | if (spki == NULL) { | |
2070 | BIO_printf(bio_err, | |
2071 | "unable to load Netscape SPKAC structure\n"); | |
2072 | ERR_print_errors(bio_err); | |
7e1b7485 | 2073 | goto end; |
0f113f3e MC |
2074 | } |
2075 | } | |
2076 | continue; | |
2077 | } | |
2078 | ||
2079 | if (!X509_NAME_add_entry_by_NID(n, nid, chtype, | |
2080 | (unsigned char *)buf, -1, -1, 0)) | |
7e1b7485 | 2081 | goto end; |
0f113f3e MC |
2082 | } |
2083 | if (spki == NULL) { | |
2084 | BIO_printf(bio_err, "Netscape SPKAC structure not found in %s\n", | |
2085 | infile); | |
7e1b7485 | 2086 | goto end; |
0f113f3e MC |
2087 | } |
2088 | ||
2089 | /* | |
2090 | * Now extract the key from the SPKI structure. | |
2091 | */ | |
2092 | ||
b5895815 | 2093 | BIO_printf(bio_err, "Check that the SPKAC request matches the signature\n"); |
0f113f3e MC |
2094 | |
2095 | if ((pktmp = NETSCAPE_SPKI_get_pubkey(spki)) == NULL) { | |
2096 | BIO_printf(bio_err, "error unpacking SPKAC public key\n"); | |
7e1b7485 | 2097 | goto end; |
0f113f3e MC |
2098 | } |
2099 | ||
2100 | j = NETSCAPE_SPKI_verify(spki, pktmp); | |
2101 | if (j <= 0) { | |
f6c006ea | 2102 | EVP_PKEY_free(pktmp); |
0f113f3e MC |
2103 | BIO_printf(bio_err, |
2104 | "signature verification failed on SPKAC public key\n"); | |
7e1b7485 | 2105 | goto end; |
0f113f3e MC |
2106 | } |
2107 | BIO_printf(bio_err, "Signature ok\n"); | |
2108 | ||
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, | |
2114 | ext_copy, 0); | |
7e1b7485 | 2115 | end: |
222561fe | 2116 | X509_REQ_free(req); |
25aaa98a RS |
2117 | CONF_free(parms); |
2118 | NETSCAPE_SPKI_free(spki); | |
222561fe | 2119 | X509_NAME_ENTRY_free(ne); |
0f113f3e | 2120 | |
26a7d938 | 2121 | return ok; |
0f113f3e | 2122 | } |
d02b48c6 | 2123 | |
7c337e00 | 2124 | static int check_time_format(const char *str) |
0f113f3e MC |
2125 | { |
2126 | return ASN1_TIME_set_string(NULL, str); | |
2127 | } | |
d02b48c6 | 2128 | |
06a79af2 F |
2129 | static int do_revoke(X509 *x509, CA_DB *db, REVINFO_TYPE rev_type, |
2130 | const char *value) | |
0f113f3e | 2131 | { |
568ce3a5 | 2132 | const ASN1_TIME *tm = NULL; |
0f113f3e MC |
2133 | char *row[DB_NUMBER], **rrow, **irow; |
2134 | char *rev_str = NULL; | |
2135 | BIGNUM *bn = NULL; | |
2136 | int ok = -1, i; | |
2137 | ||
2138 | for (i = 0; i < DB_NUMBER; i++) | |
2139 | row[i] = NULL; | |
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); | |
2142 | if (!bn) | |
7e1b7485 | 2143 | goto end; |
0f113f3e | 2144 | if (BN_is_zero(bn)) |
7644a9ae | 2145 | row[DB_serial] = OPENSSL_strdup("00"); |
0f113f3e MC |
2146 | else |
2147 | row[DB_serial] = BN_bn2hex(bn); | |
2148 | BN_free(bn); | |
5af88441 MC |
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]); | |
2153 | } | |
0f113f3e MC |
2154 | if ((row[DB_name] == NULL) || (row[DB_serial] == NULL)) { |
2155 | BIO_printf(bio_err, "Memory allocation failure\n"); | |
7e1b7485 | 2156 | goto end; |
0f113f3e MC |
2157 | } |
2158 | /* | |
2159 | * We have to lookup by serial number because name lookup skips revoked | |
2160 | * certs | |
2161 | */ | |
2162 | rrow = TXT_DB_get_by_index(db->db, DB_serial, row); | |
2163 | if (rrow == NULL) { | |
2164 | BIO_printf(bio_err, | |
2165 | "Adding Entry with serial number %s to DB for %s\n", | |
2166 | row[DB_serial], row[DB_name]); | |
2167 | ||
06a79af2 | 2168 | /* We now just add it to the database as DB_TYPE_REV('V') */ |
7644a9ae | 2169 | row[DB_type] = OPENSSL_strdup("V"); |
568ce3a5 | 2170 | tm = X509_get0_notAfter(x509); |
68dc6824 | 2171 | row[DB_exp_date] = app_malloc(tm->length + 1, "row exp_data"); |
0f113f3e MC |
2172 | memcpy(row[DB_exp_date], tm->data, tm->length); |
2173 | row[DB_exp_date][tm->length] = '\0'; | |
0f113f3e | 2174 | row[DB_rev_date] = NULL; |
7644a9ae | 2175 | row[DB_file] = OPENSSL_strdup("unknown"); |
0f113f3e | 2176 | |
0fbaef9e BE |
2177 | if (row[DB_type] == NULL || row[DB_file] == NULL) { |
2178 | BIO_printf(bio_err, "Memory allocation failure\n"); | |
2179 | goto end; | |
2180 | } | |
2181 | ||
b4faea50 | 2182 | irow = app_malloc(sizeof(*irow) * (DB_NUMBER + 1), "row ptr"); |
0fbaef9e | 2183 | for (i = 0; i < DB_NUMBER; i++) |
0f113f3e | 2184 | irow[i] = row[i]; |
0f113f3e MC |
2185 | irow[DB_NUMBER] = NULL; |
2186 | ||
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); | |
0fbaef9e | 2190 | OPENSSL_free(irow); |
7e1b7485 | 2191 | goto end; |
0f113f3e MC |
2192 | } |
2193 | ||
0fbaef9e BE |
2194 | for (i = 0; i < DB_NUMBER; i++) |
2195 | row[i] = NULL; | |
2196 | ||
0f113f3e | 2197 | /* Revoke Certificate */ |
06a79af2 | 2198 | if (rev_type == REV_VALID) |
0f113f3e MC |
2199 | ok = 1; |
2200 | else | |
06a79af2 F |
2201 | /* Retry revocation after DB insertion */ |
2202 | ok = do_revoke(x509, db, rev_type, value); | |
0f113f3e | 2203 | |
7e1b7485 | 2204 | goto end; |
0f113f3e MC |
2205 | |
2206 | } else if (index_name_cmp_noconst(row, rrow)) { | |
2207 | BIO_printf(bio_err, "ERROR:name does not match %s\n", row[DB_name]); | |
7e1b7485 | 2208 | goto end; |
06a79af2 | 2209 | } else if (rev_type == REV_VALID) { |
0f113f3e MC |
2210 | BIO_printf(bio_err, "ERROR:Already present, serial number %s\n", |
2211 | row[DB_serial]); | |
7e1b7485 | 2212 | goto end; |
06a79af2 | 2213 | } else if (rrow[DB_type][0] == DB_TYPE_REV) { |
0f113f3e MC |
2214 | BIO_printf(bio_err, "ERROR:Already revoked, serial number %s\n", |
2215 | row[DB_serial]); | |
7e1b7485 | 2216 | goto end; |
0f113f3e MC |
2217 | } else { |
2218 | BIO_printf(bio_err, "Revoking Certificate %s.\n", rrow[DB_serial]); | |
06a79af2 | 2219 | rev_str = make_revocation_str(rev_type, value); |
0f113f3e MC |
2220 | if (!rev_str) { |
2221 | BIO_printf(bio_err, "Error in revocation arguments\n"); | |
7e1b7485 | 2222 | goto end; |
0f113f3e | 2223 | } |
06a79af2 | 2224 | rrow[DB_type][0] = DB_TYPE_REV; |
0f113f3e MC |
2225 | rrow[DB_type][1] = '\0'; |
2226 | rrow[DB_rev_date] = rev_str; | |
2227 | } | |
2228 | ok = 1; | |
7e1b7485 | 2229 | end: |
0fbaef9e | 2230 | for (i = 0; i < DB_NUMBER; i++) |
b548a1f1 | 2231 | OPENSSL_free(row[i]); |
26a7d938 | 2232 | return ok; |
0f113f3e | 2233 | } |
c67cdb50 | 2234 | |
f85b68cd | 2235 | static int get_certificate_status(const char *serial, CA_DB *db) |
0f113f3e MC |
2236 | { |
2237 | char *row[DB_NUMBER], **rrow; | |
2238 | int ok = -1, i; | |
f6c460e8 | 2239 | size_t serial_len = strlen(serial); |
0f113f3e MC |
2240 | |
2241 | /* Free Resources */ | |
2242 | for (i = 0; i < DB_NUMBER; i++) | |
2243 | row[i] = NULL; | |
2244 | ||
2245 | /* Malloc needed char spaces */ | |
f6c460e8 | 2246 | row[DB_serial] = app_malloc(serial_len + 2, "row serial#"); |
0f113f3e | 2247 | |
f6c460e8 | 2248 | if (serial_len % 2) { |
0f113f3e MC |
2249 | /* |
2250 | * Set the first char to 0 | |
9fa36f5f | 2251 | */ |
0f113f3e MC |
2252 | row[DB_serial][0] = '0'; |
2253 | ||
2254 | /* Copy String from serial to row[DB_serial] */ | |
f6c460e8 F |
2255 | memcpy(row[DB_serial] + 1, serial, serial_len); |
2256 | row[DB_serial][serial_len + 1] = '\0'; | |
0f113f3e MC |
2257 | } else { |
2258 | /* Copy String from serial to row[DB_serial] */ | |
f6c460e8 F |
2259 | memcpy(row[DB_serial], serial, serial_len); |
2260 | row[DB_serial][serial_len] = '\0'; | |
0f113f3e MC |
2261 | } |
2262 | ||
2263 | /* Make it Upper Case */ | |
20967afb | 2264 | make_uppercase(row[DB_serial]); |
0f113f3e MC |
2265 | |
2266 | ok = 1; | |
2267 | ||
2268 | /* Search for the certificate */ | |
2269 | rrow = TXT_DB_get_by_index(db->db, DB_serial, row); | |
2270 | if (rrow == NULL) { | |
2271 | BIO_printf(bio_err, "Serial %s not present in db.\n", row[DB_serial]); | |
2272 | ok = -1; | |
7e1b7485 | 2273 | goto end; |
06a79af2 | 2274 | } else if (rrow[DB_type][0] == DB_TYPE_VAL) { |
0f113f3e MC |
2275 | BIO_printf(bio_err, "%s=Valid (%c)\n", |
2276 | row[DB_serial], rrow[DB_type][0]); | |
7e1b7485 | 2277 | goto end; |
06a79af2 | 2278 | } else if (rrow[DB_type][0] == DB_TYPE_REV) { |
0f113f3e MC |
2279 | BIO_printf(bio_err, "%s=Revoked (%c)\n", |
2280 | row[DB_serial], rrow[DB_type][0]); | |
7e1b7485 | 2281 | goto end; |
06a79af2 | 2282 | } else if (rrow[DB_type][0] == DB_TYPE_EXP) { |
0f113f3e MC |
2283 | BIO_printf(bio_err, "%s=Expired (%c)\n", |
2284 | row[DB_serial], rrow[DB_type][0]); | |
7e1b7485 | 2285 | goto end; |
06a79af2 | 2286 | } else if (rrow[DB_type][0] == DB_TYPE_SUSP) { |
0f113f3e MC |
2287 | BIO_printf(bio_err, "%s=Suspended (%c)\n", |
2288 | row[DB_serial], rrow[DB_type][0]); | |
7e1b7485 | 2289 | goto end; |
0f113f3e MC |
2290 | } else { |
2291 | BIO_printf(bio_err, "%s=Unknown (%c).\n", | |
2292 | row[DB_serial], rrow[DB_type][0]); | |
2293 | ok = -1; | |
2294 | } | |
7e1b7485 | 2295 | end: |
0f113f3e | 2296 | for (i = 0; i < DB_NUMBER; i++) { |
b548a1f1 | 2297 | OPENSSL_free(row[i]); |
0f113f3e | 2298 | } |
26a7d938 | 2299 | return ok; |
0f113f3e MC |
2300 | } |
2301 | ||
2302 | static int do_updatedb(CA_DB *db) | |
2303 | { | |
2304 | ASN1_UTCTIME *a_tm = NULL; | |
2305 | int i, cnt = 0; | |
2306 | int db_y2k, a_y2k; /* flags = 1 if y >= 2000 */ | |
2307 | char **rrow, *a_tm_s; | |
2308 | ||
2309 | a_tm = ASN1_UTCTIME_new(); | |
96487cdd MC |
2310 | if (a_tm == NULL) |
2311 | return -1; | |
0f113f3e MC |
2312 | |
2313 | /* get actual time and make a string */ | |
bc2a0dd2 MC |
2314 | if (X509_gmtime_adj(a_tm, 0) == NULL) { |
2315 | ASN1_UTCTIME_free(a_tm); | |
2316 | return -1; | |
2317 | } | |
edbff8da | 2318 | a_tm_s = app_malloc(a_tm->length + 1, "time string"); |
0f113f3e MC |
2319 | |
2320 | memcpy(a_tm_s, a_tm->data, a_tm->length); | |
2321 | a_tm_s[a_tm->length] = '\0'; | |
2322 | ||
2323 | if (strncmp(a_tm_s, "49", 2) <= 0) | |
2324 | a_y2k = 1; | |
2325 | else | |
2326 | a_y2k = 0; | |
2327 | ||
2328 | for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) { | |
2329 | rrow = sk_OPENSSL_PSTRING_value(db->db->data, i); | |
2330 | ||
06a79af2 | 2331 | if (rrow[DB_type][0] == DB_TYPE_VAL) { |
0f113f3e MC |
2332 | /* ignore entries that are not valid */ |
2333 | if (strncmp(rrow[DB_exp_date], "49", 2) <= 0) | |
2334 | db_y2k = 1; | |
2335 | else | |
2336 | db_y2k = 0; | |
2337 | ||
2338 | if (db_y2k == a_y2k) { | |
2339 | /* all on the same y2k side */ | |
2340 | if (strcmp(rrow[DB_exp_date], a_tm_s) <= 0) { | |
06a79af2 | 2341 | rrow[DB_type][0] = DB_TYPE_EXP; |
0f113f3e MC |
2342 | rrow[DB_type][1] = '\0'; |
2343 | cnt++; | |
2344 | ||
2345 | BIO_printf(bio_err, "%s=Expired\n", rrow[DB_serial]); | |
2346 | } | |
2347 | } else if (db_y2k < a_y2k) { | |
06a79af2 | 2348 | rrow[DB_type][0] = DB_TYPE_EXP; |
0f113f3e MC |
2349 | rrow[DB_type][1] = '\0'; |
2350 | cnt++; | |
2351 | ||
2352 | BIO_printf(bio_err, "%s=Expired\n", rrow[DB_serial]); | |
2353 | } | |
2354 | ||
2355 | } | |
2356 | } | |
2357 | ||
0f113f3e MC |
2358 | ASN1_UTCTIME_free(a_tm); |
2359 | OPENSSL_free(a_tm_s); | |
26a7d938 | 2360 | return cnt; |
0f113f3e | 2361 | } |
a6b7ffdd | 2362 | |
7d727231 | 2363 | static const char *crl_reasons[] = { |
0f113f3e MC |
2364 | /* CRL reason strings */ |
2365 | "unspecified", | |
2366 | "keyCompromise", | |
2367 | "CACompromise", | |
2368 | "affiliationChanged", | |
2369 | "superseded", | |
2370 | "cessationOfOperation", | |
2371 | "certificateHold", | |
2372 | "removeFromCRL", | |
2373 | /* Additional pseudo reasons */ | |
2374 | "holdInstruction", | |
2375 | "keyTime", | |
2376 | "CAkeyTime" | |
a6b7ffdd DSH |
2377 | }; |
2378 | ||
bdcb1a2c | 2379 | #define NUM_REASONS OSSL_NELEM(crl_reasons) |
a6b7ffdd | 2380 | |
0f113f3e MC |
2381 | /* |
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 | |
a6b7ffdd DSH |
2386 | */ |
2387 | ||
06a79af2 | 2388 | static char *make_revocation_str(REVINFO_TYPE rev_type, const char *rev_arg) |
0f113f3e | 2389 | { |
cc696296 | 2390 | char *str; |
06a79af2 | 2391 | const char *reason = NULL, *other = NULL; |
0f113f3e MC |
2392 | ASN1_OBJECT *otmp; |
2393 | ASN1_UTCTIME *revtm = NULL; | |
2394 | int i; | |
06a79af2 | 2395 | |
0f113f3e MC |
2396 | switch (rev_type) { |
2397 | case REV_NONE: | |
06a79af2 | 2398 | case REV_VALID: |
0f113f3e MC |
2399 | break; |
2400 | ||
2401 | case REV_CRL_REASON: | |
2402 | for (i = 0; i < 8; i++) { | |
86885c28 | 2403 | if (strcasecmp(rev_arg, crl_reasons[i]) == 0) { |
0f113f3e MC |
2404 | reason = crl_reasons[i]; |
2405 | break; | |
2406 | } | |
2407 | } | |
2408 | if (reason == NULL) { | |
2409 | BIO_printf(bio_err, "Unknown CRL reason %s\n", rev_arg); | |
2410 | return NULL; | |
2411 | } | |
2412 | break; | |
2413 | ||
2414 | case REV_HOLD: | |
2415 | /* Argument is an OID */ | |
0f113f3e MC |
2416 | otmp = OBJ_txt2obj(rev_arg, 0); |
2417 | ASN1_OBJECT_free(otmp); | |
2418 | ||
2419 | if (otmp == NULL) { | |
2420 | BIO_printf(bio_err, "Invalid object identifier %s\n", rev_arg); | |
2421 | return NULL; | |
2422 | } | |
2423 | ||
2424 | reason = "holdInstruction"; | |
2425 | other = rev_arg; | |
2426 | break; | |
2427 | ||
2428 | case REV_KEY_COMPROMISE: | |
2429 | case REV_CA_COMPROMISE: | |
0f113f3e MC |
2430 | /* Argument is the key compromise time */ |
2431 | if (!ASN1_GENERALIZEDTIME_set_string(NULL, rev_arg)) { | |
2432 | BIO_printf(bio_err, | |
2433 | "Invalid time format %s. Need YYYYMMDDHHMMSSZ\n", | |
2434 | rev_arg); | |
2435 | return NULL; | |
2436 | } | |
2437 | other = rev_arg; | |
2438 | if (rev_type == REV_KEY_COMPROMISE) | |
2439 | reason = "keyTime"; | |
2440 | else | |
2441 | reason = "CAkeyTime"; | |
2442 | ||
2443 | break; | |
0f113f3e MC |
2444 | } |
2445 | ||
2446 | revtm = X509_gmtime_adj(NULL, 0); | |
2447 | ||
2448 | if (!revtm) | |
2449 | return NULL; | |
2450 | ||
2451 | i = revtm->length + 1; | |
2452 | ||
2453 | if (reason) | |
2454 | i += strlen(reason) + 1; | |
2455 | if (other) | |
2456 | i += strlen(other) + 1; | |
2457 | ||
68dc6824 | 2458 | str = app_malloc(i, "revocation reason"); |
7644a9ae | 2459 | OPENSSL_strlcpy(str, (char *)revtm->data, i); |
0f113f3e | 2460 | if (reason) { |
7644a9ae RS |
2461 | OPENSSL_strlcat(str, ",", i); |
2462 | OPENSSL_strlcat(str, reason, i); | |
0f113f3e MC |
2463 | } |
2464 | if (other) { | |
7644a9ae RS |
2465 | OPENSSL_strlcat(str, ",", i); |
2466 | OPENSSL_strlcat(str, other, i); | |
0f113f3e MC |
2467 | } |
2468 | ASN1_UTCTIME_free(revtm); | |
2469 | return str; | |
2470 | } | |
a6b7ffdd | 2471 | |
1d97c843 | 2472 | /*- |
0f113f3e | 2473 | * Convert revocation field to X509_REVOKED entry |
a6b7ffdd DSH |
2474 | * return code: |
2475 | * 0 error | |
2476 | * 1 OK | |
2477 | * 2 OK and some extensions added (i.e. V2 CRL) | |
2478 | */ | |
2479 | ||
06a79af2 | 2480 | static int make_revoked(X509_REVOKED *rev, const char *str) |
0f113f3e MC |
2481 | { |
2482 | char *tmp = NULL; | |
2483 | int reason_code = -1; | |
2484 | int i, ret = 0; | |
2485 | ASN1_OBJECT *hold = NULL; | |
2486 | ASN1_GENERALIZEDTIME *comp_time = NULL; | |
2487 | ASN1_ENUMERATED *rtmp = NULL; | |
2488 | ||
2489 | ASN1_TIME *revDate = NULL; | |
2490 | ||
2491 | i = unpack_revinfo(&revDate, &reason_code, &hold, &comp_time, str); | |
2492 | ||
2493 | if (i == 0) | |
7e1b7485 | 2494 | goto end; |
0f113f3e MC |
2495 | |
2496 | if (rev && !X509_REVOKED_set_revocationDate(rev, revDate)) | |
7e1b7485 | 2497 | goto end; |
0f113f3e MC |
2498 | |
2499 | if (rev && (reason_code != OCSP_REVOKED_STATUS_NOSTATUS)) { | |
2500 | rtmp = ASN1_ENUMERATED_new(); | |
96487cdd | 2501 | if (rtmp == NULL || !ASN1_ENUMERATED_set(rtmp, reason_code)) |
7e1b7485 | 2502 | goto end; |
0f113f3e | 2503 | if (!X509_REVOKED_add1_ext_i2d(rev, NID_crl_reason, rtmp, 0, 0)) |
7e1b7485 | 2504 | goto end; |
0f113f3e MC |
2505 | } |
2506 | ||
2507 | if (rev && comp_time) { | |
2508 | if (!X509_REVOKED_add1_ext_i2d | |
2509 | (rev, NID_invalidity_date, comp_time, 0, 0)) | |
7e1b7485 | 2510 | goto end; |
0f113f3e MC |
2511 | } |
2512 | if (rev && hold) { | |
2513 | if (!X509_REVOKED_add1_ext_i2d | |
2514 | (rev, NID_hold_instruction_code, hold, 0, 0)) | |
7e1b7485 | 2515 | goto end; |
0f113f3e MC |
2516 | } |
2517 | ||
2518 | if (reason_code != OCSP_REVOKED_STATUS_NOSTATUS) | |
2519 | ret = 2; | |
2520 | else | |
2521 | ret = 1; | |
2522 | ||
7e1b7485 | 2523 | end: |
0f113f3e | 2524 | |
b548a1f1 | 2525 | OPENSSL_free(tmp); |
0f113f3e MC |
2526 | ASN1_OBJECT_free(hold); |
2527 | ASN1_GENERALIZEDTIME_free(comp_time); | |
2528 | ASN1_ENUMERATED_free(rtmp); | |
2529 | ASN1_TIME_free(revDate); | |
2530 | ||
2531 | return ret; | |
2532 | } | |
bad40585 | 2533 | |
69b017f6 | 2534 | static int old_entry_print(const ASN1_OBJECT *obj, const ASN1_STRING *str) |
0f113f3e | 2535 | { |
cc696296 F |
2536 | char buf[25], *pbuf; |
2537 | const char *p; | |
0f113f3e | 2538 | int j; |
cc696296 | 2539 | |
ecf3a1fb | 2540 | j = i2a_ASN1_OBJECT(bio_err, obj); |
0f113f3e MC |
2541 | pbuf = buf; |
2542 | for (j = 22 - j; j > 0; j--) | |
2543 | *(pbuf++) = ' '; | |
2544 | *(pbuf++) = ':'; | |
2545 | *(pbuf++) = '\0'; | |
ecf3a1fb | 2546 | BIO_puts(bio_err, buf); |
0f113f3e MC |
2547 | |
2548 | if (str->type == V_ASN1_PRINTABLESTRING) | |
ecf3a1fb | 2549 | BIO_printf(bio_err, "PRINTABLE:'"); |
0f113f3e | 2550 | else if (str->type == V_ASN1_T61STRING) |
ecf3a1fb | 2551 | BIO_printf(bio_err, "T61STRING:'"); |
0f113f3e | 2552 | else if (str->type == V_ASN1_IA5STRING) |
ecf3a1fb | 2553 | BIO_printf(bio_err, "IA5STRING:'"); |
0f113f3e | 2554 | else if (str->type == V_ASN1_UNIVERSALSTRING) |
ecf3a1fb | 2555 | BIO_printf(bio_err, "UNIVERSALSTRING:'"); |
0f113f3e | 2556 | else |
ecf3a1fb | 2557 | BIO_printf(bio_err, "ASN.1 %2d:'", str->type); |
0f113f3e | 2558 | |
cc696296 | 2559 | p = (const char *)str->data; |
0f113f3e MC |
2560 | for (j = str->length; j > 0; j--) { |
2561 | if ((*p >= ' ') && (*p <= '~')) | |
ecf3a1fb | 2562 | BIO_printf(bio_err, "%c", *p); |
0f113f3e | 2563 | else if (*p & 0x80) |
ecf3a1fb | 2564 | BIO_printf(bio_err, "\\0x%02X", *p); |
0f113f3e | 2565 | else if ((unsigned char)*p == 0xf7) |
ecf3a1fb | 2566 | BIO_printf(bio_err, "^?"); |
0f113f3e | 2567 | else |
ecf3a1fb | 2568 | BIO_printf(bio_err, "^%c", *p + '@'); |
0f113f3e MC |
2569 | p++; |
2570 | } | |
ecf3a1fb | 2571 | BIO_printf(bio_err, "'\n"); |
0f113f3e MC |
2572 | return 1; |
2573 | } | |
2574 | ||
2575 | int unpack_revinfo(ASN1_TIME **prevtm, int *preason, ASN1_OBJECT **phold, | |
2576 | ASN1_GENERALIZEDTIME **pinvtm, const char *str) | |
2577 | { | |
7e1b7485 | 2578 | char *tmp; |
0f113f3e MC |
2579 | char *rtime_str, *reason_str = NULL, *arg_str = NULL, *p; |
2580 | int reason_code = -1; | |
2581 | int ret = 0; | |
2582 | unsigned int i; | |
2583 | ASN1_OBJECT *hold = NULL; | |
2584 | ASN1_GENERALIZEDTIME *comp_time = NULL; | |
0f113f3e | 2585 | |
7644a9ae | 2586 | tmp = OPENSSL_strdup(str); |
61986d32 | 2587 | if (!tmp) { |
a561bfe9 | 2588 | BIO_printf(bio_err, "memory allocation failure\n"); |
7e1b7485 | 2589 | goto end; |
a561bfe9 MC |
2590 | } |
2591 | ||
0f113f3e MC |
2592 | p = strchr(tmp, ','); |
2593 | ||
2594 | rtime_str = tmp; | |
2595 | ||
2596 | if (p) { | |
2597 | *p = '\0'; | |
2598 | p++; | |
2599 | reason_str = p; | |
2600 | p = strchr(p, ','); | |
2601 | if (p) { | |
2602 | *p = '\0'; | |
2603 | arg_str = p + 1; | |
2604 | } | |
2605 | } | |
2606 | ||
2607 | if (prevtm) { | |
2608 | *prevtm = ASN1_UTCTIME_new(); | |
96487cdd | 2609 | if (*prevtm == NULL) { |
a561bfe9 | 2610 | BIO_printf(bio_err, "memory allocation failure\n"); |
7e1b7485 | 2611 | goto end; |
a561bfe9 | 2612 | } |
0f113f3e MC |
2613 | if (!ASN1_UTCTIME_set_string(*prevtm, rtime_str)) { |
2614 | BIO_printf(bio_err, "invalid revocation date %s\n", rtime_str); | |
7e1b7485 | 2615 | goto end; |
0f113f3e MC |
2616 | } |
2617 | } | |
2618 | if (reason_str) { | |
2619 | for (i = 0; i < NUM_REASONS; i++) { | |
86885c28 | 2620 | if (strcasecmp(reason_str, crl_reasons[i]) == 0) { |
0f113f3e MC |
2621 | reason_code = i; |
2622 | break; | |
2623 | } | |
2624 | } | |
2625 | if (reason_code == OCSP_REVOKED_STATUS_NOSTATUS) { | |
2626 | BIO_printf(bio_err, "invalid reason code %s\n", reason_str); | |
7e1b7485 | 2627 | goto end; |
0f113f3e MC |
2628 | } |
2629 | ||
2234212c | 2630 | if (reason_code == 7) { |
0f113f3e | 2631 | reason_code = OCSP_REVOKED_STATUS_REMOVEFROMCRL; |
2234212c | 2632 | } else if (reason_code == 8) { /* Hold instruction */ |
0f113f3e MC |
2633 | if (!arg_str) { |
2634 | BIO_printf(bio_err, "missing hold instruction\n"); | |
7e1b7485 | 2635 | goto end; |
0f113f3e MC |
2636 | } |
2637 | reason_code = OCSP_REVOKED_STATUS_CERTIFICATEHOLD; | |
2638 | hold = OBJ_txt2obj(arg_str, 0); | |
2639 | ||
2640 | if (!hold) { | |
b5895815 | 2641 | BIO_printf(bio_err, "invalid object identifier %s\n", arg_str); |
7e1b7485 | 2642 | goto end; |
0f113f3e MC |
2643 | } |
2644 | if (phold) | |
2645 | *phold = hold; | |
2ace7450 RS |
2646 | else |
2647 | ASN1_OBJECT_free(hold); | |
0f113f3e MC |
2648 | } else if ((reason_code == 9) || (reason_code == 10)) { |
2649 | if (!arg_str) { | |
2650 | BIO_printf(bio_err, "missing compromised time\n"); | |
7e1b7485 | 2651 | goto end; |
0f113f3e MC |
2652 | } |
2653 | comp_time = ASN1_GENERALIZEDTIME_new(); | |
96487cdd | 2654 | if (comp_time == NULL) { |
a561bfe9 | 2655 | BIO_printf(bio_err, "memory allocation failure\n"); |
7e1b7485 | 2656 | goto end; |
a561bfe9 | 2657 | } |
0f113f3e MC |
2658 | if (!ASN1_GENERALIZEDTIME_set_string(comp_time, arg_str)) { |
2659 | BIO_printf(bio_err, "invalid compromised time %s\n", arg_str); | |
7e1b7485 | 2660 | goto end; |
0f113f3e MC |
2661 | } |
2662 | if (reason_code == 9) | |
2663 | reason_code = OCSP_REVOKED_STATUS_KEYCOMPROMISE; | |
2664 | else | |
2665 | reason_code = OCSP_REVOKED_STATUS_CACOMPROMISE; | |
2666 | } | |
2667 | } | |
2668 | ||
2669 | if (preason) | |
2670 | *preason = reason_code; | |
2ace7450 | 2671 | if (pinvtm) { |
0f113f3e | 2672 | *pinvtm = comp_time; |
2ace7450 RS |
2673 | comp_time = NULL; |
2674 | } | |
0f113f3e MC |
2675 | |
2676 | ret = 1; | |
2677 | ||
7e1b7485 | 2678 | end: |
0f113f3e | 2679 | |
b548a1f1 | 2680 | OPENSSL_free(tmp); |
2ace7450 | 2681 | ASN1_GENERALIZEDTIME_free(comp_time); |
0f113f3e MC |
2682 | |
2683 | return ret; | |
2684 | } |