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