/** * This is the utility method that loads the data from the inputstream determined by the inputstream parameter into a * {@link org.w3c.dom.Document}. * * @param inputStream * @return */ public static Document load(final InputStream inputStream) throws DSSException { final Document document = DSSXMLUtils.buildDOM(inputStream); return document; }
/** * 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 * @throws ParserConfigurationException * @throws IOException * @throws SAXException */ public static Document buildDOM(final byte[] bytes) throws DSSException { final InputStream input = new ByteArrayInputStream(bytes); return buildDOM(input); }
/** * This method returns {@link org.w3c.dom.Document} based on the current {@link XmlNode}. * * @return */ public Document toDocument() { final InputStream inputStream = getInputStream(); final Document document = DSSXMLUtils.buildDOM(inputStream); return document; }
public DSSTransformXPath(final DSSTransform dssTransform) { this.dssTransform = dssTransform; document = DSSXMLUtils.buildDOM(); final Element transformDom = document.createElementNS(XMLSignature.XMLNS, SignatureBuilder.DS_TRANSFORM); document.appendChild(transformDom); SignatureBuilder.createTransform(document, dssTransform, transformDom); }
private XmlDom getXmlDomReport(final String report) { // System.out.println("############################ 2"); final Document reportDom = DSSXMLUtils.buildDOM(report); return new XmlDom(reportDom); }
/** * 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 * @throws ParserConfigurationException * @throws IOException * @throws SAXException */ public static Document buildDOM(final String xmlString) throws DSSException { final InputStream input = new ByteArrayInputStream(DSSUtils.getUtf8Bytes(xmlString)); return buildDOM(input); }
/** * This is the utility method that marshals the JAXB object into a {@link org.w3c.dom.Document}. * * @param diagnosticDataJB The JAXB object representing the diagnostic data. * @return */ public static Document convert(final DiagnosticData diagnosticDataJB) { try { final Document diagnosticData = DSSXMLUtils.buildDOM(); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.marshal(diagnosticDataJB, diagnosticData); return diagnosticData; } catch (JAXBException e) { throw new DSSException(e); } } }
/** * This method writes formatted {@link org.w3c.dom.Node} to the outputStream. * * @param dssDocument * @param out */ public static void printDocument(final DSSDocument dssDocument, final OutputStream out) { final byte[] bytes = dssDocument.getBytes(); final Document document = DSSXMLUtils.buildDOM(bytes); printDocument(document, out, false); }
/** * This method returns the {@link org.w3c.dom.Document} created based on the {@link eu.europa.ec.markt.dss.signature.DSSDocument}. * * @param dssDocument The DSS representation of the document from which the dssDocument is created. * @return * @throws DSSException */ public static Document buildDOM(final DSSDocument dssDocument) throws DSSException { final InputStream input = dssDocument.openStream(); try { final Document doc = buildDOM(input); return doc; } finally { DSSUtils.closeQuietly(input); } }
/** * The default constructor for XMLDocumentValidator. The created instance is initialised with default {@code XPathQueryHolder} and {@code XAdES111XPathQueryHolder}. * * @param dssDocument The instance of {@code DSSDocument} to validate * @throws DSSException */ public XMLDocumentValidator(final DSSDocument dssDocument) throws DSSException { xadesSignatureScopeFinder = SignatureScopeFinderFactory.geInstance(XAdESSignature.class); this.document = dssDocument; this.rootElement = DSSXMLUtils.buildDOM(dssDocument); xPathQueryHolders = new ArrayList<XPathQueryHolder>(); final XAdES111XPathQueryHolder xades111xPathQueryHolder = new XAdES111XPathQueryHolder(); xPathQueryHolders.add(xades111xPathQueryHolder); final XPathQueryHolder xades122XPathQueryHolder = new XAdES122XPathQueryHolder(); xPathQueryHolders.add(xades122XPathQueryHolder); final XPathQueryHolder xPathQueryHolder = new XPathQueryHolder(); xPathQueryHolders.add(xPathQueryHolder); }
public ValidationPolicy load(InputStream inputStream, InputStream xsdInputStream) throws DSSException { //To clone inputStream byte[] fileInputStream = cloneFileInputStream(inputStream); InputStream inputStreamClone2 = new ByteArrayInputStream(fileInputStream); byte[] fileInputStreamXsd = cloneFileInputStream(xsdInputStream); InputStream xsdStream1 = new ByteArrayInputStream(fileInputStreamXsd); InputSource sourceentree = new InputSource(xsdStream1); final Document document = DSSXMLUtils.buildDOM(inputStreamClone2); //Clean XML XPath xp = XPathFactory.newInstance().newXPath(); NodeList nl = null; try { nl = (NodeList) xp.evaluate("//text()[normalize-space(.)='']", document, XPathConstants.NODESET); } catch (XPathExpressionException e) { e.printStackTrace(); } for (int i = 0; i < nl.getLength(); ++i) { Node node = nl.item(i); node.getParentNode().removeChild(node); } document.normalizeDocument(); document.normalize(); final XmlDom xmlDom = new XmlDom(document); final ValidationPolicy validationPolicy = new ValidationPolicy(xmlDom, xsdUrl, document); return validationPolicy; }
final Document root = DSSXMLUtils.buildDOM(signature); final Element signatureEl = (Element) root.getDocumentElement().getFirstChild(); final String idIdentifier = DSSXMLUtils.getIDIdentifier(signatureEl); for (int j = 0; j < signatures.size(); j++) { final Document doc = DSSXMLUtils.buildDOM(signature); final Node signatureElement = doc.getDocumentElement().getFirstChild();
/** * Creates dss document that retains the data in memory * * @param bytes array of bytes representing the document * @param name the file name if the data originates from a file */ public AsicManifestDocument(final byte[] bytes, final String name) { this.bytes = bytes; this.name = name; this.mimeType = MimeType.XML; final Document document = DSSXMLUtils.buildDOM(bytes); signatureUri = DSSXMLUtils.getValue(document, "/asic:ASiCManifest/asic:SigReference/@URI"); if (DSSUtils.isBlank(signatureUri)) { throw new DSSException("The AsicManifest file must contains the URI of the related signature."); } }
static void createTransform(final Document document, final DSSTransform dssTransform, final Element transformDom) { transformDom.setAttribute(ALGORITHM, dssTransform.getAlgorithm()); final String elementName = dssTransform.getElementName(); final String textContent = dssTransform.getTextContent(); if (DSSUtils.isNotBlank(elementName)) { final String namespace = dssTransform.getNamespace(); DSSXMLUtils.addTextElement(document, transformDom, namespace, elementName, textContent); } else if (DSSUtils.isNotBlank(textContent)) { final Document transformContentDoc = DSSXMLUtils.buildDOM(textContent); final Element contextDocumentElement = transformContentDoc.getDocumentElement(); document.adoptNode(contextDocumentElement); transformDom.appendChild(contextDocumentElement); } }
/** * This method creates a XAdES signature. When adding a new signature, this one is appended to the already present signatures. * * @param asicParameters already present signatures * @param signature signature being created * @param outZip destination {@code ZipOutputStream} * @throws eu.europa.ec.markt.dss.exception.DSSException */ private void buildXAdES(final ASiCParameters asicParameters, final DSSDocument signature, final ZipOutputStream outZip) throws DSSException { final String signatureZipEntryName = getSignatureFileName(asicParameters); final ZipEntry entrySignature = new ZipEntry(signatureZipEntryName); createZipEntry(outZip, entrySignature); // Creates the XAdES signature final Document xmlSignatureDoc = DSSXMLUtils.buildDOM(signature); final Element documentElement = xmlSignatureDoc.getDocumentElement(); final Element xmlSignatureElement = (Element) xmlSignatureDoc.removeChild(documentElement); final Document xmlXAdESDoc; final DSSDocument enclosedSignature = asicParameters.getEnclosedSignature(); if (enclosedSignature != null && isAsics(asicParameters)) { final Document contextXmlSignatureDoc = DSSXMLUtils.buildDOM(enclosedSignature); final Element contextDocumentElement = contextXmlSignatureDoc.getDocumentElement(); contextXmlSignatureDoc.adoptNode(xmlSignatureElement); contextDocumentElement.appendChild(xmlSignatureElement); xmlXAdESDoc = contextXmlSignatureDoc; } else { xmlXAdESDoc = DSSXMLUtils.createDocument(ASiCNamespaces.ASiC, ASICS_NS, xmlSignatureElement); } storeXmlDom(outZip, xmlXAdESDoc); }
private byte[] applyTransformations(DSSDocument dssDocument, final List<DSSTransform> transforms, Node nodeToTransform, byte[] transformedReferenceBytes) { for (final DSSTransform transform : transforms) { final String transformAlgorithm = transform.getAlgorithm(); if (Transforms.TRANSFORM_XPATH.equals(transformAlgorithm)) { final DSSTransformXPath transformXPath = new DSSTransformXPath(transform); // At the moment it is impossible to go through a medium other than byte array (Set<Node>, octet stream, Node). Further investigation is needed. final byte[] transformedBytes = nodeToTransform == null ? transformXPath.transform(dssDocument) : transformXPath.transform(nodeToTransform); dssDocument = new InMemoryDocument(transformedBytes); nodeToTransform = DSSXMLUtils.buildDOM(dssDocument); } else if (DSSXMLUtils.canCanonicalize(transformAlgorithm)) { if (nodeToTransform == null) { nodeToTransform = DSSXMLUtils.buildDOM(dssDocument); } transformedReferenceBytes = DSSXMLUtils.canonicalizeSubtree(transformAlgorithm, nodeToTransform); // The supposition is made that the last transformation is the canonicalization break; } else if (CanonicalizationMethod.ENVELOPED.equals(transformAlgorithm)) { // do nothing the new signature is not existing yet! // removeExistingSignatures(document); } else { throw new DSSException("The transformation is not implemented yet, please transform the reference before signing!"); } } return transformedReferenceBytes; }
signatureValueDom.appendChild(signatureValueNode); final Document originalDocumentDom = DSSXMLUtils.buildDOM(detachedDocument); final Node copiedNode = originalDocumentDom.importNode(signatureDom, true);
private void buildAsicManifest(final SignatureParameters underlyingParameters, final DSSDocument detachedDocument, final OutputStream outputStream) { final ASiCParameters asicParameters = underlyingParameters.aSiC(); final Document documentDom = DSSXMLUtils.buildDOM(); final Element asicManifestDom = documentDom.createElementNS(ASiCNamespaces.ASiC, "asic:ASiCManifest"); documentDom.appendChild(asicManifestDom); final Element sigReferenceDom = DSSXMLUtils.addElement(documentDom, asicManifestDom, ASiCNamespaces.ASiC, "asic:SigReference"); final String signatureName = getSignatureFileName(asicParameters); sigReferenceDom.setAttribute("URI", signatureName); final String signatureMimeType = getSignatureMimeType(asicParameters); sigReferenceDom.setAttribute("MimeType", signatureMimeType); DSSDocument currentDetachedDocument = detachedDocument; do { final String detachedDocumentName = currentDetachedDocument.getName(); final Element dataObjectReferenceDom = DSSXMLUtils.addElement(documentDom, sigReferenceDom, ASiCNamespaces.ASiC, "asic:DataObjectReference"); dataObjectReferenceDom.setAttribute("URI", detachedDocumentName); final Element digestMethodDom = DSSXMLUtils.addElement(documentDom, dataObjectReferenceDom, XMLSignature.XMLNS, "DigestMethod"); final DigestAlgorithm digestAlgorithm = underlyingParameters.getDigestAlgorithm(); digestMethodDom.setAttribute("Algorithm", digestAlgorithm.getXmlId()); final Element digestValueDom = DSSXMLUtils.addElement(documentDom, dataObjectReferenceDom, XMLSignature.XMLNS, "DigestValue"); final byte[] digest = DSSUtils.digest(digestAlgorithm, currentDetachedDocument.getBytes()); final String base64Encoded = DSSUtils.base64Encode(digest); final Text textNode = documentDom.createTextNode(base64Encoded); digestValueDom.appendChild(textNode); currentDetachedDocument = currentDetachedDocument.getNextDocument(); } while (currentDetachedDocument != null); storeXmlDom(outputStream, documentDom); }
final Document document = DSSXMLUtils.buildDOM(dssDocument); DSSXMLUtils.recursiveIdBrowse(document.getDocumentElement()); final String uri_id = uri.substring(1);
/** * Load a trusted list form the specified URL. If the {@code signingCertList} contains any {@code X509Certificate} then the validation of the signature of the TSL is done. * * @param url of the TSL to load * @param signingCertList the {@code List} of the possible signing certificates * @return {@code TrustStatusList} */ private TrustStatusList getTrustStatusList(final String url, final List<X509Certificate> signingCertList) { boolean refresh = shouldRefresh(url); final byte[] bytes = dataLoader.get(url, refresh); if (bytes == null) { throw new DSSNullReturnedException(url); } boolean coreValidity = checkSignature ? validateTslSignature(signingCertList, bytes) : true; final Document doc = DSSXMLUtils.buildDOM(bytes); final TrustStatusList trustStatusList = TrustServiceListFactory.newInstance(doc); trustStatusList.setWellSigned(coreValidity); updateTslNextUpdateDate(url, trustStatusList); return trustStatusList; }