encode.c 11 KB

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