2 * Copyright 1995-2020 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
21 #include <sys/types.h>
22 #ifndef OPENSSL_NO_POSIX_IO
23 # include <sys/stat.h>
28 #include <openssl/err.h>
29 #include <openssl/x509.h>
30 #include <openssl/x509v3.h>
31 #include <openssl/pem.h>
32 #include <openssl/store.h>
33 #include <openssl/pkcs12.h>
34 #include <openssl/ui.h>
35 #include <openssl/safestack.h>
36 #ifndef OPENSSL_NO_ENGINE
37 # include <openssl/engine.h>
39 #ifndef OPENSSL_NO_RSA
40 # include <openssl/rsa.h>
42 #include <openssl/bn.h>
43 #include <openssl/ssl.h>
47 static int WIN32_rename(const char *from
, const char *to
);
48 # define rename(from,to) WIN32_rename((from),(to))
51 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)
55 #if defined(OPENSSL_SYS_MSDOS) && !defined(_WIN32)
59 #define PASS_SOURCE_SIZE_MAX 4
62 DEFINE_STACK_OF(CONF_VALUE
)
64 DEFINE_STACK_OF(X509_CRL
)
65 DEFINE_STACK_OF(X509_INFO
)
66 DEFINE_STACK_OF(X509_EXTENSION
)
67 DEFINE_STACK_OF(X509_POLICY_NODE
)
68 DEFINE_STACK_OF(GENERAL_NAME
)
69 DEFINE_STACK_OF(DIST_POINT
)
70 DEFINE_STACK_OF_STRING()
78 static int set_table_opts(unsigned long *flags
, const char *arg
,
79 const NAME_EX_TBL
* in_tbl
);
80 static int set_multi_opts(unsigned long *flags
, const char *arg
,
81 const NAME_EX_TBL
* in_tbl
);
83 int app_init(long mesgwin
);
85 int chopup_args(ARGS
*arg
, char *buf
)
88 char c
= '\0', *p
= NULL
;
93 arg
->argv
= app_malloc(sizeof(*arg
->argv
) * arg
->size
, "argv space");
97 /* Skip whitespace. */
98 while (*p
&& isspace(_UC(*p
)))
103 /* The start of something good :-) */
104 if (arg
->argc
>= arg
->size
) {
107 tmp
= OPENSSL_realloc(arg
->argv
, sizeof(*arg
->argv
) * arg
->size
);
112 quoted
= *p
== '\'' || *p
== '"';
115 arg
->argv
[arg
->argc
++] = p
;
117 /* now look for the end of this */
119 while (*p
&& *p
!= c
)
123 while (*p
&& !isspace(_UC(*p
)))
129 arg
->argv
[arg
->argc
] = NULL
;
134 int app_init(long mesgwin
)
140 int ctx_set_verify_locations(SSL_CTX
*ctx
,
141 const char *CAfile
, int noCAfile
,
142 const char *CApath
, int noCApath
,
143 const char *CAstore
, int noCAstore
)
145 if (CAfile
== NULL
&& CApath
== NULL
&& CAstore
== NULL
) {
146 if (!noCAfile
&& SSL_CTX_set_default_verify_file(ctx
) <= 0)
148 if (!noCApath
&& SSL_CTX_set_default_verify_dir(ctx
) <= 0)
150 if (!noCAstore
&& SSL_CTX_set_default_verify_store(ctx
) <= 0)
156 if (CAfile
!= NULL
&& !SSL_CTX_load_verify_file(ctx
, CAfile
))
158 if (CApath
!= NULL
&& !SSL_CTX_load_verify_dir(ctx
, CApath
))
160 if (CAstore
!= NULL
&& !SSL_CTX_load_verify_store(ctx
, CAstore
))
165 #ifndef OPENSSL_NO_CT
167 int ctx_set_ctlog_list_file(SSL_CTX
*ctx
, const char *path
)
170 return SSL_CTX_set_default_ctlog_list_file(ctx
);
172 return SSL_CTX_set_ctlog_list_file(ctx
, path
);
177 static unsigned long nmflag
= 0;
178 static char nmflag_set
= 0;
180 int set_nameopt(const char *arg
)
182 int ret
= set_name_ex(&nmflag
, arg
);
190 unsigned long get_nameopt(void)
192 return (nmflag_set
) ? nmflag
: XN_FLAG_ONELINE
;
195 int dump_cert_text(BIO
*out
, X509
*x
)
197 print_name(out
, "subject=", X509_get_subject_name(x
), get_nameopt());
199 print_name(out
, "issuer=", X509_get_issuer_name(x
), get_nameopt());
205 int wrap_password_callback(char *buf
, int bufsiz
, int verify
, void *userdata
)
207 return password_callback(buf
, bufsiz
, verify
, (PW_CB_DATA
*)userdata
);
211 static char *app_get_pass(const char *arg
, int keepbio
);
213 char *get_passwd(const char *pass
, const char *desc
)
219 if (!app_passwd(pass
, NULL
, &result
, NULL
))
220 BIO_printf(bio_err
, "Error getting password for %s\n", desc
);
221 if (pass
!= NULL
&& result
== NULL
) {
223 "Trying plain input string (better precede with 'pass:')\n");
224 result
= OPENSSL_strdup(pass
);
226 BIO_printf(bio_err
, "Out of memory getting password for %s\n", desc
);
231 int app_passwd(const char *arg1
, const char *arg2
, char **pass1
, char **pass2
)
233 int same
= arg1
!= NULL
&& arg2
!= NULL
&& strcmp(arg1
, arg2
) == 0;
236 *pass1
= app_get_pass(arg1
, same
);
239 } else if (pass1
!= NULL
) {
243 *pass2
= app_get_pass(arg2
, same
? 2 : 0);
246 } else if (pass2
!= NULL
) {
252 static char *app_get_pass(const char *arg
, int keepbio
)
254 static BIO
*pwdbio
= NULL
;
255 char *tmp
, tpass
[APP_PASS_LEN
];
258 /* PASS_SOURCE_SIZE_MAX = max number of chars before ':' in below strings */
259 if (strncmp(arg
, "pass:", 5) == 0)
260 return OPENSSL_strdup(arg
+ 5);
261 if (strncmp(arg
, "env:", 4) == 0) {
262 tmp
= getenv(arg
+ 4);
264 BIO_printf(bio_err
, "No environment variable %s\n", arg
+ 4);
267 return OPENSSL_strdup(tmp
);
269 if (!keepbio
|| pwdbio
== NULL
) {
270 if (strncmp(arg
, "file:", 5) == 0) {
271 pwdbio
= BIO_new_file(arg
+ 5, "r");
272 if (pwdbio
== NULL
) {
273 BIO_printf(bio_err
, "Can't open file %s\n", arg
+ 5);
278 * Under _WIN32, which covers even Win64 and CE, file
279 * descriptors referenced by BIO_s_fd are not inherited
280 * by child process and therefore below is not an option.
281 * It could have been an option if bss_fd.c was operating
282 * on real Windows descriptors, such as those obtained
285 } else if (strncmp(arg
, "fd:", 3) == 0) {
289 pwdbio
= BIO_new_fd(i
, BIO_NOCLOSE
);
290 if ((i
< 0) || !pwdbio
) {
291 BIO_printf(bio_err
, "Can't access file descriptor %s\n", arg
+ 3);
295 * Can't do BIO_gets on an fd BIO so add a buffering BIO
297 btmp
= BIO_new(BIO_f_buffer());
298 pwdbio
= BIO_push(btmp
, pwdbio
);
300 } else if (strcmp(arg
, "stdin") == 0) {
301 pwdbio
= dup_bio_in(FORMAT_TEXT
);
302 if (pwdbio
== NULL
) {
303 BIO_printf(bio_err
, "Can't open BIO for stdin\n");
307 /* argument syntax error; do not reveal too much about arg */
308 tmp
= strchr(arg
, ':');
309 if (tmp
== NULL
|| tmp
- arg
> PASS_SOURCE_SIZE_MAX
)
311 "Invalid password argument, missing ':' within the first %d chars\n",
312 PASS_SOURCE_SIZE_MAX
+ 1);
315 "Invalid password argument, starting with \"%.*s\"\n",
316 (int)(tmp
- arg
+ 1), arg
);
320 i
= BIO_gets(pwdbio
, tpass
, APP_PASS_LEN
);
322 BIO_free_all(pwdbio
);
326 BIO_printf(bio_err
, "Error reading password from BIO\n");
329 tmp
= strchr(tpass
, '\n');
332 return OPENSSL_strdup(tpass
);
335 CONF
*app_load_config_bio(BIO
*in
, const char *filename
)
341 conf
= NCONF_new(NULL
);
342 i
= NCONF_load_bio(conf
, in
, &errorline
);
346 if (errorline
<= 0) {
347 BIO_printf(bio_err
, "%s: Can't load ", opt_getprog());
349 BIO_printf(bio_err
, "%s: Error on line %ld of ", opt_getprog(),
352 if (filename
!= NULL
)
353 BIO_printf(bio_err
, "config file \"%s\"\n", filename
);
355 BIO_printf(bio_err
, "config input");
361 CONF
*app_load_config(const char *filename
)
366 in
= bio_open_default(filename
, 'r', FORMAT_TEXT
);
370 conf
= app_load_config_bio(in
, filename
);
375 CONF
*app_load_config_quiet(const char *filename
)
380 in
= bio_open_default_quiet(filename
, 'r', FORMAT_TEXT
);
384 conf
= app_load_config_bio(in
, filename
);
389 int app_load_modules(const CONF
*config
)
391 CONF
*to_free
= NULL
;
394 config
= to_free
= app_load_config_quiet(default_config_file
);
398 if (CONF_modules_load(config
, NULL
, 0) <= 0) {
399 BIO_printf(bio_err
, "Error configuring OpenSSL modules\n");
400 ERR_print_errors(bio_err
);
408 int add_oid_section(CONF
*conf
)
411 STACK_OF(CONF_VALUE
) *sktmp
;
415 if ((p
= NCONF_get_string(conf
, NULL
, "oid_section")) == NULL
) {
419 if ((sktmp
= NCONF_get_section(conf
, p
)) == NULL
) {
420 BIO_printf(bio_err
, "problem loading oid section %s\n", p
);
423 for (i
= 0; i
< sk_CONF_VALUE_num(sktmp
); i
++) {
424 cnf
= sk_CONF_VALUE_value(sktmp
, i
);
425 if (OBJ_create(cnf
->value
, cnf
->name
, cnf
->name
) == NID_undef
) {
426 BIO_printf(bio_err
, "problem creating object %s=%s\n",
427 cnf
->name
, cnf
->value
);
434 X509
*load_cert_pass(const char *uri
, int maybe_stdin
,
435 const char *pass
, const char *desc
)
440 desc
= "certificate";
445 (void)load_key_cert_crl(uri
, maybe_stdin
, pass
, desc
, NULL
, &cert
, NULL
);
447 BIO_printf(bio_err
, "Unable to load %s\n", desc
);
448 ERR_print_errors(bio_err
);
453 /* the format parameter is meanwhile not needed anymore and thus ignored */
454 X509
*load_cert(const char *uri
, int format
, const char *desc
)
456 return load_cert_pass(uri
, 0, NULL
, desc
);
459 /* the format parameter is meanwhile not needed anymore and thus ignored */
460 X509_CRL
*load_crl(const char *uri
, int format
, const char *desc
)
462 X509_CRL
*crl
= NULL
;
466 (void)load_key_cert_crl(uri
, 0, NULL
, desc
, NULL
, NULL
, &crl
);
468 BIO_printf(bio_err
, "Unable to load %s\n", desc
);
469 ERR_print_errors(bio_err
);
474 X509_REQ
*load_csr(const char *file
, int format
, const char *desc
)
476 X509_REQ
*req
= NULL
;
481 in
= bio_open_default(file
, 'r', format
);
485 if (format
== FORMAT_ASN1
)
486 req
= d2i_X509_REQ_bio(in
, NULL
);
487 else if (format
== FORMAT_PEM
)
488 req
= PEM_read_bio_X509_REQ(in
, NULL
, NULL
, NULL
);
490 print_format_error(format
, OPT_FMT_PEMDER
);
494 BIO_printf(bio_err
, "Unable to load %s\n", desc
);
495 ERR_print_errors(bio_err
);
501 void cleanse(char *str
)
504 OPENSSL_cleanse(str
, strlen(str
));
507 void clear_free(char *str
)
510 OPENSSL_clear_free(str
, strlen(str
));
513 EVP_PKEY
*load_key(const char *uri
, int format
, int may_stdin
,
514 const char *pass
, ENGINE
*e
, const char *desc
)
516 EVP_PKEY
*pkey
= NULL
;
519 desc
= "private key";
521 if (format
== FORMAT_ENGINE
) {
523 BIO_printf(bio_err
, "No engine specified for loading %s\n", desc
);
525 #ifndef OPENSSL_NO_ENGINE
528 cb_data
.password
= pass
;
529 cb_data
.prompt_info
= uri
;
530 if (ENGINE_init(e
)) {
531 pkey
= ENGINE_load_private_key(e
, uri
,
532 (UI_METHOD
*)get_ui_method(),
537 BIO_printf(bio_err
, "Cannot load %s from engine\n", desc
);
538 ERR_print_errors(bio_err
);
541 BIO_printf(bio_err
, "Engines not supported for loading %s\n", desc
);
545 (void)load_key_cert_crl(uri
, may_stdin
, pass
, desc
, &pkey
, NULL
, NULL
);
549 BIO_printf(bio_err
, "Unable to load %s\n", desc
);
550 ERR_print_errors(bio_err
);
555 EVP_PKEY
*load_pubkey(const char *uri
, int format
, int maybe_stdin
,
556 const char *pass
, ENGINE
*e
, const char *desc
)
558 EVP_PKEY
*pkey
= NULL
;
563 if (format
== FORMAT_ENGINE
) {
565 BIO_printf(bio_err
, "No engine specified for loading %s\n", desc
);
567 #ifndef OPENSSL_NO_ENGINE
570 cb_data
.password
= pass
;
571 cb_data
.prompt_info
= uri
;
572 pkey
= ENGINE_load_public_key(e
, uri
, (UI_METHOD
*)get_ui_method(),
575 BIO_printf(bio_err
, "Cannot load %s from engine\n", desc
);
576 ERR_print_errors(bio_err
);
579 BIO_printf(bio_err
, "Engines not supported for loading %s\n", desc
);
583 (void)load_key_cert_crl(uri
, maybe_stdin
, pass
, desc
, &pkey
,
587 BIO_printf(bio_err
, "Unable to load %s\n", desc
);
588 ERR_print_errors(bio_err
);
593 static int load_certs_crls(const char *file
, int format
,
594 const char *pass
, const char *desc
,
595 STACK_OF(X509
) **pcerts
,
596 STACK_OF(X509_CRL
) **pcrls
)
600 STACK_OF(X509_INFO
) *xis
= NULL
;
605 cb_data
.password
= pass
;
606 cb_data
.prompt_info
= file
;
608 if (format
!= FORMAT_PEM
) {
609 BIO_printf(bio_err
, "Bad input format specified for %s\n", desc
);
613 bio
= bio_open_default(file
, 'r', FORMAT_PEM
);
617 xis
= PEM_X509_INFO_read_bio(bio
, NULL
,
618 (pem_password_cb
*)password_callback
,
623 if (pcerts
!= NULL
&& *pcerts
== NULL
) {
624 *pcerts
= sk_X509_new_null();
629 if (pcrls
!= NULL
&& *pcrls
== NULL
) {
630 *pcrls
= sk_X509_CRL_new_null();
635 for (i
= 0; i
< sk_X509_INFO_num(xis
); i
++) {
636 xi
= sk_X509_INFO_value(xis
, i
);
637 if (xi
->x509
!= NULL
&& pcerts
!= NULL
) {
638 if (!sk_X509_push(*pcerts
, xi
->x509
))
642 if (xi
->crl
!= NULL
&& pcrls
!= NULL
) {
643 if (!sk_X509_CRL_push(*pcrls
, xi
->crl
))
649 if (pcerts
!= NULL
&& sk_X509_num(*pcerts
) > 0)
652 if (pcrls
!= NULL
&& sk_X509_CRL_num(*pcrls
) > 0)
657 sk_X509_INFO_pop_free(xis
, X509_INFO_free
);
660 if (pcerts
!= NULL
) {
661 sk_X509_pop_free(*pcerts
, X509_free
);
665 sk_X509_CRL_pop_free(*pcrls
, X509_CRL_free
);
668 BIO_printf(bio_err
, "Unable to load %s\n", desc
!= NULL
? desc
:
669 pcerts
!= NULL
? "certificates" : "CRLs");
674 void* app_malloc(int sz
, const char *what
)
676 void *vp
= OPENSSL_malloc(sz
);
679 BIO_printf(bio_err
, "%s: Could not allocate %d bytes for %s\n",
680 opt_getprog(), sz
, what
);
681 ERR_print_errors(bio_err
);
688 * Initialize or extend, if *certs != NULL, a certificate stack.
690 int load_certs(const char *file
, STACK_OF(X509
) **certs
, int format
,
691 const char *pass
, const char *desc
)
693 return load_certs_crls(file
, format
, pass
, desc
, certs
, NULL
);
697 * Initialize or extend, if *crls != NULL, a certificate stack.
699 int load_crls(const char *file
, STACK_OF(X509_CRL
) **crls
, int format
,
700 const char *pass
, const char *desc
)
702 return load_certs_crls(file
, format
, pass
, desc
, NULL
, crls
);
706 * Load those types of credentials for which the result pointer is not NULL.
707 * Reads from stdio if uri is NULL and maybe_stdin is nonzero.
708 * For each type the first credential found in the store is loaded.
709 * May yield partial result even if rv == 0.
711 int load_key_cert_crl(const char *uri
, int maybe_stdin
,
712 const char *pass
, const char *desc
,
713 EVP_PKEY
**ppkey
, X509
**pcert
, X509_CRL
**pcrl
)
716 OSSL_STORE_CTX
*ctx
= NULL
;
718 /* TODO make use of the engine reference 'eng' when loading pkeys */
728 desc
= "key/certificate/CRL";
729 uidata
.password
= pass
;
730 uidata
.prompt_info
= uri
;
736 BIO_printf(bio_err
, "No filename or uri specified for loading %s\n",
741 bio
= BIO_new_fp(stdin
, 0);
743 ctx
= OSSL_STORE_attach(bio
, NULL
, "file", NULL
,
744 get_ui_method(), &uidata
, NULL
, NULL
);
747 ctx
= OSSL_STORE_open(uri
, get_ui_method(), &uidata
, NULL
, NULL
);
750 BIO_printf(bio_err
, "Could not open file or uri %s for loading %s\n",
756 OSSL_STORE_INFO
*info
= OSSL_STORE_load(ctx
);
757 int type
= info
== NULL
? 0 : OSSL_STORE_INFO_get_type(info
);
758 const char *infostr
=
759 info
== NULL
? NULL
: OSSL_STORE_INFO_type_string(type
);
763 if (OSSL_STORE_eof(ctx
))
769 case OSSL_STORE_INFO_PKEY
:
770 if (ppkey
!= NULL
&& *ppkey
== NULL
)
771 err
= ((*ppkey
= OSSL_STORE_INFO_get1_PKEY(info
)) == NULL
);
773 case OSSL_STORE_INFO_CERT
:
774 if (pcert
!= NULL
&& *pcert
== NULL
)
775 err
= ((*pcert
= OSSL_STORE_INFO_get1_CERT(info
)) == NULL
);
777 case OSSL_STORE_INFO_CRL
:
778 if (pcrl
!= NULL
&& *pcrl
== NULL
)
779 err
= ((*pcrl
= OSSL_STORE_INFO_get1_CRL(info
)) == NULL
);
782 /* skip any other type */
785 OSSL_STORE_INFO_free(info
);
787 BIO_printf(bio_err
, "Could not read %s of %s from %s\n",
794 OSSL_STORE_close(ctx
);
796 ERR_print_errors(bio_err
);
801 #define X509V3_EXT_UNKNOWN_MASK (0xfL << 16)
802 /* Return error for unknown extensions */
803 #define X509V3_EXT_DEFAULT 0
804 /* Print error for unknown extensions */
805 #define X509V3_EXT_ERROR_UNKNOWN (1L << 16)
806 /* ASN1 parse unknown extensions */
807 #define X509V3_EXT_PARSE_UNKNOWN (2L << 16)
808 /* BIO_dump unknown extensions */
809 #define X509V3_EXT_DUMP_UNKNOWN (3L << 16)
811 #define X509_FLAG_CA (X509_FLAG_NO_ISSUER | X509_FLAG_NO_PUBKEY | \
812 X509_FLAG_NO_HEADER | X509_FLAG_NO_VERSION)
814 int set_cert_ex(unsigned long *flags
, const char *arg
)
816 static const NAME_EX_TBL cert_tbl
[] = {
817 {"compatible", X509_FLAG_COMPAT
, 0xffffffffl
},
818 {"ca_default", X509_FLAG_CA
, 0xffffffffl
},
819 {"no_header", X509_FLAG_NO_HEADER
, 0},
820 {"no_version", X509_FLAG_NO_VERSION
, 0},
821 {"no_serial", X509_FLAG_NO_SERIAL
, 0},
822 {"no_signame", X509_FLAG_NO_SIGNAME
, 0},
823 {"no_validity", X509_FLAG_NO_VALIDITY
, 0},
824 {"no_subject", X509_FLAG_NO_SUBJECT
, 0},
825 {"no_issuer", X509_FLAG_NO_ISSUER
, 0},
826 {"no_pubkey", X509_FLAG_NO_PUBKEY
, 0},
827 {"no_extensions", X509_FLAG_NO_EXTENSIONS
, 0},
828 {"no_sigdump", X509_FLAG_NO_SIGDUMP
, 0},
829 {"no_aux", X509_FLAG_NO_AUX
, 0},
830 {"no_attributes", X509_FLAG_NO_ATTRIBUTES
, 0},
831 {"ext_default", X509V3_EXT_DEFAULT
, X509V3_EXT_UNKNOWN_MASK
},
832 {"ext_error", X509V3_EXT_ERROR_UNKNOWN
, X509V3_EXT_UNKNOWN_MASK
},
833 {"ext_parse", X509V3_EXT_PARSE_UNKNOWN
, X509V3_EXT_UNKNOWN_MASK
},
834 {"ext_dump", X509V3_EXT_DUMP_UNKNOWN
, X509V3_EXT_UNKNOWN_MASK
},
837 return set_multi_opts(flags
, arg
, cert_tbl
);
840 int set_name_ex(unsigned long *flags
, const char *arg
)
842 static const NAME_EX_TBL ex_tbl
[] = {
843 {"esc_2253", ASN1_STRFLGS_ESC_2253
, 0},
844 {"esc_2254", ASN1_STRFLGS_ESC_2254
, 0},
845 {"esc_ctrl", ASN1_STRFLGS_ESC_CTRL
, 0},
846 {"esc_msb", ASN1_STRFLGS_ESC_MSB
, 0},
847 {"use_quote", ASN1_STRFLGS_ESC_QUOTE
, 0},
848 {"utf8", ASN1_STRFLGS_UTF8_CONVERT
, 0},
849 {"ignore_type", ASN1_STRFLGS_IGNORE_TYPE
, 0},
850 {"show_type", ASN1_STRFLGS_SHOW_TYPE
, 0},
851 {"dump_all", ASN1_STRFLGS_DUMP_ALL
, 0},
852 {"dump_nostr", ASN1_STRFLGS_DUMP_UNKNOWN
, 0},
853 {"dump_der", ASN1_STRFLGS_DUMP_DER
, 0},
854 {"compat", XN_FLAG_COMPAT
, 0xffffffffL
},
855 {"sep_comma_plus", XN_FLAG_SEP_COMMA_PLUS
, XN_FLAG_SEP_MASK
},
856 {"sep_comma_plus_space", XN_FLAG_SEP_CPLUS_SPC
, XN_FLAG_SEP_MASK
},
857 {"sep_semi_plus_space", XN_FLAG_SEP_SPLUS_SPC
, XN_FLAG_SEP_MASK
},
858 {"sep_multiline", XN_FLAG_SEP_MULTILINE
, XN_FLAG_SEP_MASK
},
859 {"dn_rev", XN_FLAG_DN_REV
, 0},
860 {"nofname", XN_FLAG_FN_NONE
, XN_FLAG_FN_MASK
},
861 {"sname", XN_FLAG_FN_SN
, XN_FLAG_FN_MASK
},
862 {"lname", XN_FLAG_FN_LN
, XN_FLAG_FN_MASK
},
863 {"align", XN_FLAG_FN_ALIGN
, 0},
864 {"oid", XN_FLAG_FN_OID
, XN_FLAG_FN_MASK
},
865 {"space_eq", XN_FLAG_SPC_EQ
, 0},
866 {"dump_unknown", XN_FLAG_DUMP_UNKNOWN_FIELDS
, 0},
867 {"RFC2253", XN_FLAG_RFC2253
, 0xffffffffL
},
868 {"oneline", XN_FLAG_ONELINE
, 0xffffffffL
},
869 {"multiline", XN_FLAG_MULTILINE
, 0xffffffffL
},
870 {"ca_default", XN_FLAG_MULTILINE
, 0xffffffffL
},
873 if (set_multi_opts(flags
, arg
, ex_tbl
) == 0)
875 if (*flags
!= XN_FLAG_COMPAT
876 && (*flags
& XN_FLAG_SEP_MASK
) == 0)
877 *flags
|= XN_FLAG_SEP_CPLUS_SPC
;
881 int set_ext_copy(int *copy_type
, const char *arg
)
883 if (strcasecmp(arg
, "none") == 0)
884 *copy_type
= EXT_COPY_NONE
;
885 else if (strcasecmp(arg
, "copy") == 0)
886 *copy_type
= EXT_COPY_ADD
;
887 else if (strcasecmp(arg
, "copyall") == 0)
888 *copy_type
= EXT_COPY_ALL
;
894 int copy_extensions(X509
*x
, X509_REQ
*req
, int copy_type
)
896 STACK_OF(X509_EXTENSION
) *exts
= NULL
;
897 X509_EXTENSION
*ext
, *tmpext
;
900 if (!x
|| !req
|| (copy_type
== EXT_COPY_NONE
))
902 exts
= X509_REQ_get_extensions(req
);
904 for (i
= 0; i
< sk_X509_EXTENSION_num(exts
); i
++) {
905 ext
= sk_X509_EXTENSION_value(exts
, i
);
906 obj
= X509_EXTENSION_get_object(ext
);
907 idx
= X509_get_ext_by_OBJ(x
, obj
, -1);
908 /* Does extension exist? */
910 /* If normal copy don't override existing extension */
911 if (copy_type
== EXT_COPY_ADD
)
913 /* Delete all extensions of same type */
915 tmpext
= X509_get_ext(x
, idx
);
916 X509_delete_ext(x
, idx
);
917 X509_EXTENSION_free(tmpext
);
918 idx
= X509_get_ext_by_OBJ(x
, obj
, -1);
921 if (!X509_add_ext(x
, ext
, -1))
929 sk_X509_EXTENSION_pop_free(exts
, X509_EXTENSION_free
);
934 static int set_multi_opts(unsigned long *flags
, const char *arg
,
935 const NAME_EX_TBL
* in_tbl
)
937 STACK_OF(CONF_VALUE
) *vals
;
942 vals
= X509V3_parse_list(arg
);
943 for (i
= 0; i
< sk_CONF_VALUE_num(vals
); i
++) {
944 val
= sk_CONF_VALUE_value(vals
, i
);
945 if (!set_table_opts(flags
, val
->name
, in_tbl
))
948 sk_CONF_VALUE_pop_free(vals
, X509V3_conf_free
);
952 static int set_table_opts(unsigned long *flags
, const char *arg
,
953 const NAME_EX_TBL
* in_tbl
)
956 const NAME_EX_TBL
*ptbl
;
962 } else if (c
== '+') {
969 for (ptbl
= in_tbl
; ptbl
->name
; ptbl
++) {
970 if (strcasecmp(arg
, ptbl
->name
) == 0) {
971 *flags
&= ~ptbl
->mask
;
973 *flags
|= ptbl
->flag
;
975 *flags
&= ~ptbl
->flag
;
982 void print_name(BIO
*out
, const char *title
, const X509_NAME
*nm
,
983 unsigned long lflags
)
990 BIO_puts(out
, title
);
991 if ((lflags
& XN_FLAG_SEP_MASK
) == XN_FLAG_SEP_MULTILINE
) {
995 if (lflags
== XN_FLAG_COMPAT
) {
996 buf
= X509_NAME_oneline(nm
, 0, 0);
1002 BIO_puts(out
, "\n");
1003 X509_NAME_print_ex(out
, nm
, indent
, lflags
);
1004 BIO_puts(out
, "\n");
1008 void print_bignum_var(BIO
*out
, const BIGNUM
*in
, const char *var
,
1009 int len
, unsigned char *buffer
)
1011 BIO_printf(out
, " static unsigned char %s_%d[] = {", var
, len
);
1012 if (BN_is_zero(in
)) {
1013 BIO_printf(out
, "\n 0x00");
1017 l
= BN_bn2bin(in
, buffer
);
1018 for (i
= 0; i
< l
; i
++) {
1019 BIO_printf(out
, (i
% 10) == 0 ? "\n " : " ");
1021 BIO_printf(out
, "0x%02X,", buffer
[i
]);
1023 BIO_printf(out
, "0x%02X", buffer
[i
]);
1026 BIO_printf(out
, "\n };\n");
1029 void print_array(BIO
*out
, const char* title
, int len
, const unsigned char* d
)
1033 BIO_printf(out
, "unsigned char %s[%d] = {", title
, len
);
1034 for (i
= 0; i
< len
; i
++) {
1036 BIO_printf(out
, "\n ");
1038 BIO_printf(out
, "0x%02X, ", d
[i
]);
1040 BIO_printf(out
, "0x%02X", d
[i
]);
1042 BIO_printf(out
, "\n};\n");
1045 X509_STORE
*setup_verify(const char *CAfile
, int noCAfile
,
1046 const char *CApath
, int noCApath
,
1047 const char *CAstore
, int noCAstore
)
1049 X509_STORE
*store
= X509_STORE_new();
1050 X509_LOOKUP
*lookup
;
1055 if (CAfile
!= NULL
|| !noCAfile
) {
1056 lookup
= X509_STORE_add_lookup(store
, X509_LOOKUP_file());
1059 if (CAfile
!= NULL
) {
1060 if (!X509_LOOKUP_load_file(lookup
, CAfile
, X509_FILETYPE_PEM
)) {
1061 BIO_printf(bio_err
, "Error loading file %s\n", CAfile
);
1065 X509_LOOKUP_load_file(lookup
, NULL
, X509_FILETYPE_DEFAULT
);
1069 if (CApath
!= NULL
|| !noCApath
) {
1070 lookup
= X509_STORE_add_lookup(store
, X509_LOOKUP_hash_dir());
1073 if (CApath
!= NULL
) {
1074 if (!X509_LOOKUP_add_dir(lookup
, CApath
, X509_FILETYPE_PEM
)) {
1075 BIO_printf(bio_err
, "Error loading directory %s\n", CApath
);
1079 X509_LOOKUP_add_dir(lookup
, NULL
, X509_FILETYPE_DEFAULT
);
1083 if (CAstore
!= NULL
|| !noCAstore
) {
1084 lookup
= X509_STORE_add_lookup(store
, X509_LOOKUP_store());
1087 if (!X509_LOOKUP_add_store(lookup
, CAstore
)) {
1088 if (CAstore
!= NULL
)
1089 BIO_printf(bio_err
, "Error loading store URI %s\n", CAstore
);
1097 ERR_print_errors(bio_err
);
1098 X509_STORE_free(store
);
1102 #ifndef OPENSSL_NO_ENGINE
1103 /* Try to load an engine in a shareable library */
1104 static ENGINE
*try_load_engine(const char *engine
)
1106 ENGINE
*e
= ENGINE_by_id("dynamic");
1108 if (!ENGINE_ctrl_cmd_string(e
, "SO_PATH", engine
, 0)
1109 || !ENGINE_ctrl_cmd_string(e
, "LOAD", NULL
, 0)) {
1118 ENGINE
*setup_engine(const char *engine
, int debug
)
1122 #ifndef OPENSSL_NO_ENGINE
1123 if (engine
!= NULL
) {
1124 if (strcmp(engine
, "auto") == 0) {
1125 BIO_printf(bio_err
, "Enabling auto ENGINE support\n");
1126 ENGINE_register_all_complete();
1129 if ((e
= ENGINE_by_id(engine
)) == NULL
1130 && (e
= try_load_engine(engine
)) == NULL
) {
1131 BIO_printf(bio_err
, "Invalid engine \"%s\"\n", engine
);
1132 ERR_print_errors(bio_err
);
1136 ENGINE_ctrl(e
, ENGINE_CTRL_SET_LOGSTREAM
, 0, bio_err
, 0);
1138 ENGINE_ctrl_cmd(e
, "SET_USER_INTERFACE", 0, (void *)get_ui_method(),
1140 if (!ENGINE_set_default(e
, ENGINE_METHOD_ALL
)) {
1141 BIO_printf(bio_err
, "Cannot use engine \"%s\"\n", ENGINE_get_id(e
));
1142 ERR_print_errors(bio_err
);
1147 BIO_printf(bio_err
, "Engine \"%s\" set.\n", ENGINE_get_id(e
));
1153 void release_engine(ENGINE
*e
)
1155 #ifndef OPENSSL_NO_ENGINE
1157 /* Free our "structural" reference. */
1162 static unsigned long index_serial_hash(const OPENSSL_CSTRING
*a
)
1169 return OPENSSL_LH_strhash(n
);
1172 static int index_serial_cmp(const OPENSSL_CSTRING
*a
,
1173 const OPENSSL_CSTRING
*b
)
1175 const char *aa
, *bb
;
1177 for (aa
= a
[DB_serial
]; *aa
== '0'; aa
++) ;
1178 for (bb
= b
[DB_serial
]; *bb
== '0'; bb
++) ;
1179 return strcmp(aa
, bb
);
1182 static int index_name_qual(char **a
)
1184 return (a
[0][0] == 'V');
1187 static unsigned long index_name_hash(const OPENSSL_CSTRING
*a
)
1189 return OPENSSL_LH_strhash(a
[DB_name
]);
1192 int index_name_cmp(const OPENSSL_CSTRING
*a
, const OPENSSL_CSTRING
*b
)
1194 return strcmp(a
[DB_name
], b
[DB_name
]);
1197 static IMPLEMENT_LHASH_HASH_FN(index_serial
, OPENSSL_CSTRING
)
1198 static IMPLEMENT_LHASH_COMP_FN(index_serial
, OPENSSL_CSTRING
)
1199 static IMPLEMENT_LHASH_HASH_FN(index_name
, OPENSSL_CSTRING
)
1200 static IMPLEMENT_LHASH_COMP_FN(index_name
, OPENSSL_CSTRING
)
1203 BIGNUM
*load_serial(const char *serialfile
, int create
, ASN1_INTEGER
**retai
)
1208 ASN1_INTEGER
*ai
= NULL
;
1210 ai
= ASN1_INTEGER_new();
1214 in
= BIO_new_file(serialfile
, "r");
1222 if (ret
== NULL
|| !rand_serial(ret
, ai
))
1223 BIO_printf(bio_err
, "Out of memory\n");
1225 if (!a2i_ASN1_INTEGER(in
, ai
, buf
, 1024)) {
1226 BIO_printf(bio_err
, "Unable to load number from %s\n",
1230 ret
= ASN1_INTEGER_to_BN(ai
, NULL
);
1232 BIO_printf(bio_err
, "Error converting number from bin to BIGNUM\n");
1242 ERR_print_errors(bio_err
);
1244 ASN1_INTEGER_free(ai
);
1248 int save_serial(const char *serialfile
, const char *suffix
, const BIGNUM
*serial
,
1249 ASN1_INTEGER
**retai
)
1254 ASN1_INTEGER
*ai
= NULL
;
1258 j
= strlen(serialfile
);
1260 j
= strlen(serialfile
) + strlen(suffix
) + 1;
1262 BIO_printf(bio_err
, "File name too long\n");
1267 OPENSSL_strlcpy(buf
[0], serialfile
, BSIZE
);
1269 #ifndef OPENSSL_SYS_VMS
1270 j
= BIO_snprintf(buf
[0], sizeof(buf
[0]), "%s.%s", serialfile
, suffix
);
1272 j
= BIO_snprintf(buf
[0], sizeof(buf
[0]), "%s-%s", serialfile
, suffix
);
1275 out
= BIO_new_file(buf
[0], "w");
1280 if ((ai
= BN_to_ASN1_INTEGER(serial
, NULL
)) == NULL
) {
1281 BIO_printf(bio_err
, "error converting serial to ASN.1 format\n");
1284 i2a_ASN1_INTEGER(out
, ai
);
1285 BIO_puts(out
, "\n");
1293 ERR_print_errors(bio_err
);
1295 ASN1_INTEGER_free(ai
);
1299 int rotate_serial(const char *serialfile
, const char *new_suffix
,
1300 const char *old_suffix
)
1305 i
= strlen(serialfile
) + strlen(old_suffix
);
1306 j
= strlen(serialfile
) + strlen(new_suffix
);
1309 if (j
+ 1 >= BSIZE
) {
1310 BIO_printf(bio_err
, "File name too long\n");
1313 #ifndef OPENSSL_SYS_VMS
1314 j
= BIO_snprintf(buf
[0], sizeof(buf
[0]), "%s.%s", serialfile
, new_suffix
);
1315 j
= BIO_snprintf(buf
[1], sizeof(buf
[1]), "%s.%s", serialfile
, old_suffix
);
1317 j
= BIO_snprintf(buf
[0], sizeof(buf
[0]), "%s-%s", serialfile
, new_suffix
);
1318 j
= BIO_snprintf(buf
[1], sizeof(buf
[1]), "%s-%s", serialfile
, old_suffix
);
1320 if (rename(serialfile
, buf
[1]) < 0 && errno
!= ENOENT
1326 "Unable to rename %s to %s\n", serialfile
, buf
[1]);
1330 if (rename(buf
[0], serialfile
) < 0) {
1332 "Unable to rename %s to %s\n", buf
[0], serialfile
);
1334 rename(buf
[1], serialfile
);
1339 ERR_print_errors(bio_err
);
1343 int rand_serial(BIGNUM
*b
, ASN1_INTEGER
*ai
)
1348 btmp
= b
== NULL
? BN_new() : b
;
1352 if (!BN_rand(btmp
, SERIAL_RAND_BITS
, BN_RAND_TOP_ANY
, BN_RAND_BOTTOM_ANY
))
1354 if (ai
&& !BN_to_ASN1_INTEGER(btmp
, ai
))
1367 CA_DB
*load_index(const char *dbfile
, DB_ATTR
*db_attr
)
1369 CA_DB
*retdb
= NULL
;
1370 TXT_DB
*tmpdb
= NULL
;
1372 CONF
*dbattr_conf
= NULL
;
1374 #ifndef OPENSSL_NO_POSIX_IO
1379 in
= BIO_new_file(dbfile
, "r");
1383 #ifndef OPENSSL_NO_POSIX_IO
1384 BIO_get_fp(in
, &dbfp
);
1385 if (fstat(fileno(dbfp
), &dbst
) == -1) {
1386 ERR_raise_data(ERR_LIB_SYS
, errno
,
1387 "calling fstat(%s)", dbfile
);
1392 if ((tmpdb
= TXT_DB_read(in
, DB_NUMBER
)) == NULL
)
1395 #ifndef OPENSSL_SYS_VMS
1396 BIO_snprintf(buf
, sizeof(buf
), "%s.attr", dbfile
);
1398 BIO_snprintf(buf
, sizeof(buf
), "%s-attr", dbfile
);
1400 dbattr_conf
= app_load_config_quiet(buf
);
1402 retdb
= app_malloc(sizeof(*retdb
), "new DB");
1406 retdb
->attributes
= *db_attr
;
1408 retdb
->attributes
.unique_subject
= 1;
1412 char *p
= NCONF_get_string(dbattr_conf
, NULL
, "unique_subject");
1414 retdb
->attributes
.unique_subject
= parse_yesno(p
, 1);
1418 retdb
->dbfname
= OPENSSL_strdup(dbfile
);
1419 #ifndef OPENSSL_NO_POSIX_IO
1424 ERR_print_errors(bio_err
);
1425 NCONF_free(dbattr_conf
);
1432 * Returns > 0 on success, <= 0 on error
1434 int index_index(CA_DB
*db
)
1436 if (!TXT_DB_create_index(db
->db
, DB_serial
, NULL
,
1437 LHASH_HASH_FN(index_serial
),
1438 LHASH_COMP_FN(index_serial
))) {
1440 "Error creating serial number index:(%ld,%ld,%ld)\n",
1441 db
->db
->error
, db
->db
->arg1
, db
->db
->arg2
);
1445 if (db
->attributes
.unique_subject
1446 && !TXT_DB_create_index(db
->db
, DB_name
, index_name_qual
,
1447 LHASH_HASH_FN(index_name
),
1448 LHASH_COMP_FN(index_name
))) {
1449 BIO_printf(bio_err
, "Error creating name index:(%ld,%ld,%ld)\n",
1450 db
->db
->error
, db
->db
->arg1
, db
->db
->arg2
);
1455 ERR_print_errors(bio_err
);
1459 int save_index(const char *dbfile
, const char *suffix
, CA_DB
*db
)
1465 j
= strlen(dbfile
) + strlen(suffix
);
1466 if (j
+ 6 >= BSIZE
) {
1467 BIO_printf(bio_err
, "File name too long\n");
1470 #ifndef OPENSSL_SYS_VMS
1471 j
= BIO_snprintf(buf
[2], sizeof(buf
[2]), "%s.attr", dbfile
);
1472 j
= BIO_snprintf(buf
[1], sizeof(buf
[1]), "%s.attr.%s", dbfile
, suffix
);
1473 j
= BIO_snprintf(buf
[0], sizeof(buf
[0]), "%s.%s", dbfile
, suffix
);
1475 j
= BIO_snprintf(buf
[2], sizeof(buf
[2]), "%s-attr", dbfile
);
1476 j
= BIO_snprintf(buf
[1], sizeof(buf
[1]), "%s-attr-%s", dbfile
, suffix
);
1477 j
= BIO_snprintf(buf
[0], sizeof(buf
[0]), "%s-%s", dbfile
, suffix
);
1479 out
= BIO_new_file(buf
[0], "w");
1482 BIO_printf(bio_err
, "Unable to open '%s'\n", dbfile
);
1485 j
= TXT_DB_write(out
, db
->db
);
1490 out
= BIO_new_file(buf
[1], "w");
1493 BIO_printf(bio_err
, "Unable to open '%s'\n", buf
[2]);
1496 BIO_printf(out
, "unique_subject = %s\n",
1497 db
->attributes
.unique_subject
? "yes" : "no");
1502 ERR_print_errors(bio_err
);
1506 int rotate_index(const char *dbfile
, const char *new_suffix
,
1507 const char *old_suffix
)
1512 i
= strlen(dbfile
) + strlen(old_suffix
);
1513 j
= strlen(dbfile
) + strlen(new_suffix
);
1516 if (j
+ 6 >= BSIZE
) {
1517 BIO_printf(bio_err
, "File name too long\n");
1520 #ifndef OPENSSL_SYS_VMS
1521 j
= BIO_snprintf(buf
[4], sizeof(buf
[4]), "%s.attr", dbfile
);
1522 j
= BIO_snprintf(buf
[3], sizeof(buf
[3]), "%s.attr.%s", dbfile
, old_suffix
);
1523 j
= BIO_snprintf(buf
[2], sizeof(buf
[2]), "%s.attr.%s", dbfile
, new_suffix
);
1524 j
= BIO_snprintf(buf
[1], sizeof(buf
[1]), "%s.%s", dbfile
, old_suffix
);
1525 j
= BIO_snprintf(buf
[0], sizeof(buf
[0]), "%s.%s", dbfile
, new_suffix
);
1527 j
= BIO_snprintf(buf
[4], sizeof(buf
[4]), "%s-attr", dbfile
);
1528 j
= BIO_snprintf(buf
[3], sizeof(buf
[3]), "%s-attr-%s", dbfile
, old_suffix
);
1529 j
= BIO_snprintf(buf
[2], sizeof(buf
[2]), "%s-attr-%s", dbfile
, new_suffix
);
1530 j
= BIO_snprintf(buf
[1], sizeof(buf
[1]), "%s-%s", dbfile
, old_suffix
);
1531 j
= BIO_snprintf(buf
[0], sizeof(buf
[0]), "%s-%s", dbfile
, new_suffix
);
1533 if (rename(dbfile
, buf
[1]) < 0 && errno
!= ENOENT
1538 BIO_printf(bio_err
, "Unable to rename %s to %s\n", dbfile
, buf
[1]);
1542 if (rename(buf
[0], dbfile
) < 0) {
1543 BIO_printf(bio_err
, "Unable to rename %s to %s\n", buf
[0], dbfile
);
1545 rename(buf
[1], dbfile
);
1548 if (rename(buf
[4], buf
[3]) < 0 && errno
!= ENOENT
1553 BIO_printf(bio_err
, "Unable to rename %s to %s\n", buf
[4], buf
[3]);
1555 rename(dbfile
, buf
[0]);
1556 rename(buf
[1], dbfile
);
1559 if (rename(buf
[2], buf
[4]) < 0) {
1560 BIO_printf(bio_err
, "Unable to rename %s to %s\n", buf
[2], buf
[4]);
1562 rename(buf
[3], buf
[4]);
1563 rename(dbfile
, buf
[0]);
1564 rename(buf
[1], dbfile
);
1569 ERR_print_errors(bio_err
);
1573 void free_index(CA_DB
*db
)
1576 TXT_DB_free(db
->db
);
1577 OPENSSL_free(db
->dbfname
);
1582 int parse_yesno(const char *str
, int def
)
1586 case 'f': /* false */
1587 case 'F': /* FALSE */
1592 case 't': /* true */
1593 case 'T': /* TRUE */
1604 * name is expected to be in the format /type0=value0/type1=value1/type2=...
1605 * where characters may be escaped by \
1607 X509_NAME
*parse_name(const char *cp
, long chtype
, int canmulti
)
1609 int nextismulti
= 0;
1615 "name is expected to be in the format "
1616 "/type0=value0/type1=value1/type2=... where characters may "
1617 "be escaped by \\. This name is not in that format: '%s'\n",
1622 n
= X509_NAME_new();
1625 work
= OPENSSL_strdup(cp
);
1627 BIO_printf(bio_err
, "%s: Error copying name input\n", opt_getprog());
1634 unsigned char *valstr
;
1636 int ismulti
= nextismulti
;
1639 /* Collect the type */
1640 while (*cp
&& *cp
!= '=')
1644 "%s: Hit end of string before finding the '='\n",
1651 /* Collect the value. */
1652 valstr
= (unsigned char *)bp
;
1653 for (; *cp
&& *cp
!= '/'; *bp
++ = *cp
++) {
1654 if (canmulti
&& *cp
== '+') {
1658 if (*cp
== '\\' && *++cp
== '\0') {
1660 "%s: Escape character at end of string\n",
1667 /* If not at EOS (must be + or /), move forward. */
1672 nid
= OBJ_txt2nid(typestr
);
1673 if (nid
== NID_undef
) {
1674 BIO_printf(bio_err
, "%s: Skipping unknown attribute \"%s\"\n",
1675 opt_getprog(), typestr
);
1678 if (*valstr
== '\0') {
1680 "%s: No value provided for Subject Attribute %s, skipped\n",
1681 opt_getprog(), typestr
);
1684 if (!X509_NAME_add_entry_by_NID(n
, nid
, chtype
,
1685 valstr
, strlen((char *)valstr
),
1686 -1, ismulti
? -1 : 0)) {
1687 BIO_printf(bio_err
, "%s: Error adding name attribute \"/%s=%s\"\n",
1688 opt_getprog(), typestr
,valstr
);
1703 * Read whole contents of a BIO into an allocated memory buffer and return
1707 int bio_to_mem(unsigned char **out
, int maxlen
, BIO
*in
)
1711 unsigned char tbuf
[1024];
1713 mem
= BIO_new(BIO_s_mem());
1717 if ((maxlen
!= -1) && maxlen
< 1024)
1721 len
= BIO_read(in
, tbuf
, len
);
1728 if (BIO_write(mem
, tbuf
, len
) != len
) {
1737 ret
= BIO_get_mem_data(mem
, (char **)out
);
1738 BIO_set_flags(mem
, BIO_FLAGS_MEM_RDONLY
);
1743 int pkey_ctrl_string(EVP_PKEY_CTX
*ctx
, const char *value
)
1746 char *stmp
, *vtmp
= NULL
;
1747 stmp
= OPENSSL_strdup(value
);
1750 vtmp
= strchr(stmp
, ':');
1755 rv
= EVP_PKEY_CTX_ctrl_str(ctx
, stmp
, vtmp
);
1760 static void nodes_print(const char *name
, STACK_OF(X509_POLICY_NODE
) *nodes
)
1762 X509_POLICY_NODE
*node
;
1765 BIO_printf(bio_err
, "%s Policies:", name
);
1767 BIO_puts(bio_err
, "\n");
1768 for (i
= 0; i
< sk_X509_POLICY_NODE_num(nodes
); i
++) {
1769 node
= sk_X509_POLICY_NODE_value(nodes
, i
);
1770 X509_POLICY_NODE_print(bio_err
, node
, 2);
1773 BIO_puts(bio_err
, " <empty>\n");
1777 void policies_print(X509_STORE_CTX
*ctx
)
1779 X509_POLICY_TREE
*tree
;
1780 int explicit_policy
;
1781 tree
= X509_STORE_CTX_get0_policy_tree(ctx
);
1782 explicit_policy
= X509_STORE_CTX_get_explicit_policy(ctx
);
1784 BIO_printf(bio_err
, "Require explicit Policy: %s\n",
1785 explicit_policy
? "True" : "False");
1787 nodes_print("Authority", X509_policy_tree_get0_policies(tree
));
1788 nodes_print("User", X509_policy_tree_get0_user_policies(tree
));
1792 * next_protos_parse parses a comma separated list of strings into a string
1793 * in a format suitable for passing to SSL_CTX_set_next_protos_advertised.
1794 * outlen: (output) set to the length of the resulting buffer on success.
1795 * err: (maybe NULL) on failure, an error message line is written to this BIO.
1796 * in: a NUL terminated string like "abc,def,ghi"
1798 * returns: a malloc'd buffer or NULL on failure.
1800 unsigned char *next_protos_parse(size_t *outlen
, const char *in
)
1804 size_t i
, start
= 0;
1808 if (len
== 0 || len
>= 65535)
1811 out
= app_malloc(len
+ 1, "NPN buffer");
1812 for (i
= 0; i
<= len
; ++i
) {
1813 if (i
== len
|| in
[i
] == ',') {
1815 * Zero-length ALPN elements are invalid on the wire, we could be
1816 * strict and reject the entire string, but just ignoring extra
1817 * commas seems harmless and more friendly.
1819 * Every comma we skip in this way puts the input buffer another
1820 * byte ahead of the output buffer, so all stores into the output
1821 * buffer need to be decremented by the number commas skipped.
1828 if (i
- start
> 255) {
1832 out
[start
-skipped
] = (unsigned char)(i
- start
);
1835 out
[i
+ 1 - skipped
] = in
[i
];
1839 if (len
<= skipped
) {
1844 *outlen
= len
+ 1 - skipped
;
1848 void print_cert_checks(BIO
*bio
, X509
*x
,
1849 const char *checkhost
,
1850 const char *checkemail
, const char *checkip
)
1855 BIO_printf(bio
, "Hostname %s does%s match certificate\n",
1857 X509_check_host(x
, checkhost
, 0, 0, NULL
) == 1
1862 BIO_printf(bio
, "Email %s does%s match certificate\n",
1863 checkemail
, X509_check_email(x
, checkemail
, 0, 0)
1868 BIO_printf(bio
, "IP %s does%s match certificate\n",
1869 checkip
, X509_check_ip_asc(x
, checkip
, 0) ? "" : " NOT");
1873 /* Get first http URL from a DIST_POINT structure */
1875 static const char *get_dp_url(DIST_POINT
*dp
)
1877 GENERAL_NAMES
*gens
;
1881 if (!dp
->distpoint
|| dp
->distpoint
->type
!= 0)
1883 gens
= dp
->distpoint
->name
.fullname
;
1884 for (i
= 0; i
< sk_GENERAL_NAME_num(gens
); i
++) {
1885 gen
= sk_GENERAL_NAME_value(gens
, i
);
1886 uri
= GENERAL_NAME_get0_value(gen
, >ype
);
1887 if (gtype
== GEN_URI
&& ASN1_STRING_length(uri
) > 6) {
1888 const char *uptr
= (const char *)ASN1_STRING_get0_data(uri
);
1889 if (strncmp(uptr
, "http://", 7) == 0)
1897 * Look through a CRLDP structure and attempt to find an http URL to
1898 * downloads a CRL from.
1901 static X509_CRL
*load_crl_crldp(STACK_OF(DIST_POINT
) *crldp
)
1904 const char *urlptr
= NULL
;
1905 for (i
= 0; i
< sk_DIST_POINT_num(crldp
); i
++) {
1906 DIST_POINT
*dp
= sk_DIST_POINT_value(crldp
, i
);
1907 urlptr
= get_dp_url(dp
);
1909 return load_crl(urlptr
, FORMAT_HTTP
, "CRL via CDP");
1915 * Example of downloading CRLs from CRLDP:
1916 * not usable for real world as it always downloads and doesn't cache anything.
1919 static STACK_OF(X509_CRL
) *crls_http_cb(const X509_STORE_CTX
*ctx
,
1920 const X509_NAME
*nm
)
1923 STACK_OF(X509_CRL
) *crls
= NULL
;
1925 STACK_OF(DIST_POINT
) *crldp
;
1927 crls
= sk_X509_CRL_new_null();
1930 x
= X509_STORE_CTX_get_current_cert(ctx
);
1931 crldp
= X509_get_ext_d2i(x
, NID_crl_distribution_points
, NULL
, NULL
);
1932 crl
= load_crl_crldp(crldp
);
1933 sk_DIST_POINT_pop_free(crldp
, DIST_POINT_free
);
1935 sk_X509_CRL_free(crls
);
1938 sk_X509_CRL_push(crls
, crl
);
1939 /* Try to download delta CRL */
1940 crldp
= X509_get_ext_d2i(x
, NID_freshest_crl
, NULL
, NULL
);
1941 crl
= load_crl_crldp(crldp
);
1942 sk_DIST_POINT_pop_free(crldp
, DIST_POINT_free
);
1944 sk_X509_CRL_push(crls
, crl
);
1948 void store_setup_crl_download(X509_STORE
*st
)
1950 X509_STORE_set_lookup_crls_cb(st
, crls_http_cb
);
1953 #ifndef OPENSSL_NO_SOCK
1954 static const char *tls_error_hint(void)
1956 unsigned long err
= ERR_peek_error();
1958 if (ERR_GET_LIB(err
) != ERR_LIB_SSL
)
1959 err
= ERR_peek_last_error();
1960 if (ERR_GET_LIB(err
) != ERR_LIB_SSL
)
1963 switch (ERR_GET_REASON(err
)) {
1964 case SSL_R_WRONG_VERSION_NUMBER
:
1965 return "The server does not support (a suitable version of) TLS";
1966 case SSL_R_UNKNOWN_PROTOCOL
:
1967 return "The server does not support HTTPS";
1968 case SSL_R_CERTIFICATE_VERIFY_FAILED
:
1969 return "Cannot authenticate server via its TLS certificate, likely due to mismatch with our trusted TLS certs or missing revocation status";
1970 case SSL_AD_REASON_OFFSET
+ TLS1_AD_UNKNOWN_CA
:
1971 return "Server did not accept our TLS certificate, likely due to mismatch with server's trust anchor or missing revocation status";
1972 case SSL_AD_REASON_OFFSET
+ SSL3_AD_HANDSHAKE_FAILURE
:
1973 return "TLS handshake failure. Possibly the server requires our TLS certificate but did not receive it";
1974 default: /* no error or no hint available for error */
1979 /* HTTP callback function that supports TLS connection also via HTTPS proxy */
1980 BIO
*app_http_tls_cb(BIO
*hbio
, void *arg
, int connect
, int detail
)
1982 APP_HTTP_TLS_INFO
*info
= (APP_HTTP_TLS_INFO
*)arg
;
1983 SSL_CTX
*ssl_ctx
= info
->ssl_ctx
;
1987 if (connect
&& detail
) { /* connecting with TLS */
1988 if ((info
->use_proxy
1989 && !OSSL_HTTP_proxy_connect(hbio
, info
->server
, info
->port
,
1990 NULL
, NULL
, /* no proxy credentials */
1991 info
->timeout
, bio_err
, opt_getprog()))
1992 || (sbio
= BIO_new(BIO_f_ssl())) == NULL
) {
1995 if (ssl_ctx
== NULL
|| (ssl
= SSL_new(ssl_ctx
)) == NULL
) {
2000 SSL_set_tlsext_host_name(ssl
, info
->server
);
2002 SSL_set_connect_state(ssl
);
2003 BIO_set_ssl(sbio
, ssl
, BIO_CLOSE
);
2005 hbio
= BIO_push(sbio
, hbio
);
2006 } else if (!connect
&& !detail
) { /* disconnecting after error */
2007 const char *hint
= tls_error_hint();
2009 ERR_add_error_data(2, " : ", hint
);
2011 * If we pop sbio and BIO_free() it this may lead to libssl double free.
2012 * Rely on BIO_free_all() done by OSSL_HTTP_transfer() in http_client.c
2018 ASN1_VALUE
*app_http_get_asn1(const char *url
, const char *proxy
,
2019 const char *no_proxy
, SSL_CTX
*ssl_ctx
,
2020 const STACK_OF(CONF_VALUE
) *headers
,
2021 long timeout
, const char *expected_content_type
,
2022 const ASN1_ITEM
*it
)
2024 APP_HTTP_TLS_INFO info
;
2028 ASN1_VALUE
*resp
= NULL
;
2030 if (url
== NULL
|| it
== NULL
) {
2031 HTTPerr(0, ERR_R_PASSED_NULL_PARAMETER
);
2035 if (!OSSL_HTTP_parse_url(url
, &server
, &port
, NULL
/* ppath */, &use_ssl
))
2037 if (use_ssl
&& ssl_ctx
== NULL
) {
2038 HTTPerr(0, ERR_R_PASSED_NULL_PARAMETER
);
2039 ERR_add_error_data(1, "missing SSL_CTX");
2043 info
.server
= server
;
2045 info
.use_proxy
= proxy
!= NULL
;
2046 info
.timeout
= timeout
;
2047 info
.ssl_ctx
= ssl_ctx
;
2048 resp
= OSSL_HTTP_get_asn1(url
, proxy
, no_proxy
,
2049 NULL
, NULL
, app_http_tls_cb
, &info
,
2050 headers
, 0 /* maxline */, 0 /* max_resp_len */,
2051 timeout
, expected_content_type
, it
);
2053 OPENSSL_free(server
);
2059 ASN1_VALUE
*app_http_post_asn1(const char *host
, const char *port
,
2060 const char *path
, const char *proxy
,
2061 const char *no_proxy
, SSL_CTX
*ssl_ctx
,
2062 const STACK_OF(CONF_VALUE
) *headers
,
2063 const char *content_type
,
2064 ASN1_VALUE
*req
, const ASN1_ITEM
*req_it
,
2065 long timeout
, const ASN1_ITEM
*rsp_it
)
2067 APP_HTTP_TLS_INFO info
;
2071 info
.use_proxy
= proxy
!= NULL
;
2072 info
.timeout
= timeout
;
2073 info
.ssl_ctx
= ssl_ctx
;
2074 return OSSL_HTTP_post_asn1(host
, port
, path
, ssl_ctx
!= NULL
,
2076 NULL
, NULL
, app_http_tls_cb
, &info
,
2077 headers
, content_type
, req
, req_it
,
2079 0 /* max_resp_len */, timeout
, NULL
, rsp_it
);
2085 * Platform-specific sections
2090 # define fileno(a) (int)_fileno(a)
2093 # include <windows.h>
2096 static int WIN32_rename(const char *from
, const char *to
)
2098 TCHAR
*tfrom
= NULL
, *tto
;
2102 if (sizeof(TCHAR
) == 1) {
2103 tfrom
= (TCHAR
*)from
;
2105 } else { /* UNICODE path */
2107 size_t i
, flen
= strlen(from
) + 1, tlen
= strlen(to
) + 1;
2108 tfrom
= malloc(sizeof(*tfrom
) * (flen
+ tlen
));
2112 # if !defined(_WIN32_WCE) || _WIN32_WCE>=101
2113 if (!MultiByteToWideChar(CP_ACP
, 0, from
, flen
, (WCHAR
*)tfrom
, flen
))
2115 for (i
= 0; i
< flen
; i
++)
2116 tfrom
[i
] = (TCHAR
)from
[i
];
2117 # if !defined(_WIN32_WCE) || _WIN32_WCE>=101
2118 if (!MultiByteToWideChar(CP_ACP
, 0, to
, tlen
, (WCHAR
*)tto
, tlen
))
2120 for (i
= 0; i
< tlen
; i
++)
2121 tto
[i
] = (TCHAR
)to
[i
];
2124 if (MoveFile(tfrom
, tto
))
2126 err
= GetLastError();
2127 if (err
== ERROR_ALREADY_EXISTS
|| err
== ERROR_FILE_EXISTS
) {
2128 if (DeleteFile(tto
) && MoveFile(tfrom
, tto
))
2130 err
= GetLastError();
2132 if (err
== ERROR_FILE_NOT_FOUND
|| err
== ERROR_PATH_NOT_FOUND
)
2134 else if (err
== ERROR_ACCESS_DENIED
)
2137 errno
= EINVAL
; /* we could map more codes... */
2141 if (tfrom
!= NULL
&& tfrom
!= (TCHAR
*)from
)
2147 /* app_tminterval section */
2149 double app_tminterval(int stop
, int usertime
)
2153 static ULARGE_INTEGER tmstart
;
2154 static int warning
= 1;
2155 # ifdef _WIN32_WINNT
2156 static HANDLE proc
= NULL
;
2160 proc
= OpenProcess(PROCESS_QUERY_INFORMATION
, FALSE
,
2161 GetCurrentProcessId());
2163 proc
= (HANDLE
) - 1;
2166 if (usertime
&& proc
!= (HANDLE
) - 1) {
2168 GetProcessTimes(proc
, &junk
, &junk
, &junk
, &now
);
2174 if (usertime
&& warning
) {
2175 BIO_printf(bio_err
, "To get meaningful results, run "
2176 "this program on idle system.\n");
2179 GetSystemTime(&systime
);
2180 SystemTimeToFileTime(&systime
, &now
);
2183 if (stop
== TM_START
) {
2184 tmstart
.u
.LowPart
= now
.dwLowDateTime
;
2185 tmstart
.u
.HighPart
= now
.dwHighDateTime
;
2187 ULARGE_INTEGER tmstop
;
2189 tmstop
.u
.LowPart
= now
.dwLowDateTime
;
2190 tmstop
.u
.HighPart
= now
.dwHighDateTime
;
2192 ret
= (__int64
)(tmstop
.QuadPart
- tmstart
.QuadPart
) * 1e-7;
2197 #elif defined(OPENSSL_SYS_VXWORKS)
2200 double app_tminterval(int stop
, int usertime
)
2203 # ifdef CLOCK_REALTIME
2204 static struct timespec tmstart
;
2205 struct timespec now
;
2207 static unsigned long tmstart
;
2210 static int warning
= 1;
2212 if (usertime
&& warning
) {
2213 BIO_printf(bio_err
, "To get meaningful results, run "
2214 "this program on idle system.\n");
2217 # ifdef CLOCK_REALTIME
2218 clock_gettime(CLOCK_REALTIME
, &now
);
2219 if (stop
== TM_START
)
2222 ret
= ((now
.tv_sec
+ now
.tv_nsec
* 1e-9)
2223 - (tmstart
.tv_sec
+ tmstart
.tv_nsec
* 1e-9));
2226 if (stop
== TM_START
)
2229 ret
= (now
- tmstart
) / (double)sysClkRateGet();
2234 #elif defined(OPENSSL_SYSTEM_VMS)
2238 double app_tminterval(int stop
, int usertime
)
2240 static clock_t tmstart
;
2248 now
= rus
.tms_utime
;
2251 now
= clock(); /* sum of user and kernel times */
2254 gettimeofday(&tv
, NULL
);
2255 now
= (clock_t)((unsigned long long)tv
.tv_sec
* CLK_TCK
+
2256 (unsigned long long)tv
.tv_usec
* (1000000 / CLK_TCK
)
2260 if (stop
== TM_START
)
2263 ret
= (now
- tmstart
) / (double)(CLK_TCK
);
2268 #elif defined(_SC_CLK_TCK) /* by means of unistd.h */
2269 # include <sys/times.h>
2271 double app_tminterval(int stop
, int usertime
)
2275 static clock_t tmstart
;
2276 long int tck
= sysconf(_SC_CLK_TCK
);
2282 now
= rus
.tms_utime
;
2285 now
= clock(); /* sum of user and kernel times */
2288 gettimeofday(&tv
, NULL
);
2289 now
= (clock_t)((unsigned long long)tv
.tv_sec
* tck
+
2290 (unsigned long long)tv
.tv_usec
* (1000000 / tck
)
2295 if (stop
== TM_START
) {
2298 ret
= (now
- tmstart
) / (double)tck
;
2305 # include <sys/time.h>
2306 # include <sys/resource.h>
2308 double app_tminterval(int stop
, int usertime
)
2313 static struct timeval tmstart
;
2316 getrusage(RUSAGE_SELF
, &rus
), now
= rus
.ru_utime
;
2318 gettimeofday(&now
, NULL
);
2320 if (stop
== TM_START
)
2323 ret
= ((now
.tv_sec
+ now
.tv_usec
* 1e-6)
2324 - (tmstart
.tv_sec
+ tmstart
.tv_usec
* 1e-6));
2330 int app_access(const char* name
, int flag
)
2333 return _access(name
, flag
);
2335 return access(name
, flag
);
2339 int app_isdir(const char *name
)
2341 return opt_isdir(name
);
2344 /* raw_read|write section */
2346 # include "vms_term_sock.h"
2347 static int stdin_sock
= -1;
2349 static void close_stdin_sock(void)
2351 TerminalSocket (TERM_SOCK_DELETE
, &stdin_sock
);
2354 int fileno_stdin(void)
2356 if (stdin_sock
== -1) {
2357 TerminalSocket(TERM_SOCK_CREATE
, &stdin_sock
);
2358 atexit(close_stdin_sock
);
2364 int fileno_stdin(void)
2366 return fileno(stdin
);
2370 int fileno_stdout(void)
2372 return fileno(stdout
);
2375 #if defined(_WIN32) && defined(STD_INPUT_HANDLE)
2376 int raw_read_stdin(void *buf
, int siz
)
2379 if (ReadFile(GetStdHandle(STD_INPUT_HANDLE
), buf
, siz
, &n
, NULL
))
2384 #elif defined(__VMS)
2385 # include <sys/socket.h>
2387 int raw_read_stdin(void *buf
, int siz
)
2389 return recv(fileno_stdin(), buf
, siz
, 0);
2392 int raw_read_stdin(void *buf
, int siz
)
2394 return read(fileno_stdin(), buf
, siz
);
2398 #if defined(_WIN32) && defined(STD_OUTPUT_HANDLE)
2399 int raw_write_stdout(const void *buf
, int siz
)
2402 if (WriteFile(GetStdHandle(STD_OUTPUT_HANDLE
), buf
, siz
, &n
, NULL
))
2408 int raw_write_stdout(const void *buf
, int siz
)
2410 return write(fileno_stdout(), buf
, siz
);
2415 * Centralized handling of input and output files with format specification
2416 * The format is meant to show what the input and output is supposed to be,
2417 * and is therefore a show of intent more than anything else. However, it
2418 * does impact behavior on some platforms, such as differentiating between
2419 * text and binary input/output on non-Unix platforms
2421 BIO
*dup_bio_in(int format
)
2423 return BIO_new_fp(stdin
,
2424 BIO_NOCLOSE
| (FMT_istext(format
) ? BIO_FP_TEXT
: 0));
2427 BIO
*dup_bio_out(int format
)
2429 BIO
*b
= BIO_new_fp(stdout
,
2430 BIO_NOCLOSE
| (FMT_istext(format
) ? BIO_FP_TEXT
: 0));
2431 void *prefix
= NULL
;
2433 #ifdef OPENSSL_SYS_VMS
2434 if (FMT_istext(format
))
2435 b
= BIO_push(BIO_new(BIO_f_linebuffer()), b
);
2438 if (FMT_istext(format
)
2439 && (prefix
= getenv("HARNESS_OSSL_PREFIX")) != NULL
) {
2440 b
= BIO_push(BIO_new(BIO_f_prefix()), b
);
2441 BIO_set_prefix(b
, prefix
);
2447 BIO
*dup_bio_err(int format
)
2449 BIO
*b
= BIO_new_fp(stderr
,
2450 BIO_NOCLOSE
| (FMT_istext(format
) ? BIO_FP_TEXT
: 0));
2451 #ifdef OPENSSL_SYS_VMS
2452 if (FMT_istext(format
))
2453 b
= BIO_push(BIO_new(BIO_f_linebuffer()), b
);
2458 void unbuffer(FILE *fp
)
2461 * On VMS, setbuf() will only take 32-bit pointers, and a compilation
2462 * with /POINTER_SIZE=64 will give off a MAYLOSEDATA2 warning here.
2463 * However, we trust that the C RTL will never give us a FILE pointer
2464 * above the first 4 GB of memory, so we simply turn off the warning
2467 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
2468 # pragma environment save
2469 # pragma message disable maylosedata2
2472 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
2473 # pragma environment restore
2477 static const char *modestr(char mode
, int format
)
2479 OPENSSL_assert(mode
== 'a' || mode
== 'r' || mode
== 'w');
2483 return FMT_istext(format
) ? "a" : "ab";
2485 return FMT_istext(format
) ? "r" : "rb";
2487 return FMT_istext(format
) ? "w" : "wb";
2489 /* The assert above should make sure we never reach this point */
2493 static const char *modeverb(char mode
)
2503 return "(doing something)";
2507 * Open a file for writing, owner-read-only.
2509 BIO
*bio_open_owner(const char *filename
, int format
, int private)
2513 int fd
= -1, bflags
, mode
, textmode
;
2515 if (!private || filename
== NULL
|| strcmp(filename
, "-") == 0)
2516 return bio_open_default(filename
, 'w', format
);
2525 textmode
= FMT_istext(format
);
2529 #elif defined(_O_BINARY)
2534 #ifdef OPENSSL_SYS_VMS
2535 /* VMS doesn't have O_BINARY, it just doesn't make sense. But,
2536 * it still needs to know that we're going binary, or fdopen()
2537 * will fail with "invalid argument"... so we tell VMS what the
2541 fd
= open(filename
, mode
, 0600, "ctx=bin");
2544 fd
= open(filename
, mode
, 0600);
2547 fp
= fdopen(fd
, modestr('w', format
));
2552 bflags
|= BIO_FP_TEXT
;
2553 b
= BIO_new_fp(fp
, bflags
);
2558 BIO_printf(bio_err
, "%s: Can't open \"%s\" for writing, %s\n",
2559 opt_getprog(), filename
, strerror(errno
));
2560 ERR_print_errors(bio_err
);
2561 /* If we have fp, then fdopen took over fd, so don't close both. */
2569 static BIO
*bio_open_default_(const char *filename
, char mode
, int format
,
2574 if (filename
== NULL
|| strcmp(filename
, "-") == 0) {
2575 ret
= mode
== 'r' ? dup_bio_in(format
) : dup_bio_out(format
);
2583 "Can't open %s, %s\n",
2584 mode
== 'r' ? "stdin" : "stdout", strerror(errno
));
2586 ret
= BIO_new_file(filename
, modestr(mode
, format
));
2594 "Can't open %s for %s, %s\n",
2595 filename
, modeverb(mode
), strerror(errno
));
2597 ERR_print_errors(bio_err
);
2601 BIO
*bio_open_default(const char *filename
, char mode
, int format
)
2603 return bio_open_default_(filename
, mode
, format
, 0);
2606 BIO
*bio_open_default_quiet(const char *filename
, char mode
, int format
)
2608 return bio_open_default_(filename
, mode
, format
, 1);
2611 void wait_for_async(SSL
*s
)
2613 /* On Windows select only works for sockets, so we simply don't wait */
2614 #ifndef OPENSSL_SYS_WINDOWS
2621 if (!SSL_get_all_async_fds(s
, NULL
, &numfds
))
2625 fds
= app_malloc(sizeof(OSSL_ASYNC_FD
) * numfds
, "allocate async fds");
2626 if (!SSL_get_all_async_fds(s
, fds
, &numfds
)) {
2632 for (i
= 0; i
< numfds
; i
++) {
2633 if (width
<= (int)fds
[i
])
2634 width
= (int)fds
[i
] + 1;
2635 openssl_fdset((int)fds
[i
], &asyncfds
);
2637 select(width
, (void *)&asyncfds
, NULL
, NULL
, NULL
);
2642 /* if OPENSSL_SYS_WINDOWS is defined then so is OPENSSL_SYS_MSDOS */
2643 #if defined(OPENSSL_SYS_MSDOS)
2644 int has_stdin_waiting(void)
2646 # if defined(OPENSSL_SYS_WINDOWS)
2647 HANDLE inhand
= GetStdHandle(STD_INPUT_HANDLE
);
2649 INPUT_RECORD inputrec
;
2653 if (inhand
== INVALID_HANDLE_VALUE
) {
2657 peeked
= PeekConsoleInput(inhand
, &inputrec
, insize
, &events
);
2659 /* Probably redirected input? _kbhit() does not work in this case */
2670 /* Corrupt a signature by modifying final byte */
2671 void corrupt_signature(const ASN1_STRING
*signature
)
2673 unsigned char *s
= signature
->data
;
2674 s
[signature
->length
- 1] ^= 0x1;
2677 int set_cert_times(X509
*x
, const char *startdate
, const char *enddate
,
2680 if (startdate
== NULL
|| strcmp(startdate
, "today") == 0) {
2681 if (X509_gmtime_adj(X509_getm_notBefore(x
), 0) == NULL
)
2684 if (!ASN1_TIME_set_string_X509(X509_getm_notBefore(x
), startdate
))
2687 if (enddate
== NULL
) {
2688 if (X509_time_adj_ex(X509_getm_notAfter(x
), days
, 0, NULL
)
2691 } else if (!ASN1_TIME_set_string_X509(X509_getm_notAfter(x
), enddate
)) {
2697 void make_uppercase(char *string
)
2701 for (i
= 0; string
[i
] != '\0'; i
++)
2702 string
[i
] = toupper((unsigned char)string
[i
]);
2705 int opt_printf_stderr(const char *fmt
, ...)
2711 ret
= BIO_vprintf(bio_err
, fmt
, ap
);
2716 OSSL_PARAM
*app_params_new_from_opts(STACK_OF(OPENSSL_STRING
) *opts
,
2717 const OSSL_PARAM
*paramdefs
)
2719 OSSL_PARAM
*params
= NULL
;
2720 size_t sz
= (size_t)sk_OPENSSL_STRING_num(opts
);
2722 char *opt
= "", *stmp
, *vtmp
= NULL
;
2728 params
= OPENSSL_zalloc(sizeof(OSSL_PARAM
) * (sz
+ 1));
2732 for (params_n
= 0; params_n
< sz
; params_n
++) {
2733 opt
= sk_OPENSSL_STRING_value(opts
, (int)params_n
);
2734 if ((stmp
= OPENSSL_strdup(opt
)) == NULL
2735 || (vtmp
= strchr(stmp
, ':')) == NULL
)
2737 /* Replace ':' with 0 to terminate the string pointed to by stmp */
2739 /* Skip over the separator so that vmtp points to the value */
2741 if (!OSSL_PARAM_allocate_from_text(¶ms
[params_n
], paramdefs
,
2742 stmp
, vtmp
, strlen(vtmp
), &found
))
2746 params
[params_n
] = OSSL_PARAM_construct_end();
2750 BIO_printf(bio_err
, "Parameter %s '%s'\n", found
? "error" : "unknown",
2752 ERR_print_errors(bio_err
);
2753 app_params_free(params
);
2757 void app_params_free(OSSL_PARAM
*params
)
2761 if (params
!= NULL
) {
2762 for (i
= 0; params
[i
].key
!= NULL
; ++i
)
2763 OPENSSL_free(params
[i
].data
);
2764 OPENSSL_free(params
);