10Sstevel@tonic-gate /*
2*11038SRao.Shoaib@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
3*11038SRao.Shoaib@Sun.COM * Use is subject to license terms.
40Sstevel@tonic-gate */
50Sstevel@tonic-gate
6*11038SRao.Shoaib@Sun.COM
70Sstevel@tonic-gate /*
8*11038SRao.Shoaib@Sun.COM * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
90Sstevel@tonic-gate * Copyright (c) 1996-1999 by Internet Software Consortium.
100Sstevel@tonic-gate *
110Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any
120Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above
130Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies.
140Sstevel@tonic-gate *
15*11038SRao.Shoaib@Sun.COM * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
16*11038SRao.Shoaib@Sun.COM * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17*11038SRao.Shoaib@Sun.COM * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
18*11038SRao.Shoaib@Sun.COM * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19*11038SRao.Shoaib@Sun.COM * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20*11038SRao.Shoaib@Sun.COM * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21*11038SRao.Shoaib@Sun.COM * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
220Sstevel@tonic-gate */
230Sstevel@tonic-gate
240Sstevel@tonic-gate /*
250Sstevel@tonic-gate * Portions Copyright (c) 1995 by International Business Machines, Inc.
260Sstevel@tonic-gate *
270Sstevel@tonic-gate * International Business Machines, Inc. (hereinafter called IBM) grants
280Sstevel@tonic-gate * permission under its copyrights to use, copy, modify, and distribute this
290Sstevel@tonic-gate * Software with or without fee, provided that the above copyright notice and
300Sstevel@tonic-gate * all paragraphs of this notice appear in all copies, and that the name of IBM
310Sstevel@tonic-gate * not be used in connection with the marketing of any product incorporating
320Sstevel@tonic-gate * the Software or modifications thereof, without specific, written prior
330Sstevel@tonic-gate * permission.
340Sstevel@tonic-gate *
350Sstevel@tonic-gate * To the extent it has a right to do so, IBM grants an immunity from suit
360Sstevel@tonic-gate * under its patents, if any, for the use, sale or manufacture of products to
370Sstevel@tonic-gate * the extent that such products are used for performing Domain Name System
380Sstevel@tonic-gate * dynamic updates in TCP/IP networks by means of the Software. No immunity is
390Sstevel@tonic-gate * granted for any product per se or for any other function of any product.
400Sstevel@tonic-gate *
410Sstevel@tonic-gate * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
420Sstevel@tonic-gate * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
430Sstevel@tonic-gate * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
440Sstevel@tonic-gate * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
450Sstevel@tonic-gate * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
460Sstevel@tonic-gate * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
470Sstevel@tonic-gate */
480Sstevel@tonic-gate
490Sstevel@tonic-gate #if !defined(LINT) && !defined(CODECENTER)
50*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: base64.c,v 1.4 2005/04/27 04:56:34 sra Exp $";
510Sstevel@tonic-gate #endif /* not lint */
520Sstevel@tonic-gate
530Sstevel@tonic-gate #include "port_before.h"
540Sstevel@tonic-gate
550Sstevel@tonic-gate #include <sys/types.h>
560Sstevel@tonic-gate #include <sys/param.h>
570Sstevel@tonic-gate #include <sys/socket.h>
580Sstevel@tonic-gate
590Sstevel@tonic-gate #include <netinet/in.h>
600Sstevel@tonic-gate #include <arpa/inet.h>
610Sstevel@tonic-gate #include <arpa/nameser.h>
620Sstevel@tonic-gate
630Sstevel@tonic-gate #include <ctype.h>
640Sstevel@tonic-gate #include <resolv.h>
650Sstevel@tonic-gate #include <stdio.h>
660Sstevel@tonic-gate #include <stdlib.h>
670Sstevel@tonic-gate #include <string.h>
680Sstevel@tonic-gate
690Sstevel@tonic-gate #include "port_after.h"
700Sstevel@tonic-gate
71*11038SRao.Shoaib@Sun.COM #ifndef ORIGINAL_ISC_CODE
720Sstevel@tonic-gate #pragma weak __b64_ntop = b64_ntop
730Sstevel@tonic-gate #pragma weak __b64_pton = b64_pton
740Sstevel@tonic-gate #endif /* ORIGINAL_ISC_CODE */
750Sstevel@tonic-gate
760Sstevel@tonic-gate #define Assert(Cond) if (!(Cond)) abort()
770Sstevel@tonic-gate
780Sstevel@tonic-gate static const char Base64[] =
790Sstevel@tonic-gate "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
800Sstevel@tonic-gate static const char Pad64 = '=';
810Sstevel@tonic-gate
820Sstevel@tonic-gate /* (From RFC1521 and draft-ietf-dnssec-secext-03.txt)
83*11038SRao.Shoaib@Sun.COM The following encoding technique is taken from RFC1521 by Borenstein
840Sstevel@tonic-gate and Freed. It is reproduced here in a slightly edited form for
850Sstevel@tonic-gate convenience.
860Sstevel@tonic-gate
870Sstevel@tonic-gate A 65-character subset of US-ASCII is used, enabling 6 bits to be
880Sstevel@tonic-gate represented per printable character. (The extra 65th character, "=",
890Sstevel@tonic-gate is used to signify a special processing function.)
900Sstevel@tonic-gate
910Sstevel@tonic-gate The encoding process represents 24-bit groups of input bits as output
920Sstevel@tonic-gate strings of 4 encoded characters. Proceeding from left to right, a
930Sstevel@tonic-gate 24-bit input group is formed by concatenating 3 8-bit input groups.
940Sstevel@tonic-gate These 24 bits are then treated as 4 concatenated 6-bit groups, each
950Sstevel@tonic-gate of which is translated into a single digit in the base64 alphabet.
960Sstevel@tonic-gate
970Sstevel@tonic-gate Each 6-bit group is used as an index into an array of 64 printable
980Sstevel@tonic-gate characters. The character referenced by the index is placed in the
990Sstevel@tonic-gate output string.
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate Table 1: The Base64 Alphabet
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate Value Encoding Value Encoding Value Encoding Value Encoding
1040Sstevel@tonic-gate 0 A 17 R 34 i 51 z
1050Sstevel@tonic-gate 1 B 18 S 35 j 52 0
1060Sstevel@tonic-gate 2 C 19 T 36 k 53 1
1070Sstevel@tonic-gate 3 D 20 U 37 l 54 2
1080Sstevel@tonic-gate 4 E 21 V 38 m 55 3
1090Sstevel@tonic-gate 5 F 22 W 39 n 56 4
1100Sstevel@tonic-gate 6 G 23 X 40 o 57 5
1110Sstevel@tonic-gate 7 H 24 Y 41 p 58 6
1120Sstevel@tonic-gate 8 I 25 Z 42 q 59 7
1130Sstevel@tonic-gate 9 J 26 a 43 r 60 8
1140Sstevel@tonic-gate 10 K 27 b 44 s 61 9
1150Sstevel@tonic-gate 11 L 28 c 45 t 62 +
1160Sstevel@tonic-gate 12 M 29 d 46 u 63 /
1170Sstevel@tonic-gate 13 N 30 e 47 v
1180Sstevel@tonic-gate 14 O 31 f 48 w (pad) =
1190Sstevel@tonic-gate 15 P 32 g 49 x
1200Sstevel@tonic-gate 16 Q 33 h 50 y
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate Special processing is performed if fewer than 24 bits are available
1230Sstevel@tonic-gate at the end of the data being encoded. A full encoding quantum is
1240Sstevel@tonic-gate always completed at the end of a quantity. When fewer than 24 input
1250Sstevel@tonic-gate bits are available in an input group, zero bits are added (on the
1260Sstevel@tonic-gate right) to form an integral number of 6-bit groups. Padding at the
1270Sstevel@tonic-gate end of the data is performed using the '=' character.
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate Since all base64 input is an integral number of octets, only the
1300Sstevel@tonic-gate -------------------------------------------------
1310Sstevel@tonic-gate following cases can arise:
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate (1) the final quantum of encoding input is an integral
1340Sstevel@tonic-gate multiple of 24 bits; here, the final unit of encoded
1350Sstevel@tonic-gate output will be an integral multiple of 4 characters
1360Sstevel@tonic-gate with no "=" padding,
1370Sstevel@tonic-gate (2) the final quantum of encoding input is exactly 8 bits;
1380Sstevel@tonic-gate here, the final unit of encoded output will be two
1390Sstevel@tonic-gate characters followed by two "=" padding characters, or
1400Sstevel@tonic-gate (3) the final quantum of encoding input is exactly 16 bits;
1410Sstevel@tonic-gate here, the final unit of encoded output will be three
1420Sstevel@tonic-gate characters followed by one "=" padding character.
1430Sstevel@tonic-gate */
1440Sstevel@tonic-gate
1450Sstevel@tonic-gate int
b64_ntop(u_char const * src,size_t srclength,char * target,size_t targsize)1460Sstevel@tonic-gate b64_ntop(u_char const *src, size_t srclength, char *target, size_t targsize) {
1470Sstevel@tonic-gate size_t datalength = 0;
1480Sstevel@tonic-gate u_char input[3];
1490Sstevel@tonic-gate u_char output[4];
1500Sstevel@tonic-gate size_t i;
1510Sstevel@tonic-gate
152*11038SRao.Shoaib@Sun.COM while (2U < srclength) {
1530Sstevel@tonic-gate input[0] = *src++;
1540Sstevel@tonic-gate input[1] = *src++;
1550Sstevel@tonic-gate input[2] = *src++;
1560Sstevel@tonic-gate srclength -= 3;
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate output[0] = input[0] >> 2;
1590Sstevel@tonic-gate output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
1600Sstevel@tonic-gate output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
1610Sstevel@tonic-gate output[3] = input[2] & 0x3f;
1620Sstevel@tonic-gate Assert(output[0] < 64);
1630Sstevel@tonic-gate Assert(output[1] < 64);
1640Sstevel@tonic-gate Assert(output[2] < 64);
1650Sstevel@tonic-gate Assert(output[3] < 64);
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate if (datalength + 4 > targsize)
1680Sstevel@tonic-gate return (-1);
1690Sstevel@tonic-gate target[datalength++] = Base64[output[0]];
1700Sstevel@tonic-gate target[datalength++] = Base64[output[1]];
1710Sstevel@tonic-gate target[datalength++] = Base64[output[2]];
1720Sstevel@tonic-gate target[datalength++] = Base64[output[3]];
1730Sstevel@tonic-gate }
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate /* Now we worry about padding. */
176*11038SRao.Shoaib@Sun.COM if (0U != srclength) {
1770Sstevel@tonic-gate /* Get what's left. */
1780Sstevel@tonic-gate input[0] = input[1] = input[2] = '\0';
1790Sstevel@tonic-gate for (i = 0; i < srclength; i++)
1800Sstevel@tonic-gate input[i] = *src++;
1810Sstevel@tonic-gate
1820Sstevel@tonic-gate output[0] = input[0] >> 2;
1830Sstevel@tonic-gate output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
1840Sstevel@tonic-gate output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
1850Sstevel@tonic-gate Assert(output[0] < 64);
1860Sstevel@tonic-gate Assert(output[1] < 64);
1870Sstevel@tonic-gate Assert(output[2] < 64);
1880Sstevel@tonic-gate
1890Sstevel@tonic-gate if (datalength + 4 > targsize)
1900Sstevel@tonic-gate return (-1);
1910Sstevel@tonic-gate target[datalength++] = Base64[output[0]];
1920Sstevel@tonic-gate target[datalength++] = Base64[output[1]];
193*11038SRao.Shoaib@Sun.COM if (srclength == 1U)
1940Sstevel@tonic-gate target[datalength++] = Pad64;
1950Sstevel@tonic-gate else
1960Sstevel@tonic-gate target[datalength++] = Base64[output[2]];
1970Sstevel@tonic-gate target[datalength++] = Pad64;
1980Sstevel@tonic-gate }
1990Sstevel@tonic-gate if (datalength >= targsize)
2000Sstevel@tonic-gate return (-1);
201*11038SRao.Shoaib@Sun.COM target[datalength] = '\0'; /*%< Returned value doesn't count \\0. */
2020Sstevel@tonic-gate return (datalength);
2030Sstevel@tonic-gate }
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate /* skips all whitespace anywhere.
2060Sstevel@tonic-gate converts characters, four at a time, starting at (or after)
2070Sstevel@tonic-gate src from base - 64 numbers into three 8 bit bytes in the target area.
2080Sstevel@tonic-gate it returns the number of data bytes stored at the target, or -1 on error.
2090Sstevel@tonic-gate */
2100Sstevel@tonic-gate
2110Sstevel@tonic-gate int
b64_pton(src,target,targsize)2120Sstevel@tonic-gate b64_pton(src, target, targsize)
2130Sstevel@tonic-gate char const *src;
2140Sstevel@tonic-gate u_char *target;
2150Sstevel@tonic-gate size_t targsize;
2160Sstevel@tonic-gate {
2170Sstevel@tonic-gate int tarindex, state, ch;
2180Sstevel@tonic-gate char *pos;
2190Sstevel@tonic-gate
2200Sstevel@tonic-gate state = 0;
2210Sstevel@tonic-gate tarindex = 0;
2220Sstevel@tonic-gate
2230Sstevel@tonic-gate while ((ch = *src++) != '\0') {
224*11038SRao.Shoaib@Sun.COM if (isspace(ch)) /*%< Skip whitespace anywhere. */
2250Sstevel@tonic-gate continue;
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate if (ch == Pad64)
2280Sstevel@tonic-gate break;
2290Sstevel@tonic-gate
2300Sstevel@tonic-gate pos = strchr(Base64, ch);
231*11038SRao.Shoaib@Sun.COM if (pos == 0) /*%< A non-base64 character. */
2320Sstevel@tonic-gate return (-1);
2330Sstevel@tonic-gate
2340Sstevel@tonic-gate switch (state) {
2350Sstevel@tonic-gate case 0:
2360Sstevel@tonic-gate if (target) {
2370Sstevel@tonic-gate if ((size_t)tarindex >= targsize)
2380Sstevel@tonic-gate return (-1);
2390Sstevel@tonic-gate target[tarindex] = (pos - Base64) << 2;
2400Sstevel@tonic-gate }
2410Sstevel@tonic-gate state = 1;
2420Sstevel@tonic-gate break;
2430Sstevel@tonic-gate case 1:
2440Sstevel@tonic-gate if (target) {
2450Sstevel@tonic-gate if ((size_t)tarindex + 1 >= targsize)
2460Sstevel@tonic-gate return (-1);
2470Sstevel@tonic-gate target[tarindex] |= (pos - Base64) >> 4;
2480Sstevel@tonic-gate target[tarindex+1] = ((pos - Base64) & 0x0f)
2490Sstevel@tonic-gate << 4 ;
2500Sstevel@tonic-gate }
2510Sstevel@tonic-gate tarindex++;
2520Sstevel@tonic-gate state = 2;
2530Sstevel@tonic-gate break;
2540Sstevel@tonic-gate case 2:
2550Sstevel@tonic-gate if (target) {
2560Sstevel@tonic-gate if ((size_t)tarindex + 1 >= targsize)
2570Sstevel@tonic-gate return (-1);
2580Sstevel@tonic-gate target[tarindex] |= (pos - Base64) >> 2;
2590Sstevel@tonic-gate target[tarindex+1] = ((pos - Base64) & 0x03)
2600Sstevel@tonic-gate << 6;
2610Sstevel@tonic-gate }
2620Sstevel@tonic-gate tarindex++;
2630Sstevel@tonic-gate state = 3;
2640Sstevel@tonic-gate break;
2650Sstevel@tonic-gate case 3:
2660Sstevel@tonic-gate if (target) {
2670Sstevel@tonic-gate if ((size_t)tarindex >= targsize)
2680Sstevel@tonic-gate return (-1);
2690Sstevel@tonic-gate target[tarindex] |= (pos - Base64);
2700Sstevel@tonic-gate }
2710Sstevel@tonic-gate tarindex++;
2720Sstevel@tonic-gate state = 0;
2730Sstevel@tonic-gate break;
2740Sstevel@tonic-gate default:
2750Sstevel@tonic-gate abort();
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate
2790Sstevel@tonic-gate /*
2800Sstevel@tonic-gate * We are done decoding Base-64 chars. Let's see if we ended
2810Sstevel@tonic-gate * on a byte boundary, and/or with erroneous trailing characters.
2820Sstevel@tonic-gate */
2830Sstevel@tonic-gate
284*11038SRao.Shoaib@Sun.COM if (ch == Pad64) { /*%< We got a pad char. */
285*11038SRao.Shoaib@Sun.COM ch = *src++; /*%< Skip it, get next. */
2860Sstevel@tonic-gate switch (state) {
287*11038SRao.Shoaib@Sun.COM case 0: /*%< Invalid = in first position */
288*11038SRao.Shoaib@Sun.COM case 1: /*%< Invalid = in second position */
2890Sstevel@tonic-gate return (-1);
2900Sstevel@tonic-gate
291*11038SRao.Shoaib@Sun.COM case 2: /*%< Valid, means one byte of info */
2920Sstevel@tonic-gate /* Skip any number of spaces. */
2930Sstevel@tonic-gate for ((void)NULL; ch != '\0'; ch = *src++)
2940Sstevel@tonic-gate if (!isspace(ch))
2950Sstevel@tonic-gate break;
2960Sstevel@tonic-gate /* Make sure there is another trailing = sign. */
2970Sstevel@tonic-gate if (ch != Pad64)
2980Sstevel@tonic-gate return (-1);
299*11038SRao.Shoaib@Sun.COM ch = *src++; /*%< Skip the = */
3000Sstevel@tonic-gate /* Fall through to "single trailing =" case. */
3010Sstevel@tonic-gate /* FALLTHROUGH */
3020Sstevel@tonic-gate
303*11038SRao.Shoaib@Sun.COM case 3: /*%< Valid, means two bytes of info */
3040Sstevel@tonic-gate /*
3050Sstevel@tonic-gate * We know this char is an =. Is there anything but
3060Sstevel@tonic-gate * whitespace after it?
3070Sstevel@tonic-gate */
3080Sstevel@tonic-gate for ((void)NULL; ch != '\0'; ch = *src++)
3090Sstevel@tonic-gate if (!isspace(ch))
3100Sstevel@tonic-gate return (-1);
3110Sstevel@tonic-gate
3120Sstevel@tonic-gate /*
3130Sstevel@tonic-gate * Now make sure for cases 2 and 3 that the "extra"
3140Sstevel@tonic-gate * bits that slopped past the last full byte were
3150Sstevel@tonic-gate * zeros. If we don't check them, they become a
3160Sstevel@tonic-gate * subliminal channel.
3170Sstevel@tonic-gate */
3180Sstevel@tonic-gate if (target && target[tarindex] != 0)
3190Sstevel@tonic-gate return (-1);
3200Sstevel@tonic-gate }
3210Sstevel@tonic-gate } else {
3220Sstevel@tonic-gate /*
3230Sstevel@tonic-gate * We ended by seeing the end of the string. Make sure we
3240Sstevel@tonic-gate * have no partial bytes lying around.
3250Sstevel@tonic-gate */
3260Sstevel@tonic-gate if (state != 0)
3270Sstevel@tonic-gate return (-1);
3280Sstevel@tonic-gate }
3290Sstevel@tonic-gate
3300Sstevel@tonic-gate return (tarindex);
3310Sstevel@tonic-gate }
332*11038SRao.Shoaib@Sun.COM
333*11038SRao.Shoaib@Sun.COM /*! \file */
334