encode.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <limits.h>
  11. #include "internal/cryptlib.h"
  12. #include <openssl/evp.h>
  13. #include "crypto/evp.h"
  14. #include "evp_local.h"
  15. static unsigned char conv_ascii2bin(unsigned char a,
  16. const unsigned char *table);
  17. static int evp_encodeblock_int(EVP_ENCODE_CTX *ctx, unsigned char *t,
  18. const unsigned char *f, int dlen);
  19. static int evp_decodeblock_int(EVP_ENCODE_CTX *ctx, unsigned char *t,
  20. const unsigned char *f, int n);
  21. #ifndef CHARSET_EBCDIC
  22. # define conv_bin2ascii(a, table) ((table)[(a)&0x3f])
  23. #else
  24. /*
  25. * We assume that PEM encoded files are EBCDIC files (i.e., printable text
  26. * files). Convert them here while decoding. When encoding, output is EBCDIC
  27. * (text) format again. (No need for conversion in the conv_bin2ascii macro,
  28. * as the underlying textstring data_bin2ascii[] is already EBCDIC)
  29. */
  30. # define conv_bin2ascii(a, table) ((table)[(a)&0x3f])
  31. #endif
  32. /*-
  33. * 64 char lines
  34. * pad input with 0
  35. * left over chars are set to =
  36. * 1 byte => xx==
  37. * 2 bytes => xxx=
  38. * 3 bytes => xxxx
  39. */
  40. #define BIN_PER_LINE (64/4*3)
  41. #define CHUNKS_PER_LINE (64/4)
  42. #define CHAR_PER_LINE (64+1)
  43. static const unsigned char data_bin2ascii[65] =
  44. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  45. /* SRP uses a different base64 alphabet */
  46. static const unsigned char srpdata_bin2ascii[65] =
  47. "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz./";
  48. /*-
  49. * 0xF0 is a EOLN
  50. * 0xF1 is ignore but next needs to be 0xF0 (for \r\n processing).
  51. * 0xF2 is EOF
  52. * 0xE0 is ignore at start of line.
  53. * 0xFF is error
  54. */
  55. #define B64_EOLN 0xF0
  56. #define B64_CR 0xF1
  57. #define B64_EOF 0xF2
  58. #define B64_WS 0xE0
  59. #define B64_ERROR 0xFF
  60. #define B64_NOT_BASE64(a) (((a)|0x13) == 0xF3)
  61. #define B64_BASE64(a) (!B64_NOT_BASE64(a))
  62. static const unsigned char data_ascii2bin[128] = {
  63. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  64. 0xFF, 0xE0, 0xF0, 0xFF, 0xFF, 0xF1, 0xFF, 0xFF,
  65. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  66. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  67. 0xE0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  68. 0xFF, 0xFF, 0xFF, 0x3E, 0xFF, 0xF2, 0xFF, 0x3F,
  69. 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B,
  70. 0x3C, 0x3D, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0xFF,
  71. 0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
  72. 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
  73. 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
  74. 0x17, 0x18, 0x19, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  75. 0xFF, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20,
  76. 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
  77. 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30,
  78. 0x31, 0x32, 0x33, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  79. };
  80. static const unsigned char srpdata_ascii2bin[128] = {
  81. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  82. 0xFF, 0xE0, 0xF0, 0xFF, 0xFF, 0xF1, 0xFF, 0xFF,
  83. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  84. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  85. 0xE0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  86. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF2, 0x3E, 0x3F,
  87. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  88. 0x08, 0x09, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0xFF,
  89. 0xFF, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10,
  90. 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
  91. 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20,
  92. 0x21, 0x22, 0x23, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  93. 0xFF, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A,
  94. 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32,
  95. 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A,
  96. 0x3B, 0x3C, 0x3D, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  97. };
  98. #ifndef CHARSET_EBCDIC
  99. static unsigned char conv_ascii2bin(unsigned char a, const unsigned char *table)
  100. {
  101. if (a & 0x80)
  102. return B64_ERROR;
  103. return table[a];
  104. }
  105. #else
  106. static unsigned char conv_ascii2bin(unsigned char a, const unsigned char *table)
  107. {
  108. a = os_toascii[a];
  109. if (a & 0x80)
  110. return B64_ERROR;
  111. return table[a];
  112. }
  113. #endif
  114. EVP_ENCODE_CTX *EVP_ENCODE_CTX_new(void)
  115. {
  116. return OPENSSL_zalloc(sizeof(EVP_ENCODE_CTX));
  117. }
  118. void EVP_ENCODE_CTX_free(EVP_ENCODE_CTX *ctx)
  119. {
  120. OPENSSL_free(ctx);
  121. }
  122. int EVP_ENCODE_CTX_copy(EVP_ENCODE_CTX *dctx, const EVP_ENCODE_CTX *sctx)
  123. {
  124. memcpy(dctx, sctx, sizeof(EVP_ENCODE_CTX));
  125. return 1;
  126. }
  127. int EVP_ENCODE_CTX_num(EVP_ENCODE_CTX *ctx)
  128. {
  129. return ctx->num;
  130. }
  131. void evp_encode_ctx_set_flags(EVP_ENCODE_CTX *ctx, unsigned int flags)
  132. {
  133. ctx->flags = flags;
  134. }
  135. void EVP_EncodeInit(EVP_ENCODE_CTX *ctx)
  136. {
  137. ctx->length = 48;
  138. ctx->num = 0;
  139. ctx->line_num = 0;
  140. ctx->flags = 0;
  141. }
  142. int EVP_EncodeUpdate(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl,
  143. const unsigned char *in, int inl)
  144. {
  145. int i, j;
  146. size_t total = 0;
  147. *outl = 0;
  148. if (inl <= 0)
  149. return 0;
  150. OPENSSL_assert(ctx->length <= (int)sizeof(ctx->enc_data));
  151. if (ctx->length - ctx->num > inl) {
  152. memcpy(&(ctx->enc_data[ctx->num]), in, inl);
  153. ctx->num += inl;
  154. return 1;
  155. }
  156. if (ctx->num != 0) {
  157. i = ctx->length - ctx->num;
  158. memcpy(&(ctx->enc_data[ctx->num]), in, i);
  159. in += i;
  160. inl -= i;
  161. j = evp_encodeblock_int(ctx, out, ctx->enc_data, ctx->length);
  162. ctx->num = 0;
  163. out += j;
  164. total = j;
  165. if ((ctx->flags & EVP_ENCODE_CTX_NO_NEWLINES) == 0) {
  166. *(out++) = '\n';
  167. total++;
  168. }
  169. *out = '\0';
  170. }
  171. while (inl >= ctx->length && total <= INT_MAX) {
  172. j = evp_encodeblock_int(ctx, out, in, ctx->length);
  173. in += ctx->length;
  174. inl -= ctx->length;
  175. out += j;
  176. total += j;
  177. if ((ctx->flags & EVP_ENCODE_CTX_NO_NEWLINES) == 0) {
  178. *(out++) = '\n';
  179. total++;
  180. }
  181. *out = '\0';
  182. }
  183. if (total > INT_MAX) {
  184. /* Too much output data! */
  185. *outl = 0;
  186. return 0;
  187. }
  188. if (inl != 0)
  189. memcpy(&(ctx->enc_data[0]), in, inl);
  190. ctx->num = inl;
  191. *outl = total;
  192. return 1;
  193. }
  194. void EVP_EncodeFinal(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl)
  195. {
  196. unsigned int ret = 0;
  197. if (ctx->num != 0) {
  198. ret = evp_encodeblock_int(ctx, out, ctx->enc_data, ctx->num);
  199. if ((ctx->flags & EVP_ENCODE_CTX_NO_NEWLINES) == 0)
  200. out[ret++] = '\n';
  201. out[ret] = '\0';
  202. ctx->num = 0;
  203. }
  204. *outl = ret;
  205. }
  206. static int evp_encodeblock_int(EVP_ENCODE_CTX *ctx, unsigned char *t,
  207. const unsigned char *f, int dlen)
  208. {
  209. int i, ret = 0;
  210. unsigned long l;
  211. const unsigned char *table;
  212. if (ctx != NULL && (ctx->flags & EVP_ENCODE_CTX_USE_SRP_ALPHABET) != 0)
  213. table = srpdata_bin2ascii;
  214. else
  215. table = data_bin2ascii;
  216. for (i = dlen; i > 0; i -= 3) {
  217. if (i >= 3) {
  218. l = (((unsigned long)f[0]) << 16L) |
  219. (((unsigned long)f[1]) << 8L) | f[2];
  220. *(t++) = conv_bin2ascii(l >> 18L, table);
  221. *(t++) = conv_bin2ascii(l >> 12L, table);
  222. *(t++) = conv_bin2ascii(l >> 6L, table);
  223. *(t++) = conv_bin2ascii(l, table);
  224. } else {
  225. l = ((unsigned long)f[0]) << 16L;
  226. if (i == 2)
  227. l |= ((unsigned long)f[1] << 8L);
  228. *(t++) = conv_bin2ascii(l >> 18L, table);
  229. *(t++) = conv_bin2ascii(l >> 12L, table);
  230. *(t++) = (i == 1) ? '=' : conv_bin2ascii(l >> 6L, table);
  231. *(t++) = '=';
  232. }
  233. ret += 4;
  234. f += 3;
  235. }
  236. *t = '\0';
  237. return ret;
  238. }
  239. int EVP_EncodeBlock(unsigned char *t, const unsigned char *f, int dlen)
  240. {
  241. return evp_encodeblock_int(NULL, t, f, dlen);
  242. }
  243. void EVP_DecodeInit(EVP_ENCODE_CTX *ctx)
  244. {
  245. /* Only ctx->num and ctx->flags are used during decoding. */
  246. ctx->num = 0;
  247. ctx->length = 0;
  248. ctx->line_num = 0;
  249. ctx->flags = 0;
  250. }
  251. /*-
  252. * -1 for error
  253. * 0 for last line
  254. * 1 for full line
  255. *
  256. * Note: even though EVP_DecodeUpdate attempts to detect and report end of
  257. * content, the context doesn't currently remember it and will accept more data
  258. * in the next call. Therefore, the caller is responsible for checking and
  259. * rejecting a 0 return value in the middle of content.
  260. *
  261. * Note: even though EVP_DecodeUpdate has historically tried to detect end of
  262. * content based on line length, this has never worked properly. Therefore,
  263. * we now return 0 when one of the following is true:
  264. * - Padding or B64_EOF was detected and the last block is complete.
  265. * - Input has zero-length.
  266. * -1 is returned if:
  267. * - Invalid characters are detected.
  268. * - There is extra trailing padding, or data after padding.
  269. * - B64_EOF is detected after an incomplete base64 block.
  270. */
  271. int EVP_DecodeUpdate(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl,
  272. const unsigned char *in, int inl)
  273. {
  274. int seof = 0, eof = 0, rv = -1, ret = 0, i, v, tmp, n, decoded_len;
  275. unsigned char *d;
  276. const unsigned char *table;
  277. n = ctx->num;
  278. d = ctx->enc_data;
  279. if (n > 0 && d[n - 1] == '=') {
  280. eof++;
  281. if (n > 1 && d[n - 2] == '=')
  282. eof++;
  283. }
  284. /* Legacy behaviour: an empty input chunk signals end of input. */
  285. if (inl == 0) {
  286. rv = 0;
  287. goto end;
  288. }
  289. if ((ctx->flags & EVP_ENCODE_CTX_USE_SRP_ALPHABET) != 0)
  290. table = srpdata_ascii2bin;
  291. else
  292. table = data_ascii2bin;
  293. for (i = 0; i < inl; i++) {
  294. tmp = *(in++);
  295. v = conv_ascii2bin(tmp, table);
  296. if (v == B64_ERROR) {
  297. rv = -1;
  298. goto end;
  299. }
  300. if (tmp == '=') {
  301. eof++;
  302. } else if (eof > 0 && B64_BASE64(v)) {
  303. /* More data after padding. */
  304. rv = -1;
  305. goto end;
  306. }
  307. if (eof > 2) {
  308. rv = -1;
  309. goto end;
  310. }
  311. if (v == B64_EOF) {
  312. seof = 1;
  313. goto tail;
  314. }
  315. /* Only save valid base64 characters. */
  316. if (B64_BASE64(v)) {
  317. if (n >= 64) {
  318. /*
  319. * We increment n once per loop, and empty the buffer as soon as
  320. * we reach 64 characters, so this can only happen if someone's
  321. * manually messed with the ctx. Refuse to write any more data.
  322. */
  323. rv = -1;
  324. goto end;
  325. }
  326. OPENSSL_assert(n < (int)sizeof(ctx->enc_data));
  327. d[n++] = tmp;
  328. }
  329. if (n == 64) {
  330. decoded_len = evp_decodeblock_int(ctx, out, d, n);
  331. n = 0;
  332. if (decoded_len < 0 || eof > decoded_len) {
  333. rv = -1;
  334. goto end;
  335. }
  336. ret += decoded_len - eof;
  337. out += decoded_len - eof;
  338. }
  339. }
  340. /*
  341. * Legacy behaviour: if the current line is a full base64-block (i.e., has
  342. * 0 mod 4 base64 characters), it is processed immediately. We keep this
  343. * behaviour as applications may not be calling EVP_DecodeFinal properly.
  344. */
  345. tail:
  346. if (n > 0) {
  347. if ((n & 3) == 0) {
  348. decoded_len = evp_decodeblock_int(ctx, out, d, n);
  349. n = 0;
  350. if (decoded_len < 0 || eof > decoded_len) {
  351. rv = -1;
  352. goto end;
  353. }
  354. ret += (decoded_len - eof);
  355. } else if (seof) {
  356. /* EOF in the middle of a base64 block. */
  357. rv = -1;
  358. goto end;
  359. }
  360. }
  361. rv = seof || (n == 0 && eof) ? 0 : 1;
  362. end:
  363. /* Legacy behaviour. This should probably rather be zeroed on error. */
  364. *outl = ret;
  365. ctx->num = n;
  366. return rv;
  367. }
  368. static int evp_decodeblock_int(EVP_ENCODE_CTX *ctx, unsigned char *t,
  369. const unsigned char *f, int n)
  370. {
  371. int i, ret = 0, a, b, c, d;
  372. unsigned long l;
  373. const unsigned char *table;
  374. if (ctx != NULL && (ctx->flags & EVP_ENCODE_CTX_USE_SRP_ALPHABET) != 0)
  375. table = srpdata_ascii2bin;
  376. else
  377. table = data_ascii2bin;
  378. /* trim whitespace from the start of the line. */
  379. while ((n > 0) && (conv_ascii2bin(*f, table) == B64_WS)) {
  380. f++;
  381. n--;
  382. }
  383. /*
  384. * strip off stuff at the end of the line ascii2bin values B64_WS,
  385. * B64_EOLN, B64_EOLN and B64_EOF
  386. */
  387. while ((n > 3) && (B64_NOT_BASE64(conv_ascii2bin(f[n - 1], table))))
  388. n--;
  389. if (n % 4 != 0)
  390. return -1;
  391. for (i = 0; i < n; i += 4) {
  392. a = conv_ascii2bin(*(f++), table);
  393. b = conv_ascii2bin(*(f++), table);
  394. c = conv_ascii2bin(*(f++), table);
  395. d = conv_ascii2bin(*(f++), table);
  396. if ((a & 0x80) || (b & 0x80) || (c & 0x80) || (d & 0x80))
  397. return -1;
  398. l = ((((unsigned long)a) << 18L) |
  399. (((unsigned long)b) << 12L) |
  400. (((unsigned long)c) << 6L) | (((unsigned long)d)));
  401. *(t++) = (unsigned char)(l >> 16L) & 0xff;
  402. *(t++) = (unsigned char)(l >> 8L) & 0xff;
  403. *(t++) = (unsigned char)(l) & 0xff;
  404. ret += 3;
  405. }
  406. return ret;
  407. }
  408. int EVP_DecodeBlock(unsigned char *t, const unsigned char *f, int n)
  409. {
  410. return evp_decodeblock_int(NULL, t, f, n);
  411. }
  412. int EVP_DecodeFinal(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl)
  413. {
  414. int i;
  415. *outl = 0;
  416. if (ctx->num != 0) {
  417. i = evp_decodeblock_int(ctx, out, ctx->enc_data, ctx->num);
  418. if (i < 0)
  419. return -1;
  420. ctx->num = 0;
  421. *outl = i;
  422. return 1;
  423. } else
  424. return 1;
  425. }