]>
git.saurik.com Git - apple/libresolv.git/blob - dst_support.c
2 static const char rcsid
[] = "$Header: /Users/Shared/libresolv_2/libresolv/dst_support.c,v 1.1 2006/03/01 19:01:36 majka Exp $";
7 * Portions Copyright (c) 1995-1998 by Trusted Information Systems, Inc.
9 * Permission to use, copy modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND TRUSTED INFORMATION SYSTEMS
14 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
16 * TRUSTED INFORMATION SYSTEMS BE LIABLE FOR ANY SPECIAL, DIRECT,
17 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
18 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
19 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
20 * WITH THE USE OR PERFORMANCE OF THE SOFTWARE.
24 #include "port_before.h"
33 #include <netinet/in.h>
34 #include <arpa/nameser.h>
37 #include "dst_internal.h"
40 #include "port_after.h"
45 * Validate that the input string(*str) is at the head of the input
46 * buffer(**buf). If so, move the buffer head pointer (*buf) to
47 * the first byte of data following the string(*str).
52 * 0 *str is not the head of **buff
53 * 1 *str is the head of **buff, *buf is is advanced to
58 dst_s_verify_str(const char **buf
, const char *str
)
61 if (*buf
== NULL
) /* error checks */
63 if (str
== NULL
|| *str
== '\0')
66 b
= strlen(*buf
); /* get length of strings */
68 if (s
> b
|| strncmp(*buf
, str
, s
)) /* check if same */
69 return (0); /* not a match */
70 (*buf
) += s
; /* advance pointer */
76 * dst_s_calculate_bits
77 * Given a binary number represented in a u_char[], determine
78 * the number of significant bits used.
80 * str An input character string containing a binary number.
81 * max_bits The maximum possible significant bits.
83 * N The number of significant bits in str.
87 dst_s_calculate_bits(const u_char
*str
, const int max_bits
)
89 const u_char
*p
= str
;
92 for (bits
= max_bits
; *p
== 0x00 && bits
> 0; p
++)
94 for (i
= *p
; (i
& j
) != j
; j
>>= 1)
101 * calculates a checksum used in dst for an id.
102 * takes an array of bytes and a length.
103 * returns a 16 bit checksum.
109 dst_s_id_calc(const u_char
*key
, const int keysize
)
112 const u_char
*kp
= key
;
115 if (!key
|| (keysize
<= 0))
118 for (ac
= 0; size
> 1; size
-= 2, kp
+= 2)
119 ac
+= ((*kp
) << 8) + *(kp
+ 1);
123 ac
+= (ac
>> 16) & 0xffff;
125 return (ac
& 0xffff);
129 * dst_s_dns_key_id() Function to calculate DNSSEC footprint from KEY record
132 * dns_key_rdata: the raw data in wire format
133 * rdata_len: the size of the input data
135 * the key footprint/id calculated from the key data
138 dst_s_dns_key_id(const u_char
*dns_key_rdata
, const int rdata_len
)
144 if (dns_key_rdata
[3] == KEY_RSA
) /* Algorithm RSA */
145 return dst_s_get_int16((const u_char
*)
146 &dns_key_rdata
[rdata_len
- 3]);
147 else if (dns_key_rdata
[3] == KEY_HMAC_MD5
)
151 /* compute a checksum on the key part of the key rr */
152 return dst_s_id_calc(dns_key_rdata
, rdata_len
);
157 * This routine extracts a 16 bit integer from a two byte character
158 * string. The character string is assumed to be in network byte
159 * order and may be unaligned. The number returned is in host order.
161 * buf A two byte character string.
163 * The converted integer value.
167 dst_s_get_int16(const u_char
*buf
)
169 register u_int16_t a
= 0;
170 a
= ((u_int16_t
)(buf
[0] << 8)) | ((u_int16_t
)(buf
[1]));
178 * This routine extracts a 32 bit integer from a four byte character
179 * string. The character string is assumed to be in network byte
180 * order and may be unaligned. The number returned is in host order.
182 * buf A four byte character string.
184 * The converted integer value.
188 dst_s_get_int32(const u_char
*buf
)
190 register u_int32_t a
= 0;
191 a
= ((u_int32_t
)(buf
[0] << 24)) | ((u_int32_t
)(buf
[1] << 16)) |
192 ((u_int32_t
)(buf
[2] << 8)) | ((u_int32_t
)(buf
[3]));
199 * Take a 16 bit integer and store the value in a two byte
200 * character string. The integer is assumed to be in network
201 * order and the string is returned in host order.
204 * buf Storage for a two byte character string.
205 * val 16 bit integer.
209 dst_s_put_int16(u_int8_t
*buf
, const u_int16_t val
)
211 buf
[0] = (u_int8_t
)(val
>> 8);
212 buf
[1] = (u_int8_t
)(val
);
218 * Take a 32 bit integer and store the value in a four byte
219 * character string. The integer is assumed to be in network
220 * order and the string is returned in host order.
223 * buf Storage for a four byte character string.
224 * val 32 bit integer.
228 dst_s_put_int32(u_int8_t
*buf
, const u_int32_t val
)
230 buf
[0] = (u_int8_t
)(val
>> 24);
231 buf
[1] = (u_int8_t
)(val
>> 16);
232 buf
[2] = (u_int8_t
)(val
>> 8);
233 buf
[3] = (u_int8_t
)(val
);
240 * dst_s_filename_length
242 * This function returns the number of bytes needed to hold the
243 * filename for a key file. '/', '\' and ':' are not allowed.
244 * form: K<keyname>+<alg>+<id>.<suffix>
246 * Returns 0 if the filename would contain either '\', '/' or ':'
249 dst_s_filename_length(const char *name
, const char *suffix
)
253 if (strrchr(name
, '\\'))
255 if (strrchr(name
, '/'))
257 if (strrchr(name
, ':'))
261 if (strrchr(suffix
, '\\'))
263 if (strrchr(suffix
, '/'))
265 if (strrchr(suffix
, ':'))
267 return (1 + strlen(name
) + 6 + strlen(suffix
));
272 * dst_s_build_filename ()
273 * Builds a key filename from the key name, it's id, and a
274 * suffix. '\', '/' and ':' are not allowed. fA filename is of the
275 * form: K<keyname><id>.<suffix>
276 * form: K<keyname>+<alg>+<id>.<suffix>
278 * Returns -1 if the conversion fails:
279 * if the filename would be too long for space allotted
280 * if the filename would contain a '\', '/' or ':'
281 * Returns 0 on success
285 dst_s_build_filename(char *filename
, const char *name
, u_int16_t id
,
286 int alg
, const char *suffix
, size_t filename_length
)
289 if (filename
== NULL
)
291 memset(filename
, 0, filename_length
);
296 if (filename_length
< 1 + strlen(name
) + 4 + 6 + 1 + strlen(suffix
))
299 sprintf(filename
, "K%s+%03d+%05d.%s", name
, alg
, my_id
,
300 (const char *) suffix
);
301 if (strrchr(filename
, '/'))
303 if (strrchr(filename
, '\\'))
305 if (strrchr(filename
, ':'))
312 * Open a file in the dst_path directory. If perm is specified, the
313 * file is checked for existence first, and not opened if it exists.
315 * filename File to open
316 * mode Mode to open the file (passed directly to fopen)
317 * perm File permission, if creating a new file.
320 * NON-NULL (FILE *) of opened file.
323 dst_s_fopen(const char *filename
, const char *mode
, int perm
)
326 char pathname
[PATH_MAX
];
327 size_t plen
= sizeof(pathname
);
329 if (*dst_path
!= '\0') {
330 strcpy(pathname
, dst_path
);
331 plen
-= strlen(pathname
);
336 if (plen
> strlen(filename
))
337 strncpy(&pathname
[PATH_MAX
- plen
], filename
, plen
-1);
341 fp
= fopen(pathname
, mode
);
343 chmod(pathname
, perm
);
349 dst_s_dump(const int mode
, const u_char
*data
, const int size
,
358 static u_char scratch
[1000];
360 n
= b64_ntop(data
, scratch
, size
, sizeof(scratch
));
361 printf("%s: %x %d %s\n", msg
, mode
, n
, scratch
);
363 printf("%s,%x %d\n", msg
, mode
, size
);