Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

443 linhas
12 KiB

  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. /* Encoding. */
  62. static const unsigned char data_bin2ascii[65] =
  63. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  64. #define conv_bin2ascii(a) (data_bin2ascii[(a) & 0x3f])
  65. OPENSSL_COMPILE_ASSERT(sizeof(((EVP_ENCODE_CTX *)(NULL))->data) % 3 == 0,
  66. data_length_must_be_multiple_of_base64_chunk_size);
  67. int EVP_EncodedLength(size_t *out_len, size_t len) {
  68. if (len + 2 < len) {
  69. return 0;
  70. }
  71. len += 2;
  72. len /= 3;
  73. if (((len << 2) >> 2) != len) {
  74. return 0;
  75. }
  76. len <<= 2;
  77. if (len + 1 < len) {
  78. return 0;
  79. }
  80. len++;
  81. *out_len = len;
  82. return 1;
  83. }
  84. void EVP_EncodeInit(EVP_ENCODE_CTX *ctx) {
  85. memset(ctx, 0, sizeof(EVP_ENCODE_CTX));
  86. }
  87. void EVP_EncodeUpdate(EVP_ENCODE_CTX *ctx, uint8_t *out, int *out_len,
  88. const uint8_t *in, size_t in_len) {
  89. size_t total = 0;
  90. *out_len = 0;
  91. if (in_len == 0) {
  92. return;
  93. }
  94. assert(ctx->data_used < sizeof(ctx->data));
  95. if (sizeof(ctx->data) - ctx->data_used > in_len) {
  96. memcpy(&ctx->data[ctx->data_used], in, in_len);
  97. ctx->data_used += (unsigned)in_len;
  98. return;
  99. }
  100. if (ctx->data_used != 0) {
  101. const size_t todo = sizeof(ctx->data) - ctx->data_used;
  102. memcpy(&ctx->data[ctx->data_used], in, todo);
  103. in += todo;
  104. in_len -= todo;
  105. size_t encoded = EVP_EncodeBlock(out, ctx->data, sizeof(ctx->data));
  106. ctx->data_used = 0;
  107. out += encoded;
  108. *(out++) = '\n';
  109. *out = '\0';
  110. total = encoded + 1;
  111. }
  112. while (in_len >= sizeof(ctx->data)) {
  113. size_t encoded = EVP_EncodeBlock(out, in, sizeof(ctx->data));
  114. in += sizeof(ctx->data);
  115. in_len -= sizeof(ctx->data);
  116. out += encoded;
  117. *(out++) = '\n';
  118. *out = '\0';
  119. if (total + encoded + 1 < total) {
  120. *out_len = 0;
  121. return;
  122. }
  123. total += encoded + 1;
  124. }
  125. if (in_len != 0) {
  126. memcpy(ctx->data, in, in_len);
  127. }
  128. ctx->data_used = (unsigned)in_len;
  129. if (total > INT_MAX) {
  130. /* We cannot signal an error, but we can at least avoid making *out_len
  131. * negative. */
  132. total = 0;
  133. }
  134. *out_len = (int)total;
  135. }
  136. void EVP_EncodeFinal(EVP_ENCODE_CTX *ctx, uint8_t *out, int *out_len) {
  137. if (ctx->data_used == 0) {
  138. *out_len = 0;
  139. return;
  140. }
  141. size_t encoded = EVP_EncodeBlock(out, ctx->data, ctx->data_used);
  142. out[encoded++] = '\n';
  143. out[encoded] = '\0';
  144. ctx->data_used = 0;
  145. /* ctx->data_used is bounded by sizeof(ctx->data), so this does not
  146. * overflow. */
  147. assert(encoded <= INT_MAX);
  148. *out_len = (int)encoded;
  149. }
  150. size_t EVP_EncodeBlock(uint8_t *dst, const uint8_t *src, size_t src_len) {
  151. uint32_t l;
  152. size_t remaining = src_len, ret = 0;
  153. while (remaining) {
  154. if (remaining >= 3) {
  155. l = (((uint32_t)src[0]) << 16L) | (((uint32_t)src[1]) << 8L) | src[2];
  156. *(dst++) = conv_bin2ascii(l >> 18L);
  157. *(dst++) = conv_bin2ascii(l >> 12L);
  158. *(dst++) = conv_bin2ascii(l >> 6L);
  159. *(dst++) = conv_bin2ascii(l);
  160. remaining -= 3;
  161. } else {
  162. l = ((uint32_t)src[0]) << 16L;
  163. if (remaining == 2) {
  164. l |= ((uint32_t)src[1] << 8L);
  165. }
  166. *(dst++) = conv_bin2ascii(l >> 18L);
  167. *(dst++) = conv_bin2ascii(l >> 12L);
  168. *(dst++) = (remaining == 1) ? '=' : conv_bin2ascii(l >> 6L);
  169. *(dst++) = '=';
  170. remaining = 0;
  171. }
  172. ret += 4;
  173. src += 3;
  174. }
  175. *dst = '\0';
  176. return ret;
  177. }
  178. /* Decoding. */
  179. int EVP_DecodedLength(size_t *out_len, size_t len) {
  180. if (len % 4 != 0) {
  181. return 0;
  182. }
  183. *out_len = (len / 4) * 3;
  184. return 1;
  185. }
  186. void EVP_DecodeInit(EVP_ENCODE_CTX *ctx) {
  187. memset(ctx, 0, sizeof(EVP_ENCODE_CTX));
  188. }
  189. /* kBase64ASCIIToBinData maps characters (c < 128) to their base64 value, or
  190. * else 0xff if they are invalid. As a special case, the padding character
  191. * ('=') is mapped to zero. */
  192. static const uint8_t kBase64ASCIIToBinData[128] = {
  193. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0xff, 0xff,
  194. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  195. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0xff, 0xff, 0xff,
  196. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
  197. 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0xff, 0xff,
  198. 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
  199. 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
  200. 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
  201. 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24,
  202. 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
  203. 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
  204. };
  205. static uint8_t base64_ascii_to_bin(uint8_t a) {
  206. if (a >= 128) {
  207. return 0xFF;
  208. }
  209. return kBase64ASCIIToBinData[a];
  210. }
  211. /* base64_decode_quad decodes a single “quad” (i.e. four characters) of base64
  212. * data and writes up to three bytes to |out|. It sets |*out_num_bytes| to the
  213. * number of bytes written, which will be less than three if the quad ended
  214. * with padding. It returns one on success or zero on error. */
  215. static int base64_decode_quad(uint8_t *out, size_t *out_num_bytes,
  216. const uint8_t *in) {
  217. const uint8_t a = base64_ascii_to_bin(in[0]);
  218. const uint8_t b = base64_ascii_to_bin(in[1]);
  219. const uint8_t c = base64_ascii_to_bin(in[2]);
  220. const uint8_t d = base64_ascii_to_bin(in[3]);
  221. if (a == 0xff || b == 0xff || c == 0xff || d == 0xff) {
  222. return 0;
  223. }
  224. const uint32_t v = ((uint32_t)a) << 18 | ((uint32_t)b) << 12 |
  225. ((uint32_t)c) << 6 | (uint32_t)d;
  226. const unsigned padding_pattern = (in[0] == '=') << 3 |
  227. (in[1] == '=') << 2 |
  228. (in[2] == '=') << 1 |
  229. (in[3] == '=');
  230. switch (padding_pattern) {
  231. case 0:
  232. /* The common case of no padding. */
  233. *out_num_bytes = 3;
  234. out[0] = v >> 16;
  235. out[1] = v >> 8;
  236. out[2] = v;
  237. break;
  238. case 1: /* xxx= */
  239. *out_num_bytes = 2;
  240. out[0] = v >> 16;
  241. out[1] = v >> 8;
  242. break;
  243. case 3: /* xx== */
  244. *out_num_bytes = 1;
  245. out[0] = v >> 16;
  246. break;
  247. default:
  248. return 0;
  249. }
  250. return 1;
  251. }
  252. int EVP_DecodeUpdate(EVP_ENCODE_CTX *ctx, uint8_t *out, int *out_len,
  253. const uint8_t *in, size_t in_len) {
  254. *out_len = 0;
  255. if (ctx->error_encountered) {
  256. return -1;
  257. }
  258. size_t bytes_out = 0, i;
  259. for (i = 0; i < in_len; i++) {
  260. const char c = in[i];
  261. switch (c) {
  262. case ' ':
  263. case '\t':
  264. case '\r':
  265. case '\n':
  266. continue;
  267. }
  268. if (base64_ascii_to_bin(c) == 0xff || ctx->eof_seen) {
  269. ctx->error_encountered = 1;
  270. return -1;
  271. }
  272. ctx->data[ctx->data_used++] = c;
  273. if (ctx->data_used == 4) {
  274. size_t num_bytes_resulting;
  275. if (!base64_decode_quad(out, &num_bytes_resulting, ctx->data)) {
  276. ctx->error_encountered = 1;
  277. return -1;
  278. }
  279. ctx->data_used = 0;
  280. bytes_out += num_bytes_resulting;
  281. out += num_bytes_resulting;
  282. if (num_bytes_resulting < 3) {
  283. ctx->eof_seen = 1;
  284. }
  285. }
  286. }
  287. if (bytes_out > INT_MAX) {
  288. ctx->error_encountered = 1;
  289. *out_len = 0;
  290. return -1;
  291. }
  292. *out_len = (int)bytes_out;
  293. if (ctx->eof_seen) {
  294. return 0;
  295. }
  296. return 1;
  297. }
  298. int EVP_DecodeFinal(EVP_ENCODE_CTX *ctx, uint8_t *out, int *out_len) {
  299. *out_len = 0;
  300. if (ctx->error_encountered || ctx->data_used != 0) {
  301. return -1;
  302. }
  303. return 1;
  304. }
  305. int EVP_DecodeBase64(uint8_t *out, size_t *out_len, size_t max_out,
  306. const uint8_t *in, size_t in_len) {
  307. *out_len = 0;
  308. if (in_len % 4 != 0) {
  309. return 0;
  310. }
  311. size_t max_len;
  312. if (!EVP_DecodedLength(&max_len, in_len) ||
  313. max_out < max_len) {
  314. return 0;
  315. }
  316. size_t i, bytes_out = 0;
  317. for (i = 0; i < in_len; i += 4) {
  318. size_t num_bytes_resulting;
  319. if (!base64_decode_quad(out, &num_bytes_resulting, &in[i])) {
  320. return 0;
  321. }
  322. bytes_out += num_bytes_resulting;
  323. out += num_bytes_resulting;
  324. if (num_bytes_resulting != 3 && i != in_len - 4) {
  325. return 0;
  326. }
  327. }
  328. *out_len = bytes_out;
  329. return 1;
  330. }
  331. int EVP_DecodeBlock(uint8_t *dst, const uint8_t *src, size_t src_len) {
  332. /* Trim spaces and tabs from the beginning of the input. */
  333. while (src_len > 0) {
  334. if (src[0] != ' ' && src[0] != '\t') {
  335. break;
  336. }
  337. src++;
  338. src_len--;
  339. }
  340. /* Trim newlines, spaces and tabs from the end of the line. */
  341. while (src_len > 0) {
  342. switch (src[src_len-1]) {
  343. case ' ':
  344. case '\t':
  345. case '\r':
  346. case '\n':
  347. src_len--;
  348. continue;
  349. }
  350. break;
  351. }
  352. size_t dst_len;
  353. if (!EVP_DecodedLength(&dst_len, src_len) ||
  354. dst_len > INT_MAX ||
  355. !EVP_DecodeBase64(dst, &dst_len, dst_len, src, src_len)) {
  356. return -1;
  357. }
  358. /* EVP_DecodeBlock does not take padding into account, so put the
  359. * NULs back in... so the caller can strip them back out. */
  360. while (dst_len % 3 != 0) {
  361. dst[dst_len++] = '\0';
  362. }
  363. assert(dst_len <= INT_MAX);
  364. return (int)dst_len;
  365. }