apps.h revision 238405
155714Skris/* apps/apps.h */ 255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 355714Skris * All rights reserved. 455714Skris * 555714Skris * This package is an SSL implementation written 655714Skris * by Eric Young (eay@cryptsoft.com). 755714Skris * The implementation was written so as to conform with Netscapes SSL. 855714Skris * 955714Skris * This library is free for commercial and non-commercial use as long as 1055714Skris * the following conditions are aheared to. The following conditions 1155714Skris * apply to all code found in this distribution, be it the RC4, RSA, 1255714Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1355714Skris * included with this distribution is covered by the same copyright terms 1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 1555714Skris * 1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in 1755714Skris * the code are not to be removed. 1855714Skris * If this package is used in a product, Eric Young should be given attribution 1955714Skris * as the author of the parts of the library used. 2055714Skris * This can be in the form of a textual message at program startup or 2155714Skris * in documentation (online or textual) provided with the package. 2255714Skris * 2355714Skris * Redistribution and use in source and binary forms, with or without 2455714Skris * modification, are permitted provided that the following conditions 2555714Skris * are met: 2655714Skris * 1. Redistributions of source code must retain the copyright 2755714Skris * notice, this list of conditions and the following disclaimer. 2855714Skris * 2. Redistributions in binary form must reproduce the above copyright 2955714Skris * notice, this list of conditions and the following disclaimer in the 3055714Skris * documentation and/or other materials provided with the distribution. 3155714Skris * 3. All advertising materials mentioning features or use of this software 3255714Skris * must display the following acknowledgement: 3355714Skris * "This product includes cryptographic software written by 3455714Skris * Eric Young (eay@cryptsoft.com)" 3555714Skris * The word 'cryptographic' can be left out if the rouines from the library 3655714Skris * being used are not cryptographic related :-). 3755714Skris * 4. If you include any Windows specific code (or a derivative thereof) from 3855714Skris * the apps directory (application code) you must include an acknowledgement: 3955714Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 4055714Skris * 4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4455714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5155714Skris * SUCH DAMAGE. 5255714Skris * 5355714Skris * The licence and distribution terms for any publically available version or 5455714Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 5555714Skris * copied and put under another distribution licence 5655714Skris * [including the GNU Public Licence.] 5755714Skris */ 58109998Smarkm/* ==================================================================== 59109998Smarkm * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved. 60109998Smarkm * 61109998Smarkm * Redistribution and use in source and binary forms, with or without 62109998Smarkm * modification, are permitted provided that the following conditions 63109998Smarkm * are met: 64109998Smarkm * 65109998Smarkm * 1. Redistributions of source code must retain the above copyright 66109998Smarkm * notice, this list of conditions and the following disclaimer. 67109998Smarkm * 68109998Smarkm * 2. Redistributions in binary form must reproduce the above copyright 69109998Smarkm * notice, this list of conditions and the following disclaimer in 70109998Smarkm * the documentation and/or other materials provided with the 71109998Smarkm * distribution. 72109998Smarkm * 73109998Smarkm * 3. All advertising materials mentioning features or use of this 74109998Smarkm * software must display the following acknowledgment: 75109998Smarkm * "This product includes software developed by the OpenSSL Project 76109998Smarkm * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 77109998Smarkm * 78109998Smarkm * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 79109998Smarkm * endorse or promote products derived from this software without 80109998Smarkm * prior written permission. For written permission, please contact 81109998Smarkm * openssl-core@openssl.org. 82109998Smarkm * 83109998Smarkm * 5. Products derived from this software may not be called "OpenSSL" 84109998Smarkm * nor may "OpenSSL" appear in their names without prior written 85109998Smarkm * permission of the OpenSSL Project. 86109998Smarkm * 87109998Smarkm * 6. Redistributions of any form whatsoever must retain the following 88109998Smarkm * acknowledgment: 89109998Smarkm * "This product includes software developed by the OpenSSL Project 90109998Smarkm * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 91109998Smarkm * 92109998Smarkm * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 93109998Smarkm * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 94109998Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 95109998Smarkm * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 96109998Smarkm * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 97109998Smarkm * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 98109998Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 99109998Smarkm * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 100109998Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 101109998Smarkm * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 102109998Smarkm * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 103109998Smarkm * OF THE POSSIBILITY OF SUCH DAMAGE. 104109998Smarkm * ==================================================================== 105109998Smarkm * 106109998Smarkm * This product includes cryptographic software written by Eric Young 107109998Smarkm * (eay@cryptsoft.com). This product includes software written by Tim 108109998Smarkm * Hudson (tjh@cryptsoft.com). 109109998Smarkm * 110109998Smarkm */ 11155714Skris 11255714Skris#ifndef HEADER_APPS_H 11355714Skris#define HEADER_APPS_H 11455714Skris 115109998Smarkm#include "e_os.h" 11655714Skris 11755714Skris#include <openssl/bio.h> 11859191Skris#include <openssl/x509.h> 11968651Skris#include <openssl/lhash.h> 12068651Skris#include <openssl/conf.h> 121109998Smarkm#include <openssl/txt_db.h> 122111147Snectar#ifndef OPENSSL_NO_ENGINE 123109998Smarkm#include <openssl/engine.h> 124111147Snectar#endif 125194206Ssimon#ifndef OPENSSL_NO_OCSP 126194206Ssimon#include <openssl/ocsp.h> 127194206Ssimon#endif 128109998Smarkm#include <openssl/ossl_typ.h> 12955714Skris 13059191Skrisint app_RAND_load_file(const char *file, BIO *bio_e, int dont_warn); 13159191Skrisint app_RAND_write_file(const char *file, BIO *bio_e); 13259191Skris/* When `file' is NULL, use defaults. 13359191Skris * `bio_e' is for error messages. */ 13459191Skrisvoid app_RAND_allow_write_file(void); 13559191Skrislong app_RAND_load_files(char *file); /* `file' is a list of files to read, 13659191Skris * separated by LIST_SEPARATOR_CHAR 13759191Skris * (see e_os.h). The string is 13859191Skris * destroyed! */ 13959191Skris 14055714Skris#ifndef MONOLITH 14155714Skris 14255714Skris#define MAIN(a,v) main(a,v) 14355714Skris 14455714Skris#ifndef NON_MAIN 145109998SmarkmCONF *config=NULL; 14655714SkrisBIO *bio_err=NULL; 14755714Skris#else 148109998Smarkmextern CONF *config; 14955714Skrisextern BIO *bio_err; 15055714Skris#endif 15155714Skris 15255714Skris#else 15355714Skris 15455714Skris#define MAIN(a,v) PROG(a,v) 155109998Smarkmextern CONF *config; 15655714Skrisextern char *default_config_file; 15755714Skrisextern BIO *bio_err; 15855714Skris 15955714Skris#endif 16055714Skris 161160814Ssimon#ifndef OPENSSL_SYS_NETWARE 16255714Skris#include <signal.h> 163160814Ssimon#endif 16455714Skris 16555714Skris#ifdef SIGPIPE 16655714Skris#define do_pipe_sig() signal(SIGPIPE,SIG_IGN) 16755714Skris#else 16855714Skris#define do_pipe_sig() 16955714Skris#endif 17055714Skris 171238405Sjkim#ifdef OPENSSL_NO_COMP 172238405Sjkim#define zlib_cleanup() 173238405Sjkim#else 174238405Sjkim#define zlib_cleanup() COMP_zlib_cleanup() 175238405Sjkim#endif 176238405Sjkim 17759191Skris#if defined(MONOLITH) && !defined(OPENSSL_C) 178109998Smarkm# define apps_startup() \ 179109998Smarkm do_pipe_sig() 180109998Smarkm# define apps_shutdown() 18155714Skris#else 182111147Snectar# ifndef OPENSSL_NO_ENGINE 183238405Sjkim# define apps_startup() \ 184238405Sjkim do { do_pipe_sig(); CRYPTO_malloc_init(); \ 185111147Snectar ERR_load_crypto_strings(); OpenSSL_add_all_algorithms(); \ 186111147Snectar ENGINE_load_builtin_engines(); setup_ui_method(); } while(0) 187111147Snectar# define apps_shutdown() \ 188111147Snectar do { CONF_modules_unload(1); destroy_ui_method(); \ 189238405Sjkim OBJ_cleanup(); EVP_cleanup(); ENGINE_cleanup(); \ 190238405Sjkim CRYPTO_cleanup_all_ex_data(); ERR_remove_thread_state(NULL); \ 191238405Sjkim ERR_free_strings(); zlib_cleanup();} while(0) 192111147Snectar# else 193238405Sjkim# define apps_startup() \ 194238405Sjkim do { do_pipe_sig(); CRYPTO_malloc_init(); \ 195111147Snectar ERR_load_crypto_strings(); OpenSSL_add_all_algorithms(); \ 196111147Snectar setup_ui_method(); } while(0) 197111147Snectar# define apps_shutdown() \ 198111147Snectar do { CONF_modules_unload(1); destroy_ui_method(); \ 199238405Sjkim OBJ_cleanup(); EVP_cleanup(); \ 200238405Sjkim CRYPTO_cleanup_all_ex_data(); ERR_remove_thread_state(NULL); \ 201238405Sjkim ERR_free_strings(); zlib_cleanup(); } while(0) 20255714Skris# endif 20355714Skris#endif 20455714Skris 205194206Ssimon#ifdef OPENSSL_SYSNAME_WIN32 206194206Ssimon# define openssl_fdset(a,b) FD_SET((unsigned int)a, b) 207194206Ssimon#else 208194206Ssimon# define openssl_fdset(a,b) FD_SET(a, b) 209194206Ssimon#endif 210194206Ssimon 211238405Sjkim 21255714Skristypedef struct args_st 21355714Skris { 21455714Skris char **data; 21555714Skris int count; 21655714Skris } ARGS; 21755714Skris 218109998Smarkm#define PW_MIN_LENGTH 4 219109998Smarkmtypedef struct pw_cb_data 220109998Smarkm { 221109998Smarkm const void *password; 222109998Smarkm const char *prompt_info; 223109998Smarkm } PW_CB_DATA; 224109998Smarkm 225109998Smarkmint password_callback(char *buf, int bufsiz, int verify, 226109998Smarkm PW_CB_DATA *cb_data); 227109998Smarkm 228109998Smarkmint setup_ui_method(void); 229109998Smarkmvoid destroy_ui_method(void); 230109998Smarkm 23155714Skrisint should_retry(int i); 23255714Skrisint args_from_file(char *file, int *argc, char **argv[]); 23355714Skrisint str2fmt(char *s); 23455714Skrisvoid program_name(char *in,char *out,int size); 23555714Skrisint chopup_args(ARGS *arg,char *buf, int *argc, char **argv[]); 23659191Skris#ifdef HEADER_X509_H 23759191Skrisint dump_cert_text(BIO *out, X509 *x); 238160814Ssimonvoid print_name(BIO *out, const char *title, X509_NAME *nm, unsigned long lflags); 23959191Skris#endif 240109998Smarkmint set_cert_ex(unsigned long *flags, const char *arg); 24168651Skrisint set_name_ex(unsigned long *flags, const char *arg); 242109998Smarkmint set_ext_copy(int *copy_type, const char *arg); 243109998Smarkmint copy_extensions(X509 *x, X509_REQ *req, int copy_type); 24459191Skrisint app_passwd(BIO *err, char *arg1, char *arg2, char **pass1, char **pass2); 245109998Smarkmint add_oid_section(BIO *err, CONF *conf); 246109998SmarkmX509 *load_cert(BIO *err, const char *file, int format, 247109998Smarkm const char *pass, ENGINE *e, const char *cert_descrip); 248109998SmarkmEVP_PKEY *load_key(BIO *err, const char *file, int format, int maybe_stdin, 249109998Smarkm const char *pass, ENGINE *e, const char *key_descrip); 250109998SmarkmEVP_PKEY *load_pubkey(BIO *err, const char *file, int format, int maybe_stdin, 251109998Smarkm const char *pass, ENGINE *e, const char *key_descrip); 252109998SmarkmSTACK_OF(X509) *load_certs(BIO *err, const char *file, int format, 253109998Smarkm const char *pass, ENGINE *e, const char *cert_descrip); 254238405SjkimSTACK_OF(X509_CRL) *load_crls(BIO *err, const char *file, int format, 255238405Sjkim const char *pass, ENGINE *e, const char *cert_descrip); 256109998SmarkmX509_STORE *setup_verify(BIO *bp, char *CAfile, char *CApath); 257111147Snectar#ifndef OPENSSL_NO_ENGINE 258109998SmarkmENGINE *setup_engine(BIO *err, const char *engine, int debug); 259111147Snectar#endif 26068651Skris 261194206Ssimon#ifndef OPENSSL_NO_OCSP 262194206SsimonOCSP_RESPONSE *process_responder(BIO *err, OCSP_REQUEST *req, 263194206Ssimon char *host, char *path, char *port, int use_ssl, 264238405Sjkim STACK_OF(CONF_VALUE) *headers, 265194206Ssimon int req_timeout); 266194206Ssimon#endif 267194206Ssimon 268109998Smarkmint load_config(BIO *err, CONF *cnf); 269109998Smarkmchar *make_config_name(void); 270109998Smarkm 271109998Smarkm/* Functions defined in ca.c and also used in ocsp.c */ 272109998Smarkmint unpack_revinfo(ASN1_TIME **prevtm, int *preason, ASN1_OBJECT **phold, 273160814Ssimon ASN1_GENERALIZEDTIME **pinvtm, const char *str); 274109998Smarkm 275127128Snectar#define DB_type 0 276127128Snectar#define DB_exp_date 1 277127128Snectar#define DB_rev_date 2 278127128Snectar#define DB_serial 3 /* index - unique */ 279127128Snectar#define DB_file 4 280127128Snectar#define DB_name 5 /* index - unique when active and not disabled */ 281127128Snectar#define DB_NUMBER 6 282127128Snectar 283127128Snectar#define DB_TYPE_REV 'R' 284127128Snectar#define DB_TYPE_EXP 'E' 285127128Snectar#define DB_TYPE_VAL 'V' 286127128Snectar 287127128Snectartypedef struct db_attr_st 288127128Snectar { 289127128Snectar int unique_subject; 290127128Snectar } DB_ATTR; 291127128Snectartypedef struct ca_db_st 292127128Snectar { 293127128Snectar DB_ATTR attributes; 294127128Snectar TXT_DB *db; 295127128Snectar } CA_DB; 296127128Snectar 297127128SnectarBIGNUM *load_serial(char *serialfile, int create, ASN1_INTEGER **retai); 298127128Snectarint save_serial(char *serialfile, char *suffix, BIGNUM *serial, ASN1_INTEGER **retai); 299127128Snectarint rotate_serial(char *serialfile, char *new_suffix, char *old_suffix); 300142425Snectarint rand_serial(BIGNUM *b, ASN1_INTEGER *ai); 301127128SnectarCA_DB *load_index(char *dbfile, DB_ATTR *dbattr); 302127128Snectarint index_index(CA_DB *db); 303160814Ssimonint save_index(const char *dbfile, const char *suffix, CA_DB *db); 304160814Ssimonint rotate_index(const char *dbfile, const char *new_suffix, const char *old_suffix); 305127128Snectarvoid free_index(CA_DB *db); 306238405Sjkim#define index_name_cmp_noconst(a, b) \ 307238405Sjkim index_name_cmp((const OPENSSL_CSTRING *)CHECKED_PTR_OF(OPENSSL_STRING, a), \ 308238405Sjkim (const OPENSSL_CSTRING *)CHECKED_PTR_OF(OPENSSL_STRING, b)) 309238405Sjkimint index_name_cmp(const OPENSSL_CSTRING *a, const OPENSSL_CSTRING *b); 310160814Ssimonint parse_yesno(const char *str, int def); 311127128Snectar 312160814SsimonX509_NAME *parse_name(char *str, long chtype, int multirdn); 313160814Ssimonint args_verify(char ***pargs, int *pargc, 314160814Ssimon int *badarg, BIO *err, X509_VERIFY_PARAM **pm); 315160814Ssimonvoid policies_print(BIO *out, X509_STORE_CTX *ctx); 316238405Sjkimint bio_to_mem(unsigned char **out, int maxlen, BIO *in); 317238405Sjkimint pkey_ctrl_string(EVP_PKEY_CTX *ctx, char *value); 318238405Sjkimint init_gen_str(BIO *err, EVP_PKEY_CTX **pctx, 319238405Sjkim const char *algname, ENGINE *e, int do_param); 320238405Sjkimint do_X509_sign(BIO *err, X509 *x, EVP_PKEY *pkey, const EVP_MD *md, 321238405Sjkim STACK_OF(OPENSSL_STRING) *sigopts); 322238405Sjkimint do_X509_REQ_sign(BIO *err, X509_REQ *x, EVP_PKEY *pkey, const EVP_MD *md, 323238405Sjkim STACK_OF(OPENSSL_STRING) *sigopts); 324238405Sjkimint do_X509_CRL_sign(BIO *err, X509_CRL *x, EVP_PKEY *pkey, const EVP_MD *md, 325238405Sjkim STACK_OF(OPENSSL_STRING) *sigopts); 326238405Sjkim#ifndef OPENSSL_NO_PSK 327238405Sjkimextern char *psk_key; 328238405Sjkim#endif 329194206Ssimon#ifndef OPENSSL_NO_JPAKE 330194206Ssimonvoid jpake_client_auth(BIO *out, BIO *conn, const char *secret); 331194206Ssimonvoid jpake_server_auth(BIO *out, BIO *conn, const char *secret); 332194206Ssimon#endif 333109998Smarkm 334238405Sjkim#if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG) 335238405Sjkimunsigned char *next_protos_parse(unsigned short *outlen, const char *in); 336238405Sjkim#endif /* !OPENSSL_NO_TLSEXT && !OPENSSL_NO_NEXTPROTONEG */ 337238405Sjkim 33855714Skris#define FORMAT_UNDEF 0 33955714Skris#define FORMAT_ASN1 1 34055714Skris#define FORMAT_TEXT 2 34155714Skris#define FORMAT_PEM 3 34255714Skris#define FORMAT_NETSCAPE 4 34368651Skris#define FORMAT_PKCS12 5 34468651Skris#define FORMAT_SMIME 6 345109998Smarkm#define FORMAT_ENGINE 7 346109998Smarkm#define FORMAT_IISSGC 8 /* XXX this stupid macro helps us to avoid 347109998Smarkm * adding yet another param to load_*key() */ 348238405Sjkim#define FORMAT_PEMRSA 9 /* PEM RSAPubicKey format */ 349238405Sjkim#define FORMAT_ASN1RSA 10 /* DER RSAPubicKey format */ 350238405Sjkim#define FORMAT_MSBLOB 11 /* MS Key blob format */ 351238405Sjkim#define FORMAT_PVK 12 /* MS PVK file format */ 35255714Skris 353109998Smarkm#define EXT_COPY_NONE 0 354109998Smarkm#define EXT_COPY_ADD 1 355109998Smarkm#define EXT_COPY_ALL 2 356109998Smarkm 35768651Skris#define NETSCAPE_CERT_HDR "certificate" 35868651Skris 35959191Skris#define APP_PASS_LEN 1024 36059191Skris 361142425Snectar#define SERIAL_RAND_BITS 64 362142425Snectar 363238405Sjkimint app_isdir(const char *); 364238405Sjkimint raw_read_stdin(void *,int); 365238405Sjkimint raw_write_stdout(const void *,int); 366238405Sjkim 367238405Sjkim#define TM_START 0 368238405Sjkim#define TM_STOP 1 369238405Sjkimdouble app_tminterval (int stop,int usertime); 370238405Sjkim 371238405Sjkim#define OPENSSL_NO_SSL_INTERN 372238405Sjkim 37355714Skris#endif 374