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

Go to the source code of this file.

Classes

struct  trad_pkware
 

Functions

static int encrypt_header (zip_source_t *, struct trad_pkware *)
 
static zip_int64_t pkware_encrypt (zip_source_t *, void *, void *, zip_uint64_t, zip_source_cmd_t)
 
static void trad_pkware_free (struct trad_pkware *)
 
static struct trad_pkwaretrad_pkware_new (const char *password, zip_error_t *error)
 
zip_source_tzip_source_pkware_encode (zip_t *za, zip_source_t *src, zip_uint16_t em, int flags, const char *password)
 

Function Documentation

◆ encrypt_header()

static int encrypt_header ( zip_source_t src,
struct trad_pkware ctx 
)
static

Definition at line 83 of file zip_source_pkware_encode.c.

83  {
84  struct zip_stat st;
85  unsigned short dostime, dosdate;
87 
88  if (zip_source_stat(src, &st) != 0) {
90  return -1;
91  }
92 
93  _zip_u2d_time(st.mtime, &dostime, &dosdate);
94 
97  return -1;
98  }
99 
100  header = _zip_buffer_data(ctx->buffer);
101 
102  /* generate header from random bytes and mtime
103  see appnote.iz, XIII. Decryption, Step 2, last paragraph */
106  _zip_buffer_free(ctx->buffer);
107  ctx->buffer = NULL;
108  return -1;
109  }
110  header[ZIP_CRYPTO_PKWARE_HEADERLEN - 1] = (zip_uint8_t)((dostime >> 8) & 0xff);
111 
113 
114  return 0;
115 }
lzma_index * src
Definition: index.h:567
#define NULL
Definition: cris-opc.c:27
#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 int zip_source_stat(zip_source_t *_Nonnull, zip_stat_t *_Nonnull)
#define header(is_bt, len_min, ret_op)
zip_error_t * error
Definition: zip.h:300
zip_uint8_t * _zip_buffer_data(zip_buffer_t *buffer)
Definition: zip_buffer.c:40
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
ZIP_EXTERN bool zip_secure_random(zip_uint8_t *buffer, zip_uint16_t length)
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_encrypt(zip_pkware_keys_t *keys, zip_uint8_t *out, const zip_uint8_t *in, zip_uint64_t len)
Definition: zip_pkware.c:72
uint8_t zip_uint8_t
Definition: zipconf.h:33
#define ZIP_CRYPTO_PKWARE_HEADERLEN
Definition: zipint.h:70

References _zip_buffer_data(), _zip_buffer_free(), _zip_buffer_new(), _zip_error_set_from_source(), _zip_pkware_encrypt(), _zip_u2d_time(), ctx::error, header, zip_stat::mtime, NULL, src, ZIP_CRYPTO_PKWARE_HEADERLEN, ZIP_ER_INTERNAL, ZIP_ER_MEMORY, zip_error_set(), zip_secure_random(), and zip_source_stat().

Referenced by pkware_encrypt().

◆ pkware_encrypt()

static zip_int64_t pkware_encrypt ( zip_source_t src,
void *  ud,
void *  data,
zip_uint64_t  length,
zip_source_cmd_t  cmd 
)
static

Definition at line 119 of file zip_source_pkware_encode.c.

119  {
120  struct trad_pkware *ctx;
121  zip_int64_t n;
122  zip_uint64_t buffer_n;
123 
124  ctx = (struct trad_pkware *)ud;
125 
126  switch (cmd) {
127  case ZIP_SOURCE_OPEN:
128  ctx->eof = false;
129 
130  /* initialize keys */
131  _zip_pkware_keys_reset(&ctx->keys);
132  _zip_pkware_encrypt(&ctx->keys, NULL, (const zip_uint8_t *)ctx->password, strlen(ctx->password));
133 
134  if (encrypt_header(src, ctx) < 0) {
135  return -1;
136  }
137  return 0;
138 
139  case ZIP_SOURCE_READ:
140  buffer_n = 0;
141 
142  if (ctx->buffer) {
143  /* write header values to data */
144  buffer_n = _zip_buffer_read(ctx->buffer, data, length);
145  data = (zip_uint8_t *)data + buffer_n;
146  length -= buffer_n;
147 
148  if (_zip_buffer_eof(ctx->buffer)) {
149  _zip_buffer_free(ctx->buffer);
150  ctx->buffer = NULL;
151  }
152  }
153 
154  if (ctx->eof) {
155  return (zip_int64_t)buffer_n;
156  }
157 
158  if ((n = zip_source_read(src, data, length)) < 0) {
160  return -1;
161  }
162 
163  _zip_pkware_encrypt(&ctx->keys, (zip_uint8_t *)data, (zip_uint8_t *)data, (zip_uint64_t)n);
164 
165  if ((zip_uint64_t)n < length) {
166  ctx->eof = true;
167  }
168 
169  return (zip_int64_t)buffer_n + n;
170 
171  case ZIP_SOURCE_CLOSE:
172  _zip_buffer_free(ctx->buffer);
173  ctx->buffer = NULL;
174  return 0;
175 
176  case ZIP_SOURCE_STAT: {
177  zip_stat_t *st;
178 
179  st = (zip_stat_t *)data;
182  if (st->valid & ZIP_STAT_COMP_SIZE) {
184  }
185 
186  return 0;
187  }
188 
190  zip_file_attributes_t *attributes = (zip_file_attributes_t *)data;
191  if (length < sizeof(*attributes)) {
193  return -1;
194  }
196  attributes->version_needed = 20;
197 
198  return 0;
199  }
200 
201  case ZIP_SOURCE_SUPPORTS:
203 
204  case ZIP_SOURCE_ERROR:
205  return zip_error_to_data(&ctx->error, data, length);
206 
207  case ZIP_SOURCE_FREE:
209  return 0;
210 
211  default:
213  return -1;
214  }
215 }
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
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_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
#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
int n
Definition: mipsasm.c:19
zip_uint8_t version_needed
Definition: zip.h:323
zip_uint64_t valid
Definition: zip.h:319
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_uint64_t _zip_buffer_read(zip_buffer_t *buffer, zip_uint8_t *data, zip_uint64_t length)
Definition: zip_buffer.c:134
bool _zip_buffer_eof(zip_buffer_t *buffer)
Definition: zip_buffer.c:60
void _zip_pkware_keys_reset(zip_pkware_keys_t *keys)
Definition: zip_pkware.c:64
static void trad_pkware_free(struct trad_pkware *)
static int encrypt_header(zip_source_t *, struct trad_pkware *)
uint64_t zip_uint64_t
Definition: zipconf.h:39
int64_t zip_int64_t
Definition: zipconf.h:38

