Rizin
unix-like reverse engineering framework and cli tools
zip_source_pkware_decode.c
Go to the documentation of this file.
1 /*
2  zip_source_pkware_decode.c -- Traditional PKWARE decryption routines
3  Copyright (C) 2009-2020 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;
44 };
45 
46 
47 static int decrypt_header(zip_source_t *, struct trad_pkware *);
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 
55  struct trad_pkware *ctx;
57 
58  if (password == NULL || src == NULL || em != ZIP_EM_TRAD_PKWARE) {
60  return NULL;
61  }
62  if (flags & ZIP_CODEC_ENCODE) {
64  return NULL;
65  }
66 
67  if ((ctx = trad_pkware_new(password, &za->error)) == NULL) {
68  return NULL;
69  }
70 
73  return NULL;
74  }
75 
76  return s2;
77 }
78 
79 
80 static int
83  struct zip_stat st;
84  zip_int64_t n;
85  bool ok = false;
86 
89  return -1;
90  }
91 
94  return -1;
95  }
96 
98 
99  if (zip_source_stat(src, &st)) {
100  /* stat failed, skip password validation */
101  return 0;
102  }
103 
104  /* password verification - two ways:
105  * mtime - InfoZIP way, to avoid computing complete CRC before encrypting data
106  * CRC - old PKWare way
107  */
108 
109  if (st.valid & ZIP_STAT_MTIME) {
110  unsigned short dostime, dosdate;
111  _zip_u2d_time(st.mtime, &dostime, &dosdate);
112  if (header[ZIP_CRYPTO_PKWARE_HEADERLEN - 1] == dostime >> 8) {
113  ok = true;
114  }
115  }
116 
117  if (st.valid & ZIP_STAT_CRC) {
118  if (header[ZIP_CRYPTO_PKWARE_HEADERLEN - 1] == st.crc >> 24) {
119  ok = true;
120  }
121  }
122 
123  if (!ok && ((st.valid & (ZIP_STAT_MTIME | ZIP_STAT_CRC)) != 0)) {
125  return -1;
126  }
127 
128  return 0;
129 }
130 
131 
132 static zip_int64_t
134  struct trad_pkware *ctx;
135  zip_int64_t n;
136 
137  ctx = (struct trad_pkware *)ud;
138 
139  switch (cmd) {
140  case ZIP_SOURCE_OPEN:
141  _zip_pkware_keys_reset(&ctx->keys);
142  _zip_pkware_decrypt(&ctx->keys, NULL, (const zip_uint8_t *)ctx->password, strlen(ctx->password));
143  if (decrypt_header(src, ctx) < 0) {
144  return -1;
145  }
146  return 0;
147 
148  case ZIP_SOURCE_READ:
149  if ((n = zip_source_read(src, data, len)) < 0) {
151  return -1;
152  }
153 
154  _zip_pkware_decrypt(&ctx->keys, (zip_uint8_t *)data, (zip_uint8_t *)data, (zip_uint64_t)n);
155  return n;
156 
157  case ZIP_SOURCE_CLOSE:
158  return 0;
159 
160  case ZIP_SOURCE_STAT: {
161  zip_stat_t *st;
162 
163  st = (zip_stat_t *)data;
164 
167  if (st->valid & ZIP_STAT_COMP_SIZE) {
169  }
170 
171  return 0;
172  }
173 
174  case ZIP_SOURCE_SUPPORTS:
176 
177  case ZIP_SOURCE_ERROR:
178  return zip_error_to_data(&ctx->error, data, len);
179 
180  case ZIP_SOURCE_FREE:
182  return 0;
183 
184  default:
186  return -1;
187  }
188 }
189 
190 
191 static struct trad_pkware *
193  struct trad_pkware *ctx;
194 
195  if ((ctx = (struct trad_pkware *)malloc(sizeof(*ctx))) == NULL) {
197  return NULL;
198  }
199 
200  if ((ctx->password = strdup(password)) == NULL) {
202  free(ctx);
203  return NULL;
204  }
205 
207 
208  return ctx;
209 }
210 
211 
212 static void
214  if (ctx == NULL) {
215  return;
216  }
217 
218  free(ctx->password);
219  free(ctx);
220 }
size_t len
Definition: 6502dis.c:15
lzma_index * src
Definition: index.h:567
#define NULL
Definition: cris-opc.c:27
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags cmd
Definition: sflib.h:79
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
ZIP_EXTERN void zip_error_set(zip_error_t *_Nullable, int, int)
Definition: zip_error.c:126
#define ZIP_ER_MEMORY
Definition: zip.h:119
ZIP_EXTERN zip_int64_t zip_source_read(zip_source_t *_Nonnull, void *_Nonnull, zip_uint64_t)
#define ZIP_EM_TRAD_PKWARE
Definition: zip.h:178
#define ZIP_ER_ENCRNOTSUPP
Definition: zip.h:129
ZIP_EXTERN void zip_error_init(zip_error_t *_Nonnull)
Definition: zip_error.c:59
ZIP_EXTERN int zip_source_stat(zip_source_t *_Nonnull, zip_stat_t *_Nonnull)
enum zip_source_cmd zip_source_cmd_t
Definition: zip.h:241
#define ZIP_ER_WRONGPASSWD
Definition: zip.h:132
#define ZIP_STAT_ENCRYPTION_METHOD
Definition: zip.h:297
#define ZIP_STAT_COMP_SIZE
Definition: zip.h:293
#define ZIP_EM_NONE
Definition: zip.h:177
@ ZIP_SOURCE_CLOSE
Definition: zip.h:222
@ ZIP_SOURCE_READ
Definition: zip.h:221
@ ZIP_SOURCE_FREE
Definition: zip.h:225
@ ZIP_SOURCE_SUPPORTS
Definition: zip.h:234
@ ZIP_SOURCE_STAT
Definition: zip.h:223
@ ZIP_SOURCE_OPEN
Definition: zip.h:220
@ ZIP_SOURCE_ERROR
Definition: zip.h:224
#define ZIP_ER_INVAL
Definition: zip.h:123
ZIP_EXTERN zip_int64_t zip_source_make_command_bitmap(zip_source_cmd_t,...)
#define ZIP_STAT_MTIME
Definition: zip.h:294
#define ZIP_ER_EOF
Definition: zip.h:122
#define ZIP_STAT_CRC
Definition: zip.h:295
ZIP_EXTERN zip_int64_t zip_error_to_data(const zip_error_t *_Nonnull, void *_Nonnull, zip_uint64_t)
Definition: zip_error.c:141
void * malloc(size_t size)
Definition: malloc.c:123
return strdup("=SP r13\n" "=LR r14\n" "=PC r15\n" "=A0 r0\n" "=A1 r1\n" "=A2 r2\n" "=A3 r3\n" "=ZF zf\n" "=SF nf\n" "=OF vf\n" "=CF cf\n" "=SN or0\n" "gpr lr .32 56 0\n" "gpr pc .32 60 0\n" "gpr cpsr .32 64 0 ____tfiae_________________qvczn\n" "gpr or0 .32 68 0\n" "gpr tf .1 64.5 0 thumb\n" "gpr ef .1 64.9 0 endian\n" "gpr jf .1 64.24 0 java\n" "gpr qf .1 64.27 0 sticky_overflow\n" "gpr vf .1 64.28 0 overflow\n" "gpr cf .1 64.29 0 carry\n" "gpr zf .1 64.30 0 zero\n" "gpr nf .1 64.31 0 negative\n" "gpr itc .4 64.10 0 if_then_count\n" "gpr gef .4 64.16 0 great_or_equal\n" "gpr r0 .32 0 0\n" "gpr r1 .32 4 0\n" "gpr r2 .32 8 0\n" "gpr r3 .32 12 0\n" "gpr r4 .32 16 0\n" "gpr r5 .32 20 0\n" "gpr r6 .32 24 0\n" "gpr r7 .32 28 0\n" "gpr r8 .32 32 0\n" "gpr r9 .32 36 0\n" "gpr r10 .32 40 0\n" "gpr r11 .32 44 0\n" "gpr r12 .32 48 0\n" "gpr r13 .32 52 0\n" "gpr r14 .32 56 0\n" "gpr r15 .32 60 0\n" "gpr r16 .32 64 0\n" "gpr r17 .32 68 0\n")
@ ok
Definition: lz4.c:1706
#define header(is_bt, len_min, ret_op)
int n
Definition: mipsasm.c:19
static struct sockaddr static addrlen static backlog const void static flags void flags
Definition: sfsocketcall.h:123
zip_error_t * error
zip_pkware_keys_t keys
Definition: zip.h:284
Definition: zip.h:300
time_t mtime
Definition: zip.h:306
zip_uint16_t encryption_method
Definition: zip.h:309
zip_uint64_t valid
Definition: zip.h:301
zip_uint64_t comp_size
Definition: zip.h:305
zip_uint32_t crc
Definition: zip.h:307
Definition: zipint.h:278
zip_error_t error
Definition: zipint.h:281
void error(const char *msg)
Definition: untgz.c:593
void _zip_u2d_time(time_t intime, zip_uint16_t *dtime, zip_uint16_t *ddate)
Definition: zip_dirent.c:1090
void _zip_error_set_from_source(zip_error_t *err, zip_source_t *src)
Definition: zip_error.c:135
void _zip_pkware_decrypt(zip_pkware_keys_t *keys, zip_uint8_t *out, const zip_uint8_t *in, zip_uint64_t len)
Definition: zip_pkware.c:95
void _zip_pkware_keys_reset(zip_pkware_keys_t *keys)
Definition: zip_pkware.c:64
zip_source_t * zip_source_layered(zip_t *za, zip_source_t *src, zip_source_layered_callback cb, void *ud)
static zip_int64_t pkware_decrypt(zip_source_t *, void *, void *, zip_uint64_t, zip_source_cmd_t)
zip_source_t * zip_source_pkware_decode(zip_t *za, zip_source_t *src, zip_uint16_t em, int flags, const char *password)
static int decrypt_header(zip_source_t *, struct trad_pkware *)
static struct trad_pkware * trad_pkware_new(const char *password, zip_error_t *error)
static void trad_pkware_free(struct trad_pkware *)
uint64_t zip_uint64_t
Definition: zipconf.h:39
uint8_t zip_uint8_t
Definition: zipconf.h:33
uint16_t zip_uint16_t
Definition: zipconf.h:35
int64_t zip_int64_t
Definition: zipconf.h:38
#define ZIP_CODEC_ENCODE
Definition: zipint.h:108
#define ZIP_CRYPTO_PKWARE_HEADERLEN
Definition: zipint.h:70
zip_t * za
Definition: ziptool.c:79