1/*- 2 * Copyright (c) 2013 Konrad Kleine 3 * Copyright (c) 2014 Michihiro NAKAJIMA 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26#include "test.h" 27 28static void 29test_winzip_aes(const char *refname, int need_libz) 30{ 31 struct archive_entry *ae; 32 struct archive *a; 33 char buff[512]; 34 35 /* Check if running system has cryptographic functionality. */ 36 assert((a = archive_write_new()) != NULL); 37 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_zip(a)); 38 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a)); 39 if (ARCHIVE_OK != archive_write_set_options(a, 40 "zip:encryption=aes256")) { 41 skipping("This system does not have cryptographic liberary"); 42 archive_write_free(a); 43 return; 44 } 45 archive_write_free(a); 46 47 48 extract_reference_file(refname); 49 50 /* 51 * Extract a zip file without password. 52 */ 53 assert((a = archive_read_new()) != NULL); 54 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); 55 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); 56 assertEqualIntA(a, ARCHIVE_OK, 57 archive_read_open_filename(a, refname, 10240)); 58 59 assertEqualIntA(a, ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW, 60 archive_read_has_encrypted_entries(a)); 61 62 /* Verify encrypted file "README" */ 63 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); 64 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae)); 65 assertEqualString("README", archive_entry_pathname(ae)); 66 assertEqualInt(6818, archive_entry_size(ae)); 67 assertEqualInt(1, archive_entry_is_data_encrypted(ae)); 68 assertEqualInt(0, archive_entry_is_metadata_encrypted(ae)); 69 assertEqualIntA(a, 1, archive_read_has_encrypted_entries(a)); 70 assertEqualInt(ARCHIVE_FAILED, archive_read_data(a, buff, sizeof(buff))); 71 assertEqualInt(1, archive_file_count(a)); 72 73 /* End of archive. */ 74 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae)); 75 76 /* Verify archive format. */ 77 assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0)); 78 assertEqualIntA(a, ARCHIVE_FORMAT_ZIP, archive_format(a)); 79 80 /* Close the archive. */ 81 assertEqualInt(ARCHIVE_OK, archive_read_close(a)); 82 assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 83 84 85 /* 86 * Extract a zip file with password. 87 */ 88 assert((a = archive_read_new()) != NULL); 89 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); 90 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); 91 /* Pass three passphrases to decrypt a file content. */ 92 assertEqualIntA(a, ARCHIVE_OK, 93 archive_read_add_passphrase(a, "invalid_pass")); 94 assertEqualIntA(a, ARCHIVE_OK, 95 archive_read_add_passphrase(a, "invalid_phrase")); 96 assertEqualIntA(a, ARCHIVE_OK, 97 archive_read_add_passphrase(a, "password")); 98 assertEqualIntA(a, ARCHIVE_OK, 99 archive_read_open_filename(a, refname, 10240)); 100 101 assertEqualIntA(a, ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW, 102 archive_read_has_encrypted_entries(a)); 103 104 /* Verify encrypted file "README" */ 105 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); 106 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae)); 107 assertEqualString("README", archive_entry_pathname(ae)); 108 assertEqualInt(6818, archive_entry_size(ae)); 109 assertEqualInt(1, archive_entry_is_data_encrypted(ae)); 110 assertEqualInt(0, archive_entry_is_metadata_encrypted(ae)); 111 assertEqualIntA(a, 1, archive_read_has_encrypted_entries(a)); 112 if (!need_libz || archive_zlib_version() != NULL) { 113 assertEqualInt(512, archive_read_data(a, buff, sizeof(buff))); 114 } else { 115 assertEqualInt(ARCHIVE_FAILED, archive_read_data(a, buff, 19)); 116 assertEqualString(archive_error_string(a), 117 "Unsupported ZIP compression method (8: deflation)"); 118 assert(archive_errno(a) != 0); 119 } 120 121 assertEqualInt(1, archive_file_count(a)); 122 123 /* End of archive. */ 124 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae)); 125 126 /* Verify archive format. */ 127 assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0)); 128 assertEqualIntA(a, ARCHIVE_FORMAT_ZIP, archive_format(a)); 129 130 /* Close the archive. */ 131 assertEqualInt(ARCHIVE_OK, archive_read_close(a)); 132 assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 133} 134 135DEFINE_TEST(test_read_format_zip_winzip_aes128) 136{ 137 /* WinZip AES-128 encryption. */ 138 test_winzip_aes("test_read_format_zip_winzip_aes128.zip", 1); 139} 140 141DEFINE_TEST(test_read_format_zip_winzip_aes256) 142{ 143 /* WinZip AES-256 encryption. */ 144 test_winzip_aes("test_read_format_zip_winzip_aes256.zip", 1); 145} 146 147DEFINE_TEST(test_read_format_zip_winzip_aes256_stored) 148{ 149 /* WinZip AES-256 encryption with stored data. */ 150 test_winzip_aes("test_read_format_zip_winzip_aes256_stored.zip", 0); 151} 152