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