blob: 929f63f4a9cc16c8a70fc186bd4758fa75eb4f38 [file] [log] [blame]
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001From 3da48ec13a44b71ca51adbc803b42c1b29a43f57 Mon Sep 17 00:00:00 2001
2From: Changqing Li <changqing.li@windriver.com>
3Date: Tue, 24 Jul 2018 14:03:51 +0800
Brad Bishop6e60e8b2018-02-01 10:27:11 -05004Subject: [PATCH] bundle own base64 encode/decode functions
5
6Not all libc implementations provide it.
7as an aside libresolv is no longer needed
8
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08009Upstream-Status: Pending
10
Brad Bishop6e60e8b2018-02-01 10:27:11 -050011Signed-off-by: Khem Raj <raj.khem@gmail.com>
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080012
13Update to version 1.190-2
14Signed-off-by: Changqing Li <changqing.li@windriver.com>
Brad Bishop6e60e8b2018-02-01 10:27:11 -050015---
16 Makefile | 4 +-
17 base64.c | 313 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080018 netcat.c | 3 +
Brad Bishop6e60e8b2018-02-01 10:27:11 -050019 socks.c | 3 +
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080020 4 files changed, 321 insertions(+), 2 deletions(-)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050021 create mode 100644 base64.c
22
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080023diff --git a/Makefile b/Makefile
24index 8247cfd..b8d8547 100644
25--- a/Makefile
26+++ b/Makefile
27@@ -1,10 +1,10 @@
28 # $OpenBSD: Makefile,v 1.7 2015/09/11 21:07:01 beck Exp $
Brad Bishop6e60e8b2018-02-01 10:27:11 -050029
30 PROG= nc
31-SRCS= netcat.c atomicio.c socks.c
32+SRCS= netcat.c atomicio.c socks.c base64.c
33
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080034 PKG_CONFIG ?= pkg-config
35-LIBS= `$(PKG_CONFIG) --libs libbsd` -lresolv
36+LIBS= `$(PKG_CONFIG) --libs libbsd`
Brad Bishop6e60e8b2018-02-01 10:27:11 -050037 OBJS= $(SRCS:.c=.o)
38 CFLAGS= -g -O2
39 LDFLAGS= -Wl,--no-add-needed
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080040diff --git a/base64.c b/base64.c
41new file mode 100644
42index 0000000..b0ee6c2
Brad Bishop6e60e8b2018-02-01 10:27:11 -050043--- /dev/null
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080044+++ b/base64.c
Brad Bishop6e60e8b2018-02-01 10:27:11 -050045@@ -0,0 +1,313 @@
46+/*
47+ * Copyright (c) 1996-1999 by Internet Software Consortium.
48+ *
49+ * Permission to use, copy, modify, and distribute this software for any
50+ * purpose with or without fee is hereby granted, provided that the above
51+ * copyright notice and this permission notice appear in all copies.
52+ *
53+ * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
54+ * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
55+ * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
56+ * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
57+ * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
58+ * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
59+ * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
60+ * SOFTWARE.
61+ */
62+
63+/*
64+ * Portions Copyright (c) 1995 by International Business Machines, Inc.
65+ *
66+ * International Business Machines, Inc. (hereinafter called IBM) grants
67+ * permission under its copyrights to use, copy, modify, and distribute this
68+ * Software with or without fee, provided that the above copyright notice and
69+ * all paragraphs of this notice appear in all copies, and that the name of IBM
70+ * not be used in connection with the marketing of any product incorporating
71+ * the Software or modifications thereof, without specific, written prior
72+ * permission.
73+ *
74+ * To the extent it has a right to do so, IBM grants an immunity from suit
75+ * under its patents, if any, for the use, sale or manufacture of products to
76+ * the extent that such products are used for performing Domain Name System
77+ * dynamic updates in TCP/IP networks by means of the Software. No immunity is
78+ * granted for any product per se or for any other function of any product.
79+ *
80+ * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
81+ * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
82+ * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
83+ * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
84+ * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
85+ * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
86+ */
87+
88+#if !defined(LINT) && !defined(CODECENTER)
89+static const char rcsid[] = "$BINDId: base64.c,v 8.7 1999/10/13 16:39:33 vixie Exp $";
90+#endif /* not lint */
91+
92+#include <sys/types.h>
93+#include <sys/param.h>
94+#include <sys/socket.h>
95+
96+#include <netinet/in.h>
97+#include <arpa/inet.h>
98+#include <arpa/nameser.h>
99+
100+#include <ctype.h>
101+#include <resolv.h>
102+#include <stdio.h>
103+#include <stdlib.h>
104+#include <string.h>
105+
106+#define Assert(Cond) if (!(Cond)) abort()
107+
108+static const char Base64[] =
109+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
110+static const char Pad64 = '=';
111+
112+/* (From RFC1521 and draft-ietf-dnssec-secext-03.txt)
113+ The following encoding technique is taken from RFC 1521 by Borenstein
114+ and Freed. It is reproduced here in a slightly edited form for
115+ convenience.
116+
117+ A 65-character subset of US-ASCII is used, enabling 6 bits to be
118+ represented per printable character. (The extra 65th character, "=",
119+ is used to signify a special processing function.)
120+
121+ The encoding process represents 24-bit groups of input bits as output
122+ strings of 4 encoded characters. Proceeding from left to right, a
123+ 24-bit input group is formed by concatenating 3 8-bit input groups.
124+ These 24 bits are then treated as 4 concatenated 6-bit groups, each
125+ of which is translated into a single digit in the base64 alphabet.
126+
127+ Each 6-bit group is used as an index into an array of 64 printable
128+ characters. The character referenced by the index is placed in the
129+ output string.
130+
131+ Table 1: The Base64 Alphabet
132+
133+ Value Encoding Value Encoding Value Encoding Value Encoding
134+ 0 A 17 R 34 i 51 z
135+ 1 B 18 S 35 j 52 0
136+ 2 C 19 T 36 k 53 1
137+ 3 D 20 U 37 l 54 2
138+ 4 E 21 V 38 m 55 3
139+ 5 F 22 W 39 n 56 4
140+ 6 G 23 X 40 o 57 5
141+ 7 H 24 Y 41 p 58 6
142+ 8 I 25 Z 42 q 59 7
143+ 9 J 26 a 43 r 60 8
144+ 10 K 27 b 44 s 61 9
145+ 11 L 28 c 45 t 62 +
146+ 12 M 29 d 46 u 63 /
147+ 13 N 30 e 47 v
148+ 14 O 31 f 48 w (pad) =
149+ 15 P 32 g 49 x
150+ 16 Q 33 h 50 y
151+
152+ Special processing is performed if fewer than 24 bits are available
153+ at the end of the data being encoded. A full encoding quantum is
154+ always completed at the end of a quantity. When fewer than 24 input
155+ bits are available in an input group, zero bits are added (on the
156+ right) to form an integral number of 6-bit groups. Padding at the
157+ end of the data is performed using the '=' character.
158+
159+ Since all base64 input is an integral number of octets, only the
160+ -------------------------------------------------
161+ following cases can arise:
162+
163+ (1) the final quantum of encoding input is an integral
164+ multiple of 24 bits; here, the final unit of encoded
165+ output will be an integral multiple of 4 characters
166+ with no "=" padding,
167+ (2) the final quantum of encoding input is exactly 8 bits;
168+ here, the final unit of encoded output will be two
169+ characters followed by two "=" padding characters, or
170+ (3) the final quantum of encoding input is exactly 16 bits;
171+ here, the final unit of encoded output will be three
172+ characters followed by one "=" padding character.
173+ */
174+
175+int
176+b64_ntop(u_char const *src, size_t srclength, char *target, size_t targsize) {
177+ size_t datalength = 0;
178+ u_char input[3];
179+ u_char output[4];
180+ size_t i;
181+
182+ while (2 < srclength) {
183+ input[0] = *src++;
184+ input[1] = *src++;
185+ input[2] = *src++;
186+ srclength -= 3;
187+
188+ output[0] = input[0] >> 2;
189+ output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
190+ output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
191+ output[3] = input[2] & 0x3f;
192+ Assert(output[0] < 64);
193+ Assert(output[1] < 64);
194+ Assert(output[2] < 64);
195+ Assert(output[3] < 64);
196+
197+ if (datalength + 4 > targsize)
198+ return (-1);
199+ target[datalength++] = Base64[output[0]];
200+ target[datalength++] = Base64[output[1]];
201+ target[datalength++] = Base64[output[2]];
202+ target[datalength++] = Base64[output[3]];
203+ }
204+
205+ /* Now we worry about padding. */
206+ if (0 != srclength) {
207+ /* Get what's left. */
208+ input[0] = input[1] = input[2] = '\0';
209+ for (i = 0; i < srclength; i++)
210+ input[i] = *src++;
211+
212+ output[0] = input[0] >> 2;
213+ output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
214+ output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
215+ Assert(output[0] < 64);
216+ Assert(output[1] < 64);
217+ Assert(output[2] < 64);
218+
219+ if (datalength + 4 > targsize)
220+ return (-1);
221+ target[datalength++] = Base64[output[0]];
222+ target[datalength++] = Base64[output[1]];
223+ if (srclength == 1)
224+ target[datalength++] = Pad64;
225+ else
226+ target[datalength++] = Base64[output[2]];
227+ target[datalength++] = Pad64;
228+ }
229+ if (datalength >= targsize)
230+ return (-1);
231+ target[datalength] = '\0'; /* Returned value doesn't count \0. */
232+ return (datalength);
233+}
234+//libresolv_hidden_def (b64_ntop)
235+
236+/* skips all whitespace anywhere.
237+ converts characters, four at a time, starting at (or after)
238+ src from base - 64 numbers into three 8 bit bytes in the target area.
239+ it returns the number of data bytes stored at the target, or -1 on error.
240+ */
241+
242+int
243+b64_pton(char const *src, u_char *target, size_t targsize) {
244+ int tarindex, state, ch;
245+ char *pos;
246+
247+ state = 0;
248+ tarindex = 0;
249+
250+ while ((ch = *src++) != '\0') {
251+ if (isspace(ch)) /* Skip whitespace anywhere. */
252+ continue;
253+
254+ if (ch == Pad64)
255+ break;
256+
257+ pos = strchr(Base64, ch);
258+ if (pos == 0) /* A non-base64 character. */
259+ return (-1);
260+
261+ switch (state) {
262+ case 0:
263+ if (target) {
264+ if ((size_t)tarindex >= targsize)
265+ return (-1);
266+ target[tarindex] = (pos - Base64) << 2;
267+ }
268+ state = 1;
269+ break;
270+ case 1:
271+ if (target) {
272+ if ((size_t)tarindex + 1 >= targsize)
273+ return (-1);
274+ target[tarindex] |= (pos - Base64) >> 4;
275+ target[tarindex+1] = ((pos - Base64) & 0x0f)
276+ << 4 ;
277+ }
278+ tarindex++;
279+ state = 2;
280+ break;
281+ case 2:
282+ if (target) {
283+ if ((size_t)tarindex + 1 >= targsize)
284+ return (-1);
285+ target[tarindex] |= (pos - Base64) >> 2;
286+ target[tarindex+1] = ((pos - Base64) & 0x03)
287+ << 6;
288+ }
289+ tarindex++;
290+ state = 3;
291+ break;
292+ case 3:
293+ if (target) {
294+ if ((size_t)tarindex >= targsize)
295+ return (-1);
296+ target[tarindex] |= (pos - Base64);
297+ }
298+ tarindex++;
299+ state = 0;
300+ break;
301+ default:
302+ abort();
303+ }
304+ }
305+
306+ /*
307+ * We are done decoding Base-64 chars. Let's see if we ended
308+ * on a byte boundary, and/or with erroneous trailing characters.
309+ */
310+
311+ if (ch == Pad64) { /* We got a pad char. */
312+ ch = *src++; /* Skip it, get next. */
313+ switch (state) {
314+ case 0: /* Invalid = in first position */
315+ case 1: /* Invalid = in second position */
316+ return (-1);
317+
318+ case 2: /* Valid, means one byte of info */
319+ /* Skip any number of spaces. */
320+ for ((void)NULL; ch != '\0'; ch = *src++)
321+ if (!isspace(ch))
322+ break;
323+ /* Make sure there is another trailing = sign. */
324+ if (ch != Pad64)
325+ return (-1);
326+ ch = *src++; /* Skip the = */
327+ /* Fall through to "single trailing =" case. */
328+ /* FALLTHROUGH */
329+
330+ case 3: /* Valid, means two bytes of info */
331+ /*
332+ * We know this char is an =. Is there anything but
333+ * whitespace after it?
334+ */
335+ for ((void)NULL; ch != '\0'; ch = *src++)
336+ if (!isspace(ch))
337+ return (-1);
338+
339+ /*
340+ * Now make sure for cases 2 and 3 that the "extra"
341+ * bits that slopped past the last full byte were
342+ * zeros. If we don't check them, they become a
343+ * subliminal channel.
344+ */
345+ if (target && target[tarindex] != 0)
346+ return (-1);
347+ }
348+ } else {
349+ /*
350+ * We ended by seeing the end of the string. Make sure we
351+ * have no partial bytes lying around.
352+ */
353+ if (state != 0)
354+ return (-1);
355+ }
356+
357+ return (tarindex);
358+}
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800359diff --git a/netcat.c b/netcat.c
360index a0fb51b..9c4ed23 100644
361--- a/netcat.c
362+++ b/netcat.c
363@@ -240,6 +240,9 @@ static int connect_with_timeout(int fd, const struct sockaddr *sa,
364
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500365 static void quit();
366
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800367+int b64_ntop(u_char const *src, size_t srclength, char *target, size_t targsize);
368+int b64_pton(char const *src, u_char *target, size_t targsize);
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500369+
370 int
371 main(int argc, char *argv[])
372 {
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800373diff --git a/socks.c b/socks.c
374index 9068f39..c576f6b 100644
375--- a/socks.c
376+++ b/socks.c
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500377@@ -53,6 +53,9 @@
378 #define SOCKS_DOMAIN 3
379 #define SOCKS_IPV6 4
380
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800381+int b64_ntop(u_char const *src, size_t srclength, char *target, size_t targsize);
382+int b64_pton(char const *src, u_char *target, size_t targsize);
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500383+
384 int remote_connect(const char *, const char *, struct addrinfo);
385 int socks_connect(const char *, const char *, struct addrinfo,
386 const char *, const char *, struct addrinfo, int,
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800387--
3882.7.4
389