archive_write_set_format_zip.c 50 KB

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