archive_write_set_format_zip.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693
  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 <cm3p/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. case ENCRYPTION_NONE:
  533. default:
  534. break;
  535. }
  536. }
  537. #if defined(_WIN32) && !defined(__CYGWIN__)
  538. /* Make sure the path separators in pathname, hardlink and symlink
  539. * are all slash '/', not the Windows path separator '\'. */
  540. zip->entry = __la_win_entry_in_posix_pathseparator(entry);
  541. if (zip->entry == entry)
  542. zip->entry = archive_entry_clone(entry);
  543. #else
  544. zip->entry = archive_entry_clone(entry);
  545. #endif
  546. if (zip->entry == NULL) {
  547. archive_set_error(&a->archive, ENOMEM,
  548. "Can't allocate zip header data");
  549. return (ARCHIVE_FATAL);
  550. }
  551. if (sconv != NULL) {
  552. const char *p;
  553. size_t len;
  554. if (archive_entry_pathname_l(entry, &p, &len, sconv) != 0) {
  555. if (errno == ENOMEM) {
  556. archive_set_error(&a->archive, ENOMEM,
  557. "Can't allocate memory for Pathname");
  558. return (ARCHIVE_FATAL);
  559. }
  560. archive_set_error(&a->archive,
  561. ARCHIVE_ERRNO_FILE_FORMAT,
  562. "Can't translate Pathname '%s' to %s",
  563. archive_entry_pathname(entry),
  564. archive_string_conversion_charset_name(sconv));
  565. ret2 = ARCHIVE_WARN;
  566. }
  567. if (len > 0)
  568. archive_entry_set_pathname(zip->entry, p);
  569. /*
  570. * There is no standard for symlink handling; we convert
  571. * it using the same character-set translation that we use
  572. * for filename.
  573. */
  574. if (type == AE_IFLNK) {
  575. if (archive_entry_symlink_l(entry, &p, &len, sconv)) {
  576. if (errno == ENOMEM) {
  577. archive_set_error(&a->archive, ENOMEM,
  578. "Can't allocate memory "
  579. " for Symlink");
  580. return (ARCHIVE_FATAL);
  581. }
  582. /* No error if we can't convert. */
  583. } else if (len > 0)
  584. archive_entry_set_symlink(zip->entry, p);
  585. }
  586. }
  587. /* If filename isn't ASCII and we can use UTF-8, set the UTF-8 flag. */
  588. if (!is_all_ascii(archive_entry_pathname(zip->entry))) {
  589. if (zip->opt_sconv != NULL) {
  590. if (strcmp(archive_string_conversion_charset_name(
  591. zip->opt_sconv), "UTF-8") == 0)
  592. zip->entry_flags |= ZIP_ENTRY_FLAG_UTF8_NAME;
  593. #if HAVE_NL_LANGINFO
  594. } else if (strcmp(nl_langinfo(CODESET), "UTF-8") == 0) {
  595. zip->entry_flags |= ZIP_ENTRY_FLAG_UTF8_NAME;
  596. #endif
  597. }
  598. }
  599. filename_length = path_length(zip->entry);
  600. /* Determine appropriate compression and size for this entry. */
  601. if (type == AE_IFLNK) {
  602. slink = archive_entry_symlink(zip->entry);
  603. if (slink != NULL)
  604. slink_size = strlen(slink);
  605. else
  606. slink_size = 0;
  607. zip->entry_uncompressed_limit = slink_size;
  608. zip->entry_compressed_size = slink_size;
  609. zip->entry_uncompressed_size = slink_size;
  610. zip->entry_crc32 = zip->crc32func(zip->entry_crc32,
  611. (const unsigned char *)slink, slink_size);
  612. zip->entry_compression = COMPRESSION_STORE;
  613. version_needed = 20;
  614. } else if (type != AE_IFREG) {
  615. zip->entry_compression = COMPRESSION_STORE;
  616. zip->entry_uncompressed_limit = 0;
  617. version_needed = 20;
  618. } else if (archive_entry_size_is_set(zip->entry)) {
  619. int64_t size = archive_entry_size(zip->entry);
  620. int64_t additional_size = 0;
  621. zip->entry_uncompressed_limit = size;
  622. zip->entry_compression = zip->requested_compression;
  623. if (zip->entry_compression == COMPRESSION_UNSPECIFIED) {
  624. zip->entry_compression = COMPRESSION_DEFAULT;
  625. }
  626. if (zip->entry_compression == COMPRESSION_STORE) {
  627. zip->entry_compressed_size = size;
  628. zip->entry_uncompressed_size = size;
  629. version_needed = 10;
  630. } else {
  631. zip->entry_uncompressed_size = size;
  632. version_needed = 20;
  633. }
  634. if (zip->entry_flags & ZIP_ENTRY_FLAG_ENCRYPTED) {
  635. switch (zip->entry_encryption) {
  636. case ENCRYPTION_TRADITIONAL:
  637. additional_size = TRAD_HEADER_SIZE;
  638. version_needed = 20;
  639. break;
  640. case ENCRYPTION_WINZIP_AES128:
  641. additional_size = WINZIP_AES128_HEADER_SIZE
  642. + AUTH_CODE_SIZE;
  643. version_needed = 20;
  644. break;
  645. case ENCRYPTION_WINZIP_AES256:
  646. additional_size = WINZIP_AES256_HEADER_SIZE
  647. + AUTH_CODE_SIZE;
  648. version_needed = 20;
  649. break;
  650. case ENCRYPTION_NONE:
  651. default:
  652. break;
  653. }
  654. if (zip->entry_compression == COMPRESSION_STORE)
  655. zip->entry_compressed_size += additional_size;
  656. }
  657. /*
  658. * Set Zip64 extension in any of the following cases
  659. * (this was suggested by discussion on info-zip-dev
  660. * mailing list):
  661. * = Zip64 is being forced by user
  662. * = File is over 4GiB uncompressed
  663. * (including encryption header, if any)
  664. * = File is close to 4GiB and is being compressed
  665. * (compression might make file larger)
  666. */
  667. if ((zip->flags & ZIP_FLAG_FORCE_ZIP64)
  668. || (zip->entry_uncompressed_size + additional_size > ZIP_4GB_MAX)
  669. || (zip->entry_uncompressed_size > ZIP_4GB_MAX_UNCOMPRESSED
  670. && zip->entry_compression != COMPRESSION_STORE)) {
  671. zip->entry_uses_zip64 = 1;
  672. version_needed = 45;
  673. }
  674. /* We may know the size, but never the CRC. */
  675. zip->entry_flags |= ZIP_ENTRY_FLAG_LENGTH_AT_END;
  676. } else {
  677. /* We don't know the size. Use the default
  678. * compression unless specified otherwise.
  679. * We enable Zip64 extensions unless we're told not to.
  680. */
  681. zip->entry_compression = zip->requested_compression;
  682. if(zip->entry_compression == COMPRESSION_UNSPECIFIED){
  683. zip->entry_compression = COMPRESSION_DEFAULT;
  684. }
  685. zip->entry_flags |= ZIP_ENTRY_FLAG_LENGTH_AT_END;
  686. if ((zip->flags & ZIP_FLAG_AVOID_ZIP64) == 0) {
  687. zip->entry_uses_zip64 = 1;
  688. version_needed = 45;
  689. } else if (zip->entry_compression == COMPRESSION_STORE) {
  690. version_needed = 10;
  691. } else {
  692. version_needed = 20;
  693. }
  694. if (zip->entry_flags & ZIP_ENTRY_FLAG_ENCRYPTED) {
  695. switch (zip->entry_encryption) {
  696. case ENCRYPTION_TRADITIONAL:
  697. case ENCRYPTION_WINZIP_AES128:
  698. case ENCRYPTION_WINZIP_AES256:
  699. if (version_needed < 20)
  700. version_needed = 20;
  701. break;
  702. case ENCRYPTION_NONE:
  703. default:
  704. break;
  705. }
  706. }
  707. }
  708. /* Format the local header. */
  709. memset(local_header, 0, sizeof(local_header));
  710. memcpy(local_header, "PK\003\004", 4);
  711. archive_le16enc(local_header + 4, version_needed);
  712. archive_le16enc(local_header + 6, zip->entry_flags);
  713. if (zip->entry_encryption == ENCRYPTION_WINZIP_AES128
  714. || zip->entry_encryption == ENCRYPTION_WINZIP_AES256)
  715. archive_le16enc(local_header + 8, WINZIP_AES_ENCRYPTION);
  716. else
  717. archive_le16enc(local_header + 8, zip->entry_compression);
  718. archive_le32enc(local_header + 10,
  719. dos_time(archive_entry_mtime(zip->entry)));
  720. archive_le32enc(local_header + 14, zip->entry_crc32);
  721. if (zip->entry_uses_zip64) {
  722. /* Zip64 data in the local header "must" include both
  723. * compressed and uncompressed sizes AND those fields
  724. * are included only if these are 0xffffffff;
  725. * THEREFORE these must be set this way, even if we
  726. * know one of them is smaller. */
  727. archive_le32enc(local_header + 18, ZIP_4GB_MAX);
  728. archive_le32enc(local_header + 22, ZIP_4GB_MAX);
  729. } else {
  730. archive_le32enc(local_header + 18, (uint32_t)zip->entry_compressed_size);
  731. archive_le32enc(local_header + 22, (uint32_t)zip->entry_uncompressed_size);
  732. }
  733. archive_le16enc(local_header + 26, (uint16_t)filename_length);
  734. if (zip->entry_encryption == ENCRYPTION_TRADITIONAL) {
  735. if (zip->entry_flags & ZIP_ENTRY_FLAG_LENGTH_AT_END)
  736. zip->trad_chkdat = local_header[11];
  737. else
  738. zip->trad_chkdat = local_header[17];
  739. }
  740. /* Format as much of central directory file header as we can: */
  741. zip->file_header = cd_alloc(zip, 46);
  742. /* If (zip->file_header == NULL) XXXX */
  743. ++zip->central_directory_entries;
  744. memset(zip->file_header, 0, 46);
  745. memcpy(zip->file_header, "PK\001\002", 4);
  746. /* "Made by PKZip 2.0 on Unix." */
  747. archive_le16enc(zip->file_header + 4, 3 * 256 + version_needed);
  748. archive_le16enc(zip->file_header + 6, version_needed);
  749. archive_le16enc(zip->file_header + 8, zip->entry_flags);
  750. if (zip->entry_encryption == ENCRYPTION_WINZIP_AES128
  751. || zip->entry_encryption == ENCRYPTION_WINZIP_AES256)
  752. archive_le16enc(zip->file_header + 10, WINZIP_AES_ENCRYPTION);
  753. else
  754. archive_le16enc(zip->file_header + 10, zip->entry_compression);
  755. archive_le32enc(zip->file_header + 12,
  756. dos_time(archive_entry_mtime(zip->entry)));
  757. archive_le16enc(zip->file_header + 28, (uint16_t)filename_length);
  758. /* Following Info-Zip, store mode in the "external attributes" field. */
  759. archive_le32enc(zip->file_header + 38,
  760. ((uint32_t)archive_entry_mode(zip->entry)) << 16);
  761. e = cd_alloc(zip, filename_length);
  762. /* If (e == NULL) XXXX */
  763. copy_path(zip->entry, e);
  764. /* Format extra data. */
  765. memset(local_extra, 0, sizeof(local_extra));
  766. e = local_extra;
  767. /* First, extra blocks that are the same between
  768. * the local file header and the central directory.
  769. * We format them once and then duplicate them. */
  770. /* UT timestamp, length depends on what timestamps are set. */
  771. memcpy(e, "UT", 2);
  772. archive_le16enc(e + 2,
  773. 1
  774. + (archive_entry_mtime_is_set(entry) ? 4 : 0)
  775. + (archive_entry_atime_is_set(entry) ? 4 : 0)
  776. + (archive_entry_ctime_is_set(entry) ? 4 : 0));
  777. e += 4;
  778. *e++ =
  779. (archive_entry_mtime_is_set(entry) ? 1 : 0)
  780. | (archive_entry_atime_is_set(entry) ? 2 : 0)
  781. | (archive_entry_ctime_is_set(entry) ? 4 : 0);
  782. if (archive_entry_mtime_is_set(entry)) {
  783. archive_le32enc(e, (uint32_t)archive_entry_mtime(entry));
  784. e += 4;
  785. }
  786. if (archive_entry_atime_is_set(entry)) {
  787. archive_le32enc(e, (uint32_t)archive_entry_atime(entry));
  788. e += 4;
  789. }
  790. if (archive_entry_ctime_is_set(entry)) {
  791. archive_le32enc(e, (uint32_t)archive_entry_ctime(entry));
  792. e += 4;
  793. }
  794. /* ux Unix extra data, length 11, version 1 */
  795. /* TODO: If uid < 64k, use 2 bytes, ditto for gid. */
  796. memcpy(e, "ux\013\000\001", 5);
  797. e += 5;
  798. *e++ = 4; /* Length of following UID */
  799. archive_le32enc(e, (uint32_t)archive_entry_uid(entry));
  800. e += 4;
  801. *e++ = 4; /* Length of following GID */
  802. archive_le32enc(e, (uint32_t)archive_entry_gid(entry));
  803. e += 4;
  804. /* AES extra data field: WinZIP AES information, ID=0x9901 */
  805. if ((zip->entry_flags & ZIP_ENTRY_FLAG_ENCRYPTED)
  806. && (zip->entry_encryption == ENCRYPTION_WINZIP_AES128
  807. || zip->entry_encryption == ENCRYPTION_WINZIP_AES256)) {
  808. memcpy(e, "\001\231\007\000\001\000AE", 8);
  809. /* AES vendor version AE-2 does not store a CRC.
  810. * WinZip 11 uses AE-1, which does store the CRC,
  811. * but it does not store the CRC when the file size
  812. * is less than 20 bytes. So we simulate what
  813. * WinZip 11 does.
  814. * NOTE: WinZip 9.0 and 10.0 uses AE-2 by default. */
  815. if (archive_entry_size_is_set(zip->entry)
  816. && archive_entry_size(zip->entry) < 20) {
  817. archive_le16enc(e+4, AES_VENDOR_AE_2);
  818. zip->aes_vendor = AES_VENDOR_AE_2;/* no CRC. */
  819. } else
  820. zip->aes_vendor = AES_VENDOR_AE_1;
  821. e += 8;
  822. /* AES encryption strength. */
  823. *e++ = (zip->entry_encryption == ENCRYPTION_WINZIP_AES128)?1:3;
  824. /* Actual compression method. */
  825. archive_le16enc(e, zip->entry_compression);
  826. e += 2;
  827. }
  828. /* Copy UT ,ux, and AES-extra into central directory as well. */
  829. zip->file_header_extra_offset = zip->central_directory_bytes;
  830. cd_extra = cd_alloc(zip, e - local_extra);
  831. memcpy(cd_extra, local_extra, e - local_extra);
  832. /*
  833. * Following extra blocks vary between local header and
  834. * central directory. These are the local header versions.
  835. * Central directory versions get formatted in
  836. * archive_write_zip_finish_entry() below.
  837. */
  838. /* "[Zip64 entry] in the local header MUST include BOTH
  839. * original [uncompressed] and compressed size fields." */
  840. if (zip->entry_uses_zip64) {
  841. unsigned char *zip64_start = e;
  842. memcpy(e, "\001\000\020\000", 4);
  843. e += 4;
  844. archive_le64enc(e, zip->entry_uncompressed_size);
  845. e += 8;
  846. archive_le64enc(e, zip->entry_compressed_size);
  847. e += 8;
  848. archive_le16enc(zip64_start + 2, (uint16_t)(e - (zip64_start + 4)));
  849. }
  850. if (zip->flags & ZIP_FLAG_EXPERIMENT_xl) {
  851. /* Experimental 'xl' extension to improve streaming. */
  852. unsigned char *external_info = e;
  853. int included = 7;
  854. memcpy(e, "xl\000\000", 4); // 0x6c65 + 2-byte length
  855. e += 4;
  856. e[0] = included; /* bitmap of included fields */
  857. e += 1;
  858. if (included & 1) {
  859. archive_le16enc(e, /* "Version created by" */
  860. 3 * 256 + version_needed);
  861. e += 2;
  862. }
  863. if (included & 2) {
  864. archive_le16enc(e, 0); /* internal file attributes */
  865. e += 2;
  866. }
  867. if (included & 4) {
  868. archive_le32enc(e, /* external file attributes */
  869. ((uint32_t)archive_entry_mode(zip->entry)) << 16);
  870. e += 4;
  871. }
  872. if (included & 8) {
  873. // Libarchive does not currently support file comments.
  874. }
  875. archive_le16enc(external_info + 2, (uint16_t)(e - (external_info + 4)));
  876. }
  877. /* Update local header with size of extra data and write it all out: */
  878. archive_le16enc(local_header + 28, (uint16_t)(e - local_extra));
  879. ret = __archive_write_output(a, local_header, 30);
  880. if (ret != ARCHIVE_OK)
  881. return (ARCHIVE_FATAL);
  882. zip->written_bytes += 30;
  883. ret = write_path(zip->entry, a);
  884. if (ret <= ARCHIVE_OK)
  885. return (ARCHIVE_FATAL);
  886. zip->written_bytes += ret;
  887. ret = __archive_write_output(a, local_extra, e - local_extra);
  888. if (ret != ARCHIVE_OK)
  889. return (ARCHIVE_FATAL);
  890. zip->written_bytes += e - local_extra;
  891. /* For symlinks, write the body now. */
  892. if (slink != NULL) {
  893. ret = __archive_write_output(a, slink, slink_size);
  894. if (ret != ARCHIVE_OK)
  895. return (ARCHIVE_FATAL);
  896. zip->entry_compressed_written += slink_size;
  897. zip->entry_uncompressed_written += slink_size;
  898. zip->written_bytes += slink_size;
  899. }
  900. #ifdef HAVE_ZLIB_H
  901. if (zip->entry_compression == COMPRESSION_DEFLATE) {
  902. zip->stream.zalloc = Z_NULL;
  903. zip->stream.zfree = Z_NULL;
  904. zip->stream.opaque = Z_NULL;
  905. zip->stream.next_out = zip->buf;
  906. zip->stream.avail_out = (uInt)zip->len_buf;
  907. if (deflateInit2(&zip->stream, zip->deflate_compression_level,
  908. Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY) != Z_OK) {
  909. archive_set_error(&a->archive, ENOMEM,
  910. "Can't init deflate compressor");
  911. return (ARCHIVE_FATAL);
  912. }
  913. }
  914. #endif
  915. return (ret2);
  916. }
  917. static ssize_t
  918. archive_write_zip_data(struct archive_write *a, const void *buff, size_t s)
  919. {
  920. int ret;
  921. struct zip *zip = a->format_data;
  922. if ((int64_t)s > zip->entry_uncompressed_limit)
  923. s = (size_t)zip->entry_uncompressed_limit;
  924. zip->entry_uncompressed_written += s;
  925. if (s == 0) return 0;
  926. if (zip->entry_flags & ZIP_ENTRY_FLAG_ENCRYPTED) {
  927. switch (zip->entry_encryption) {
  928. case ENCRYPTION_TRADITIONAL:
  929. /* Initialize traditional PKWARE encryption context. */
  930. if (!zip->tctx_valid) {
  931. ret = init_traditional_pkware_encryption(a);
  932. if (ret != ARCHIVE_OK)
  933. return (ret);
  934. zip->tctx_valid = 1;
  935. }
  936. break;
  937. case ENCRYPTION_WINZIP_AES128:
  938. case ENCRYPTION_WINZIP_AES256:
  939. if (!zip->cctx_valid) {
  940. ret = init_winzip_aes_encryption(a);
  941. if (ret != ARCHIVE_OK)
  942. return (ret);
  943. zip->cctx_valid = zip->hctx_valid = 1;
  944. }
  945. break;
  946. case ENCRYPTION_NONE:
  947. default:
  948. break;
  949. }
  950. }
  951. switch (zip->entry_compression) {
  952. case COMPRESSION_STORE:
  953. if (zip->tctx_valid || zip->cctx_valid) {
  954. const uint8_t *rb = (const uint8_t *)buff;
  955. const uint8_t * const re = rb + s;
  956. while (rb < re) {
  957. size_t l;
  958. if (zip->tctx_valid) {
  959. l = trad_enc_encrypt_update(&zip->tctx,
  960. rb, re - rb,
  961. zip->buf, zip->len_buf);
  962. } else {
  963. l = zip->len_buf;
  964. ret = archive_encrypto_aes_ctr_update(
  965. &zip->cctx,
  966. rb, re - rb, zip->buf, &l);
  967. if (ret < 0) {
  968. archive_set_error(&a->archive,
  969. ARCHIVE_ERRNO_MISC,
  970. "Failed to encrypt file");
  971. return (ARCHIVE_FAILED);
  972. }
  973. archive_hmac_sha1_update(&zip->hctx,
  974. zip->buf, l);
  975. }
  976. ret = __archive_write_output(a, zip->buf, l);
  977. if (ret != ARCHIVE_OK)
  978. return (ret);
  979. zip->entry_compressed_written += l;
  980. zip->written_bytes += l;
  981. rb += l;
  982. }
  983. } else {
  984. ret = __archive_write_output(a, buff, s);
  985. if (ret != ARCHIVE_OK)
  986. return (ret);
  987. zip->written_bytes += s;
  988. zip->entry_compressed_written += s;
  989. }
  990. break;
  991. #if HAVE_ZLIB_H
  992. case COMPRESSION_DEFLATE:
  993. zip->stream.next_in = (unsigned char*)(uintptr_t)buff;
  994. zip->stream.avail_in = (uInt)s;
  995. do {
  996. ret = deflate(&zip->stream, Z_NO_FLUSH);
  997. if (ret == Z_STREAM_ERROR)
  998. return (ARCHIVE_FATAL);
  999. if (zip->stream.avail_out == 0) {
  1000. if (zip->tctx_valid) {
  1001. trad_enc_encrypt_update(&zip->tctx,
  1002. zip->buf, zip->len_buf,
  1003. zip->buf, zip->len_buf);
  1004. } else if (zip->cctx_valid) {
  1005. size_t outl = zip->len_buf;
  1006. ret = archive_encrypto_aes_ctr_update(
  1007. &zip->cctx,
  1008. zip->buf, zip->len_buf,
  1009. zip->buf, &outl);
  1010. if (ret < 0) {
  1011. archive_set_error(&a->archive,
  1012. ARCHIVE_ERRNO_MISC,
  1013. "Failed to encrypt file");
  1014. return (ARCHIVE_FAILED);
  1015. }
  1016. archive_hmac_sha1_update(&zip->hctx,
  1017. zip->buf, zip->len_buf);
  1018. }
  1019. ret = __archive_write_output(a, zip->buf,
  1020. zip->len_buf);
  1021. if (ret != ARCHIVE_OK)
  1022. return (ret);
  1023. zip->entry_compressed_written += zip->len_buf;
  1024. zip->written_bytes += zip->len_buf;
  1025. zip->stream.next_out = zip->buf;
  1026. zip->stream.avail_out = (uInt)zip->len_buf;
  1027. }
  1028. } while (zip->stream.avail_in != 0);
  1029. break;
  1030. #endif
  1031. case COMPRESSION_UNSPECIFIED:
  1032. default:
  1033. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1034. "Invalid ZIP compression type");
  1035. return ARCHIVE_FATAL;
  1036. }
  1037. zip->entry_uncompressed_limit -= s;
  1038. if (!zip->cctx_valid || zip->aes_vendor != AES_VENDOR_AE_2)
  1039. zip->entry_crc32 =
  1040. zip->crc32func(zip->entry_crc32, buff, (unsigned)s);
  1041. return (s);
  1042. }
  1043. static int
  1044. archive_write_zip_finish_entry(struct archive_write *a)
  1045. {
  1046. struct zip *zip = a->format_data;
  1047. int ret;
  1048. #if HAVE_ZLIB_H
  1049. if (zip->entry_compression == COMPRESSION_DEFLATE) {
  1050. for (;;) {
  1051. size_t remainder;
  1052. ret = deflate(&zip->stream, Z_FINISH);
  1053. if (ret == Z_STREAM_ERROR)
  1054. return (ARCHIVE_FATAL);
  1055. remainder = zip->len_buf - zip->stream.avail_out;
  1056. if (zip->tctx_valid) {
  1057. trad_enc_encrypt_update(&zip->tctx,
  1058. zip->buf, remainder, zip->buf, remainder);
  1059. } else if (zip->cctx_valid) {
  1060. size_t outl = remainder;
  1061. ret = archive_encrypto_aes_ctr_update(
  1062. &zip->cctx, zip->buf, remainder,
  1063. zip->buf, &outl);
  1064. if (ret < 0) {
  1065. archive_set_error(&a->archive,
  1066. ARCHIVE_ERRNO_MISC,
  1067. "Failed to encrypt file");
  1068. return (ARCHIVE_FAILED);
  1069. }
  1070. archive_hmac_sha1_update(&zip->hctx,
  1071. zip->buf, remainder);
  1072. }
  1073. ret = __archive_write_output(a, zip->buf, remainder);
  1074. if (ret != ARCHIVE_OK)
  1075. return (ret);
  1076. zip->entry_compressed_written += remainder;
  1077. zip->written_bytes += remainder;
  1078. zip->stream.next_out = zip->buf;
  1079. if (zip->stream.avail_out != 0)
  1080. break;
  1081. zip->stream.avail_out = (uInt)zip->len_buf;
  1082. }
  1083. deflateEnd(&zip->stream);
  1084. }
  1085. #endif
  1086. if (zip->hctx_valid) {
  1087. uint8_t hmac[20];
  1088. size_t hmac_len = 20;
  1089. archive_hmac_sha1_final(&zip->hctx, hmac, &hmac_len);
  1090. ret = __archive_write_output(a, hmac, AUTH_CODE_SIZE);
  1091. if (ret != ARCHIVE_OK)
  1092. return (ret);
  1093. zip->entry_compressed_written += AUTH_CODE_SIZE;
  1094. zip->written_bytes += AUTH_CODE_SIZE;
  1095. }
  1096. /* Write trailing data descriptor. */
  1097. if ((zip->entry_flags & ZIP_ENTRY_FLAG_LENGTH_AT_END) != 0) {
  1098. char d[24];
  1099. memcpy(d, "PK\007\010", 4);
  1100. if (zip->cctx_valid && zip->aes_vendor == AES_VENDOR_AE_2)
  1101. archive_le32enc(d + 4, 0);/* no CRC.*/
  1102. else
  1103. archive_le32enc(d + 4, zip->entry_crc32);
  1104. if (zip->entry_uses_zip64) {
  1105. archive_le64enc(d + 8,
  1106. (uint64_t)zip->entry_compressed_written);
  1107. archive_le64enc(d + 16,
  1108. (uint64_t)zip->entry_uncompressed_written);
  1109. ret = __archive_write_output(a, d, 24);
  1110. zip->written_bytes += 24;
  1111. } else {
  1112. archive_le32enc(d + 8,
  1113. (uint32_t)zip->entry_compressed_written);
  1114. archive_le32enc(d + 12,
  1115. (uint32_t)zip->entry_uncompressed_written);
  1116. ret = __archive_write_output(a, d, 16);
  1117. zip->written_bytes += 16;
  1118. }
  1119. if (ret != ARCHIVE_OK)
  1120. return (ARCHIVE_FATAL);
  1121. }
  1122. /* Append Zip64 extra data to central directory information. */
  1123. if (zip->entry_compressed_written > ZIP_4GB_MAX
  1124. || zip->entry_uncompressed_written > ZIP_4GB_MAX
  1125. || zip->entry_offset > ZIP_4GB_MAX) {
  1126. unsigned char zip64[32];
  1127. unsigned char *z = zip64, *zd;
  1128. memcpy(z, "\001\000\000\000", 4);
  1129. z += 4;
  1130. if (zip->entry_uncompressed_written >= ZIP_4GB_MAX) {
  1131. archive_le64enc(z, zip->entry_uncompressed_written);
  1132. z += 8;
  1133. }
  1134. if (zip->entry_compressed_written >= ZIP_4GB_MAX) {
  1135. archive_le64enc(z, zip->entry_compressed_written);
  1136. z += 8;
  1137. }
  1138. if (zip->entry_offset >= ZIP_4GB_MAX) {
  1139. archive_le64enc(z, zip->entry_offset);
  1140. z += 8;
  1141. }
  1142. archive_le16enc(zip64 + 2, (uint16_t)(z - (zip64 + 4)));
  1143. zd = cd_alloc(zip, z - zip64);
  1144. if (zd == NULL) {
  1145. archive_set_error(&a->archive, ENOMEM,
  1146. "Can't allocate zip data");
  1147. return (ARCHIVE_FATAL);
  1148. }
  1149. memcpy(zd, zip64, z - zip64);
  1150. /* Zip64 means version needs to be set to at least 4.5 */
  1151. if (archive_le16dec(zip->file_header + 6) < 45)
  1152. archive_le16enc(zip->file_header + 6, 45);
  1153. }
  1154. /* Fix up central directory file header. */
  1155. if (zip->cctx_valid && zip->aes_vendor == AES_VENDOR_AE_2)
  1156. archive_le32enc(zip->file_header + 16, 0);/* no CRC.*/
  1157. else
  1158. archive_le32enc(zip->file_header + 16, zip->entry_crc32);
  1159. archive_le32enc(zip->file_header + 20,
  1160. (uint32_t)zipmin(zip->entry_compressed_written,
  1161. ZIP_4GB_MAX));
  1162. archive_le32enc(zip->file_header + 24,
  1163. (uint32_t)zipmin(zip->entry_uncompressed_written,
  1164. ZIP_4GB_MAX));
  1165. archive_le16enc(zip->file_header + 30,
  1166. (uint16_t)(zip->central_directory_bytes - zip->file_header_extra_offset));
  1167. archive_le32enc(zip->file_header + 42,
  1168. (uint32_t)zipmin(zip->entry_offset,
  1169. ZIP_4GB_MAX));
  1170. return (ARCHIVE_OK);
  1171. }
  1172. static int
  1173. archive_write_zip_close(struct archive_write *a)
  1174. {
  1175. uint8_t buff[64];
  1176. int64_t offset_start, offset_end;
  1177. struct zip *zip = a->format_data;
  1178. struct cd_segment *segment;
  1179. int ret;
  1180. offset_start = zip->written_bytes;
  1181. segment = zip->central_directory;
  1182. while (segment != NULL) {
  1183. ret = __archive_write_output(a,
  1184. segment->buff, segment->p - segment->buff);
  1185. if (ret != ARCHIVE_OK)
  1186. return (ARCHIVE_FATAL);
  1187. zip->written_bytes += segment->p - segment->buff;
  1188. segment = segment->next;
  1189. }
  1190. offset_end = zip->written_bytes;
  1191. /* If central dir info is too large, write Zip64 end-of-cd */
  1192. if (offset_end - offset_start > ZIP_4GB_MAX
  1193. || offset_start > ZIP_4GB_MAX
  1194. || zip->central_directory_entries > 0xffffUL
  1195. || (zip->flags & ZIP_FLAG_FORCE_ZIP64)) {
  1196. /* Zip64 end-of-cd record */
  1197. memset(buff, 0, 56);
  1198. memcpy(buff, "PK\006\006", 4);
  1199. archive_le64enc(buff + 4, 44);
  1200. archive_le16enc(buff + 12, 45);
  1201. archive_le16enc(buff + 14, 45);
  1202. /* This is disk 0 of 0. */
  1203. archive_le64enc(buff + 24, zip->central_directory_entries);
  1204. archive_le64enc(buff + 32, zip->central_directory_entries);
  1205. archive_le64enc(buff + 40, offset_end - offset_start);
  1206. archive_le64enc(buff + 48, offset_start);
  1207. ret = __archive_write_output(a, buff, 56);
  1208. if (ret != ARCHIVE_OK)
  1209. return (ARCHIVE_FATAL);
  1210. zip->written_bytes += 56;
  1211. /* Zip64 end-of-cd locator record. */
  1212. memset(buff, 0, 20);
  1213. memcpy(buff, "PK\006\007", 4);
  1214. archive_le32enc(buff + 4, 0);
  1215. archive_le64enc(buff + 8, offset_end);
  1216. archive_le32enc(buff + 16, 1);
  1217. ret = __archive_write_output(a, buff, 20);
  1218. if (ret != ARCHIVE_OK)
  1219. return (ARCHIVE_FATAL);
  1220. zip->written_bytes += 20;
  1221. }
  1222. /* Format and write end of central directory. */
  1223. memset(buff, 0, sizeof(buff));
  1224. memcpy(buff, "PK\005\006", 4);
  1225. archive_le16enc(buff + 8, (uint16_t)zipmin(0xffffU,
  1226. zip->central_directory_entries));
  1227. archive_le16enc(buff + 10, (uint16_t)zipmin(0xffffU,
  1228. zip->central_directory_entries));
  1229. archive_le32enc(buff + 12,
  1230. (uint32_t)zipmin(ZIP_4GB_MAX, (offset_end - offset_start)));
  1231. archive_le32enc(buff + 16,
  1232. (uint32_t)zipmin(ZIP_4GB_MAX, offset_start));
  1233. ret = __archive_write_output(a, buff, 22);
  1234. if (ret != ARCHIVE_OK)
  1235. return (ARCHIVE_FATAL);
  1236. zip->written_bytes += 22;
  1237. return (ARCHIVE_OK);
  1238. }
  1239. static int
  1240. archive_write_zip_free(struct archive_write *a)
  1241. {
  1242. struct zip *zip;
  1243. struct cd_segment *segment;
  1244. zip = a->format_data;
  1245. while (zip->central_directory != NULL) {
  1246. segment = zip->central_directory;
  1247. zip->central_directory = segment->next;
  1248. free(segment->buff);
  1249. free(segment);
  1250. }
  1251. free(zip->buf);
  1252. archive_entry_free(zip->entry);
  1253. if (zip->cctx_valid)
  1254. archive_encrypto_aes_ctr_release(&zip->cctx);
  1255. if (zip->hctx_valid)
  1256. archive_hmac_sha1_cleanup(&zip->hctx);
  1257. /* TODO: Free opt_sconv, sconv_default */
  1258. free(zip);
  1259. a->format_data = NULL;
  1260. return (ARCHIVE_OK);
  1261. }
  1262. /* Convert into MSDOS-style date/time. */
  1263. static unsigned int
  1264. dos_time(const time_t unix_time)
  1265. {
  1266. struct tm *t;
  1267. unsigned int dt;
  1268. #if defined(HAVE_LOCALTIME_R) || defined(HAVE_LOCALTIME_S)
  1269. struct tm tmbuf;
  1270. #endif
  1271. #if defined(HAVE_LOCALTIME_S)
  1272. t = localtime_s(&tmbuf, &unix_time) ? NULL : &tmbuf;
  1273. #elif defined(HAVE_LOCALTIME_R)
  1274. t = localtime_r(&unix_time, &tmbuf);
  1275. #else
  1276. t = localtime(&unix_time);
  1277. #endif
  1278. /* MSDOS-style date/time is only between 1980-01-01 and 2107-12-31 */
  1279. if (t->tm_year < 1980 - 1900)
  1280. /* Set minimum date/time '1980-01-01 00:00:00'. */
  1281. dt = 0x00210000U;
  1282. else if (t->tm_year > 2107 - 1900)
  1283. /* Set maximum date/time '2107-12-31 23:59:58'. */
  1284. dt = 0xff9fbf7dU;
  1285. else {
  1286. dt = 0;
  1287. dt += ((t->tm_year - 80) & 0x7f) << 9;
  1288. dt += ((t->tm_mon + 1) & 0x0f) << 5;
  1289. dt += (t->tm_mday & 0x1f);
  1290. dt <<= 16;
  1291. dt += (t->tm_hour & 0x1f) << 11;
  1292. dt += (t->tm_min & 0x3f) << 5;
  1293. dt += (t->tm_sec & 0x3e) >> 1; /* Only counting every 2 seconds. */
  1294. }
  1295. return dt;
  1296. }
  1297. static size_t
  1298. path_length(struct archive_entry *entry)
  1299. {
  1300. mode_t type;
  1301. const char *path;
  1302. size_t len;
  1303. type = archive_entry_filetype(entry);
  1304. path = archive_entry_pathname(entry);
  1305. if (path == NULL)
  1306. return (0);
  1307. len = strlen(path);
  1308. if (type == AE_IFDIR && (path[0] == '\0' || path[len - 1] != '/'))
  1309. ++len; /* Space for the trailing / */
  1310. return len;
  1311. }
  1312. static int
  1313. write_path(struct archive_entry *entry, struct archive_write *archive)
  1314. {
  1315. int ret;
  1316. const char *path;
  1317. mode_t type;
  1318. size_t written_bytes;
  1319. path = archive_entry_pathname(entry);
  1320. type = archive_entry_filetype(entry);
  1321. written_bytes = 0;
  1322. if (path == NULL)
  1323. return (ARCHIVE_FATAL);
  1324. ret = __archive_write_output(archive, path, strlen(path));
  1325. if (ret != ARCHIVE_OK)
  1326. return (ARCHIVE_FATAL);
  1327. written_bytes += strlen(path);
  1328. /* Folders are recognized by a trailing slash. */
  1329. if ((type == AE_IFDIR) & (path[strlen(path) - 1] != '/')) {
  1330. ret = __archive_write_output(archive, "/", 1);
  1331. if (ret != ARCHIVE_OK)
  1332. return (ARCHIVE_FATAL);
  1333. written_bytes += 1;
  1334. }
  1335. return ((int)written_bytes);
  1336. }
  1337. static void
  1338. copy_path(struct archive_entry *entry, unsigned char *p)
  1339. {
  1340. const char *path;
  1341. size_t pathlen;
  1342. mode_t type;
  1343. path = archive_entry_pathname(entry);
  1344. pathlen = strlen(path);
  1345. type = archive_entry_filetype(entry);
  1346. memcpy(p, path, pathlen);
  1347. /* Folders are recognized by a trailing slash. */
  1348. if ((type == AE_IFDIR) && (path[pathlen - 1] != '/'))
  1349. p[pathlen] = '/';
  1350. }
  1351. static struct archive_string_conv *
  1352. get_sconv(struct archive_write *a, struct zip *zip)
  1353. {
  1354. if (zip->opt_sconv != NULL)
  1355. return (zip->opt_sconv);
  1356. if (!zip->init_default_conversion) {
  1357. zip->sconv_default =
  1358. archive_string_default_conversion_for_write(&(a->archive));
  1359. zip->init_default_conversion = 1;
  1360. }
  1361. return (zip->sconv_default);
  1362. }
  1363. /*
  1364. Traditional PKWARE Decryption functions.
  1365. */
  1366. static void
  1367. trad_enc_update_keys(struct trad_enc_ctx *ctx, uint8_t c)
  1368. {
  1369. uint8_t t;
  1370. #define CRC32(c, b) (crc32(c ^ 0xffffffffUL, &b, 1) ^ 0xffffffffUL)
  1371. ctx->keys[0] = CRC32(ctx->keys[0], c);
  1372. ctx->keys[1] = (ctx->keys[1] + (ctx->keys[0] & 0xff)) * 134775813L + 1;
  1373. t = (ctx->keys[1] >> 24) & 0xff;
  1374. ctx->keys[2] = CRC32(ctx->keys[2], t);
  1375. #undef CRC32
  1376. }
  1377. static uint8_t
  1378. trad_enc_decrypt_byte(struct trad_enc_ctx *ctx)
  1379. {
  1380. unsigned temp = ctx->keys[2] | 2;
  1381. return (uint8_t)((temp * (temp ^ 1)) >> 8) & 0xff;
  1382. }
  1383. static unsigned
  1384. trad_enc_encrypt_update(struct trad_enc_ctx *ctx, const uint8_t *in,
  1385. size_t in_len, uint8_t *out, size_t out_len)
  1386. {
  1387. unsigned i, max;
  1388. max = (unsigned)((in_len < out_len)? in_len: out_len);
  1389. for (i = 0; i < max; i++) {
  1390. uint8_t t = in[i];
  1391. out[i] = t ^ trad_enc_decrypt_byte(ctx);
  1392. trad_enc_update_keys(ctx, t);
  1393. }
  1394. return i;
  1395. }
  1396. static int
  1397. trad_enc_init(struct trad_enc_ctx *ctx, const char *pw, size_t pw_len)
  1398. {
  1399. ctx->keys[0] = 305419896L;
  1400. ctx->keys[1] = 591751049L;
  1401. ctx->keys[2] = 878082192L;
  1402. for (;pw_len; --pw_len)
  1403. trad_enc_update_keys(ctx, *pw++);
  1404. return 0;
  1405. }
  1406. static int
  1407. is_traditional_pkware_encryption_supported(void)
  1408. {
  1409. uint8_t key[TRAD_HEADER_SIZE];
  1410. if (archive_random(key, sizeof(key)-1) != ARCHIVE_OK)
  1411. return (0);
  1412. return (1);
  1413. }
  1414. static int
  1415. init_traditional_pkware_encryption(struct archive_write *a)
  1416. {
  1417. struct zip *zip = a->format_data;
  1418. const char *passphrase;
  1419. uint8_t key[TRAD_HEADER_SIZE];
  1420. uint8_t key_encrypted[TRAD_HEADER_SIZE];
  1421. int ret;
  1422. passphrase = __archive_write_get_passphrase(a);
  1423. if (passphrase == NULL) {
  1424. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1425. "Encryption needs passphrase");
  1426. return ARCHIVE_FAILED;
  1427. }
  1428. if (archive_random(key, sizeof(key)-1) != ARCHIVE_OK) {
  1429. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1430. "Can't generate random number for encryption");
  1431. return ARCHIVE_FATAL;
  1432. }
  1433. trad_enc_init(&zip->tctx, passphrase, strlen(passphrase));
  1434. /* Set the last key code which will be used as a check code
  1435. * for verifying passphrase in decryption. */
  1436. key[TRAD_HEADER_SIZE-1] = zip->trad_chkdat;
  1437. trad_enc_encrypt_update(&zip->tctx, key, TRAD_HEADER_SIZE,
  1438. key_encrypted, TRAD_HEADER_SIZE);
  1439. /* Write encrypted keys in the top of the file content. */
  1440. ret = __archive_write_output(a, key_encrypted, TRAD_HEADER_SIZE);
  1441. if (ret != ARCHIVE_OK)
  1442. return (ret);
  1443. zip->written_bytes += TRAD_HEADER_SIZE;
  1444. zip->entry_compressed_written += TRAD_HEADER_SIZE;
  1445. return (ret);
  1446. }
  1447. static int
  1448. init_winzip_aes_encryption(struct archive_write *a)
  1449. {
  1450. struct zip *zip = a->format_data;
  1451. const char *passphrase;
  1452. size_t key_len, salt_len;
  1453. uint8_t salt[16 + 2];
  1454. uint8_t derived_key[MAX_DERIVED_KEY_BUF_SIZE];
  1455. int ret;
  1456. passphrase = __archive_write_get_passphrase(a);
  1457. if (passphrase == NULL) {
  1458. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1459. "Encryption needs passphrase");
  1460. return (ARCHIVE_FAILED);
  1461. }
  1462. if (zip->entry_encryption == ENCRYPTION_WINZIP_AES128) {
  1463. salt_len = 8;
  1464. key_len = 16;
  1465. } else {
  1466. /* AES 256 */
  1467. salt_len = 16;
  1468. key_len = 32;
  1469. }
  1470. if (archive_random(salt, salt_len) != ARCHIVE_OK) {
  1471. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1472. "Can't generate random number for encryption");
  1473. return (ARCHIVE_FATAL);
  1474. }
  1475. archive_pbkdf2_sha1(passphrase, strlen(passphrase),
  1476. salt, salt_len, 1000, derived_key, key_len * 2 + 2);
  1477. ret = archive_encrypto_aes_ctr_init(&zip->cctx, derived_key, key_len);
  1478. if (ret != 0) {
  1479. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1480. "Decryption is unsupported due to lack of crypto library");
  1481. return (ARCHIVE_FAILED);
  1482. }
  1483. ret = archive_hmac_sha1_init(&zip->hctx, derived_key + key_len,
  1484. key_len);
  1485. if (ret != 0) {
  1486. archive_encrypto_aes_ctr_release(&zip->cctx);
  1487. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1488. "Failed to initialize HMAC-SHA1");
  1489. return (ARCHIVE_FAILED);
  1490. }
  1491. /* Set a password verification value after the 'salt'. */
  1492. salt[salt_len] = derived_key[key_len * 2];
  1493. salt[salt_len + 1] = derived_key[key_len * 2 + 1];
  1494. /* Write encrypted keys in the top of the file content. */
  1495. ret = __archive_write_output(a, salt, salt_len + 2);
  1496. if (ret != ARCHIVE_OK)
  1497. return (ret);
  1498. zip->written_bytes += salt_len + 2;
  1499. zip->entry_compressed_written += salt_len + 2;
  1500. return (ARCHIVE_OK);
  1501. }
  1502. static int
  1503. is_winzip_aes_encryption_supported(int encryption)
  1504. {
  1505. size_t key_len, salt_len;
  1506. uint8_t salt[16 + 2];
  1507. uint8_t derived_key[MAX_DERIVED_KEY_BUF_SIZE];
  1508. archive_crypto_ctx cctx;
  1509. archive_hmac_sha1_ctx hctx;
  1510. int ret;
  1511. if (encryption == ENCRYPTION_WINZIP_AES128) {
  1512. salt_len = 8;
  1513. key_len = 16;
  1514. } else {
  1515. /* AES 256 */
  1516. salt_len = 16;
  1517. key_len = 32;
  1518. }
  1519. if (archive_random(salt, salt_len) != ARCHIVE_OK)
  1520. return (0);
  1521. ret = archive_pbkdf2_sha1("p", 1, salt, salt_len, 1000,
  1522. derived_key, key_len * 2 + 2);
  1523. if (ret != 0)
  1524. return (0);
  1525. ret = archive_encrypto_aes_ctr_init(&cctx, derived_key, key_len);
  1526. if (ret != 0)
  1527. return (0);
  1528. ret = archive_hmac_sha1_init(&hctx, derived_key + key_len,
  1529. key_len);
  1530. archive_encrypto_aes_ctr_release(&cctx);
  1531. if (ret != 0)
  1532. return (0);
  1533. archive_hmac_sha1_cleanup(&hctx);
  1534. return (1);
  1535. }