Coverage Report

Created: 2026-02-14 06:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libzip/lib/zip_source_pkware_decode.c
Line
Count
Source
1
/*
2
  zip_source_pkware_decode.c -- Traditional PKWARE decryption 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
struct trad_pkware {
41
    char *password;
42
    zip_pkware_keys_t keys;
43
    zip_error_t error;
44
};
45
46
47
static int decrypt_header(zip_source_t *, struct trad_pkware *);
48
static zip_int64_t pkware_decrypt(zip_source_t *, void *, void *, zip_uint64_t, zip_source_cmd_t);
49
static struct trad_pkware *trad_pkware_new(const char *password, zip_error_t *error);
50
static void trad_pkware_free(struct trad_pkware *);
51
52
53
zip_source_t *
54
5.48k
zip_source_pkware_decode(zip_t *za, zip_source_t *src, zip_uint16_t em, int flags, const char *password) {
55
5.48k
    struct trad_pkware *ctx;
56
5.48k
    zip_source_t *s2;
57
58
5.48k
    if (password == NULL || src == NULL || em != ZIP_EM_TRAD_PKWARE) {
59
0
        zip_error_set(&za->error, ZIP_ER_INVAL, 0);
60
0
        return NULL;
61
0
    }
62
5.48k
    if (flags & ZIP_CODEC_ENCODE) {
63
0
        zip_error_set(&za->error, ZIP_ER_ENCRNOTSUPP, 0);
64
0
        return NULL;
65
0
    }
66
67
5.48k
    if ((ctx = trad_pkware_new(password, &za->error)) == NULL) {
68
0
        return NULL;
69
0
    }
70
71
5.48k
    if ((s2 = zip_source_layered(za, src, pkware_decrypt, ctx)) == NULL) {
72
0
        trad_pkware_free(ctx);
73
0
        return NULL;
74
0
    }
75
76
5.48k
    return s2;
77
5.48k
}
78
79
80
static int
81
4.27k
decrypt_header(zip_source_t *src, struct trad_pkware *ctx) {
82
4.27k
    zip_uint8_t header[ZIP_CRYPTO_PKWARE_HEADERLEN];
83
4.27k
    zip_stat_t st;
84
4.27k
    zip_dostime_t dostime;
85
4.27k
    zip_int64_t n;
86
87
4.27k
    if ((n = zip_source_read(src, header, ZIP_CRYPTO_PKWARE_HEADERLEN)) < 0) {
88
721
        zip_error_set_from_source(&ctx->error, src);
89
721
        return -1;
90
721
    }
91
92
3.55k
    if (n != ZIP_CRYPTO_PKWARE_HEADERLEN) {
93
500
        zip_error_set(&ctx->error, ZIP_ER_EOF, 0);
94
500
        return -1;
95
500
    }
96
97
3.05k
    _zip_pkware_decrypt(&ctx->keys, header, header, ZIP_CRYPTO_PKWARE_HEADERLEN);
98
99
3.05k
    if (zip_source_stat(src, &st) < 0 || (st.valid & ZIP_STAT_CRC) == 0) {
100
        /* skip password validation */
101
0
        return 0;
102
0
    }
103
104
3.05k
    if (zip_source_get_dos_time(src, &dostime) <= 0) {
105
0
        if ((st.valid & ZIP_STAT_MTIME) == 0) {
106
            /* no date available, skip password validation */
107
0
            return 0;
108
0
        }
109
110
0
        if (_zip_u2d_time(st.mtime, &dostime, &ctx->error) < 0) {
111
0
            return -1;
112
0
        }
113
0
    }
114
115
    /*
116
       password verification - two ways:
117
       - mtime - InfoZIP way, to avoid computing complete CRC before encrypting data
118
       - CRC - old PKWare way
119
    */
120
3.05k
    if (header[ZIP_CRYPTO_PKWARE_HEADERLEN - 1] == dostime.time >> 8
121
2.16k
        || header[ZIP_CRYPTO_PKWARE_HEADERLEN - 1] == st.crc >> 24) {
122
2.03k
        return 0;
123
2.03k
    }
