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