You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

535 regels
15 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 <assert.h>
  57. #include <errno.h>
  58. #include <stdio.h>
  59. #include <string.h>
  60. #include <openssl/base64.h>
  61. #include <openssl/bio.h>
  62. #include <openssl/buffer.h>
  63. #include <openssl/evp.h>
  64. #include <openssl/mem.h>
  65. #include "../../crypto/internal.h"
  66. #define B64_BLOCK_SIZE 1024
  67. #define B64_BLOCK_SIZE2 768
  68. #define B64_NONE 0
  69. #define B64_ENCODE 1
  70. #define B64_DECODE 2
  71. #define EVP_ENCODE_LENGTH(l) (((l+2)/3*4)+(l/48+1)*2+80)
  72. typedef struct b64_struct {
  73. int buf_len;
  74. int buf_off;
  75. int tmp_len; /* used to find the start when decoding */
  76. int tmp_nl; /* If true, scan until '\n' */
  77. int encode;
  78. int start; /* have we started decoding yet? */
  79. int cont; /* <= 0 when finished */
  80. EVP_ENCODE_CTX base64;
  81. char buf[EVP_ENCODE_LENGTH(B64_BLOCK_SIZE) + 10];
  82. char tmp[B64_BLOCK_SIZE];
  83. } BIO_B64_CTX;
  84. static int b64_new(BIO *bio) {
  85. BIO_B64_CTX *ctx;
  86. ctx = OPENSSL_malloc(sizeof(*ctx));
  87. if (ctx == NULL) {
  88. return 0;
  89. }
  90. OPENSSL_memset(ctx, 0, sizeof(*ctx));
  91. ctx->cont = 1;
  92. ctx->start = 1;
  93. bio->init = 1;
  94. bio->ptr = (char *)ctx;
  95. return 1;
  96. }
  97. static int b64_free(BIO *bio) {
  98. if (bio == NULL) {
  99. return 0;
  100. }
  101. OPENSSL_free(bio->ptr);
  102. bio->ptr = NULL;
  103. bio->init = 0;
  104. bio->flags = 0;
  105. return 1;
  106. }
  107. static int b64_read(BIO *b, char *out, int outl) {
  108. int ret = 0, i, ii, j, k, x, n, num, ret_code = 0;
  109. BIO_B64_CTX *ctx;
  110. uint8_t *p, *q;
  111. if (out == NULL) {
  112. return 0;
  113. }
  114. ctx = (BIO_B64_CTX *) b->ptr;
  115. if (ctx == NULL || b->next_bio == NULL) {
  116. return 0;
  117. }
  118. BIO_clear_retry_flags(b);
  119. if (ctx->encode != B64_DECODE) {
  120. ctx->encode = B64_DECODE;
  121. ctx->buf_len = 0;
  122. ctx->buf_off = 0;
  123. ctx->tmp_len = 0;
  124. EVP_DecodeInit(&ctx->base64);
  125. }
  126. /* First check if there are bytes decoded/encoded */
  127. if (ctx->buf_len > 0) {
  128. assert(ctx->buf_len >= ctx->buf_off);
  129. i = ctx->buf_len - ctx->buf_off;
  130. if (i > outl) {
  131. i = outl;
  132. }
  133. assert(ctx->buf_off + i < (int)sizeof(ctx->buf));
  134. OPENSSL_memcpy(out, &ctx->buf[ctx->buf_off], i);
  135. ret = i;
  136. out += i;
  137. outl -= i;
  138. ctx->buf_off += i;
  139. if (ctx->buf_len == ctx->buf_off) {
  140. ctx->buf_len = 0;
  141. ctx->buf_off = 0;
  142. }
  143. }
  144. /* At this point, we have room of outl bytes and an empty buffer, so we
  145. * should read in some more. */
  146. ret_code = 0;
  147. while (outl > 0) {
  148. if (ctx->cont <= 0) {
  149. break;
  150. }
  151. i = BIO_read(b->next_bio, &(ctx->tmp[ctx->tmp_len]),
  152. B64_BLOCK_SIZE - ctx->tmp_len);
  153. if (i <= 0) {
  154. ret_code = i;
  155. /* Should we continue next time we are called? */
  156. if (!BIO_should_retry(b->next_bio)) {
  157. ctx->cont = i;
  158. /* If buffer empty break */
  159. if (ctx->tmp_len == 0) {
  160. break;
  161. } else {
  162. /* Fall through and process what we have */
  163. i = 0;
  164. }
  165. } else {
  166. /* else we retry and add more data to buffer */
  167. break;
  168. }
  169. }
  170. i += ctx->tmp_len;
  171. ctx->tmp_len = i;
  172. /* We need to scan, a line at a time until we have a valid line if we are
  173. * starting. */
  174. if (ctx->start && (BIO_test_flags(b, BIO_FLAGS_BASE64_NO_NL))) {
  175. /* ctx->start = 1; */
  176. ctx->tmp_len = 0;
  177. } else if (ctx->start) {
  178. q = p = (uint8_t *)ctx->tmp;
  179. num = 0;
  180. for (j = 0; j < i; j++) {
  181. if (*(q++) != '\n') {
  182. continue;
  183. }
  184. /* due to a previous very long line, we need to keep on scanning for a
  185. * '\n' before we even start looking for base64 encoded stuff. */
  186. if (ctx->tmp_nl) {
  187. p = q;
  188. ctx->tmp_nl = 0;
  189. continue;
  190. }
  191. k = EVP_DecodeUpdate(&(ctx->base64), (uint8_t *)ctx->buf, &num, p,
  192. q - p);
  193. if (k <= 0 && num == 0 && ctx->start) {
  194. EVP_DecodeInit(&ctx->base64);
  195. } else {
  196. if (p != (uint8_t *)&(ctx->tmp[0])) {
  197. i -= (p - (uint8_t *)&(ctx->tmp[0]));
  198. for (x = 0; x < i; x++) {
  199. ctx->tmp[x] = p[x];
  200. }
  201. }
  202. EVP_DecodeInit(&ctx->base64);
  203. ctx->start = 0;
  204. break;
  205. }
  206. p = q;
  207. }
  208. /* we fell off the end without starting */
  209. if (j == i && num == 0) {
  210. /* Is this is one long chunk?, if so, keep on reading until a new
  211. * line. */
  212. if (p == (uint8_t *)&(ctx->tmp[0])) {
  213. /* Check buffer full */
  214. if (i == B64_BLOCK_SIZE) {
  215. ctx->tmp_nl = 1;
  216. ctx->tmp_len = 0;
  217. }
  218. } else if (p != q) { /* finished on a '\n' */
  219. n = q - p;
  220. for (ii = 0; ii < n; ii++) {
  221. ctx->tmp[ii] = p[ii];
  222. }
  223. ctx->tmp_len = n;
  224. }
  225. /* else finished on a '\n' */
  226. continue;
  227. } else {
  228. ctx->tmp_len = 0;
  229. }
  230. } else if (i < B64_BLOCK_SIZE && ctx->cont > 0) {
  231. /* If buffer isn't full and we can retry then restart to read in more
  232. * data. */
  233. continue;
  234. }
  235. if (BIO_test_flags(b, BIO_FLAGS_BASE64_NO_NL)) {
  236. int z, jj;
  237. jj = i & ~3; /* process per 4 */
  238. z = EVP_DecodeBlock((uint8_t *)ctx->buf, (uint8_t *)ctx->tmp, jj);
  239. if (jj > 2) {
  240. if (ctx->tmp[jj - 1] == '=') {
  241. z--;
  242. if (ctx->tmp[jj - 2] == '=') {
  243. z--;
  244. }
  245. }
  246. }
  247. /* z is now number of output bytes and jj is the number consumed. */
  248. if (jj != i) {
  249. OPENSSL_memmove(ctx->tmp, &ctx->tmp[jj], i - jj);
  250. ctx->tmp_len = i - jj;
  251. }
  252. ctx->buf_len = 0;
  253. if (z > 0) {
  254. ctx->buf_len = z;
  255. }
  256. i = z;
  257. } else {
  258. i = EVP_DecodeUpdate(&(ctx->base64), (uint8_t *)ctx->buf,
  259. &ctx->buf_len, (uint8_t *)ctx->tmp, i);
  260. ctx->tmp_len = 0;
  261. }
  262. ctx->buf_off = 0;
  263. if (i < 0) {
  264. ret_code = 0;
  265. ctx->buf_len = 0;
  266. break;
  267. }
  268. if (ctx->buf_len <= outl) {
  269. i = ctx->buf_len;
  270. } else {
  271. i = outl;
  272. }
  273. OPENSSL_memcpy(out, ctx->buf, i);
  274. ret += i;
  275. ctx->buf_off = i;
  276. if (ctx->buf_off == ctx->buf_len) {
  277. ctx->buf_len = 0;
  278. ctx->buf_off = 0;
  279. }
  280. outl -= i;
  281. out += i;
  282. }
  283. BIO_copy_next_retry(b);
  284. return ret == 0 ? ret_code : ret;
  285. }
  286. static int b64_write(BIO *b, const char *in, int inl) {
  287. int ret = 0, n, i;
  288. BIO_B64_CTX *ctx;
  289. ctx = (BIO_B64_CTX *)b->ptr;
  290. BIO_clear_retry_flags(b);
  291. if (ctx->encode != B64_ENCODE) {
  292. ctx->encode = B64_ENCODE;
  293. ctx->buf_len = 0;
  294. ctx->buf_off = 0;
  295. ctx->tmp_len = 0;
  296. EVP_EncodeInit(&(ctx->base64));
  297. }
  298. assert(ctx->buf_off < (int)sizeof(ctx->buf));
  299. assert(ctx->buf_len <= (int)sizeof(ctx->buf));
  300. assert(ctx->buf_len >= ctx->buf_off);
  301. n = ctx->buf_len - ctx->buf_off;
  302. while (n > 0) {
  303. i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n);
  304. if (i <= 0) {
  305. BIO_copy_next_retry(b);
  306. return i;
  307. }
  308. assert(i <= n);
  309. ctx->buf_off += i;
  310. assert(ctx->buf_off <= (int)sizeof(ctx->buf));
  311. assert(ctx->buf_len >= ctx->buf_off);
  312. n -= i;
  313. }
  314. /* at this point all pending data has been written. */
  315. ctx->buf_off = 0;
  316. ctx->buf_len = 0;
  317. if (in == NULL || inl <= 0) {
  318. return 0;
  319. }
  320. while (inl > 0) {
  321. n = (inl > B64_BLOCK_SIZE) ? B64_BLOCK_SIZE : inl;
  322. if (BIO_test_flags(b, BIO_FLAGS_BASE64_NO_NL)) {
  323. if (ctx->tmp_len > 0) {
  324. assert(ctx->tmp_len <= 3);
  325. n = 3 - ctx->tmp_len;
  326. /* There's a theoretical possibility of this. */
  327. if (n > inl) {
  328. n = inl;
  329. }
  330. OPENSSL_memcpy(&(ctx->tmp[ctx->tmp_len]), in, n);
  331. ctx->tmp_len += n;
  332. ret += n;
  333. if (ctx->tmp_len < 3) {
  334. break;
  335. }
  336. ctx->buf_len = EVP_EncodeBlock((uint8_t *)ctx->buf, (uint8_t *)ctx->tmp,
  337. ctx->tmp_len);
  338. assert(ctx->buf_len <= (int)sizeof(ctx->buf));
  339. assert(ctx->buf_len >= ctx->buf_off);
  340. /* Since we're now done using the temporary buffer, the length should
  341. * be zeroed. */
  342. ctx->tmp_len = 0;
  343. } else {
  344. if (n < 3) {
  345. OPENSSL_memcpy(ctx->tmp, in, n);
  346. ctx->tmp_len = n;
  347. ret += n;
  348. break;
  349. }
  350. n -= n % 3;
  351. ctx->buf_len =
  352. EVP_EncodeBlock((uint8_t *)ctx->buf, (const uint8_t *)in, n);
  353. assert(ctx->buf_len <= (int)sizeof(ctx->buf));
  354. assert(ctx->buf_len >= ctx->buf_off);
  355. ret += n;
  356. }
  357. } else {
  358. EVP_EncodeUpdate(&(ctx->base64), (uint8_t *)ctx->buf, &ctx->buf_len,
  359. (uint8_t *)in, n);
  360. assert(ctx->buf_len <= (int)sizeof(ctx->buf));
  361. assert(ctx->buf_len >= ctx->buf_off);
  362. ret += n;
  363. }
  364. inl -= n;
  365. in += n;
  366. ctx->buf_off = 0;
  367. n = ctx->buf_len;
  368. while (n > 0) {
  369. i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n);
  370. if (i <= 0) {
  371. BIO_copy_next_retry(b);
  372. return ret == 0 ? i : ret;
  373. }
  374. assert(i <= n);
  375. n -= i;
  376. ctx->buf_off += i;
  377. assert(ctx->buf_off <= (int)sizeof(ctx->buf));
  378. assert(ctx->buf_len >= ctx->buf_off);
  379. }
  380. ctx->buf_len = 0;
  381. ctx->buf_off = 0;
  382. }
  383. return ret;
  384. }
  385. static long b64_ctrl(BIO *b, int cmd, long num, void *ptr) {
  386. BIO_B64_CTX *ctx;
  387. long ret = 1;
  388. int i;
  389. ctx = (BIO_B64_CTX *)b->ptr;
  390. switch (cmd) {
  391. case BIO_CTRL_RESET:
  392. ctx->cont = 1;
  393. ctx->start = 1;
  394. ctx->encode = B64_NONE;
  395. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  396. break;
  397. case BIO_CTRL_EOF: /* More to read */
  398. if (ctx->cont <= 0) {
  399. ret = 1;
  400. } else {
  401. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  402. }
  403. break;
  404. case BIO_CTRL_WPENDING: /* More to write in buffer */
  405. assert(ctx->buf_len >= ctx->buf_off);
  406. ret = ctx->buf_len - ctx->buf_off;
  407. if ((ret == 0) && (ctx->encode != B64_NONE) && (ctx->base64.data_used != 0)) {
  408. ret = 1;
  409. } else if (ret <= 0) {
  410. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  411. }
  412. break;
  413. case BIO_CTRL_PENDING: /* More to read in buffer */
  414. assert(ctx->buf_len >= ctx->buf_off);
  415. ret = ctx->buf_len - ctx->buf_off;
  416. if (ret <= 0) {
  417. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  418. }
  419. break;
  420. case BIO_CTRL_FLUSH:
  421. /* do a final write */
  422. again:
  423. while (ctx->buf_len != ctx->buf_off) {
  424. i = b64_write(b, NULL, 0);
  425. if (i < 0) {
  426. return i;
  427. }
  428. }
  429. if (BIO_test_flags(b, BIO_FLAGS_BASE64_NO_NL)) {
  430. if (ctx->tmp_len != 0) {
  431. ctx->buf_len = EVP_EncodeBlock((uint8_t *)ctx->buf,
  432. (uint8_t *)ctx->tmp, ctx->tmp_len);
  433. ctx->buf_off = 0;
  434. ctx->tmp_len = 0;
  435. goto again;
  436. }
  437. } else if (ctx->encode != B64_NONE && ctx->base64.data_used != 0) {
  438. ctx->buf_off = 0;
  439. EVP_EncodeFinal(&(ctx->base64), (uint8_t *)ctx->buf, &(ctx->buf_len));
  440. /* push out the bytes */
  441. goto again;
  442. }
  443. /* Finally flush the underlying BIO */
  444. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  445. break;
  446. case BIO_C_DO_STATE_MACHINE:
  447. BIO_clear_retry_flags(b);
  448. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  449. BIO_copy_next_retry(b);
  450. break;
  451. case BIO_CTRL_INFO:
  452. case BIO_CTRL_GET:
  453. case BIO_CTRL_SET:
  454. default:
  455. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  456. break;
  457. }
  458. return ret;
  459. }
  460. static long b64_callback_ctrl(BIO *b, int cmd, bio_info_cb fp) {
  461. long ret = 1;
  462. if (b->next_bio == NULL) {
  463. return 0;
  464. }
  465. switch (cmd) {
  466. default:
  467. ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
  468. break;
  469. }
  470. return ret;
  471. }
  472. static const BIO_METHOD b64_method = {
  473. BIO_TYPE_BASE64, "base64 encoding", b64_write, b64_read, NULL /* puts */,
  474. NULL /* gets */, b64_ctrl, b64_new, b64_free, b64_callback_ctrl,
  475. };
  476. const BIO_METHOD *BIO_f_base64(void) { return &b64_method; }