2 * Copyright (c) 2008 Anselm Strauss
3 * Copyright (c) 2009 Joerg Sonnenberger
4 * Copyright (c) 2011-2012,2014 Michihiro NAKAJIMA
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
29 * Development supported by Google Summer of Code 2008.
32 #include "archive_platform.h"
33 __FBSDID("$FreeBSD$");
38 #ifdef HAVE_LANGINFO_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"
63 #include "archive_crc32.h"
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)
70 #define ZIP_4GB_MAX ARCHIVE_LITERAL_LL(0xffffffff)
71 #define ZIP_4GB_MAX_UNCOMPRESSED ARCHIVE_LITERAL_LL(0xff000000)
74 COMPRESSION_UNSPECIFIED = -1,
75 COMPRESSION_STORE = 0,
76 COMPRESSION_DEFLATE = 8
80 #define COMPRESSION_DEFAULT COMPRESSION_DEFLATE
82 #define COMPRESSION_DEFAULT COMPRESSION_STORE
87 ENCRYPTION_TRADITIONAL, /* Traditional PKWARE encryption. */
88 ENCRYPTION_WINZIP_AES128, /* WinZIP AES-128 encryption. */
89 ENCRYPTION_WINZIP_AES256, /* WinZIP AES-256 encryption. */
92 #define TRAD_HEADER_SIZE 12
94 * See "WinZip - AES Encryption Information"
95 * http://www.winzip.com/aes_info.htm
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
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
109 #define MAX_DERIVED_KEY_BUF_SIZE (AES_MAX_KEY_SIZE * 2 + 2)
112 struct cd_segment *next;
118 struct trad_enc_ctx {
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;
135 int entry_uses_zip64;
137 struct trad_enc_ctx tctx;
139 unsigned char trad_chkdat;
141 archive_crypto_ctx cctx;
143 archive_hmac_sha1_ctx hctx;
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);
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;
155 int64_t written_bytes; /* Overall position in file. */
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;
164 #define ZIP_FLAG_AVOID_ZIP64 1
165 #define ZIP_FLAG_FORCE_ZIP64 2
166 #define ZIP_FLAG_EXPERIMENT_xl 4
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))
180 static ssize_t archive_write_zip_data(struct archive_write *,
181 const void *buff, size_t s);
182 static int archive_write_zip_close(struct archive_write *);
183 static int archive_write_zip_free(struct archive_write *);
184 static int archive_write_zip_finish_entry(struct archive_write *);
185 static int archive_write_zip_header(struct archive_write *,
186 struct archive_entry *);
187 static int archive_write_zip_options(struct archive_write *,
188 const char *, const char *);
189 static unsigned int dos_time(const time_t);
190 static size_t path_length(struct archive_entry *);
191 static int write_path(struct archive_entry *, struct archive_write *);
192 static void copy_path(struct archive_entry *, unsigned char *);
193 static struct archive_string_conv *get_sconv(struct archive_write *, struct zip *);
194 static int trad_enc_init(struct trad_enc_ctx *, const char *, size_t);
195 static unsigned trad_enc_encrypt_update(struct trad_enc_ctx *, const uint8_t *,
196 size_t, uint8_t *, size_t);
197 static int init_traditional_pkware_encryption(struct archive_write *);
198 static int is_traditional_pkware_encryption_supported(void);
199 static int init_winzip_aes_encryption(struct archive_write *);
200 static int is_winzip_aes_encryption_supported(int encryption);
202 static unsigned char *
203 cd_alloc(struct zip *zip, size_t length)
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));
213 segment->buff_size = 64 * 1024;
214 segment->buff = malloc(segment->buff_size);
215 if (segment->buff == NULL) {
219 segment->p = segment->buff;
221 if (zip->central_directory == NULL) {
222 zip->central_directory
223 = zip->central_directory_last
226 zip->central_directory_last->next = segment;
227 zip->central_directory_last = segment;
231 p = zip->central_directory_last->p;
232 zip->central_directory_last->p += length;
233 zip->central_directory_bytes += length;
238 real_crc32(unsigned long crc, const void *buff, size_t len)
240 return crc32(crc, buff, (unsigned int)len);
244 fake_crc32(unsigned long crc, const void *buff, size_t len)
246 (void)crc; /* UNUSED */
247 (void)buff; /* UNUSED */
248 (void)len; /* UNUSED */
253 archive_write_zip_options(struct archive_write *a, const char *key,
256 struct zip *zip = a->format_data;
257 int ret = ARCHIVE_FAILED;
259 if (strcmp(key, "compression") == 0) {
261 * Set compression to use on all future entries.
262 * This only affects regular files.
264 if (val == NULL || val[0] == 0) {
265 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
266 "%s: compression option needs a compression name",
268 } else if (strcmp(val, "deflate") == 0) {
270 zip->requested_compression = COMPRESSION_DEFLATE;
273 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
274 "deflate compression not supported");
276 } else if (strcmp(val, "store") == 0) {
277 zip->requested_compression = COMPRESSION_STORE;
281 } else if (strcmp(key, "compression-level") == 0) {
282 if (val == NULL || !(val[0] >= '0' && val[0] <= '9') || val[1] != '\0') {
287 zip->requested_compression = COMPRESSION_STORE;
291 zip->requested_compression = COMPRESSION_DEFLATE;
292 zip->deflate_compression_level = val[0] - '0';
295 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
296 "deflate compression not supported");
299 } else if (strcmp(key, "encryption") == 0) {
301 zip->encryption_type = ENCRYPTION_NONE;
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;
310 archive_set_error(&a->archive,
312 "encryption not supported");
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;
320 archive_set_error(&a->archive,
322 "encryption not supported");
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;
330 archive_set_error(&a->archive,
332 "encryption not supported");
335 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
336 "%s: unknown encryption '%s'",
337 a->format_name, val);
340 } else if (strcmp(key, "experimental") == 0) {
341 if (val == NULL || val[0] == 0) {
342 zip->flags &= ~ ZIP_FLAG_EXPERIMENT_xl;
344 zip->flags |= ZIP_FLAG_EXPERIMENT_xl;
347 } else if (strcmp(key, "fakecrc32") == 0) {
349 * FOR TESTING ONLY: disable CRC calculation to speed up
350 * certain complex tests.
352 if (val == NULL || val[0] == 0) {
353 zip->crc32func = real_crc32;
355 zip->crc32func = fake_crc32;
358 } else if (strcmp(key, "hdrcharset") == 0) {
360 * Set the character set used in translating filenames.
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",
367 zip->opt_sconv = archive_string_conversion_to_charset(
368 &a->archive, val, 0);
369 if (zip->opt_sconv != NULL)
375 } else if (strcmp(key, "zip64") == 0) {
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.
382 if (val != NULL && *val != '\0') {
383 zip->flags |= ZIP_FLAG_FORCE_ZIP64;
384 zip->flags &= ~ZIP_FLAG_AVOID_ZIP64;
386 zip->flags &= ~ZIP_FLAG_FORCE_ZIP64;
387 zip->flags |= ZIP_FLAG_AVOID_ZIP64;
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);
399 archive_write_zip_set_compression_deflate(struct archive *_a)
401 struct archive_write *a = (struct archive_write *)_a;
402 int ret = ARCHIVE_FAILED;
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"
414 struct zip *zip = a->format_data;
415 zip->requested_compression = COMPRESSION_DEFLATE;
418 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
419 "deflate compression not supported");
420 ret = ARCHIVE_FAILED;
427 archive_write_zip_set_compression_store(struct archive *_a)
429 struct archive_write *a = (struct archive_write *)_a;
430 struct zip *zip = a->format_data;
431 int ret = ARCHIVE_FAILED;
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"
442 zip->requested_compression = COMPRESSION_STORE;
449 archive_write_set_format_zip(struct archive *_a)
451 struct archive_write *a = (struct archive_write *)_a;
454 archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
455 ARCHIVE_STATE_NEW, "archive_write_set_format_zip");
457 /* If another format was already registered, unregister it. */
458 if (a->format_free != NULL)
461 zip = (struct zip *) calloc(1, sizeof(*zip));
463 archive_set_error(&a->archive, ENOMEM,
464 "Can't allocate zip data");
465 return (ARCHIVE_FATAL);
468 /* "Unspecified" lets us choose the appropriate compression. */
469 zip->requested_compression = COMPRESSION_UNSPECIFIED;
471 zip->deflate_compression_level = Z_DEFAULT_COMPRESSION;
473 zip->crc32func = real_crc32;
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) {
480 archive_set_error(&a->archive, ENOMEM,
481 "Can't allocate compression buffer");
482 return (ARCHIVE_FATAL);
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";
500 is_all_ascii(const char *p)
502 const unsigned char *pp = (const unsigned char *)p;
512 archive_write_zip_header(struct archive_write *a, struct archive_entry *entry)
514 unsigned char local_header[32];
515 unsigned char local_extra[144];
516 struct zip *zip = a->format_data;
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;
525 int version_needed = 10;
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;
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;
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;
552 /* Only regular files can have size > 0. */
553 if (type != AE_IFREG)
554 archive_entry_set_size(entry, 0);
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);
572 archive_encrypto_aes_ctr_release(&zip->cctx);
574 archive_hmac_sha1_cleanup(&zip->hctx);
575 zip->tctx_valid = zip->cctx_valid = zip->hctx_valid = 0;
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;
587 case ENCRYPTION_NONE:
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);
601 zip->entry = archive_entry_clone(entry);
603 if (zip->entry == NULL) {
604 archive_set_error(&a->archive, ENOMEM,
605 "Can't allocate zip header data");
606 return (ARCHIVE_FATAL);
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);
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));
627 archive_entry_set_pathname(zip->entry, p);
630 * There is no standard for symlink handling; we convert
631 * it using the same character-set translation that we use
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 "
640 return (ARCHIVE_FATAL);
642 /* No error if we can't convert. */
644 archive_entry_set_symlink(zip->entry, p);
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;
655 } else if (strcmp(nl_langinfo(CODESET), "UTF-8") == 0) {
656 zip->entry_flags |= ZIP_ENTRY_FLAG_UTF8_NAME;
660 filename_length = path_length(zip->entry);
662 /* Determine appropriate compression and size for this entry. */
663 if (type == AE_IFLNK) {
664 slink = archive_entry_symlink(zip->entry);
666 slink_size = strlen(slink);
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;
676 } else if (type != AE_IFREG) {
677 zip->entry_compression = COMPRESSION_STORE;
678 zip->entry_uncompressed_limit = 0;
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;
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;
689 if (zip->entry_compression == COMPRESSION_STORE) {
690 zip->entry_compressed_size = size;
691 zip->entry_uncompressed_size = size;
694 zip->entry_uncompressed_size = size;
698 if (zip->entry_flags & ZIP_ENTRY_FLAG_ENCRYPTED) {
699 switch (zip->entry_encryption) {
700 case ENCRYPTION_TRADITIONAL:
701 additional_size = TRAD_HEADER_SIZE;
704 case ENCRYPTION_WINZIP_AES128:
705 additional_size = WINZIP_AES128_HEADER_SIZE
709 case ENCRYPTION_WINZIP_AES256:
710 additional_size = WINZIP_AES256_HEADER_SIZE
714 case ENCRYPTION_NONE:
718 if (zip->entry_compression == COMPRESSION_STORE)
719 zip->entry_compressed_size += additional_size;
723 * Set Zip64 extension in any of the following cases
724 * (this was suggested by discussion on info-zip-dev
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)
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;
740 /* We may know the size, but never the CRC. */
741 zip->entry_flags |= ZIP_ENTRY_FLAG_LENGTH_AT_END;
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.
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;
753 } else if (zip->entry_compression == COMPRESSION_STORE) {
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)
767 case ENCRYPTION_NONE:
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);
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);
796 archive_le32enc(local_header + 18, (uint32_t)zip->entry_compressed_size);
797 archive_le32enc(local_header + 22, (uint32_t)zip->entry_uncompressed_size);
799 archive_le16enc(local_header + 26, (uint16_t)filename_length);
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];
805 zip->trad_chkdat = local_header[17];
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);
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);
833 /* Format extra data. */
834 memset(local_extra, 0, sizeof(local_extra));
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. */
841 /* UT timestamp, length depends on what timestamps are set. */
843 archive_le16enc(e + 2,
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));
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));
857 if (archive_entry_atime_is_set(entry)) {
858 archive_le32enc(e, (uint32_t)archive_entry_atime(entry));
861 if (archive_entry_ctime_is_set(entry)) {
862 archive_le32enc(e, (uint32_t)archive_entry_ctime(entry));
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);
870 *e++ = 4; /* Length of following UID */
871 archive_le32enc(e, (uint32_t)archive_entry_uid(entry));
873 *e++ = 4; /* Length of following GID */
874 archive_le32enc(e, (uint32_t)archive_entry_gid(entry));
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)) {
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
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. */
894 zip->aes_vendor = AES_VENDOR_AE_1;
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);
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);
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.
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);
921 archive_le64enc(e, zip->entry_uncompressed_size);
923 archive_le64enc(e, zip->entry_compressed_size);
925 archive_le16enc(zip64_start + 2, (uint16_t)(e - (zip64_start + 4)));
928 if (zip->flags & ZIP_FLAG_EXPERIMENT_xl) {
929 /* Experimental 'xl' extension to improve streaming. */
930 unsigned char *external_info = e;
932 memcpy(e, "xl\000\000", 4); // 0x6c65 + 2-byte length
934 e[0] = included; /* bitmap of included fields */
937 archive_le16enc(e, /* "Version created by" */
938 3 * 256 + version_needed);
942 archive_le16enc(e, 0); /* internal file attributes */
946 archive_le32enc(e, /* external file attributes */
947 ((uint32_t)archive_entry_mode(zip->entry)) << 16);
951 // Libarchive does not currently support file comments.
953 archive_le16enc(external_info + 2, (uint16_t)(e - (external_info + 4)));
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));
959 ret = __archive_write_output(a, local_header, 30);
960 if (ret != ARCHIVE_OK)
961 return (ARCHIVE_FATAL);
962 zip->written_bytes += 30;
964 ret = write_path(zip->entry, a);
965 if (ret <= ARCHIVE_OK)
966 return (ARCHIVE_FATAL);
967 zip->written_bytes += ret;
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;
974 /* For symlinks, write the body now. */
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;
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);
1004 archive_write_zip_data(struct archive_write *a, const void *buff, size_t s)
1007 struct zip *zip = a->format_data;
1009 if ((int64_t)s > zip->entry_uncompressed_limit)
1010 s = (size_t)zip->entry_uncompressed_limit;
1011 zip->entry_uncompressed_written += s;
1013 if (s == 0) return 0;
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)
1023 zip->tctx_valid = 1;
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)
1032 zip->cctx_valid = zip->hctx_valid = 1;
1035 case ENCRYPTION_NONE:
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;
1050 if (zip->tctx_valid) {
1051 l = trad_enc_encrypt_update(&zip->tctx,
1053 zip->buf, zip->len_buf);
1056 ret = archive_encrypto_aes_ctr_update(
1058 rb, re - rb, zip->buf, &l);
1060 archive_set_error(&a->archive,
1062 "Failed to encrypt file");
1063 return (ARCHIVE_FAILED);
1065 archive_hmac_sha1_update(&zip->hctx,
1068 ret = __archive_write_output(a, zip->buf, l);
1069 if (ret != ARCHIVE_OK)
1071 zip->entry_compressed_written += l;
1072 zip->written_bytes += l;
1076 ret = __archive_write_output(a, buff, s);
1077 if (ret != ARCHIVE_OK)
1079 zip->written_bytes += s;
1080 zip->entry_compressed_written += s;
1084 case COMPRESSION_DEFLATE:
1085 zip->stream.next_in = (unsigned char*)(uintptr_t)buff;
1086 zip->stream.avail_in = (uInt)s;
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(
1100 zip->buf, zip->len_buf,
1103 archive_set_error(&a->archive,
1105 "Failed to encrypt file");
1106 return (ARCHIVE_FAILED);
1108 archive_hmac_sha1_update(&zip->hctx,
1109 zip->buf, zip->len_buf);
1111 ret = __archive_write_output(a, zip->buf,
1113 if (ret != ARCHIVE_OK)
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;
1120 } while (zip->stream.avail_in != 0);
1124 case COMPRESSION_UNSPECIFIED:
1126 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1127 "Invalid ZIP compression type");
1128 return ARCHIVE_FATAL;
1131 zip->entry_uncompressed_limit -= s;
1132 if (!zip->cctx_valid || zip->aes_vendor != AES_VENDOR_AE_2)
1134 zip->crc32func(zip->entry_crc32, buff, (unsigned)s);
1140 archive_write_zip_finish_entry(struct archive_write *a)
1142 struct zip *zip = a->format_data;
1146 if (zip->entry_compression == COMPRESSION_DEFLATE) {
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,
1163 archive_set_error(&a->archive,
1165 "Failed to encrypt file");
1166 return (ARCHIVE_FAILED);
1168 archive_hmac_sha1_update(&zip->hctx,
1169 zip->buf, remainder);
1171 ret = __archive_write_output(a, zip->buf, remainder);
1172 if (ret != ARCHIVE_OK)
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)
1179 zip->stream.avail_out = (uInt)zip->len_buf;
1181 deflateEnd(&zip->stream);
1184 if (zip->hctx_valid) {
1186 size_t hmac_len = 20;
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)
1192 zip->entry_compressed_written += AUTH_CODE_SIZE;
1193 zip->written_bytes += AUTH_CODE_SIZE;
1196 /* Write trailing data descriptor. */
1197 if ((zip->entry_flags & ZIP_ENTRY_FLAG_LENGTH_AT_END) != 0) {
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.*/
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;
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;
1219 if (ret != ARCHIVE_OK)
1220 return (ARCHIVE_FATAL);
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);
1231 if (zip->entry_uncompressed_written >= ZIP_4GB_MAX) {
1232 archive_le64enc(z, zip->entry_uncompressed_written);
1235 if (zip->entry_compressed_written >= ZIP_4GB_MAX) {
1236 archive_le64enc(z, zip->entry_compressed_written);
1239 if (zip->entry_offset >= ZIP_4GB_MAX) {
1240 archive_le64enc(z, zip->entry_offset);
1243 archive_le16enc(zip64 + 2, (uint16_t)(z - (zip64 + 4)));
1244 zd = cd_alloc(zip, z - zip64);
1246 archive_set_error(&a->archive, ENOMEM,
1247 "Can't allocate zip data");
1248 return (ARCHIVE_FATAL);
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);
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.*/
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,
1264 archive_le32enc(zip->file_header + 24,
1265 (uint32_t)zipmin(zip->entry_uncompressed_written,
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,
1273 return (ARCHIVE_OK);
1277 archive_write_zip_close(struct archive_write *a)
1280 int64_t offset_start, offset_end;
1281 struct zip *zip = a->format_data;
1282 struct cd_segment *segment;
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;
1295 offset_end = zip->written_bytes;
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;
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;
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);
1350 archive_write_zip_free(struct archive_write *a)
1353 struct cd_segment *segment;
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);
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 */
1371 a->format_data = NULL;
1372 return (ARCHIVE_OK);
1375 /* Convert into MSDOS-style date/time. */
1377 dos_time(const time_t unix_time)
1381 #if defined(HAVE_LOCALTIME_R) || defined(HAVE__LOCALTIME64_S)
1384 #if defined(HAVE__LOCALTIME64_S)
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);
1401 t = localtime(&unix_time);
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'. */
1408 else if (t->tm_year > 2107 - 1900)
1409 /* Set maximum date/time '2107-12-31 23:59:58'. */
1413 dt += ((t->tm_year - 80) & 0x7f) << 9;
1414 dt += ((t->tm_mon + 1) & 0x0f) << 5;
1415 dt += (t->tm_mday & 0x1f);
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. */
1425 path_length(struct archive_entry *entry)
1431 type = archive_entry_filetype(entry);
1432 path = archive_entry_pathname(entry);
1437 if (type == AE_IFDIR && (path[0] == '\0' || path[len - 1] != '/'))
1438 ++len; /* Space for the trailing / */
1443 write_path(struct archive_entry *entry, struct archive_write *archive)
1448 size_t written_bytes;
1450 path = archive_entry_pathname(entry);
1451 type = archive_entry_filetype(entry);
1455 return (ARCHIVE_FATAL);
1457 ret = __archive_write_output(archive, path, strlen(path));
1458 if (ret != ARCHIVE_OK)
1459 return (ARCHIVE_FATAL);
1460 written_bytes += strlen(path);
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);
1470 return ((int)written_bytes);
1474 copy_path(struct archive_entry *entry, unsigned char *p)
1480 path = archive_entry_pathname(entry);
1481 pathlen = strlen(path);
1482 type = archive_entry_filetype(entry);
1484 memcpy(p, path, pathlen);
1486 /* Folders are recognized by a trailing slash. */
1487 if ((type == AE_IFDIR) && (path[pathlen - 1] != '/'))
1492 static struct archive_string_conv *
1493 get_sconv(struct archive_write *a, struct zip *zip)
1495 if (zip->opt_sconv != NULL)
1496 return (zip->opt_sconv);
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;
1503 return (zip->sconv_default);
1507 Traditional PKWARE Decryption functions.
1511 trad_enc_update_keys(struct trad_enc_ctx *ctx, uint8_t c)
1514 #define CRC32(c, b) (crc32(c ^ 0xffffffffUL, &b, 1) ^ 0xffffffffUL)
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);
1524 trad_enc_decrypt_byte(struct trad_enc_ctx *ctx)
1526 unsigned temp = ctx->keys[2] | 2;
1527 return (uint8_t)((temp * (temp ^ 1)) >> 8) & 0xff;
1531 trad_enc_encrypt_update(struct trad_enc_ctx *ctx, const uint8_t *in,
1532 size_t in_len, uint8_t *out, size_t out_len)
1536 max = (unsigned)((in_len < out_len)? in_len: out_len);
1538 for (i = 0; i < max; i++) {
1540 out[i] = t ^ trad_enc_decrypt_byte(ctx);
1541 trad_enc_update_keys(ctx, t);
1547 trad_enc_init(struct trad_enc_ctx *ctx, const char *pw, size_t pw_len)
1550 ctx->keys[0] = 305419896L;
1551 ctx->keys[1] = 591751049L;
1552 ctx->keys[2] = 878082192L;
1554 for (;pw_len; --pw_len)
1555 trad_enc_update_keys(ctx, *pw++);
1560 is_traditional_pkware_encryption_supported(void)
1562 uint8_t key[TRAD_HEADER_SIZE];
1564 if (archive_random(key, sizeof(key)-1) != ARCHIVE_OK)
1570 init_traditional_pkware_encryption(struct archive_write *a)
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];
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;
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;
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)
1599 zip->written_bytes += TRAD_HEADER_SIZE;
1600 zip->entry_compressed_written += TRAD_HEADER_SIZE;
1605 init_winzip_aes_encryption(struct archive_write *a)
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];
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);
1620 if (zip->entry_encryption == ENCRYPTION_WINZIP_AES128) {
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);
1633 archive_pbkdf2_sha1(passphrase, strlen(passphrase),
1634 salt, salt_len, 1000, derived_key, key_len * 2 + 2);
1636 ret = archive_encrypto_aes_ctr_init(&zip->cctx, derived_key, key_len);
1638 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1639 "Decryption is unsupported due to lack of crypto library");
1640 return (ARCHIVE_FAILED);
1642 ret = archive_hmac_sha1_init(&zip->hctx, derived_key + key_len,
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);
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];
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)
1659 zip->written_bytes += salt_len + 2;
1660 zip->entry_compressed_written += salt_len + 2;
1662 return (ARCHIVE_OK);
1666 is_winzip_aes_encryption_supported(int encryption)
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;
1675 if (encryption == ENCRYPTION_WINZIP_AES128) {
1683 if (archive_random(salt, salt_len) != ARCHIVE_OK)
1685 ret = archive_pbkdf2_sha1("p", 1, salt, salt_len, 1000,
1686 derived_key, key_len * 2 + 2);
1690 ret = archive_encrypto_aes_ctr_init(&cctx, derived_key, key_len);
1693 ret = archive_hmac_sha1_init(&hctx, derived_key + key_len,
1695 archive_encrypto_aes_ctr_release(&cctx);
1698 archive_hmac_sha1_cleanup(&hctx);