xref: /titanic_41/usr/src/lib/libresolv2/common/dst/support.c (revision 9525b14bcdeb5b5f6f95ab27c2f48f18bd2ec829)
1 static const char rcsid[] = "$Header: /proj/cvs/prod/libbind/dst/support.c,v 1.6 2005/10/11 00:10:13 marka Exp $";
2 
3 
4 /*
5  * Portions Copyright (c) 1995-1998 by Trusted Information Systems, Inc.
6  *
7  * Permission to use, copy modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND TRUSTED INFORMATION SYSTEMS
12  * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
13  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL
14  * TRUSTED INFORMATION SYSTEMS BE LIABLE FOR ANY SPECIAL, DIRECT,
15  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
16  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
17  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
18  * WITH THE USE OR PERFORMANCE OF THE SOFTWARE.
19  */
20 
21 #include "port_before.h"
22 
23 #include <stdio.h>
24 #include <unistd.h>
25 #include <memory.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <sys/stat.h>
29 #include <netinet/in.h>
30 #include <arpa/nameser.h>
31 #include <resolv.h>
32 
33 #include "dst_internal.h"
34 
35 #include "port_after.h"
36 
37 /*%
38  * dst_s_verify_str()
39  *     Validate that the input string(*str) is at the head of the input
40  *     buffer(**buf).  If so, move the buffer head pointer (*buf) to
41  *     the first byte of data following the string(*str).
42  * Parameters
43  *     buf     Input buffer.
44  *     str     Input string.
45  * Return
46  *	0       *str is not the head of **buff
47  *	1       *str is the head of **buff, *buf is is advanced to
48  *	the tail of **buf.
49  */
50 
51 int
dst_s_verify_str(const char ** buf,const char * str)52 dst_s_verify_str(const char **buf, const char *str)
53 {
54 	int b, s;
55 	if (*buf == NULL)	/*%< error checks */
56 		return (0);
57 	if (str == NULL || *str == '\0')
58 		return (1);
59 
60 	b = strlen(*buf);	/*%< get length of strings */
61 	s = strlen(str);
62 	if (s > b || strncmp(*buf, str, s))	/*%< check if same */
63 		return (0);	/*%< not a match */
64 	(*buf) += s;		/*%< advance pointer */
65 	return (1);
66 }
67 
68 /*%
69  * dst_s_calculate_bits
70  *     Given a binary number represented in a u_char[], determine
71  *     the number of significant bits used.
72  * Parameters
73  *     str       An input character string containing a binary number.
74  *     max_bits The maximum possible significant bits.
75  * Return
76  *     N       The number of significant bits in str.
77  */
78 
79 int
dst_s_calculate_bits(const u_char * str,const int max_bits)80 dst_s_calculate_bits(const u_char *str, const int max_bits)
81 {
82 	const u_char *p = str;
83 	u_char i, j = 0x80;
84 	int bits;
85 	for (bits = max_bits; *p == 0x00 && bits > 0; p++)
86 		bits -= 8;
87 	for (i = *p; (i & j) != j; j >>= 1)
88 		bits--;
89 	return (bits);
90 }
91 
92 /*%
93  * calculates a checksum used in dst for an id.
94  * takes an array of bytes and a length.
95  * returns a 16  bit checksum.
96  */
97 u_int16_t
dst_s_id_calc(const u_char * key,const int keysize)98 dst_s_id_calc(const u_char *key, const int keysize)
99 {
100 	u_int32_t ac;
101 	const u_char *kp = key;
102 	int size = keysize;
103 
104 	if (!key || (keysize <= 0))
105 		return (0xffffU);
106 
107 	for (ac = 0; size > 1; size -= 2, kp += 2)
108 		ac += ((*kp) << 8) + *(kp + 1);
109 
110 	if (size > 0)
111 		ac += ((*kp) << 8);
112 	ac += (ac >> 16) & 0xffff;
113 
114 	return (ac & 0xffff);
115 }
116 
117 /*%
118  * dst_s_dns_key_id() Function to calculate DNSSEC footprint from KEY record
119  *   rdata
120  * Input:
121  *	dns_key_rdata: the raw data in wire format
122  *      rdata_len: the size of the input data
123  * Output:
124  *      the key footprint/id calculated from the key data
125  */
126 u_int16_t
dst_s_dns_key_id(const u_char * dns_key_rdata,const int rdata_len)127 dst_s_dns_key_id(const u_char *dns_key_rdata, const int rdata_len)
128 {
129 	if (!dns_key_rdata)
130 		return 0;
131 
132 	/* compute id */
133 	if (dns_key_rdata[3] == KEY_RSA)	/*%< Algorithm RSA */
134 		return dst_s_get_int16((const u_char *)
135 				       &dns_key_rdata[rdata_len - 3]);
136 	else if (dns_key_rdata[3] == KEY_HMAC_MD5)
137 		/* compatibility */
138 		return 0;
139 	else
140 		/* compute a checksum on the key part of the key rr */
141 		return dst_s_id_calc(dns_key_rdata, rdata_len);
142 }
143 
144 /*%
145  * dst_s_get_int16
146  *     This routine extracts a 16 bit integer from a two byte character
147  *     string.  The character string is assumed to be in network byte
148  *     order and may be unaligned.  The number returned is in host order.
149  * Parameter
150  *     buf     A two byte character string.
151  * Return
152  *     The converted integer value.
153  */
154 
155 u_int16_t
dst_s_get_int16(const u_char * buf)156 dst_s_get_int16(const u_char *buf)
157 {
158 	register u_int16_t a = 0;
159 	a = ((u_int16_t)(buf[0] << 8)) | ((u_int16_t)(buf[1]));
160 	return (a);
161 }
162 
163 /*%
164  * dst_s_get_int32
165  *     This routine extracts a 32 bit integer from a four byte character
166  *     string.  The character string is assumed to be in network byte
167  *     order and may be unaligned.  The number returned is in host order.
168  * Parameter
169  *     buf     A four byte character string.
170  * Return
171  *     The converted integer value.
172  */
173 
174 u_int32_t
dst_s_get_int32(const u_char * buf)175 dst_s_get_int32(const u_char *buf)
176 {
177 	register u_int32_t a = 0;
178 	a = ((u_int32_t)(buf[0] << 24)) | ((u_int32_t)(buf[1] << 16)) |
179 		((u_int32_t)(buf[2] << 8)) | ((u_int32_t)(buf[3]));
180 	return (a);
181 }
182 
183 /*%
184  * dst_s_put_int16
185  *     Take a 16 bit integer and store the value in a two byte
186  *     character string.  The integer is assumed to be in network
187  *     order and the string is returned in host order.
188  *
189  * Parameters
190  *     buf     Storage for a two byte character string.
191  *     val     16 bit integer.
192  */
193 
194 void
dst_s_put_int16(u_int8_t * buf,const u_int16_t val)195 dst_s_put_int16(u_int8_t *buf, const u_int16_t val)
196 {
197 	buf[0] = (u_int8_t)(val >> 8);
198 	buf[1] = (u_int8_t)(val);
199 }
200 
201 /*%
202  * dst_s_put_int32
203  *     Take a 32 bit integer and store the value in a four byte
204  *     character string.  The integer is assumed to be in network
205  *     order and the string is returned in host order.
206  *
207  * Parameters
208  *     buf     Storage for a four byte character string.
209  *     val     32 bit integer.
210  */
211 
212 void
dst_s_put_int32(u_int8_t * buf,const u_int32_t val)213 dst_s_put_int32(u_int8_t *buf, const u_int32_t val)
214 {
215 	buf[0] = (u_int8_t)(val >> 24);
216 	buf[1] = (u_int8_t)(val >> 16);
217 	buf[2] = (u_int8_t)(val >> 8);
218 	buf[3] = (u_int8_t)(val);
219 }
220 
221 /*%
222  *  dst_s_filename_length
223  *
224  *	This function returns the number of bytes needed to hold the
225  *	filename for a key file.  '/', '\' and ':' are not allowed.
226  *	form:  K&lt;keyname&gt;+&lt;alg&gt;+&lt;id&gt;.&lt;suffix&gt;
227  *
228  *	Returns 0 if the filename would contain either '\', '/' or ':'
229  */
230 size_t
dst_s_filename_length(const char * name,const char * suffix)231 dst_s_filename_length(const char *name, const char *suffix)
232 {
233 	if (name == NULL)
234 		return (0);
235 	if (strrchr(name, '\\'))
236 		return (0);
237 	if (strrchr(name, '/'))
238 		return (0);
239 	if (strrchr(name, ':'))
240 		return (0);
241 	if (suffix == NULL)
242 		return (0);
243 	if (strrchr(suffix, '\\'))
244 		return (0);
245 	if (strrchr(suffix, '/'))
246 		return (0);
247 	if (strrchr(suffix, ':'))
248 		return (0);
249 	return (1 + strlen(name) + 6 + strlen(suffix));
250 }
251 
252 /*%
253  *  dst_s_build_filename ()
254  *	Builds a key filename from the key name, it's id, and a
255  *	suffix.  '\', '/' and ':' are not allowed. fA filename is of the
256  *	form:  K&lt;keyname&gt;&lt;id&gt;.&lt;suffix&gt;
257  *	form: K&lt;keyname&gt;+&lt;alg&gt;+&lt;id&gt;.&lt;suffix&gt;
258  *
259  *	Returns -1 if the conversion fails:
260  *	  if the filename would be too long for space allotted
261  *	  if the filename would contain a '\', '/' or ':'
262  *	Returns 0 on success
263  */
264 
265 int
dst_s_build_filename(char * filename,const char * name,u_int16_t id,int alg,const char * suffix,size_t filename_length)266 dst_s_build_filename(char *filename, const char *name, u_int16_t id,
267 		     int alg, const char *suffix, size_t filename_length)
268 {
269 	u_int32_t my_id;
270 	if (filename == NULL)
271 		return (-1);
272 	memset(filename, 0, filename_length);
273 	if (name == NULL)
274 		return (-1);
275 	if (suffix == NULL)
276 		return (-1);
277 	if (filename_length < 1 + strlen(name) + 4 + 6 + 1 + strlen(suffix))
278 		return (-1);
279 	my_id = id;
280 	sprintf(filename, "K%s+%03d+%05d.%s", name, alg, my_id,
281 		(const char *) suffix);
282 	if (strrchr(filename, '/'))
283 		return (-1);
284 	if (strrchr(filename, '\\'))
285 		return (-1);
286 	if (strrchr(filename, ':'))
287 		return (-1);
288 	return (0);
289 }
290 
291 /*%
292  *  dst_s_fopen ()
293  *     Open a file in the dst_path directory.  If perm is specified, the
294  *     file is checked for existence first, and not opened if it exists.
295  *  Parameters
296  *     filename  File to open
297  *     mode       Mode to open the file (passed directly to fopen)
298  *     perm       File permission, if creating a new file.
299  *  Returns
300  *     NULL       Failure
301  *     NON-NULL  (FILE *) of opened file.
302  */
303 FILE *
dst_s_fopen(const char * filename,const char * mode,int perm)304 dst_s_fopen(const char *filename, const char *mode, int perm)
305 {
306 	FILE *fp;
307 	char pathname[PATH_MAX];
308 
309 	if (strlen(filename) + strlen(dst_path) >= sizeof(pathname))
310 		return (NULL);
311 
312 	if (*dst_path != '\0') {
313 		strcpy(pathname, dst_path);
314 		strcat(pathname, filename);
315 	} else
316 		strcpy(pathname, filename);
317 
318 	fp = fopen(pathname, mode);
319 	if (perm)
320 		chmod(pathname, perm);
321 	return (fp);
322 }
323 
324 void
dst_s_dump(const int mode,const u_char * data,const int size,const char * msg)325 dst_s_dump(const int mode, const u_char *data, const int size,
326 	    const char *msg)
327 {
328 	UNUSED(data);
329 
330 	if (size > 0) {
331 #ifdef LONG_TEST
332 		static u_char scratch[1000];
333 		int n ;
334 		n = b64_ntop(data, scratch, size, sizeof(scratch));
335 		printf("%s: %x %d %s\n", msg, mode, n, scratch);
336 #else
337 		printf("%s,%x %d\n", msg, mode, size);
338 #endif
339 	}
340 }
341 
342 /*! \file */
343