Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

StandardDecryptor.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.standard;
  16. import static org.apache.poi.poifs.crypt.CryptoFunctions.hashPassword;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.security.GeneralSecurityException;
  20. import java.security.MessageDigest;
  21. import java.util.Arrays;
  22. import javax.crypto.Cipher;
  23. import javax.crypto.CipherInputStream;
  24. import javax.crypto.SecretKey;
  25. import javax.crypto.spec.SecretKeySpec;
  26. import org.apache.poi.EncryptedDocumentException;
  27. import org.apache.poi.poifs.crypt.ChainingMode;
  28. import org.apache.poi.poifs.crypt.CryptoFunctions;
  29. import org.apache.poi.poifs.crypt.Decryptor;
  30. import org.apache.poi.poifs.crypt.EncryptionHeader;
  31. import org.apache.poi.poifs.crypt.EncryptionInfoBuilder;
  32. import org.apache.poi.poifs.crypt.EncryptionVerifier;
  33. import org.apache.poi.poifs.crypt.HashAlgorithm;
  34. import org.apache.poi.poifs.filesystem.DirectoryNode;
  35. import org.apache.poi.poifs.filesystem.DocumentInputStream;
  36. import org.apache.poi.util.BoundedInputStream;
  37. import org.apache.poi.util.LittleEndian;
  38. /**
  39. */
  40. public class StandardDecryptor extends Decryptor {
  41. private long _length = -1;
  42. protected StandardDecryptor(EncryptionInfoBuilder builder) {
  43. super(builder);
  44. }
  45. public boolean verifyPassword(String password) {
  46. EncryptionVerifier ver = builder.getVerifier();
  47. SecretKey skey = generateSecretKey(password, ver, getKeySizeInBytes());
  48. Cipher cipher = getCipher(skey);
  49. try {
  50. byte encryptedVerifier[] = ver.getEncryptedVerifier();
  51. byte verifier[] = cipher.doFinal(encryptedVerifier);
  52. setVerifier(verifier);
  53. MessageDigest sha1 = CryptoFunctions.getMessageDigest(ver.getHashAlgorithm());
  54. byte[] calcVerifierHash = sha1.digest(verifier);
  55. byte encryptedVerifierHash[] = ver.getEncryptedVerifierHash();
  56. byte decryptedVerifierHash[] = cipher.doFinal(encryptedVerifierHash);
  57. // see 2.3.4.9 Password Verification (Standard Encryption)
  58. // ... The number of bytes used by the encrypted Verifier hash MUST be 32 ...
  59. // TODO: check and trim/pad the hashes to 32
  60. byte[] verifierHash = Arrays.copyOf(decryptedVerifierHash, calcVerifierHash.length);
  61. if (Arrays.equals(calcVerifierHash, verifierHash)) {
  62. setSecretKey(skey);
  63. return true;
  64. } else {
  65. return false;
  66. }
  67. } catch (GeneralSecurityException e) {
  68. throw new EncryptedDocumentException(e);
  69. }
  70. }
  71. protected static SecretKey generateSecretKey(String password, EncryptionVerifier ver, int keySize) {
  72. HashAlgorithm hashAlgo = ver.getHashAlgorithm();
  73. byte pwHash[] = hashPassword(password, hashAlgo, ver.getSalt(), ver.getSpinCount());
  74. byte[] blockKey = new byte[4];
  75. LittleEndian.putInt(blockKey, 0, 0);
  76. byte[] finalHash = CryptoFunctions.generateKey(pwHash, hashAlgo, blockKey, hashAlgo.hashSize);
  77. byte x1[] = fillAndXor(finalHash, (byte) 0x36);
  78. byte x2[] = fillAndXor(finalHash, (byte) 0x5c);
  79. byte[] x3 = new byte[x1.length + x2.length];
  80. System.arraycopy(x1, 0, x3, 0, x1.length);
  81. System.arraycopy(x2, 0, x3, x1.length, x2.length);
  82. byte[] key = Arrays.copyOf(x3, keySize);
  83. SecretKey skey = new SecretKeySpec(key, ver.getCipherAlgorithm().jceId);
  84. return skey;
  85. }
  86. protected static byte[] fillAndXor(byte hash[], byte fillByte) {
  87. byte[] buff = new byte[64];
  88. Arrays.fill(buff, fillByte);
  89. for (int i=0; i<hash.length; i++) {
  90. buff[i] = (byte) (buff[i] ^ hash[i]);
  91. }
  92. MessageDigest sha1 = CryptoFunctions.getMessageDigest(HashAlgorithm.sha1);
  93. return sha1.digest(buff);
  94. }
  95. private Cipher getCipher(SecretKey key) {
  96. EncryptionHeader em = builder.getHeader();
  97. ChainingMode cm = em.getChainingMode();
  98. assert(cm == ChainingMode.ecb);
  99. return CryptoFunctions.getCipher(key, em.getCipherAlgorithm(), cm, null, Cipher.DECRYPT_MODE);
  100. }
  101. public InputStream getDataStream(DirectoryNode dir) throws IOException {
  102. DocumentInputStream dis = dir.createDocumentInputStream(DEFAULT_POIFS_ENTRY);
  103. _length = dis.readLong();
  104. // limit wrong calculated ole entries - (bug #57080)
  105. // standard encryption always uses aes encoding, so blockSize is always 16
  106. // http://stackoverflow.com/questions/3283787/size-of-data-after-aes-encryption
  107. int blockSize = builder.getHeader().getCipherAlgorithm().blockSize;
  108. long cipherLen = (_length/blockSize + 1) * blockSize;
  109. Cipher cipher = getCipher(getSecretKey());
  110. InputStream boundedDis = new BoundedInputStream(dis, cipherLen);
  111. return new BoundedInputStream(new CipherInputStream(boundedDis, cipher), _length);
  112. }
  113. /**
  114. * @return the length of the stream returned by {@link #getDataStream(DirectoryNode)}
  115. */
  116. public long getLength(){
  117. if(_length == -1) throw new IllegalStateException("Decryptor.getDataStream() was not called");
  118. return _length;
  119. }
  120. }