]>
Commit | Line | Data |
---|---|---|
58964a49 | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
d02b48c6 RE |
2 | * All rights reserved. |
3 | * | |
4 | * This package is an SSL implementation written | |
5 | * by Eric Young (eay@cryptsoft.com). | |
6 | * The implementation was written so as to conform with Netscapes SSL. | |
0f113f3e | 7 | * |
d02b48c6 RE |
8 | * This library is free for commercial and non-commercial use as long as |
9 | * the following conditions are aheared to. The following conditions | |
10 | * apply to all code found in this distribution, be it the RC4, RSA, | |
11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | |
12 | * included with this distribution is covered by the same copyright terms | |
13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | |
0f113f3e | 14 | * |
d02b48c6 RE |
15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
16 | * the code are not to be removed. | |
17 | * If this package is used in a product, Eric Young should be given attribution | |
18 | * as the author of the parts of the library used. | |
19 | * This can be in the form of a textual message at program startup or | |
20 | * in documentation (online or textual) provided with the package. | |
0f113f3e | 21 | * |
d02b48c6 RE |
22 | * Redistribution and use in source and binary forms, with or without |
23 | * modification, are permitted provided that the following conditions | |
24 | * are met: | |
25 | * 1. Redistributions of source code must retain the copyright | |
26 | * notice, this list of conditions and the following disclaimer. | |
27 | * 2. Redistributions in binary form must reproduce the above copyright | |
28 | * notice, this list of conditions and the following disclaimer in the | |
29 | * documentation and/or other materials provided with the distribution. | |
30 | * 3. All advertising materials mentioning features or use of this software | |
31 | * must display the following acknowledgement: | |
32 | * "This product includes cryptographic software written by | |
33 | * Eric Young (eay@cryptsoft.com)" | |
34 | * The word 'cryptographic' can be left out if the rouines from the library | |
35 | * being used are not cryptographic related :-). | |
0f113f3e | 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
d02b48c6 RE |
37 | * the apps directory (application code) you must include an acknowledgement: |
38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | |
0f113f3e | 39 | * |
d02b48c6 RE |
40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
50 | * SUCH DAMAGE. | |
0f113f3e | 51 | * |
d02b48c6 RE |
52 | * The licence and distribution terms for any publically available version or |
53 | * derivative of this code cannot be changed. i.e. this code cannot simply be | |
54 | * copied and put under another distribution licence | |
55 | * [including the GNU Public Licence.] | |
56 | */ | |
57 | ||
58 | /* The PPKI stuff has been donated by Jeff Barber <jeffb@issl.atl.hp.com> */ | |
59 | ||
60 | #include <stdio.h> | |
61 | #include <stdlib.h> | |
62 | #include <string.h> | |
c67cdb50 | 63 | #include <ctype.h> |
d02b48c6 | 64 | #include <sys/types.h> |
ec577822 BM |
65 | #include <openssl/conf.h> |
66 | #include <openssl/bio.h> | |
67 | #include <openssl/err.h> | |
68 | #include <openssl/bn.h> | |
69 | #include <openssl/txt_db.h> | |
70 | #include <openssl/evp.h> | |
71 | #include <openssl/x509.h> | |
72 | #include <openssl/x509v3.h> | |
73 | #include <openssl/objects.h> | |
a6b7ffdd | 74 | #include <openssl/ocsp.h> |
ec577822 | 75 | #include <openssl/pem.h> |
d02b48c6 RE |
76 | |
77 | #ifndef W_OK | |
0f113f3e MC |
78 | # ifdef OPENSSL_SYS_VMS |
79 | # if defined(__DECC) | |
80 | # include <unistd.h> | |
81 | # else | |
82 | # include <unixlib.h> | |
7d7d2cbc | 83 | # endif |
0f113f3e MC |
84 | # elif !defined(OPENSSL_SYS_VXWORKS) && !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_NETWARE) |
85 | # include <sys/file.h> | |
86 | # endif | |
7d7d2cbc UM |
87 | #endif |
88 | ||
d610d27f RL |
89 | #include "apps.h" |
90 | ||
7d7d2cbc | 91 | #ifndef W_OK |
0f113f3e MC |
92 | # define F_OK 0 |
93 | # define X_OK 1 | |
94 | # define W_OK 2 | |
95 | # define R_OK 4 | |
d02b48c6 RE |
96 | #endif |
97 | ||
7e1b7485 RS |
98 | #undef BSIZE |
99 | #define BSIZE 256 | |
d02b48c6 | 100 | |
0f113f3e | 101 | #define BASE_SECTION "ca" |
d5a2ea4b | 102 | #define CONFIG_FILE "openssl.cnf" |
d02b48c6 | 103 | |
0f113f3e MC |
104 | #define ENV_DEFAULT_CA "default_ca" |
105 | ||
106 | #define STRING_MASK "string_mask" | |
107 | #define UTF8_IN "utf8" | |
108 | ||
109 | #define ENV_DIR "dir" | |
110 | #define ENV_CERTS "certs" | |
111 | #define ENV_CRL_DIR "crl_dir" | |
112 | #define ENV_CA_DB "CA_DB" | |
113 | #define ENV_NEW_CERTS_DIR "new_certs_dir" | |
7e1b7485 | 114 | #define ENV_CERTIFICATE "certificate" |
0f113f3e MC |
115 | #define ENV_SERIAL "serial" |
116 | #define ENV_CRLNUMBER "crlnumber" | |
117 | #define ENV_CRL "crl" | |
118 | #define ENV_PRIVATE_KEY "private_key" | |
119 | #define ENV_RANDFILE "RANDFILE" | |
120 | #define ENV_DEFAULT_DAYS "default_days" | |
121 | #define ENV_DEFAULT_STARTDATE "default_startdate" | |
122 | #define ENV_DEFAULT_ENDDATE "default_enddate" | |
123 | #define ENV_DEFAULT_CRL_DAYS "default_crl_days" | |
124 | #define ENV_DEFAULT_CRL_HOURS "default_crl_hours" | |
125 | #define ENV_DEFAULT_MD "default_md" | |
126 | #define ENV_DEFAULT_EMAIL_DN "email_in_dn" | |
127 | #define ENV_PRESERVE "preserve" | |
128 | #define ENV_POLICY "policy" | |
129 | #define ENV_EXTENSIONS "x509_extensions" | |
130 | #define ENV_CRLEXT "crl_extensions" | |
131 | #define ENV_MSIE_HACK "msie_hack" | |
132 | #define ENV_NAMEOPT "name_opt" | |
133 | #define ENV_CERTOPT "cert_opt" | |
134 | #define ENV_EXTCOPY "copy_extensions" | |
135 | #define ENV_UNIQUE_SUBJECT "unique_subject" | |
136 | ||
137 | #define ENV_DATABASE "database" | |
d02b48c6 | 138 | |
a6b7ffdd DSH |
139 | /* Additional revocation information types */ |
140 | ||
0f113f3e MC |
141 | #define REV_NONE 0 /* No addditional information */ |
142 | #define REV_CRL_REASON 1 /* Value is CRL reason code */ | |
143 | #define REV_HOLD 2 /* Value is hold instruction */ | |
144 | #define REV_KEY_COMPROMISE 3 /* Value is cert key compromise time */ | |
145 | #define REV_CA_COMPROMISE 4 /* Value is CA key compromise time */ | |
146 | ||
d02b48c6 RE |
147 | #ifdef EFENCE |
148 | extern int EF_PROTECT_FREE; | |
149 | extern int EF_PROTECT_BELOW; | |
150 | extern int EF_ALIGNMENT; | |
151 | #endif | |
152 | ||
7d727231 | 153 | static void lookup_fail(const char *name, const char *tag); |
0f113f3e MC |
154 | static int certify(X509 **xret, char *infile, EVP_PKEY *pkey, X509 *x509, |
155 | const EVP_MD *dgst, STACK_OF(OPENSSL_STRING) *sigopts, | |
156 | STACK_OF(CONF_VALUE) *policy, CA_DB *db, | |
157 | BIGNUM *serial, char *subj, unsigned long chtype, | |
158 | int multirdn, int email_dn, char *startdate, char *enddate, | |
159 | long days, int batch, char *ext_sect, CONF *conf, | |
160 | int verbose, unsigned long certopt, unsigned long nameopt, | |
161 | int default_op, int ext_copy, int selfsign); | |
162 | static int certify_cert(X509 **xret, char *infile, EVP_PKEY *pkey, X509 *x509, | |
163 | const EVP_MD *dgst, STACK_OF(OPENSSL_STRING) *sigopts, | |
164 | STACK_OF(CONF_VALUE) *policy, CA_DB *db, | |
165 | BIGNUM *serial, char *subj, unsigned long chtype, | |
166 | int multirdn, int email_dn, char *startdate, | |
167 | char *enddate, long days, int batch, char *ext_sect, | |
168 | CONF *conf, int verbose, unsigned long certopt, | |
169 | unsigned long nameopt, int default_op, int ext_copy, | |
170 | ENGINE *e); | |
171 | static int certify_spkac(X509 **xret, char *infile, EVP_PKEY *pkey, | |
172 | X509 *x509, const EVP_MD *dgst, | |
173 | STACK_OF(OPENSSL_STRING) *sigopts, | |
174 | STACK_OF(CONF_VALUE) *policy, CA_DB *db, | |
175 | BIGNUM *serial, char *subj, unsigned long chtype, | |
176 | int multirdn, int email_dn, char *startdate, | |
177 | char *enddate, long days, char *ext_sect, CONF *conf, | |
178 | int verbose, unsigned long certopt, | |
179 | unsigned long nameopt, int default_op, int ext_copy); | |
180 | static void write_new_certificate(BIO *bp, X509 *x, int output_der, | |
181 | int notext); | |
182 | static int do_body(X509 **xret, EVP_PKEY *pkey, X509 *x509, | |
183 | const EVP_MD *dgst, STACK_OF(OPENSSL_STRING) *sigopts, | |
184 | STACK_OF(CONF_VALUE) *policy, CA_DB *db, BIGNUM *serial, | |
185 | char *subj, unsigned long chtype, int multirdn, | |
186 | int email_dn, char *startdate, char *enddate, long days, | |
187 | int batch, int verbose, X509_REQ *req, char *ext_sect, | |
188 | CONF *conf, unsigned long certopt, unsigned long nameopt, | |
189 | int default_op, int ext_copy, int selfsign); | |
f85b68cd RL |
190 | static int do_revoke(X509 *x509, CA_DB *db, int ext, char *extval); |
191 | static int get_certificate_status(const char *ser_status, CA_DB *db); | |
192 | static int do_updatedb(CA_DB *db); | |
7c337e00 | 193 | static int check_time_format(const char *str); |
a6b7ffdd | 194 | char *make_revocation_str(int rev_type, char *rev_arg); |
7d727231 | 195 | int make_revoked(X509_REVOKED *rev, const char *str); |
535d79da | 196 | int old_entry_print(BIO *bp, ASN1_OBJECT *obj, ASN1_STRING *str); |
7e1b7485 | 197 | |
0f113f3e MC |
198 | static CONF *conf = NULL; |
199 | static CONF *extconf = NULL; | |
200 | static char *section = NULL; | |
0f113f3e MC |
201 | static int preserve = 0; |
202 | static int msie_hack = 0; | |
35a99b63 | 203 | |
7e1b7485 RS |
204 | typedef enum OPTION_choice { |
205 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
206 | OPT_ENGINE, OPT_VERBOSE, OPT_CONFIG, OPT_NAME, OPT_SUBJ, OPT_UTF8, | |
207 | OPT_CREATE_SERIAL, OPT_MULTIVALUE_RDN, OPT_STARTDATE, OPT_ENDDATE, | |
208 | OPT_DAYS, OPT_MD, OPT_POLICY, OPT_KEYFILE, OPT_KEYFORM, OPT_PASSIN, | |
209 | OPT_KEY, OPT_CERT, OPT_SELFSIGN, OPT_IN, OPT_OUT, OPT_OUTDIR, | |
210 | OPT_SIGOPT, OPT_NOTEXT, OPT_BATCH, OPT_PRESERVEDN, OPT_NOEMAILDN, | |
211 | OPT_GENCRL, OPT_MSIE_HACK, OPT_CRLDAYS, OPT_CRLHOURS, OPT_CRLSEC, | |
212 | OPT_INFILES, OPT_SS_CERT, OPT_SPKAC, OPT_REVOKE, OPT_VALID, | |
213 | OPT_EXTENSIONS, OPT_EXTFILE, OPT_STATUS, OPT_UPDATEDB, OPT_CRLEXTS, | |
214 | OPT_CRL_REASON, OPT_CRL_HOLD, OPT_CRL_COMPROMISE, | |
215 | OPT_CRL_CA_COMPROMISE | |
216 | } OPTION_CHOICE; | |
217 | ||
218 | OPTIONS ca_options[] = { | |
219 | {"help", OPT_HELP, '-', "Display this summary"}, | |
220 | {"verbose", OPT_VERBOSE, '-', "Verbose output during processing"}, | |
221 | {"config", OPT_CONFIG, 's', "A config file"}, | |
222 | {"name", OPT_NAME, 's', "The particular CA definition to use"}, | |
223 | {"subj", OPT_SUBJ, 's', "Use arg instead of request's subject"}, | |
224 | {"utf8", OPT_UTF8, '-', "Input characters are UTF8 (default ASCII)"}, | |
225 | {"create_serial", OPT_CREATE_SERIAL, '-'}, | |
226 | {"multivalue-rdn", OPT_MULTIVALUE_RDN, '-', | |
227 | "Enable support for multivalued RDNs"}, | |
228 | {"startdate", OPT_STARTDATE, 's', "Cert notBefore, YYMMDDHHMMSSZ"}, | |
229 | {"enddate", OPT_ENDDATE, 's', | |
230 | "YYMMDDHHMMSSZ cert notAfter (overrides -days)"}, | |
231 | {"days", OPT_DAYS, 'p', "Number of days to certify the cert for"}, | |
232 | {"md", OPT_MD, 's', "md to use; one of md2, md5, sha or sha1"}, | |
233 | {"policy", OPT_POLICY, 's', "The CA 'policy' to support"}, | |
234 | {"keyfile", OPT_KEYFILE, '<', "Private key file"}, | |
235 | {"keyform", OPT_KEYFORM, 'f', "Private key file format (PEM or ENGINE)"}, | |
236 | {"passin", OPT_PASSIN, 's'}, | |
237 | {"key", OPT_KEY, 's', "Key to decode the private key if it is encrypted"}, | |
238 | {"cert", OPT_CERT, '<', "The CA cert"}, | |
239 | {"selfsign", OPT_SELFSIGN, '-', | |
240 | "Sign a cert with the key associated with it"}, | |
241 | {"in", OPT_IN, '<', "The input PEM encoded cert request(s)"}, | |
242 | {"out", OPT_OUT, '>', "Where to put the output file(s)"}, | |
243 | {"outdir", OPT_OUTDIR, '/', "Where to put output cert"}, | |
244 | {"sigopt", OPT_SIGOPT, 's'}, | |
245 | {"notext", OPT_NOTEXT, '-'}, | |
246 | {"batch", OPT_BATCH, '-', "Don't ask questions"}, | |
247 | {"preserveDN", OPT_PRESERVEDN, '-', "Don't re-order the DN"}, | |
248 | {"noemailDN", OPT_NOEMAILDN, '-', "Don't add the EMAIL field to the DN"}, | |
249 | {"gencrl", OPT_GENCRL, '-', "Generate a new CRL"}, | |
250 | {"msie_hack", OPT_MSIE_HACK, '-', | |
251 | "msie modifications to handle all those universal strings"}, | |
252 | {"crldays", OPT_CRLDAYS, 'p', "Days is when the next CRL is due"}, | |
253 | {"crlhours", OPT_CRLHOURS, 'p', "Hours is when the next CRL is due"}, | |
254 | {"crlsec", OPT_CRLSEC, 'p'}, | |
255 | {"infiles", OPT_INFILES, '-', "The last argument, requests to process"}, | |
256 | {"ss_cert", OPT_SS_CERT, '<', "File contains a self signed cert to sign"}, | |
257 | {"spkac", OPT_SPKAC, '<', | |
258 | "File contains DN and signed public key and challenge"}, | |
259 | {"revoke", OPT_REVOKE, '<', "Revoke a cert (given in file)"}, | |
260 | {"valid", OPT_VALID, 's'}, | |
261 | {"extensions", OPT_EXTENSIONS, 's', | |
262 | "Extension section (override value in config file)"}, | |
263 | {"extfile", OPT_EXTFILE, '<', | |
264 | "Configuration file with X509v3 extensions to add"}, | |
265 | {"status", OPT_STATUS, 's', "Shows cert status given the serial number"}, | |
266 | {"updatedb", OPT_UPDATEDB, '-', "Updates db for expired cert"}, | |
267 | {"crlexts", OPT_CRLEXTS, 's', | |
268 | "CRL extension section (override value in config file)"}, | |
269 | {"crl_reason", OPT_CRL_REASON, 's'}, | |
270 | {"crl_hold", OPT_CRL_HOLD, 's'}, | |
271 | {"crl_compromise", OPT_CRL_COMPROMISE, 's'}, | |
272 | {"crl_CA_compromise", OPT_CRL_CA_COMPROMISE, 's'}, | |
273 | #ifndef OPENSSL_NO_ENGINE | |
274 | {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, | |
275 | #endif | |
276 | {NULL} | |
277 | }; | |
667ac4ec | 278 | |
7e1b7485 | 279 | int ca_main(int argc, char **argv) |
0f113f3e MC |
280 | { |
281 | ENGINE *e = NULL; | |
7e1b7485 | 282 | BIGNUM *crlnumber = NULL, *serial = NULL; |
0f113f3e | 283 | EVP_PKEY *pkey = NULL; |
0f113f3e | 284 | BIO *in = NULL, *out = NULL, *Sout = NULL, *Cout = NULL; |
0f113f3e | 285 | ASN1_INTEGER *tmpser; |
7e1b7485 RS |
286 | ASN1_TIME *tmptm; |
287 | CA_DB *db = NULL; | |
288 | DB_ATTR db_attr; | |
0f113f3e | 289 | STACK_OF(CONF_VALUE) *attribs = NULL; |
0f113f3e | 290 | STACK_OF(OPENSSL_STRING) *sigopts = NULL; |
7e1b7485 RS |
291 | STACK_OF(X509) *cert_sk = NULL; |
292 | X509_CRL *crl = NULL; | |
293 | const EVP_MD *dgst = NULL; | |
294 | char *configfile = NULL, *md = NULL, *policy = NULL, *keyfile = NULL; | |
295 | char *certfile = NULL, *crl_ext = NULL, *crlnumberfile = NULL, *enddate = | |
296 | NULL; | |
297 | char *infile = NULL, *spkac_file = NULL, *ss_cert_file = NULL; | |
298 | char *extensions = NULL, *extfile = NULL, *key = NULL, *passinarg = NULL; | |
299 | char *outdir = NULL, *outfile = NULL, *rev_arg = NULL, *ser_status = NULL; | |
300 | char *serialfile = NULL, *startdate = NULL, *subj = NULL, *tmp_email_dn = | |
301 | NULL; | |
302 | char *prog; | |
303 | char *const *pp; | |
304 | char *dbfile = NULL, *engine = NULL, *f, *randfile = NULL, *tofree = NULL; | |
68b00c23 | 305 | char buf[3][BSIZE]; |
7e1b7485 RS |
306 | const char *p; |
307 | int create_ser = 0, free_key = 0, total = 0, total_done = 0; | |
308 | int batch = 0, default_op = 1, doupdatedb = 0, ext_copy = EXT_COPY_NONE; | |
309 | int keyformat = FORMAT_PEM, multirdn = 0, notext = 0, output_der = 0; | |
310 | int ret = 1, email_dn = 1, req = 0, verbose = 0, gencrl = 0, dorevoke = 0; | |
311 | int i, j, rev_type = REV_NONE, selfsign = 0; | |
312 | long crldays = 0, crlhours = 0, crlsec = 0, errorline = -1, days = 0; | |
313 | unsigned long chtype = MBSTRING_ASC, nameopt = 0, certopt = 0; | |
314 | X509 *x509 = NULL, *x509p = NULL, *x = NULL; | |
315 | X509_REVOKED *r = NULL; | |
316 | OPTION_CHOICE o; | |
d02b48c6 RE |
317 | |
318 | #ifdef EFENCE | |
0f113f3e MC |
319 | EF_PROTECT_FREE = 1; |
320 | EF_PROTECT_BELOW = 1; | |
321 | EF_ALIGNMENT = 0; | |
d02b48c6 RE |
322 | #endif |
323 | ||
0f113f3e | 324 | conf = NULL; |
0f113f3e | 325 | section = NULL; |
0f113f3e MC |
326 | preserve = 0; |
327 | msie_hack = 0; | |
7e1b7485 RS |
328 | |
329 | prog = opt_init(argc, argv, ca_options); | |
330 | while ((o = opt_next()) != OPT_EOF) { | |
331 | switch (o) { | |
332 | case OPT_EOF: | |
333 | case OPT_ERR: | |
334 | opthelp: | |
335 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
336 | goto end; | |
337 | case OPT_HELP: | |
338 | opt_help(ca_options); | |
339 | ret = 0; | |
340 | goto end; | |
341 | case OPT_IN: | |
342 | infile = opt_arg(); | |
343 | break; | |
344 | case OPT_OUT: | |
345 | outfile = opt_arg(); | |
346 | break; | |
347 | case OPT_VERBOSE: | |
348 | verbose = 1; | |
349 | break; | |
350 | case OPT_CONFIG: | |
351 | configfile = opt_arg(); | |
352 | break; | |
353 | case OPT_NAME: | |
354 | section = opt_arg(); | |
355 | break; | |
356 | case OPT_SUBJ: | |
357 | subj = opt_arg(); | |
358 | /* preserve=1; */ | |
359 | break; | |
360 | case OPT_UTF8: | |
361 | chtype = MBSTRING_UTF8; | |
362 | break; | |
363 | case OPT_CREATE_SERIAL: | |
0f113f3e | 364 | create_ser = 1; |
7e1b7485 RS |
365 | break; |
366 | case OPT_MULTIVALUE_RDN: | |
0f113f3e | 367 | multirdn = 1; |
7e1b7485 RS |
368 | break; |
369 | case OPT_STARTDATE: | |
370 | startdate = opt_arg(); | |
371 | break; | |
372 | case OPT_ENDDATE: | |
373 | enddate = opt_arg(); | |
374 | break; | |
375 | case OPT_DAYS: | |
376 | days = atoi(opt_arg()); | |
377 | break; | |
378 | case OPT_MD: | |
379 | md = opt_arg(); | |
380 | break; | |
381 | case OPT_POLICY: | |
382 | policy = opt_arg(); | |
383 | break; | |
384 | case OPT_KEYFILE: | |
385 | keyfile = opt_arg(); | |
386 | break; | |
387 | case OPT_KEYFORM: | |
388 | if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyformat)) | |
389 | goto opthelp; | |
390 | break; | |
391 | case OPT_PASSIN: | |
392 | passinarg = opt_arg(); | |
393 | break; | |
394 | case OPT_KEY: | |
395 | key = opt_arg(); | |
396 | break; | |
397 | case OPT_CERT: | |
398 | certfile = opt_arg(); | |
399 | break; | |
400 | case OPT_SELFSIGN: | |
0f113f3e | 401 | selfsign = 1; |
7e1b7485 RS |
402 | break; |
403 | case OPT_OUTDIR: | |
404 | outdir = opt_arg(); | |
405 | break; | |
406 | case OPT_SIGOPT: | |
407 | if (sigopts == NULL) | |
0f113f3e | 408 | sigopts = sk_OPENSSL_STRING_new_null(); |
7e1b7485 RS |
409 | if (sigopts == NULL |
410 | || !sk_OPENSSL_STRING_push(sigopts, opt_arg())) | |
411 | goto end; | |
412 | break; | |
413 | case OPT_NOTEXT: | |
0f113f3e | 414 | notext = 1; |
7e1b7485 RS |
415 | break; |
416 | case OPT_BATCH: | |
0f113f3e | 417 | batch = 1; |
7e1b7485 RS |
418 | break; |
419 | case OPT_PRESERVEDN: | |
0f113f3e | 420 | preserve = 1; |
7e1b7485 RS |
421 | break; |
422 | case OPT_NOEMAILDN: | |
0f113f3e | 423 | email_dn = 0; |
7e1b7485 RS |
424 | break; |
425 | case OPT_GENCRL: | |
0f113f3e | 426 | gencrl = 1; |
7e1b7485 RS |
427 | break; |
428 | case OPT_MSIE_HACK: | |
0f113f3e | 429 | msie_hack = 1; |
0f113f3e | 430 | break; |
7e1b7485 RS |
431 | case OPT_CRLDAYS: |
432 | crldays = atol(opt_arg()); | |
433 | break; | |
434 | case OPT_CRLHOURS: | |
435 | crlhours = atol(opt_arg()); | |
436 | break; | |
437 | case OPT_CRLSEC: | |
438 | crlsec = atol(opt_arg()); | |
439 | break; | |
440 | case OPT_INFILES: | |
0f113f3e | 441 | req = 1; |
7e1b7485 RS |
442 | goto end_of_options; |
443 | case OPT_SS_CERT: | |
444 | ss_cert_file = opt_arg(); | |
0f113f3e | 445 | req = 1; |
7e1b7485 RS |
446 | break; |
447 | case OPT_SPKAC: | |
448 | spkac_file = opt_arg(); | |
449 | req = 1; | |
450 | break; | |
451 | case OPT_REVOKE: | |
452 | infile = opt_arg(); | |
0f113f3e | 453 | dorevoke = 1; |
7e1b7485 RS |
454 | break; |
455 | case OPT_VALID: | |
456 | infile = opt_arg(); | |
0f113f3e | 457 | dorevoke = 2; |
7e1b7485 RS |
458 | break; |
459 | case OPT_EXTENSIONS: | |
460 | extensions = opt_arg(); | |
461 | break; | |
462 | case OPT_EXTFILE: | |
463 | extfile = opt_arg(); | |
464 | break; | |
465 | case OPT_STATUS: | |
466 | ser_status = opt_arg(); | |
467 | break; | |
468 | case OPT_UPDATEDB: | |
0f113f3e | 469 | doupdatedb = 1; |
7e1b7485 RS |
470 | break; |
471 | case OPT_CRLEXTS: | |
472 | crl_ext = opt_arg(); | |
473 | break; | |
474 | case OPT_CRL_REASON: | |
475 | rev_arg = opt_arg(); | |
0f113f3e | 476 | rev_type = REV_CRL_REASON; |
7e1b7485 RS |
477 | break; |
478 | case OPT_CRL_HOLD: | |
479 | rev_arg = opt_arg(); | |
0f113f3e | 480 | rev_type = REV_HOLD; |
7e1b7485 RS |
481 | break; |
482 | case OPT_CRL_COMPROMISE: | |
483 | rev_arg = opt_arg(); | |
0f113f3e | 484 | rev_type = REV_KEY_COMPROMISE; |
7e1b7485 RS |
485 | break; |
486 | case OPT_CRL_CA_COMPROMISE: | |
487 | rev_arg = opt_arg(); | |
0f113f3e | 488 | rev_type = REV_CA_COMPROMISE; |
7e1b7485 RS |
489 | break; |
490 | case OPT_ENGINE: | |
491 | engine = opt_arg(); | |
0f113f3e MC |
492 | break; |
493 | } | |
0f113f3e | 494 | } |
7e1b7485 RS |
495 | end_of_options: |
496 | argc = opt_num_rest(); | |
497 | argv = opt_rest(); | |
0f113f3e | 498 | |
0f113f3e MC |
499 | tofree = NULL; |
500 | if (configfile == NULL) | |
501 | configfile = getenv("OPENSSL_CONF"); | |
502 | if (configfile == NULL) | |
503 | configfile = getenv("SSLEAY_CONF"); | |
504 | if (configfile == NULL) { | |
505 | const char *s = X509_get_default_cert_area(); | |
506 | size_t len; | |
54a656ef | 507 | |
bc36ee62 | 508 | #ifdef OPENSSL_SYS_VMS |
0f113f3e MC |
509 | len = strlen(s) + sizeof(CONFIG_FILE); |
510 | tofree = OPENSSL_malloc(len); | |
61986d32 | 511 | if (!tofree) { |
918bb865 | 512 | BIO_printf(bio_err, "Out of memory\n"); |
7e1b7485 | 513 | goto end; |
918bb865 | 514 | } |
0f113f3e | 515 | strcpy(tofree, s); |
7d7d2cbc | 516 | #else |
0f113f3e MC |
517 | len = strlen(s) + sizeof(CONFIG_FILE) + 1; |
518 | tofree = OPENSSL_malloc(len); | |
61986d32 | 519 | if (!tofree) { |
918bb865 | 520 | BIO_printf(bio_err, "Out of memory\n"); |
7e1b7485 | 521 | goto end; |
918bb865 | 522 | } |
0f113f3e MC |
523 | BUF_strlcpy(tofree, s, len); |
524 | BUF_strlcat(tofree, "/", len); | |
7d7d2cbc | 525 | #endif |
0f113f3e MC |
526 | BUF_strlcat(tofree, CONFIG_FILE, len); |
527 | configfile = tofree; | |
528 | } | |
529 | ||
530 | BIO_printf(bio_err, "Using configuration from %s\n", configfile); | |
531 | conf = NCONF_new(NULL); | |
532 | if (NCONF_load(conf, configfile, &errorline) <= 0) { | |
533 | if (errorline <= 0) | |
534 | BIO_printf(bio_err, "error loading the config file '%s'\n", | |
535 | configfile); | |
536 | else | |
537 | BIO_printf(bio_err, "error on line %ld of config file '%s'\n", | |
538 | errorline, configfile); | |
7e1b7485 | 539 | goto end; |
0f113f3e MC |
540 | } |
541 | if (tofree) { | |
542 | OPENSSL_free(tofree); | |
543 | tofree = NULL; | |
544 | } | |
b5a93e22 | 545 | #ifndef OPENSSL_NO_ENGINE |
7e1b7485 | 546 | e = setup_engine(engine, 0); |
b5a93e22 DSH |
547 | #endif |
548 | ||
0f113f3e MC |
549 | /* Lets get the config section we are using */ |
550 | if (section == NULL) { | |
551 | section = NCONF_get_string(conf, BASE_SECTION, ENV_DEFAULT_CA); | |
552 | if (section == NULL) { | |
553 | lookup_fail(BASE_SECTION, ENV_DEFAULT_CA); | |
7e1b7485 | 554 | goto end; |
0f113f3e MC |
555 | } |
556 | } | |
557 | ||
558 | if (conf != NULL) { | |
559 | p = NCONF_get_string(conf, NULL, "oid_file"); | |
560 | if (p == NULL) | |
561 | ERR_clear_error(); | |
562 | if (p != NULL) { | |
563 | BIO *oid_bio; | |
564 | ||
565 | oid_bio = BIO_new_file(p, "r"); | |
566 | if (oid_bio == NULL) { | |
50e735f9 MC |
567 | /*- |
568 | BIO_printf(bio_err,"problems opening %s for extra oid's\n",p); | |
569 | ERR_print_errors(bio_err); | |
570 | */ | |
0f113f3e MC |
571 | ERR_clear_error(); |
572 | } else { | |
573 | OBJ_create_objects(oid_bio); | |
574 | BIO_free(oid_bio); | |
575 | } | |
576 | } | |
7e1b7485 | 577 | if (!add_oid_section(conf)) { |
0f113f3e | 578 | ERR_print_errors(bio_err); |
7e1b7485 | 579 | goto end; |
0f113f3e MC |
580 | } |
581 | } | |
582 | ||
583 | randfile = NCONF_get_string(conf, BASE_SECTION, "RANDFILE"); | |
584 | if (randfile == NULL) | |
585 | ERR_clear_error(); | |
7e1b7485 | 586 | app_RAND_load_file(randfile, 0); |
0f113f3e MC |
587 | |
588 | f = NCONF_get_string(conf, section, STRING_MASK); | |
589 | if (!f) | |
590 | ERR_clear_error(); | |
591 | ||
592 | if (f && !ASN1_STRING_set_default_mask_asc(f)) { | |
593 | BIO_printf(bio_err, "Invalid global string mask setting %s\n", f); | |
7e1b7485 | 594 | goto end; |
0f113f3e MC |
595 | } |
596 | ||
597 | if (chtype != MBSTRING_UTF8) { | |
598 | f = NCONF_get_string(conf, section, UTF8_IN); | |
599 | if (!f) | |
600 | ERR_clear_error(); | |
601 | else if (!strcmp(f, "yes")) | |
602 | chtype = MBSTRING_UTF8; | |
603 | } | |
604 | ||
605 | db_attr.unique_subject = 1; | |
606 | p = NCONF_get_string(conf, section, ENV_UNIQUE_SUBJECT); | |
607 | if (p) { | |
0f113f3e MC |
608 | db_attr.unique_subject = parse_yesno(p, 1); |
609 | } else | |
610 | ERR_clear_error(); | |
0f113f3e MC |
611 | |
612 | /*****************************************************************/ | |
613 | /* report status of cert with serial number given on command line */ | |
614 | if (ser_status) { | |
615 | if ((dbfile = NCONF_get_string(conf, section, ENV_DATABASE)) == NULL) { | |
616 | lookup_fail(section, ENV_DATABASE); | |
7e1b7485 | 617 | goto end; |
0f113f3e MC |
618 | } |
619 | db = load_index(dbfile, &db_attr); | |
620 | if (db == NULL) | |
7e1b7485 | 621 | goto end; |
0f113f3e MC |
622 | |
623 | if (!index_index(db)) | |
7e1b7485 | 624 | goto end; |
0f113f3e MC |
625 | |
626 | if (get_certificate_status(ser_status, db) != 1) | |
627 | BIO_printf(bio_err, "Error verifying serial %s!\n", ser_status); | |
7e1b7485 | 628 | goto end; |
0f113f3e MC |
629 | } |
630 | ||
631 | /*****************************************************************/ | |
632 | /* we definitely need a private key, so let's get it */ | |
633 | ||
634 | if ((keyfile == NULL) && ((keyfile = NCONF_get_string(conf, | |
635 | section, | |
636 | ENV_PRIVATE_KEY)) == | |
637 | NULL)) { | |
638 | lookup_fail(section, ENV_PRIVATE_KEY); | |
7e1b7485 | 639 | goto end; |
0f113f3e MC |
640 | } |
641 | if (!key) { | |
642 | free_key = 1; | |
7e1b7485 | 643 | if (!app_passwd(passinarg, NULL, &key, NULL)) { |
0f113f3e | 644 | BIO_printf(bio_err, "Error getting password\n"); |
7e1b7485 | 645 | goto end; |
0f113f3e MC |
646 | } |
647 | } | |
7e1b7485 | 648 | pkey = load_key(keyfile, keyformat, 0, key, e, "CA private key"); |
0f113f3e MC |
649 | if (key) |
650 | OPENSSL_cleanse(key, strlen(key)); | |
651 | if (pkey == NULL) { | |
652 | /* load_key() has already printed an appropriate message */ | |
7e1b7485 | 653 | goto end; |
0f113f3e MC |
654 | } |
655 | ||
656 | /*****************************************************************/ | |
657 | /* we need a certificate */ | |
658 | if (!selfsign || spkac_file || ss_cert_file || gencrl) { | |
659 | if ((certfile == NULL) | |
660 | && ((certfile = NCONF_get_string(conf, | |
661 | section, | |
662 | ENV_CERTIFICATE)) == NULL)) { | |
663 | lookup_fail(section, ENV_CERTIFICATE); | |
7e1b7485 | 664 | goto end; |
0f113f3e | 665 | } |
7e1b7485 | 666 | x509 = load_cert(certfile, FORMAT_PEM, NULL, e, "CA certificate"); |
0f113f3e | 667 | if (x509 == NULL) |
7e1b7485 | 668 | goto end; |
0f113f3e MC |
669 | |
670 | if (!X509_check_private_key(x509, pkey)) { | |
671 | BIO_printf(bio_err, | |
672 | "CA certificate and CA private key do not match\n"); | |
7e1b7485 | 673 | goto end; |
0f113f3e MC |
674 | } |
675 | } | |
676 | if (!selfsign) | |
677 | x509p = x509; | |
678 | ||
679 | f = NCONF_get_string(conf, BASE_SECTION, ENV_PRESERVE); | |
680 | if (f == NULL) | |
681 | ERR_clear_error(); | |
682 | if ((f != NULL) && ((*f == 'y') || (*f == 'Y'))) | |
683 | preserve = 1; | |
684 | f = NCONF_get_string(conf, BASE_SECTION, ENV_MSIE_HACK); | |
685 | if (f == NULL) | |
686 | ERR_clear_error(); | |
687 | if ((f != NULL) && ((*f == 'y') || (*f == 'Y'))) | |
688 | msie_hack = 1; | |
689 | ||
690 | f = NCONF_get_string(conf, section, ENV_NAMEOPT); | |
691 | ||
692 | if (f) { | |
693 | if (!set_name_ex(&nameopt, f)) { | |
694 | BIO_printf(bio_err, "Invalid name options: \"%s\"\n", f); | |
7e1b7485 | 695 | goto end; |
0f113f3e MC |
696 | } |
697 | default_op = 0; | |
698 | } else | |
699 | ERR_clear_error(); | |
700 | ||
701 | f = NCONF_get_string(conf, section, ENV_CERTOPT); | |
702 | ||
703 | if (f) { | |
704 | if (!set_cert_ex(&certopt, f)) { | |
705 | BIO_printf(bio_err, "Invalid certificate options: \"%s\"\n", f); | |
7e1b7485 | 706 | goto end; |
0f113f3e MC |
707 | } |
708 | default_op = 0; | |
709 | } else | |
710 | ERR_clear_error(); | |
711 | ||
712 | f = NCONF_get_string(conf, section, ENV_EXTCOPY); | |
713 | ||
714 | if (f) { | |
715 | if (!set_ext_copy(&ext_copy, f)) { | |
716 | BIO_printf(bio_err, "Invalid extension copy option: \"%s\"\n", f); | |
7e1b7485 | 717 | goto end; |
0f113f3e MC |
718 | } |
719 | } else | |
720 | ERR_clear_error(); | |
721 | ||
722 | /*****************************************************************/ | |
723 | /* lookup where to write new certificates */ | |
724 | if ((outdir == NULL) && (req)) { | |
725 | ||
726 | if ((outdir = NCONF_get_string(conf, section, ENV_NEW_CERTS_DIR)) | |
727 | == NULL) { | |
728 | BIO_printf(bio_err, | |
729 | "there needs to be defined a directory for new certificate to be placed in\n"); | |
7e1b7485 | 730 | goto end; |
0f113f3e | 731 | } |
bc36ee62 | 732 | #ifndef OPENSSL_SYS_VMS |
0f113f3e MC |
733 | /* |
734 | * outdir is a directory spec, but access() for VMS demands a | |
735 | * filename. In any case, stat(), below, will catch the problem if | |
736 | * outdir is not a directory spec, and the fopen() or open() will | |
737 | * catch an error if there is no write access. | |
738 | * | |
739 | * Presumably, this problem could also be solved by using the DEC C | |
740 | * routines to convert the directory syntax to Unixly, and give that | |
741 | * to access(). However, time's too short to do that just now. | |
742 | */ | |
7e1b7485 | 743 | if (app_access(outdir, R_OK | W_OK | X_OK) != 0) |
0f113f3e MC |
744 | { |
745 | BIO_printf(bio_err, "I am unable to access the %s directory\n", | |
746 | outdir); | |
747 | perror(outdir); | |
7e1b7485 | 748 | goto end; |
0f113f3e MC |
749 | } |
750 | ||
751 | if (app_isdir(outdir) <= 0) { | |
752 | BIO_printf(bio_err, "%s need to be a directory\n", outdir); | |
753 | perror(outdir); | |
7e1b7485 | 754 | goto end; |
0f113f3e | 755 | } |
79875776 | 756 | #endif |
0f113f3e MC |
757 | } |
758 | ||
759 | /*****************************************************************/ | |
760 | /* we need to load the database file */ | |
761 | if ((dbfile = NCONF_get_string(conf, section, ENV_DATABASE)) == NULL) { | |
762 | lookup_fail(section, ENV_DATABASE); | |
7e1b7485 | 763 | goto end; |
0f113f3e MC |
764 | } |
765 | db = load_index(dbfile, &db_attr); | |
766 | if (db == NULL) | |
7e1b7485 | 767 | goto end; |
0f113f3e MC |
768 | |
769 | /* Lets check some fields */ | |
770 | for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) { | |
771 | pp = sk_OPENSSL_PSTRING_value(db->db->data, i); | |
772 | if ((pp[DB_type][0] != DB_TYPE_REV) && (pp[DB_rev_date][0] != '\0')) { | |
773 | BIO_printf(bio_err, | |
774 | "entry %d: not revoked yet, but has a revocation date\n", | |
775 | i + 1); | |
7e1b7485 | 776 | goto end; |
0f113f3e MC |
777 | } |
778 | if ((pp[DB_type][0] == DB_TYPE_REV) && | |
779 | !make_revoked(NULL, pp[DB_rev_date])) { | |
780 | BIO_printf(bio_err, " in entry %d\n", i + 1); | |
7e1b7485 | 781 | goto end; |
0f113f3e MC |
782 | } |
783 | if (!check_time_format((char *)pp[DB_exp_date])) { | |
784 | BIO_printf(bio_err, "entry %d: invalid expiry date\n", i + 1); | |
7e1b7485 | 785 | goto end; |
0f113f3e MC |
786 | } |
787 | p = pp[DB_serial]; | |
788 | j = strlen(p); | |
789 | if (*p == '-') { | |
790 | p++; | |
791 | j--; | |
792 | } | |
793 | if ((j & 1) || (j < 2)) { | |
794 | BIO_printf(bio_err, "entry %d: bad serial number length (%d)\n", | |
795 | i + 1, j); | |
7e1b7485 | 796 | goto end; |
0f113f3e MC |
797 | } |
798 | while (*p) { | |
799 | if (!(((*p >= '0') && (*p <= '9')) || | |
800 | ((*p >= 'A') && (*p <= 'F')) || | |
801 | ((*p >= 'a') && (*p <= 'f')))) { | |
802 | BIO_printf(bio_err, | |
803 | "entry %d: bad serial number characters, char pos %ld, char is '%c'\n", | |
804 | i + 1, (long)(p - pp[DB_serial]), *p); | |
7e1b7485 | 805 | goto end; |
0f113f3e MC |
806 | } |
807 | p++; | |
808 | } | |
809 | } | |
810 | if (verbose) { | |
7e1b7485 | 811 | TXT_DB_write(bio_out, db->db); |
0f113f3e MC |
812 | BIO_printf(bio_err, "%d entries loaded from the database\n", |
813 | sk_OPENSSL_PSTRING_num(db->db->data)); | |
814 | BIO_printf(bio_err, "generating index\n"); | |
815 | } | |
816 | ||
817 | if (!index_index(db)) | |
7e1b7485 | 818 | goto end; |
0f113f3e MC |
819 | |
820 | /*****************************************************************/ | |
821 | /* Update the db file for expired certificates */ | |
822 | if (doupdatedb) { | |
823 | if (verbose) | |
824 | BIO_printf(bio_err, "Updating %s ...\n", dbfile); | |
825 | ||
826 | i = do_updatedb(db); | |
827 | if (i == -1) { | |
828 | BIO_printf(bio_err, "Malloc failure\n"); | |
7e1b7485 | 829 | goto end; |
0f113f3e MC |
830 | } else if (i == 0) { |
831 | if (verbose) | |
832 | BIO_printf(bio_err, "No entries found to mark expired\n"); | |
833 | } else { | |
834 | if (!save_index(dbfile, "new", db)) | |
7e1b7485 | 835 | goto end; |
0f113f3e MC |
836 | |
837 | if (!rotate_index(dbfile, "new", "old")) | |
7e1b7485 | 838 | goto end; |
0f113f3e MC |
839 | |
840 | if (verbose) | |
841 | BIO_printf(bio_err, | |
842 | "Done. %d entries marked as expired\n", i); | |
843 | } | |
844 | } | |
845 | ||
846 | /*****************************************************************/ | |
847 | /* Read extensions config file */ | |
848 | if (extfile) { | |
849 | extconf = NCONF_new(NULL); | |
850 | if (NCONF_load(extconf, extfile, &errorline) <= 0) { | |
851 | if (errorline <= 0) | |
852 | BIO_printf(bio_err, "ERROR: loading the config file '%s'\n", | |
853 | extfile); | |
854 | else | |
855 | BIO_printf(bio_err, | |
856 | "ERROR: on line %ld of config file '%s'\n", | |
857 | errorline, extfile); | |
858 | ret = 1; | |
7e1b7485 | 859 | goto end; |
0f113f3e MC |
860 | } |
861 | ||
862 | if (verbose) | |
863 | BIO_printf(bio_err, "Successfully loaded extensions file %s\n", | |
864 | extfile); | |
865 | ||
866 | /* We can have sections in the ext file */ | |
867 | if (!extensions | |
868 | && !(extensions = | |
869 | NCONF_get_string(extconf, "default", "extensions"))) | |
870 | extensions = "default"; | |
871 | } | |
872 | ||
873 | /*****************************************************************/ | |
874 | if (req || gencrl) { | |
7e1b7485 RS |
875 | Sout = bio_open_default(outfile, "w"); |
876 | if (Sout == NULL) | |
877 | goto end; | |
0f113f3e MC |
878 | } |
879 | ||
880 | if ((md == NULL) && ((md = NCONF_get_string(conf, | |
881 | section, | |
882 | ENV_DEFAULT_MD)) == NULL)) { | |
883 | lookup_fail(section, ENV_DEFAULT_MD); | |
7e1b7485 | 884 | goto end; |
0f113f3e MC |
885 | } |
886 | ||
887 | if (!strcmp(md, "default")) { | |
888 | int def_nid; | |
889 | if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) <= 0) { | |
890 | BIO_puts(bio_err, "no default digest\n"); | |
7e1b7485 | 891 | goto end; |
0f113f3e MC |
892 | } |
893 | md = (char *)OBJ_nid2sn(def_nid); | |
894 | } | |
895 | ||
7e1b7485 RS |
896 | if (!opt_md(md, &dgst)) { |
897 | goto end; | |
0f113f3e MC |
898 | } |
899 | ||
900 | if (req) { | |
901 | if ((email_dn == 1) && ((tmp_email_dn = NCONF_get_string(conf, | |
902 | section, | |
903 | ENV_DEFAULT_EMAIL_DN)) | |
904 | != NULL)) { | |
905 | if (strcmp(tmp_email_dn, "no") == 0) | |
906 | email_dn = 0; | |
907 | } | |
908 | if (verbose) | |
909 | BIO_printf(bio_err, "message digest is %s\n", | |
910 | OBJ_nid2ln(dgst->type)); | |
911 | if ((policy == NULL) && ((policy = NCONF_get_string(conf, | |
912 | section, | |
913 | ENV_POLICY)) == | |
914 | NULL)) { | |
915 | lookup_fail(section, ENV_POLICY); | |
7e1b7485 | 916 | goto end; |
0f113f3e MC |
917 | } |
918 | if (verbose) | |
919 | BIO_printf(bio_err, "policy is %s\n", policy); | |
920 | ||
921 | if ((serialfile = NCONF_get_string(conf, section, ENV_SERIAL)) | |
922 | == NULL) { | |
923 | lookup_fail(section, ENV_SERIAL); | |
7e1b7485 | 924 | goto end; |
0f113f3e MC |
925 | } |
926 | ||
927 | if (!extconf) { | |
928 | /* | |
929 | * no '-extfile' option, so we look for extensions in the main | |
930 | * configuration file | |
931 | */ | |
932 | if (!extensions) { | |
933 | extensions = NCONF_get_string(conf, section, ENV_EXTENSIONS); | |
934 | if (!extensions) | |
935 | ERR_clear_error(); | |
936 | } | |
937 | if (extensions) { | |
938 | /* Check syntax of file */ | |
939 | X509V3_CTX ctx; | |
940 | X509V3_set_ctx_test(&ctx); | |
941 | X509V3_set_nconf(&ctx, conf); | |
942 | if (!X509V3_EXT_add_nconf(conf, &ctx, extensions, NULL)) { | |
943 | BIO_printf(bio_err, | |
944 | "Error Loading extension section %s\n", | |
945 | extensions); | |
946 | ret = 1; | |
7e1b7485 | 947 | goto end; |
0f113f3e MC |
948 | } |
949 | } | |
950 | } | |
951 | ||
952 | if (startdate == NULL) { | |
953 | startdate = NCONF_get_string(conf, section, | |
954 | ENV_DEFAULT_STARTDATE); | |
955 | if (startdate == NULL) | |
956 | ERR_clear_error(); | |
957 | } | |
958 | if (startdate && !ASN1_TIME_set_string(NULL, startdate)) { | |
959 | BIO_printf(bio_err, | |
960 | "start date is invalid, it should be YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ\n"); | |
7e1b7485 | 961 | goto end; |
0f113f3e MC |
962 | } |
963 | if (startdate == NULL) | |
964 | startdate = "today"; | |
965 | ||
966 | if (enddate == NULL) { | |
967 | enddate = NCONF_get_string(conf, section, ENV_DEFAULT_ENDDATE); | |
968 | if (enddate == NULL) | |
969 | ERR_clear_error(); | |
970 | } | |
971 | if (enddate && !ASN1_TIME_set_string(NULL, enddate)) { | |
972 | BIO_printf(bio_err, | |
973 | "end date is invalid, it should be YYMMDDHHMMSSZ or YYYYMMDDHHMMSSZ\n"); | |
7e1b7485 | 974 | goto end; |
0f113f3e MC |
975 | } |
976 | ||
977 | if (days == 0) { | |
978 | if (!NCONF_get_number(conf, section, ENV_DEFAULT_DAYS, &days)) | |
979 | days = 0; | |
980 | } | |
981 | if (!enddate && (days == 0)) { | |
982 | BIO_printf(bio_err, | |
983 | "cannot lookup how many days to certify for\n"); | |
7e1b7485 | 984 | goto end; |
0f113f3e MC |
985 | } |
986 | ||
987 | if ((serial = load_serial(serialfile, create_ser, NULL)) == NULL) { | |
988 | BIO_printf(bio_err, "error while loading serial number\n"); | |
7e1b7485 | 989 | goto end; |
0f113f3e MC |
990 | } |
991 | if (verbose) { | |
992 | if (BN_is_zero(serial)) | |
993 | BIO_printf(bio_err, "next serial number is 00\n"); | |
994 | else { | |
995 | if ((f = BN_bn2hex(serial)) == NULL) | |
7e1b7485 | 996 | goto end; |
0f113f3e MC |
997 | BIO_printf(bio_err, "next serial number is %s\n", f); |
998 | OPENSSL_free(f); | |
999 | } | |
1000 | } | |
1001 | ||
1002 | if ((attribs = NCONF_get_section(conf, policy)) == NULL) { | |
1003 | BIO_printf(bio_err, "unable to find 'section' for %s\n", policy); | |
7e1b7485 | 1004 | goto end; |
0f113f3e MC |
1005 | } |
1006 | ||
1007 | if ((cert_sk = sk_X509_new_null()) == NULL) { | |
1008 | BIO_printf(bio_err, "Memory allocation failure\n"); | |
7e1b7485 | 1009 | goto end; |
0f113f3e MC |
1010 | } |
1011 | if (spkac_file != NULL) { | |
1012 | total++; | |
1013 | j = certify_spkac(&x, spkac_file, pkey, x509, dgst, sigopts, | |
1014 | attribs, db, serial, subj, chtype, multirdn, | |
1015 | email_dn, startdate, enddate, days, extensions, | |
1016 | conf, verbose, certopt, nameopt, default_op, | |
1017 | ext_copy); | |
1018 | if (j < 0) | |
7e1b7485 | 1019 | goto end; |
0f113f3e MC |
1020 | if (j > 0) { |
1021 | total_done++; | |
1022 | BIO_printf(bio_err, "\n"); | |
1023 | if (!BN_add_word(serial, 1)) | |
7e1b7485 | 1024 | goto end; |
0f113f3e MC |
1025 | if (!sk_X509_push(cert_sk, x)) { |
1026 | BIO_printf(bio_err, "Memory allocation failure\n"); | |
7e1b7485 | 1027 | goto end; |
0f113f3e MC |
1028 | } |
1029 | if (outfile) { | |
1030 | output_der = 1; | |
1031 | batch = 1; | |
1032 | } | |
1033 | } | |
1034 | } | |
1035 | if (ss_cert_file != NULL) { | |
1036 | total++; | |
1037 | j = certify_cert(&x, ss_cert_file, pkey, x509, dgst, sigopts, | |
1038 | attribs, | |
1039 | db, serial, subj, chtype, multirdn, email_dn, | |
1040 | startdate, enddate, days, batch, extensions, | |
1041 | conf, verbose, certopt, nameopt, default_op, | |
1042 | ext_copy, e); | |
1043 | if (j < 0) | |
7e1b7485 | 1044 | goto end; |
0f113f3e MC |
1045 | if (j > 0) { |
1046 | total_done++; | |
1047 | BIO_printf(bio_err, "\n"); | |
1048 | if (!BN_add_word(serial, 1)) | |
7e1b7485 | 1049 | goto end; |
0f113f3e MC |
1050 | if (!sk_X509_push(cert_sk, x)) { |
1051 | BIO_printf(bio_err, "Memory allocation failure\n"); | |
7e1b7485 | 1052 | goto end; |
0f113f3e MC |
1053 | } |
1054 | } | |
1055 | } | |
1056 | if (infile != NULL) { | |
1057 | total++; | |
1058 | j = certify(&x, infile, pkey, x509p, dgst, sigopts, attribs, db, | |
1059 | serial, subj, chtype, multirdn, email_dn, startdate, | |
1060 | enddate, days, batch, extensions, conf, verbose, | |
1061 | certopt, nameopt, default_op, ext_copy, selfsign); | |
1062 | if (j < 0) | |
7e1b7485 | 1063 | goto end; |
0f113f3e MC |
1064 | if (j > 0) { |
1065 | total_done++; | |
1066 | BIO_printf(bio_err, "\n"); | |
1067 | if (!BN_add_word(serial, 1)) | |
7e1b7485 | 1068 | goto end; |
0f113f3e MC |
1069 | if (!sk_X509_push(cert_sk, x)) { |
1070 | BIO_printf(bio_err, "Memory allocation failure\n"); | |
7e1b7485 | 1071 | goto end; |
0f113f3e MC |
1072 | } |
1073 | } | |
1074 | } | |
1075 | for (i = 0; i < argc; i++) { | |
1076 | total++; | |
1077 | j = certify(&x, argv[i], pkey, x509p, dgst, sigopts, attribs, db, | |
1078 | serial, subj, chtype, multirdn, email_dn, startdate, | |
1079 | enddate, days, batch, extensions, conf, verbose, | |
1080 | certopt, nameopt, default_op, ext_copy, selfsign); | |
1081 | if (j < 0) | |
7e1b7485 | 1082 | goto end; |
0f113f3e MC |
1083 | if (j > 0) { |
1084 | total_done++; | |
1085 | BIO_printf(bio_err, "\n"); | |
1086 | if (!BN_add_word(serial, 1)) | |
7e1b7485 | 1087 | goto end; |
0f113f3e MC |
1088 | if (!sk_X509_push(cert_sk, x)) { |
1089 | BIO_printf(bio_err, "Memory allocation failure\n"); | |
7e1b7485 | 1090 | goto end; |
0f113f3e MC |
1091 | } |
1092 | } | |
1093 | } | |
1094 | /* | |
1095 | * we have a stack of newly certified certificates and a data base | |
1096 | * and serial number that need updating | |
1097 | */ | |
1098 | ||
1099 | if (sk_X509_num(cert_sk) > 0) { | |
1100 | if (!batch) { | |
1101 | BIO_printf(bio_err, | |
1102 | "\n%d out of %d certificate requests certified, commit? [y/n]", | |
1103 | total_done, total); | |
1104 | (void)BIO_flush(bio_err); | |
1105 | buf[0][0] = '\0'; | |
1106 | if (!fgets(buf[0], 10, stdin)) { | |
1107 | BIO_printf(bio_err, | |
1108 | "CERTIFICATION CANCELED: I/O error\n"); | |
1109 | ret = 0; | |
7e1b7485 | 1110 | goto end; |
0f113f3e MC |
1111 | } |
1112 | if ((buf[0][0] != 'y') && (buf[0][0] != 'Y')) { | |
1113 | BIO_printf(bio_err, "CERTIFICATION CANCELED\n"); | |
1114 | ret = 0; | |
7e1b7485 | 1115 | goto end; |
0f113f3e MC |
1116 | } |
1117 | } | |
1118 | ||
1119 | BIO_printf(bio_err, "Write out database with %d new entries\n", | |
1120 | sk_X509_num(cert_sk)); | |
1121 | ||
1122 | if (!save_serial(serialfile, "new", serial, NULL)) | |
7e1b7485 | 1123 | goto end; |
0f113f3e MC |
1124 | |
1125 | if (!save_index(dbfile, "new", db)) | |
7e1b7485 | 1126 | goto end; |
0f113f3e MC |
1127 | } |
1128 | ||
1129 | if (verbose) | |
1130 | BIO_printf(bio_err, "writing new certificates\n"); | |
1131 | for (i = 0; i < sk_X509_num(cert_sk); i++) { | |
1132 | int k; | |
1133 | char *n; | |
1134 | ||
1135 | x = sk_X509_value(cert_sk, i); | |
1136 | ||
1137 | j = x->cert_info->serialNumber->length; | |
1138 | p = (const char *)x->cert_info->serialNumber->data; | |
1139 | ||
1140 | if (strlen(outdir) >= (size_t)(j ? BSIZE - j * 2 - 6 : BSIZE - 8)) { | |
1141 | BIO_printf(bio_err, "certificate file name too long\n"); | |
7e1b7485 | 1142 | goto end; |
0f113f3e MC |
1143 | } |
1144 | ||
1145 | strcpy(buf[2], outdir); | |
7d7d2cbc | 1146 | |
bc36ee62 | 1147 | #ifndef OPENSSL_SYS_VMS |
0f113f3e | 1148 | BUF_strlcat(buf[2], "/", sizeof(buf[2])); |
7d7d2cbc UM |
1149 | #endif |
1150 | ||
0f113f3e MC |
1151 | n = (char *)&(buf[2][strlen(buf[2])]); |
1152 | if (j > 0) { | |
1153 | for (k = 0; k < j; k++) { | |
1154 | if (n >= &(buf[2][sizeof(buf[2])])) | |
1155 | break; | |
1156 | BIO_snprintf(n, | |
1157 | &buf[2][0] + sizeof(buf[2]) - n, | |
1158 | "%02X", (unsigned char)*(p++)); | |
1159 | n += 2; | |
1160 | } | |
1161 | } else { | |
1162 | *(n++) = '0'; | |
1163 | *(n++) = '0'; | |
1164 | } | |
1165 | *(n++) = '.'; | |
1166 | *(n++) = 'p'; | |
1167 | *(n++) = 'e'; | |
1168 | *(n++) = 'm'; | |
1169 | *n = '\0'; | |
1170 | if (verbose) | |
1171 | BIO_printf(bio_err, "writing %s\n", buf[2]); | |
1172 | ||
7e1b7485 RS |
1173 | Cout = BIO_new_file(buf[2], "w"); |
1174 | if (Cout == NULL) { | |
0f113f3e | 1175 | perror(buf[2]); |
7e1b7485 | 1176 | goto end; |
0f113f3e MC |
1177 | } |
1178 | write_new_certificate(Cout, x, 0, notext); | |
1179 | write_new_certificate(Sout, x, output_der, notext); | |
1180 | } | |
1181 | ||
1182 | if (sk_X509_num(cert_sk)) { | |
1183 | /* Rename the database and the serial file */ | |
1184 | if (!rotate_serial(serialfile, "new", "old")) | |
7e1b7485 | 1185 | goto end; |
0f113f3e MC |
1186 | |
1187 | if (!rotate_index(dbfile, "new", "old")) | |
7e1b7485 | 1188 | goto end; |
0f113f3e MC |
1189 | |
1190 | BIO_printf(bio_err, "Data Base Updated\n"); | |
1191 | } | |
1192 | } | |
1193 | ||
1194 | /*****************************************************************/ | |
1195 | if (gencrl) { | |
1196 | int crl_v2 = 0; | |
1197 | if (!crl_ext) { | |
1198 | crl_ext = NCONF_get_string(conf, section, ENV_CRLEXT); | |
1199 | if (!crl_ext) | |
1200 | ERR_clear_error(); | |
1201 | } | |
1202 | if (crl_ext) { | |
1203 | /* Check syntax of file */ | |
1204 | X509V3_CTX ctx; | |
1205 | X509V3_set_ctx_test(&ctx); | |
1206 | X509V3_set_nconf(&ctx, conf); | |
1207 | if (!X509V3_EXT_add_nconf(conf, &ctx, crl_ext, NULL)) { | |
1208 | BIO_printf(bio_err, | |
1209 | "Error Loading CRL extension section %s\n", | |
1210 | crl_ext); | |
1211 | ret = 1; | |
7e1b7485 | 1212 | goto end; |
0f113f3e MC |
1213 | } |
1214 | } | |
1215 | ||
1216 | if ((crlnumberfile = NCONF_get_string(conf, section, ENV_CRLNUMBER)) | |
1217 | != NULL) | |
1218 | if ((crlnumber = load_serial(crlnumberfile, 0, NULL)) == NULL) { | |
1219 | BIO_printf(bio_err, "error while loading CRL number\n"); | |
7e1b7485 | 1220 | goto end; |
0f113f3e MC |
1221 | } |
1222 | ||
1223 | if (!crldays && !crlhours && !crlsec) { | |
1224 | if (!NCONF_get_number(conf, section, | |
1225 | ENV_DEFAULT_CRL_DAYS, &crldays)) | |
1226 | crldays = 0; | |
1227 | if (!NCONF_get_number(conf, section, | |
1228 | ENV_DEFAULT_CRL_HOURS, &crlhours)) | |
1229 | crlhours = 0; | |
1230 | ERR_clear_error(); | |
1231 | } | |
1232 | if ((crldays == 0) && (crlhours == 0) && (crlsec == 0)) { | |
1233 | BIO_printf(bio_err, | |
1234 | "cannot lookup how long until the next CRL is issued\n"); | |
7e1b7485 | 1235 | goto end; |
0f113f3e MC |
1236 | } |
1237 | ||
1238 | if (verbose) | |
1239 | BIO_printf(bio_err, "making CRL\n"); | |
1240 | if ((crl = X509_CRL_new()) == NULL) | |
7e1b7485 | 1241 | goto end; |
0f113f3e | 1242 | if (!X509_CRL_set_issuer_name(crl, X509_get_subject_name(x509))) |
7e1b7485 | 1243 | goto end; |
0f113f3e MC |
1244 | |
1245 | tmptm = ASN1_TIME_new(); | |
1246 | if (!tmptm) | |
7e1b7485 | 1247 | goto end; |
0f113f3e MC |
1248 | X509_gmtime_adj(tmptm, 0); |
1249 | X509_CRL_set_lastUpdate(crl, tmptm); | |
1250 | if (!X509_time_adj_ex(tmptm, crldays, crlhours * 60 * 60 + crlsec, | |
1251 | NULL)) { | |
1252 | BIO_puts(bio_err, "error setting CRL nextUpdate\n"); | |
7e1b7485 | 1253 | goto end; |
0f113f3e MC |
1254 | } |
1255 | X509_CRL_set_nextUpdate(crl, tmptm); | |
1256 | ||
1257 | ASN1_TIME_free(tmptm); | |
1258 | ||
1259 | for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) { | |
1260 | pp = sk_OPENSSL_PSTRING_value(db->db->data, i); | |
1261 | if (pp[DB_type][0] == DB_TYPE_REV) { | |
1262 | if ((r = X509_REVOKED_new()) == NULL) | |
7e1b7485 | 1263 | goto end; |
0f113f3e MC |
1264 | j = make_revoked(r, pp[DB_rev_date]); |
1265 | if (!j) | |
7e1b7485 | 1266 | goto end; |
0f113f3e MC |
1267 | if (j == 2) |
1268 | crl_v2 = 1; | |
1269 | if (!BN_hex2bn(&serial, pp[DB_serial])) | |
7e1b7485 | 1270 | goto end; |
0f113f3e MC |
1271 | tmpser = BN_to_ASN1_INTEGER(serial, NULL); |
1272 | BN_free(serial); | |
1273 | serial = NULL; | |
1274 | if (!tmpser) | |
7e1b7485 | 1275 | goto end; |
0f113f3e MC |
1276 | X509_REVOKED_set_serialNumber(r, tmpser); |
1277 | ASN1_INTEGER_free(tmpser); | |
1278 | X509_CRL_add0_revoked(crl, r); | |
1279 | } | |
1280 | } | |
1281 | ||
1282 | /* | |
1283 | * sort the data so it will be written in serial number order | |
1284 | */ | |
1285 | X509_CRL_sort(crl); | |
1286 | ||
1287 | /* we now have a CRL */ | |
1288 | if (verbose) | |
1289 | BIO_printf(bio_err, "signing CRL\n"); | |
1290 | ||
1291 | /* Add any extensions asked for */ | |
1292 | ||
1293 | if (crl_ext || crlnumberfile != NULL) { | |
1294 | X509V3_CTX crlctx; | |
1295 | X509V3_set_ctx(&crlctx, x509, NULL, NULL, crl, 0); | |
1296 | X509V3_set_nconf(&crlctx, conf); | |
1297 | ||
1298 | if (crl_ext) | |
1299 | if (!X509V3_EXT_CRL_add_nconf(conf, &crlctx, crl_ext, crl)) | |
7e1b7485 | 1300 | goto end; |
0f113f3e MC |
1301 | if (crlnumberfile != NULL) { |
1302 | tmpser = BN_to_ASN1_INTEGER(crlnumber, NULL); | |
1303 | if (!tmpser) | |
7e1b7485 | 1304 | goto end; |
0f113f3e MC |
1305 | X509_CRL_add1_ext_i2d(crl, NID_crl_number, tmpser, 0, 0); |
1306 | ASN1_INTEGER_free(tmpser); | |
1307 | crl_v2 = 1; | |
1308 | if (!BN_add_word(crlnumber, 1)) | |
7e1b7485 | 1309 | goto end; |
0f113f3e MC |
1310 | } |
1311 | } | |
1312 | if (crl_ext || crl_v2) { | |
1313 | if (!X509_CRL_set_version(crl, 1)) | |
7e1b7485 | 1314 | goto end; /* version 2 CRL */ |
0f113f3e MC |
1315 | } |
1316 | ||
1317 | /* we have a CRL number that need updating */ | |
1318 | if (crlnumberfile != NULL) | |
1319 | if (!save_serial(crlnumberfile, "new", crlnumber, NULL)) | |
7e1b7485 | 1320 | goto end; |
0f113f3e MC |
1321 | |
1322 | if (crlnumber) { | |
1323 | BN_free(crlnumber); | |
1324 | crlnumber = NULL; | |
1325 | } | |
1326 | ||
7e1b7485 RS |
1327 | if (!do_X509_CRL_sign(crl, pkey, dgst, sigopts)) |
1328 | goto end; | |
0f113f3e MC |
1329 | |
1330 | PEM_write_bio_X509_CRL(Sout, crl); | |
1331 | ||
1332 | if (crlnumberfile != NULL) /* Rename the crlnumber file */ | |
1333 | if (!rotate_serial(crlnumberfile, "new", "old")) | |
7e1b7485 | 1334 | goto end; |
0f113f3e MC |
1335 | |
1336 | } | |
1337 | /*****************************************************************/ | |
1338 | if (dorevoke) { | |
1339 | if (infile == NULL) { | |
1340 | BIO_printf(bio_err, "no input files\n"); | |
7e1b7485 | 1341 | goto end; |
0f113f3e MC |
1342 | } else { |
1343 | X509 *revcert; | |
7e1b7485 | 1344 | revcert = load_cert(infile, FORMAT_PEM, NULL, e, infile); |
0f113f3e | 1345 | if (revcert == NULL) |
7e1b7485 | 1346 | goto end; |
0f113f3e MC |
1347 | if (dorevoke == 2) |
1348 | rev_type = -1; | |
1349 | j = do_revoke(revcert, db, rev_type, rev_arg); | |
1350 | if (j <= 0) | |
7e1b7485 | 1351 | goto end; |
0f113f3e MC |
1352 | X509_free(revcert); |
1353 | ||
1354 | if (!save_index(dbfile, "new", db)) | |
7e1b7485 | 1355 | goto end; |
0f113f3e MC |
1356 | |
1357 | if (!rotate_index(dbfile, "new", "old")) | |
7e1b7485 | 1358 | goto end; |
0f113f3e MC |
1359 | |
1360 | BIO_printf(bio_err, "Data Base Updated\n"); | |
1361 | } | |
1362 | } | |
1363 | /*****************************************************************/ | |
1364 | ret = 0; | |
7e1b7485 | 1365 | end: |
0f113f3e MC |
1366 | if (tofree) |
1367 | OPENSSL_free(tofree); | |
1368 | BIO_free_all(Cout); | |
1369 | BIO_free_all(Sout); | |
1370 | BIO_free_all(out); | |
1371 | BIO_free_all(in); | |
1372 | ||
1373 | if (cert_sk) | |
1374 | sk_X509_pop_free(cert_sk, X509_free); | |
1375 | ||
1376 | if (ret) | |
1377 | ERR_print_errors(bio_err); | |
7e1b7485 | 1378 | app_RAND_write_file(randfile); |
0f113f3e MC |
1379 | if (free_key && key) |
1380 | OPENSSL_free(key); | |
1381 | BN_free(serial); | |
1382 | BN_free(crlnumber); | |
1383 | free_index(db); | |
1384 | if (sigopts) | |
1385 | sk_OPENSSL_STRING_free(sigopts); | |
1386 | EVP_PKEY_free(pkey); | |
1387 | if (x509) | |
1388 | X509_free(x509); | |
1389 | X509_CRL_free(crl); | |
1390 | NCONF_free(conf); | |
1391 | NCONF_free(extconf); | |
1392 | OBJ_cleanup(); | |
7e1b7485 | 1393 | return (ret); |
0f113f3e | 1394 | } |
d02b48c6 | 1395 | |
7d727231 | 1396 | static void lookup_fail(const char *name, const char *tag) |
0f113f3e MC |
1397 | { |
1398 | BIO_printf(bio_err, "variable lookup failed for %s::%s\n", name, tag); | |
1399 | } | |
d02b48c6 | 1400 | |
6b691a5c | 1401 | static int certify(X509 **xret, char *infile, EVP_PKEY *pkey, X509 *x509, |
0f113f3e MC |
1402 | const EVP_MD *dgst, STACK_OF(OPENSSL_STRING) *sigopts, |
1403 | STACK_OF(CONF_VALUE) *policy, CA_DB *db, | |
1404 | BIGNUM *serial, char *subj, unsigned long chtype, | |
1405 | int multirdn, int email_dn, char *startdate, char *enddate, | |
1406 | long days, int batch, char *ext_sect, CONF *lconf, | |
1407 | int verbose, unsigned long certopt, unsigned long nameopt, | |
1408 | int default_op, int ext_copy, int selfsign) | |
1409 | { | |
1410 | X509_REQ *req = NULL; | |
1411 | BIO *in = NULL; | |
1412 | EVP_PKEY *pktmp = NULL; | |
1413 | int ok = -1, i; | |
1414 | ||
7e1b7485 RS |
1415 | in = BIO_new_file(infile, "r"); |
1416 | if (in == NULL) { | |
1417 | ERR_print_errors(bio_err); | |
1418 | goto end; | |
0f113f3e MC |
1419 | } |
1420 | if ((req = PEM_read_bio_X509_REQ(in, NULL, NULL, NULL)) == NULL) { | |
1421 | BIO_printf(bio_err, "Error reading certificate request in %s\n", | |
1422 | infile); | |
7e1b7485 | 1423 | goto end; |
0f113f3e MC |
1424 | } |
1425 | if (verbose) | |
1426 | X509_REQ_print(bio_err, req); | |
1427 | ||
1428 | BIO_printf(bio_err, "Check that the request matches the signature\n"); | |
1429 | ||
1430 | if (selfsign && !X509_REQ_check_private_key(req, pkey)) { | |
1431 | BIO_printf(bio_err, | |
1432 | "Certificate request and CA private key do not match\n"); | |
1433 | ok = 0; | |
7e1b7485 | 1434 | goto end; |
0f113f3e MC |
1435 | } |
1436 | if ((pktmp = X509_REQ_get_pubkey(req)) == NULL) { | |
1437 | BIO_printf(bio_err, "error unpacking public key\n"); | |
7e1b7485 | 1438 | goto end; |
0f113f3e MC |
1439 | } |
1440 | i = X509_REQ_verify(req, pktmp); | |
1441 | EVP_PKEY_free(pktmp); | |
1442 | if (i < 0) { | |
1443 | ok = 0; | |
1444 | BIO_printf(bio_err, "Signature verification problems....\n"); | |
1445 | ERR_print_errors(bio_err); | |
7e1b7485 | 1446 | goto end; |
0f113f3e MC |
1447 | } |
1448 | if (i == 0) { | |
1449 | ok = 0; | |
1450 | BIO_printf(bio_err, | |
1451 | "Signature did not match the certificate request\n"); | |
1452 | ERR_print_errors(bio_err); | |
7e1b7485 | 1453 | goto end; |
0f113f3e MC |
1454 | } else |
1455 | BIO_printf(bio_err, "Signature ok\n"); | |
1456 | ||
1457 | ok = do_body(xret, pkey, x509, dgst, sigopts, policy, db, serial, subj, | |
1458 | chtype, multirdn, email_dn, startdate, enddate, days, batch, | |
1459 | verbose, req, ext_sect, lconf, certopt, nameopt, default_op, | |
1460 | ext_copy, selfsign); | |
1461 | ||
7e1b7485 | 1462 | end: |
0f113f3e MC |
1463 | if (req != NULL) |
1464 | X509_REQ_free(req); | |
ca3a82c3 | 1465 | BIO_free(in); |
0f113f3e MC |
1466 | return (ok); |
1467 | } | |
d02b48c6 | 1468 | |
6b691a5c | 1469 | static int certify_cert(X509 **xret, char *infile, EVP_PKEY *pkey, X509 *x509, |
0f113f3e MC |
1470 | const EVP_MD *dgst, STACK_OF(OPENSSL_STRING) *sigopts, |
1471 | STACK_OF(CONF_VALUE) *policy, CA_DB *db, | |
1472 | BIGNUM *serial, char *subj, unsigned long chtype, | |
1473 | int multirdn, int email_dn, char *startdate, | |
1474 | char *enddate, long days, int batch, char *ext_sect, | |
1475 | CONF *lconf, int verbose, unsigned long certopt, | |
1476 | unsigned long nameopt, int default_op, int ext_copy, | |
1477 | ENGINE *e) | |
1478 | { | |
1479 | X509 *req = NULL; | |
1480 | X509_REQ *rreq = NULL; | |
1481 | EVP_PKEY *pktmp = NULL; | |
1482 | int ok = -1, i; | |
1483 | ||
7e1b7485 RS |
1484 | if ((req = load_cert(infile, FORMAT_PEM, NULL, e, infile)) == NULL) |
1485 | goto end; | |
0f113f3e MC |
1486 | if (verbose) |
1487 | X509_print(bio_err, req); | |
1488 | ||
1489 | BIO_printf(bio_err, "Check that the request matches the signature\n"); | |
1490 | ||
1491 | if ((pktmp = X509_get_pubkey(req)) == NULL) { | |
1492 | BIO_printf(bio_err, "error unpacking public key\n"); | |
7e1b7485 | 1493 | goto end; |
0f113f3e MC |
1494 | } |
1495 | i = X509_verify(req, pktmp); | |
1496 | EVP_PKEY_free(pktmp); | |
1497 | if (i < 0) { | |
1498 | ok = 0; | |
1499 | BIO_printf(bio_err, "Signature verification problems....\n"); | |
7e1b7485 | 1500 | goto end; |
0f113f3e MC |
1501 | } |
1502 | if (i == 0) { | |
1503 | ok = 0; | |
1504 | BIO_printf(bio_err, "Signature did not match the certificate\n"); | |
7e1b7485 | 1505 | goto end; |
0f113f3e MC |
1506 | } else |
1507 | BIO_printf(bio_err, "Signature ok\n"); | |
1508 | ||
1509 | if ((rreq = X509_to_X509_REQ(req, NULL, EVP_md5())) == NULL) | |
7e1b7485 | 1510 | goto end; |
0f113f3e MC |
1511 | |
1512 | ok = do_body(xret, pkey, x509, dgst, sigopts, policy, db, serial, subj, | |
1513 | chtype, multirdn, email_dn, startdate, enddate, days, batch, | |
1514 | verbose, rreq, ext_sect, lconf, certopt, nameopt, default_op, | |
1515 | ext_copy, 0); | |
1516 | ||
7e1b7485 | 1517 | end: |
0f113f3e MC |
1518 | if (rreq != NULL) |
1519 | X509_REQ_free(rreq); | |
1520 | if (req != NULL) | |
1521 | X509_free(req); | |
1522 | return (ok); | |
1523 | } | |
1524 | ||
1525 | static int do_body(X509 **xret, EVP_PKEY *pkey, X509 *x509, | |
1526 | const EVP_MD *dgst, STACK_OF(OPENSSL_STRING) *sigopts, | |
1527 | STACK_OF(CONF_VALUE) *policy, CA_DB *db, BIGNUM *serial, | |
1528 | char *subj, unsigned long chtype, int multirdn, | |
1529 | int email_dn, char *startdate, char *enddate, long days, | |
1530 | int batch, int verbose, X509_REQ *req, char *ext_sect, | |
1531 | CONF *lconf, unsigned long certopt, unsigned long nameopt, | |
1532 | int default_op, int ext_copy, int selfsign) | |
1533 | { | |
1534 | X509_NAME *name = NULL, *CAname = NULL, *subject = NULL, *dn_subject = | |
1535 | NULL; | |
1536 | ASN1_UTCTIME *tm, *tmptm; | |
1537 | ASN1_STRING *str, *str2; | |
1538 | ASN1_OBJECT *obj; | |
1539 | X509 *ret = NULL; | |
1540 | X509_CINF *ci; | |
1541 | X509_NAME_ENTRY *ne; | |
1542 | X509_NAME_ENTRY *tne, *push; | |
1543 | EVP_PKEY *pktmp; | |
1544 | int ok = -1, i, j, last, nid; | |
1545 | const char *p; | |
1546 | CONF_VALUE *cv; | |
1547 | OPENSSL_STRING row[DB_NUMBER]; | |
1548 | OPENSSL_STRING *irow = NULL; | |
1549 | OPENSSL_STRING *rrow = NULL; | |
1550 | char buf[25]; | |
1551 | ||
1552 | tmptm = ASN1_UTCTIME_new(); | |
1553 | if (tmptm == NULL) { | |
1554 | BIO_printf(bio_err, "malloc error\n"); | |
1555 | return (0); | |
1556 | } | |
1557 | ||
1558 | for (i = 0; i < DB_NUMBER; i++) | |
1559 | row[i] = NULL; | |
1560 | ||
1561 | if (subj) { | |
1562 | X509_NAME *n = parse_name(subj, chtype, multirdn); | |
1563 | ||
1564 | if (!n) { | |
1565 | ERR_print_errors(bio_err); | |
7e1b7485 | 1566 | goto end; |
0f113f3e MC |
1567 | } |
1568 | X509_REQ_set_subject_name(req, n); | |
1569 | req->req_info->enc.modified = 1; | |
1570 | X509_NAME_free(n); | |
1571 | } | |
1572 | ||
1573 | if (default_op) | |
1574 | BIO_printf(bio_err, | |
1575 | "The Subject's Distinguished Name is as follows\n"); | |
1576 | ||
1577 | name = X509_REQ_get_subject_name(req); | |
1578 | for (i = 0; i < X509_NAME_entry_count(name); i++) { | |
1579 | ne = X509_NAME_get_entry(name, i); | |
1580 | str = X509_NAME_ENTRY_get_data(ne); | |
1581 | obj = X509_NAME_ENTRY_get_object(ne); | |
1582 | ||
1583 | if (msie_hack) { | |
1584 | /* assume all type should be strings */ | |
2743e38c | 1585 | nid = OBJ_obj2nid(X509_NAME_ENTRY_get_object(ne)); |
0f113f3e MC |
1586 | |
1587 | if (str->type == V_ASN1_UNIVERSALSTRING) | |
1588 | ASN1_UNIVERSALSTRING_to_string(str); | |
1589 | ||
1590 | if ((str->type == V_ASN1_IA5STRING) && | |
1591 | (nid != NID_pkcs9_emailAddress)) | |
1592 | str->type = V_ASN1_T61STRING; | |
1593 | ||
1594 | if ((nid == NID_pkcs9_emailAddress) && | |
1595 | (str->type == V_ASN1_PRINTABLESTRING)) | |
1596 | str->type = V_ASN1_IA5STRING; | |
1597 | } | |
1598 | ||
1599 | /* If no EMAIL is wanted in the subject */ | |
1600 | if ((OBJ_obj2nid(obj) == NID_pkcs9_emailAddress) && (!email_dn)) | |
1601 | continue; | |
1602 | ||
1603 | /* check some things */ | |
1604 | if ((OBJ_obj2nid(obj) == NID_pkcs9_emailAddress) && | |
1605 | (str->type != V_ASN1_IA5STRING)) { | |
1606 | BIO_printf(bio_err, | |
1607 | "\nemailAddress type needs to be of type IA5STRING\n"); | |
7e1b7485 | 1608 | goto end; |
0f113f3e MC |
1609 | } |
1610 | if ((str->type != V_ASN1_BMPSTRING) | |
1611 | && (str->type != V_ASN1_UTF8STRING)) { | |
1612 | j = ASN1_PRINTABLE_type(str->data, str->length); | |
1613 | if (((j == V_ASN1_T61STRING) && | |
1614 | (str->type != V_ASN1_T61STRING)) || | |
1615 | ((j == V_ASN1_IA5STRING) && | |
1616 | (str->type == V_ASN1_PRINTABLESTRING))) { | |
1617 | BIO_printf(bio_err, | |
1618 | "\nThe string contains characters that are illegal for the ASN.1 type\n"); | |
7e1b7485 | 1619 | goto end; |
0f113f3e MC |
1620 | } |
1621 | } | |
1622 | ||
1623 | if (default_op) | |
1624 | old_entry_print(bio_err, obj, str); | |
1625 | } | |
1626 | ||
1627 | /* Ok, now we check the 'policy' stuff. */ | |
1628 | if ((subject = X509_NAME_new()) == NULL) { | |
1629 | BIO_printf(bio_err, "Memory allocation failure\n"); | |
7e1b7485 | 1630 | goto end; |
0f113f3e MC |
1631 | } |
1632 | ||
1633 | /* take a copy of the issuer name before we mess with it. */ | |
1634 | if (selfsign) | |
1635 | CAname = X509_NAME_dup(name); | |
1636 | else | |
1637 | CAname = X509_NAME_dup(x509->cert_info->subject); | |
1638 | if (CAname == NULL) | |
7e1b7485 | 1639 | goto end; |
0f113f3e MC |
1640 | str = str2 = NULL; |
1641 | ||
1642 | for (i = 0; i < sk_CONF_VALUE_num(policy); i++) { | |
1643 | cv = sk_CONF_VALUE_value(policy, i); /* get the object id */ | |
1644 | if ((j = OBJ_txt2nid(cv->name)) == NID_undef) { | |
1645 | BIO_printf(bio_err, | |
1646 | "%s:unknown object type in 'policy' configuration\n", | |
1647 | cv->name); | |
7e1b7485 | 1648 | goto end; |
0f113f3e MC |
1649 | } |
1650 | obj = OBJ_nid2obj(j); | |
1651 | ||
1652 | last = -1; | |
1653 | for (;;) { | |
1654 | /* lookup the object in the supplied name list */ | |
1655 | j = X509_NAME_get_index_by_OBJ(name, obj, last); | |
1656 | if (j < 0) { | |
1657 | if (last != -1) | |
1658 | break; | |
1659 | tne = NULL; | |
1660 | } else { | |
1661 | tne = X509_NAME_get_entry(name, j); | |
1662 | } | |
1663 | last = j; | |
1664 | ||
1665 | /* depending on the 'policy', decide what to do. */ | |
1666 | push = NULL; | |
1667 | if (strcmp(cv->value, "optional") == 0) { | |
1668 | if (tne != NULL) | |
1669 | push = tne; | |
1670 | } else if (strcmp(cv->value, "supplied") == 0) { | |
1671 | if (tne == NULL) { | |
1672 | BIO_printf(bio_err, | |
1673 | "The %s field needed to be supplied and was missing\n", | |
1674 | cv->name); | |
7e1b7485 | 1675 | goto end; |
0f113f3e MC |
1676 | } else |
1677 | push = tne; | |
1678 | } else if (strcmp(cv->value, "match") == 0) { | |
1679 | int last2; | |
1680 | ||
1681 | if (tne == NULL) { | |
1682 | BIO_printf(bio_err, | |
1683 | "The mandatory %s field was missing\n", | |
1684 | cv->name); | |
7e1b7485 | 1685 | goto end; |
0f113f3e MC |
1686 | } |
1687 | ||
1688 | last2 = -1; | |
1689 | ||
1690 | again2: | |
1691 | j = X509_NAME_get_index_by_OBJ(CAname, obj, last2); | |
1692 | if ((j < 0) && (last2 == -1)) { | |
1693 | BIO_printf(bio_err, | |
1694 | "The %s field does not exist in the CA certificate,\nthe 'policy' is misconfigured\n", | |
1695 | cv->name); | |
7e1b7485 | 1696 | goto end; |
0f113f3e MC |
1697 | } |
1698 | if (j >= 0) { | |
1699 | push = X509_NAME_get_entry(CAname, j); | |
1700 | str = X509_NAME_ENTRY_get_data(tne); | |
1701 | str2 = X509_NAME_ENTRY_get_data(push); | |
1702 | last2 = j; | |
1703 | if (ASN1_STRING_cmp(str, str2) != 0) | |
1704 | goto again2; | |
1705 | } | |
1706 | if (j < 0) { | |
1707 | BIO_printf(bio_err, | |
1708 | "The %s field needed to be the same in the\nCA certificate (%s) and the request (%s)\n", | |
1709 | cv->name, | |
1710 | ((str2 == NULL) ? "NULL" : (char *)str2->data), | |
1711 | ((str == NULL) ? "NULL" : (char *)str->data)); | |
7e1b7485 | 1712 | goto end; |
0f113f3e MC |
1713 | } |
1714 | } else { | |
1715 | BIO_printf(bio_err, | |
1716 | "%s:invalid type in 'policy' configuration\n", | |
1717 | cv->value); | |
7e1b7485 | 1718 | goto end; |
0f113f3e MC |
1719 | } |
1720 | ||
1721 | if (push != NULL) { | |
1722 | if (!X509_NAME_add_entry(subject, push, -1, 0)) { | |
1723 | if (push != NULL) | |
1724 | X509_NAME_ENTRY_free(push); | |
1725 | BIO_printf(bio_err, "Memory allocation failure\n"); | |
7e1b7485 | 1726 | goto end; |
0f113f3e MC |
1727 | } |
1728 | } | |
1729 | if (j < 0) | |
1730 | break; | |
1731 | } | |
1732 | } | |
1733 | ||
1734 | if (preserve) { | |
1735 | X509_NAME_free(subject); | |
1736 | /* subject=X509_NAME_dup(X509_REQ_get_subject_name(req)); */ | |
1737 | subject = X509_NAME_dup(name); | |
1738 | if (subject == NULL) | |
7e1b7485 | 1739 | goto end; |
0f113f3e MC |
1740 | } |
1741 | ||
1742 | if (verbose) | |
1743 | BIO_printf(bio_err, | |
1744 | "The subject name appears to be ok, checking data base for clashes\n"); | |
1745 | ||
1746 | /* Build the correct Subject if no e-mail is wanted in the subject */ | |
1747 | /* | |
1748 | * and add it later on because of the method extensions are added | |
1749 | * (altName) | |
1750 | */ | |
1751 | ||
1752 | if (email_dn) | |
1753 | dn_subject = subject; | |
1754 | else { | |
1755 | X509_NAME_ENTRY *tmpne; | |
1756 | /* | |
1757 | * Its best to dup the subject DN and then delete any email addresses | |
1758 | * because this retains its structure. | |
1759 | */ | |
1760 | if (!(dn_subject = X509_NAME_dup(subject))) { | |
1761 | BIO_printf(bio_err, "Memory allocation failure\n"); | |
7e1b7485 | 1762 | goto end; |
0f113f3e MC |
1763 | } |
1764 | while ((i = X509_NAME_get_index_by_NID(dn_subject, | |
1765 | NID_pkcs9_emailAddress, | |
1766 | -1)) >= 0) { | |
1767 | tmpne = X509_NAME_get_entry(dn_subject, i); | |
1768 | X509_NAME_delete_entry(dn_subject, i); | |
1769 | X509_NAME_ENTRY_free(tmpne); | |
1770 | } | |
1771 | } | |
1772 | ||
1773 | if (BN_is_zero(serial)) | |
1774 | row[DB_serial] = BUF_strdup("00"); | |
1775 | else | |
1776 | row[DB_serial] = BN_bn2hex(serial); | |
1777 | if (row[DB_serial] == NULL) { | |
1778 | BIO_printf(bio_err, "Memory allocation failure\n"); | |
7e1b7485 | 1779 | goto end; |
0f113f3e MC |
1780 | } |
1781 | ||
1782 | if (db->attributes.unique_subject) { | |
1783 | OPENSSL_STRING *crow = row; | |
1784 | ||
1785 | rrow = TXT_DB_get_by_index(db->db, DB_name, crow); | |
1786 | if (rrow != NULL) { | |
1787 | BIO_printf(bio_err, | |
1788 | "ERROR:There is already a certificate for %s\n", | |
1789 | row[DB_name]); | |
1790 | } | |
1791 | } | |
1792 | if (rrow == NULL) { | |
1793 | rrow = TXT_DB_get_by_index(db->db, DB_serial, row); | |
1794 | if (rrow != NULL) { | |
1795 | BIO_printf(bio_err, | |
1796 | "ERROR:Serial number %s has already been issued,\n", | |
1797 | row[DB_serial]); | |
1798 | BIO_printf(bio_err, | |
1799 | " check the database/serial_file for corruption\n"); | |
1800 | } | |
1801 | } | |
1802 | ||
1803 | if (rrow != NULL) { | |
1804 | BIO_printf(bio_err, "The matching entry has the following details\n"); | |
1805 | if (rrow[DB_type][0] == 'E') | |
1806 | p = "Expired"; | |
1807 | else if (rrow[DB_type][0] == 'R') | |
1808 | p = "Revoked"; | |
1809 | else if (rrow[DB_type][0] == 'V') | |
1810 | p = "Valid"; | |
1811 | else | |
1812 | p = "\ninvalid type, Data base error\n"; | |
1813 | BIO_printf(bio_err, "Type :%s\n", p);; | |
1814 | if (rrow[DB_type][0] == 'R') { | |
1815 | p = rrow[DB_exp_date]; | |
1816 | if (p == NULL) | |
1817 | p = "undef"; | |
1818 | BIO_printf(bio_err, "Was revoked on:%s\n", p); | |
1819 | } | |
1820 | p = rrow[DB_exp_date]; | |
1821 | if (p == NULL) | |
1822 | p = "undef"; | |
1823 | BIO_printf(bio_err, "Expires on :%s\n", p); | |
1824 | p = rrow[DB_serial]; | |
1825 | if (p == NULL) | |
1826 | p = "undef"; | |
1827 | BIO_printf(bio_err, "Serial Number :%s\n", p); | |
1828 | p = rrow[DB_file]; | |
1829 | if (p == NULL) | |
1830 | p = "undef"; | |
1831 | BIO_printf(bio_err, "File name :%s\n", p); | |
1832 | p = rrow[DB_name]; | |
1833 | if (p == NULL) | |
1834 | p = "undef"; | |
1835 | BIO_printf(bio_err, "Subject Name :%s\n", p); | |
1836 | ok = -1; /* This is now a 'bad' error. */ | |
7e1b7485 | 1837 | goto end; |
0f113f3e MC |
1838 | } |
1839 | ||
1840 | /* We are now totally happy, lets make and sign the certificate */ | |
1841 | if (verbose) | |
1842 | BIO_printf(bio_err, | |
1843 | "Everything appears to be ok, creating and signing the certificate\n"); | |
1844 | ||
1845 | if ((ret = X509_new()) == NULL) | |
7e1b7485 | 1846 | goto end; |
0f113f3e | 1847 | ci = ret->cert_info; |
d02b48c6 RE |
1848 | |
1849 | #ifdef X509_V3 | |
0f113f3e MC |
1850 | /* Make it an X509 v3 certificate. */ |
1851 | if (!X509_set_version(ret, 2)) | |
7e1b7485 | 1852 | goto end; |
d02b48c6 RE |
1853 | #endif |
1854 | ||
0f113f3e | 1855 | if (BN_to_ASN1_INTEGER(serial, ci->serialNumber) == NULL) |
7e1b7485 | 1856 | goto end; |
0f113f3e MC |
1857 | if (selfsign) { |
1858 | if (!X509_set_issuer_name(ret, subject)) | |
7e1b7485 | 1859 | goto end; |
0f113f3e MC |
1860 | } else { |
1861 | if (!X509_set_issuer_name(ret, X509_get_subject_name(x509))) | |
7e1b7485 | 1862 | goto end; |
0f113f3e MC |
1863 | } |
1864 | ||
1865 | if (strcmp(startdate, "today") == 0) | |
1866 | X509_gmtime_adj(X509_get_notBefore(ret), 0); | |
1867 | else | |
1868 | ASN1_TIME_set_string(X509_get_notBefore(ret), startdate); | |
1869 | ||
1870 | if (enddate == NULL) | |
1871 | X509_time_adj_ex(X509_get_notAfter(ret), days, 0, NULL); | |
1872 | else { | |
1873 | int tdays; | |
1874 | ASN1_TIME_set_string(X509_get_notAfter(ret), enddate); | |
1875 | ASN1_TIME_diff(&tdays, NULL, NULL, X509_get_notAfter(ret)); | |
1876 | days = tdays; | |
1877 | } | |
1878 | ||
1879 | if (!X509_set_subject_name(ret, subject)) | |
7e1b7485 | 1880 | goto end; |
0f113f3e MC |
1881 | |
1882 | pktmp = X509_REQ_get_pubkey(req); | |
1883 | i = X509_set_pubkey(ret, pktmp); | |
1884 | EVP_PKEY_free(pktmp); | |
1885 | if (!i) | |
7e1b7485 | 1886 | goto end; |
0f113f3e MC |
1887 | |
1888 | /* Lets add the extensions, if there are any */ | |
1889 | if (ext_sect) { | |
1890 | X509V3_CTX ctx; | |
1891 | if (ci->version == NULL) | |
1892 | if ((ci->version = ASN1_INTEGER_new()) == NULL) | |
7e1b7485 | 1893 | goto end; |
0f113f3e MC |
1894 | ASN1_INTEGER_set(ci->version, 2); /* version 3 certificate */ |
1895 | ||
1896 | /* | |
1897 | * Free the current entries if any, there should not be any I believe | |
1898 | */ | |
1899 | if (ci->extensions != NULL) | |
1900 | sk_X509_EXTENSION_pop_free(ci->extensions, X509_EXTENSION_free); | |
1901 | ||
1902 | ci->extensions = NULL; | |
1903 | ||
1904 | /* Initialize the context structure */ | |
1905 | if (selfsign) | |
1906 | X509V3_set_ctx(&ctx, ret, ret, req, NULL, 0); | |
1907 | else | |
1908 | X509V3_set_ctx(&ctx, x509, ret, req, NULL, 0); | |
1909 | ||
1910 | if (extconf) { | |
1911 | if (verbose) | |
1912 | BIO_printf(bio_err, "Extra configuration file found\n"); | |
1913 | ||
1914 | /* Use the extconf configuration db LHASH */ | |
1915 | X509V3_set_nconf(&ctx, extconf); | |
1916 | ||
1917 | /* Test the structure (needed?) */ | |
1918 | /* X509V3_set_ctx_test(&ctx); */ | |
1919 | ||
1920 | /* Adds exts contained in the configuration file */ | |
1921 | if (!X509V3_EXT_add_nconf(extconf, &ctx, ext_sect, ret)) { | |
1922 | BIO_printf(bio_err, | |
1923 | "ERROR: adding extensions in section %s\n", | |
1924 | ext_sect); | |
1925 | ERR_print_errors(bio_err); | |
7e1b7485 | 1926 | goto end; |
0f113f3e MC |
1927 | } |
1928 | if (verbose) | |
1929 | BIO_printf(bio_err, | |
1930 | "Successfully added extensions from file.\n"); | |
1931 | } else if (ext_sect) { | |
1932 | /* We found extensions to be set from config file */ | |
1933 | X509V3_set_nconf(&ctx, lconf); | |
1934 | ||
1935 | if (!X509V3_EXT_add_nconf(lconf, &ctx, ext_sect, ret)) { | |
1936 | BIO_printf(bio_err, | |
1937 | "ERROR: adding extensions in section %s\n", | |
1938 | ext_sect); | |
1939 | ERR_print_errors(bio_err); | |
7e1b7485 | 1940 | goto end; |
0f113f3e MC |
1941 | } |
1942 | ||
1943 | if (verbose) | |
1944 | BIO_printf(bio_err, | |
1945 | "Successfully added extensions from config\n"); | |
1946 | } | |
1947 | } | |
1948 | ||
1949 | /* Copy extensions from request (if any) */ | |
1950 | ||
1951 | if (!copy_extensions(ret, req, ext_copy)) { | |
1952 | BIO_printf(bio_err, "ERROR: adding extensions from request\n"); | |
1953 | ERR_print_errors(bio_err); | |
7e1b7485 | 1954 | goto end; |
0f113f3e MC |
1955 | } |
1956 | ||
1957 | /* Set the right value for the noemailDN option */ | |
1958 | if (email_dn == 0) { | |
1959 | if (!X509_set_subject_name(ret, dn_subject)) | |
7e1b7485 | 1960 | goto end; |
0f113f3e MC |
1961 | } |
1962 | ||
1963 | if (!default_op) { | |
1964 | BIO_printf(bio_err, "Certificate Details:\n"); | |
1965 | /* | |
1966 | * Never print signature details because signature not present | |
1967 | */ | |
1968 | certopt |= X509_FLAG_NO_SIGDUMP | X509_FLAG_NO_SIGNAME; | |
1969 | X509_print_ex(bio_err, ret, nameopt, certopt); | |
1970 | } | |
1971 | ||
1972 | BIO_printf(bio_err, "Certificate is to be certified until "); | |
1973 | ASN1_TIME_print(bio_err, X509_get_notAfter(ret)); | |
1974 | if (days) | |
1975 | BIO_printf(bio_err, " (%ld days)", days); | |
1976 | BIO_printf(bio_err, "\n"); | |
1977 | ||
1978 | if (!batch) { | |
1979 | ||
1980 | BIO_printf(bio_err, "Sign the certificate? [y/n]:"); | |
1981 | (void)BIO_flush(bio_err); | |
1982 | buf[0] = '\0'; | |
1983 | if (!fgets(buf, sizeof(buf) - 1, stdin)) { | |
1984 | BIO_printf(bio_err, | |
1985 | "CERTIFICATE WILL NOT BE CERTIFIED: I/O error\n"); | |
1986 | ok = 0; | |
7e1b7485 | 1987 | goto end; |
0f113f3e MC |
1988 | } |
1989 | if (!((buf[0] == 'y') || (buf[0] == 'Y'))) { | |
1990 | BIO_printf(bio_err, "CERTIFICATE WILL NOT BE CERTIFIED\n"); | |
1991 | ok = 0; | |
7e1b7485 | 1992 | goto end; |
0f113f3e MC |
1993 | } |
1994 | } | |
1995 | ||
1996 | pktmp = X509_get_pubkey(ret); | |
1997 | if (EVP_PKEY_missing_parameters(pktmp) && | |
1998 | !EVP_PKEY_missing_parameters(pkey)) | |
1999 | EVP_PKEY_copy_parameters(pktmp, pkey); | |
2000 | EVP_PKEY_free(pktmp); | |
2001 | ||
7e1b7485 RS |
2002 | if (!do_X509_sign(ret, pkey, dgst, sigopts)) |
2003 | goto end; | |
0f113f3e MC |
2004 | |
2005 | /* We now just add it to the database */ | |
2006 | row[DB_type] = (char *)OPENSSL_malloc(2); | |
2007 | ||
2008 | tm = X509_get_notAfter(ret); | |
2009 | row[DB_exp_date] = (char *)OPENSSL_malloc(tm->length + 1); | |
2010 | memcpy(row[DB_exp_date], tm->data, tm->length); | |
2011 | row[DB_exp_date][tm->length] = '\0'; | |
2012 | ||
2013 | row[DB_rev_date] = NULL; | |
2014 | ||
2015 | /* row[DB_serial] done already */ | |
2016 | row[DB_file] = (char *)OPENSSL_malloc(8); | |
2017 | row[DB_name] = X509_NAME_oneline(X509_get_subject_name(ret), NULL, 0); | |
2018 | ||
2019 | if ((row[DB_type] == NULL) || (row[DB_exp_date] == NULL) || | |
2020 | (row[DB_file] == NULL) || (row[DB_name] == NULL)) { | |
2021 | BIO_printf(bio_err, "Memory allocation failure\n"); | |
7e1b7485 | 2022 | goto end; |
0f113f3e MC |
2023 | } |
2024 | BUF_strlcpy(row[DB_file], "unknown", 8); | |
2025 | row[DB_type][0] = 'V'; | |
2026 | row[DB_type][1] = '\0'; | |
2027 | ||
2028 | if ((irow = | |
2029 | (char **)OPENSSL_malloc(sizeof(char *) * (DB_NUMBER + 1))) == NULL) { | |
2030 | BIO_printf(bio_err, "Memory allocation failure\n"); | |
7e1b7485 | 2031 | goto end; |
0f113f3e MC |
2032 | } |
2033 | ||
2034 | for (i = 0; i < DB_NUMBER; i++) { | |
2035 | irow[i] = row[i]; | |
2036 | row[i] = NULL; | |
2037 | } | |
2038 | irow[DB_NUMBER] = NULL; | |
2039 | ||
2040 | if (!TXT_DB_insert(db->db, irow)) { | |
2041 | BIO_printf(bio_err, "failed to update database\n"); | |
2042 | BIO_printf(bio_err, "TXT_DB error number %ld\n", db->db->error); | |
7e1b7485 | 2043 | goto end; |
0f113f3e MC |
2044 | } |
2045 | ok = 1; | |
7e1b7485 | 2046 | end: |
0f113f3e MC |
2047 | for (i = 0; i < DB_NUMBER; i++) |
2048 | if (row[i] != NULL) | |
2049 | OPENSSL_free(row[i]); | |
2050 | ||
2051 | if (CAname != NULL) | |
2052 | X509_NAME_free(CAname); | |
2053 | if (subject != NULL) | |
2054 | X509_NAME_free(subject); | |
2055 | if ((dn_subject != NULL) && !email_dn) | |
2056 | X509_NAME_free(dn_subject); | |
2057 | if (tmptm != NULL) | |
2058 | ASN1_UTCTIME_free(tmptm); | |
2059 | if (ok <= 0) { | |
2060 | if (ret != NULL) | |
2061 | X509_free(ret); | |
2062 | ret = NULL; | |
2063 | } else | |
2064 | *xret = ret; | |
2065 | return (ok); | |
2066 | } | |
2067 | ||
2068 | static void write_new_certificate(BIO *bp, X509 *x, int output_der, | |
2069 | int notext) | |
2070 | { | |
2071 | ||
2072 | if (output_der) { | |
2073 | (void)i2d_X509_bio(bp, x); | |
2074 | return; | |
2075 | } | |
0f113f3e MC |
2076 | if (!notext) |
2077 | X509_print(bp, x); | |
2078 | PEM_write_bio_X509(bp, x); | |
2079 | } | |
2080 | ||
2081 | static int certify_spkac(X509 **xret, char *infile, EVP_PKEY *pkey, | |
2082 | X509 *x509, const EVP_MD *dgst, | |
2083 | STACK_OF(OPENSSL_STRING) *sigopts, | |
2084 | STACK_OF(CONF_VALUE) *policy, CA_DB *db, | |
2085 | BIGNUM *serial, char *subj, unsigned long chtype, | |
2086 | int multirdn, int email_dn, char *startdate, | |
2087 | char *enddate, long days, char *ext_sect, | |
2088 | CONF *lconf, int verbose, unsigned long certopt, | |
2089 | unsigned long nameopt, int default_op, int ext_copy) | |
2090 | { | |
2091 | STACK_OF(CONF_VALUE) *sk = NULL; | |
2092 | LHASH_OF(CONF_VALUE) *parms = NULL; | |
2093 | X509_REQ *req = NULL; | |
2094 | CONF_VALUE *cv = NULL; | |
2095 | NETSCAPE_SPKI *spki = NULL; | |
2096 | X509_REQ_INFO *ri; | |
2097 | char *type, *buf; | |
2098 | EVP_PKEY *pktmp = NULL; | |
2099 | X509_NAME *n = NULL; | |
2100 | X509_NAME_ENTRY *ne = NULL; | |
2101 | int ok = -1, i, j; | |
2102 | long errline; | |
2103 | int nid; | |
2104 | ||
2105 | /* | |
2106 | * Load input file into a hash table. (This is just an easy | |
2107 | * way to read and parse the file, then put it into a convenient | |
2108 | * STACK format). | |
2109 | */ | |
2110 | parms = CONF_load(NULL, infile, &errline); | |
2111 | if (parms == NULL) { | |
2112 | BIO_printf(bio_err, "error on line %ld of %s\n", errline, infile); | |
2113 | ERR_print_errors(bio_err); | |
7e1b7485 | 2114 | goto end; |
0f113f3e MC |
2115 | } |
2116 | ||
2117 | sk = CONF_get_section(parms, "default"); | |
2118 | if (sk_CONF_VALUE_num(sk) == 0) { | |
2119 | BIO_printf(bio_err, "no name/value pairs found in %s\n", infile); | |
2120 | CONF_free(parms); | |
7e1b7485 | 2121 | goto end; |
0f113f3e MC |
2122 | } |
2123 | ||
2124 | /* | |
2125 | * Now create a dummy X509 request structure. We don't actually | |
2126 | * have an X509 request, but we have many of the components | |
2127 | * (a public key, various DN components). The idea is that we | |
2128 | * put these components into the right X509 request structure | |
2129 | * and we can use the same code as if you had a real X509 request. | |
2130 | */ | |
2131 | req = X509_REQ_new(); | |
2132 | if (req == NULL) { | |
2133 | ERR_print_errors(bio_err); | |
7e1b7485 | 2134 | goto end; |
0f113f3e MC |
2135 | } |
2136 | ||
2137 | /* | |
2138 | * Build up the subject name set. | |
2139 | */ | |
2140 | ri = req->req_info; | |
2141 | n = ri->subject; | |
2142 | ||
2143 | for (i = 0;; i++) { | |
2144 | if (sk_CONF_VALUE_num(sk) <= i) | |
2145 | break; | |
2146 | ||
2147 | cv = sk_CONF_VALUE_value(sk, i); | |
2148 | type = cv->name; | |
2149 | /* | |
2150 | * Skip past any leading X. X: X, etc to allow for multiple instances | |
2151 | */ | |
2152 | for (buf = cv->name; *buf; buf++) | |
2153 | if ((*buf == ':') || (*buf == ',') || (*buf == '.')) { | |
2154 | buf++; | |
2155 | if (*buf) | |
2156 | type = buf; | |
2157 | break; | |
2158 | } | |
2159 | ||
2160 | buf = cv->value; | |
2161 | if ((nid = OBJ_txt2nid(type)) == NID_undef) { | |
2162 | if (strcmp(type, "SPKAC") == 0) { | |
2163 | spki = NETSCAPE_SPKI_b64_decode(cv->value, -1); | |
2164 | if (spki == NULL) { | |
2165 | BIO_printf(bio_err, | |
2166 | "unable to load Netscape SPKAC structure\n"); | |
2167 | ERR_print_errors(bio_err); | |
7e1b7485 | 2168 | goto end; |
0f113f3e MC |
2169 | } |
2170 | } | |
2171 | continue; | |
2172 | } | |
2173 | ||
2174 | if (!X509_NAME_add_entry_by_NID(n, nid, chtype, | |
2175 | (unsigned char *)buf, -1, -1, 0)) | |
7e1b7485 | 2176 | goto end; |
0f113f3e MC |
2177 | } |
2178 | if (spki == NULL) { | |
2179 | BIO_printf(bio_err, "Netscape SPKAC structure not found in %s\n", | |
2180 | infile); | |
7e1b7485 | 2181 | goto end; |
0f113f3e MC |
2182 | } |
2183 | ||
2184 | /* | |
2185 | * Now extract the key from the SPKI structure. | |
2186 | */ | |
2187 | ||
2188 | BIO_printf(bio_err, | |
2189 | "Check that the SPKAC request matches the signature\n"); | |
2190 | ||
2191 | if ((pktmp = NETSCAPE_SPKI_get_pubkey(spki)) == NULL) { | |
2192 | BIO_printf(bio_err, "error unpacking SPKAC public key\n"); | |
7e1b7485 | 2193 | goto end; |
0f113f3e MC |
2194 | } |
2195 | ||
2196 | j = NETSCAPE_SPKI_verify(spki, pktmp); | |
2197 | if (j <= 0) { | |
2198 | BIO_printf(bio_err, | |
2199 | "signature verification failed on SPKAC public key\n"); | |
7e1b7485 | 2200 | goto end; |
0f113f3e MC |
2201 | } |
2202 | BIO_printf(bio_err, "Signature ok\n"); | |
2203 | ||
2204 | X509_REQ_set_pubkey(req, pktmp); | |
2205 | EVP_PKEY_free(pktmp); | |
2206 | ok = do_body(xret, pkey, x509, dgst, sigopts, policy, db, serial, subj, | |
2207 | chtype, multirdn, email_dn, startdate, enddate, days, 1, | |
2208 | verbose, req, ext_sect, lconf, certopt, nameopt, default_op, | |
2209 | ext_copy, 0); | |
7e1b7485 | 2210 | end: |
0f113f3e MC |
2211 | if (req != NULL) |
2212 | X509_REQ_free(req); | |
2213 | if (parms != NULL) | |
2214 | CONF_free(parms); | |
2215 | if (spki != NULL) | |
2216 | NETSCAPE_SPKI_free(spki); | |
2217 | if (ne != NULL) | |
2218 | X509_NAME_ENTRY_free(ne); | |
2219 | ||
2220 | return (ok); | |
2221 | } | |
d02b48c6 | 2222 | |
7c337e00 | 2223 | static int check_time_format(const char *str) |
0f113f3e MC |
2224 | { |
2225 | return ASN1_TIME_set_string(NULL, str); | |
2226 | } | |
d02b48c6 | 2227 | |
f85b68cd | 2228 | static int do_revoke(X509 *x509, CA_DB *db, int type, char *value) |
0f113f3e MC |
2229 | { |
2230 | ASN1_UTCTIME *tm = NULL; | |
2231 | char *row[DB_NUMBER], **rrow, **irow; | |
2232 | char *rev_str = NULL; | |
2233 | BIGNUM *bn = NULL; | |
2234 | int ok = -1, i; | |
2235 | ||
2236 | for (i = 0; i < DB_NUMBER; i++) | |
2237 | row[i] = NULL; | |
2238 | row[DB_name] = X509_NAME_oneline(X509_get_subject_name(x509), NULL, 0); | |
2239 | bn = ASN1_INTEGER_to_BN(X509_get_serialNumber(x509), NULL); | |
2240 | if (!bn) | |
7e1b7485 | 2241 | goto end; |
0f113f3e MC |
2242 | if (BN_is_zero(bn)) |
2243 | row[DB_serial] = BUF_strdup("00"); | |
2244 | else | |
2245 | row[DB_serial] = BN_bn2hex(bn); | |
2246 | BN_free(bn); | |
2247 | if ((row[DB_name] == NULL) || (row[DB_serial] == NULL)) { | |
2248 | BIO_printf(bio_err, "Memory allocation failure\n"); | |
7e1b7485 | 2249 | goto end; |
0f113f3e MC |
2250 | } |
2251 | /* | |
2252 | * We have to lookup by serial number because name lookup skips revoked | |
2253 | * certs | |
2254 | */ | |
2255 | rrow = TXT_DB_get_by_index(db->db, DB_serial, row); | |
2256 | if (rrow == NULL) { | |
2257 | BIO_printf(bio_err, | |
2258 | "Adding Entry with serial number %s to DB for %s\n", | |
2259 | row[DB_serial], row[DB_name]); | |
2260 | ||
2261 | /* We now just add it to the database */ | |
2262 | row[DB_type] = (char *)OPENSSL_malloc(2); | |
2263 | ||
2264 | tm = X509_get_notAfter(x509); | |
2265 | row[DB_exp_date] = (char *)OPENSSL_malloc(tm->length + 1); | |
2266 | memcpy(row[DB_exp_date], tm->data, tm->length); | |
2267 | row[DB_exp_date][tm->length] = '\0'; | |
2268 | ||
2269 | row[DB_rev_date] = NULL; | |
2270 | ||
2271 | /* row[DB_serial] done already */ | |
2272 | row[DB_file] = (char *)OPENSSL_malloc(8); | |
2273 | ||
2274 | /* row[DB_name] done already */ | |
2275 | ||
2276 | if ((row[DB_type] == NULL) || (row[DB_exp_date] == NULL) || | |
2277 | (row[DB_file] == NULL)) { | |
2278 | BIO_printf(bio_err, "Memory allocation failure\n"); | |
7e1b7485 | 2279 | goto end; |
0f113f3e MC |
2280 | } |
2281 | BUF_strlcpy(row[DB_file], "unknown", 8); | |
2282 | row[DB_type][0] = 'V'; | |
2283 | row[DB_type][1] = '\0'; | |
2284 | ||
2285 | if ((irow = | |
2286 | (char **)OPENSSL_malloc(sizeof(char *) * (DB_NUMBER + 1))) == | |
2287 | NULL) { | |
2288 | BIO_printf(bio_err, "Memory allocation failure\n"); | |
7e1b7485 | 2289 | goto end; |
0f113f3e MC |
2290 | } |
2291 | ||
2292 | for (i = 0; i < DB_NUMBER; i++) { | |
2293 | irow[i] = row[i]; | |
2294 | row[i] = NULL; | |
2295 | } | |
2296 | irow[DB_NUMBER] = NULL; | |
2297 | ||
2298 | if (!TXT_DB_insert(db->db, irow)) { | |
2299 | BIO_printf(bio_err, "failed to update database\n"); | |
2300 | BIO_printf(bio_err, "TXT_DB error number %ld\n", db->db->error); | |
7e1b7485 | 2301 | goto end; |
0f113f3e MC |
2302 | } |
2303 | ||
2304 | /* Revoke Certificate */ | |
2305 | if (type == -1) | |
2306 | ok = 1; | |
2307 | else | |
2308 | ok = do_revoke(x509, db, type, value); | |
2309 | ||
7e1b7485 | 2310 | goto end; |
0f113f3e MC |
2311 | |
2312 | } else if (index_name_cmp_noconst(row, rrow)) { | |
2313 | BIO_printf(bio_err, "ERROR:name does not match %s\n", row[DB_name]); | |
7e1b7485 | 2314 | goto end; |
0f113f3e MC |
2315 | } else if (type == -1) { |
2316 | BIO_printf(bio_err, "ERROR:Already present, serial number %s\n", | |
2317 | row[DB_serial]); | |
7e1b7485 | 2318 | goto end; |
0f113f3e MC |
2319 | } else if (rrow[DB_type][0] == 'R') { |
2320 | BIO_printf(bio_err, "ERROR:Already revoked, serial number %s\n", | |
2321 | row[DB_serial]); | |
7e1b7485 | 2322 | goto end; |
0f113f3e MC |
2323 | } else { |
2324 | BIO_printf(bio_err, "Revoking Certificate %s.\n", rrow[DB_serial]); | |
2325 | rev_str = make_revocation_str(type, value); | |
2326 | if (!rev_str) { | |
2327 | BIO_printf(bio_err, "Error in revocation arguments\n"); | |
7e1b7485 | 2328 | goto end; |
0f113f3e MC |
2329 | } |
2330 | rrow[DB_type][0] = 'R'; | |
2331 | rrow[DB_type][1] = '\0'; | |
2332 | rrow[DB_rev_date] = rev_str; | |
2333 | } | |
2334 | ok = 1; | |
7e1b7485 | 2335 | end: |
0f113f3e MC |
2336 | for (i = 0; i < DB_NUMBER; i++) { |
2337 | if (row[i] != NULL) | |
2338 | OPENSSL_free(row[i]); | |
2339 | } | |
2340 | return (ok); | |
2341 | } | |
c67cdb50 | 2342 | |
f85b68cd | 2343 | static int get_certificate_status(const char *serial, CA_DB *db) |
0f113f3e MC |
2344 | { |
2345 | char *row[DB_NUMBER], **rrow; | |
2346 | int ok = -1, i; | |
2347 | ||
2348 | /* Free Resources */ | |
2349 | for (i = 0; i < DB_NUMBER; i++) | |
2350 | row[i] = NULL; | |
2351 | ||
2352 | /* Malloc needed char spaces */ | |
2353 | row[DB_serial] = OPENSSL_malloc(strlen(serial) + 2); | |
2354 | if (row[DB_serial] == NULL) { | |
2355 | BIO_printf(bio_err, "Malloc failure\n"); | |
7e1b7485 | 2356 | goto end; |
0f113f3e MC |
2357 | } |
2358 | ||
2359 | if (strlen(serial) % 2) { | |
2360 | /* | |
2361 | * Set the first char to 0 | |
2362 | */ ; | |
2363 | row[DB_serial][0] = '0'; | |
2364 | ||
2365 | /* Copy String from serial to row[DB_serial] */ | |
2366 | memcpy(row[DB_serial] + 1, serial, strlen(serial)); | |
2367 | row[DB_serial][strlen(serial) + 1] = '\0'; | |
2368 | } else { | |
2369 | /* Copy String from serial to row[DB_serial] */ | |
2370 | memcpy(row[DB_serial], serial, strlen(serial)); | |
2371 | row[DB_serial][strlen(serial)] = '\0'; | |
2372 | } | |
2373 | ||
2374 | /* Make it Upper Case */ | |
2375 | for (i = 0; row[DB_serial][i] != '\0'; i++) | |
2376 | row[DB_serial][i] = toupper((unsigned char)row[DB_serial][i]); | |
2377 | ||
2378 | ok = 1; | |
2379 | ||
2380 | /* Search for the certificate */ | |
2381 | rrow = TXT_DB_get_by_index(db->db, DB_serial, row); | |
2382 | if (rrow == NULL) { | |
2383 | BIO_printf(bio_err, "Serial %s not present in db.\n", row[DB_serial]); | |
2384 | ok = -1; | |
7e1b7485 | 2385 | goto end; |
0f113f3e MC |
2386 | } else if (rrow[DB_type][0] == 'V') { |
2387 | BIO_printf(bio_err, "%s=Valid (%c)\n", | |
2388 | row[DB_serial], rrow[DB_type][0]); | |
7e1b7485 | 2389 | goto end; |
0f113f3e MC |
2390 | } else if (rrow[DB_type][0] == 'R') { |
2391 | BIO_printf(bio_err, "%s=Revoked (%c)\n", | |
2392 | row[DB_serial], rrow[DB_type][0]); | |
7e1b7485 | 2393 | goto end; |
0f113f3e MC |
2394 | } else if (rrow[DB_type][0] == 'E') { |
2395 | BIO_printf(bio_err, "%s=Expired (%c)\n", | |
2396 | row[DB_serial], rrow[DB_type][0]); | |
7e1b7485 | 2397 | goto end; |
0f113f3e MC |
2398 | } else if (rrow[DB_type][0] == 'S') { |
2399 | BIO_printf(bio_err, "%s=Suspended (%c)\n", | |
2400 | row[DB_serial], rrow[DB_type][0]); | |
7e1b7485 | 2401 | goto end; |
0f113f3e MC |
2402 | } else { |
2403 | BIO_printf(bio_err, "%s=Unknown (%c).\n", | |
2404 | row[DB_serial], rrow[DB_type][0]); | |
2405 | ok = -1; | |
2406 | } | |
7e1b7485 | 2407 | end: |
0f113f3e MC |
2408 | for (i = 0; i < DB_NUMBER; i++) { |
2409 | if (row[i] != NULL) | |
2410 | OPENSSL_free(row[i]); | |
2411 | } | |
2412 | return (ok); | |
2413 | } | |
2414 | ||
2415 | static int do_updatedb(CA_DB *db) | |
2416 | { | |
2417 | ASN1_UTCTIME *a_tm = NULL; | |
2418 | int i, cnt = 0; | |
2419 | int db_y2k, a_y2k; /* flags = 1 if y >= 2000 */ | |
2420 | char **rrow, *a_tm_s; | |
2421 | ||
2422 | a_tm = ASN1_UTCTIME_new(); | |
2423 | ||
2424 | /* get actual time and make a string */ | |
2425 | a_tm = X509_gmtime_adj(a_tm, 0); | |
2426 | a_tm_s = (char *)OPENSSL_malloc(a_tm->length + 1); | |
2427 | if (a_tm_s == NULL) { | |
2428 | cnt = -1; | |
7e1b7485 | 2429 | goto end; |
0f113f3e MC |
2430 | } |
2431 | ||
2432 | memcpy(a_tm_s, a_tm->data, a_tm->length); | |
2433 | a_tm_s[a_tm->length] = '\0'; | |
2434 | ||
2435 | if (strncmp(a_tm_s, "49", 2) <= 0) | |
2436 | a_y2k = 1; | |
2437 | else | |
2438 | a_y2k = 0; | |
2439 | ||
2440 | for (i = 0; i < sk_OPENSSL_PSTRING_num(db->db->data); i++) { | |
2441 | rrow = sk_OPENSSL_PSTRING_value(db->db->data, i); | |
2442 | ||
2443 | if (rrow[DB_type][0] == 'V') { | |
2444 | /* ignore entries that are not valid */ | |
2445 | if (strncmp(rrow[DB_exp_date], "49", 2) <= 0) | |
2446 | db_y2k = 1; | |
2447 | else | |
2448 | db_y2k = 0; | |
2449 | ||
2450 | if (db_y2k == a_y2k) { | |
2451 | /* all on the same y2k side */ | |
2452 | if (strcmp(rrow[DB_exp_date], a_tm_s) <= 0) { | |
2453 | rrow[DB_type][0] = 'E'; | |
2454 | rrow[DB_type][1] = '\0'; | |
2455 | cnt++; | |
2456 | ||
2457 | BIO_printf(bio_err, "%s=Expired\n", rrow[DB_serial]); | |
2458 | } | |
2459 | } else if (db_y2k < a_y2k) { | |
2460 | rrow[DB_type][0] = 'E'; | |
2461 | rrow[DB_type][1] = '\0'; | |
2462 | cnt++; | |
2463 | ||
2464 | BIO_printf(bio_err, "%s=Expired\n", rrow[DB_serial]); | |
2465 | } | |
2466 | ||
2467 | } | |
2468 | } | |
2469 | ||
7e1b7485 | 2470 | end: |
0f113f3e MC |
2471 | |
2472 | ASN1_UTCTIME_free(a_tm); | |
2473 | OPENSSL_free(a_tm_s); | |
2474 | ||
2475 | return (cnt); | |
2476 | } | |
a6b7ffdd | 2477 | |
7d727231 | 2478 | static const char *crl_reasons[] = { |
0f113f3e MC |
2479 | /* CRL reason strings */ |
2480 | "unspecified", | |
2481 | "keyCompromise", | |
2482 | "CACompromise", | |
2483 | "affiliationChanged", | |
2484 | "superseded", | |
2485 | "cessationOfOperation", | |
2486 | "certificateHold", | |
2487 | "removeFromCRL", | |
2488 | /* Additional pseudo reasons */ | |
2489 | "holdInstruction", | |
2490 | "keyTime", | |
2491 | "CAkeyTime" | |
a6b7ffdd DSH |
2492 | }; |
2493 | ||
2494 | #define NUM_REASONS (sizeof(crl_reasons) / sizeof(char *)) | |
2495 | ||
0f113f3e MC |
2496 | /* |
2497 | * Given revocation information convert to a DB string. The format of the | |
2498 | * string is: revtime[,reason,extra]. Where 'revtime' is the revocation time | |
2499 | * (the current time). 'reason' is the optional CRL reason and 'extra' is any | |
2500 | * additional argument | |
a6b7ffdd DSH |
2501 | */ |
2502 | ||
2503 | char *make_revocation_str(int rev_type, char *rev_arg) | |
0f113f3e MC |
2504 | { |
2505 | char *other = NULL, *str; | |
2506 | const char *reason = NULL; | |
2507 | ASN1_OBJECT *otmp; | |
2508 | ASN1_UTCTIME *revtm = NULL; | |
2509 | int i; | |
2510 | switch (rev_type) { | |
2511 | case REV_NONE: | |
2512 | break; | |
2513 | ||
2514 | case REV_CRL_REASON: | |
2515 | for (i = 0; i < 8; i++) { | |
2516 | if (!strcasecmp(rev_arg, crl_reasons[i])) { | |
2517 | reason = crl_reasons[i]; | |
2518 | break; | |
2519 | } | |
2520 | } | |
2521 | if (reason == NULL) { | |
2522 | BIO_printf(bio_err, "Unknown CRL reason %s\n", rev_arg); | |
2523 | return NULL; | |
2524 | } | |
2525 | break; | |
2526 | ||
2527 | case REV_HOLD: | |
2528 | /* Argument is an OID */ | |
2529 | ||
2530 | otmp = OBJ_txt2obj(rev_arg, 0); | |
2531 | ASN1_OBJECT_free(otmp); | |
2532 | ||
2533 | if (otmp == NULL) { | |
2534 | BIO_printf(bio_err, "Invalid object identifier %s\n", rev_arg); | |
2535 | return NULL; | |
2536 | } | |
2537 | ||
2538 | reason = "holdInstruction"; | |
2539 | other = rev_arg; | |
2540 | break; | |
2541 | ||
2542 | case REV_KEY_COMPROMISE: | |
2543 | case REV_CA_COMPROMISE: | |
2544 | ||
2545 | /* Argument is the key compromise time */ | |
2546 | if (!ASN1_GENERALIZEDTIME_set_string(NULL, rev_arg)) { | |
2547 | BIO_printf(bio_err, | |
2548 | "Invalid time format %s. Need YYYYMMDDHHMMSSZ\n", | |
2549 | rev_arg); | |
2550 | return NULL; | |
2551 | } | |
2552 | other = rev_arg; | |
2553 | if (rev_type == REV_KEY_COMPROMISE) | |
2554 | reason = "keyTime"; | |
2555 | else | |
2556 | reason = "CAkeyTime"; | |
2557 | ||
2558 | break; | |
2559 | ||
2560 | } | |
2561 | ||
2562 | revtm = X509_gmtime_adj(NULL, 0); | |
2563 | ||
2564 | if (!revtm) | |
2565 | return NULL; | |
2566 | ||
2567 | i = revtm->length + 1; | |
2568 | ||
2569 | if (reason) | |
2570 | i += strlen(reason) + 1; | |
2571 | if (other) | |
2572 | i += strlen(other) + 1; | |
2573 | ||
2574 | str = OPENSSL_malloc(i); | |
2575 | ||
2576 | if (!str) | |
2577 | return NULL; | |
2578 | ||
2579 | BUF_strlcpy(str, (char *)revtm->data, i); | |
2580 | if (reason) { | |
2581 | BUF_strlcat(str, ",", i); | |
2582 | BUF_strlcat(str, reason, i); | |
2583 | } | |
2584 | if (other) { | |
2585 | BUF_strlcat(str, ",", i); | |
2586 | BUF_strlcat(str, other, i); | |
2587 | } | |
2588 | ASN1_UTCTIME_free(revtm); | |
2589 | return str; | |
2590 | } | |
a6b7ffdd | 2591 | |
1d97c843 | 2592 | /*- |
0f113f3e | 2593 | * Convert revocation field to X509_REVOKED entry |
a6b7ffdd DSH |
2594 | * return code: |
2595 | * 0 error | |
2596 | * 1 OK | |
2597 | * 2 OK and some extensions added (i.e. V2 CRL) | |
2598 | */ | |
2599 | ||
7d727231 | 2600 | int make_revoked(X509_REVOKED *rev, const char *str) |
0f113f3e MC |
2601 | { |
2602 | char *tmp = NULL; | |
2603 | int reason_code = -1; | |
2604 | int i, ret = 0; | |
2605 | ASN1_OBJECT *hold = NULL; | |
2606 | ASN1_GENERALIZEDTIME *comp_time = NULL; | |
2607 | ASN1_ENUMERATED *rtmp = NULL; | |
2608 | ||
2609 | ASN1_TIME *revDate = NULL; | |
2610 | ||
2611 | i = unpack_revinfo(&revDate, &reason_code, &hold, &comp_time, str); | |
2612 | ||
2613 | if (i == 0) | |
7e1b7485 | 2614 | goto end; |
0f113f3e MC |
2615 | |
2616 | if (rev && !X509_REVOKED_set_revocationDate(rev, revDate)) | |
7e1b7485 | 2617 | goto end; |
0f113f3e MC |
2618 | |
2619 | if (rev && (reason_code != OCSP_REVOKED_STATUS_NOSTATUS)) { | |
2620 | rtmp = ASN1_ENUMERATED_new(); | |
2621 | if (!rtmp || !ASN1_ENUMERATED_set(rtmp, reason_code)) | |
7e1b7485 | 2622 | goto end; |
0f113f3e | 2623 | if (!X509_REVOKED_add1_ext_i2d(rev, NID_crl_reason, rtmp, 0, 0)) |
7e1b7485 | 2624 | goto end; |
0f113f3e MC |
2625 | } |
2626 | ||
2627 | if (rev && comp_time) { | |
2628 | if (!X509_REVOKED_add1_ext_i2d | |
2629 | (rev, NID_invalidity_date, comp_time, 0, 0)) | |
7e1b7485 | 2630 | goto end; |
0f113f3e MC |
2631 | } |
2632 | if (rev && hold) { | |
2633 | if (!X509_REVOKED_add1_ext_i2d | |
2634 | (rev, NID_hold_instruction_code, hold, 0, 0)) | |
7e1b7485 | 2635 | goto end; |
0f113f3e MC |
2636 | } |
2637 | ||
2638 | if (reason_code != OCSP_REVOKED_STATUS_NOSTATUS) | |
2639 | ret = 2; | |
2640 | else | |
2641 | ret = 1; | |
2642 | ||
7e1b7485 | 2643 | end: |
0f113f3e MC |
2644 | |
2645 | if (tmp) | |
2646 | OPENSSL_free(tmp); | |
2647 | ASN1_OBJECT_free(hold); | |
2648 | ASN1_GENERALIZEDTIME_free(comp_time); | |
2649 | ASN1_ENUMERATED_free(rtmp); | |
2650 | ASN1_TIME_free(revDate); | |
2651 | ||
2652 | return ret; | |
2653 | } | |
bad40585 | 2654 | |
535d79da | 2655 | int old_entry_print(BIO *bp, ASN1_OBJECT *obj, ASN1_STRING *str) |
0f113f3e MC |
2656 | { |
2657 | char buf[25], *pbuf, *p; | |
2658 | int j; | |
2659 | j = i2a_ASN1_OBJECT(bp, obj); | |
2660 | pbuf = buf; | |
2661 | for (j = 22 - j; j > 0; j--) | |
2662 | *(pbuf++) = ' '; | |
2663 | *(pbuf++) = ':'; | |
2664 | *(pbuf++) = '\0'; | |
2665 | BIO_puts(bp, buf); | |
2666 | ||
2667 | if (str->type == V_ASN1_PRINTABLESTRING) | |
2668 | BIO_printf(bp, "PRINTABLE:'"); | |
2669 | else if (str->type == V_ASN1_T61STRING) | |
2670 | BIO_printf(bp, "T61STRING:'"); | |
2671 | else if (str->type == V_ASN1_IA5STRING) | |
2672 | BIO_printf(bp, "IA5STRING:'"); | |
2673 | else if (str->type == V_ASN1_UNIVERSALSTRING) | |
2674 | BIO_printf(bp, "UNIVERSALSTRING:'"); | |
2675 | else | |
2676 | BIO_printf(bp, "ASN.1 %2d:'", str->type); | |
2677 | ||
2678 | p = (char *)str->data; | |
2679 | for (j = str->length; j > 0; j--) { | |
2680 | if ((*p >= ' ') && (*p <= '~')) | |
2681 | BIO_printf(bp, "%c", *p); | |
2682 | else if (*p & 0x80) | |
2683 | BIO_printf(bp, "\\0x%02X", *p); | |
2684 | else if ((unsigned char)*p == 0xf7) | |
2685 | BIO_printf(bp, "^?"); | |
2686 | else | |
2687 | BIO_printf(bp, "^%c", *p + '@'); | |
2688 | p++; | |
2689 | } | |
2690 | BIO_printf(bp, "'\n"); | |
2691 | return 1; | |
2692 | } | |
2693 | ||
2694 | int unpack_revinfo(ASN1_TIME **prevtm, int *preason, ASN1_OBJECT **phold, | |
2695 | ASN1_GENERALIZEDTIME **pinvtm, const char *str) | |
2696 | { | |
7e1b7485 | 2697 | char *tmp; |
0f113f3e MC |
2698 | char *rtime_str, *reason_str = NULL, *arg_str = NULL, *p; |
2699 | int reason_code = -1; | |
2700 | int ret = 0; | |
2701 | unsigned int i; | |
2702 | ASN1_OBJECT *hold = NULL; | |
2703 | ASN1_GENERALIZEDTIME *comp_time = NULL; | |
0f113f3e | 2704 | |
7e1b7485 | 2705 | tmp = BUF_strdup(str); |
61986d32 | 2706 | if (!tmp) { |
a561bfe9 | 2707 | BIO_printf(bio_err, "memory allocation failure\n"); |
7e1b7485 | 2708 | goto end; |
a561bfe9 MC |
2709 | } |
2710 | ||
0f113f3e MC |
2711 | p = strchr(tmp, ','); |
2712 | ||
2713 | rtime_str = tmp; | |
2714 | ||
2715 | if (p) { | |
2716 | *p = '\0'; | |
2717 | p++; | |
2718 | reason_str = p; | |
2719 | p = strchr(p, ','); | |
2720 | if (p) { | |
2721 | *p = '\0'; | |
2722 | arg_str = p + 1; | |
2723 | } | |
2724 | } | |
2725 | ||
2726 | if (prevtm) { | |
2727 | *prevtm = ASN1_UTCTIME_new(); | |
61986d32 | 2728 | if (!*prevtm) { |
a561bfe9 | 2729 | BIO_printf(bio_err, "memory allocation failure\n"); |
7e1b7485 | 2730 | goto end; |
a561bfe9 | 2731 | } |
0f113f3e MC |
2732 | if (!ASN1_UTCTIME_set_string(*prevtm, rtime_str)) { |
2733 | BIO_printf(bio_err, "invalid revocation date %s\n", rtime_str); | |
7e1b7485 | 2734 | goto end; |
0f113f3e MC |
2735 | } |
2736 | } | |
2737 | if (reason_str) { | |
2738 | for (i = 0; i < NUM_REASONS; i++) { | |
2739 | if (!strcasecmp(reason_str, crl_reasons[i])) { | |
2740 | reason_code = i; | |
2741 | break; | |
2742 | } | |
2743 | } | |
2744 | if (reason_code == OCSP_REVOKED_STATUS_NOSTATUS) { | |
2745 | BIO_printf(bio_err, "invalid reason code %s\n", reason_str); | |
7e1b7485 | 2746 | goto end; |
0f113f3e MC |
2747 | } |
2748 | ||
2749 | if (reason_code == 7) | |
2750 | reason_code = OCSP_REVOKED_STATUS_REMOVEFROMCRL; | |
2751 | else if (reason_code == 8) { /* Hold instruction */ | |
2752 | if (!arg_str) { | |
2753 | BIO_printf(bio_err, "missing hold instruction\n"); | |
7e1b7485 | 2754 | goto end; |
0f113f3e MC |
2755 | } |
2756 | reason_code = OCSP_REVOKED_STATUS_CERTIFICATEHOLD; | |
2757 | hold = OBJ_txt2obj(arg_str, 0); | |
2758 | ||
2759 | if (!hold) { | |
2760 | BIO_printf(bio_err, "invalid object identifier %s\n", | |
2761 | arg_str); | |
7e1b7485 | 2762 | goto end; |
0f113f3e MC |
2763 | } |
2764 | if (phold) | |
2765 | *phold = hold; | |
2766 | } else if ((reason_code == 9) || (reason_code == 10)) { | |
2767 | if (!arg_str) { | |
2768 | BIO_printf(bio_err, "missing compromised time\n"); | |
7e1b7485 | 2769 | goto end; |
0f113f3e MC |
2770 | } |
2771 | comp_time = ASN1_GENERALIZEDTIME_new(); | |
61986d32 | 2772 | if (!comp_time) { |
a561bfe9 | 2773 | BIO_printf(bio_err, "memory allocation failure\n"); |
7e1b7485 | 2774 | goto end; |
a561bfe9 | 2775 | } |
0f113f3e MC |
2776 | if (!ASN1_GENERALIZEDTIME_set_string(comp_time, arg_str)) { |
2777 | BIO_printf(bio_err, "invalid compromised time %s\n", arg_str); | |
7e1b7485 | 2778 | goto end; |
0f113f3e MC |
2779 | } |
2780 | if (reason_code == 9) | |
2781 | reason_code = OCSP_REVOKED_STATUS_KEYCOMPROMISE; | |
2782 | else | |
2783 | reason_code = OCSP_REVOKED_STATUS_CACOMPROMISE; | |
2784 | } | |
2785 | } | |
2786 | ||
2787 | if (preason) | |
2788 | *preason = reason_code; | |
2789 | if (pinvtm) | |
2790 | *pinvtm = comp_time; | |
2791 | else | |
2792 | ASN1_GENERALIZEDTIME_free(comp_time); | |
2793 | ||
2794 | ret = 1; | |
2795 | ||
7e1b7485 | 2796 | end: |
0f113f3e MC |
2797 | |
2798 | if (tmp) | |
2799 | OPENSSL_free(tmp); | |
2800 | if (!phold) | |
2801 | ASN1_OBJECT_free(hold); | |
2802 | if (!pinvtm) | |
2803 | ASN1_GENERALIZEDTIME_free(comp_time); | |
2804 | ||
2805 | return ret; | |
2806 | } |