You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EncryptionInfo.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.poifs.crypt;
  16. import static org.apache.poi.poifs.crypt.EncryptionMode.agile;
  17. import static org.apache.poi.poifs.crypt.EncryptionMode.binaryRC4;
  18. import static org.apache.poi.poifs.crypt.EncryptionMode.cryptoAPI;
  19. import static org.apache.poi.poifs.crypt.EncryptionMode.standard;
  20. import static org.apache.poi.poifs.crypt.EncryptionMode.xor;
  21. import static org.apache.poi.util.GenericRecordUtil.getBitsAsString;
  22. import java.io.IOException;
  23. import java.util.Collections;
  24. import java.util.LinkedHashMap;
  25. import java.util.Map;
  26. import java.util.function.Supplier;
  27. import org.apache.poi.EncryptedDocumentException;
  28. import org.apache.poi.common.usermodel.GenericRecord;
  29. import org.apache.poi.poifs.filesystem.DirectoryNode;
  30. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  31. import org.apache.poi.util.BitField;
  32. import org.apache.poi.util.BitFieldFactory;
  33. import org.apache.poi.util.LittleEndianInput;
  34. /**
  35. * Wrapper for the EncryptionInfo node of encrypted documents
  36. */
  37. public class EncryptionInfo implements GenericRecord {
  38. /**
  39. * Document entry name for encryption info xml descriptor
  40. */
  41. public static final String ENCRYPTION_INFO_ENTRY = "EncryptionInfo";
  42. /**
  43. * A flag that specifies whether CryptoAPI RC4 or ECMA-376 encryption
  44. * ECMA-376 is used. It MUST be 1 unless flagExternal is 1. If flagExternal is 1, it MUST be 0.
  45. */
  46. public static final BitField flagCryptoAPI = BitFieldFactory.getInstance(0x04);
  47. /**
  48. * A value that MUST be 0 if document properties are encrypted.
  49. * The encryption of document properties is specified in section 2.3.5.4.
  50. */
  51. @SuppressWarnings("WeakerAccess")
  52. public static final BitField flagDocProps = BitFieldFactory.getInstance(0x08);
  53. /**
  54. * A value that MUST be 1 if extensible encryption is used. If this value is 1,
  55. * the value of every other field in this structure MUST be 0.
  56. */
  57. @SuppressWarnings("WeakerAccess")
  58. public static final BitField flagExternal = BitFieldFactory.getInstance(0x10);
  59. /**
  60. * A value that MUST be 1 if the protected content is an ECMA-376 document
  61. * ECMA-376. If the fAES bit is 1, the fCryptoAPI bit MUST also be 1.
  62. */
  63. public static final BitField flagAES = BitFieldFactory.getInstance(0x20);
  64. private static final int[] FLAGS_MASKS = {
  65. 0x04, 0x08, 0x10, 0x20
  66. };
  67. private static final String[] FLAGS_NAMES = {
  68. "CRYPTO_API", "DOC_PROPS", "EXTERNAL", "AES"
  69. };
  70. private final EncryptionMode encryptionMode;
  71. private final int versionMajor;
  72. private final int versionMinor;
  73. private final int encryptionFlags;
  74. private EncryptionHeader header;
  75. private EncryptionVerifier verifier;
  76. private Decryptor decryptor;
  77. private Encryptor encryptor;
  78. /**
  79. * Opens for decryption
  80. */
  81. public EncryptionInfo(POIFSFileSystem fs) throws IOException {
  82. this(fs.getRoot());
  83. }
  84. /**
  85. * Opens for decryption
  86. */
  87. public EncryptionInfo(DirectoryNode dir) throws IOException {
  88. this(dir.createDocumentInputStream(ENCRYPTION_INFO_ENTRY), null);
  89. }
  90. public EncryptionInfo(LittleEndianInput dis, EncryptionMode preferredEncryptionMode) throws IOException {
  91. if (preferredEncryptionMode == xor) {
  92. versionMajor = xor.versionMajor;
  93. versionMinor = xor.versionMinor;
  94. } else {
  95. versionMajor = dis.readUShort();
  96. versionMinor = dis.readUShort();
  97. }
  98. if (versionMajor == xor.versionMajor && versionMinor == xor.versionMinor) {
  99. encryptionMode = xor;
  100. encryptionFlags = -1;
  101. } else if (versionMajor == binaryRC4.versionMajor && versionMinor == binaryRC4.versionMinor) {
  102. encryptionMode = binaryRC4;
  103. encryptionFlags = -1;
  104. } else if (2 <= versionMajor && versionMajor <= 4 && versionMinor == 2) {
  105. encryptionFlags = dis.readInt();
  106. encryptionMode = (preferredEncryptionMode == cryptoAPI || !flagAES.isSet(encryptionFlags)) ? cryptoAPI : standard;
  107. } else if (versionMajor == agile.versionMajor && versionMinor == agile.versionMinor){
  108. encryptionMode = agile;
  109. encryptionFlags = dis.readInt();
  110. } else {
  111. encryptionFlags = dis.readInt();
  112. throw new EncryptedDocumentException(
  113. "Unknown encryption: version major: "+versionMajor+
  114. " / version minor: "+versionMinor+
  115. " / fCrypto: "+flagCryptoAPI.isSet(encryptionFlags)+
  116. " / fExternal: "+flagExternal.isSet(encryptionFlags)+
  117. " / fDocProps: "+flagDocProps.isSet(encryptionFlags)+
  118. " / fAES: "+flagAES.isSet(encryptionFlags));
  119. }
  120. EncryptionInfoBuilder eib;
  121. try {
  122. eib = getBuilder(encryptionMode);
  123. } catch (Exception e) {
  124. throw new IOException(e);
  125. }
  126. eib.initialize(this, dis);
  127. }
  128. /**
  129. * Prepares for encryption, using the given Encryption Mode, and
  130. * all other parameters as default.
  131. * @see #EncryptionInfo(EncryptionMode, CipherAlgorithm, HashAlgorithm, int, int, ChainingMode)
  132. */
  133. public EncryptionInfo(EncryptionMode encryptionMode) {
  134. this(encryptionMode, null, null, -1, -1, null);
  135. }
  136. /**
  137. * Constructs an EncryptionInfo from scratch
  138. *
  139. * @param encryptionMode see {@link EncryptionMode} for values, {@link EncryptionMode#cryptoAPI} is for
  140. * internal use only, as it's record based
  141. * @param cipherAlgorithm the cipher algorithm
  142. * @param hashAlgorithm the hash algorithm
  143. * @param keyBits the bit count of the key
  144. * @param blockSize the size of a cipher block
  145. * @param chainingMode the chaining mode
  146. *
  147. * @throws EncryptedDocumentException if the given parameters mismatch, e.g. only certain combinations
  148. * of keyBits, blockSize are allowed for a given {@link CipherAlgorithm}
  149. */
  150. public EncryptionInfo(
  151. EncryptionMode encryptionMode
  152. , CipherAlgorithm cipherAlgorithm
  153. , HashAlgorithm hashAlgorithm
  154. , int keyBits
  155. , int blockSize
  156. , ChainingMode chainingMode
  157. ) {
  158. this.encryptionMode = encryptionMode;
  159. versionMajor = encryptionMode.versionMajor;
  160. versionMinor = encryptionMode.versionMinor;
  161. encryptionFlags = encryptionMode.encryptionFlags;
  162. EncryptionInfoBuilder eib;
  163. try {
  164. eib = getBuilder(encryptionMode);
  165. } catch (Exception e) {
  166. throw new EncryptedDocumentException(e);
  167. }
  168. eib.initialize(this, cipherAlgorithm, hashAlgorithm, keyBits, blockSize, chainingMode);
  169. }
  170. public EncryptionInfo(EncryptionInfo other) {
  171. encryptionMode = other.encryptionMode;
  172. versionMajor = other.versionMajor;
  173. versionMinor = other.versionMinor;
  174. encryptionFlags = other.encryptionFlags;
  175. header = (other.header == null) ? null : other.header.copy();
  176. verifier = (other.verifier == null) ? null : other.verifier.copy();
  177. if (other.decryptor != null) {
  178. decryptor = other.decryptor.copy();
  179. decryptor.setEncryptionInfo(this);
  180. }
  181. if (other.encryptor != null) {
  182. encryptor = other.encryptor.copy();
  183. encryptor.setEncryptionInfo(this);
  184. }
  185. }
  186. /**
  187. * Create the builder instance
  188. *
  189. * @param encryptionMode the encryption mode
  190. * @return an encryption info builder
  191. */
  192. private static EncryptionInfoBuilder getBuilder(EncryptionMode encryptionMode) {
  193. return encryptionMode.builder.get();
  194. }
  195. public int getVersionMajor() {
  196. return versionMajor;
  197. }
  198. public int getVersionMinor() {
  199. return versionMinor;
  200. }
  201. public int getEncryptionFlags() {
  202. return encryptionFlags;
  203. }
  204. public EncryptionHeader getHeader() {
  205. return header;
  206. }
  207. public EncryptionVerifier getVerifier() {
  208. return verifier;
  209. }
  210. public Decryptor getDecryptor() {
  211. return decryptor;
  212. }
  213. public Encryptor getEncryptor() {
  214. return encryptor;
  215. }
  216. public void setHeader(EncryptionHeader header) {
  217. this.header = header;
  218. }
  219. public void setVerifier(EncryptionVerifier verifier) {
  220. this.verifier = verifier;
  221. }
  222. public void setDecryptor(Decryptor decryptor) {
  223. this.decryptor = decryptor;
  224. }
  225. public void setEncryptor(Encryptor encryptor) {
  226. this.encryptor = encryptor;
  227. }
  228. public EncryptionMode getEncryptionMode() {
  229. return encryptionMode;
  230. }
  231. /**
  232. * @return true, if Document Summary / Summary are encrypted and stored in the {@code EncryptedStream} stream,
  233. * otherwise the Summaries aren't encrypted and located in their usual streams
  234. */
  235. public boolean isDocPropsEncrypted() {
  236. return !flagDocProps.isSet(getEncryptionFlags());
  237. }
  238. public EncryptionInfo copy() {
  239. return new EncryptionInfo(this);
  240. }
  241. @Override
  242. public Map<String, Supplier<?>> getGenericProperties() {
  243. final Map<String,Supplier<?>> m = new LinkedHashMap<>();
  244. m.put("encryptionMode", this::getEncryptionMode);
  245. m.put("versionMajor", this::getVersionMajor);
  246. m.put("versionMinor", this::getVersionMinor);
  247. m.put("encryptionFlags", getBitsAsString(this::getEncryptionFlags, FLAGS_MASKS, FLAGS_NAMES));
  248. m.put("header", this::getHeader);
  249. m.put("verifier", this::getVerifier);
  250. m.put("decryptor", this::getDecryptor);
  251. m.put("encryptor", this::getEncryptor);
  252. return Collections.unmodifiableMap(m);
  253. }
  254. }