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"
62 #include "archive_crc32.h"
65 #define ZIP_ENTRY_FLAG_ENCRYPTED (1<<0)
66 #define ZIP_ENTRY_FLAG_LENGTH_AT_END (1<<3)
67 #define ZIP_ENTRY_FLAG_UTF8_NAME (1 << 11)
69 #define ZIP_4GB_MAX ARCHIVE_LITERAL_LL(0xffffffff)
70 #define ZIP_4GB_MAX_UNCOMPRESSED ARCHIVE_LITERAL_LL(0xff000000)
73 COMPRESSION_UNSPECIFIED = -1,
74 COMPRESSION_STORE = 0,
75 COMPRESSION_DEFLATE = 8
79 #define COMPRESSION_DEFAULT COMPRESSION_DEFLATE
81 #define COMPRESSION_DEFAULT COMPRESSION_STORE
86 ENCRYPTION_TRADITIONAL, /* Traditional PKWARE encryption. */
87 ENCRYPTION_WINZIP_AES128, /* WinZIP AES-128 encryption. */
88 ENCRYPTION_WINZIP_AES256, /* WinZIP AES-256 encryption. */
91 #define TRAD_HEADER_SIZE 12
93 * See "WinZip - AES Encryption Information"
94 * http://www.winzip.com/aes_info.htm
96 /* Value used in compression method. */
97 #define WINZIP_AES_ENCRYPTION 99
98 /* A WinZip AES header size which is stored at the beginning of
100 #define WINZIP_AES128_HEADER_SIZE (8 + 2)
101 #define WINZIP_AES256_HEADER_SIZE (16 + 2)
102 /* AES vendor version. */
103 #define AES_VENDOR_AE_1 0x0001
104 #define AES_VENDOR_AE_2 0x0002
105 /* Authentication code size. */
106 #define AUTH_CODE_SIZE 10
108 #define MAX_DERIVED_KEY_BUF_SIZE (AES_MAX_KEY_SIZE * 2 + 2)
111 struct cd_segment *next;
117 struct trad_enc_ctx {
123 int64_t entry_offset;
124 int64_t entry_compressed_size;
125 int64_t entry_uncompressed_size;
126 int64_t entry_compressed_written;
127 int64_t entry_uncompressed_written;
128 int64_t entry_uncompressed_limit;
129 struct archive_entry *entry;
130 uint32_t entry_crc32;
131 enum compression entry_compression;
132 enum encryption entry_encryption;
134 int entry_uses_zip64;
136 struct trad_enc_ctx tctx;
138 unsigned char trad_chkdat;
140 archive_crypto_ctx cctx;
142 archive_hmac_sha1_ctx hctx;
145 unsigned char *file_header;
146 size_t file_header_extra_offset;
147 unsigned long (*crc32func)(unsigned long crc, const void *buff, size_t len);
149 struct cd_segment *central_directory;
150 struct cd_segment *central_directory_last;
151 size_t central_directory_bytes;
152 size_t central_directory_entries;
154 int64_t written_bytes; /* Overall position in file. */
156 struct archive_string_conv *opt_sconv;
157 struct archive_string_conv *sconv_default;
158 enum compression requested_compression;
159 int deflate_compression_level;
160 int init_default_conversion;
161 enum encryption encryption_type;
163 #define ZIP_FLAG_AVOID_ZIP64 1
164 #define ZIP_FLAG_FORCE_ZIP64 2
165 #define ZIP_FLAG_EXPERIMENT_xl 4
175 /* Don't call this min or MIN, since those are already defined
176 on lots of platforms (but not all). */
177 #define zipmin(a, b) ((a) > (b) ? (b) : (a))
179 static ssize_t archive_write_zip_data(struct archive_write *,
180 const void *buff, size_t s);
181 static int archive_write_zip_close(struct archive_write *);
182 static int archive_write_zip_free(struct archive_write *);
183 static int archive_write_zip_finish_entry(struct archive_write *);
184 static int archive_write_zip_header(struct archive_write *,
185 struct archive_entry *);
186 static int archive_write_zip_options(struct archive_write *,
187 const char *, const char *);
188 static unsigned int dos_time(const time_t);
189 static size_t path_length(struct archive_entry *);
190 static int write_path(struct archive_entry *, struct archive_write *);
191 static void copy_path(struct archive_entry *, unsigned char *);
192 static struct archive_string_conv *get_sconv(struct archive_write *, struct zip *);
193 static int trad_enc_init(struct trad_enc_ctx *, const char *, size_t);
194 static unsigned trad_enc_encrypt_update(struct trad_enc_ctx *, const uint8_t *,
195 size_t, uint8_t *, size_t);
196 static int init_traditional_pkware_encryption(struct archive_write *);
197 static int is_traditional_pkware_encryption_supported(void);
198 static int init_winzip_aes_encryption(struct archive_write *);
199 static int is_winzip_aes_encryption_supported(int encryption);
201 static unsigned char *
202 cd_alloc(struct zip *zip, size_t length)
206 if (zip->central_directory == NULL
207 || (zip->central_directory_last->p + length
208 > zip->central_directory_last->buff + zip->central_directory_last->buff_size)) {
209 struct cd_segment *segment = calloc(1, sizeof(*segment));
212 segment->buff_size = 64 * 1024;
213 segment->buff = malloc(segment->buff_size);
214 if (segment->buff == NULL) {
218 segment->p = segment->buff;
220 if (zip->central_directory == NULL) {
221 zip->central_directory
222 = zip->central_directory_last
225 zip->central_directory_last->next = segment;
226 zip->central_directory_last = segment;
230 p = zip->central_directory_last->p;
231 zip->central_directory_last->p += length;
232 zip->central_directory_bytes += length;
237 real_crc32(unsigned long crc, const void *buff, size_t len)
239 return crc32(crc, buff, (unsigned int)len);
243 fake_crc32(unsigned long crc, const void *buff, size_t len)
245 (void)crc; /* UNUSED */
246 (void)buff; /* UNUSED */
247 (void)len; /* UNUSED */
252 archive_write_zip_options(struct archive_write *a, const char *key,
255 struct zip *zip = a->format_data;
256 int ret = ARCHIVE_FAILED;
258 if (strcmp(key, "compression") == 0) {
260 * Set compression to use on all future entries.
261 * This only affects regular files.
263 if (val == NULL || val[0] == 0) {
264 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
265 "%s: compression option needs a compression name",
267 } else if (strcmp(val, "deflate") == 0) {
269 zip->requested_compression = COMPRESSION_DEFLATE;
272 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
273 "deflate compression not supported");
275 } else if (strcmp(val, "store") == 0) {
276 zip->requested_compression = COMPRESSION_STORE;
280 } else if (strcmp(key, "compression-level") == 0) {
281 if (val == NULL || !(val[0] >= '0' && val[0] <= '9') || val[1] != '\0') {
286 zip->requested_compression = COMPRESSION_STORE;
290 zip->requested_compression = COMPRESSION_DEFLATE;
291 zip->deflate_compression_level = val[0] - '0';
294 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
295 "deflate compression not supported");
298 } else if (strcmp(key, "encryption") == 0) {
300 zip->encryption_type = ENCRYPTION_NONE;
302 } else if (val[0] == '1' || strcmp(val, "traditional") == 0
303 || strcmp(val, "zipcrypt") == 0
304 || strcmp(val, "ZipCrypt") == 0) {
305 if (is_traditional_pkware_encryption_supported()) {
306 zip->encryption_type = ENCRYPTION_TRADITIONAL;
309 archive_set_error(&a->archive,
311 "encryption not supported");
313 } else if (strcmp(val, "aes128") == 0) {
314 if (is_winzip_aes_encryption_supported(
315 ENCRYPTION_WINZIP_AES128)) {
316 zip->encryption_type = ENCRYPTION_WINZIP_AES128;
319 archive_set_error(&a->archive,
321 "encryption not supported");
323 } else if (strcmp(val, "aes256") == 0) {
324 if (is_winzip_aes_encryption_supported(
325 ENCRYPTION_WINZIP_AES256)) {
326 zip->encryption_type = ENCRYPTION_WINZIP_AES256;
329 archive_set_error(&a->archive,
331 "encryption not supported");
334 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
335 "%s: unknown encryption '%s'",
336 a->format_name, val);
339 } else if (strcmp(key, "experimental") == 0) {
340 if (val == NULL || val[0] == 0) {
341 zip->flags &= ~ ZIP_FLAG_EXPERIMENT_xl;
343 zip->flags |= ZIP_FLAG_EXPERIMENT_xl;
346 } else if (strcmp(key, "fakecrc32") == 0) {
348 * FOR TESTING ONLY: disable CRC calculation to speed up
349 * certain complex tests.
351 if (val == NULL || val[0] == 0) {
352 zip->crc32func = real_crc32;
354 zip->crc32func = fake_crc32;
357 } else if (strcmp(key, "hdrcharset") == 0) {
359 * Set the character set used in translating filenames.
361 if (val == NULL || val[0] == 0) {
362 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
363 "%s: hdrcharset option needs a character-set name",
366 zip->opt_sconv = archive_string_conversion_to_charset(
367 &a->archive, val, 0);
368 if (zip->opt_sconv != NULL)
374 } else if (strcmp(key, "zip64") == 0) {
376 * Bias decisions about Zip64: force them to be
377 * generated in certain cases where they are not
378 * forbidden or avoid them in certain cases where they
379 * are not strictly required.
381 if (val != NULL && *val != '\0') {
382 zip->flags |= ZIP_FLAG_FORCE_ZIP64;
383 zip->flags &= ~ZIP_FLAG_AVOID_ZIP64;
385 zip->flags &= ~ZIP_FLAG_FORCE_ZIP64;
386 zip->flags |= ZIP_FLAG_AVOID_ZIP64;
391 /* Note: The "warn" return is just to inform the options
392 * supervisor that we didn't handle it. It will generate
393 * a suitable error if no one used this option. */
394 return (ARCHIVE_WARN);
398 archive_write_zip_set_compression_deflate(struct archive *_a)
400 struct archive_write *a = (struct archive_write *)_a;
401 int ret = ARCHIVE_FAILED;
403 archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
404 ARCHIVE_STATE_NEW | ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
405 "archive_write_zip_set_compression_deflate");
406 if (a->archive.archive_format != ARCHIVE_FORMAT_ZIP) {
407 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
408 "Can only use archive_write_zip_set_compression_deflate"
413 struct zip *zip = a->format_data;
414 zip->requested_compression = COMPRESSION_DEFLATE;
417 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
418 "deflate compression not supported");
419 ret = ARCHIVE_FAILED;
426 archive_write_zip_set_compression_store(struct archive *_a)
428 struct archive_write *a = (struct archive_write *)_a;
429 struct zip *zip = a->format_data;
430 int ret = ARCHIVE_FAILED;
432 archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
433 ARCHIVE_STATE_NEW | ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
434 "archive_write_zip_set_compression_deflate");
435 if (a->archive.archive_format != ARCHIVE_FORMAT_ZIP) {
436 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
437 "Can only use archive_write_zip_set_compression_store"
441 zip->requested_compression = COMPRESSION_STORE;
448 archive_write_set_format_zip(struct archive *_a)
450 struct archive_write *a = (struct archive_write *)_a;
453 archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
454 ARCHIVE_STATE_NEW, "archive_write_set_format_zip");
456 /* If another format was already registered, unregister it. */
457 if (a->format_free != NULL)
460 zip = (struct zip *) calloc(1, sizeof(*zip));
462 archive_set_error(&a->archive, ENOMEM,
463 "Can't allocate zip data");
464 return (ARCHIVE_FATAL);
467 /* "Unspecified" lets us choose the appropriate compression. */
468 zip->requested_compression = COMPRESSION_UNSPECIFIED;
470 zip->deflate_compression_level = Z_DEFAULT_COMPRESSION;
472 zip->crc32func = real_crc32;
474 /* A buffer used for both compression and encryption. */
475 zip->len_buf = 65536;
476 zip->buf = malloc(zip->len_buf);
477 if (zip->buf == NULL) {
479 archive_set_error(&a->archive, ENOMEM,
480 "Can't allocate compression buffer");
481 return (ARCHIVE_FATAL);
484 a->format_data = zip;
485 a->format_name = "zip";
486 a->format_options = archive_write_zip_options;
487 a->format_write_header = archive_write_zip_header;
488 a->format_write_data = archive_write_zip_data;
489 a->format_finish_entry = archive_write_zip_finish_entry;
490 a->format_close = archive_write_zip_close;
491 a->format_free = archive_write_zip_free;
492 a->archive.archive_format = ARCHIVE_FORMAT_ZIP;
493 a->archive.archive_format_name = "ZIP";
499 is_all_ascii(const char *p)
501 const unsigned char *pp = (const unsigned char *)p;
511 archive_write_zip_header(struct archive_write *a, struct archive_entry *entry)
513 unsigned char local_header[32];
514 unsigned char local_extra[144];
515 struct zip *zip = a->format_data;
517 unsigned char *cd_extra;
518 size_t filename_length;
519 const char *slink = NULL;
520 size_t slink_size = 0;
521 struct archive_string_conv *sconv = get_sconv(a, zip);
522 int ret, ret2 = ARCHIVE_OK;
524 int version_needed = 10;
526 /* Ignore types of entries that we don't support. */
527 type = archive_entry_filetype(entry);
528 if (type != AE_IFREG && type != AE_IFDIR && type != AE_IFLNK) {
529 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
530 "Filetype not supported");
531 return ARCHIVE_FAILED;
534 /* If we're not using Zip64, reject large files. */
535 if (zip->flags & ZIP_FLAG_AVOID_ZIP64) {
536 /* Reject entries over 4GB. */
537 if (archive_entry_size_is_set(entry)
538 && (archive_entry_size(entry) > ZIP_4GB_MAX)) {
539 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
540 "Files > 4GB require Zip64 extensions");
541 return ARCHIVE_FAILED;
543 /* Reject entries if archive is > 4GB. */
544 if (zip->written_bytes > ZIP_4GB_MAX) {
545 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
546 "Archives > 4GB require Zip64 extensions");
547 return ARCHIVE_FAILED;
551 /* Only regular files can have size > 0. */
552 if (type != AE_IFREG)
553 archive_entry_set_size(entry, 0);
556 /* Reset information from last entry. */
557 zip->entry_offset = zip->written_bytes;
558 zip->entry_uncompressed_limit = INT64_MAX;
559 zip->entry_compressed_size = 0;
560 zip->entry_uncompressed_size = 0;
561 zip->entry_compressed_written = 0;
562 zip->entry_uncompressed_written = 0;
563 zip->entry_flags = 0;
564 zip->entry_uses_zip64 = 0;
565 zip->entry_crc32 = zip->crc32func(0, NULL, 0);
566 zip->entry_encryption = 0;
567 if (zip->entry != NULL) {
568 archive_entry_free(zip->entry);
573 archive_encrypto_aes_ctr_release(&zip->cctx);
575 archive_hmac_sha1_cleanup(&zip->hctx);
576 zip->tctx_valid = zip->cctx_valid = zip->hctx_valid = 0;
579 &&(!archive_entry_size_is_set(entry)
580 || archive_entry_size(entry) > 0)) {
581 switch (zip->encryption_type) {
582 case ENCRYPTION_TRADITIONAL:
583 case ENCRYPTION_WINZIP_AES128:
584 case ENCRYPTION_WINZIP_AES256:
585 zip->entry_flags |= ZIP_ENTRY_FLAG_ENCRYPTED;
586 zip->entry_encryption = zip->encryption_type;
594 #if defined(_WIN32) && !defined(__CYGWIN__)
595 /* Make sure the path separators in pahtname, 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
717 if (zip->entry_compression == COMPRESSION_STORE)
718 zip->entry_compressed_size += additional_size;
722 * Set Zip64 extension in any of the following cases
723 * (this was suggested by discussion on info-zip-dev
725 * = Zip64 is being forced by user
726 * = File is over 4GiB uncompressed
727 * (including encryption header, if any)
728 * = File is close to 4GiB and is being compressed
729 * (compression might make file larger)
731 if ((zip->flags & ZIP_FLAG_FORCE_ZIP64)
732 || (zip->entry_uncompressed_size + additional_size > ZIP_4GB_MAX)
733 || (zip->entry_uncompressed_size > ZIP_4GB_MAX_UNCOMPRESSED
734 && zip->entry_compression != COMPRESSION_STORE)) {
735 zip->entry_uses_zip64 = 1;
739 /* We may know the size, but never the CRC. */
740 zip->entry_flags |= ZIP_ENTRY_FLAG_LENGTH_AT_END;
742 /* We don't know the size. In this case, we prefer
743 * deflate (it has a clear end-of-data marker which
744 * makes length-at-end more reliable) and will
745 * enable Zip64 extensions unless we're told not to.
747 zip->entry_compression = COMPRESSION_DEFAULT;
748 zip->entry_flags |= ZIP_ENTRY_FLAG_LENGTH_AT_END;
749 if ((zip->flags & ZIP_FLAG_AVOID_ZIP64) == 0) {
750 zip->entry_uses_zip64 = 1;
752 } else if (zip->entry_compression == COMPRESSION_STORE) {
758 if (zip->entry_flags & ZIP_ENTRY_FLAG_ENCRYPTED) {
759 switch (zip->entry_encryption) {
760 case ENCRYPTION_TRADITIONAL:
761 case ENCRYPTION_WINZIP_AES128:
762 case ENCRYPTION_WINZIP_AES256:
763 if (version_needed < 20)
772 /* Format the local header. */
773 memset(local_header, 0, sizeof(local_header));
774 memcpy(local_header, "PK\003\004", 4);
775 archive_le16enc(local_header + 4, version_needed);
776 archive_le16enc(local_header + 6, zip->entry_flags);
777 if (zip->entry_encryption == ENCRYPTION_WINZIP_AES128
778 || zip->entry_encryption == ENCRYPTION_WINZIP_AES256)
779 archive_le16enc(local_header + 8, WINZIP_AES_ENCRYPTION);
781 archive_le16enc(local_header + 8, zip->entry_compression);
782 archive_le32enc(local_header + 10,
783 dos_time(archive_entry_mtime(zip->entry)));
784 archive_le32enc(local_header + 14, zip->entry_crc32);
785 if (zip->entry_uses_zip64) {
786 /* Zip64 data in the local header "must" include both
787 * compressed and uncompressed sizes AND those fields
788 * are included only if these are 0xffffffff;
789 * THEREFORE these must be set this way, even if we
790 * know one of them is smaller. */
791 archive_le32enc(local_header + 18, ZIP_4GB_MAX);
792 archive_le32enc(local_header + 22, ZIP_4GB_MAX);
794 archive_le32enc(local_header + 18, (uint32_t)zip->entry_compressed_size);
795 archive_le32enc(local_header + 22, (uint32_t)zip->entry_uncompressed_size);
797 archive_le16enc(local_header + 26, (uint16_t)filename_length);
799 if (zip->entry_encryption == ENCRYPTION_TRADITIONAL) {
800 if (zip->entry_flags & ZIP_ENTRY_FLAG_LENGTH_AT_END)
801 zip->trad_chkdat = local_header[11];
803 zip->trad_chkdat = local_header[17];
806 /* Format as much of central directory file header as we can: */
807 zip->file_header = cd_alloc(zip, 46);
808 /* If (zip->file_header == NULL) XXXX */
809 ++zip->central_directory_entries;
810 memset(zip->file_header, 0, 46);
811 memcpy(zip->file_header, "PK\001\002", 4);
812 /* "Made by PKZip 2.0 on Unix." */
813 archive_le16enc(zip->file_header + 4, 3 * 256 + version_needed);
814 archive_le16enc(zip->file_header + 6, version_needed);
815 archive_le16enc(zip->file_header + 8, zip->entry_flags);
816 if (zip->entry_encryption == ENCRYPTION_WINZIP_AES128
817 || zip->entry_encryption == ENCRYPTION_WINZIP_AES256)
818 archive_le16enc(zip->file_header + 10, WINZIP_AES_ENCRYPTION);
820 archive_le16enc(zip->file_header + 10, zip->entry_compression);
821 archive_le32enc(zip->file_header + 12,
822 dos_time(archive_entry_mtime(zip->entry)));
823 archive_le16enc(zip->file_header + 28, (uint16_t)filename_length);
824 /* Following Info-Zip, store mode in the "external attributes" field. */
825 archive_le32enc(zip->file_header + 38,
826 ((uint32_t)archive_entry_mode(zip->entry)) << 16);
827 e = cd_alloc(zip, filename_length);
828 /* If (e == NULL) XXXX */
829 copy_path(zip->entry, e);
831 /* Format extra data. */
832 memset(local_extra, 0, sizeof(local_extra));
835 /* First, extra blocks that are the same between
836 * the local file header and the central directory.
837 * We format them once and then duplicate them. */
839 /* UT timestamp, length depends on what timestamps are set. */
841 archive_le16enc(e + 2,
843 + (archive_entry_mtime_is_set(entry) ? 4 : 0)
844 + (archive_entry_atime_is_set(entry) ? 4 : 0)
845 + (archive_entry_ctime_is_set(entry) ? 4 : 0));
848 (archive_entry_mtime_is_set(entry) ? 1 : 0)
849 | (archive_entry_atime_is_set(entry) ? 2 : 0)
850 | (archive_entry_ctime_is_set(entry) ? 4 : 0);
851 if (archive_entry_mtime_is_set(entry)) {
852 archive_le32enc(e, (uint32_t)archive_entry_mtime(entry));
855 if (archive_entry_atime_is_set(entry)) {
856 archive_le32enc(e, (uint32_t)archive_entry_atime(entry));
859 if (archive_entry_ctime_is_set(entry)) {
860 archive_le32enc(e, (uint32_t)archive_entry_ctime(entry));
864 /* ux Unix extra data, length 11, version 1 */
865 /* TODO: If uid < 64k, use 2 bytes, ditto for gid. */
866 memcpy(e, "ux\013\000\001", 5);
868 *e++ = 4; /* Length of following UID */
869 archive_le32enc(e, (uint32_t)archive_entry_uid(entry));
871 *e++ = 4; /* Length of following GID */
872 archive_le32enc(e, (uint32_t)archive_entry_gid(entry));
875 /* AES extra data field: WinZIP AES information, ID=0x9901 */
876 if ((zip->entry_flags & ZIP_ENTRY_FLAG_ENCRYPTED)
877 && (zip->entry_encryption == ENCRYPTION_WINZIP_AES128
878 || zip->entry_encryption == ENCRYPTION_WINZIP_AES256)) {
880 memcpy(e, "\001\231\007\000\001\000AE", 8);
881 /* AES vendoer version AE-2 does not store a CRC.
882 * WinZip 11 uses AE-1, which does store the CRC,
883 * but it does not store the CRC when the file size
884 * is less than 20 bytes. So we simulate what
886 * NOTE: WinZip 9.0 and 10.0 uses AE-2 by default. */
887 if (archive_entry_size_is_set(zip->entry)
888 && archive_entry_size(zip->entry) < 20) {
889 archive_le16enc(e+4, AES_VENDOR_AE_2);
890 zip->aes_vendor = AES_VENDOR_AE_2;/* no CRC. */
892 zip->aes_vendor = AES_VENDOR_AE_1;
894 /* AES encryption strength. */
895 *e++ = (zip->entry_encryption == ENCRYPTION_WINZIP_AES128)?1:3;
896 /* Actual compression method. */
897 archive_le16enc(e, zip->entry_compression);
901 /* Copy UT ,ux, and AES-extra into central directory as well. */
902 zip->file_header_extra_offset = zip->central_directory_bytes;
903 cd_extra = cd_alloc(zip, e - local_extra);
904 memcpy(cd_extra, local_extra, e - local_extra);
907 * Following extra blocks vary between local header and
908 * central directory. These are the local header versions.
909 * Central directory versions get formatted in
910 * archive_write_zip_finish_entry() below.
913 /* "[Zip64 entry] in the local header MUST include BOTH
914 * original [uncompressed] and compressed size fields." */
915 if (zip->entry_uses_zip64) {
916 unsigned char *zip64_start = e;
917 memcpy(e, "\001\000\020\000", 4);
919 archive_le64enc(e, zip->entry_uncompressed_size);
921 archive_le64enc(e, zip->entry_compressed_size);
923 archive_le16enc(zip64_start + 2, (uint16_t)(e - (zip64_start + 4)));
926 if (zip->flags & ZIP_FLAG_EXPERIMENT_xl) {
927 /* Experimental 'xl' extension to improve streaming. */
928 unsigned char *external_info = e;
930 memcpy(e, "xl\000\000", 4); // 0x6c65 + 2-byte length
932 e[0] = included; /* bitmap of included fields */
935 archive_le16enc(e, /* "Version created by" */
936 3 * 256 + version_needed);
940 archive_le16enc(e, 0); /* internal file attributes */
944 archive_le32enc(e, /* external file attributes */
945 ((uint32_t)archive_entry_mode(zip->entry)) << 16);
949 // Libarchive does not currently support file comments.
951 archive_le16enc(external_info + 2, (uint16_t)(e - (external_info + 4)));
954 /* Update local header with size of extra data and write it all out: */
955 archive_le16enc(local_header + 28, (uint16_t)(e - local_extra));
957 ret = __archive_write_output(a, local_header, 30);
958 if (ret != ARCHIVE_OK)
959 return (ARCHIVE_FATAL);
960 zip->written_bytes += 30;
962 ret = write_path(zip->entry, a);
963 if (ret <= ARCHIVE_OK)
964 return (ARCHIVE_FATAL);
965 zip->written_bytes += ret;
967 ret = __archive_write_output(a, local_extra, e - local_extra);
968 if (ret != ARCHIVE_OK)
969 return (ARCHIVE_FATAL);
970 zip->written_bytes += e - local_extra;
972 /* For symlinks, write the body now. */
974 ret = __archive_write_output(a, slink, slink_size);
975 if (ret != ARCHIVE_OK)
976 return (ARCHIVE_FATAL);
977 zip->entry_compressed_written += slink_size;
978 zip->entry_uncompressed_written += slink_size;
979 zip->written_bytes += slink_size;
983 if (zip->entry_compression == COMPRESSION_DEFLATE) {
984 zip->stream.zalloc = Z_NULL;
985 zip->stream.zfree = Z_NULL;
986 zip->stream.opaque = Z_NULL;
987 zip->stream.next_out = zip->buf;
988 zip->stream.avail_out = (uInt)zip->len_buf;
989 if (deflateInit2(&zip->stream, zip->deflate_compression_level,
990 Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY) != Z_OK) {
991 archive_set_error(&a->archive, ENOMEM,
992 "Can't init deflate compressor");
993 return (ARCHIVE_FATAL);
1002 archive_write_zip_data(struct archive_write *a, const void *buff, size_t s)
1005 struct zip *zip = a->format_data;
1007 if ((int64_t)s > zip->entry_uncompressed_limit)
1008 s = (size_t)zip->entry_uncompressed_limit;
1009 zip->entry_uncompressed_written += s;
1011 if (s == 0) return 0;
1013 if (zip->entry_flags & ZIP_ENTRY_FLAG_ENCRYPTED) {
1014 switch (zip->entry_encryption) {
1015 case ENCRYPTION_TRADITIONAL:
1016 /* Initialize traditoinal PKWARE encryption context. */
1017 if (!zip->tctx_valid) {
1018 ret = init_traditional_pkware_encryption(a);
1019 if (ret != ARCHIVE_OK)
1021 zip->tctx_valid = 1;
1024 case ENCRYPTION_WINZIP_AES128:
1025 case ENCRYPTION_WINZIP_AES256:
1026 if (!zip->cctx_valid) {
1027 ret = init_winzip_aes_encryption(a);
1028 if (ret != ARCHIVE_OK)
1030 zip->cctx_valid = zip->hctx_valid = 1;
1038 switch (zip->entry_compression) {
1039 case COMPRESSION_STORE:
1040 if (zip->tctx_valid || zip->cctx_valid) {
1041 const uint8_t *rb = (const uint8_t *)buff;
1042 const uint8_t * const re = rb + s;
1047 if (zip->tctx_valid) {
1048 l = trad_enc_encrypt_update(&zip->tctx,
1050 zip->buf, zip->len_buf);
1053 ret = archive_encrypto_aes_ctr_update(
1055 rb, re - rb, zip->buf, &l);
1057 archive_set_error(&a->archive,
1059 "Failed to encrypt file");
1060 return (ARCHIVE_FAILED);
1062 archive_hmac_sha1_update(&zip->hctx,
1065 ret = __archive_write_output(a, zip->buf, l);
1066 if (ret != ARCHIVE_OK)
1068 zip->entry_compressed_written += l;
1069 zip->written_bytes += l;
1073 ret = __archive_write_output(a, buff, s);
1074 if (ret != ARCHIVE_OK)
1076 zip->written_bytes += s;
1077 zip->entry_compressed_written += s;
1081 case COMPRESSION_DEFLATE:
1082 zip->stream.next_in = (unsigned char*)(uintptr_t)buff;
1083 zip->stream.avail_in = (uInt)s;
1085 ret = deflate(&zip->stream, Z_NO_FLUSH);
1086 if (ret == Z_STREAM_ERROR)
1087 return (ARCHIVE_FATAL);
1088 if (zip->stream.avail_out == 0) {
1089 if (zip->tctx_valid) {
1090 trad_enc_encrypt_update(&zip->tctx,
1091 zip->buf, zip->len_buf,
1092 zip->buf, zip->len_buf);
1093 } else if (zip->cctx_valid) {
1094 size_t outl = zip->len_buf;
1095 ret = archive_encrypto_aes_ctr_update(
1097 zip->buf, zip->len_buf,
1100 archive_set_error(&a->archive,
1102 "Failed to encrypt file");
1103 return (ARCHIVE_FAILED);
1105 archive_hmac_sha1_update(&zip->hctx,
1106 zip->buf, zip->len_buf);
1108 ret = __archive_write_output(a, zip->buf,
1110 if (ret != ARCHIVE_OK)
1112 zip->entry_compressed_written += zip->len_buf;
1113 zip->written_bytes += zip->len_buf;
1114 zip->stream.next_out = zip->buf;
1115 zip->stream.avail_out = (uInt)zip->len_buf;
1117 } while (zip->stream.avail_in != 0);
1122 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1123 "Invalid ZIP compression type");
1124 return ARCHIVE_FATAL;
1127 zip->entry_uncompressed_limit -= s;
1128 if (!zip->cctx_valid || zip->aes_vendor != AES_VENDOR_AE_2)
1130 zip->crc32func(zip->entry_crc32, buff, (unsigned)s);
1136 archive_write_zip_finish_entry(struct archive_write *a)
1138 struct zip *zip = a->format_data;
1142 if (zip->entry_compression == COMPRESSION_DEFLATE) {
1146 ret = deflate(&zip->stream, Z_FINISH);
1147 if (ret == Z_STREAM_ERROR)
1148 return (ARCHIVE_FATAL);
1149 remainder = zip->len_buf - zip->stream.avail_out;
1150 if (zip->tctx_valid) {
1151 trad_enc_encrypt_update(&zip->tctx,
1152 zip->buf, remainder, zip->buf, remainder);
1153 } else if (zip->cctx_valid) {
1154 size_t outl = remainder;
1155 ret = archive_encrypto_aes_ctr_update(
1156 &zip->cctx, zip->buf, remainder,
1159 archive_set_error(&a->archive,
1161 "Failed to encrypt file");
1162 return (ARCHIVE_FAILED);
1164 archive_hmac_sha1_update(&zip->hctx,
1165 zip->buf, remainder);
1167 ret = __archive_write_output(a, zip->buf, remainder);
1168 if (ret != ARCHIVE_OK)
1170 zip->entry_compressed_written += remainder;
1171 zip->written_bytes += remainder;
1172 zip->stream.next_out = zip->buf;
1173 if (zip->stream.avail_out != 0)
1175 zip->stream.avail_out = (uInt)zip->len_buf;
1177 deflateEnd(&zip->stream);
1180 if (zip->hctx_valid) {
1182 size_t hmac_len = 20;
1184 archive_hmac_sha1_final(&zip->hctx, hmac, &hmac_len);
1185 ret = __archive_write_output(a, hmac, AUTH_CODE_SIZE);
1186 if (ret != ARCHIVE_OK)
1188 zip->entry_compressed_written += AUTH_CODE_SIZE;
1189 zip->written_bytes += AUTH_CODE_SIZE;
1192 /* Write trailing data descriptor. */
1193 if ((zip->entry_flags & ZIP_ENTRY_FLAG_LENGTH_AT_END) != 0) {
1195 memcpy(d, "PK\007\010", 4);
1196 if (zip->cctx_valid && zip->aes_vendor == AES_VENDOR_AE_2)
1197 archive_le32enc(d + 4, 0);/* no CRC.*/
1199 archive_le32enc(d + 4, zip->entry_crc32);
1200 if (zip->entry_uses_zip64) {
1201 archive_le64enc(d + 8,
1202 (uint64_t)zip->entry_compressed_written);
1203 archive_le64enc(d + 16,
1204 (uint64_t)zip->entry_uncompressed_written);
1205 ret = __archive_write_output(a, d, 24);
1206 zip->written_bytes += 24;
1208 archive_le32enc(d + 8,
1209 (uint32_t)zip->entry_compressed_written);
1210 archive_le32enc(d + 12,
1211 (uint32_t)zip->entry_uncompressed_written);
1212 ret = __archive_write_output(a, d, 16);
1213 zip->written_bytes += 16;
1215 if (ret != ARCHIVE_OK)
1216 return (ARCHIVE_FATAL);
1219 /* Append Zip64 extra data to central directory information. */
1220 if (zip->entry_compressed_written > ZIP_4GB_MAX
1221 || zip->entry_uncompressed_written > ZIP_4GB_MAX
1222 || zip->entry_offset > ZIP_4GB_MAX) {
1223 unsigned char zip64[32];
1224 unsigned char *z = zip64, *zd;
1225 memcpy(z, "\001\000\000\000", 4);
1227 if (zip->entry_uncompressed_written >= ZIP_4GB_MAX) {
1228 archive_le64enc(z, zip->entry_uncompressed_written);
1231 if (zip->entry_compressed_written >= ZIP_4GB_MAX) {
1232 archive_le64enc(z, zip->entry_compressed_written);
1235 if (zip->entry_offset >= ZIP_4GB_MAX) {
1236 archive_le64enc(z, zip->entry_offset);
1239 archive_le16enc(zip64 + 2, (uint16_t)(z - (zip64 + 4)));
1240 zd = cd_alloc(zip, z - zip64);
1242 archive_set_error(&a->archive, ENOMEM,
1243 "Can't allocate zip data");
1244 return (ARCHIVE_FATAL);
1246 memcpy(zd, zip64, z - zip64);
1247 /* Zip64 means version needs to be set to at least 4.5 */
1248 if (archive_le16dec(zip->file_header + 6) < 45)
1249 archive_le16enc(zip->file_header + 6, 45);
1252 /* Fix up central directory file header. */
1253 if (zip->cctx_valid && zip->aes_vendor == AES_VENDOR_AE_2)
1254 archive_le32enc(zip->file_header + 16, 0);/* no CRC.*/
1256 archive_le32enc(zip->file_header + 16, zip->entry_crc32);
1257 archive_le32enc(zip->file_header + 20,
1258 (uint32_t)zipmin(zip->entry_compressed_written,
1260 archive_le32enc(zip->file_header + 24,
1261 (uint32_t)zipmin(zip->entry_uncompressed_written,
1263 archive_le16enc(zip->file_header + 30,
1264 (uint16_t)(zip->central_directory_bytes - zip->file_header_extra_offset));
1265 archive_le32enc(zip->file_header + 42,
1266 (uint32_t)zipmin(zip->entry_offset,
1269 return (ARCHIVE_OK);
1273 archive_write_zip_close(struct archive_write *a)
1276 int64_t offset_start, offset_end;
1277 struct zip *zip = a->format_data;
1278 struct cd_segment *segment;
1281 offset_start = zip->written_bytes;
1282 segment = zip->central_directory;
1283 while (segment != NULL) {
1284 ret = __archive_write_output(a,
1285 segment->buff, segment->p - segment->buff);
1286 if (ret != ARCHIVE_OK)
1287 return (ARCHIVE_FATAL);
1288 zip->written_bytes += segment->p - segment->buff;
1289 segment = segment->next;
1291 offset_end = zip->written_bytes;
1293 /* If central dir info is too large, write Zip64 end-of-cd */
1294 if (offset_end - offset_start > ZIP_4GB_MAX
1295 || offset_start > ZIP_4GB_MAX
1296 || zip->central_directory_entries > 0xffffUL
1297 || (zip->flags & ZIP_FLAG_FORCE_ZIP64)) {
1298 /* Zip64 end-of-cd record */
1299 memset(buff, 0, 56);
1300 memcpy(buff, "PK\006\006", 4);
1301 archive_le64enc(buff + 4, 44);
1302 archive_le16enc(buff + 12, 45);
1303 archive_le16enc(buff + 14, 45);
1304 /* This is disk 0 of 0. */
1305 archive_le64enc(buff + 24, zip->central_directory_entries);
1306 archive_le64enc(buff + 32, zip->central_directory_entries);
1307 archive_le64enc(buff + 40, offset_end - offset_start);
1308 archive_le64enc(buff + 48, offset_start);
1309 ret = __archive_write_output(a, buff, 56);
1310 if (ret != ARCHIVE_OK)
1311 return (ARCHIVE_FATAL);
1312 zip->written_bytes += 56;
1314 /* Zip64 end-of-cd locator record. */
1315 memset(buff, 0, 20);
1316 memcpy(buff, "PK\006\007", 4);
1317 archive_le32enc(buff + 4, 0);
1318 archive_le64enc(buff + 8, offset_end);
1319 archive_le32enc(buff + 16, 1);
1320 ret = __archive_write_output(a, buff, 20);
1321 if (ret != ARCHIVE_OK)
1322 return (ARCHIVE_FATAL);
1323 zip->written_bytes += 20;
1327 /* Format and write end of central directory. */
1328 memset(buff, 0, sizeof(buff));
1329 memcpy(buff, "PK\005\006", 4);
1330 archive_le16enc(buff + 8, (uint16_t)zipmin(0xffffU,
1331 zip->central_directory_entries));
1332 archive_le16enc(buff + 10, (uint16_t)zipmin(0xffffU,
1333 zip->central_directory_entries));
1334 archive_le32enc(buff + 12,
1335 (uint32_t)zipmin(ZIP_4GB_MAX, (offset_end - offset_start)));
1336 archive_le32enc(buff + 16,
1337 (uint32_t)zipmin(ZIP_4GB_MAX, offset_start));
1338 ret = __archive_write_output(a, buff, 22);
1339 if (ret != ARCHIVE_OK)
1340 return (ARCHIVE_FATAL);
1341 zip->written_bytes += 22;
1342 return (ARCHIVE_OK);
1346 archive_write_zip_free(struct archive_write *a)
1349 struct cd_segment *segment;
1351 zip = a->format_data;
1352 while (zip->central_directory != NULL) {
1353 segment = zip->central_directory;
1354 zip->central_directory = segment->next;
1355 free(segment->buff);
1359 archive_entry_free(zip->entry);
1360 if (zip->cctx_valid)
1361 archive_encrypto_aes_ctr_release(&zip->cctx);
1362 if (zip->hctx_valid)
1363 archive_hmac_sha1_cleanup(&zip->hctx);
1364 /* TODO: Free opt_sconv, sconv_default */
1367 a->format_data = NULL;
1368 return (ARCHIVE_OK);
1371 /* Convert into MSDOS-style date/time. */
1373 dos_time(const time_t unix_time)
1378 /* This will not preserve time when creating/extracting the archive
1379 * on two systems with different time zones. */
1380 t = localtime(&unix_time);
1382 /* MSDOS-style date/time is only between 1980-01-01 and 2107-12-31 */
1383 if (t->tm_year < 1980 - 1900)
1384 /* Set minimum date/time '1980-01-01 00:00:00'. */
1386 else if (t->tm_year > 2107 - 1900)
1387 /* Set maximum date/time '2107-12-31 23:59:58'. */
1391 dt += ((t->tm_year - 80) & 0x7f) << 9;
1392 dt += ((t->tm_mon + 1) & 0x0f) << 5;
1393 dt += (t->tm_mday & 0x1f);
1395 dt += (t->tm_hour & 0x1f) << 11;
1396 dt += (t->tm_min & 0x3f) << 5;
1397 dt += (t->tm_sec & 0x3e) >> 1; /* Only counting every 2 seconds. */
1403 path_length(struct archive_entry *entry)
1408 type = archive_entry_filetype(entry);
1409 path = archive_entry_pathname(entry);
1413 if (type == AE_IFDIR &&
1414 (path[0] == '\0' || path[strlen(path) - 1] != '/')) {
1415 return strlen(path) + 1;
1417 return strlen(path);
1422 write_path(struct archive_entry *entry, struct archive_write *archive)
1427 size_t written_bytes;
1429 path = archive_entry_pathname(entry);
1430 type = archive_entry_filetype(entry);
1433 ret = __archive_write_output(archive, path, strlen(path));
1434 if (ret != ARCHIVE_OK)
1435 return (ARCHIVE_FATAL);
1436 written_bytes += strlen(path);
1438 /* Folders are recognized by a trailing slash. */
1439 if ((type == AE_IFDIR) & (path[strlen(path) - 1] != '/')) {
1440 ret = __archive_write_output(archive, "/", 1);
1441 if (ret != ARCHIVE_OK)
1442 return (ARCHIVE_FATAL);
1446 return ((int)written_bytes);
1450 copy_path(struct archive_entry *entry, unsigned char *p)
1456 path = archive_entry_pathname(entry);
1457 pathlen = strlen(path);
1458 type = archive_entry_filetype(entry);
1460 memcpy(p, path, pathlen);
1462 /* Folders are recognized by a trailing slash. */
1463 if ((type == AE_IFDIR) & (path[pathlen - 1] != '/')) {
1465 p[pathlen + 1] = '\0';
1470 static struct archive_string_conv *
1471 get_sconv(struct archive_write *a, struct zip *zip)
1473 if (zip->opt_sconv != NULL)
1474 return (zip->opt_sconv);
1476 if (!zip->init_default_conversion) {
1477 zip->sconv_default =
1478 archive_string_default_conversion_for_write(&(a->archive));
1479 zip->init_default_conversion = 1;
1481 return (zip->sconv_default);
1485 Traditional PKWARE Decryption functions.
1489 trad_enc_update_keys(struct trad_enc_ctx *ctx, uint8_t c)
1492 #define CRC32(c, b) (crc32(c ^ 0xffffffffUL, &b, 1) ^ 0xffffffffUL)
1494 ctx->keys[0] = CRC32(ctx->keys[0], c);
1495 ctx->keys[1] = (ctx->keys[1] + (ctx->keys[0] & 0xff)) * 134775813L + 1;
1496 t = (ctx->keys[1] >> 24) & 0xff;
1497 ctx->keys[2] = CRC32(ctx->keys[2], t);
1502 trad_enc_decypt_byte(struct trad_enc_ctx *ctx)
1504 unsigned temp = ctx->keys[2] | 2;
1505 return (uint8_t)((temp * (temp ^ 1)) >> 8) & 0xff;
1509 trad_enc_encrypt_update(struct trad_enc_ctx *ctx, const uint8_t *in,
1510 size_t in_len, uint8_t *out, size_t out_len)
1514 max = (unsigned)((in_len < out_len)? in_len: out_len);
1516 for (i = 0; i < max; i++) {
1518 out[i] = t ^ trad_enc_decypt_byte(ctx);
1519 trad_enc_update_keys(ctx, t);
1525 trad_enc_init(struct trad_enc_ctx *ctx, const char *pw, size_t pw_len)
1528 ctx->keys[0] = 305419896L;
1529 ctx->keys[1] = 591751049L;
1530 ctx->keys[2] = 878082192L;
1532 for (;pw_len; --pw_len)
1533 trad_enc_update_keys(ctx, *pw++);
1538 is_traditional_pkware_encryption_supported(void)
1540 uint8_t key[TRAD_HEADER_SIZE];
1542 if (archive_random(key, sizeof(key)-1) != ARCHIVE_OK)
1548 init_traditional_pkware_encryption(struct archive_write *a)
1550 struct zip *zip = a->format_data;
1551 const char *passphrase;
1552 uint8_t key[TRAD_HEADER_SIZE];
1553 uint8_t key_encrypted[TRAD_HEADER_SIZE];
1556 passphrase = __archive_write_get_passphrase(a);
1557 if (passphrase == NULL) {
1558 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1559 "Encryption needs passphrase");
1560 return ARCHIVE_FAILED;
1562 if (archive_random(key, sizeof(key)-1) != ARCHIVE_OK) {
1563 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1564 "Can't generate random number for encryption");
1565 return ARCHIVE_FATAL;
1567 trad_enc_init(&zip->tctx, passphrase, strlen(passphrase));
1568 /* Set the last key code which will be used as a check code
1569 * for verifying passphrase in decryption. */
1570 key[TRAD_HEADER_SIZE-1] = zip->trad_chkdat;
1571 trad_enc_encrypt_update(&zip->tctx, key, TRAD_HEADER_SIZE,
1572 key_encrypted, TRAD_HEADER_SIZE);
1573 /* Write encrypted keys in the top of the file content. */
1574 ret = __archive_write_output(a, key_encrypted, TRAD_HEADER_SIZE);
1575 if (ret != ARCHIVE_OK)
1577 zip->written_bytes += TRAD_HEADER_SIZE;
1578 zip->entry_compressed_written += TRAD_HEADER_SIZE;
1583 init_winzip_aes_encryption(struct archive_write *a)
1585 struct zip *zip = a->format_data;
1586 const char *passphrase;
1587 size_t key_len, salt_len;
1588 uint8_t salt[16 + 2];
1589 uint8_t derived_key[MAX_DERIVED_KEY_BUF_SIZE];
1592 passphrase = __archive_write_get_passphrase(a);
1593 if (passphrase == NULL) {
1594 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1595 "Encryption needs passphrase");
1596 return (ARCHIVE_FAILED);
1598 if (zip->entry_encryption == ENCRYPTION_WINZIP_AES128) {
1606 if (archive_random(salt, salt_len) != ARCHIVE_OK) {
1607 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1608 "Can't generate random number for encryption");
1609 return (ARCHIVE_FATAL);
1611 archive_pbkdf2_sha1(passphrase, strlen(passphrase),
1612 salt, salt_len, 1000, derived_key, key_len * 2 + 2);
1614 ret = archive_encrypto_aes_ctr_init(&zip->cctx, derived_key, key_len);
1616 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1617 "Decryption is unsupported due to lack of crypto library");
1618 return (ARCHIVE_FAILED);
1620 ret = archive_hmac_sha1_init(&zip->hctx, derived_key + key_len,
1623 archive_encrypto_aes_ctr_release(&zip->cctx);
1624 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1625 "Failed to initialize HMAC-SHA1");
1626 return (ARCHIVE_FAILED);
1629 /* Set a passowrd verification value after the 'salt'. */
1630 salt[salt_len] = derived_key[key_len * 2];
1631 salt[salt_len + 1] = derived_key[key_len * 2 + 1];
1633 /* Write encrypted keys in the top of the file content. */
1634 ret = __archive_write_output(a, salt, salt_len + 2);
1635 if (ret != ARCHIVE_OK)
1637 zip->written_bytes += salt_len + 2;
1638 zip->entry_compressed_written += salt_len + 2;
1640 return (ARCHIVE_OK);
1644 is_winzip_aes_encryption_supported(int encryption)
1646 size_t key_len, salt_len;
1647 uint8_t salt[16 + 2];
1648 uint8_t derived_key[MAX_DERIVED_KEY_BUF_SIZE];
1649 archive_crypto_ctx cctx;
1650 archive_hmac_sha1_ctx hctx;
1653 if (encryption == ENCRYPTION_WINZIP_AES128) {
1661 if (archive_random(salt, salt_len) != ARCHIVE_OK)
1663 ret = archive_pbkdf2_sha1("p", 1, salt, salt_len, 1000,
1664 derived_key, key_len * 2 + 2);
1668 ret = archive_encrypto_aes_ctr_init(&cctx, derived_key, key_len);
1671 ret = archive_hmac_sha1_init(&hctx, derived_key + key_len,
1673 archive_encrypto_aes_ctr_release(&cctx);
1676 archive_hmac_sha1_cleanup(&hctx);