zip_source_pkware.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. zip_source_pkware.c -- Traditional PKWARE de/encryption routines
  3. Copyright (C) 2009-2015 Dieter Baron and Thomas Klausner
  4. This file is part of libzip, a library to manipulate ZIP archives.
  5. The authors can be contacted at <libzip@nih.at>
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions
  8. are met:
  9. 1. Redistributions of source code must retain the above copyright
  10. notice, this list of conditions and the following disclaimer.
  11. 2. Redistributions in binary form must reproduce the above copyright
  12. notice, this list of conditions and the following disclaimer in
  13. the documentation and/or other materials provided with the
  14. distribution.
  15. 3. The names of the authors may not be used to endorse or promote
  16. products derived from this software without specific prior
  17. written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
  19. OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
  22. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  24. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  27. OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  28. IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include "zipint.h"
  33. struct trad_pkware {
  34. zip_error_t error;
  35. zip_uint32_t key[3];
  36. };
  37. #define HEADERLEN 12
  38. #define KEY0 305419896
  39. #define KEY1 591751049
  40. #define KEY2 878082192
  41. static void decrypt(struct trad_pkware *, zip_uint8_t *,
  42. const zip_uint8_t *, zip_uint64_t, int);
  43. static int decrypt_header(zip_source_t *, struct trad_pkware *);
  44. static zip_int64_t pkware_decrypt(zip_source_t *, void *, void *,
  45. zip_uint64_t, zip_source_cmd_t);
  46. static void pkware_free(struct trad_pkware *);
  47. zip_source_t *
  48. zip_source_pkware(zip_t *za, zip_source_t *src,
  49. zip_uint16_t em, int flags, const char *password)
  50. {
  51. struct trad_pkware *ctx;
  52. zip_source_t *s2;
  53. if (password == NULL || src == NULL || em != ZIP_EM_TRAD_PKWARE) {
  54. zip_error_set(&za->error, ZIP_ER_INVAL, 0);
  55. return NULL;
  56. }
  57. if (flags & ZIP_CODEC_ENCODE) {
  58. zip_error_set(&za->error, ZIP_ER_ENCRNOTSUPP, 0);
  59. return NULL;
  60. }
  61. if ((ctx=(struct trad_pkware *)malloc(sizeof(*ctx))) == NULL) {
  62. zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
  63. return NULL;
  64. }
  65. zip_error_init(&ctx->error);
  66. ctx->key[0] = KEY0;
  67. ctx->key[1] = KEY1;
  68. ctx->key[2] = KEY2;
  69. decrypt(ctx, NULL, (const zip_uint8_t *)password, strlen(password), 1);
  70. if ((s2=zip_source_layered(za, src, pkware_decrypt, ctx)) == NULL) {
  71. pkware_free(ctx);
  72. return NULL;
  73. }
  74. return s2;
  75. }
  76. static void
  77. decrypt(struct trad_pkware *ctx, zip_uint8_t *out, const zip_uint8_t *in,
  78. zip_uint64_t len, int update_only)
  79. {
  80. zip_uint16_t tmp;
  81. zip_uint64_t i;
  82. Bytef b;
  83. for (i=0; i<len; i++) {
  84. b = in[i];
  85. if (!update_only) {
  86. /* decrypt next byte */
  87. tmp = (zip_uint16_t)(ctx->key[2] | 2);
  88. tmp = (zip_uint16_t)(((zip_uint32_t)tmp * (tmp ^ 1)) >> 8);
  89. b ^= (Bytef)tmp;
  90. }
  91. /* store cleartext */
  92. if (out)
  93. out[i] = b;
  94. /* update keys */
  95. ctx->key[0] = (zip_uint32_t)crc32(ctx->key[0] ^ 0xffffffffUL, &b, 1) ^ 0xffffffffUL;
  96. ctx->key[1] = (ctx->key[1] + (ctx->key[0] & 0xff)) * 134775813 + 1;
  97. b = (Bytef)(ctx->key[1] >> 24);
  98. ctx->key[2] = (zip_uint32_t)crc32(ctx->key[2] ^ 0xffffffffUL, &b, 1) ^ 0xffffffffUL;
  99. }
  100. }
  101. static int
  102. decrypt_header(zip_source_t *src, struct trad_pkware *ctx)
  103. {
  104. zip_uint8_t header[HEADERLEN];
  105. struct zip_stat st;
  106. zip_int64_t n;
  107. unsigned short dostime, dosdate;
  108. if ((n=zip_source_read(src, header, HEADERLEN)) < 0) {
  109. _zip_error_set_from_source(&ctx->error, src);
  110. return -1;
  111. }
  112. if (n != HEADERLEN) {
  113. zip_error_set(&ctx->error, ZIP_ER_EOF, 0);
  114. return -1;
  115. }
  116. decrypt(ctx, header, header, HEADERLEN, 0);
  117. if (zip_source_stat(src, &st) < 0) {
  118. /* stat failed, skip password validation */
  119. return 0;
  120. }
  121. _zip_u2d_time(st.mtime, &dostime, &dosdate);
  122. if (header[HEADERLEN-1] != st.crc>>24 && header[HEADERLEN-1] != dostime>>8) {
  123. zip_error_set(&ctx->error, ZIP_ER_WRONGPASSWD, 0);
  124. return -1;
  125. }
  126. return 0;
  127. }
  128. static zip_int64_t
  129. pkware_decrypt(zip_source_t *src, void *ud, void *data,
  130. zip_uint64_t len, zip_source_cmd_t cmd)
  131. {
  132. struct trad_pkware *ctx;
  133. zip_int64_t n;
  134. ctx = (struct trad_pkware *)ud;
  135. switch (cmd) {
  136. case ZIP_SOURCE_OPEN:
  137. if (decrypt_header(src, ctx) < 0)
  138. return -1;
  139. return 0;
  140. case ZIP_SOURCE_READ:
  141. if ((n=zip_source_read(src, data, len)) < 0) {
  142. _zip_error_set_from_source(&ctx->error, src);
  143. return -1;
  144. }
  145. decrypt((struct trad_pkware *)ud, (zip_uint8_t *)data, (zip_uint8_t *)data, (zip_uint64_t)n, 0);
  146. return n;
  147. case ZIP_SOURCE_CLOSE:
  148. return 0;
  149. case ZIP_SOURCE_STAT:
  150. {
  151. zip_stat_t *st;
  152. st = (zip_stat_t *)data;
  153. st->encryption_method = ZIP_EM_NONE;
  154. st->valid |= ZIP_STAT_ENCRYPTION_METHOD;
  155. /* TODO: deduce HEADERLEN from size for uncompressed */
  156. if (st->valid & ZIP_STAT_COMP_SIZE)
  157. st->comp_size -= HEADERLEN;
  158. return 0;
  159. }
  160. case ZIP_SOURCE_SUPPORTS:
  161. return zip_source_make_command_bitmap(ZIP_SOURCE_OPEN, ZIP_SOURCE_READ, ZIP_SOURCE_CLOSE, ZIP_SOURCE_STAT, ZIP_SOURCE_ERROR, ZIP_SOURCE_FREE, -1);
  162. case ZIP_SOURCE_ERROR:
  163. return zip_error_to_data(&ctx->error, data, len);
  164. case ZIP_SOURCE_FREE:
  165. pkware_free(ctx);
  166. return 0;
  167. default:
  168. zip_error_set(&ctx->error, ZIP_ER_INVAL, 0);
  169. return -1;
  170. }
  171. }
  172. static void
  173. pkware_free(struct trad_pkware *ctx)
  174. {
  175. free(ctx);
  176. }