zip_source_winzip_aes_decode.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. zip_source_winzip_aes_decode.c -- Winzip AES decryption routines
  3. Copyright (C) 2009-2021 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 winzip_aes {
  34. char *password;
  35. zip_uint16_t encryption_method;
  36. zip_uint64_t data_length;
  37. zip_uint64_t current_position;
  38. zip_winzip_aes_t *aes_ctx;
  39. zip_error_t error;
  40. };
  41. static int decrypt_header(zip_source_t *src, struct winzip_aes *ctx);
  42. static void winzip_aes_free(struct winzip_aes *);
  43. static zip_int64_t winzip_aes_decrypt(zip_source_t *src, void *ud, void *data, zip_uint64_t len, zip_source_cmd_t cmd);
  44. static struct winzip_aes *winzip_aes_new(zip_uint16_t encryption_method, const char *password, zip_error_t *error);
  45. zip_source_t *
  46. zip_source_winzip_aes_decode(zip_t *za, zip_source_t *src, zip_uint16_t encryption_method, int flags, const char *password) {
  47. zip_source_t *s2;
  48. zip_stat_t st;
  49. zip_uint64_t aux_length;
  50. struct winzip_aes *ctx;
  51. if ((encryption_method != ZIP_EM_AES_128 && encryption_method != ZIP_EM_AES_192 && encryption_method != ZIP_EM_AES_256) || password == NULL || src == NULL) {
  52. zip_error_set(&za->error, ZIP_ER_INVAL, 0);
  53. return NULL;
  54. }
  55. if (flags & ZIP_CODEC_ENCODE) {
  56. zip_error_set(&za->error, ZIP_ER_ENCRNOTSUPP, 0);
  57. return NULL;
  58. }
  59. if (zip_source_stat(src, &st) != 0) {
  60. _zip_error_set_from_source(&za->error, src);
  61. return NULL;
  62. }
  63. aux_length = WINZIP_AES_PASSWORD_VERIFY_LENGTH + SALT_LENGTH(encryption_method) + HMAC_LENGTH;
  64. if ((st.valid & ZIP_STAT_COMP_SIZE) == 0 || st.comp_size < aux_length) {
  65. zip_error_set(&za->error, ZIP_ER_OPNOTSUPP, 0);
  66. return NULL;
  67. }
  68. if ((ctx = winzip_aes_new(encryption_method, password, &za->error)) == NULL) {
  69. return NULL;
  70. }
  71. ctx->data_length = st.comp_size - aux_length;
  72. if ((s2 = zip_source_layered(za, src, winzip_aes_decrypt, ctx)) == NULL) {
  73. winzip_aes_free(ctx);
  74. return NULL;
  75. }
  76. return s2;
  77. }
  78. static int
  79. decrypt_header(zip_source_t *src, struct winzip_aes *ctx) {
  80. zip_uint8_t header[WINZIP_AES_MAX_HEADER_LENGTH];
  81. zip_uint8_t password_verification[WINZIP_AES_PASSWORD_VERIFY_LENGTH];
  82. unsigned int headerlen;
  83. zip_int64_t n;
  84. headerlen = WINZIP_AES_PASSWORD_VERIFY_LENGTH + SALT_LENGTH(ctx->encryption_method);
  85. if ((n = zip_source_read(src, header, headerlen)) < 0) {
  86. _zip_error_set_from_source(&ctx->error, src);
  87. return -1;
  88. }
  89. if (n != headerlen) {
  90. zip_error_set(&ctx->error, ZIP_ER_EOF, 0);
  91. return -1;
  92. }
  93. if ((ctx->aes_ctx = _zip_winzip_aes_new((zip_uint8_t *)ctx->password, strlen(ctx->password), header, ctx->encryption_method, password_verification, &ctx->error)) == NULL) {
  94. return -1;
  95. }
  96. if (memcmp(password_verification, header + SALT_LENGTH(ctx->encryption_method), WINZIP_AES_PASSWORD_VERIFY_LENGTH) != 0) {
  97. _zip_winzip_aes_free(ctx->aes_ctx);
  98. ctx->aes_ctx = NULL;
  99. zip_error_set(&ctx->error, ZIP_ER_WRONGPASSWD, 0);
  100. return -1;
  101. }
  102. return 0;
  103. }
  104. static bool
  105. verify_hmac(zip_source_t *src, struct winzip_aes *ctx) {
  106. unsigned char computed[SHA1_LENGTH], from_file[HMAC_LENGTH];
  107. if (zip_source_read(src, from_file, HMAC_LENGTH) < HMAC_LENGTH) {
  108. _zip_error_set_from_source(&ctx->error, src);
  109. return false;
  110. }
  111. if (!_zip_winzip_aes_finish(ctx->aes_ctx, computed)) {
  112. zip_error_set(&ctx->error, ZIP_ER_INTERNAL, 0);
  113. return false;
  114. }
  115. _zip_winzip_aes_free(ctx->aes_ctx);
  116. ctx->aes_ctx = NULL;
  117. if (memcmp(from_file, computed, HMAC_LENGTH) != 0) {
  118. zip_error_set(&ctx->error, ZIP_ER_CRC, 0);
  119. return false;
  120. }
  121. return true;
  122. }
  123. static zip_int64_t
  124. winzip_aes_decrypt(zip_source_t *src, void *ud, void *data, zip_uint64_t len, zip_source_cmd_t cmd) {
  125. struct winzip_aes *ctx;
  126. zip_int64_t n;
  127. ctx = (struct winzip_aes *)ud;
  128. switch (cmd) {
  129. case ZIP_SOURCE_OPEN:
  130. if (decrypt_header(src, ctx) < 0) {
  131. return -1;
  132. }
  133. ctx->current_position = 0;
  134. return 0;
  135. case ZIP_SOURCE_READ:
  136. if (len > ctx->data_length - ctx->current_position) {
  137. len = ctx->data_length - ctx->current_position;
  138. }
  139. if (len == 0) {
  140. if (!verify_hmac(src, ctx)) {
  141. return -1;
  142. }
  143. return 0;
  144. }
  145. if ((n = zip_source_read(src, data, len)) < 0) {
  146. _zip_error_set_from_source(&ctx->error, src);
  147. return -1;
  148. }
  149. ctx->current_position += (zip_uint64_t)n;
  150. if (!_zip_winzip_aes_decrypt(ctx->aes_ctx, (zip_uint8_t *)data, (zip_uint64_t)n)) {
  151. zip_error_set(&ctx->error, ZIP_ER_INTERNAL, 0);
  152. return -1;
  153. }
  154. return n;
  155. case ZIP_SOURCE_CLOSE:
  156. return 0;
  157. case ZIP_SOURCE_STAT: {
  158. zip_stat_t *st;
  159. st = (zip_stat_t *)data;
  160. st->encryption_method = ZIP_EM_NONE;
  161. st->valid |= ZIP_STAT_ENCRYPTION_METHOD;
  162. if (st->valid & ZIP_STAT_COMP_SIZE) {
  163. st->comp_size -= 12 + SALT_LENGTH(ctx->encryption_method);
  164. }
  165. return 0;
  166. }
  167. case ZIP_SOURCE_SUPPORTS:
  168. 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);
  169. case ZIP_SOURCE_ERROR:
  170. return zip_error_to_data(&ctx->error, data, len);
  171. case ZIP_SOURCE_FREE:
  172. winzip_aes_free(ctx);
  173. return 0;
  174. default:
  175. zip_error_set(&ctx->error, ZIP_ER_INVAL, 0);
  176. return -1;
  177. }
  178. }
  179. static void
  180. winzip_aes_free(struct winzip_aes *ctx) {
  181. if (ctx == NULL) {
  182. return;
  183. }
  184. _zip_crypto_clear(ctx->password, strlen(ctx->password));
  185. free(ctx->password);
  186. zip_error_fini(&ctx->error);
  187. _zip_winzip_aes_free(ctx->aes_ctx);
  188. free(ctx);
  189. }
  190. static struct winzip_aes *
  191. winzip_aes_new(zip_uint16_t encryption_method, const char *password, zip_error_t *error) {
  192. struct winzip_aes *ctx;
  193. if ((ctx = (struct winzip_aes *)malloc(sizeof(*ctx))) == NULL) {
  194. zip_error_set(error, ZIP_ER_MEMORY, 0);
  195. return NULL;
  196. }
  197. if ((ctx->password = strdup(password)) == NULL) {
  198. zip_error_set(error, ZIP_ER_MEMORY, 0);
  199. free(ctx);
  200. return NULL;
  201. }
  202. ctx->encryption_method = encryption_method;
  203. ctx->aes_ctx = NULL;
  204. zip_error_init(&ctx->error);
  205. return ctx;
  206. }