apps.h 12 KB

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