base64.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, 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. ***************************************************************************/
  22. /* Base64 encoding/decoding */
  23. #include "curl_setup.h"
  24. #if !defined(CURL_DISABLE_HTTP_AUTH) || defined(USE_SSH) || \
  25. !defined(CURL_DISABLE_LDAP) || \
  26. !defined(CURL_DISABLE_SMTP) || \
  27. !defined(CURL_DISABLE_POP3) || \
  28. !defined(CURL_DISABLE_IMAP) || \
  29. !defined(CURL_DISABLE_DOH) || defined(USE_SSL)
  30. #include "urldata.h" /* for the Curl_easy definition */
  31. #include "warnless.h"
  32. #include "curl_base64.h"
  33. #include "non-ascii.h"
  34. /* The last 3 #include files should be in this order */
  35. #include "curl_printf.h"
  36. #include "curl_memory.h"
  37. #include "memdebug.h"
  38. /* ---- Base64 Encoding/Decoding Table --- */
  39. static const char base64[]=
  40. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  41. /* The Base 64 encoding with an URL and filename safe alphabet, RFC 4648
  42. section 5 */
  43. static const char base64url[]=
  44. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
  45. static size_t decodeQuantum(unsigned char *dest, const char *src)
  46. {
  47. size_t padding = 0;
  48. const char *s, *p;
  49. unsigned long i, x = 0;
  50. for(i = 0, s = src; i < 4; i++, s++) {
  51. if(*s == '=') {
  52. x = (x << 6);
  53. padding++;
  54. }
  55. else {
  56. unsigned long v = 0;
  57. p = base64;
  58. while(*p && (*p != *s)) {
  59. v++;
  60. p++;
  61. }
  62. if(*p == *s)
  63. x = (x << 6) + v;
  64. else
  65. return 0;
  66. }
  67. }
  68. if(padding < 1)
  69. dest[2] = curlx_ultouc(x & 0xFFUL);
  70. x >>= 8;
  71. if(padding < 2)
  72. dest[1] = curlx_ultouc(x & 0xFFUL);
  73. x >>= 8;
  74. dest[0] = curlx_ultouc(x & 0xFFUL);
  75. return 3 - padding;
  76. }
  77. /*
  78. * Curl_base64_decode()
  79. *
  80. * Given a base64 NUL-terminated string at src, decode it and return a
  81. * pointer in *outptr to a newly allocated memory area holding decoded
  82. * data. Size of decoded data is returned in variable pointed by outlen.
  83. *
  84. * Returns CURLE_OK on success, otherwise specific error code. Function
  85. * output shall not be considered valid unless CURLE_OK is returned.
  86. *
  87. * When decoded data length is 0, returns NULL in *outptr.
  88. *
  89. * @unittest: 1302
  90. */
  91. CURLcode Curl_base64_decode(const char *src,
  92. unsigned char **outptr, size_t *outlen)
  93. {
  94. size_t srclen = 0;
  95. size_t length = 0;
  96. size_t padding = 0;
  97. size_t i;
  98. size_t numQuantums;
  99. size_t rawlen = 0;
  100. unsigned char *pos;
  101. unsigned char *newstr;
  102. *outptr = NULL;
  103. *outlen = 0;
  104. srclen = strlen(src);
  105. /* Check the length of the input string is valid */
  106. if(!srclen || srclen % 4)
  107. return CURLE_BAD_CONTENT_ENCODING;
  108. /* Find the position of any = padding characters */
  109. while((src[length] != '=') && src[length])
  110. length++;
  111. /* A maximum of two = padding characters is allowed */
  112. if(src[length] == '=') {
  113. padding++;
  114. if(src[length + 1] == '=')
  115. padding++;
  116. }
  117. /* Check the = padding characters weren't part way through the input */
  118. if(length + padding != srclen)
  119. return CURLE_BAD_CONTENT_ENCODING;
  120. /* Calculate the number of quantums */
  121. numQuantums = srclen / 4;
  122. /* Calculate the size of the decoded string */
  123. rawlen = (numQuantums * 3) - padding;
  124. /* Allocate our buffer including room for a zero terminator */
  125. newstr = malloc(rawlen + 1);
  126. if(!newstr)
  127. return CURLE_OUT_OF_MEMORY;
  128. pos = newstr;
  129. /* Decode the quantums */
  130. for(i = 0; i < numQuantums; i++) {
  131. size_t result = decodeQuantum(pos, src);
  132. if(!result) {
  133. free(newstr);
  134. return CURLE_BAD_CONTENT_ENCODING;
  135. }
  136. pos += result;
  137. src += 4;
  138. }
  139. /* Zero terminate */
  140. *pos = '\0';
  141. /* Return the decoded data */
  142. *outptr = newstr;
  143. *outlen = rawlen;
  144. return CURLE_OK;
  145. }
  146. static CURLcode base64_encode(const char *table64,
  147. struct Curl_easy *data,
  148. const char *inputbuff, size_t insize,
  149. char **outptr, size_t *outlen)
  150. {
  151. CURLcode result;
  152. unsigned char ibuf[3];
  153. unsigned char obuf[4];
  154. int i;
  155. int inputparts;
  156. char *output;
  157. char *base64data;
  158. char *convbuf = NULL;
  159. const char *indata = inputbuff;
  160. *outptr = NULL;
  161. *outlen = 0;
  162. if(!insize)
  163. insize = strlen(indata);
  164. #if SIZEOF_SIZE_T == 4
  165. if(insize > UINT_MAX/4)
  166. return CURLE_OUT_OF_MEMORY;
  167. #endif
  168. base64data = output = malloc(insize * 4 / 3 + 4);
  169. if(!output)
  170. return CURLE_OUT_OF_MEMORY;
  171. /*
  172. * The base64 data needs to be created using the network encoding
  173. * not the host encoding. And we can't change the actual input
  174. * so we copy it to a buffer, translate it, and use that instead.
  175. */
  176. result = Curl_convert_clone(data, indata, insize, &convbuf);
  177. if(result) {
  178. free(output);
  179. return result;
  180. }
  181. if(convbuf)
  182. indata = (char *)convbuf;
  183. while(insize > 0) {
  184. for(i = inputparts = 0; i < 3; i++) {
  185. if(insize > 0) {
  186. inputparts++;
  187. ibuf[i] = (unsigned char) *indata;
  188. indata++;
  189. insize--;
  190. }
  191. else
  192. ibuf[i] = 0;
  193. }
  194. obuf[0] = (unsigned char) ((ibuf[0] & 0xFC) >> 2);
  195. obuf[1] = (unsigned char) (((ibuf[0] & 0x03) << 4) | \
  196. ((ibuf[1] & 0xF0) >> 4));
  197. obuf[2] = (unsigned char) (((ibuf[1] & 0x0F) << 2) | \
  198. ((ibuf[2] & 0xC0) >> 6));
  199. obuf[3] = (unsigned char) (ibuf[2] & 0x3F);
  200. switch(inputparts) {
  201. case 1: /* only one byte read */
  202. msnprintf(output, 5, "%c%c==",
  203. table64[obuf[0]],
  204. table64[obuf[1]]);
  205. break;
  206. case 2: /* two bytes read */
  207. msnprintf(output, 5, "%c%c%c=",
  208. table64[obuf[0]],
  209. table64[obuf[1]],
  210. table64[obuf[2]]);
  211. break;
  212. default:
  213. msnprintf(output, 5, "%c%c%c%c",
  214. table64[obuf[0]],
  215. table64[obuf[1]],
  216. table64[obuf[2]],
  217. table64[obuf[3]]);
  218. break;
  219. }
  220. output += 4;
  221. }
  222. /* Zero terminate */
  223. *output = '\0';
  224. /* Return the pointer to the new data (allocated memory) */
  225. *outptr = base64data;
  226. free(convbuf);
  227. /* Return the length of the new data */
  228. *outlen = strlen(base64data);
  229. return CURLE_OK;
  230. }
  231. /*
  232. * Curl_base64_encode()
  233. *
  234. * Given a pointer to an input buffer and an input size, encode it and
  235. * return a pointer in *outptr to a newly allocated memory area holding
  236. * encoded data. Size of encoded data is returned in variable pointed by
  237. * outlen.
  238. *
  239. * Input length of 0 indicates input buffer holds a NUL-terminated string.
  240. *
  241. * Returns CURLE_OK on success, otherwise specific error code. Function
  242. * output shall not be considered valid unless CURLE_OK is returned.
  243. *
  244. * When encoded data length is 0, returns NULL in *outptr.
  245. *
  246. * @unittest: 1302
  247. */
  248. CURLcode Curl_base64_encode(struct Curl_easy *data,
  249. const char *inputbuff, size_t insize,
  250. char **outptr, size_t *outlen)
  251. {
  252. return base64_encode(base64, data, inputbuff, insize, outptr, outlen);
  253. }
  254. /*
  255. * Curl_base64url_encode()
  256. *
  257. * Given a pointer to an input buffer and an input size, encode it and
  258. * return a pointer in *outptr to a newly allocated memory area holding
  259. * encoded data. Size of encoded data is returned in variable pointed by
  260. * outlen.
  261. *
  262. * Input length of 0 indicates input buffer holds a NUL-terminated string.
  263. *
  264. * Returns CURLE_OK on success, otherwise specific error code. Function
  265. * output shall not be considered valid unless CURLE_OK is returned.
  266. *
  267. * When encoded data length is 0, returns NULL in *outptr.
  268. *
  269. * @unittest: 1302
  270. */
  271. CURLcode Curl_base64url_encode(struct Curl_easy *data,
  272. const char *inputbuff, size_t insize,
  273. char **outptr, size_t *outlen)
  274. {
  275. return base64_encode(base64url, data, inputbuff, insize, outptr, outlen);
  276. }
  277. #endif /* no users so disabled */