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.

DocumentEncryptionAtom.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.hslf.record;
  16. import java.io.ByteArrayInputStream;
  17. import java.io.IOException;
  18. import java.io.OutputStream;
  19. import java.util.Map;
  20. import org.apache.poi.EncryptedDocumentException;
  21. import org.apache.poi.poifs.crypt.CipherAlgorithm;
  22. import org.apache.poi.poifs.crypt.EncryptionInfo;
  23. import org.apache.poi.poifs.crypt.EncryptionMode;
  24. import org.apache.poi.poifs.crypt.HashAlgorithm;
  25. import org.apache.poi.poifs.crypt.cryptoapi.CryptoAPIEncryptionHeader;
  26. import org.apache.poi.poifs.crypt.cryptoapi.CryptoAPIEncryptionVerifier;
  27. import org.apache.poi.util.LittleEndian;
  28. import org.apache.poi.util.LittleEndianByteArrayOutputStream;
  29. import org.apache.poi.util.LittleEndianInputStream;
  30. /**
  31. * A Document Encryption Atom (type 12052). Holds information
  32. * on the Encryption of a Document
  33. *
  34. * @author Nick Burch
  35. */
  36. public final class DocumentEncryptionAtom extends PositionDependentRecordAtom {
  37. private static final long _type = 12052l;
  38. private final byte[] _header;
  39. private EncryptionInfo ei;
  40. /**
  41. * For the Document Encryption Atom
  42. */
  43. protected DocumentEncryptionAtom(byte[] source, int start, int len) {
  44. // Get the header
  45. _header = new byte[8];
  46. System.arraycopy(source,start,_header,0,8);
  47. ByteArrayInputStream bis = new ByteArrayInputStream(source, start+8, len-8);
  48. try (LittleEndianInputStream leis = new LittleEndianInputStream(bis)) {
  49. ei = new EncryptionInfo(leis, EncryptionMode.cryptoAPI);
  50. } catch (IOException e) {
  51. throw new EncryptedDocumentException(e);
  52. }
  53. }
  54. public DocumentEncryptionAtom() {
  55. _header = new byte[8];
  56. LittleEndian.putShort(_header, 0, (short)0x000F);
  57. LittleEndian.putShort(_header, 2, (short)_type);
  58. // record length not yet known ...
  59. ei = new EncryptionInfo(EncryptionMode.cryptoAPI);
  60. }
  61. /**
  62. * Initializes the encryption settings
  63. *
  64. * @param keyBits see {@link CipherAlgorithm#rc4} for allowed values, use -1 for default size
  65. */
  66. public void initializeEncryptionInfo(int keyBits) {
  67. ei = new EncryptionInfo(EncryptionMode.cryptoAPI, CipherAlgorithm.rc4, HashAlgorithm.sha1, keyBits, -1, null);
  68. }
  69. /**
  70. * Return the length of the encryption key, in bits
  71. */
  72. public int getKeyLength() {
  73. return ei.getHeader().getKeySize();
  74. }
  75. /**
  76. * Return the name of the encryption provider used
  77. */
  78. public String getEncryptionProviderName() {
  79. return ei.getHeader().getCspName();
  80. }
  81. /**
  82. * @return the {@link EncryptionInfo} object for details about encryption settings
  83. */
  84. public EncryptionInfo getEncryptionInfo() {
  85. return ei;
  86. }
  87. /**
  88. * We are of type 12052
  89. */
  90. public long getRecordType() { return _type; }
  91. /**
  92. * Write the contents of the record back, so it can be written
  93. * to disk
  94. */
  95. public void writeOut(OutputStream out) throws IOException {
  96. // Data
  97. byte data[] = new byte[1024];
  98. LittleEndianByteArrayOutputStream bos = new LittleEndianByteArrayOutputStream(data, 0);
  99. bos.writeShort(ei.getVersionMajor());
  100. bos.writeShort(ei.getVersionMinor());
  101. bos.writeInt(ei.getEncryptionFlags());
  102. ((CryptoAPIEncryptionHeader)ei.getHeader()).write(bos);
  103. ((CryptoAPIEncryptionVerifier)ei.getVerifier()).write(bos);
  104. // Header
  105. LittleEndian.putInt(_header, 4, bos.getWriteIndex());
  106. out.write(_header);
  107. out.write(data, 0, bos.getWriteIndex());
  108. bos.close();
  109. }
  110. @Override
  111. public void updateOtherRecordReferences(Map<Integer,Integer> oldToNewReferencesLookup) {
  112. // nothing to update
  113. }
  114. }