Search in sources :

Example 6 with If

use of org.w3._2007.rif.If in project poi by apache.

the class TestSignatureInfo method testSignEnvelopingDocument.

@Test
public void testSignEnvelopingDocument() throws Exception {
    String testFile = "hello-world-unsigned.xlsx";
    OPCPackage pkg = OPCPackage.open(copy(testdata.getFile(testFile)), PackageAccess.READ_WRITE);
    initKeyPair("Test", "CN=Test");
    final X509CRL crl = PkiTestUtils.generateCrl(x509, keyPair.getPrivate());
    // setup
    SignatureConfig signatureConfig = new SignatureConfig();
    signatureConfig.setOpcPackage(pkg);
    signatureConfig.setKey(keyPair.getPrivate());
    /*
         * We need at least 2 certificates for the XAdES-C complete certificate
         * refs construction.
         */
    List<X509Certificate> certificateChain = new ArrayList<X509Certificate>();
    certificateChain.add(x509);
    certificateChain.add(x509);
    signatureConfig.setSigningCertificateChain(certificateChain);
    signatureConfig.addSignatureFacet(new EnvelopedSignatureFacet());
    signatureConfig.addSignatureFacet(new KeyInfoSignatureFacet());
    signatureConfig.addSignatureFacet(new XAdESSignatureFacet());
    signatureConfig.addSignatureFacet(new XAdESXLSignatureFacet());
    // check for internet, no error means it works
    boolean mockTsp = (getAccessError("http://timestamp.comodoca.com/rfc3161", true, 10000) != null);
    // http://timestamping.edelweb.fr/service/tsp
    // http://tsa.belgium.be/connect
    // http://timestamp.comodoca.com/authenticode
    // http://timestamp.comodoca.com/rfc3161
    // http://services.globaltrustfinder.com/adss/tsa
    signatureConfig.setTspUrl("http://timestamp.comodoca.com/rfc3161");
    // comodoca request fails, if default policy is set ...
    signatureConfig.setTspRequestPolicy(null);
    signatureConfig.setTspOldProtocol(false);
    //set proxy info if any
    String proxy = System.getProperty("http_proxy");
    if (proxy != null && proxy.trim().length() > 0) {
        signatureConfig.setProxyUrl(proxy);
    }
    if (mockTsp) {
        TimeStampService tspService = new TimeStampService() {

            @Override
            public byte[] timeStamp(byte[] data, RevocationData revocationData) throws Exception {
                revocationData.addCRL(crl);
                return "time-stamp-token".getBytes(LocaleUtil.CHARSET_1252);
            }

            @Override
            public void setSignatureConfig(SignatureConfig config) {
            // empty on purpose
            }
        };
        signatureConfig.setTspService(tspService);
    } else {
        TimeStampServiceValidator tspValidator = new TimeStampServiceValidator() {

            @Override
            public void validate(List<X509Certificate> validateChain, RevocationData revocationData) throws Exception {
                for (X509Certificate certificate : validateChain) {
                    LOG.log(POILogger.DEBUG, "certificate: " + certificate.getSubjectX500Principal());
                    LOG.log(POILogger.DEBUG, "validity: " + certificate.getNotBefore() + " - " + certificate.getNotAfter());
                }
            }
        };
        signatureConfig.setTspValidator(tspValidator);
        signatureConfig.setTspOldProtocol(signatureConfig.getTspUrl().contains("edelweb"));
    }
    final RevocationData revocationData = new RevocationData();
    revocationData.addCRL(crl);
    OCSPResp ocspResp = PkiTestUtils.createOcspResp(x509, false, x509, x509, keyPair.getPrivate(), "SHA1withRSA", cal.getTimeInMillis());
    revocationData.addOCSP(ocspResp.getEncoded());
    RevocationDataService revocationDataService = new RevocationDataService() {

        @Override
        public RevocationData getRevocationData(List<X509Certificate> revocationChain) {
            return revocationData;
        }
    };
    signatureConfig.setRevocationDataService(revocationDataService);
    // operate
    SignatureInfo si = new SignatureInfo();
    si.setSignatureConfig(signatureConfig);
    try {
        si.confirmSignature();
    } catch (RuntimeException e) {
        pkg.close();
        // only allow a ConnectException because of timeout, we see this in Jenkins from time to time...
        if (e.getCause() == null) {
            throw e;
        }
        if ((e.getCause() instanceof ConnectException) || (e.getCause() instanceof SocketTimeoutException)) {
            Assume.assumeFalse("Only allowing ConnectException with 'timed out' as message here, but had: " + e, e.getCause().getMessage().contains("timed out"));
        } else if (e.getCause() instanceof IOException) {
            Assume.assumeFalse("Only allowing IOException with 'Error contacting TSP server' as message here, but had: " + e, e.getCause().getMessage().contains("Error contacting TSP server"));
        } else if (e.getCause() instanceof RuntimeException) {
            Assume.assumeFalse("Only allowing RuntimeException with 'This site is cur' as message here, but had: " + e, e.getCause().getMessage().contains("This site is cur"));
        }
        throw e;
    }
    // verify
    Iterator<SignaturePart> spIter = si.getSignatureParts().iterator();
    assertTrue("Had: " + si.getSignatureConfig().getOpcPackage().getRelationshipsByType(PackageRelationshipTypes.DIGITAL_SIGNATURE_ORIGIN), spIter.hasNext());
    SignaturePart sp = spIter.next();
    boolean valid = sp.validate();
    assertTrue(valid);
    SignatureDocument sigDoc = sp.getSignatureDocument();
    String declareNS = "declare namespace xades='http://uri.etsi.org/01903/v1.3.2#'; " + "declare namespace ds='http://www.w3.org/2000/09/xmldsig#'; ";
    String digestValXQuery = declareNS + "$this/ds:Signature/ds:SignedInfo/ds:Reference";
    for (ReferenceType rt : (ReferenceType[]) sigDoc.selectPath(digestValXQuery)) {
        assertNotNull(rt.getDigestValue());
        assertEquals(signatureConfig.getDigestMethodUri(), rt.getDigestMethod().getAlgorithm());
    }
    String certDigestXQuery = declareNS + "$this//xades:SigningCertificate/xades:Cert/xades:CertDigest";
    XmlObject[] xoList = sigDoc.selectPath(certDigestXQuery);
    assertEquals(xoList.length, 1);
    DigestAlgAndValueType certDigest = (DigestAlgAndValueType) xoList[0];
    assertNotNull(certDigest.getDigestValue());
    String qualPropXQuery = declareNS + "$this/ds:Signature/ds:Object/xades:QualifyingProperties";
    xoList = sigDoc.selectPath(qualPropXQuery);
    assertEquals(xoList.length, 1);
    QualifyingPropertiesType qualProp = (QualifyingPropertiesType) xoList[0];
    boolean qualPropXsdOk = qualProp.validate();
    assertTrue(qualPropXsdOk);
    pkg.close();
}
Also used : X509CRL(java.security.cert.X509CRL) EnvelopedSignatureFacet(org.apache.poi.poifs.crypt.dsig.facets.EnvelopedSignatureFacet) SignatureDocument(org.w3.x2000.x09.xmldsig.SignatureDocument) ArrayList(java.util.ArrayList) RevocationDataService(org.apache.poi.poifs.crypt.dsig.services.RevocationDataService) XAdESXLSignatureFacet(org.apache.poi.poifs.crypt.dsig.facets.XAdESXLSignatureFacet) ReferenceType(org.w3.x2000.x09.xmldsig.ReferenceType) DigestAlgAndValueType(org.etsi.uri.x01903.v13.DigestAlgAndValueType) OCSPResp(org.bouncycastle.cert.ocsp.OCSPResp) TimeStampService(org.apache.poi.poifs.crypt.dsig.services.TimeStampService) KeyInfoSignatureFacet(org.apache.poi.poifs.crypt.dsig.facets.KeyInfoSignatureFacet) List(java.util.List) ArrayList(java.util.ArrayList) ConnectException(java.net.ConnectException) RevocationData(org.apache.poi.poifs.crypt.dsig.services.RevocationData) SignatureConfig(org.apache.poi.poifs.crypt.dsig.SignatureConfig) IOException(java.io.IOException) X509Certificate(java.security.cert.X509Certificate) SignatureInfo(org.apache.poi.poifs.crypt.dsig.SignatureInfo) TimeStampServiceValidator(org.apache.poi.poifs.crypt.dsig.services.TimeStampServiceValidator) SocketTimeoutException(java.net.SocketTimeoutException) QualifyingPropertiesType(org.etsi.uri.x01903.v13.QualifyingPropertiesType) XmlObject(org.apache.xmlbeans.XmlObject) SignaturePart(org.apache.poi.poifs.crypt.dsig.SignatureInfo.SignaturePart) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) XAdESSignatureFacet(org.apache.poi.poifs.crypt.dsig.facets.XAdESSignatureFacet) Test(org.junit.Test)