124
1.02k
    else {
125
1.02k
        zip_error_set(&ctx->error, ZIP_ER_WRONGPASSWD, 0);
126
1.02k
        return -1;
127
1.02k
    }
128
3.05k
}
129
130
131
static zip_int64_t
132
27.3k
pkware_decrypt(zip_source_t *src, void *ud, void *data, zip_uint64_t len, zip_source_cmd_t cmd) {
133
27.3k
    struct trad_pkware *ctx;
134
27.3k
    zip_int64_t n;
135
136
27.3k
    ctx = (struct trad_pkware *)ud;
137
138
27.3k
    switch (cmd) {
139
4.27k
    case ZIP_SOURCE_OPEN:
140
4.27k
        _zip_pkware_keys_reset(&ctx->keys);
141
4.27k
        _zip_pkware_decrypt(&ctx->keys, NULL, (const zip_uint8_t *)ctx->password, strlen(ctx->password));
142
4.27k
        if (decrypt_header(src, ctx) < 0) {
143
2.24k
            return -1;
144
2.24k
        }
145
2.03k
        return 0;
146
147
4.47k
    case ZIP_SOURCE_READ:
148
4.47k
        if ((n = zip_source_read(src, data, len)) < 0) {
149
865
            zip_error_set_from_source(&ctx->error, src);
150
865
            return -1;
151
865
        }
152
153
3.60k
        _zip_pkware_decrypt(&ctx->keys, (zip_uint8_t *)data, (zip_uint8_t *)data, (zip_uint64_t)n);
154
3.60k
        return n;
155
156
2.03k
    case ZIP_SOURCE_CLOSE:
157
2.03k
        return 0;
158
159
2.46k
    case ZIP_SOURCE_STAT: {
160
2.46k
        zip_stat_t *st;
161
162
2.46k
        st = (zip_stat_t *)data;
163
164
2.46k
        st->encryption_method = ZIP_EM_NONE;
165
2.46k
        st->valid |= ZIP_STAT_ENCRYPTION_METHOD;
166
2.46k
        if (st->valid & ZIP_STAT_COMP_SIZE) {
167
2.46k
            st->comp_size -= ZIP_CRYPTO_PKWARE_HEADERLEN;
168
2.46k
        }
169
170
2.46k
        return 0;
171
4.47k
    }
172
173
5.48k
    case ZIP_SOURCE_SUPPORTS:
174
5.48k
        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_SUPPORTS_REOPEN, -1);
175
176
3.10k
    case ZIP_SOURCE_ERROR:
177
3.10k
        return zip_error_to_data(&ctx->error, data, len);
178
179
5.48k
    case ZIP_SOURCE_FREE:
180
5.48k
        trad_pkware_free(ctx);
181
5.48k
        return 0;
182
183
0
    default:
184
0
        return zip_source_pass_to_lower_layer(src, data, len, cmd);
185
27.3k
    }
186
27.3k
}
187
188
189
static struct trad_pkware *
190
5.48k
trad_pkware_new(const char *password, zip_error_t *error) {
191
5.48k
    struct trad_pkware *ctx;
192
193
5.48k
    if ((ctx = (struct trad_pkware *)malloc(sizeof(*ctx))) == NULL) {
194
0
        zip_error_set(error, ZIP_ER_MEMORY, 0);
195
0
        return NULL;
196
0
    }
197
198
5.48k
    if ((ctx->password = strdup(password)) == NULL) {
199
0
        zip_error_set(error, ZIP_ER_MEMORY, 0);
200
0
        free(ctx);
201
0
        return NULL;
202
0
    }
203
204
5.48k
    zip_error_init(&ctx->error);
205
206
5.48k
    return ctx;
207
5.48k
}
208
209
210
static void
211
5.48k
trad_pkware_free(struct trad_pkware *ctx) {
212
5.48k
    if (ctx == NULL) {
213
0
        return;
214
0
    }
215
216
5.48k
    free(ctx->password);
217
5.48k
    free(ctx);
218
5.48k
}