base64.c 8.5 KB

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