Coverage Report

Created: 2026-12-01 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libzip/lib/zip_source_pkware_encode.c
Line
Count
Source
1
/*
2
  zip_source_pkware_encode.c -- Traditional PKWARE encryption routines
3
  Copyright (C) 2009-2025 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
#include "zipint.h"
35
36
#include "zip_random.h"
37
38
#include <stdlib.h>
39
#include <string.h>
40
41
struct trad_pkware {
42
    char *password;
43
    zip_pkware_keys_t keys;
44
    zip_buffer_t *buffer;
45
    bool eof;
46
    zip_dostime_t dostime;
47
    zip_error_t error;
48
};
49
50
51
static int encrypt_header(zip_source_t *, struct trad_pkware *);
52
static zip_int64_t pkware_encrypt(zip_source_t *, void *, void *, zip_uint64_t, zip_source_cmd_t);
53
static void trad_pkware_free(struct trad_pkware *);
54
static struct trad_pkware *trad_pkware_new(const char *password, zip_error_t *error);
55
56
2.00k
zip_source_t *zip_source_pkware_encode(zip_t *za, zip_source_t *src, zip_uint16_t em, int flags, const char *password) {
57
2.00k
    struct trad_pkware *ctx;
58
2.00k
    zip_source_t *s2;
59
60
2.00k
    if (password == NULL || src == NULL || em != ZIP_EM_TRAD_PKWARE) {
61
0
        zip_error_set(&za->error, ZIP_ER_INVAL, 0);
62
0
        return NULL;
63
0
    }
64
2.00k
    if (!(flags & ZIP_CODEC_ENCODE)) {
65
0
        zip_error_set(&za->error, ZIP_ER_ENCRNOTSUPP, 0);
66
0
        return NULL;
67
0
    }
68
69
2.00k
    if ((ctx = trad_pkware_new(password, &za->error)) == NULL) {
70
0
        zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
71
0
        return NULL;
72
0
    }
73
74
2.00k
    if (zip_source_get_dos_time(src, &ctx->dostime) <= 0) {
75
2.00k
        zip_stat_t st;
76
77
2.00k
        if (zip_source_stat(src, &st) < 0) {
78
0
            zip_error_set_from_source(&za->error, src);
79
0
            trad_pkware_free(ctx);
80
0
            return NULL;
81
0
        }
82
2.00k
        if (_zip_u2d_time((st.valid & ZIP_STAT_MTIME) ? st.mtime : time(NULL), &ctx->dostime, &za->error) < 0) {
83
0
            trad_pkware_free(ctx);
84
0
            return NULL;
85
0
        }
86
2.00k
    }
87
88
2.00k
    if ((s2 = zip_source_layered(za, src, pkware_encrypt, ctx)) == NULL) {
89
0
        trad_pkware_free(ctx);
90
0
        return NULL;
91
0
    }
92
93
2.00k
    return s2;
94
2.00k
}
95
96
97
2.00k
static int encrypt_header(zip_source_t *src, struct trad_pkware *ctx) {
98
2.00k
    zip_uint8_t *header;
99
100
2.00k
    if ((ctx->buffer = _zip_buffer_new(NULL, ZIP_CRYPTO_PKWARE_HEADERLEN)) == NULL) {
101
0
        zip_error_set(&ctx->error, ZIP_ER_MEMORY, 0);
102
0
        return -1;
103
0
    }
104
105
2.00k
    header = _zip_buffer_data(ctx->buffer);
106
107
    /* generate header from random bytes and mtime
108
       see appnote.iz, XIII. Decryption, Step 2, last paragraph */
109
2.00k
    if (!zip_secure_random(header, ZIP_CRYPTO_PKWARE_HEADERLEN - 1)) {
110
0
        zip_error_set(&ctx->error, ZIP_ER_INTERNAL, 0);
111
0
        _zip_buffer_free(ctx->buffer);
112
0
        ctx->buffer = NULL;
113
0
        return -1;
114
0
    }
115
2.00k
    header[ZIP_CRYPTO_PKWARE_HEADERLEN - 1] = (zip_uint8_t)((ctx->dostime.time >> 8) & 0xff);
116
117
2.00k
    _zip_pkware_encrypt(&ctx->keys, header, header, ZIP_CRYPTO_PKWARE_HEADERLEN);
118
119
2.00k
    return 0;
