Coverage Report

Created: 2026-07-30 07:17

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