Search in sources :

Example 51 with CodedException

use of ee.ria.xroad.common.CodedException in project X-Road by nordic-institute.

the class SaxSoapParserImpl method parseMessage.

private Soap parseMessage(InputStream is, String mimeType, String contentType, String charset) throws Exception {
    log.trace("parseMessage({}, {})", mimeType, charset);
    ByteArrayOutputStream rawXml = new ByteArrayOutputStream();
    ByteArrayOutputStream processedXml = new ByteArrayOutputStream();
    InputStream proxyStream = excludeUtf8Bom(contentType, new TeeInputStream(is, rawXml));
    Writer outputWriter = new OutputStreamWriter(processedXml, charset);
    XRoadSoapHandler handler = handleSoap(outputWriter, proxyStream);
    CodedException fault = handler.getFault();
    if (fault != null) {
        return createSoapFault(charset, rawXml, fault);
    }
    byte[] xmlBytes = isProcessedXmlRequired() ? processedXml.toByteArray() : rawXml.toByteArray();
    return createSoapMessage(contentType, charset, handler, xmlBytes);
}
Also used : CodedException(ee.ria.xroad.common.CodedException) BOMInputStream(org.apache.commons.io.input.BOMInputStream) TeeInputStream(org.apache.commons.io.input.TeeInputStream) InputStream(java.io.InputStream) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TeeInputStream(org.apache.commons.io.input.TeeInputStream) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter)

Example 52 with CodedException

use of ee.ria.xroad.common.CodedException in project X-Road by nordic-institute.

the class Signature method getOcspResponses.

/**
 * Return list of OCSP responses included in the signature.
 */
List<OCSPResp> getOcspResponses() {
    List<OCSPResp> ocspResponses = new ArrayList<>();
    NodeList ocspValueElements = getEncapsulatedOCSPValueElements(objectContainer.getElement());
    if (ocspValueElements == null || ocspValueElements.getLength() == 0) {
        throw new CodedException(X_MALFORMED_SIGNATURE, "Could not get any OCSP elements from signature");
    }
    for (int i = 0; i < ocspValueElements.getLength(); i++) {
        Element ocspResponseElem = (Element) ocspValueElements.item(i);
        // we have the ocsp response in base64 form, attempt to parse it
        String base64 = ocspResponseElem.getTextContent();
        try {
            ocspResponses.add(new OCSPResp(decodeBase64(base64)));
        } catch (IOException e) {
            throw new CodedException(X_MALFORMED_SIGNATURE, e);
        }
    }
    return ocspResponses;
}
Also used : CodedException(ee.ria.xroad.common.CodedException) NodeList(org.w3c.dom.NodeList) Helper.dsElement(ee.ria.xroad.common.signature.Helper.dsElement) Helper.xadesElement(ee.ria.xroad.common.signature.Helper.xadesElement) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) IOException(java.io.IOException) OCSPResp(org.bouncycastle.cert.ocsp.OCSPResp)

Example 53 with CodedException

use of ee.ria.xroad.common.CodedException in project X-Road by nordic-institute.

the class Signature method getExtraCertificates.

/**
 * Returns list of additional certificates that are included in the signature.
 */
List<X509Certificate> getExtraCertificates() {
    List<X509Certificate> extraCertificates = new ArrayList<>();
    NodeList certificateRefs = getCertificateRefElements(objectContainer.getElement());
    if (certificateRefs == null || certificateRefs.getLength() == 0) {
        // returning empty list, since there are no extra certificates
        return extraCertificates;
    }
    for (int i = 0; i < certificateRefs.getLength(); i++) {
        Element certRef = (Element) certificateRefs.item(i);
        String certId = certRef.getAttribute(URI_ATTRIBUTE);
        if (certId == null || certId.isEmpty()) {
            throw new CodedException(X_MALFORMED_SIGNATURE, "Missing certificate id attribute");
        }
        // we have the ocsp response element id, let's find the response
        Element certElem = XmlUtils.getElementById(document, certId);
        if (certElem == null) {
            throw new CodedException(X_MALFORMED_SIGNATURE, "Could not find certificate with id " + certId);
        }
        try {
            X509Certificate x509 = CryptoUtils.readCertificate(certElem.getTextContent());
            // we now have the certificate constructed, verify the digest
            if (!verifyDigest((Element) certRef.getFirstChild(), x509.getEncoded())) {
                throw new CodedException(X_MALFORMED_SIGNATURE, "Certificate (%s) digest does not match", x509.getSerialNumber());
            }
            extraCertificates.add(x509);
        } catch (CertificateException | NoSuchAlgorithmException | IOException | OperatorCreationException e) {
            throw new CodedException(X_MALFORMED_SIGNATURE, e);
        }
    }
    return extraCertificates;
}
Also used : NodeList(org.w3c.dom.NodeList) Helper.dsElement(ee.ria.xroad.common.signature.Helper.dsElement) Helper.xadesElement(ee.ria.xroad.common.signature.Helper.xadesElement) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) X509Certificate(java.security.cert.X509Certificate) CodedException(ee.ria.xroad.common.CodedException) OperatorCreationException(org.bouncycastle.operator.OperatorCreationException)

