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