/** * This method returns true if the binaries contains a {@link org.w3c.dom.Document} * * @param bytes * the binaries to be tested * @return true if the binaries is a XML */ public static boolean isDOM(final byte[] bytes) { try { final Document dom = buildDOM(bytes); return dom != null; } catch (DSSException e) { // NOT DOM return false; } }
/** * This method returns the list of children's names for a given {@code Node}. * * @param xmlNode * The node where the search should be performed. * @param xPathString * XPath query string * @return {@code List} of children's names */ public static List<String> getChildrenNames(final Node xmlNode, final String xPathString) { List<String> childrenNames = new ArrayList<String>(); final Element element = getElement(xmlNode, xPathString); if (element != null) { final NodeList unsignedProperties = element.getChildNodes(); for (int ii = 0; ii < unsignedProperties.getLength(); ++ii) { final Node node = unsignedProperties.item(ii); childrenNames.add(node.getLocalName()); } } return childrenNames; }
/** * Returns the Element corresponding to the XPath query. * * @param xmlNode * The node where the search should be performed. * @param xPathString * XPath query string * @return the Element corresponding to the XPath query */ public static Element getElement(final Node xmlNode, final String xPathString) { return (Element) getNode(xmlNode, xPathString); }
private TimeStampToken findTimestampToken() { logger.debug("Finding timestamp token"); NodeList timestampNodes = DomUtils.getNodeList(signatureElement, xPathQueryHolder.XPATH_SIGNATURE_TIMESTAMP); if (timestampNodes.getLength() == 0) { logger.warn("Signature timestamp element was not found"); return null; } if (timestampNodes.getLength() > 1) { logger.warn("Signature contains more than one timestamp: " + timestampNodes.getLength() + ". Using only the first one"); } Node timestampNode = timestampNodes.item(0); Element timestampTokenNode = DomUtils.getElement(timestampNode, xPathQueryHolder.XPATH__ENCAPSULATED_TIMESTAMP); if (timestampTokenNode == null) { logger.warn("The timestamp cannot be extracted from the signature"); return null; } String base64EncodedTimestamp = timestampTokenNode.getTextContent(); return createTimeStampToken(base64EncodedTimestamp); }
/** * This method writes the {@link org.w3c.dom.Document} content to an * outputStream * * @param dom * the {@link org.w3c.dom.Document} to be writed * @param os * the OutputStream @ if any error occurred */ public static void writeDocumentTo(final Document dom, final OutputStream os) { try { final DOMSource xmlSource = new DOMSource(dom); final StreamResult outputTarget = new StreamResult(os); Transformer transformer = getSecureTransformer(); transformer.transform(xmlSource, outputTarget); } catch (Exception e) { throw new DSSException(e); } }
/** * Returns true if the xpath query contains something * * @param xmlNode * the current node * @param xPathString * the expected child node * @return true if the current node has any filled child node */ public static boolean isNotEmpty(final Node xmlNode, final String xPathString) { // xpath suffix allows to skip text nodes and empty lines NodeList nodeList = getNodeList(xmlNode, xPathString + "/child::node()[not(self::text())]"); return (nodeList != null) && (nodeList.getLength() > 0); }
private Set<ManifestEntry> getSignatureEntries(Signature signature) { Set<ManifestEntry> signatureEntries = new HashSet<>(); XadesSignature origin; if (signature.getClass() == BDocSignature.class) { origin = ((BDocSignature) signature).getOrigin(); } else { origin = ((AsicESignature) signature).getOrigin(); } List<Reference> references = origin.getReferences(); for (Reference reference : references) { if (reference.getType().equals("")) { String mimeTypeString = null; Node signatureNode = origin.getDssSignature().getSignatureElement(); Node node = DomUtils.getNode(signatureNode, "./ds:SignedInfo/ds:Reference[@URI=\"" + reference.getURI() + "\"]"); if (node != null) { String referenceId = node.getAttributes().getNamedItem("Id").getNodeValue(); mimeTypeString = DomUtils.getValue(signatureNode, "./ds:Object/xades:QualifyingProperties/xades:SignedProperties/" + "xades:SignedDataObjectProperties/xades:DataObjectFormat" + "[@ObjectReference=\"#" + referenceId + "\"]/xades:MimeType"); } // TODO: mimeTypeString == null ? node == null? String uri = getFileURI(reference); signatureEntries.add(new ManifestEntry(uri, mimeTypeString)); } } return signatureEntries; }
/** * Returns the String value of the corresponding to the XPath query. * * @param xmlNode * The node where the search should be performed. * @param xPathString * XPath query string * @return string value of the XPath query */ public static String getValue(final Node xmlNode, final String xPathString) { try { final XPathExpression xPathExpression = createXPathExpression(xPathString); final String string = (String) xPathExpression.evaluate(xmlNode, XPathConstants.STRING); return string.trim(); } catch (XPathExpressionException e) { throw new DSSException(e); } }
public static String getXPathByIdAttribute(String uri) { return "[@Id='" + getId(uri) + "']"; }
/** * This method returns a new instance of Transformer with secured features enabled * * @return an instance of Transformer with enabled secure features */ public static Transformer getSecureTransformer() { TransformerFactory transformerFactory = getSecureTransformerFactory(); Transformer transformer = null; try { transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); } catch (TransformerConfigurationException e) { throw new DSSException(e); } transformer.setErrorListener(new DSSXmlErrorListener()); return transformer; }
/** * This method allows to convert an XML {@code Node} to a {@code String}. * * @param node * {@code Node} to be converted * @return {@code String} representation of the node */ public static String xmlToString(final Node node) { try { final Source source = new DOMSource(node); final StringWriter stringWriter = new StringWriter(); final Result result = new StreamResult(stringWriter); final Transformer transformer = getSecureTransformer(); transformer.transform(source, result); return stringWriter.getBuffer().toString(); } catch (Exception e) { throw new DSSException(e); } }
/** * Returns the Node corresponding to the XPath query. * * @param xmlNode * The node where the search should be performed. * @param xPathString * XPath query string * @return the Node corresponding to the XPath query. */ public static Node getNode(final Node xmlNode, final String xPathString) { final NodeList list = getNodeList(xmlNode, xPathString); if (list.getLength() > 1) { throw new DSSException("More than one result for XPath: " + xPathString); } return list.item(0); }
/** * Returns the NodeList corresponding to the XPath query. * * @param xmlNode * The node where the search should be performed. * @param xPathString * XPath query string * @return the NodeList corresponding to the XPath query */ public static NodeList getNodeList(final Node xmlNode, final String xPathString) { try { final XPathExpression expr = createXPathExpression(xPathString); return (NodeList) expr.evaluate(xmlNode, XPathConstants.NODESET); } catch (XPathExpressionException e) { throw new DSSException(e); } }
/** * This method returns the {@link org.w3c.dom.Document} created based on the XML string. * * @param xmlString * The string representing the dssDocument to be created. * @return a new {@link org.w3c.dom.Document} with the xmlString content */ public static Document buildDOM(final String xmlString) { return buildDOM(xmlString.getBytes(StandardCharsets.UTF_8)); }
private void addPolicyIdentifierQualifierValidationErrors() { LOGGER.debug("Extracting policy identifier qualifier validation errors"); XPathQueryHolder xPathQueryHolder = getDssSignature().getXPathQueryHolder(); Element signatureElement = getDssSignature().getSignatureElement(); Element element = DomUtils.getElement(signatureElement, xPathQueryHolder.XPATH_SIGNATURE_POLICY_IDENTIFIER); Element identifier = DomUtils.getElement(element, "./xades:SignaturePolicyId/xades:SigPolicyId/xades:Identifier"); String qualifier = identifier.getAttribute("Qualifier"); if (!StringUtils.equals(XadesSignatureValidator.OIDAS_URN, qualifier)) { this.addValidationError(new WrongPolicyIdentifierQualifierException(String.format("Wrong policy identifier qualifier: %s", qualifier))); } }
@Test public void getSecureTransformer() { assertNotNull(DomUtils.getSecureTransformer()); }
protected Set<CertificateToken> findCertificates(String xPath) { Set<CertificateToken> certificates = new HashSet<>(); NodeList nodeList = DomUtils.getNodeList(signatureElement, xPath); for (int i = 0; i < nodeList.getLength(); i++) { Element certificateElement = (Element) nodeList.item(i); CertificateToken certToken = createCertificateToken(certificateElement); if (!certificates.contains(certToken)) { String idIdentifier = DSSXMLUtils.getIDIdentifier(certificateElement); certToken.setXmlId(idIdentifier); certificates.add(certToken); } } return certificates; }
/** * This method returns the {@link org.w3c.dom.Document} created based on byte array. * * @param bytes * The bytes array representing the dssDocument to be created. * @return a new {@link org.w3c.dom.Document} with the bytes content */ public static Document buildDOM(final byte[] bytes) { return buildDOM(new ByteArrayInputStream(bytes)); }
protected boolean isSignaturePolicyImpliedElementPresented() { XPathQueryHolder xPathQueryHolder = this.getDssSignature().getXPathQueryHolder(); Element signaturePolicyImpliedElement = DomUtils.getElement(this.getDssSignature().getSignatureElement(), String.format("%s%s", xPathQueryHolder.XPATH_SIGNATURE_POLICY_IDENTIFIER, xPathQueryHolder.XPATH__SIGNATURE_POLICY_IMPLIED.replace(".", ""))); return signaturePolicyImpliedElement != null; }
private Element loadManifestXml() { return DomUtils.buildDOM(manifestFile).getDocumentElement(); }