apps.h 12 KB

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