Coverage Report

Created: 2026-04-01 07:49

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