blob: 5c4ab3a47e8ba0d09cc3d3cc72027b17ba124d92 [file] [log] [blame]
willy tarreau9e138862006-05-14 23:06:28 +02001/*
Krzysztof Piotr Oledzkifccbdc82010-01-29 13:36:23 +01002 * ASCII <-> Base64 conversion as described in RFC1421.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003 *
Willy Tarreauc01062b2010-10-07 19:27:29 +02004 * Copyright 2006-2010 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzkifccbdc82010-01-29 13:36:23 +01005 * Copyright 2009-2010 Krzysztof Piotr Oledzki <ole@ans.pl>
willy tarreau9e138862006-05-14 23:06:28 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
Krzysztof Piotr Oledzkifccbdc82010-01-29 13:36:23 +010014#include <stdlib.h>
15#include <string.h>
16
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020017#include <haproxy/api.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020018#include <haproxy/base64.h>
willy tarreau9e138862006-05-14 23:06:28 +020019
Krzysztof Piotr Oledzkifccbdc82010-01-29 13:36:23 +010020#define B64BASE '#' /* arbitrary chosen base value */
21#define B64CMIN '+'
Moemen MHEDHBI92f7d432021-04-01 20:53:59 +020022#define UB64CMIN '-'
Krzysztof Piotr Oledzkifccbdc82010-01-29 13:36:23 +010023#define B64CMAX 'z'
24#define B64PADV 64 /* Base64 chosen special pad value */
25
Willy Tarreau69e989c2008-06-29 17:17:38 +020026const char base64tab[65]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
Krzysztof Piotr Oledzkifccbdc82010-01-29 13:36:23 +010027const char base64rev[]="b###cXYZ[\\]^_`a###d###$%&'()*+,-./0123456789:;<=######>?@ABCDEFGHIJKLMNOPQRSTUVW";
Moemen MHEDHBI92f7d432021-04-01 20:53:59 +020028const char ubase64tab[65]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
29const char ubase64rev[]="b##XYZ[\\]^_`a###c###$%&'()*+,-./0123456789:;<=####c#>?@ABCDEFGHIJKLMNOPQRSTUVW";
willy tarreau9e138862006-05-14 23:06:28 +020030
31/* Encodes <ilen> bytes from <in> to <out> for at most <olen> chars (including
32 * the trailing zero). Returns the number of bytes written. No check is made
33 * for <in> or <out> to be NULL. Returns negative value if <olen> is too short
34 * to accept <ilen>. 4 output bytes are produced for 1 to 3 input bytes.
35 */
36int a2base64(char *in, int ilen, char *out, int olen)
37{
willy tarreau9e138862006-05-14 23:06:28 +020038 int convlen;
39
40 convlen = ((ilen + 2) / 3) * 4;
41
42 if (convlen >= olen)
43 return -1;
44
45 /* we don't need to check olen anymore */
46 while (ilen >= 3) {
Willy Tarreaubaaee002006-06-26 02:48:02 +020047 out[0] = base64tab[(((unsigned char)in[0]) >> 2)];
48 out[1] = base64tab[(((unsigned char)in[0] & 0x03) << 4) | (((unsigned char)in[1]) >> 4)];
49 out[2] = base64tab[(((unsigned char)in[1] & 0x0F) << 2) | (((unsigned char)in[2]) >> 6)];
50 out[3] = base64tab[(((unsigned char)in[2] & 0x3F))];
willy tarreau9e138862006-05-14 23:06:28 +020051 out += 4;
52 in += 3; ilen -= 3;
53 }
54
55 if (!ilen) {
56 out[0] = '\0';
57 } else {
Willy Tarreaubaaee002006-06-26 02:48:02 +020058 out[0] = base64tab[((unsigned char)in[0]) >> 2];
willy tarreau9e138862006-05-14 23:06:28 +020059 if (ilen == 1) {
Willy Tarreaubaaee002006-06-26 02:48:02 +020060 out[1] = base64tab[((unsigned char)in[0] & 0x03) << 4];
willy tarreau9e138862006-05-14 23:06:28 +020061 out[2] = '=';
62 } else {
Willy Tarreaubaaee002006-06-26 02:48:02 +020063 out[1] = base64tab[(((unsigned char)in[0] & 0x03) << 4) |
willy tarreau9e138862006-05-14 23:06:28 +020064 (((unsigned char)in[1]) >> 4)];
Willy Tarreaubaaee002006-06-26 02:48:02 +020065 out[2] = base64tab[((unsigned char)in[1] & 0x0F) << 2];
willy tarreau9e138862006-05-14 23:06:28 +020066 }
67 out[3] = '=';
68 out[4] = '\0';
69 }
70
71 return convlen;
72}
Krzysztof Piotr Oledzkifccbdc82010-01-29 13:36:23 +010073
Moemen MHEDHBI92f7d432021-04-01 20:53:59 +020074/* url variant of a2base64 */
75int a2base64url(const char *in, size_t ilen, char *out, size_t olen)
76{
77 int convlen;
78
79 convlen = ((ilen + 2) / 3) * 4;
80
81 if (convlen >= olen)
82 return -1;
83
84 /* we don't need to check olen anymore */
85 while (ilen >= 3) {
86 out[0] = ubase64tab[(((unsigned char)in[0]) >> 2)];
87 out[1] = ubase64tab[(((unsigned char)in[0] & 0x03) << 4) | (((unsigned char)in[1]) >> 4)];
88 out[2] = ubase64tab[(((unsigned char)in[1] & 0x0F) << 2) | (((unsigned char)in[2]) >> 6)];
89 out[3] = ubase64tab[(((unsigned char)in[2] & 0x3F))];
90 out += 4;
91 in += 3;
92 ilen -= 3;
93 }
94
95 if (!ilen) {
96 out[0] = '\0';
97 return convlen;
98 }
99
100 out[0] = ubase64tab[((unsigned char)in[0]) >> 2];
101 if (ilen == 1) {
102 out[1] = ubase64tab[((unsigned char)in[0] & 0x03) << 4];
103 out[2] = '\0';
104 convlen -= 2;
105 } else {
106 out[1] = ubase64tab[(((unsigned char)in[0] & 0x03) << 4) |
107 (((unsigned char)in[1]) >> 4)];
108 out[2] = ubase64tab[((unsigned char)in[1] & 0x0F) << 2];
109 out[3] = '\0';
110 convlen -= 1;
111 }
112
113 return convlen;
114}
115
Krzysztof Piotr Oledzkifccbdc82010-01-29 13:36:23 +0100116/* Decodes <ilen> bytes from <in> to <out> for at most <olen> chars.
117 * Returns the number of bytes converted. No check is made for
118 * <in> or <out> to be NULL. Returns -1 if <in> is invalid or ilen
119 * has wrong size, -2 if <olen> is too short.
120 * 1 to 3 output bytes are produced for 4 input bytes.
121 */
122int base64dec(const char *in, size_t ilen, char *out, size_t olen) {
123
124 unsigned char t[4];
125 signed char b;
126 int convlen = 0, i = 0, pad = 0;
127
128 if (ilen % 4)
129 return -1;
130
Emeric Bruned697e42019-01-14 14:38:39 +0100131 if (olen < ((ilen / 4 * 3)
132 - (in[ilen-1] == '=' ? 1 : 0)
133 - (in[ilen-2] == '=' ? 1 : 0)))
Krzysztof Piotr Oledzkifccbdc82010-01-29 13:36:23 +0100134 return -2;
135
136 while (ilen) {
137
138 /* if (*p < B64CMIN || *p > B64CMAX) */
139 b = (signed char)*in - B64CMIN;
140 if ((unsigned char)b > (B64CMAX-B64CMIN))
141 return -1;
142
143 b = base64rev[b] - B64BASE - 1;
144
145 /* b == -1: invalid character */
146 if (b < 0)
147 return -1;
148
Joseph Herlant3b4e8e12018-11-25 13:16:35 -0800149 /* padding has to be continuous */
Krzysztof Piotr Oledzkifccbdc82010-01-29 13:36:23 +0100150 if (pad && b != B64PADV)
151 return -1;
152
153 /* valid padding: "XX==" or "XXX=", but never "X===" or "====" */
154 if (pad && i < 2)
155 return -1;
156
157 if (b == B64PADV)
158 pad++;
159
160 t[i++] = b;
161
162 if (i == 4) {
163 /*
164 * WARNING: we allow to write little more data than we
165 * should, but the checks from the beginning of the
166 * functions guarantee that we can safely do that.
167 */
168
169 /* xx000000 xx001111 xx111122 xx222222 */
170 out[convlen] = ((t[0] << 2) + (t[1] >> 4));
171 out[convlen+1] = ((t[1] << 4) + (t[2] >> 2));
172 out[convlen+2] = ((t[2] << 6) + (t[3] >> 0));
173
174 convlen += 3-pad;
175
176 pad = i = 0;
177 }
178
179 in++;
180 ilen--;
181 }
182
183 return convlen;
184}
Willy Tarreauc01062b2010-10-07 19:27:29 +0200185
Moemen MHEDHBI92f7d432021-04-01 20:53:59 +0200186/* url variant of base64dec */
187/* The reverse tab used to decode base64 is generated via /dev/base64/base64rev-gen.c */
188int base64urldec(const char *in, size_t ilen, char *out, size_t olen)
189{
190 unsigned char t[4];
191 signed char b;
192 int convlen = 0, i = 0, pad = 0, padlen = 0;
193
194 if (olen < ((ilen / 4 * 3)))
195 return -2;
196
197 switch (ilen % 4) {
198 case 0:
199 break;
200 case 2:
201 padlen = pad = 2;
202 break;
203 case 3:
204 padlen = pad = 1;
205 break;
206 default:
207 return -1;
208 }
209
210 while (ilen + pad) {
211 if (ilen) {
212 /* if (*p < UB64CMIN || *p > B64CMAX) */
213 b = (signed char) * in - UB64CMIN;
214 if ((unsigned char)b > (B64CMAX - UB64CMIN))
215 return -1;
216
217 b = ubase64rev[b] - B64BASE - 1;
218 /* b == -1: invalid character */
219 if (b < 0)
220 return -1;
221
222 in++;
223 ilen--;
224
225 } else {
226 b = B64PADV;
227 pad--;
228 }
229
230 t[i++] = b;
231
232 if (i == 4) {
233 /*
234 * WARNING: we allow to write little more data than we
235 * should, but the checks from the beginning of the
236 * functions guarantee that we can safely do that.
237 */
238
239 /* xx000000 xx001111 xx111122 xx222222 */
240 out[convlen] = ((t[0] << 2) + (t[1] >> 4));
241 out[convlen + 1] = ((t[1] << 4) + (t[2] >> 2));
242 out[convlen + 2] = ((t[2] << 6) + (t[3] >> 0));
243
244 convlen += 3;
245 i = 0;
246 }
247 }
248 convlen -= padlen;
249
250 return convlen;
251}
Willy Tarreauc01062b2010-10-07 19:27:29 +0200252
253/* Converts the lower 30 bits of an integer to a 5-char base64 string. The
254 * caller is responsible for ensuring that the output buffer can accept 6 bytes
255 * (5 + the trailing zero). The pointer to the string is returned. The
256 * conversion is performed with MSB first and in a format that can be
257 * decoded with b64tos30(). This format is not padded and thus is not
258 * compatible with usual base64 routines.
259 */
260const char *s30tob64(int in, char *out)
261{
262 int i;
263 for (i = 0; i < 5; i++) {
264 out[i] = base64tab[(in >> 24) & 0x3F];
265 in <<= 6;
266 }
267 out[5] = '\0';
268 return out;
269}
270
271/* Converts a 5-char base64 string encoded by s30tob64() into a 30-bit integer.
272 * The caller is responsible for ensuring that the input contains at least 5
273 * chars. If any unexpected character is encountered, a negative value is
274 * returned. Otherwise the decoded value is returned.
275 */
276int b64tos30(const char *in)
277{
278 int i, out;
279 signed char b;
280
281 out = 0;
282 for (i = 0; i < 5; i++) {
283 b = (signed char)in[i] - B64CMIN;
284 if ((unsigned char)b > (B64CMAX - B64CMIN))
285 return -1; /* input character out of range */
286
287 b = base64rev[b] - B64BASE - 1;
288 if (b < 0) /* invalid character */
289 return -1;
290
291 if (b == B64PADV) /* padding not allowed */
292 return -1;
293
294 out = (out << 6) + b;
295 }
296 return out;
297}