From: Willem Toorop Date: Fri, 19 Apr 2013 12:40:42 +0000 (+0000) Subject: Correct b{32,64}_{ntop,pton} handling... X-Git-Tag: release-1.6.17rc1~118 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9b85b673ed9036ea21667b459d8fcce42a7dd7c0;p=thirdparty%2Fldns.git Correct b{32,64}_{ntop,pton} handling... --- diff --git a/Makefile.in b/Makefile.in index 66a5dde3..11fd0406 100644 --- a/Makefile.in +++ b/Makefile.in @@ -689,8 +689,6 @@ zone.lo zone.o: $(srcdir)/zone.c ldns/config.h $(srcdir)/ldns/ldns.h ldns/util.h $(srcdir)/ldns/higher.h $(srcdir)/ldns/host2wire.h ldns/net.h $(srcdir)/ldns/str2host.h $(srcdir)/ldns/update.h \ $(srcdir)/ldns/wire2host.h $(srcdir)/ldns/rr_functions.h $(srcdir)/ldns/parse.h $(srcdir)/ldns/radix.h \ $(srcdir)/ldns/sha1.h $(srcdir)/ldns/sha2.h -compat/b32_ntop.lo compat/b32_ntop.o: $(srcdir)/compat/b32_ntop.c ldns/config.h -compat/b32_pton.lo compat/b32_pton.o: $(srcdir)/compat/b32_pton.c ldns/config.h compat/b64_ntop.lo compat/b64_ntop.o: $(srcdir)/compat/b64_ntop.c ldns/config.h compat/b64_pton.lo compat/b64_pton.o: $(srcdir)/compat/b64_pton.c ldns/config.h compat/calloc.lo compat/calloc.o: $(srcdir)/compat/calloc.c ldns/config.h @@ -749,7 +747,7 @@ examples/ldns-dpa.lo examples/ldns-dpa.o: $(srcdir)/examples/ldns-dpa.c ldns/con $(srcdir)/ldns/host2str.h $(srcdir)/ldns/dnssec_verify.h $(srcdir)/ldns/dnssec_sign.h $(srcdir)/ldns/duration.h \ $(srcdir)/ldns/higher.h $(srcdir)/ldns/host2wire.h ldns/net.h $(srcdir)/ldns/str2host.h $(srcdir)/ldns/update.h \ $(srcdir)/ldns/wire2host.h $(srcdir)/ldns/rr_functions.h $(srcdir)/ldns/parse.h $(srcdir)/ldns/radix.h \ - $(srcdir)/ldns/sha1.h $(srcdir)/ldns/sha2.h + $(srcdir)/ldns/sha1.h $(srcdir)/ldns/sha2.h $(srcdir)/examples/ldns-dpa.h examples/ldns-gen-zone.lo examples/ldns-gen-zone.o: $(srcdir)/examples/ldns-gen-zone.c ldns/config.h $(srcdir)/ldns/ldns.h \ ldns/util.h ldns/common.h $(srcdir)/ldns/buffer.h $(srcdir)/ldns/error.h $(srcdir)/ldns/dane.h \ $(srcdir)/ldns/rdata.h $(srcdir)/ldns/rr.h $(srcdir)/ldns/dname.h $(srcdir)/ldns/dnssec.h $(srcdir)/ldns/packet.h \ diff --git a/compat/b32_ntop.c b/compat/b32_ntop.c deleted file mode 100644 index 038ebdc9..00000000 --- a/compat/b32_ntop.c +++ /dev/null @@ -1,333 +0,0 @@ -/* - * Copyright (c) 1996, 1998 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS - * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE - * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL - * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR - * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS - * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS - * SOFTWARE. - */ - -/* - * Portions Copyright (c) 1995 by International Business Machines, Inc. - * - * International Business Machines, Inc. (hereinafter called IBM) grants - * permission under its copyrights to use, copy, modify, and distribute this - * Software with or without fee, provided that the above copyright notice and - * all paragraphs of this notice appear in all copies, and that the name of IBM - * not be used in connection with the marketing of any product incorporating - * the Software or modifications thereof, without specific, written prior - * permission. - * - * To the extent it has a right to do so, IBM grants an immunity from suit - * under its patents, if any, for the use, sale or manufacture of products to - * the extent that such products are used for performing Domain Name System - * dynamic updates in TCP/IP networks by means of the Software. No immunity is - * granted for any product per se or for any other function of any product. - * - * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES, - * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A - * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, - * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING - * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN - * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES. - */ -#include - -#include -#include -#ifdef HAVE_SYS_SOCKET_H -#include -#endif - -#ifdef HAVE_NETINET_IN_H -#include -#endif -#ifdef HAVE_ARPA_INET_H -#include -#endif - -#include -#include -#include -#include - -#include - -static const char Base32[] = - "abcdefghijklmnopqrstuvwxyz234567"; -/* "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";*/ -/* 00000000001111111111222222222233 - 01234567890123456789012345678901*/ -static const char Base32_extended_hex[] = -/* "0123456789ABCDEFGHIJKLMNOPQRSTUV";*/ - "0123456789abcdefghijklmnopqrstuv"; -static const char Pad32 = '='; - -/* (From RFC3548 and draft-josefsson-rfc3548bis-00.txt) -5. Base 32 Encoding - - The Base 32 encoding is designed to represent arbitrary sequences of - octets in a form that needs to be case insensitive but need not be - humanly readable. - - A 33-character subset of US-ASCII is used, enabling 5 bits to be - represented per printable character. (The extra 33rd character, "=", - is used to signify a special processing function.) - - The encoding process represents 40-bit groups of input bits as output - strings of 8 encoded characters. Proceeding from left to right, a - 40-bit input group is formed by concatenating 5 8bit input groups. - These 40 bits are then treated as 8 concatenated 5-bit groups, each - of which is translated into a single digit in the base 32 alphabet. - When encoding a bit stream via the base 32 encoding, the bit stream - must be presumed to be ordered with the most-significant-bit first. - That is, the first bit in the stream will be the high-order bit in - the first 8bit byte, and the eighth bit will be the low-order bit in - the first 8bit byte, and so on. - - Each 5-bit group is used as an index into an array of 32 printable - characters. The character referenced by the index is placed in the - output string. These characters, identified in Table 3, below, are - selected from US-ASCII digits and uppercase letters. - - Table 3: The Base 32 Alphabet - - Value Encoding Value Encoding Value Encoding Value Encoding - 0 A 9 J 18 S 27 3 - 1 B 10 K 19 T 28 4 - 2 C 11 L 20 U 29 5 - 3 D 12 M 21 V 30 6 - 4 E 13 N 22 W 31 7 - 5 F 14 O 23 X - 6 G 15 P 24 Y (pad) = - 7 H 16 Q 25 Z - 8 I 17 R 26 2 - - - Special processing is performed if fewer than 40 bits are available - at the end of the data being encoded. A full encoding quantum is - always completed at the end of a body. When fewer than 40 input bits - are available in an input group, zero bits are added (on the right) - to form an integral number of 5-bit groups. Padding at the end of - the data is performed using the "=" character. Since all base 32 - input is an integral number of octets, only the following cases can - arise: - - (1) the final quantum of encoding input is an integral multiple of 40 - bits; here, the final unit of encoded output will be an integral - multiple of 8 characters with no "=" padding, - - (2) the final quantum of encoding input is exactly 8 bits; here, the - final unit of encoded output will be two characters followed by six - "=" padding characters, - - (3) the final quantum of encoding input is exactly 16 bits; here, the - final unit of encoded output will be four characters followed by four - "=" padding characters, - - (4) the final quantum of encoding input is exactly 24 bits; here, the - final unit of encoded output will be five characters followed by - three "=" padding characters, or - - (5) the final quantum of encoding input is exactly 32 bits; here, the - final unit of encoded output will be seven characters followed by one - "=" padding character. - - -6. Base 32 Encoding with Extended Hex Alphabet - - The following description of base 32 is due to [7]. This encoding - should not be regarded as the same as the "base32" encoding, and - should not be referred to as only "base32". - - One property with this alphabet, that the base64 and base32 alphabet - lack, is that encoded data maintain its sort order when the encoded - data is compared bit-wise. - - This encoding is identical to the previous one, except for the - alphabet. The new alphabet is found in table 4. - - Table 4: The "Extended Hex" Base 32 Alphabet - - Value Encoding Value Encoding Value Encoding Value Encoding - 0 0 9 9 18 I 27 R - 1 1 10 A 19 J 28 S - 2 2 11 B 20 K 29 T - 3 3 12 C 21 L 30 U - 4 4 13 D 22 M 31 V - 5 5 14 E 23 N - 6 6 15 F 24 O (pad) = - 7 7 16 G 25 P - 8 8 17 H 26 Q - -*/ - - -int -ldns_b32_ntop_ar(uint8_t const *src, size_t srclength, char *target, size_t targsize, const char B32_ar[]) { - size_t datalength = 0; - uint8_t input[5]; - uint8_t output[8]; - size_t i; - memset(output, 0, 8); - - while (4 < srclength) { - input[0] = *src++; - input[1] = *src++; - input[2] = *src++; - input[3] = *src++; - input[4] = *src++; - srclength -= 5; - - output[0] = (input[0] & 0xf8) >> 3; - output[1] = ((input[0] & 0x07) << 2) + ((input[1] & 0xc0) >> 6); - output[2] = (input[1] & 0x3e) >> 1; - output[3] = ((input[1] & 0x01) << 4) + ((input[2] & 0xf0) >> 4); - output[4] = ((input[2] & 0x0f) << 1) + ((input[3] & 0x80) >> 7); - output[5] = (input[3] & 0x7c) >> 2; - output[6] = ((input[3] & 0x03) << 3) + ((input[4] & 0xe0) >> 5); - output[7] = (input[4] & 0x1f); - - assert(output[0] < 32); - assert(output[1] < 32); - assert(output[2] < 32); - assert(output[3] < 32); - assert(output[4] < 32); - assert(output[5] < 32); - assert(output[6] < 32); - assert(output[7] < 32); - - if (datalength + 8 > targsize) { - return (-1); - } - target[datalength++] = B32_ar[output[0]]; - target[datalength++] = B32_ar[output[1]]; - target[datalength++] = B32_ar[output[2]]; - target[datalength++] = B32_ar[output[3]]; - target[datalength++] = B32_ar[output[4]]; - target[datalength++] = B32_ar[output[5]]; - target[datalength++] = B32_ar[output[6]]; - target[datalength++] = B32_ar[output[7]]; - } - - /* Now we worry about padding. */ - if (0 != srclength) { - /* Get what's left. */ - input[0] = input[1] = input[2] = input[3] = input[4] = (uint8_t) '\0'; - for (i = 0; i < srclength; i++) - input[i] = *src++; - - output[0] = (input[0] & 0xf8) >> 3; - assert(output[0] < 32); - if (srclength >= 1) { - output[1] = ((input[0] & 0x07) << 2) + ((input[1] & 0xc0) >> 6); - assert(output[1] < 32); - output[2] = (input[1] & 0x3e) >> 1; - assert(output[2] < 32); - } - if (srclength >= 2) { - output[3] = ((input[1] & 0x01) << 4) + ((input[2] & 0xf0) >> 4); - assert(output[3] < 32); - } - if (srclength >= 3) { - output[4] = ((input[2] & 0x0f) << 1) + ((input[3] & 0x80) >> 7); - assert(output[4] < 32); - output[5] = (input[3] & 0x7c) >> 2; - assert(output[5] < 32); - } - if (srclength >= 4) { - output[6] = ((input[3] & 0x03) << 3) + ((input[4] & 0xe0) >> 5); - assert(output[6] < 32); - } - - - if (datalength + 1 > targsize) { - return (-2); - } - target[datalength++] = B32_ar[output[0]]; - if (srclength >= 1) { - if (datalength + 1 > targsize) { return (-2); } - target[datalength++] = B32_ar[output[1]]; - if (srclength == 1 && output[2] == 0) { - if (datalength + 1 > targsize) { return (-2); } - target[datalength++] = Pad32; - } else { - if (datalength + 1 > targsize) { return (-2); } - target[datalength++] = B32_ar[output[2]]; - } - } else { - if (datalength + 1 > targsize) { return (-2); } - target[datalength++] = Pad32; - if (datalength + 1 > targsize) { return (-2); } - target[datalength++] = Pad32; - } - if (srclength >= 2) { - if (datalength + 1 > targsize) { return (-2); } - target[datalength++] = B32_ar[output[3]]; - } else { - if (datalength + 1 > targsize) { return (-2); } - target[datalength++] = Pad32; - } - if (srclength >= 3) { - if (datalength + 1 > targsize) { return (-2); } - target[datalength++] = B32_ar[output[4]]; - if (srclength == 3 && output[5] == 0) { - if (datalength + 1 > targsize) { return (-2); } - target[datalength++] = Pad32; - } else { - if (datalength + 1 > targsize) { return (-2); } - target[datalength++] = B32_ar[output[5]]; - } - } else { - if (datalength + 1 > targsize) { return (-2); } - target[datalength++] = Pad32; - if (datalength + 1 > targsize) { return (-2); } - target[datalength++] = Pad32; - } - if (srclength >= 4) { - if (datalength + 1 > targsize) { return (-2); } - target[datalength++] = B32_ar[output[6]]; - } else { - if (datalength + 1 > targsize) { return (-2); } - target[datalength++] = Pad32; - } - if (datalength + 1 > targsize) { return (-2); } - target[datalength++] = Pad32; - } - if (datalength+1 > targsize) { - return (int) (datalength); - } - target[datalength] = '\0'; /* Returned value doesn't count \0. */ - return (int) (datalength); -} - -int -ldns_b32_ntop(uint8_t const *src, size_t srclength, char *target, size_t targsize) { - return ldns_b32_ntop_ar(src, srclength, target, targsize, Base32); -} - -/* deprecated, here for backwards compatibility */ -int -b32_ntop(uint8_t const *src, size_t srclength, char *target, size_t targsize) { - return ldns_b32_ntop_ar(src, srclength, target, targsize, Base32); -} - -int -ldns_b32_ntop_extended_hex(uint8_t const *src, size_t srclength, char *target, size_t targsize) { - return ldns_b32_ntop_ar(src, srclength, target, targsize, Base32_extended_hex); -} - -/* deprecated, here for backwards compatibility */ -int -b32_ntop_extended_hex(uint8_t const *src, size_t srclength, char *target, size_t targsize) { - return ldns_b32_ntop_ar(src, srclength, target, targsize, Base32_extended_hex); -} - diff --git a/compat/b32_pton.c b/compat/b32_pton.c deleted file mode 100644 index 9c261e61..00000000 --- a/compat/b32_pton.c +++ /dev/null @@ -1,387 +0,0 @@ -/* - * Copyright (c) 1996, 1998 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS - * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE - * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL - * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR - * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS - * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS - * SOFTWARE. - */ - -/* - * Portions Copyright (c) 1995 by International Business Machines, Inc. - * - * International Business Machines, Inc. (hereinafter called IBM) grants - * permission under its copyrights to use, copy, modify, and distribute this - * Software with or without fee, provided that the above copyright notice and - * all paragraphs of this notice appear in all copies, and that the name of IBM - * not be used in connection with the marketing of any product incorporating - * the Software or modifications thereof, without specific, written prior - * permission. - * - * To the extent it has a right to do so, IBM grants an immunity from suit - * under its patents, if any, for the use, sale or manufacture of products to - * the extent that such products are used for performing Domain Name System - * dynamic updates in TCP/IP networks by means of the Software. No immunity is - * granted for any product per se or for any other function of any product. - * - * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES, - * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A - * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, - * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING - * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN - * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES. - */ -#include - -#include -#include -#ifdef HAVE_SYS_SOCKET_H -#include -#endif - -#ifdef HAVE_NETINET_IN_H -#include -#endif -#ifdef HAVE_ARPA_INET_H -#include -#endif - -#include -#include -#include -#include - -/* "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";*/ -static const char Base32[] = - "abcdefghijklmnopqrstuvwxyz234567"; -/* "0123456789ABCDEFGHIJKLMNOPQRSTUV";*/ -static const char Base32_extended_hex[] = - "0123456789abcdefghijklmnopqrstuv"; -static const char Pad32 = '='; - -/* (From RFC1521 and draft-ietf-dnssec-secext-03.txt) -5. Base 32 Encoding - - The Base 32 encoding is designed to represent arbitrary sequences of - octets in a form that needs to be case insensitive but need not be - humanly readable. - - A 33-character subset of US-ASCII is used, enabling 5 bits to be - represented per printable character. (The extra 33rd character, "=", - is used to signify a special processing function.) - - The encoding process represents 40-bit groups of input bits as output - strings of 8 encoded characters. Proceeding from left to right, a - 40-bit input group is formed by concatenating 5 8bit input groups. - These 40 bits are then treated as 8 concatenated 5-bit groups, each - of which is translated into a single digit in the base 32 alphabet. - When encoding a bit stream via the base 32 encoding, the bit stream - must be presumed to be ordered with the most-significant-bit first. - That is, the first bit in the stream will be the high-order bit in - the first 8bit byte, and the eighth bit will be the low-order bit in - the first 8bit byte, and so on. - - Each 5-bit group is used as an index into an array of 32 printable - characters. The character referenced by the index is placed in the - output string. These characters, identified in Table 3, below, are - selected from US-ASCII digits and uppercase letters. - - Table 3: The Base 32 Alphabet - - Value Encoding Value Encoding Value Encoding Value Encoding - 0 A 9 J 18 S 27 3 - 1 B 10 K 19 T 28 4 - 2 C 11 L 20 U 29 5 - 3 D 12 M 21 V 30 6 - 4 E 13 N 22 W 31 7 - 5 F 14 O 23 X - 6 G 15 P 24 Y (pad) = - 7 H 16 Q 25 Z - 8 I 17 R 26 2 - - - Special processing is performed if fewer than 40 bits are available - at the end of the data being encoded. A full encoding quantum is - always completed at the end of a body. When fewer than 40 input bits - are available in an input group, zero bits are added (on the right) - to form an integral number of 5-bit groups. Padding at the end of - the data is performed using the "=" character. Since all base 32 - input is an integral number of octets, only the following cases can - arise: - - (1) the final quantum of encoding input is an integral multiple of 40 - bits; here, the final unit of encoded output will be an integral - multiple of 8 characters with no "=" padding, - - (2) the final quantum of encoding input is exactly 8 bits; here, the - final unit of encoded output will be two characters followed by six - "=" padding characters, - - (3) the final quantum of encoding input is exactly 16 bits; here, the - final unit of encoded output will be four characters followed by four - "=" padding characters, - - (4) the final quantum of encoding input is exactly 24 bits; here, the - final unit of encoded output will be five characters followed by - three "=" padding characters, or - - (5) the final quantum of encoding input is exactly 32 bits; here, the - final unit of encoded output will be seven characters followed by one - "=" padding character. - - -6. Base 32 Encoding with Extended Hex Alphabet - - The following description of base 32 is due to [7]. This encoding - should not be regarded as the same as the "base32" encoding, and - should not be referred to as only "base32". - - One property with this alphabet, that the base32 and base32 alphabet - lack, is that encoded data maintain its sort order when the encoded - data is compared bit-wise. - - This encoding is identical to the previous one, except for the - alphabet. The new alphabet is found in table 4. - - Table 4: The "Extended Hex" Base 32 Alphabet - - Value Encoding Value Encoding Value Encoding Value Encoding - 0 0 9 9 18 I 27 R - 1 1 10 A 19 J 28 S - 2 2 11 B 20 K 29 T - 3 3 12 C 21 L 30 U - 4 4 13 D 22 M 31 V - 5 5 14 E 23 N - 6 6 15 F 24 O (pad) = - 7 7 16 G 25 P - 8 8 17 H 26 Q - - - - -*/ -/* skips all whitespace anywhere. - converts characters, four at a time, starting at (or after) - src from base - 32 numbers into three 8 bit bytes in the target area. - it returns the number of data bytes stored at the target, or -1 on error. - */ - -int -ldns_b32_pton_ar(char const *src, size_t hashed_owner_str_len, uint8_t *target, size_t targsize, const char B32_ar[]) -{ - int tarindex, state, ch; - char *pos; - int i = 0; - - state = 0; - tarindex = 0; - - while ((ch = *src++) != '\0' && (i == 0 || i < (int) hashed_owner_str_len)) { - i++; - ch = tolower(ch); - if (isspace((unsigned char)ch)) /* Skip whitespace anywhere. */ - continue; - - if (ch == Pad32) - break; - - pos = strchr(B32_ar, ch); - if (pos == 0) { - /* A non-base32 character. */ - return (-ch); - } - - switch (state) { - case 0: - if (target) { - if ((size_t)tarindex >= targsize) { - return (-2); - } - target[tarindex] = (pos - B32_ar) << 3; - } - state = 1; - break; - case 1: - if (target) { - if ((size_t)tarindex + 1 >= targsize) { - return (-3); - } - target[tarindex] |= (pos - B32_ar) >> 2; - target[tarindex+1] = ((pos - B32_ar) & 0x03) - << 6 ; - } - tarindex++; - state = 2; - break; - case 2: - if (target) { - if ((size_t)tarindex + 1 >= targsize) { - return (-4); - } - target[tarindex] |= (pos - B32_ar) << 1; - } - /*tarindex++;*/ - state = 3; - break; - case 3: - if (target) { - if ((size_t)tarindex + 1 >= targsize) { - return (-5); - } - target[tarindex] |= (pos - B32_ar) >> 4; - target[tarindex+1] = ((pos - B32_ar) & 0x0f) << 4 ; - } - tarindex++; - state = 4; - break; - case 4: - if (target) { - if ((size_t)tarindex + 1 >= targsize) { - return (-6); - } - target[tarindex] |= (pos - B32_ar) >> 1; - target[tarindex+1] = ((pos - B32_ar) & 0x01) - << 7 ; - } - tarindex++; - state = 5; - break; - case 5: - if (target) { - if ((size_t)tarindex + 1 >= targsize) { - return (-7); - } - target[tarindex] |= (pos - B32_ar) << 2; - } - state = 6; - break; - case 6: - if (target) { - if ((size_t)tarindex + 1 >= targsize) { - return (-8); - } - target[tarindex] |= (pos - B32_ar) >> 3; - target[tarindex+1] = ((pos - B32_ar) & 0x07) - << 5 ; - } - tarindex++; - state = 7; - break; - case 7: - if (target) { - if ((size_t)tarindex + 1 >= targsize) { - return (-9); - } - target[tarindex] |= (pos - B32_ar); - } - tarindex++; - state = 0; - break; - default: - abort(); - } - } - - /* - * We are done decoding Base-32 chars. Let's see if we ended - * on a byte boundary, and/or with erroneous trailing characters. - */ - - if (ch == Pad32) { /* We got a pad char. */ - ch = *src++; /* Skip it, get next. */ - switch (state) { - case 0: /* Invalid = in first position */ - case 1: /* Invalid = in second position */ - return (-10); - - case 2: /* Valid, means one byte of info */ - case 3: - /* Skip any number of spaces. */ - for ((void)NULL; ch != '\0'; ch = *src++) - if (!isspace((unsigned char)ch)) - break; - /* Make sure there is another trailing = sign. */ - if (ch != Pad32) { - return (-11); - } - ch = *src++; /* Skip the = */ - /* Fall through to "single trailing =" case. */ - /* FALLTHROUGH */ - - case 4: /* Valid, means two bytes of info */ - case 5: - case 6: - /* - * We know this char is an =. Is there anything but - * whitespace after it? - */ - for ((void)NULL; ch != '\0'; ch = *src++) - if (!(isspace((unsigned char)ch) || ch == '=')) { - return (-12); - } - - case 7: /* Valid, means three bytes of info */ - /* - * We know this char is an =. Is there anything but - * whitespace after it? - */ - for ((void)NULL; ch != '\0'; ch = *src++) - if (!isspace((unsigned char)ch)) { - return (-13); - } - - /* - * Now make sure for cases 2 and 3 that the "extra" - * bits that slopped past the last full byte were - * zeros. If we don't check them, they become a - * subliminal channel. - */ - if (target && target[tarindex] != 0) { - return (-14); - } - } - } else { - /* - * We ended by seeing the end of the string. Make sure we - * have no partial bytes lying around. - */ - if (state != 0) - return (-15); - } - - return (tarindex); -} - -int -ldns_b32_pton(char const *src, size_t hashed_owner_str_len, uint8_t *target, size_t targsize) -{ - return ldns_b32_pton_ar(src, hashed_owner_str_len, target, targsize, Base32); -} - -/* deprecated, here for backwards compatibility */ -int -b32_pton(char const *src, size_t hashed_owner_str_len, uint8_t *target, size_t targsize) -{ - return ldns_b32_pton_ar(src, hashed_owner_str_len, target, targsize, Base32); -} - -int -ldns_b32_pton_extended_hex(char const *src, size_t hashed_owner_str_len, uint8_t *target, size_t targsize) -{ - return ldns_b32_pton_ar(src, hashed_owner_str_len, target, targsize, Base32_extended_hex); -} - -/* deprecated, here for backwards compatibility */ -int -b32_pton_extended_hex(char const *src, size_t hashed_owner_str_len, uint8_t *target, size_t targsize) -{ - return ldns_b32_pton_ar(src, hashed_owner_str_len, target, targsize, Base32_extended_hex); -} diff --git a/compat/b64_ntop.c b/compat/b64_ntop.c index d0b52b51..6895acaf 100644 --- a/compat/b64_ntop.c +++ b/compat/b64_ntop.c @@ -40,27 +40,10 @@ * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES. */ #include - -#include -#include -#ifdef HAVE_SYS_SOCKET_H -#include -#endif - -#ifdef HAVE_NETINET_IN_H -#include -#endif -#ifdef HAVE_ARPA_INET_H -#include -#endif - #include -#include #include #include -#define Assert(Cond) if (!(Cond)) abort() - static const char Base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; static const char Pad64 = '='; @@ -154,10 +137,10 @@ ldns_b64_ntop(uint8_t const *src, size_t srclength, char *target, size_t targsiz output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4); output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6); output[3] = input[2] & 0x3f; - Assert(output[0] < 64); - Assert(output[1] < 64); - Assert(output[2] < 64); - Assert(output[3] < 64); + assert(output[0] < 64); + assert(output[1] < 64); + assert(output[2] < 64); + assert(output[3] < 64); if (datalength + 4 > targsize) { return (-1); @@ -178,9 +161,9 @@ ldns_b64_ntop(uint8_t const *src, size_t srclength, char *target, size_t targsiz output[0] = input[0] >> 2; output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4); output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6); - Assert(output[0] < 64); - Assert(output[1] < 64); - Assert(output[2] < 64); + assert(output[0] < 64); + assert(output[1] < 64); + assert(output[2] < 64); if (datalength + 4 > targsize) { return (-2); diff --git a/compat/b64_pton.c b/compat/b64_pton.c index aa637d22..abe32819 100644 --- a/compat/b64_pton.c +++ b/compat/b64_pton.c @@ -40,27 +40,10 @@ * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES. */ #include - -#include -#include -#ifdef HAVE_SYS_SOCKET_H -#include -#endif - -#ifdef HAVE_NETINET_IN_H -#include -#endif -#ifdef HAVE_ARPA_INET_H -#include -#endif - #include -#include #include #include -#define Assert(Cond) if (!(Cond)) abort() - static const char Base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; static const char Pad64 = '='; diff --git a/configure.ac b/configure.ac index 401debc6..46be5e3a 100644 --- a/configure.ac +++ b/configure.ac @@ -500,8 +500,6 @@ AC_FUNC_REALLOC AC_REPLACE_FUNCS(b64_pton) AC_REPLACE_FUNCS(b64_ntop) -AC_REPLACE_FUNCS(b32_pton) -AC_REPLACE_FUNCS(b32_ntop) AC_REPLACE_FUNCS(calloc) AC_REPLACE_FUNCS(timegm) AC_REPLACE_FUNCS(gmtime_r) @@ -516,7 +514,17 @@ AC_REPLACE_FUNCS(snprintf) AC_REPLACE_FUNCS(strlcpy) AC_REPLACE_FUNCS(memmove) AC_FUNC_FORK -AC_CHECK_FUNCS([endprotoent endservent sleep random fcntl strtoul bzero memset]) +AC_CHECK_FUNCS([endprotoent endservent sleep random fcntl strtoul bzero memset b32_ntop b32_pton]) +if test "x$HAVE_B32_NTOP" = "xyes"; then + AC_SUBST(ldns_build_config_have_b32_ntop, 1) +else + AC_SUBST(ldns_build_config_have_b32_ntop, 0) +fi +if test "x$HAVE_B32_PTON" = "xyes"; then + AC_SUBST(ldns_build_config_have_b32_pton, 1) +else + AC_SUBST(ldns_build_config_have_b32_pton, 0) +fi ACX_CHECK_GETADDRINFO_WITH_INCLUDES if test $ac_cv_func_getaddrinfo = no; then @@ -637,7 +645,6 @@ AH_BOTTOM([ extern "C" { #endif -#ifndef B64_PTON int ldns_b64_ntop(uint8_t const *src, size_t srclength, char *target, size_t targsize); /** @@ -648,8 +655,6 @@ static inline size_t ldns_b64_ntop_calculate_size(size_t srcsize) { return ((((srcsize + 2) / 3) * 4) + 1); } -#endif /* !B64_PTON */ -#ifndef B64_NTOP int ldns_b64_pton(char const *src, uint8_t *target, size_t targsize); /** * calculates the size needed to store the result of ldns_b64_pton @@ -659,7 +664,12 @@ static inline size_t ldns_b64_pton_calculate_size(size_t srcsize) { return (((((srcsize + 3) / 4) * 3)) + 1); } -#endif /* !B64_NTOP */ + +/** + * Given in dnssec_zone.c, also used in dnssec_sign.c:w + + */ +int ldns_dname_compare_v(const void *a, const void *b); #ifndef HAVE_SLEEP /* use windows sleep, in millisecs, instead */ diff --git a/dnssec_sign.c b/dnssec_sign.c index 2e5a7a92..abce4852 100644 --- a/dnssec_sign.c +++ b/dnssec_sign.c @@ -771,9 +771,6 @@ ldns_dnssec_zone_create_nsecs(ldns_dnssec_zone *zone, } #ifdef HAVE_SSL -/* in dnssec_zone.c */ -extern int ldns_dname_compare_v(const void *a, const void *b); - static ldns_status ldns_dnssec_zone_create_nsec3s_mkmap(ldns_dnssec_zone *zone, ldns_rr_list *new_rrs, diff --git a/ldns/common.h.in b/ldns/common.h.in index b3614f18..8bf9654b 100644 --- a/ldns/common.h.in +++ b/ldns/common.h.in @@ -25,6 +25,8 @@ #define LDNS_BUILD_CONFIG_HAVE_ATTR_UNUSED @ldns_build_config_have_attr_unused@ #define LDNS_BUILD_CONFIG_HAVE_SOCKLEN_T @ldns_build_config_have_socklen_t@ #define LDNS_BUILD_CONFIG_USE_DANE @ldns_build_config_use_dane@ +#define LDNS_BUILD_CONFIG_HAVE_B32_PTON @ldns_build_config_have_b32_pton@ +#define LDNS_BUILD_CONFIG_HAVE_B32_NTOP @ldns_build_config_have_b32_ntop@ /* * HAVE_STDBOOL_H is not available when distributed as a library, but no build diff --git a/ldns/util.h.in b/ldns/util.h.in index fe4ff372..b6c9abe0 100644 --- a/ldns/util.h.in +++ b/ldns/util.h.in @@ -325,42 +325,66 @@ uint16_t ldns_get_random(void); */ char *ldns_bubblebabble(uint8_t *data, size_t len); -#ifndef B32_NTOP -int ldns_b32_ntop(uint8_t const *src, size_t srclength, - char *target, size_t targsize); -int b32_ntop(uint8_t const *src, size_t srclength, - char *target, size_t targsize); -int ldns_b32_ntop_extended_hex(uint8_t const *src, size_t srclength, - char *target, size_t targsize); -int b32_ntop_extended_hex(uint8_t const *src, size_t srclength, - char *target, size_t targsize); + +INLINE time_t ldns_time(time_t *t) { return time(t); } + + /** * calculates the size needed to store the result of b32_ntop */ /*@unused@*/ -INLINE size_t ldns_b32_ntop_calculate_size(size_t srcsize) +INLINE size_t ldns_b32_ntop_calculate_size(size_t src_data_length) { - size_t result = ((((srcsize / 5) * 8) - 2) + 2); - return result; + return src_data_length == 0 ? 0 : ((src_data_length - 1) / 5 + 1) * 8; +} + +INLINE size_t ldns_b32_ntop_calculate_size_no_padding(size_t src_data_length) +{ + return ((src_data_length + 3) * 8 / 5) - 4; } -#endif /* !B32_NTOP */ -#ifndef B32_PTON -int ldns_b32_pton(char const *src, size_t hashed_owner_str_len, uint8_t *target, size_t targsize); -int b32_pton(char const *src, size_t hashed_owner_str_len, uint8_t *target, size_t targsize); -int ldns_b32_pton_extended_hex(char const *src, size_t hashed_owner_str_len, uint8_t *target, size_t targsize); -int b32_pton_extended_hex(char const *src, size_t hashed_owner_str_len, uint8_t *target, size_t targsize); + +int ldns_b32_ntop(const uint8_t* src_data, size_t src_data_length, + char* target_text_buffer, size_t target_text_buffer_size); + +int ldns_b32_ntop_extended_hex(const uint8_t* src_data, size_t src_data_length, + char* target_text_buffer, size_t target_text_buffer_size); + +#if ! LDNS_BUILD_CONFIG_HAVE_B32_NTOP + +int b32_ntop(const uint8_t* src_data, size_t src_data_length, + char* target_text_buffer, size_t target_text_buffer_size); + +int b32_ntop_extended_hex(const uint8_t* src_data, size_t src_data_length, + char* target_text_buffer, size_t target_text_buffer_size); + +#endif /* ! LDNS_BUILD_CONFIG_HAVE_B32_NTOP */ + + /** * calculates the size needed to store the result of b32_pton */ /*@unused@*/ -INLINE size_t ldns_b32_pton_calculate_size(size_t srcsize) +INLINE size_t ldns_b32_pton_calculate_size(size_t src_text_length) { - size_t result = ((((srcsize) / 8) * 5)); - return result; + return src_text_length * 5 / 8; } -#endif /* !B32_PTON */ -INLINE time_t ldns_time(time_t *t) { return time(t); } +int ldns_b32_pton(const char* src_text, size_t src_text_length, + uint8_t* target_data_buffer, size_t target_data_buffer_size); + +int ldns_b32_pton_extended_hex(const char* src_text, size_t src_text_length, + uint8_t* target_data_buffer, size_t target_data_buffer_size); + +#if ! LDNS_BUILD_CONFIG_HAVE_B32_PTON + +int b32_pton(const char* src_text, size_t src_text_length, + uint8_t* target_data_buffer, size_t target_data_buffer_size); + +int b32_pton_extended_hex(const char* src_text, size_t src_text_length, + uint8_t* target_data_buffer, size_t target_data_buffer_size); + +#endif /* ! LDNS_BUILD_CONFIG_HAVE_B32_PTON */ + #ifdef __cplusplus } diff --git a/util.c b/util.c index 0439fbe2..435cc344 100644 --- a/util.c +++ b/util.c @@ -20,6 +20,7 @@ #include #include #include +#include #ifdef HAVE_SSL #include @@ -460,3 +461,313 @@ ldns_bubblebabble(uint8_t *data, size_t len) retval[j++] = '\0'; return retval; } + +/* + * For backwards compatibility, because we have always exported this symbol. + */ +#ifdef HAVE_B64_NTOP +int ldns_b64_ntop(const uint8_t* src, size_t srclength, + char *target, size_t targsize); +{ + return b64_ntop(src, srclength, target, targsize); +} +#endif + +/* + * For backwards compatibility, because we have always exported this symbol. + */ +#ifdef HAVE_B64_PTON +int ldns_b64_pton(const char* src, uint8_t *target, size_t targsize) +{ + return b64_pton(src, target, targsize); +} +#endif + + +static int +ldns_b32_ntop_base(const uint8_t* src, size_t src_sz, + char* dst, size_t dst_sz, + bool extended_hex, bool add_padding) +{ + size_t ret_sz; + const char* b32 = extended_hex ? "0123456789abcdefghijklmnopqrstuv" + : "abcdefghijklmnopqrstuvwxyz234567"; + + size_t c = 0; /* c is used to carry partial base32 character over + * byte boundaries for sizes with a remainder. + * (i.e. src_sz % 5 != 0) + */ + + ret_sz = add_padding ? ldns_b32_ntop_calculate_size(src_sz) + : ldns_b32_ntop_calculate_size_no_padding(src_sz); + + /* Do we have enough space? */ + if (dst_sz < ret_sz + 1) + return -1; + + /* We know the size; terminate the string */ + dst[ret_sz + 1] = '\0'; + + /* First process all chunks of five */ + while (src_sz >= 5) { + /* 00000... ........ ........ ........ ........ */ + dst[0] = b32[(src[0] ) >> 3]; + + /* .....111 11...... ........ ........ ........ */ + dst[1] = b32[(src[0] & 0x07) << 2 | src[1] >> 6]; + + /* ........ ..22222. ........ ........ ........ */ + dst[2] = b32[(src[1] & 0x3e) >> 1]; + + /* ........ .......3 3333.... ........ ........ */ + dst[3] = b32[(src[1] & 0x01) << 4 | src[2] >> 4]; + + /* ........ ........ ....4444 4....... ........ */ + dst[4] = b32[(src[2] & 0x0f) << 1 | src[3] >> 7]; + + /* ........ ........ ........ .55555.. ........ */ + dst[5] = b32[(src[3] & 0x7c) >> 2]; + + /* ........ ........ ........ ......66 666..... */ + dst[6] = b32[(src[3] & 0x03) << 3 | src[4] >> 5]; + + /* ........ ........ ........ ........ ...77777 */ + dst[7] = b32[(src[4] & 0x1f) ]; + + src_sz -= 5; + src += 5; + dst += 8; + } + /* Process what remains */ + switch (src_sz) { + case 4: /* ........ ........ ........ ......66 666..... */ + dst[6] = b32[(src[3] & 0x03) << 3]; + + /* ........ ........ ........ .55555.. ........ */ + dst[5] = b32[(src[3] & 0x7c) >> 2]; + + /* ........ ........ ....4444 4....... ........ */ + c = src[3] >> 7 ; + case 3: dst[4] = b32[(src[2] & 0x0f) << 1 | c]; + + /* ........ .......3 3333.... ........ ........ */ + c = src[2] >> 4 ; + case 2: dst[3] = b32[(src[1] & 0x01) << 4 | c]; + + /* ........ ..22222. ........ ........ ........ */ + dst[2] = b32[(src[1] & 0x3e) >> 1]; + + /* .....111 11...... ........ ........ ........ */ + c = src[1] >> 6 ; + case 1: dst[1] = b32[(src[0] & 0x07) << 2 | c]; + + /* 00000... ........ ........ ........ ........ */ + dst[0] = b32[ src[0] >> 3]; + } + /* Add padding */ + if (add_padding) { + switch (src_sz) { + case 1: dst[2] = '='; + dst[3] = '='; + case 2: dst[4] = '='; + case 3: dst[5] = '='; + dst[6] = '='; + case 4: dst[7] = '='; + } + } + return ret_sz; +} + +int +ldns_b32_ntop(const uint8_t* src, size_t src_sz, char* dst, size_t dst_sz) +{ + return ldns_b32_ntop_base(src, src_sz, dst, dst_sz, false, true); +} + +int +ldns_b32_ntop_extended_hex(const uint8_t* src, size_t src_sz, + char* dst, size_t dst_sz) +{ + return ldns_b32_ntop_base(src, src_sz, dst, dst_sz, true, true); +} + +#ifndef HAVE_B32_NTOP + +int +b32_ntop(const uint8_t* src, size_t src_sz, char* dst, size_t dst_sz) +{ + return ldns_b32_ntop_base(src, src_sz, dst, dst_sz, false, true); +} + +int +b32_ntop_extended_hex(const uint8_t* src, size_t src_sz, + char* dst, size_t dst_sz) +{ + return ldns_b32_ntop_base(src, src_sz, dst, dst_sz, true, true); +} + +#endif /* ! HAVE_B32_NTOP */ + +static int +ldns_b32_pton_base(const char* src, size_t src_sz, + uint8_t* dst, size_t dst_sz, + bool extended_hex, bool check_padding) +{ + size_t i = 0; + char ch = '\0'; + uint8_t buf[8]; + uint8_t* start = dst; + + while (src_sz) { + /* Collect 8 characters in buf (if possible) */ + for (i = 0; i < 8; i++) { + + do { + ch = *src++; + --src_sz; + + } while (isspace(ch) && src_sz > 0); + + if (ch == '=' || ch == '\0') + break; + + else if (extended_hex) + + if (ch >= '0' && ch <= '9') + buf[i] = ch - '0'; + else if (ch >= 'a' && ch <= 'v') + buf[i] = ch - 'a' + 10; + else if (ch >= 'A' && ch <= 'V') + buf[i] = ch - 'A' + 10; + else + return -1; + + else if (ch >= 'a' && ch <= 'z') + buf[i] = ch - 'a'; + else if (ch >= 'A' && ch <= 'Z') + buf[i] = ch - 'A'; + else if (ch >= '2' && ch <= '7') + buf[i] = ch - '2' + 26; + else + return -1; + } + /* Less that 8 characters. We're done. */ + if (i < 8) + break; + + /* Enough space available at the destination? */ + if (dst_sz < 5) + return -1; + + /* 00000... ........ ........ ........ ........ */ + /* .....111 11...... ........ ........ ........ */ + dst[0] = buf[0] << 3 | buf[1] >> 2; + + /* .....111 11...... ........ ........ ........ */ + /* ........ ..22222. ........ ........ ........ */ + /* ........ .......3 3333.... ........ ........ */ + dst[1] = buf[1] << 6 | buf[2] << 1 | buf[3] >> 4; + + /* ........ .......3 3333.... ........ ........ */ + /* ........ ........ ....4444 4....... ........ */ + dst[2] = buf[3] << 4 | buf[4] >> 1; + + /* ........ ........ ....4444 4....... ........ */ + /* ........ ........ ........ .55555.. ........ */ + /* ........ ........ ........ ......66 666..... */ + dst[3] = buf[4] << 7 | buf[5] << 2 | buf[6] >> 3; + + /* ........ ........ ........ ......66 666..... */ + /* ........ ........ ........ ........ ...77777 */ + dst[4] = buf[6] << 5 | buf[7]; + + dst += 5; + dst_sz -= 5; + } + /* Not ending on a eight byte boundary? */ + if (i > 0 && i < 8) { + + /* Enough space available at the destination? */ + if (dst_sz < (i + 1) / 2) + return -1; + + switch (i) { + case 7: /* ........ ........ ........ ......66 666..... */ + /* ........ ........ ........ .55555.. ........ */ + /* ........ ........ ....4444 4....... ........ */ + dst[3] = buf[4] << 7 | buf[5] << 2 | buf[6] >> 3; + + case 5: /* ........ ........ ....4444 4....... ........ */ + /* ........ .......3 3333.... ........ ........ */ + dst[2] = buf[3] << 4 | buf[4] >> 1; + + case 4: /* ........ .......3 3333.... ........ ........ */ + /* ........ ..22222. ........ ........ ........ */ + /* .....111 11...... ........ ........ ........ */ + dst[1] = buf[1] << 6 | buf[2] << 1 | buf[3] >> 4; + + case 2: /* .....111 11...... ........ ........ ........ */ + /* 00000... ........ ........ ........ ........ */ + dst[0] = buf[0] << 3 | buf[1] >> 2; + + break; + + default: + return -1; + } + dst += (i + 1) / 2; + + if (check_padding) { + /* Check remaining padding characters */ + if (ch != '=') + return -1; + + /* One down, 8 - i - 1 more to come... */ + for (i = 8 - i - 1; i > 0; i--) { + + do { + if (src_sz == 0) + return -1; + ch = *src++; + src_sz--; + + } while (isspace(ch)); + + if (ch != '=') + return -1; + } + } + } + return dst - start; +} + +int +ldns_b32_pton(const char* src, size_t src_sz, uint8_t* dst, size_t dst_sz) +{ + return ldns_b32_pton_base(src, src_sz, dst, dst_sz, false, true); +} + +int +ldns_b32_pton_extended_hex(const char* src, size_t src_sz, + uint8_t* dst, size_t dst_sz) +{ + return ldns_b32_pton_base(src, src_sz, dst, dst_sz, true, true); +} + +#ifndef HAVE_B32_PTON + +int +b32_pton(const char* src, size_t src_sz, uint8_t* dst, size_t dst_sz) +{ + return ldns_b32_pton_base(src, src_sz, dst, dst_sz, false, true); +} + +int +b32_pton_extended_hex(const char* src, size_t src_sz, + uint8_t* dst, size_t dst_sz) +{ + return ldns_b32_pton_base(src, src_sz, dst, dst_sz, true, true); +} + +#endif /* ! HAVE_B32_PTON */ +