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