c_zlib.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. /*
  2. * Copyright 1998-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 <stdlib.h>
  11. #include <string.h>
  12. #include <openssl/objects.h>
  13. #include "internal/comp.h"
  14. #include <openssl/err.h>
  15. #include "crypto/cryptlib.h"
  16. #include "internal/bio.h"
  17. #include "comp_local.h"
  18. COMP_METHOD *COMP_zlib(void);
  19. static COMP_METHOD zlib_method_nozlib = {
  20. NID_undef,
  21. "(undef)",
  22. NULL,
  23. NULL,
  24. NULL,
  25. NULL,
  26. };
  27. #ifndef ZLIB
  28. # undef ZLIB_SHARED
  29. #else
  30. # include <zlib.h>
  31. static int zlib_stateful_init(COMP_CTX *ctx);
  32. static void zlib_stateful_finish(COMP_CTX *ctx);
  33. static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
  34. unsigned int olen, unsigned char *in,
  35. unsigned int ilen);
  36. static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
  37. unsigned int olen, unsigned char *in,
  38. unsigned int ilen);
  39. /* memory allocations functions for zlib initialisation */
  40. static void *zlib_zalloc(void *opaque, unsigned int no, unsigned int size)
  41. {
  42. void *p;
  43. p = OPENSSL_zalloc(no * size);
  44. return p;
  45. }
  46. static void zlib_zfree(void *opaque, void *address)
  47. {
  48. OPENSSL_free(address);
  49. }
  50. static COMP_METHOD zlib_stateful_method = {
  51. NID_zlib_compression,
  52. LN_zlib_compression,
  53. zlib_stateful_init,
  54. zlib_stateful_finish,
  55. zlib_stateful_compress_block,
  56. zlib_stateful_expand_block
  57. };
  58. /*
  59. * When OpenSSL is built on Windows, we do not want to require that
  60. * the ZLIB.DLL be available in order for the OpenSSL DLLs to
  61. * work. Therefore, all ZLIB routines are loaded at run time
  62. * and we do not link to a .LIB file when ZLIB_SHARED is set.
  63. */
  64. # if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
  65. # include <windows.h>
  66. # endif /* !(OPENSSL_SYS_WINDOWS ||
  67. * OPENSSL_SYS_WIN32) */
  68. # ifdef ZLIB_SHARED
  69. # include "internal/dso.h"
  70. /* Function pointers */
  71. typedef int (*compress_ft) (Bytef *dest, uLongf * destLen,
  72. const Bytef *source, uLong sourceLen);
  73. typedef int (*inflateEnd_ft) (z_streamp strm);
  74. typedef int (*inflate_ft) (z_streamp strm, int flush);
  75. typedef int (*inflateInit__ft) (z_streamp strm,
  76. const char *version, int stream_size);
  77. typedef int (*deflateEnd_ft) (z_streamp strm);
  78. typedef int (*deflate_ft) (z_streamp strm, int flush);
  79. typedef int (*deflateInit__ft) (z_streamp strm, int level,
  80. const char *version, int stream_size);
  81. typedef const char *(*zError__ft) (int err);
  82. static compress_ft p_compress = NULL;
  83. static inflateEnd_ft p_inflateEnd = NULL;
  84. static inflate_ft p_inflate = NULL;
  85. static inflateInit__ft p_inflateInit_ = NULL;
  86. static deflateEnd_ft p_deflateEnd = NULL;
  87. static deflate_ft p_deflate = NULL;
  88. static deflateInit__ft p_deflateInit_ = NULL;
  89. static zError__ft p_zError = NULL;
  90. static int zlib_loaded = 0; /* only attempt to init func pts once */
  91. static DSO *zlib_dso = NULL;
  92. # define compress p_compress
  93. # define inflateEnd p_inflateEnd
  94. # define inflate p_inflate
  95. # define inflateInit_ p_inflateInit_
  96. # define deflateEnd p_deflateEnd
  97. # define deflate p_deflate
  98. # define deflateInit_ p_deflateInit_
  99. # define zError p_zError
  100. # endif /* ZLIB_SHARED */
  101. struct zlib_state {
  102. z_stream istream;
  103. z_stream ostream;
  104. };
  105. static int zlib_stateful_init(COMP_CTX *ctx)
  106. {
  107. int err;
  108. struct zlib_state *state = OPENSSL_zalloc(sizeof(*state));
  109. if (state == NULL)
  110. goto err;
  111. state->istream.zalloc = zlib_zalloc;
  112. state->istream.zfree = zlib_zfree;
  113. state->istream.opaque = Z_NULL;
  114. state->istream.next_in = Z_NULL;
  115. state->istream.next_out = Z_NULL;
  116. err = inflateInit_(&state->istream, ZLIB_VERSION, sizeof(z_stream));
  117. if (err != Z_OK)
  118. goto err;
  119. state->ostream.zalloc = zlib_zalloc;
  120. state->ostream.zfree = zlib_zfree;
  121. state->ostream.opaque = Z_NULL;
  122. state->ostream.next_in = Z_NULL;
  123. state->ostream.next_out = Z_NULL;
  124. err = deflateInit_(&state->ostream, Z_DEFAULT_COMPRESSION,
  125. ZLIB_VERSION, sizeof(z_stream));
  126. if (err != Z_OK)
  127. goto err;
  128. ctx->data = state;
  129. return 1;
  130. err:
  131. OPENSSL_free(state);
  132. return 0;
  133. }
  134. static void zlib_stateful_finish(COMP_CTX *ctx)
  135. {
  136. struct zlib_state *state = ctx->data;
  137. inflateEnd(&state->istream);
  138. deflateEnd(&state->ostream);
  139. OPENSSL_free(state);
  140. }
  141. static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
  142. unsigned int olen, unsigned char *in,
  143. unsigned int ilen)
  144. {
  145. int err = Z_OK;
  146. struct zlib_state *state = ctx->data;
  147. if (state == NULL)
  148. return -1;
  149. state->ostream.next_in = in;
  150. state->ostream.avail_in = ilen;
  151. state->ostream.next_out = out;
  152. state->ostream.avail_out = olen;
  153. if (ilen > 0)
  154. err = deflate(&state->ostream, Z_SYNC_FLUSH);
  155. if (err != Z_OK)
  156. return -1;
  157. return olen - state->ostream.avail_out;
  158. }
  159. static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
  160. unsigned int olen, unsigned char *in,
  161. unsigned int ilen)
  162. {
  163. int err = Z_OK;
  164. struct zlib_state *state = ctx->data;
  165. if (state == NULL)
  166. return 0;
  167. state->istream.next_in = in;
  168. state->istream.avail_in = ilen;
  169. state->istream.next_out = out;
  170. state->istream.avail_out = olen;
  171. if (ilen > 0)
  172. err = inflate(&state->istream, Z_SYNC_FLUSH);
  173. if (err != Z_OK)
  174. return -1;
  175. return olen - state->istream.avail_out;
  176. }
  177. #endif
  178. COMP_METHOD *COMP_zlib(void)
  179. {
  180. COMP_METHOD *meth = &zlib_method_nozlib;
  181. #ifdef ZLIB_SHARED
  182. /* LIBZ may be externally defined, and we should respect that value */
  183. # ifndef LIBZ
  184. # if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
  185. # define LIBZ "ZLIB1"
  186. # elif defined(OPENSSL_SYS_VMS)
  187. # define LIBZ "LIBZ"
  188. # else
  189. # define LIBZ "z"
  190. # endif
  191. # endif
  192. if (!zlib_loaded) {
  193. zlib_dso = DSO_load(NULL, LIBZ, NULL, 0);
  194. if (zlib_dso != NULL) {
  195. p_compress = (compress_ft) DSO_bind_func(zlib_dso, "compress");
  196. p_inflateEnd
  197. = (inflateEnd_ft) DSO_bind_func(zlib_dso, "inflateEnd");
  198. p_inflate = (inflate_ft) DSO_bind_func(zlib_dso, "inflate");
  199. p_inflateInit_
  200. = (inflateInit__ft) DSO_bind_func(zlib_dso, "inflateInit_");
  201. p_deflateEnd
  202. = (deflateEnd_ft) DSO_bind_func(zlib_dso, "deflateEnd");
  203. p_deflate = (deflate_ft) DSO_bind_func(zlib_dso, "deflate");
  204. p_deflateInit_
  205. = (deflateInit__ft) DSO_bind_func(zlib_dso, "deflateInit_");
  206. p_zError = (zError__ft) DSO_bind_func(zlib_dso, "zError");
  207. if (p_compress && p_inflateEnd && p_inflate
  208. && p_inflateInit_ && p_deflateEnd
  209. && p_deflate && p_deflateInit_ && p_zError)
  210. zlib_loaded++;
  211. if (!OPENSSL_init_crypto(OPENSSL_INIT_ZLIB, NULL)) {
  212. comp_zlib_cleanup_int();
  213. return meth;
  214. }
  215. if (zlib_loaded)
  216. meth = &zlib_stateful_method;
  217. }
  218. }
  219. #endif
  220. #if defined(ZLIB)
  221. meth = &zlib_stateful_method;
  222. #endif
  223. return meth;
  224. }
  225. void comp_zlib_cleanup_int(void)
  226. {
  227. #ifdef ZLIB_SHARED
  228. DSO_free(zlib_dso);
  229. zlib_dso = NULL;
  230. #endif
  231. }
  232. #ifdef ZLIB
  233. /* Zlib based compression/decompression filter BIO */
  234. typedef struct {
  235. unsigned char *ibuf; /* Input buffer */
  236. int ibufsize; /* Buffer size */
  237. z_stream zin; /* Input decompress context */
  238. unsigned char *obuf; /* Output buffer */
  239. int obufsize; /* Output buffer size */
  240. unsigned char *optr; /* Position in output buffer */
  241. int ocount; /* Amount of data in output buffer */
  242. int odone; /* deflate EOF */
  243. int comp_level; /* Compression level to use */
  244. z_stream zout; /* Output compression context */
  245. } BIO_ZLIB_CTX;
  246. # define ZLIB_DEFAULT_BUFSIZE 1024
  247. static int bio_zlib_new(BIO *bi);
  248. static int bio_zlib_free(BIO *bi);
  249. static int bio_zlib_read(BIO *b, char *out, int outl);
  250. static int bio_zlib_write(BIO *b, const char *in, int inl);
  251. static long bio_zlib_ctrl(BIO *b, int cmd, long num, void *ptr);
  252. static long bio_zlib_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp);
  253. static const BIO_METHOD bio_meth_zlib = {
  254. BIO_TYPE_COMP,
  255. "zlib",
  256. /* TODO: Convert to new style write function */
  257. bwrite_conv,
  258. bio_zlib_write,
  259. /* TODO: Convert to new style read function */
  260. bread_conv,
  261. bio_zlib_read,
  262. NULL, /* bio_zlib_puts, */
  263. NULL, /* bio_zlib_gets, */
  264. bio_zlib_ctrl,
  265. bio_zlib_new,
  266. bio_zlib_free,
  267. bio_zlib_callback_ctrl
  268. };
  269. const BIO_METHOD *BIO_f_zlib(void)
  270. {
  271. return &bio_meth_zlib;
  272. }
  273. static int bio_zlib_new(BIO *bi)
  274. {
  275. BIO_ZLIB_CTX *ctx;
  276. # ifdef ZLIB_SHARED
  277. (void)COMP_zlib();
  278. if (!zlib_loaded) {
  279. COMPerr(COMP_F_BIO_ZLIB_NEW, COMP_R_ZLIB_NOT_SUPPORTED);
  280. return 0;
  281. }
  282. # endif
  283. ctx = OPENSSL_zalloc(sizeof(*ctx));
  284. if (ctx == NULL) {
  285. COMPerr(COMP_F_BIO_ZLIB_NEW, ERR_R_MALLOC_FAILURE);
  286. return 0;
  287. }
  288. ctx->ibufsize = ZLIB_DEFAULT_BUFSIZE;
  289. ctx->obufsize = ZLIB_DEFAULT_BUFSIZE;
  290. ctx->zin.zalloc = Z_NULL;
  291. ctx->zin.zfree = Z_NULL;
  292. ctx->zout.zalloc = Z_NULL;
  293. ctx->zout.zfree = Z_NULL;
  294. ctx->comp_level = Z_DEFAULT_COMPRESSION;
  295. BIO_set_init(bi, 1);
  296. BIO_set_data(bi, ctx);
  297. return 1;
  298. }
  299. static int bio_zlib_free(BIO *bi)
  300. {
  301. BIO_ZLIB_CTX *ctx;
  302. if (!bi)
  303. return 0;
  304. ctx = BIO_get_data(bi);
  305. if (ctx->ibuf) {
  306. /* Destroy decompress context */
  307. inflateEnd(&ctx->zin);
  308. OPENSSL_free(ctx->ibuf);
  309. }
  310. if (ctx->obuf) {
  311. /* Destroy compress context */
  312. deflateEnd(&ctx->zout);
  313. OPENSSL_free(ctx->obuf);
  314. }
  315. OPENSSL_free(ctx);
  316. BIO_set_data(bi, NULL);
  317. BIO_set_init(bi, 0);
  318. return 1;
  319. }
  320. static int bio_zlib_read(BIO *b, char *out, int outl)
  321. {
  322. BIO_ZLIB_CTX *ctx;
  323. int ret;
  324. z_stream *zin;
  325. BIO *next = BIO_next(b);
  326. if (!out || !outl)
  327. return 0;
  328. ctx = BIO_get_data(b);
  329. zin = &ctx->zin;
  330. BIO_clear_retry_flags(b);
  331. if (!ctx->ibuf) {
  332. ctx->ibuf = OPENSSL_malloc(ctx->ibufsize);
  333. if (ctx->ibuf == NULL) {
  334. COMPerr(COMP_F_BIO_ZLIB_READ, ERR_R_MALLOC_FAILURE);
  335. return 0;
  336. }
  337. inflateInit(zin);
  338. zin->next_in = ctx->ibuf;
  339. zin->avail_in = 0;
  340. }
  341. /* Copy output data directly to supplied buffer */
  342. zin->next_out = (unsigned char *)out;
  343. zin->avail_out = (unsigned int)outl;
  344. for (;;) {
  345. /* Decompress while data available */
  346. while (zin->avail_in) {
  347. ret = inflate(zin, 0);
  348. if ((ret != Z_OK) && (ret != Z_STREAM_END)) {
  349. COMPerr(COMP_F_BIO_ZLIB_READ, COMP_R_ZLIB_INFLATE_ERROR);
  350. ERR_add_error_data(2, "zlib error:", zError(ret));
  351. return 0;
  352. }
  353. /* If EOF or we've read everything then return */
  354. if ((ret == Z_STREAM_END) || !zin->avail_out)
  355. return outl - zin->avail_out;
  356. }
  357. /*
  358. * No data in input buffer try to read some in, if an error then
  359. * return the total data read.
  360. */
  361. ret = BIO_read(next, ctx->ibuf, ctx->ibufsize);
  362. if (ret <= 0) {
  363. /* Total data read */
  364. int tot = outl - zin->avail_out;
  365. BIO_copy_next_retry(b);
  366. if (ret < 0)
  367. return (tot > 0) ? tot : ret;
  368. return tot;
  369. }
  370. zin->avail_in = ret;
  371. zin->next_in = ctx->ibuf;
  372. }
  373. }
  374. static int bio_zlib_write(BIO *b, const char *in, int inl)
  375. {
  376. BIO_ZLIB_CTX *ctx;
  377. int ret;
  378. z_stream *zout;
  379. BIO *next = BIO_next(b);
  380. if (!in || !inl)
  381. return 0;
  382. ctx = BIO_get_data(b);
  383. if (ctx->odone)
  384. return 0;
  385. zout = &ctx->zout;
  386. BIO_clear_retry_flags(b);
  387. if (!ctx->obuf) {
  388. ctx->obuf = OPENSSL_malloc(ctx->obufsize);
  389. /* Need error here */
  390. if (ctx->obuf == NULL) {
  391. COMPerr(COMP_F_BIO_ZLIB_WRITE, ERR_R_MALLOC_FAILURE);
  392. return 0;
  393. }
  394. ctx->optr = ctx->obuf;
  395. ctx->ocount = 0;
  396. deflateInit(zout, ctx->comp_level);
  397. zout->next_out = ctx->obuf;
  398. zout->avail_out = ctx->obufsize;
  399. }
  400. /* Obtain input data directly from supplied buffer */
  401. zout->next_in = (void *)in;
  402. zout->avail_in = inl;
  403. for (;;) {
  404. /* If data in output buffer write it first */
  405. while (ctx->ocount) {
  406. ret = BIO_write(next, ctx->optr, ctx->ocount);
  407. if (ret <= 0) {
  408. /* Total data written */
  409. int tot = inl - zout->avail_in;
  410. BIO_copy_next_retry(b);
  411. if (ret < 0)
  412. return (tot > 0) ? tot : ret;
  413. return tot;
  414. }
  415. ctx->optr += ret;
  416. ctx->ocount -= ret;
  417. }
  418. /* Have we consumed all supplied data? */
  419. if (!zout->avail_in)
  420. return inl;
  421. /* Compress some more */
  422. /* Reset buffer */
  423. ctx->optr = ctx->obuf;
  424. zout->next_out = ctx->obuf;
  425. zout->avail_out = ctx->obufsize;
  426. /* Compress some more */
  427. ret = deflate(zout, 0);
  428. if (ret != Z_OK) {
  429. COMPerr(COMP_F_BIO_ZLIB_WRITE, COMP_R_ZLIB_DEFLATE_ERROR);
  430. ERR_add_error_data(2, "zlib error:", zError(ret));
  431. return 0;
  432. }
  433. ctx->ocount = ctx->obufsize - zout->avail_out;
  434. }
  435. }
  436. static int bio_zlib_flush(BIO *b)
  437. {
  438. BIO_ZLIB_CTX *ctx;
  439. int ret;
  440. z_stream *zout;
  441. BIO *next = BIO_next(b);
  442. ctx = BIO_get_data(b);
  443. /* If no data written or already flush show success */
  444. if (!ctx->obuf || (ctx->odone && !ctx->ocount))
  445. return 1;
  446. zout = &ctx->zout;
  447. BIO_clear_retry_flags(b);
  448. /* No more input data */
  449. zout->next_in = NULL;
  450. zout->avail_in = 0;
  451. for (;;) {
  452. /* If data in output buffer write it first */
  453. while (ctx->ocount) {
  454. ret = BIO_write(next, ctx->optr, ctx->ocount);
  455. if (ret <= 0) {
  456. BIO_copy_next_retry(b);
  457. return ret;
  458. }
  459. ctx->optr += ret;
  460. ctx->ocount -= ret;
  461. }
  462. if (ctx->odone)
  463. return 1;
  464. /* Compress some more */
  465. /* Reset buffer */
  466. ctx->optr = ctx->obuf;
  467. zout->next_out = ctx->obuf;
  468. zout->avail_out = ctx->obufsize;
  469. /* Compress some more */
  470. ret = deflate(zout, Z_FINISH);
  471. if (ret == Z_STREAM_END)
  472. ctx->odone = 1;
  473. else if (ret != Z_OK) {
  474. COMPerr(COMP_F_BIO_ZLIB_FLUSH, COMP_R_ZLIB_DEFLATE_ERROR);
  475. ERR_add_error_data(2, "zlib error:", zError(ret));
  476. return 0;
  477. }
  478. ctx->ocount = ctx->obufsize - zout->avail_out;
  479. }
  480. }
  481. static long bio_zlib_ctrl(BIO *b, int cmd, long num, void *ptr)
  482. {
  483. BIO_ZLIB_CTX *ctx;
  484. int ret, *ip;
  485. int ibs, obs;
  486. BIO *next = BIO_next(b);
  487. if (next == NULL)
  488. return 0;
  489. ctx = BIO_get_data(b);
  490. switch (cmd) {
  491. case BIO_CTRL_RESET:
  492. ctx->ocount = 0;
  493. ctx->odone = 0;
  494. ret = 1;
  495. break;
  496. case BIO_CTRL_FLUSH:
  497. ret = bio_zlib_flush(b);
  498. if (ret > 0)
  499. ret = BIO_flush(next);
  500. break;
  501. case BIO_C_SET_BUFF_SIZE:
  502. ibs = -1;
  503. obs = -1;
  504. if (ptr != NULL) {
  505. ip = ptr;
  506. if (*ip == 0)
  507. ibs = (int)num;
  508. else
  509. obs = (int)num;
  510. } else {
  511. ibs = (int)num;
  512. obs = ibs;
  513. }
  514. if (ibs != -1) {
  515. OPENSSL_free(ctx->ibuf);
  516. ctx->ibuf = NULL;
  517. ctx->ibufsize = ibs;
  518. }
  519. if (obs != -1) {
  520. OPENSSL_free(ctx->obuf);
  521. ctx->obuf = NULL;
  522. ctx->obufsize = obs;
  523. }
  524. ret = 1;
  525. break;
  526. case BIO_C_DO_STATE_MACHINE:
  527. BIO_clear_retry_flags(b);
  528. ret = BIO_ctrl(next, cmd, num, ptr);
  529. BIO_copy_next_retry(b);
  530. break;
  531. case BIO_CTRL_WPENDING:
  532. if (ctx->obuf == NULL)
  533. return 0;
  534. if (ctx->odone) {
  535. ret = ctx->ocount;
  536. } else {
  537. ret = ctx->ocount;
  538. if (ret == 0)
  539. /* Unknown amount pending but we are not finished */
  540. ret = 1;
  541. }
  542. if (ret == 0)
  543. ret = BIO_ctrl(next, cmd, num, ptr);
  544. break;
  545. case BIO_CTRL_PENDING:
  546. ret = ctx->zin.avail_in;
  547. if (ret == 0)
  548. ret = BIO_ctrl(next, cmd, num, ptr);
  549. break;
  550. default:
  551. ret = BIO_ctrl(next, cmd, num, ptr);
  552. break;
  553. }
  554. return ret;
  555. }
  556. static long bio_zlib_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
  557. {
  558. BIO *next = BIO_next(b);
  559. if (next == NULL)
  560. return 0;
  561. return BIO_callback_ctrl(next, cmd, fp);
  562. }
  563. #endif