WinZipAesEntryExtraField.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. namespace PhpZip\Extra\Fields;
  3. use PhpZip\Exception\ZipException;
  4. use PhpZip\Extra\ExtraField;
  5. use PhpZip\ZipFile;
  6. /**
  7. * WinZip AES Extra Field.
  8. *
  9. * @see http://www.winzip.com/win/en/aes_info.htm AES Encryption Information: Encryption Specification AE-1 and AE-2
  10. * (WinZip Computing, S.L.)
  11. * @see http://www.winzip.com/win/en/aes_tips.htm AES Coding Tips for Developers (WinZip Computing, S.L.)
  12. *
  13. * @author Ne-Lexa alexey@nelexa.ru
  14. * @license MIT
  15. */
  16. class WinZipAesEntryExtraField implements ExtraField
  17. {
  18. const DATA_SIZE = 7;
  19. const VENDOR_ID = 17729; // 'A' | ('E' << 8);
  20. /**
  21. * Entries of this type <em>do</em> include the standard ZIP CRC-32 value.
  22. * For use with @see WinZipAesEntryExtraField::setVendorVersion()}/@see
  23. * WinZipAesEntryExtraField::getVendorVersion().
  24. */
  25. const VV_AE_1 = 1;
  26. /**
  27. * Entries of this type do <em>not</em> include the standard ZIP CRC-32 value.
  28. * For use with @see WinZipAesEntryExtraField::setVendorVersion()}/@see
  29. * WinZipAesEntryExtraField::getVendorVersion().
  30. */
  31. const VV_AE_2 = 2;
  32. const KEY_STRENGTH_128BIT = 128;
  33. const KEY_STRENGTH_192BIT = 192;
  34. const KEY_STRENGTH_256BIT = 256;
  35. protected static $keyStrengths = [
  36. self::KEY_STRENGTH_128BIT => 0x01,
  37. self::KEY_STRENGTH_192BIT => 0x02,
  38. self::KEY_STRENGTH_256BIT => 0x03,
  39. ];
  40. protected static $encryptionMethods = [
  41. self::KEY_STRENGTH_128BIT => ZipFile::ENCRYPTION_METHOD_WINZIP_AES_128,
  42. self::KEY_STRENGTH_192BIT => ZipFile::ENCRYPTION_METHOD_WINZIP_AES_192,
  43. self::KEY_STRENGTH_256BIT => ZipFile::ENCRYPTION_METHOD_WINZIP_AES_256,
  44. ];
  45. /**
  46. * Vendor version.
  47. *
  48. * @var int
  49. */
  50. protected $vendorVersion = self::VV_AE_1;
  51. /**
  52. * Encryption strength.
  53. *
  54. * @var int
  55. */
  56. protected $encryptionStrength = self::KEY_STRENGTH_256BIT;
  57. /**
  58. * Zip compression method.
  59. *
  60. * @var int
  61. */
  62. protected $method;
  63. /**
  64. * Returns the Header ID (type) of this Extra Field.
  65. * The Header ID is an unsigned short integer (two bytes)
  66. * which must be constant during the life cycle of this object.
  67. *
  68. * @return int
  69. */
  70. public static function getHeaderId()
  71. {
  72. return 0x9901;
  73. }
  74. /**
  75. * Returns the vendor version.
  76. *
  77. * @see WinZipAesEntryExtraField::VV_AE_1
  78. * @see WinZipAesEntryExtraField::VV_AE_2
  79. */
  80. public function getVendorVersion()
  81. {
  82. return $this->vendorVersion & 0xffff;
  83. }
  84. /**
  85. * Sets the vendor version.
  86. *
  87. * @param int $vendorVersion the vendor version
  88. *
  89. * @throws ZipException unsupport vendor version
  90. *
  91. * @see WinZipAesEntryExtraField::VV_AE_1
  92. * @see WinZipAesEntryExtraField::VV_AE_2
  93. */
  94. public function setVendorVersion($vendorVersion)
  95. {
  96. if ($vendorVersion < self::VV_AE_1 || $vendorVersion > self::VV_AE_2) {
  97. throw new ZipException($vendorVersion);
  98. }
  99. $this->vendorVersion = $vendorVersion;
  100. }
  101. /**
  102. * Returns vendor id.
  103. *
  104. * @return int
  105. */
  106. public function getVendorId()
  107. {
  108. return self::VENDOR_ID;
  109. }
  110. /**
  111. * @throws ZipException
  112. *
  113. * @return bool|int
  114. */
  115. public function getKeyStrength()
  116. {
  117. return self::keyStrength($this->encryptionStrength);
  118. }
  119. /**
  120. * @param int $encryptionStrength encryption strength as bits
  121. *
  122. * @throws ZipException if unsupport encryption strength
  123. *
  124. * @return int
  125. */
  126. public static function keyStrength($encryptionStrength)
  127. {
  128. $flipKeyStrength = array_flip(self::$keyStrengths);
  129. if (!isset($flipKeyStrength[$encryptionStrength])) {
  130. throw new ZipException('Unsupport encryption strength ' . $encryptionStrength);
  131. }
  132. return $flipKeyStrength[$encryptionStrength];
  133. }
  134. /**
  135. * Returns compression method.
  136. *
  137. * @return int
  138. */
  139. public function getMethod()
  140. {
  141. return $this->method & 0xffff;
  142. }
  143. /**
  144. * Internal encryption method.
  145. *
  146. * @throws ZipException
  147. *
  148. * @return int
  149. */
  150. public function getEncryptionMethod()
  151. {
  152. return isset(self::$encryptionMethods[$this->getKeyStrength()]) ?
  153. self::$encryptionMethods[$this->getKeyStrength()] :
  154. self::$encryptionMethods[self::KEY_STRENGTH_256BIT];
  155. }
  156. /**
  157. * @param int $encryptionMethod
  158. *
  159. * @throws ZipException
  160. *
  161. * @return int
  162. */
  163. public static function getKeyStrangeFromEncryptionMethod($encryptionMethod)
  164. {
  165. $flipKey = array_flip(self::$encryptionMethods);
  166. if (!isset($flipKey[$encryptionMethod])) {
  167. throw new ZipException('Unsupport encryption method ' . $encryptionMethod);
  168. }
  169. return $flipKey[$encryptionMethod];
  170. }
  171. /**
  172. * Sets compression method.
  173. *
  174. * @param int $compressionMethod Compression method
  175. *
  176. * @throws ZipException compression method out of range
  177. */
  178. public function setMethod($compressionMethod)
  179. {
  180. if ($compressionMethod < 0x0000 || $compressionMethod > 0xffff) {
  181. throw new ZipException('Compression method out of range');
  182. }
  183. $this->method = $compressionMethod;
  184. }
  185. /**
  186. * Set key strength.
  187. *
  188. * @param int $keyStrength
  189. */
  190. public function setKeyStrength($keyStrength)
  191. {
  192. $this->encryptionStrength = self::encryptionStrength($keyStrength);
  193. }
  194. /**
  195. * Returns encryption strength.
  196. *
  197. * @param int $keyStrength key strength in bits
  198. *
  199. * @return int
  200. */
  201. public static function encryptionStrength($keyStrength)
  202. {
  203. return isset(self::$keyStrengths[$keyStrength]) ?
  204. self::$keyStrengths[$keyStrength] :
  205. self::$keyStrengths[self::KEY_STRENGTH_128BIT];
  206. }
  207. /**
  208. * Serializes a Data Block.
  209. *
  210. * @return string
  211. */
  212. public function serialize()
  213. {
  214. return pack(
  215. 'vvcv',
  216. $this->vendorVersion,
  217. self::VENDOR_ID,
  218. $this->encryptionStrength,
  219. $this->method
  220. );
  221. }
  222. /**
  223. * Initializes this Extra Field by deserializing a Data Block.
  224. *
  225. * @param string $data
  226. *
  227. * @throws ZipException
  228. */
  229. public function deserialize($data)
  230. {
  231. $size = \strlen($data);
  232. if ($size !== self::DATA_SIZE) {
  233. throw new ZipException('WinZip AES Extra data invalid size: ' . $size . '. Must be ' . self::DATA_SIZE);
  234. }
  235. /**
  236. * @var int $vendorVersion
  237. * @var int $vendorId
  238. * @var int $keyStrength
  239. * @var int $method
  240. */
  241. $unpack = unpack('vvendorVersion/vvendorId/ckeyStrength/vmethod', $data);
  242. $this->setVendorVersion($unpack['vendorVersion']);
  243. if ($unpack['vendorId'] !== self::VENDOR_ID) {
  244. throw new ZipException('Vendor id invalid: ' . $unpack['vendorId'] . '. Must be ' . self::VENDOR_ID);
  245. }
  246. $this->setKeyStrength(self::keyStrength($unpack['keyStrength'])); // checked
  247. $this->setMethod($unpack['method']);
  248. }
  249. }