120
2.00k
}
121
122
123
18.0k
static zip_int64_t pkware_encrypt(zip_source_t *src, void *ud, void *data, zip_uint64_t length, zip_source_cmd_t cmd) {
124
18.0k
    struct trad_pkware *ctx;
125
18.0k
    zip_int64_t n;
126
18.0k
    zip_uint64_t buffer_n;
127
128
18.0k
    ctx = (struct trad_pkware *)ud;
129
130
18.0k
    switch (cmd) {
131
0
    case ZIP_SOURCE_AT_EOF:
132
0
        return ctx->eof;
133
134
2.00k
    case ZIP_SOURCE_OPEN:
135
2.00k
        ctx->eof = false;
136
137
        /* initialize keys */
138
2.00k
        _zip_pkware_keys_reset(&ctx->keys);
139
2.00k
        _zip_pkware_encrypt(&ctx->keys, NULL, (const zip_uint8_t *)ctx->password, strlen(ctx->password));
140
141
2.00k
        if (encrypt_header(src, ctx) < 0) {
142
0
            return -1;
143
0
        }
144
2.00k
        return 0;
145
146
4.00k
    case ZIP_SOURCE_READ:
147
4.00k
        buffer_n = 0;
148
149
4.00k
        if (ctx->buffer) {
150
            /* write header values to data */
151
2.00k
            buffer_n = _zip_buffer_read(ctx->buffer, data, length);
152
2.00k
            data = (zip_uint8_t *)data + buffer_n;
153
2.00k
            length -= buffer_n;
154
155
2.00k
            if (_zip_buffer_eof(ctx->buffer)) {
156
2.00k
                _zip_buffer_free(ctx->buffer);
157
2.00k
                ctx->buffer = NULL;
158
2.00k
            }
159
2.00k
        }
160
161
4.00k
        if (ctx->eof) {
162
2.00k
            return (zip_int64_t)buffer_n;
163
2.00k
        }
164
165
2.00k
        if ((n = zip_source_read(src, data, length)) < 0) {
166
0
            zip_error_set_from_source(&ctx->error, src);
167
0
            return -1;
168
0
        }
169
170
2.00k
        _zip_pkware_encrypt(&ctx->keys, (zip_uint8_t *)data, (zip_uint8_t *)data, (zip_uint64_t)n);
171
172
2.00k
        if ((zip_uint64_t)n < length) {
173
2.00k
            ctx->eof = true;
174
2.00k
        }
175
176
2.00k
        return (zip_int64_t)buffer_n + n;
177
178
2.00k
    case ZIP_SOURCE_CLOSE:
179
2.00k
        _zip_buffer_free(ctx->buffer);
180
2.00k
        ctx->buffer = NULL;
181
2.00k
        return 0;
182
183
2.00k
    case ZIP_SOURCE_STAT: {
184
2.00k
        zip_stat_t *st;
185
186
2.00k
        st = (zip_stat_t *)data;
187
2.00k
        st->encryption_method = ZIP_EM_TRAD_PKWARE;
188
2.00k
        st->valid |= ZIP_STAT_ENCRYPTION_METHOD;
189
2.00k
        if (st->valid & ZIP_STAT_COMP_SIZE) {
190
2.00k
            if (!ASSIGN_UNSIGNED_ADD_FITS(st->comp_size, st->comp_size, ZIP_CRYPTO_PKWARE_HEADERLEN)) {
191
0
                st->valid &= ~ZIP_STAT_COMP_SIZE;
192
0
            }
193
2.00k
        }
194
195
2.00k
        return 0;
196
2.00k
    }
197
198
4.00k
    case ZIP_SOURCE_GET_FILE_ATTRIBUTES: {
199
4.00k
        zip_file_attributes_t *attributes = (zip_file_attributes_t *)data;
200
4.00k
        if (length < sizeof(*attributes)) {
201
0
            zip_error_set(&ctx->error, ZIP_ER_INVAL, 0);
202
0
            return -1;
203
0
        }
204
4.00k
        attributes->valid |= ZIP_FILE_ATTRIBUTES_VERSION_NEEDED | ZIP_FILE_ATTRIBUTES_GENERAL_PURPOSE_BIT_FLAGS;
205
4.00k
        attributes->version_needed = 20;
206
4.00k
        attributes->general_purpose_bit_flags = ZIP_GPBF_DATA_DESCRIPTOR;
207
4.00k
        attributes->general_purpose_bit_mask = ZIP_GPBF_DATA_DESCRIPTOR;
208
209
4.00k
        return 0;
210
4.00k
    }
211
212
0
    case ZIP_SOURCE_GET_DOS_TIME:
213
0
        if (length < sizeof(ctx->dostime)) {
214
0
            zip_error_set(&ctx->error, ZIP_ER_INVAL, 0);
215
0
            return -1;
216
0
        }
217
0
        (void)memcpy_s(data, sizeof(ctx->dostime), &ctx->dostime, sizeof(ctx->dostime));
218
0
        return sizeof(ctx->dostime);
219
220
2.00k
    case ZIP_SOURCE_SUPPORTS:
221
2.00k
        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_GET_FILE_ATTRIBUTES, ZIP_SOURCE_GET_DOS_TIME, -1);
222
223
0
    case ZIP_SOURCE_ERROR:
224
0
        return zip_error_to_data(&ctx->error, data, length);
225
226
2.00k
    case ZIP_SOURCE_FREE:
227
2.00k
        trad_pkware_free(ctx);
228
2.00k
        return 0;
229
230
0
    default:
231
0
        return zip_source_pass_to_lower_layer(src, data, length, cmd);
232
18.0k
    }
233
18.0k
}
234
235
236
2.00k
static struct trad_pkware *trad_pkware_new(const char *password, zip_error_t *error) {
237
2.00k
    struct trad_pkware *ctx;
238
239
2.00k
    if ((ctx = (struct trad_pkware *)malloc(sizeof(*ctx))) == NULL) {
240
0
        zip_error_set(error, ZIP_ER_MEMORY, 0);
241
0
        return NULL;
242
0
    }
243
244
2.00k
    if ((ctx->password = strdup(password)) == NULL) {
245
0
        zip_error_set(error, ZIP_ER_MEMORY, 0);
246
0
        free(ctx);
247
0
        return NULL;
248
0
    }
249
2.00k
    ctx->buffer = NULL;
250
2.00k
    zip_error_init(&ctx->error);
251
252
2.00k
    return ctx;
253
2.00k
}
254
255
256
2.00k
static void trad_pkware_free(struct trad_pkware *ctx) {
257
2.00k
    if (ctx == NULL) {
258
0
        return;
259
0
    }
260
261
2.00k
    _zip_crypto_clear(&ctx->keys, sizeof(ctx->keys));
262
2.00k
    _zip_crypto_clear(ctx->password, strlen(ctx->password));
263
2.00k
    free(ctx->password);
264
2.00k
    _zip_buffer_free(ctx->buffer);
265
2.00k
    zip_error_fini(&ctx->error);
266
2.00k
    free(ctx);
267
2.00k
}