/src/libzip/lib/zip_source_winzip_aes_encode.c
Line | Count | Source |
1 | | /* |
2 | | zip_source_winzip_aes_encode.c -- Winzip AES encryption 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_uint8_t data[ZIP_MAX(WINZIP_AES_MAX_HEADER_LENGTH, ZIP_CRYPTO_SHA1_LENGTH)]; |
47 | | zip_buffer_t *buffer; |
48 | | |
49 | | zip_winzip_aes_t *aes_ctx; |
50 | | bool eof; |
51 | | zip_error_t error; |
52 | | }; |
53 | | |
54 | | |
55 | | static int encrypt_header(zip_source_t *src, struct winzip_aes *ctx); |
56 | | static void winzip_aes_free(struct winzip_aes *); |
57 | | static zip_int64_t winzip_aes_encrypt(zip_source_t *src, void *ud, void *data, zip_uint64_t len, zip_source_cmd_t cmd); |
58 | | static struct winzip_aes *winzip_aes_new(zip_uint16_t encryption_method, const char *password, zip_error_t *error); |
59 | | |
60 | | |
61 | 0 | zip_source_t *zip_source_winzip_aes_encode(zip_t *za, zip_source_t *src, zip_uint16_t encryption_method, int flags, const char *password) { |
62 | 0 | zip_source_t *s2; |
63 | 0 | struct winzip_aes *ctx; |
64 | |
|
65 | 0 | if ((encryption_method != ZIP_EM_AES_128 && encryption_method != ZIP_EM_AES_192 && encryption_method != ZIP_EM_AES_256) || password == NULL || src == NULL) { |
66 | 0 | zip_error_set(&za->error, ZIP_ER_INVAL, 0); |
67 | 0 | return NULL; |
68 | 0 | } |
69 | | |
70 | 0 | if ((ctx = winzip_aes_new(encryption_method, password, &za->error)) == NULL) { |
71 | 0 | return NULL; |
72 | 0 | } |
73 | | |
74 | 0 | if ((s2 = zip_source_layered(za, src, winzip_aes_encrypt, ctx)) == NULL) { |
75 | 0 | winzip_aes_free(ctx); |
76 | 0 | return NULL; |
77 | 0 | } |
78 | | |
79 | 0 | return s2; |
80 | 0 | } |
81 | | |
82 | | |
83 | 0 | static int encrypt_header(zip_source_t *src, struct winzip_aes *ctx) { |
84 | 0 | zip_uint16_t salt_length = SALT_LENGTH(ctx->encryption_method); |
85 | 0 | if (!zip_secure_random(ctx->data, salt_length)) { |
86 | 0 | zip_error_set(&ctx->error, ZIP_ER_INTERNAL, 0); |
87 | 0 | return -1; |
88 | 0 | } |
89 | | |
90 | 0 | if ((ctx->aes_ctx = _zip_winzip_aes_new((zip_uint8_t *)ctx->password, strlen(ctx->password), ctx->data, ctx->encryption_method, ctx->data + salt_length, &ctx->error)) == NULL) { |
91 | 0 | return -1; |
92 | 0 | } |
93 | | |
94 | 0 | if ((ctx->buffer = _zip_buffer_new(ctx->data, salt_length + WINZIP_AES_PASSWORD_VERIFY_LENGTH)) == NULL) { |
95 | 0 | _zip_winzip_aes_free(ctx->aes_ctx); |
96 | 0 | ctx->aes_ctx = NULL; |
97 | 0 | zip_error_set(&ctx->error, ZIP_ER_MEMORY, 0); |
98 | 0 | return -1; |
99 | 0 | } |
100 | | |
101 | 0 | return 0; |
102 | 0 | } |
103 | | |
104 | | |
105 | 0 | static zip_int64_t winzip_aes_encrypt(zip_source_t *src, void *ud, void *data, zip_uint64_t length, zip_source_cmd_t cmd) { |
106 | 0 | struct winzip_aes *ctx; |
107 | 0 | zip_int64_t ret; |
108 | 0 | zip_uint64_t buffer_n; |
109 | |
|
110 | 0 | ctx = (struct winzip_aes *)ud; |
111 | |
|
112 | 0 | switch (cmd) { |
113 | 0 | case ZIP_SOURCE_OPEN: |
114 | 0 | ctx->eof = false; |
115 | 0 | if (encrypt_header(src, ctx) < 0) { |
116 | 0 | return -1; |
117 | 0 | } |
118 | 0 | return 0; |
119 | | |
120 | 0 | case ZIP_SOURCE_READ: |
121 | 0 | buffer_n = 0; |
122 | |
|
123 | 0 | if (ctx->buffer) { |
124 | 0 | buffer_n = _zip_buffer_read(ctx->buffer, data, length); |
125 | |
|
126 | 0 | data = (zip_uint8_t *)data + buffer_n; |
127 | 0 | length -= buffer_n; |
128 | |
|
129 | 0 | if (_zip_buffer_eof(ctx->buffer)) { |
130 | 0 | _zip_buffer_free(ctx->buffer); |
131 | 0 | ctx->buffer = NULL; |
132 | 0 | } |
133 | 0 | } |
134 | |
|
135 | 0 | if (ctx->eof) { |
136 | 0 | return (zip_int64_t)buffer_n; |
137 | 0 | } |
138 | | |
139 | 0 | if ((ret = zip_source_read(src, data, length)) < 0) { |
140 | 0 | zip_error_set_from_source(&ctx->error, src); |
141 | 0 | return -1; |
142 | 0 | } |
143 | | |
144 | 0 | if (!_zip_winzip_aes_encrypt(ctx->aes_ctx, data, (zip_uint64_t)ret)) { |
145 | 0 | zip_error_set(&ctx->error, ZIP_ER_INTERNAL, 0); |
146 | | /* TODO: return partial read? */ |
147 | 0 | return -1; |
148 | 0 | } |
149 | | |
150 | 0 | if ((zip_uint64_t)ret < length) { |
151 | 0 | ctx->eof = true; |
152 | 0 | if (!_zip_winzip_aes_finish(ctx->aes_ctx, ctx->data)) { |
153 | 0 | zip_error_set(&ctx->error, ZIP_ER_INTERNAL, 0); |
154 | | /* TODO: return partial read? */ |
155 | 0 | return -1; |
156 | 0 | } |
157 | 0 | _zip_winzip_aes_free(ctx->aes_ctx); |
158 | 0 | ctx->aes_ctx = NULL; |
159 | 0 | if ((ctx->buffer = _zip_buffer_new(ctx->data, HMAC_LENGTH)) == NULL) { |
160 | 0 | zip_error_set(&ctx->error, ZIP_ER_MEMORY, 0); |
161 | | /* TODO: return partial read? */ |
162 | 0 | return -1; |
163 | 0 | } |
164 | 0 | buffer_n += _zip_buffer_read(ctx->buffer, (zip_uint8_t *)data + ret, length - (zip_uint64_t)ret); |
165 | 0 | } |
166 | | |
167 | 0 | return (zip_int64_t)(buffer_n + (zip_uint64_t)ret); |
168 | | |
169 | 0 | case ZIP_SOURCE_CLOSE: |
170 | 0 | return 0; |
171 | | |
172 | 0 | case ZIP_SOURCE_STAT: { |
173 | 0 | zip_stat_t *st; |
174 | |
|
175 | 0 | st = (zip_stat_t *)data; |
176 | 0 | st->encryption_method = ctx->encryption_method; |
177 | 0 | st->valid |= ZIP_STAT_ENCRYPTION_METHOD; |
178 | 0 | if (st->valid & ZIP_STAT_COMP_SIZE) { |
179 | 0 | st->comp_size += 12 + SALT_LENGTH(ctx->encryption_method); |
180 | 0 | } |
181 | |
|
182 | 0 | return 0; |
183 | 0 | } |
184 | | |
185 | 0 | case ZIP_SOURCE_GET_FILE_ATTRIBUTES: { |
186 | 0 | zip_file_attributes_t *attributes = (zip_file_attributes_t *)data; |
187 | 0 | if (length < sizeof(*attributes)) { |
188 | 0 | zip_error_set(&ctx->error, ZIP_ER_INVAL, 0); |
189 | 0 | return -1; |
190 | 0 | } |
191 | 0 | attributes->valid |= ZIP_FILE_ATTRIBUTES_VERSION_NEEDED; |
192 | 0 | attributes->version_needed = 51; |
193 | |
|
194 | 0 | return 0; |
195 | 0 | } |
196 | | |
197 | 0 | case ZIP_SOURCE_SUPPORTS: |
198 | 0 | 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_GET_FILE_ATTRIBUTES, -1); |
199 | | |
200 | 0 | case ZIP_SOURCE_ERROR: |
201 | 0 | return zip_error_to_data(&ctx->error, data, length); |
202 | | |
203 | 0 | case ZIP_SOURCE_FREE: |
204 | 0 | winzip_aes_free(ctx); |
205 | 0 | return 0; |
206 | | |
207 | 0 | default: |
208 | 0 | return zip_source_pass_to_lower_layer(src, data, length, cmd); |
209 | 0 | } |
210 | 0 | } |
211 | | |
212 | | |
213 | 0 | static void winzip_aes_free(struct winzip_aes *ctx) { |
214 | 0 | if (ctx == NULL) { |
215 | 0 | return; |
216 | 0 | } |
217 | | |
218 | 0 | _zip_crypto_clear(ctx->password, strlen(ctx->password)); |
219 | 0 | free(ctx->password); |
220 | 0 | zip_error_fini(&ctx->error); |
221 | 0 | _zip_buffer_free(ctx->buffer); |
222 | 0 | _zip_winzip_aes_free(ctx->aes_ctx); |
223 | 0 | free(ctx); |
224 | 0 | } |
225 | | |
226 | | |
227 | 0 | static struct winzip_aes *winzip_aes_new(zip_uint16_t encryption_method, const char *password, zip_error_t *error) { |
228 | 0 | struct winzip_aes *ctx; |
229 | |
|
230 | 0 | if ((ctx = (struct winzip_aes *)malloc(sizeof(*ctx))) == NULL) { |
231 | 0 | zip_error_set(error, ZIP_ER_MEMORY, 0); |
232 | 0 | return NULL; |
233 | 0 | } |
234 | | |
235 | 0 | if ((ctx->password = strdup(password)) == NULL) { |
236 | 0 | free(ctx); |
237 | 0 | zip_error_set(error, ZIP_ER_MEMORY, 0); |
238 | 0 | return NULL; |
239 | 0 | } |
240 | | |
241 | 0 | ctx->encryption_method = encryption_method; |
242 | 0 | ctx->buffer = NULL; |
243 | 0 | ctx->aes_ctx = NULL; |
244 | |
|
245 | 0 | zip_error_init(&ctx->error); |
246 | |
|
247 | | ctx->eof = false; |
248 | 0 | return ctx; |
249 | 0 | } |