/src/libzip/lib/zip_source_winzip_aes_decode.c
Line | Count | Source |
1 | | /* |
2 | | zip_source_winzip_aes_decode.c -- Winzip AES decryption routines |
3 | | Copyright (C) 2009-2024 Dieter Baron and Thomas Klausner |
4 | | |
5 | | This file is part of libzip, a library to manipulate ZIP archives. |
6 | | The authors can be contacted at <info@libzip.org> |
7 | | |
8 | | Redistribution and use in source and binary forms, with or without |
9 | | modification, are permitted provided that the following conditions |
10 | | are met: |
11 | | 1. Redistributions of source code must retain the above copyright |
12 | | notice, this list of conditions and the following disclaimer. |
13 | | 2. Redistributions in binary form must reproduce the above copyright |
14 | | notice, this list of conditions and the following disclaimer in |
15 | | the documentation and/or other materials provided with the |
16 | | distribution. |
17 | | 3. The names of the authors may not be used to endorse or promote |
18 | | products derived from this software without specific prior |
19 | | written permission. |
20 | | |
21 | | THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS |
22 | | OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
23 | | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
24 | | ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY |
25 | | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
26 | | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
27 | | GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
28 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
29 | | IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
30 | | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
31 | | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
32 | | */ |
33 | | |
34 | | |
35 | | #include <stdlib.h> |
36 | | #include <string.h> |
37 | | |
38 | | #include "zipint.h" |
39 | | |
40 | | #include "zip_crypto.h" |
41 | | |
42 | | struct winzip_aes { |
43 | | char *password; |
44 | | zip_uint16_t encryption_method; |
45 | | |
46 | | zip_uint64_t data_length; |
47 | | zip_uint64_t current_position; |
48 | | |
49 | | zip_winzip_aes_t *aes_ctx; |
50 | | zip_error_t error; |
51 | | }; |
52 | | |
53 | | |
54 | | static int decrypt_header(zip_source_t *src, struct winzip_aes *ctx); |
55 | | static void winzip_aes_free(struct winzip_aes *); |
56 | | static zip_int64_t winzip_aes_decrypt(zip_source_t *src, void *ud, void *data, zip_uint64_t len, zip_source_cmd_t cmd); |
57 | | static struct winzip_aes *winzip_aes_new(zip_uint16_t encryption_method, const char *password, zip_error_t *error); |
58 | | |
59 | | |
60 | 559 | zip_source_t *zip_source_winzip_aes_decode(zip_t *za, zip_source_t *src, zip_uint16_t encryption_method, int flags, const char *password) { |
61 | 559 | zip_source_t *s2; |
62 | 559 | zip_stat_t st; |
63 | 559 | zip_uint64_t aux_length; |
64 | 559 | struct winzip_aes *ctx; |
65 | | |
66 | 559 | if ((encryption_method != ZIP_EM_AES_128 && encryption_method != ZIP_EM_AES_192 && encryption_method != ZIP_EM_AES_256) || password == NULL || src == NULL) { |
67 | 0 | zip_error_set(&za->error, ZIP_ER_INVAL, 0); |
68 | 0 | return NULL; |
69 | 0 | } |
70 | 559 | if (flags & ZIP_CODEC_ENCODE) { |
71 | 0 | zip_error_set(&za->error, ZIP_ER_ENCRNOTSUPP, 0); |
72 | 0 | return NULL; |
73 | 0 | } |
74 | | |
75 | 559 | if (zip_source_stat(src, &st) != 0) { |
76 | 0 | zip_error_set_from_source(&za->error, src); |
77 | 0 | return NULL; |
78 | 0 | } |
79 | | |
80 | 559 | aux_length = WINZIP_AES_PASSWORD_VERIFY_LENGTH + SALT_LENGTH(encryption_method) + HMAC_LENGTH; |
81 | | |
82 | 559 | if ((st.valid & ZIP_STAT_COMP_SIZE) == 0 || st.comp_size < aux_length) { |
83 | 15 | zip_error_set(&za->error, ZIP_ER_OPNOTSUPP, 0); |
84 | 15 | return NULL; |
85 | 15 | } |
86 | | |
87 | 544 | if ((ctx = winzip_aes_new(encryption_method, password, &za->error)) == NULL) { |
88 | 0 | return NULL; |
89 | 0 | } |
90 | | |
91 | 544 | ctx->data_length = st.comp_size - aux_length; |
92 | | |
93 | 544 | if ((s2 = zip_source_layered(za, src, winzip_aes_decrypt, ctx)) == NULL) { |
94 | 0 | winzip_aes_free(ctx); |
95 | 0 | return NULL; |
96 | 0 | } |
97 | | |
98 | 544 | return s2; |
99 | 544 | } |
100 | | |
101 | | |
102 | 484 | static int decrypt_header(zip_source_t *src, struct winzip_aes *ctx) { |
103 | 484 | zip_uint8_t header[WINZIP_AES_MAX_HEADER_LENGTH]; |
104 | 484 | zip_uint8_t password_verification[WINZIP_AES_PASSWORD_VERIFY_LENGTH]; |
105 | 484 | unsigned int headerlen; |
106 | 484 | zip_int64_t n; |
107 | | |
108 | 484 | headerlen = WINZIP_AES_PASSWORD_VERIFY_LENGTH + SALT_LENGTH(ctx->encryption_method); |
109 | 484 | if ((n = zip_source_read(src, header, headerlen)) < 0) { |
110 | 29 | zip_error_set_from_source(&ctx->error, src); |
111 | 29 | return -1; |
112 | 29 | } |
113 | | |
114 | 455 | if (n != headerlen) { |
115 | 25 | zip_error_set(&ctx->error, ZIP_ER_EOF, 0); |
116 | 25 | return -1; |
117 | 25 | } |
118 | | |
119 | 430 | 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) { |
120 | 0 | return -1; |
121 | 0 | } |
122 | 430 | if (memcmp(password_verification, header + SALT_LENGTH(ctx->encryption_method), WINZIP_AES_PASSWORD_VERIFY_LENGTH) != 0) { |
123 | 90 | _zip_winzip_aes_free(ctx->aes_ctx); |
124 | 90 | ctx->aes_ctx = NULL; |
125 | 90 | zip_error_set(&ctx->error, ZIP_ER_WRONGPASSWD, 0); |
126 | 90 | return -1; |
127 | 90 | } |
128 | 340 | return 0; |
129 | 430 | } |
130 | | |
131 | | |
132 | 124 | static bool verify_hmac(zip_source_t *src, struct winzip_aes *ctx) { |
133 | 124 | unsigned char computed[ZIP_CRYPTO_SHA1_LENGTH], from_file[HMAC_LENGTH]; |
134 | 124 | if (zip_source_read(src, from_file, HMAC_LENGTH) < HMAC_LENGTH) { |
135 | 19 | zip_error_set_from_source(&ctx->error, src); |
136 | 19 | return false; |
137 | 19 | } |
138 | | |
139 | 105 | if (!_zip_winzip_aes_finish(ctx->aes_ctx, computed)) { |
140 | 0 | zip_error_set(&ctx->error, ZIP_ER_INTERNAL, 0); |
141 | 0 | return false; |
142 | 0 | } |
143 | 105 | _zip_winzip_aes_free(ctx->aes_ctx); |
144 | 105 | ctx->aes_ctx = NULL; |
145 | | |
146 | 105 | if (memcmp(from_file, computed, HMAC_LENGTH) != 0) { |
147 | 90 | zip_error_set(&ctx->error, ZIP_ER_CRC, 0); |
148 | 90 | return false; |
149 | 90 | } |
150 | | |
151 | 15 | return true; |
152 | 105 | } |
153 | | |
154 | | |
155 | 4.48k | static zip_int64_t winzip_aes_decrypt(zip_source_t *src, void *ud, void *data, zip_uint64_t len, zip_source_cmd_t cmd) { |
156 | 4.48k | struct winzip_aes *ctx; |
157 | 4.48k | zip_int64_t n; |
158 | | |
159 | 4.48k | ctx = (struct winzip_aes *)ud; |
160 | | |
161 | 4.48k | switch (cmd) { |
162 | 484 | case ZIP_SOURCE_OPEN: |
163 | 484 | if (decrypt_header(src, ctx) < 0) { |
164 | 144 | return -1; |
165 | 144 | } |
166 | 340 | ctx->current_position = 0; |
167 | 340 | return 0; |
168 | | |
169 | 1.82k | case ZIP_SOURCE_READ: |
170 | 1.82k | if (len > ctx->data_length - ctx->current_position) { |
171 | 284 | len = ctx->data_length - ctx->current_position; |
172 | 284 | } |
173 | | |
174 | 1.82k | if (len == 0) { |
175 | 124 | if (!verify_hmac(src, ctx)) { |
176 | 109 | return -1; |
177 | 109 | } |
178 | 15 | return 0; |
179 | 124 | } |
180 | | |
181 | 1.70k | if ((n = zip_source_read(src, data, len)) < 0) { |
182 | 209 | zip_error_set_from_source(&ctx->error, src); |
183 | 209 | return -1; |
184 | 209 | } |
185 | 1.49k | ctx->current_position += (zip_uint64_t)n; |
186 | | |
187 | 1.49k | if (!_zip_winzip_aes_decrypt(ctx->aes_ctx, (zip_uint8_t *)data, (zip_uint64_t)n)) { |
188 | 0 | zip_error_set(&ctx->error, ZIP_ER_INTERNAL, 0); |
189 | 0 | return -1; |
190 | 0 | } |
191 | | |
192 | 1.49k | return n; |
193 | | |
194 | 340 | case ZIP_SOURCE_CLOSE: |
195 | 340 | return 0; |
196 | | |
197 | 280 | case ZIP_SOURCE_STAT: { |
198 | 280 | zip_stat_t *st; |
199 | | |
200 | 280 | st = (zip_stat_t *)data; |
201 | | |
202 | 280 | st->encryption_method = ZIP_EM_NONE; |
203 | 280 | st->valid |= ZIP_STAT_ENCRYPTION_METHOD; |
204 | 280 | if (st->valid & ZIP_STAT_COMP_SIZE) { |
205 | 280 | st->comp_size -= 12 + SALT_LENGTH(ctx->encryption_method); |
206 | 280 | } |
207 | | |
208 | 280 | return 0; |
209 | 1.49k | } |
210 | | |
211 | 544 | case ZIP_SOURCE_SUPPORTS: |
212 | 544 | return zip_source_make_command_bitmap(ZIP_SOURCE_OPEN, ZIP_SOURCE_READ, ZIP_SOURCE_CLOSE, ZIP_SOURCE_STAT, ZIP_SOURCE_ERROR, ZIP_SOURCE_FREE, ZIP_SOURCE_SUPPORTS_REOPEN, -1); |
213 | | |
214 | 462 | case ZIP_SOURCE_ERROR: |
215 | 462 | return zip_error_to_data(&ctx->error, data, len); |
216 | | |
217 | 544 | case ZIP_SOURCE_FREE: |
218 | 544 | winzip_aes_free(ctx); |
219 | 544 | return 0; |
220 | | |
221 | 0 | default: |
222 | 0 | return zip_source_pass_to_lower_layer(src, data, len, cmd); |
223 | 4.48k | } |
224 | 4.48k | } |
225 | | |
226 | | |
227 | 544 | static void winzip_aes_free(struct winzip_aes *ctx) { |
228 | 544 | if (ctx == NULL) { |
229 | 0 | return; |
230 | 0 | } |
231 | | |
232 | 544 | _zip_crypto_clear(ctx->password, strlen(ctx->password)); |
233 | 544 | free(ctx->password); |
234 | 544 | zip_error_fini(&ctx->error); |
235 | 544 | _zip_winzip_aes_free(ctx->aes_ctx); |
236 | 544 | free(ctx); |
237 | 544 | } |
238 | | |
239 | | |
240 | 544 | static struct winzip_aes *winzip_aes_new(zip_uint16_t encryption_method, const char *password, zip_error_t *error) { |
241 | 544 | struct winzip_aes *ctx; |
242 | | |
243 | 544 | if ((ctx = (struct winzip_aes *)malloc(sizeof(*ctx))) == NULL) { |
244 | 0 | zip_error_set(error, ZIP_ER_MEMORY, 0); |
245 | 0 | return NULL; |
246 | 0 | } |
247 | | |
248 | 544 | if ((ctx->password = strdup(password)) == NULL) { |
249 | 0 | zip_error_set(error, ZIP_ER_MEMORY, 0); |
250 | 0 | free(ctx); |
251 | 0 | return NULL; |
252 | 0 | } |
253 | | |
254 | 544 | ctx->encryption_method = encryption_method; |
255 | 544 | ctx->aes_ctx = NULL; |
256 | | |
257 | 544 | zip_error_init(&ctx->error); |
258 | | |
259 | 544 | return ctx; |
260 | 544 | } |