archive_write_set_format_zip.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695
  1. /*-
  2. * Copyright (c) 2008 Anselm Strauss
  3. * Copyright (c) 2009 Joerg Sonnenberger
  4. * Copyright (c) 2011-2012,2014 Michihiro NAKAJIMA
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. /*
  28. * Development supported by Google Summer of Code 2008.
  29. */
  30. #include "archive_platform.h"
  31. __FBSDID("$FreeBSD: head/lib/libarchive/archive_write_set_format_zip.c 201168 2009-12-29 06:15:32Z kientzle $");
  32. #ifdef HAVE_ERRNO_H
  33. #include <errno.h>
  34. #endif
  35. #ifdef HAVE_LANGINFO_H
  36. #include <langinfo.h>
  37. #endif
  38. #ifdef HAVE_STDLIB_H
  39. #include <stdlib.h>
  40. #endif
  41. #ifdef HAVE_STRING_H
  42. #include <string.h>
  43. #endif
  44. #ifdef HAVE_ZLIB_H
  45. #include <cm_zlib.h>
  46. #endif
  47. #include "archive.h"
  48. #include "archive_cryptor_private.h"
  49. #include "archive_endian.h"
  50. #include "archive_entry.h"
  51. #include "archive_entry_locale.h"
  52. #include "archive_hmac_private.h"
  53. #include "archive_private.h"
  54. #include "archive_random_private.h"
  55. #include "archive_write_private.h"
  56. #include "archive_write_set_format_private.h"
  57. #ifndef HAVE_ZLIB_H
  58. #include "archive_crc32.h"
  59. #endif
  60. #define ZIP_ENTRY_FLAG_ENCRYPTED (1<<0)
  61. #define ZIP_ENTRY_FLAG_LENGTH_AT_END (1<<3)
  62. #define ZIP_ENTRY_FLAG_UTF8_NAME (1 << 11)
  63. #define ZIP_4GB_MAX ARCHIVE_LITERAL_LL(0xffffffff)
  64. #define ZIP_4GB_MAX_UNCOMPRESSED ARCHIVE_LITERAL_LL(0xff000000)
  65. enum compression {
  66. COMPRESSION_UNSPECIFIED = -1,
  67. COMPRESSION_STORE = 0,
  68. COMPRESSION_DEFLATE = 8
  69. };
  70. #ifdef HAVE_ZLIB_H
  71. #define COMPRESSION_DEFAULT COMPRESSION_DEFLATE
  72. #else
  73. #define COMPRESSION_DEFAULT COMPRESSION_STORE
  74. #endif
  75. enum encryption {
  76. ENCRYPTION_NONE = 0,
  77. ENCRYPTION_TRADITIONAL, /* Traditional PKWARE encryption. */
  78. ENCRYPTION_WINZIP_AES128, /* WinZIP AES-128 encryption. */
  79. ENCRYPTION_WINZIP_AES256, /* WinZIP AES-256 encryption. */
  80. };
  81. #define TRAD_HEADER_SIZE 12
  82. /*
  83. * See "WinZip - AES Encryption Information"
  84. * http://www.winzip.com/aes_info.htm
  85. */
  86. /* Value used in compression method. */
  87. #define WINZIP_AES_ENCRYPTION 99
  88. /* A WinZip AES header size which is stored at the beginning of
  89. * file contents. */
  90. #define WINZIP_AES128_HEADER_SIZE (8 + 2)
  91. #define WINZIP_AES256_HEADER_SIZE (16 + 2)
  92. /* AES vendor version. */
  93. #define AES_VENDOR_AE_1 0x0001
  94. #define AES_VENDOR_AE_2 0x0002
  95. /* Authentication code size. */
  96. #define AUTH_CODE_SIZE 10
  97. /**/
  98. #define MAX_DERIVED_KEY_BUF_SIZE (AES_MAX_KEY_SIZE * 2 + 2)
  99. struct cd_segment {
  100. struct cd_segment *next;
  101. size_t buff_size;
  102. unsigned char *buff;
  103. unsigned char *p;
  104. };
  105. struct trad_enc_ctx {
  106. uint32_t keys[3];
  107. };
  108. struct zip {
  109. int64_t entry_offset;
  110. int64_t entry_compressed_size;
  111. int64_t entry_uncompressed_size;
  112. int64_t entry_compressed_written;
  113. int64_t entry_uncompressed_written;
  114. int64_t entry_uncompressed_limit;
  115. struct archive_entry *entry;
  116. uint32_t entry_crc32;
  117. enum compression entry_compression;
  118. enum encryption entry_encryption;
  119. int entry_flags;
  120. int entry_uses_zip64;
  121. int experiments;
  122. struct trad_enc_ctx tctx;
  123. char tctx_valid;
  124. unsigned char trad_chkdat;
  125. unsigned aes_vendor;
  126. archive_crypto_ctx cctx;
  127. char cctx_valid;
  128. archive_hmac_sha1_ctx hctx;
  129. char hctx_valid;
  130. unsigned char *file_header;
  131. size_t file_header_extra_offset;
  132. unsigned long (*crc32func)(unsigned long crc, const void *buff, size_t len);
  133. struct cd_segment *central_directory;
  134. struct cd_segment *central_directory_last;
  135. size_t central_directory_bytes;
  136. size_t central_directory_entries;
  137. int64_t written_bytes; /* Overall position in file. */
  138. struct archive_string_conv *opt_sconv;
  139. struct archive_string_conv *sconv_default;
  140. enum compression requested_compression;
  141. int deflate_compression_level;
  142. int init_default_conversion;
  143. enum encryption encryption_type;
  144. #define ZIP_FLAG_AVOID_ZIP64 1
  145. #define ZIP_FLAG_FORCE_ZIP64 2
  146. #define ZIP_FLAG_EXPERIMENT_xl 4
  147. int flags;
  148. #ifdef HAVE_ZLIB_H
  149. z_stream stream;
  150. #endif
  151. size_t len_buf;
  152. unsigned char *buf;
  153. };
  154. /* Don't call this min or MIN, since those are already defined
  155. on lots of platforms (but not all). */
  156. #define zipmin(a, b) ((a) > (b) ? (b) : (a))
  157. static ssize_t archive_write_zip_data(struct archive_write *,
  158. const void *buff, size_t s);
  159. static int archive_write_zip_close(struct archive_write *);
  160. static int archive_write_zip_free(struct archive_write *);
  161. static int archive_write_zip_finish_entry(struct archive_write *);
  162. static int archive_write_zip_header(struct archive_write *,
  163. struct archive_entry *);
  164. static int archive_write_zip_options(struct archive_write *,
  165. const char *, const char *);
  166. static unsigned int dos_time(const time_t);
  167. static size_t path_length(struct archive_entry *);
  168. static int write_path(struct archive_entry *, struct archive_write *);
  169. static void copy_path(struct archive_entry *, unsigned char *);
  170. static struct archive_string_conv *get_sconv(struct archive_write *, struct zip *);
  171. static int trad_enc_init(struct trad_enc_ctx *, const char *, size_t);
  172. static unsigned trad_enc_encrypt_update(struct trad_enc_ctx *, const uint8_t *,
  173. size_t, uint8_t *, size_t);
  174. static int init_traditional_pkware_encryption(struct archive_write *);
  175. static int is_traditional_pkware_encryption_supported(void);
  176. static int init_winzip_aes_encryption(struct archive_write *);
  177. static int is_winzip_aes_encryption_supported(int encryption);
  178. static unsigned char *
  179. cd_alloc(struct zip *zip, size_t length)
  180. {
  181. unsigned char *p;
  182. if (zip->central_directory == NULL
  183. || (zip->central_directory_last->p + length
  184. > zip->central_directory_last->buff + zip->central_directory_last->buff_size)) {
  185. struct cd_segment *segment = calloc(1, sizeof(*segment));
  186. if (segment == NULL)
  187. return NULL;
  188. segment->buff_size = 64 * 1024;
  189. segment->buff = malloc(segment->buff_size);
  190. if (segment->buff == NULL) {
  191. free(segment);
  192. return NULL;
  193. }
  194. segment->p = segment->buff;
  195. if (zip->central_directory == NULL) {
  196. zip->central_directory
  197. = zip->central_directory_last
  198. = segment;
  199. } else {
  200. zip->central_directory_last->next = segment;
  201. zip->central_directory_last = segment;
  202. }
  203. }
  204. p = zip->central_directory_last->p;
  205. zip->central_directory_last->p += length;
  206. zip->central_directory_bytes += length;
  207. return (p);
  208. }
  209. static unsigned long
  210. real_crc32(unsigned long crc, const void *buff, size_t len)
  211. {
  212. return crc32(crc, buff, (unsigned int)len);
  213. }
  214. static unsigned long
  215. fake_crc32(unsigned long crc, const void *buff, size_t len)
  216. {
  217. (void)crc; /* UNUSED */
  218. (void)buff; /* UNUSED */
  219. (void)len; /* UNUSED */
  220. return 0;
  221. }
  222. static int
  223. archive_write_zip_options(struct archive_write *a, const char *key,
  224. const char *val)
  225. {
  226. struct zip *zip = a->format_data;
  227. int ret = ARCHIVE_FAILED;
  228. if (strcmp(key, "compression") == 0) {
  229. /*
  230. * Set compression to use on all future entries.
  231. * This only affects regular files.
  232. */
  233. if (val == NULL || val[0] == 0) {
  234. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  235. "%s: compression option needs a compression name",
  236. a->format_name);
  237. } else if (strcmp(val, "deflate") == 0) {
  238. #ifdef HAVE_ZLIB_H
  239. zip->requested_compression = COMPRESSION_DEFLATE;
  240. ret = ARCHIVE_OK;
  241. #else
  242. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  243. "deflate compression not supported");
  244. #endif
  245. } else if (strcmp(val, "store") == 0) {
  246. zip->requested_compression = COMPRESSION_STORE;
  247. ret = ARCHIVE_OK;
  248. }
  249. return (ret);
  250. } else if (strcmp(key, "compression-level") == 0) {
  251. if (val == NULL || !(val[0] >= '0' && val[0] <= '9') || val[1] != '\0') {
  252. return ARCHIVE_WARN;
  253. }
  254. if (val[0] == '0') {
  255. zip->requested_compression = COMPRESSION_STORE;
  256. return ARCHIVE_OK;
  257. } else {
  258. #ifdef HAVE_ZLIB_H
  259. zip->requested_compression = COMPRESSION_DEFLATE;
  260. zip->deflate_compression_level = val[0] - '0';
  261. return ARCHIVE_OK;
  262. #else
  263. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  264. "deflate compression not supported");
  265. #endif
  266. }
  267. } else if (strcmp(key, "encryption") == 0) {
  268. if (val == NULL) {
  269. zip->encryption_type = ENCRYPTION_NONE;
  270. ret = ARCHIVE_OK;
  271. } else if (val[0] == '1' || strcmp(val, "traditional") == 0
  272. || strcmp(val, "zipcrypt") == 0
  273. || strcmp(val, "ZipCrypt") == 0) {
  274. if (is_traditional_pkware_encryption_supported()) {
  275. zip->encryption_type = ENCRYPTION_TRADITIONAL;
  276. ret = ARCHIVE_OK;
  277. } else {
  278. archive_set_error(&a->archive,
  279. ARCHIVE_ERRNO_MISC,
  280. "encryption not supported");
  281. }
  282. } else if (strcmp(val, "aes128") == 0) {
  283. if (is_winzip_aes_encryption_supported(
  284. ENCRYPTION_WINZIP_AES128)) {
  285. zip->encryption_type = ENCRYPTION_WINZIP_AES128;
  286. ret = ARCHIVE_OK;
  287. } else {
  288. archive_set_error(&a->archive,
  289. ARCHIVE_ERRNO_MISC,
  290. "encryption not supported");
  291. }
  292. } else if (strcmp(val, "aes256") == 0) {
  293. if (is_winzip_aes_encryption_supported(
  294. ENCRYPTION_WINZIP_AES256)) {
  295. zip->encryption_type = ENCRYPTION_WINZIP_AES256;
  296. ret = ARCHIVE_OK;
  297. } else {
  298. archive_set_error(&a->archive,
  299. ARCHIVE_ERRNO_MISC,
  300. "encryption not supported");
  301. }
  302. } else {
  303. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  304. "%s: unknown encryption '%s'",
  305. a->format_name, val);
  306. }
  307. return (ret);
  308. } else if (strcmp(key, "experimental") == 0) {
  309. if (val == NULL || val[0] == 0) {
  310. zip->flags &= ~ ZIP_FLAG_EXPERIMENT_xl;
  311. } else {
  312. zip->flags |= ZIP_FLAG_EXPERIMENT_xl;
  313. }
  314. return (ARCHIVE_OK);
  315. } else if (strcmp(key, "fakecrc32") == 0) {
  316. /*
  317. * FOR TESTING ONLY: disable CRC calculation to speed up
  318. * certain complex tests.
  319. */
  320. if (val == NULL || val[0] == 0) {
  321. zip->crc32func = real_crc32;
  322. } else {
  323. zip->crc32func = fake_crc32;
  324. }
  325. return (ARCHIVE_OK);
  326. } else if (strcmp(key, "hdrcharset") == 0) {
  327. /*
  328. * Set the character set used in translating filenames.
  329. */
  330. if (val == NULL || val[0] == 0) {
  331. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  332. "%s: hdrcharset option needs a character-set name",
  333. a->format_name);
  334. } else {
  335. zip->opt_sconv = archive_string_conversion_to_charset(
  336. &a->archive, val, 0);
  337. if (zip->opt_sconv != NULL)
  338. ret = ARCHIVE_OK;
  339. else
  340. ret = ARCHIVE_FATAL;
  341. }
  342. return (ret);
  343. } else if (strcmp(key, "zip64") == 0) {
  344. /*
  345. * Bias decisions about Zip64: force them to be
  346. * generated in certain cases where they are not
  347. * forbidden or avoid them in certain cases where they
  348. * are not strictly required.
  349. */
  350. if (val != NULL && *val != '\0') {
  351. zip->flags |= ZIP_FLAG_FORCE_ZIP64;
  352. zip->flags &= ~ZIP_FLAG_AVOID_ZIP64;
  353. } else {
  354. zip->flags &= ~ZIP_FLAG_FORCE_ZIP64;
  355. zip->flags |= ZIP_FLAG_AVOID_ZIP64;
  356. }
  357. return (ARCHIVE_OK);
  358. }
  359. /* Note: The "warn" return is just to inform the options
  360. * supervisor that we didn't handle it. It will generate
  361. * a suitable error if no one used this option. */
  362. return (ARCHIVE_WARN);
  363. }
  364. int
  365. archive_write_zip_set_compression_deflate(struct archive *_a)
  366. {
  367. struct archive_write *a = (struct archive_write *)_a;
  368. int ret = ARCHIVE_FAILED;
  369. archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
  370. ARCHIVE_STATE_NEW | ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
  371. "archive_write_zip_set_compression_deflate");
  372. if (a->archive.archive_format != ARCHIVE_FORMAT_ZIP) {
  373. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  374. "Can only use archive_write_zip_set_compression_deflate"
  375. " with zip format");
  376. ret = ARCHIVE_FATAL;
  377. } else {
  378. #ifdef HAVE_ZLIB_H
  379. struct zip *zip = a->format_data;
  380. zip->requested_compression = COMPRESSION_DEFLATE;
  381. ret = ARCHIVE_OK;
  382. #else
  383. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  384. "deflate compression not supported");
  385. ret = ARCHIVE_FAILED;
  386. #endif
  387. }
  388. return (ret);
  389. }
  390. int
  391. archive_write_zip_set_compression_store(struct archive *_a)
  392. {
  393. struct archive_write *a = (struct archive_write *)_a;
  394. struct zip *zip = a->format_data;
  395. int ret = ARCHIVE_FAILED;
  396. archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
  397. ARCHIVE_STATE_NEW | ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
  398. "archive_write_zip_set_compression_deflate");
  399. if (a->archive.archive_format != ARCHIVE_FORMAT_ZIP) {
  400. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  401. "Can only use archive_write_zip_set_compression_store"
  402. " with zip format");
  403. ret = ARCHIVE_FATAL;
  404. } else {
  405. zip->requested_compression = COMPRESSION_STORE;
  406. ret = ARCHIVE_OK;
  407. }
  408. return (ret);
  409. }
  410. int
  411. archive_write_set_format_zip(struct archive *_a)
  412. {
  413. struct archive_write *a = (struct archive_write *)_a;
  414. struct zip *zip;
  415. archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
  416. ARCHIVE_STATE_NEW, "archive_write_set_format_zip");
  417. /* If another format was already registered, unregister it. */
  418. if (a->format_free != NULL)
  419. (a->format_free)(a);
  420. zip = (struct zip *) calloc(1, sizeof(*zip));
  421. if (zip == NULL) {
  422. archive_set_error(&a->archive, ENOMEM,
  423. "Can't allocate zip data");
  424. return (ARCHIVE_FATAL);
  425. }
  426. /* "Unspecified" lets us choose the appropriate compression. */
  427. zip->requested_compression = COMPRESSION_UNSPECIFIED;
  428. #ifdef HAVE_ZLIB_H
  429. zip->deflate_compression_level = Z_DEFAULT_COMPRESSION;
  430. #endif
  431. zip->crc32func = real_crc32;
  432. /* A buffer used for both compression and encryption. */
  433. zip->len_buf = 65536;
  434. zip->buf = malloc(zip->len_buf);
  435. if (zip->buf == NULL) {
  436. free(zip);
  437. archive_set_error(&a->archive, ENOMEM,
  438. "Can't allocate compression buffer");
  439. return (ARCHIVE_FATAL);
  440. }
  441. a->format_data = zip;
  442. a->format_name = "zip";
  443. a->format_options = archive_write_zip_options;
  444. a->format_write_header = archive_write_zip_header;
  445. a->format_write_data = archive_write_zip_data;
  446. a->format_finish_entry = archive_write_zip_finish_entry;
  447. a->format_close = archive_write_zip_close;
  448. a->format_free = archive_write_zip_free;
  449. a->archive.archive_format = ARCHIVE_FORMAT_ZIP;
  450. a->archive.archive_format_name = "ZIP";
  451. return (ARCHIVE_OK);
  452. }
  453. static int
  454. is_all_ascii(const char *p)
  455. {
  456. const unsigned char *pp = (const unsigned char *)p;
  457. while (*pp) {
  458. if (*pp++ > 127)
  459. return (0);
  460. }
  461. return (1);
  462. }
  463. static int
  464. archive_write_zip_header(struct archive_write *a, struct archive_entry *entry)
  465. {
  466. unsigned char local_header[32];
  467. unsigned char local_extra[144];
  468. struct zip *zip = a->format_data;
  469. unsigned char *e;
  470. unsigned char *cd_extra;
  471. size_t filename_length;
  472. const char *slink = NULL;
  473. size_t slink_size = 0;
  474. struct archive_string_conv *sconv = get_sconv(a, zip);
  475. int ret, ret2 = ARCHIVE_OK;
  476. mode_t type;
  477. int version_needed = 10;
  478. /* Ignore types of entries that we don't support. */
  479. type = archive_entry_filetype(entry);
  480. if (type != AE_IFREG && type != AE_IFDIR && type != AE_IFLNK) {
  481. __archive_write_entry_filetype_unsupported(
  482. &a->archive, entry, "zip");
  483. return ARCHIVE_FAILED;
  484. };
  485. /* If we're not using Zip64, reject large files. */
  486. if (zip->flags & ZIP_FLAG_AVOID_ZIP64) {
  487. /* Reject entries over 4GB. */
  488. if (archive_entry_size_is_set(entry)
  489. && (archive_entry_size(entry) > ZIP_4GB_MAX)) {
  490. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  491. "Files > 4GB require Zip64 extensions");
  492. return ARCHIVE_FAILED;
  493. }
  494. /* Reject entries if archive is > 4GB. */
  495. if (zip->written_bytes > ZIP_4GB_MAX) {
  496. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  497. "Archives > 4GB require Zip64 extensions");
  498. return ARCHIVE_FAILED;
  499. }
  500. }
  501. /* Only regular files can have size > 0. */
  502. if (type != AE_IFREG)
  503. archive_entry_set_size(entry, 0);
  504. /* Reset information from last entry. */
  505. zip->entry_offset = zip->written_bytes;
  506. zip->entry_uncompressed_limit = INT64_MAX;
  507. zip->entry_compressed_size = 0;
  508. zip->entry_uncompressed_size = 0;
  509. zip->entry_compressed_written = 0;
  510. zip->entry_uncompressed_written = 0;
  511. zip->entry_flags = 0;
  512. zip->entry_uses_zip64 = 0;
  513. zip->entry_crc32 = zip->crc32func(0, NULL, 0);
  514. zip->entry_encryption = 0;
  515. archive_entry_free(zip->entry);
  516. zip->entry = NULL;
  517. if (zip->cctx_valid)
  518. archive_encrypto_aes_ctr_release(&zip->cctx);
  519. if (zip->hctx_valid)
  520. archive_hmac_sha1_cleanup(&zip->hctx);
  521. zip->tctx_valid = zip->cctx_valid = zip->hctx_valid = 0;
  522. if (type == AE_IFREG
  523. &&(!archive_entry_size_is_set(entry)
  524. || archive_entry_size(entry) > 0)) {
  525. switch (zip->encryption_type) {
  526. case ENCRYPTION_TRADITIONAL:
  527. case ENCRYPTION_WINZIP_AES128:
  528. case ENCRYPTION_WINZIP_AES256:
  529. zip->entry_flags |= ZIP_ENTRY_FLAG_ENCRYPTED;
  530. zip->entry_encryption = zip->encryption_type;
  531. break;
  532. default:
  533. break;
  534. }
  535. }
  536. #if defined(_WIN32) && !defined(__CYGWIN__)
  537. /* Make sure the path separators in pathname, hardlink and symlink
  538. * are all slash '/', not the Windows path separator '\'. */
  539. zip->entry = __la_win_entry_in_posix_pathseparator(entry);
  540. if (zip->entry == entry)
  541. zip->entry = archive_entry_clone(entry);
  542. #else
  543. zip->entry = archive_entry_clone(entry);
  544. #endif
  545. if (zip->entry == NULL) {
  546. archive_set_error(&a->archive, ENOMEM,
  547. "Can't allocate zip header data");
  548. return (ARCHIVE_FATAL);
  549. }
  550. if (sconv != NULL) {
  551. const char *p;
  552. size_t len;
  553. if (archive_entry_pathname_l(entry, &p, &len, sconv) != 0) {
  554. if (errno == ENOMEM) {
  555. archive_set_error(&a->archive, ENOMEM,
  556. "Can't allocate memory for Pathname");
  557. return (ARCHIVE_FATAL);
  558. }
  559. archive_set_error(&a->archive,
  560. ARCHIVE_ERRNO_FILE_FORMAT,
  561. "Can't translate Pathname '%s' to %s",
  562. archive_entry_pathname(entry),
  563. archive_string_conversion_charset_name(sconv));
  564. ret2 = ARCHIVE_WARN;
  565. }
  566. if (len > 0)
  567. archive_entry_set_pathname(zip->entry, p);
  568. /*
  569. * There is no standard for symlink handling; we convert
  570. * it using the same character-set translation that we use
  571. * for filename.
  572. */
  573. if (type == AE_IFLNK) {
  574. if (archive_entry_symlink_l(entry, &p, &len, sconv)) {
  575. if (errno == ENOMEM) {
  576. archive_set_error(&a->archive, ENOMEM,
  577. "Can't allocate memory "
  578. " for Symlink");
  579. return (ARCHIVE_FATAL);
  580. }
  581. /* No error if we can't convert. */
  582. } else if (len > 0)
  583. archive_entry_set_symlink(zip->entry, p);
  584. }
  585. }
  586. /* If filename isn't ASCII and we can use UTF-8, set the UTF-8 flag. */
  587. if (!is_all_ascii(archive_entry_pathname(zip->entry))) {
  588. if (zip->opt_sconv != NULL) {
  589. if (strcmp(archive_string_conversion_charset_name(
  590. zip->opt_sconv), "UTF-8") == 0)
  591. zip->entry_flags |= ZIP_ENTRY_FLAG_UTF8_NAME;
  592. #if HAVE_NL_LANGINFO
  593. } else if (strcmp(nl_langinfo(CODESET), "UTF-8") == 0) {
  594. zip->entry_flags |= ZIP_ENTRY_FLAG_UTF8_NAME;
  595. #endif
  596. }
  597. }
  598. filename_length = path_length(zip->entry);
  599. /* Determine appropriate compression and size for this entry. */
  600. if (type == AE_IFLNK) {
  601. slink = archive_entry_symlink(zip->entry);
  602. if (slink != NULL)
  603. slink_size = strlen(slink);
  604. else
  605. slink_size = 0;
  606. zip->entry_uncompressed_limit = slink_size;
  607. zip->entry_compressed_size = slink_size;
  608. zip->entry_uncompressed_size = slink_size;
  609. zip->entry_crc32 = zip->crc32func(zip->entry_crc32,
  610. (const unsigned char *)slink, slink_size);
  611. zip->entry_compression = COMPRESSION_STORE;
  612. version_needed = 20;
  613. } else if (type != AE_IFREG) {
  614. zip->entry_compression = COMPRESSION_STORE;
  615. zip->entry_uncompressed_limit = 0;
  616. version_needed = 20;
  617. } else if (archive_entry_size_is_set(zip->entry)) {
  618. int64_t size = archive_entry_size(zip->entry);
  619. int64_t additional_size = 0;
  620. zip->entry_uncompressed_limit = size;
  621. zip->entry_compression = zip->requested_compression;
  622. if (zip->entry_compression == COMPRESSION_UNSPECIFIED) {
  623. zip->entry_compression = COMPRESSION_DEFAULT;
  624. }
  625. if (zip->entry_compression == COMPRESSION_STORE) {
  626. zip->entry_compressed_size = size;
  627. zip->entry_uncompressed_size = size;
  628. version_needed = 10;
  629. } else {
  630. zip->entry_uncompressed_size = size;
  631. version_needed = 20;
  632. }
  633. if (zip->entry_flags & ZIP_ENTRY_FLAG_ENCRYPTED) {
  634. switch (zip->entry_encryption) {
  635. case ENCRYPTION_TRADITIONAL:
  636. additional_size = TRAD_HEADER_SIZE;
  637. version_needed = 20;
  638. break;
  639. case ENCRYPTION_WINZIP_AES128:
  640. additional_size = WINZIP_AES128_HEADER_SIZE
  641. + AUTH_CODE_SIZE;
  642. version_needed = 20;
  643. break;
  644. case ENCRYPTION_WINZIP_AES256:
  645. additional_size = WINZIP_AES256_HEADER_SIZE
  646. + AUTH_CODE_SIZE;
  647. version_needed = 20;
  648. break;
  649. default:
  650. break;
  651. }
  652. if (zip->entry_compression == COMPRESSION_STORE)
  653. zip->entry_compressed_size += additional_size;
  654. }
  655. /*
  656. * Set Zip64 extension in any of the following cases
  657. * (this was suggested by discussion on info-zip-dev
  658. * mailing list):
  659. * = Zip64 is being forced by user
  660. * = File is over 4GiB uncompressed
  661. * (including encryption header, if any)
  662. * = File is close to 4GiB and is being compressed
  663. * (compression might make file larger)
  664. */
  665. if ((zip->flags & ZIP_FLAG_FORCE_ZIP64)
  666. || (zip->entry_uncompressed_size + additional_size > ZIP_4GB_MAX)
  667. || (zip->entry_uncompressed_size > ZIP_4GB_MAX_UNCOMPRESSED
  668. && zip->entry_compression != COMPRESSION_STORE)) {
  669. zip->entry_uses_zip64 = 1;
  670. version_needed = 45;
  671. }
  672. /* We may know the size, but never the CRC. */
  673. zip->entry_flags |= ZIP_ENTRY_FLAG_LENGTH_AT_END;
  674. } else {
  675. /* We don't know the size. In this case, we prefer
  676. * deflate (it has a clear end-of-data marker which
  677. * makes length-at-end more reliable) and will
  678. * enable Zip64 extensions unless we're told not to.
  679. */
  680. zip->entry_compression = COMPRESSION_DEFAULT;
  681. zip->entry_flags |= ZIP_ENTRY_FLAG_LENGTH_AT_END;
  682. if ((zip->flags & ZIP_FLAG_AVOID_ZIP64) == 0) {
  683. zip->entry_uses_zip64 = 1;
  684. version_needed = 45;
  685. } else if (zip->entry_compression == COMPRESSION_STORE) {
  686. version_needed = 10;
  687. } else {
  688. version_needed = 20;
  689. }
  690. if (zip->entry_flags & ZIP_ENTRY_FLAG_ENCRYPTED) {
  691. switch (zip->entry_encryption) {
  692. case ENCRYPTION_TRADITIONAL:
  693. case ENCRYPTION_WINZIP_AES128:
  694. case ENCRYPTION_WINZIP_AES256:
  695. if (version_needed < 20)
  696. version_needed = 20;
  697. break;
  698. default:
  699. break;
  700. }
  701. }
  702. }
  703. /* Format the local header. */
  704. memset(local_header, 0, sizeof(local_header));
  705. memcpy(local_header, "PK\003\004", 4);
  706. archive_le16enc(local_header + 4, version_needed);
  707. archive_le16enc(local_header + 6, zip->entry_flags);
  708. if (zip->entry_encryption == ENCRYPTION_WINZIP_AES128
  709. || zip->entry_encryption == ENCRYPTION_WINZIP_AES256)
  710. archive_le16enc(local_header + 8, WINZIP_AES_ENCRYPTION);
  711. else
  712. archive_le16enc(local_header + 8, zip->entry_compression);
  713. archive_le32enc(local_header + 10,
  714. dos_time(archive_entry_mtime(zip->entry)));
  715. archive_le32enc(local_header + 14, zip->entry_crc32);
  716. if (zip->entry_uses_zip64) {
  717. /* Zip64 data in the local header "must" include both
  718. * compressed and uncompressed sizes AND those fields
  719. * are included only if these are 0xffffffff;
  720. * THEREFORE these must be set this way, even if we
  721. * know one of them is smaller. */
  722. archive_le32enc(local_header + 18, ZIP_4GB_MAX);
  723. archive_le32enc(local_header + 22, ZIP_4GB_MAX);
  724. } else {
  725. archive_le32enc(local_header + 18, (uint32_t)zip->entry_compressed_size);
  726. archive_le32enc(local_header + 22, (uint32_t)zip->entry_uncompressed_size);
  727. }
  728. archive_le16enc(local_header + 26, (uint16_t)filename_length);
  729. if (zip->entry_encryption == ENCRYPTION_TRADITIONAL) {
  730. if (zip->entry_flags & ZIP_ENTRY_FLAG_LENGTH_AT_END)
  731. zip->trad_chkdat = local_header[11];
  732. else
  733. zip->trad_chkdat = local_header[17];
  734. }
  735. /* Format as much of central directory file header as we can: */
  736. zip->file_header = cd_alloc(zip, 46);
  737. /* If (zip->file_header == NULL) XXXX */
  738. ++zip->central_directory_entries;
  739. memset(zip->file_header, 0, 46);
  740. memcpy(zip->file_header, "PK\001\002", 4);
  741. /* "Made by PKZip 2.0 on Unix." */
  742. archive_le16enc(zip->file_header + 4, 3 * 256 + version_needed);
  743. archive_le16enc(zip->file_header + 6, version_needed);
  744. archive_le16enc(zip->file_header + 8, zip->entry_flags);
  745. if (zip->entry_encryption == ENCRYPTION_WINZIP_AES128
  746. || zip->entry_encryption == ENCRYPTION_WINZIP_AES256)
  747. archive_le16enc(zip->file_header + 10, WINZIP_AES_ENCRYPTION);
  748. else
  749. archive_le16enc(zip->file_header + 10, zip->entry_compression);
  750. archive_le32enc(zip->file_header + 12,
  751. dos_time(archive_entry_mtime(zip->entry)));
  752. archive_le16enc(zip->file_header + 28, (uint16_t)filename_length);
  753. /* Following Info-Zip, store mode in the "external attributes" field. */
  754. archive_le32enc(zip->file_header + 38,
  755. ((uint32_t)archive_entry_mode(zip->entry)) << 16);
  756. e = cd_alloc(zip, filename_length);
  757. /* If (e == NULL) XXXX */
  758. copy_path(zip->entry, e);
  759. /* Format extra data. */
  760. memset(local_extra, 0, sizeof(local_extra));
  761. e = local_extra;
  762. /* First, extra blocks that are the same between
  763. * the local file header and the central directory.
  764. * We format them once and then duplicate them. */
  765. /* UT timestamp, length depends on what timestamps are set. */
  766. memcpy(e, "UT", 2);
  767. archive_le16enc(e + 2,
  768. 1
  769. + (archive_entry_mtime_is_set(entry) ? 4 : 0)
  770. + (archive_entry_atime_is_set(entry) ? 4 : 0)
  771. + (archive_entry_ctime_is_set(entry) ? 4 : 0));
  772. e += 4;
  773. *e++ =
  774. (archive_entry_mtime_is_set(entry) ? 1 : 0)
  775. | (archive_entry_atime_is_set(entry) ? 2 : 0)
  776. | (archive_entry_ctime_is_set(entry) ? 4 : 0);
  777. if (archive_entry_mtime_is_set(entry)) {
  778. archive_le32enc(e, (uint32_t)archive_entry_mtime(entry));
  779. e += 4;
  780. }
  781. if (archive_entry_atime_is_set(entry)) {
  782. archive_le32enc(e, (uint32_t)archive_entry_atime(entry));
  783. e += 4;
  784. }
  785. if (archive_entry_ctime_is_set(entry)) {
  786. archive_le32enc(e, (uint32_t)archive_entry_ctime(entry));
  787. e += 4;
  788. }
  789. /* ux Unix extra data, length 11, version 1 */
  790. /* TODO: If uid < 64k, use 2 bytes, ditto for gid. */
  791. memcpy(e, "ux\013\000\001", 5);
  792. e += 5;
  793. *e++ = 4; /* Length of following UID */
  794. archive_le32enc(e, (uint32_t)archive_entry_uid(entry));
  795. e += 4;
  796. *e++ = 4; /* Length of following GID */
  797. archive_le32enc(e, (uint32_t)archive_entry_gid(entry));
  798. e += 4;
  799. /* AES extra data field: WinZIP AES information, ID=0x9901 */
  800. if ((zip->entry_flags & ZIP_ENTRY_FLAG_ENCRYPTED)
  801. && (zip->entry_encryption == ENCRYPTION_WINZIP_AES128
  802. || zip->entry_encryption == ENCRYPTION_WINZIP_AES256)) {
  803. memcpy(e, "\001\231\007\000\001\000AE", 8);
  804. /* AES vendor version AE-2 does not store a CRC.
  805. * WinZip 11 uses AE-1, which does store the CRC,
  806. * but it does not store the CRC when the file size
  807. * is less than 20 bytes. So we simulate what
  808. * WinZip 11 does.
  809. * NOTE: WinZip 9.0 and 10.0 uses AE-2 by default. */
  810. if (archive_entry_size_is_set(zip->entry)
  811. && archive_entry_size(zip->entry) < 20) {
  812. archive_le16enc(e+4, AES_VENDOR_AE_2);
  813. zip->aes_vendor = AES_VENDOR_AE_2;/* no CRC. */
  814. } else
  815. zip->aes_vendor = AES_VENDOR_AE_1;
  816. e += 8;
  817. /* AES encryption strength. */
  818. *e++ = (zip->entry_encryption == ENCRYPTION_WINZIP_AES128)?1:3;
  819. /* Actual compression method. */
  820. archive_le16enc(e, zip->entry_compression);
  821. e += 2;
  822. }
  823. /* Copy UT ,ux, and AES-extra into central directory as well. */
  824. zip->file_header_extra_offset = zip->central_directory_bytes;
  825. cd_extra = cd_alloc(zip, e - local_extra);
  826. memcpy(cd_extra, local_extra, e - local_extra);
  827. /*
  828. * Following extra blocks vary between local header and
  829. * central directory. These are the local header versions.
  830. * Central directory versions get formatted in
  831. * archive_write_zip_finish_entry() below.
  832. */
  833. /* "[Zip64 entry] in the local header MUST include BOTH
  834. * original [uncompressed] and compressed size fields." */
  835. if (zip->entry_uses_zip64) {
  836. unsigned char *zip64_start = e;
  837. memcpy(e, "\001\000\020\000", 4);
  838. e += 4;
  839. archive_le64enc(e, zip->entry_uncompressed_size);
  840. e += 8;
  841. archive_le64enc(e, zip->entry_compressed_size);
  842. e += 8;
  843. archive_le16enc(zip64_start + 2, (uint16_t)(e - (zip64_start + 4)));
  844. }
  845. if (zip->flags & ZIP_FLAG_EXPERIMENT_xl) {
  846. /* Experimental 'xl' extension to improve streaming. */
  847. unsigned char *external_info = e;
  848. int included = 7;
  849. memcpy(e, "xl\000\000", 4); // 0x6c65 + 2-byte length
  850. e += 4;
  851. e[0] = included; /* bitmap of included fields */
  852. e += 1;
  853. if (included & 1) {
  854. archive_le16enc(e, /* "Version created by" */
  855. 3 * 256 + version_needed);
  856. e += 2;
  857. }
  858. if (included & 2) {
  859. archive_le16enc(e, 0); /* internal file attributes */
  860. e += 2;
  861. }
  862. if (included & 4) {
  863. archive_le32enc(e, /* external file attributes */
  864. ((uint32_t)archive_entry_mode(zip->entry)) << 16);
  865. e += 4;
  866. }
  867. if (included & 8) {
  868. // Libarchive does not currently support file comments.
  869. }
  870. archive_le16enc(external_info + 2, (uint16_t)(e - (external_info + 4)));
  871. }
  872. /* Update local header with size of extra data and write it all out: */
  873. archive_le16enc(local_header + 28, (uint16_t)(e - local_extra));
  874. ret = __archive_write_output(a, local_header, 30);
  875. if (ret != ARCHIVE_OK)
  876. return (ARCHIVE_FATAL);
  877. zip->written_bytes += 30;
  878. ret = write_path(zip->entry, a);
  879. if (ret <= ARCHIVE_OK)
  880. return (ARCHIVE_FATAL);
  881. zip->written_bytes += ret;
  882. ret = __archive_write_output(a, local_extra, e - local_extra);
  883. if (ret != ARCHIVE_OK)
  884. return (ARCHIVE_FATAL);
  885. zip->written_bytes += e - local_extra;
  886. /* For symlinks, write the body now. */
  887. if (slink != NULL) {
  888. ret = __archive_write_output(a, slink, slink_size);
  889. if (ret != ARCHIVE_OK)
  890. return (ARCHIVE_FATAL);
  891. zip->entry_compressed_written += slink_size;
  892. zip->entry_uncompressed_written += slink_size;
  893. zip->written_bytes += slink_size;
  894. }
  895. #ifdef HAVE_ZLIB_H
  896. if (zip->entry_compression == COMPRESSION_DEFLATE) {
  897. zip->stream.zalloc = Z_NULL;
  898. zip->stream.zfree = Z_NULL;
  899. zip->stream.opaque = Z_NULL;
  900. zip->stream.next_out = zip->buf;
  901. zip->stream.avail_out = (uInt)zip->len_buf;
  902. if (deflateInit2(&zip->stream, zip->deflate_compression_level,
  903. Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY) != Z_OK) {
  904. archive_set_error(&a->archive, ENOMEM,
  905. "Can't init deflate compressor");
  906. return (ARCHIVE_FATAL);
  907. }
  908. }
  909. #endif
  910. return (ret2);
  911. }
  912. static ssize_t
  913. archive_write_zip_data(struct archive_write *a, const void *buff, size_t s)
  914. {
  915. int ret;
  916. struct zip *zip = a->format_data;
  917. if ((int64_t)s > zip->entry_uncompressed_limit)
  918. s = (size_t)zip->entry_uncompressed_limit;
  919. zip->entry_uncompressed_written += s;
  920. if (s == 0) return 0;
  921. if (zip->entry_flags & ZIP_ENTRY_FLAG_ENCRYPTED) {
  922. switch (zip->entry_encryption) {
  923. case ENCRYPTION_TRADITIONAL:
  924. /* Initialize traditional PKWARE encryption context. */
  925. if (!zip->tctx_valid) {
  926. ret = init_traditional_pkware_encryption(a);
  927. if (ret != ARCHIVE_OK)
  928. return (ret);
  929. zip->tctx_valid = 1;
  930. }
  931. break;
  932. case ENCRYPTION_WINZIP_AES128:
  933. case ENCRYPTION_WINZIP_AES256:
  934. if (!zip->cctx_valid) {
  935. ret = init_winzip_aes_encryption(a);
  936. if (ret != ARCHIVE_OK)
  937. return (ret);
  938. zip->cctx_valid = zip->hctx_valid = 1;
  939. }
  940. break;
  941. default:
  942. break;
  943. }
  944. }
  945. switch (zip->entry_compression) {
  946. case COMPRESSION_STORE:
  947. if (zip->tctx_valid || zip->cctx_valid) {
  948. const uint8_t *rb = (const uint8_t *)buff;
  949. const uint8_t * const re = rb + s;
  950. while (rb < re) {
  951. size_t l;
  952. if (zip->tctx_valid) {
  953. l = trad_enc_encrypt_update(&zip->tctx,
  954. rb, re - rb,
  955. zip->buf, zip->len_buf);
  956. } else {
  957. l = zip->len_buf;
  958. ret = archive_encrypto_aes_ctr_update(
  959. &zip->cctx,
  960. rb, re - rb, zip->buf, &l);
  961. if (ret < 0) {
  962. archive_set_error(&a->archive,
  963. ARCHIVE_ERRNO_MISC,
  964. "Failed to encrypt file");
  965. return (ARCHIVE_FAILED);
  966. }
  967. archive_hmac_sha1_update(&zip->hctx,
  968. zip->buf, l);
  969. }
  970. ret = __archive_write_output(a, zip->buf, l);
  971. if (ret != ARCHIVE_OK)
  972. return (ret);
  973. zip->entry_compressed_written += l;
  974. zip->written_bytes += l;
  975. rb += l;
  976. }
  977. } else {
  978. ret = __archive_write_output(a, buff, s);
  979. if (ret != ARCHIVE_OK)
  980. return (ret);
  981. zip->written_bytes += s;
  982. zip->entry_compressed_written += s;
  983. }
  984. break;
  985. #if HAVE_ZLIB_H
  986. case COMPRESSION_DEFLATE:
  987. zip->stream.next_in = (unsigned char*)(uintptr_t)buff;
  988. zip->stream.avail_in = (uInt)s;
  989. do {
  990. ret = deflate(&zip->stream, Z_NO_FLUSH);
  991. if (ret == Z_STREAM_ERROR)
  992. return (ARCHIVE_FATAL);
  993. if (zip->stream.avail_out == 0) {
  994. if (zip->tctx_valid) {
  995. trad_enc_encrypt_update(&zip->tctx,
  996. zip->buf, zip->len_buf,
  997. zip->buf, zip->len_buf);
  998. } else if (zip->cctx_valid) {
  999. size_t outl = zip->len_buf;
  1000. ret = archive_encrypto_aes_ctr_update(
  1001. &zip->cctx,
  1002. zip->buf, zip->len_buf,
  1003. zip->buf, &outl);
  1004. if (ret < 0) {
  1005. archive_set_error(&a->archive,
  1006. ARCHIVE_ERRNO_MISC,
  1007. "Failed to encrypt file");
  1008. return (ARCHIVE_FAILED);
  1009. }
  1010. archive_hmac_sha1_update(&zip->hctx,
  1011. zip->buf, zip->len_buf);
  1012. }
  1013. ret = __archive_write_output(a, zip->buf,
  1014. zip->len_buf);
  1015. if (ret != ARCHIVE_OK)
  1016. return (ret);
  1017. zip->entry_compressed_written += zip->len_buf;
  1018. zip->written_bytes += zip->len_buf;
  1019. zip->stream.next_out = zip->buf;
  1020. zip->stream.avail_out = (uInt)zip->len_buf;
  1021. }
  1022. } while (zip->stream.avail_in != 0);
  1023. break;
  1024. #endif
  1025. default:
  1026. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1027. "Invalid ZIP compression type");
  1028. return ARCHIVE_FATAL;
  1029. }
  1030. zip->entry_uncompressed_limit -= s;
  1031. if (!zip->cctx_valid || zip->aes_vendor != AES_VENDOR_AE_2)
  1032. zip->entry_crc32 =
  1033. zip->crc32func(zip->entry_crc32, buff, (unsigned)s);
  1034. return (s);
  1035. }
  1036. static int
  1037. archive_write_zip_finish_entry(struct archive_write *a)
  1038. {
  1039. struct zip *zip = a->format_data;
  1040. int ret;
  1041. #if HAVE_ZLIB_H
  1042. if (zip->entry_compression == COMPRESSION_DEFLATE) {
  1043. for (;;) {
  1044. size_t remainder;
  1045. ret = deflate(&zip->stream, Z_FINISH);
  1046. if (ret == Z_STREAM_ERROR)
  1047. return (ARCHIVE_FATAL);
  1048. remainder = zip->len_buf - zip->stream.avail_out;
  1049. if (zip->tctx_valid) {
  1050. trad_enc_encrypt_update(&zip->tctx,
  1051. zip->buf, remainder, zip->buf, remainder);
  1052. } else if (zip->cctx_valid) {
  1053. size_t outl = remainder;
  1054. ret = archive_encrypto_aes_ctr_update(
  1055. &zip->cctx, zip->buf, remainder,
  1056. zip->buf, &outl);
  1057. if (ret < 0) {
  1058. archive_set_error(&a->archive,
  1059. ARCHIVE_ERRNO_MISC,
  1060. "Failed to encrypt file");
  1061. return (ARCHIVE_FAILED);
  1062. }
  1063. archive_hmac_sha1_update(&zip->hctx,
  1064. zip->buf, remainder);
  1065. }
  1066. ret = __archive_write_output(a, zip->buf, remainder);
  1067. if (ret != ARCHIVE_OK)
  1068. return (ret);
  1069. zip->entry_compressed_written += remainder;
  1070. zip->written_bytes += remainder;
  1071. zip->stream.next_out = zip->buf;
  1072. if (zip->stream.avail_out != 0)
  1073. break;
  1074. zip->stream.avail_out = (uInt)zip->len_buf;
  1075. }
  1076. deflateEnd(&zip->stream);
  1077. }
  1078. #endif
  1079. if (zip->hctx_valid) {
  1080. uint8_t hmac[20];
  1081. size_t hmac_len = 20;
  1082. archive_hmac_sha1_final(&zip->hctx, hmac, &hmac_len);
  1083. ret = __archive_write_output(a, hmac, AUTH_CODE_SIZE);
  1084. if (ret != ARCHIVE_OK)
  1085. return (ret);
  1086. zip->entry_compressed_written += AUTH_CODE_SIZE;
  1087. zip->written_bytes += AUTH_CODE_SIZE;
  1088. }
  1089. /* Write trailing data descriptor. */
  1090. if ((zip->entry_flags & ZIP_ENTRY_FLAG_LENGTH_AT_END) != 0) {
  1091. char d[24];
  1092. memcpy(d, "PK\007\010", 4);
  1093. if (zip->cctx_valid && zip->aes_vendor == AES_VENDOR_AE_2)
  1094. archive_le32enc(d + 4, 0);/* no CRC.*/
  1095. else
  1096. archive_le32enc(d + 4, zip->entry_crc32);
  1097. if (zip->entry_uses_zip64) {
  1098. archive_le64enc(d + 8,
  1099. (uint64_t)zip->entry_compressed_written);
  1100. archive_le64enc(d + 16,
  1101. (uint64_t)zip->entry_uncompressed_written);
  1102. ret = __archive_write_output(a, d, 24);
  1103. zip->written_bytes += 24;
  1104. } else {
  1105. archive_le32enc(d + 8,
  1106. (uint32_t)zip->entry_compressed_written);
  1107. archive_le32enc(d + 12,
  1108. (uint32_t)zip->entry_uncompressed_written);
  1109. ret = __archive_write_output(a, d, 16);
  1110. zip->written_bytes += 16;
  1111. }
  1112. if (ret != ARCHIVE_OK)
  1113. return (ARCHIVE_FATAL);
  1114. }
  1115. /* Append Zip64 extra data to central directory information. */
  1116. if (zip->entry_compressed_written > ZIP_4GB_MAX
  1117. || zip->entry_uncompressed_written > ZIP_4GB_MAX
  1118. || zip->entry_offset > ZIP_4GB_MAX) {
  1119. unsigned char zip64[32];
  1120. unsigned char *z = zip64, *zd;
  1121. memcpy(z, "\001\000\000\000", 4);
  1122. z += 4;
  1123. if (zip->entry_uncompressed_written >= ZIP_4GB_MAX) {
  1124. archive_le64enc(z, zip->entry_uncompressed_written);
  1125. z += 8;
  1126. }
  1127. if (zip->entry_compressed_written >= ZIP_4GB_MAX) {
  1128. archive_le64enc(z, zip->entry_compressed_written);
  1129. z += 8;
  1130. }
  1131. if (zip->entry_offset >= ZIP_4GB_MAX) {
  1132. archive_le64enc(z, zip->entry_offset);
  1133. z += 8;
  1134. }
  1135. archive_le16enc(zip64 + 2, (uint16_t)(z - (zip64 + 4)));
  1136. zd = cd_alloc(zip, z - zip64);
  1137. if (zd == NULL) {
  1138. archive_set_error(&a->archive, ENOMEM,
  1139. "Can't allocate zip data");
  1140. return (ARCHIVE_FATAL);
  1141. }
  1142. memcpy(zd, zip64, z - zip64);
  1143. /* Zip64 means version needs to be set to at least 4.5 */
  1144. if (archive_le16dec(zip->file_header + 6) < 45)
  1145. archive_le16enc(zip->file_header + 6, 45);
  1146. }
  1147. /* Fix up central directory file header. */
  1148. if (zip->cctx_valid && zip->aes_vendor == AES_VENDOR_AE_2)
  1149. archive_le32enc(zip->file_header + 16, 0);/* no CRC.*/
  1150. else
  1151. archive_le32enc(zip->file_header + 16, zip->entry_crc32);
  1152. archive_le32enc(zip->file_header + 20,
  1153. (uint32_t)zipmin(zip->entry_compressed_written,
  1154. ZIP_4GB_MAX));
  1155. archive_le32enc(zip->file_header + 24,
  1156. (uint32_t)zipmin(zip->entry_uncompressed_written,
  1157. ZIP_4GB_MAX));
  1158. archive_le16enc(zip->file_header + 30,
  1159. (uint16_t)(zip->central_directory_bytes - zip->file_header_extra_offset));
  1160. archive_le32enc(zip->file_header + 42,
  1161. (uint32_t)zipmin(zip->entry_offset,
  1162. ZIP_4GB_MAX));
  1163. return (ARCHIVE_OK);
  1164. }
  1165. static int
  1166. archive_write_zip_close(struct archive_write *a)
  1167. {
  1168. uint8_t buff[64];
  1169. int64_t offset_start, offset_end;
  1170. struct zip *zip = a->format_data;
  1171. struct cd_segment *segment;
  1172. int ret;
  1173. offset_start = zip->written_bytes;
  1174. segment = zip->central_directory;
  1175. while (segment != NULL) {
  1176. ret = __archive_write_output(a,
  1177. segment->buff, segment->p - segment->buff);
  1178. if (ret != ARCHIVE_OK)
  1179. return (ARCHIVE_FATAL);
  1180. zip->written_bytes += segment->p - segment->buff;
  1181. segment = segment->next;
  1182. }
  1183. offset_end = zip->written_bytes;
  1184. /* If central dir info is too large, write Zip64 end-of-cd */
  1185. if (offset_end - offset_start > ZIP_4GB_MAX
  1186. || offset_start > ZIP_4GB_MAX
  1187. || zip->central_directory_entries > 0xffffUL
  1188. || (zip->flags & ZIP_FLAG_FORCE_ZIP64)) {
  1189. /* Zip64 end-of-cd record */
  1190. memset(buff, 0, 56);
  1191. memcpy(buff, "PK\006\006", 4);
  1192. archive_le64enc(buff + 4, 44);
  1193. archive_le16enc(buff + 12, 45);
  1194. archive_le16enc(buff + 14, 45);
  1195. /* This is disk 0 of 0. */
  1196. archive_le64enc(buff + 24, zip->central_directory_entries);
  1197. archive_le64enc(buff + 32, zip->central_directory_entries);
  1198. archive_le64enc(buff + 40, offset_end - offset_start);
  1199. archive_le64enc(buff + 48, offset_start);
  1200. ret = __archive_write_output(a, buff, 56);
  1201. if (ret != ARCHIVE_OK)
  1202. return (ARCHIVE_FATAL);
  1203. zip->written_bytes += 56;
  1204. /* Zip64 end-of-cd locator record. */
  1205. memset(buff, 0, 20);
  1206. memcpy(buff, "PK\006\007", 4);
  1207. archive_le32enc(buff + 4, 0);
  1208. archive_le64enc(buff + 8, offset_end);
  1209. archive_le32enc(buff + 16, 1);
  1210. ret = __archive_write_output(a, buff, 20);
  1211. if (ret != ARCHIVE_OK)
  1212. return (ARCHIVE_FATAL);
  1213. zip->written_bytes += 20;
  1214. }
  1215. /* Format and write end of central directory. */
  1216. memset(buff, 0, sizeof(buff));
  1217. memcpy(buff, "PK\005\006", 4);
  1218. archive_le16enc(buff + 8, (uint16_t)zipmin(0xffffU,
  1219. zip->central_directory_entries));
  1220. archive_le16enc(buff + 10, (uint16_t)zipmin(0xffffU,
  1221. zip->central_directory_entries));
  1222. archive_le32enc(buff + 12,
  1223. (uint32_t)zipmin(ZIP_4GB_MAX, (offset_end - offset_start)));
  1224. archive_le32enc(buff + 16,
  1225. (uint32_t)zipmin(ZIP_4GB_MAX, offset_start));
  1226. ret = __archive_write_output(a, buff, 22);
  1227. if (ret != ARCHIVE_OK)
  1228. return (ARCHIVE_FATAL);
  1229. zip->written_bytes += 22;
  1230. return (ARCHIVE_OK);
  1231. }
  1232. static int
  1233. archive_write_zip_free(struct archive_write *a)
  1234. {
  1235. struct zip *zip;
  1236. struct cd_segment *segment;
  1237. zip = a->format_data;
  1238. while (zip->central_directory != NULL) {
  1239. segment = zip->central_directory;
  1240. zip->central_directory = segment->next;
  1241. free(segment->buff);
  1242. free(segment);
  1243. }
  1244. free(zip->buf);
  1245. archive_entry_free(zip->entry);
  1246. if (zip->cctx_valid)
  1247. archive_encrypto_aes_ctr_release(&zip->cctx);
  1248. if (zip->hctx_valid)
  1249. archive_hmac_sha1_cleanup(&zip->hctx);
  1250. /* TODO: Free opt_sconv, sconv_default */
  1251. free(zip);
  1252. a->format_data = NULL;
  1253. return (ARCHIVE_OK);
  1254. }
  1255. /* Convert into MSDOS-style date/time. */
  1256. static unsigned int
  1257. dos_time(const time_t unix_time)
  1258. {
  1259. struct tm *t;
  1260. unsigned int dt;
  1261. #if defined(HAVE_LOCALTIME_R) || defined(HAVE__LOCALTIME64_S)
  1262. struct tm tmbuf;
  1263. #endif
  1264. #if defined(HAVE__LOCALTIME64_S)
  1265. errno_t terr;
  1266. __time64_t tmptime;
  1267. #endif
  1268. /* This will not preserve time when creating/extracting the archive
  1269. * on two systems with different time zones. */
  1270. #if defined(HAVE_LOCALTIME_R)
  1271. t = localtime_r(&unix_time, &tmbuf);
  1272. #elif defined(HAVE__LOCALTIME64_S)
  1273. tmptime = unix_time;
  1274. terr = _localtime64_s(&tmbuf, &tmptime);
  1275. if (terr)
  1276. t = NULL;
  1277. else
  1278. t = &tmbuf;
  1279. #else
  1280. t = localtime(&unix_time);
  1281. #endif
  1282. /* MSDOS-style date/time is only between 1980-01-01 and 2107-12-31 */
  1283. if (t->tm_year < 1980 - 1900)
  1284. /* Set minimum date/time '1980-01-01 00:00:00'. */
  1285. dt = 0x00210000U;
  1286. else if (t->tm_year > 2107 - 1900)
  1287. /* Set maximum date/time '2107-12-31 23:59:58'. */
  1288. dt = 0xff9fbf7dU;
  1289. else {
  1290. dt = 0;
  1291. dt += ((t->tm_year - 80) & 0x7f) << 9;
  1292. dt += ((t->tm_mon + 1) & 0x0f) << 5;
  1293. dt += (t->tm_mday & 0x1f);
  1294. dt <<= 16;
  1295. dt += (t->tm_hour & 0x1f) << 11;
  1296. dt += (t->tm_min & 0x3f) << 5;
  1297. dt += (t->tm_sec & 0x3e) >> 1; /* Only counting every 2 seconds. */
  1298. }
  1299. return dt;
  1300. }
  1301. static size_t
  1302. path_length(struct archive_entry *entry)
  1303. {
  1304. mode_t type;
  1305. const char *path;
  1306. size_t len;
  1307. type = archive_entry_filetype(entry);
  1308. path = archive_entry_pathname(entry);
  1309. if (path == NULL)
  1310. return (0);
  1311. len = strlen(path);
  1312. if (type == AE_IFDIR && (path[0] == '\0' || path[len - 1] != '/'))
  1313. ++len; /* Space for the trailing / */
  1314. return len;
  1315. }
  1316. static int
  1317. write_path(struct archive_entry *entry, struct archive_write *archive)
  1318. {
  1319. int ret;
  1320. const char *path;
  1321. mode_t type;
  1322. size_t written_bytes;
  1323. path = archive_entry_pathname(entry);
  1324. type = archive_entry_filetype(entry);
  1325. written_bytes = 0;
  1326. if (path == NULL)
  1327. return (ARCHIVE_FATAL);
  1328. ret = __archive_write_output(archive, path, strlen(path));
  1329. if (ret != ARCHIVE_OK)
  1330. return (ARCHIVE_FATAL);
  1331. written_bytes += strlen(path);
  1332. /* Folders are recognized by a trailing slash. */
  1333. if ((type == AE_IFDIR) & (path[strlen(path) - 1] != '/')) {
  1334. ret = __archive_write_output(archive, "/", 1);
  1335. if (ret != ARCHIVE_OK)
  1336. return (ARCHIVE_FATAL);
  1337. written_bytes += 1;
  1338. }
  1339. return ((int)written_bytes);
  1340. }
  1341. static void
  1342. copy_path(struct archive_entry *entry, unsigned char *p)
  1343. {
  1344. const char *path;
  1345. size_t pathlen;
  1346. mode_t type;
  1347. path = archive_entry_pathname(entry);
  1348. pathlen = strlen(path);
  1349. type = archive_entry_filetype(entry);
  1350. memcpy(p, path, pathlen);
  1351. /* Folders are recognized by a trailing slash. */
  1352. if ((type == AE_IFDIR) && (path[pathlen - 1] != '/'))
  1353. p[pathlen] = '/';
  1354. }
  1355. static struct archive_string_conv *
  1356. get_sconv(struct archive_write *a, struct zip *zip)
  1357. {
  1358. if (zip->opt_sconv != NULL)
  1359. return (zip->opt_sconv);
  1360. if (!zip->init_default_conversion) {
  1361. zip->sconv_default =
  1362. archive_string_default_conversion_for_write(&(a->archive));
  1363. zip->init_default_conversion = 1;
  1364. }
  1365. return (zip->sconv_default);
  1366. }
  1367. /*
  1368. Traditional PKWARE Decryption functions.
  1369. */
  1370. static void
  1371. trad_enc_update_keys(struct trad_enc_ctx *ctx, uint8_t c)
  1372. {
  1373. uint8_t t;
  1374. #define CRC32(c, b) (crc32(c ^ 0xffffffffUL, &b, 1) ^ 0xffffffffUL)
  1375. ctx->keys[0] = CRC32(ctx->keys[0], c);
  1376. ctx->keys[1] = (ctx->keys[1] + (ctx->keys[0] & 0xff)) * 134775813L + 1;
  1377. t = (ctx->keys[1] >> 24) & 0xff;
  1378. ctx->keys[2] = CRC32(ctx->keys[2], t);
  1379. #undef CRC32
  1380. }
  1381. static uint8_t
  1382. trad_enc_decrypt_byte(struct trad_enc_ctx *ctx)
  1383. {
  1384. unsigned temp = ctx->keys[2] | 2;
  1385. return (uint8_t)((temp * (temp ^ 1)) >> 8) & 0xff;
  1386. }
  1387. static unsigned
  1388. trad_enc_encrypt_update(struct trad_enc_ctx *ctx, const uint8_t *in,
  1389. size_t in_len, uint8_t *out, size_t out_len)
  1390. {
  1391. unsigned i, max;
  1392. max = (unsigned)((in_len < out_len)? in_len: out_len);
  1393. for (i = 0; i < max; i++) {
  1394. uint8_t t = in[i];
  1395. out[i] = t ^ trad_enc_decrypt_byte(ctx);
  1396. trad_enc_update_keys(ctx, t);
  1397. }
  1398. return i;
  1399. }
  1400. static int
  1401. trad_enc_init(struct trad_enc_ctx *ctx, const char *pw, size_t pw_len)
  1402. {
  1403. ctx->keys[0] = 305419896L;
  1404. ctx->keys[1] = 591751049L;
  1405. ctx->keys[2] = 878082192L;
  1406. for (;pw_len; --pw_len)
  1407. trad_enc_update_keys(ctx, *pw++);
  1408. return 0;
  1409. }
  1410. static int
  1411. is_traditional_pkware_encryption_supported(void)
  1412. {
  1413. uint8_t key[TRAD_HEADER_SIZE];
  1414. if (archive_random(key, sizeof(key)-1) != ARCHIVE_OK)
  1415. return (0);
  1416. return (1);
  1417. }
  1418. static int
  1419. init_traditional_pkware_encryption(struct archive_write *a)
  1420. {
  1421. struct zip *zip = a->format_data;
  1422. const char *passphrase;
  1423. uint8_t key[TRAD_HEADER_SIZE];
  1424. uint8_t key_encrypted[TRAD_HEADER_SIZE];
  1425. int ret;
  1426. passphrase = __archive_write_get_passphrase(a);
  1427. if (passphrase == NULL) {
  1428. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1429. "Encryption needs passphrase");
  1430. return ARCHIVE_FAILED;
  1431. }
  1432. if (archive_random(key, sizeof(key)-1) != ARCHIVE_OK) {
  1433. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1434. "Can't generate random number for encryption");
  1435. return ARCHIVE_FATAL;
  1436. }
  1437. trad_enc_init(&zip->tctx, passphrase, strlen(passphrase));
  1438. /* Set the last key code which will be used as a check code
  1439. * for verifying passphrase in decryption. */
  1440. key[TRAD_HEADER_SIZE-1] = zip->trad_chkdat;
  1441. trad_enc_encrypt_update(&zip->tctx, key, TRAD_HEADER_SIZE,
  1442. key_encrypted, TRAD_HEADER_SIZE);
  1443. /* Write encrypted keys in the top of the file content. */
  1444. ret = __archive_write_output(a, key_encrypted, TRAD_HEADER_SIZE);
  1445. if (ret != ARCHIVE_OK)
  1446. return (ret);
  1447. zip->written_bytes += TRAD_HEADER_SIZE;
  1448. zip->entry_compressed_written += TRAD_HEADER_SIZE;
  1449. return (ret);
  1450. }
  1451. static int
  1452. init_winzip_aes_encryption(struct archive_write *a)
  1453. {
  1454. struct zip *zip = a->format_data;
  1455. const char *passphrase;
  1456. size_t key_len, salt_len;
  1457. uint8_t salt[16 + 2];
  1458. uint8_t derived_key[MAX_DERIVED_KEY_BUF_SIZE];
  1459. int ret;
  1460. passphrase = __archive_write_get_passphrase(a);
  1461. if (passphrase == NULL) {
  1462. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1463. "Encryption needs passphrase");
  1464. return (ARCHIVE_FAILED);
  1465. }
  1466. if (zip->entry_encryption == ENCRYPTION_WINZIP_AES128) {
  1467. salt_len = 8;
  1468. key_len = 16;
  1469. } else {
  1470. /* AES 256 */
  1471. salt_len = 16;
  1472. key_len = 32;
  1473. }
  1474. if (archive_random(salt, salt_len) != ARCHIVE_OK) {
  1475. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1476. "Can't generate random number for encryption");
  1477. return (ARCHIVE_FATAL);
  1478. }
  1479. archive_pbkdf2_sha1(passphrase, strlen(passphrase),
  1480. salt, salt_len, 1000, derived_key, key_len * 2 + 2);
  1481. ret = archive_encrypto_aes_ctr_init(&zip->cctx, derived_key, key_len);
  1482. if (ret != 0) {
  1483. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1484. "Decryption is unsupported due to lack of crypto library");
  1485. return (ARCHIVE_FAILED);
  1486. }
  1487. ret = archive_hmac_sha1_init(&zip->hctx, derived_key + key_len,
  1488. key_len);
  1489. if (ret != 0) {
  1490. archive_encrypto_aes_ctr_release(&zip->cctx);
  1491. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1492. "Failed to initialize HMAC-SHA1");
  1493. return (ARCHIVE_FAILED);
  1494. }
  1495. /* Set a password verification value after the 'salt'. */
  1496. salt[salt_len] = derived_key[key_len * 2];
  1497. salt[salt_len + 1] = derived_key[key_len * 2 + 1];
  1498. /* Write encrypted keys in the top of the file content. */
  1499. ret = __archive_write_output(a, salt, salt_len + 2);
  1500. if (ret != ARCHIVE_OK)
  1501. return (ret);
  1502. zip->written_bytes += salt_len + 2;
  1503. zip->entry_compressed_written += salt_len + 2;
  1504. return (ARCHIVE_OK);
  1505. }
  1506. static int
  1507. is_winzip_aes_encryption_supported(int encryption)
  1508. {
  1509. size_t key_len, salt_len;
  1510. uint8_t salt[16 + 2];
  1511. uint8_t derived_key[MAX_DERIVED_KEY_BUF_SIZE];
  1512. archive_crypto_ctx cctx;
  1513. archive_hmac_sha1_ctx hctx;
  1514. int ret;
  1515. if (encryption == ENCRYPTION_WINZIP_AES128) {
  1516. salt_len = 8;
  1517. key_len = 16;
  1518. } else {
  1519. /* AES 256 */
  1520. salt_len = 16;
  1521. key_len = 32;
  1522. }
  1523. if (archive_random(salt, salt_len) != ARCHIVE_OK)
  1524. return (0);
  1525. ret = archive_pbkdf2_sha1("p", 1, salt, salt_len, 1000,
  1526. derived_key, key_len * 2 + 2);
  1527. if (ret != 0)
  1528. return (0);
  1529. ret = archive_encrypto_aes_ctr_init(&cctx, derived_key, key_len);
  1530. if (ret != 0)
  1531. return (0);
  1532. ret = archive_hmac_sha1_init(&hctx, derived_key + key_len,
  1533. key_len);
  1534. archive_encrypto_aes_ctr_release(&cctx);
  1535. if (ret != 0)
  1536. return (0);
  1537. archive_hmac_sha1_cleanup(&hctx);
  1538. return (1);
  1539. }