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.

DigestOutputStream.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.dsig;
  16. import java.io.IOException;
  17. import java.io.OutputStream;
  18. import java.security.GeneralSecurityException;
  19. import java.security.MessageDigest;
  20. import java.security.PrivateKey;
  21. import javax.crypto.Cipher;
  22. import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
  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.CryptoFunctions;
  27. import org.apache.poi.poifs.crypt.HashAlgorithm;
  28. import org.ietf.jgss.GSSException;
  29. import org.ietf.jgss.Oid;
  30. /* package */ class DigestOutputStream extends OutputStream {
  31. final HashAlgorithm algo;
  32. final PrivateKey key;
  33. private MessageDigest md;
  34. DigestOutputStream(final HashAlgorithm algo, final PrivateKey key) {
  35. this.algo = algo;
  36. this.key = key;
  37. }
  38. public void init() throws GeneralSecurityException {
  39. if (isMSCapi(key)) {
  40. // SunMSCAPI can't be used to sign the calculated digest
  41. throw new EncryptedDocumentException(
  42. "Windows keystore entries can't be signed with the "+algo+" hash. Please "+
  43. "use one digest algorithm of sha1 / sha256 / sha384 / sha512.");
  44. }
  45. md = CryptoFunctions.getMessageDigest(algo);
  46. }
  47. @Override
  48. public void write(final int b) throws IOException {
  49. md.update((byte)b);
  50. }
  51. @Override
  52. public void write(final byte[] data, final int off, final int len) throws IOException {
  53. md.update(data, off, len);
  54. }
  55. public byte[] sign() throws IOException, GeneralSecurityException {
  56. try (UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream()) {
  57. bos.write(getHashMagic());
  58. bos.write(md.digest());
  59. final Cipher cipher = CryptoFunctions.getCipher(key, CipherAlgorithm.rsa
  60. , ChainingMode.ecb, null, Cipher.ENCRYPT_MODE, "PKCS1Padding");
  61. return cipher.doFinal(bos.toByteArray());
  62. }
  63. }
  64. static boolean isMSCapi(final PrivateKey key) {
  65. return key != null && key.getClass().getName().contains("mscapi");
  66. }
  67. /**
  68. * Each digest method has its own ASN1 header
  69. *
  70. * @return the ASN1 header bytes for the signatureValue / digestInfo
  71. *
  72. * @see <a href="https://tools.ietf.org/html/rfc2313#section-10.1.2">Data encoding</a>
  73. */
  74. byte[] getHashMagic() {
  75. // in an earlier release the hashMagic (aka DigestAlgorithmIdentifier) contained only
  76. // an object identifier, but to conform with the header generated by the
  77. // javax-signature API, the empty <associated parameters> are also included
  78. try (UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream()) {
  79. final byte[] oidBytes = new Oid(algo.rsaOid).getDER();
  80. bos.write(0x30);
  81. bos.write(algo.hashSize+oidBytes.length+6);
  82. bos.write(0x30);
  83. bos.write(oidBytes.length+2);
  84. bos.write(oidBytes);
  85. bos.write(new byte[] {5,0,4});
  86. bos.write(algo.hashSize);
  87. return bos.toByteArray();
  88. } catch (GSSException|IOException e) {
  89. throw new IllegalStateException(e);
  90. }
  91. }
  92. }