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