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.

AgileEncryptionHeader.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.agile;
  16. import java.util.Map;
  17. import java.util.function.Supplier;
  18. import org.apache.poi.EncryptedDocumentException;
  19. import org.apache.poi.poifs.crypt.ChainingMode;
  20. import org.apache.poi.poifs.crypt.CipherAlgorithm;
  21. import org.apache.poi.poifs.crypt.EncryptionHeader;
  22. import org.apache.poi.poifs.crypt.HashAlgorithm;
  23. import org.apache.poi.util.GenericRecordUtil;
  24. public class AgileEncryptionHeader extends EncryptionHeader {
  25. private byte[] encryptedHmacKey;
  26. private byte[] encryptedHmacValue;
  27. public AgileEncryptionHeader(String descriptor) {
  28. this(AgileEncryptionInfoBuilder.parseDescriptor(descriptor));
  29. }
  30. public AgileEncryptionHeader(AgileEncryptionHeader other) {
  31. super(other);
  32. encryptedHmacKey = (other.encryptedHmacKey == null) ? null : other.encryptedHmacKey.clone();
  33. encryptedHmacValue = (other.encryptedHmacValue == null) ? null : other.encryptedHmacValue.clone();
  34. }
  35. protected AgileEncryptionHeader(EncryptionDocument ed) {
  36. KeyData keyData;
  37. try {
  38. keyData = ed.getKeyData();
  39. if (keyData == null) {
  40. throw new NullPointerException("keyData not set");
  41. }
  42. } catch (Exception e) {
  43. throw new EncryptedDocumentException("Unable to parse keyData");
  44. }
  45. int keyBits = keyData.getKeyBits();
  46. CipherAlgorithm ca = keyData.getCipherAlgorithm();
  47. setCipherAlgorithm(ca);
  48. setCipherProvider(ca.provider);
  49. setKeySize(keyBits);
  50. setFlags(0);
  51. setSizeExtra(0);
  52. setCspName(null);
  53. setBlockSize(keyData.getBlockSize() == null ? 0 : keyData.getBlockSize());
  54. setChainingMode(keyData.getCipherChaining());
  55. if (getChainingMode() != ChainingMode.cbc && getChainingMode() != ChainingMode.cfb) {
  56. throw new EncryptedDocumentException("Unsupported chaining mode - "+ keyData.getCipherChaining());
  57. }
  58. int hashSize = keyData.getHashSize();
  59. HashAlgorithm ha = keyData.getHashAlgorithm();
  60. setHashAlgorithm(ha);
  61. if (getHashAlgorithm().hashSize != hashSize) {
  62. throw new EncryptedDocumentException("Unsupported hash algorithm: " +
  63. keyData.getHashAlgorithm() + " @ " + hashSize + " bytes");
  64. }
  65. if (keyData.getSaltSize() == null) {
  66. throw new EncryptedDocumentException("Invalid salt length: " + keyData.getSaltSize());
  67. }
  68. int saltLength = keyData.getSaltSize();
  69. setKeySalt(keyData.getSaltValue());
  70. if (getKeySalt().length != saltLength) {
  71. throw new EncryptedDocumentException("Invalid salt length: " + getKeySalt().length + " and " + saltLength);
  72. }
  73. DataIntegrity di = ed.getDataIntegrity();
  74. setEncryptedHmacKey(di.getEncryptedHmacKey());
  75. setEncryptedHmacValue(di.getEncryptedHmacValue());
  76. }
  77. public AgileEncryptionHeader(CipherAlgorithm algorithm, HashAlgorithm hashAlgorithm, int keyBits, int blockSize, ChainingMode chainingMode) {
  78. setCipherAlgorithm(algorithm);
  79. setHashAlgorithm(hashAlgorithm);
  80. setKeySize(keyBits);
  81. setBlockSize(blockSize);
  82. setChainingMode(chainingMode);
  83. }
  84. // make method visible for this package
  85. @Override
  86. public void setKeySalt(byte[] salt) {
  87. if (salt == null || salt.length != getBlockSize()) {
  88. throw new EncryptedDocumentException("invalid verifier salt");
  89. }
  90. super.setKeySalt(salt);
  91. }
  92. public byte[] getEncryptedHmacKey() {
  93. return encryptedHmacKey;
  94. }
  95. protected void setEncryptedHmacKey(byte[] encryptedHmacKey) {
  96. this.encryptedHmacKey = (encryptedHmacKey == null) ? null : encryptedHmacKey.clone();
  97. }
  98. public byte[] getEncryptedHmacValue() {
  99. return encryptedHmacValue;
  100. }
  101. protected void setEncryptedHmacValue(byte[] encryptedHmacValue) {
  102. this.encryptedHmacValue = (encryptedHmacValue == null) ? null : encryptedHmacValue.clone();
  103. }
  104. @Override
  105. public Map<String, Supplier<?>> getGenericProperties() {
  106. return GenericRecordUtil.getGenericProperties(
  107. "base", super::getGenericProperties,
  108. "encryptedHmacKey", this::getEncryptedHmacKey,
  109. "encryptedHmacValue", this::getEncryptedHmacValue
  110. );
  111. }
  112. @Override
  113. public AgileEncryptionHeader copy() {
  114. return new AgileEncryptionHeader(this);
  115. }
  116. }