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.

OOXMLURIDereferencer.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.net.URI;
  25. import java.net.URISyntaxException;
  26. import javax.xml.crypto.Data;
  27. import javax.xml.crypto.OctetStreamData;
  28. import javax.xml.crypto.URIDereferencer;
  29. import javax.xml.crypto.URIReference;
  30. import javax.xml.crypto.URIReferenceException;
  31. import javax.xml.crypto.XMLCryptoContext;
  32. import org.apache.commons.io.IOUtils;
  33. import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
  34. import org.apache.logging.log4j.LogManager;
  35. import org.apache.logging.log4j.Logger;
  36. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  37. import org.apache.poi.openxml4j.opc.PackagePart;
  38. import org.apache.poi.openxml4j.opc.PackagePartName;
  39. import org.apache.poi.openxml4j.opc.PackagingURIHelper;
  40. /**
  41. * JSR105 URI dereferencer for Office Open XML documents.
  42. */
  43. public class OOXMLURIDereferencer implements URIDereferencer {
  44. private static final Logger LOG = LogManager.getLogger(OOXMLURIDereferencer.class);
  45. private SignatureInfo signatureInfo;
  46. private URIDereferencer baseUriDereferencer;
  47. public void setSignatureInfo(SignatureInfo signatureInfo) {
  48. this.signatureInfo = signatureInfo;
  49. baseUriDereferencer = signatureInfo.getSignatureFactory().getURIDereferencer();
  50. }
  51. @Override
  52. public Data dereference(URIReference uriReference, XMLCryptoContext context) throws URIReferenceException {
  53. if (uriReference == null) {
  54. throw new NullPointerException("URIReference cannot be null - call setSignatureInfo(...) before");
  55. }
  56. if (context == null) {
  57. throw new NullPointerException("XMLCryptoContext cannot be null");
  58. }
  59. URI uri;
  60. try {
  61. uri = new URI(uriReference.getURI());
  62. } catch (URISyntaxException e) {
  63. throw new URIReferenceException("could not URL decode the uri: "+uriReference.getURI(), e);
  64. }
  65. PackagePart part = findPart(uri);
  66. if (part == null) {
  67. LOG.atDebug().log("cannot resolve {}, delegating to base DOM URI dereferencer", uri);
  68. return baseUriDereferencer.dereference(uriReference, context);
  69. }
  70. InputStream dataStream = null;
  71. try {
  72. dataStream = part.getInputStream();
  73. // workaround for office 2007 pretty-printed .rels files
  74. if (part.getPartName().toString().endsWith(".rels")) {
  75. // although xmlsec has an option to ignore line breaks, currently this
  76. // only affects .rels files, so we only modify these
  77. try (UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream()) {
  78. for (int ch; (ch = dataStream.read()) != -1; ) {
  79. if (ch == 10 || ch == 13) continue;
  80. bos.write(ch);
  81. }
  82. dataStream = bos.toInputStream();
  83. }
  84. }
  85. } catch (IOException e) {
  86. IOUtils.closeQuietly(dataStream);
  87. throw new URIReferenceException("I/O error: " + e.getMessage(), e);
  88. }
  89. return new OctetStreamData(dataStream, uri.toString(), null);
  90. }
  91. private PackagePart findPart(URI uri) {
  92. LOG.atDebug().log("dereference: {}", uri);
  93. String path = uri.getPath();
  94. if (path == null || path.isEmpty()) {
  95. LOG.atDebug().log("illegal part name (expected): {}", uri);
  96. return null;
  97. }
  98. PackagePartName ppn;
  99. try {
  100. ppn = PackagingURIHelper.createPartName(path);
  101. return signatureInfo.getOpcPackage().getPart(ppn);
  102. } catch (InvalidFormatException e) {
  103. LOG.atWarn().log("illegal part name (not expected) in {}", uri);
  104. return null;
  105. }
  106. }
  107. }