Rizin
unix-like reverse engineering framework and cli tools
zip_source_winzip_aes_decode.c File Reference
#include <stdlib.h>
#include <string.h>
#include "zipint.h"

Go to the source code of this file.

Classes

struct  winzip_aes
 

Functions

static int decrypt_header (zip_source_t *src, struct winzip_aes *ctx)
 
static void winzip_aes_free (struct winzip_aes *)
 
static zip_int64_t winzip_aes_decrypt (zip_source_t *src, void *ud, void *data, zip_uint64_t len, zip_source_cmd_t cmd)
 
static struct winzip_aeswinzip_aes_new (zip_uint16_t encryption_method, const char *password, zip_error_t *error)
 
zip_source_tzip_source_winzip_aes_decode (zip_t *za, zip_source_t *src, zip_uint16_t encryption_method, int flags, const char *password)
 
static bool verify_hmac (zip_source_t *src, struct winzip_aes *ctx)
 

Function Documentation

◆ decrypt_header()

static int decrypt_header ( zip_source_t src,
struct winzip_aes ctx 
)
static

Definition at line 102 of file zip_source_winzip_aes_decode.c.

102  {
104  zip_uint8_t password_verification[WINZIP_AES_PASSWORD_VERIFY_LENGTH];
105  unsigned int headerlen;
106  zip_int64_t n;
107 
108  headerlen = WINZIP_AES_PASSWORD_VERIFY_LENGTH + SALT_LENGTH(ctx->encryption_method);
109  if ((n = zip_source_read(src, header, headerlen)) < 0) {
111  return -1;
112  }
113 
114  if (n != headerlen) {
116  return -1;
117  }
118 
119  if ((ctx->aes_ctx = _zip_winzip_aes_new((zip_uint8_t *)ctx->password, strlen(ctx->password), header, ctx->encryption_method, password_verification, &ctx->error)) == NULL) {
120  return -1;
121  }
122  if (memcmp(password_verification, header + SALT_LENGTH(ctx->encryption_method), WINZIP_AES_PASSWORD_VERIFY_LENGTH) != 0) {
123  _zip_winzip_aes_free(ctx->aes_ctx);
124  ctx->aes_ctx = NULL;
126  return -1;
127  }
128  return 0;
129 }
lzma_index * src
Definition: index.h:567
#define NULL
Definition: cris-opc.c:27
ZIP_EXTERN void zip_error_set(zip_error_t *_Nullable, int, int)
Definition: zip_error.c:126
ZIP_EXTERN zip_int64_t zip_source_read(zip_source_t *_Nonnull, void *_Nonnull, zip_uint64_t)
#define ZIP_ER_WRONGPASSWD
Definition: zip.h:132
#define ZIP_ER_EOF
Definition: zip.h:122
#define header(is_bt, len_min, ret_op)
int n
Definition: mipsasm.c:19
zip_error_t * error
void _zip_error_set_from_source(zip_error_t *err, zip_source_t *src)
Definition: zip_error.c:135
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)
uint8_t zip_uint8_t
Definition: zipconf.h:33
int64_t zip_int64_t
Definition: zipconf.h:38
#define WINZIP_AES_MAX_HEADER_LENGTH
Definition: zipint.h:76
#define WINZIP_AES_PASSWORD_VERIFY_LENGTH
Definition: zipint.h:75
#define SALT_LENGTH(method)
Definition: zipint.h:80

References _zip_error_set_from_source(), _zip_winzip_aes_free(), _zip_winzip_aes_new(), ctx::error, header, n, NULL, SALT_LENGTH, src, WINZIP_AES_MAX_HEADER_LENGTH, WINZIP_AES_PASSWORD_VERIFY_LENGTH, ZIP_ER_EOF, ZIP_ER_WRONGPASSWD, zip_error_set(), and zip_source_read().

Referenced by winzip_aes_decrypt().

◆ verify_hmac()

static bool verify_hmac ( zip_source_t src,
struct winzip_aes ctx 
)
static

Definition at line 133 of file zip_source_winzip_aes_decode.c.

133  {
134  unsigned char computed[SHA1_LENGTH], from_file[HMAC_LENGTH];
135  if (zip_source_read(src, from_file, HMAC_LENGTH) < HMAC_LENGTH) {
137  return false;
138  }
139 
140  if (!_zip_winzip_aes_finish(ctx->aes_ctx, computed)) {
142  return false;
143  }
144  _zip_winzip_aes_free(ctx->aes_ctx);
145  ctx->aes_ctx = NULL;
146 
147  if (memcmp(from_file, computed, HMAC_LENGTH) != 0) {
149  return false;
150  }
151 
152  return true;
153 }
#define ZIP_ER_INTERNAL
Definition: zip.h:125
#define ZIP_ER_CRC
Definition: zip.h:112
bool _zip_winzip_aes_finish(zip_winzip_aes_t *ctx, zip_uint8_t *hmac)
#define HMAC_LENGTH
Definition: zipint.h:78
#define SHA1_LENGTH
Definition: zipint.h:79

