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