選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

KeyData.java 5.5KB

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.agile;
  16. import static org.apache.poi.poifs.crypt.agile.EncryptionDocument.ENC_NS;
  17. import static org.apache.poi.poifs.crypt.agile.EncryptionDocument.getBinAttr;
  18. import static org.apache.poi.poifs.crypt.agile.EncryptionDocument.getIntAttr;
  19. import static org.apache.poi.poifs.crypt.agile.EncryptionDocument.getTag;
  20. import static org.apache.poi.poifs.crypt.agile.EncryptionDocument.setAttr;
  21. import static org.apache.poi.poifs.crypt.agile.EncryptionDocument.setBinAttr;
  22. import static org.apache.poi.poifs.crypt.agile.EncryptionDocument.setIntAttr;
  23. import org.apache.poi.EncryptedDocumentException;
  24. import org.apache.poi.poifs.crypt.ChainingMode;
  25. import org.apache.poi.poifs.crypt.CipherAlgorithm;
  26. import org.apache.poi.poifs.crypt.HashAlgorithm;
  27. import org.w3c.dom.Document;
  28. import org.w3c.dom.Element;
  29. /**
  30. * A complex type that specifies the encryption used within this element. The saltValue attribute is a base64-encoded
  31. * binary value that is randomly generated. The number of bytes required to decode the saltValue attribute MUST be equal
  32. * to the value of the saltSize attribute.
  33. */
  34. public class KeyData {
  35. private Integer saltSize;
  36. private Integer blockSize;
  37. private Integer keyBits;
  38. private Integer hashSize;
  39. private CipherAlgorithm cipherAlgorithm;
  40. private ChainingMode cipherChaining;
  41. private HashAlgorithm hashAlgorithm;
  42. private byte[] saltValue;
  43. public KeyData() {
  44. }
  45. public KeyData(Element parent) {
  46. Element keyData = getTag(parent, ENC_NS, "keyData");
  47. if (keyData == null) {
  48. throw new EncryptedDocumentException("Unable to parse encryption descriptor");
  49. }
  50. saltSize = getIntAttr(keyData, "saltSize");
  51. blockSize = getIntAttr(keyData, "blockSize");
  52. keyBits = getIntAttr(keyData, "keyBits");
  53. hashSize = getIntAttr(keyData, "hashSize");
  54. cipherAlgorithm = CipherAlgorithm.fromXmlId(keyData.getAttribute("cipherAlgorithm"), keyBits == null ? -1 : keyBits);
  55. cipherChaining = ChainingMode.fromXmlId(keyData.getAttribute("cipherChaining"));
  56. hashAlgorithm = HashAlgorithm.fromEcmaId(keyData.getAttribute("hashAlgorithm"));
  57. if (cipherAlgorithm == null || cipherChaining == null || hashAlgorithm == null) {
  58. throw new EncryptedDocumentException("Cipher algorithm, chaining mode or hash algorithm was null");
  59. }
  60. saltValue = getBinAttr(keyData, "saltValue");
  61. }
  62. void write(Element encryption) {
  63. Document doc = encryption.getOwnerDocument();
  64. Element keyData = (Element)encryption.appendChild(doc.createElementNS(ENC_NS, "keyData"));
  65. setIntAttr(keyData, "saltSize", saltSize);
  66. setIntAttr(keyData, "blockSize", blockSize);
  67. setIntAttr(keyData, "keyBits", keyBits);
  68. setIntAttr(keyData, "hashSize", hashSize);
  69. setAttr(keyData, "cipherAlgorithm", cipherAlgorithm == null ? null : cipherAlgorithm.xmlId);
  70. setAttr(keyData, "cipherChaining", cipherChaining == null ? null : cipherChaining.xmlId);
  71. setAttr(keyData, "hashAlgorithm", hashAlgorithm == null ? null : hashAlgorithm.ecmaString);
  72. setBinAttr(keyData, "saltValue", saltValue);
  73. }
  74. public Integer getSaltSize() {
  75. return saltSize;
  76. }
  77. public void setSaltSize(Integer saltSize) {
  78. this.saltSize = saltSize;
  79. }
  80. public Integer getBlockSize() {
  81. return blockSize;
  82. }
  83. public void setBlockSize(Integer blockSize) {
  84. this.blockSize = blockSize;
  85. }
  86. public Integer getKeyBits() {
  87. return keyBits;
  88. }
  89. public void setKeyBits(Integer keyBits) {
  90. this.keyBits = keyBits;
  91. }
  92. public Integer getHashSize() {
  93. return hashSize;
  94. }
  95. public void setHashSize(Integer hashSize) {
  96. this.hashSize = hashSize;
  97. }
  98. public CipherAlgorithm getCipherAlgorithm() {
  99. return cipherAlgorithm;
  100. }
  101. public void setCipherAlgorithm(CipherAlgorithm cipherAlgorithm) {
  102. this.cipherAlgorithm = cipherAlgorithm;
  103. }
  104. public ChainingMode getCipherChaining() {
  105. return cipherChaining;
  106. }
  107. public void setCipherChaining(ChainingMode cipherChaining) {
  108. this.cipherChaining = cipherChaining;
  109. }
  110. public HashAlgorithm getHashAlgorithm() {
  111. return hashAlgorithm;
  112. }
  113. public void setHashAlgorithm(HashAlgorithm hashAlgorithm) {
  114. this.hashAlgorithm = hashAlgorithm;
  115. }
  116. public byte[] getSaltValue() {
  117. return saltValue;
  118. }
  119. public void setSaltValue(byte[] saltValue) {
  120. this.saltValue = (saltValue == null) ? null : saltValue.clone();
  121. }
  122. }