1*75e1de3cSAugustin Cavalier /* $NetBSD: dst_internal.h,v 1.2 2012/11/16 02:11:05 joerg Exp $ */ 2*75e1de3cSAugustin Cavalier 3aa6411e2SAugustin Cavalier #ifndef DST_INTERNAL_H 4aa6411e2SAugustin Cavalier #define DST_INTERNAL_H 5aa6411e2SAugustin Cavalier 6aa6411e2SAugustin Cavalier /* 7aa6411e2SAugustin Cavalier * Portions Copyright (c) 1995-1998 by Trusted Information Systems, Inc. 8aa6411e2SAugustin Cavalier * 9aa6411e2SAugustin Cavalier * Permission to use, copy modify, and distribute this software for any 10aa6411e2SAugustin Cavalier * purpose with or without fee is hereby granted, provided that the above 11aa6411e2SAugustin Cavalier * copyright notice and this permission notice appear in all copies. 12aa6411e2SAugustin Cavalier * 13aa6411e2SAugustin Cavalier * THE SOFTWARE IS PROVIDED "AS IS" AND TRUSTED INFORMATION SYSTEMS 14aa6411e2SAugustin Cavalier * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL 15aa6411e2SAugustin Cavalier * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL 16aa6411e2SAugustin Cavalier * TRUSTED INFORMATION SYSTEMS BE LIABLE FOR ANY SPECIAL, DIRECT, 17aa6411e2SAugustin Cavalier * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING 18aa6411e2SAugustin Cavalier * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 19aa6411e2SAugustin Cavalier * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 20aa6411e2SAugustin Cavalier * WITH THE USE OR PERFORMANCE OF THE SOFTWARE. 21aa6411e2SAugustin Cavalier */ 22aa6411e2SAugustin Cavalier #include <limits.h> 23aa6411e2SAugustin Cavalier #include <sys/param.h> 24aa6411e2SAugustin Cavalier #if (!defined(BSD)) || (BSD < 199306) 25aa6411e2SAugustin Cavalier # include <sys/bitypes.h> 26aa6411e2SAugustin Cavalier #else 27aa6411e2SAugustin Cavalier # include <sys/types.h> 28aa6411e2SAugustin Cavalier #endif 29aa6411e2SAugustin Cavalier 30aa6411e2SAugustin Cavalier #ifndef PATH_MAX 31aa6411e2SAugustin Cavalier # ifdef POSIX_PATH_MAX 32aa6411e2SAugustin Cavalier # define PATH_MAX POSIX_PATH_MAX 33aa6411e2SAugustin Cavalier # else 34aa6411e2SAugustin Cavalier # define PATH_MAX 255 /*%< this is the value of POSIX_PATH_MAX */ 35aa6411e2SAugustin Cavalier # endif 36aa6411e2SAugustin Cavalier #endif 37aa6411e2SAugustin Cavalier 38aa6411e2SAugustin Cavalier typedef struct dst_key { 39aa6411e2SAugustin Cavalier char *dk_key_name; /*%< name of the key */ 40aa6411e2SAugustin Cavalier int dk_key_size; /*%< this is the size of the key in bits */ 41aa6411e2SAugustin Cavalier int dk_proto; /*%< what protocols this key can be used for */ 42aa6411e2SAugustin Cavalier int dk_alg; /*%< algorithm number from key record */ 43aa6411e2SAugustin Cavalier u_int32_t dk_flags; /*%< and the flags of the public key */ 44aa6411e2SAugustin Cavalier u_int16_t dk_id; /*%< identifier of the key */ 45aa6411e2SAugustin Cavalier void *dk_KEY_struct; /*%< pointer to key in crypto pkg fmt */ 46aa6411e2SAugustin Cavalier struct dst_func *dk_func; /*%< point to cryptto pgk specific function table */ 47aa6411e2SAugustin Cavalier } DST_KEY; 48aa6411e2SAugustin Cavalier #define HAS_DST_KEY 49aa6411e2SAugustin Cavalier 50aa6411e2SAugustin Cavalier #include <isc/dst.h> 51aa6411e2SAugustin Cavalier /* 52aa6411e2SAugustin Cavalier * define what crypto systems are supported for RSA, 53aa6411e2SAugustin Cavalier * BSAFE is prefered over RSAREF; only one can be set at any time 54aa6411e2SAugustin Cavalier */ 55aa6411e2SAugustin Cavalier #if defined(BSAFE) && defined(RSAREF) 56aa6411e2SAugustin Cavalier # error "Cannot have both BSAFE and RSAREF defined" 57aa6411e2SAugustin Cavalier #endif 58aa6411e2SAugustin Cavalier 59aa6411e2SAugustin Cavalier /* Declare dst_lib specific constants */ 60aa6411e2SAugustin Cavalier #define KEY_FILE_FORMAT "1.2" 61aa6411e2SAugustin Cavalier 62aa6411e2SAugustin Cavalier /* suffixes for key file names */ 63aa6411e2SAugustin Cavalier #define PRIVATE_KEY "private" 64aa6411e2SAugustin Cavalier #define PUBLIC_KEY "key" 65aa6411e2SAugustin Cavalier 66aa6411e2SAugustin Cavalier /* error handling */ 67*75e1de3cSAugustin Cavalier #ifdef DEBUG 68aa6411e2SAugustin Cavalier #define EREPORT(str) printf str 69aa6411e2SAugustin Cavalier #else 70*75e1de3cSAugustin Cavalier #define EREPORT(str) do {} while (/*CONSTCOND*/0) 71aa6411e2SAugustin Cavalier #endif 72aa6411e2SAugustin Cavalier 73aa6411e2SAugustin Cavalier /* use our own special macro to FRRE memory */ 74aa6411e2SAugustin Cavalier 75*75e1de3cSAugustin Cavalier #ifndef SAFE_FREE2 76*75e1de3cSAugustin Cavalier #define SAFE_FREE2(a, s) do { \ 77*75e1de3cSAugustin Cavalier if ((a) != NULL) { \ 78*75e1de3cSAugustin Cavalier memset((a), 0, (s)); \ 79*75e1de3cSAugustin Cavalier free((a)); \ 80*75e1de3cSAugustin Cavalier (a) = NULL; \ 81*75e1de3cSAugustin Cavalier } \ 82*75e1de3cSAugustin Cavalier } while (/*CONSTCOND*/0) 83*75e1de3cSAugustin Cavalier #endif 84*75e1de3cSAugustin Cavalier 85aa6411e2SAugustin Cavalier #ifndef SAFE_FREE 86*75e1de3cSAugustin Cavalier #define SAFE_FREE(a) SAFE_FREE2((a), sizeof(*(a))) 87aa6411e2SAugustin Cavalier #endif 88aa6411e2SAugustin Cavalier 89aa6411e2SAugustin Cavalier typedef struct dst_func { 90aa6411e2SAugustin Cavalier int (*sign)(const int mode, DST_KEY *key, void **context, 91aa6411e2SAugustin Cavalier const u_int8_t *data, const int len, 92aa6411e2SAugustin Cavalier u_int8_t *signature, const int sig_len); 93aa6411e2SAugustin Cavalier int (*verify)(const int mode, DST_KEY *key, void **context, 94aa6411e2SAugustin Cavalier const u_int8_t *data, const int len, 95aa6411e2SAugustin Cavalier const u_int8_t *signature, const int sig_len); 96aa6411e2SAugustin Cavalier int (*compare)(const DST_KEY *key1, const DST_KEY *key2); 97aa6411e2SAugustin Cavalier int (*generate)(DST_KEY *key, int parms); 98aa6411e2SAugustin Cavalier void *(*destroy)(void *key); 99aa6411e2SAugustin Cavalier /* conversion functions */ 100aa6411e2SAugustin Cavalier int (*to_dns_key)(const DST_KEY *key, u_int8_t *out, 101aa6411e2SAugustin Cavalier const int out_len); 102aa6411e2SAugustin Cavalier int (*from_dns_key)(DST_KEY *key, const u_int8_t *str, 103aa6411e2SAugustin Cavalier const int str_len); 104aa6411e2SAugustin Cavalier int (*to_file_fmt)(const DST_KEY *key, char *out, 105aa6411e2SAugustin Cavalier const int out_len); 106aa6411e2SAugustin Cavalier int (*from_file_fmt)(DST_KEY *key, const char *out, 107aa6411e2SAugustin Cavalier const int out_len); 108aa6411e2SAugustin Cavalier 109aa6411e2SAugustin Cavalier } dst_func; 110aa6411e2SAugustin Cavalier 111aa6411e2SAugustin Cavalier extern dst_func *dst_t_func[DST_MAX_ALGS]; 112aa6411e2SAugustin Cavalier extern const char *key_file_fmt_str; 113aa6411e2SAugustin Cavalier extern const char *dst_path; 114aa6411e2SAugustin Cavalier 115aa6411e2SAugustin Cavalier #ifndef DST_HASH_SIZE 116aa6411e2SAugustin Cavalier #define DST_HASH_SIZE 20 /*%< RIPEMD160 and SHA-1 are 20 bytes MD5 is 16 */ 117aa6411e2SAugustin Cavalier #endif 118aa6411e2SAugustin Cavalier 119aa6411e2SAugustin Cavalier int dst_bsafe_init(void); 120aa6411e2SAugustin Cavalier 121aa6411e2SAugustin Cavalier int dst_rsaref_init(void); 122aa6411e2SAugustin Cavalier 123aa6411e2SAugustin Cavalier int dst_hmac_md5_init(void); 124aa6411e2SAugustin Cavalier 125aa6411e2SAugustin Cavalier int dst_cylink_init(void); 126aa6411e2SAugustin Cavalier 127aa6411e2SAugustin Cavalier int dst_eay_dss_init(void); 128aa6411e2SAugustin Cavalier 129aa6411e2SAugustin Cavalier /* from higher level support routines */ 130aa6411e2SAugustin Cavalier int dst_s_calculate_bits( const u_int8_t *str, const int max_bits); 131aa6411e2SAugustin Cavalier int dst_s_verify_str( const char **buf, const char *str); 132aa6411e2SAugustin Cavalier 133aa6411e2SAugustin Cavalier 134aa6411e2SAugustin Cavalier /* conversion between dns names and key file names */ 135aa6411e2SAugustin Cavalier size_t dst_s_filename_length( const char *name, const char *suffix); 136aa6411e2SAugustin Cavalier int dst_s_build_filename( char *filename, const char *name, 137aa6411e2SAugustin Cavalier u_int16_t id, int alg, const char *suffix, 138aa6411e2SAugustin Cavalier size_t filename_length); 139aa6411e2SAugustin Cavalier 140aa6411e2SAugustin Cavalier FILE *dst_s_fopen (const char *filename, const char *mode, int perm); 141aa6411e2SAugustin Cavalier 142aa6411e2SAugustin Cavalier /*% 143aa6411e2SAugustin Cavalier * read and write network byte order into u_int?_t 144aa6411e2SAugustin Cavalier * all of these should be retired 145aa6411e2SAugustin Cavalier */ 146aa6411e2SAugustin Cavalier u_int16_t dst_s_get_int16( const u_int8_t *buf); 147aa6411e2SAugustin Cavalier void dst_s_put_int16( u_int8_t *buf, const u_int16_t val); 148aa6411e2SAugustin Cavalier 149aa6411e2SAugustin Cavalier u_int32_t dst_s_get_int32( const u_int8_t *buf); 150aa6411e2SAugustin Cavalier void dst_s_put_int32( u_int8_t *buf, const u_int32_t val); 151aa6411e2SAugustin Cavalier 152aa6411e2SAugustin Cavalier #ifdef DUMP 153aa6411e2SAugustin Cavalier # undef DUMP 154aa6411e2SAugustin Cavalier # define DUMP(a,b,c,d) dst_s_dump(a,b,c,d) 155aa6411e2SAugustin Cavalier #else 156aa6411e2SAugustin Cavalier # define DUMP(a,b,c,d) 157aa6411e2SAugustin Cavalier #endif 158aa6411e2SAugustin Cavalier void 159aa6411e2SAugustin Cavalier dst_s_dump(const int mode, const u_char *data, const int size, 160aa6411e2SAugustin Cavalier const char *msg); 161aa6411e2SAugustin Cavalier 162*75e1de3cSAugustin Cavalier #define KEY_FILE_FMT_STR "Private-key-format: v%s\nAlgorithm: %d (%s)\n" 163aa6411e2SAugustin Cavalier 164aa6411e2SAugustin Cavalier 165aa6411e2SAugustin Cavalier #endif /* DST_INTERNAL_H */ 166aa6411e2SAugustin Cavalier /*! \file */ 167