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.

Office2010SignatureFacet.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 static org.apache.poi.ooxml.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
  23. import javax.xml.crypto.MarshalException;
  24. import org.apache.poi.poifs.crypt.dsig.SignatureInfo;
  25. import org.apache.xmlbeans.XmlException;
  26. import org.etsi.uri.x01903.v13.QualifyingPropertiesType;
  27. import org.etsi.uri.x01903.v13.UnsignedPropertiesType;
  28. import org.etsi.uri.x01903.v13.UnsignedSignaturePropertiesType;
  29. import org.w3c.dom.Document;
  30. import org.w3c.dom.Node;
  31. import org.w3c.dom.NodeList;
  32. /**
  33. * Work-around for Office2010 to accept the XAdES-BES/EPES signature.
  34. *
  35. * xades:UnsignedProperties/xades:UnsignedSignatureProperties needs to be present.
  36. *
  37. * @author Frank Cornelis
  38. */
  39. public class Office2010SignatureFacet implements SignatureFacet {
  40. @Override
  41. public void postSign(SignatureInfo signatureInfo, Document document)
  42. throws MarshalException {
  43. // check for XAdES-BES
  44. NodeList nl = document.getElementsByTagNameNS(XADES_132_NS, "QualifyingProperties");
  45. if (nl.getLength() != 1) {
  46. throw new MarshalException("no XAdES-BES extension present");
  47. }
  48. QualifyingPropertiesType qualProps;
  49. try {
  50. qualProps = QualifyingPropertiesType.Factory.parse(nl.item(0), DEFAULT_XML_OPTIONS);
  51. } catch (XmlException e) {
  52. throw new MarshalException(e);
  53. }
  54. // create basic XML container structure
  55. UnsignedPropertiesType unsignedProps = qualProps.getUnsignedProperties();
  56. if (unsignedProps == null) {
  57. unsignedProps = qualProps.addNewUnsignedProperties();
  58. }
  59. UnsignedSignaturePropertiesType unsignedSigProps = unsignedProps.getUnsignedSignatureProperties();
  60. if (unsignedSigProps == null) {
  61. /* unsignedSigProps = */ unsignedProps.addNewUnsignedSignatureProperties();
  62. }
  63. Node n = document.importNode(qualProps.getDomNode().getFirstChild(), true);
  64. nl.item(0).getParentNode().replaceChild(n, nl.item(0));
  65. }
  66. }