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