Beispiel #1
0
  @Override
  public XMLSignatureInput engineResolveURI(ResourceResolverContext context)
      throws ResourceResolverException {

    final Attr uriAttr = context.attr;
    final String baseUriString = context.baseUri;
    String documentUri = uriAttr.getNodeValue();
    documentUri = decodeUrl(documentUri);
    final DSSDocument document = getDocument(documentUri);
    if (document != null) {

      // The input stream is closed automatically by XMLSignatureInput class

      // TODO-Bob (05/09/2014):  There is an error concerning the input streams base64 encoded. Some
      // extra bytes are added within the santuario which breaks the HASH.
      // TODO-Vin (05/09/2014): Can you create an isolated test-case JIRA DSS-?
      InputStream inputStream = document.openStream();
      //			final byte[] bytes = DSSUtils.toByteArray(inputStream);
      //			final String string = new String(bytes);
      //			inputStream = DSSUtils.toInputStream(bytes);
      final XMLSignatureInput result = new XMLSignatureInput(inputStream);
      result.setSourceURI(documentUri);
      final MimeType mimeType = document.getMimeType();
      if (mimeType != null) {
        result.setMIMEType(mimeType.getMimeTypeString());
      }
      return result;
    } else {

      Object exArgs[] = {"The uriNodeValue " + documentUri + " is not configured for offline work"};
      throw new ResourceResolverException(
          "generic.EmptyMessage", exArgs, documentUri, baseUriString);
    }
  }
 protected void circumventBugIfNeeded(XMLSignatureInput input)
     throws CanonicalizationException, ParserConfigurationException, IOException, SAXException {
   if (!input.isNeedsToBeExpanded()) {
     return;
   }
   Document doc = null;
   if (input.getSubNode() != null) {
     doc = XMLUtils.getOwnerDocument(input.getSubNode());
   } else {
     doc = XMLUtils.getOwnerDocument(input.getNodeSet());
   }
   XMLUtils.circumventBug2650(doc);
 }
  /** @inheritDoc */
  public XMLSignatureInput engineResolve(Attr uri, String baseURI)
      throws ResourceResolverException {
    try {
      // calculate new URI
      URI uriNew = getNewURI(uri.getNodeValue(), baseURI);

      String fileName = ResolverLocalFilesystem.translateUriToFilename(uriNew.toString());
      FileInputStream inputStream = new FileInputStream(fileName);
      XMLSignatureInput result = new XMLSignatureInput(inputStream);

      result.setSourceURI(uriNew.toString());

      return result;
    } catch (Exception e) {
      throw new ResourceResolverException("generic.EmptyMessage", e, uri, baseURI);
    }
  }
  protected XMLSignatureInput enginePerformTransform(XMLSignatureInput input, OutputStream os)
      throws CanonicalizationException {
    try {
      String inclusiveNamespaces = null;

      if (this._transformObject.length(
              InclusiveNamespaces.ExclusiveCanonicalizationNamespace,
              InclusiveNamespaces._TAG_EC_INCLUSIVENAMESPACES)
          == 1) {
        Element inclusiveElement =
            XMLUtils.selectNode(
                this._transformObject.getElement().getFirstChild(),
                InclusiveNamespaces.ExclusiveCanonicalizationNamespace,
                InclusiveNamespaces._TAG_EC_INCLUSIVENAMESPACES,
                0);

        inclusiveNamespaces =
            new InclusiveNamespaces(inclusiveElement, this._transformObject.getBaseURI())
                .getInclusiveNamespaces();
      }

      Canonicalizer20010315ExclOmitComments c14n = new Canonicalizer20010315ExclOmitComments();
      if (os != null) {
        c14n.setWriter(os);
      }
      byte[] result;
      if (input.isOctetStream()) {
        result = c14n.engineCanonicalize(input.get_Bytes());
      } else if (input.isElement()) {
        org.w3c.dom.Node excl = input.getExcludeNode();
        result = c14n.engineCanonicalizeSubTree(input.getSubNode(), inclusiveNamespaces, excl);
      } else {
        result = c14n.engineCanonicalizeXPathNodeSet(input.getNodeSet(), inclusiveNamespaces);
      }
      XMLSignatureInput output = new XMLSignatureInput(result);
      if (os != null) {
        output.setOutputStream(os);
      }
      return output;
    } catch (IOException ex) {
      throw new CanonicalizationException("empty", ex);
    } catch (ParserConfigurationException ex) {
      throw new CanonicalizationException("empty", ex);
    } catch (XMLSecurityException ex) {
      throw new CanonicalizationException("empty", ex);
    } catch (SAXException ex) {
      throw new CanonicalizationException("empty", ex);
    }
  }
  @Override
  public XMLSignatureInput engineResolveURI(ResourceResolverContext context)
      throws ResourceResolverException {

    final Attr uriAttr = context.attr;
    final String baseUri = context.baseUri;

    String uriNodeValue = uriAttr.getNodeValue();

    if (uriNodeValue.charAt(0) != '#') {
      return null;
    }

    String xpURI;
    try {
      xpURI = URLDecoder.decode(uriNodeValue, "utf-8");
    } catch (UnsupportedEncodingException e) {
      LOG.warn("utf-8 not a valid encoding", e);
      return null;
    }

    String parts[] = xpURI.substring(1).split("\\s");

    int i = 0;

    DSigNamespaceContext nsContext = null;

    if (parts.length > 1) {
      nsContext = new DSigNamespaceContext();

      for (; i < parts.length - 1; ++i) {
        if (!parts[i].endsWith(")") || !parts[i].startsWith(XNS_OPEN)) {
          return null;
        }

        String mapping = parts[i].substring(XNS_OPEN.length(), parts[i].length() - 1);

        int pos = mapping.indexOf('=');

        if (pos <= 0 || pos >= mapping.length() - 1) {
          throw new ResourceResolverException(
              "malformed namespace part of XPointer expression", uriNodeValue, baseUri);
        }

        nsContext.addNamespace(mapping.substring(0, pos), mapping.substring(pos + 1));
      }
    }

    try {
      Node node = null;
      NodeList nodes = null;

      // plain ID reference.
      if (i == 0 && !parts[i].startsWith(XP_OPEN)) {
        node = this.baseNode.getOwnerDocument().getElementById(parts[i]);
      } else {
        if (!parts[i].endsWith(")") || !parts[i].startsWith(XP_OPEN)) {
          return null;
        }

        XPath xp = this.xPathFactory.newXPath();

        if (nsContext != null) {
          xp.setNamespaceContext(nsContext);
        }

        nodes =
            (NodeList)
                xp.evaluate(
                    parts[i].substring(XP_OPEN.length(), parts[i].length() - 1),
                    this.baseNode,
                    XPathConstants.NODESET);

        if (nodes.getLength() == 0) {
          return null;
        }
        if (nodes.getLength() == 1) {
          node = nodes.item(0);
        }
      }

      XMLSignatureInput result = null;

      if (node != null) {
        result = new XMLSignatureInput(node);
      } else if (nodes != null) {
        Set<Node> nodeSet = new HashSet<Node>(nodes.getLength());

        for (int j = 0; j < nodes.getLength(); ++j) {
          nodeSet.add(nodes.item(j));
        }

        result = new XMLSignatureInput(nodeSet);
      } else {
        return null;
      }

      result.setMIMEType("text/xml");
      result.setExcludeComments(true);
      result.setSourceURI((baseUri != null) ? baseUri.concat(uriNodeValue) : uriNodeValue);

      return result;

    } catch (XPathExpressionException e) {
      throw new ResourceResolverException(
          "malformed XPath inside XPointer expression", e, uriNodeValue, baseUri);
    }
  }