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.

ChunkedCipherInputStream.java 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 java.io.EOFException;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.security.GeneralSecurityException;
  20. import javax.crypto.Cipher;
  21. import org.apache.poi.EncryptedDocumentException;
  22. import org.apache.poi.util.IOUtils;
  23. import org.apache.poi.util.Internal;
  24. import org.apache.poi.util.LittleEndianInputStream;
  25. @Internal
  26. public abstract class ChunkedCipherInputStream extends LittleEndianInputStream {
  27. private final int chunkSize;
  28. private final int chunkBits;
  29. private final long size;
  30. private final byte[] chunk, plain;
  31. private final Cipher cipher;
  32. private int lastIndex;
  33. private long pos;
  34. private boolean chunkIsValid;
  35. public ChunkedCipherInputStream(InputStream stream, long size, int chunkSize)
  36. throws GeneralSecurityException {
  37. this(stream, size, chunkSize, 0);
  38. }
  39. public ChunkedCipherInputStream(InputStream stream, long size, int chunkSize, int initialPos)
  40. throws GeneralSecurityException {
  41. super(stream);
  42. this.size = size;
  43. this.pos = initialPos;
  44. this.chunkSize = chunkSize;
  45. int cs = chunkSize == -1 ? 4096 : chunkSize;
  46. this.chunk = IOUtils.safelyAllocate(cs, CryptoFunctions.MAX_RECORD_LENGTH);
  47. this.plain = IOUtils.safelyAllocate(cs, CryptoFunctions.MAX_RECORD_LENGTH);
  48. this.chunkBits = Integer.bitCount(chunk.length-1);
  49. this.lastIndex = (int)(pos >> chunkBits);
  50. this.cipher = initCipherForBlock(null, lastIndex);
  51. }
  52. public final Cipher initCipherForBlock(int block) throws IOException, GeneralSecurityException {
  53. if (chunkSize != -1) {
  54. throw new GeneralSecurityException("the cipher block can only be set for streaming encryption, e.g. CryptoAPI...");
  55. }
  56. chunkIsValid = false;
  57. return initCipherForBlock(cipher, block);
  58. }
  59. protected abstract Cipher initCipherForBlock(Cipher existing, int block)
  60. throws GeneralSecurityException;
  61. @Override
  62. public int read() throws IOException {
  63. byte[] b = { 0 };
  64. return (read(b) == 1) ? (b[0] & 0xFF) : -1;
  65. }
  66. // do not implement! -> recursion
  67. // public int read(byte[] b) throws IOException;
  68. @Override
  69. public int read(byte[] b, int off, int len) throws IOException {
  70. return read(b, off, len, false);
  71. }
  72. private int read(byte[] b, int off, int len, boolean readPlain) throws IOException {
  73. int total = 0;
  74. if (remainingBytes() <= 0) {
  75. return -1;
  76. }
  77. final int chunkMask = getChunkMask();
  78. while (len > 0) {
  79. if (!chunkIsValid) {
  80. try {
  81. nextChunk();
  82. chunkIsValid = true;
  83. } catch (GeneralSecurityException e) {
  84. throw new EncryptedDocumentException(e.getMessage(), e);
  85. }
  86. }
  87. int count = (int)(chunk.length - (pos & chunkMask));
  88. int avail = remainingBytes();
  89. if (avail == 0) {
  90. return total;
  91. }
  92. count = Math.min(avail, Math.min(count, len));
  93. System.arraycopy(readPlain ? plain : chunk, (int)(pos & chunkMask), b, off, count);
  94. off += count;
  95. len -= count;
  96. pos += count;
  97. if ((pos & chunkMask) == 0) {
  98. chunkIsValid = false;
  99. }
  100. total += count;
  101. }
  102. return total;
  103. }
  104. @Override
  105. public long skip(final long n) {
  106. long start = pos;
  107. long skip = Math.min(remainingBytes(), n);
  108. if ((((pos + skip) ^ start) & ~getChunkMask()) != 0) {
  109. chunkIsValid = false;
  110. }
  111. pos += skip;
  112. return skip;
  113. }
  114. @Override
  115. public int available() {
  116. return remainingBytes();
  117. }
  118. /**
  119. * Helper method for forbidden available call - we know the size beforehand, so it's ok ...
  120. *
  121. * @return the remaining byte until EOF
  122. */
  123. private int remainingBytes() {
  124. return (int)(size - pos);
  125. }
  126. @Override
  127. public boolean markSupported() {
  128. return false;
  129. }
  130. @Override
  131. public synchronized void mark(int readlimit) {
  132. throw new UnsupportedOperationException();
  133. }
  134. @Override
  135. public synchronized void reset() {
  136. throw new UnsupportedOperationException();
  137. }
  138. protected int getChunkMask() {
  139. return chunk.length-1;
  140. }
  141. private void nextChunk() throws GeneralSecurityException, IOException {
  142. if (chunkSize != -1) {
  143. int index = (int)(pos >> chunkBits);
  144. initCipherForBlock(cipher, index);
  145. if (lastIndex != index) {
  146. long skipN = (index - lastIndex) << chunkBits;
  147. if (super.skip(skipN) < skipN) {
  148. throw new EOFException("buffer underrun");
  149. }
  150. }
  151. lastIndex = index + 1;
  152. }
  153. final int todo = (int)Math.min(size, chunk.length);
  154. int readBytes, totalBytes = 0;
  155. do {
  156. readBytes = super.read(plain, totalBytes, todo-totalBytes);
  157. totalBytes += Math.max(0, readBytes);
  158. } while (readBytes != -1 && totalBytes < todo);
  159. if (readBytes == -1 && pos+totalBytes < size && size < Integer.MAX_VALUE) {
  160. throw new EOFException("buffer underrun");
  161. }
  162. System.arraycopy(plain, 0, chunk, 0, totalBytes);
  163. invokeCipher(totalBytes, totalBytes == chunkSize);
  164. }
  165. /**
  166. * Helper function for overriding the cipher invocation, i.e. XOR doesn't use a cipher
  167. * and uses its own implementation
  168. */
  169. protected int invokeCipher(int totalBytes, boolean doFinal) throws GeneralSecurityException {
  170. if (doFinal) {
  171. return cipher.doFinal(chunk, 0, totalBytes, chunk);
  172. } else {
  173. return cipher.update(chunk, 0, totalBytes, chunk);
  174. }
  175. }
  176. /**
  177. * Used when BIFF header fields (sid, size) are being read. The internal
  178. * {@link Cipher} instance must step even when unencrypted bytes are read
  179. *
  180. */
  181. @Override
  182. public void readPlain(byte[] b, int off, int len) {
  183. if (len <= 0) {
  184. return;
  185. }
  186. try {
  187. int readBytes, total = 0;
  188. do {
  189. readBytes = read(b, off, len, true);
  190. total += Math.max(0, readBytes);
  191. } while (readBytes > -1 && total < len);
  192. if (total < len) {
  193. throw new EOFException("buffer underrun");
  194. }
  195. } catch (IOException e) {
  196. // need to wrap checked exception, because of LittleEndianInput interface :(
  197. throw new RuntimeException(e);
  198. }
  199. }
  200. /**
  201. * Some ciphers (actually just XOR) are based on the record size,
  202. * which needs to be set before decryption
  203. *
  204. * @param recordSize the size of the next record
  205. */
  206. public void setNextRecordSize(int recordSize) {
  207. }
  208. /**
  209. * @return the chunk bytes
  210. */
  211. protected byte[] getChunk() {
  212. return chunk;
  213. }
  214. /**
  215. * @return the plain bytes
  216. */
  217. protected byte[] getPlain() {
  218. return plain;
  219. }
  220. /**
  221. * @return the absolute position in the stream
  222. */
  223. public long getPos() {
  224. return pos;
  225. }
  226. }