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