1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 /* Base64 encoding/decoding */
25 #include "curl_setup.h"
27 #if !defined(CURL_DISABLE_HTTP_AUTH) || defined(USE_SSH) || \
28 !defined(CURL_DISABLE_LDAP) || \
29 !defined(CURL_DISABLE_SMTP) || \
30 !defined(CURL_DISABLE_POP3) || \
31 !defined(CURL_DISABLE_IMAP) || \
32 !defined(CURL_DISABLE_DOH) || defined(USE_SSL)
34 #include "urldata.h" /* for the Curl_easy definition */
36 #include "curl_base64.h"
37 #include "non-ascii.h"
39 /* The last 3 #include files should be in this order */
40 #include "curl_printf.h"
41 #include "curl_memory.h"
44 /* ---- Base64 Encoding/Decoding Table --- */
45 static const char base64[]=
46 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
48 /* The Base 64 encoding with an URL and filename safe alphabet, RFC 4648
50 static const char base64url[]=
51 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
53 static size_t decodeQuantum(unsigned char *dest, const char *src)
57 unsigned long i, x = 0;
59 for(i = 0, s = src; i < 4; i++, s++) {
68 while(*p && (*p != *s)) {
81 dest[2] = curlx_ultouc(x & 0xFFUL);
85 dest[1] = curlx_ultouc(x & 0xFFUL);
88 dest[0] = curlx_ultouc(x & 0xFFUL);
94 * Curl_base64_decode()
96 * Given a base64 NUL-terminated string at src, decode it and return a
97 * pointer in *outptr to a newly allocated memory area holding decoded
98 * data. Size of decoded data is returned in variable pointed by outlen.
100 * Returns CURLE_OK on success, otherwise specific error code. Function
101 * output shall not be considered valid unless CURLE_OK is returned.
103 * When decoded data length is 0, returns NULL in *outptr.
107 CURLcode Curl_base64_decode(const char *src,
108 unsigned char **outptr, size_t *outlen)
117 unsigned char *newstr;
121 srclen = strlen(src);
123 /* Check the length of the input string is valid */
124 if(!srclen || srclen % 4)
125 return CURLE_BAD_CONTENT_ENCODING;
127 /* Find the position of any = padding characters */
128 while((src[length] != '=') && src[length])
131 /* A maximum of two = padding characters is allowed */
132 if(src[length] == '=') {
134 if(src[length + 1] == '=')
138 /* Check the = padding characters weren't part way through the input */
139 if(length + padding != srclen)
140 return CURLE_BAD_CONTENT_ENCODING;
142 /* Calculate the number of quantums */
143 numQuantums = srclen / 4;
145 /* Calculate the size of the decoded string */
146 rawlen = (numQuantums * 3) - padding;
148 /* Allocate our buffer including room for a zero terminator */
149 newstr = malloc(rawlen + 1);
151 return CURLE_OUT_OF_MEMORY;
155 /* Decode the quantums */
156 for(i = 0; i < numQuantums; i++) {
157 size_t result = decodeQuantum(pos, src);
161 return CURLE_BAD_CONTENT_ENCODING;
171 /* Return the decoded data */
178 static CURLcode base64_encode(const char *table64,
179 struct Curl_easy *data,
180 const char *inputbuff, size_t insize,
181 char **outptr, size_t *outlen)
184 unsigned char ibuf[3];
185 unsigned char obuf[4];
190 char *convbuf = NULL;
192 const char *indata = inputbuff;
198 insize = strlen(indata);
200 #if SIZEOF_SIZE_T == 4
201 if(insize > UINT_MAX/4)
202 return CURLE_OUT_OF_MEMORY;
205 base64data = output = malloc(insize * 4 / 3 + 4);
207 return CURLE_OUT_OF_MEMORY;
210 * The base64 data needs to be created using the network encoding
211 * not the host encoding. And we can't change the actual input
212 * so we copy it to a buffer, translate it, and use that instead.
214 result = Curl_convert_clone(data, indata, insize, &convbuf);
221 indata = (char *)convbuf;
224 for(i = inputparts = 0; i < 3; i++) {
227 ibuf[i] = (unsigned char) *indata;
235 obuf[0] = (unsigned char) ((ibuf[0] & 0xFC) >> 2);
236 obuf[1] = (unsigned char) (((ibuf[0] & 0x03) << 4) | \
237 ((ibuf[1] & 0xF0) >> 4));
238 obuf[2] = (unsigned char) (((ibuf[1] & 0x0F) << 2) | \
239 ((ibuf[2] & 0xC0) >> 6));
240 obuf[3] = (unsigned char) (ibuf[2] & 0x3F);
243 case 1: /* only one byte read */
244 msnprintf(output, 5, "%c%c==",
249 case 2: /* two bytes read */
250 msnprintf(output, 5, "%c%c%c=",
257 msnprintf(output, 5, "%c%c%c%c",
270 /* Return the pointer to the new data (allocated memory) */
271 *outptr = base64data;
275 /* Return the length of the new data */
276 *outlen = strlen(base64data);
282 * Curl_base64_encode()
284 * Given a pointer to an input buffer and an input size, encode it and
285 * return a pointer in *outptr to a newly allocated memory area holding
286 * encoded data. Size of encoded data is returned in variable pointed by
289 * Input length of 0 indicates input buffer holds a NUL-terminated string.
291 * Returns CURLE_OK on success, otherwise specific error code. Function
292 * output shall not be considered valid unless CURLE_OK is returned.
294 * When encoded data length is 0, returns NULL in *outptr.
298 CURLcode Curl_base64_encode(struct Curl_easy *data,
299 const char *inputbuff, size_t insize,
300 char **outptr, size_t *outlen)
302 return base64_encode(base64, data, inputbuff, insize, outptr, outlen);
306 * Curl_base64url_encode()
308 * Given a pointer to an input buffer and an input size, encode it and
309 * return a pointer in *outptr to a newly allocated memory area holding
310 * encoded data. Size of encoded data is returned in variable pointed by
313 * Input length of 0 indicates input buffer holds a NUL-terminated string.
315 * Returns CURLE_OK on success, otherwise specific error code. Function
316 * output shall not be considered valid unless CURLE_OK is returned.
318 * When encoded data length is 0, returns NULL in *outptr.
322 CURLcode Curl_base64url_encode(struct Curl_easy *data,
323 const char *inputbuff, size_t insize,
324 char **outptr, size_t *outlen)
326 return base64_encode(base64url, data, inputbuff, insize, outptr, outlen);
329 #endif /* no users so disabled */