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.

KeyInfoSignatureFacet.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. /* ====================================================================
  16. This product contains an ASLv2 licensed version of the OOXML signer
  17. package from the eID Applet project
  18. http://code.google.com/p/eid-applet/source/browse/trunk/README.txt
  19. Copyright (C) 2008-2014 FedICT.
  20. ================================================================= */
  21. package org.apache.poi.poifs.crypt.dsig.facets;
  22. import java.security.Key;
  23. import java.security.KeyException;
  24. import java.security.cert.X509Certificate;
  25. import java.util.ArrayList;
  26. import java.util.List;
  27. import java.util.Map;
  28. import javax.xml.crypto.MarshalException;
  29. import javax.xml.crypto.XMLStructure;
  30. import javax.xml.crypto.dom.DOMStructure;
  31. import javax.xml.crypto.dsig.dom.DOMSignContext;
  32. import javax.xml.crypto.dsig.keyinfo.KeyInfo;
  33. import javax.xml.crypto.dsig.keyinfo.KeyInfoFactory;
  34. import javax.xml.crypto.dsig.keyinfo.KeyValue;
  35. import javax.xml.crypto.dsig.keyinfo.X509Data;
  36. import org.apache.jcp.xml.dsig.internal.dom.DOMKeyInfo;
  37. import org.apache.poi.util.POILogFactory;
  38. import org.apache.poi.util.POILogger;
  39. import org.w3c.dom.Document;
  40. import org.w3c.dom.Element;
  41. import org.w3c.dom.Node;
  42. import org.w3c.dom.NodeList;
  43. /**
  44. * Signature Facet implementation that adds ds:KeyInfo to the XML signature.
  45. *
  46. * @author Frank Cornelis
  47. *
  48. */
  49. public class KeyInfoSignatureFacet extends SignatureFacet {
  50. private static final POILogger LOG = POILogFactory.getLogger(KeyInfoSignatureFacet.class);
  51. @Override
  52. public void postSign(Document document)
  53. throws MarshalException {
  54. LOG.log(POILogger.DEBUG, "postSign");
  55. NodeList nl = document.getElementsByTagNameNS(XML_DIGSIG_NS, "Object");
  56. /*
  57. * Make sure we insert right after the ds:SignatureValue element, just
  58. * before the first ds:Object element.
  59. */
  60. Node nextSibling = (nl.getLength() == 0) ? null : nl.item(0);
  61. /*
  62. * Construct the ds:KeyInfo element using JSR 105.
  63. */
  64. KeyInfoFactory keyInfoFactory = signatureConfig.getKeyInfoFactory();
  65. List<Object> x509DataObjects = new ArrayList<>();
  66. X509Certificate signingCertificate = signatureConfig.getSigningCertificateChain().get(0);
  67. List<XMLStructure> keyInfoContent = new ArrayList<>();
  68. if (signatureConfig.isIncludeKeyValue()) {
  69. KeyValue keyValue;
  70. try {
  71. keyValue = keyInfoFactory.newKeyValue(signingCertificate.getPublicKey());
  72. } catch (KeyException e) {
  73. throw new RuntimeException("key exception: " + e.getMessage(), e);
  74. }
  75. keyInfoContent.add(keyValue);
  76. }
  77. if (signatureConfig.isIncludeIssuerSerial()) {
  78. x509DataObjects.add(keyInfoFactory.newX509IssuerSerial(
  79. signingCertificate.getIssuerX500Principal().toString(),
  80. signingCertificate.getSerialNumber()));
  81. }
  82. if (signatureConfig.isIncludeEntireCertificateChain()) {
  83. x509DataObjects.addAll(signatureConfig.getSigningCertificateChain());
  84. } else {
  85. x509DataObjects.add(signingCertificate);
  86. }
  87. if (!x509DataObjects.isEmpty()) {
  88. X509Data x509Data = keyInfoFactory.newX509Data(x509DataObjects);
  89. keyInfoContent.add(x509Data);
  90. }
  91. KeyInfo keyInfo = keyInfoFactory.newKeyInfo(keyInfoContent);
  92. DOMKeyInfo domKeyInfo = (DOMKeyInfo)keyInfo;
  93. Key key = new Key() {
  94. private static final long serialVersionUID = 1L;
  95. public String getAlgorithm() {
  96. return null;
  97. }
  98. public byte[] getEncoded() {
  99. return null;
  100. }
  101. public String getFormat() {
  102. return null;
  103. }
  104. };
  105. Element n = document.getDocumentElement();
  106. DOMSignContext domSignContext = (nextSibling == null)
  107. ? new DOMSignContext(key, n)
  108. : new DOMSignContext(key, n, nextSibling);
  109. signatureConfig.getNamespacePrefixes().forEach(domSignContext::putNamespacePrefix);
  110. DOMStructure domStructure = new DOMStructure(n);
  111. domKeyInfo.marshal(domStructure, domSignContext);
  112. // move keyinfo into the right place
  113. if (nextSibling != null) {
  114. NodeList kiNl = document.getElementsByTagNameNS(XML_DIGSIG_NS, "KeyInfo");
  115. if (kiNl.getLength() != 1) {
  116. throw new RuntimeException("KeyInfo wasn't set");
  117. }
  118. nextSibling.getParentNode().insertBefore(kiNl.item(0), nextSibling);
  119. }
  120. }
  121. }