Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

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