c_zlib.c 17 KB

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