Coverage Report

Created: 2026-10-17 07:04

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.70k
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.70k
    zip_source_t *s2;
65
9.70k
    struct winzip_aes *ctx;
66
67
9.70k
    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.70k
    if ((ctx = winzip_aes_new(encryption_method, password, &za->error)) == NULL) {
73
0
        return NULL;
74
0
    }
75
76
9.70k
    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.70k
    return s2;
82
9.70k
}
83
84
85
9.70k
static int encrypt_header(zip_source_t *src, struct winzip_aes *ctx) {
86
9.70k
    zip_uint16_t salt_length = SALT_LENGTH(ctx->encryption_method);
87
9.70k
    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.70k
    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.70k
    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.70k
    return 0;
104
9.70k
}
105
106
107
87.3k
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
87.3k
    struct winzip_aes *ctx;
109
87.3k
    zip_int64_t ret;
110
87.3k
    zip_uint64_t buffer_n;
111
112
87.3k
    ctx = (struct winzip_aes *)ud;
113
114
87.3k
    switch (cmd) {
115
0
    case ZIP_SOURCE_AT_EOF:
116
0
        return ctx->eof && ctx->buffer == NULL;
117
118
9.70k
    case ZIP_SOURCE_OPEN:
119
9.70k
        ctx->eof = false;
120
9.70k
        if (encrypt_header(src, ctx) < 0) {
121
0
            return -1;
122
0
        }
123
9.70k
        return 0;
124
125
19.4k
    case ZIP_SOURCE_READ:
126
19.4k
        buffer_n = 0;
127
128
19.4k
        if (ctx->buffer) {
129
9.70k
            buffer_n = _zip_buffer_read(ctx->buffer, data, length);
130
131
9.70k
            data = (zip_uint8_t *)data + buffer_n;
132
9.70k
            length -= buffer_n;
133
134
9.70k
            if (_zip_buffer_eof(ctx->buffer)) {
135
9.70k
                _zip_buffer_free(ctx->buffer);
136
9.70k
                ctx->buffer = NULL;
137
9.70k
            }
138
9.70k
        }
139
140
19.4k
        if (ctx->eof) {
141
9.70k
            return (zip_int64_t)buffer_n;
142
9.70k
        }
143
144
9.70k
        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.70k
        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.70k
        if ((zip_uint64_t)ret < length) {
156
9.70k
            ctx->eof = true;
157
9.70k
            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.70k
            _zip_winzip_aes_free(ctx->aes_ctx);
163
9.70k
            ctx->aes_ctx = NULL;
164
9.70k
            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.70k
            buffer_n += _zip_buffer_read(ctx->buffer, (zip_uint8_t *)data + ret, length - (zip_uint64_t)ret);
170
9.70k
            if (_zip_buffer_eof(ctx->buffer)) {
171
9.70k
                _zip_buffer_free(ctx->buffer);
172
9.70k
                ctx->buffer = NULL;
173
9.70k
            }
174
9.70k
        }
175
176
9.70k
        return (zip_int64_t)(buffer_n + (zip_uint64_t)ret);
177
178
9.70k
    case ZIP_SOURCE_CLOSE:
179
9.70k
        return 0;
180
181
9.70k
    case ZIP_SOURCE_STAT: {
182
9.70k
        zip_stat_t *st;
183
184
9.70k
        st = (zip_stat_t *)data;
185
9.70k
        st->encryption_method = ctx->encryption_method;
186
9.70k
        st->valid |= ZIP_STAT_ENCRYPTION_METHOD;
187
9.70k
        if (st->valid & ZIP_STAT_COMP_SIZE) {
188
9.70k
            st->comp_size += 12 + SALT_LENGTH(ctx->encryption_method);
189
9.70k
        }
190
191
9.70k
        return 0;
192
9.70k
    }
193
194
19.4k
    case ZIP_SOURCE_GET_FILE_ATTRIBUTES: {
195
19.4k
        zip_file_attributes_t *attributes = (zip_file_attributes_t *)data;
196
19.4k
        if (length < sizeof(*attributes)) {
197
0
            zip_error_set(&ctx->error, ZIP_ER_INVAL, 0);
198
0
            return -1;
199
0
        }
200
19.4k
        attributes->valid |= ZIP_FILE_ATTRIBUTES_VERSION_NEEDED;
201
19.4k
        attributes->version_needed = 51;
202
203
19.4k
        return 0;
204
19.4k
    }
205
206
9.70k
    case ZIP_SOURCE_SUPPORTS:
207
9.70k
        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.70k
    case ZIP_SOURCE_FREE:
213
9.70k
        winzip_aes_free(ctx);
214
9.70k
        return 0;
215
216
0
    default:
217
0
        return zip_source_pass_to_lower_layer(src, data, length, cmd);
218
87.3k
    }
219
87.3k
}
220
221
222
9.70k
static void winzip_aes_free(struct winzip_aes *ctx) {
223
9.70k
    if (ctx == NULL) {
224
0
        return;
225
0
    }
226
227
9.70k
    _zip_crypto_clear(ctx->password, strlen(ctx->password));
228
9.70k
    free(ctx->password);
229
9.70k
    zip_error_fini(&ctx->error);
230
9.70k
    _zip_buffer_free(ctx->buffer);
231
9.70k
    _zip_winzip_aes_free(ctx->aes_ctx);
232
9.70k
    free(ctx);
233
9.70k
}
234
235
236
9.70k
static struct winzip_aes *winzip_aes_new(zip_uint16_t encryption_method, const char *password, zip_error_t *error) {
237
9.70k
    struct winzip_aes *ctx;
238
239
9.70k
    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.70k
    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.70k
    ctx->encryption_method = encryption_method;
251
9.70k
    ctx->buffer = NULL;
252
9.70k
    ctx->aes_ctx = NULL;
253
254
9.70k
    zip_error_init(&ctx->error);
255
256
    ctx->eof = false;
257
9.70k
    return ctx;
258
9.70k
}