Example 7 with If

use of org.w3._2007.rif.If in project poi by apache.

the class TestSignatureInfo method sign.

private void sign(OPCPackage pkgCopy, String alias, String signerDn, int signerCount) throws Exception {
    initKeyPair(alias, signerDn);
    SignatureConfig signatureConfig = new SignatureConfig();
    signatureConfig.setKey(keyPair.getPrivate());
    signatureConfig.setSigningCertificateChain(Collections.singletonList(x509));
    signatureConfig.setExecutionTime(cal.getTime());
    signatureConfig.setDigestAlgo(HashAlgorithm.sha1);
    signatureConfig.setOpcPackage(pkgCopy);
    SignatureInfo si = new SignatureInfo();
    si.setSignatureConfig(signatureConfig);
    Document document = DocumentHelper.createDocument();
    // operate
    DigestInfo digestInfo = si.preSign(document, null);
    // verify
    assertNotNull(digestInfo);
    LOG.log(POILogger.DEBUG, "digest algo: " + digestInfo.hashAlgo);
    LOG.log(POILogger.DEBUG, "digest description: " + digestInfo.description);
    assertEquals("Office OpenXML Document", digestInfo.description);
    assertNotNull(digestInfo.hashAlgo);
    assertNotNull(digestInfo.digestValue);
    // setup: key material, signature value
    byte[] signatureValue = si.signDigest(digestInfo.digestValue);
    // operate: postSign
    si.postSign(document, signatureValue);
    // verify: signature
    si.getSignatureConfig().setOpcPackage(pkgCopy);
    List<X509Certificate> result = new ArrayList<X509Certificate>();
    for (SignaturePart sp : si.getSignatureParts()) {
        if (sp.validate()) {
            result.add(sp.getSigner());
        }
    }
    assertEquals(signerCount, result.size());
}
Also used : SignatureInfo(org.apache.poi.poifs.crypt.dsig.SignatureInfo) DigestInfo(org.apache.poi.poifs.crypt.dsig.DigestInfo) SignatureConfig(org.apache.poi.poifs.crypt.dsig.SignatureConfig) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) SignatureDocument(org.w3.x2000.x09.xmldsig.SignatureDocument) SignaturePart(org.apache.poi.poifs.crypt.dsig.SignatureInfo.SignaturePart) X509Certificate(java.security.cert.X509Certificate)

