2 * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
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
10 #if !defined(_POSIX_C_SOURCE) && defined(OPENSSL_SYS_VMS)
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.
15 # define _POSIX_C_SOURCE 2
18 #ifndef OPENSSL_NO_ENGINE
19 /* We need to use some deprecated APIs */
20 # define OPENSSL_SUPPRESS_DEPRECATED
21 # include <openssl/engine.h>
27 #include <sys/types.h>
28 #ifndef OPENSSL_NO_POSIX_IO
29 # include <sys/stat.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>
52 static int WIN32_rename(const char *from
, const char *to
);
53 # define rename(from, to) WIN32_rename((from), (to))
56 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
60 #if defined(OPENSSL_SYS_MSDOS) && !defined(_WIN32) || defined(__BORLANDC__)
64 static BIO
*bio_open_default_(const char *filename
, char mode
, int format
,
67 #define PASS_SOURCE_SIZE_MAX 4
77 static int set_table_opts(unsigned long *flags
, const char *arg
,
78 const NAME_EX_TBL
* in_tbl
);
79 static int set_multi_opts(unsigned long *flags
, const char *arg
,
80 const NAME_EX_TBL
* in_tbl
);
81 int app_init(long mesgwin
);
83 int chopup_args(ARGS
*arg
, char *buf
)
86 char c
= '\0', *p
= NULL
;
91 arg
->argv
= app_malloc(sizeof(*arg
->argv
) * arg
->size
, "argv space");
95 /* Skip whitespace. */
96 while (*p
&& isspace(_UC(*p
)))
101 /* The start of something good :-) */
102 if (arg
->argc
>= arg
->size
) {
106 tmp
= OPENSSL_realloc(arg
->argv
, sizeof(*arg
->argv
) * arg
->size
);
111 quoted
= *p
== '\'' || *p
== '"';
114 arg
->argv
[arg
->argc
++] = p
;
116 /* now look for the end of this */
118 while (*p
&& *p
!= c
)
122 while (*p
&& !isspace(_UC(*p
)))
128 arg
->argv
[arg
->argc
] = NULL
;
133 int app_init(long mesgwin
)
139 int ctx_set_verify_locations(SSL_CTX
*ctx
,
140 const char *CAfile
, int noCAfile
,
141 const char *CApath
, int noCApath
,
142 const char *CAstore
, int noCAstore
)
144 if (CAfile
== NULL
&& CApath
== NULL
&& CAstore
== NULL
) {
145 if (!noCAfile
&& SSL_CTX_set_default_verify_file(ctx
) <= 0)
147 if (!noCApath
&& SSL_CTX_set_default_verify_dir(ctx
) <= 0)
149 if (!noCAstore
&& SSL_CTX_set_default_verify_store(ctx
) <= 0)
155 if (CAfile
!= NULL
&& !SSL_CTX_load_verify_file(ctx
, CAfile
))
157 if (CApath
!= NULL
&& !SSL_CTX_load_verify_dir(ctx
, CApath
))
159 if (CAstore
!= NULL
&& !SSL_CTX_load_verify_store(ctx
, CAstore
))
164 #ifndef OPENSSL_NO_CT
166 int ctx_set_ctlog_list_file(SSL_CTX
*ctx
, const char *path
)
169 return SSL_CTX_set_default_ctlog_list_file(ctx
);
171 return SSL_CTX_set_ctlog_list_file(ctx
, path
);
176 static unsigned long nmflag
= 0;
177 static char nmflag_set
= 0;
179 int set_nameopt(const char *arg
)
181 int ret
= set_name_ex(&nmflag
, arg
);
189 unsigned long get_nameopt(void)
192 nmflag_set
? nmflag
: XN_FLAG_SEP_CPLUS_SPC
| ASN1_STRFLGS_UTF8_CONVERT
;
195 void dump_cert_text(BIO
*out
, X509
*x
)
197 print_name(out
, "subject=", X509_get_subject_name(x
));
198 print_name(out
, "issuer=", X509_get_issuer_name(x
));
201 int wrap_password_callback(char *buf
, int bufsiz
, int verify
, void *userdata
)
203 return password_callback(buf
, bufsiz
, verify
, (PW_CB_DATA
*)userdata
);
206 static char *app_get_pass(const char *arg
, int keepbio
);
208 char *get_passwd(const char *pass
, const char *desc
)
214 if (!app_passwd(pass
, NULL
, &result
, NULL
))
215 BIO_printf(bio_err
, "Error getting password for %s\n", desc
);
216 if (pass
!= NULL
&& result
== NULL
) {
218 "Trying plain input string (better precede with 'pass:')\n");
219 result
= OPENSSL_strdup(pass
);
222 "Out of memory getting password for %s\n", desc
);
227 int app_passwd(const char *arg1
, const char *arg2
, char **pass1
, char **pass2
)
229 int same
= arg1
!= NULL
&& arg2
!= NULL
&& strcmp(arg1
, arg2
) == 0;
232 *pass1
= app_get_pass(arg1
, same
);
235 } else if (pass1
!= NULL
) {
239 *pass2
= app_get_pass(arg2
, same
? 2 : 0);
242 } else if (pass2
!= NULL
) {
248 static char *app_get_pass(const char *arg
, int keepbio
)
250 static BIO
*pwdbio
= NULL
;
251 char *tmp
, tpass
[APP_PASS_LEN
];
254 /* PASS_SOURCE_SIZE_MAX = max number of chars before ':' in below strings */
255 if (CHECK_AND_SKIP_PREFIX(arg
, "pass:"))
256 return OPENSSL_strdup(arg
);
257 if (CHECK_AND_SKIP_PREFIX(arg
, "env:")) {
260 BIO_printf(bio_err
, "No environment variable %s\n", arg
);
263 return OPENSSL_strdup(tmp
);
265 if (!keepbio
|| pwdbio
== NULL
) {
266 if (CHECK_AND_SKIP_PREFIX(arg
, "file:")) {
267 pwdbio
= BIO_new_file(arg
, "r");
268 if (pwdbio
== NULL
) {
269 BIO_printf(bio_err
, "Can't open file %s\n", arg
);
274 * Under _WIN32, which covers even Win64 and CE, file
275 * descriptors referenced by BIO_s_fd are not inherited
276 * by child process and therefore below is not an option.
277 * It could have been an option if bss_fd.c was operating
278 * on real Windows descriptors, such as those obtained
281 } else if (CHECK_AND_SKIP_PREFIX(arg
, "fd:")) {
286 pwdbio
= BIO_new_fd(i
, BIO_NOCLOSE
);
287 if ((i
< 0) || pwdbio
== NULL
) {
288 BIO_printf(bio_err
, "Can't access file descriptor %s\n", arg
);
292 * Can't do BIO_gets on an fd BIO so add a buffering BIO
294 btmp
= BIO_new(BIO_f_buffer());
296 BIO_free_all(pwdbio
);
298 BIO_printf(bio_err
, "Out of memory\n");
301 pwdbio
= BIO_push(btmp
, pwdbio
);
303 } else if (strcmp(arg
, "stdin") == 0) {
304 pwdbio
= dup_bio_in(FORMAT_TEXT
);
305 if (pwdbio
== NULL
) {
306 BIO_printf(bio_err
, "Can't open BIO for stdin\n");
310 /* argument syntax error; do not reveal too much about arg */
311 tmp
= strchr(arg
, ':');
312 if (tmp
== NULL
|| tmp
- arg
> PASS_SOURCE_SIZE_MAX
)
314 "Invalid password argument, missing ':' within the first %d chars\n",
315 PASS_SOURCE_SIZE_MAX
+ 1);
318 "Invalid password argument, starting with \"%.*s\"\n",
319 (int)(tmp
- arg
+ 1), arg
);
323 i
= BIO_gets(pwdbio
, tpass
, APP_PASS_LEN
);
325 BIO_free_all(pwdbio
);
329 BIO_printf(bio_err
, "Error reading password from BIO\n");
332 tmp
= strchr(tpass
, '\n');
335 return OPENSSL_strdup(tpass
);
338 CONF
*app_load_config_bio(BIO
*in
, const char *filename
)
344 conf
= NCONF_new_ex(app_get0_libctx(), NULL
);
345 i
= NCONF_load_bio(conf
, in
, &errorline
);
349 if (errorline
<= 0) {
350 BIO_printf(bio_err
, "%s: Can't load ", opt_getprog());
352 BIO_printf(bio_err
, "%s: Error on line %ld of ", opt_getprog(),
355 if (filename
!= NULL
)
356 BIO_printf(bio_err
, "config file \"%s\"\n", filename
);
358 BIO_printf(bio_err
, "config input");
364 CONF
*app_load_config_verbose(const char *filename
, int verbose
)
367 if (*filename
== '\0')
368 BIO_printf(bio_err
, "No configuration used\n");
370 BIO_printf(bio_err
, "Using configuration from %s\n", filename
);
372 return app_load_config_internal(filename
, 0);
375 CONF
*app_load_config_internal(const char *filename
, int quiet
)
380 if (filename
== NULL
|| *filename
!= '\0') {
381 if ((in
= bio_open_default_(filename
, 'r', FORMAT_TEXT
, quiet
)) == NULL
)
383 conf
= app_load_config_bio(in
, filename
);
386 /* Return empty config if filename is empty string. */
387 conf
= NCONF_new_ex(app_get0_libctx(), NULL
);
392 int app_load_modules(const CONF
*config
)
394 CONF
*to_free
= NULL
;
397 config
= to_free
= app_load_config_quiet(default_config_file
);
401 if (CONF_modules_load(config
, NULL
, 0) <= 0) {
402 BIO_printf(bio_err
, "Error configuring OpenSSL modules\n");
403 ERR_print_errors(bio_err
);
411 int add_oid_section(CONF
*conf
)
414 STACK_OF(CONF_VALUE
) *sktmp
;
418 if ((p
= NCONF_get_string(conf
, NULL
, "oid_section")) == NULL
) {
422 if ((sktmp
= NCONF_get_section(conf
, p
)) == NULL
) {
423 BIO_printf(bio_err
, "problem loading oid section %s\n", p
);
426 for (i
= 0; i
< sk_CONF_VALUE_num(sktmp
); i
++) {
427 cnf
= sk_CONF_VALUE_value(sktmp
, i
);
428 if (OBJ_create(cnf
->value
, cnf
->name
, cnf
->name
) == NID_undef
) {
429 BIO_printf(bio_err
, "problem creating object %s=%s\n",
430 cnf
->name
, cnf
->value
);
437 CONF
*app_load_config_modules(const char *configfile
)
441 if (configfile
!= NULL
) {
442 if ((conf
= app_load_config_verbose(configfile
, 1)) == NULL
)
444 if (configfile
!= default_config_file
&& !app_load_modules(conf
)) {
452 #define IS_HTTP(uri) ((uri) != NULL && HAS_PREFIX(uri, OSSL_HTTP_PREFIX))
453 #define IS_HTTPS(uri) ((uri) != NULL && HAS_PREFIX(uri, OSSL_HTTPS_PREFIX))
455 X509
*load_cert_pass(const char *uri
, int format
, int maybe_stdin
,
456 const char *pass
, const char *desc
)
461 desc
= "certificate";
463 BIO_printf(bio_err
, "Loading %s over HTTPS is unsupported\n", desc
);
464 } else if (IS_HTTP(uri
)) {
465 cert
= X509_load_http(uri
, NULL
, NULL
, 0 /* timeout */);
467 ERR_print_errors(bio_err
);
468 BIO_printf(bio_err
, "Unable to load %s from %s\n", desc
, uri
);
471 (void)load_key_certs_crls(uri
, format
, maybe_stdin
, pass
, desc
,
472 NULL
, NULL
, NULL
, &cert
, NULL
, NULL
, NULL
);
477 X509_CRL
*load_crl(const char *uri
, int format
, int maybe_stdin
,
480 X509_CRL
*crl
= NULL
;
485 BIO_printf(bio_err
, "Loading %s over HTTPS is unsupported\n", desc
);
486 } else if (IS_HTTP(uri
)) {
487 crl
= X509_CRL_load_http(uri
, NULL
, NULL
, 0 /* timeout */);
489 ERR_print_errors(bio_err
);
490 BIO_printf(bio_err
, "Unable to load %s from %s\n", desc
, uri
);
493 (void)load_key_certs_crls(uri
, format
, maybe_stdin
, NULL
, desc
,
494 NULL
, NULL
, NULL
, NULL
, NULL
, &crl
, NULL
);
499 X509_REQ
*load_csr(const char *file
, int format
, const char *desc
)
501 X509_REQ
*req
= NULL
;
504 if (format
== FORMAT_UNDEF
)
508 in
= bio_open_default(file
, 'r', format
);
512 if (format
== FORMAT_ASN1
)
513 req
= d2i_X509_REQ_bio(in
, NULL
);
514 else if (format
== FORMAT_PEM
)
515 req
= PEM_read_bio_X509_REQ(in
, NULL
, NULL
, NULL
);
517 print_format_error(format
, OPT_FMT_PEMDER
);
521 ERR_print_errors(bio_err
);
522 BIO_printf(bio_err
, "Unable to load %s\n", desc
);
528 void cleanse(char *str
)
531 OPENSSL_cleanse(str
, strlen(str
));
534 void clear_free(char *str
)
537 OPENSSL_clear_free(str
, strlen(str
));
540 EVP_PKEY
*load_key(const char *uri
, int format
, int may_stdin
,
541 const char *pass
, ENGINE
*e
, const char *desc
)
543 EVP_PKEY
*pkey
= NULL
;
544 char *allocated_uri
= NULL
;
547 desc
= "private key";
549 if (format
== FORMAT_ENGINE
) {
550 uri
= allocated_uri
= make_engine_uri(e
, uri
, desc
);
552 (void)load_key_certs_crls(uri
, format
, may_stdin
, pass
, desc
,
553 &pkey
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
555 OPENSSL_free(allocated_uri
);
559 EVP_PKEY
*load_pubkey(const char *uri
, int format
, int maybe_stdin
,
560 const char *pass
, ENGINE
*e
, const char *desc
)
562 EVP_PKEY
*pkey
= NULL
;
563 char *allocated_uri
= NULL
;
568 if (format
== FORMAT_ENGINE
) {
569 uri
= allocated_uri
= make_engine_uri(e
, uri
, desc
);
571 (void)load_key_certs_crls(uri
, format
, maybe_stdin
, pass
, desc
,
572 NULL
, &pkey
, NULL
, NULL
, NULL
, NULL
, NULL
);
574 OPENSSL_free(allocated_uri
);
578 EVP_PKEY
*load_keyparams_suppress(const char *uri
, int format
, int maybe_stdin
,
579 const char *keytype
, const char *desc
,
580 int suppress_decode_errors
)
582 EVP_PKEY
*params
= NULL
;
583 BIO
*bio_bak
= bio_err
;
586 desc
= "key parameters";
587 if (suppress_decode_errors
)
589 (void)load_key_certs_crls(uri
, format
, maybe_stdin
, NULL
, desc
,
590 NULL
, NULL
, ¶ms
, NULL
, NULL
, NULL
, NULL
);
591 if (params
!= NULL
&& keytype
!= NULL
&& !EVP_PKEY_is_a(params
, keytype
)) {
592 ERR_print_errors(bio_err
);
594 "Unable to load %s from %s (unexpected parameters type)\n",
596 EVP_PKEY_free(params
);
603 EVP_PKEY
*load_keyparams(const char *uri
, int format
, int maybe_stdin
,
604 const char *keytype
, const char *desc
)
606 return load_keyparams_suppress(uri
, format
, maybe_stdin
, keytype
, desc
, 0);
609 void app_bail_out(char *fmt
, ...)
614 BIO_vprintf(bio_err
, fmt
, args
);
616 ERR_print_errors(bio_err
);
620 void *app_malloc(size_t sz
, const char *what
)
622 void *vp
= OPENSSL_malloc(sz
);
625 app_bail_out("%s: Could not allocate %zu bytes for %s\n",
626 opt_getprog(), sz
, what
);
630 char *next_item(char *opt
) /* in list separated by comma and/or space */
632 /* advance to separator (comma or whitespace), if any */
633 while (*opt
!= ',' && !isspace(*opt
) && *opt
!= '\0')
636 /* terminate current item */
638 /* skip over any whitespace after separator */
639 while (isspace(*opt
))
642 return *opt
== '\0' ? NULL
: opt
; /* NULL indicates end of input */
645 static void warn_cert_msg(const char *uri
, X509
*cert
, const char *msg
)
647 char *subj
= X509_NAME_oneline(X509_get_subject_name(cert
), NULL
, 0);
649 BIO_printf(bio_err
, "Warning: certificate from '%s' with subject '%s' %s\n",
654 static void warn_cert(const char *uri
, X509
*cert
, int warn_EE
,
655 X509_VERIFY_PARAM
*vpm
)
657 uint32_t ex_flags
= X509_get_extension_flags(cert
);
658 int res
= X509_cmp_timeframe(vpm
, X509_get0_notBefore(cert
),
659 X509_get0_notAfter(cert
));
662 warn_cert_msg(uri
, cert
, res
> 0 ? "has expired" : "not yet valid");
663 if (warn_EE
&& (ex_flags
& EXFLAG_V1
) == 0 && (ex_flags
& EXFLAG_CA
) == 0)
664 warn_cert_msg(uri
, cert
, "is not a CA cert");
667 static void warn_certs(const char *uri
, STACK_OF(X509
) *certs
, int warn_EE
,
668 X509_VERIFY_PARAM
*vpm
)
672 for (i
= 0; i
< sk_X509_num(certs
); i
++)
673 warn_cert(uri
, sk_X509_value(certs
, i
), warn_EE
, vpm
);
676 int load_cert_certs(const char *uri
,
677 X509
**pcert
, STACK_OF(X509
) **pcerts
,
678 int exclude_http
, const char *pass
, const char *desc
,
679 X509_VERIFY_PARAM
*vpm
)
685 desc
= pcerts
== NULL
? "certificate" : "certificates";
686 if (exclude_http
&& (HAS_CASE_PREFIX(uri
, "http://")
687 || HAS_CASE_PREFIX(uri
, "https://"))) {
688 BIO_printf(bio_err
, "error: HTTP retrieval not allowed for %s\n", desc
);
691 pass_string
= get_passwd(pass
, desc
);
692 ret
= load_key_certs_crls(uri
, FORMAT_UNDEF
, 0, pass_string
, desc
,
693 NULL
, NULL
, NULL
, pcert
, pcerts
, NULL
, NULL
);
694 clear_free(pass_string
);
698 warn_cert(uri
, *pcert
, 0, vpm
);
700 warn_certs(uri
, *pcerts
, 1, vpm
);
702 if (pcerts
!= NULL
) {
703 OSSL_STACK_OF_X509_free(*pcerts
);
710 STACK_OF(X509
) *load_certs_multifile(char *files
, const char *pass
,
711 const char *desc
, X509_VERIFY_PARAM
*vpm
)
713 STACK_OF(X509
) *certs
= NULL
;
714 STACK_OF(X509
) *result
= sk_X509_new_null();
721 while (files
!= NULL
) {
722 char *next
= next_item(files
);
724 if (!load_cert_certs(files
, NULL
, &certs
, 0, pass
, desc
, vpm
))
726 if (!X509_add_certs(result
, certs
,
727 X509_ADD_FLAG_UP_REF
| X509_ADD_FLAG_NO_DUP
))
729 OSSL_STACK_OF_X509_free(certs
);
736 BIO_printf(bio_err
, "out of memory\n");
738 OSSL_STACK_OF_X509_free(certs
);
739 OSSL_STACK_OF_X509_free(result
);
743 static X509_STORE
*sk_X509_to_store(X509_STORE
*store
/* may be NULL */,
744 const STACK_OF(X509
) *certs
/* may NULL */)
749 store
= X509_STORE_new();
752 for (i
= 0; i
< sk_X509_num(certs
); i
++) {
753 if (!X509_STORE_add_cert(store
, sk_X509_value(certs
, i
))) {
754 X509_STORE_free(store
);
762 * Create cert store structure with certificates read from given file(s).
763 * Returns pointer to created X509_STORE on success, NULL on error.
765 X509_STORE
*load_certstore(char *input
, const char *pass
, const char *desc
,
766 X509_VERIFY_PARAM
*vpm
)
768 X509_STORE
*store
= NULL
;
769 STACK_OF(X509
) *certs
= NULL
;
771 while (input
!= NULL
) {
772 char *next
= next_item(input
);
775 if (!load_cert_certs(input
, NULL
, &certs
, 1, pass
, desc
, vpm
)) {
776 X509_STORE_free(store
);
779 ok
= (store
= sk_X509_to_store(store
, certs
)) != NULL
;
780 OSSL_STACK_OF_X509_free(certs
);
790 * Initialize or extend, if *certs != NULL, a certificate stack.
791 * The caller is responsible for freeing *certs if its value is left not NULL.
793 int load_certs(const char *uri
, int maybe_stdin
, STACK_OF(X509
) **certs
,
794 const char *pass
, const char *desc
)
796 int ret
, was_NULL
= *certs
== NULL
;
799 desc
= "certificates";
800 ret
= load_key_certs_crls(uri
, FORMAT_UNDEF
, maybe_stdin
, pass
, desc
,
801 NULL
, NULL
, NULL
, NULL
, certs
, NULL
, NULL
);
803 if (!ret
&& was_NULL
) {
804 OSSL_STACK_OF_X509_free(*certs
);
811 * Initialize or extend, if *crls != NULL, a certificate stack.
812 * The caller is responsible for freeing *crls if its value is left not NULL.
814 int load_crls(const char *uri
, STACK_OF(X509_CRL
) **crls
,
815 const char *pass
, const char *desc
)
817 int ret
, was_NULL
= *crls
== NULL
;
821 ret
= load_key_certs_crls(uri
, FORMAT_UNDEF
, 0, pass
, desc
,
822 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, crls
);
824 if (!ret
&& was_NULL
) {
825 sk_X509_CRL_pop_free(*crls
, X509_CRL_free
);
831 static const char *format2string(int format
)
842 /* Set type expectation, but clear it if objects of different types expected. */
843 #define SET_EXPECT(val) \
844 (expect = expect < 0 ? (val) : (expect == (val) ? (val) : 0))
845 #define SET_EXPECT1(pvar, val) \
846 if ((pvar) != NULL) { \
851 (ppkey != NULL ? "key etc." : ppubkey != NULL ? "public key etc." : \
852 pparams != NULL ? "params etc." : \
853 pcert != NULL ? "cert etc." : pcerts != NULL ? "certs etc." : \
854 pcrl != NULL ? "CRL etc." : pcrls != NULL ? "CRLs etc." : NULL)
856 * Load those types of credentials for which the result pointer is not NULL.
857 * Reads from stdio if uri is NULL and maybe_stdin is nonzero.
858 * For non-NULL ppkey, pcert, and pcrl the first suitable value found is loaded.
859 * If pcerts is non-NULL and *pcerts == NULL then a new cert list is allocated.
860 * If pcerts is non-NULL then all available certificates are appended to *pcerts
861 * except any certificate assigned to *pcert.
862 * If pcrls is non-NULL and *pcrls == NULL then a new list of CRLs is allocated.
863 * If pcrls is non-NULL then all available CRLs are appended to *pcerts
864 * except any CRL assigned to *pcrl.
865 * In any case (also on error) the caller is responsible for freeing all members
866 * of *pcerts and *pcrls (as far as they are not NULL).
868 int load_key_certs_crls(const char *uri
, int format
, int maybe_stdin
,
869 const char *pass
, const char *desc
, EVP_PKEY
**ppkey
,
870 EVP_PKEY
**ppubkey
, EVP_PKEY
**pparams
,
871 X509
**pcert
, STACK_OF(X509
) **pcerts
,
872 X509_CRL
**pcrl
, STACK_OF(X509_CRL
) **pcrls
)
875 OSSL_STORE_CTX
*ctx
= NULL
;
876 OSSL_LIB_CTX
*libctx
= app_get0_libctx();
877 const char *propq
= app_get0_propq();
878 int ncerts
= 0, ncrls
= 0, expect
= -1;
879 const char *failed
= FAIL_NAME
;
880 const char *input_type
;
882 const OSSL_PARAM
*params
= NULL
;
884 if (failed
== NULL
) {
885 BIO_printf(bio_err
, "Internal error: nothing to load from %s\n",
886 uri
!= NULL
? uri
: "<stdin>");
891 SET_EXPECT1(ppkey
, OSSL_STORE_INFO_PKEY
);
892 SET_EXPECT1(ppubkey
, OSSL_STORE_INFO_PUBKEY
);
893 SET_EXPECT1(pparams
, OSSL_STORE_INFO_PARAMS
);
894 SET_EXPECT1(pcert
, OSSL_STORE_INFO_CERT
);
895 if (pcerts
!= NULL
) {
896 if (*pcerts
== NULL
&& (*pcerts
= sk_X509_new_null()) == NULL
) {
897 BIO_printf(bio_err
, "Out of memory loading");
900 SET_EXPECT(OSSL_STORE_INFO_CERT
);
902 SET_EXPECT1(pcrl
, OSSL_STORE_INFO_CRL
);
904 if (*pcrls
== NULL
&& (*pcrls
= sk_X509_CRL_new_null()) == NULL
) {
905 BIO_printf(bio_err
, "Out of memory loading");
908 SET_EXPECT(OSSL_STORE_INFO_CRL
);
911 uidata
.password
= pass
;
912 uidata
.prompt_info
= uri
;
914 if ((input_type
= format2string(format
)) != NULL
) {
915 itp
[0] = OSSL_PARAM_construct_utf8_string(OSSL_STORE_PARAM_INPUT_TYPE
,
916 (char *)input_type
, 0);
917 itp
[1] = OSSL_PARAM_construct_end();
925 BIO_printf(bio_err
, "No filename or uri specified for loading");
930 bio
= BIO_new_fp(stdin
, 0);
932 ctx
= OSSL_STORE_attach(bio
, "file", libctx
, propq
,
933 get_ui_method(), &uidata
, params
,
938 ctx
= OSSL_STORE_open_ex(uri
, libctx
, propq
, get_ui_method(), &uidata
,
942 BIO_printf(bio_err
, "Could not open file or uri for loading");
945 if (expect
> 0 && !OSSL_STORE_expect(ctx
, expect
))
949 while ((ppkey
!= NULL
|| ppubkey
!= NULL
|| pparams
!= NULL
950 || pcert
!= NULL
|| pcerts
!= NULL
|| pcrl
!= NULL
|| pcrls
!= NULL
)
951 && !OSSL_STORE_eof(ctx
)) {
952 OSSL_STORE_INFO
*info
= OSSL_STORE_load(ctx
);
956 * This can happen (for example) if we attempt to load a file with
957 * multiple different types of things in it - but the thing we just
958 * tried to load wasn't one of the ones we wanted, e.g. if we're trying
959 * to load a certificate but the file has both the private key and the
960 * certificate in it. We just retry until eof.
966 type
= OSSL_STORE_INFO_get_type(info
);
968 case OSSL_STORE_INFO_PKEY
:
970 ok
= (*ppkey
= OSSL_STORE_INFO_get1_PKEY(info
)) != NULL
;
976 * An EVP_PKEY with private parts also holds the public parts,
977 * so if the caller asked for a public key, and we got a private
978 * key, we can still pass it back.
981 case OSSL_STORE_INFO_PUBKEY
:
982 if (ppubkey
!= NULL
) {
983 ok
= (*ppubkey
= OSSL_STORE_INFO_get1_PUBKEY(info
)) != NULL
;
988 case OSSL_STORE_INFO_PARAMS
:
989 if (pparams
!= NULL
) {
990 ok
= (*pparams
= OSSL_STORE_INFO_get1_PARAMS(info
)) != NULL
;
995 case OSSL_STORE_INFO_CERT
:
997 ok
= (*pcert
= OSSL_STORE_INFO_get1_CERT(info
)) != NULL
;
1000 } else if (pcerts
!= NULL
) {
1001 ok
= X509_add_cert(*pcerts
,
1002 OSSL_STORE_INFO_get1_CERT(info
),
1003 X509_ADD_FLAG_DEFAULT
);
1007 case OSSL_STORE_INFO_CRL
:
1009 ok
= (*pcrl
= OSSL_STORE_INFO_get1_CRL(info
)) != NULL
;
1012 } else if (pcrls
!= NULL
) {
1013 ok
= sk_X509_CRL_push(*pcrls
, OSSL_STORE_INFO_get1_CRL(info
));
1018 /* skip any other type */
1021 OSSL_STORE_INFO_free(info
);
1023 failed
= OSSL_STORE_INFO_type_string(type
);
1024 BIO_printf(bio_err
, "Error reading");
1030 OSSL_STORE_close(ctx
);
1035 if (failed
== NULL
) {
1038 BIO_printf(bio_err
, "Could not read");
1040 if (failed
!= NULL
) {
1041 unsigned long err
= ERR_peek_last_error();
1043 if (desc
!= NULL
&& strstr(desc
, failed
) != NULL
) {
1044 BIO_printf(bio_err
, " %s", desc
);
1046 BIO_printf(bio_err
, " %s", failed
);
1048 BIO_printf(bio_err
, " of %s", desc
);
1051 BIO_printf(bio_err
, " from %s", uri
);
1052 if (ERR_SYSTEM_ERROR(err
)) {
1053 /* provide more readable diagnostic output */
1054 BIO_printf(bio_err
, ": %s", strerror(ERR_GET_REASON(err
)));
1058 BIO_printf(bio_err
, "\n");
1059 ERR_print_errors(bio_err
);
1061 if (bio_err
== NULL
|| failed
== NULL
)
1062 /* clear any suppressed or spurious errors */
1065 ERR_clear_last_mark();
1066 return failed
== NULL
;
1069 #define X509V3_EXT_UNKNOWN_MASK (0xfL << 16)
1070 #define X509V3_EXT_DEFAULT 0 /* Return error for unknown exts */
1071 #define X509V3_EXT_ERROR_UNKNOWN (1L << 16) /* Print error for unknown exts */
1072 #define X509V3_EXT_PARSE_UNKNOWN (2L << 16) /* ASN1 parse unknown extensions */
1073 #define X509V3_EXT_DUMP_UNKNOWN (3L << 16) /* BIO_dump unknown extensions */
1075 #define X509_FLAG_CA (X509_FLAG_NO_ISSUER | X509_FLAG_NO_PUBKEY | \
1076 X509_FLAG_NO_HEADER | X509_FLAG_NO_VERSION)
1078 int set_cert_ex(unsigned long *flags
, const char *arg
)
1080 static const NAME_EX_TBL cert_tbl
[] = {
1081 {"compatible", X509_FLAG_COMPAT
, 0xffffffffl
},
1082 {"ca_default", X509_FLAG_CA
, 0xffffffffl
},
1083 {"no_header", X509_FLAG_NO_HEADER
, 0},
1084 {"no_version", X509_FLAG_NO_VERSION
, 0},
1085 {"no_serial", X509_FLAG_NO_SERIAL
, 0},
1086 {"no_signame", X509_FLAG_NO_SIGNAME
, 0},
1087 {"no_validity", X509_FLAG_NO_VALIDITY
, 0},
1088 {"no_subject", X509_FLAG_NO_SUBJECT
, 0},
1089 {"no_issuer", X509_FLAG_NO_ISSUER
, 0},
1090 {"no_pubkey", X509_FLAG_NO_PUBKEY
, 0},
1091 {"no_extensions", X509_FLAG_NO_EXTENSIONS
, 0},
1092 {"no_sigdump", X509_FLAG_NO_SIGDUMP
, 0},
1093 {"no_aux", X509_FLAG_NO_AUX
, 0},
1094 {"no_attributes", X509_FLAG_NO_ATTRIBUTES
, 0},
1095 {"ext_default", X509V3_EXT_DEFAULT
, X509V3_EXT_UNKNOWN_MASK
},
1096 {"ext_error", X509V3_EXT_ERROR_UNKNOWN
, X509V3_EXT_UNKNOWN_MASK
},
1097 {"ext_parse", X509V3_EXT_PARSE_UNKNOWN
, X509V3_EXT_UNKNOWN_MASK
},
1098 {"ext_dump", X509V3_EXT_DUMP_UNKNOWN
, X509V3_EXT_UNKNOWN_MASK
},
1101 return set_multi_opts(flags
, arg
, cert_tbl
);
1104 int set_name_ex(unsigned long *flags
, const char *arg
)
1106 static const NAME_EX_TBL ex_tbl
[] = {
1107 {"esc_2253", ASN1_STRFLGS_ESC_2253
, 0},
1108 {"esc_2254", ASN1_STRFLGS_ESC_2254
, 0},
1109 {"esc_ctrl", ASN1_STRFLGS_ESC_CTRL
, 0},
1110 {"esc_msb", ASN1_STRFLGS_ESC_MSB
, 0},
1111 {"use_quote", ASN1_STRFLGS_ESC_QUOTE
, 0},
1112 {"utf8", ASN1_STRFLGS_UTF8_CONVERT
, 0},
1113 {"ignore_type", ASN1_STRFLGS_IGNORE_TYPE
, 0},
1114 {"show_type", ASN1_STRFLGS_SHOW_TYPE
, 0},
1115 {"dump_all", ASN1_STRFLGS_DUMP_ALL
, 0},
1116 {"dump_nostr", ASN1_STRFLGS_DUMP_UNKNOWN
, 0},
1117 {"dump_der", ASN1_STRFLGS_DUMP_DER
, 0},
1118 {"compat", XN_FLAG_COMPAT
, 0xffffffffL
},
1119 {"sep_comma_plus", XN_FLAG_SEP_COMMA_PLUS
, XN_FLAG_SEP_MASK
},
1120 {"sep_comma_plus_space", XN_FLAG_SEP_CPLUS_SPC
, XN_FLAG_SEP_MASK
},
1121 {"sep_semi_plus_space", XN_FLAG_SEP_SPLUS_SPC
, XN_FLAG_SEP_MASK
},
1122 {"sep_multiline", XN_FLAG_SEP_MULTILINE
, XN_FLAG_SEP_MASK
},
1123 {"dn_rev", XN_FLAG_DN_REV
, 0},
1124 {"nofname", XN_FLAG_FN_NONE
, XN_FLAG_FN_MASK
},
1125 {"sname", XN_FLAG_FN_SN
, XN_FLAG_FN_MASK
},
1126 {"lname", XN_FLAG_FN_LN
, XN_FLAG_FN_MASK
},
1127 {"align", XN_FLAG_FN_ALIGN
, 0},
1128 {"oid", XN_FLAG_FN_OID
, XN_FLAG_FN_MASK
},
1129 {"space_eq", XN_FLAG_SPC_EQ
, 0},
1130 {"dump_unknown", XN_FLAG_DUMP_UNKNOWN_FIELDS
, 0},
1131 {"RFC2253", XN_FLAG_RFC2253
, 0xffffffffL
},
1132 {"oneline", XN_FLAG_ONELINE
, 0xffffffffL
},
1133 {"multiline", XN_FLAG_MULTILINE
, 0xffffffffL
},
1134 {"ca_default", XN_FLAG_MULTILINE
, 0xffffffffL
},
1137 if (set_multi_opts(flags
, arg
, ex_tbl
) == 0)
1139 if (*flags
!= XN_FLAG_COMPAT
1140 && (*flags
& XN_FLAG_SEP_MASK
) == 0)
1141 *flags
|= XN_FLAG_SEP_CPLUS_SPC
;
1145 int set_dateopt(unsigned long *dateopt
, const char *arg
)
1147 if (OPENSSL_strcasecmp(arg
, "rfc_822") == 0)
1148 *dateopt
= ASN1_DTFLGS_RFC822
;
1149 else if (OPENSSL_strcasecmp(arg
, "iso_8601") == 0)
1150 *dateopt
= ASN1_DTFLGS_ISO8601
;
1156 int set_ext_copy(int *copy_type
, const char *arg
)
1158 if (OPENSSL_strcasecmp(arg
, "none") == 0)
1159 *copy_type
= EXT_COPY_NONE
;
1160 else if (OPENSSL_strcasecmp(arg
, "copy") == 0)
1161 *copy_type
= EXT_COPY_ADD
;
1162 else if (OPENSSL_strcasecmp(arg
, "copyall") == 0)
1163 *copy_type
= EXT_COPY_ALL
;
1169 int copy_extensions(X509
*x
, X509_REQ
*req
, int copy_type
)
1171 STACK_OF(X509_EXTENSION
) *exts
;
1174 if (x
== NULL
|| req
== NULL
)
1176 if (copy_type
== EXT_COPY_NONE
)
1178 exts
= X509_REQ_get_extensions(req
);
1180 for (i
= 0; i
< sk_X509_EXTENSION_num(exts
); i
++) {
1181 X509_EXTENSION
*ext
= sk_X509_EXTENSION_value(exts
, i
);
1182 ASN1_OBJECT
*obj
= X509_EXTENSION_get_object(ext
);
1183 int idx
= X509_get_ext_by_OBJ(x
, obj
, -1);
1185 /* Does extension exist in target? */
1187 /* If normal copy don't override existing extension */
1188 if (copy_type
== EXT_COPY_ADD
)
1190 /* Delete all extensions of same type */
1192 X509_EXTENSION_free(X509_delete_ext(x
, idx
));
1193 idx
= X509_get_ext_by_OBJ(x
, obj
, -1);
1194 } while (idx
!= -1);
1196 if (!X509_add_ext(x
, ext
, -1))
1202 sk_X509_EXTENSION_pop_free(exts
, X509_EXTENSION_free
);
1206 static int set_multi_opts(unsigned long *flags
, const char *arg
,
1207 const NAME_EX_TBL
* in_tbl
)
1209 STACK_OF(CONF_VALUE
) *vals
;
1215 vals
= X509V3_parse_list(arg
);
1216 for (i
= 0; i
< sk_CONF_VALUE_num(vals
); i
++) {
1217 val
= sk_CONF_VALUE_value(vals
, i
);
1218 if (!set_table_opts(flags
, val
->name
, in_tbl
))
1221 sk_CONF_VALUE_pop_free(vals
, X509V3_conf_free
);
1225 static int set_table_opts(unsigned long *flags
, const char *arg
,
1226 const NAME_EX_TBL
* in_tbl
)
1229 const NAME_EX_TBL
*ptbl
;
1235 } else if (c
== '+') {
1242 for (ptbl
= in_tbl
; ptbl
->name
; ptbl
++) {
1243 if (OPENSSL_strcasecmp(arg
, ptbl
->name
) == 0) {
1244 *flags
&= ~ptbl
->mask
;
1246 *flags
|= ptbl
->flag
;
1248 *flags
&= ~ptbl
->flag
;
1255 void print_name(BIO
*out
, const char *title
, const X509_NAME
*nm
)
1260 unsigned long lflags
= get_nameopt();
1265 BIO_puts(out
, title
);
1266 if ((lflags
& XN_FLAG_SEP_MASK
) == XN_FLAG_SEP_MULTILINE
) {
1270 if (lflags
== XN_FLAG_COMPAT
) {
1271 buf
= X509_NAME_oneline(nm
, 0, 0);
1273 BIO_puts(out
, "\n");
1277 BIO_puts(out
, "\n");
1278 X509_NAME_print_ex(out
, nm
, indent
, lflags
);
1279 BIO_puts(out
, "\n");
1283 void print_bignum_var(BIO
*out
, const BIGNUM
*in
, const char *var
,
1284 int len
, unsigned char *buffer
)
1286 BIO_printf(out
, " static unsigned char %s_%d[] = {", var
, len
);
1287 if (BN_is_zero(in
)) {
1288 BIO_printf(out
, "\n 0x00");
1292 l
= BN_bn2bin(in
, buffer
);
1293 for (i
= 0; i
< l
; i
++) {
1294 BIO_printf(out
, (i
% 10) == 0 ? "\n " : " ");
1296 BIO_printf(out
, "0x%02X,", buffer
[i
]);
1298 BIO_printf(out
, "0x%02X", buffer
[i
]);
1301 BIO_printf(out
, "\n };\n");
1304 void print_array(BIO
*out
, const char *title
, int len
, const unsigned char *d
)
1308 BIO_printf(out
, "unsigned char %s[%d] = {", title
, len
);
1309 for (i
= 0; i
< len
; i
++) {
1311 BIO_printf(out
, "\n ");
1313 BIO_printf(out
, "0x%02X, ", d
[i
]);
1315 BIO_printf(out
, "0x%02X", d
[i
]);
1317 BIO_printf(out
, "\n};\n");
1320 X509_STORE
*setup_verify(const char *CAfile
, int noCAfile
,
1321 const char *CApath
, int noCApath
,
1322 const char *CAstore
, int noCAstore
)
1324 X509_STORE
*store
= X509_STORE_new();
1325 X509_LOOKUP
*lookup
;
1326 OSSL_LIB_CTX
*libctx
= app_get0_libctx();
1327 const char *propq
= app_get0_propq();
1332 if (CAfile
!= NULL
|| !noCAfile
) {
1333 lookup
= X509_STORE_add_lookup(store
, X509_LOOKUP_file());
1336 if (CAfile
!= NULL
) {
1337 if (X509_LOOKUP_load_file_ex(lookup
, CAfile
, X509_FILETYPE_PEM
,
1338 libctx
, propq
) <= 0) {
1339 BIO_printf(bio_err
, "Error loading file %s\n", CAfile
);
1343 X509_LOOKUP_load_file_ex(lookup
, NULL
, X509_FILETYPE_DEFAULT
,
1348 if (CApath
!= NULL
|| !noCApath
) {
1349 lookup
= X509_STORE_add_lookup(store
, X509_LOOKUP_hash_dir());
1352 if (CApath
!= NULL
) {
1353 if (X509_LOOKUP_add_dir(lookup
, CApath
, X509_FILETYPE_PEM
) <= 0) {
1354 BIO_printf(bio_err
, "Error loading directory %s\n", CApath
);
1358 X509_LOOKUP_add_dir(lookup
, NULL
, X509_FILETYPE_DEFAULT
);
1362 if (CAstore
!= NULL
|| !noCAstore
) {
1363 lookup
= X509_STORE_add_lookup(store
, X509_LOOKUP_store());
1366 if (!X509_LOOKUP_add_store_ex(lookup
, CAstore
, libctx
, propq
)) {
1367 if (CAstore
!= NULL
)
1368 BIO_printf(bio_err
, "Error loading store URI %s\n", CAstore
);
1376 ERR_print_errors(bio_err
);
1377 X509_STORE_free(store
);
1381 static unsigned long index_serial_hash(const OPENSSL_CSTRING
*a
)
1388 return OPENSSL_LH_strhash(n
);
1391 static int index_serial_cmp(const OPENSSL_CSTRING
*a
,
1392 const OPENSSL_CSTRING
*b
)
1394 const char *aa
, *bb
;
1396 for (aa
= a
[DB_serial
]; *aa
== '0'; aa
++) ;
1397 for (bb
= b
[DB_serial
]; *bb
== '0'; bb
++) ;
1398 return strcmp(aa
, bb
);
1401 static int index_name_qual(char **a
)
1403 return (a
[0][0] == 'V');
1406 static unsigned long index_name_hash(const OPENSSL_CSTRING
*a
)
1408 return OPENSSL_LH_strhash(a
[DB_name
]);
1411 int index_name_cmp(const OPENSSL_CSTRING
*a
, const OPENSSL_CSTRING
*b
)
1413 return strcmp(a
[DB_name
], b
[DB_name
]);
1416 static IMPLEMENT_LHASH_HASH_FN(index_serial
, OPENSSL_CSTRING
)
1417 static IMPLEMENT_LHASH_COMP_FN(index_serial
, OPENSSL_CSTRING
)
1418 static IMPLEMENT_LHASH_HASH_FN(index_name
, OPENSSL_CSTRING
)
1419 static IMPLEMENT_LHASH_COMP_FN(index_name
, OPENSSL_CSTRING
)
1422 BIGNUM
*load_serial(const char *serialfile
, int create
, ASN1_INTEGER
**retai
)
1427 ASN1_INTEGER
*ai
= NULL
;
1429 ai
= ASN1_INTEGER_new();
1433 in
= BIO_new_file(serialfile
, "r");
1441 if (ret
== NULL
|| !rand_serial(ret
, ai
))
1442 BIO_printf(bio_err
, "Out of memory\n");
1444 if (!a2i_ASN1_INTEGER(in
, ai
, buf
, 1024)) {
1445 BIO_printf(bio_err
, "Unable to load number from %s\n",
1449 ret
= ASN1_INTEGER_to_BN(ai
, NULL
);
1451 BIO_printf(bio_err
, "Error converting number from bin to BIGNUM\n");
1461 ERR_print_errors(bio_err
);
1463 ASN1_INTEGER_free(ai
);
1467 int save_serial(const char *serialfile
, const char *suffix
,
1468 const BIGNUM
*serial
, ASN1_INTEGER
**retai
)
1473 ASN1_INTEGER
*ai
= NULL
;
1477 j
= strlen(serialfile
);
1479 j
= strlen(serialfile
) + strlen(suffix
) + 1;
1481 BIO_printf(bio_err
, "File name too long\n");
1485 if (suffix
== NULL
) {
1486 OPENSSL_strlcpy(buf
[0], serialfile
, BSIZE
);
1488 #ifndef OPENSSL_SYS_VMS
1489 j
= BIO_snprintf(buf
[0], sizeof(buf
[0]), "%s.%s", serialfile
, suffix
);
1491 j
= BIO_snprintf(buf
[0], sizeof(buf
[0]), "%s-%s", serialfile
, suffix
);
1494 out
= BIO_new_file(buf
[0], "w");
1499 if ((ai
= BN_to_ASN1_INTEGER(serial
, NULL
)) == NULL
) {
1500 BIO_printf(bio_err
, "error converting serial to ASN.1 format\n");
1503 i2a_ASN1_INTEGER(out
, ai
);
1504 BIO_puts(out
, "\n");
1512 ERR_print_errors(bio_err
);
1514 ASN1_INTEGER_free(ai
);
1518 int rotate_serial(const char *serialfile
, const char *new_suffix
,
1519 const char *old_suffix
)
1524 i
= strlen(serialfile
) + strlen(old_suffix
);
1525 j
= strlen(serialfile
) + strlen(new_suffix
);
1528 if (j
+ 1 >= BSIZE
) {
1529 BIO_printf(bio_err
, "File name too long\n");
1532 #ifndef OPENSSL_SYS_VMS
1533 j
= BIO_snprintf(buf
[0], sizeof(buf
[0]), "%s.%s", serialfile
, new_suffix
);
1534 j
= BIO_snprintf(buf
[1], sizeof(buf
[1]), "%s.%s", serialfile
, old_suffix
);
1536 j
= BIO_snprintf(buf
[0], sizeof(buf
[0]), "%s-%s", serialfile
, new_suffix
);
1537 j
= BIO_snprintf(buf
[1], sizeof(buf
[1]), "%s-%s", serialfile
, old_suffix
);
1539 if (rename(serialfile
, buf
[1]) < 0 && errno
!= ENOENT
1545 "Unable to rename %s to %s\n", serialfile
, buf
[1]);
1549 if (rename(buf
[0], serialfile
) < 0) {
1551 "Unable to rename %s to %s\n", buf
[0], serialfile
);
1553 rename(buf
[1], serialfile
);
1558 ERR_print_errors(bio_err
);
1562 int rand_serial(BIGNUM
*b
, ASN1_INTEGER
*ai
)
1567 btmp
= b
== NULL
? BN_new() : b
;
1571 if (!BN_rand(btmp
, SERIAL_RAND_BITS
, BN_RAND_TOP_ANY
, BN_RAND_BOTTOM_ANY
))
1573 if (ai
&& !BN_to_ASN1_INTEGER(btmp
, ai
))
1586 CA_DB
*load_index(const char *dbfile
, DB_ATTR
*db_attr
)
1588 CA_DB
*retdb
= NULL
;
1589 TXT_DB
*tmpdb
= NULL
;
1591 CONF
*dbattr_conf
= NULL
;
1593 #ifndef OPENSSL_NO_POSIX_IO
1598 in
= BIO_new_file(dbfile
, "r");
1602 #ifndef OPENSSL_NO_POSIX_IO
1603 BIO_get_fp(in
, &dbfp
);
1604 if (fstat(fileno(dbfp
), &dbst
) == -1) {
1605 ERR_raise_data(ERR_LIB_SYS
, errno
,
1606 "calling fstat(%s)", dbfile
);
1611 if ((tmpdb
= TXT_DB_read(in
, DB_NUMBER
)) == NULL
)
1614 #ifndef OPENSSL_SYS_VMS
1615 BIO_snprintf(buf
, sizeof(buf
), "%s.attr", dbfile
);
1617 BIO_snprintf(buf
, sizeof(buf
), "%s-attr", dbfile
);
1619 dbattr_conf
= app_load_config_quiet(buf
);
1621 retdb
= app_malloc(sizeof(*retdb
), "new DB");
1625 retdb
->attributes
= *db_attr
;
1627 retdb
->attributes
.unique_subject
= 1;
1630 char *p
= NCONF_get_string(dbattr_conf
, NULL
, "unique_subject");
1633 retdb
->attributes
.unique_subject
= parse_yesno(p
, 1);
1637 retdb
->dbfname
= OPENSSL_strdup(dbfile
);
1638 #ifndef OPENSSL_NO_POSIX_IO
1643 ERR_print_errors(bio_err
);
1644 NCONF_free(dbattr_conf
);
1651 * Returns > 0 on success, <= 0 on error
1653 int index_index(CA_DB
*db
)
1655 if (!TXT_DB_create_index(db
->db
, DB_serial
, NULL
,
1656 LHASH_HASH_FN(index_serial
),
1657 LHASH_COMP_FN(index_serial
))) {
1659 "Error creating serial number index:(%ld,%ld,%ld)\n",
1660 db
->db
->error
, db
->db
->arg1
, db
->db
->arg2
);
1664 if (db
->attributes
.unique_subject
1665 && !TXT_DB_create_index(db
->db
, DB_name
, index_name_qual
,
1666 LHASH_HASH_FN(index_name
),
1667 LHASH_COMP_FN(index_name
))) {
1668 BIO_printf(bio_err
, "Error creating name index:(%ld,%ld,%ld)\n",
1669 db
->db
->error
, db
->db
->arg1
, db
->db
->arg2
);
1674 ERR_print_errors(bio_err
);
1678 int save_index(const char *dbfile
, const char *suffix
, CA_DB
*db
)
1684 j
= strlen(dbfile
) + strlen(suffix
);
1685 if (j
+ 6 >= BSIZE
) {
1686 BIO_printf(bio_err
, "File name too long\n");
1689 #ifndef OPENSSL_SYS_VMS
1690 j
= BIO_snprintf(buf
[2], sizeof(buf
[2]), "%s.attr", dbfile
);
1691 j
= BIO_snprintf(buf
[1], sizeof(buf
[1]), "%s.attr.%s", dbfile
, suffix
);
1692 j
= BIO_snprintf(buf
[0], sizeof(buf
[0]), "%s.%s", dbfile
, suffix
);
1694 j
= BIO_snprintf(buf
[2], sizeof(buf
[2]), "%s-attr", dbfile
);
1695 j
= BIO_snprintf(buf
[1], sizeof(buf
[1]), "%s-attr-%s", dbfile
, suffix
);
1696 j
= BIO_snprintf(buf
[0], sizeof(buf
[0]), "%s-%s", dbfile
, suffix
);
1698 out
= BIO_new_file(buf
[0], "w");
1701 BIO_printf(bio_err
, "Unable to open '%s'\n", dbfile
);
1704 j
= TXT_DB_write(out
, db
->db
);
1709 out
= BIO_new_file(buf
[1], "w");
1712 BIO_printf(bio_err
, "Unable to open '%s'\n", buf
[2]);
1715 BIO_printf(out
, "unique_subject = %s\n",
1716 db
->attributes
.unique_subject
? "yes" : "no");
1721 ERR_print_errors(bio_err
);
1725 int rotate_index(const char *dbfile
, const char *new_suffix
,
1726 const char *old_suffix
)
1731 i
= strlen(dbfile
) + strlen(old_suffix
);
1732 j
= strlen(dbfile
) + strlen(new_suffix
);
1735 if (j
+ 6 >= BSIZE
) {
1736 BIO_printf(bio_err
, "File name too long\n");
1739 #ifndef OPENSSL_SYS_VMS
1740 j
= BIO_snprintf(buf
[4], sizeof(buf
[4]), "%s.attr", dbfile
);
1741 j
= BIO_snprintf(buf
[3], sizeof(buf
[3]), "%s.attr.%s", dbfile
, old_suffix
);
1742 j
= BIO_snprintf(buf
[2], sizeof(buf
[2]), "%s.attr.%s", dbfile
, new_suffix
);
1743 j
= BIO_snprintf(buf
[1], sizeof(buf
[1]), "%s.%s", dbfile
, old_suffix
);
1744 j
= BIO_snprintf(buf
[0], sizeof(buf
[0]), "%s.%s", dbfile
, new_suffix
);
1746 j
= BIO_snprintf(buf
[4], sizeof(buf
[4]), "%s-attr", dbfile
);
1747 j
= BIO_snprintf(buf
[3], sizeof(buf
[3]), "%s-attr-%s", dbfile
, old_suffix
);
1748 j
= BIO_snprintf(buf
[2], sizeof(buf
[2]), "%s-attr-%s", dbfile
, new_suffix
);
1749 j
= BIO_snprintf(buf
[1], sizeof(buf
[1]), "%s-%s", dbfile
, old_suffix
);
1750 j
= BIO_snprintf(buf
[0], sizeof(buf
[0]), "%s-%s", dbfile
, new_suffix
);
1752 if (rename(dbfile
, buf
[1]) < 0 && errno
!= ENOENT
1757 BIO_printf(bio_err
, "Unable to rename %s to %s\n", dbfile
, buf
[1]);
1761 if (rename(buf
[0], dbfile
) < 0) {
1762 BIO_printf(bio_err
, "Unable to rename %s to %s\n", buf
[0], dbfile
);
1764 rename(buf
[1], dbfile
);
1767 if (rename(buf
[4], buf
[3]) < 0 && errno
!= ENOENT
1772 BIO_printf(bio_err
, "Unable to rename %s to %s\n", buf
[4], buf
[3]);
1774 rename(dbfile
, buf
[0]);
1775 rename(buf
[1], dbfile
);
1778 if (rename(buf
[2], buf
[4]) < 0) {
1779 BIO_printf(bio_err
, "Unable to rename %s to %s\n", buf
[2], buf
[4]);
1781 rename(buf
[3], buf
[4]);
1782 rename(dbfile
, buf
[0]);
1783 rename(buf
[1], dbfile
);
1788 ERR_print_errors(bio_err
);
1792 void free_index(CA_DB
*db
)
1795 TXT_DB_free(db
->db
);
1796 OPENSSL_free(db
->dbfname
);
1801 int parse_yesno(const char *str
, int def
)
1805 case 'f': /* false */
1806 case 'F': /* FALSE */
1811 case 't': /* true */
1812 case 'T': /* TRUE */
1823 * name is expected to be in the format /type0=value0/type1=value1/type2=...
1824 * where + can be used instead of / to form multi-valued RDNs if canmulti
1825 * and characters may be escaped by \
1827 X509_NAME
*parse_name(const char *cp
, int chtype
, int canmulti
,
1830 int nextismulti
= 0;
1836 "%s: %s name is expected to be in the format "
1837 "/type0=value0/type1=value1/type2=... where characters may "
1838 "be escaped by \\. This name is not in that format: '%s'\n",
1839 opt_getprog(), desc
, --cp
);
1843 n
= X509_NAME_new();
1845 BIO_printf(bio_err
, "%s: Out of memory\n", opt_getprog());
1848 work
= OPENSSL_strdup(cp
);
1850 BIO_printf(bio_err
, "%s: Error copying %s name input\n",
1851 opt_getprog(), desc
);
1855 while (*cp
!= '\0') {
1858 unsigned char *valstr
;
1860 int ismulti
= nextismulti
;
1864 /* Collect the type */
1865 while (*cp
!= '\0' && *cp
!= '=')
1870 "%s: Missing '=' after RDN type string '%s' in %s name string\n",
1871 opt_getprog(), typestr
, desc
);
1876 /* Collect the value. */
1877 valstr
= (unsigned char *)bp
;
1878 for (; *cp
!= '\0' && *cp
!= '/'; *bp
++ = *cp
++) {
1879 /* unescaped '+' symbol string signals further member of multiRDN */
1880 if (canmulti
&& *cp
== '+') {
1884 if (*cp
== '\\' && *++cp
== '\0') {
1886 "%s: Escape character at end of %s name string\n",
1887 opt_getprog(), desc
);
1893 /* If not at EOS (must be + or /), move forward. */
1898 nid
= OBJ_txt2nid(typestr
);
1899 if (nid
== NID_undef
) {
1901 "%s: Skipping unknown %s name attribute \"%s\"\n",
1902 opt_getprog(), desc
, typestr
);
1905 "Hint: a '+' in a value string needs be escaped using '\\' else a new member of a multi-valued RDN is expected\n");
1908 if (*valstr
== '\0') {
1910 "%s: No value provided for %s name attribute \"%s\", skipped\n",
1911 opt_getprog(), desc
, typestr
);
1914 if (!X509_NAME_add_entry_by_NID(n
, nid
, chtype
,
1915 valstr
, strlen((char *)valstr
),
1916 -1, ismulti
? -1 : 0)) {
1917 ERR_print_errors(bio_err
);
1919 "%s: Error adding %s name attribute \"/%s=%s\"\n",
1920 opt_getprog(), desc
, typestr
, valstr
);
1935 * Read whole contents of a BIO into an allocated memory buffer and return
1939 int bio_to_mem(unsigned char **out
, int maxlen
, BIO
*in
)
1943 unsigned char tbuf
[1024];
1945 mem
= BIO_new(BIO_s_mem());
1949 if ((maxlen
!= -1) && maxlen
< 1024)
1953 len
= BIO_read(in
, tbuf
, len
);
1960 if (BIO_write(mem
, tbuf
, len
) != len
) {
1969 ret
= BIO_get_mem_data(mem
, (char **)out
);
1970 BIO_set_flags(mem
, BIO_FLAGS_MEM_RDONLY
);
1975 int pkey_ctrl_string(EVP_PKEY_CTX
*ctx
, const char *value
)
1978 char *stmp
, *vtmp
= NULL
;
1980 stmp
= OPENSSL_strdup(value
);
1983 vtmp
= strchr(stmp
, ':');
1989 rv
= EVP_PKEY_CTX_ctrl_str(ctx
, stmp
, vtmp
);
1996 static void nodes_print(const char *name
, STACK_OF(X509_POLICY_NODE
) *nodes
)
1998 X509_POLICY_NODE
*node
;
2001 BIO_printf(bio_err
, "%s Policies:", name
);
2003 BIO_puts(bio_err
, "\n");
2004 for (i
= 0; i
< sk_X509_POLICY_NODE_num(nodes
); i
++) {
2005 node
= sk_X509_POLICY_NODE_value(nodes
, i
);
2006 X509_POLICY_NODE_print(bio_err
, node
, 2);
2009 BIO_puts(bio_err
, " <empty>\n");
2013 void policies_print(X509_STORE_CTX
*ctx
)
2015 X509_POLICY_TREE
*tree
;
2016 int explicit_policy
;
2018 tree
= X509_STORE_CTX_get0_policy_tree(ctx
);
2019 explicit_policy
= X509_STORE_CTX_get_explicit_policy(ctx
);
2021 BIO_printf(bio_err
, "Require explicit Policy: %s\n",
2022 explicit_policy
? "True" : "False");
2024 nodes_print("Authority", X509_policy_tree_get0_policies(tree
));
2025 nodes_print("User", X509_policy_tree_get0_user_policies(tree
));
2029 * next_protos_parse parses a comma separated list of strings into a string
2030 * in a format suitable for passing to SSL_CTX_set_next_protos_advertised.
2031 * outlen: (output) set to the length of the resulting buffer on success.
2032 * err: (maybe NULL) on failure, an error message line is written to this BIO.
2033 * in: a NUL terminated string like "abc,def,ghi"
2035 * returns: a malloc'd buffer or NULL on failure.
2037 unsigned char *next_protos_parse(size_t *outlen
, const char *in
)
2041 size_t i
, start
= 0;
2045 if (len
== 0 || len
>= 65535)
2048 out
= app_malloc(len
+ 1, "NPN buffer");
2049 for (i
= 0; i
<= len
; ++i
) {
2050 if (i
== len
|| in
[i
] == ',') {
2052 * Zero-length ALPN elements are invalid on the wire, we could be
2053 * strict and reject the entire string, but just ignoring extra
2054 * commas seems harmless and more friendly.
2056 * Every comma we skip in this way puts the input buffer another
2057 * byte ahead of the output buffer, so all stores into the output
2058 * buffer need to be decremented by the number commas skipped.
2065 if (i
- start
> 255) {
2069 out
[start
- skipped
] = (unsigned char)(i
- start
);
2072 out
[i
+ 1 - skipped
] = in
[i
];
2076 if (len
<= skipped
) {
2081 *outlen
= len
+ 1 - skipped
;
2085 int check_cert_attributes(BIO
*bio
, X509
*x
, const char *checkhost
,
2086 const char *checkemail
, const char *checkip
,
2097 if (checkhost
!= NULL
) {
2098 valid_host
= X509_check_host(x
, checkhost
, 0, 0, NULL
);
2100 BIO_printf(bio
, "Hostname %s does%s match certificate\n",
2101 checkhost
, valid_host
== 1 ? "" : " NOT");
2102 ret
= ret
&& valid_host
;
2105 if (checkemail
!= NULL
) {
2106 valid_mail
= X509_check_email(x
, checkemail
, 0, 0);
2108 BIO_printf(bio
, "Email %s does%s match certificate\n",
2109 checkemail
, valid_mail
? "" : " NOT");
2110 ret
= ret
&& valid_mail
;
2113 if (checkip
!= NULL
) {
2114 valid_ip
= X509_check_ip_asc(x
, checkip
, 0);
2116 BIO_printf(bio
, "IP %s does%s match certificate\n",
2117 checkip
, valid_ip
? "" : " NOT");
2118 ret
= ret
&& valid_ip
;
2124 static int do_pkey_ctx_init(EVP_PKEY_CTX
*pkctx
, STACK_OF(OPENSSL_STRING
) *opts
)
2131 for (i
= 0; i
< sk_OPENSSL_STRING_num(opts
); i
++) {
2132 char *opt
= sk_OPENSSL_STRING_value(opts
, i
);
2134 if (pkey_ctrl_string(pkctx
, opt
) <= 0) {
2135 BIO_printf(bio_err
, "parameter error \"%s\"\n", opt
);
2136 ERR_print_errors(bio_err
);
2144 static int do_x509_init(X509
*x
, STACK_OF(OPENSSL_STRING
) *opts
)
2151 for (i
= 0; i
< sk_OPENSSL_STRING_num(opts
); i
++) {
2152 char *opt
= sk_OPENSSL_STRING_value(opts
, i
);
2154 if (x509_ctrl_string(x
, opt
) <= 0) {
2155 BIO_printf(bio_err
, "parameter error \"%s\"\n", opt
);
2156 ERR_print_errors(bio_err
);
2164 static int do_x509_req_init(X509_REQ
*x
, STACK_OF(OPENSSL_STRING
) *opts
)
2171 for (i
= 0; i
< sk_OPENSSL_STRING_num(opts
); i
++) {
2172 char *opt
= sk_OPENSSL_STRING_value(opts
, i
);
2174 if (x509_req_ctrl_string(x
, opt
) <= 0) {
2175 BIO_printf(bio_err
, "parameter error \"%s\"\n", opt
);
2176 ERR_print_errors(bio_err
);
2184 static int do_sign_init(EVP_MD_CTX
*ctx
, EVP_PKEY
*pkey
,
2185 const char *md
, STACK_OF(OPENSSL_STRING
) *sigopts
)
2187 EVP_PKEY_CTX
*pkctx
= NULL
;
2193 * EVP_PKEY_get_default_digest_name() returns 2 if the digest is mandatory
2194 * for this algorithm.
2196 if (EVP_PKEY_get_default_digest_name(pkey
, def_md
, sizeof(def_md
)) == 2
2197 && strcmp(def_md
, "UNDEF") == 0) {
2198 /* The signing algorithm requires there to be no digest */
2202 return EVP_DigestSignInit_ex(ctx
, &pkctx
, md
, app_get0_libctx(),
2203 app_get0_propq(), pkey
, NULL
)
2204 && do_pkey_ctx_init(pkctx
, sigopts
);
2207 static int adapt_keyid_ext(X509
*cert
, X509V3_CTX
*ext_ctx
,
2208 const char *name
, const char *value
, int add_default
)
2210 const STACK_OF(X509_EXTENSION
) *exts
= X509_get0_extensions(cert
);
2211 X509_EXTENSION
*new_ext
= X509V3_EXT_nconf(NULL
, ext_ctx
, name
, value
);
2214 if (new_ext
== NULL
)
2217 idx
= X509v3_get_ext_by_OBJ(exts
, X509_EXTENSION_get_object(new_ext
), -1);
2219 X509_EXTENSION
*found_ext
= X509v3_get_ext(exts
, idx
);
2220 ASN1_OCTET_STRING
*encoded
= X509_EXTENSION_get_data(found_ext
);
2221 int disabled
= ASN1_STRING_length(encoded
) <= 2; /* indicating "none" */
2224 X509_delete_ext(cert
, idx
);
2225 X509_EXTENSION_free(found_ext
);
2226 } /* else keep existing key identifier, which might be outdated */
2229 rv
= !add_default
|| X509_add_ext(cert
, new_ext
, -1);
2231 X509_EXTENSION_free(new_ext
);
2235 int cert_matches_key(const X509
*cert
, const EVP_PKEY
*pkey
)
2240 match
= X509_check_private_key(cert
, pkey
);
2245 /* Ensure RFC 5280 compliance, adapt keyIDs as needed, and sign the cert info */
2246 int do_X509_sign(X509
*cert
, EVP_PKEY
*pkey
, const char *md
,
2247 STACK_OF(OPENSSL_STRING
) *sigopts
, X509V3_CTX
*ext_ctx
)
2249 const STACK_OF(X509_EXTENSION
) *exts
= X509_get0_extensions(cert
);
2250 EVP_MD_CTX
*mctx
= EVP_MD_CTX_new();
2254 if (sk_X509_EXTENSION_num(exts
/* may be NULL */) > 0) {
2255 /* Prevent X509_V_ERR_EXTENSIONS_REQUIRE_VERSION_3 */
2256 if (!X509_set_version(cert
, X509_VERSION_3
))
2260 * Add default SKID before AKID such that AKID can make use of it
2261 * in case the certificate is self-signed
2263 /* Prevent X509_V_ERR_MISSING_SUBJECT_KEY_IDENTIFIER */
2264 if (!adapt_keyid_ext(cert
, ext_ctx
, "subjectKeyIdentifier", "hash", 1))
2266 /* Prevent X509_V_ERR_MISSING_AUTHORITY_KEY_IDENTIFIER */
2267 self_sign
= cert_matches_key(cert
, pkey
);
2268 if (!adapt_keyid_ext(cert
, ext_ctx
, "authorityKeyIdentifier",
2269 "keyid, issuer", !self_sign
))
2273 if (mctx
!= NULL
&& do_sign_init(mctx
, pkey
, md
, sigopts
) > 0)
2274 rv
= (X509_sign_ctx(cert
, mctx
) > 0);
2276 EVP_MD_CTX_free(mctx
);
2280 /* Sign the certificate request info */
2281 int do_X509_REQ_sign(X509_REQ
*x
, EVP_PKEY
*pkey
, const char *md
,
2282 STACK_OF(OPENSSL_STRING
) *sigopts
)
2285 EVP_MD_CTX
*mctx
= EVP_MD_CTX_new();
2287 if (do_sign_init(mctx
, pkey
, md
, sigopts
) > 0)
2288 rv
= (X509_REQ_sign_ctx(x
, mctx
) > 0);
2289 EVP_MD_CTX_free(mctx
);
2293 /* Sign the CRL info */
2294 int do_X509_CRL_sign(X509_CRL
*x
, EVP_PKEY
*pkey
, const char *md
,
2295 STACK_OF(OPENSSL_STRING
) *sigopts
)
2298 EVP_MD_CTX
*mctx
= EVP_MD_CTX_new();
2300 if (do_sign_init(mctx
, pkey
, md
, sigopts
) > 0)
2301 rv
= (X509_CRL_sign_ctx(x
, mctx
) > 0);
2302 EVP_MD_CTX_free(mctx
);
2307 * do_X509_verify returns 1 if the signature is valid,
2308 * 0 if the signature check fails, or -1 if error occurs.
2310 int do_X509_verify(X509
*x
, EVP_PKEY
*pkey
, STACK_OF(OPENSSL_STRING
) *vfyopts
)
2314 if (do_x509_init(x
, vfyopts
) > 0)
2315 rv
= X509_verify(x
, pkey
);
2322 * do_X509_REQ_verify returns 1 if the signature is valid,
2323 * 0 if the signature check fails, or -1 if error occurs.
2325 int do_X509_REQ_verify(X509_REQ
*x
, EVP_PKEY
*pkey
,
2326 STACK_OF(OPENSSL_STRING
) *vfyopts
)
2330 if (do_x509_req_init(x
, vfyopts
) > 0)
2331 rv
= X509_REQ_verify_ex(x
, pkey
, app_get0_libctx(), app_get0_propq());
2337 /* Get first http URL from a DIST_POINT structure */
2339 static const char *get_dp_url(DIST_POINT
*dp
)
2341 GENERAL_NAMES
*gens
;
2346 if (!dp
->distpoint
|| dp
->distpoint
->type
!= 0)
2348 gens
= dp
->distpoint
->name
.fullname
;
2349 for (i
= 0; i
< sk_GENERAL_NAME_num(gens
); i
++) {
2350 gen
= sk_GENERAL_NAME_value(gens
, i
);
2351 uri
= GENERAL_NAME_get0_value(gen
, >ype
);
2352 if (gtype
== GEN_URI
&& ASN1_STRING_length(uri
) > 6) {
2353 const char *uptr
= (const char *)ASN1_STRING_get0_data(uri
);
2355 if (IS_HTTP(uptr
)) /* can/should not use HTTPS here */
2363 * Look through a CRLDP structure and attempt to find an http URL to
2364 * downloads a CRL from.
2367 static X509_CRL
*load_crl_crldp(STACK_OF(DIST_POINT
) *crldp
)
2370 const char *urlptr
= NULL
;
2372 for (i
= 0; i
< sk_DIST_POINT_num(crldp
); i
++) {
2373 DIST_POINT
*dp
= sk_DIST_POINT_value(crldp
, i
);
2375 urlptr
= get_dp_url(dp
);
2377 return load_crl(urlptr
, FORMAT_UNDEF
, 0, "CRL via CDP");
2383 * Example of downloading CRLs from CRLDP:
2384 * not usable for real world as it always downloads and doesn't cache anything.
2387 static STACK_OF(X509_CRL
) *crls_http_cb(const X509_STORE_CTX
*ctx
,
2388 const X509_NAME
*nm
)
2391 STACK_OF(X509_CRL
) *crls
= NULL
;
2393 STACK_OF(DIST_POINT
) *crldp
;
2395 crls
= sk_X509_CRL_new_null();
2398 x
= X509_STORE_CTX_get_current_cert(ctx
);
2399 crldp
= X509_get_ext_d2i(x
, NID_crl_distribution_points
, NULL
, NULL
);
2400 crl
= load_crl_crldp(crldp
);
2401 sk_DIST_POINT_pop_free(crldp
, DIST_POINT_free
);
2403 sk_X509_CRL_free(crls
);
2406 sk_X509_CRL_push(crls
, crl
);
2407 /* Try to download delta CRL */
2408 crldp
= X509_get_ext_d2i(x
, NID_freshest_crl
, NULL
, NULL
);
2409 crl
= load_crl_crldp(crldp
);
2410 sk_DIST_POINT_pop_free(crldp
, DIST_POINT_free
);
2412 sk_X509_CRL_push(crls
, crl
);
2416 void store_setup_crl_download(X509_STORE
*st
)
2418 X509_STORE_set_lookup_crls_cb(st
, crls_http_cb
);
2421 #ifndef OPENSSL_NO_SOCK
2422 static const char *tls_error_hint(void)
2424 unsigned long err
= ERR_peek_error();
2426 if (ERR_GET_LIB(err
) != ERR_LIB_SSL
)
2427 err
= ERR_peek_last_error();
2428 if (ERR_GET_LIB(err
) != ERR_LIB_SSL
)
2431 switch (ERR_GET_REASON(err
)) {
2432 case SSL_R_WRONG_VERSION_NUMBER
:
2433 return "The server does not support (a suitable version of) TLS";
2434 case SSL_R_UNKNOWN_PROTOCOL
:
2435 return "The server does not support HTTPS";
2436 case SSL_R_CERTIFICATE_VERIFY_FAILED
:
2437 return "Cannot authenticate server via its TLS certificate, likely due to mismatch with our trusted TLS certs or missing revocation status";
2438 case SSL_AD_REASON_OFFSET
+ TLS1_AD_UNKNOWN_CA
:
2439 return "Server did not accept our TLS certificate, likely due to mismatch with server's trust anchor or missing revocation status";
2440 case SSL_AD_REASON_OFFSET
+ SSL3_AD_HANDSHAKE_FAILURE
:
2441 return "TLS handshake failure. Possibly the server requires our TLS certificate but did not receive it";
2442 default: /* no error or no hint available for error */
2447 /* HTTP callback function that supports TLS connection also via HTTPS proxy */
2448 BIO
*app_http_tls_cb(BIO
*bio
, void *arg
, int connect
, int detail
)
2450 APP_HTTP_TLS_INFO
*info
= (APP_HTTP_TLS_INFO
*)arg
;
2451 SSL_CTX
*ssl_ctx
= info
->ssl_ctx
;
2453 if (ssl_ctx
== NULL
) /* not using TLS */
2459 /* adapt after fixing callback design flaw, see #17088 */
2460 if ((info
->use_proxy
2461 && !OSSL_HTTP_proxy_connect(bio
, info
->server
, info
->port
,
2462 NULL
, NULL
, /* no proxy credentials */
2463 info
->timeout
, bio_err
, opt_getprog()))
2464 || (sbio
= BIO_new(BIO_f_ssl())) == NULL
) {
2467 if (ssl_ctx
== NULL
|| (ssl
= SSL_new(ssl_ctx
)) == NULL
) {
2472 /* adapt after fixing callback design flaw, see #17088 */
2473 SSL_set_tlsext_host_name(ssl
, info
->server
); /* not critical to do */
2475 SSL_set_connect_state(ssl
);
2476 BIO_set_ssl(sbio
, ssl
, BIO_CLOSE
);
2478 bio
= BIO_push(sbio
, bio
);
2484 if (!detail
) { /* disconnecting after error */
2485 hint
= tls_error_hint();
2487 ERR_add_error_data(2, " : ", hint
);
2489 if (ssl_ctx
!= NULL
) {
2490 (void)ERR_set_mark();
2491 BIO_ssl_shutdown(bio
);
2492 cbio
= BIO_pop(bio
); /* connect+HTTP BIO */
2493 BIO_free(bio
); /* SSL BIO */
2494 (void)ERR_pop_to_mark(); /* hide SSL_R_READ_BIO_NOT_SET etc. */
2501 void APP_HTTP_TLS_INFO_free(APP_HTTP_TLS_INFO
*info
)
2504 SSL_CTX_free(info
->ssl_ctx
);
2509 ASN1_VALUE
*app_http_get_asn1(const char *url
, const char *proxy
,
2510 const char *no_proxy
, SSL_CTX
*ssl_ctx
,
2511 const STACK_OF(CONF_VALUE
) *headers
,
2512 long timeout
, const char *expected_content_type
,
2513 const ASN1_ITEM
*it
)
2515 APP_HTTP_TLS_INFO info
;
2520 ASN1_VALUE
*resp
= NULL
;
2522 if (url
== NULL
|| it
== NULL
) {
2523 ERR_raise(ERR_LIB_HTTP
, ERR_R_PASSED_NULL_PARAMETER
);
2527 if (!OSSL_HTTP_parse_url(url
, &use_ssl
, NULL
/* userinfo */, &server
, &port
,
2528 NULL
/* port_num, */, NULL
, NULL
, NULL
))
2530 if (use_ssl
&& ssl_ctx
== NULL
) {
2531 ERR_raise_data(ERR_LIB_HTTP
, ERR_R_PASSED_NULL_PARAMETER
,
2535 if (!use_ssl
&& ssl_ctx
!= NULL
) {
2536 ERR_raise_data(ERR_LIB_HTTP
, ERR_R_PASSED_INVALID_ARGUMENT
,
2537 "SSL_CTX given but use_ssl == 0");
2541 info
.server
= server
;
2543 info
.use_proxy
= /* workaround for callback design flaw, see #17088 */
2544 OSSL_HTTP_adapt_proxy(proxy
, no_proxy
, server
, use_ssl
) != NULL
;
2545 info
.timeout
= timeout
;
2546 info
.ssl_ctx
= ssl_ctx
;
2547 mem
= OSSL_HTTP_get(url
, proxy
, no_proxy
, NULL
/* bio */, NULL
/* rbio */,
2548 app_http_tls_cb
, &info
, 0 /* buf_size */, headers
,
2549 expected_content_type
, 1 /* expect_asn1 */,
2550 OSSL_HTTP_DEFAULT_MAX_RESP_LEN
, timeout
);
2551 resp
= ASN1_item_d2i_bio(it
, mem
, NULL
);
2555 OPENSSL_free(server
);
2561 ASN1_VALUE
*app_http_post_asn1(const char *host
, const char *port
,
2562 const char *path
, const char *proxy
,
2563 const char *no_proxy
, SSL_CTX
*ssl_ctx
,
2564 const STACK_OF(CONF_VALUE
) *headers
,
2565 const char *content_type
,
2566 ASN1_VALUE
*req
, const ASN1_ITEM
*req_it
,
2567 const char *expected_content_type
,
2568 long timeout
, const ASN1_ITEM
*rsp_it
)
2570 int use_ssl
= ssl_ctx
!= NULL
;
2571 APP_HTTP_TLS_INFO info
;
2572 BIO
*rsp
, *req_mem
= ASN1_item_i2d_mem_bio(req_it
, req
);
2575 if (req_mem
== NULL
)
2580 info
.use_proxy
= /* workaround for callback design flaw, see #17088 */
2581 OSSL_HTTP_adapt_proxy(proxy
, no_proxy
, host
, use_ssl
) != NULL
;
2582 info
.timeout
= timeout
;
2583 info
.ssl_ctx
= ssl_ctx
;
2584 rsp
= OSSL_HTTP_transfer(NULL
, host
, port
, path
, use_ssl
,
2585 proxy
, no_proxy
, NULL
/* bio */, NULL
/* rbio */,
2586 app_http_tls_cb
, &info
,
2587 0 /* buf_size */, headers
, content_type
, req_mem
,
2588 expected_content_type
, 1 /* expect_asn1 */,
2589 OSSL_HTTP_DEFAULT_MAX_RESP_LEN
, timeout
,
2590 0 /* keep_alive */);
2592 res
= ASN1_item_d2i_bio(rsp_it
, rsp
, NULL
);
2600 * Platform-specific sections
2605 # define fileno(a) (int)_fileno(a)
2608 # include <windows.h>
2611 static int WIN32_rename(const char *from
, const char *to
)
2613 TCHAR
*tfrom
= NULL
, *tto
;
2617 if (sizeof(TCHAR
) == 1) {
2618 tfrom
= (TCHAR
*)from
;
2620 } else { /* UNICODE path */
2621 size_t i
, flen
= strlen(from
) + 1, tlen
= strlen(to
) + 1;
2623 tfrom
= malloc(sizeof(*tfrom
) * (flen
+ tlen
));
2627 # if !defined(_WIN32_WCE) || _WIN32_WCE >= 101
2628 if (!MultiByteToWideChar(CP_ACP
, 0, from
, flen
, (WCHAR
*)tfrom
, flen
))
2630 for (i
= 0; i
< flen
; i
++)
2631 tfrom
[i
] = (TCHAR
)from
[i
];
2632 # if !defined(_WIN32_WCE) || _WIN32_WCE >= 101
2633 if (!MultiByteToWideChar(CP_ACP
, 0, to
, tlen
, (WCHAR
*)tto
, tlen
))
2635 for (i
= 0; i
< tlen
; i
++)
2636 tto
[i
] = (TCHAR
)to
[i
];
2639 if (MoveFile(tfrom
, tto
))
2641 err
= GetLastError();
2642 if (err
== ERROR_ALREADY_EXISTS
|| err
== ERROR_FILE_EXISTS
) {
2643 if (DeleteFile(tto
) && MoveFile(tfrom
, tto
))
2645 err
= GetLastError();
2647 if (err
== ERROR_FILE_NOT_FOUND
|| err
== ERROR_PATH_NOT_FOUND
)
2649 else if (err
== ERROR_ACCESS_DENIED
)
2652 errno
= EINVAL
; /* we could map more codes... */
2656 if (tfrom
!= NULL
&& tfrom
!= (TCHAR
*)from
)
2662 /* app_tminterval section */
2664 double app_tminterval(int stop
, int usertime
)
2668 static ULARGE_INTEGER tmstart
;
2669 static int warning
= 1;
2670 int use_GetSystemTime
= 1;
2671 # ifdef _WIN32_WINNT
2672 static HANDLE proc
= NULL
;
2676 proc
= OpenProcess(PROCESS_QUERY_INFORMATION
, FALSE
,
2677 GetCurrentProcessId());
2679 proc
= (HANDLE
) - 1;
2682 if (usertime
&& proc
!= (HANDLE
) - 1) {
2685 GetProcessTimes(proc
, &junk
, &junk
, &junk
, &now
);
2686 use_GetSystemTime
= 0;
2689 if (use_GetSystemTime
) {
2692 if (usertime
&& warning
) {
2693 BIO_printf(bio_err
, "To get meaningful results, run "
2694 "this program on idle system.\n");
2697 GetSystemTime(&systime
);
2698 SystemTimeToFileTime(&systime
, &now
);
2701 if (stop
== TM_START
) {
2702 tmstart
.u
.LowPart
= now
.dwLowDateTime
;
2703 tmstart
.u
.HighPart
= now
.dwHighDateTime
;
2705 ULARGE_INTEGER tmstop
;
2707 tmstop
.u
.LowPart
= now
.dwLowDateTime
;
2708 tmstop
.u
.HighPart
= now
.dwHighDateTime
;
2710 ret
= (__int64
)(tmstop
.QuadPart
- tmstart
.QuadPart
) * 1e-7;
2715 #elif defined(OPENSSL_SYS_VXWORKS)
2718 double app_tminterval(int stop
, int usertime
)
2721 # ifdef CLOCK_REALTIME
2722 static struct timespec tmstart
;
2723 struct timespec now
;
2725 static unsigned long tmstart
;
2728 static int warning
= 1;
2730 if (usertime
&& warning
) {
2731 BIO_printf(bio_err
, "To get meaningful results, run "
2732 "this program on idle system.\n");
2735 # ifdef CLOCK_REALTIME
2736 clock_gettime(CLOCK_REALTIME
, &now
);
2737 if (stop
== TM_START
)
2740 ret
= ((now
.tv_sec
+ now
.tv_nsec
* 1e-9)
2741 - (tmstart
.tv_sec
+ tmstart
.tv_nsec
* 1e-9));
2744 if (stop
== TM_START
)
2747 ret
= (now
- tmstart
) / (double)sysClkRateGet();
2752 #elif defined(_SC_CLK_TCK) /* by means of unistd.h */
2753 # include <sys/times.h>
2755 double app_tminterval(int stop
, int usertime
)
2759 clock_t now
= times(&rus
);
2760 static clock_t tmstart
;
2763 now
= rus
.tms_utime
;
2765 if (stop
== TM_START
) {
2768 long int tck
= sysconf(_SC_CLK_TCK
);
2770 ret
= (now
- tmstart
) / (double)tck
;
2777 # include <sys/time.h>
2778 # include <sys/resource.h>
2780 double app_tminterval(int stop
, int usertime
)
2785 static struct timeval tmstart
;
2788 getrusage(RUSAGE_SELF
, &rus
), now
= rus
.ru_utime
;
2790 gettimeofday(&now
, NULL
);
2792 if (stop
== TM_START
)
2795 ret
= ((now
.tv_sec
+ now
.tv_usec
* 1e-6)
2796 - (tmstart
.tv_sec
+ tmstart
.tv_usec
* 1e-6));
2802 int app_access(const char *name
, int flag
)
2805 return _access(name
, flag
);
2807 return access(name
, flag
);
2811 int app_isdir(const char *name
)
2813 return opt_isdir(name
);
2816 /* raw_read|write section */
2818 # include "vms_term_sock.h"
2819 static int stdin_sock
= -1;
2821 static void close_stdin_sock(void)
2823 TerminalSocket(TERM_SOCK_DELETE
, &stdin_sock
);
2826 int fileno_stdin(void)
2828 if (stdin_sock
== -1) {
2829 TerminalSocket(TERM_SOCK_CREATE
, &stdin_sock
);
2830 atexit(close_stdin_sock
);
2836 int fileno_stdin(void)
2838 return fileno(stdin
);
2842 int fileno_stdout(void)
2844 return fileno(stdout
);
2847 #if defined(_WIN32) && defined(STD_INPUT_HANDLE)
2848 int raw_read_stdin(void *buf
, int siz
)
2852 if (ReadFile(GetStdHandle(STD_INPUT_HANDLE
), buf
, siz
, &n
, NULL
))
2857 #elif defined(__VMS)
2858 # include <sys/socket.h>
2860 int raw_read_stdin(void *buf
, int siz
)
2862 return recv(fileno_stdin(), buf
, siz
, 0);
2865 # if defined(__TANDEM)
2866 # if defined(OPENSSL_TANDEM_FLOSS)
2867 # include <floss.h(floss_read)>
2870 int raw_read_stdin(void *buf
, int siz
)
2872 return read(fileno_stdin(), buf
, siz
);
2876 #if defined(_WIN32) && defined(STD_OUTPUT_HANDLE)
2877 int raw_write_stdout(const void *buf
, int siz
)
2881 if (WriteFile(GetStdHandle(STD_OUTPUT_HANDLE
), buf
, siz
, &n
, NULL
))
2886 #elif defined(OPENSSL_SYS_TANDEM) && defined(OPENSSL_THREADS) \
2887 && defined(_SPT_MODEL_)
2888 # if defined(__TANDEM)
2889 # if defined(OPENSSL_TANDEM_FLOSS)
2890 # include <floss.h(floss_write)>
2893 int raw_write_stdout(const void *buf
, int siz
)
2895 return write(fileno(stdout
), (void *)buf
, siz
);
2898 # if defined(__TANDEM)
2899 # if defined(OPENSSL_TANDEM_FLOSS)
2900 # include <floss.h(floss_write)>
2903 int raw_write_stdout(const void *buf
, int siz
)
2905 return write(fileno_stdout(), buf
, siz
);
2910 * Centralized handling of input and output files with format specification
2911 * The format is meant to show what the input and output is supposed to be,
2912 * and is therefore a show of intent more than anything else. However, it
2913 * does impact behavior on some platforms, such as differentiating between
2914 * text and binary input/output on non-Unix platforms
2916 BIO
*dup_bio_in(int format
)
2918 return BIO_new_fp(stdin
,
2919 BIO_NOCLOSE
| (FMT_istext(format
) ? BIO_FP_TEXT
: 0));
2922 BIO
*dup_bio_out(int format
)
2924 BIO
*b
= BIO_new_fp(stdout
,
2925 BIO_NOCLOSE
| (FMT_istext(format
) ? BIO_FP_TEXT
: 0));
2926 void *prefix
= NULL
;
2928 #ifdef OPENSSL_SYS_VMS
2929 if (FMT_istext(format
))
2930 b
= BIO_push(BIO_new(BIO_f_linebuffer()), b
);
2933 if (FMT_istext(format
)
2934 && (prefix
= getenv("HARNESS_OSSL_PREFIX")) != NULL
) {
2935 b
= BIO_push(BIO_new(BIO_f_prefix()), b
);
2936 BIO_set_prefix(b
, prefix
);
2942 BIO
*dup_bio_err(int format
)
2944 BIO
*b
= BIO_new_fp(stderr
,
2945 BIO_NOCLOSE
| (FMT_istext(format
) ? BIO_FP_TEXT
: 0));
2947 #ifdef OPENSSL_SYS_VMS
2948 if (FMT_istext(format
))
2949 b
= BIO_push(BIO_new(BIO_f_linebuffer()), b
);
2954 void unbuffer(FILE *fp
)
2957 * On VMS, setbuf() will only take 32-bit pointers, and a compilation
2958 * with /POINTER_SIZE=64 will give off a MAYLOSEDATA2 warning here.
2959 * However, we trust that the C RTL will never give us a FILE pointer
2960 * above the first 4 GB of memory, so we simply turn off the warning
2963 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
2964 # pragma environment save
2965 # pragma message disable maylosedata2
2968 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
2969 # pragma environment restore
2973 static const char *modestr(char mode
, int format
)
2975 OPENSSL_assert(mode
== 'a' || mode
== 'r' || mode
== 'w');
2979 return FMT_istext(format
) ? "a" : "ab";
2981 return FMT_istext(format
) ? "r" : "rb";
2983 return FMT_istext(format
) ? "w" : "wb";
2985 /* The assert above should make sure we never reach this point */
2989 static const char *modeverb(char mode
)
2999 return "(doing something)";
3003 * Open a file for writing, owner-read-only.
3005 BIO
*bio_open_owner(const char *filename
, int format
, int private)
3009 int textmode
, bflags
;
3010 #ifndef OPENSSL_NO_POSIX_IO
3014 if (!private || filename
== NULL
|| strcmp(filename
, "-") == 0)
3015 return bio_open_default(filename
, 'w', format
);
3017 textmode
= FMT_istext(format
);
3018 #ifndef OPENSSL_NO_POSIX_IO
3029 # elif defined(_O_BINARY)
3034 # ifdef OPENSSL_SYS_VMS
3036 * VMS doesn't have O_BINARY, it just doesn't make sense. But,
3037 * it still needs to know that we're going binary, or fdopen()
3038 * will fail with "invalid argument"... so we tell VMS what the
3042 fd
= open(filename
, mode
, 0600, "ctx=bin");
3045 fd
= open(filename
, mode
, 0600);
3048 fp
= fdopen(fd
, modestr('w', format
));
3049 #else /* OPENSSL_NO_POSIX_IO */
3050 /* Have stdio but not Posix IO, do the best we can */
3051 fp
= fopen(filename
, modestr('w', format
));
3052 #endif /* OPENSSL_NO_POSIX_IO */
3057 bflags
|= BIO_FP_TEXT
;
3058 b
= BIO_new_fp(fp
, bflags
);
3063 BIO_printf(bio_err
, "%s: Can't open \"%s\" for writing, %s\n",
3064 opt_getprog(), filename
, strerror(errno
));
3065 ERR_print_errors(bio_err
);
3066 /* If we have fp, then fdopen took over fd, so don't close both. */
3069 #ifndef OPENSSL_NO_POSIX_IO
3076 static BIO
*bio_open_default_(const char *filename
, char mode
, int format
,
3081 if (filename
== NULL
|| strcmp(filename
, "-") == 0) {
3082 ret
= mode
== 'r' ? dup_bio_in(format
) : dup_bio_out(format
);
3090 "Can't open %s, %s\n",
3091 mode
== 'r' ? "stdin" : "stdout", strerror(errno
));
3093 ret
= BIO_new_file(filename
, modestr(mode
, format
));
3101 "Can't open \"%s\" for %s, %s\n",
3102 filename
, modeverb(mode
), strerror(errno
));
3104 ERR_print_errors(bio_err
);
3108 BIO
*bio_open_default(const char *filename
, char mode
, int format
)
3110 return bio_open_default_(filename
, mode
, format
, 0);
3113 BIO
*bio_open_default_quiet(const char *filename
, char mode
, int format
)
3115 return bio_open_default_(filename
, mode
, format
, 1);
3118 void wait_for_async(SSL
*s
)
3120 /* On Windows select only works for sockets, so we simply don't wait */
3121 #ifndef OPENSSL_SYS_WINDOWS
3128 if (!SSL_get_all_async_fds(s
, NULL
, &numfds
))
3132 fds
= app_malloc(sizeof(OSSL_ASYNC_FD
) * numfds
, "allocate async fds");
3133 if (!SSL_get_all_async_fds(s
, fds
, &numfds
)) {
3139 for (i
= 0; i
< numfds
; i
++) {
3140 if (width
<= (int)fds
[i
])
3141 width
= (int)fds
[i
] + 1;
3142 openssl_fdset((int)fds
[i
], &asyncfds
);
3144 select(width
, (void *)&asyncfds
, NULL
, NULL
, NULL
);
3149 /* if OPENSSL_SYS_WINDOWS is defined then so is OPENSSL_SYS_MSDOS */
3150 #if defined(OPENSSL_SYS_MSDOS)
3151 int has_stdin_waiting(void)
3153 # if defined(OPENSSL_SYS_WINDOWS)
3154 HANDLE inhand
= GetStdHandle(STD_INPUT_HANDLE
);
3156 INPUT_RECORD inputrec
;
3160 if (inhand
== INVALID_HANDLE_VALUE
) {
3164 peeked
= PeekConsoleInput(inhand
, &inputrec
, insize
, &events
);
3166 /* Probably redirected input? _kbhit() does not work in this case */
3177 /* Corrupt a signature by modifying final byte */
3178 void corrupt_signature(const ASN1_STRING
*signature
)
3180 unsigned char *s
= signature
->data
;
3182 s
[signature
->length
- 1] ^= 0x1;
3185 int set_cert_times(X509
*x
, const char *startdate
, const char *enddate
,
3188 if (startdate
== NULL
|| strcmp(startdate
, "today") == 0) {
3189 if (X509_gmtime_adj(X509_getm_notBefore(x
), 0) == NULL
)
3192 if (!ASN1_TIME_set_string_X509(X509_getm_notBefore(x
), startdate
))
3195 if (enddate
== NULL
) {
3196 if (X509_time_adj_ex(X509_getm_notAfter(x
), days
, 0, NULL
)
3199 } else if (!ASN1_TIME_set_string_X509(X509_getm_notAfter(x
), enddate
)) {
3205 int set_crl_lastupdate(X509_CRL
*crl
, const char *lastupdate
)
3208 ASN1_TIME
*tm
= ASN1_TIME_new();
3213 if (lastupdate
== NULL
) {
3214 if (X509_gmtime_adj(tm
, 0) == NULL
)
3217 if (!ASN1_TIME_set_string_X509(tm
, lastupdate
))
3221 if (!X509_CRL_set1_lastUpdate(crl
, tm
))
3230 int set_crl_nextupdate(X509_CRL
*crl
, const char *nextupdate
,
3231 long days
, long hours
, long secs
)
3234 ASN1_TIME
*tm
= ASN1_TIME_new();
3239 if (nextupdate
== NULL
) {
3240 if (X509_time_adj_ex(tm
, days
, hours
* 60 * 60 + secs
, NULL
) == NULL
)
3243 if (!ASN1_TIME_set_string_X509(tm
, nextupdate
))
3247 if (!X509_CRL_set1_nextUpdate(crl
, tm
))
3256 void make_uppercase(char *string
)
3260 for (i
= 0; string
[i
] != '\0'; i
++)
3261 string
[i
] = toupper((unsigned char)string
[i
]);
3264 OSSL_PARAM
*app_params_new_from_opts(STACK_OF(OPENSSL_STRING
) *opts
,
3265 const OSSL_PARAM
*paramdefs
)
3267 OSSL_PARAM
*params
= NULL
;
3268 size_t sz
= (size_t)sk_OPENSSL_STRING_num(opts
);
3270 char *opt
= "", *stmp
, *vtmp
= NULL
;
3276 params
= OPENSSL_zalloc(sizeof(OSSL_PARAM
) * (sz
+ 1));
3280 for (params_n
= 0; params_n
< sz
; params_n
++) {
3281 opt
= sk_OPENSSL_STRING_value(opts
, (int)params_n
);
3282 if ((stmp
= OPENSSL_strdup(opt
)) == NULL
3283 || (vtmp
= strchr(stmp
, ':')) == NULL
)
3285 /* Replace ':' with 0 to terminate the string pointed to by stmp */
3287 /* Skip over the separator so that vmtp points to the value */
3289 if (!OSSL_PARAM_allocate_from_text(¶ms
[params_n
], paramdefs
,
3290 stmp
, vtmp
, strlen(vtmp
), &found
))
3294 params
[params_n
] = OSSL_PARAM_construct_end();
3298 BIO_printf(bio_err
, "Parameter %s '%s'\n", found
? "error" : "unknown",
3300 ERR_print_errors(bio_err
);
3301 app_params_free(params
);
3305 void app_params_free(OSSL_PARAM
*params
)
3309 if (params
!= NULL
) {
3310 for (i
= 0; params
[i
].key
!= NULL
; ++i
)
3311 OPENSSL_free(params
[i
].data
);
3312 OPENSSL_free(params
);
3316 EVP_PKEY
*app_keygen(EVP_PKEY_CTX
*ctx
, const char *alg
, int bits
, int verbose
)
3318 EVP_PKEY
*res
= NULL
;
3320 if (verbose
&& alg
!= NULL
) {
3321 BIO_printf(bio_err
, "Generating %s key", alg
);
3323 BIO_printf(bio_err
, " with %d bits\n", bits
);
3325 BIO_printf(bio_err
, "\n");
3328 BIO_printf(bio_err
, "Warning: generating random key material may take a long time\n"
3329 "if the system has a poor entropy source\n");
3330 if (EVP_PKEY_keygen(ctx
, &res
) <= 0)
3331 app_bail_out("%s: Error generating %s key\n", opt_getprog(),
3332 alg
!= NULL
? alg
: "asymmetric");
3336 EVP_PKEY
*app_paramgen(EVP_PKEY_CTX
*ctx
, const char *alg
)
3338 EVP_PKEY
*res
= NULL
;
3341 BIO_printf(bio_err
, "Warning: generating random key parameters may take a long time\n"
3342 "if the system has a poor entropy source\n");
3343 if (EVP_PKEY_paramgen(ctx
, &res
) <= 0)
3344 app_bail_out("%s: Generating %s key parameters failed\n",
3345 opt_getprog(), alg
!= NULL
? alg
: "asymmetric");
3350 * Return non-zero if the legacy path is still an option.
3351 * This decision is based on the global command line operations and the
3352 * behaviour thus far.
3354 int opt_legacy_okay(void)
3356 int provider_options
= opt_provider_option_given();
3357 int libctx
= app_get0_libctx() != NULL
|| app_get0_propq() != NULL
;
3358 #ifndef OPENSSL_NO_ENGINE
3359 ENGINE
*e
= ENGINE_get_first();
3367 * Having a provider option specified or a custom library context or
3368 * property query, is a sure sign we're not using legacy.
3370 if (provider_options
|| libctx
)