base64.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <openssl/base64.h>
  57. #include <assert.h>
  58. #include <limits.h>
  59. #include <string.h>
  60. #include <openssl/type_check.h>
  61. #include "../internal.h"
  62. /* Encoding. */
  63. static const unsigned char data_bin2ascii[65] =
  64. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  65. #define conv_bin2ascii(a) (data_bin2ascii[(a) & 0x3f])
  66. OPENSSL_COMPILE_ASSERT(sizeof(((EVP_ENCODE_CTX *)(NULL))->data) % 3 == 0,
  67. data_length_must_be_multiple_of_base64_chunk_size);
  68. int EVP_EncodedLength(size_t *out_len, size_t len) {
  69. if (len + 2 < len) {
  70. return 0;
  71. }
  72. len += 2;
  73. len /= 3;
  74. if (((len << 2) >> 2) != len) {
  75. return 0;
  76. }
  77. len <<= 2;
  78. if (len + 1 < len) {
  79. return 0;
  80. }
  81. len++;
  82. *out_len = len;
  83. return 1;
  84. }
  85. void EVP_EncodeInit(EVP_ENCODE_CTX *ctx) {
  86. OPENSSL_memset(ctx, 0, sizeof(EVP_ENCODE_CTX));
  87. }
  88. void EVP_EncodeUpdate(EVP_ENCODE_CTX *ctx, uint8_t *out, int *out_len,
  89. const uint8_t *in, size_t in_len) {
  90. size_t total = 0;
  91. *out_len = 0;
  92. if (in_len == 0) {
  93. return;
  94. }
  95. assert(ctx->data_used < sizeof(ctx->data));
  96. if (sizeof(ctx->data) - ctx->data_used > in_len) {
  97. OPENSSL_memcpy(&ctx->data[ctx->data_used], in, in_len);
  98. ctx->data_used += (unsigned)in_len;
  99. return;
  100. }
  101. if (ctx->data_used != 0) {
  102. const size_t todo = sizeof(ctx->data) - ctx->data_used;
  103. OPENSSL_memcpy(&ctx->data[ctx->data_used], in, todo);
  104. in += todo;
  105. in_len -= todo;
  106. size_t encoded = EVP_EncodeBlock(out, ctx->data, sizeof(ctx->data));
  107. ctx->data_used = 0;
  108. out += encoded;
  109. *(out++) = '\n';
  110. *out = '\0';
  111. total = encoded + 1;
  112. }
  113. while (in_len >= sizeof(ctx->data)) {
  114. size_t encoded = EVP_EncodeBlock(out, in, sizeof(ctx->data));
  115. in += sizeof(ctx->data);
  116. in_len -= sizeof(ctx->data);
  117. out += encoded;
  118. *(out++) = '\n';
  119. *out = '\0';
  120. if (total + encoded + 1 < total) {
  121. *out_len = 0;
  122. return;
  123. }
  124. total += encoded + 1;
  125. }
  126. if (in_len != 0) {
  127. OPENSSL_memcpy(ctx->data, in, in_len);
  128. }
  129. ctx->data_used = (unsigned)in_len;
  130. if (total > INT_MAX) {
  131. /* We cannot signal an error, but we can at least avoid making *out_len
  132. * negative. */
  133. total = 0;
  134. }
  135. *out_len = (int)total;
  136. }
  137. void EVP_EncodeFinal(EVP_ENCODE_CTX *ctx, uint8_t *out, int *out_len) {
  138. if (ctx->data_used == 0) {
  139. *out_len = 0;
  140. return;
  141. }
  142. size_t encoded = EVP_EncodeBlock(out, ctx->data, ctx->data_used);
  143. out[encoded++] = '\n';
  144. out[encoded] = '\0';
  145. ctx->data_used = 0;
  146. /* ctx->data_used is bounded by sizeof(ctx->data), so this does not
  147. * overflow. */
  148. assert(encoded <= INT_MAX);
  149. *out_len = (int)encoded;
  150. }
  151. size_t EVP_EncodeBlock(uint8_t *dst, const uint8_t *src, size_t src_len) {
  152. uint32_t l;
  153. size_t remaining = src_len, ret = 0;
  154. while (remaining) {
  155. if (remaining >= 3) {
  156. l = (((uint32_t)src[0]) << 16L) | (((uint32_t)src[1]) << 8L) | src[2];
  157. *(dst++) = conv_bin2ascii(l >> 18L);
  158. *(dst++) = conv_bin2ascii(l >> 12L);
  159. *(dst++) = conv_bin2ascii(l >> 6L);
  160. *(dst++) = conv_bin2ascii(l);
  161. remaining -= 3;
  162. } else {
  163. l = ((uint32_t)src[0]) << 16L;
  164. if (remaining == 2) {
  165. l |= ((uint32_t)src[1] << 8L);
  166. }
  167. *(dst++) = conv_bin2ascii(l >> 18L);
  168. *(dst++) = conv_bin2ascii(l >> 12L);
  169. *(dst++) = (remaining == 1) ? '=' : conv_bin2ascii(l >> 6L);
  170. *(dst++) = '=';
  171. remaining = 0;
  172. }
  173. ret += 4;
  174. src += 3;
  175. }
  176. *dst = '\0';
  177. return ret;
  178. }
  179. /* Decoding. */
  180. int EVP_DecodedLength(size_t *out_len, size_t len) {
  181. if (len % 4 != 0) {
  182. return 0;
  183. }
  184. *out_len = (len / 4) * 3;
  185. return 1;
  186. }
  187. void EVP_DecodeInit(EVP_ENCODE_CTX *ctx) {
  188. OPENSSL_memset(ctx, 0, sizeof(EVP_ENCODE_CTX));
  189. }
  190. /* kBase64ASCIIToBinData maps characters (c < 128) to their base64 value, or
  191. * else 0xff if they are invalid. As a special case, the padding character
  192. * ('=') is mapped to zero. */
  193. static const uint8_t kBase64ASCIIToBinData[128] = {
  194. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0xff, 0xff,
  195. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  196. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0xff, 0xff, 0xff,
  197. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
  198. 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0xff, 0xff,
  199. 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
  200. 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
  201. 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
  202. 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24,
  203. 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
  204. 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
  205. };
  206. static uint8_t base64_ascii_to_bin(uint8_t a) {
  207. if (a >= 128) {
  208. return 0xFF;
  209. }
  210. return kBase64ASCIIToBinData[a];
  211. }
  212. /* base64_decode_quad decodes a single “quad” (i.e. four characters) of base64
  213. * data and writes up to three bytes to |out|. It sets |*out_num_bytes| to the
  214. * number of bytes written, which will be less than three if the quad ended
  215. * with padding. It returns one on success or zero on error. */
  216. static int base64_decode_quad(uint8_t *out, size_t *out_num_bytes,
  217. const uint8_t *in) {
  218. const uint8_t a = base64_ascii_to_bin(in[0]);
  219. const uint8_t b = base64_ascii_to_bin(in[1]);
  220. const uint8_t c = base64_ascii_to_bin(in[2]);
  221. const uint8_t d = base64_ascii_to_bin(in[3]);
  222. if (a == 0xff || b == 0xff || c == 0xff || d == 0xff) {
  223. return 0;
  224. }
  225. const uint32_t v = ((uint32_t)a) << 18 | ((uint32_t)b) << 12 |
  226. ((uint32_t)c) << 6 | (uint32_t)d;
  227. const unsigned padding_pattern = (in[0] == '=') << 3 |
  228. (in[1] == '=') << 2 |
  229. (in[2] == '=') << 1 |
  230. (in[3] == '=');
  231. switch (padding_pattern) {
  232. case 0:
  233. /* The common case of no padding. */
  234. *out_num_bytes = 3;
  235. out[0] = v >> 16;
  236. out[1] = v >> 8;
  237. out[2] = v;
  238. break;
  239. case 1: /* xxx= */
  240. *out_num_bytes = 2;
  241. out[0] = v >> 16;
  242. out[1] = v >> 8;
  243. break;
  244. case 3: /* xx== */
  245. *out_num_bytes = 1;
  246. out[0] = v >> 16;
  247. break;
  248. default:
  249. return 0;
  250. }
  251. return 1;
  252. }
  253. int EVP_DecodeUpdate(EVP_ENCODE_CTX *ctx, uint8_t *out, int *out_len,
  254. const uint8_t *in, size_t in_len) {
  255. *out_len = 0;
  256. if (ctx->error_encountered) {
  257. return -1;
  258. }
  259. size_t bytes_out = 0, i;
  260. for (i = 0; i < in_len; i++) {
  261. const char c = in[i];
  262. switch (c) {
  263. case ' ':
  264. case '\t':
  265. case '\r':
  266. case '\n':
  267. continue;
  268. }
  269. if (base64_ascii_to_bin(c) == 0xff || ctx->eof_seen) {
  270. ctx->error_encountered = 1;
  271. return -1;
  272. }
  273. ctx->data[ctx->data_used++] = c;
  274. if (ctx->data_used == 4) {
  275. size_t num_bytes_resulting;
  276. if (!base64_decode_quad(out, &num_bytes_resulting, ctx->data)) {
  277. ctx->error_encountered = 1;
  278. return -1;
  279. }
  280. ctx->data_used = 0;
  281. bytes_out += num_bytes_resulting;
  282. out += num_bytes_resulting;
  283. if (num_bytes_resulting < 3) {
  284. ctx->eof_seen = 1;
  285. }
  286. }
  287. }
  288. if (bytes_out > INT_MAX) {
  289. ctx->error_encountered = 1;
  290. *out_len = 0;
  291. return -1;
  292. }
  293. *out_len = (int)bytes_out;
  294. if (ctx->eof_seen) {
  295. return 0;
  296. }
  297. return 1;
  298. }
  299. int EVP_DecodeFinal(EVP_ENCODE_CTX *ctx, uint8_t *out, int *out_len) {
  300. *out_len = 0;
  301. if (ctx->error_encountered || ctx->data_used != 0) {
  302. return -1;
  303. }
  304. return 1;
  305. }
  306. int EVP_DecodeBase64(uint8_t *out, size_t *out_len, size_t max_out,
  307. const uint8_t *in, size_t in_len) {
  308. *out_len = 0;
  309. if (in_len % 4 != 0) {
  310. return 0;
  311. }
  312. size_t max_len;
  313. if (!EVP_DecodedLength(&max_len, in_len) ||
  314. max_out < max_len) {
  315. return 0;
  316. }
  317. size_t i, bytes_out = 0;
  318. for (i = 0; i < in_len; i += 4) {
  319. size_t num_bytes_resulting;
  320. if (!base64_decode_quad(out, &num_bytes_resulting, &in[i])) {
  321. return 0;
  322. }
  323. bytes_out += num_bytes_resulting;
  324. out += num_bytes_resulting;
  325. if (num_bytes_resulting != 3 && i != in_len - 4) {
  326. return 0;
  327. }
  328. }
  329. *out_len = bytes_out;
  330. return 1;
  331. }
  332. int EVP_DecodeBlock(uint8_t *dst, const uint8_t *src, size_t src_len) {
  333. /* Trim spaces and tabs from the beginning of the input. */
  334. while (src_len > 0) {
  335. if (src[0] != ' ' && src[0] != '\t') {
  336. break;
  337. }
  338. src++;
  339. src_len--;
  340. }
  341. /* Trim newlines, spaces and tabs from the end of the line. */
  342. while (src_len > 0) {
  343. switch (src[src_len-1]) {
  344. case ' ':
  345. case '\t':
  346. case '\r':
  347. case '\n':
  348. src_len--;
  349. continue;
  350. }
  351. break;
  352. }
  353. size_t dst_len;
  354. if (!EVP_DecodedLength(&dst_len, src_len) ||
  355. dst_len > INT_MAX ||
  356. !EVP_DecodeBase64(dst, &dst_len, dst_len, src, src_len)) {
  357. return -1;
  358. }
  359. /* EVP_DecodeBlock does not take padding into account, so put the
  360. * NULs back in... so the caller can strip them back out. */
  361. while (dst_len % 3 != 0) {
  362. dst[dst_len++] = '\0';
  363. }
  364. assert(dst_len <= INT_MAX);
  365. return (int)dst_len;
  366. }