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