References _zip_buffer_eof(), _zip_buffer_free(), _zip_buffer_read(), _zip_error_set_from_source(), _zip_pkware_encrypt(), _zip_pkware_keys_reset(), cmd, zip_stat::comp_size, encrypt_header(), zip_stat::encryption_method, ctx::error, length, n, NULL, src, trad_pkware_free(), zip_stat::valid, zip_file_attributes::valid, zip_file_attributes::version_needed, ZIP_CRYPTO_PKWARE_HEADERLEN, ZIP_EM_TRAD_PKWARE, ZIP_ER_INVAL, zip_error_set(), zip_error_to_data(), ZIP_FILE_ATTRIBUTES_VERSION_NEEDED, ZIP_SOURCE_CLOSE, ZIP_SOURCE_ERROR, ZIP_SOURCE_FREE, ZIP_SOURCE_GET_FILE_ATTRIBUTES, 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_pkware_encode().

◆ trad_pkware_free()

static void trad_pkware_free ( struct trad_pkware ctx)
static

Definition at line 240 of file zip_source_pkware_encode.c.

240  {
241  if (ctx == NULL) {
242  return;
243  }
244 
245  free(ctx->password);
246  _zip_buffer_free(ctx->buffer);
248  free(ctx);
249 }
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

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

Referenced by pkware_encrypt(), and zip_source_pkware_encode().

◆ trad_pkware_new()

static struct trad_pkware * trad_pkware_new ( const char *  password,
zip_error_t error 
)
static

Definition at line 219 of file zip_source_pkware_encode.c.

219  {
220  struct trad_pkware *ctx;
221 
222  if ((ctx = (struct trad_pkware *)malloc(sizeof(*ctx))) == NULL) {
224  return NULL;
225  }
226 
227  if ((ctx->password = strdup(password)) == NULL) {
229  free(ctx);
230  return NULL;
231  }
232  ctx->buffer = NULL;
234 
235  return ctx;
236 }
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")
void error(const char *msg)
Definition: untgz.c:593

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

Referenced by zip_source_pkware_encode().

◆ zip_source_pkware_encode()

zip_source_t* zip_source_pkware_encode ( zip_t za,
zip_source_t src,
zip_uint16_t  em,
int  flags,
const char *  password 
)

Definition at line 56 of file zip_source_pkware_encode.c.

56  {
57  struct trad_pkware *ctx;
59 
60  if (password == NULL || src == NULL || em != ZIP_EM_TRAD_PKWARE) {
62  return NULL;
63  }
64  if (!(flags & ZIP_CODEC_ENCODE)) {
66  return NULL;
67  }
68 
69  if ((ctx = trad_pkware_new(password, &za->error)) == NULL) {
70  return NULL;
71  }
72 
75  return NULL;
76  }
77 
78  return s2;
79 }
#define ZIP_ER_ENCRNOTSUPP
Definition: zip.h:129
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 zip_int64_t pkware_encrypt(zip_source_t *, void *, void *, zip_uint64_t, zip_source_cmd_t)
static struct trad_pkware * trad_pkware_new(const char *password, zip_error_t *error)
#define ZIP_CODEC_ENCODE
Definition: zipint.h:108
zip_t * za
Definition: ziptool.c:79

References zip::error, flags, NULL, trad_pkware::password, pkware_encrypt(), s2, src, trad_pkware_free(), trad_pkware_new(), za, ZIP_CODEC_ENCODE, ZIP_EM_TRAD_PKWARE, ZIP_ER_ENCRNOTSUPP, ZIP_ER_INVAL, zip_error_set(), and zip_source_layered().

Referenced by _zip_get_encryption_implementation().