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.

XSSFPaswordHelper.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.apache.poi.xssf.usermodel.helpers;
  20. import java.security.SecureRandom;
  21. import java.util.Arrays;
  22. import javax.xml.bind.DatatypeConverter;
  23. import javax.xml.namespace.QName;
  24. import org.apache.poi.poifs.crypt.CryptoFunctions;
  25. import org.apache.poi.poifs.crypt.HashAlgorithm;
  26. import org.apache.xmlbeans.XmlCursor;
  27. import org.apache.xmlbeans.XmlObject;
  28. public class XSSFPaswordHelper {
  29. /**
  30. * Sets the XORed or hashed password
  31. *
  32. * @param xobj the xmlbeans object which contains the password attributes
  33. * @param password the password, if null, the password attributes will be removed
  34. * @param hashAlgo the hash algorithm, if null the password will be XORed
  35. * @param prefix the prefix of the password attributes, may be null
  36. */
  37. public static void setPassword(XmlObject xobj, String password, HashAlgorithm hashAlgo, String prefix) {
  38. XmlCursor cur = xobj.newCursor();
  39. if (password == null) {
  40. cur.removeAttribute(getAttrName(prefix, "password"));
  41. cur.removeAttribute(getAttrName(prefix, "algorithmName"));
  42. cur.removeAttribute(getAttrName(prefix, "hashValue"));
  43. cur.removeAttribute(getAttrName(prefix, "saltValue"));
  44. cur.removeAttribute(getAttrName(prefix, "spinCount"));
  45. return;
  46. }
  47. cur.toFirstContentToken();
  48. if (hashAlgo == null) {
  49. int hash = CryptoFunctions.createXorVerifier1(password);
  50. cur.insertAttributeWithValue(getAttrName(prefix, "password"), Integer.toHexString(hash).toUpperCase());
  51. } else {
  52. SecureRandom random = new SecureRandom();
  53. byte salt[] = random.generateSeed(16);
  54. // Iterations specifies the number of times the hashing function shall be iteratively run (using each
  55. // iteration's result as the input for the next iteration).
  56. int spinCount = 100000;
  57. // Implementation Notes List:
  58. // --> In this third stage, the reversed byte order legacy hash from the second stage shall
  59. // be converted to Unicode hex string representation
  60. byte hash[] = CryptoFunctions.hashPassword(password, hashAlgo, salt, spinCount, false);
  61. cur.insertAttributeWithValue(getAttrName(prefix, "algorithmName"), hashAlgo.jceId);
  62. cur.insertAttributeWithValue(getAttrName(prefix, "hashValue"), DatatypeConverter.printBase64Binary(hash));
  63. cur.insertAttributeWithValue(getAttrName(prefix, "saltValue"), DatatypeConverter.printBase64Binary(salt));
  64. cur.insertAttributeWithValue(getAttrName(prefix, "spinCount"), ""+spinCount);
  65. }
  66. cur.dispose();
  67. }
  68. /**
  69. * Validates the password, i.e.
  70. * calculates the hash of the given password and compares it against the stored hash
  71. *
  72. * @param xobj the xmlbeans object which contains the password attributes
  73. * @param password the password, if null the method will always return false,
  74. * even if there's no password set
  75. * @param prefix the prefix of the password attributes, may be null
  76. *
  77. * @return true, if the hashes match
  78. */
  79. public static boolean validatePassword(XmlObject xobj, String password, String prefix) {
  80. // TODO: is "velvetSweatshop" the default password?
  81. if (password == null) return false;
  82. XmlCursor cur = xobj.newCursor();
  83. String xorHashVal = cur.getAttributeText(getAttrName(prefix, "password"));
  84. String algoName = cur.getAttributeText(getAttrName(prefix, "algorithmName"));
  85. String hashVal = cur.getAttributeText(getAttrName(prefix, "hashValue"));
  86. String saltVal = cur.getAttributeText(getAttrName(prefix, "saltValue"));
  87. String spinCount = cur.getAttributeText(getAttrName(prefix, "spinCount"));
  88. cur.dispose();
  89. if (xorHashVal != null) {
  90. int hash1 = Integer.parseInt(xorHashVal, 16);
  91. int hash2 = CryptoFunctions.createXorVerifier1(password);
  92. return hash1 == hash2;
  93. } else {
  94. if (hashVal == null || algoName == null || saltVal == null || spinCount == null) {
  95. return false;
  96. }
  97. byte hash1[] = DatatypeConverter.parseBase64Binary(hashVal);
  98. HashAlgorithm hashAlgo = HashAlgorithm.fromString(algoName);
  99. byte salt[] = DatatypeConverter.parseBase64Binary(saltVal);
  100. int spinCnt = Integer.parseInt(spinCount);
  101. byte hash2[] = CryptoFunctions.hashPassword(password, hashAlgo, salt, spinCnt, false);
  102. return Arrays.equals(hash1, hash2);
  103. }
  104. }
  105. private static QName getAttrName(String prefix, String name) {
  106. if (prefix == null || "".equals(prefix)) {
  107. return new QName(name);
  108. } else {
  109. return new QName(prefix+Character.toUpperCase(name.charAt(0))+name.substring(1));
  110. }
  111. }
  112. }