Example 8 with If

use of org.w3._2007.rif.If in project camel by apache.

the class Soap12DataFormatAdapter method doUnmarshal.

@Override
public Object doUnmarshal(Exchange exchange, InputStream stream, Object rootObject) throws IOException {
    if (rootObject.getClass() != Envelope.class) {
        throw new RuntimeCamelException("Expected Soap Envelope but got " + rootObject.getClass());
    }
    Envelope envelope = (Envelope) rootObject;
    Header header = envelope.getHeader();
    if (header != null) {
        List<Object> returnHeaders;
        List<Object> anyHeaderElements = envelope.getHeader().getAny();
        if (null != anyHeaderElements && !(getDataFormat().isIgnoreUnmarshalledHeaders())) {
            if (getDataFormat().isIgnoreJAXBElement()) {
                returnHeaders = new ArrayList<Object>();
                for (Object headerEl : anyHeaderElements) {
                    returnHeaders.add(JAXBIntrospector.getValue(headerEl));
                }
            } else {
                returnHeaders = anyHeaderElements;
            }
            exchange.getOut().setHeader(SoapJaxbDataFormat.SOAP_UNMARSHALLED_HEADER_LIST, returnHeaders);
        }
    }
    List<Object> anyElement = envelope.getBody().getAny();
    if (anyElement.size() == 0) {
        // No parameter so return null
        return null;
    }
    Object payloadEl = anyElement.get(0);
    Object payload = JAXBIntrospector.getValue(payloadEl);
    if (payload instanceof Fault) {
        Exception exception = createExceptionFromFault((Fault) payload);
        exchange.setException(exception);
        return null;
    } else {
        return getDataFormat().isIgnoreJAXBElement() ? payload : payloadEl;
    }
}
Also used : Header(org.w3._2003._05.soap_envelope.Header) RuntimeCamelException(org.apache.camel.RuntimeCamelException) Fault(org.w3._2003._05.soap_envelope.Fault) WebFault(javax.xml.ws.WebFault) Envelope(org.w3._2003._05.soap_envelope.Envelope) SOAPException(javax.xml.soap.SOAPException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) IOException(java.io.IOException) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException)

