2
0

WinZipAesEntryExtraField.php 6.4 KB

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