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