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