Example 9 with If

use of org.w3._2007.rif.If in project camel by apache.

the class Soap12DataFormatAdapter method createExceptionFromFault.

/**
     * Creates an exception and eventually an embedded bean that contains the
     * fault detail. The exception class is determined by using the
     * elementNameStrategy. The qName of the fault detail should match the
     * WebFault annotation of the Exception class. If no fault detail is set
     * a {@link javax.xml.ws.soap.SOAPFaultException} is created.
     * 
     * @param fault Soap fault
     * @return created Exception
     */
private Exception createExceptionFromFault(Fault fault) {
    StringBuilder sb = new StringBuilder();
    for (Reasontext text : fault.getReason().getText()) {
        sb.append(text.getValue());
    }
    String message = sb.toString();
    Detail faultDetail = fault.getDetail();
    if (faultDetail == null || faultDetail.getAny().size() == 0) {
        try {
            return new SOAPFaultException(SOAPFactory.newInstance().createFault(message, fault.getCode().getValue()));
        } catch (SOAPException e) {
            throw new RuntimeCamelException(e);
        }
    }
    JAXBElement<?> detailEl = (JAXBElement<?>) faultDetail.getAny().get(0);
    Class<? extends Exception> exceptionClass = getDataFormat().getElementNameStrategy().findExceptionForFaultName(detailEl.getName());
    Constructor<? extends Exception> messageConstructor;
    Constructor<? extends Exception> constructor;
    try {
        messageConstructor = exceptionClass.getConstructor(String.class);
        Object detail = JAXBIntrospector.getValue(detailEl);
        try {
            constructor = exceptionClass.getConstructor(String.class, detail.getClass());
            return constructor.newInstance(message, detail);
        } catch (NoSuchMethodException e) {
            return messageConstructor.newInstance(message);
        }
    } catch (Exception e) {
        throw new RuntimeCamelException(e);
    }
}
Also used : Reasontext(org.w3._2003._05.soap_envelope.Reasontext) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) JAXBElement(javax.xml.bind.JAXBElement) SOAPException(javax.xml.soap.SOAPException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) IOException(java.io.IOException) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) SOAPException(javax.xml.soap.SOAPException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) Detail(org.w3._2003._05.soap_envelope.Detail)

Example 10 with If

use of org.w3._2007.rif.If in project hale by halestudio.

the class TestModelRifToRifTranslator method checkAndChildren.

private void checkAndChildren(And and) {
    assertNotNull(and.getFormula());
    assertTrue(and.getFormula().size() >= 1);
    for (Formula formula : and.getFormula()) {
        if (formula.getMember() != null) {
            checkMemberElement(formula.getMember());
        } else if (formula.getFrame() != null) {
        // checkPropertyFrame(formula.getFrame());
        }
    }
}
Also used : Formula(org.w3._2007.rif.Formula)

Aggregations

Actuate (org.n52.shetland.w3c.xlink.Actuate)15 Show (org.n52.shetland.w3c.xlink.Show)15 ActuateType (org.w3.x1999.xlink.ActuateType)15 ShowType (org.w3.x1999.xlink.ShowType)15 Reference (org.n52.shetland.w3c.xlink.Reference)14 Type (org.n52.shetland.w3c.xlink.Type)14 TypeType (org.w3.x1999.xlink.TypeType)14 IOException (java.io.IOException)13 XmlObject (org.apache.xmlbeans.XmlObject)11 AbstractCRSType (net.opengis.gml.x32.AbstractCRSType)10 CodeType (net.opengis.gml.x32.CodeType)10 EXExtentType (org.isotc211.x2005.gmd.EXExtentType)10 ProvideAndRegisterDocumentSetRequestType (ihe.iti.xds_b._2007.ProvideAndRegisterDocumentSetRequestType)9 ArrayList (java.util.ArrayList)8 CIResponsiblePartyPropertyType (org.isotc211.x2005.gmd.CIResponsiblePartyPropertyType)8 CIResponsiblePartyType (org.isotc211.x2005.gmd.CIResponsiblePartyType)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 BaseUnitType (net.opengis.gml.x32.BaseUnitType)6 VerticalDatumPropertyType (net.opengis.gml.x32.VerticalDatumPropertyType)5 VerticalDatumType (net.opengis.gml.x32.VerticalDatumType)5