Search in sources :

Example 1 with OxalisAs4Exception

use of network.oxalis.as4.lang.OxalisAs4Exception in project Oxalis-AS4 by OxalisCommunity.

the class SOAPHeaderParser method getAttachmentDigest.

public static byte[] getAttachmentDigest(String refId, SOAPHeader header) throws OxalisAs4Exception {
    NodeList sigInfoNode = header.getElementsByTagNameNS(NS_ALL, SIG_INFO);
    if (sigInfoNode.getLength() != 1) {
        throw new OxalisAs4Exception(String.format("Expected one Signature elements in header, but found %d", sigInfoNode.getLength()));
    }
    Element sigInfoElement = (Element) sigInfoNode.item(0);
    NodeList refNodes = sigInfoElement.getElementsByTagNameNS(NS_ALL, REF);
    for (int i = 0; i < refNodes.getLength(); i++) {
        Element refElement = (Element) refNodes.item(i);
        if (refId.equals(refElement.getAttribute("URI"))) {
            NodeList digestValueNode = refElement.getElementsByTagNameNS(NS_ALL, DIGEST_VAL);
            return digestValueNode.item(0).getTextContent().getBytes(StandardCharsets.UTF_8);
        }
    }
    return null;
}
Also used : NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) OxalisAs4Exception(network.oxalis.as4.lang.OxalisAs4Exception)

Example 2 with OxalisAs4Exception

use of network.oxalis.as4.lang.OxalisAs4Exception in project Oxalis-AS4 by OxalisCommunity.

the class SOAPHeaderParser method getSenderCertificate.

public static X509Certificate getSenderCertificate(SOAPHeader header) throws OxalisAs4Exception {
    NodeList sigNode = header.getElementsByTagNameNS(NS_ALL, SIG);
    if (sigNode.getLength() != 1) {
        throw new OxalisAs4Exception(String.format("Expected one Signature element in header, but found %d", sigNode.getLength()));
    }
    Element sigElement = (Element) sigNode.item(0);
    NodeList keyInfoNode = sigElement.getElementsByTagNameNS(NS_ALL, KEY_INFO);
    if (keyInfoNode.getLength() != 1) {
        throw new OxalisAs4Exception(String.format("Expected one KeyInfo child of Signature, but found %d", keyInfoNode.getLength()));
    }
    Element keyInfoElement = (Element) keyInfoNode.item(0);
    NodeList refNode = keyInfoElement.getElementsByTagNameNS(NS_ALL, REF);
    if (refNode == null || refNode.getLength() != 1) {
        throw new OxalisAs4Exception(("Zero or multiple Reference nodes under Signature->KeyInfo"));
    }
    String refUri = ((Element) refNode.item(0)).getAttribute("URI").replace("#", "");
    NodeList bstNodes = header.getElementsByTagNameNS(NS_ALL, BST);
    if (bstNodes != null) {
        for (int i = 0; i < bstNodes.getLength(); i++) {
            Element bstElem = (Element) bstNodes.item(i);
            if (bstElem.getAttributeNS("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd", "Id").equals(refUri)) {
                try {
                    String pem = bstElem.getTextContent().replaceAll("[\r\n]+", "");
                    byte[] buf = Base64.getDecoder().decode(pem);
                    return (X509Certificate) CertificateFactory.getInstance("X.509").generateCertificate(new ByteArrayInputStream(buf));
                } catch (CertificateException e) {
                    throw new OxalisAs4Exception("Could not create certificate from BinarySecurityToken", e);
                }
            }
        }
    }
    return null;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) OxalisAs4Exception(network.oxalis.as4.lang.OxalisAs4Exception) CertificateException(java.security.cert.CertificateException) X509Certificate(java.security.cert.X509Certificate)

Example 3 with OxalisAs4Exception

use of network.oxalis.as4.lang.OxalisAs4Exception in project Oxalis-AS4 by OxalisCommunity.

the class SOAPHeaderParser method getSignature.

public static byte[] getSignature(SOAPHeader header) throws OxalisAs4Exception {
    NodeList sigNode = header.getElementsByTagNameNS(NS_ALL, SIG);
    if (sigNode.getLength() != 1) {
        throw new OxalisAs4Exception(String.format("Expected one Signature element in header, but found %d", sigNode.getLength()));
    }
    Element sigElement = (Element) sigNode.item(0);
    NodeList sigValNode = sigElement.getElementsByTagNameNS(NS_ALL, SIG_VAL);
    if (sigValNode == null || sigValNode.getLength() != 1) {
        throw new OxalisAs4Exception("Zero or multiple SignatureValue elements in header");
    }
    return sigValNode.item(0).getTextContent().replace("\r\n", "").getBytes(StandardCharsets.UTF_8);
}
Also used : NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) OxalisAs4Exception(network.oxalis.as4.lang.OxalisAs4Exception)

Example 4 with OxalisAs4Exception

