/*1* Copyright (c) 1996, 1998 by Internet Software Consortium.2*3* Permission to use, copy, modify, and distribute this software for any4* purpose with or without fee is hereby granted, provided that the above5* copyright notice and this permission notice appear in all copies.6*7* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS8* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES9* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE10* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL11* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR12* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS13* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS14* SOFTWARE.15*/1617/*18* Portions Copyright (c) 1995 by International Business Machines, Inc.19*20* International Business Machines, Inc. (hereinafter called IBM) grants21* permission under its copyrights to use, copy, modify, and distribute this22* Software with or without fee, provided that the above copyright notice and23* all paragraphs of this notice appear in all copies, and that the name of IBM24* not be used in connection with the marketing of any product incorporating25* the Software or modifications thereof, without specific, written prior26* permission.27*28* To the extent it has a right to do so, IBM grants an immunity from suit29* under its patents, if any, for the use, sale or manufacture of products to30* the extent that such products are used for performing Domain Name System31* dynamic updates in TCP/IP networks by means of the Software. No immunity is32* granted for any product per se or for any other function of any product.33*34* THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,35* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A36* PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,37* DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING38* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN39* IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.40*/4142#include <sys/param.h>43#include <sys/socket.h>4445#include <netinet/in.h>46#include <arpa/inet.h>47#include <arpa/nameser.h>4849#include <ctype.h>50#include <resolv.h>51#include <stdio.h>52#include <stdlib.h>53#include <string.h>5455#define Assert(Cond) if (!(Cond)) abort()5657static const char Base64[] =58"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";59static const char Pad64 = '=';6061/* (From RFC1521 and draft-ietf-dnssec-secext-03.txt)62The following encoding technique is taken from RFC 1521 by Borenstein63and Freed. It is reproduced here in a slightly edited form for64convenience.6566A 65-character subset of US-ASCII is used, enabling 6 bits to be67represented per printable character. (The extra 65th character, "=",68is used to signify a special processing function.)6970The encoding process represents 24-bit groups of input bits as output71strings of 4 encoded characters. Proceeding from left to right, a7224-bit input group is formed by concatenating 3 8-bit input groups.73These 24 bits are then treated as 4 concatenated 6-bit groups, each74of which is translated into a single digit in the base64 alphabet.7576Each 6-bit group is used as an index into an array of 64 printable77characters. The character referenced by the index is placed in the78output string.7980Table 1: The Base64 Alphabet8182Value Encoding Value Encoding Value Encoding Value Encoding830 A 17 R 34 i 51 z841 B 18 S 35 j 52 0852 C 19 T 36 k 53 1863 D 20 U 37 l 54 2874 E 21 V 38 m 55 3885 F 22 W 39 n 56 4896 G 23 X 40 o 57 5907 H 24 Y 41 p 58 6918 I 25 Z 42 q 59 7929 J 26 a 43 r 60 89310 K 27 b 44 s 61 99411 L 28 c 45 t 62 +9512 M 29 d 46 u 63 /9613 N 30 e 47 v9714 O 31 f 48 w (pad) =9815 P 32 g 49 x9916 Q 33 h 50 y100101Special processing is performed if fewer than 24 bits are available102at the end of the data being encoded. A full encoding quantum is103always completed at the end of a quantity. When fewer than 24 input104bits are available in an input group, zero bits are added (on the105right) to form an integral number of 6-bit groups. Padding at the106end of the data is performed using the '=' character.107108Since all base64 input is an integral number of octets, only the109-------------------------------------------------110following cases can arise:111112(1) the final quantum of encoding input is an integral113multiple of 24 bits; here, the final unit of encoded114output will be an integral multiple of 4 characters115with no "=" padding,116(2) the final quantum of encoding input is exactly 8 bits;117here, the final unit of encoded output will be two118characters followed by two "=" padding characters, or119(3) the final quantum of encoding input is exactly 16 bits;120here, the final unit of encoded output will be three121characters followed by one "=" padding character.122*/123124int125b64_ntop(u_char const *src, size_t srclength, char *target, size_t targsize) {126size_t datalength = 0;127u_char input[3];128u_char output[4];129size_t i;130131while (2 < srclength) {132input[0] = *src++;133input[1] = *src++;134input[2] = *src++;135srclength -= 3;136137output[0] = input[0] >> 2;138output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);139output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);140output[3] = input[2] & 0x3f;141Assert(output[0] < 64);142Assert(output[1] < 64);143Assert(output[2] < 64);144Assert(output[3] < 64);145146if (datalength + 4 > targsize)147return (-1);148target[datalength++] = Base64[output[0]];149target[datalength++] = Base64[output[1]];150target[datalength++] = Base64[output[2]];151target[datalength++] = Base64[output[3]];152}153154/* Now we worry about padding. */155if (0 != srclength) {156/* Get what's left. */157input[0] = input[1] = input[2] = '\0';158for (i = 0; i < srclength; i++)159input[i] = *src++;160161output[0] = input[0] >> 2;162output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);163output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);164Assert(output[0] < 64);165Assert(output[1] < 64);166Assert(output[2] < 64);167168if (datalength + 4 > targsize)169return (-1);170target[datalength++] = Base64[output[0]];171target[datalength++] = Base64[output[1]];172if (srclength == 1)173target[datalength++] = Pad64;174else175target[datalength++] = Base64[output[2]];176target[datalength++] = Pad64;177}178if (datalength >= targsize)179return (-1);180target[datalength] = '\0'; /* Returned value doesn't count \0. */181return (datalength);182}183184/* skips all whitespace anywhere.185converts characters, four at a time, starting at (or after)186src from base - 64 numbers into three 8 bit bytes in the target area.187it returns the number of data bytes stored at the target, or -1 on error.188*/189190int191b64_pton(const char *src, u_char *target, size_t targsize)192{193int tarindex, state, ch;194u_char nextbyte;195char *pos;196197state = 0;198tarindex = 0;199200while ((ch = *src++) != '\0') {201if (isspace((unsigned char)ch)) /* Skip whitespace anywhere. */202continue;203204if (ch == Pad64)205break;206207pos = strchr(Base64, ch);208if (pos == NULL) /* A non-base64 character. */209return (-1);210211switch (state) {212case 0:213if (target) {214if ((size_t)tarindex >= targsize)215return (-1);216target[tarindex] = (pos - Base64) << 2;217}218state = 1;219break;220case 1:221if (target) {222if ((size_t)tarindex >= targsize)223return (-1);224target[tarindex] |= (pos - Base64) >> 4;225nextbyte = ((pos - Base64) & 0x0f) << 4;226if ((size_t)tarindex + 1 < targsize)227target[tarindex + 1] = nextbyte;228else if (nextbyte)229return (-1);230}231tarindex++;232state = 2;233break;234case 2:235if (target) {236if ((size_t)tarindex >= targsize)237return (-1);238target[tarindex] |= (pos - Base64) >> 2;239nextbyte = ((pos - Base64) & 0x03) << 6;240if ((size_t)tarindex + 1 < targsize)241target[tarindex + 1] = nextbyte;242else if (nextbyte)243return (-1);244}245tarindex++;246state = 3;247break;248case 3:249if (target) {250if ((size_t)tarindex >= targsize)251return (-1);252target[tarindex] |= (pos - Base64);253}254tarindex++;255state = 0;256break;257default:258abort();259}260}261262/*263* We are done decoding Base-64 chars. Let's see if we ended264* on a byte boundary, and/or with erroneous trailing characters.265*/266267if (ch == Pad64) { /* We got a pad char. */268ch = *src++; /* Skip it, get next. */269switch (state) {270case 0: /* Invalid = in first position */271case 1: /* Invalid = in second position */272return (-1);273274case 2: /* Valid, means one byte of info */275/* Skip any number of spaces. */276for ((void)NULL; ch != '\0'; ch = *src++)277if (!isspace((unsigned char)ch))278break;279/* Make sure there is another trailing = sign. */280if (ch != Pad64)281return (-1);282ch = *src++; /* Skip the = */283/* Fall through to "single trailing =" case. */284/* FALLTHROUGH */285286case 3: /* Valid, means two bytes of info */287/*288* We know this char is an =. Is there anything but289* whitespace after it?290*/291for ((void)NULL; ch != '\0'; ch = *src++)292if (!isspace((unsigned char)ch))293return (-1);294295/*296* Now make sure for cases 2 and 3 that the "extra"297* bits that slopped past the last full byte were298* zeros. If we don't check them, they become a299* subliminal channel.300*/301if (target && (size_t)tarindex < targsize &&302target[tarindex] != 0)303return (-1);304}305} else {306/*307* We ended by seeing the end of the string. Make sure we308* have no partial bytes lying around.309*/310if (state != 0)311return (-1);312}313314return (tarindex);315}316317318