References _zip_error_set_from_source(), _zip_winzip_aes_finish(), _zip_winzip_aes_free(), ctx::error, HMAC_LENGTH, NULL, SHA1_LENGTH, src, ZIP_ER_CRC, ZIP_ER_INTERNAL, zip_error_set(), and zip_source_read().

Referenced by winzip_aes_decrypt().

◆ winzip_aes_decrypt()

static zip_int64_t winzip_aes_decrypt ( zip_source_t src,
void *  ud,
void *  data,
zip_uint64_t  len,
zip_source_cmd_t  cmd 
)
static

Definition at line 157 of file zip_source_winzip_aes_decode.c.

157  {
158  struct winzip_aes *ctx;
159  zip_int64_t n;
160 
161  ctx = (struct winzip_aes *)ud;
162 
163  switch (cmd) {
164  case ZIP_SOURCE_OPEN:
165  if (decrypt_header(src, ctx) < 0) {
166  return -1;
167  }
168  ctx->current_position = 0;
169  return 0;
170 
171  case ZIP_SOURCE_READ:
172  if (len > ctx->data_length - ctx->current_position) {
173  len = ctx->data_length - ctx->current_position;
174  }
175 
176  if (len == 0) {
177  if (!verify_hmac(src, ctx)) {
178  return -1;
179  }
180  return 0;
181  }
182 
183  if ((n = zip_source_read(src, data, len)) < 0) {
185  return -1;
186  }
187  ctx->current_position += (zip_uint64_t)n;
188 
189  if (!_zip_winzip_aes_decrypt(ctx->aes_ctx, (zip_uint8_t *)data, (zip_uint64_t)n)) {
191  return -1;
192  }
193 
194  return n;
195 
196  case ZIP_SOURCE_CLOSE:
197  return 0;
198 
199  case ZIP_SOURCE_STAT: {
200  zip_stat_t *st;
201 
202  st = (zip_stat_t *)data;
203 
206  if (st->valid & ZIP_STAT_COMP_SIZE) {
207  st->comp_size -= 12 + SALT_LENGTH(ctx->encryption_method);
208  }
209 
210  return 0;
211  }
212 
213  case ZIP_SOURCE_SUPPORTS:
215 
216  case ZIP_SOURCE_ERROR:
217  return zip_error_to_data(&ctx->error, data, len);
218 
219  case ZIP_SOURCE_FREE:
221  return 0;
222 
223  default:
225  return -1;
226  }
227 }
size_t len
Definition: 6502dis.c:15
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
#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,...)
ZIP_EXTERN zip_int64_t zip_error_to_data(const zip_error_t *_Nonnull, void *_Nonnull, zip_uint64_t)
Definition: zip_error.c:141
zip_uint8_t data[ZIP_MAX(WINZIP_AES_MAX_HEADER_LENGTH, SHA1_LENGTH)]
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
static bool verify_hmac(zip_source_t *src, struct winzip_aes *ctx)
static int decrypt_header(zip_source_t *src, struct winzip_aes *ctx)
static void winzip_aes_free(struct winzip_aes *)
bool _zip_winzip_aes_decrypt(zip_winzip_aes_t *ctx, zip_uint8_t *data, zip_uint64_t length)
uint64_t zip_uint64_t
Definition: zipconf.h:39

References _zip_error_set_from_source(), _zip_winzip_aes_decrypt(), cmd, zip_stat::comp_size, winzip_aes::data, decrypt_header(), zip_stat::encryption_method, ctx::error, len, n, SALT_LENGTH, src, zip_stat::valid, verify_hmac(), winzip_aes_free(), ZIP_EM_NONE, ZIP_ER_INTERNAL, ZIP_ER_INVAL, zip_error_set(), zip_error_to_data(), ZIP_SOURCE_CLOSE, ZIP_SOURCE_ERROR, ZIP_SOURCE_FREE, zip_source_make_command_bitmap(), ZIP_SOURCE_OPEN, ZIP_SOURCE_READ, zip_source_read(), ZIP_SOURCE_STAT, ZIP_SOURCE_SUPPORTS, ZIP_STAT_COMP_SIZE, and ZIP_STAT_ENCRYPTION_METHOD.

Referenced by zip_source_winzip_aes_decode().

◆ winzip_aes_free()

static void winzip_aes_free ( struct winzip_aes ctx)
static

Definition at line 231 of file zip_source_winzip_aes_decode.c.