use of network.oxalis.as4.lang.OxalisAs4Exception in project Oxalis-AS4 by OxalisCommunity.

the class SOAPHeaderParser method refListFromElement.

private static List<ReferenceType> refListFromElement(Element element) throws OxalisAs4Exception {
    NodeList refNodes = element.getElementsByTagNameNS(NS_ALL, REF);
    List<ReferenceType> referenceList = Lists.newArrayList();
    if (refNodes == null) {
        return Collections.emptyList();
    }
    try {
        Unmarshaller unmarshaller = JAXB_CONTEXT.createUnmarshaller();
        for (int i = 0; i < refNodes.getLength(); i++) {
            referenceList.add(unmarshaller.unmarshal(refNodes.item(i), ReferenceType.class).getValue());
        }
    } catch (JAXBException e) {
        throw new OxalisAs4Exception("Could not unmarshal reference node", e);
    }
    return referenceList;
}
Also used : NodeList(org.w3c.dom.NodeList) JAXBException(javax.xml.bind.JAXBException) OxalisAs4Exception(network.oxalis.as4.lang.OxalisAs4Exception) Unmarshaller(javax.xml.bind.Unmarshaller) ReferenceType(org.w3.xmldsig.ReferenceType)

Example 5 with OxalisAs4Exception

use of network.oxalis.as4.lang.OxalisAs4Exception in project Oxalis-AS4 by OxalisCommunity.

the class As4MessageFactory method createReceiptMessage.

public SOAPMessage createReceiptMessage(UserMessage inUserMessage, ProsessingContext prosessingContext) throws OxalisAs4Exception {
    XMLGregorianCalendar xmlGc = XMLUtil.dateToXMLGeorgianCalendar(prosessingContext.getReceiptTimestamp().getDate());
    MessageInfo messageInfo = MessageInfo.builder().withTimestamp(xmlGc).withMessageId(messageIdGenerator.generate()).withRefToMessageId(inUserMessage.getMessageInfo().getMessageId()).build();
    List<MessagePartNRInformation> mpList = prosessingContext.getReferenceList().stream().map(reference -> MessagePartNRInformation.builder().withReference(reference).build()).collect(Collectors.toList());
    NonRepudiationInformation nri = NonRepudiationInformation.builder().addMessagePartNRInformation(mpList).build();
    SignalMessage signalMessage = SignalMessage.builder().withMessageInfo(messageInfo).withReceipt(Receipt.builder().withAny(nri).build()).build();
    return marshalSignalMessage(signalMessage);
}
Also used : javax.xml.soap(javax.xml.soap) NonRepudiationInformation(org.oasis_open.docs.ebxml_bp.ebbp_signals_2.NonRepudiationInformation) Date(java.util.Date) Inject(com.google.inject.Inject) MessageIdGenerator(network.oxalis.as4.api.MessageIdGenerator) JAXBElement(javax.xml.bind.JAXBElement) AS4Error(network.oxalis.as4.lang.AS4Error) org.oasis_open.docs.ebxml_msg.ebms.v3_0.ns.core._200704(org.oasis_open.docs.ebxml_msg.ebms.v3_0.ns.core._200704) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) ProsessingContext(network.oxalis.as4.inbound.ProsessingContext) Collectors(java.util.stream.Collectors) List(java.util.List) Fault(org.apache.cxf.interceptor.Fault) OxalisAs4Exception(network.oxalis.as4.lang.OxalisAs4Exception) JAXBContext(javax.xml.bind.JAXBContext) Singleton(com.google.inject.Singleton) MessagePartNRInformation(org.oasis_open.docs.ebxml_bp.ebbp_signals_2.MessagePartNRInformation) Error(org.oasis_open.docs.ebxml_msg.ebms.v3_0.ns.core._200704.Error) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) NonRepudiationInformation(org.oasis_open.docs.ebxml_bp.ebbp_signals_2.NonRepudiationInformation) MessagePartNRInformation(org.oasis_open.docs.ebxml_bp.ebbp_signals_2.MessagePartNRInformation)

Aggregations

OxalisAs4Exception (network.oxalis.as4.lang.OxalisAs4Exception)16 Fault (org.apache.cxf.interceptor.Fault)4 NodeList (org.w3c.dom.NodeList)4 BufferedInputStream (java.io.BufferedInputStream)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 Path (java.nio.file.Path)3 X509Certificate (java.security.cert.X509Certificate)3 GZIPInputStream (java.util.zip.GZIPInputStream)3 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)3 UnclosableInputStream (network.oxalis.commons.io.UnclosableInputStream)3 ReferenceType (org.w3.xmldsig.ReferenceType)3 Element (org.w3c.dom.Element)3 Inject (com.google.inject.Inject)2 Singleton (com.google.inject.Singleton)2 Date (java.util.Date)2 Collectors (java.util.stream.Collectors)2 Stream (java.util.stream.Stream)2 JAXBElement (javax.xml.bind.JAXBElement)2 JAXBException (javax.xml.bind.JAXBException)2