Example 54 with CodedException

use of ee.ria.xroad.common.CodedException in project X-Road by nordic-institute.

the class SoapParserImpl method unmarshalHeader.

@SuppressWarnings("unchecked")
static <T> T unmarshalHeader(Class<?> clazz, SOAPHeader soapHeader, boolean checkRequiredFields) throws Exception {
    Unmarshaller unmarshaller = JaxbUtils.createUnmarshaller(clazz);
    if (checkRequiredFields) {
        unmarshaller.setListener(new RequiredHeaderFieldsChecker(clazz));
    }
    unmarshaller.setEventHandler(event -> {
        switch(event.getSeverity()) {
            case ValidationEvent.WARNING:
                return true;
            case ValidationEvent.ERROR:
                Throwable t = event.getLinkedException();
                return !(t instanceof AccessorException && t.getCause() instanceof CodedException);
            case ValidationEvent.FATAL_ERROR:
                return false;
            default:
                return true;
        }
    });
    JAXBElement<T> jaxbElement = (JAXBElement<T>) unmarshaller.unmarshal(soapHeader, clazz);
    return jaxbElement.getValue();
}
Also used : CodedException(ee.ria.xroad.common.CodedException) AccessorException(com.sun.xml.bind.api.AccessorException) JAXBElement(javax.xml.bind.JAXBElement) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 55 with CodedException

use of ee.ria.xroad.common.CodedException in project X-Road by nordic-institute.

the class SoapMessageDecoder method readMultipart.

private void readMultipart(InputStream is) throws Exception {
    log.trace("readMultipart");
    MimeConfig config = new MimeConfig.Builder().setHeadlessParsing(contentType).build();
    MimeStreamParser mimeStreamParser = new MimeStreamParser(config);
    mimeStreamParser.setContentHandler(new MultipartHandler());
    // Parse the request.
    try {
        mimeStreamParser.parse(is);
    } catch (MimeException ex) {
        // invalid request from client and we want to report it as that.
        throw new CodedException(X_MIME_PARSING_FAILED, ex);
    }
}
Also used : MimeConfig(org.apache.james.mime4j.stream.MimeConfig) CodedException(ee.ria.xroad.common.CodedException) MimeStreamParser(org.apache.james.mime4j.parser.MimeStreamParser) MimeException(org.apache.james.mime4j.MimeException)

Aggregations

CodedException (ee.ria.xroad.common.CodedException)131 X509Certificate (java.security.cert.X509Certificate)28 IOException (java.io.IOException)17 ErrorCodes.translateException (ee.ria.xroad.common.ErrorCodes.translateException)15 SignerNotReachableException (org.niis.xroad.restapi.service.SignerNotReachableException)14 TokenInfo (ee.ria.xroad.signer.protocol.dto.TokenInfo)12 OCSPResp (org.bouncycastle.cert.ocsp.OCSPResp)11 ServiceException (org.niis.xroad.restapi.service.ServiceException)11 ClientId (ee.ria.xroad.common.identifier.ClientId)10 ArrayList (java.util.ArrayList)10 Test (org.junit.Test)10 KeyInfo (ee.ria.xroad.signer.protocol.dto.KeyInfo)8 InputStream (java.io.InputStream)8 URISyntaxException (java.net.URISyntaxException)7 Date (java.util.Date)7 SoapFault (ee.ria.xroad.common.message.SoapFault)6 ServiceId (ee.ria.xroad.common.identifier.ServiceId)5 Soap (ee.ria.xroad.common.message.Soap)5 SoapMessageImpl (ee.ria.xroad.common.message.SoapMessageImpl)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5