231  {
232  if (ctx == NULL) {
233  return;
234  }
235 
236  _zip_crypto_clear(ctx->password, strlen(ctx->password));
237  free(ctx->password);
239  _zip_winzip_aes_free(ctx->aes_ctx);
240  free(ctx);
241 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
ZIP_EXTERN void zip_error_fini(zip_error_t *_Nonnull)
Definition: zip_error.c:52
#define _zip_crypto_clear(b, l)
Definition: zipint.h:489

References _zip_crypto_clear, _zip_winzip_aes_free(), ctx::error, free(), NULL, and zip_error_fini().

Referenced by winzip_aes_decrypt(), and zip_source_winzip_aes_decode().

◆ winzip_aes_new()

static struct winzip_aes * winzip_aes_new ( zip_uint16_t  encryption_method,
const char *  password,
zip_error_t error 
)
static

Definition at line 245 of file zip_source_winzip_aes_decode.c.

245  {
246  struct winzip_aes *ctx;
247 
248  if ((ctx = (struct winzip_aes *)malloc(sizeof(*ctx))) == NULL) {
250  return NULL;
251  }
252 
253  if ((ctx->password = strdup(password)) == NULL) {
255  free(ctx);
256  return NULL;
257  }
258 
259  ctx->encryption_method = encryption_method;
260  ctx->aes_ctx = NULL;
261 
263 
264  return ctx;
265 }
#define ZIP_ER_MEMORY
Definition: zip.h:119
ZIP_EXTERN void zip_error_init(zip_error_t *_Nonnull)
Definition: zip_error.c:59
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")
zip_uint16_t encryption_method
void error(const char *msg)
Definition: untgz.c:593

References winzip_aes::encryption_method, ctx::error, error(), free(), malloc(), NULL, winzip_aes::password, strdup(), ZIP_ER_MEMORY, zip_error_init(), and zip_error_set().

Referenced by zip_source_winzip_aes_decode().

◆ zip_source_winzip_aes_decode()

zip_source_t* zip_source_winzip_aes_decode ( zip_t za,
zip_source_t src,
zip_uint16_t  encryption_method,
int  flags,
const char *  password 
)

Definition at line 59 of file zip_source_winzip_aes_decode.c.

59  {
61  zip_stat_t st;
62  zip_uint64_t aux_length;
63  struct winzip_aes *ctx;
64 
67  return NULL;
68  }
69  if (flags & ZIP_CODEC_ENCODE) {
71  return NULL;
72  }
73 
74  if (zip_source_stat(src, &st) != 0) {
76  return NULL;
77  }
78 
80 
81  if ((st.valid & ZIP_STAT_COMP_SIZE) == 0 || st.comp_size < aux_length) {
83  return NULL;
84  }
85 
87  return NULL;
88  }
89 
90  ctx->data_length = st.comp_size - aux_length;
91 
94  return NULL;
95  }
96 
97  return s2;
98 }
#define ZIP_EM_AES_256
Definition: zip.h:192
#define ZIP_ER_ENCRNOTSUPP
Definition: zip.h:129
ZIP_EXTERN int zip_source_stat(zip_source_t *_Nonnull, zip_stat_t *_Nonnull)
#define ZIP_EM_AES_192
Definition: zip.h:191
#define ZIP_ER_OPNOTSUPP
Definition: zip.h:133
#define ZIP_EM_AES_128
Definition: zip.h:190
static struct sockaddr static addrlen static backlog const void static flags void flags
Definition: sfsocketcall.h:123
zip_error_t error
Definition: zipint.h:281
zip_source_t * zip_source_layered(zip_t *za, zip_source_t *src, zip_source_layered_callback cb, void *ud)
static struct winzip_aes * winzip_aes_new(zip_uint16_t encryption_method, const char *password, zip_error_t *error)
static zip_int64_t winzip_aes_decrypt(zip_source_t *src, void *ud, void *data, zip_uint64_t len, zip_source_cmd_t cmd)
#define ZIP_CODEC_ENCODE
Definition: zipint.h:108
zip_t * za
Definition: ziptool.c:79

References _zip_error_set_from_source(), zip_stat::comp_size, winzip_aes::encryption_method, zip::error, flags, HMAC_LENGTH, NULL, winzip_aes::password, s2, SALT_LENGTH, src, zip_stat::valid, winzip_aes_decrypt(), winzip_aes_free(), winzip_aes_new(), WINZIP_AES_PASSWORD_VERIFY_LENGTH, za, ZIP_CODEC_ENCODE, ZIP_EM_AES_128, ZIP_EM_AES_192, ZIP_EM_AES_256, ZIP_ER_ENCRNOTSUPP, ZIP_ER_INVAL, ZIP_ER_OPNOTSUPP, zip_error_set(), zip_source_layered(), zip_source_stat(), and ZIP_STAT_COMP_SIZE.

Referenced by _zip_get_encryption_implementation().