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