2
0

c_zlib.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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 "internal/cryptlib_int.h"
  16. #include "internal/bio.h"
  17. #include "comp_lcl.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. if (zlib_dso != NULL)
  229. DSO_free(zlib_dso);
  230. zlib_dso = NULL;
  231. #endif
  232. }
  233. #ifdef ZLIB
  234. /* Zlib based compression/decompression filter BIO */
  235. typedef struct {
  236. unsigned char *ibuf; /* Input buffer */
  237. int ibufsize; /* Buffer size */
  238. z_stream zin; /* Input decompress context */
  239. unsigned char *obuf; /* Output buffer */
  240. int obufsize; /* Output buffer size */
  241. unsigned char *optr; /* Position in output buffer */
  242. int ocount; /* Amount of data in output buffer */
  243. int odone; /* deflate EOF */
  244. int comp_level; /* Compression level to use */
  245. z_stream zout; /* Output compression context */
  246. } BIO_ZLIB_CTX;
  247. # define ZLIB_DEFAULT_BUFSIZE 1024
  248. static int bio_zlib_new(BIO *bi);
  249. static int bio_zlib_free(BIO *bi);
  250. static int bio_zlib_read(BIO *b, char *out, int outl);
  251. static int bio_zlib_write(BIO *b, const char *in, int inl);
  252. static long bio_zlib_ctrl(BIO *b, int cmd, long num, void *ptr);
  253. static long bio_zlib_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp);
  254. static const BIO_METHOD bio_meth_zlib = {
  255. BIO_TYPE_COMP,
  256. "zlib",
  257. /* TODO: Convert to new style write function */
  258. bwrite_conv,
  259. bio_zlib_write,
  260. /* TODO: Convert to new style read function */
  261. bread_conv,
  262. bio_zlib_read,
  263. NULL,
  264. NULL,
  265. bio_zlib_ctrl,
  266. bio_zlib_new,
  267. bio_zlib_free,
  268. bio_zlib_callback_ctrl
  269. };
  270. const BIO_METHOD *BIO_f_zlib(void)
  271. {
  272. return &bio_meth_zlib;
  273. }
  274. static int bio_zlib_new(BIO *bi)
  275. {
  276. BIO_ZLIB_CTX *ctx;
  277. # ifdef ZLIB_SHARED
  278. (void)COMP_zlib();
  279. if (!zlib_loaded) {
  280. COMPerr(COMP_F_BIO_ZLIB_NEW, COMP_R_ZLIB_NOT_SUPPORTED);
  281. return 0;
  282. }
  283. # endif
  284. ctx = OPENSSL_zalloc(sizeof(*ctx));
  285. if (ctx == NULL) {
  286. COMPerr(COMP_F_BIO_ZLIB_NEW, ERR_R_MALLOC_FAILURE);
  287. return 0;
  288. }
  289. ctx->ibufsize = ZLIB_DEFAULT_BUFSIZE;
  290. ctx->obufsize = ZLIB_DEFAULT_BUFSIZE;
  291. ctx->zin.zalloc = Z_NULL;
  292. ctx->zin.zfree = Z_NULL;
  293. ctx->zout.zalloc = Z_NULL;
  294. ctx->zout.zfree = Z_NULL;
  295. ctx->comp_level = Z_DEFAULT_COMPRESSION;
  296. BIO_set_init(bi, 1);
  297. BIO_set_data(bi, ctx);
  298. return 1;
  299. }
  300. static int bio_zlib_free(BIO *bi)
  301. {
  302. BIO_ZLIB_CTX *ctx;
  303. if (!bi)
  304. return 0;
  305. ctx = BIO_get_data(bi);
  306. if (ctx->ibuf) {
  307. /* Destroy decompress context */
  308. inflateEnd(&ctx->zin);
  309. OPENSSL_free(ctx->ibuf);
  310. }
  311. if (ctx->obuf) {
  312. /* Destroy compress context */
  313. deflateEnd(&ctx->zout);
  314. OPENSSL_free(ctx->obuf);
  315. }
  316. OPENSSL_free(ctx);
  317. BIO_set_data(bi, NULL);
  318. BIO_set_init(bi, 0);
  319. return 1;
  320. }
  321. static int bio_zlib_read(BIO *b, char *out, int outl)
  322. {
  323. BIO_ZLIB_CTX *ctx;
  324. int ret;
  325. z_stream *zin;
  326. BIO *next = BIO_next(b);
  327. if (!out || !outl)
  328. return 0;
  329. ctx = BIO_get_data(b);
  330. zin = &ctx->zin;
  331. BIO_clear_retry_flags(b);
  332. if (!ctx->ibuf) {
  333. ctx->ibuf = OPENSSL_malloc(ctx->ibufsize);
  334. if (ctx->ibuf == NULL) {
  335. COMPerr(COMP_F_BIO_ZLIB_READ, ERR_R_MALLOC_FAILURE);
  336. return 0;
  337. }
  338. inflateInit(zin);
  339. zin->next_in = ctx->ibuf;
  340. zin->avail_in = 0;
  341. }
  342. /* Copy output data directly to supplied buffer */
  343. zin->next_out = (unsigned char *)out;
  344. zin->avail_out = (unsigned int)outl;
  345. for (;;) {
  346. /* Decompress while data available */
  347. while (zin->avail_in) {
  348. ret = inflate(zin, 0);
  349. if ((ret != Z_OK) && (ret != Z_STREAM_END)) {
  350. COMPerr(COMP_F_BIO_ZLIB_READ, COMP_R_ZLIB_INFLATE_ERROR);
  351. ERR_add_error_data(2, "zlib error:", zError(ret));
  352. return 0;
  353. }
  354. /* If EOF or we've read everything then return */
  355. if ((ret == Z_STREAM_END) || !zin->avail_out)
  356. return outl - zin->avail_out;
  357. }
  358. /*
  359. * No data in input buffer try to read some in, if an error then
  360. * return the total data read.
  361. */
  362. ret = BIO_read(next, ctx->ibuf, ctx->ibufsize);
  363. if (ret <= 0) {
  364. /* Total data read */
  365. int tot = outl - zin->avail_out;
  366. BIO_copy_next_retry(b);
  367. if (ret < 0)
  368. return (tot > 0) ? tot : ret;
  369. return tot;
  370. }
  371. zin->avail_in = ret;
  372. zin->next_in = ctx->ibuf;
  373. }
  374. }
  375. static int bio_zlib_write(BIO *b, const char *in, int inl)
  376. {
  377. BIO_ZLIB_CTX *ctx;
  378. int ret;
  379. z_stream *zout;
  380. BIO *next = BIO_next(b);
  381. if (!in || !inl)
  382. return 0;
  383. ctx = BIO_get_data(b);
  384. if (ctx->odone)
  385. return 0;
  386. zout = &ctx->zout;
  387. BIO_clear_retry_flags(b);
  388. if (!ctx->obuf) {
  389. ctx->obuf = OPENSSL_malloc(ctx->obufsize);
  390. /* Need error here */
  391. if (ctx->obuf == NULL) {
  392. COMPerr(COMP_F_BIO_ZLIB_WRITE, ERR_R_MALLOC_FAILURE);
  393. return 0;
  394. }
  395. ctx->optr = ctx->obuf;
  396. ctx->ocount = 0;
  397. deflateInit(zout, ctx->comp_level);
  398. zout->next_out = ctx->obuf;
  399. zout->avail_out = ctx->obufsize;
  400. }
  401. /* Obtain input data directly from supplied buffer */
  402. zout->next_in = (void *)in;
  403. zout->avail_in = inl;
  404. for (;;) {
  405. /* If data in output buffer write it first */
  406. while (ctx->ocount) {
  407. ret = BIO_write(next, ctx->optr, ctx->ocount);
  408. if (ret <= 0) {
  409. /* Total data written */
  410. int tot = inl - zout->avail_in;
  411. BIO_copy_next_retry(b);
  412. if (ret < 0)
  413. return (tot > 0) ? tot : ret;
  414. return tot;
  415. }
  416. ctx->optr += ret;
  417. ctx->ocount -= ret;
  418. }
  419. /* Have we consumed all supplied data? */
  420. if (!zout->avail_in)
  421. return inl;
  422. /* Compress some more */
  423. /* Reset buffer */
  424. ctx->optr = ctx->obuf;
  425. zout->next_out = ctx->obuf;
  426. zout->avail_out = ctx->obufsize;
  427. /* Compress some more */
  428. ret = deflate(zout, 0);
  429. if (ret != Z_OK) {
  430. COMPerr(COMP_F_BIO_ZLIB_WRITE, COMP_R_ZLIB_DEFLATE_ERROR);
  431. ERR_add_error_data(2, "zlib error:", zError(ret));
  432. return 0;
  433. }
  434. ctx->ocount = ctx->obufsize - zout->avail_out;
  435. }
  436. }
  437. static int bio_zlib_flush(BIO *b)
  438. {
  439. BIO_ZLIB_CTX *ctx;
  440. int ret;
  441. z_stream *zout;
  442. BIO *next = BIO_next(b);
  443. ctx = BIO_get_data(b);
  444. /* If no data written or already flush show success */
  445. if (!ctx->obuf || (ctx->odone && !ctx->ocount))
  446. return 1;
  447. zout = &ctx->zout;
  448. BIO_clear_retry_flags(b);
  449. /* No more input data */
  450. zout->next_in = NULL;
  451. zout->avail_in = 0;
  452. for (;;) {
  453. /* If data in output buffer write it first */
  454. while (ctx->ocount) {
  455. ret = BIO_write(next, ctx->optr, ctx->ocount);
  456. if (ret <= 0) {
  457. BIO_copy_next_retry(b);
  458. return ret;
  459. }
  460. ctx->optr += ret;
  461. ctx->ocount -= ret;
  462. }
  463. if (ctx->odone)
  464. return 1;
  465. /* Compress some more */
  466. /* Reset buffer */
  467. ctx->optr = ctx->obuf;
  468. zout->next_out = ctx->obuf;
  469. zout->avail_out = ctx->obufsize;
  470. /* Compress some more */
  471. ret = deflate(zout, Z_FINISH);
  472. if (ret == Z_STREAM_END)
  473. ctx->odone = 1;
  474. else if (ret != Z_OK) {
  475. COMPerr(COMP_F_BIO_ZLIB_FLUSH, COMP_R_ZLIB_DEFLATE_ERROR);
  476. ERR_add_error_data(2, "zlib error:", zError(ret));
  477. return 0;
  478. }
  479. ctx->ocount = ctx->obufsize - zout->avail_out;
  480. }
  481. }
  482. static long bio_zlib_ctrl(BIO *b, int cmd, long num, void *ptr)
  483. {
  484. BIO_ZLIB_CTX *ctx;
  485. int ret, *ip;
  486. int ibs, obs;
  487. BIO *next = BIO_next(b);
  488. if (next == NULL)
  489. return 0;
  490. ctx = BIO_get_data(b);
  491. switch (cmd) {
  492. case BIO_CTRL_RESET:
  493. ctx->ocount = 0;
  494. ctx->odone = 0;
  495. ret = 1;
  496. break;
  497. case BIO_CTRL_FLUSH:
  498. ret = bio_zlib_flush(b);
  499. if (ret > 0)
  500. ret = BIO_flush(next);
  501. break;
  502. case BIO_C_SET_BUFF_SIZE:
  503. ibs = -1;
  504. obs = -1;
  505. if (ptr != NULL) {
  506. ip = ptr;
  507. if (*ip == 0)
  508. ibs = (int)num;
  509. else
  510. obs = (int)num;
  511. } else {
  512. ibs = (int)num;
  513. obs = ibs;
  514. }
  515. if (ibs != -1) {
  516. OPENSSL_free(ctx->ibuf);
  517. ctx->ibuf = NULL;
  518. ctx->ibufsize = ibs;
  519. }
  520. if (obs != -1) {
  521. OPENSSL_free(ctx->obuf);
  522. ctx->obuf = NULL;
  523. ctx->obufsize = obs;
  524. }
  525. ret = 1;
  526. break;
  527. case BIO_C_DO_STATE_MACHINE:
  528. BIO_clear_retry_flags(b);
  529. ret = BIO_ctrl(next, cmd, num, ptr);
  530. BIO_copy_next_retry(b);
  531. break;
  532. default:
  533. ret = BIO_ctrl(next, cmd, num, ptr);
  534. break;
  535. }
  536. return ret;
  537. }
  538. static long bio_zlib_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
  539. {
  540. BIO *next = BIO_next(b);
  541. if (next == NULL)
  542. return 0;
  543. return BIO_callback_ctrl(next, cmd, fp);
  544. }
  545. #endif