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