]>
Commit | Line | Data |
---|---|---|
846e33c7 | 1 | /* |
71bb86f0 | 2 | * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. |
a661b653 | 3 | * |
dffa7520 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
846e33c7 RS |
5 | * this file except in compliance with the License. You can obtain a copy |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
a661b653 | 8 | */ |
d02b48c6 | 9 | |
fc7dae52 | 10 | #if !defined(_POSIX_C_SOURCE) && defined(OPENSSL_SYS_VMS) |
0f113f3e MC |
11 | /* |
12 | * On VMS, you need to define this to get the declaration of fileno(). The | |
13 | * value 2 is to make sure no function defined in POSIX-2 is left undefined. | |
68d39f3c | 14 | */ |
0f113f3e | 15 | # define _POSIX_C_SOURCE 2 |
83d8fa7d | 16 | #endif |
75dd6c1a | 17 | |
d02b48c6 RE |
18 | #include <stdio.h> |
19 | #include <stdlib.h> | |
20 | #include <string.h> | |
b379fe6c | 21 | #include <sys/types.h> |
a412b891 RL |
22 | #ifndef OPENSSL_NO_POSIX_IO |
23 | # include <sys/stat.h> | |
24 | # include <fcntl.h> | |
25 | #endif | |
d652a095 | 26 | #include <ctype.h> |
a1ad253f | 27 | #include <errno.h> |
90ae4673 RL |
28 | #include <openssl/err.h> |
29 | #include <openssl/x509.h> | |
535d79da | 30 | #include <openssl/x509v3.h> |
90ae4673 RL |
31 | #include <openssl/pem.h> |
32 | #include <openssl/pkcs12.h> | |
2fe5adc3 | 33 | #include <openssl/ui.h> |
90ae4673 | 34 | #include <openssl/safestack.h> |
0b13e9f0 | 35 | #ifndef OPENSSL_NO_ENGINE |
0f113f3e | 36 | # include <openssl/engine.h> |
0b13e9f0 | 37 | #endif |
3eeaab4b | 38 | #ifndef OPENSSL_NO_RSA |
0f113f3e | 39 | # include <openssl/rsa.h> |
3eeaab4b | 40 | #endif |
f0eae953 | 41 | #include <openssl/bn.h> |
7e1b7485 | 42 | #include <openssl/ssl.h> |
d610d27f | 43 | #include "apps.h" |
d610d27f | 44 | |
a1ad253f AP |
45 | #ifdef _WIN32 |
46 | static int WIN32_rename(const char *from, const char *to); | |
0f113f3e | 47 | # define rename(from,to) WIN32_rename((from),(to)) |
a1ad253f AP |
48 | #endif |
49 | ||
d0cf719e DMSP |
50 | #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) |
51 | # include <conio.h> | |
52 | #endif | |
53 | ||
54 | #if defined(OPENSSL_SYS_MSDOS) && !defined(_WIN32) | |
55 | # define _kbhit kbhit | |
56 | #endif | |
57 | ||
62ca1565 DO |
58 | #define PASS_SOURCE_SIZE_MAX 4 |
59 | ||
8ca533e3 | 60 | typedef struct { |
0f113f3e MC |
61 | const char *name; |
62 | unsigned long flag; | |
63 | unsigned long mask; | |
8ca533e3 DSH |
64 | } NAME_EX_TBL; |
65 | ||
0f113f3e MC |
66 | static int set_table_opts(unsigned long *flags, const char *arg, |
67 | const NAME_EX_TBL * in_tbl); | |
68 | static int set_multi_opts(unsigned long *flags, const char *arg, | |
69 | const NAME_EX_TBL * in_tbl); | |
8ca533e3 | 70 | |
d02b48c6 | 71 | int app_init(long mesgwin); |
0f113f3e | 72 | |
7e1b7485 | 73 | int chopup_args(ARGS *arg, char *buf) |
0f113f3e | 74 | { |
7e1b7485 | 75 | int quoted; |
4c9b0a03 | 76 | char c = '\0', *p = NULL; |
0f113f3e | 77 | |
7e1b7485 RS |
78 | arg->argc = 0; |
79 | if (arg->size == 0) { | |
80 | arg->size = 20; | |
b4faea50 | 81 | arg->argv = app_malloc(sizeof(*arg->argv) * arg->size, "argv space"); |
0f113f3e | 82 | } |
0f113f3e | 83 | |
7e1b7485 RS |
84 | for (p = buf;;) { |
85 | /* Skip whitespace. */ | |
18295f0c | 86 | while (*p && isspace(_UC(*p))) |
0f113f3e MC |
87 | p++; |
88 | if (!*p) | |
89 | break; | |
90 | ||
91 | /* The start of something good :-) */ | |
7e1b7485 | 92 | if (arg->argc >= arg->size) { |
7c0ef843 | 93 | char **tmp; |
7e1b7485 | 94 | arg->size += 20; |
7c0ef843 DSH |
95 | tmp = OPENSSL_realloc(arg->argv, sizeof(*arg->argv) * arg->size); |
96 | if (tmp == NULL) | |
0f113f3e | 97 | return 0; |
7c0ef843 | 98 | arg->argv = tmp; |
0f113f3e | 99 | } |
7e1b7485 RS |
100 | quoted = *p == '\'' || *p == '"'; |
101 | if (quoted) | |
102 | c = *p++; | |
103 | arg->argv[arg->argc++] = p; | |
0f113f3e MC |
104 | |
105 | /* now look for the end of this */ | |
7e1b7485 RS |
106 | if (quoted) { |
107 | while (*p && *p != c) | |
0f113f3e | 108 | p++; |
7e1b7485 | 109 | *p++ = '\0'; |
0f113f3e | 110 | } else { |
18295f0c | 111 | while (*p && !isspace(_UC(*p))) |
0f113f3e | 112 | p++; |
7e1b7485 RS |
113 | if (*p) |
114 | *p++ = '\0'; | |
0f113f3e | 115 | } |
0f113f3e | 116 | } |
7e1b7485 | 117 | arg->argv[arg->argc] = NULL; |
208fb891 | 118 | return 1; |
0f113f3e | 119 | } |
d02b48c6 RE |
120 | |
121 | #ifndef APP_INIT | |
6b691a5c | 122 | int app_init(long mesgwin) |
0f113f3e | 123 | { |
208fb891 | 124 | return 1; |
0f113f3e | 125 | } |
d02b48c6 | 126 | #endif |
53b1899e | 127 | |
2b6bcb70 MC |
128 | int ctx_set_verify_locations(SSL_CTX *ctx, const char *CAfile, |
129 | const char *CApath, int noCAfile, int noCApath) | |
7e1b7485 | 130 | { |
2b6bcb70 MC |
131 | if (CAfile == NULL && CApath == NULL) { |
132 | if (!noCAfile && SSL_CTX_set_default_verify_file(ctx) <= 0) | |
133 | return 0; | |
134 | if (!noCApath && SSL_CTX_set_default_verify_dir(ctx) <= 0) | |
135 | return 0; | |
136 | ||
137 | return 1; | |
138 | } | |
7e1b7485 RS |
139 | return SSL_CTX_load_verify_locations(ctx, CAfile, CApath); |
140 | } | |
141 | ||
b5369582 RP |
142 | #ifndef OPENSSL_NO_CT |
143 | ||
dd696a55 RP |
144 | int ctx_set_ctlog_list_file(SSL_CTX *ctx, const char *path) |
145 | { | |
2234212c | 146 | if (path == NULL) |
328f36c5 | 147 | return SSL_CTX_set_default_ctlog_list_file(ctx); |
dd696a55 RP |
148 | |
149 | return SSL_CTX_set_ctlog_list_file(ctx, path); | |
150 | } | |
151 | ||
b5369582 RP |
152 | #endif |
153 | ||
b5c4209b DB |
154 | static unsigned long nmflag = 0; |
155 | static char nmflag_set = 0; | |
156 | ||
157 | int set_nameopt(const char *arg) | |
954ef7ef | 158 | { |
b5c4209b DB |
159 | int ret = set_name_ex(&nmflag, arg); |
160 | ||
161 | if (ret) | |
162 | nmflag_set = 1; | |
163 | ||
164 | return ret; | |
165 | } | |
54a656ef | 166 | |
b5c4209b DB |
167 | unsigned long get_nameopt(void) |
168 | { | |
169 | return (nmflag_set) ? nmflag : XN_FLAG_ONELINE; | |
170 | } | |
954ef7ef | 171 | |
b5c4209b DB |
172 | int dump_cert_text(BIO *out, X509 *x) |
173 | { | |
174 | print_name(out, "subject=", X509_get_subject_name(x), get_nameopt()); | |
175 | BIO_puts(out, "\n"); | |
176 | print_name(out, "issuer=", X509_get_issuer_name(x), get_nameopt()); | |
0f113f3e | 177 | BIO_puts(out, "\n"); |
54a656ef | 178 | |
0f113f3e | 179 | return 0; |
954ef7ef | 180 | } |
a3fe382e | 181 | |
229446df RS |
182 | int wrap_password_callback(char *buf, int bufsiz, int verify, void *userdata) |
183 | { | |
184 | return password_callback(buf, bufsiz, verify, (PW_CB_DATA *)userdata); | |
185 | } | |
186 | ||
a43ce58f | 187 | |
cc696296 | 188 | static char *app_get_pass(const char *arg, int keepbio); |
a3fe382e | 189 | |
cc696296 | 190 | int app_passwd(const char *arg1, const char *arg2, char **pass1, char **pass2) |
a3fe382e | 191 | { |
229446df RS |
192 | int same = arg1 != NULL && arg2 != NULL && strcmp(arg1, arg2) == 0; |
193 | ||
2234212c | 194 | if (arg1 != NULL) { |
7e1b7485 | 195 | *pass1 = app_get_pass(arg1, same); |
2234212c | 196 | if (*pass1 == NULL) |
0f113f3e | 197 | return 0; |
2234212c | 198 | } else if (pass1 != NULL) { |
0f113f3e | 199 | *pass1 = NULL; |
2234212c PY |
200 | } |
201 | if (arg2 != NULL) { | |
7e1b7485 | 202 | *pass2 = app_get_pass(arg2, same ? 2 : 0); |
2234212c | 203 | if (*pass2 == NULL) |
0f113f3e | 204 | return 0; |
2234212c | 205 | } else if (pass2 != NULL) { |
0f113f3e | 206 | *pass2 = NULL; |
2234212c | 207 | } |
0f113f3e | 208 | return 1; |
a3fe382e DSH |
209 | } |
210 | ||
cc696296 | 211 | static char *app_get_pass(const char *arg, int keepbio) |
a3fe382e | 212 | { |
0f113f3e | 213 | static BIO *pwdbio = NULL; |
229446df | 214 | char *tmp, tpass[APP_PASS_LEN]; |
0f113f3e | 215 | int i; |
86885c28 | 216 | |
62ca1565 | 217 | /* PASS_SOURCE_SIZE_MAX = max number of chars before ':' in below strings */ |
86885c28 | 218 | if (strncmp(arg, "pass:", 5) == 0) |
7644a9ae | 219 | return OPENSSL_strdup(arg + 5); |
86885c28 | 220 | if (strncmp(arg, "env:", 4) == 0) { |
0f113f3e | 221 | tmp = getenv(arg + 4); |
2234212c | 222 | if (tmp == NULL) { |
229446df | 223 | BIO_printf(bio_err, "No environment variable %s\n", arg + 4); |
0f113f3e MC |
224 | return NULL; |
225 | } | |
7644a9ae | 226 | return OPENSSL_strdup(tmp); |
0f113f3e | 227 | } |
2234212c | 228 | if (!keepbio || pwdbio == NULL) { |
86885c28 | 229 | if (strncmp(arg, "file:", 5) == 0) { |
0f113f3e | 230 | pwdbio = BIO_new_file(arg + 5, "r"); |
2234212c | 231 | if (pwdbio == NULL) { |
7e1b7485 | 232 | BIO_printf(bio_err, "Can't open file %s\n", arg + 5); |
0f113f3e MC |
233 | return NULL; |
234 | } | |
eff7cb41 | 235 | #if !defined(_WIN32) |
0f113f3e MC |
236 | /* |
237 | * Under _WIN32, which covers even Win64 and CE, file | |
238 | * descriptors referenced by BIO_s_fd are not inherited | |
239 | * by child process and therefore below is not an option. | |
240 | * It could have been an option if bss_fd.c was operating | |
241 | * on real Windows descriptors, such as those obtained | |
242 | * with CreateFile. | |
243 | */ | |
86885c28 | 244 | } else if (strncmp(arg, "fd:", 3) == 0) { |
0f113f3e MC |
245 | BIO *btmp; |
246 | i = atoi(arg + 3); | |
247 | if (i >= 0) | |
248 | pwdbio = BIO_new_fd(i, BIO_NOCLOSE); | |
249 | if ((i < 0) || !pwdbio) { | |
7e1b7485 | 250 | BIO_printf(bio_err, "Can't access file descriptor %s\n", arg + 3); |
0f113f3e MC |
251 | return NULL; |
252 | } | |
253 | /* | |
254 | * Can't do BIO_gets on an fd BIO so add a buffering BIO | |
255 | */ | |
256 | btmp = BIO_new(BIO_f_buffer()); | |
257 | pwdbio = BIO_push(btmp, pwdbio); | |
258 | #endif | |
86885c28 | 259 | } else if (strcmp(arg, "stdin") == 0) { |
a60994df | 260 | pwdbio = dup_bio_in(FORMAT_TEXT); |
0f113f3e | 261 | if (!pwdbio) { |
7e1b7485 | 262 | BIO_printf(bio_err, "Can't open BIO for stdin\n"); |
0f113f3e MC |
263 | return NULL; |
264 | } | |
265 | } else { | |
62ca1565 DO |
266 | /* argument syntax error; do not reveal too much about arg */ |
267 | tmp = strchr(arg, ':'); | |
268 | if (tmp == NULL || tmp - arg > PASS_SOURCE_SIZE_MAX) | |
269 | BIO_printf(bio_err, | |
270 | "Invalid password argument, missing ':' within the first %d chars\n", | |
271 | PASS_SOURCE_SIZE_MAX + 1); | |
272 | else | |
273 | BIO_printf(bio_err, | |
274 | "Invalid password argument, starting with \"%.*s\"\n", | |
275 | (int)(tmp - arg + 1), arg); | |
0f113f3e MC |
276 | return NULL; |
277 | } | |
278 | } | |
279 | i = BIO_gets(pwdbio, tpass, APP_PASS_LEN); | |
280 | if (keepbio != 1) { | |
281 | BIO_free_all(pwdbio); | |
282 | pwdbio = NULL; | |
283 | } | |
284 | if (i <= 0) { | |
7e1b7485 | 285 | BIO_printf(bio_err, "Error reading password from BIO\n"); |
0f113f3e MC |
286 | return NULL; |
287 | } | |
288 | tmp = strchr(tpass, '\n'); | |
2234212c | 289 | if (tmp != NULL) |
0f113f3e | 290 | *tmp = 0; |
7644a9ae | 291 | return OPENSSL_strdup(tpass); |
a3fe382e | 292 | } |
90ae4673 | 293 | |
bfa470a4 | 294 | CONF *app_load_config_bio(BIO *in, const char *filename) |
cc01d217 RS |
295 | { |
296 | long errorline = -1; | |
297 | CONF *conf; | |
298 | int i; | |
cc01d217 RS |
299 | |
300 | conf = NCONF_new(NULL); | |
301 | i = NCONF_load_bio(conf, in, &errorline); | |
cc01d217 RS |
302 | if (i > 0) |
303 | return conf; | |
304 | ||
bfa470a4 RL |
305 | if (errorline <= 0) { |
306 | BIO_printf(bio_err, "%s: Can't load ", opt_getprog()); | |
307 | } else { | |
308 | BIO_printf(bio_err, "%s: Error on line %ld of ", opt_getprog(), | |
309 | errorline); | |
310 | } | |
311 | if (filename != NULL) | |
312 | BIO_printf(bio_err, "config file \"%s\"\n", filename); | |
cc01d217 | 313 | else |
bfa470a4 RL |
314 | BIO_printf(bio_err, "config input"); |
315 | ||
cc01d217 RS |
316 | NCONF_free(conf); |
317 | return NULL; | |
318 | } | |
2234212c | 319 | |
296f54ee RL |
320 | CONF *app_load_config(const char *filename) |
321 | { | |
322 | BIO *in; | |
323 | CONF *conf; | |
324 | ||
bdd58d98 | 325 | in = bio_open_default(filename, 'r', FORMAT_TEXT); |
296f54ee RL |
326 | if (in == NULL) |
327 | return NULL; | |
328 | ||
bfa470a4 | 329 | conf = app_load_config_bio(in, filename); |
296f54ee RL |
330 | BIO_free(in); |
331 | return conf; | |
332 | } | |
2234212c | 333 | |
296f54ee RL |
334 | CONF *app_load_config_quiet(const char *filename) |
335 | { | |
336 | BIO *in; | |
337 | CONF *conf; | |
338 | ||
bdd58d98 | 339 | in = bio_open_default_quiet(filename, 'r', FORMAT_TEXT); |
296f54ee RL |
340 | if (in == NULL) |
341 | return NULL; | |
342 | ||
bfa470a4 | 343 | conf = app_load_config_bio(in, filename); |
296f54ee RL |
344 | BIO_free(in); |
345 | return conf; | |
346 | } | |
347 | ||
348 | int app_load_modules(const CONF *config) | |
349 | { | |
350 | CONF *to_free = NULL; | |
351 | ||
352 | if (config == NULL) | |
dccd20d1 | 353 | config = to_free = app_load_config_quiet(default_config_file); |
296f54ee | 354 | if (config == NULL) |
dccd20d1 | 355 | return 1; |
296f54ee RL |
356 | |
357 | if (CONF_modules_load(config, NULL, 0) <= 0) { | |
358 | BIO_printf(bio_err, "Error configuring OpenSSL modules\n"); | |
359 | ERR_print_errors(bio_err); | |
360 | NCONF_free(to_free); | |
361 | return 0; | |
362 | } | |
363 | NCONF_free(to_free); | |
364 | return 1; | |
365 | } | |
cc01d217 | 366 | |
7e1b7485 | 367 | int add_oid_section(CONF *conf) |
0f113f3e MC |
368 | { |
369 | char *p; | |
370 | STACK_OF(CONF_VALUE) *sktmp; | |
371 | CONF_VALUE *cnf; | |
372 | int i; | |
75ebbd9a RS |
373 | |
374 | if ((p = NCONF_get_string(conf, NULL, "oid_section")) == NULL) { | |
0f113f3e MC |
375 | ERR_clear_error(); |
376 | return 1; | |
377 | } | |
75ebbd9a | 378 | if ((sktmp = NCONF_get_section(conf, p)) == NULL) { |
7e1b7485 | 379 | BIO_printf(bio_err, "problem loading oid section %s\n", p); |
0f113f3e MC |
380 | return 0; |
381 | } | |
382 | for (i = 0; i < sk_CONF_VALUE_num(sktmp); i++) { | |
383 | cnf = sk_CONF_VALUE_value(sktmp, i); | |
384 | if (OBJ_create(cnf->value, cnf->name, cnf->name) == NID_undef) { | |
7e1b7485 | 385 | BIO_printf(bio_err, "problem creating object %s=%s\n", |
0f113f3e MC |
386 | cnf->name, cnf->value); |
387 | return 0; | |
388 | } | |
389 | } | |
390 | return 1; | |
431b0cce RL |
391 | } |
392 | ||
7e1b7485 | 393 | static int load_pkcs12(BIO *in, const char *desc, |
229446df | 394 | pem_password_cb *pem_cb, PW_CB_DATA *cb_data, |
0f113f3e MC |
395 | EVP_PKEY **pkey, X509 **cert, STACK_OF(X509) **ca) |
396 | { | |
397 | const char *pass; | |
398 | char tpass[PEM_BUFSIZE]; | |
399 | int len, ret = 0; | |
400 | PKCS12 *p12; | |
401 | p12 = d2i_PKCS12_bio(in, NULL); | |
402 | if (p12 == NULL) { | |
7e1b7485 | 403 | BIO_printf(bio_err, "Error loading PKCS12 file for %s\n", desc); |
0f113f3e MC |
404 | goto die; |
405 | } | |
406 | /* See if an empty password will do */ | |
2234212c | 407 | if (PKCS12_verify_mac(p12, "", 0) || PKCS12_verify_mac(p12, NULL, 0)) { |
0f113f3e | 408 | pass = ""; |
2234212c | 409 | } else { |
0f113f3e MC |
410 | if (!pem_cb) |
411 | pem_cb = (pem_password_cb *)password_callback; | |
412 | len = pem_cb(tpass, PEM_BUFSIZE, 0, cb_data); | |
413 | if (len < 0) { | |
7e1b7485 | 414 | BIO_printf(bio_err, "Passphrase callback error for %s\n", desc); |
0f113f3e MC |
415 | goto die; |
416 | } | |
417 | if (len < PEM_BUFSIZE) | |
418 | tpass[len] = 0; | |
419 | if (!PKCS12_verify_mac(p12, tpass, len)) { | |
7e1b7485 | 420 | BIO_printf(bio_err, |
0f113f3e MC |
421 | "Mac verify error (wrong password?) in PKCS12 file for %s\n", |
422 | desc); | |
423 | goto die; | |
424 | } | |
425 | pass = tpass; | |
426 | } | |
427 | ret = PKCS12_parse(p12, pass, pkey, cert, ca); | |
428 | die: | |
e0e920b1 | 429 | PKCS12_free(p12); |
0f113f3e MC |
430 | return ret; |
431 | } | |
e90fadda | 432 | |
f9e55034 | 433 | #if !defined(OPENSSL_NO_OCSP) && !defined(OPENSSL_NO_SOCK) |
5d322287 | 434 | static int load_cert_crl_http(const char *url, X509 **pcert, X509_CRL **pcrl) |
0f113f3e MC |
435 | { |
436 | char *host = NULL, *port = NULL, *path = NULL; | |
437 | BIO *bio = NULL; | |
438 | OCSP_REQ_CTX *rctx = NULL; | |
439 | int use_ssl, rv = 0; | |
440 | if (!OCSP_parse_url(url, &host, &port, &path, &use_ssl)) | |
441 | goto err; | |
442 | if (use_ssl) { | |
7e1b7485 | 443 | BIO_puts(bio_err, "https not supported\n"); |
0f113f3e MC |
444 | goto err; |
445 | } | |
446 | bio = BIO_new_connect(host); | |
447 | if (!bio || !BIO_set_conn_port(bio, port)) | |
448 | goto err; | |
449 | rctx = OCSP_REQ_CTX_new(bio, 1024); | |
96487cdd | 450 | if (rctx == NULL) |
0f113f3e MC |
451 | goto err; |
452 | if (!OCSP_REQ_CTX_http(rctx, "GET", path)) | |
453 | goto err; | |
454 | if (!OCSP_REQ_CTX_add1_header(rctx, "Host", host)) | |
455 | goto err; | |
456 | if (pcert) { | |
457 | do { | |
458 | rv = X509_http_nbio(rctx, pcert); | |
7e1b7485 | 459 | } while (rv == -1); |
0f113f3e MC |
460 | } else { |
461 | do { | |
462 | rv = X509_CRL_http_nbio(rctx, pcrl); | |
463 | } while (rv == -1); | |
464 | } | |
465 | ||
466 | err: | |
25aaa98a RS |
467 | OPENSSL_free(host); |
468 | OPENSSL_free(path); | |
469 | OPENSSL_free(port); | |
2234212c | 470 | BIO_free_all(bio); |
895cba19 | 471 | OCSP_REQ_CTX_free(rctx); |
0f113f3e | 472 | if (rv != 1) { |
7e1b7485 RS |
473 | BIO_printf(bio_err, "Error loading %s from %s\n", |
474 | pcert ? "certificate" : "CRL", url); | |
0f113f3e MC |
475 | ERR_print_errors(bio_err); |
476 | } | |
477 | return rv; | |
478 | } | |
5d322287 | 479 | #endif |
95ea5318 | 480 | |
a773b52a | 481 | X509 *load_cert(const char *file, int format, const char *cert_descrip) |
0f113f3e MC |
482 | { |
483 | X509 *x = NULL; | |
484 | BIO *cert; | |
485 | ||
486 | if (format == FORMAT_HTTP) { | |
f9e55034 | 487 | #if !defined(OPENSSL_NO_OCSP) && !defined(OPENSSL_NO_SOCK) |
7e1b7485 | 488 | load_cert_crl_http(file, &x, NULL); |
5d322287 | 489 | #endif |
0f113f3e MC |
490 | return x; |
491 | } | |
492 | ||
0f113f3e | 493 | if (file == NULL) { |
7e1b7485 | 494 | unbuffer(stdin); |
a60994df | 495 | cert = dup_bio_in(format); |
2234212c | 496 | } else { |
bdd58d98 | 497 | cert = bio_open_default(file, 'r', format); |
2234212c | 498 | } |
7e1b7485 RS |
499 | if (cert == NULL) |
500 | goto end; | |
0f113f3e | 501 | |
2234212c | 502 | if (format == FORMAT_ASN1) { |
0f113f3e | 503 | x = d2i_X509_bio(cert, NULL); |
2234212c | 504 | } else if (format == FORMAT_PEM) { |
0f113f3e MC |
505 | x = PEM_read_bio_X509_AUX(cert, NULL, |
506 | (pem_password_cb *)password_callback, NULL); | |
2234212c | 507 | } else if (format == FORMAT_PKCS12) { |
7e1b7485 | 508 | if (!load_pkcs12(cert, cert_descrip, NULL, NULL, NULL, &x, NULL)) |
0f113f3e MC |
509 | goto end; |
510 | } else { | |
7e1b7485 | 511 | BIO_printf(bio_err, "bad input format specified for %s\n", cert_descrip); |
0f113f3e MC |
512 | goto end; |
513 | } | |
514 | end: | |
515 | if (x == NULL) { | |
7e1b7485 RS |
516 | BIO_printf(bio_err, "unable to load certificate\n"); |
517 | ERR_print_errors(bio_err); | |
0f113f3e | 518 | } |
25aaa98a | 519 | BIO_free(cert); |
26a7d938 | 520 | return x; |
0f113f3e | 521 | } |
90ae4673 | 522 | |
0090a686 | 523 | X509_CRL *load_crl(const char *infile, int format) |
0f113f3e MC |
524 | { |
525 | X509_CRL *x = NULL; | |
526 | BIO *in = NULL; | |
527 | ||
528 | if (format == FORMAT_HTTP) { | |
f9e55034 | 529 | #if !defined(OPENSSL_NO_OCSP) && !defined(OPENSSL_NO_SOCK) |
7e1b7485 | 530 | load_cert_crl_http(infile, NULL, &x); |
5d322287 | 531 | #endif |
0f113f3e MC |
532 | return x; |
533 | } | |
534 | ||
bdd58d98 | 535 | in = bio_open_default(infile, 'r', format); |
7e1b7485 | 536 | if (in == NULL) |
0f113f3e | 537 | goto end; |
2234212c | 538 | if (format == FORMAT_ASN1) { |
0f113f3e | 539 | x = d2i_X509_CRL_bio(in, NULL); |
2234212c | 540 | } else if (format == FORMAT_PEM) { |
0f113f3e | 541 | x = PEM_read_bio_X509_CRL(in, NULL, NULL, NULL); |
2234212c | 542 | } else { |
0f113f3e MC |
543 | BIO_printf(bio_err, "bad input format specified for input crl\n"); |
544 | goto end; | |
545 | } | |
546 | if (x == NULL) { | |
547 | BIO_printf(bio_err, "unable to load CRL\n"); | |
548 | ERR_print_errors(bio_err); | |
549 | goto end; | |
550 | } | |
fdb78f3d | 551 | |
0f113f3e MC |
552 | end: |
553 | BIO_free(in); | |
26a7d938 | 554 | return x; |
0f113f3e | 555 | } |
fdb78f3d | 556 | |
7e1b7485 | 557 | EVP_PKEY *load_key(const char *file, int format, int maybe_stdin, |
0f113f3e MC |
558 | const char *pass, ENGINE *e, const char *key_descrip) |
559 | { | |
560 | BIO *key = NULL; | |
561 | EVP_PKEY *pkey = NULL; | |
562 | PW_CB_DATA cb_data; | |
563 | ||
564 | cb_data.password = pass; | |
565 | cb_data.prompt_info = file; | |
566 | ||
567 | if (file == NULL && (!maybe_stdin || format == FORMAT_ENGINE)) { | |
7e1b7485 | 568 | BIO_printf(bio_err, "no keyfile specified\n"); |
0f113f3e MC |
569 | goto end; |
570 | } | |
0f113f3e | 571 | if (format == FORMAT_ENGINE) { |
2234212c | 572 | if (e == NULL) { |
7e1b7485 | 573 | BIO_printf(bio_err, "no engine specified\n"); |
2234212c | 574 | } else { |
0c20802c | 575 | #ifndef OPENSSL_NO_ENGINE |
49e476a5 | 576 | if (ENGINE_init(e)) { |
a43ce58f SL |
577 | pkey = ENGINE_load_private_key(e, file, |
578 | (UI_METHOD *)get_ui_method(), | |
579 | &cb_data); | |
49e476a5 RL |
580 | ENGINE_finish(e); |
581 | } | |
0c20802c | 582 | if (pkey == NULL) { |
7e1b7485 RS |
583 | BIO_printf(bio_err, "cannot load %s from engine\n", key_descrip); |
584 | ERR_print_errors(bio_err); | |
0f113f3e | 585 | } |
0c20802c VD |
586 | #else |
587 | BIO_printf(bio_err, "engines not supported\n"); | |
588 | #endif | |
0f113f3e MC |
589 | } |
590 | goto end; | |
591 | } | |
0f113f3e | 592 | if (file == NULL && maybe_stdin) { |
7e1b7485 | 593 | unbuffer(stdin); |
a60994df | 594 | key = dup_bio_in(format); |
2234212c | 595 | } else { |
bdd58d98 | 596 | key = bio_open_default(file, 'r', format); |
2234212c | 597 | } |
7e1b7485 | 598 | if (key == NULL) |
0f113f3e | 599 | goto end; |
0f113f3e MC |
600 | if (format == FORMAT_ASN1) { |
601 | pkey = d2i_PrivateKey_bio(key, NULL); | |
602 | } else if (format == FORMAT_PEM) { | |
229446df | 603 | pkey = PEM_read_bio_PrivateKey(key, NULL, wrap_password_callback, &cb_data); |
2234212c | 604 | } else if (format == FORMAT_PKCS12) { |
229446df | 605 | if (!load_pkcs12(key, key_descrip, wrap_password_callback, &cb_data, |
0f113f3e MC |
606 | &pkey, NULL, NULL)) |
607 | goto end; | |
00a37b5a | 608 | #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DSA) && !defined (OPENSSL_NO_RC4) |
2234212c | 609 | } else if (format == FORMAT_MSBLOB) { |
0f113f3e | 610 | pkey = b2i_PrivateKey_bio(key); |
2234212c | 611 | } else if (format == FORMAT_PVK) { |
229446df | 612 | pkey = b2i_PVK_bio(key, wrap_password_callback, &cb_data); |
0f113f3e | 613 | #endif |
2234212c | 614 | } else { |
7e1b7485 | 615 | BIO_printf(bio_err, "bad input format specified for key file\n"); |
0f113f3e MC |
616 | goto end; |
617 | } | |
90ae4673 | 618 | end: |
25aaa98a | 619 | BIO_free(key); |
0f113f3e | 620 | if (pkey == NULL) { |
7e1b7485 RS |
621 | BIO_printf(bio_err, "unable to load %s\n", key_descrip); |
622 | ERR_print_errors(bio_err); | |
0f113f3e | 623 | } |
26a7d938 | 624 | return pkey; |
0f113f3e | 625 | } |
90ae4673 | 626 | |
7e1b7485 | 627 | EVP_PKEY *load_pubkey(const char *file, int format, int maybe_stdin, |
0f113f3e MC |
628 | const char *pass, ENGINE *e, const char *key_descrip) |
629 | { | |
630 | BIO *key = NULL; | |
631 | EVP_PKEY *pkey = NULL; | |
632 | PW_CB_DATA cb_data; | |
633 | ||
634 | cb_data.password = pass; | |
635 | cb_data.prompt_info = file; | |
636 | ||
637 | if (file == NULL && (!maybe_stdin || format == FORMAT_ENGINE)) { | |
7e1b7485 | 638 | BIO_printf(bio_err, "no keyfile specified\n"); |
0f113f3e MC |
639 | goto end; |
640 | } | |
0f113f3e | 641 | if (format == FORMAT_ENGINE) { |
2234212c | 642 | if (e == NULL) { |
0f113f3e | 643 | BIO_printf(bio_err, "no engine specified\n"); |
2234212c | 644 | } else { |
0c20802c | 645 | #ifndef OPENSSL_NO_ENGINE |
a43ce58f SL |
646 | pkey = ENGINE_load_public_key(e, file, (UI_METHOD *)get_ui_method(), |
647 | &cb_data); | |
0c20802c VD |
648 | if (pkey == NULL) { |
649 | BIO_printf(bio_err, "cannot load %s from engine\n", key_descrip); | |
650 | ERR_print_errors(bio_err); | |
651 | } | |
652 | #else | |
653 | BIO_printf(bio_err, "engines not supported\n"); | |
654 | #endif | |
655 | } | |
0f113f3e MC |
656 | goto end; |
657 | } | |
0f113f3e | 658 | if (file == NULL && maybe_stdin) { |
7e1b7485 | 659 | unbuffer(stdin); |
a60994df | 660 | key = dup_bio_in(format); |
2234212c | 661 | } else { |
bdd58d98 | 662 | key = bio_open_default(file, 'r', format); |
2234212c | 663 | } |
7e1b7485 | 664 | if (key == NULL) |
0f113f3e | 665 | goto end; |
0f113f3e MC |
666 | if (format == FORMAT_ASN1) { |
667 | pkey = d2i_PUBKEY_bio(key, NULL); | |
2234212c | 668 | } else if (format == FORMAT_ASN1RSA) { |
0c20802c | 669 | #ifndef OPENSSL_NO_RSA |
0f113f3e MC |
670 | RSA *rsa; |
671 | rsa = d2i_RSAPublicKey_bio(key, NULL); | |
672 | if (rsa) { | |
673 | pkey = EVP_PKEY_new(); | |
96487cdd | 674 | if (pkey != NULL) |
0f113f3e MC |
675 | EVP_PKEY_set1_RSA(pkey, rsa); |
676 | RSA_free(rsa); | |
677 | } else | |
0c20802c VD |
678 | #else |
679 | BIO_printf(bio_err, "RSA keys not supported\n"); | |
680 | #endif | |
0f113f3e MC |
681 | pkey = NULL; |
682 | } else if (format == FORMAT_PEMRSA) { | |
0c20802c | 683 | #ifndef OPENSSL_NO_RSA |
0f113f3e MC |
684 | RSA *rsa; |
685 | rsa = PEM_read_bio_RSAPublicKey(key, NULL, | |
686 | (pem_password_cb *)password_callback, | |
687 | &cb_data); | |
96487cdd | 688 | if (rsa != NULL) { |
0f113f3e | 689 | pkey = EVP_PKEY_new(); |
96487cdd | 690 | if (pkey != NULL) |
0f113f3e MC |
691 | EVP_PKEY_set1_RSA(pkey, rsa); |
692 | RSA_free(rsa); | |
693 | } else | |
0c20802c VD |
694 | #else |
695 | BIO_printf(bio_err, "RSA keys not supported\n"); | |
696 | #endif | |
0f113f3e | 697 | pkey = NULL; |
2234212c | 698 | } else if (format == FORMAT_PEM) { |
0f113f3e MC |
699 | pkey = PEM_read_bio_PUBKEY(key, NULL, |
700 | (pem_password_cb *)password_callback, | |
701 | &cb_data); | |
d4f0339c | 702 | #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DSA) |
2234212c | 703 | } else if (format == FORMAT_MSBLOB) { |
0f113f3e | 704 | pkey = b2i_PublicKey_bio(key); |
d4f0339c | 705 | #endif |
2234212c | 706 | } |
bd08a2bd | 707 | end: |
25aaa98a | 708 | BIO_free(key); |
0f113f3e | 709 | if (pkey == NULL) |
7e1b7485 | 710 | BIO_printf(bio_err, "unable to load %s\n", key_descrip); |
26a7d938 | 711 | return pkey; |
0f113f3e | 712 | } |
bd08a2bd | 713 | |
7e1b7485 | 714 | static int load_certs_crls(const char *file, int format, |
a773b52a | 715 | const char *pass, const char *desc, |
0f113f3e MC |
716 | STACK_OF(X509) **pcerts, |
717 | STACK_OF(X509_CRL) **pcrls) | |
718 | { | |
719 | int i; | |
720 | BIO *bio; | |
721 | STACK_OF(X509_INFO) *xis = NULL; | |
722 | X509_INFO *xi; | |
723 | PW_CB_DATA cb_data; | |
724 | int rv = 0; | |
725 | ||
726 | cb_data.password = pass; | |
727 | cb_data.prompt_info = file; | |
728 | ||
729 | if (format != FORMAT_PEM) { | |
7e1b7485 | 730 | BIO_printf(bio_err, "bad input format specified for %s\n", desc); |
0f113f3e MC |
731 | return 0; |
732 | } | |
733 | ||
bdd58d98 | 734 | bio = bio_open_default(file, 'r', FORMAT_PEM); |
7e1b7485 | 735 | if (bio == NULL) |
0f113f3e | 736 | return 0; |
0f113f3e MC |
737 | |
738 | xis = PEM_X509_INFO_read_bio(bio, NULL, | |
739 | (pem_password_cb *)password_callback, | |
740 | &cb_data); | |
741 | ||
742 | BIO_free(bio); | |
743 | ||
2234212c | 744 | if (pcerts != NULL && *pcerts == NULL) { |
0f113f3e | 745 | *pcerts = sk_X509_new_null(); |
2234212c | 746 | if (*pcerts == NULL) |
0f113f3e MC |
747 | goto end; |
748 | } | |
749 | ||
2234212c | 750 | if (pcrls != NULL && *pcrls == NULL) { |
0f113f3e | 751 | *pcrls = sk_X509_CRL_new_null(); |
2234212c | 752 | if (*pcrls == NULL) |
0f113f3e MC |
753 | goto end; |
754 | } | |
755 | ||
756 | for (i = 0; i < sk_X509_INFO_num(xis); i++) { | |
757 | xi = sk_X509_INFO_value(xis, i); | |
2234212c | 758 | if (xi->x509 != NULL && pcerts != NULL) { |
0f113f3e MC |
759 | if (!sk_X509_push(*pcerts, xi->x509)) |
760 | goto end; | |
761 | xi->x509 = NULL; | |
762 | } | |
2234212c | 763 | if (xi->crl != NULL && pcrls != NULL) { |
0f113f3e MC |
764 | if (!sk_X509_CRL_push(*pcrls, xi->crl)) |
765 | goto end; | |
766 | xi->crl = NULL; | |
767 | } | |
768 | } | |
769 | ||
2234212c | 770 | if (pcerts != NULL && sk_X509_num(*pcerts) > 0) |
0f113f3e MC |
771 | rv = 1; |
772 | ||
2234212c | 773 | if (pcrls != NULL && sk_X509_CRL_num(*pcrls) > 0) |
0f113f3e MC |
774 | rv = 1; |
775 | ||
776 | end: | |
777 | ||
222561fe | 778 | sk_X509_INFO_pop_free(xis, X509_INFO_free); |
0f113f3e MC |
779 | |
780 | if (rv == 0) { | |
2234212c | 781 | if (pcerts != NULL) { |
0f113f3e MC |
782 | sk_X509_pop_free(*pcerts, X509_free); |
783 | *pcerts = NULL; | |
784 | } | |
2234212c | 785 | if (pcrls != NULL) { |
0f113f3e MC |
786 | sk_X509_CRL_pop_free(*pcrls, X509_CRL_free); |
787 | *pcrls = NULL; | |
788 | } | |
7e1b7485 | 789 | BIO_printf(bio_err, "unable to load %s\n", |
0f113f3e | 790 | pcerts ? "certificates" : "CRLs"); |
7e1b7485 | 791 | ERR_print_errors(bio_err); |
0f113f3e MC |
792 | } |
793 | return rv; | |
794 | } | |
90ae4673 | 795 | |
68dc6824 RS |
796 | void* app_malloc(int sz, const char *what) |
797 | { | |
798 | void *vp = OPENSSL_malloc(sz); | |
799 | ||
800 | if (vp == NULL) { | |
801 | BIO_printf(bio_err, "%s: Could not allocate %d bytes for %s\n", | |
802 | opt_getprog(), sz, what); | |
803 | ERR_print_errors(bio_err); | |
804 | exit(1); | |
805 | } | |
806 | return vp; | |
807 | } | |
808 | ||
0996dc54 | 809 | /* |
6b4a77f5 | 810 | * Initialize or extend, if *certs != NULL, a certificate stack. |
0996dc54 VD |
811 | */ |
812 | int load_certs(const char *file, STACK_OF(X509) **certs, int format, | |
a773b52a | 813 | const char *pass, const char *desc) |
0f113f3e | 814 | { |
a773b52a | 815 | return load_certs_crls(file, format, pass, desc, certs, NULL); |
0f113f3e | 816 | } |
245d2ee3 | 817 | |
0996dc54 | 818 | /* |
6b4a77f5 | 819 | * Initialize or extend, if *crls != NULL, a certificate stack. |
0996dc54 VD |
820 | */ |
821 | int load_crls(const char *file, STACK_OF(X509_CRL) **crls, int format, | |
a773b52a | 822 | const char *pass, const char *desc) |
0f113f3e | 823 | { |
a773b52a | 824 | return load_certs_crls(file, format, pass, desc, NULL, crls); |
0f113f3e MC |
825 | } |
826 | ||
827 | #define X509V3_EXT_UNKNOWN_MASK (0xfL << 16) | |
8ca533e3 | 828 | /* Return error for unknown extensions */ |
0f113f3e | 829 | #define X509V3_EXT_DEFAULT 0 |
8ca533e3 | 830 | /* Print error for unknown extensions */ |
0f113f3e | 831 | #define X509V3_EXT_ERROR_UNKNOWN (1L << 16) |
8ca533e3 | 832 | /* ASN1 parse unknown extensions */ |
0f113f3e | 833 | #define X509V3_EXT_PARSE_UNKNOWN (2L << 16) |
8ca533e3 | 834 | /* BIO_dump unknown extensions */ |
0f113f3e | 835 | #define X509V3_EXT_DUMP_UNKNOWN (3L << 16) |
8ca533e3 | 836 | |
535d79da | 837 | #define X509_FLAG_CA (X509_FLAG_NO_ISSUER | X509_FLAG_NO_PUBKEY | \ |
0f113f3e | 838 | X509_FLAG_NO_HEADER | X509_FLAG_NO_VERSION) |
535d79da | 839 | |
8ca533e3 DSH |
840 | int set_cert_ex(unsigned long *flags, const char *arg) |
841 | { | |
0f113f3e MC |
842 | static const NAME_EX_TBL cert_tbl[] = { |
843 | {"compatible", X509_FLAG_COMPAT, 0xffffffffl}, | |
844 | {"ca_default", X509_FLAG_CA, 0xffffffffl}, | |
845 | {"no_header", X509_FLAG_NO_HEADER, 0}, | |
846 | {"no_version", X509_FLAG_NO_VERSION, 0}, | |
847 | {"no_serial", X509_FLAG_NO_SERIAL, 0}, | |
848 | {"no_signame", X509_FLAG_NO_SIGNAME, 0}, | |
849 | {"no_validity", X509_FLAG_NO_VALIDITY, 0}, | |
850 | {"no_subject", X509_FLAG_NO_SUBJECT, 0}, | |
851 | {"no_issuer", X509_FLAG_NO_ISSUER, 0}, | |
852 | {"no_pubkey", X509_FLAG_NO_PUBKEY, 0}, | |
853 | {"no_extensions", X509_FLAG_NO_EXTENSIONS, 0}, | |
854 | {"no_sigdump", X509_FLAG_NO_SIGDUMP, 0}, | |
855 | {"no_aux", X509_FLAG_NO_AUX, 0}, | |
856 | {"no_attributes", X509_FLAG_NO_ATTRIBUTES, 0}, | |
857 | {"ext_default", X509V3_EXT_DEFAULT, X509V3_EXT_UNKNOWN_MASK}, | |
858 | {"ext_error", X509V3_EXT_ERROR_UNKNOWN, X509V3_EXT_UNKNOWN_MASK}, | |
859 | {"ext_parse", X509V3_EXT_PARSE_UNKNOWN, X509V3_EXT_UNKNOWN_MASK}, | |
860 | {"ext_dump", X509V3_EXT_DUMP_UNKNOWN, X509V3_EXT_UNKNOWN_MASK}, | |
861 | {NULL, 0, 0} | |
862 | }; | |
863 | return set_multi_opts(flags, arg, cert_tbl); | |
8ca533e3 | 864 | } |
a657546f DSH |
865 | |
866 | int set_name_ex(unsigned long *flags, const char *arg) | |
867 | { | |
0f113f3e MC |
868 | static const NAME_EX_TBL ex_tbl[] = { |
869 | {"esc_2253", ASN1_STRFLGS_ESC_2253, 0}, | |
bc776510 | 870 | {"esc_2254", ASN1_STRFLGS_ESC_2254, 0}, |
0f113f3e MC |
871 | {"esc_ctrl", ASN1_STRFLGS_ESC_CTRL, 0}, |
872 | {"esc_msb", ASN1_STRFLGS_ESC_MSB, 0}, | |
873 | {"use_quote", ASN1_STRFLGS_ESC_QUOTE, 0}, | |
874 | {"utf8", ASN1_STRFLGS_UTF8_CONVERT, 0}, | |
875 | {"ignore_type", ASN1_STRFLGS_IGNORE_TYPE, 0}, | |
876 | {"show_type", ASN1_STRFLGS_SHOW_TYPE, 0}, | |
877 | {"dump_all", ASN1_STRFLGS_DUMP_ALL, 0}, | |
878 | {"dump_nostr", ASN1_STRFLGS_DUMP_UNKNOWN, 0}, | |
879 | {"dump_der", ASN1_STRFLGS_DUMP_DER, 0}, | |
880 | {"compat", XN_FLAG_COMPAT, 0xffffffffL}, | |
881 | {"sep_comma_plus", XN_FLAG_SEP_COMMA_PLUS, XN_FLAG_SEP_MASK}, | |
882 | {"sep_comma_plus_space", XN_FLAG_SEP_CPLUS_SPC, XN_FLAG_SEP_MASK}, | |
883 | {"sep_semi_plus_space", XN_FLAG_SEP_SPLUS_SPC, XN_FLAG_SEP_MASK}, | |
884 | {"sep_multiline", XN_FLAG_SEP_MULTILINE, XN_FLAG_SEP_MASK}, | |
885 | {"dn_rev", XN_FLAG_DN_REV, 0}, | |
886 | {"nofname", XN_FLAG_FN_NONE, XN_FLAG_FN_MASK}, | |
887 | {"sname", XN_FLAG_FN_SN, XN_FLAG_FN_MASK}, | |
888 | {"lname", XN_FLAG_FN_LN, XN_FLAG_FN_MASK}, | |
889 | {"align", XN_FLAG_FN_ALIGN, 0}, | |
890 | {"oid", XN_FLAG_FN_OID, XN_FLAG_FN_MASK}, | |
891 | {"space_eq", XN_FLAG_SPC_EQ, 0}, | |
892 | {"dump_unknown", XN_FLAG_DUMP_UNKNOWN_FIELDS, 0}, | |
893 | {"RFC2253", XN_FLAG_RFC2253, 0xffffffffL}, | |
894 | {"oneline", XN_FLAG_ONELINE, 0xffffffffL}, | |
895 | {"multiline", XN_FLAG_MULTILINE, 0xffffffffL}, | |
896 | {"ca_default", XN_FLAG_MULTILINE, 0xffffffffL}, | |
897 | {NULL, 0, 0} | |
898 | }; | |
03706afa DSH |
899 | if (set_multi_opts(flags, arg, ex_tbl) == 0) |
900 | return 0; | |
3190d1dc RL |
901 | if (*flags != XN_FLAG_COMPAT |
902 | && (*flags & XN_FLAG_SEP_MASK) == 0) | |
03706afa DSH |
903 | *flags |= XN_FLAG_SEP_CPLUS_SPC; |
904 | return 1; | |
535d79da DSH |
905 | } |
906 | ||
791bd0cd DSH |
907 | int set_ext_copy(int *copy_type, const char *arg) |
908 | { | |
86885c28 | 909 | if (strcasecmp(arg, "none") == 0) |
0f113f3e | 910 | *copy_type = EXT_COPY_NONE; |
86885c28 | 911 | else if (strcasecmp(arg, "copy") == 0) |
0f113f3e | 912 | *copy_type = EXT_COPY_ADD; |
86885c28 | 913 | else if (strcasecmp(arg, "copyall") == 0) |
0f113f3e MC |
914 | *copy_type = EXT_COPY_ALL; |
915 | else | |
916 | return 0; | |
917 | return 1; | |
791bd0cd DSH |
918 | } |
919 | ||
920 | int copy_extensions(X509 *x, X509_REQ *req, int copy_type) | |
921 | { | |
0f113f3e MC |
922 | STACK_OF(X509_EXTENSION) *exts = NULL; |
923 | X509_EXTENSION *ext, *tmpext; | |
924 | ASN1_OBJECT *obj; | |
925 | int i, idx, ret = 0; | |
926 | if (!x || !req || (copy_type == EXT_COPY_NONE)) | |
927 | return 1; | |
928 | exts = X509_REQ_get_extensions(req); | |
929 | ||
930 | for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) { | |
931 | ext = sk_X509_EXTENSION_value(exts, i); | |
932 | obj = X509_EXTENSION_get_object(ext); | |
933 | idx = X509_get_ext_by_OBJ(x, obj, -1); | |
934 | /* Does extension exist? */ | |
935 | if (idx != -1) { | |
936 | /* If normal copy don't override existing extension */ | |
937 | if (copy_type == EXT_COPY_ADD) | |
938 | continue; | |
939 | /* Delete all extensions of same type */ | |
940 | do { | |
941 | tmpext = X509_get_ext(x, idx); | |
942 | X509_delete_ext(x, idx); | |
943 | X509_EXTENSION_free(tmpext); | |
944 | idx = X509_get_ext_by_OBJ(x, obj, -1); | |
945 | } while (idx != -1); | |
946 | } | |
947 | if (!X509_add_ext(x, ext, -1)) | |
948 | goto end; | |
949 | } | |
950 | ||
951 | ret = 1; | |
952 | ||
953 | end: | |
954 | ||
955 | sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free); | |
956 | ||
957 | return ret; | |
958 | } | |
959 | ||
960 | static int set_multi_opts(unsigned long *flags, const char *arg, | |
961 | const NAME_EX_TBL * in_tbl) | |
962 | { | |
963 | STACK_OF(CONF_VALUE) *vals; | |
964 | CONF_VALUE *val; | |
965 | int i, ret = 1; | |
966 | if (!arg) | |
967 | return 0; | |
968 | vals = X509V3_parse_list(arg); | |
969 | for (i = 0; i < sk_CONF_VALUE_num(vals); i++) { | |
970 | val = sk_CONF_VALUE_value(vals, i); | |
971 | if (!set_table_opts(flags, val->name, in_tbl)) | |
972 | ret = 0; | |
973 | } | |
974 | sk_CONF_VALUE_pop_free(vals, X509V3_conf_free); | |
975 | return ret; | |
976 | } | |
977 | ||
978 | static int set_table_opts(unsigned long *flags, const char *arg, | |
979 | const NAME_EX_TBL * in_tbl) | |
980 | { | |
981 | char c; | |
982 | const NAME_EX_TBL *ptbl; | |
983 | c = arg[0]; | |
984 | ||
985 | if (c == '-') { | |
986 | c = 0; | |
987 | arg++; | |
988 | } else if (c == '+') { | |
989 | c = 1; | |
990 | arg++; | |
2234212c | 991 | } else { |
0f113f3e | 992 | c = 1; |
2234212c | 993 | } |
0f113f3e MC |
994 | |
995 | for (ptbl = in_tbl; ptbl->name; ptbl++) { | |
86885c28 | 996 | if (strcasecmp(arg, ptbl->name) == 0) { |
0f113f3e MC |
997 | *flags &= ~ptbl->mask; |
998 | if (c) | |
999 | *flags |= ptbl->flag; | |
1000 | else | |
1001 | *flags &= ~ptbl->flag; | |
1002 | return 1; | |
1003 | } | |
1004 | } | |
1005 | return 0; | |
1006 | } | |
1007 | ||
1008 | void print_name(BIO *out, const char *title, X509_NAME *nm, | |
1009 | unsigned long lflags) | |
1010 | { | |
1011 | char *buf; | |
1012 | char mline = 0; | |
1013 | int indent = 0; | |
1014 | ||
1015 | if (title) | |
1016 | BIO_puts(out, title); | |
1017 | if ((lflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) { | |
1018 | mline = 1; | |
1019 | indent = 4; | |
1020 | } | |
1021 | if (lflags == XN_FLAG_COMPAT) { | |
1022 | buf = X509_NAME_oneline(nm, 0, 0); | |
1023 | BIO_puts(out, buf); | |
1024 | BIO_puts(out, "\n"); | |
1025 | OPENSSL_free(buf); | |
1026 | } else { | |
1027 | if (mline) | |
1028 | BIO_puts(out, "\n"); | |
1029 | X509_NAME_print_ex(out, nm, indent, lflags); | |
1030 | BIO_puts(out, "\n"); | |
1031 | } | |
a657546f DSH |
1032 | } |
1033 | ||
2ac6115d | 1034 | void print_bignum_var(BIO *out, const BIGNUM *in, const char *var, |
7e1b7485 | 1035 | int len, unsigned char *buffer) |
81f169e9 | 1036 | { |
7e1b7485 | 1037 | BIO_printf(out, " static unsigned char %s_%d[] = {", var, len); |
2234212c | 1038 | if (BN_is_zero(in)) { |
06deb932 | 1039 | BIO_printf(out, "\n 0x00"); |
2234212c | 1040 | } else { |
7e1b7485 RS |
1041 | int i, l; |
1042 | ||
1043 | l = BN_bn2bin(in, buffer); | |
1044 | for (i = 0; i < l; i++) { | |
06deb932 | 1045 | BIO_printf(out, (i % 10) == 0 ? "\n " : " "); |
7e1b7485 | 1046 | if (i < l - 1) |
06deb932 | 1047 | BIO_printf(out, "0x%02X,", buffer[i]); |
7e1b7485 RS |
1048 | else |
1049 | BIO_printf(out, "0x%02X", buffer[i]); | |
1050 | } | |
1051 | } | |
1052 | BIO_printf(out, "\n };\n"); | |
1053 | } | |
2234212c | 1054 | |
7e1b7485 RS |
1055 | void print_array(BIO *out, const char* title, int len, const unsigned char* d) |
1056 | { | |
1057 | int i; | |
1058 | ||
1059 | BIO_printf(out, "unsigned char %s[%d] = {", title, len); | |
1060 | for (i = 0; i < len; i++) { | |
1061 | if ((i % 10) == 0) | |
1062 | BIO_printf(out, "\n "); | |
1063 | if (i < len - 1) | |
1064 | BIO_printf(out, "0x%02X, ", d[i]); | |
1065 | else | |
1066 | BIO_printf(out, "0x%02X", d[i]); | |
1067 | } | |
1068 | BIO_printf(out, "\n};\n"); | |
1069 | } | |
1070 | ||
cc696296 | 1071 | X509_STORE *setup_verify(const char *CAfile, const char *CApath, int noCAfile, int noCApath) |
7e1b7485 RS |
1072 | { |
1073 | X509_STORE *store = X509_STORE_new(); | |
0f113f3e | 1074 | X509_LOOKUP *lookup; |
7e1b7485 | 1075 | |
96487cdd | 1076 | if (store == NULL) |
0f113f3e | 1077 | goto end; |
2b6bcb70 | 1078 | |
e8aa8b6c | 1079 | if (CAfile != NULL || !noCAfile) { |
2b6bcb70 MC |
1080 | lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()); |
1081 | if (lookup == NULL) | |
0f113f3e | 1082 | goto end; |
2b6bcb70 MC |
1083 | if (CAfile) { |
1084 | if (!X509_LOOKUP_load_file(lookup, CAfile, X509_FILETYPE_PEM)) { | |
1085 | BIO_printf(bio_err, "Error loading file %s\n", CAfile); | |
1086 | goto end; | |
1087 | } | |
2234212c | 1088 | } else { |
2b6bcb70 | 1089 | X509_LOOKUP_load_file(lookup, NULL, X509_FILETYPE_DEFAULT); |
2234212c | 1090 | } |
2b6bcb70 | 1091 | } |
0f113f3e | 1092 | |
e8aa8b6c | 1093 | if (CApath != NULL || !noCApath) { |
2b6bcb70 MC |
1094 | lookup = X509_STORE_add_lookup(store, X509_LOOKUP_hash_dir()); |
1095 | if (lookup == NULL) | |
0f113f3e | 1096 | goto end; |
2b6bcb70 MC |
1097 | if (CApath) { |
1098 | if (!X509_LOOKUP_add_dir(lookup, CApath, X509_FILETYPE_PEM)) { | |
1099 | BIO_printf(bio_err, "Error loading directory %s\n", CApath); | |
1100 | goto end; | |
1101 | } | |
2234212c | 1102 | } else { |
2b6bcb70 | 1103 | X509_LOOKUP_add_dir(lookup, NULL, X509_FILETYPE_DEFAULT); |
2234212c | 1104 | } |
2b6bcb70 | 1105 | } |
0f113f3e MC |
1106 | |
1107 | ERR_clear_error(); | |
1108 | return store; | |
1109 | end: | |
1110 | X509_STORE_free(store); | |
1111 | return NULL; | |
81f169e9 | 1112 | } |
531d630b | 1113 | |
0b13e9f0 | 1114 | #ifndef OPENSSL_NO_ENGINE |
e1a00d7d | 1115 | /* Try to load an engine in a shareable library */ |
a773b52a | 1116 | static ENGINE *try_load_engine(const char *engine) |
0f113f3e MC |
1117 | { |
1118 | ENGINE *e = ENGINE_by_id("dynamic"); | |
1119 | if (e) { | |
1120 | if (!ENGINE_ctrl_cmd_string(e, "SO_PATH", engine, 0) | |
1121 | || !ENGINE_ctrl_cmd_string(e, "LOAD", NULL, 0)) { | |
1122 | ENGINE_free(e); | |
1123 | e = NULL; | |
1124 | } | |
1125 | } | |
1126 | return e; | |
1127 | } | |
907c6c86 | 1128 | #endif |
e1a00d7d | 1129 | |
7e1b7485 | 1130 | ENGINE *setup_engine(const char *engine, int debug) |
0f113f3e MC |
1131 | { |
1132 | ENGINE *e = NULL; | |
1133 | ||
907c6c86 | 1134 | #ifndef OPENSSL_NO_ENGINE |
2234212c | 1135 | if (engine != NULL) { |
0f113f3e | 1136 | if (strcmp(engine, "auto") == 0) { |
7e1b7485 | 1137 | BIO_printf(bio_err, "enabling auto ENGINE support\n"); |
0f113f3e MC |
1138 | ENGINE_register_all_complete(); |
1139 | return NULL; | |
1140 | } | |
1141 | if ((e = ENGINE_by_id(engine)) == NULL | |
a773b52a | 1142 | && (e = try_load_engine(engine)) == NULL) { |
7e1b7485 RS |
1143 | BIO_printf(bio_err, "invalid engine \"%s\"\n", engine); |
1144 | ERR_print_errors(bio_err); | |
0f113f3e MC |
1145 | return NULL; |
1146 | } | |
1147 | if (debug) { | |
7e1b7485 | 1148 | ENGINE_ctrl(e, ENGINE_CTRL_SET_LOGSTREAM, 0, bio_err, 0); |
531d630b | 1149 | } |
a43ce58f SL |
1150 | ENGINE_ctrl_cmd(e, "SET_USER_INTERFACE", 0, (void *)get_ui_method(), |
1151 | 0, 1); | |
56e36bda | 1152 | if (!ENGINE_set_default(e, ENGINE_METHOD_ALL)) { |
7e1b7485 RS |
1153 | BIO_printf(bio_err, "can't use that engine\n"); |
1154 | ERR_print_errors(bio_err); | |
0f113f3e MC |
1155 | ENGINE_free(e); |
1156 | return NULL; | |
1157 | } | |
1158 | ||
7e1b7485 | 1159 | BIO_printf(bio_err, "engine \"%s\" set.\n", ENGINE_get_id(e)); |
0f113f3e | 1160 | } |
907c6c86 | 1161 | #endif |
0f113f3e MC |
1162 | return e; |
1163 | } | |
3647bee2 | 1164 | |
dd1abd44 RL |
1165 | void release_engine(ENGINE *e) |
1166 | { | |
1167 | #ifndef OPENSSL_NO_ENGINE | |
1168 | if (e != NULL) | |
1169 | /* Free our "structural" reference. */ | |
1170 | ENGINE_free(e); | |
1171 | #endif | |
1172 | } | |
1173 | ||
c869da88 | 1174 | static unsigned long index_serial_hash(const OPENSSL_CSTRING *a) |
0f113f3e MC |
1175 | { |
1176 | const char *n; | |
f85b68cd | 1177 | |
0f113f3e MC |
1178 | n = a[DB_serial]; |
1179 | while (*n == '0') | |
1180 | n++; | |
739a1eb1 | 1181 | return OPENSSL_LH_strhash(n); |
0f113f3e | 1182 | } |
f85b68cd | 1183 | |
0f113f3e MC |
1184 | static int index_serial_cmp(const OPENSSL_CSTRING *a, |
1185 | const OPENSSL_CSTRING *b) | |
1186 | { | |
1187 | const char *aa, *bb; | |
f85b68cd | 1188 | |
0f113f3e MC |
1189 | for (aa = a[DB_serial]; *aa == '0'; aa++) ; |
1190 | for (bb = b[DB_serial]; *bb == '0'; bb++) ; | |
26a7d938 | 1191 | return strcmp(aa, bb); |
0f113f3e | 1192 | } |
f85b68cd RL |
1193 | |
1194 | static int index_name_qual(char **a) | |
0f113f3e MC |
1195 | { |
1196 | return (a[0][0] == 'V'); | |
1197 | } | |
f85b68cd | 1198 | |
c869da88 | 1199 | static unsigned long index_name_hash(const OPENSSL_CSTRING *a) |
0f113f3e | 1200 | { |
739a1eb1 | 1201 | return OPENSSL_LH_strhash(a[DB_name]); |
0f113f3e | 1202 | } |
f85b68cd | 1203 | |
c869da88 | 1204 | int index_name_cmp(const OPENSSL_CSTRING *a, const OPENSSL_CSTRING *b) |
0f113f3e | 1205 | { |
26a7d938 | 1206 | return strcmp(a[DB_name], b[DB_name]); |
0f113f3e | 1207 | } |
f85b68cd | 1208 | |
c869da88 DSH |
1209 | static IMPLEMENT_LHASH_HASH_FN(index_serial, OPENSSL_CSTRING) |
1210 | static IMPLEMENT_LHASH_COMP_FN(index_serial, OPENSSL_CSTRING) | |
1211 | static IMPLEMENT_LHASH_HASH_FN(index_name, OPENSSL_CSTRING) | |
1212 | static IMPLEMENT_LHASH_COMP_FN(index_name, OPENSSL_CSTRING) | |
f85b68cd RL |
1213 | #undef BSIZE |
1214 | #define BSIZE 256 | |
cc696296 | 1215 | BIGNUM *load_serial(const char *serialfile, int create, ASN1_INTEGER **retai) |
0f113f3e MC |
1216 | { |
1217 | BIO *in = NULL; | |
1218 | BIGNUM *ret = NULL; | |
68b00c23 | 1219 | char buf[1024]; |
0f113f3e MC |
1220 | ASN1_INTEGER *ai = NULL; |
1221 | ||
1222 | ai = ASN1_INTEGER_new(); | |
1223 | if (ai == NULL) | |
1224 | goto err; | |
1225 | ||
7e1b7485 RS |
1226 | in = BIO_new_file(serialfile, "r"); |
1227 | if (in == NULL) { | |
0f113f3e MC |
1228 | if (!create) { |
1229 | perror(serialfile); | |
1230 | goto err; | |
0f113f3e | 1231 | } |
7e1b7485 RS |
1232 | ERR_clear_error(); |
1233 | ret = BN_new(); | |
1234 | if (ret == NULL || !rand_serial(ret, ai)) | |
1235 | BIO_printf(bio_err, "Out of memory\n"); | |
0f113f3e MC |
1236 | } else { |
1237 | if (!a2i_ASN1_INTEGER(in, ai, buf, 1024)) { | |
1238 | BIO_printf(bio_err, "unable to load number from %s\n", | |
1239 | serialfile); | |
1240 | goto err; | |
1241 | } | |
1242 | ret = ASN1_INTEGER_to_BN(ai, NULL); | |
1243 | if (ret == NULL) { | |
1244 | BIO_printf(bio_err, | |
1245 | "error converting number from bin to BIGNUM\n"); | |
1246 | goto err; | |
1247 | } | |
1248 | } | |
1249 | ||
1250 | if (ret && retai) { | |
1251 | *retai = ai; | |
1252 | ai = NULL; | |
1253 | } | |
f85b68cd | 1254 | err: |
ca3a82c3 | 1255 | BIO_free(in); |
2ace7450 | 1256 | ASN1_INTEGER_free(ai); |
26a7d938 | 1257 | return ret; |
0f113f3e MC |
1258 | } |
1259 | ||
cc696296 | 1260 | int save_serial(const char *serialfile, const char *suffix, const BIGNUM *serial, |
0f113f3e MC |
1261 | ASN1_INTEGER **retai) |
1262 | { | |
1263 | char buf[1][BSIZE]; | |
1264 | BIO *out = NULL; | |
1265 | int ret = 0; | |
1266 | ASN1_INTEGER *ai = NULL; | |
1267 | int j; | |
1268 | ||
1269 | if (suffix == NULL) | |
1270 | j = strlen(serialfile); | |
1271 | else | |
1272 | j = strlen(serialfile) + strlen(suffix) + 1; | |
1273 | if (j >= BSIZE) { | |
1274 | BIO_printf(bio_err, "file name too long\n"); | |
1275 | goto err; | |
1276 | } | |
1277 | ||
1278 | if (suffix == NULL) | |
7644a9ae | 1279 | OPENSSL_strlcpy(buf[0], serialfile, BSIZE); |
0f113f3e | 1280 | else { |
4c771796 | 1281 | #ifndef OPENSSL_SYS_VMS |
cbe29648 | 1282 | j = BIO_snprintf(buf[0], sizeof(buf[0]), "%s.%s", serialfile, suffix); |
4c771796 | 1283 | #else |
cbe29648 | 1284 | j = BIO_snprintf(buf[0], sizeof(buf[0]), "%s-%s", serialfile, suffix); |
4c771796 | 1285 | #endif |
0f113f3e | 1286 | } |
7e1b7485 | 1287 | out = BIO_new_file(buf[0], "w"); |
0f113f3e MC |
1288 | if (out == NULL) { |
1289 | ERR_print_errors(bio_err); | |
1290 | goto err; | |
1291 | } | |
0f113f3e MC |
1292 | |
1293 | if ((ai = BN_to_ASN1_INTEGER(serial, NULL)) == NULL) { | |
1294 | BIO_printf(bio_err, "error converting serial to ASN.1 format\n"); | |
1295 | goto err; | |
1296 | } | |
1297 | i2a_ASN1_INTEGER(out, ai); | |
1298 | BIO_puts(out, "\n"); | |
1299 | ret = 1; | |
1300 | if (retai) { | |
1301 | *retai = ai; | |
1302 | ai = NULL; | |
1303 | } | |
1304 | err: | |
ca3a82c3 | 1305 | BIO_free_all(out); |
2ace7450 | 1306 | ASN1_INTEGER_free(ai); |
26a7d938 | 1307 | return ret; |
0f113f3e | 1308 | } |
f85b68cd | 1309 | |
cc696296 F |
1310 | int rotate_serial(const char *serialfile, const char *new_suffix, |
1311 | const char *old_suffix) | |
0f113f3e | 1312 | { |
bde136c8 | 1313 | char buf[2][BSIZE]; |
0f113f3e MC |
1314 | int i, j; |
1315 | ||
1316 | i = strlen(serialfile) + strlen(old_suffix); | |
1317 | j = strlen(serialfile) + strlen(new_suffix); | |
1318 | if (i > j) | |
1319 | j = i; | |
1320 | if (j + 1 >= BSIZE) { | |
1321 | BIO_printf(bio_err, "file name too long\n"); | |
1322 | goto err; | |
1323 | } | |
4c771796 | 1324 | #ifndef OPENSSL_SYS_VMS |
cbe29648 RS |
1325 | j = BIO_snprintf(buf[0], sizeof(buf[0]), "%s.%s", serialfile, new_suffix); |
1326 | j = BIO_snprintf(buf[1], sizeof(buf[1]), "%s.%s", serialfile, old_suffix); | |
4c771796 | 1327 | #else |
cbe29648 RS |
1328 | j = BIO_snprintf(buf[0], sizeof(buf[0]), "%s-%s", serialfile, new_suffix); |
1329 | j = BIO_snprintf(buf[1], sizeof(buf[1]), "%s-%s", serialfile, old_suffix); | |
a1ad253f | 1330 | #endif |
0f113f3e | 1331 | if (rename(serialfile, buf[1]) < 0 && errno != ENOENT |
4c771796 | 1332 | #ifdef ENOTDIR |
0f113f3e MC |
1333 | && errno != ENOTDIR |
1334 | #endif | |
1335 | ) { | |
1336 | BIO_printf(bio_err, | |
1337 | "unable to rename %s to %s\n", serialfile, buf[1]); | |
1338 | perror("reason"); | |
1339 | goto err; | |
1340 | } | |
0f113f3e MC |
1341 | if (rename(buf[0], serialfile) < 0) { |
1342 | BIO_printf(bio_err, | |
1343 | "unable to rename %s to %s\n", buf[0], serialfile); | |
1344 | perror("reason"); | |
1345 | rename(buf[1], serialfile); | |
1346 | goto err; | |
1347 | } | |
1348 | return 1; | |
4c771796 | 1349 | err: |
0f113f3e MC |
1350 | return 0; |
1351 | } | |
4c771796 | 1352 | |
64674bcc | 1353 | int rand_serial(BIGNUM *b, ASN1_INTEGER *ai) |
0f113f3e MC |
1354 | { |
1355 | BIGNUM *btmp; | |
1356 | int ret = 0; | |
23a1d5e9 | 1357 | |
ffb46830 | 1358 | btmp = b == NULL ? BN_new() : b; |
96487cdd | 1359 | if (btmp == NULL) |
0f113f3e MC |
1360 | return 0; |
1361 | ||
ffb46830 | 1362 | if (!BN_rand(btmp, SERIAL_RAND_BITS, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY)) |
0f113f3e MC |
1363 | goto error; |
1364 | if (ai && !BN_to_ASN1_INTEGER(btmp, ai)) | |
1365 | goto error; | |
1366 | ||
1367 | ret = 1; | |
1368 | ||
1369 | error: | |
1370 | ||
23a1d5e9 | 1371 | if (btmp != b) |
0f113f3e MC |
1372 | BN_free(btmp); |
1373 | ||
1374 | return ret; | |
1375 | } | |
64674bcc | 1376 | |
cc696296 | 1377 | CA_DB *load_index(const char *dbfile, DB_ATTR *db_attr) |
0f113f3e MC |
1378 | { |
1379 | CA_DB *retdb = NULL; | |
1380 | TXT_DB *tmpdb = NULL; | |
7e1b7485 | 1381 | BIO *in; |
0f113f3e | 1382 | CONF *dbattr_conf = NULL; |
cc01d217 | 1383 | char buf[BSIZE]; |
c7d5ea26 VD |
1384 | #ifndef OPENSSL_NO_POSIX_IO |
1385 | FILE *dbfp; | |
1386 | struct stat dbst; | |
1387 | #endif | |
0f113f3e | 1388 | |
7e1b7485 | 1389 | in = BIO_new_file(dbfile, "r"); |
0f113f3e MC |
1390 | if (in == NULL) { |
1391 | ERR_print_errors(bio_err); | |
1392 | goto err; | |
1393 | } | |
c7d5ea26 VD |
1394 | |
1395 | #ifndef OPENSSL_NO_POSIX_IO | |
1396 | BIO_get_fp(in, &dbfp); | |
1397 | if (fstat(fileno(dbfp), &dbst) == -1) { | |
ff988500 RS |
1398 | ERR_raise_data(ERR_LIB_SYS, errno, |
1399 | "calling fstat(%s)", dbfile); | |
c7d5ea26 VD |
1400 | ERR_print_errors(bio_err); |
1401 | goto err; | |
1402 | } | |
1403 | #endif | |
1404 | ||
0f113f3e MC |
1405 | if ((tmpdb = TXT_DB_read(in, DB_NUMBER)) == NULL) |
1406 | goto err; | |
f85b68cd RL |
1407 | |
1408 | #ifndef OPENSSL_SYS_VMS | |
cbe29648 | 1409 | BIO_snprintf(buf, sizeof(buf), "%s.attr", dbfile); |
f85b68cd | 1410 | #else |
cbe29648 | 1411 | BIO_snprintf(buf, sizeof(buf), "%s-attr", dbfile); |
0f113f3e | 1412 | #endif |
ac454d8d | 1413 | dbattr_conf = app_load_config_quiet(buf); |
0f113f3e | 1414 | |
b4faea50 | 1415 | retdb = app_malloc(sizeof(*retdb), "new DB"); |
0f113f3e MC |
1416 | retdb->db = tmpdb; |
1417 | tmpdb = NULL; | |
1418 | if (db_attr) | |
1419 | retdb->attributes = *db_attr; | |
1420 | else { | |
1421 | retdb->attributes.unique_subject = 1; | |
1422 | } | |
1423 | ||
1424 | if (dbattr_conf) { | |
1425 | char *p = NCONF_get_string(dbattr_conf, NULL, "unique_subject"); | |
1426 | if (p) { | |
0f113f3e MC |
1427 | retdb->attributes.unique_subject = parse_yesno(p, 1); |
1428 | } | |
1429 | } | |
f85b68cd | 1430 | |
c7d5ea26 VD |
1431 | retdb->dbfname = OPENSSL_strdup(dbfile); |
1432 | #ifndef OPENSSL_NO_POSIX_IO | |
1433 | retdb->dbst = dbst; | |
1434 | #endif | |
1435 | ||
f85b68cd | 1436 | err: |
efa7dd64 | 1437 | NCONF_free(dbattr_conf); |
895cba19 | 1438 | TXT_DB_free(tmpdb); |
ca3a82c3 | 1439 | BIO_free_all(in); |
0f113f3e MC |
1440 | return retdb; |
1441 | } | |
f85b68cd | 1442 | |
a4107d73 VD |
1443 | /* |
1444 | * Returns > 0 on success, <= 0 on error | |
1445 | */ | |
f85b68cd | 1446 | int index_index(CA_DB *db) |
0f113f3e MC |
1447 | { |
1448 | if (!TXT_DB_create_index(db->db, DB_serial, NULL, | |
1449 | LHASH_HASH_FN(index_serial), | |
1450 | LHASH_COMP_FN(index_serial))) { | |
1451 | BIO_printf(bio_err, | |
1452 | "error creating serial number index:(%ld,%ld,%ld)\n", | |
1453 | db->db->error, db->db->arg1, db->db->arg2); | |
1454 | return 0; | |
1455 | } | |
1456 | ||
1457 | if (db->attributes.unique_subject | |
1458 | && !TXT_DB_create_index(db->db, DB_name, index_name_qual, | |
1459 | LHASH_HASH_FN(index_name), | |
1460 | LHASH_COMP_FN(index_name))) { | |
1461 | BIO_printf(bio_err, "error creating name index:(%ld,%ld,%ld)\n", | |
1462 | db->db->error, db->db->arg1, db->db->arg2); | |
1463 | return 0; | |
1464 | } | |
1465 | return 1; | |
1466 | } | |
f85b68cd | 1467 | |
7d727231 | 1468 | int save_index(const char *dbfile, const char *suffix, CA_DB *db) |
0f113f3e MC |
1469 | { |
1470 | char buf[3][BSIZE]; | |
7e1b7485 | 1471 | BIO *out; |
0f113f3e MC |
1472 | int j; |
1473 | ||
0f113f3e MC |
1474 | j = strlen(dbfile) + strlen(suffix); |
1475 | if (j + 6 >= BSIZE) { | |
1476 | BIO_printf(bio_err, "file name too long\n"); | |
1477 | goto err; | |
1478 | } | |
f85b68cd | 1479 | #ifndef OPENSSL_SYS_VMS |
cbe29648 RS |
1480 | j = BIO_snprintf(buf[2], sizeof(buf[2]), "%s.attr", dbfile); |
1481 | j = BIO_snprintf(buf[1], sizeof(buf[1]), "%s.attr.%s", dbfile, suffix); | |
1482 | j = BIO_snprintf(buf[0], sizeof(buf[0]), "%s.%s", dbfile, suffix); | |
f85b68cd | 1483 | #else |
cbe29648 RS |
1484 | j = BIO_snprintf(buf[2], sizeof(buf[2]), "%s-attr", dbfile); |
1485 | j = BIO_snprintf(buf[1], sizeof(buf[1]), "%s-attr-%s", dbfile, suffix); | |
1486 | j = BIO_snprintf(buf[0], sizeof(buf[0]), "%s-%s", dbfile, suffix); | |
63b6fe2b | 1487 | #endif |
7e1b7485 RS |
1488 | out = BIO_new_file(buf[0], "w"); |
1489 | if (out == NULL) { | |
0f113f3e MC |
1490 | perror(dbfile); |
1491 | BIO_printf(bio_err, "unable to open '%s'\n", dbfile); | |
1492 | goto err; | |
1493 | } | |
1494 | j = TXT_DB_write(out, db->db); | |
7e1b7485 | 1495 | BIO_free(out); |
0f113f3e MC |
1496 | if (j <= 0) |
1497 | goto err; | |
1498 | ||
7e1b7485 | 1499 | out = BIO_new_file(buf[1], "w"); |
7e1b7485 | 1500 | if (out == NULL) { |
0f113f3e MC |
1501 | perror(buf[2]); |
1502 | BIO_printf(bio_err, "unable to open '%s'\n", buf[2]); | |
1503 | goto err; | |
1504 | } | |
1505 | BIO_printf(out, "unique_subject = %s\n", | |
1506 | db->attributes.unique_subject ? "yes" : "no"); | |
1507 | BIO_free(out); | |
1508 | ||
1509 | return 1; | |
f85b68cd | 1510 | err: |
0f113f3e MC |
1511 | return 0; |
1512 | } | |
f85b68cd | 1513 | |
0f113f3e MC |
1514 | int rotate_index(const char *dbfile, const char *new_suffix, |
1515 | const char *old_suffix) | |
1516 | { | |
1517 | char buf[5][BSIZE]; | |
1518 | int i, j; | |
1519 | ||
1520 | i = strlen(dbfile) + strlen(old_suffix); | |
1521 | j = strlen(dbfile) + strlen(new_suffix); | |
1522 | if (i > j) | |
1523 | j = i; | |
1524 | if (j + 6 >= BSIZE) { | |
1525 | BIO_printf(bio_err, "file name too long\n"); | |
1526 | goto err; | |
1527 | } | |
f85b68cd | 1528 | #ifndef OPENSSL_SYS_VMS |
cbe29648 RS |
1529 | j = BIO_snprintf(buf[4], sizeof(buf[4]), "%s.attr", dbfile); |
1530 | j = BIO_snprintf(buf[3], sizeof(buf[3]), "%s.attr.%s", dbfile, old_suffix); | |
1531 | j = BIO_snprintf(buf[2], sizeof(buf[2]), "%s.attr.%s", dbfile, new_suffix); | |
1532 | j = BIO_snprintf(buf[1], sizeof(buf[1]), "%s.%s", dbfile, old_suffix); | |
1533 | j = BIO_snprintf(buf[0], sizeof(buf[0]), "%s.%s", dbfile, new_suffix); | |
f85b68cd | 1534 | #else |
cbe29648 RS |
1535 | j = BIO_snprintf(buf[4], sizeof(buf[4]), "%s-attr", dbfile); |
1536 | j = BIO_snprintf(buf[3], sizeof(buf[3]), "%s-attr-%s", dbfile, old_suffix); | |
1537 | j = BIO_snprintf(buf[2], sizeof(buf[2]), "%s-attr-%s", dbfile, new_suffix); | |
1538 | j = BIO_snprintf(buf[1], sizeof(buf[1]), "%s-%s", dbfile, old_suffix); | |
1539 | j = BIO_snprintf(buf[0], sizeof(buf[0]), "%s-%s", dbfile, new_suffix); | |
63b6fe2b | 1540 | #endif |
0f113f3e | 1541 | if (rename(dbfile, buf[1]) < 0 && errno != ENOENT |
a1ad253f | 1542 | #ifdef ENOTDIR |
0f113f3e | 1543 | && errno != ENOTDIR |
a1ad253f | 1544 | #endif |
0f113f3e MC |
1545 | ) { |
1546 | BIO_printf(bio_err, "unable to rename %s to %s\n", dbfile, buf[1]); | |
1547 | perror("reason"); | |
1548 | goto err; | |
1549 | } | |
0f113f3e MC |
1550 | if (rename(buf[0], dbfile) < 0) { |
1551 | BIO_printf(bio_err, "unable to rename %s to %s\n", buf[0], dbfile); | |
1552 | perror("reason"); | |
1553 | rename(buf[1], dbfile); | |
1554 | goto err; | |
1555 | } | |
0f113f3e | 1556 | if (rename(buf[4], buf[3]) < 0 && errno != ENOENT |
a1ad253f | 1557 | #ifdef ENOTDIR |
0f113f3e MC |
1558 | && errno != ENOTDIR |
1559 | #endif | |
1560 | ) { | |
1561 | BIO_printf(bio_err, "unable to rename %s to %s\n", buf[4], buf[3]); | |
1562 | perror("reason"); | |
1563 | rename(dbfile, buf[0]); | |
1564 | rename(buf[1], dbfile); | |
1565 | goto err; | |
1566 | } | |
0f113f3e MC |
1567 | if (rename(buf[2], buf[4]) < 0) { |
1568 | BIO_printf(bio_err, "unable to rename %s to %s\n", buf[2], buf[4]); | |
1569 | perror("reason"); | |
1570 | rename(buf[3], buf[4]); | |
1571 | rename(dbfile, buf[0]); | |
1572 | rename(buf[1], dbfile); | |
1573 | goto err; | |
1574 | } | |
1575 | return 1; | |
f85b68cd | 1576 | err: |
0f113f3e MC |
1577 | return 0; |
1578 | } | |
f85b68cd RL |
1579 | |
1580 | void free_index(CA_DB *db) | |
0f113f3e MC |
1581 | { |
1582 | if (db) { | |
895cba19 | 1583 | TXT_DB_free(db->db); |
c7d5ea26 | 1584 | OPENSSL_free(db->dbfname); |
0f113f3e MC |
1585 | OPENSSL_free(db); |
1586 | } | |
1587 | } | |
6d5ffb59 | 1588 | |
ff990440 | 1589 | int parse_yesno(const char *str, int def) |
0f113f3e | 1590 | { |
0f113f3e MC |
1591 | if (str) { |
1592 | switch (*str) { | |
1593 | case 'f': /* false */ | |
1594 | case 'F': /* FALSE */ | |
1595 | case 'n': /* no */ | |
1596 | case 'N': /* NO */ | |
1597 | case '0': /* 0 */ | |
1bb2daea | 1598 | return 0; |
0f113f3e MC |
1599 | case 't': /* true */ |
1600 | case 'T': /* TRUE */ | |
1601 | case 'y': /* yes */ | |
1602 | case 'Y': /* YES */ | |
1603 | case '1': /* 1 */ | |
1bb2daea | 1604 | return 1; |
0f113f3e MC |
1605 | } |
1606 | } | |
1bb2daea | 1607 | return def; |
0f113f3e | 1608 | } |
03ddbdd9 | 1609 | |
6d5ffb59 | 1610 | /* |
db4c08f0 | 1611 | * name is expected to be in the format /type0=value0/type1=value1/type2=... |
6d5ffb59 RL |
1612 | * where characters may be escaped by \ |
1613 | */ | |
db4c08f0 | 1614 | X509_NAME *parse_name(const char *cp, long chtype, int canmulti) |
0f113f3e | 1615 | { |
db4c08f0 RS |
1616 | int nextismulti = 0; |
1617 | char *work; | |
1618 | X509_NAME *n; | |
0f113f3e | 1619 | |
2167640b EC |
1620 | if (*cp++ != '/') { |
1621 | BIO_printf(bio_err, | |
1622 | "name is expected to be in the format " | |
1623 | "/type0=value0/type1=value1/type2=... where characters may " | |
1624 | "be escaped by \\. This name is not in that format: '%s'\n", | |
1625 | --cp); | |
db4c08f0 | 1626 | return NULL; |
2167640b | 1627 | } |
db4c08f0 RS |
1628 | |
1629 | n = X509_NAME_new(); | |
1630 | if (n == NULL) | |
1631 | return NULL; | |
a3ed492f | 1632 | work = OPENSSL_strdup(cp); |
52958608 DO |
1633 | if (work == NULL) { |
1634 | BIO_printf(bio_err, "%s: Error copying name input\n", opt_getprog()); | |
db4c08f0 | 1635 | goto err; |
52958608 | 1636 | } |
db4c08f0 RS |
1637 | |
1638 | while (*cp) { | |
1639 | char *bp = work; | |
1640 | char *typestr = bp; | |
1641 | unsigned char *valstr; | |
1642 | int nid; | |
1643 | int ismulti = nextismulti; | |
1644 | nextismulti = 0; | |
1645 | ||
1646 | /* Collect the type */ | |
1647 | while (*cp && *cp != '=') | |
1648 | *bp++ = *cp++; | |
1649 | if (*cp == '\0') { | |
0f113f3e | 1650 | BIO_printf(bio_err, |
52958608 | 1651 | "%s: Hit end of string before finding the '='\n", |
db4c08f0 RS |
1652 | opt_getprog()); |
1653 | goto err; | |
0f113f3e | 1654 | } |
db4c08f0 RS |
1655 | *bp++ = '\0'; |
1656 | ++cp; | |
1657 | ||
1658 | /* Collect the value. */ | |
1659 | valstr = (unsigned char *)bp; | |
1660 | for (; *cp && *cp != '/'; *bp++ = *cp++) { | |
1661 | if (canmulti && *cp == '+') { | |
1662 | nextismulti = 1; | |
0f113f3e | 1663 | break; |
db4c08f0 RS |
1664 | } |
1665 | if (*cp == '\\' && *++cp == '\0') { | |
1666 | BIO_printf(bio_err, | |
52958608 DO |
1667 | "%s: escape character at end of string\n", |
1668 | opt_getprog()); | |
db4c08f0 RS |
1669 | goto err; |
1670 | } | |
0f113f3e MC |
1671 | } |
1672 | *bp++ = '\0'; | |
0f113f3e | 1673 | |
db4c08f0 RS |
1674 | /* If not at EOS (must be + or /), move forward. */ |
1675 | if (*cp) | |
1676 | ++cp; | |
0f113f3e | 1677 | |
db4c08f0 RS |
1678 | /* Parse */ |
1679 | nid = OBJ_txt2nid(typestr); | |
1680 | if (nid == NID_undef) { | |
1681 | BIO_printf(bio_err, "%s: Skipping unknown attribute \"%s\"\n", | |
52958608 | 1682 | opt_getprog(), typestr); |
0f113f3e MC |
1683 | continue; |
1684 | } | |
3d362f19 BK |
1685 | if (*valstr == '\0') { |
1686 | BIO_printf(bio_err, | |
1687 | "%s: No value provided for Subject Attribute %s, skipped\n", | |
1688 | opt_getprog(), typestr); | |
1689 | continue; | |
1690 | } | |
db4c08f0 RS |
1691 | if (!X509_NAME_add_entry_by_NID(n, nid, chtype, |
1692 | valstr, strlen((char *)valstr), | |
52958608 DO |
1693 | -1, ismulti ? -1 : 0)) { |
1694 | BIO_printf(bio_err, "%s: Error adding name attribute \"/%s=%s\"\n", | |
1695 | opt_getprog(), typestr ,valstr); | |
db4c08f0 | 1696 | goto err; |
52958608 | 1697 | } |
0f113f3e MC |
1698 | } |
1699 | ||
a3ed492f | 1700 | OPENSSL_free(work); |
0f113f3e MC |
1701 | return n; |
1702 | ||
db4c08f0 | 1703 | err: |
0f113f3e | 1704 | X509_NAME_free(n); |
a3ed492f | 1705 | OPENSSL_free(work); |
0f113f3e | 1706 | return NULL; |
6d5ffb59 RL |
1707 | } |
1708 | ||
0f113f3e MC |
1709 | /* |
1710 | * Read whole contents of a BIO into an allocated memory buffer and return | |
1711 | * it. | |
a9164153 DSH |
1712 | */ |
1713 | ||
1714 | int bio_to_mem(unsigned char **out, int maxlen, BIO *in) | |
0f113f3e MC |
1715 | { |
1716 | BIO *mem; | |
1717 | int len, ret; | |
1718 | unsigned char tbuf[1024]; | |
bde136c8 | 1719 | |
0f113f3e | 1720 | mem = BIO_new(BIO_s_mem()); |
96487cdd | 1721 | if (mem == NULL) |
0f113f3e MC |
1722 | return -1; |
1723 | for (;;) { | |
1724 | if ((maxlen != -1) && maxlen < 1024) | |
1725 | len = maxlen; | |
1726 | else | |
1727 | len = 1024; | |
1728 | len = BIO_read(in, tbuf, len); | |
0c20802c VD |
1729 | if (len < 0) { |
1730 | BIO_free(mem); | |
1731 | return -1; | |
1732 | } | |
1733 | if (len == 0) | |
0f113f3e MC |
1734 | break; |
1735 | if (BIO_write(mem, tbuf, len) != len) { | |
1736 | BIO_free(mem); | |
1737 | return -1; | |
1738 | } | |
1739 | maxlen -= len; | |
1740 | ||
1741 | if (maxlen == 0) | |
1742 | break; | |
1743 | } | |
1744 | ret = BIO_get_mem_data(mem, (char **)out); | |
1745 | BIO_set_flags(mem, BIO_FLAGS_MEM_RDONLY); | |
1746 | BIO_free(mem); | |
1747 | return ret; | |
1748 | } | |
a9164153 | 1749 | |
0c20802c | 1750 | int pkey_ctrl_string(EVP_PKEY_CTX *ctx, const char *value) |
0f113f3e MC |
1751 | { |
1752 | int rv; | |
1753 | char *stmp, *vtmp = NULL; | |
7644a9ae | 1754 | stmp = OPENSSL_strdup(value); |
0f113f3e MC |
1755 | if (!stmp) |
1756 | return -1; | |
1757 | vtmp = strchr(stmp, ':'); | |
1758 | if (vtmp) { | |
1759 | *vtmp = 0; | |
1760 | vtmp++; | |
1761 | } | |
1762 | rv = EVP_PKEY_CTX_ctrl_str(ctx, stmp, vtmp); | |
1763 | OPENSSL_free(stmp); | |
1764 | return rv; | |
1765 | } | |
a2318e86 | 1766 | |
ecf3a1fb | 1767 | static void nodes_print(const char *name, STACK_OF(X509_POLICY_NODE) *nodes) |
0f113f3e MC |
1768 | { |
1769 | X509_POLICY_NODE *node; | |
1770 | int i; | |
ecf3a1fb RS |
1771 | |
1772 | BIO_printf(bio_err, "%s Policies:", name); | |
0f113f3e | 1773 | if (nodes) { |
ecf3a1fb | 1774 | BIO_puts(bio_err, "\n"); |
0f113f3e MC |
1775 | for (i = 0; i < sk_X509_POLICY_NODE_num(nodes); i++) { |
1776 | node = sk_X509_POLICY_NODE_value(nodes, i); | |
ecf3a1fb | 1777 | X509_POLICY_NODE_print(bio_err, node, 2); |
0f113f3e | 1778 | } |
2234212c | 1779 | } else { |
ecf3a1fb | 1780 | BIO_puts(bio_err, " <empty>\n"); |
2234212c | 1781 | } |
0f113f3e | 1782 | } |
c431798e | 1783 | |
ecf3a1fb | 1784 | void policies_print(X509_STORE_CTX *ctx) |
0f113f3e MC |
1785 | { |
1786 | X509_POLICY_TREE *tree; | |
1787 | int explicit_policy; | |
0f113f3e MC |
1788 | tree = X509_STORE_CTX_get0_policy_tree(ctx); |
1789 | explicit_policy = X509_STORE_CTX_get_explicit_policy(ctx); | |
1790 | ||
ecf3a1fb | 1791 | BIO_printf(bio_err, "Require explicit Policy: %s\n", |
0f113f3e MC |
1792 | explicit_policy ? "True" : "False"); |
1793 | ||
ecf3a1fb RS |
1794 | nodes_print("Authority", X509_policy_tree_get0_policies(tree)); |
1795 | nodes_print("User", X509_policy_tree_get0_user_policies(tree)); | |
0f113f3e | 1796 | } |
ffa10187 | 1797 | |
3a83462d MC |
1798 | /*- |
1799 | * next_protos_parse parses a comma separated list of strings into a string | |
71fa4513 BL |
1800 | * in a format suitable for passing to SSL_CTX_set_next_protos_advertised. |
1801 | * outlen: (output) set to the length of the resulting buffer on success. | |
1802 | * err: (maybe NULL) on failure, an error message line is written to this BIO. | |
8483a003 | 1803 | * in: a NUL terminated string like "abc,def,ghi" |
71fa4513 | 1804 | * |
8483a003 | 1805 | * returns: a malloc'd buffer or NULL on failure. |
71fa4513 | 1806 | */ |
817cd0d5 | 1807 | unsigned char *next_protos_parse(size_t *outlen, const char *in) |
0f113f3e MC |
1808 | { |
1809 | size_t len; | |
1810 | unsigned char *out; | |
1811 | size_t i, start = 0; | |
1812 | ||
1813 | len = strlen(in); | |
1814 | if (len >= 65535) | |
1815 | return NULL; | |
1816 | ||
68dc6824 | 1817 | out = app_malloc(strlen(in) + 1, "NPN buffer"); |
0f113f3e MC |
1818 | for (i = 0; i <= len; ++i) { |
1819 | if (i == len || in[i] == ',') { | |
1820 | if (i - start > 255) { | |
1821 | OPENSSL_free(out); | |
1822 | return NULL; | |
1823 | } | |
3a63c0ed | 1824 | out[start] = (unsigned char)(i - start); |
0f113f3e | 1825 | start = i + 1; |
2234212c | 1826 | } else { |
0f113f3e | 1827 | out[i + 1] = in[i]; |
2234212c | 1828 | } |
0f113f3e MC |
1829 | } |
1830 | ||
1831 | *outlen = len + 1; | |
1832 | return out; | |
1833 | } | |
71fa4513 | 1834 | |
a70da5b3 | 1835 | void print_cert_checks(BIO *bio, X509 *x, |
0f113f3e MC |
1836 | const char *checkhost, |
1837 | const char *checkemail, const char *checkip) | |
1838 | { | |
1839 | if (x == NULL) | |
1840 | return; | |
1841 | if (checkhost) { | |
1842 | BIO_printf(bio, "Hostname %s does%s match certificate\n", | |
7e1b7485 RS |
1843 | checkhost, |
1844 | X509_check_host(x, checkhost, 0, 0, NULL) == 1 | |
1845 | ? "" : " NOT"); | |
0f113f3e MC |
1846 | } |
1847 | ||
1848 | if (checkemail) { | |
1849 | BIO_printf(bio, "Email %s does%s match certificate\n", | |
7e1b7485 RS |
1850 | checkemail, X509_check_email(x, checkemail, 0, 0) |
1851 | ? "" : " NOT"); | |
0f113f3e MC |
1852 | } |
1853 | ||
1854 | if (checkip) { | |
1855 | BIO_printf(bio, "IP %s does%s match certificate\n", | |
1856 | checkip, X509_check_ip_asc(x, checkip, 0) ? "" : " NOT"); | |
1857 | } | |
1858 | } | |
a70da5b3 | 1859 | |
0090a686 DSH |
1860 | /* Get first http URL from a DIST_POINT structure */ |
1861 | ||
1862 | static const char *get_dp_url(DIST_POINT *dp) | |
0f113f3e MC |
1863 | { |
1864 | GENERAL_NAMES *gens; | |
1865 | GENERAL_NAME *gen; | |
1866 | int i, gtype; | |
1867 | ASN1_STRING *uri; | |
1868 | if (!dp->distpoint || dp->distpoint->type != 0) | |
1869 | return NULL; | |
1870 | gens = dp->distpoint->name.fullname; | |
1871 | for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) { | |
1872 | gen = sk_GENERAL_NAME_value(gens, i); | |
1873 | uri = GENERAL_NAME_get0_value(gen, >ype); | |
1874 | if (gtype == GEN_URI && ASN1_STRING_length(uri) > 6) { | |
17ebf85a | 1875 | const char *uptr = (const char *)ASN1_STRING_get0_data(uri); |
86885c28 | 1876 | if (strncmp(uptr, "http://", 7) == 0) |
0f113f3e MC |
1877 | return uptr; |
1878 | } | |
1879 | } | |
1880 | return NULL; | |
1881 | } | |
1882 | ||
1883 | /* | |
1884 | * Look through a CRLDP structure and attempt to find an http URL to | |
1885 | * downloads a CRL from. | |
0090a686 DSH |
1886 | */ |
1887 | ||
1888 | static X509_CRL *load_crl_crldp(STACK_OF(DIST_POINT) *crldp) | |
0f113f3e MC |
1889 | { |
1890 | int i; | |
1891 | const char *urlptr = NULL; | |
1892 | for (i = 0; i < sk_DIST_POINT_num(crldp); i++) { | |
1893 | DIST_POINT *dp = sk_DIST_POINT_value(crldp, i); | |
1894 | urlptr = get_dp_url(dp); | |
1895 | if (urlptr) | |
1896 | return load_crl(urlptr, FORMAT_HTTP); | |
1897 | } | |
1898 | return NULL; | |
1899 | } | |
1900 | ||
1901 | /* | |
1902 | * Example of downloading CRLs from CRLDP: not usable for real world as it | |
1903 | * always downloads, doesn't support non-blocking I/O and doesn't cache | |
1904 | * anything. | |
0090a686 DSH |
1905 | */ |
1906 | ||
1907 | static STACK_OF(X509_CRL) *crls_http_cb(X509_STORE_CTX *ctx, X509_NAME *nm) | |
0f113f3e MC |
1908 | { |
1909 | X509 *x; | |
1910 | STACK_OF(X509_CRL) *crls = NULL; | |
1911 | X509_CRL *crl; | |
1912 | STACK_OF(DIST_POINT) *crldp; | |
7e1b7485 RS |
1913 | |
1914 | crls = sk_X509_CRL_new_null(); | |
1915 | if (!crls) | |
1916 | return NULL; | |
0f113f3e MC |
1917 | x = X509_STORE_CTX_get_current_cert(ctx); |
1918 | crldp = X509_get_ext_d2i(x, NID_crl_distribution_points, NULL, NULL); | |
1919 | crl = load_crl_crldp(crldp); | |
1920 | sk_DIST_POINT_pop_free(crldp, DIST_POINT_free); | |
fe2b7dfd MC |
1921 | if (!crl) { |
1922 | sk_X509_CRL_free(crls); | |
0f113f3e | 1923 | return NULL; |
fe2b7dfd | 1924 | } |
0f113f3e MC |
1925 | sk_X509_CRL_push(crls, crl); |
1926 | /* Try to download delta CRL */ | |
1927 | crldp = X509_get_ext_d2i(x, NID_freshest_crl, NULL, NULL); | |
1928 | crl = load_crl_crldp(crldp); | |
1929 | sk_DIST_POINT_pop_free(crldp, DIST_POINT_free); | |
1930 | if (crl) | |
1931 | sk_X509_CRL_push(crls, crl); | |
1932 | return crls; | |
1933 | } | |
0090a686 DSH |
1934 | |
1935 | void store_setup_crl_download(X509_STORE *st) | |
0f113f3e MC |
1936 | { |
1937 | X509_STORE_set_lookup_crls_cb(st, crls_http_cb); | |
1938 | } | |
0090a686 | 1939 | |
0a39d8f2 AP |
1940 | /* |
1941 | * Platform-specific sections | |
1942 | */ | |
ffa10187 | 1943 | #if defined(_WIN32) |
a1ad253f AP |
1944 | # ifdef fileno |
1945 | # undef fileno | |
1946 | # define fileno(a) (int)_fileno(a) | |
1947 | # endif | |
1948 | ||
1949 | # include <windows.h> | |
1950 | # include <tchar.h> | |
1951 | ||
1952 | static int WIN32_rename(const char *from, const char *to) | |
0f113f3e MC |
1953 | { |
1954 | TCHAR *tfrom = NULL, *tto; | |
1955 | DWORD err; | |
1956 | int ret = 0; | |
1957 | ||
1958 | if (sizeof(TCHAR) == 1) { | |
1959 | tfrom = (TCHAR *)from; | |
1960 | tto = (TCHAR *)to; | |
1961 | } else { /* UNICODE path */ | |
1962 | ||
1963 | size_t i, flen = strlen(from) + 1, tlen = strlen(to) + 1; | |
b1ad95e3 | 1964 | tfrom = malloc(sizeof(*tfrom) * (flen + tlen)); |
0f113f3e MC |
1965 | if (tfrom == NULL) |
1966 | goto err; | |
1967 | tto = tfrom + flen; | |
1968 | # if !defined(_WIN32_WCE) || _WIN32_WCE>=101 | |
1969 | if (!MultiByteToWideChar(CP_ACP, 0, from, flen, (WCHAR *)tfrom, flen)) | |
1970 | # endif | |
1971 | for (i = 0; i < flen; i++) | |
1972 | tfrom[i] = (TCHAR)from[i]; | |
1973 | # if !defined(_WIN32_WCE) || _WIN32_WCE>=101 | |
1974 | if (!MultiByteToWideChar(CP_ACP, 0, to, tlen, (WCHAR *)tto, tlen)) | |
1975 | # endif | |
1976 | for (i = 0; i < tlen; i++) | |
1977 | tto[i] = (TCHAR)to[i]; | |
1978 | } | |
1979 | ||
1980 | if (MoveFile(tfrom, tto)) | |
1981 | goto ok; | |
1982 | err = GetLastError(); | |
1983 | if (err == ERROR_ALREADY_EXISTS || err == ERROR_FILE_EXISTS) { | |
1984 | if (DeleteFile(tto) && MoveFile(tfrom, tto)) | |
1985 | goto ok; | |
1986 | err = GetLastError(); | |
1987 | } | |
1988 | if (err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND) | |
1989 | errno = ENOENT; | |
1990 | else if (err == ERROR_ACCESS_DENIED) | |
1991 | errno = EACCES; | |
1992 | else | |
1993 | errno = EINVAL; /* we could map more codes... */ | |
1994 | err: | |
1995 | ret = -1; | |
1996 | ok: | |
1997 | if (tfrom != NULL && tfrom != (TCHAR *)from) | |
1998 | free(tfrom); | |
1999 | return ret; | |
2000 | } | |
0a39d8f2 AP |
2001 | #endif |
2002 | ||
2003 | /* app_tminterval section */ | |
2004 | #if defined(_WIN32) | |
0f113f3e MC |
2005 | double app_tminterval(int stop, int usertime) |
2006 | { | |
2007 | FILETIME now; | |
2008 | double ret = 0; | |
2009 | static ULARGE_INTEGER tmstart; | |
2010 | static int warning = 1; | |
2011 | # ifdef _WIN32_WINNT | |
2012 | static HANDLE proc = NULL; | |
2013 | ||
2014 | if (proc == NULL) { | |
2015 | if (check_winnt()) | |
2016 | proc = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, | |
2017 | GetCurrentProcessId()); | |
2018 | if (proc == NULL) | |
2019 | proc = (HANDLE) - 1; | |
2020 | } | |
2021 | ||
2022 | if (usertime && proc != (HANDLE) - 1) { | |
2023 | FILETIME junk; | |
2024 | GetProcessTimes(proc, &junk, &junk, &junk, &now); | |
2025 | } else | |
2026 | # endif | |
2027 | { | |
2028 | SYSTEMTIME systime; | |
9135fddb | 2029 | |
0f113f3e MC |
2030 | if (usertime && warning) { |
2031 | BIO_printf(bio_err, "To get meaningful results, run " | |
2032 | "this program on idle system.\n"); | |
2033 | warning = 0; | |
2034 | } | |
2035 | GetSystemTime(&systime); | |
2036 | SystemTimeToFileTime(&systime, &now); | |
2037 | } | |
9135fddb | 2038 | |
0f113f3e MC |
2039 | if (stop == TM_START) { |
2040 | tmstart.u.LowPart = now.dwLowDateTime; | |
2041 | tmstart.u.HighPart = now.dwHighDateTime; | |
2042 | } else { | |
2043 | ULARGE_INTEGER tmstop; | |
9135fddb | 2044 | |
0f113f3e MC |
2045 | tmstop.u.LowPart = now.dwLowDateTime; |
2046 | tmstop.u.HighPart = now.dwHighDateTime; | |
9135fddb | 2047 | |
0f113f3e MC |
2048 | ret = (__int64)(tmstop.QuadPart - tmstart.QuadPart) * 1e-7; |
2049 | } | |
9135fddb | 2050 | |
26a7d938 | 2051 | return ret; |
0f113f3e | 2052 | } |
5c8b7b4c | 2053 | #elif defined(OPENSSL_SYS_VXWORKS) |
0f113f3e | 2054 | # include <time.h> |
0a39d8f2 | 2055 | |
0f113f3e MC |
2056 | double app_tminterval(int stop, int usertime) |
2057 | { | |
2058 | double ret = 0; | |
2059 | # ifdef CLOCK_REALTIME | |
2060 | static struct timespec tmstart; | |
2061 | struct timespec now; | |
2062 | # else | |
2063 | static unsigned long tmstart; | |
2064 | unsigned long now; | |
2065 | # endif | |
2066 | static int warning = 1; | |
2067 | ||
2068 | if (usertime && warning) { | |
2069 | BIO_printf(bio_err, "To get meaningful results, run " | |
2070 | "this program on idle system.\n"); | |
2071 | warning = 0; | |
2072 | } | |
2073 | # ifdef CLOCK_REALTIME | |
2074 | clock_gettime(CLOCK_REALTIME, &now); | |
2075 | if (stop == TM_START) | |
2076 | tmstart = now; | |
2077 | else | |
2078 | ret = ((now.tv_sec + now.tv_nsec * 1e-9) | |
2079 | - (tmstart.tv_sec + tmstart.tv_nsec * 1e-9)); | |
2080 | # else | |
2081 | now = tickGet(); | |
2082 | if (stop == TM_START) | |
2083 | tmstart = now; | |
2084 | else | |
2085 | ret = (now - tmstart) / (double)sysClkRateGet(); | |
2086 | # endif | |
26a7d938 | 2087 | return ret; |
0f113f3e | 2088 | } |
0a39d8f2 | 2089 | |
0f113f3e MC |
2090 | #elif defined(OPENSSL_SYSTEM_VMS) |
2091 | # include <time.h> | |
2092 | # include <times.h> | |
0a39d8f2 | 2093 | |
0f113f3e MC |
2094 | double app_tminterval(int stop, int usertime) |
2095 | { | |
2096 | static clock_t tmstart; | |
2097 | double ret = 0; | |
2098 | clock_t now; | |
2099 | # ifdef __TMS | |
2100 | struct tms rus; | |
2101 | ||
2102 | now = times(&rus); | |
2103 | if (usertime) | |
2104 | now = rus.tms_utime; | |
2105 | # else | |
2106 | if (usertime) | |
2107 | now = clock(); /* sum of user and kernel times */ | |
2108 | else { | |
2109 | struct timeval tv; | |
2110 | gettimeofday(&tv, NULL); | |
2111 | now = (clock_t)((unsigned long long)tv.tv_sec * CLK_TCK + | |
2112 | (unsigned long long)tv.tv_usec * (1000000 / CLK_TCK) | |
2113 | ); | |
2114 | } | |
2115 | # endif | |
2116 | if (stop == TM_START) | |
2117 | tmstart = now; | |
2118 | else | |
2119 | ret = (now - tmstart) / (double)(CLK_TCK); | |
0a39d8f2 | 2120 | |
26a7d938 | 2121 | return ret; |
0f113f3e | 2122 | } |
0a39d8f2 | 2123 | |
0f113f3e MC |
2124 | #elif defined(_SC_CLK_TCK) /* by means of unistd.h */ |
2125 | # include <sys/times.h> | |
0a39d8f2 | 2126 | |
0f113f3e MC |
2127 | double app_tminterval(int stop, int usertime) |
2128 | { | |
2129 | double ret = 0; | |
2130 | struct tms rus; | |
2131 | clock_t now = times(&rus); | |
2132 | static clock_t tmstart; | |
2133 | ||
2134 | if (usertime) | |
2135 | now = rus.tms_utime; | |
2136 | ||
2234212c | 2137 | if (stop == TM_START) { |
0f113f3e | 2138 | tmstart = now; |
2234212c | 2139 | } else { |
0f113f3e MC |
2140 | long int tck = sysconf(_SC_CLK_TCK); |
2141 | ret = (now - tmstart) / (double)tck; | |
2142 | } | |
2143 | ||
26a7d938 | 2144 | return ret; |
0f113f3e | 2145 | } |
0a39d8f2 | 2146 | |
0f113f3e MC |
2147 | #else |
2148 | # include <sys/time.h> | |
2149 | # include <sys/resource.h> | |
0a39d8f2 | 2150 | |
0f113f3e MC |
2151 | double app_tminterval(int stop, int usertime) |
2152 | { | |
2153 | double ret = 0; | |
2154 | struct rusage rus; | |
2155 | struct timeval now; | |
2156 | static struct timeval tmstart; | |
2157 | ||
2158 | if (usertime) | |
2159 | getrusage(RUSAGE_SELF, &rus), now = rus.ru_utime; | |
2160 | else | |
2161 | gettimeofday(&now, NULL); | |
2162 | ||
2163 | if (stop == TM_START) | |
2164 | tmstart = now; | |
2165 | else | |
2166 | ret = ((now.tv_sec + now.tv_usec * 1e-6) | |
2167 | - (tmstart.tv_sec + tmstart.tv_usec * 1e-6)); | |
2168 | ||
2169 | return ret; | |
2170 | } | |
0a39d8f2 | 2171 | #endif |
a1ad253f | 2172 | |
7e1b7485 RS |
2173 | int app_access(const char* name, int flag) |
2174 | { | |
2175 | #ifdef _WIN32 | |
2176 | return _access(name, flag); | |
2177 | #else | |
2178 | return access(name, flag); | |
2179 | #endif | |
2180 | } | |
2181 | ||
ffa10187 | 2182 | int app_isdir(const char *name) |
0f113f3e | 2183 | { |
a43ce58f | 2184 | return opt_isdir(name); |
0f113f3e | 2185 | } |
ffa10187 | 2186 | |
0a39d8f2 | 2187 | /* raw_read|write section */ |
51e5133d RL |
2188 | #if defined(__VMS) |
2189 | # include "vms_term_sock.h" | |
2190 | static int stdin_sock = -1; | |
2191 | ||
2192 | static void close_stdin_sock(void) | |
2193 | { | |
2194 | TerminalSocket (TERM_SOCK_DELETE, &stdin_sock); | |
2195 | } | |
2196 | ||
2197 | int fileno_stdin(void) | |
2198 | { | |
2199 | if (stdin_sock == -1) { | |
2200 | TerminalSocket(TERM_SOCK_CREATE, &stdin_sock); | |
2201 | atexit(close_stdin_sock); | |
2202 | } | |
2203 | ||
2204 | return stdin_sock; | |
2205 | } | |
2206 | #else | |
2207 | int fileno_stdin(void) | |
2208 | { | |
2209 | return fileno(stdin); | |
2210 | } | |
2211 | #endif | |
2212 | ||
2213 | int fileno_stdout(void) | |
2214 | { | |
2215 | return fileno(stdout); | |
2216 | } | |
2217 | ||
ffa10187 | 2218 | #if defined(_WIN32) && defined(STD_INPUT_HANDLE) |
0f113f3e MC |
2219 | int raw_read_stdin(void *buf, int siz) |
2220 | { | |
2221 | DWORD n; | |
2222 | if (ReadFile(GetStdHandle(STD_INPUT_HANDLE), buf, siz, &n, NULL)) | |
26a7d938 | 2223 | return n; |
0f113f3e | 2224 | else |
26a7d938 | 2225 | return -1; |
0f113f3e | 2226 | } |
51e5133d | 2227 | #elif defined(__VMS) |
2234212c | 2228 | # include <sys/socket.h> |
a19228b7 | 2229 | |
51e5133d RL |
2230 | int raw_read_stdin(void *buf, int siz) |
2231 | { | |
2232 | return recv(fileno_stdin(), buf, siz, 0); | |
2233 | } | |
ffa10187 | 2234 | #else |
0f113f3e MC |
2235 | int raw_read_stdin(void *buf, int siz) |
2236 | { | |
51e5133d | 2237 | return read(fileno_stdin(), buf, siz); |
0f113f3e | 2238 | } |
ffa10187 AP |
2239 | #endif |
2240 | ||
2241 | #if defined(_WIN32) && defined(STD_OUTPUT_HANDLE) | |
0f113f3e MC |
2242 | int raw_write_stdout(const void *buf, int siz) |
2243 | { | |
2244 | DWORD n; | |
2245 | if (WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buf, siz, &n, NULL)) | |
26a7d938 | 2246 | return n; |
0f113f3e | 2247 | else |
26a7d938 | 2248 | return -1; |
0f113f3e | 2249 | } |
ffa10187 | 2250 | #else |
0f113f3e MC |
2251 | int raw_write_stdout(const void *buf, int siz) |
2252 | { | |
51e5133d | 2253 | return write(fileno_stdout(), buf, siz); |
0f113f3e | 2254 | } |
ffa10187 | 2255 | #endif |
a412b891 RL |
2256 | |
2257 | /* | |
a43ce58f | 2258 | * Centralized handling of input and output files with format specification |
a412b891 RL |
2259 | * The format is meant to show what the input and output is supposed to be, |
2260 | * and is therefore a show of intent more than anything else. However, it | |
a43ce58f | 2261 | * does impact behavior on some platforms, such as differentiating between |
a412b891 RL |
2262 | * text and binary input/output on non-Unix platforms |
2263 | */ | |
a60994df | 2264 | BIO *dup_bio_in(int format) |
a412b891 | 2265 | { |
a60994df | 2266 | return BIO_new_fp(stdin, |
a43ce58f | 2267 | BIO_NOCLOSE | (FMT_istext(format) ? BIO_FP_TEXT : 0)); |
a60994df RL |
2268 | } |
2269 | ||
2270 | BIO *dup_bio_out(int format) | |
2271 | { | |
2272 | BIO *b = BIO_new_fp(stdout, | |
a43ce58f | 2273 | BIO_NOCLOSE | (FMT_istext(format) ? BIO_FP_TEXT : 0)); |
71bb86f0 RL |
2274 | void *prefix = NULL; |
2275 | ||
a412b891 | 2276 | #ifdef OPENSSL_SYS_VMS |
a43ce58f | 2277 | if (FMT_istext(format)) |
a60994df | 2278 | b = BIO_push(BIO_new(BIO_f_linebuffer()), b); |
149bd5d6 | 2279 | #endif |
71bb86f0 | 2280 | |
682b444f RL |
2281 | if (FMT_istext(format) |
2282 | && (prefix = getenv("HARNESS_OSSL_PREFIX")) != NULL) { | |
2283 | b = BIO_push(BIO_new(apps_bf_prefix()), b); | |
71bb86f0 RL |
2284 | BIO_ctrl(b, PREFIX_CTRL_SET_PREFIX, 0, prefix); |
2285 | } | |
2286 | ||
149bd5d6 RL |
2287 | return b; |
2288 | } | |
2289 | ||
2290 | BIO *dup_bio_err(int format) | |
2291 | { | |
2292 | BIO *b = BIO_new_fp(stderr, | |
a43ce58f | 2293 | BIO_NOCLOSE | (FMT_istext(format) ? BIO_FP_TEXT : 0)); |
149bd5d6 | 2294 | #ifdef OPENSSL_SYS_VMS |
a43ce58f | 2295 | if (FMT_istext(format)) |
149bd5d6 | 2296 | b = BIO_push(BIO_new(BIO_f_linebuffer()), b); |
a412b891 RL |
2297 | #endif |
2298 | return b; | |
2299 | } | |
2300 | ||
682b444f RL |
2301 | /* |
2302 | * Because the prefix method is created dynamically, we must also be able | |
2303 | * to destroy it. | |
2304 | */ | |
3cb7c5cf | 2305 | void destroy_prefix_method(void) |
71bb86f0 | 2306 | { |
682b444f | 2307 | BIO_METHOD *prefix_method = apps_bf_prefix(); |
71bb86f0 RL |
2308 | BIO_meth_free(prefix_method); |
2309 | prefix_method = NULL; | |
2310 | } | |
2311 | ||
a412b891 RL |
2312 | void unbuffer(FILE *fp) |
2313 | { | |
90dbd250 RL |
2314 | /* |
2315 | * On VMS, setbuf() will only take 32-bit pointers, and a compilation | |
2316 | * with /POINTER_SIZE=64 will give off a MAYLOSEDATA2 warning here. | |
2317 | * However, we trust that the C RTL will never give us a FILE pointer | |
2318 | * above the first 4 GB of memory, so we simply turn off the warning | |
2319 | * temporarily. | |
2320 | */ | |
2321 | #if defined(OPENSSL_SYS_VMS) && defined(__DECC) | |
2322 | # pragma environment save | |
2323 | # pragma message disable maylosedata2 | |
2324 | #endif | |
a412b891 | 2325 | setbuf(fp, NULL); |
90dbd250 RL |
2326 | #if defined(OPENSSL_SYS_VMS) && defined(__DECC) |
2327 | # pragma environment restore | |
2328 | #endif | |
a412b891 RL |
2329 | } |
2330 | ||
2331 | static const char *modestr(char mode, int format) | |
2332 | { | |
2333 | OPENSSL_assert(mode == 'a' || mode == 'r' || mode == 'w'); | |
2334 | ||
2335 | switch (mode) { | |
2336 | case 'a': | |
a43ce58f | 2337 | return FMT_istext(format) ? "a" : "ab"; |
a412b891 | 2338 | case 'r': |
a43ce58f | 2339 | return FMT_istext(format) ? "r" : "rb"; |
a412b891 | 2340 | case 'w': |
a43ce58f | 2341 | return FMT_istext(format) ? "w" : "wb"; |
a412b891 RL |
2342 | } |
2343 | /* The assert above should make sure we never reach this point */ | |
2344 | return NULL; | |
2345 | } | |
2346 | ||
2347 | static const char *modeverb(char mode) | |
2348 | { | |
2349 | switch (mode) { | |
2350 | case 'a': | |
2351 | return "appending"; | |
2352 | case 'r': | |
2353 | return "reading"; | |
2354 | case 'w': | |
2355 | return "writing"; | |
2356 | } | |
2357 | return "(doing something)"; | |
2358 | } | |
2359 | ||
2360 | /* | |
2361 | * Open a file for writing, owner-read-only. | |
2362 | */ | |
2363 | BIO *bio_open_owner(const char *filename, int format, int private) | |
2364 | { | |
2365 | FILE *fp = NULL; | |
2366 | BIO *b = NULL; | |
1cd5cc36 | 2367 | int fd = -1, bflags, mode, textmode; |
a412b891 RL |
2368 | |
2369 | if (!private || filename == NULL || strcmp(filename, "-") == 0) | |
2370 | return bio_open_default(filename, 'w', format); | |
2371 | ||
2372 | mode = O_WRONLY; | |
2373 | #ifdef O_CREAT | |
2374 | mode |= O_CREAT; | |
2375 | #endif | |
2376 | #ifdef O_TRUNC | |
2377 | mode |= O_TRUNC; | |
2378 | #endif | |
a43ce58f | 2379 | textmode = FMT_istext(format); |
1cd5cc36 | 2380 | if (!textmode) { |
a412b891 RL |
2381 | #ifdef O_BINARY |
2382 | mode |= O_BINARY; | |
2383 | #elif defined(_O_BINARY) | |
2384 | mode |= _O_BINARY; | |
2385 | #endif | |
2386 | } | |
2387 | ||
fbd03b09 RL |
2388 | #ifdef OPENSSL_SYS_VMS |
2389 | /* VMS doesn't have O_BINARY, it just doesn't make sense. But, | |
2390 | * it still needs to know that we're going binary, or fdopen() | |
2391 | * will fail with "invalid argument"... so we tell VMS what the | |
2392 | * context is. | |
2393 | */ | |
2394 | if (!textmode) | |
2395 | fd = open(filename, mode, 0600, "ctx=bin"); | |
2396 | else | |
2397 | #endif | |
2398 | fd = open(filename, mode, 0600); | |
a412b891 RL |
2399 | if (fd < 0) |
2400 | goto err; | |
2401 | fp = fdopen(fd, modestr('w', format)); | |
2402 | if (fp == NULL) | |
2403 | goto err; | |
2404 | bflags = BIO_CLOSE; | |
1cd5cc36 | 2405 | if (textmode) |
a412b891 RL |
2406 | bflags |= BIO_FP_TEXT; |
2407 | b = BIO_new_fp(fp, bflags); | |
2408 | if (b) | |
2409 | return b; | |
2410 | ||
2411 | err: | |
2412 | BIO_printf(bio_err, "%s: Can't open \"%s\" for writing, %s\n", | |
2413 | opt_getprog(), filename, strerror(errno)); | |
2414 | ERR_print_errors(bio_err); | |
2415 | /* If we have fp, then fdopen took over fd, so don't close both. */ | |
2416 | if (fp) | |
2417 | fclose(fp); | |
2418 | else if (fd >= 0) | |
2419 | close(fd); | |
2420 | return NULL; | |
2421 | } | |
2422 | ||
2423 | static BIO *bio_open_default_(const char *filename, char mode, int format, | |
2424 | int quiet) | |
2425 | { | |
2426 | BIO *ret; | |
2427 | ||
2428 | if (filename == NULL || strcmp(filename, "-") == 0) { | |
a60994df | 2429 | ret = mode == 'r' ? dup_bio_in(format) : dup_bio_out(format); |
a412b891 RL |
2430 | if (quiet) { |
2431 | ERR_clear_error(); | |
2432 | return ret; | |
2433 | } | |
2434 | if (ret != NULL) | |
2435 | return ret; | |
2436 | BIO_printf(bio_err, | |
2437 | "Can't open %s, %s\n", | |
2438 | mode == 'r' ? "stdin" : "stdout", strerror(errno)); | |
2439 | } else { | |
2440 | ret = BIO_new_file(filename, modestr(mode, format)); | |
2441 | if (quiet) { | |
2442 | ERR_clear_error(); | |
2443 | return ret; | |
2444 | } | |
2445 | if (ret != NULL) | |
2446 | return ret; | |
2447 | BIO_printf(bio_err, | |
2448 | "Can't open %s for %s, %s\n", | |
2449 | filename, modeverb(mode), strerror(errno)); | |
2450 | } | |
2451 | ERR_print_errors(bio_err); | |
2452 | return NULL; | |
2453 | } | |
2454 | ||
2455 | BIO *bio_open_default(const char *filename, char mode, int format) | |
2456 | { | |
2457 | return bio_open_default_(filename, mode, format, 0); | |
2458 | } | |
2459 | ||
2460 | BIO *bio_open_default_quiet(const char *filename, char mode, int format) | |
2461 | { | |
2462 | return bio_open_default_(filename, mode, format, 1); | |
2463 | } | |
2464 | ||
e1b9840e MC |
2465 | void wait_for_async(SSL *s) |
2466 | { | |
d6e03b70 MC |
2467 | /* On Windows select only works for sockets, so we simply don't wait */ |
2468 | #ifndef OPENSSL_SYS_WINDOWS | |
ff75a257 | 2469 | int width = 0; |
e1b9840e | 2470 | fd_set asyncfds; |
ff75a257 MC |
2471 | OSSL_ASYNC_FD *fds; |
2472 | size_t numfds; | |
0a345252 | 2473 | size_t i; |
e1b9840e | 2474 | |
ff75a257 MC |
2475 | if (!SSL_get_all_async_fds(s, NULL, &numfds)) |
2476 | return; | |
2477 | if (numfds == 0) | |
e1b9840e | 2478 | return; |
589902b2 | 2479 | fds = app_malloc(sizeof(OSSL_ASYNC_FD) * numfds, "allocate async fds"); |
ff75a257 MC |
2480 | if (!SSL_get_all_async_fds(s, fds, &numfds)) { |
2481 | OPENSSL_free(fds); | |
0a345252 | 2482 | return; |
ff75a257 | 2483 | } |
e1b9840e | 2484 | |
e1b9840e | 2485 | FD_ZERO(&asyncfds); |
0a345252 P |
2486 | for (i = 0; i < numfds; i++) { |
2487 | if (width <= (int)fds[i]) | |
2488 | width = (int)fds[i] + 1; | |
2489 | openssl_fdset((int)fds[i], &asyncfds); | |
ff75a257 | 2490 | } |
e1b9840e | 2491 | select(width, (void *)&asyncfds, NULL, NULL, NULL); |
0a345252 | 2492 | OPENSSL_free(fds); |
d6e03b70 | 2493 | #endif |
e1b9840e | 2494 | } |
75dd6c1a MC |
2495 | |
2496 | /* if OPENSSL_SYS_WINDOWS is defined then so is OPENSSL_SYS_MSDOS */ | |
2497 | #if defined(OPENSSL_SYS_MSDOS) | |
2498 | int has_stdin_waiting(void) | |
2499 | { | |
2500 | # if defined(OPENSSL_SYS_WINDOWS) | |
2501 | HANDLE inhand = GetStdHandle(STD_INPUT_HANDLE); | |
2502 | DWORD events = 0; | |
2503 | INPUT_RECORD inputrec; | |
2504 | DWORD insize = 1; | |
2505 | BOOL peeked; | |
2506 | ||
2507 | if (inhand == INVALID_HANDLE_VALUE) { | |
2508 | return 0; | |
2509 | } | |
2510 | ||
2511 | peeked = PeekConsoleInput(inhand, &inputrec, insize, &events); | |
2512 | if (!peeked) { | |
2513 | /* Probably redirected input? _kbhit() does not work in this case */ | |
2514 | if (!feof(stdin)) { | |
2515 | return 1; | |
2516 | } | |
2517 | return 0; | |
2518 | } | |
2519 | # endif | |
2520 | return _kbhit(); | |
2521 | } | |
2522 | #endif | |
17ebf85a DSH |
2523 | |
2524 | /* Corrupt a signature by modifying final byte */ | |
a0754084 | 2525 | void corrupt_signature(const ASN1_STRING *signature) |
17ebf85a | 2526 | { |
a0754084 DSH |
2527 | unsigned char *s = signature->data; |
2528 | s[signature->length - 1] ^= 0x1; | |
17ebf85a | 2529 | } |
dc047d31 DSH |
2530 | |
2531 | int set_cert_times(X509 *x, const char *startdate, const char *enddate, | |
2532 | int days) | |
2533 | { | |
dc047d31 | 2534 | if (startdate == NULL || strcmp(startdate, "today") == 0) { |
0b7347ef DSH |
2535 | if (X509_gmtime_adj(X509_getm_notBefore(x), 0) == NULL) |
2536 | return 0; | |
2537 | } else { | |
04e62715 | 2538 | if (!ASN1_TIME_set_string_X509(X509_getm_notBefore(x), startdate)) |
0b7347ef | 2539 | return 0; |
dc047d31 | 2540 | } |
dc047d31 | 2541 | if (enddate == NULL) { |
0b7347ef DSH |
2542 | if (X509_time_adj_ex(X509_getm_notAfter(x), days, 0, NULL) |
2543 | == NULL) | |
2544 | return 0; | |
04e62715 | 2545 | } else if (!ASN1_TIME_set_string_X509(X509_getm_notAfter(x), enddate)) { |
0b7347ef | 2546 | return 0; |
dc047d31 | 2547 | } |
0b7347ef | 2548 | return 1; |
dc047d31 | 2549 | } |
20967afb RS |
2550 | |
2551 | void make_uppercase(char *string) | |
2552 | { | |
2553 | int i; | |
2554 | ||
2555 | for (i = 0; string[i] != '\0'; i++) | |
2556 | string[i] = toupper((unsigned char)string[i]); | |
2557 | } | |
a43ce58f SL |
2558 | |
2559 | int opt_printf_stderr(const char *fmt, ...) | |
2560 | { | |
2561 | va_list ap; | |
2562 | int ret; | |
2563 | ||
2564 | va_start(ap, fmt); | |
2565 | ret = BIO_vprintf(bio_err, fmt, ap); | |
2566 | va_end(ap); | |
2567 | return ret; | |
2568 | } |