Coverage Report

Created: 2026-08-11 07:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
    bool hmac_verify_failed;
51
    zip_error_t error;
52
};
53
54
55
static int decrypt_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_decrypt(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_decode(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
    zip_stat_t st;
64
0
    zip_uint64_t aux_length;
65
0
    struct winzip_aes *ctx;
66
67
0
    if ((encryption_method != ZIP_EM_AES_128 && encryption_method != ZIP_EM_AES_192 && encryption_method != ZIP_EM_AES_256) || password == NULL || src == NULL) {
68
0
        zip_error_set(&za->error, ZIP_ER_INVAL, 0);
69
0
        return NULL;
70
0
    }
71
0
    if (flags & ZIP_CODEC_ENCODE) {
72
0
        zip_error_set(&za->error, ZIP_ER_ENCRNOTSUPP, 0);
73
0
        return NULL;
74
0
    }
75
76
0
    if (zip_source_stat(src, &st) != 0) {
77
0
        zip_error_set_from_source(&za->error, src);
78
0
        return NULL;
79
0
    }
80
81
0
    aux_length = WINZIP_AES_PASSWORD_VERIFY_LENGTH + SALT_LENGTH(encryption_method) + HMAC_LENGTH;
82
83
0
    if ((st.valid & ZIP_STAT_COMP_SIZE) == 0 || st.comp_size < aux_length) {
84
0
        zip_error_set(&za->error, ZIP_ER_OPNOTSUPP, 0);
85
0
        return NULL;
86
0
    }
87
88
0
    if ((ctx = winzip_aes_new(encryption_method, password, &za->error)) == NULL) {
89
0
        return NULL;
90
0
    }
91
92
0
    ctx->data_length = st.comp_size - aux_length;
93
94
0
    if ((s2 = zip_source_layered(za, src, winzip_aes_decrypt, ctx)) == NULL) {
95
0
        winzip_aes_free(ctx);
96
0
        return NULL;
97
0
    }
98
99
0
    return s2;
100
0
}
101
102
103
0
static int decrypt_header(zip_source_t *src, struct winzip_aes *ctx) {
104
0
    zip_uint8_t header[WINZIP_AES_MAX_HEADER_LENGTH];
105
0
    zip_uint8_t password_verification[WINZIP_AES_PASSWORD_VERIFY_LENGTH];
106
0
    unsigned int headerlen;
107
0
    zip_int64_t n;
108
109
0
    headerlen = WINZIP_AES_PASSWORD_VERIFY_LENGTH + SALT_LENGTH(ctx->encryption_method);
110
0
    if ((n = zip_source_read(src, header, headerlen)) < 0) {
111
0
        zip_error_set_from_source(&ctx->error, src);
112
0
        return -1;
113
0
    }
114
115
0
    if (n != headerlen) {
116
0
        zip_error_set(&ctx->error, ZIP_ER_EOF, 0);
117
0
        return -1;
118
0
    }
119
120
0
    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) {
121
0
        return -1;
122
0
    }
123
0
    if (memcmp(password_verification, header + SALT_LENGTH(ctx->encryption_method), WINZIP_AES_PASSWORD_VERIFY_LENGTH) != 0) {
124
0
        _zip_winzip_aes_free(ctx->aes_ctx);
125
0
        ctx->aes_ctx = NULL;
126
0
        zip_error_set(&ctx->error, ZIP_ER_WRONGPASSWD, 0);
127
0
        return -1;
128
0
    }
129
0
    return 0;
130
0
}
131
132
133
0
static void verify_hmac(zip_source_t *src, struct winzip_aes *ctx) {
134
0
    unsigned char computed[ZIP_CRYPTO_SHA1_LENGTH], from_file[HMAC_LENGTH];
135
0
    if (zip_source_read(src, from_file, HMAC_LENGTH) < HMAC_LENGTH) {
136
0
        zip_error_set_from_source(&ctx->error, src);
137
0
        ctx->hmac_verify_failed = true;
138
0
        return;
139
0
    }
140
141
0
    if (!_zip_winzip_aes_finish(ctx->aes_ctx, computed)) {
142
0
        zip_error_set(&ctx->error, ZIP_ER_INTERNAL, 0);
143
0
        ctx->hmac_verify_failed = true;
144
0
        return;
145
0
    }
146
0
    _zip_winzip_aes_free(ctx->aes_ctx);
147
0
    ctx->aes_ctx = NULL;
148
149
0
    if (memcmp(from_file, computed, HMAC_LENGTH) != 0) {
150
0
        zip_error_set(&ctx->error, ZIP_ER_CRC, 0);
151
0
        ctx->hmac_verify_failed = true;
152
0
        return;
153
0
    }
154
155
0
    return;
156
0
}
157
158
159
0
static zip_int64_t winzip_aes_decrypt(zip_source_t *src, void *ud, void *data, zip_uint64_t len, zip_source_cmd_t cmd) {
160
0
    struct winzip_aes *ctx;
161
0
    zip_int64_t n;
162
163
0
    ctx = (struct winzip_aes *)ud;
164
165
0
    switch (cmd) {
166
0
    case ZIP_SOURCE_AT_EOF:
167
0
        return ctx->current_position == ctx->data_length;
168
169
0
    case ZIP_SOURCE_OPEN:
170
0
        ctx->hmac_verify_failed = false;
171
0
        if (decrypt_header(src, ctx) < 0) {
172
0
            return -1;
173
0
        }
174
0
        ctx->current_position = 0;
175
0
        return 0;
176
177
0
    case ZIP_SOURCE_READ:
178
0
        len = ZIP_MIN(len, ctx->data_length - ctx->current_position);
179
180
0
        if (len > 0) {
181
0
            if ((n = zip_source_read(src, data, len)) < 0) {
182
0
                zip_error_set_from_source(&ctx->error, src);
183
0
                return -1;
184
0
            }
185
186
0
            if (n == 0) {
187
0
                zip_error_set(&ctx->error, ZIP_ER_EOF, 0);
188
0
                return -1;
189
0
            }
190
191
0
            ctx->current_position += (zip_uint64_t)n;
192
193
0
            if (!_zip_winzip_aes_decrypt(ctx->aes_ctx, (zip_uint8_t *)data, (zip_uint64_t)n)) {
194
0
                zip_error_set(&ctx->error, ZIP_ER_INTERNAL, 0);
195
0
                return -1;
196
0
            }
197
198
0
            if (ctx->current_position == ctx->data_length) {
199
0
                verify_hmac(src, ctx);
200
0
            }
201
202
0
            return n;
203
0
        }
204
0
        else {
205
0
            return ctx->hmac_verify_failed ? -1 : 0;
206
0
        }
207
208
0
    case ZIP_SOURCE_CLOSE:
209
0
        return ctx->hmac_verify_failed ? -1 : 0;
210
211
0
    case ZIP_SOURCE_STAT: {
212
0
        zip_stat_t *st;
213
214
0
        st = (zip_stat_t *)data;
215
216
0
        st->encryption_method = ZIP_EM_NONE;
217
0
        st->valid |= ZIP_STAT_ENCRYPTION_METHOD;
218
0
        if (st->valid & ZIP_STAT_COMP_SIZE) {
219
0
            if (st->comp_size < WINZIP_AES_PASSWORD_VERIFY_LENGTH + SALT_LENGTH(ctx->encryption_method) + HMAC_LENGTH) {
220
0
                zip_error_set(&ctx->error, ZIP_ER_DATA_LENGTH, 0);
221
0
                return -1;
222
0
            }
223
0
            st->comp_size -= WINZIP_AES_PASSWORD_VERIFY_LENGTH + SALT_LENGTH(ctx->encryption_method) + HMAC_LENGTH;
224
0
        }
225
226
0
        return 0;
227
0
    }
228
229
0
    case ZIP_SOURCE_SUPPORTS:
230
0
        return zip_source_make_command_bitmap(ZIP_SOURCE_AT_EOF, ZIP_SOURCE_OPEN, ZIP_SOURCE_READ, ZIP_SOURCE_CLOSE, ZIP_SOURCE_STAT, ZIP_SOURCE_ERROR, ZIP_SOURCE_FREE, ZIP_SOURCE_SUPPORTS_REOPEN, -1);
231
232
0
    case ZIP_SOURCE_ERROR:
233
0
        return zip_error_to_data(&ctx->error, data, len);
234
235
0
    case ZIP_SOURCE_FREE:
236
0
        winzip_aes_free(ctx);
237
0
        return 0;
238
239
0
    default:
240
0
        return zip_source_pass_to_lower_layer(src, data, len, cmd);
241
0
    }
242
0
}
243
244
245
0
static void winzip_aes_free(struct winzip_aes *ctx) {
246
0
    if (ctx == NULL) {
247
0
        return;
248
0
    }
249
250
0
    _zip_crypto_clear(ctx->password, strlen(ctx->password));
251
0
    free(ctx->password);
252
0
    zip_error_fini(&ctx->error);
253
0
    _zip_winzip_aes_free(ctx->aes_ctx);
254
0
    free(ctx);
255
0
}
256
257
258
0
static struct winzip_aes *winzip_aes_new(zip_uint16_t encryption_method, const char *password, zip_error_t *error) {
259
0
    struct winzip_aes *ctx;
260
261
0
    if ((ctx = (struct winzip_aes *)malloc(sizeof(*ctx))) == NULL) {
262
0
        zip_error_set(error, ZIP_ER_MEMORY, 0);
263
0
        return NULL;
264
0
    }
265
266
0
    if ((ctx->password = strdup(password)) == NULL) {
267
0
        zip_error_set(error, ZIP_ER_MEMORY, 0);
268
0
        free(ctx);
269
0
        return NULL;
270
0
    }
271
272
0
    ctx->encryption_method = encryption_method;
273
0
    ctx->aes_ctx = NULL;
274
0
    ctx->hmac_verify_failed = false;
275
276
0
    zip_error_init(&ctx->error);
277
278
0
    return ctx;
279
0
}