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