]>
Commit | Line | Data |
---|---|---|
846e33c7 | 1 | /* |
065121ff | 2 | * Copyright 1995-2022 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 | |
ff215713 P |
18 | #ifndef OPENSSL_NO_ENGINE |
19 | /* We need to use some deprecated APIs */ | |
20 | # define OPENSSL_SUPPRESS_DEPRECATED | |
21 | # include <openssl/engine.h> | |
22 | #endif | |
23 | ||
d02b48c6 RE |
24 | #include <stdio.h> |
25 | #include <stdlib.h> | |
26 | #include <string.h> | |
b379fe6c | 27 | #include <sys/types.h> |
a412b891 RL |
28 | #ifndef OPENSSL_NO_POSIX_IO |
29 | # include <sys/stat.h> | |
30 | # include <fcntl.h> | |
31 | #endif | |
d652a095 | 32 | #include <ctype.h> |
a1ad253f | 33 | #include <errno.h> |
90ae4673 RL |
34 | #include <openssl/err.h> |
35 | #include <openssl/x509.h> | |
535d79da | 36 | #include <openssl/x509v3.h> |
9498dac4 | 37 | #include <openssl/http.h> |
90ae4673 | 38 | #include <openssl/pem.h> |
6d382c74 | 39 | #include <openssl/store.h> |
90ae4673 | 40 | #include <openssl/pkcs12.h> |
2fe5adc3 | 41 | #include <openssl/ui.h> |
90ae4673 | 42 | #include <openssl/safestack.h> |
3a1ee3c1 | 43 | #include <openssl/rsa.h> |
a7e4ca5b | 44 | #include <openssl/rand.h> |
f0eae953 | 45 | #include <openssl/bn.h> |
7e1b7485 | 46 | #include <openssl/ssl.h> |
d382e796 | 47 | #include <openssl/core_names.h> |
0b27381f | 48 | #include "s_apps.h" |
d610d27f | 49 | #include "apps.h" |
d610d27f | 50 | |
a1ad253f AP |
51 | #ifdef _WIN32 |
52 | static int WIN32_rename(const char *from, const char *to); | |
8cdb993d | 53 | # define rename(from, to) WIN32_rename((from), (to)) |
a1ad253f AP |
54 | #endif |
55 | ||
d0cf719e DMSP |
56 | #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) |
57 | # include <conio.h> | |
58 | #endif | |
59 | ||
96d4ec67 | 60 | #if defined(OPENSSL_SYS_MSDOS) && !defined(_WIN32) || defined(__BORLANDC__) |
d0cf719e DMSP |
61 | # define _kbhit kbhit |
62 | #endif | |
63 | ||
15795943 DDO |
64 | static BIO *bio_open_default_(const char *filename, char mode, int format, |
65 | int quiet); | |
66 | ||
62ca1565 DO |
67 | #define PASS_SOURCE_SIZE_MAX 4 |
68 | ||
852c2ed2 | 69 | DEFINE_STACK_OF(CONF) |
852c2ed2 | 70 | |
8ca533e3 | 71 | typedef struct { |
0f113f3e MC |
72 | const char *name; |
73 | unsigned long flag; | |
74 | unsigned long mask; | |
8ca533e3 DSH |
75 | } NAME_EX_TBL; |
76 | ||
0f113f3e MC |
77 | static int set_table_opts(unsigned long *flags, const char *arg, |
78 | const NAME_EX_TBL * in_tbl); | |
79 | static int set_multi_opts(unsigned long *flags, const char *arg, | |
80 | const NAME_EX_TBL * in_tbl); | |
d02b48c6 | 81 | int app_init(long mesgwin); |
0f113f3e | 82 | |
7e1b7485 | 83 | int chopup_args(ARGS *arg, char *buf) |
0f113f3e | 84 | { |
7e1b7485 | 85 | int quoted; |
4c9b0a03 | 86 | char c = '\0', *p = NULL; |
0f113f3e | 87 | |
7e1b7485 RS |
88 | arg->argc = 0; |
89 | if (arg->size == 0) { | |
90 | arg->size = 20; | |
b4faea50 | 91 | arg->argv = app_malloc(sizeof(*arg->argv) * arg->size, "argv space"); |
0f113f3e | 92 | } |
0f113f3e | 93 | |
7e1b7485 RS |
94 | for (p = buf;;) { |
95 | /* Skip whitespace. */ | |
18295f0c | 96 | while (*p && isspace(_UC(*p))) |
0f113f3e | 97 | p++; |
12a765a5 | 98 | if (*p == '\0') |
0f113f3e MC |
99 | break; |
100 | ||
101 | /* The start of something good :-) */ | |
7e1b7485 | 102 | if (arg->argc >= arg->size) { |
7c0ef843 | 103 | char **tmp; |
8cdb993d | 104 | |
7e1b7485 | 105 | arg->size += 20; |
7c0ef843 DSH |
106 | tmp = OPENSSL_realloc(arg->argv, sizeof(*arg->argv) * arg->size); |
107 | if (tmp == NULL) | |
0f113f3e | 108 | return 0; |
7c0ef843 | 109 | arg->argv = tmp; |
0f113f3e | 110 | } |
7e1b7485 RS |
111 | quoted = *p == '\'' || *p == '"'; |
112 | if (quoted) | |
113 | c = *p++; | |
114 | arg->argv[arg->argc++] = p; | |
0f113f3e MC |
115 | |
116 | /* now look for the end of this */ | |
7e1b7485 RS |
117 | if (quoted) { |
118 | while (*p && *p != c) | |
0f113f3e | 119 | p++; |
7e1b7485 | 120 | *p++ = '\0'; |
0f113f3e | 121 | } else { |
18295f0c | 122 | while (*p && !isspace(_UC(*p))) |
0f113f3e | 123 | p++; |
7e1b7485 RS |
124 | if (*p) |
125 | *p++ = '\0'; | |
0f113f3e | 126 | } |
0f113f3e | 127 | } |
7e1b7485 | 128 | arg->argv[arg->argc] = NULL; |
208fb891 | 129 | return 1; |
0f113f3e | 130 | } |
d02b48c6 RE |
131 | |
132 | #ifndef APP_INIT | |
6b691a5c | 133 | int app_init(long mesgwin) |
0f113f3e | 134 | { |
208fb891 | 135 | return 1; |
0f113f3e | 136 | } |
d02b48c6 | 137 | #endif |
53b1899e | 138 | |
fd3397fc RL |
139 | int ctx_set_verify_locations(SSL_CTX *ctx, |
140 | const char *CAfile, int noCAfile, | |
141 | const char *CApath, int noCApath, | |
142 | const char *CAstore, int noCAstore) | |
7e1b7485 | 143 | { |
fd3397fc | 144 | if (CAfile == NULL && CApath == NULL && CAstore == NULL) { |
2b6bcb70 MC |
145 | if (!noCAfile && SSL_CTX_set_default_verify_file(ctx) <= 0) |
146 | return 0; | |
147 | if (!noCApath && SSL_CTX_set_default_verify_dir(ctx) <= 0) | |
148 | return 0; | |
fd3397fc RL |
149 | if (!noCAstore && SSL_CTX_set_default_verify_store(ctx) <= 0) |
150 | return 0; | |
2b6bcb70 MC |
151 | |
152 | return 1; | |
153 | } | |
fd3397fc RL |
154 | |
155 | if (CAfile != NULL && !SSL_CTX_load_verify_file(ctx, CAfile)) | |
156 | return 0; | |
157 | if (CApath != NULL && !SSL_CTX_load_verify_dir(ctx, CApath)) | |
158 | return 0; | |
159 | if (CAstore != NULL && !SSL_CTX_load_verify_store(ctx, CAstore)) | |
160 | return 0; | |
161 | return 1; | |
7e1b7485 RS |
162 | } |
163 | ||
b5369582 RP |
164 | #ifndef OPENSSL_NO_CT |
165 | ||
dd696a55 RP |
166 | int ctx_set_ctlog_list_file(SSL_CTX *ctx, const char *path) |
167 | { | |
2234212c | 168 | if (path == NULL) |
328f36c5 | 169 | return SSL_CTX_set_default_ctlog_list_file(ctx); |
dd696a55 RP |
170 | |
171 | return SSL_CTX_set_ctlog_list_file(ctx, path); | |
172 | } | |
173 | ||
b5369582 RP |
174 | #endif |
175 | ||
b5c4209b DB |
176 | static unsigned long nmflag = 0; |
177 | static char nmflag_set = 0; | |
178 | ||
179 | int set_nameopt(const char *arg) | |
954ef7ef | 180 | { |
b5c4209b DB |
181 | int ret = set_name_ex(&nmflag, arg); |
182 | ||
183 | if (ret) | |
184 | nmflag_set = 1; | |
185 | ||
186 | return ret; | |
187 | } | |
54a656ef | 188 | |
b5c4209b DB |
189 | unsigned long get_nameopt(void) |
190 | { | |
8cdb993d DDO |
191 | return |
192 | nmflag_set ? nmflag : XN_FLAG_SEP_CPLUS_SPC | ASN1_STRFLGS_UTF8_CONVERT; | |
b5c4209b | 193 | } |
954ef7ef | 194 | |
32f7be2a | 195 | void dump_cert_text(BIO *out, X509 *x) |
b5c4209b | 196 | { |
46a11faf | 197 | print_name(out, "subject=", X509_get_subject_name(x)); |
46a11faf | 198 | print_name(out, "issuer=", X509_get_issuer_name(x)); |
954ef7ef | 199 | } |
a3fe382e | 200 | |
229446df RS |
201 | int wrap_password_callback(char *buf, int bufsiz, int verify, void *userdata) |
202 | { | |
203 | return password_callback(buf, bufsiz, verify, (PW_CB_DATA *)userdata); | |
204 | } | |
205 | ||
cc696296 | 206 | static char *app_get_pass(const char *arg, int keepbio); |
a3fe382e | 207 | |
6d382c74 DDO |
208 | char *get_passwd(const char *pass, const char *desc) |
209 | { | |
210 | char *result = NULL; | |
211 | ||
212 | if (desc == NULL) | |
213 | desc = "<unknown>"; | |
214 | if (!app_passwd(pass, NULL, &result, NULL)) | |
215 | BIO_printf(bio_err, "Error getting password for %s\n", desc); | |
216 | if (pass != NULL && result == NULL) { | |
217 | BIO_printf(bio_err, | |
218 | "Trying plain input string (better precede with 'pass:')\n"); | |
219 | result = OPENSSL_strdup(pass); | |
220 | if (result == NULL) | |
8cdb993d DDO |
221 | BIO_printf(bio_err, |
222 | "Out of memory getting password for %s\n", desc); | |
6d382c74 DDO |
223 | } |
224 | return result; | |
225 | } | |
226 | ||
cc696296 | 227 | int app_passwd(const char *arg1, const char *arg2, char **pass1, char **pass2) |
a3fe382e | 228 | { |
229446df RS |
229 | int same = arg1 != NULL && arg2 != NULL && strcmp(arg1, arg2) == 0; |
230 | ||
2234212c | 231 | if (arg1 != NULL) { |
7e1b7485 | 232 | *pass1 = app_get_pass(arg1, same); |
2234212c | 233 | if (*pass1 == NULL) |
0f113f3e | 234 | return 0; |
2234212c | 235 | } else if (pass1 != NULL) { |
0f113f3e | 236 | *pass1 = NULL; |
2234212c PY |
237 | } |
238 | if (arg2 != NULL) { | |
7e1b7485 | 239 | *pass2 = app_get_pass(arg2, same ? 2 : 0); |
2234212c | 240 | if (*pass2 == NULL) |
0f113f3e | 241 | return 0; |
2234212c | 242 | } else if (pass2 != NULL) { |
0f113f3e | 243 | *pass2 = NULL; |
2234212c | 244 | } |
0f113f3e | 245 | return 1; |
a3fe382e DSH |
246 | } |
247 | ||
cc696296 | 248 | static char *app_get_pass(const char *arg, int keepbio) |
a3fe382e | 249 | { |
0f113f3e | 250 | static BIO *pwdbio = NULL; |
229446df | 251 | char *tmp, tpass[APP_PASS_LEN]; |
0f113f3e | 252 | int i; |
86885c28 | 253 | |
62ca1565 | 254 | /* PASS_SOURCE_SIZE_MAX = max number of chars before ':' in below strings */ |
2ff286c2 DDO |
255 | if (CHECK_AND_SKIP_PREFIX(arg, "pass:")) |
256 | return OPENSSL_strdup(arg); | |
257 | if (CHECK_AND_SKIP_PREFIX(arg, "env:")) { | |
258 | tmp = getenv(arg); | |
2234212c | 259 | if (tmp == NULL) { |
2ff286c2 | 260 | BIO_printf(bio_err, "No environment variable %s\n", arg); |
0f113f3e MC |
261 | return NULL; |
262 | } | |
7644a9ae | 263 | return OPENSSL_strdup(tmp); |
0f113f3e | 264 | } |
2234212c | 265 | if (!keepbio || pwdbio == NULL) { |
2ff286c2 DDO |
266 | if (CHECK_AND_SKIP_PREFIX(arg, "file:")) { |
267 | pwdbio = BIO_new_file(arg, "r"); | |
2234212c | 268 | if (pwdbio == NULL) { |
2ff286c2 | 269 | BIO_printf(bio_err, "Can't open file %s\n", arg); |
0f113f3e MC |
270 | return NULL; |
271 | } | |
eff7cb41 | 272 | #if !defined(_WIN32) |
0f113f3e MC |
273 | /* |
274 | * Under _WIN32, which covers even Win64 and CE, file | |
275 | * descriptors referenced by BIO_s_fd are not inherited | |
276 | * by child process and therefore below is not an option. | |
277 | * It could have been an option if bss_fd.c was operating | |
278 | * on real Windows descriptors, such as those obtained | |
279 | * with CreateFile. | |
280 | */ | |
2ff286c2 | 281 | } else if (CHECK_AND_SKIP_PREFIX(arg, "fd:")) { |
0f113f3e | 282 | BIO *btmp; |
8cdb993d | 283 | |
2ff286c2 | 284 | i = atoi(arg); |
0f113f3e MC |
285 | if (i >= 0) |
286 | pwdbio = BIO_new_fd(i, BIO_NOCLOSE); | |
fa17f5c9 | 287 | if ((i < 0) || pwdbio == NULL) { |
2ff286c2 | 288 | BIO_printf(bio_err, "Can't access file descriptor %s\n", arg); |
0f113f3e MC |
289 | return NULL; |
290 | } | |
291 | /* | |
292 | * Can't do BIO_gets on an fd BIO so add a buffering BIO | |
293 | */ | |
294 | btmp = BIO_new(BIO_f_buffer()); | |
fa17f5c9 | 295 | if (btmp == NULL) { |
296 | BIO_free_all(pwdbio); | |
297 | pwdbio = NULL; | |
298 | BIO_printf(bio_err, "Out of memory\n"); | |
299 | return NULL; | |
300 | } | |
0f113f3e MC |
301 | pwdbio = BIO_push(btmp, pwdbio); |
302 | #endif | |
86885c28 | 303 | } else if (strcmp(arg, "stdin") == 0) { |
efec0f46 | 304 | unbuffer(stdin); |
a60994df | 305 | pwdbio = dup_bio_in(FORMAT_TEXT); |
12a765a5 | 306 | if (pwdbio == NULL) { |
7e1b7485 | 307 | BIO_printf(bio_err, "Can't open BIO for stdin\n"); |
0f113f3e MC |
308 | return NULL; |
309 | } | |
310 | } else { | |
62ca1565 DO |
311 | /* argument syntax error; do not reveal too much about arg */ |
312 | tmp = strchr(arg, ':'); | |
313 | if (tmp == NULL || tmp - arg > PASS_SOURCE_SIZE_MAX) | |
314 | BIO_printf(bio_err, | |
315 | "Invalid password argument, missing ':' within the first %d chars\n", | |
316 | PASS_SOURCE_SIZE_MAX + 1); | |
317 | else | |
318 | BIO_printf(bio_err, | |
319 | "Invalid password argument, starting with \"%.*s\"\n", | |
320 | (int)(tmp - arg + 1), arg); | |
0f113f3e MC |
321 | return NULL; |
322 | } | |
323 | } | |
324 | i = BIO_gets(pwdbio, tpass, APP_PASS_LEN); | |
325 | if (keepbio != 1) { | |
326 | BIO_free_all(pwdbio); | |
327 | pwdbio = NULL; | |
328 | } | |
329 | if (i <= 0) { | |
7e1b7485 | 330 | BIO_printf(bio_err, "Error reading password from BIO\n"); |
0f113f3e MC |
331 | return NULL; |
332 | } | |
333 | tmp = strchr(tpass, '\n'); | |
2234212c | 334 | if (tmp != NULL) |
0f113f3e | 335 | *tmp = 0; |
7644a9ae | 336 | return OPENSSL_strdup(tpass); |
a3fe382e | 337 | } |
90ae4673 | 338 | |
da7f81d3 DDO |
339 | char *app_conf_try_string(const CONF *conf, const char *group, const char *name) |
340 | { | |
341 | char *res; | |
342 | ||
343 | ERR_set_mark(); | |
344 | res = NCONF_get_string(conf, group, name); | |
345 | if (res == NULL) | |
346 | ERR_pop_to_mark(); | |
347 | else | |
348 | ERR_clear_last_mark(); | |
349 | return res; | |
350 | } | |
351 | ||
352 | ||
bfa470a4 | 353 | CONF *app_load_config_bio(BIO *in, const char *filename) |
cc01d217 RS |
354 | { |
355 | long errorline = -1; | |
356 | CONF *conf; | |
357 | int i; | |
cc01d217 | 358 | |
a1fb5eb9 | 359 | conf = NCONF_new_ex(app_get0_libctx(), NULL); |
cc01d217 | 360 | i = NCONF_load_bio(conf, in, &errorline); |
cc01d217 RS |
361 | if (i > 0) |
362 | return conf; | |
363 | ||
bfa470a4 RL |
364 | if (errorline <= 0) { |
365 | BIO_printf(bio_err, "%s: Can't load ", opt_getprog()); | |
366 | } else { | |
367 | BIO_printf(bio_err, "%s: Error on line %ld of ", opt_getprog(), | |
368 | errorline); | |
369 | } | |
370 | if (filename != NULL) | |
371 | BIO_printf(bio_err, "config file \"%s\"\n", filename); | |
cc01d217 | 372 | else |
bfa470a4 RL |
373 | BIO_printf(bio_err, "config input"); |
374 | ||
cc01d217 RS |
375 | NCONF_free(conf); |
376 | return NULL; | |
377 | } | |
2234212c | 378 | |
15795943 | 379 | CONF *app_load_config_verbose(const char *filename, int verbose) |
296f54ee | 380 | { |
15795943 DDO |
381 | if (verbose) { |
382 | if (*filename == '\0') | |
383 | BIO_printf(bio_err, "No configuration used\n"); | |
384 | else | |
385 | BIO_printf(bio_err, "Using configuration from %s\n", filename); | |
386 | } | |
387 | return app_load_config_internal(filename, 0); | |
296f54ee | 388 | } |
2234212c | 389 | |
15795943 | 390 | CONF *app_load_config_internal(const char *filename, int quiet) |
296f54ee | 391 | { |
18d9c9bf | 392 | BIO *in; |
296f54ee RL |
393 | CONF *conf; |
394 | ||
18d9c9bf TM |
395 | if (filename == NULL || *filename != '\0') { |
396 | if ((in = bio_open_default_(filename, 'r', FORMAT_TEXT, quiet)) == NULL) | |
397 | return NULL; | |
398 | conf = app_load_config_bio(in, filename); | |
399 | BIO_free(in); | |
400 | } else { | |
401 | /* Return empty config if filename is empty string. */ | |
a1fb5eb9 | 402 | conf = NCONF_new_ex(app_get0_libctx(), NULL); |
18d9c9bf | 403 | } |
296f54ee RL |
404 | return conf; |
405 | } | |
406 | ||
407 | int app_load_modules(const CONF *config) | |
408 | { | |
409 | CONF *to_free = NULL; | |
410 | ||
411 | if (config == NULL) | |
dccd20d1 | 412 | config = to_free = app_load_config_quiet(default_config_file); |
296f54ee | 413 | if (config == NULL) |
dccd20d1 | 414 | return 1; |
296f54ee RL |
415 | |
416 | if (CONF_modules_load(config, NULL, 0) <= 0) { | |
417 | BIO_printf(bio_err, "Error configuring OpenSSL modules\n"); | |
418 | ERR_print_errors(bio_err); | |
419 | NCONF_free(to_free); | |
420 | return 0; | |
421 | } | |
422 | NCONF_free(to_free); | |
423 | return 1; | |
424 | } | |
cc01d217 | 425 | |
7e1b7485 | 426 | int add_oid_section(CONF *conf) |
0f113f3e MC |
427 | { |
428 | char *p; | |
429 | STACK_OF(CONF_VALUE) *sktmp; | |
430 | CONF_VALUE *cnf; | |
431 | int i; | |
75ebbd9a | 432 | |
da7f81d3 | 433 | if ((p = app_conf_try_string(conf, NULL, "oid_section")) == NULL) |
0f113f3e | 434 | return 1; |
75ebbd9a | 435 | if ((sktmp = NCONF_get_section(conf, p)) == NULL) { |
7e1b7485 | 436 | BIO_printf(bio_err, "problem loading oid section %s\n", p); |
0f113f3e MC |
437 | return 0; |
438 | } | |
439 | for (i = 0; i < sk_CONF_VALUE_num(sktmp); i++) { | |
440 | cnf = sk_CONF_VALUE_value(sktmp, i); | |
441 | if (OBJ_create(cnf->value, cnf->name, cnf->name) == NID_undef) { | |
7e1b7485 | 442 | BIO_printf(bio_err, "problem creating object %s=%s\n", |
0f113f3e MC |
443 | cnf->name, cnf->value); |
444 | return 0; | |
445 | } | |
446 | } | |
447 | return 1; | |
431b0cce RL |
448 | } |
449 | ||
ae89578b SL |
450 | CONF *app_load_config_modules(const char *configfile) |
451 | { | |
452 | CONF *conf = NULL; | |
453 | ||
454 | if (configfile != NULL) { | |
15795943 | 455 | if ((conf = app_load_config_verbose(configfile, 1)) == NULL) |
ae89578b SL |
456 | return NULL; |
457 | if (configfile != default_config_file && !app_load_modules(conf)) { | |
458 | NCONF_free(conf); | |
459 | conf = NULL; | |
460 | } | |
461 | } | |
462 | return conf; | |
463 | } | |
464 | ||
2ff286c2 DDO |
465 | #define IS_HTTP(uri) ((uri) != NULL && HAS_PREFIX(uri, OSSL_HTTP_PREFIX)) |
466 | #define IS_HTTPS(uri) ((uri) != NULL && HAS_PREFIX(uri, OSSL_HTTPS_PREFIX)) | |
9498dac4 | 467 | |
d382e796 | 468 | X509 *load_cert_pass(const char *uri, int format, int maybe_stdin, |
6d382c74 | 469 | const char *pass, const char *desc) |
0f113f3e | 470 | { |
6d382c74 | 471 | X509 *cert = NULL; |
0f113f3e | 472 | |
6d382c74 DDO |
473 | if (desc == NULL) |
474 | desc = "certificate"; | |
6e249947 | 475 | if (IS_HTTPS(uri)) { |
9498dac4 | 476 | BIO_printf(bio_err, "Loading %s over HTTPS is unsupported\n", desc); |
6e249947 | 477 | } else if (IS_HTTP(uri)) { |
9498dac4 | 478 | cert = X509_load_http(uri, NULL, NULL, 0 /* timeout */); |
6e249947 DDO |
479 | if (cert == NULL) { |
480 | ERR_print_errors(bio_err); | |
481 | BIO_printf(bio_err, "Unable to load %s from %s\n", desc, uri); | |
482 | } | |
483 | } else { | |
0e89b396 | 484 | (void)load_key_certs_crls(uri, format, maybe_stdin, pass, desc, 0, |
9498dac4 | 485 | NULL, NULL, NULL, &cert, NULL, NULL, NULL); |
0f113f3e | 486 | } |
6d382c74 | 487 | return cert; |
0f113f3e | 488 | } |
90ae4673 | 489 | |
d382e796 TM |
490 | X509_CRL *load_crl(const char *uri, int format, int maybe_stdin, |
491 | const char *desc) | |
6d382c74 DDO |
492 | { |
493 | X509_CRL *crl = NULL; | |
9d5aca65 | 494 | |
6d382c74 DDO |
495 | if (desc == NULL) |
496 | desc = "CRL"; | |
6e249947 | 497 | if (IS_HTTPS(uri)) { |
9498dac4 | 498 | BIO_printf(bio_err, "Loading %s over HTTPS is unsupported\n", desc); |
6e249947 | 499 | } else if (IS_HTTP(uri)) { |
9498dac4 | 500 | crl = X509_CRL_load_http(uri, NULL, NULL, 0 /* timeout */); |
6e249947 DDO |
501 | if (crl == NULL) { |
502 | ERR_print_errors(bio_err); | |
503 | BIO_printf(bio_err, "Unable to load %s from %s\n", desc, uri); | |
504 | } | |
505 | } else { | |
0e89b396 | 506 | (void)load_key_certs_crls(uri, format, maybe_stdin, NULL, desc, 0, |
9498dac4 | 507 | NULL, NULL, NULL, NULL, NULL, &crl, NULL); |
0f113f3e | 508 | } |
6d382c74 | 509 | return crl; |
9d5aca65 DO |
510 | } |
511 | ||
200d8447 | 512 | /* Could be simplified if OSSL_STORE supported CSRs, see FR #15725 */ |
9d5aca65 DO |
513 | X509_REQ *load_csr(const char *file, int format, const char *desc) |
514 | { | |
515 | X509_REQ *req = NULL; | |
516 | BIO *in; | |
517 | ||
d382e796 TM |
518 | if (format == FORMAT_UNDEF) |
519 | format = FORMAT_PEM; | |
9d5aca65 DO |
520 | in = bio_open_default(file, 'r', format); |
521 | if (in == NULL) | |
522 | goto end; | |
523 | ||
524 | if (format == FORMAT_ASN1) | |
525 | req = d2i_X509_REQ_bio(in, NULL); | |
526 | else if (format == FORMAT_PEM) | |
527 | req = PEM_read_bio_X509_REQ(in, NULL, NULL, NULL); | |
3ee4e8ce | 528 | else |
51c833ac | 529 | print_format_error(format, OPT_FMT_PEMDER); |
fdb78f3d | 530 | |
0f113f3e | 531 | end: |
6d382c74 | 532 | if (req == NULL) { |
9d5aca65 | 533 | ERR_print_errors(bio_err); |
200d8447 DDO |
534 | if (desc != NULL) |
535 | BIO_printf(bio_err, "Unable to load %s\n", desc); | |
9d5aca65 | 536 | } |
0f113f3e | 537 | BIO_free(in); |
9d5aca65 | 538 | return req; |
0f113f3e | 539 | } |
fdb78f3d | 540 | |
200d8447 | 541 | /* Better extend OSSL_STORE to support CSRs, see FR #15725 */ |
a75f707f TZ |
542 | X509_REQ *load_csr_autofmt(const char *infile, int format, |
543 | STACK_OF(OPENSSL_STRING) *vfyopts, const char *desc) | |
200d8447 DDO |
544 | { |
545 | X509_REQ *csr; | |
546 | ||
547 | if (format != FORMAT_UNDEF) { | |
548 | csr = load_csr(infile, format, desc); | |
549 | } else { /* try PEM, then DER */ | |
550 | BIO *bio_bak = bio_err; | |
551 | ||
552 | bio_err = NULL; /* do not show errors on more than one try */ | |
553 | csr = load_csr(infile, FORMAT_PEM, NULL /* desc */); | |
554 | bio_err = bio_bak; | |
555 | if (csr == NULL) { | |
556 | ERR_clear_error(); | |
557 | csr = load_csr(infile, FORMAT_ASN1, NULL /* desc */); | |
558 | } | |
559 | if (csr == NULL) { | |
560 | BIO_printf(bio_err, "error: unable to load %s from file '%s'\n", | |
561 | desc, infile); | |
562 | } | |
563 | } | |
564 | if (csr != NULL) { | |
565 | EVP_PKEY *pkey = X509_REQ_get0_pubkey(csr); | |
a75f707f | 566 | int ret = do_X509_REQ_verify(csr, pkey, vfyopts); |
200d8447 DDO |
567 | |
568 | if (pkey == NULL || ret < 0) | |
a75f707f | 569 | BIO_puts(bio_err, "Warning: error while verifying CSR self-signature\n"); |
200d8447 | 570 | else if (ret == 0) |
a75f707f | 571 | BIO_puts(bio_err, "Warning: CSR self-signature does not match the contents\n"); |
200d8447 DDO |
572 | return csr; |
573 | } | |
574 | return csr; | |
575 | } | |
576 | ||
6d382c74 DDO |
577 | void cleanse(char *str) |
578 | { | |
579 | if (str != NULL) | |
580 | OPENSSL_cleanse(str, strlen(str)); | |
581 | } | |
582 | ||
583 | void clear_free(char *str) | |
584 | { | |
585 | if (str != NULL) | |
586 | OPENSSL_clear_free(str, strlen(str)); | |
587 | } | |
588 | ||
589 | EVP_PKEY *load_key(const char *uri, int format, int may_stdin, | |
9d5aca65 | 590 | const char *pass, ENGINE *e, const char *desc) |
0f113f3e | 591 | { |
0f113f3e | 592 | EVP_PKEY *pkey = NULL; |
f91d003a | 593 | char *allocated_uri = NULL; |
0f113f3e | 594 | |
6d382c74 DDO |
595 | if (desc == NULL) |
596 | desc = "private key"; | |
0f113f3e | 597 | |
0e89b396 | 598 | if (format == FORMAT_ENGINE) |
f91d003a | 599 | uri = allocated_uri = make_engine_uri(e, uri, desc); |
0e89b396 | 600 | (void)load_key_certs_crls(uri, format, may_stdin, pass, desc, 0, |
f91d003a | 601 | &pkey, NULL, NULL, NULL, NULL, NULL, NULL); |
3ee4e8ce | 602 | |
f91d003a | 603 | OPENSSL_free(allocated_uri); |
26a7d938 | 604 | return pkey; |
0f113f3e | 605 | } |
90ae4673 | 606 | |
0e89b396 | 607 | /* first try reading public key, on failure resort to loading private key */ |
6d382c74 | 608 | EVP_PKEY *load_pubkey(const char *uri, int format, int maybe_stdin, |
9d5aca65 | 609 | const char *pass, ENGINE *e, const char *desc) |
0f113f3e | 610 | { |
0f113f3e | 611 | EVP_PKEY *pkey = NULL; |
f91d003a | 612 | char *allocated_uri = NULL; |
0f113f3e | 613 | |
6d382c74 DDO |
614 | if (desc == NULL) |
615 | desc = "public key"; | |
0f113f3e | 616 | |
0e89b396 | 617 | if (format == FORMAT_ENGINE) |
f91d003a | 618 | uri = allocated_uri = make_engine_uri(e, uri, desc); |
0e89b396 | 619 | (void)load_key_certs_crls(uri, format, maybe_stdin, pass, desc, 1, |
f91d003a | 620 | NULL, &pkey, NULL, NULL, NULL, NULL, NULL); |
0e89b396 DDO |
621 | if (pkey == NULL) |
622 | (void)load_key_certs_crls(uri, format, maybe_stdin, pass, desc, 0, | |
623 | &pkey, NULL, NULL, NULL, NULL, NULL, NULL); | |
f91d003a | 624 | OPENSSL_free(allocated_uri); |
26a7d938 | 625 | return pkey; |
0f113f3e | 626 | } |
bd08a2bd | 627 | |
ef044913 | 628 | EVP_PKEY *load_keyparams_suppress(const char *uri, int format, int maybe_stdin, |
8cdb993d DDO |
629 | const char *keytype, const char *desc, |
630 | int suppress_decode_errors) | |
b78c777e RL |
631 | { |
632 | EVP_PKEY *params = NULL; | |
633 | ||
634 | if (desc == NULL) | |
635 | desc = "key parameters"; | |
6e249947 | 636 | (void)load_key_certs_crls(uri, format, maybe_stdin, NULL, desc, |
0e89b396 | 637 | suppress_decode_errors, |
6e249947 | 638 | NULL, NULL, ¶ms, NULL, NULL, NULL, NULL); |
8b0ec099 | 639 | if (params != NULL && keytype != NULL && !EVP_PKEY_is_a(params, keytype)) { |
6e249947 DDO |
640 | ERR_print_errors(bio_err); |
641 | BIO_printf(bio_err, | |
642 | "Unable to load %s from %s (unexpected parameters type)\n", | |
643 | desc, uri); | |
8b0ec099 MC |
644 | EVP_PKEY_free(params); |
645 | params = NULL; | |
b78c777e RL |
646 | } |
647 | return params; | |
648 | } | |
649 | ||
ef044913 SL |
650 | EVP_PKEY *load_keyparams(const char *uri, int format, int maybe_stdin, |
651 | const char *keytype, const char *desc) | |
652 | { | |
653 | return load_keyparams_suppress(uri, format, maybe_stdin, keytype, desc, 0); | |
654 | } | |
655 | ||
e306f83c RL |
656 | void app_bail_out(char *fmt, ...) |
657 | { | |
658 | va_list args; | |
659 | ||
660 | va_start(args, fmt); | |
661 | BIO_vprintf(bio_err, fmt, args); | |
662 | va_end(args); | |
663 | ERR_print_errors(bio_err); | |
a7e4ca5b | 664 | exit(EXIT_FAILURE); |
e306f83c RL |
665 | } |
666 | ||
b1c908f4 | 667 | void *app_malloc(size_t sz, const char *what) |
68dc6824 RS |
668 | { |
669 | void *vp = OPENSSL_malloc(sz); | |
670 | ||
e306f83c | 671 | if (vp == NULL) |
b1c908f4 | 672 | app_bail_out("%s: Could not allocate %zu bytes for %s\n", |
e306f83c | 673 | opt_getprog(), sz, what); |
68dc6824 RS |
674 | return vp; |
675 | } | |
676 | ||
f62846b7 DDO |
677 | char *next_item(char *opt) /* in list separated by comma and/or space */ |
678 | { | |
679 | /* advance to separator (comma or whitespace), if any */ | |
db89d8f0 | 680 | while (*opt != ',' && !isspace(*opt) && *opt != '\0') |
f62846b7 | 681 | opt++; |
f62846b7 DDO |
682 | if (*opt != '\0') { |
683 | /* terminate current item */ | |
684 | *opt++ = '\0'; | |
685 | /* skip over any whitespace after separator */ | |
686 | while (isspace(*opt)) | |
687 | opt++; | |
688 | } | |
689 | return *opt == '\0' ? NULL : opt; /* NULL indicates end of input */ | |
690 | } | |
691 | ||
692 | static void warn_cert_msg(const char *uri, X509 *cert, const char *msg) | |
693 | { | |
694 | char *subj = X509_NAME_oneline(X509_get_subject_name(cert), NULL, 0); | |
695 | ||
49f07be4 | 696 | BIO_printf(bio_err, "Warning: certificate from '%s' with subject '%s' %s\n", |
f62846b7 DDO |
697 | uri, subj, msg); |
698 | OPENSSL_free(subj); | |
699 | } | |
700 | ||
701 | static void warn_cert(const char *uri, X509 *cert, int warn_EE, | |
702 | X509_VERIFY_PARAM *vpm) | |
703 | { | |
704 | uint32_t ex_flags = X509_get_extension_flags(cert); | |
705 | int res = X509_cmp_timeframe(vpm, X509_get0_notBefore(cert), | |
706 | X509_get0_notAfter(cert)); | |
707 | ||
708 | if (res != 0) | |
709 | warn_cert_msg(uri, cert, res > 0 ? "has expired" : "not yet valid"); | |
710 | if (warn_EE && (ex_flags & EXFLAG_V1) == 0 && (ex_flags & EXFLAG_CA) == 0) | |
711 | warn_cert_msg(uri, cert, "is not a CA cert"); | |
712 | } | |
713 | ||
714 | static void warn_certs(const char *uri, STACK_OF(X509) *certs, int warn_EE, | |
715 | X509_VERIFY_PARAM *vpm) | |
716 | { | |
717 | int i; | |
718 | ||
719 | for (i = 0; i < sk_X509_num(certs); i++) | |
720 | warn_cert(uri, sk_X509_value(certs, i), warn_EE, vpm); | |
721 | } | |
722 | ||
723 | int load_cert_certs(const char *uri, | |
724 | X509 **pcert, STACK_OF(X509) **pcerts, | |
725 | int exclude_http, const char *pass, const char *desc, | |
726 | X509_VERIFY_PARAM *vpm) | |
727 | { | |
728 | int ret = 0; | |
729 | char *pass_string; | |
730 | ||
6e249947 DDO |
731 | if (desc == NULL) |
732 | desc = pcerts == NULL ? "certificate" : "certificates"; | |
747adb6a DDO |
733 | if (exclude_http && (HAS_CASE_PREFIX(uri, "http://") |
734 | || HAS_CASE_PREFIX(uri, "https://"))) { | |
f62846b7 DDO |
735 | BIO_printf(bio_err, "error: HTTP retrieval not allowed for %s\n", desc); |
736 | return ret; | |
737 | } | |
738 | pass_string = get_passwd(pass, desc); | |
0e89b396 | 739 | ret = load_key_certs_crls(uri, FORMAT_UNDEF, 0, pass_string, desc, 0, |
6e249947 | 740 | NULL, NULL, NULL, pcert, pcerts, NULL, NULL); |
f62846b7 DDO |
741 | clear_free(pass_string); |
742 | ||
743 | if (ret) { | |
744 | if (pcert != NULL) | |
745 | warn_cert(uri, *pcert, 0, vpm); | |
8c870f6b P |
746 | if (pcerts != NULL) |
747 | warn_certs(uri, *pcerts, 1, vpm); | |
f62846b7 | 748 | } else { |
8c870f6b P |
749 | if (pcerts != NULL) { |
750 | OSSL_STACK_OF_X509_free(*pcerts); | |
751 | *pcerts = NULL; | |
752 | } | |
f62846b7 DDO |
753 | } |
754 | return ret; | |
755 | } | |
756 | ||
757 | STACK_OF(X509) *load_certs_multifile(char *files, const char *pass, | |
758 | const char *desc, X509_VERIFY_PARAM *vpm) | |
759 | { | |
760 | STACK_OF(X509) *certs = NULL; | |
761 | STACK_OF(X509) *result = sk_X509_new_null(); | |
762 | ||
763 | if (files == NULL) | |
764 | goto err; | |
765 | if (result == NULL) | |
766 | goto oom; | |
767 | ||
768 | while (files != NULL) { | |
769 | char *next = next_item(files); | |
770 | ||
771 | if (!load_cert_certs(files, NULL, &certs, 0, pass, desc, vpm)) | |
772 | goto err; | |
773 | if (!X509_add_certs(result, certs, | |
774 | X509_ADD_FLAG_UP_REF | X509_ADD_FLAG_NO_DUP)) | |
775 | goto oom; | |
79b2a2f2 | 776 | OSSL_STACK_OF_X509_free(certs); |
f62846b7 DDO |
777 | certs = NULL; |
778 | files = next; | |
779 | } | |
780 | return result; | |
781 | ||
782 | oom: | |
783 | BIO_printf(bio_err, "out of memory\n"); | |
784 | err: | |
79b2a2f2 DDO |
785 | OSSL_STACK_OF_X509_free(certs); |
786 | OSSL_STACK_OF_X509_free(result); | |
f62846b7 DDO |
787 | return NULL; |
788 | } | |
789 | ||
790 | static X509_STORE *sk_X509_to_store(X509_STORE *store /* may be NULL */, | |
791 | const STACK_OF(X509) *certs /* may NULL */) | |
792 | { | |
793 | int i; | |
794 | ||
795 | if (store == NULL) | |
796 | store = X509_STORE_new(); | |
797 | if (store == NULL) | |
798 | return NULL; | |
799 | for (i = 0; i < sk_X509_num(certs); i++) { | |
800 | if (!X509_STORE_add_cert(store, sk_X509_value(certs, i))) { | |
801 | X509_STORE_free(store); | |
802 | return NULL; | |
803 | } | |
804 | } | |
805 | return store; | |
806 | } | |
807 | ||
808 | /* | |
809 | * Create cert store structure with certificates read from given file(s). | |
810 | * Returns pointer to created X509_STORE on success, NULL on error. | |
811 | */ | |
812 | X509_STORE *load_certstore(char *input, const char *pass, const char *desc, | |
813 | X509_VERIFY_PARAM *vpm) | |
814 | { | |
815 | X509_STORE *store = NULL; | |
816 | STACK_OF(X509) *certs = NULL; | |
817 | ||
818 | while (input != NULL) { | |
819 | char *next = next_item(input); | |
820 | int ok; | |
821 | ||
822 | if (!load_cert_certs(input, NULL, &certs, 1, pass, desc, vpm)) { | |
823 | X509_STORE_free(store); | |
824 | return NULL; | |
825 | } | |
826 | ok = (store = sk_X509_to_store(store, certs)) != NULL; | |
79b2a2f2 | 827 | OSSL_STACK_OF_X509_free(certs); |
f62846b7 DDO |
828 | certs = NULL; |
829 | if (!ok) | |
830 | return NULL; | |
831 | input = next; | |
832 | } | |
833 | return store; | |
834 | } | |
835 | ||
0996dc54 | 836 | /* |
6b4a77f5 | 837 | * Initialize or extend, if *certs != NULL, a certificate stack. |
c4adc5ba | 838 | * The caller is responsible for freeing *certs if its value is left not NULL. |
0996dc54 | 839 | */ |
ea51096e | 840 | int load_certs(const char *uri, int maybe_stdin, STACK_OF(X509) **certs, |
a773b52a | 841 | const char *pass, const char *desc) |
0f113f3e | 842 | { |
6e249947 DDO |
843 | int ret, was_NULL = *certs == NULL; |
844 | ||
845 | if (desc == NULL) | |
846 | desc = "certificates"; | |
0e89b396 | 847 | ret = load_key_certs_crls(uri, FORMAT_UNDEF, maybe_stdin, pass, desc, 0, |
6e249947 | 848 | NULL, NULL, NULL, NULL, certs, NULL, NULL); |
c4adc5ba DDO |
849 | |
850 | if (!ret && was_NULL) { | |
79b2a2f2 | 851 | OSSL_STACK_OF_X509_free(*certs); |
c4adc5ba DDO |
852 | *certs = NULL; |
853 | } | |
854 | return ret; | |
0f113f3e | 855 | } |
245d2ee3 | 856 | |
0996dc54 | 857 | /* |
6b4a77f5 | 858 | * Initialize or extend, if *crls != NULL, a certificate stack. |
c4adc5ba | 859 | * The caller is responsible for freeing *crls if its value is left not NULL. |
0996dc54 | 860 | */ |
b3c5aadf | 861 | int load_crls(const char *uri, STACK_OF(X509_CRL) **crls, |
a773b52a | 862 | const char *pass, const char *desc) |
0f113f3e | 863 | { |
6e249947 DDO |
864 | int ret, was_NULL = *crls == NULL; |
865 | ||
866 | if (desc == NULL) | |
867 | desc = "CRLs"; | |
0e89b396 | 868 | ret = load_key_certs_crls(uri, FORMAT_UNDEF, 0, pass, desc, 0, |
6e249947 | 869 | NULL, NULL, NULL, NULL, NULL, NULL, crls); |
c4adc5ba DDO |
870 | |
871 | if (!ret && was_NULL) { | |
872 | sk_X509_CRL_pop_free(*crls, X509_CRL_free); | |
873 | *crls = NULL; | |
874 | } | |
875 | return ret; | |
0f113f3e MC |
876 | } |
877 | ||
d382e796 TM |
878 | static const char *format2string(int format) |
879 | { | |
1287dabd | 880 | switch (format) { |
d382e796 TM |
881 | case FORMAT_PEM: |
882 | return "PEM"; | |
883 | case FORMAT_ASN1: | |
884 | return "DER"; | |
885 | } | |
886 | return NULL; | |
887 | } | |
888 | ||
6c3d101a | 889 | /* Set type expectation, but clear it if objects of different types expected. */ |
8cdb993d DDO |
890 | #define SET_EXPECT(val) \ |
891 | (expect = expect < 0 ? (val) : (expect == (val) ? (val) : 0)) | |
892 | #define SET_EXPECT1(pvar, val) \ | |
893 | if ((pvar) != NULL) { \ | |
894 | *(pvar) = NULL; \ | |
895 | SET_EXPECT(val); \ | |
896 | } | |
897 | #define FAIL_NAME \ | |
898 | (ppkey != NULL ? "key etc." : ppubkey != NULL ? "public key etc." : \ | |
899 | pparams != NULL ? "params etc." : \ | |
900 | pcert != NULL ? "cert etc." : pcerts != NULL ? "certs etc." : \ | |
901 | pcrl != NULL ? "CRL etc." : pcrls != NULL ? "CRLs etc." : NULL) | |
6d382c74 DDO |
902 | /* |
903 | * Load those types of credentials for which the result pointer is not NULL. | |
904 | * Reads from stdio if uri is NULL and maybe_stdin is nonzero. | |
b3c5aadf DDO |
905 | * For non-NULL ppkey, pcert, and pcrl the first suitable value found is loaded. |
906 | * If pcerts is non-NULL and *pcerts == NULL then a new cert list is allocated. | |
907 | * If pcerts is non-NULL then all available certificates are appended to *pcerts | |
908 | * except any certificate assigned to *pcert. | |
909 | * If pcrls is non-NULL and *pcrls == NULL then a new list of CRLs is allocated. | |
910 | * If pcrls is non-NULL then all available CRLs are appended to *pcerts | |
911 | * except any CRL assigned to *pcrl. | |
912 | * In any case (also on error) the caller is responsible for freeing all members | |
913 | * of *pcerts and *pcrls (as far as they are not NULL). | |
6d382c74 | 914 | */ |
6e249947 | 915 | int load_key_certs_crls(const char *uri, int format, int maybe_stdin, |
0e89b396 DDO |
916 | const char *pass, const char *desc, int quiet, |
917 | EVP_PKEY **ppkey, EVP_PKEY **ppubkey, | |
918 | EVP_PKEY **pparams, | |
6e249947 DDO |
919 | X509 **pcert, STACK_OF(X509) **pcerts, |
920 | X509_CRL **pcrl, STACK_OF(X509_CRL) **pcrls) | |
6d382c74 DDO |
921 | { |
922 | PW_CB_DATA uidata; | |
923 | OSSL_STORE_CTX *ctx = NULL; | |
b4250010 | 924 | OSSL_LIB_CTX *libctx = app_get0_libctx(); |
6725682d | 925 | const char *propq = app_get0_propq(); |
8cdb993d DDO |
926 | int ncerts = 0, ncrls = 0, expect = -1; |
927 | const char *failed = FAIL_NAME; | |
d382e796 TM |
928 | const char *input_type; |
929 | OSSL_PARAM itp[2]; | |
930 | const OSSL_PARAM *params = NULL; | |
6d382c74 | 931 | |
8cdb993d | 932 | if (failed == NULL) { |
0e89b396 DDO |
933 | if (!quiet) |
934 | BIO_printf(bio_err, "Internal error: nothing to load from %s\n", | |
935 | uri != NULL ? uri : "<stdin>"); | |
8cdb993d | 936 | return 0; |
f91d003a | 937 | } |
8cdb993d DDO |
938 | ERR_set_mark(); |
939 | ||
940 | SET_EXPECT1(ppkey, OSSL_STORE_INFO_PKEY); | |
941 | SET_EXPECT1(ppubkey, OSSL_STORE_INFO_PUBKEY); | |
942 | SET_EXPECT1(pparams, OSSL_STORE_INFO_PARAMS); | |
943 | SET_EXPECT1(pcert, OSSL_STORE_INFO_CERT); | |
3a2171f6 MC |
944 | if (pcerts != NULL) { |
945 | if (*pcerts == NULL && (*pcerts = sk_X509_new_null()) == NULL) { | |
0e89b396 DDO |
946 | if (!quiet) |
947 | BIO_printf(bio_err, "Out of memory loading"); | |
3a2171f6 MC |
948 | goto end; |
949 | } | |
8cdb993d | 950 | SET_EXPECT(OSSL_STORE_INFO_CERT); |
f91d003a | 951 | } |
8cdb993d | 952 | SET_EXPECT1(pcrl, OSSL_STORE_INFO_CRL); |
3a2171f6 MC |
953 | if (pcrls != NULL) { |
954 | if (*pcrls == NULL && (*pcrls = sk_X509_CRL_new_null()) == NULL) { | |
0e89b396 DDO |
955 | if (!quiet) |
956 | BIO_printf(bio_err, "Out of memory loading"); | |
3a2171f6 MC |
957 | goto end; |
958 | } | |
8cdb993d | 959 | SET_EXPECT(OSSL_STORE_INFO_CRL); |
b3c5aadf | 960 | } |
6d382c74 | 961 | |
6d382c74 DDO |
962 | uidata.password = pass; |
963 | uidata.prompt_info = uri; | |
964 | ||
d382e796 | 965 | if ((input_type = format2string(format)) != NULL) { |
8cdb993d DDO |
966 | itp[0] = OSSL_PARAM_construct_utf8_string(OSSL_STORE_PARAM_INPUT_TYPE, |
967 | (char *)input_type, 0); | |
968 | itp[1] = OSSL_PARAM_construct_end(); | |
969 | params = itp; | |
d382e796 TM |
970 | } |
971 | ||
6d382c74 DDO |
972 | if (uri == NULL) { |
973 | BIO *bio; | |
974 | ||
975 | if (!maybe_stdin) { | |
0e89b396 DDO |
976 | if (!quiet) |
977 | BIO_printf(bio_err, "No filename or uri specified for loading"); | |
6d382c74 DDO |
978 | goto end; |
979 | } | |
50eb2a50 | 980 | uri = "<stdin>"; |
6d382c74 DDO |
981 | unbuffer(stdin); |
982 | bio = BIO_new_fp(stdin, 0); | |
857c223b | 983 | if (bio != NULL) { |
6725682d | 984 | ctx = OSSL_STORE_attach(bio, "file", libctx, propq, |
d382e796 TM |
985 | get_ui_method(), &uidata, params, |
986 | NULL, NULL); | |
857c223b SL |
987 | BIO_free(bio); |
988 | } | |
6d382c74 | 989 | } else { |
d8652be0 | 990 | ctx = OSSL_STORE_open_ex(uri, libctx, propq, get_ui_method(), &uidata, |
d382e796 | 991 | params, NULL, NULL); |
6d382c74 DDO |
992 | } |
993 | if (ctx == NULL) { | |
0e89b396 DDO |
994 | if (!quiet) |
995 | BIO_printf(bio_err, "Could not open file or uri for loading"); | |
6d382c74 DDO |
996 | goto end; |
997 | } | |
6c3d101a | 998 | if (expect > 0 && !OSSL_STORE_expect(ctx, expect)) |
f91d003a RL |
999 | goto end; |
1000 | ||
87495d56 | 1001 | failed = NULL; |
8cdb993d DDO |
1002 | while ((ppkey != NULL || ppubkey != NULL || pparams != NULL |
1003 | || pcert != NULL || pcerts != NULL || pcrl != NULL || pcrls != NULL) | |
1004 | && !OSSL_STORE_eof(ctx)) { | |
6d382c74 | 1005 | OSSL_STORE_INFO *info = OSSL_STORE_load(ctx); |
50eb2a50 | 1006 | int type, ok = 1; |
6d382c74 | 1007 | |
3a2171f6 MC |
1008 | /* |
1009 | * This can happen (for example) if we attempt to load a file with | |
1010 | * multiple different types of things in it - but the thing we just | |
1011 | * tried to load wasn't one of the ones we wanted, e.g. if we're trying | |
1012 | * to load a certificate but the file has both the private key and the | |
1013 | * certificate in it. We just retry until eof. | |
1014 | */ | |
1015 | if (info == NULL) { | |
3a2171f6 MC |
1016 | continue; |
1017 | } | |
1018 | ||
50eb2a50 | 1019 | type = OSSL_STORE_INFO_get_type(info); |
6d382c74 DDO |
1020 | switch (type) { |
1021 | case OSSL_STORE_INFO_PKEY: | |
8cdb993d | 1022 | if (ppkey != NULL) { |
b3c5aadf | 1023 | ok = (*ppkey = OSSL_STORE_INFO_get1_PKEY(info)) != NULL; |
8cdb993d DDO |
1024 | if (ok) |
1025 | ppkey = NULL; | |
1026 | break; | |
d8a809db | 1027 | } |
2274d22d RL |
1028 | /* |
1029 | * An EVP_PKEY with private parts also holds the public parts, | |
1030 | * so if the caller asked for a public key, and we got a private | |
1031 | * key, we can still pass it back. | |
1032 | */ | |
9929c817 | 1033 | /* fall through */ |
2274d22d | 1034 | case OSSL_STORE_INFO_PUBKEY: |
8cdb993d DDO |
1035 | if (ppubkey != NULL) { |
1036 | ok = (*ppubkey = OSSL_STORE_INFO_get1_PUBKEY(info)) != NULL; | |
1037 | if (ok) | |
1038 | ppubkey = NULL; | |
d8a809db | 1039 | } |
6d382c74 | 1040 | break; |
b78c777e | 1041 | case OSSL_STORE_INFO_PARAMS: |
8cdb993d DDO |
1042 | if (pparams != NULL) { |
1043 | ok = (*pparams = OSSL_STORE_INFO_get1_PARAMS(info)) != NULL; | |
1044 | if (ok) | |
1045 | pparams = NULL; | |
d8a809db | 1046 | } |
b78c777e | 1047 | break; |
6d382c74 | 1048 | case OSSL_STORE_INFO_CERT: |
8cdb993d | 1049 | if (pcert != NULL) { |
b3c5aadf | 1050 | ok = (*pcert = OSSL_STORE_INFO_get1_CERT(info)) != NULL; |
8cdb993d DDO |
1051 | if (ok) |
1052 | pcert = NULL; | |
1053 | } else if (pcerts != NULL) { | |
b3c5aadf DDO |
1054 | ok = X509_add_cert(*pcerts, |
1055 | OSSL_STORE_INFO_get1_CERT(info), | |
1056 | X509_ADD_FLAG_DEFAULT); | |
8cdb993d | 1057 | } |
b3c5aadf | 1058 | ncerts += ok; |
6d382c74 DDO |
1059 | break; |
1060 | case OSSL_STORE_INFO_CRL: | |
8cdb993d | 1061 | if (pcrl != NULL) { |
b3c5aadf | 1062 | ok = (*pcrl = OSSL_STORE_INFO_get1_CRL(info)) != NULL; |
8cdb993d DDO |
1063 | if (ok) |
1064 | pcrl = NULL; | |
1065 | } else if (pcrls != NULL) { | |
b3c5aadf | 1066 | ok = sk_X509_CRL_push(*pcrls, OSSL_STORE_INFO_get1_CRL(info)); |
8cdb993d | 1067 | } |
b3c5aadf | 1068 | ncrls += ok; |
6d382c74 DDO |
1069 | break; |
1070 | default: | |
1071 | /* skip any other type */ | |
1072 | break; | |
1073 | } | |
1074 | OSSL_STORE_INFO_free(info); | |
b3c5aadf | 1075 | if (!ok) { |
8cdb993d | 1076 | failed = OSSL_STORE_INFO_type_string(type); |
0e89b396 DDO |
1077 | if (!quiet) |
1078 | BIO_printf(bio_err, "Error reading"); | |
6d382c74 DDO |
1079 | break; |
1080 | } | |
1081 | } | |
1082 | ||
1083 | end: | |
1084 | OSSL_STORE_close(ctx); | |
8cdb993d DDO |
1085 | if (ncerts > 0) |
1086 | pcerts = NULL; | |
1087 | if (ncrls > 0) | |
1088 | pcrls = NULL; | |
87495d56 | 1089 | if (failed == NULL) { |
8cdb993d | 1090 | failed = FAIL_NAME; |
0e89b396 | 1091 | if (failed != NULL && !quiet) |
fedab100 | 1092 | BIO_printf(bio_err, "Could not find"); |
0e89b396 | 1093 | } else if (!quiet) { |
fedab100 | 1094 | BIO_printf(bio_err, "Could not read"); |
b3c5aadf | 1095 | } |
0e89b396 | 1096 | if (failed != NULL && !quiet) { |
6e249947 DDO |
1097 | unsigned long err = ERR_peek_last_error(); |
1098 | ||
50eb2a50 DDO |
1099 | if (desc != NULL && strstr(desc, failed) != NULL) { |
1100 | BIO_printf(bio_err, " %s", desc); | |
1101 | } else { | |
1102 | BIO_printf(bio_err, " %s", failed); | |
1103 | if (desc != NULL) | |
1104 | BIO_printf(bio_err, " of %s", desc); | |
1105 | } | |
1106 | if (uri != NULL) | |
1107 | BIO_printf(bio_err, " from %s", uri); | |
6e249947 DDO |
1108 | if (ERR_SYSTEM_ERROR(err)) { |
1109 | /* provide more readable diagnostic output */ | |
1110 | BIO_printf(bio_err, ": %s", strerror(ERR_GET_REASON(err))); | |
1111 | ERR_pop_to_mark(); | |
1112 | ERR_set_mark(); | |
1113 | } | |
50eb2a50 | 1114 | BIO_printf(bio_err, "\n"); |
87495d56 | 1115 | ERR_print_errors(bio_err); |
50eb2a50 | 1116 | } |
0e89b396 | 1117 | if (quiet || failed == NULL) |
6e249947 DDO |
1118 | /* clear any suppressed or spurious errors */ |
1119 | ERR_pop_to_mark(); | |
1120 | else | |
1121 | ERR_clear_last_mark(); | |
b3c5aadf | 1122 | return failed == NULL; |
6d382c74 DDO |
1123 | } |
1124 | ||
8cdb993d DDO |
1125 | #define X509V3_EXT_UNKNOWN_MASK (0xfL << 16) |
1126 | #define X509V3_EXT_DEFAULT 0 /* Return error for unknown exts */ | |
1127 | #define X509V3_EXT_ERROR_UNKNOWN (1L << 16) /* Print error for unknown exts */ | |
1128 | #define X509V3_EXT_PARSE_UNKNOWN (2L << 16) /* ASN1 parse unknown extensions */ | |
1129 | #define X509V3_EXT_DUMP_UNKNOWN (3L << 16) /* BIO_dump unknown extensions */ | |
8ca533e3 | 1130 | |
535d79da | 1131 | #define X509_FLAG_CA (X509_FLAG_NO_ISSUER | X509_FLAG_NO_PUBKEY | \ |
8cdb993d | 1132 | X509_FLAG_NO_HEADER | X509_FLAG_NO_VERSION) |
535d79da | 1133 | |
8ca533e3 DSH |
1134 | int set_cert_ex(unsigned long *flags, const char *arg) |
1135 | { | |
0f113f3e MC |
1136 | static const NAME_EX_TBL cert_tbl[] = { |
1137 | {"compatible", X509_FLAG_COMPAT, 0xffffffffl}, | |
1138 | {"ca_default", X509_FLAG_CA, 0xffffffffl}, | |
1139 | {"no_header", X509_FLAG_NO_HEADER, 0}, | |
1140 | {"no_version", X509_FLAG_NO_VERSION, 0}, | |
1141 | {"no_serial", X509_FLAG_NO_SERIAL, 0}, | |
1142 | {"no_signame", X509_FLAG_NO_SIGNAME, 0}, | |
1143 | {"no_validity", X509_FLAG_NO_VALIDITY, 0}, | |
1144 | {"no_subject", X509_FLAG_NO_SUBJECT, 0}, | |
1145 | {"no_issuer", X509_FLAG_NO_ISSUER, 0}, | |
1146 | {"no_pubkey", X509_FLAG_NO_PUBKEY, 0}, | |
1147 | {"no_extensions", X509_FLAG_NO_EXTENSIONS, 0}, | |
1148 | {"no_sigdump", X509_FLAG_NO_SIGDUMP, 0}, | |
1149 | {"no_aux", X509_FLAG_NO_AUX, 0}, | |
1150 | {"no_attributes", X509_FLAG_NO_ATTRIBUTES, 0}, | |
1151 | {"ext_default", X509V3_EXT_DEFAULT, X509V3_EXT_UNKNOWN_MASK}, | |
1152 | {"ext_error", X509V3_EXT_ERROR_UNKNOWN, X509V3_EXT_UNKNOWN_MASK}, | |
1153 | {"ext_parse", X509V3_EXT_PARSE_UNKNOWN, X509V3_EXT_UNKNOWN_MASK}, | |
1154 | {"ext_dump", X509V3_EXT_DUMP_UNKNOWN, X509V3_EXT_UNKNOWN_MASK}, | |
1155 | {NULL, 0, 0} | |
1156 | }; | |
1157 | return set_multi_opts(flags, arg, cert_tbl); | |
8ca533e3 | 1158 | } |
a657546f DSH |
1159 | |
1160 | int set_name_ex(unsigned long *flags, const char *arg) | |
1161 | { | |
0f113f3e MC |
1162 | static const NAME_EX_TBL ex_tbl[] = { |
1163 | {"esc_2253", ASN1_STRFLGS_ESC_2253, 0}, | |
bc776510 | 1164 | {"esc_2254", ASN1_STRFLGS_ESC_2254, 0}, |
0f113f3e MC |
1165 | {"esc_ctrl", ASN1_STRFLGS_ESC_CTRL, 0}, |
1166 | {"esc_msb", ASN1_STRFLGS_ESC_MSB, 0}, | |
1167 | {"use_quote", ASN1_STRFLGS_ESC_QUOTE, 0}, | |
1168 | {"utf8", ASN1_STRFLGS_UTF8_CONVERT, 0}, | |
1169 | {"ignore_type", ASN1_STRFLGS_IGNORE_TYPE, 0}, | |
1170 | {"show_type", ASN1_STRFLGS_SHOW_TYPE, 0}, | |
1171 | {"dump_all", ASN1_STRFLGS_DUMP_ALL, 0}, | |
1172 | {"dump_nostr", ASN1_STRFLGS_DUMP_UNKNOWN, 0}, | |
1173 | {"dump_der", ASN1_STRFLGS_DUMP_DER, 0}, | |
1174 | {"compat", XN_FLAG_COMPAT, 0xffffffffL}, | |
1175 | {"sep_comma_plus", XN_FLAG_SEP_COMMA_PLUS, XN_FLAG_SEP_MASK}, | |
1176 | {"sep_comma_plus_space", XN_FLAG_SEP_CPLUS_SPC, XN_FLAG_SEP_MASK}, | |
1177 | {"sep_semi_plus_space", XN_FLAG_SEP_SPLUS_SPC, XN_FLAG_SEP_MASK}, | |
1178 | {"sep_multiline", XN_FLAG_SEP_MULTILINE, XN_FLAG_SEP_MASK}, | |
1179 | {"dn_rev", XN_FLAG_DN_REV, 0}, | |
1180 | {"nofname", XN_FLAG_FN_NONE, XN_FLAG_FN_MASK}, | |
1181 | {"sname", XN_FLAG_FN_SN, XN_FLAG_FN_MASK}, | |
1182 | {"lname", XN_FLAG_FN_LN, XN_FLAG_FN_MASK}, | |
1183 | {"align", XN_FLAG_FN_ALIGN, 0}, | |
1184 | {"oid", XN_FLAG_FN_OID, XN_FLAG_FN_MASK}, | |
1185 | {"space_eq", XN_FLAG_SPC_EQ, 0}, | |
1186 | {"dump_unknown", XN_FLAG_DUMP_UNKNOWN_FIELDS, 0}, | |
1187 | {"RFC2253", XN_FLAG_RFC2253, 0xffffffffL}, | |
1188 | {"oneline", XN_FLAG_ONELINE, 0xffffffffL}, | |
1189 | {"multiline", XN_FLAG_MULTILINE, 0xffffffffL}, | |
1190 | {"ca_default", XN_FLAG_MULTILINE, 0xffffffffL}, | |
1191 | {NULL, 0, 0} | |
1192 | }; | |
03706afa DSH |
1193 | if (set_multi_opts(flags, arg, ex_tbl) == 0) |
1194 | return 0; | |
3190d1dc RL |
1195 | if (*flags != XN_FLAG_COMPAT |
1196 | && (*flags & XN_FLAG_SEP_MASK) == 0) | |
03706afa DSH |
1197 | *flags |= XN_FLAG_SEP_CPLUS_SPC; |
1198 | return 1; | |
535d79da DSH |
1199 | } |
1200 | ||
8c5bff22 WE |
1201 | int set_dateopt(unsigned long *dateopt, const char *arg) |
1202 | { | |
fba140c7 | 1203 | if (OPENSSL_strcasecmp(arg, "rfc_822") == 0) |
8c5bff22 | 1204 | *dateopt = ASN1_DTFLGS_RFC822; |
fba140c7 | 1205 | else if (OPENSSL_strcasecmp(arg, "iso_8601") == 0) |
8c5bff22 | 1206 | *dateopt = ASN1_DTFLGS_ISO8601; |
55b7fa26 HH |
1207 | else |
1208 | return 0; | |
1209 | return 1; | |
8c5bff22 WE |
1210 | } |
1211 | ||
791bd0cd DSH |
1212 | int set_ext_copy(int *copy_type, const char *arg) |
1213 | { | |
fba140c7 | 1214 | if (OPENSSL_strcasecmp(arg, "none") == 0) |
0f113f3e | 1215 | *copy_type = EXT_COPY_NONE; |
fba140c7 | 1216 | else if (OPENSSL_strcasecmp(arg, "copy") == 0) |
0f113f3e | 1217 | *copy_type = EXT_COPY_ADD; |
fba140c7 | 1218 | else if (OPENSSL_strcasecmp(arg, "copyall") == 0) |
0f113f3e MC |
1219 | *copy_type = EXT_COPY_ALL; |
1220 | else | |
1221 | return 0; | |
1222 | return 1; | |
791bd0cd DSH |
1223 | } |
1224 | ||
1225 | int copy_extensions(X509 *x, X509_REQ *req, int copy_type) | |
1226 | { | |
03f4e3de DDO |
1227 | STACK_OF(X509_EXTENSION) *exts; |
1228 | int i, ret = 0; | |
1229 | ||
1230 | if (x == NULL || req == NULL) | |
1231 | return 0; | |
1232 | if (copy_type == EXT_COPY_NONE) | |
0f113f3e MC |
1233 | return 1; |
1234 | exts = X509_REQ_get_extensions(req); | |
1235 | ||
1236 | for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) { | |
03f4e3de DDO |
1237 | X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i); |
1238 | ASN1_OBJECT *obj = X509_EXTENSION_get_object(ext); | |
1239 | int idx = X509_get_ext_by_OBJ(x, obj, -1); | |
1240 | ||
1241 | /* Does extension exist in target? */ | |
0f113f3e MC |
1242 | if (idx != -1) { |
1243 | /* If normal copy don't override existing extension */ | |
1244 | if (copy_type == EXT_COPY_ADD) | |
1245 | continue; | |
1246 | /* Delete all extensions of same type */ | |
1247 | do { | |
05458fdb | 1248 | X509_EXTENSION_free(X509_delete_ext(x, idx)); |
0f113f3e MC |
1249 | idx = X509_get_ext_by_OBJ(x, obj, -1); |
1250 | } while (idx != -1); | |
1251 | } | |
1252 | if (!X509_add_ext(x, ext, -1)) | |
1253 | goto end; | |
1254 | } | |
0f113f3e MC |
1255 | ret = 1; |
1256 | ||
1257 | end: | |
0f113f3e | 1258 | sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free); |
0f113f3e MC |
1259 | return ret; |
1260 | } | |
1261 | ||
1262 | static int set_multi_opts(unsigned long *flags, const char *arg, | |
1263 | const NAME_EX_TBL * in_tbl) | |
1264 | { | |
1265 | STACK_OF(CONF_VALUE) *vals; | |
1266 | CONF_VALUE *val; | |
1267 | int i, ret = 1; | |
8cdb993d | 1268 | |
0f113f3e MC |
1269 | if (!arg) |
1270 | return 0; | |
1271 | vals = X509V3_parse_list(arg); | |
1272 | for (i = 0; i < sk_CONF_VALUE_num(vals); i++) { | |
1273 | val = sk_CONF_VALUE_value(vals, i); | |
1274 | if (!set_table_opts(flags, val->name, in_tbl)) | |
1275 | ret = 0; | |
1276 | } | |
1277 | sk_CONF_VALUE_pop_free(vals, X509V3_conf_free); | |
1278 | return ret; | |
1279 | } | |
1280 | ||
1281 | static int set_table_opts(unsigned long *flags, const char *arg, | |
1282 | const NAME_EX_TBL * in_tbl) | |
1283 | { | |
1284 | char c; | |
1285 | const NAME_EX_TBL *ptbl; | |
0f113f3e | 1286 | |
8cdb993d | 1287 | c = arg[0]; |
0f113f3e MC |
1288 | if (c == '-') { |
1289 | c = 0; | |
1290 | arg++; | |
1291 | } else if (c == '+') { | |
1292 | c = 1; | |
1293 | arg++; | |
2234212c | 1294 | } else { |
0f113f3e | 1295 | c = 1; |
2234212c | 1296 | } |
0f113f3e MC |
1297 | |
1298 | for (ptbl = in_tbl; ptbl->name; ptbl++) { | |
fba140c7 | 1299 | if (OPENSSL_strcasecmp(arg, ptbl->name) == 0) { |
0f113f3e MC |
1300 | *flags &= ~ptbl->mask; |
1301 | if (c) | |
1302 | *flags |= ptbl->flag; | |
1303 | else | |
1304 | *flags &= ~ptbl->flag; | |
1305 | return 1; | |
1306 | } | |
1307 | } | |
1308 | return 0; | |
1309 | } | |
1310 | ||
46a11faf | 1311 | void print_name(BIO *out, const char *title, const X509_NAME *nm) |
0f113f3e MC |
1312 | { |
1313 | char *buf; | |
1314 | char mline = 0; | |
1315 | int indent = 0; | |
46a11faf | 1316 | unsigned long lflags = get_nameopt(); |
e60e9744 | 1317 | |
32f7be2a DDO |
1318 | if (out == NULL) |
1319 | return; | |
46a11faf | 1320 | if (title != NULL) |
0f113f3e MC |
1321 | BIO_puts(out, title); |
1322 | if ((lflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) { | |
1323 | mline = 1; | |
1324 | indent = 4; | |
1325 | } | |
1326 | if (lflags == XN_FLAG_COMPAT) { | |
1327 | buf = X509_NAME_oneline(nm, 0, 0); | |
1328 | BIO_puts(out, buf); | |
1329 | BIO_puts(out, "\n"); | |
1330 | OPENSSL_free(buf); | |
1331 | } else { | |
1332 | if (mline) | |
1333 | BIO_puts(out, "\n"); | |
1334 | X509_NAME_print_ex(out, nm, indent, lflags); | |
1335 | BIO_puts(out, "\n"); | |
1336 | } | |
a657546f DSH |
1337 | } |
1338 | ||
2ac6115d | 1339 | void print_bignum_var(BIO *out, const BIGNUM *in, const char *var, |
7e1b7485 | 1340 | int len, unsigned char *buffer) |
81f169e9 | 1341 | { |
7e1b7485 | 1342 | BIO_printf(out, " static unsigned char %s_%d[] = {", var, len); |
2234212c | 1343 | if (BN_is_zero(in)) { |
06deb932 | 1344 | BIO_printf(out, "\n 0x00"); |
2234212c | 1345 | } else { |
7e1b7485 RS |
1346 | int i, l; |
1347 | ||
1348 | l = BN_bn2bin(in, buffer); | |
1349 | for (i = 0; i < l; i++) { | |
06deb932 | 1350 | BIO_printf(out, (i % 10) == 0 ? "\n " : " "); |
7e1b7485 | 1351 | if (i < l - 1) |
06deb932 | 1352 | BIO_printf(out, "0x%02X,", buffer[i]); |
7e1b7485 RS |
1353 | else |
1354 | BIO_printf(out, "0x%02X", buffer[i]); | |
1355 | } | |
1356 | } | |
1357 | BIO_printf(out, "\n };\n"); | |
1358 | } | |
2234212c | 1359 | |
8cdb993d | 1360 | void print_array(BIO *out, const char *title, int len, const unsigned char *d) |
7e1b7485 RS |
1361 | { |
1362 | int i; | |
1363 | ||
1364 | BIO_printf(out, "unsigned char %s[%d] = {", title, len); | |
1365 | for (i = 0; i < len; i++) { | |
1366 | if ((i % 10) == 0) | |
1367 | BIO_printf(out, "\n "); | |
1368 | if (i < len - 1) | |
1369 | BIO_printf(out, "0x%02X, ", d[i]); | |
1370 | else | |
1371 | BIO_printf(out, "0x%02X", d[i]); | |
1372 | } | |
1373 | BIO_printf(out, "\n};\n"); | |
1374 | } | |
1375 | ||
fd3397fc RL |
1376 | X509_STORE *setup_verify(const char *CAfile, int noCAfile, |
1377 | const char *CApath, int noCApath, | |
1378 | const char *CAstore, int noCAstore) | |
7e1b7485 RS |
1379 | { |
1380 | X509_STORE *store = X509_STORE_new(); | |
0f113f3e | 1381 | X509_LOOKUP *lookup; |
b4250010 | 1382 | OSSL_LIB_CTX *libctx = app_get0_libctx(); |
6725682d | 1383 | const char *propq = app_get0_propq(); |
7e1b7485 | 1384 | |
96487cdd | 1385 | if (store == NULL) |
0f113f3e | 1386 | goto end; |
2b6bcb70 | 1387 | |
e8aa8b6c | 1388 | if (CAfile != NULL || !noCAfile) { |
2b6bcb70 MC |
1389 | lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()); |
1390 | if (lookup == NULL) | |
0f113f3e | 1391 | goto end; |
fd3397fc | 1392 | if (CAfile != NULL) { |
e22ea36f | 1393 | if (X509_LOOKUP_load_file_ex(lookup, CAfile, X509_FILETYPE_PEM, |
0e89b396 | 1394 | libctx, propq) <= 0) { |
57c0205b DDO |
1395 | ERR_clear_error(); |
1396 | if (X509_LOOKUP_load_file_ex(lookup, CAfile, X509_FILETYPE_ASN1, | |
1397 | libctx, propq) <= 0) { | |
1398 | BIO_printf(bio_err, "Error loading file %s\n", CAfile); | |
1399 | goto end; | |
1400 | } | |
2b6bcb70 | 1401 | } |
2234212c | 1402 | } else { |
d8652be0 MC |
1403 | X509_LOOKUP_load_file_ex(lookup, NULL, X509_FILETYPE_DEFAULT, |
1404 | libctx, propq); | |
2234212c | 1405 | } |
2b6bcb70 | 1406 | } |
0f113f3e | 1407 | |
e8aa8b6c | 1408 | if (CApath != NULL || !noCApath) { |
2b6bcb70 MC |
1409 | lookup = X509_STORE_add_lookup(store, X509_LOOKUP_hash_dir()); |
1410 | if (lookup == NULL) | |
0f113f3e | 1411 | goto end; |
fd3397fc | 1412 | if (CApath != NULL) { |
e22ea36f | 1413 | if (X509_LOOKUP_add_dir(lookup, CApath, X509_FILETYPE_PEM) <= 0) { |
2b6bcb70 MC |
1414 | BIO_printf(bio_err, "Error loading directory %s\n", CApath); |
1415 | goto end; | |
1416 | } | |
2234212c | 1417 | } else { |
2b6bcb70 | 1418 | X509_LOOKUP_add_dir(lookup, NULL, X509_FILETYPE_DEFAULT); |
2234212c | 1419 | } |
2b6bcb70 | 1420 | } |
0f113f3e | 1421 | |
fd3397fc RL |
1422 | if (CAstore != NULL || !noCAstore) { |
1423 | lookup = X509_STORE_add_lookup(store, X509_LOOKUP_store()); | |
1424 | if (lookup == NULL) | |
1425 | goto end; | |
d8652be0 | 1426 | if (!X509_LOOKUP_add_store_ex(lookup, CAstore, libctx, propq)) { |
fd3397fc RL |
1427 | if (CAstore != NULL) |
1428 | BIO_printf(bio_err, "Error loading store URI %s\n", CAstore); | |
1429 | goto end; | |
1430 | } | |
1431 | } | |
1432 | ||
0f113f3e MC |
1433 | ERR_clear_error(); |
1434 | return store; | |
1435 | end: | |
01c12100 | 1436 | ERR_print_errors(bio_err); |
0f113f3e MC |
1437 | X509_STORE_free(store); |
1438 | return NULL; | |
81f169e9 | 1439 | } |
531d630b | 1440 | |
c869da88 | 1441 | static unsigned long index_serial_hash(const OPENSSL_CSTRING *a) |
0f113f3e MC |
1442 | { |
1443 | const char *n; | |
f85b68cd | 1444 | |
0f113f3e MC |
1445 | n = a[DB_serial]; |
1446 | while (*n == '0') | |
1447 | n++; | |
739a1eb1 | 1448 | return OPENSSL_LH_strhash(n); |
0f113f3e | 1449 | } |
f85b68cd | 1450 | |
0f113f3e MC |
1451 | static int index_serial_cmp(const OPENSSL_CSTRING *a, |
1452 | const OPENSSL_CSTRING *b) | |
1453 | { | |
1454 | const char *aa, *bb; | |
f85b68cd | 1455 | |
0f113f3e MC |
1456 | for (aa = a[DB_serial]; *aa == '0'; aa++) ; |
1457 | for (bb = b[DB_serial]; *bb == '0'; bb++) ; | |
26a7d938 | 1458 | return strcmp(aa, bb); |
0f113f3e | 1459 | } |
f85b68cd RL |
1460 | |
1461 | static int index_name_qual(char **a) | |
0f113f3e MC |
1462 | { |
1463 | return (a[0][0] == 'V'); | |
1464 | } | |
f85b68cd | 1465 | |
c869da88 | 1466 | static unsigned long index_name_hash(const OPENSSL_CSTRING *a) |
0f113f3e | 1467 | { |
739a1eb1 | 1468 | return OPENSSL_LH_strhash(a[DB_name]); |
0f113f3e | 1469 | } |
f85b68cd | 1470 | |
c869da88 | 1471 | int index_name_cmp(const OPENSSL_CSTRING *a, const OPENSSL_CSTRING *b) |
0f113f3e | 1472 | { |
26a7d938 | 1473 | return strcmp(a[DB_name], b[DB_name]); |
0f113f3e | 1474 | } |
f85b68cd | 1475 | |
c869da88 DSH |
1476 | static IMPLEMENT_LHASH_HASH_FN(index_serial, OPENSSL_CSTRING) |
1477 | static IMPLEMENT_LHASH_COMP_FN(index_serial, OPENSSL_CSTRING) | |
1478 | static IMPLEMENT_LHASH_HASH_FN(index_name, OPENSSL_CSTRING) | |
1479 | static IMPLEMENT_LHASH_COMP_FN(index_name, OPENSSL_CSTRING) | |
f85b68cd RL |
1480 | #undef BSIZE |
1481 | #define BSIZE 256 | |
ec8a3409 DDO |
1482 | BIGNUM *load_serial(const char *serialfile, int *exists, int create, |
1483 | ASN1_INTEGER **retai) | |
0f113f3e MC |
1484 | { |
1485 | BIO *in = NULL; | |
1486 | BIGNUM *ret = NULL; | |
68b00c23 | 1487 | char buf[1024]; |
0f113f3e MC |
1488 | ASN1_INTEGER *ai = NULL; |
1489 | ||
1490 | ai = ASN1_INTEGER_new(); | |
1491 | if (ai == NULL) | |
1492 | goto err; | |
1493 | ||
7e1b7485 | 1494 | in = BIO_new_file(serialfile, "r"); |
ec8a3409 DDO |
1495 | if (exists != NULL) |
1496 | *exists = in != NULL; | |
7e1b7485 | 1497 | if (in == NULL) { |
0f113f3e MC |
1498 | if (!create) { |
1499 | perror(serialfile); | |
1500 | goto err; | |
0f113f3e | 1501 | } |
7e1b7485 RS |
1502 | ERR_clear_error(); |
1503 | ret = BN_new(); | |
ec8a3409 | 1504 | if (ret == NULL) { |
7e1b7485 | 1505 | BIO_printf(bio_err, "Out of memory\n"); |
ec8a3409 DDO |
1506 | } else if (!rand_serial(ret, ai)) { |
1507 | BIO_printf(bio_err, "Error creating random number to store in %s\n", | |
1508 | serialfile); | |
1509 | BN_free(ret); | |
1510 | ret = NULL; | |
1511 | } | |
0f113f3e MC |
1512 | } else { |
1513 | if (!a2i_ASN1_INTEGER(in, ai, buf, 1024)) { | |
01c12100 | 1514 | BIO_printf(bio_err, "Unable to load number from %s\n", |
0f113f3e MC |
1515 | serialfile); |
1516 | goto err; | |
1517 | } | |
1518 | ret = ASN1_INTEGER_to_BN(ai, NULL); | |
1519 | if (ret == NULL) { | |
01c12100 | 1520 | BIO_printf(bio_err, "Error converting number from bin to BIGNUM\n"); |
0f113f3e MC |
1521 | goto err; |
1522 | } | |
1523 | } | |
1524 | ||
ec8a3409 | 1525 | if (ret != NULL && retai != NULL) { |
0f113f3e MC |
1526 | *retai = ai; |
1527 | ai = NULL; | |
1528 | } | |
f85b68cd | 1529 | err: |
ec8a3409 DDO |
1530 | if (ret == NULL) |
1531 | ERR_print_errors(bio_err); | |
ca3a82c3 | 1532 | BIO_free(in); |
2ace7450 | 1533 | ASN1_INTEGER_free(ai); |
26a7d938 | 1534 | return ret; |
0f113f3e MC |
1535 | } |
1536 | ||
8cdb993d DDO |
1537 | int save_serial(const char *serialfile, const char *suffix, |
1538 | const BIGNUM *serial, ASN1_INTEGER **retai) | |
0f113f3e MC |
1539 | { |
1540 | char buf[1][BSIZE]; | |
1541 | BIO *out = NULL; | |
1542 | int ret = 0; | |
1543 | ASN1_INTEGER *ai = NULL; | |
1544 | int j; | |
1545 | ||
1546 | if (suffix == NULL) | |
1547 | j = strlen(serialfile); | |
1548 | else | |
1549 | j = strlen(serialfile) + strlen(suffix) + 1; | |
1550 | if (j >= BSIZE) { | |
01c12100 | 1551 | BIO_printf(bio_err, "File name too long\n"); |
0f113f3e MC |
1552 | goto err; |
1553 | } | |
1554 | ||
8cdb993d | 1555 | if (suffix == NULL) { |
7644a9ae | 1556 | OPENSSL_strlcpy(buf[0], serialfile, BSIZE); |
8cdb993d | 1557 | } else { |
4c771796 | 1558 | #ifndef OPENSSL_SYS_VMS |
cbe29648 | 1559 | j = BIO_snprintf(buf[0], sizeof(buf[0]), "%s.%s", serialfile, suffix); |
4c771796 | 1560 | #else |
cbe29648 | 1561 | j = BIO_snprintf(buf[0], sizeof(buf[0]), "%s-%s", serialfile, suffix); |
4c771796 | 1562 | #endif |
0f113f3e | 1563 | } |
7e1b7485 | 1564 | out = BIO_new_file(buf[0], "w"); |
0f113f3e | 1565 | if (out == NULL) { |
0f113f3e MC |
1566 | goto err; |
1567 | } | |
0f113f3e MC |
1568 | |
1569 | if ((ai = BN_to_ASN1_INTEGER(serial, NULL)) == NULL) { | |
1570 | BIO_printf(bio_err, "error converting serial to ASN.1 format\n"); | |
1571 | goto err; | |
1572 | } | |
1573 | i2a_ASN1_INTEGER(out, ai); | |
1574 | BIO_puts(out, "\n"); | |
1575 | ret = 1; | |
1576 | if (retai) { | |
1577 | *retai = ai; | |
1578 | ai = NULL; | |
1579 | } | |
1580 | err: | |
01c12100 DDO |
1581 | if (!ret) |
1582 | ERR_print_errors(bio_err); | |
ca3a82c3 | 1583 | BIO_free_all(out); |
2ace7450 | 1584 | ASN1_INTEGER_free(ai); |
26a7d938 | 1585 | return ret; |
0f113f3e | 1586 | } |
f85b68cd | 1587 | |
cc696296 F |
1588 | int rotate_serial(const char *serialfile, const char *new_suffix, |
1589 | const char *old_suffix) | |
0f113f3e | 1590 | { |
bde136c8 | 1591 | char buf[2][BSIZE]; |
0f113f3e MC |
1592 | int i, j; |
1593 | ||
1594 | i = strlen(serialfile) + strlen(old_suffix); | |
1595 | j = strlen(serialfile) + strlen(new_suffix); | |
1596 | if (i > j) | |
1597 | j = i; | |
1598 | if (j + 1 >= BSIZE) { | |
01c12100 | 1599 | BIO_printf(bio_err, "File name too long\n"); |
0f113f3e MC |
1600 | goto err; |
1601 | } | |
4c771796 | 1602 | #ifndef OPENSSL_SYS_VMS |
cbe29648 RS |
1603 | j = BIO_snprintf(buf[0], sizeof(buf[0]), "%s.%s", serialfile, new_suffix); |
1604 | j = BIO_snprintf(buf[1], sizeof(buf[1]), "%s.%s", serialfile, old_suffix); | |
4c771796 | 1605 | #else |
cbe29648 RS |
1606 | j = BIO_snprintf(buf[0], sizeof(buf[0]), "%s-%s", serialfile, new_suffix); |
1607 | j = BIO_snprintf(buf[1], sizeof(buf[1]), "%s-%s", serialfile, old_suffix); | |
a1ad253f | 1608 | #endif |
0f113f3e | 1609 | if (rename(serialfile, buf[1]) < 0 && errno != ENOENT |
4c771796 | 1610 | #ifdef ENOTDIR |
0f113f3e MC |
1611 | && errno != ENOTDIR |
1612 | #endif | |
1613 | ) { | |
1614 | BIO_printf(bio_err, | |
01c12100 | 1615 | "Unable to rename %s to %s\n", serialfile, buf[1]); |
0f113f3e MC |
1616 | perror("reason"); |
1617 | goto err; | |
1618 | } | |
0f113f3e MC |
1619 | if (rename(buf[0], serialfile) < 0) { |
1620 | BIO_printf(bio_err, | |
01c12100 | 1621 | "Unable to rename %s to %s\n", buf[0], serialfile); |
0f113f3e MC |
1622 | perror("reason"); |
1623 | rename(buf[1], serialfile); | |
1624 | goto err; | |
1625 | } | |
1626 | return 1; | |
4c771796 | 1627 | err: |
01c12100 | 1628 | ERR_print_errors(bio_err); |
0f113f3e MC |
1629 | return 0; |
1630 | } | |
4c771796 | 1631 | |
64674bcc | 1632 | int rand_serial(BIGNUM *b, ASN1_INTEGER *ai) |
0f113f3e MC |
1633 | { |
1634 | BIGNUM *btmp; | |
1635 | int ret = 0; | |
23a1d5e9 | 1636 | |
ffb46830 | 1637 | btmp = b == NULL ? BN_new() : b; |
96487cdd | 1638 | if (btmp == NULL) |
0f113f3e MC |
1639 | return 0; |
1640 | ||
ffb46830 | 1641 | if (!BN_rand(btmp, SERIAL_RAND_BITS, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY)) |
0f113f3e MC |
1642 | goto error; |
1643 | if (ai && !BN_to_ASN1_INTEGER(btmp, ai)) | |
1644 | goto error; | |
1645 | ||
1646 | ret = 1; | |
1647 | ||
1648 | error: | |
1649 | ||
23a1d5e9 | 1650 | if (btmp != b) |
0f113f3e MC |
1651 | BN_free(btmp); |
1652 | ||
1653 | return ret; | |
1654 | } | |
64674bcc | 1655 | |
cc696296 | 1656 | CA_DB *load_index(const char *dbfile, DB_ATTR *db_attr) |
0f113f3e MC |
1657 | { |
1658 | CA_DB *retdb = NULL; | |
1659 | TXT_DB *tmpdb = NULL; | |
7e1b7485 | 1660 | BIO *in; |
0f113f3e | 1661 | CONF *dbattr_conf = NULL; |
cc01d217 | 1662 | char buf[BSIZE]; |
c7d5ea26 VD |
1663 | #ifndef OPENSSL_NO_POSIX_IO |
1664 | FILE *dbfp; | |
1665 | struct stat dbst; | |
1666 | #endif | |
0f113f3e | 1667 | |
7e1b7485 | 1668 | in = BIO_new_file(dbfile, "r"); |
01c12100 | 1669 | if (in == NULL) |
0f113f3e | 1670 | goto err; |
c7d5ea26 VD |
1671 | |
1672 | #ifndef OPENSSL_NO_POSIX_IO | |
1673 | BIO_get_fp(in, &dbfp); | |
1674 | if (fstat(fileno(dbfp), &dbst) == -1) { | |
ff988500 RS |
1675 | ERR_raise_data(ERR_LIB_SYS, errno, |
1676 | "calling fstat(%s)", dbfile); | |
c7d5ea26 VD |
1677 | goto err; |
1678 | } | |
1679 | #endif | |
1680 | ||
0f113f3e MC |
1681 | if ((tmpdb = TXT_DB_read(in, DB_NUMBER)) == NULL) |
1682 | goto err; | |
f85b68cd RL |
1683 | |
1684 | #ifndef OPENSSL_SYS_VMS | |
cbe29648 | 1685 | BIO_snprintf(buf, sizeof(buf), "%s.attr", dbfile); |
f85b68cd | 1686 | #else |
cbe29648 | 1687 | BIO_snprintf(buf, sizeof(buf), "%s-attr", dbfile); |
0f113f3e | 1688 | #endif |
ac454d8d | 1689 | dbattr_conf = app_load_config_quiet(buf); |
0f113f3e | 1690 | |
b4faea50 | 1691 | retdb = app_malloc(sizeof(*retdb), "new DB"); |
0f113f3e MC |
1692 | retdb->db = tmpdb; |
1693 | tmpdb = NULL; | |
1694 | if (db_attr) | |
1695 | retdb->attributes = *db_attr; | |
8cdb993d | 1696 | else |
0f113f3e | 1697 | retdb->attributes.unique_subject = 1; |
0f113f3e | 1698 | |
da7f81d3 DDO |
1699 | if (dbattr_conf != NULL) { |
1700 | char *p = app_conf_try_string(dbattr_conf, NULL, "unique_subject"); | |
8cdb993d | 1701 | |
da7f81d3 | 1702 | if (p != NULL) |
0f113f3e | 1703 | retdb->attributes.unique_subject = parse_yesno(p, 1); |
0f113f3e | 1704 | } |
f85b68cd | 1705 | |
c7d5ea26 VD |
1706 | retdb->dbfname = OPENSSL_strdup(dbfile); |
1707 | #ifndef OPENSSL_NO_POSIX_IO | |
1708 | retdb->dbst = dbst; | |
1709 | #endif | |
1710 | ||
f85b68cd | 1711 | err: |
01c12100 | 1712 | ERR_print_errors(bio_err); |
efa7dd64 | 1713 | NCONF_free(dbattr_conf); |
895cba19 | 1714 | TXT_DB_free(tmpdb); |
ca3a82c3 | 1715 | BIO_free_all(in); |
0f113f3e MC |
1716 | return retdb; |
1717 | } | |
f85b68cd | 1718 | |
a4107d73 VD |
1719 | /* |
1720 | * Returns > 0 on success, <= 0 on error | |
1721 | */ | |
f85b68cd | 1722 | int index_index(CA_DB *db) |
0f113f3e MC |
1723 | { |
1724 | if (!TXT_DB_create_index(db->db, DB_serial, NULL, | |
1725 | LHASH_HASH_FN(index_serial), | |
1726 | LHASH_COMP_FN(index_serial))) { | |
1727 | BIO_printf(bio_err, | |
01c12100 | 1728 | "Error creating serial number index:(%ld,%ld,%ld)\n", |
0f113f3e | 1729 | db->db->error, db->db->arg1, db->db->arg2); |
01c12100 | 1730 | goto err; |
0f113f3e MC |
1731 | } |
1732 | ||
1733 | if (db->attributes.unique_subject | |
1734 | && !TXT_DB_create_index(db->db, DB_name, index_name_qual, | |
1735 | LHASH_HASH_FN(index_name), | |
1736 | LHASH_COMP_FN(index_name))) { | |
01c12100 | 1737 | BIO_printf(bio_err, "Error creating name index:(%ld,%ld,%ld)\n", |
0f113f3e | 1738 | db->db->error, db->db->arg1, db->db->arg2); |
01c12100 | 1739 | goto err; |
0f113f3e MC |
1740 | } |
1741 | return 1; | |
01c12100 DDO |
1742 | err: |
1743 | ERR_print_errors(bio_err); | |
1744 | return 0; | |
0f113f3e | 1745 | } |
f85b68cd | 1746 | |
7d727231 | 1747 | int save_index(const char *dbfile, const char *suffix, CA_DB *db) |
0f113f3e MC |
1748 | { |
1749 | char buf[3][BSIZE]; | |
7e1b7485 | 1750 | BIO *out; |
0f113f3e MC |
1751 | int j; |
1752 | ||
0f113f3e MC |
1753 | j = strlen(dbfile) + strlen(suffix); |
1754 | if (j + 6 >= BSIZE) { | |
01c12100 | 1755 | BIO_printf(bio_err, "File name too long\n"); |
0f113f3e MC |
1756 | goto err; |
1757 | } | |
f85b68cd | 1758 | #ifndef OPENSSL_SYS_VMS |
cbe29648 RS |
1759 | j = BIO_snprintf(buf[2], sizeof(buf[2]), "%s.attr", dbfile); |
1760 | j = BIO_snprintf(buf[1], sizeof(buf[1]), "%s.attr.%s", dbfile, suffix); | |
1761 | j = BIO_snprintf(buf[0], sizeof(buf[0]), "%s.%s", dbfile, suffix); | |
f85b68cd | 1762 | #else |
cbe29648 RS |
1763 | j = BIO_snprintf(buf[2], sizeof(buf[2]), "%s-attr", dbfile); |
1764 | j = BIO_snprintf(buf[1], sizeof(buf[1]), "%s-attr-%s", dbfile, suffix); | |
1765 | j = BIO_snprintf(buf[0], sizeof(buf[0]), "%s-%s", dbfile, suffix); | |
63b6fe2b | 1766 | #endif |
7e1b7485 RS |
1767 | out = BIO_new_file(buf[0], "w"); |
1768 | if (out == NULL) { | |
0f113f3e | 1769 | perror(dbfile); |
01c12100 | 1770 | BIO_printf(bio_err, "Unable to open '%s'\n", dbfile); |
0f113f3e MC |
1771 | goto err; |
1772 | } | |
1773 | j = TXT_DB_write(out, db->db); | |
7e1b7485 | 1774 | BIO_free(out); |
0f113f3e MC |
1775 | if (j <= 0) |
1776 | goto err; | |
1777 | ||
7e1b7485 | 1778 | out = BIO_new_file(buf[1], "w"); |
7e1b7485 | 1779 | if (out == NULL) { |
0f113f3e | 1780 | perror(buf[2]); |
01c12100 | 1781 | BIO_printf(bio_err, "Unable to open '%s'\n", buf[2]); |
0f113f3e MC |
1782 | goto err; |
1783 | } | |
1784 | BIO_printf(out, "unique_subject = %s\n", | |
1785 | db->attributes.unique_subject ? "yes" : "no"); | |
1786 | BIO_free(out); | |
1787 | ||
1788 | return 1; | |
f85b68cd | 1789 | err: |
01c12100 | 1790 | ERR_print_errors(bio_err); |
0f113f3e MC |
1791 | return 0; |
1792 | } | |
f85b68cd | 1793 | |
0f113f3e MC |
1794 | int rotate_index(const char *dbfile, const char *new_suffix, |
1795 | const char *old_suffix) | |
1796 | { | |
1797 | char buf[5][BSIZE]; | |
1798 | int i, j; | |
1799 | ||
1800 | i = strlen(dbfile) + strlen(old_suffix); | |
1801 | j = strlen(dbfile) + strlen(new_suffix); | |
1802 | if (i > j) | |
1803 | j = i; | |
1804 | if (j + 6 >= BSIZE) { | |
01c12100 | 1805 | BIO_printf(bio_err, "File name too long\n"); |
0f113f3e MC |
1806 | goto err; |
1807 | } | |
f85b68cd | 1808 | #ifndef OPENSSL_SYS_VMS |
cbe29648 RS |
1809 | j = BIO_snprintf(buf[4], sizeof(buf[4]), "%s.attr", dbfile); |
1810 | j = BIO_snprintf(buf[3], sizeof(buf[3]), "%s.attr.%s", dbfile, old_suffix); | |
1811 | j = BIO_snprintf(buf[2], sizeof(buf[2]), "%s.attr.%s", dbfile, new_suffix); | |
1812 | j = BIO_snprintf(buf[1], sizeof(buf[1]), "%s.%s", dbfile, old_suffix); | |
1813 | j = BIO_snprintf(buf[0], sizeof(buf[0]), "%s.%s", dbfile, new_suffix); | |
f85b68cd | 1814 | #else |
cbe29648 RS |
1815 | j = BIO_snprintf(buf[4], sizeof(buf[4]), "%s-attr", dbfile); |
1816 | j = BIO_snprintf(buf[3], sizeof(buf[3]), "%s-attr-%s", dbfile, old_suffix); | |
1817 | j = BIO_snprintf(buf[2], sizeof(buf[2]), "%s-attr-%s", dbfile, new_suffix); | |
1818 | j = BIO_snprintf(buf[1], sizeof(buf[1]), "%s-%s", dbfile, old_suffix); | |
1819 | j = BIO_snprintf(buf[0], sizeof(buf[0]), "%s-%s", dbfile, new_suffix); | |
63b6fe2b | 1820 | #endif |
0f113f3e | 1821 | if (rename(dbfile, buf[1]) < 0 && errno != ENOENT |
a1ad253f | 1822 | #ifdef ENOTDIR |
0f113f3e | 1823 | && errno != ENOTDIR |
a1ad253f | 1824 | #endif |
0f113f3e | 1825 | ) { |
01c12100 | 1826 | BIO_printf(bio_err, "Unable to rename %s to %s\n", dbfile, buf[1]); |
0f113f3e MC |
1827 | perror("reason"); |
1828 | goto err; | |
1829 | } | |
0f113f3e | 1830 | if (rename(buf[0], dbfile) < 0) { |
01c12100 | 1831 | BIO_printf(bio_err, "Unable to rename %s to %s\n", buf[0], dbfile); |
0f113f3e MC |
1832 | perror("reason"); |
1833 | rename(buf[1], dbfile); | |
1834 | goto err; | |
1835 | } | |
0f113f3e | 1836 | if (rename(buf[4], buf[3]) < 0 && errno != ENOENT |
a1ad253f | 1837 | #ifdef ENOTDIR |
0f113f3e MC |
1838 | && errno != ENOTDIR |
1839 | #endif | |
1840 | ) { | |
01c12100 | 1841 | BIO_printf(bio_err, "Unable to rename %s to %s\n", buf[4], buf[3]); |
0f113f3e MC |
1842 | perror("reason"); |
1843 | rename(dbfile, buf[0]); | |
1844 | rename(buf[1], dbfile); | |
1845 | goto err; | |
1846 | } | |
0f113f3e | 1847 | if (rename(buf[2], buf[4]) < 0) { |
01c12100 | 1848 | BIO_printf(bio_err, "Unable to rename %s to %s\n", buf[2], buf[4]); |
0f113f3e MC |
1849 | perror("reason"); |
1850 | rename(buf[3], buf[4]); | |
1851 | rename(dbfile, buf[0]); | |
1852 | rename(buf[1], dbfile); | |
1853 | goto err; | |
1854 | } | |
1855 | return 1; | |
f85b68cd | 1856 | err: |
01c12100 | 1857 | ERR_print_errors(bio_err); |
0f113f3e MC |
1858 | return 0; |
1859 | } | |
f85b68cd RL |
1860 | |
1861 | void free_index(CA_DB *db) | |
0f113f3e MC |
1862 | { |
1863 | if (db) { | |
895cba19 | 1864 | TXT_DB_free(db->db); |
c7d5ea26 | 1865 | OPENSSL_free(db->dbfname); |
0f113f3e MC |
1866 | OPENSSL_free(db); |
1867 | } | |
1868 | } | |
6d5ffb59 | 1869 | |
ff990440 | 1870 | int parse_yesno(const char *str, int def) |
0f113f3e | 1871 | { |
0f113f3e MC |
1872 | if (str) { |
1873 | switch (*str) { | |
1874 | case 'f': /* false */ | |
1875 | case 'F': /* FALSE */ | |
1876 | case 'n': /* no */ | |
1877 | case 'N': /* NO */ | |
1878 | case '0': /* 0 */ | |
1bb2daea | 1879 | return 0; |
0f113f3e MC |
1880 | case 't': /* true */ |
1881 | case 'T': /* TRUE */ | |
1882 | case 'y': /* yes */ | |
1883 | case 'Y': /* YES */ | |
1884 | case '1': /* 1 */ | |
1bb2daea | 1885 | return 1; |
0f113f3e MC |
1886 | } |
1887 | } | |
1bb2daea | 1888 | return def; |
0f113f3e | 1889 | } |
03ddbdd9 | 1890 | |
6d5ffb59 | 1891 | /* |
db4c08f0 | 1892 | * name is expected to be in the format /type0=value0/type1=value1/type2=... |
5a0991d0 DDO |
1893 | * where + can be used instead of / to form multi-valued RDNs if canmulti |
1894 | * and characters may be escaped by \ | |
6d5ffb59 | 1895 | */ |
57c05c57 DDO |
1896 | X509_NAME *parse_name(const char *cp, int chtype, int canmulti, |
1897 | const char *desc) | |
0f113f3e | 1898 | { |
db4c08f0 RS |
1899 | int nextismulti = 0; |
1900 | char *work; | |
1901 | X509_NAME *n; | |
0f113f3e | 1902 | |
2167640b EC |
1903 | if (*cp++ != '/') { |
1904 | BIO_printf(bio_err, | |
57c05c57 | 1905 | "%s: %s name is expected to be in the format " |
2167640b EC |
1906 | "/type0=value0/type1=value1/type2=... where characters may " |
1907 | "be escaped by \\. This name is not in that format: '%s'\n", | |
57c05c57 | 1908 | opt_getprog(), desc, --cp); |
db4c08f0 | 1909 | return NULL; |
2167640b | 1910 | } |
db4c08f0 RS |
1911 | |
1912 | n = X509_NAME_new(); | |
57c05c57 DDO |
1913 | if (n == NULL) { |
1914 | BIO_printf(bio_err, "%s: Out of memory\n", opt_getprog()); | |
db4c08f0 | 1915 | return NULL; |
57c05c57 | 1916 | } |
a3ed492f | 1917 | work = OPENSSL_strdup(cp); |
52958608 | 1918 | if (work == NULL) { |
57c05c57 DDO |
1919 | BIO_printf(bio_err, "%s: Error copying %s name input\n", |
1920 | opt_getprog(), desc); | |
db4c08f0 | 1921 | goto err; |
52958608 | 1922 | } |
db4c08f0 | 1923 | |
7e998a0f | 1924 | while (*cp != '\0') { |
db4c08f0 RS |
1925 | char *bp = work; |
1926 | char *typestr = bp; | |
1927 | unsigned char *valstr; | |
1928 | int nid; | |
1929 | int ismulti = nextismulti; | |
8cdb993d | 1930 | |
db4c08f0 RS |
1931 | nextismulti = 0; |
1932 | ||
1933 | /* Collect the type */ | |
7e998a0f | 1934 | while (*cp != '\0' && *cp != '=') |
db4c08f0 | 1935 | *bp++ = *cp++; |
57c05c57 | 1936 | *bp++ = '\0'; |
db4c08f0 | 1937 | if (*cp == '\0') { |
0f113f3e | 1938 | BIO_printf(bio_err, |
57c05c57 DDO |
1939 | "%s: Missing '=' after RDN type string '%s' in %s name string\n", |
1940 | opt_getprog(), typestr, desc); | |
db4c08f0 | 1941 | goto err; |
0f113f3e | 1942 | } |
db4c08f0 RS |
1943 | ++cp; |
1944 | ||
1945 | /* Collect the value. */ | |
1946 | valstr = (unsigned char *)bp; | |
7e998a0f | 1947 | for (; *cp != '\0' && *cp != '/'; *bp++ = *cp++) { |
5a0991d0 | 1948 | /* unescaped '+' symbol string signals further member of multiRDN */ |
db4c08f0 RS |
1949 | if (canmulti && *cp == '+') { |
1950 | nextismulti = 1; | |
0f113f3e | 1951 | break; |
db4c08f0 RS |
1952 | } |
1953 | if (*cp == '\\' && *++cp == '\0') { | |
1954 | BIO_printf(bio_err, | |
57c05c57 DDO |
1955 | "%s: Escape character at end of %s name string\n", |
1956 | opt_getprog(), desc); | |
db4c08f0 RS |
1957 | goto err; |
1958 | } | |
0f113f3e MC |
1959 | } |
1960 | *bp++ = '\0'; | |
0f113f3e | 1961 | |
db4c08f0 | 1962 | /* If not at EOS (must be + or /), move forward. */ |
7e998a0f | 1963 | if (*cp != '\0') |
db4c08f0 | 1964 | ++cp; |
0f113f3e | 1965 | |
db4c08f0 RS |
1966 | /* Parse */ |
1967 | nid = OBJ_txt2nid(typestr); | |
1968 | if (nid == NID_undef) { | |
57c05c57 DDO |
1969 | BIO_printf(bio_err, |
1970 | "%s: Skipping unknown %s name attribute \"%s\"\n", | |
1971 | opt_getprog(), desc, typestr); | |
5a0991d0 DDO |
1972 | if (ismulti) |
1973 | BIO_printf(bio_err, | |
1974 | "Hint: a '+' in a value string needs be escaped using '\\' else a new member of a multi-valued RDN is expected\n"); | |
0f113f3e MC |
1975 | continue; |
1976 | } | |
3d362f19 BK |
1977 | if (*valstr == '\0') { |
1978 | BIO_printf(bio_err, | |
57c05c57 DDO |
1979 | "%s: No value provided for %s name attribute \"%s\", skipped\n", |
1980 | opt_getprog(), desc, typestr); | |
3d362f19 BK |
1981 | continue; |
1982 | } | |
db4c08f0 RS |
1983 | if (!X509_NAME_add_entry_by_NID(n, nid, chtype, |
1984 | valstr, strlen((char *)valstr), | |
52958608 | 1985 | -1, ismulti ? -1 : 0)) { |
7e998a0f | 1986 | ERR_print_errors(bio_err); |
57c05c57 DDO |
1987 | BIO_printf(bio_err, |
1988 | "%s: Error adding %s name attribute \"/%s=%s\"\n", | |
1287dabd | 1989 | opt_getprog(), desc, typestr, valstr); |
db4c08f0 | 1990 | goto err; |
52958608 | 1991 | } |
0f113f3e MC |
1992 | } |
1993 | ||
a3ed492f | 1994 | OPENSSL_free(work); |
0f113f3e MC |
1995 | return n; |
1996 | ||
db4c08f0 | 1997 | err: |
0f113f3e | 1998 | X509_NAME_free(n); |
a3ed492f | 1999 | OPENSSL_free(work); |
0f113f3e | 2000 | return NULL; |
6d5ffb59 RL |
2001 | } |
2002 | ||
0f113f3e MC |
2003 | /* |
2004 | * Read whole contents of a BIO into an allocated memory buffer and return | |
2005 | * it. | |
a9164153 DSH |
2006 | */ |
2007 | ||
2008 | int bio_to_mem(unsigned char **out, int maxlen, BIO *in) | |
0f113f3e MC |
2009 | { |
2010 | BIO *mem; | |
2011 | int len, ret; | |
2012 | unsigned char tbuf[1024]; | |
bde136c8 | 2013 | |
0f113f3e | 2014 | mem = BIO_new(BIO_s_mem()); |
96487cdd | 2015 | if (mem == NULL) |
0f113f3e MC |
2016 | return -1; |
2017 | for (;;) { | |
2018 | if ((maxlen != -1) && maxlen < 1024) | |
2019 | len = maxlen; | |
2020 | else | |
2021 | len = 1024; | |
2022 | len = BIO_read(in, tbuf, len); | |
0c20802c VD |
2023 | if (len < 0) { |
2024 | BIO_free(mem); | |
2025 | return -1; | |
2026 | } | |
2027 | if (len == 0) | |
0f113f3e MC |
2028 | break; |
2029 | if (BIO_write(mem, tbuf, len) != len) { | |
2030 | BIO_free(mem); | |
2031 | return -1; | |
2032 | } | |
2033 | maxlen -= len; | |
2034 | ||
2035 | if (maxlen == 0) | |
2036 | break; | |
2037 | } | |
2038 | ret = BIO_get_mem_data(mem, (char **)out); | |
2039 | BIO_set_flags(mem, BIO_FLAGS_MEM_RDONLY); | |
2040 | BIO_free(mem); | |
2041 | return ret; | |
2042 | } | |
a9164153 | 2043 | |
0c20802c | 2044 | int pkey_ctrl_string(EVP_PKEY_CTX *ctx, const char *value) |
0f113f3e | 2045 | { |
3d0b5678 | 2046 | int rv = 0; |
0f113f3e | 2047 | char *stmp, *vtmp = NULL; |
3d0b5678 | 2048 | |
7644a9ae | 2049 | stmp = OPENSSL_strdup(value); |
3d0b5678 | 2050 | if (stmp == NULL) |
0f113f3e MC |
2051 | return -1; |
2052 | vtmp = strchr(stmp, ':'); | |
3d0b5678 MC |
2053 | if (vtmp == NULL) |
2054 | goto err; | |
2055 | ||
2056 | *vtmp = 0; | |
2057 | vtmp++; | |
0f113f3e | 2058 | rv = EVP_PKEY_CTX_ctrl_str(ctx, stmp, vtmp); |
3d0b5678 MC |
2059 | |
2060 | err: | |
0f113f3e MC |
2061 | OPENSSL_free(stmp); |
2062 | return rv; | |
2063 | } | |
a2318e86 | 2064 | |
ecf3a1fb | 2065 | static void nodes_print(const char *name, STACK_OF(X509_POLICY_NODE) *nodes) |
0f113f3e MC |
2066 | { |
2067 | X509_POLICY_NODE *node; | |
2068 | int i; | |
ecf3a1fb RS |
2069 | |
2070 | BIO_printf(bio_err, "%s Policies:", name); | |
0f113f3e | 2071 | if (nodes) { |
ecf3a1fb | 2072 | BIO_puts(bio_err, "\n"); |
0f113f3e MC |
2073 | for (i = 0; i < sk_X509_POLICY_NODE_num(nodes); i++) { |
2074 | node = sk_X509_POLICY_NODE_value(nodes, i); | |
ecf3a1fb | 2075 | X509_POLICY_NODE_print(bio_err, node, 2); |
0f113f3e | 2076 | } |
2234212c | 2077 | } else { |
ecf3a1fb | 2078 | BIO_puts(bio_err, " <empty>\n"); |
2234212c | 2079 | } |
0f113f3e | 2080 | } |
c431798e | 2081 | |
ecf3a1fb | 2082 | void policies_print(X509_STORE_CTX *ctx) |
0f113f3e MC |
2083 | { |
2084 | X509_POLICY_TREE *tree; | |
2085 | int explicit_policy; | |
8cdb993d | 2086 | |
0f113f3e MC |
2087 | tree = X509_STORE_CTX_get0_policy_tree(ctx); |
2088 | explicit_policy = X509_STORE_CTX_get_explicit_policy(ctx); | |
2089 | ||
ecf3a1fb | 2090 | BIO_printf(bio_err, "Require explicit Policy: %s\n", |
0f113f3e MC |
2091 | explicit_policy ? "True" : "False"); |
2092 | ||
ecf3a1fb RS |
2093 | nodes_print("Authority", X509_policy_tree_get0_policies(tree)); |
2094 | nodes_print("User", X509_policy_tree_get0_user_policies(tree)); | |
0f113f3e | 2095 | } |
ffa10187 | 2096 | |
3a83462d MC |
2097 | /*- |
2098 | * next_protos_parse parses a comma separated list of strings into a string | |
71fa4513 BL |
2099 | * in a format suitable for passing to SSL_CTX_set_next_protos_advertised. |
2100 | * outlen: (output) set to the length of the resulting buffer on success. | |
2101 | * err: (maybe NULL) on failure, an error message line is written to this BIO. | |
8483a003 | 2102 | * in: a NUL terminated string like "abc,def,ghi" |
71fa4513 | 2103 | * |
8483a003 | 2104 | * returns: a malloc'd buffer or NULL on failure. |
71fa4513 | 2105 | */ |
817cd0d5 | 2106 | unsigned char *next_protos_parse(size_t *outlen, const char *in) |
0f113f3e MC |
2107 | { |
2108 | size_t len; | |
2109 | unsigned char *out; | |
2110 | size_t i, start = 0; | |
e78253f2 | 2111 | size_t skipped = 0; |
0f113f3e MC |
2112 | |
2113 | len = strlen(in); | |
e78253f2 | 2114 | if (len == 0 || len >= 65535) |
0f113f3e MC |
2115 | return NULL; |
2116 | ||
e78253f2 | 2117 | out = app_malloc(len + 1, "NPN buffer"); |
0f113f3e MC |
2118 | for (i = 0; i <= len; ++i) { |
2119 | if (i == len || in[i] == ',') { | |
e78253f2 VD |
2120 | /* |
2121 | * Zero-length ALPN elements are invalid on the wire, we could be | |
2122 | * strict and reject the entire string, but just ignoring extra | |
2123 | * commas seems harmless and more friendly. | |
2124 | * | |
2125 | * Every comma we skip in this way puts the input buffer another | |
2126 | * byte ahead of the output buffer, so all stores into the output | |
2127 | * buffer need to be decremented by the number commas skipped. | |
2128 | */ | |
2129 | if (i == start) { | |
2130 | ++start; | |
2131 | ++skipped; | |
2132 | continue; | |
2133 | } | |
0f113f3e MC |
2134 | if (i - start > 255) { |
2135 | OPENSSL_free(out); | |
2136 | return NULL; | |
2137 | } | |
8cdb993d | 2138 | out[start - skipped] = (unsigned char)(i - start); |
0f113f3e | 2139 | start = i + 1; |
2234212c | 2140 | } else { |
e78253f2 | 2141 | out[i + 1 - skipped] = in[i]; |
2234212c | 2142 | } |
0f113f3e MC |
2143 | } |
2144 | ||
e78253f2 VD |
2145 | if (len <= skipped) { |
2146 | OPENSSL_free(out); | |
2147 | return NULL; | |
2148 | } | |
2149 | ||
2150 | *outlen = len + 1 - skipped; | |
0f113f3e MC |
2151 | return out; |
2152 | } | |
71fa4513 | 2153 | |
8cdb993d DDO |
2154 | int check_cert_attributes(BIO *bio, X509 *x, const char *checkhost, |
2155 | const char *checkemail, const char *checkip, | |
2156 | int print) | |
0f113f3e | 2157 | { |
9567fd38 RK |
2158 | int valid_host = 0; |
2159 | int valid_mail = 0; | |
2160 | int valid_ip = 0; | |
2161 | int ret = 1; | |
2162 | ||
0f113f3e | 2163 | if (x == NULL) |
9567fd38 RK |
2164 | return 0; |
2165 | ||
2166 | if (checkhost != NULL) { | |
2167 | valid_host = X509_check_host(x, checkhost, 0, 0, NULL); | |
2168 | if (print) | |
2169 | BIO_printf(bio, "Hostname %s does%s match certificate\n", | |
2170 | checkhost, valid_host == 1 ? "" : " NOT"); | |
2171 | ret = ret && valid_host; | |
0f113f3e MC |
2172 | } |
2173 | ||
9567fd38 RK |
2174 | if (checkemail != NULL) { |
2175 | valid_mail = X509_check_email(x, checkemail, 0, 0); | |
2176 | if (print) | |
2177 | BIO_printf(bio, "Email %s does%s match certificate\n", | |
2178 | checkemail, valid_mail ? "" : " NOT"); | |
2179 | ret = ret && valid_mail; | |
0f113f3e MC |
2180 | } |
2181 | ||
9567fd38 | 2182 | if (checkip != NULL) { |
8cdb993d | 2183 | valid_ip = X509_check_ip_asc(x, checkip, 0); |
9567fd38 RK |
2184 | if (print) |
2185 | BIO_printf(bio, "IP %s does%s match certificate\n", | |
8cdb993d | 2186 | checkip, valid_ip ? "" : " NOT"); |
9567fd38 | 2187 | ret = ret && valid_ip; |
0f113f3e | 2188 | } |
9567fd38 RK |
2189 | |
2190 | return ret; | |
0f113f3e | 2191 | } |
a70da5b3 | 2192 | |
6c9515b7 DDO |
2193 | static int do_pkey_ctx_init(EVP_PKEY_CTX *pkctx, STACK_OF(OPENSSL_STRING) *opts) |
2194 | { | |
2195 | int i; | |
2196 | ||
2197 | if (opts == NULL) | |
2198 | return 1; | |
2199 | ||
2200 | for (i = 0; i < sk_OPENSSL_STRING_num(opts); i++) { | |
2201 | char *opt = sk_OPENSSL_STRING_value(opts, i); | |
8cdb993d | 2202 | |
6c9515b7 DDO |
2203 | if (pkey_ctrl_string(pkctx, opt) <= 0) { |
2204 | BIO_printf(bio_err, "parameter error \"%s\"\n", opt); | |
2205 | ERR_print_errors(bio_err); | |
2206 | return 0; | |
2207 | } | |
2208 | } | |
2209 | ||
2210 | return 1; | |
2211 | } | |
2212 | ||
2213 | static int do_x509_init(X509 *x, STACK_OF(OPENSSL_STRING) *opts) | |
2214 | { | |
2215 | int i; | |
2216 | ||
2217 | if (opts == NULL) | |
2218 | return 1; | |
2219 | ||
2220 | for (i = 0; i < sk_OPENSSL_STRING_num(opts); i++) { | |
2221 | char *opt = sk_OPENSSL_STRING_value(opts, i); | |
8cdb993d | 2222 | |
6c9515b7 DDO |
2223 | if (x509_ctrl_string(x, opt) <= 0) { |
2224 | BIO_printf(bio_err, "parameter error \"%s\"\n", opt); | |
2225 | ERR_print_errors(bio_err); | |
2226 | return 0; | |
2227 | } | |
2228 | } | |
2229 | ||
2230 | return 1; | |
2231 | } | |
2232 | ||
2233 | static int do_x509_req_init(X509_REQ *x, STACK_OF(OPENSSL_STRING) *opts) | |
2234 | { | |
2235 | int i; | |
2236 | ||
2237 | if (opts == NULL) | |
2238 | return 1; | |
2239 | ||
2240 | for (i = 0; i < sk_OPENSSL_STRING_num(opts); i++) { | |
2241 | char *opt = sk_OPENSSL_STRING_value(opts, i); | |
8cdb993d | 2242 | |
6c9515b7 DDO |
2243 | if (x509_req_ctrl_string(x, opt) <= 0) { |
2244 | BIO_printf(bio_err, "parameter error \"%s\"\n", opt); | |
2245 | ERR_print_errors(bio_err); | |
2246 | return 0; | |
2247 | } | |
2248 | } | |
2249 | ||
2250 | return 1; | |
2251 | } | |
2252 | ||
2253 | static int do_sign_init(EVP_MD_CTX *ctx, EVP_PKEY *pkey, | |
91034b68 | 2254 | const char *md, STACK_OF(OPENSSL_STRING) *sigopts) |
6c9515b7 DDO |
2255 | { |
2256 | EVP_PKEY_CTX *pkctx = NULL; | |
91034b68 | 2257 | char def_md[80]; |
6c9515b7 DDO |
2258 | |
2259 | if (ctx == NULL) | |
2260 | return 0; | |
2261 | /* | |
91034b68 | 2262 | * EVP_PKEY_get_default_digest_name() returns 2 if the digest is mandatory |
6c9515b7 DDO |
2263 | * for this algorithm. |
2264 | */ | |
91034b68 PG |
2265 | if (EVP_PKEY_get_default_digest_name(pkey, def_md, sizeof(def_md)) == 2 |
2266 | && strcmp(def_md, "UNDEF") == 0) { | |
6c9515b7 DDO |
2267 | /* The signing algorithm requires there to be no digest */ |
2268 | md = NULL; | |
2269 | } | |
91034b68 PG |
2270 | |
2271 | return EVP_DigestSignInit_ex(ctx, &pkctx, md, app_get0_libctx(), | |
2272 | app_get0_propq(), pkey, NULL) | |
6c9515b7 DDO |
2273 | && do_pkey_ctx_init(pkctx, sigopts); |
2274 | } | |
2275 | ||
ec2bfb7d DDO |
2276 | static int adapt_keyid_ext(X509 *cert, X509V3_CTX *ext_ctx, |
2277 | const char *name, const char *value, int add_default) | |
2278 | { | |
2279 | const STACK_OF(X509_EXTENSION) *exts = X509_get0_extensions(cert); | |
2280 | X509_EXTENSION *new_ext = X509V3_EXT_nconf(NULL, ext_ctx, name, value); | |
2281 | int idx, rv = 0; | |
2282 | ||
2283 | if (new_ext == NULL) | |
2284 | return rv; | |
2285 | ||
2286 | idx = X509v3_get_ext_by_OBJ(exts, X509_EXTENSION_get_object(new_ext), -1); | |
2287 | if (idx >= 0) { | |
2288 | X509_EXTENSION *found_ext = X509v3_get_ext(exts, idx); | |
adbd77f6 DDO |
2289 | ASN1_OCTET_STRING *encoded = X509_EXTENSION_get_data(found_ext); |
2290 | int disabled = ASN1_STRING_length(encoded) <= 2; /* indicating "none" */ | |
ec2bfb7d DDO |
2291 | |
2292 | if (disabled) { | |
2293 | X509_delete_ext(cert, idx); | |
2294 | X509_EXTENSION_free(found_ext); | |
2295 | } /* else keep existing key identifier, which might be outdated */ | |
2296 | rv = 1; | |
8cdb993d | 2297 | } else { |
ec2bfb7d DDO |
2298 | rv = !add_default || X509_add_ext(cert, new_ext, -1); |
2299 | } | |
2300 | X509_EXTENSION_free(new_ext); | |
2301 | return rv; | |
2302 | } | |
2303 | ||
adbd77f6 DDO |
2304 | int cert_matches_key(const X509 *cert, const EVP_PKEY *pkey) |
2305 | { | |
2306 | int match; | |
2307 | ||
2308 | ERR_set_mark(); | |
2309 | match = X509_check_private_key(cert, pkey); | |
2310 | ERR_pop_to_mark(); | |
2311 | return match; | |
2312 | } | |
2313 | ||
ec2bfb7d | 2314 | /* Ensure RFC 5280 compliance, adapt keyIDs as needed, and sign the cert info */ |
342e3652 | 2315 | int do_X509_sign(X509 *cert, int force_v1, EVP_PKEY *pkey, const char *md, |
ec2bfb7d | 2316 | STACK_OF(OPENSSL_STRING) *sigopts, X509V3_CTX *ext_ctx) |
6c9515b7 | 2317 | { |
6c9515b7 | 2318 | EVP_MD_CTX *mctx = EVP_MD_CTX_new(); |
ec2bfb7d | 2319 | int self_sign; |
6c9515b7 DDO |
2320 | int rv = 0; |
2321 | ||
342e3652 | 2322 | if (!force_v1) { |
cdf63a37 | 2323 | if (!X509_set_version(cert, X509_VERSION_3)) |
6c9515b7 DDO |
2324 | goto end; |
2325 | ||
ec2bfb7d | 2326 | /* |
adbd77f6 | 2327 | * Add default SKID before AKID such that AKID can make use of it |
ec2bfb7d DDO |
2328 | * in case the certificate is self-signed |
2329 | */ | |
2330 | /* Prevent X509_V_ERR_MISSING_SUBJECT_KEY_IDENTIFIER */ | |
2331 | if (!adapt_keyid_ext(cert, ext_ctx, "subjectKeyIdentifier", "hash", 1)) | |
2332 | goto end; | |
2333 | /* Prevent X509_V_ERR_MISSING_AUTHORITY_KEY_IDENTIFIER */ | |
adbd77f6 | 2334 | self_sign = cert_matches_key(cert, pkey); |
ec2bfb7d DDO |
2335 | if (!adapt_keyid_ext(cert, ext_ctx, "authorityKeyIdentifier", |
2336 | "keyid, issuer", !self_sign)) | |
2337 | goto end; | |
6c9515b7 DDO |
2338 | } |
2339 | ||
2340 | if (mctx != NULL && do_sign_init(mctx, pkey, md, sigopts) > 0) | |
2341 | rv = (X509_sign_ctx(cert, mctx) > 0); | |
2342 | end: | |
2343 | EVP_MD_CTX_free(mctx); | |
2344 | return rv; | |
2345 | } | |
2346 | ||
2347 | /* Sign the certificate request info */ | |
91034b68 | 2348 | int do_X509_REQ_sign(X509_REQ *x, EVP_PKEY *pkey, const char *md, |
6c9515b7 DDO |
2349 | STACK_OF(OPENSSL_STRING) *sigopts) |
2350 | { | |
2351 | int rv = 0; | |
2352 | EVP_MD_CTX *mctx = EVP_MD_CTX_new(); | |
2353 | ||
2354 | if (do_sign_init(mctx, pkey, md, sigopts) > 0) | |
2355 | rv = (X509_REQ_sign_ctx(x, mctx) > 0); | |
2356 | EVP_MD_CTX_free(mctx); | |
2357 | return rv; | |
2358 | } | |
2359 | ||
2360 | /* Sign the CRL info */ | |
91034b68 | 2361 | int do_X509_CRL_sign(X509_CRL *x, EVP_PKEY *pkey, const char *md, |
6c9515b7 DDO |
2362 | STACK_OF(OPENSSL_STRING) *sigopts) |
2363 | { | |
2364 | int rv = 0; | |
2365 | EVP_MD_CTX *mctx = EVP_MD_CTX_new(); | |
2366 | ||
2367 | if (do_sign_init(mctx, pkey, md, sigopts) > 0) | |
2368 | rv = (X509_CRL_sign_ctx(x, mctx) > 0); | |
2369 | EVP_MD_CTX_free(mctx); | |
2370 | return rv; | |
2371 | } | |
2372 | ||
bc42cf51 PH |
2373 | /* |
2374 | * do_X509_verify returns 1 if the signature is valid, | |
2375 | * 0 if the signature check fails, or -1 if error occurs. | |
2376 | */ | |
6c9515b7 DDO |
2377 | int do_X509_verify(X509 *x, EVP_PKEY *pkey, STACK_OF(OPENSSL_STRING) *vfyopts) |
2378 | { | |
2379 | int rv = 0; | |
2380 | ||
2381 | if (do_x509_init(x, vfyopts) > 0) | |
bc42cf51 PH |
2382 | rv = X509_verify(x, pkey); |
2383 | else | |
2384 | rv = -1; | |
6c9515b7 DDO |
2385 | return rv; |
2386 | } | |
2387 | ||
bc42cf51 PH |
2388 | /* |
2389 | * do_X509_REQ_verify returns 1 if the signature is valid, | |
2390 | * 0 if the signature check fails, or -1 if error occurs. | |
2391 | */ | |
6c9515b7 DDO |
2392 | int do_X509_REQ_verify(X509_REQ *x, EVP_PKEY *pkey, |
2393 | STACK_OF(OPENSSL_STRING) *vfyopts) | |
2394 | { | |
2395 | int rv = 0; | |
2396 | ||
2397 | if (do_x509_req_init(x, vfyopts) > 0) | |
8cdb993d | 2398 | rv = X509_REQ_verify_ex(x, pkey, app_get0_libctx(), app_get0_propq()); |
bc42cf51 PH |
2399 | else |
2400 | rv = -1; | |
6c9515b7 DDO |
2401 | return rv; |
2402 | } | |
2403 | ||
0090a686 DSH |
2404 | /* Get first http URL from a DIST_POINT structure */ |
2405 | ||
2406 | static const char *get_dp_url(DIST_POINT *dp) | |
0f113f3e MC |
2407 | { |
2408 | GENERAL_NAMES *gens; | |
2409 | GENERAL_NAME *gen; | |
2410 | int i, gtype; | |
2411 | ASN1_STRING *uri; | |
8cdb993d | 2412 | |
0f113f3e MC |
2413 | if (!dp->distpoint || dp->distpoint->type != 0) |
2414 | return NULL; | |
2415 | gens = dp->distpoint->name.fullname; | |
2416 | for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) { | |
2417 | gen = sk_GENERAL_NAME_value(gens, i); | |
2418 | uri = GENERAL_NAME_get0_value(gen, >ype); | |
2419 | if (gtype == GEN_URI && ASN1_STRING_length(uri) > 6) { | |
17ebf85a | 2420 | const char *uptr = (const char *)ASN1_STRING_get0_data(uri); |
9498dac4 DDO |
2421 | |
2422 | if (IS_HTTP(uptr)) /* can/should not use HTTPS here */ | |
0f113f3e MC |
2423 | return uptr; |
2424 | } | |
2425 | } | |
2426 | return NULL; | |
2427 | } | |
2428 | ||
2429 | /* | |
2430 | * Look through a CRLDP structure and attempt to find an http URL to | |
2431 | * downloads a CRL from. | |
0090a686 DSH |
2432 | */ |
2433 | ||
2434 | static X509_CRL *load_crl_crldp(STACK_OF(DIST_POINT) *crldp) | |
0f113f3e MC |
2435 | { |
2436 | int i; | |
2437 | const char *urlptr = NULL; | |
8cdb993d | 2438 | |
0f113f3e MC |
2439 | for (i = 0; i < sk_DIST_POINT_num(crldp); i++) { |
2440 | DIST_POINT *dp = sk_DIST_POINT_value(crldp, i); | |
8cdb993d | 2441 | |
0f113f3e | 2442 | urlptr = get_dp_url(dp); |
e9d62da6 | 2443 | if (urlptr != NULL) |
d382e796 | 2444 | return load_crl(urlptr, FORMAT_UNDEF, 0, "CRL via CDP"); |
0f113f3e MC |
2445 | } |
2446 | return NULL; | |
2447 | } | |
2448 | ||
2449 | /* | |
0aa87e86 DDO |
2450 | * Example of downloading CRLs from CRLDP: |
2451 | * not usable for real world as it always downloads and doesn't cache anything. | |
0090a686 DSH |
2452 | */ |
2453 | ||
8cc86b81 DDO |
2454 | static STACK_OF(X509_CRL) *crls_http_cb(const X509_STORE_CTX *ctx, |
2455 | const X509_NAME *nm) | |
0f113f3e MC |
2456 | { |
2457 | X509 *x; | |
2458 | STACK_OF(X509_CRL) *crls = NULL; | |
2459 | X509_CRL *crl; | |
2460 | STACK_OF(DIST_POINT) *crldp; | |
7e1b7485 RS |
2461 | |
2462 | crls = sk_X509_CRL_new_null(); | |
2463 | if (!crls) | |
2464 | return NULL; | |
0f113f3e MC |
2465 | x = X509_STORE_CTX_get_current_cert(ctx); |
2466 | crldp = X509_get_ext_d2i(x, NID_crl_distribution_points, NULL, NULL); | |
2467 | crl = load_crl_crldp(crldp); | |
2468 | sk_DIST_POINT_pop_free(crldp, DIST_POINT_free); | |
fe2b7dfd MC |
2469 | if (!crl) { |
2470 | sk_X509_CRL_free(crls); | |
0f113f3e | 2471 | return NULL; |
fe2b7dfd | 2472 | } |
0f113f3e MC |
2473 | sk_X509_CRL_push(crls, crl); |
2474 | /* Try to download delta CRL */ | |
2475 | crldp = X509_get_ext_d2i(x, NID_freshest_crl, NULL, NULL); | |
2476 | crl = load_crl_crldp(crldp); | |
2477 | sk_DIST_POINT_pop_free(crldp, DIST_POINT_free); | |
2478 | if (crl) | |
2479 | sk_X509_CRL_push(crls, crl); | |
2480 | return crls; | |
2481 | } | |
0090a686 DSH |
2482 | |
2483 | void store_setup_crl_download(X509_STORE *st) | |
0f113f3e MC |
2484 | { |
2485 | X509_STORE_set_lookup_crls_cb(st, crls_http_cb); | |
2486 | } | |
0090a686 | 2487 | |
29f178bd DDO |
2488 | #ifndef OPENSSL_NO_SOCK |
2489 | static const char *tls_error_hint(void) | |
2490 | { | |
2491 | unsigned long err = ERR_peek_error(); | |
2492 | ||
2493 | if (ERR_GET_LIB(err) != ERR_LIB_SSL) | |
2494 | err = ERR_peek_last_error(); | |
2495 | if (ERR_GET_LIB(err) != ERR_LIB_SSL) | |
db302550 | 2496 | return NULL; /* likely no TLS error */ |
29f178bd DDO |
2497 | |
2498 | switch (ERR_GET_REASON(err)) { | |
2499 | case SSL_R_WRONG_VERSION_NUMBER: | |
2500 | return "The server does not support (a suitable version of) TLS"; | |
2501 | case SSL_R_UNKNOWN_PROTOCOL: | |
2502 | return "The server does not support HTTPS"; | |
2503 | case SSL_R_CERTIFICATE_VERIFY_FAILED: | |
2504 | return "Cannot authenticate server via its TLS certificate, likely due to mismatch with our trusted TLS certs or missing revocation status"; | |
2505 | case SSL_AD_REASON_OFFSET + TLS1_AD_UNKNOWN_CA: | |
2506 | return "Server did not accept our TLS certificate, likely due to mismatch with server's trust anchor or missing revocation status"; | |
2507 | case SSL_AD_REASON_OFFSET + SSL3_AD_HANDSHAKE_FAILURE: | |
2508 | return "TLS handshake failure. Possibly the server requires our TLS certificate but did not receive it"; | |
db302550 DDO |
2509 | default: |
2510 | return NULL; /* no hint available for TLS error */ | |
2511 | } | |
2512 | } | |
2513 | ||
2514 | static BIO *http_tls_shutdown(BIO *bio) | |
2515 | { | |
2516 | if (bio != NULL) { | |
2517 | BIO *cbio; | |
2518 | const char *hint = tls_error_hint(); | |
2519 | ||
2520 | if (hint != NULL) | |
2521 | BIO_printf(bio_err, "%s\n", hint); | |
2522 | (void)ERR_set_mark(); | |
2523 | BIO_ssl_shutdown(bio); | |
2524 | cbio = BIO_pop(bio); /* connect+HTTP BIO */ | |
2525 | BIO_free(bio); /* SSL BIO */ | |
2526 | (void)ERR_pop_to_mark(); /* hide SSL_R_READ_BIO_NOT_SET etc. */ | |
2527 | bio = cbio; | |
29f178bd | 2528 | } |
db302550 | 2529 | return bio; |
29f178bd DDO |
2530 | } |
2531 | ||
2532 | /* HTTP callback function that supports TLS connection also via HTTPS proxy */ | |
cdaf072f | 2533 | BIO *app_http_tls_cb(BIO *bio, void *arg, int connect, int detail) |
29f178bd | 2534 | { |
97b8c859 DDO |
2535 | APP_HTTP_TLS_INFO *info = (APP_HTTP_TLS_INFO *)arg; |
2536 | SSL_CTX *ssl_ctx = info->ssl_ctx; | |
2537 | ||
96e13a16 DDO |
2538 | if (ssl_ctx == NULL) /* not using TLS */ |
2539 | return bio; | |
2540 | if (connect) { | |
ef203432 DDO |
2541 | SSL *ssl; |
2542 | BIO *sbio = NULL; | |
30b9a6ec DDO |
2543 | X509_STORE *ts = SSL_CTX_get_cert_store(ssl_ctx); |
2544 | X509_VERIFY_PARAM *vpm = X509_STORE_get0_param(ts); | |
2545 | const char *host = vpm == NULL ? NULL : | |
2546 | X509_VERIFY_PARAM_get0_host(vpm, 0 /* first hostname */); | |
ef203432 | 2547 | |
068549f8 | 2548 | /* adapt after fixing callback design flaw, see #17088 */ |
29f178bd | 2549 | if ((info->use_proxy |
cdaf072f | 2550 | && !OSSL_HTTP_proxy_connect(bio, info->server, info->port, |
29f178bd DDO |
2551 | NULL, NULL, /* no proxy credentials */ |
2552 | info->timeout, bio_err, opt_getprog())) | |
2553 | || (sbio = BIO_new(BIO_f_ssl())) == NULL) { | |
2554 | return NULL; | |
2555 | } | |
db302550 | 2556 | if ((ssl = SSL_new(ssl_ctx)) == NULL) { |
29f178bd DDO |
2557 | BIO_free(sbio); |
2558 | return NULL; | |
2559 | } | |
2560 | ||
30b9a6ec DDO |
2561 | if (vpm != NULL) |
2562 | SSL_set_tlsext_host_name(ssl, host /* may be NULL */); | |
29f178bd DDO |
2563 | |
2564 | SSL_set_connect_state(ssl); | |
2565 | BIO_set_ssl(sbio, ssl, BIO_CLOSE); | |
2566 | ||
cdaf072f | 2567 | bio = BIO_push(sbio, bio); |
db302550 DDO |
2568 | } else { /* disconnect from TLS */ |
2569 | bio = http_tls_shutdown(bio); | |
cdaf072f DDO |
2570 | } |
2571 | return bio; | |
29f178bd DDO |
2572 | } |
2573 | ||
ef203432 DDO |
2574 | void APP_HTTP_TLS_INFO_free(APP_HTTP_TLS_INFO *info) |
2575 | { | |
2576 | if (info != NULL) { | |
2577 | SSL_CTX_free(info->ssl_ctx); | |
2578 | OPENSSL_free(info); | |
2579 | } | |
2580 | } | |
2581 | ||
29f178bd | 2582 | ASN1_VALUE *app_http_get_asn1(const char *url, const char *proxy, |
afe554c2 | 2583 | const char *no_proxy, SSL_CTX *ssl_ctx, |
29f178bd DDO |
2584 | const STACK_OF(CONF_VALUE) *headers, |
2585 | long timeout, const char *expected_content_type, | |
2586 | const ASN1_ITEM *it) | |
2587 | { | |
2588 | APP_HTTP_TLS_INFO info; | |
2589 | char *server; | |
2590 | char *port; | |
2591 | int use_ssl; | |
8f965908 | 2592 | BIO *mem; |
29f178bd DDO |
2593 | ASN1_VALUE *resp = NULL; |
2594 | ||
2595 | if (url == NULL || it == NULL) { | |
9311d0c4 | 2596 | ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER); |
29f178bd DDO |
2597 | return NULL; |
2598 | } | |
2599 | ||
7932982b DDO |
2600 | if (!OSSL_HTTP_parse_url(url, &use_ssl, NULL /* userinfo */, &server, &port, |
2601 | NULL /* port_num, */, NULL, NULL, NULL)) | |
29f178bd DDO |
2602 | return NULL; |
2603 | if (use_ssl && ssl_ctx == NULL) { | |
a150f8e1 RL |
2604 | ERR_raise_data(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER, |
2605 | "missing SSL_CTX"); | |
29f178bd DDO |
2606 | goto end; |
2607 | } | |
96e13a16 DDO |
2608 | if (!use_ssl && ssl_ctx != NULL) { |
2609 | ERR_raise_data(ERR_LIB_HTTP, ERR_R_PASSED_INVALID_ARGUMENT, | |
2610 | "SSL_CTX given but use_ssl == 0"); | |
2611 | goto end; | |
2612 | } | |
29f178bd DDO |
2613 | |
2614 | info.server = server; | |
2615 | info.port = port; | |
068549f8 DDO |
2616 | info.use_proxy = /* workaround for callback design flaw, see #17088 */ |
2617 | OSSL_HTTP_adapt_proxy(proxy, no_proxy, server, use_ssl) != NULL; | |
29f178bd DDO |
2618 | info.timeout = timeout; |
2619 | info.ssl_ctx = ssl_ctx; | |
8f965908 DDO |
2620 | mem = OSSL_HTTP_get(url, proxy, no_proxy, NULL /* bio */, NULL /* rbio */, |
2621 | app_http_tls_cb, &info, 0 /* buf_size */, headers, | |
2622 | expected_content_type, 1 /* expect_asn1 */, | |
647a5dbf | 2623 | OSSL_HTTP_DEFAULT_MAX_RESP_LEN, timeout); |
8f965908 DDO |
2624 | resp = ASN1_item_d2i_bio(it, mem, NULL); |
2625 | BIO_free(mem); | |
2626 | ||
29f178bd DDO |
2627 | end: |
2628 | OPENSSL_free(server); | |
2629 | OPENSSL_free(port); | |
2630 | return resp; | |
2631 | ||
2632 | } | |
2633 | ||
2634 | ASN1_VALUE *app_http_post_asn1(const char *host, const char *port, | |
2635 | const char *path, const char *proxy, | |
afe554c2 | 2636 | const char *no_proxy, SSL_CTX *ssl_ctx, |
29f178bd DDO |
2637 | const STACK_OF(CONF_VALUE) *headers, |
2638 | const char *content_type, | |
2639 | ASN1_VALUE *req, const ASN1_ITEM *req_it, | |
82990287 | 2640 | const char *expected_content_type, |
29f178bd DDO |
2641 | long timeout, const ASN1_ITEM *rsp_it) |
2642 | { | |
068549f8 | 2643 | int use_ssl = ssl_ctx != NULL; |
29f178bd | 2644 | APP_HTTP_TLS_INFO info; |
8f965908 DDO |
2645 | BIO *rsp, *req_mem = ASN1_item_i2d_mem_bio(req_it, req); |
2646 | ASN1_VALUE *res; | |
29f178bd | 2647 | |
8f965908 DDO |
2648 | if (req_mem == NULL) |
2649 | return NULL; | |
068549f8 | 2650 | |
29f178bd DDO |
2651 | info.server = host; |
2652 | info.port = port; | |
068549f8 DDO |
2653 | info.use_proxy = /* workaround for callback design flaw, see #17088 */ |
2654 | OSSL_HTTP_adapt_proxy(proxy, no_proxy, host, use_ssl) != NULL; | |
29f178bd DDO |
2655 | info.timeout = timeout; |
2656 | info.ssl_ctx = ssl_ctx; | |
068549f8 | 2657 | rsp = OSSL_HTTP_transfer(NULL, host, port, path, use_ssl, |
8f965908 DDO |
2658 | proxy, no_proxy, NULL /* bio */, NULL /* rbio */, |
2659 | app_http_tls_cb, &info, | |
2660 | 0 /* buf_size */, headers, content_type, req_mem, | |
82990287 | 2661 | expected_content_type, 1 /* expect_asn1 */, |
647a5dbf | 2662 | OSSL_HTTP_DEFAULT_MAX_RESP_LEN, timeout, |
8f965908 DDO |
2663 | 0 /* keep_alive */); |
2664 | BIO_free(req_mem); | |
2665 | res = ASN1_item_d2i_bio(rsp_it, rsp, NULL); | |
2666 | BIO_free(rsp); | |
2667 | return res; | |
29f178bd DDO |
2668 | } |
2669 | ||
2670 | #endif | |
2671 | ||
0a39d8f2 AP |
2672 | /* |
2673 | * Platform-specific sections | |
2674 | */ | |
ffa10187 | 2675 | #if defined(_WIN32) |
a1ad253f AP |
2676 | # ifdef fileno |
2677 | # undef fileno | |
2678 | # define fileno(a) (int)_fileno(a) | |
2679 | # endif | |
2680 | ||
2681 | # include <windows.h> | |
2682 | # include <tchar.h> | |
2683 | ||
2684 | static int WIN32_rename(const char *from, const char *to) | |
0f113f3e MC |
2685 | { |
2686 | TCHAR *tfrom = NULL, *tto; | |
2687 | DWORD err; | |
2688 | int ret = 0; | |
2689 | ||
2690 | if (sizeof(TCHAR) == 1) { | |
2691 | tfrom = (TCHAR *)from; | |
2692 | tto = (TCHAR *)to; | |
2693 | } else { /* UNICODE path */ | |
0f113f3e | 2694 | size_t i, flen = strlen(from) + 1, tlen = strlen(to) + 1; |
8cdb993d | 2695 | |
b1ad95e3 | 2696 | tfrom = malloc(sizeof(*tfrom) * (flen + tlen)); |
0f113f3e MC |
2697 | if (tfrom == NULL) |
2698 | goto err; | |
2699 | tto = tfrom + flen; | |
8cdb993d | 2700 | # if !defined(_WIN32_WCE) || _WIN32_WCE >= 101 |
0f113f3e MC |
2701 | if (!MultiByteToWideChar(CP_ACP, 0, from, flen, (WCHAR *)tfrom, flen)) |
2702 | # endif | |
2703 | for (i = 0; i < flen; i++) | |
2704 | tfrom[i] = (TCHAR)from[i]; | |
8cdb993d | 2705 | # if !defined(_WIN32_WCE) || _WIN32_WCE >= 101 |
0f113f3e MC |
2706 | if (!MultiByteToWideChar(CP_ACP, 0, to, tlen, (WCHAR *)tto, tlen)) |
2707 | # endif | |
2708 | for (i = 0; i < tlen; i++) | |
2709 | tto[i] = (TCHAR)to[i]; | |
2710 | } | |
2711 | ||
2712 | if (MoveFile(tfrom, tto)) | |
2713 | goto ok; | |
2714 | err = GetLastError(); | |
2715 | if (err == ERROR_ALREADY_EXISTS || err == ERROR_FILE_EXISTS) { | |
2716 | if (DeleteFile(tto) && MoveFile(tfrom, tto)) | |
2717 | goto ok; | |
2718 | err = GetLastError(); | |
2719 | } | |
2720 | if (err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND) | |
2721 | errno = ENOENT; | |
2722 | else if (err == ERROR_ACCESS_DENIED) | |
2723 | errno = EACCES; | |
2724 | else | |
2725 | errno = EINVAL; /* we could map more codes... */ | |
2726 | err: | |
2727 | ret = -1; | |
2728 | ok: | |
2729 | if (tfrom != NULL && tfrom != (TCHAR *)from) | |
2730 | free(tfrom); | |
2731 | return ret; | |
2732 | } | |
0a39d8f2 AP |
2733 | #endif |
2734 | ||
2735 | /* app_tminterval section */ | |
2736 | #if defined(_WIN32) | |
0f113f3e MC |
2737 | double app_tminterval(int stop, int usertime) |
2738 | { | |
2739 | FILETIME now; | |
2740 | double ret = 0; | |
2741 | static ULARGE_INTEGER tmstart; | |
2742 | static int warning = 1; | |
8cdb993d | 2743 | int use_GetSystemTime = 1; |
0f113f3e MC |
2744 | # ifdef _WIN32_WINNT |
2745 | static HANDLE proc = NULL; | |
2746 | ||
2747 | if (proc == NULL) { | |
2748 | if (check_winnt()) | |
2749 | proc = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, | |
2750 | GetCurrentProcessId()); | |
2751 | if (proc == NULL) | |
2752 | proc = (HANDLE) - 1; | |
2753 | } | |
2754 | ||
2755 | if (usertime && proc != (HANDLE) - 1) { | |
2756 | FILETIME junk; | |
8cdb993d | 2757 | |
0f113f3e | 2758 | GetProcessTimes(proc, &junk, &junk, &junk, &now); |
8cdb993d DDO |
2759 | use_GetSystemTime = 0; |
2760 | } | |
0f113f3e | 2761 | # endif |
8cdb993d | 2762 | if (use_GetSystemTime) { |
0f113f3e | 2763 | SYSTEMTIME systime; |
9135fddb | 2764 | |
0f113f3e MC |
2765 | if (usertime && warning) { |
2766 | BIO_printf(bio_err, "To get meaningful results, run " | |
2767 | "this program on idle system.\n"); | |
2768 | warning = 0; | |
2769 | } | |
2770 | GetSystemTime(&systime); | |
2771 | SystemTimeToFileTime(&systime, &now); | |
2772 | } | |
9135fddb | 2773 | |
0f113f3e MC |
2774 | if (stop == TM_START) { |
2775 | tmstart.u.LowPart = now.dwLowDateTime; | |
2776 | tmstart.u.HighPart = now.dwHighDateTime; | |
2777 | } else { | |
2778 | ULARGE_INTEGER tmstop; | |
9135fddb | 2779 | |
0f113f3e MC |
2780 | tmstop.u.LowPart = now.dwLowDateTime; |
2781 | tmstop.u.HighPart = now.dwHighDateTime; | |
9135fddb | 2782 | |
0f113f3e MC |
2783 | ret = (__int64)(tmstop.QuadPart - tmstart.QuadPart) * 1e-7; |
2784 | } | |
9135fddb | 2785 | |
26a7d938 | 2786 | return ret; |
0f113f3e | 2787 | } |
5c8b7b4c | 2788 | #elif defined(OPENSSL_SYS_VXWORKS) |
0f113f3e | 2789 | # include <time.h> |
0a39d8f2 | 2790 | |
0f113f3e MC |
2791 | double app_tminterval(int stop, int usertime) |
2792 | { | |
2793 | double ret = 0; | |
2794 | # ifdef CLOCK_REALTIME | |
2795 | static struct timespec tmstart; | |
2796 | struct timespec now; | |
2797 | # else | |
2798 | static unsigned long tmstart; | |
2799 | unsigned long now; | |
2800 | # endif | |
2801 | static int warning = 1; | |
2802 | ||
2803 | if (usertime && warning) { | |
2804 | BIO_printf(bio_err, "To get meaningful results, run " | |
2805 | "this program on idle system.\n"); | |
2806 | warning = 0; | |
2807 | } | |
2808 | # ifdef CLOCK_REALTIME | |
2809 | clock_gettime(CLOCK_REALTIME, &now); | |
2810 | if (stop == TM_START) | |
2811 | tmstart = now; | |
2812 | else | |
2813 | ret = ((now.tv_sec + now.tv_nsec * 1e-9) | |
2814 | - (tmstart.tv_sec + tmstart.tv_nsec * 1e-9)); | |
2815 | # else | |
2816 | now = tickGet(); | |
2817 | if (stop == TM_START) | |
2818 | tmstart = now; | |
2819 | else | |
2820 | ret = (now - tmstart) / (double)sysClkRateGet(); | |
2821 | # endif | |
26a7d938 | 2822 | return ret; |
0f113f3e | 2823 | } |
0a39d8f2 | 2824 | |
0f113f3e MC |
2825 | #elif defined(_SC_CLK_TCK) /* by means of unistd.h */ |
2826 | # include <sys/times.h> | |
0a39d8f2 | 2827 | |
0f113f3e MC |
2828 | double app_tminterval(int stop, int usertime) |
2829 | { | |
2830 | double ret = 0; | |
db71d315 | 2831 | struct tms rus; |
2bd928a1 TM |
2832 | clock_t now = times(&rus); |
2833 | static clock_t tmstart; | |
0f113f3e MC |
2834 | |
2835 | if (usertime) | |
2836 | now = rus.tms_utime; | |
2837 | ||
2234212c | 2838 | if (stop == TM_START) { |
0f113f3e | 2839 | tmstart = now; |
2234212c | 2840 | } else { |
2bd928a1 | 2841 | long int tck = sysconf(_SC_CLK_TCK); |
8cdb993d | 2842 | |
0f113f3e MC |
2843 | ret = (now - tmstart) / (double)tck; |
2844 | } | |
2845 | ||
26a7d938 | 2846 | return ret; |
0f113f3e | 2847 | } |
0a39d8f2 | 2848 | |
0f113f3e MC |
2849 | #else |
2850 | # include <sys/time.h> | |
2851 | # include <sys/resource.h> | |
0a39d8f2 | 2852 | |
0f113f3e MC |
2853 | double app_tminterval(int stop, int usertime) |
2854 | { | |
2855 | double ret = 0; | |
2856 | struct rusage rus; | |
2857 | struct timeval now; | |
2858 | static struct timeval tmstart; | |
2859 | ||
2860 | if (usertime) | |
2861 | getrusage(RUSAGE_SELF, &rus), now = rus.ru_utime; | |
2862 | else | |
2863 | gettimeofday(&now, NULL); | |
2864 | ||
2865 | if (stop == TM_START) | |
2866 | tmstart = now; | |
2867 | else | |
2868 | ret = ((now.tv_sec + now.tv_usec * 1e-6) | |
2869 | - (tmstart.tv_sec + tmstart.tv_usec * 1e-6)); | |
2870 | ||
2871 | return ret; | |
2872 | } | |
0a39d8f2 | 2873 | #endif |
a1ad253f | 2874 | |
8cdb993d | 2875 | int app_access(const char *name, int flag) |
7e1b7485 RS |
2876 | { |
2877 | #ifdef _WIN32 | |
2878 | return _access(name, flag); | |
2879 | #else | |
2880 | return access(name, flag); | |
2881 | #endif | |
2882 | } | |
2883 | ||
ffa10187 | 2884 | int app_isdir(const char *name) |
0f113f3e | 2885 | { |
a43ce58f | 2886 | return opt_isdir(name); |
0f113f3e | 2887 | } |
ffa10187 | 2888 | |
0a39d8f2 | 2889 | /* raw_read|write section */ |
51e5133d RL |
2890 | #if defined(__VMS) |
2891 | # include "vms_term_sock.h" | |
2892 | static int stdin_sock = -1; | |
2893 | ||
2894 | static void close_stdin_sock(void) | |
2895 | { | |
8cdb993d | 2896 | TerminalSocket(TERM_SOCK_DELETE, &stdin_sock); |
51e5133d RL |
2897 | } |
2898 | ||
2899 | int fileno_stdin(void) | |
2900 | { | |
2901 | if (stdin_sock == -1) { | |
2902 | TerminalSocket(TERM_SOCK_CREATE, &stdin_sock); | |
2903 | atexit(close_stdin_sock); | |
2904 | } | |
2905 | ||
2906 | return stdin_sock; | |
2907 | } | |
2908 | #else | |
2909 | int fileno_stdin(void) | |
2910 | { | |
2911 | return fileno(stdin); | |
2912 | } | |
2913 | #endif | |
2914 | ||
2915 | int fileno_stdout(void) | |
2916 | { | |
2917 | return fileno(stdout); | |
2918 | } | |
2919 | ||
ffa10187 | 2920 | #if defined(_WIN32) && defined(STD_INPUT_HANDLE) |
0f113f3e MC |
2921 | int raw_read_stdin(void *buf, int siz) |
2922 | { | |
2923 | DWORD n; | |
8cdb993d | 2924 | |
0f113f3e | 2925 | if (ReadFile(GetStdHandle(STD_INPUT_HANDLE), buf, siz, &n, NULL)) |
26a7d938 | 2926 | return n; |
0f113f3e | 2927 | else |
26a7d938 | 2928 | return -1; |
0f113f3e | 2929 | } |
51e5133d | 2930 | #elif defined(__VMS) |
2234212c | 2931 | # include <sys/socket.h> |
a19228b7 | 2932 | |
51e5133d RL |
2933 | int raw_read_stdin(void *buf, int siz) |
2934 | { | |
2935 | return recv(fileno_stdin(), buf, siz, 0); | |
2936 | } | |
ffa10187 | 2937 | #else |
08073700 RB |
2938 | # if defined(__TANDEM) |
2939 | # if defined(OPENSSL_TANDEM_FLOSS) | |
2940 | # include <floss.h(floss_read)> | |
2941 | # endif | |
2942 | # endif | |
0f113f3e MC |
2943 | int raw_read_stdin(void *buf, int siz) |
2944 | { | |
51e5133d | 2945 | return read(fileno_stdin(), buf, siz); |
0f113f3e | 2946 | } |
ffa10187 AP |
2947 | #endif |
2948 | ||
2949 | #if defined(_WIN32) && defined(STD_OUTPUT_HANDLE) | |
0f113f3e MC |
2950 | int raw_write_stdout(const void *buf, int siz) |
2951 | { | |
2952 | DWORD n; | |
8cdb993d | 2953 | |
0f113f3e | 2954 | if (WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buf, siz, &n, NULL)) |
26a7d938 | 2955 | return n; |
0f113f3e | 2956 | else |
26a7d938 | 2957 | return -1; |
0f113f3e | 2958 | } |
8cdb993d DDO |
2959 | #elif defined(OPENSSL_SYS_TANDEM) && defined(OPENSSL_THREADS) \ |
2960 | && defined(_SPT_MODEL_) | |
08073700 RB |
2961 | # if defined(__TANDEM) |
2962 | # if defined(OPENSSL_TANDEM_FLOSS) | |
2963 | # include <floss.h(floss_write)> | |
2964 | # endif | |
2965 | # endif | |
1287dabd | 2966 | int raw_write_stdout(const void *buf, int siz) |
08073700 | 2967 | { |
8cdb993d | 2968 | return write(fileno(stdout), (void *)buf, siz); |
08073700 | 2969 | } |
ffa10187 | 2970 | #else |
08073700 RB |
2971 | # if defined(__TANDEM) |
2972 | # if defined(OPENSSL_TANDEM_FLOSS) | |
2973 | # include <floss.h(floss_write)> | |
2974 | # endif | |
2975 | # endif | |
0f113f3e MC |
2976 | int raw_write_stdout(const void *buf, int siz) |
2977 | { | |
51e5133d | 2978 | return write(fileno_stdout(), buf, siz); |
0f113f3e | 2979 | } |
ffa10187 | 2980 | #endif |
a412b891 RL |
2981 | |
2982 | /* | |
a43ce58f | 2983 | * Centralized handling of input and output files with format specification |
a412b891 RL |
2984 | * The format is meant to show what the input and output is supposed to be, |
2985 | * and is therefore a show of intent more than anything else. However, it | |
a43ce58f | 2986 | * does impact behavior on some platforms, such as differentiating between |
a412b891 RL |
2987 | * text and binary input/output on non-Unix platforms |
2988 | */ | |
a60994df | 2989 | BIO *dup_bio_in(int format) |
a412b891 | 2990 | { |
a60994df | 2991 | return BIO_new_fp(stdin, |
a43ce58f | 2992 | BIO_NOCLOSE | (FMT_istext(format) ? BIO_FP_TEXT : 0)); |
a60994df RL |
2993 | } |
2994 | ||
2995 | BIO *dup_bio_out(int format) | |
2996 | { | |
2997 | BIO *b = BIO_new_fp(stdout, | |
a43ce58f | 2998 | BIO_NOCLOSE | (FMT_istext(format) ? BIO_FP_TEXT : 0)); |
71bb86f0 RL |
2999 | void *prefix = NULL; |
3000 | ||
fb03e614 | 3001 | if (b == NULL) |
3002 | return NULL; | |
3003 | ||
a412b891 | 3004 | #ifdef OPENSSL_SYS_VMS |
a43ce58f | 3005 | if (FMT_istext(format)) |
a60994df | 3006 | b = BIO_push(BIO_new(BIO_f_linebuffer()), b); |
149bd5d6 | 3007 | #endif |
71bb86f0 | 3008 | |
682b444f RL |
3009 | if (FMT_istext(format) |
3010 | && (prefix = getenv("HARNESS_OSSL_PREFIX")) != NULL) { | |
e79ae962 RL |
3011 | b = BIO_push(BIO_new(BIO_f_prefix()), b); |
3012 | BIO_set_prefix(b, prefix); | |
71bb86f0 RL |
3013 | } |
3014 | ||
149bd5d6 RL |
3015 | return b; |
3016 | } | |
3017 | ||
3018 | BIO *dup_bio_err(int format) | |
3019 | { | |
3020 | BIO *b = BIO_new_fp(stderr, | |
a43ce58f | 3021 | BIO_NOCLOSE | (FMT_istext(format) ? BIO_FP_TEXT : 0)); |
8cdb993d | 3022 | |
149bd5d6 | 3023 | #ifdef OPENSSL_SYS_VMS |
fb03e614 | 3024 | if (b != NULL && FMT_istext(format)) |
149bd5d6 | 3025 | b = BIO_push(BIO_new(BIO_f_linebuffer()), b); |
a412b891 RL |
3026 | #endif |
3027 | return b; | |
3028 | } | |
3029 | ||
3030 | void unbuffer(FILE *fp) | |
3031 | { | |
90dbd250 RL |
3032 | /* |
3033 | * On VMS, setbuf() will only take 32-bit pointers, and a compilation | |
3034 | * with /POINTER_SIZE=64 will give off a MAYLOSEDATA2 warning here. | |
3035 | * However, we trust that the C RTL will never give us a FILE pointer | |
3036 | * above the first 4 GB of memory, so we simply turn off the warning | |
3037 | * temporarily. | |
3038 | */ | |
3039 | #if defined(OPENSSL_SYS_VMS) && defined(__DECC) | |
3040 | # pragma environment save | |
3041 | # pragma message disable maylosedata2 | |
3042 | #endif | |
a412b891 | 3043 | setbuf(fp, NULL); |
90dbd250 RL |
3044 | #if defined(OPENSSL_SYS_VMS) && defined(__DECC) |
3045 | # pragma environment restore | |
3046 | #endif | |
a412b891 RL |
3047 | } |
3048 | ||
3049 | static const char *modestr(char mode, int format) | |
3050 | { | |
3051 | OPENSSL_assert(mode == 'a' || mode == 'r' || mode == 'w'); | |
3052 | ||
3053 | switch (mode) { | |
3054 | case 'a': | |
a43ce58f | 3055 | return FMT_istext(format) ? "a" : "ab"; |
a412b891 | 3056 | case 'r': |
a43ce58f | 3057 | return FMT_istext(format) ? "r" : "rb"; |
a412b891 | 3058 | case 'w': |
a43ce58f | 3059 | return FMT_istext(format) ? "w" : "wb"; |
a412b891 RL |
3060 | } |
3061 | /* The assert above should make sure we never reach this point */ | |
3062 | return NULL; | |
3063 | } | |
3064 | ||
3065 | static const char *modeverb(char mode) | |
3066 | { | |
3067 | switch (mode) { | |
3068 | case 'a': | |
3069 | return "appending"; | |
3070 | case 'r': | |
3071 | return "reading"; | |
3072 | case 'w': | |
3073 | return "writing"; | |
3074 | } | |
3075 | return "(doing something)"; | |
3076 | } | |
3077 | ||
3078 | /* | |
3079 | * Open a file for writing, owner-read-only. | |
3080 | */ | |
3081 | BIO *bio_open_owner(const char *filename, int format, int private) | |
3082 | { | |
3083 | FILE *fp = NULL; | |
3084 | BIO *b = NULL; | |
2f0a5381 P |
3085 | int textmode, bflags; |
3086 | #ifndef OPENSSL_NO_POSIX_IO | |
3087 | int fd = -1, mode; | |
3088 | #endif | |
a412b891 RL |
3089 | |
3090 | if (!private || filename == NULL || strcmp(filename, "-") == 0) | |
3091 | return bio_open_default(filename, 'w', format); | |
3092 | ||
2f0a5381 P |
3093 | textmode = FMT_istext(format); |
3094 | #ifndef OPENSSL_NO_POSIX_IO | |
a412b891 | 3095 | mode = O_WRONLY; |
2f0a5381 | 3096 | # ifdef O_CREAT |
a412b891 | 3097 | mode |= O_CREAT; |
2f0a5381 P |
3098 | # endif |
3099 | # ifdef O_TRUNC | |
a412b891 | 3100 | mode |= O_TRUNC; |
2f0a5381 | 3101 | # endif |
1cd5cc36 | 3102 | if (!textmode) { |
2f0a5381 | 3103 | # ifdef O_BINARY |
a412b891 | 3104 | mode |= O_BINARY; |
2f0a5381 | 3105 | # elif defined(_O_BINARY) |
a412b891 | 3106 | mode |= _O_BINARY; |
2f0a5381 | 3107 | # endif |
a412b891 RL |
3108 | } |
3109 | ||
2f0a5381 | 3110 | # ifdef OPENSSL_SYS_VMS |
8cdb993d DDO |
3111 | /* |
3112 | * VMS doesn't have O_BINARY, it just doesn't make sense. But, | |
fbd03b09 RL |
3113 | * it still needs to know that we're going binary, or fdopen() |
3114 | * will fail with "invalid argument"... so we tell VMS what the | |
3115 | * context is. | |
3116 | */ | |
3117 | if (!textmode) | |
3118 | fd = open(filename, mode, 0600, "ctx=bin"); | |
3119 | else | |
2f0a5381 | 3120 | # endif |
fbd03b09 | 3121 | fd = open(filename, mode, 0600); |
a412b891 RL |
3122 | if (fd < 0) |
3123 | goto err; | |
3124 | fp = fdopen(fd, modestr('w', format)); | |
2f0a5381 P |
3125 | #else /* OPENSSL_NO_POSIX_IO */ |
3126 | /* Have stdio but not Posix IO, do the best we can */ | |
3127 | fp = fopen(filename, modestr('w', format)); | |
3128 | #endif /* OPENSSL_NO_POSIX_IO */ | |
a412b891 RL |
3129 | if (fp == NULL) |
3130 | goto err; | |
3131 | bflags = BIO_CLOSE; | |
1cd5cc36 | 3132 | if (textmode) |
a412b891 RL |
3133 | bflags |= BIO_FP_TEXT; |
3134 | b = BIO_new_fp(fp, bflags); | |
2f0a5381 | 3135 | if (b != NULL) |
a412b891 RL |
3136 | return b; |
3137 | ||
3138 | err: | |
3139 | BIO_printf(bio_err, "%s: Can't open \"%s\" for writing, %s\n", | |
3140 | opt_getprog(), filename, strerror(errno)); | |
3141 | ERR_print_errors(bio_err); | |
3142 | /* If we have fp, then fdopen took over fd, so don't close both. */ | |
2f0a5381 | 3143 | if (fp != NULL) |
a412b891 | 3144 | fclose(fp); |
2f0a5381 | 3145 | #ifndef OPENSSL_NO_POSIX_IO |
a412b891 RL |
3146 | else if (fd >= 0) |
3147 | close(fd); | |
2f0a5381 | 3148 | #endif |
a412b891 RL |
3149 | return NULL; |
3150 | } | |
3151 | ||
3152 | static BIO *bio_open_default_(const char *filename, char mode, int format, | |
3153 | int quiet) | |
3154 | { | |
3155 | BIO *ret; | |
3156 | ||
3157 | if (filename == NULL || strcmp(filename, "-") == 0) { | |
a60994df | 3158 | ret = mode == 'r' ? dup_bio_in(format) : dup_bio_out(format); |
a412b891 RL |
3159 | if (quiet) { |
3160 | ERR_clear_error(); | |
3161 | return ret; | |
3162 | } | |
3163 | if (ret != NULL) | |
3164 | return ret; | |
3165 | BIO_printf(bio_err, | |
3166 | "Can't open %s, %s\n", | |
3167 | mode == 'r' ? "stdin" : "stdout", strerror(errno)); | |
3168 | } else { | |
3169 | ret = BIO_new_file(filename, modestr(mode, format)); | |
3170 | if (quiet) { | |
3171 | ERR_clear_error(); | |
3172 | return ret; | |
3173 | } | |
3174 | if (ret != NULL) | |
3175 | return ret; | |
3176 | BIO_printf(bio_err, | |
15795943 | 3177 | "Can't open \"%s\" for %s, %s\n", |
a412b891 RL |
3178 | filename, modeverb(mode), strerror(errno)); |
3179 | } | |
3180 | ERR_print_errors(bio_err); | |
3181 | return NULL; | |
3182 | } | |
3183 | ||
3184 | BIO *bio_open_default(const char *filename, char mode, int format) | |
3185 | { | |
3186 | return bio_open_default_(filename, mode, format, 0); | |
3187 | } | |
3188 | ||
3189 | BIO *bio_open_default_quiet(const char *filename, char mode, int format) | |
3190 | { | |
3191 | return bio_open_default_(filename, mode, format, 1); | |
3192 | } | |
3193 | ||
e1b9840e MC |
3194 | void wait_for_async(SSL *s) |
3195 | { | |
d6e03b70 MC |
3196 | /* On Windows select only works for sockets, so we simply don't wait */ |
3197 | #ifndef OPENSSL_SYS_WINDOWS | |
ff75a257 | 3198 | int width = 0; |
e1b9840e | 3199 | fd_set asyncfds; |
ff75a257 MC |
3200 | OSSL_ASYNC_FD *fds; |
3201 | size_t numfds; | |
0a345252 | 3202 | size_t i; |
e1b9840e | 3203 | |
ff75a257 MC |
3204 | if (!SSL_get_all_async_fds(s, NULL, &numfds)) |
3205 | return; | |
3206 | if (numfds == 0) | |
e1b9840e | 3207 | return; |
589902b2 | 3208 | fds = app_malloc(sizeof(OSSL_ASYNC_FD) * numfds, "allocate async fds"); |
ff75a257 MC |
3209 | if (!SSL_get_all_async_fds(s, fds, &numfds)) { |
3210 | OPENSSL_free(fds); | |
0a345252 | 3211 | return; |
ff75a257 | 3212 | } |
e1b9840e | 3213 | |
e1b9840e | 3214 | FD_ZERO(&asyncfds); |
0a345252 P |
3215 | for (i = 0; i < numfds; i++) { |
3216 | if (width <= (int)fds[i]) | |
3217 | width = (int)fds[i] + 1; | |
3218 | openssl_fdset((int)fds[i], &asyncfds); | |
ff75a257 | 3219 | } |
e1b9840e | 3220 | select(width, (void *)&asyncfds, NULL, NULL, NULL); |
0a345252 | 3221 | OPENSSL_free(fds); |
d6e03b70 | 3222 | #endif |
e1b9840e | 3223 | } |
75dd6c1a MC |
3224 | |
3225 | /* if OPENSSL_SYS_WINDOWS is defined then so is OPENSSL_SYS_MSDOS */ | |
3226 | #if defined(OPENSSL_SYS_MSDOS) | |
3227 | int has_stdin_waiting(void) | |
3228 | { | |
3229 | # if defined(OPENSSL_SYS_WINDOWS) | |
3230 | HANDLE inhand = GetStdHandle(STD_INPUT_HANDLE); | |
3231 | DWORD events = 0; | |
3232 | INPUT_RECORD inputrec; | |
3233 | DWORD insize = 1; | |
3234 | BOOL peeked; | |
3235 | ||
3236 | if (inhand == INVALID_HANDLE_VALUE) { | |
3237 | return 0; | |
3238 | } | |
3239 | ||
3240 | peeked = PeekConsoleInput(inhand, &inputrec, insize, &events); | |
3241 | if (!peeked) { | |
3242 | /* Probably redirected input? _kbhit() does not work in this case */ | |
3243 | if (!feof(stdin)) { | |
3244 | return 1; | |
3245 | } | |
3246 | return 0; | |
3247 | } | |
3248 | # endif | |
3249 | return _kbhit(); | |
3250 | } | |
3251 | #endif | |
17ebf85a DSH |
3252 | |
3253 | /* Corrupt a signature by modifying final byte */ | |
a0754084 | 3254 | void corrupt_signature(const ASN1_STRING *signature) |
17ebf85a | 3255 | { |
8cdb993d DDO |
3256 | unsigned char *s = signature->data; |
3257 | ||
3258 | s[signature->length - 1] ^= 0x1; | |
17ebf85a | 3259 | } |
dc047d31 DSH |
3260 | |
3261 | int set_cert_times(X509 *x, const char *startdate, const char *enddate, | |
3262 | int days) | |
3263 | { | |
dc047d31 | 3264 | if (startdate == NULL || strcmp(startdate, "today") == 0) { |
0b7347ef DSH |
3265 | if (X509_gmtime_adj(X509_getm_notBefore(x), 0) == NULL) |
3266 | return 0; | |
3267 | } else { | |
04e62715 | 3268 | if (!ASN1_TIME_set_string_X509(X509_getm_notBefore(x), startdate)) |
0b7347ef | 3269 | return 0; |
dc047d31 | 3270 | } |
dc047d31 | 3271 | if (enddate == NULL) { |
0b7347ef DSH |
3272 | if (X509_time_adj_ex(X509_getm_notAfter(x), days, 0, NULL) |
3273 | == NULL) | |
3274 | return 0; | |
04e62715 | 3275 | } else if (!ASN1_TIME_set_string_X509(X509_getm_notAfter(x), enddate)) { |
0b7347ef | 3276 | return 0; |
dc047d31 | 3277 | } |
0b7347ef | 3278 | return 1; |
dc047d31 | 3279 | } |
20967afb | 3280 | |
64713cb1 CN |
3281 | int set_crl_lastupdate(X509_CRL *crl, const char *lastupdate) |
3282 | { | |
3283 | int ret = 0; | |
3284 | ASN1_TIME *tm = ASN1_TIME_new(); | |
3285 | ||
3286 | if (tm == NULL) | |
3287 | goto end; | |
3288 | ||
3289 | if (lastupdate == NULL) { | |
3290 | if (X509_gmtime_adj(tm, 0) == NULL) | |
3291 | goto end; | |
3292 | } else { | |
3293 | if (!ASN1_TIME_set_string_X509(tm, lastupdate)) | |
3294 | goto end; | |
3295 | } | |
3296 | ||
3297 | if (!X509_CRL_set1_lastUpdate(crl, tm)) | |
3298 | goto end; | |
3299 | ||
3300 | ret = 1; | |
3301 | end: | |
3302 | ASN1_TIME_free(tm); | |
3303 | return ret; | |
3304 | } | |
3305 | ||
3306 | int set_crl_nextupdate(X509_CRL *crl, const char *nextupdate, | |
3307 | long days, long hours, long secs) | |
3308 | { | |
3309 | int ret = 0; | |
3310 | ASN1_TIME *tm = ASN1_TIME_new(); | |
3311 | ||
3312 | if (tm == NULL) | |
3313 | goto end; | |
3314 | ||
3315 | if (nextupdate == NULL) { | |
3316 | if (X509_time_adj_ex(tm, days, hours * 60 * 60 + secs, NULL) == NULL) | |
3317 | goto end; | |
3318 | } else { | |
3319 | if (!ASN1_TIME_set_string_X509(tm, nextupdate)) | |
3320 | goto end; | |
3321 | } | |
3322 | ||
3323 | if (!X509_CRL_set1_nextUpdate(crl, tm)) | |
3324 | goto end; | |
3325 | ||
3326 | ret = 1; | |
3327 | end: | |
3328 | ASN1_TIME_free(tm); | |
3329 | return ret; | |
3330 | } | |
3331 | ||
20967afb RS |
3332 | void make_uppercase(char *string) |
3333 | { | |
3334 | int i; | |
3335 | ||
3336 | for (i = 0; string[i] != '\0'; i++) | |
3337 | string[i] = toupper((unsigned char)string[i]); | |
3338 | } | |
a43ce58f | 3339 | |
95214b43 SL |
3340 | OSSL_PARAM *app_params_new_from_opts(STACK_OF(OPENSSL_STRING) *opts, |
3341 | const OSSL_PARAM *paramdefs) | |
3342 | { | |
3343 | OSSL_PARAM *params = NULL; | |
3344 | size_t sz = (size_t)sk_OPENSSL_STRING_num(opts); | |
3345 | size_t params_n; | |
3346 | char *opt = "", *stmp, *vtmp = NULL; | |
e1dcac22 | 3347 | int found = 1; |
95214b43 SL |
3348 | |
3349 | if (opts == NULL) | |
3350 | return NULL; | |
3351 | ||
3352 | params = OPENSSL_zalloc(sizeof(OSSL_PARAM) * (sz + 1)); | |
3353 | if (params == NULL) | |
3354 | return NULL; | |
3355 | ||
3356 | for (params_n = 0; params_n < sz; params_n++) { | |
3357 | opt = sk_OPENSSL_STRING_value(opts, (int)params_n); | |
3358 | if ((stmp = OPENSSL_strdup(opt)) == NULL | |
3359 | || (vtmp = strchr(stmp, ':')) == NULL) | |
3360 | goto err; | |
3361 | /* Replace ':' with 0 to terminate the string pointed to by stmp */ | |
3362 | *vtmp = 0; | |
3363 | /* Skip over the separator so that vmtp points to the value */ | |
3364 | vtmp++; | |
3365 | if (!OSSL_PARAM_allocate_from_text(¶ms[params_n], paramdefs, | |
e1dcac22 | 3366 | stmp, vtmp, strlen(vtmp), &found)) |
95214b43 SL |
3367 | goto err; |
3368 | OPENSSL_free(stmp); | |
3369 | } | |
3370 | params[params_n] = OSSL_PARAM_construct_end(); | |
3371 | return params; | |
3372 | err: | |
3373 | OPENSSL_free(stmp); | |
e1dcac22 P |
3374 | BIO_printf(bio_err, "Parameter %s '%s'\n", found ? "error" : "unknown", |
3375 | opt); | |
95214b43 SL |
3376 | ERR_print_errors(bio_err); |
3377 | app_params_free(params); | |
3378 | return NULL; | |
3379 | } | |
3380 | ||
3381 | void app_params_free(OSSL_PARAM *params) | |
3382 | { | |
3383 | int i; | |
3384 | ||
3385 | if (params != NULL) { | |
3386 | for (i = 0; params[i].key != NULL; ++i) | |
3387 | OPENSSL_free(params[i].data); | |
3388 | OPENSSL_free(params); | |
3389 | } | |
3390 | } | |
a7e4ca5b DDO |
3391 | |
3392 | EVP_PKEY *app_keygen(EVP_PKEY_CTX *ctx, const char *alg, int bits, int verbose) | |
3393 | { | |
3394 | EVP_PKEY *res = NULL; | |
3395 | ||
3396 | if (verbose && alg != NULL) { | |
3397 | BIO_printf(bio_err, "Generating %s key", alg); | |
3398 | if (bits > 0) | |
3399 | BIO_printf(bio_err, " with %d bits\n", bits); | |
3400 | else | |
3401 | BIO_printf(bio_err, "\n"); | |
3402 | } | |
3403 | if (!RAND_status()) | |
3404 | BIO_printf(bio_err, "Warning: generating random key material may take a long time\n" | |
3405 | "if the system has a poor entropy source\n"); | |
3406 | if (EVP_PKEY_keygen(ctx, &res) <= 0) | |
3407 | app_bail_out("%s: Error generating %s key\n", opt_getprog(), | |
3408 | alg != NULL ? alg : "asymmetric"); | |
3409 | return res; | |
3410 | } | |
3411 | ||
3412 | EVP_PKEY *app_paramgen(EVP_PKEY_CTX *ctx, const char *alg) | |
3413 | { | |
3414 | EVP_PKEY *res = NULL; | |
3415 | ||
3416 | if (!RAND_status()) | |
3417 | BIO_printf(bio_err, "Warning: generating random key parameters may take a long time\n" | |
3418 | "if the system has a poor entropy source\n"); | |
3419 | if (EVP_PKEY_paramgen(ctx, &res) <= 0) | |
3420 | app_bail_out("%s: Generating %s key parameters failed\n", | |
3421 | opt_getprog(), alg != NULL ? alg : "asymmetric"); | |
3422 | return res; | |
3423 | } | |
ff215713 P |
3424 | |
3425 | /* | |
3426 | * Return non-zero if the legacy path is still an option. | |
3427 | * This decision is based on the global command line operations and the | |
3428 | * behaviour thus far. | |
3429 | */ | |
3430 | int opt_legacy_okay(void) | |
3431 | { | |
3432 | int provider_options = opt_provider_option_given(); | |
3433 | int libctx = app_get0_libctx() != NULL || app_get0_propq() != NULL; | |
ff215713 P |
3434 | /* |
3435 | * Having a provider option specified or a custom library context or | |
3436 | * property query, is a sure sign we're not using legacy. | |
3437 | */ | |
3438 | if (provider_options || libctx) | |
3439 | return 0; | |
3440 | return 1; | |
3441 | } |