base64.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, 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_DIGEST_AUTH) || \
  32. !defined(CURL_DISABLE_DOH) || defined(USE_SSL) || !defined(BUILDING_LIBCURL)
  33. #include <curl/curl.h>
  34. #include "warnless.h"
  35. #include "base64.h"
  36. /* The last 2 #include files should be in this order */
  37. #ifdef BUILDING_LIBCURL
  38. #include "../curl_memory.h"
  39. #endif
  40. #include "../memdebug.h"
  41. /* ---- Base64 Encoding/Decoding Table --- */
  42. const char Curl_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. * curlx_base64_decode()
  56. *
  57. * Given a base64 null-terminated string at src, decode it and return a
  58. * pointer in *outptr to a newly allocated memory area holding decoded data.
  59. * 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 curlx_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. /* Decode the complete quantums first */
  107. for(i = 0; i < fullQuantums; i++) {
  108. unsigned char val;
  109. unsigned int x = 0;
  110. int j;
  111. for(j = 0; j < 4; j++) {
  112. val = lookup[(unsigned char)*src++];
  113. if(val == 0xff) /* bad symbol */
  114. goto bad;
  115. x = (x << 6) | val;
  116. }
  117. pos[2] = x & 0xff;
  118. pos[1] = (x >> 8) & 0xff;
  119. pos[0] = (x >> 16) & 0xff;
  120. pos += 3;
  121. }
  122. if(padding) {
  123. /* this means either 8 or 16 bits output */
  124. unsigned char val;
  125. unsigned int x = 0;
  126. int j;
  127. size_t padc = 0;
  128. for(j = 0; j < 4; j++) {
  129. if(*src == '=') {
  130. x <<= 6;
  131. src++;
  132. if(++padc > padding)
  133. /* this is a badly placed '=' symbol! */
  134. goto bad;
  135. }
  136. else {
  137. val = lookup[(unsigned char)*src++];
  138. if(val == 0xff) /* bad symbol */
  139. goto bad;
  140. x = (x << 6) | val;
  141. }
  142. }
  143. if(padding == 1)
  144. pos[1] = (x >> 8) & 0xff;
  145. pos[0] = (x >> 16) & 0xff;
  146. pos += 3 - padding;
  147. }
  148. /* Zero terminate */
  149. *pos = '\0';
  150. /* Return the decoded data */
  151. *outptr = newstr;
  152. *outlen = rawlen;
  153. return CURLE_OK;
  154. bad:
  155. free(newstr);
  156. return CURLE_BAD_CONTENT_ENCODING;
  157. }
  158. static CURLcode base64_encode(const char *table64,
  159. unsigned char padbyte,
  160. const char *inputbuff, size_t insize,
  161. char **outptr, size_t *outlen)
  162. {
  163. char *output;
  164. char *base64data;
  165. const unsigned char *in = (const unsigned char *)inputbuff;
  166. *outptr = NULL;
  167. *outlen = 0;
  168. if(!insize)
  169. insize = strlen(inputbuff);
  170. #if SIZEOF_SIZE_T == 4
  171. if(insize > UINT_MAX/4)
  172. return CURLE_OUT_OF_MEMORY;
  173. #endif
  174. base64data = output = malloc((insize + 2) / 3 * 4 + 1);
  175. if(!output)
  176. return CURLE_OUT_OF_MEMORY;
  177. while(insize >= 3) {
  178. *output++ = table64[ in[0] >> 2 ];
  179. *output++ = table64[ ((in[0] & 0x03) << 4) | (in[1] >> 4) ];
  180. *output++ = table64[ ((in[1] & 0x0F) << 2) | ((in[2] & 0xC0) >> 6) ];
  181. *output++ = table64[ in[2] & 0x3F ];
  182. insize -= 3;
  183. in += 3;
  184. }
  185. if(insize) {
  186. /* this is only one or two bytes now */
  187. *output++ = table64[ in[0] >> 2 ];
  188. if(insize == 1) {
  189. *output++ = table64[ ((in[0] & 0x03) << 4) ];
  190. if(padbyte) {
  191. *output++ = padbyte;
  192. *output++ = padbyte;
  193. }
  194. }
  195. else {
  196. /* insize == 2 */
  197. *output++ = table64[ ((in[0] & 0x03) << 4) | ((in[1] & 0xF0) >> 4) ];
  198. *output++ = table64[ ((in[1] & 0x0F) << 2) ];
  199. if(padbyte)
  200. *output++ = padbyte;
  201. }
  202. }
  203. /* Zero terminate */
  204. *output = '\0';
  205. /* Return the pointer to the new data (allocated memory) */
  206. *outptr = base64data;
  207. /* Return the length of the new data */
  208. *outlen = (size_t)(output - base64data);
  209. return CURLE_OK;
  210. }
  211. /*
  212. * curlx_base64_encode()
  213. *
  214. * Given a pointer to an input buffer and an input size, encode it and
  215. * return a pointer in *outptr to a newly allocated memory area holding
  216. * encoded data. Size of encoded data is returned in variable pointed by
  217. * outlen.
  218. *
  219. * Input length of 0 indicates input buffer holds a null-terminated string.
  220. *
  221. * Returns CURLE_OK on success, otherwise specific error code. Function
  222. * output shall not be considered valid unless CURLE_OK is returned.
  223. *
  224. * @unittest: 1302
  225. */
  226. CURLcode curlx_base64_encode(const char *inputbuff, size_t insize,
  227. char **outptr, size_t *outlen)
  228. {
  229. return base64_encode(Curl_base64encdec, '=',
  230. inputbuff, insize, outptr, outlen);
  231. }
  232. /*
  233. * curlx_base64url_encode()
  234. *
  235. * Given a pointer to an input buffer and an input size, encode it and
  236. * return a pointer in *outptr to a newly allocated memory area holding
  237. * encoded data. Size of encoded data is returned in variable pointed by
  238. * outlen.
  239. *
  240. * Input length of 0 indicates input buffer holds a null-terminated string.
  241. *
  242. * Returns CURLE_OK on success, otherwise specific error code. Function
  243. * output shall not be considered valid unless CURLE_OK is returned.
  244. *
  245. * @unittest: 1302
  246. */
  247. CURLcode curlx_base64url_encode(const char *inputbuff, size_t insize,
  248. char **outptr, size_t *outlen)
  249. {
  250. return base64_encode(base64url, 0, inputbuff, insize, outptr, outlen);
  251. }
  252. #endif /* no users so disabled */