base64.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  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.se/docs/copyright.html.
  13. *
  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.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. /* Base64 encoding/decoding */
  25. #include "curl_setup.h"
  26. #if !defined(CURL_DISABLE_HTTP_AUTH) || defined(USE_SSH) || \
  27. !defined(CURL_DISABLE_LDAP) || \
  28. !defined(CURL_DISABLE_SMTP) || \
  29. !defined(CURL_DISABLE_POP3) || \
  30. !defined(CURL_DISABLE_IMAP) || \
  31. !defined(CURL_DISABLE_DOH) || defined(USE_SSL)
  32. #include "curl/curl.h"
  33. #include "warnless.h"
  34. #include "curl_base64.h"
  35. /* The last 2 #include files should be in this order */
  36. #ifdef BUILDING_LIBCURL
  37. #include "curl_memory.h"
  38. #endif
  39. #include "memdebug.h"
  40. /* ---- Base64 Encoding/Decoding Table --- */
  41. /* Padding character string starts at offset 64. */
  42. static const char base64encdec[]=
  43. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  44. /* The Base 64 encoding with a URL and filename safe alphabet, RFC 4648
  45. section 5 */
  46. static const char base64url[]=
  47. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
  48. static const unsigned char decodetable[] =
  49. { 62, 255, 255, 255, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 255,
  50. 255, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
  51. 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255, 255, 26, 27, 28,
  52. 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
  53. 48, 49, 50, 51 };
  54. /*
  55. * Curl_base64_decode()
  56. *
  57. * Given a base64 NUL-terminated string at src, decode it and return a
  58. * pointer in *outptr to a newly allocated memory area holding decoded
  59. * data. Size of decoded data is returned in variable pointed by outlen.
  60. *
  61. * Returns CURLE_OK on success, otherwise specific error code. Function
  62. * output shall not be considered valid unless CURLE_OK is returned.
  63. *
  64. * When decoded data length is 0, returns NULL in *outptr.
  65. *
  66. * @unittest: 1302
  67. */
  68. CURLcode Curl_base64_decode(const char *src,
  69. unsigned char **outptr, size_t *outlen)
  70. {
  71. size_t srclen = 0;
  72. size_t padding = 0;
  73. size_t i;
  74. size_t numQuantums;
  75. size_t fullQuantums;
  76. size_t rawlen = 0;
  77. unsigned char *pos;
  78. unsigned char *newstr;
  79. unsigned char lookup[256];
  80. *outptr = NULL;
  81. *outlen = 0;
  82. srclen = strlen(src);
  83. /* Check the length of the input string is valid */
  84. if(!srclen || srclen % 4)
  85. return CURLE_BAD_CONTENT_ENCODING;
  86. /* srclen is at least 4 here */
  87. while(src[srclen - 1 - padding] == '=') {
  88. /* count padding characters */
  89. padding++;
  90. /* A maximum of two = padding characters is allowed */
  91. if(padding > 2)
  92. return CURLE_BAD_CONTENT_ENCODING;
  93. }
  94. /* Calculate the number of quantums */
  95. numQuantums = srclen / 4;
  96. fullQuantums = numQuantums - (padding ? 1 : 0);
  97. /* Calculate the size of the decoded string */
  98. rawlen = (numQuantums * 3) - padding;
  99. /* Allocate our buffer including room for a null-terminator */
  100. newstr = malloc(rawlen + 1);
  101. if(!newstr)
  102. return CURLE_OUT_OF_MEMORY;
  103. pos = newstr;
  104. memset(lookup, 0xff, sizeof(lookup));
  105. memcpy(&lookup['+'], decodetable, sizeof(decodetable));
  106. /* replaces
  107. {
  108. unsigned char c;
  109. const unsigned char *p = (const unsigned char *)base64encdec;
  110. for(c = 0; *p; c++, p++)
  111. lookup[*p] = c;
  112. }
  113. */
  114. /* Decode the complete quantums first */
  115. for(i = 0; i < fullQuantums; i++) {
  116. unsigned char val;
  117. unsigned int x = 0;
  118. int j;
  119. for(j = 0; j < 4; j++) {
  120. val = lookup[(unsigned char)*src++];
  121. if(val == 0xff) /* bad symbol */
  122. goto bad;
  123. x = (x << 6) | val;
  124. }
  125. pos[2] = x & 0xff;
  126. pos[1] = (x >> 8) & 0xff;
  127. pos[0] = (x >> 16) & 0xff;
  128. pos += 3;
  129. }
  130. if(padding) {
  131. /* this means either 8 or 16 bits output */
  132. unsigned char val;
  133. unsigned int x = 0;
  134. int j;
  135. size_t padc = 0;
  136. for(j = 0; j < 4; j++) {
  137. if(*src == '=') {
  138. x <<= 6;
  139. src++;
  140. if(++padc > padding)
  141. /* this is a badly placed '=' symbol! */
  142. goto bad;
  143. }
  144. else {
  145. val = lookup[(unsigned char)*src++];
  146. if(val == 0xff) /* bad symbol */
  147. goto bad;
  148. x = (x << 6) | val;
  149. }
  150. }
  151. if(padding == 1)
  152. pos[1] = (x >> 8) & 0xff;
  153. pos[0] = (x >> 16) & 0xff;
  154. pos += 3 - padding;
  155. }
  156. /* Zero terminate */
  157. *pos = '\0';
  158. /* Return the decoded data */
  159. *outptr = newstr;
  160. *outlen = rawlen;
  161. return CURLE_OK;
  162. bad:
  163. free(newstr);
  164. return CURLE_BAD_CONTENT_ENCODING;
  165. }
  166. static CURLcode base64_encode(const char *table64,
  167. const char *inputbuff, size_t insize,
  168. char **outptr, size_t *outlen)
  169. {
  170. char *output;
  171. char *base64data;
  172. const unsigned char *in = (unsigned char *)inputbuff;
  173. const char *padstr = &table64[64]; /* Point to padding string. */
  174. *outptr = NULL;
  175. *outlen = 0;
  176. if(!insize)
  177. insize = strlen(inputbuff);
  178. #if SIZEOF_SIZE_T == 4
  179. if(insize > UINT_MAX/4)
  180. return CURLE_OUT_OF_MEMORY;
  181. #endif
  182. base64data = output = malloc((insize + 2) / 3 * 4 + 1);
  183. if(!output)
  184. return CURLE_OUT_OF_MEMORY;
  185. while(insize >= 3) {
  186. *output++ = table64[ in[0] >> 2 ];
  187. *output++ = table64[ ((in[0] & 0x03) << 4) | (in[1] >> 4) ];
  188. *output++ = table64[ ((in[1] & 0x0F) << 2) | ((in[2] & 0xC0) >> 6) ];
  189. *output++ = table64[ in[2] & 0x3F ];
  190. insize -= 3;
  191. in += 3;
  192. }
  193. if(insize) {
  194. /* this is only one or two bytes now */
  195. *output++ = table64[ in[0] >> 2 ];
  196. if(insize == 1) {
  197. *output++ = table64[ ((in[0] & 0x03) << 4) ];
  198. if(*padstr) {
  199. *output++ = *padstr;
  200. *output++ = *padstr;
  201. }
  202. }
  203. else {
  204. /* insize == 2 */
  205. *output++ = table64[ ((in[0] & 0x03) << 4) | ((in[1] & 0xF0) >> 4) ];
  206. *output++ = table64[ ((in[1] & 0x0F) << 2) ];
  207. if(*padstr)
  208. *output++ = *padstr;
  209. }
  210. }
  211. /* Zero terminate */
  212. *output = '\0';
  213. /* Return the pointer to the new data (allocated memory) */
  214. *outptr = base64data;
  215. /* Return the length of the new data */
  216. *outlen = output - base64data;
  217. return CURLE_OK;
  218. }
  219. /*
  220. * Curl_base64_encode()
  221. *
  222. * Given a pointer to an input buffer and an input size, encode it and
  223. * return a pointer in *outptr to a newly allocated memory area holding
  224. * encoded data. Size of encoded data is returned in variable pointed by
  225. * outlen.
  226. *
  227. * Input length of 0 indicates input buffer holds a NUL-terminated string.
  228. *
  229. * Returns CURLE_OK on success, otherwise specific error code. Function
  230. * output shall not be considered valid unless CURLE_OK is returned.
  231. *
  232. * @unittest: 1302
  233. */
  234. CURLcode Curl_base64_encode(const char *inputbuff, size_t insize,
  235. char **outptr, size_t *outlen)
  236. {
  237. return base64_encode(base64encdec, inputbuff, insize, outptr, outlen);
  238. }
  239. /*
  240. * Curl_base64url_encode()
  241. *
  242. * Given a pointer to an input buffer and an input size, encode it and
  243. * return a pointer in *outptr to a newly allocated memory area holding
  244. * encoded data. Size of encoded data is returned in variable pointed by
  245. * outlen.
  246. *
  247. * Input length of 0 indicates input buffer holds a NUL-terminated string.
  248. *
  249. * Returns CURLE_OK on success, otherwise specific error code. Function
  250. * output shall not be considered valid unless CURLE_OK is returned.
  251. *
  252. * @unittest: 1302
  253. */
  254. CURLcode Curl_base64url_encode(const char *inputbuff, size_t insize,
  255. char **outptr, size_t *outlen)
  256. {
  257. return base64_encode(base64url, inputbuff, insize, outptr, outlen);
  258. }
  259. #endif /* no users so disabled */