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