/src/libzip/lib/zip_source_pkware_decode.c
Line | Count | Source |
1 | | /* |
2 | | zip_source_pkware_decode.c -- Traditional PKWARE 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 | | struct trad_pkware { |
41 | | char *password; |
42 | | zip_pkware_keys_t keys; |
43 | | zip_error_t error; |
44 | | }; |
45 | | |
46 | | |
47 | | static int decrypt_header(zip_source_t *, struct trad_pkware *); |
48 | | static zip_int64_t pkware_decrypt(zip_source_t *, void *, void *, zip_uint64_t, zip_source_cmd_t); |
49 | | static struct trad_pkware *trad_pkware_new(const char *password, zip_error_t *error); |
50 | | static void trad_pkware_free(struct trad_pkware *); |
51 | | |
52 | | |
53 | 0 | zip_source_t *zip_source_pkware_decode(zip_t *za, zip_source_t *src, zip_uint16_t em, int flags, const char *password) { |
54 | 0 | struct trad_pkware *ctx; |
55 | 0 | zip_source_t *s2; |
56 | 0 | zip_stat_t st; |
57 | |
|
58 | 0 | if (password == NULL || src == NULL || em != ZIP_EM_TRAD_PKWARE) { |
59 | 0 | zip_error_set(&za->error, ZIP_ER_INVAL, 0); |
60 | 0 | return NULL; |
61 | 0 | } |
62 | 0 | if (flags & ZIP_CODEC_ENCODE) { |
63 | 0 | zip_error_set(&za->error, ZIP_ER_ENCRNOTSUPP, 0); |
64 | 0 | return NULL; |
65 | 0 | } |
66 | | |
67 | 0 | if (zip_source_stat(src, &st) != 0) { |
68 | 0 | zip_error_set_from_source(&za->error, src); |
69 | 0 | return NULL; |
70 | 0 | } |
71 | | |
72 | 0 | if ((st.valid & ZIP_STAT_COMP_SIZE) == 0 || st.comp_size < ZIP_CRYPTO_PKWARE_HEADERLEN) { |
73 | 0 | zip_error_set(&za->error, ZIP_ER_OPNOTSUPP, 0); |
74 | 0 | return NULL; |
75 | 0 | } |
76 | | |
77 | 0 | if ((ctx = trad_pkware_new(password, &za->error)) == NULL) { |
78 | 0 | return NULL; |
79 | 0 | } |
80 | | |
81 | 0 | if ((s2 = zip_source_layered(za, src, pkware_decrypt, ctx)) == NULL) { |
82 | 0 | trad_pkware_free(ctx); |
83 | 0 | return NULL; |
84 | 0 | } |
85 | | |
86 | 0 | return s2; |
87 | 0 | } |
88 | | |
89 | | |
90 | 0 | static int decrypt_header(zip_source_t *src, struct trad_pkware *ctx) { |
91 | 0 | zip_uint8_t header[ZIP_CRYPTO_PKWARE_HEADERLEN]; |
92 | 0 | zip_stat_t st; |
93 | 0 | zip_dostime_t dostime; |
94 | 0 | zip_int64_t n; |
95 | |
|
96 | 0 | if ((n = zip_source_read(src, header, ZIP_CRYPTO_PKWARE_HEADERLEN)) < 0) { |
97 | 0 | zip_error_set_from_source(&ctx->error, src); |
98 | 0 | return -1; |
99 | 0 | } |
100 | | |
101 | 0 | if (n != ZIP_CRYPTO_PKWARE_HEADERLEN) { |
102 | 0 | zip_error_set(&ctx->error, ZIP_ER_EOF, 0); |
103 | 0 | return -1; |
104 | 0 | } |
105 | | |
106 | 0 | _zip_pkware_decrypt(&ctx->keys, header, header, ZIP_CRYPTO_PKWARE_HEADERLEN); |
107 | |
|
108 | 0 | if (zip_source_stat(src, &st) < 0 || (st.valid & ZIP_STAT_CRC) == 0) { |
109 | | /* skip password validation */ |
110 | 0 | return 0; |
111 | 0 | } |
112 | | |
113 | 0 | if (zip_source_get_dos_time(src, &dostime) <= 0) { |
114 | 0 | if ((st.valid & ZIP_STAT_MTIME) == 0) { |
115 | | /* no date available, skip password validation */ |
116 | 0 | return 0; |
117 | 0 | } |
118 | | |
119 | 0 | if (_zip_u2d_time(st.mtime, &dostime, &ctx->error) < 0) { |
120 | 0 | return -1; |
121 | 0 | } |
122 | 0 | } |
123 | | |
124 | | /* |
125 | | password verification - two ways: |
126 | | - mtime - InfoZIP way, to avoid computing complete CRC before encrypting data |
127 | | - CRC - old PKWare way |
128 | | */ |
129 | 0 | if (header[ZIP_CRYPTO_PKWARE_HEADERLEN - 1] == dostime.time >> 8 || header[ZIP_CRYPTO_PKWARE_HEADERLEN - 1] == st.crc >> 24) { |
130 | 0 | return 0; |
131 | 0 | } |
132 | 0 | else { |
133 | 0 | zip_error_set(&ctx->error, ZIP_ER_WRONGPASSWD, 0); |
134 | 0 | return -1; |
135 | 0 | } |
136 | 0 | } |
137 | | |
138 | | |
139 | 0 | static zip_int64_t pkware_decrypt(zip_source_t *src, void *ud, void *data, zip_uint64_t len, zip_source_cmd_t cmd) { |
140 | 0 | struct trad_pkware *ctx; |
141 | 0 | zip_int64_t n; |
142 | |
|
143 | 0 | ctx = (struct trad_pkware *)ud; |
144 | |
|
145 | 0 | switch (cmd) { |
146 | 0 | case ZIP_SOURCE_OPEN: |
147 | 0 | _zip_pkware_keys_reset(&ctx->keys); |
148 | 0 | _zip_pkware_decrypt(&ctx->keys, NULL, (const zip_uint8_t *)ctx->password, strlen(ctx->password)); |
149 | 0 | if (decrypt_header(src, ctx) < 0) { |
150 | 0 | return -1; |
151 | 0 | } |
152 | 0 | return 0; |
153 | | |
154 | 0 | case ZIP_SOURCE_READ: |
155 | 0 | if ((n = zip_source_read(src, data, len)) < 0) { |
156 | 0 | zip_error_set_from_source(&ctx->error, src); |
157 | 0 | return -1; |
158 | 0 | } |
159 | | |
160 | 0 | _zip_pkware_decrypt(&ctx->keys, (zip_uint8_t *)data, (zip_uint8_t *)data, (zip_uint64_t)n); |
161 | 0 | return n; |
162 | | |
163 | 0 | case ZIP_SOURCE_CLOSE: |
164 | 0 | return 0; |
165 | | |
166 | 0 | case ZIP_SOURCE_STAT: { |
167 | 0 | zip_stat_t *st; |
168 | |
|
169 | 0 | st = (zip_stat_t *)data; |
170 | |
|
171 | 0 | st->encryption_method = ZIP_EM_NONE; |
172 | 0 | st->valid |= ZIP_STAT_ENCRYPTION_METHOD; |
173 | 0 | if (st->valid & ZIP_STAT_COMP_SIZE) { |
174 | 0 | if (st->comp_size < ZIP_CRYPTO_PKWARE_HEADERLEN) { |
175 | 0 | zip_error_set(&ctx->error, ZIP_ER_DATA_LENGTH, 0); |
176 | 0 | return -1; |
177 | 0 | } |
178 | 0 | st->comp_size -= ZIP_CRYPTO_PKWARE_HEADERLEN; |
179 | 0 | } |
180 | | |
181 | 0 | return 0; |
182 | 0 | } |
183 | | |
184 | 0 | case ZIP_SOURCE_SUPPORTS: |
185 | 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_SUPPORTS_REOPEN, -1); |
186 | | |
187 | 0 | case ZIP_SOURCE_ERROR: |
188 | 0 | return zip_error_to_data(&ctx->error, data, len); |
189 | | |
190 | 0 | case ZIP_SOURCE_FREE: |
191 | 0 | trad_pkware_free(ctx); |
192 | 0 | return 0; |
193 | | |
194 | 0 | default: |
195 | 0 | return zip_source_pass_to_lower_layer(src, data, len, cmd); |
196 | 0 | } |
197 | 0 | } |
198 | | |
199 | | |
200 | 0 | static struct trad_pkware *trad_pkware_new(const char *password, zip_error_t *error) { |
201 | 0 | struct trad_pkware *ctx; |
202 | |
|
203 | 0 | if ((ctx = (struct trad_pkware *)malloc(sizeof(*ctx))) == NULL) { |
204 | 0 | zip_error_set(error, ZIP_ER_MEMORY, 0); |
205 | 0 | return NULL; |
206 | 0 | } |
207 | | |
208 | 0 | if ((ctx->password = strdup(password)) == NULL) { |
209 | 0 | zip_error_set(error, ZIP_ER_MEMORY, 0); |
210 | 0 | free(ctx); |
211 | 0 | return NULL; |
212 | 0 | } |
213 | | |
214 | 0 | zip_error_init(&ctx->error); |
215 | |
|
216 | 0 | return ctx; |
217 | 0 | } |
218 | | |
219 | | |
220 | 0 | static void trad_pkware_free(struct trad_pkware *ctx) { |
221 | 0 | if (ctx == NULL) { |
222 | 0 | return; |
223 | 0 | } |
224 | | |
225 | 0 | _zip_crypto_clear(ctx->password, strlen(ctx->password)); |
226 | 0 | free(ctx->password); |
227 | 0 | free(ctx); |
228 | 0 | } |