Coverage Report

Created: 2026-08-14 06:47

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