Coverage Report

Created: 2026-11-12 06:52

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