archive_write_set_format_zip.c 48 KB

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