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