1b0d17251Schristos /* 2b0d17251Schristos * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved. 3b0d17251Schristos * 4b0d17251Schristos * Licensed under the Apache License 2.0 (the "License"). You may not use 5b0d17251Schristos * this file except in compliance with the License. You can obtain a copy 6b0d17251Schristos * in the file LICENSE in the source distribution or at 7b0d17251Schristos * https://www.openssl.org/source/license.html 8b0d17251Schristos */ 9b0d17251Schristos 10b0d17251Schristos #ifndef OSSL_APPS_H 11b0d17251Schristos # define OSSL_APPS_H 12b0d17251Schristos 13b0d17251Schristos # include "e_os.h" /* struct timeval for DTLS */ 14b0d17251Schristos # include "internal/nelem.h" 15b0d17251Schristos # include "internal/sockets.h" /* for openssl_fdset() */ 16b0d17251Schristos # include "internal/cryptlib.h" /* ossl_assert() */ 17b0d17251Schristos # include <assert.h> 18b0d17251Schristos 19b0d17251Schristos # include <stdarg.h> 20b0d17251Schristos # include <sys/types.h> 21b0d17251Schristos # ifndef OPENSSL_NO_POSIX_IO 22b0d17251Schristos # include <sys/stat.h> 23b0d17251Schristos # include <fcntl.h> 24b0d17251Schristos # endif 25b0d17251Schristos 26b0d17251Schristos # include <openssl/e_os2.h> 27b0d17251Schristos # include <openssl/types.h> 28b0d17251Schristos # include <openssl/bio.h> 29b0d17251Schristos # include <openssl/x509.h> 30b0d17251Schristos # include <openssl/conf.h> 31b0d17251Schristos # include <openssl/txt_db.h> 32b0d17251Schristos # include <openssl/engine.h> 33b0d17251Schristos # include <openssl/ocsp.h> 34b0d17251Schristos # include <openssl/http.h> 35b0d17251Schristos # include <signal.h> 36b0d17251Schristos # include "apps_ui.h" 37b0d17251Schristos # include "opt.h" 38b0d17251Schristos # include "fmt.h" 39b0d17251Schristos # include "platform.h" 40b0d17251Schristos # include "engine_loader.h" 41b0d17251Schristos # include "app_libctx.h" 42b0d17251Schristos 43b0d17251Schristos /* 44b0d17251Schristos * quick macro when you need to pass an unsigned char instead of a char. 45b0d17251Schristos * this is true for some implementations of the is*() functions, for 46b0d17251Schristos * example. 47b0d17251Schristos */ 48b0d17251Schristos # define _UC(c) ((unsigned char)(c)) 49b0d17251Schristos 50b0d17251Schristos void app_RAND_load_conf(CONF *c, const char *section); 51b0d17251Schristos int app_RAND_write(void); 52b0d17251Schristos int app_RAND_load(void); 53b0d17251Schristos 54b0d17251Schristos extern char *default_config_file; /* may be "" */ 55b0d17251Schristos extern BIO *bio_in; 56b0d17251Schristos extern BIO *bio_out; 57b0d17251Schristos extern BIO *bio_err; 58b0d17251Schristos extern const unsigned char tls13_aes128gcmsha256_id[]; 59b0d17251Schristos extern const unsigned char tls13_aes256gcmsha384_id[]; 60b0d17251Schristos extern BIO_ADDR *ourpeer; 61b0d17251Schristos 62b0d17251Schristos BIO *dup_bio_in(int format); 63b0d17251Schristos BIO *dup_bio_out(int format); 64b0d17251Schristos BIO *dup_bio_err(int format); 65b0d17251Schristos BIO *bio_open_owner(const char *filename, int format, int private); 66b0d17251Schristos BIO *bio_open_default(const char *filename, char mode, int format); 67b0d17251Schristos BIO *bio_open_default_quiet(const char *filename, char mode, int format); 68b0d17251Schristos CONF *app_load_config_bio(BIO *in, const char *filename); 69b0d17251Schristos #define app_load_config(filename) app_load_config_internal(filename, 0) 70b0d17251Schristos #define app_load_config_quiet(filename) app_load_config_internal(filename, 1) 71b0d17251Schristos CONF *app_load_config_internal(const char *filename, int quiet); 72b0d17251Schristos CONF *app_load_config_verbose(const char *filename, int verbose); 73b0d17251Schristos int app_load_modules(const CONF *config); 74b0d17251Schristos CONF *app_load_config_modules(const char *configfile); 75b0d17251Schristos void unbuffer(FILE *fp); 76b0d17251Schristos void wait_for_async(SSL *s); 77b0d17251Schristos # if defined(OPENSSL_SYS_MSDOS) 78b0d17251Schristos int has_stdin_waiting(void); 79b0d17251Schristos # endif 80b0d17251Schristos 81b0d17251Schristos void corrupt_signature(const ASN1_STRING *signature); 82b0d17251Schristos int set_cert_times(X509 *x, const char *startdate, const char *enddate, 83b0d17251Schristos int days); 84b0d17251Schristos int set_crl_lastupdate(X509_CRL *crl, const char *lastupdate); 85b0d17251Schristos int set_crl_nextupdate(X509_CRL *crl, const char *nextupdate, 86b0d17251Schristos long days, long hours, long secs); 87b0d17251Schristos 88b0d17251Schristos typedef struct args_st { 89b0d17251Schristos int size; 90b0d17251Schristos int argc; 91b0d17251Schristos char **argv; 92b0d17251Schristos } ARGS; 93b0d17251Schristos 94b0d17251Schristos /* We need both wrap and the "real" function because libcrypto uses both. */ 95b0d17251Schristos int wrap_password_callback(char *buf, int bufsiz, int verify, void *cb_data); 96b0d17251Schristos 97b0d17251Schristos int chopup_args(ARGS *arg, char *buf); 98b0d17251Schristos void dump_cert_text(BIO *out, X509 *x); 99b0d17251Schristos void print_name(BIO *out, const char *title, const X509_NAME *nm); 100b0d17251Schristos void print_bignum_var(BIO *, const BIGNUM *, const char*, 101b0d17251Schristos int, unsigned char *); 102b0d17251Schristos void print_array(BIO *, const char *, int, const unsigned char *); 103b0d17251Schristos int set_nameopt(const char *arg); 104b0d17251Schristos unsigned long get_nameopt(void); 105b0d17251Schristos int set_dateopt(unsigned long *dateopt, const char *arg); 106b0d17251Schristos int set_cert_ex(unsigned long *flags, const char *arg); 107b0d17251Schristos int set_name_ex(unsigned long *flags, const char *arg); 108b0d17251Schristos int set_ext_copy(int *copy_type, const char *arg); 109b0d17251Schristos int copy_extensions(X509 *x, X509_REQ *req, int copy_type); 110b0d17251Schristos char *get_passwd(const char *pass, const char *desc); 111b0d17251Schristos int app_passwd(const char *arg1, const char *arg2, char **pass1, char **pass2); 112b0d17251Schristos int add_oid_section(CONF *conf); 113b0d17251Schristos X509_REQ *load_csr(const char *file, int format, const char *desc); 114b0d17251Schristos X509 *load_cert_pass(const char *uri, int format, int maybe_stdin, 115b0d17251Schristos const char *pass, const char *desc); 116b0d17251Schristos #define load_cert(uri, format, desc) load_cert_pass(uri, format, 1, NULL, desc) 117b0d17251Schristos X509_CRL *load_crl(const char *uri, int format, int maybe_stdin, 118b0d17251Schristos const char *desc); 119b0d17251Schristos void cleanse(char *str); 120b0d17251Schristos void clear_free(char *str); 121b0d17251Schristos EVP_PKEY *load_key(const char *uri, int format, int maybe_stdin, 122b0d17251Schristos const char *pass, ENGINE *e, const char *desc); 123b0d17251Schristos EVP_PKEY *load_pubkey(const char *uri, int format, int maybe_stdin, 124b0d17251Schristos const char *pass, ENGINE *e, const char *desc); 125b0d17251Schristos EVP_PKEY *load_keyparams(const char *uri, int format, int maybe_stdin, 126b0d17251Schristos const char *keytype, const char *desc); 127b0d17251Schristos EVP_PKEY *load_keyparams_suppress(const char *uri, int format, int maybe_stdin, 128b0d17251Schristos const char *keytype, const char *desc, 129b0d17251Schristos int suppress_decode_errors); 130b0d17251Schristos char *next_item(char *opt); /* in list separated by comma and/or space */ 131b0d17251Schristos int load_cert_certs(const char *uri, 132b0d17251Schristos X509 **pcert, STACK_OF(X509) **pcerts, 133b0d17251Schristos int exclude_http, const char *pass, const char *desc, 134b0d17251Schristos X509_VERIFY_PARAM *vpm); 135b0d17251Schristos STACK_OF(X509) *load_certs_multifile(char *files, const char *pass, 136b0d17251Schristos const char *desc, X509_VERIFY_PARAM *vpm); 137b0d17251Schristos X509_STORE *load_certstore(char *input, const char *pass, const char *desc, 138b0d17251Schristos X509_VERIFY_PARAM *vpm); 139b0d17251Schristos int load_certs(const char *uri, int maybe_stdin, STACK_OF(X509) **certs, 140b0d17251Schristos const char *pass, const char *desc); 141b0d17251Schristos int load_crls(const char *uri, STACK_OF(X509_CRL) **crls, 142b0d17251Schristos const char *pass, const char *desc); 143b0d17251Schristos int load_key_certs_crls(const char *uri, int format, int maybe_stdin, 144b0d17251Schristos const char *pass, const char *desc, 145b0d17251Schristos EVP_PKEY **ppkey, EVP_PKEY **ppubkey, 146b0d17251Schristos EVP_PKEY **pparams, 147b0d17251Schristos X509 **pcert, STACK_OF(X509) **pcerts, 148b0d17251Schristos X509_CRL **pcrl, STACK_OF(X509_CRL) **pcrls); 149b0d17251Schristos int load_key_cert_crl(const char *uri, int format, int maybe_stdin, 150b0d17251Schristos const char *pass, const char *desc, 151b0d17251Schristos EVP_PKEY **ppkey, EVP_PKEY **ppubkey, 152b0d17251Schristos X509 **pcert, X509_CRL **pcrl); 153b0d17251Schristos X509_STORE *setup_verify(const char *CAfile, int noCAfile, 154b0d17251Schristos const char *CApath, int noCApath, 155b0d17251Schristos const char *CAstore, int noCAstore); 156b0d17251Schristos __owur int ctx_set_verify_locations(SSL_CTX *ctx, 157b0d17251Schristos const char *CAfile, int noCAfile, 158b0d17251Schristos const char *CApath, int noCApath, 159b0d17251Schristos const char *CAstore, int noCAstore); 160b0d17251Schristos 161b0d17251Schristos # ifndef OPENSSL_NO_CT 162b0d17251Schristos 163b0d17251Schristos /* 164b0d17251Schristos * Sets the file to load the Certificate Transparency log list from. 165b0d17251Schristos * If path is NULL, loads from the default file path. 166b0d17251Schristos * Returns 1 on success, 0 otherwise. 167b0d17251Schristos */ 168b0d17251Schristos __owur int ctx_set_ctlog_list_file(SSL_CTX *ctx, const char *path); 169b0d17251Schristos 170b0d17251Schristos # endif 171b0d17251Schristos 172b0d17251Schristos ENGINE *setup_engine_methods(const char *id, unsigned int methods, int debug); 173b0d17251Schristos # define setup_engine(e, debug) setup_engine_methods(e, (unsigned int)-1, debug) 174b0d17251Schristos void release_engine(ENGINE *e); 175b0d17251Schristos int init_engine(ENGINE *e); 176b0d17251Schristos int finish_engine(ENGINE *e); 177b0d17251Schristos char *make_engine_uri(ENGINE *e, const char *key_id, const char *desc); 178b0d17251Schristos 179b0d17251Schristos int get_legacy_pkey_id(OSSL_LIB_CTX *libctx, const char *algname, ENGINE *e); 180b0d17251Schristos const EVP_MD *get_digest_from_engine(const char *name); 181b0d17251Schristos const EVP_CIPHER *get_cipher_from_engine(const char *name); 182b0d17251Schristos 183b0d17251Schristos # ifndef OPENSSL_NO_OCSP 184b0d17251Schristos OCSP_RESPONSE *process_responder(OCSP_REQUEST *req, const char *host, 185b0d17251Schristos const char *port, const char *path, 186b0d17251Schristos const char *proxy, const char *no_proxy, 187b0d17251Schristos int use_ssl, STACK_OF(CONF_VALUE) *headers, 188b0d17251Schristos int req_timeout); 189b0d17251Schristos # endif 190b0d17251Schristos 191b0d17251Schristos /* Functions defined in ca.c and also used in ocsp.c */ 192b0d17251Schristos int unpack_revinfo(ASN1_TIME **prevtm, int *preason, ASN1_OBJECT **phold, 193b0d17251Schristos ASN1_GENERALIZEDTIME **pinvtm, const char *str); 194b0d17251Schristos 195b0d17251Schristos # define DB_type 0 196b0d17251Schristos # define DB_exp_date 1 197b0d17251Schristos # define DB_rev_date 2 198b0d17251Schristos # define DB_serial 3 /* index - unique */ 199b0d17251Schristos # define DB_file 4 200b0d17251Schristos # define DB_name 5 /* index - unique when active and not 201b0d17251Schristos * disabled */ 202b0d17251Schristos # define DB_NUMBER 6 203b0d17251Schristos 204b0d17251Schristos # define DB_TYPE_REV 'R' /* Revoked */ 205b0d17251Schristos # define DB_TYPE_EXP 'E' /* Expired */ 206b0d17251Schristos # define DB_TYPE_VAL 'V' /* Valid ; inserted with: ca ... -valid */ 207b0d17251Schristos # define DB_TYPE_SUSP 'S' /* Suspended */ 208b0d17251Schristos 209b0d17251Schristos typedef struct db_attr_st { 210b0d17251Schristos int unique_subject; 211b0d17251Schristos } DB_ATTR; 212b0d17251Schristos typedef struct ca_db_st { 213b0d17251Schristos DB_ATTR attributes; 214b0d17251Schristos TXT_DB *db; 215b0d17251Schristos char *dbfname; 216b0d17251Schristos # ifndef OPENSSL_NO_POSIX_IO 217b0d17251Schristos struct stat dbst; 218b0d17251Schristos # endif 219b0d17251Schristos } CA_DB; 220b0d17251Schristos 221*0ca4a209Schristos void app_bail_out(const char *fmt, ...); 222b0d17251Schristos void *app_malloc(size_t sz, const char *what); 223b0d17251Schristos 224b0d17251Schristos /* load_serial, save_serial, and rotate_serial are also used for CRL numbers */ 225b0d17251Schristos BIGNUM *load_serial(const char *serialfile, int *exists, int create, 226b0d17251Schristos ASN1_INTEGER **retai); 227b0d17251Schristos int save_serial(const char *serialfile, const char *suffix, 228b0d17251Schristos const BIGNUM *serial, ASN1_INTEGER **retai); 229b0d17251Schristos int rotate_serial(const char *serialfile, const char *new_suffix, 230b0d17251Schristos const char *old_suffix); 231b0d17251Schristos int rand_serial(BIGNUM *b, ASN1_INTEGER *ai); 232b0d17251Schristos 233b0d17251Schristos CA_DB *load_index(const char *dbfile, DB_ATTR *dbattr); 234b0d17251Schristos int index_index(CA_DB *db); 235b0d17251Schristos int save_index(const char *dbfile, const char *suffix, CA_DB *db); 236b0d17251Schristos int rotate_index(const char *dbfile, const char *new_suffix, 237b0d17251Schristos const char *old_suffix); 238b0d17251Schristos void free_index(CA_DB *db); 239b0d17251Schristos # define index_name_cmp_noconst(a, b) \ 240b0d17251Schristos index_name_cmp((const OPENSSL_CSTRING *)CHECKED_PTR_OF(OPENSSL_STRING, a), \ 241b0d17251Schristos (const OPENSSL_CSTRING *)CHECKED_PTR_OF(OPENSSL_STRING, b)) 242b0d17251Schristos int index_name_cmp(const OPENSSL_CSTRING *a, const OPENSSL_CSTRING *b); 243b0d17251Schristos int parse_yesno(const char *str, int def); 244b0d17251Schristos 245b0d17251Schristos X509_NAME *parse_name(const char *str, int chtype, int multirdn, 246b0d17251Schristos const char *desc); 247b0d17251Schristos void policies_print(X509_STORE_CTX *ctx); 248b0d17251Schristos int bio_to_mem(unsigned char **out, int maxlen, BIO *in); 249b0d17251Schristos int pkey_ctrl_string(EVP_PKEY_CTX *ctx, const char *value); 250b0d17251Schristos int x509_ctrl_string(X509 *x, const char *value); 251b0d17251Schristos int x509_req_ctrl_string(X509_REQ *x, const char *value); 252b0d17251Schristos int init_gen_str(EVP_PKEY_CTX **pctx, 253b0d17251Schristos const char *algname, ENGINE *e, int do_param, 254b0d17251Schristos OSSL_LIB_CTX *libctx, const char *propq); 255b0d17251Schristos int do_X509_sign(X509 *x, EVP_PKEY *pkey, const char *md, 256b0d17251Schristos STACK_OF(OPENSSL_STRING) *sigopts, X509V3_CTX *ext_ctx); 257b0d17251Schristos int do_X509_verify(X509 *x, EVP_PKEY *pkey, STACK_OF(OPENSSL_STRING) *vfyopts); 258b0d17251Schristos int do_X509_REQ_sign(X509_REQ *x, EVP_PKEY *pkey, const char *md, 259b0d17251Schristos STACK_OF(OPENSSL_STRING) *sigopts); 260b0d17251Schristos int do_X509_REQ_verify(X509_REQ *x, EVP_PKEY *pkey, 261b0d17251Schristos STACK_OF(OPENSSL_STRING) *vfyopts); 262b0d17251Schristos int do_X509_CRL_sign(X509_CRL *x, EVP_PKEY *pkey, const char *md, 263b0d17251Schristos STACK_OF(OPENSSL_STRING) *sigopts); 264b0d17251Schristos 265b0d17251Schristos extern char *psk_key; 266b0d17251Schristos 267b0d17251Schristos 268b0d17251Schristos unsigned char *next_protos_parse(size_t *outlen, const char *in); 269b0d17251Schristos 270b0d17251Schristos void print_cert_checks(BIO *bio, X509 *x, 271b0d17251Schristos const char *checkhost, 272b0d17251Schristos const char *checkemail, const char *checkip); 273b0d17251Schristos 274b0d17251Schristos void store_setup_crl_download(X509_STORE *st); 275b0d17251Schristos 276b0d17251Schristos typedef struct app_http_tls_info_st { 277b0d17251Schristos const char *server; 278b0d17251Schristos const char *port; 279b0d17251Schristos int use_proxy; 280b0d17251Schristos long timeout; 281b0d17251Schristos SSL_CTX *ssl_ctx; 282b0d17251Schristos } APP_HTTP_TLS_INFO; 283b0d17251Schristos BIO *app_http_tls_cb(BIO *hbio, /* APP_HTTP_TLS_INFO */ void *arg, 284b0d17251Schristos int connect, int detail); 285b0d17251Schristos void APP_HTTP_TLS_INFO_free(APP_HTTP_TLS_INFO *info); 286b0d17251Schristos # ifndef OPENSSL_NO_SOCK 287b0d17251Schristos ASN1_VALUE *app_http_get_asn1(const char *url, const char *proxy, 288b0d17251Schristos const char *no_proxy, SSL_CTX *ssl_ctx, 289b0d17251Schristos const STACK_OF(CONF_VALUE) *headers, 290b0d17251Schristos long timeout, const char *expected_content_type, 291b0d17251Schristos const ASN1_ITEM *it); 292b0d17251Schristos ASN1_VALUE *app_http_post_asn1(const char *host, const char *port, 293b0d17251Schristos const char *path, const char *proxy, 294b0d17251Schristos const char *no_proxy, SSL_CTX *ctx, 295b0d17251Schristos const STACK_OF(CONF_VALUE) *headers, 296b0d17251Schristos const char *content_type, 297b0d17251Schristos ASN1_VALUE *req, const ASN1_ITEM *req_it, 298b0d17251Schristos const char *expected_content_type, 299b0d17251Schristos long timeout, const ASN1_ITEM *rsp_it); 300b0d17251Schristos # endif 301b0d17251Schristos 302b0d17251Schristos # define EXT_COPY_NONE 0 303b0d17251Schristos # define EXT_COPY_ADD 1 304b0d17251Schristos # define EXT_COPY_ALL 2 305b0d17251Schristos 306b0d17251Schristos # define NETSCAPE_CERT_HDR "certificate" 307b0d17251Schristos 308b0d17251Schristos # define APP_PASS_LEN 1024 309b0d17251Schristos 310b0d17251Schristos /* 311b0d17251Schristos * IETF RFC 5280 says serial number must be <= 20 bytes. Use 159 bits 312b0d17251Schristos * so that the first bit will never be one, so that the DER encoding 313b0d17251Schristos * rules won't force a leading octet. 314b0d17251Schristos */ 315b0d17251Schristos # define SERIAL_RAND_BITS 159 316b0d17251Schristos 317b0d17251Schristos int app_isdir(const char *); 318b0d17251Schristos int app_access(const char *, int flag); 319b0d17251Schristos int fileno_stdin(void); 320b0d17251Schristos int fileno_stdout(void); 321b0d17251Schristos int raw_read_stdin(void *, int); 322b0d17251Schristos int raw_write_stdout(const void *, int); 323b0d17251Schristos 324b0d17251Schristos # define TM_START 0 325b0d17251Schristos # define TM_STOP 1 326b0d17251Schristos double app_tminterval(int stop, int usertime); 327b0d17251Schristos 328b0d17251Schristos void make_uppercase(char *string); 329b0d17251Schristos 330b0d17251Schristos typedef struct verify_options_st { 331b0d17251Schristos int depth; 332b0d17251Schristos int quiet; 333b0d17251Schristos int error; 334b0d17251Schristos int return_error; 335b0d17251Schristos } VERIFY_CB_ARGS; 336b0d17251Schristos 337b0d17251Schristos extern VERIFY_CB_ARGS verify_args; 338b0d17251Schristos 339b0d17251Schristos OSSL_PARAM *app_params_new_from_opts(STACK_OF(OPENSSL_STRING) *opts, 340b0d17251Schristos const OSSL_PARAM *paramdefs); 341b0d17251Schristos void app_params_free(OSSL_PARAM *params); 342b0d17251Schristos int app_provider_load(OSSL_LIB_CTX *libctx, const char *provider_name); 343b0d17251Schristos void app_providers_cleanup(void); 344b0d17251Schristos 345b0d17251Schristos EVP_PKEY *app_keygen(EVP_PKEY_CTX *ctx, const char *alg, int bits, int verbose); 346b0d17251Schristos EVP_PKEY *app_paramgen(EVP_PKEY_CTX *ctx, const char *alg); 347b0d17251Schristos 348b0d17251Schristos #endif 349