Rizin
unix-like reverse engineering framework and cli tools
zip_source_winzip_aes_encode.c
Go to the documentation of this file.
1 /*
2  zip_source_winzip_aes_encode.c -- Winzip AES encryption routines
3  Copyright (C) 2009-2021 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 
41 struct winzip_aes {
42  char *password;
44 
47 
49  bool eof;
51 };
52 
53 
54 static int encrypt_header(zip_source_t *src, struct winzip_aes *ctx);
55 static void winzip_aes_free(struct winzip_aes *);
58 
59 
63  struct winzip_aes *ctx;
64 
67  return NULL;
68  }
69 
71  return NULL;
72  }
73 
76  return NULL;
77  }
78 
79  return s2;
80 }
81 
82 
83 static int
85  zip_uint16_t salt_length = SALT_LENGTH(ctx->encryption_method);
86  if (!zip_secure_random(ctx->data, salt_length)) {
88  return -1;
89  }
90 
91  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) {
92  return -1;
93  }
94 
95  if ((ctx->buffer = _zip_buffer_new(ctx->data, salt_length + WINZIP_AES_PASSWORD_VERIFY_LENGTH)) == NULL) {
96  _zip_winzip_aes_free(ctx->aes_ctx);
97  ctx->aes_ctx = NULL;
99  return -1;
100  }
101 
102  return 0;
103 }
104 
105 
106 static zip_int64_t
108  struct winzip_aes *ctx;
109  zip_int64_t ret;
110  zip_uint64_t buffer_n;
111 
112  ctx = (struct winzip_aes *)ud;
113 
114  switch (cmd) {
115  case ZIP_SOURCE_OPEN:
116  ctx->eof = false;
117  if (encrypt_header(src, ctx) < 0) {
118  return -1;
119  }
120  return 0;
121 
122  case ZIP_SOURCE_READ:
123  buffer_n = 0;
124 
125  if (ctx->buffer) {
126  buffer_n = _zip_buffer_read(ctx->buffer, data, length);
127 
128  data = (zip_uint8_t *)data + buffer_n;
129  length -= buffer_n;
130 
131  if (_zip_buffer_eof(ctx->buffer)) {
132  _zip_buffer_free(ctx->buffer);
133  ctx->buffer = NULL;
134  }
135  }
136 
137  if (ctx->eof) {
138  return (zip_int64_t)buffer_n;
139  }
140 
141  if ((ret = zip_source_read(src, data, length)) < 0) {
143  return -1;
144  }
145 
146  if (!_zip_winzip_aes_encrypt(ctx->aes_ctx, data, (zip_uint64_t)ret)) {
148  /* TODO: return partial read? */
149  return -1;
150  }
151 
152  if ((zip_uint64_t)ret < length) {
153  ctx->eof = true;
154  if (!_zip_winzip_aes_finish(ctx->aes_ctx, ctx->data)) {
156  /* TODO: return partial read? */
157  return -1;
158  }
159  _zip_winzip_aes_free(ctx->aes_ctx);
160  ctx->aes_ctx = NULL;
161  if ((ctx->buffer = _zip_buffer_new(ctx->data, HMAC_LENGTH)) == NULL) {
163  /* TODO: return partial read? */
164  return -1;
165  }
166  buffer_n += _zip_buffer_read(ctx->buffer, (zip_uint8_t *)data + ret, length - (zip_uint64_t)ret);
167  }
168 
169  return (zip_int64_t)(buffer_n + (zip_uint64_t)ret);
170 
171  case ZIP_SOURCE_CLOSE:
172  return 0;
173 
174  case ZIP_SOURCE_STAT: {
175  zip_stat_t *st;
176 
177  st = (zip_stat_t *)data;
178  st->encryption_method = ctx->encryption_method;
180  if (st->valid & ZIP_STAT_COMP_SIZE) {
181  st->comp_size += 12 + SALT_LENGTH(ctx->encryption_method);
182  }
183 
184  return 0;
185  }
186 
189  if (length < sizeof(*attributes)) {
191  return -1;
192  }
194  attributes->version_needed = 51;
195 
196  return 0;
197  }
198 
199  case ZIP_SOURCE_SUPPORTS:
201 
202  case ZIP_SOURCE_ERROR:
203  return zip_error_to_data(&ctx->error, data, length);
204 
205  case ZIP_SOURCE_FREE:
207  return 0;
208 
209  default:
211  return -1;
212  }
213 }
214 
215 
216 static void
218  if (ctx == NULL) {
219  return;
220  }
221 
222  _zip_crypto_clear(ctx->password, strlen(ctx->password));
223  free(ctx->password);
225  _zip_buffer_free(ctx->buffer);
226  _zip_winzip_aes_free(ctx->aes_ctx);
227  free(ctx);
228 }
229 
230 
231 static struct winzip_aes *
233  struct winzip_aes *ctx;
234 
235  if ((ctx = (struct winzip_aes *)malloc(sizeof(*ctx))) == NULL) {
237  return NULL;
238  }
239 
240  if ((ctx->password = strdup(password)) == NULL) {
241  free(ctx);
243  return NULL;
244  }
245 
246  ctx->encryption_method = encryption_method;
247  ctx->buffer = NULL;
248  ctx->aes_ctx = NULL;
249 
251 
252  ctx->eof = false;
253  return ctx;
254 }
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
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 static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void length
Definition: sflib.h:133
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
#define ZIP_ER_INTERNAL
Definition: zip.h:125
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_AES_256
Definition: zip.h:192
ZIP_EXTERN void zip_error_init(zip_error_t *_Nonnull)
Definition: zip_error.c:59
enum zip_source_cmd zip_source_cmd_t
Definition: zip.h:241
#define ZIP_EM_AES_192
Definition: zip.h:191
#define ZIP_STAT_ENCRYPTION_METHOD
Definition: zip.h:297
#define ZIP_STAT_COMP_SIZE
Definition: zip.h:293
@ ZIP_SOURCE_CLOSE
Definition: zip.h:222
@ ZIP_SOURCE_READ
Definition: zip.h:221
@ ZIP_SOURCE_GET_FILE_ATTRIBUTES
Definition: zip.h:239
@ 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
ZIP_EXTERN void zip_error_fini(zip_error_t *_Nonnull)
Definition: zip_error.c:52
#define ZIP_ER_INVAL
Definition: zip.h:123
ZIP_EXTERN zip_int64_t zip_source_make_command_bitmap(zip_source_cmd_t,...)
#define ZIP_FILE_ATTRIBUTES_VERSION_NEEDED
Definition: zip.h:331
ZIP_EXTERN zip_int64_t zip_error_to_data(const zip_error_t *_Nonnull, void *_Nonnull, zip_uint64_t)
Definition: zip_error.c:141
#define ZIP_EM_AES_128
Definition: zip.h:190
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")
static struct sockaddr static addrlen static backlog const void static flags void flags
Definition: sfsocketcall.h:123
zip_error_t * error
zip_winzip_aes_t * aes_ctx
zip_uint16_t encryption_method
zip_uint8_t data[ZIP_MAX(WINZIP_AES_MAX_HEADER_LENGTH, SHA1_LENGTH)]
Definition: zip.h:284
zip_uint8_t version_needed
Definition: zip.h:323
zip_uint64_t valid
Definition: zip.h:319
Definition: zip.h:300
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
Definition: zipint.h:278
zip_error_t error
Definition: zipint.h:281
void error(const char *msg)
Definition: untgz.c:593
zip_uint64_t _zip_buffer_read(zip_buffer_t *buffer, zip_uint8_t *data, zip_uint64_t length)
Definition: zip_buffer.c:134
zip_buffer_t * _zip_buffer_new(zip_uint8_t *data, zip_uint64_t size)
Definition: zip_buffer.c:146
void _zip_buffer_free(zip_buffer_t *buffer)
Definition: zip_buffer.c:46
bool _zip_buffer_eof(zip_buffer_t *buffer)
Definition: zip_buffer.c:60
ZIP_EXTERN bool zip_secure_random(zip_uint8_t *buffer, zip_uint16_t length)
void _zip_error_set_from_source(zip_error_t *err, zip_source_t *src)
Definition: zip_error.c:135
zip_source_t * zip_source_layered(zip_t *za, zip_source_t *src, zip_source_layered_callback cb, void *ud)
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)
static zip_int64_t winzip_aes_encrypt(zip_source_t *src, void *ud, void *data, zip_uint64_t len, zip_source_cmd_t cmd)
static int encrypt_header(zip_source_t *src, struct winzip_aes *ctx)
static void winzip_aes_free(struct winzip_aes *)
static struct winzip_aes * winzip_aes_new(zip_uint16_t encryption_method, const char *password, zip_error_t *error)
bool _zip_winzip_aes_encrypt(zip_winzip_aes_t *ctx, zip_uint8_t *data, zip_uint64_t length)
bool _zip_winzip_aes_finish(zip_winzip_aes_t *ctx, zip_uint8_t *hmac)
void _zip_winzip_aes_free(zip_winzip_aes_t *ctx)
zip_winzip_aes_t * _zip_winzip_aes_new(const zip_uint8_t *password, zip_uint64_t password_length, const zip_uint8_t *salt, zip_uint16_t encryption_method, zip_uint8_t *password_verify, zip_error_t *error)
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 WINZIP_AES_MAX_HEADER_LENGTH
Definition: zipint.h:76
#define HMAC_LENGTH
Definition: zipint.h:78
#define ZIP_MAX(a, b)
Definition: zipint.h:472
#define _zip_crypto_clear(b, l)
Definition: zipint.h:489
#define WINZIP_AES_PASSWORD_VERIFY_LENGTH
Definition: zipint.h:75
#define SHA1_LENGTH
Definition: zipint.h:79
#define SALT_LENGTH(method)
Definition: zipint.h:80
zip_t * za
Definition: ziptool.c:79