Example usage for javax.xml.crypto KeySelectorException KeySelectorException

List of usage examples for javax.xml.crypto KeySelectorException KeySelectorException

Introduction

In this page you can find the example usage for javax.xml.crypto KeySelectorException KeySelectorException.

Prototype

public KeySelectorException(Throwable cause) 

Source Link

Document

Constructs a new KeySelectorException with the specified cause and a detail message of (cause==null ?

Usage

From source file:be.fedict.eid.tsl.KeyInfoKeySelector.java

@Override
public KeySelectorResult select(KeyInfo keyInfo, Purpose purpose, AlgorithmMethod method,
        XMLCryptoContext context) throws KeySelectorException {
    LOG.debug("select key");
    List<XMLStructure> keyInfoContent = keyInfo.getContent();
    for (XMLStructure keyInfoStructure : keyInfoContent) {
        if (false == (keyInfoStructure instanceof X509Data)) {
            continue;
        }//www  .  j ava  2s .  c  o m
        X509Data x509Data = (X509Data) keyInfoStructure;
        List<Object> x509DataList = x509Data.getContent();
        for (Object x509DataObject : x509DataList) {
            if (false == (x509DataObject instanceof X509Certificate)) {
                continue;
            }
            this.certificate = (X509Certificate) x509DataObject;
            // stop after first match
            return this;
        }
    }
    throw new KeySelectorException("No key found!");
}

From source file:be.fedict.eid.applet.service.signer.KeyInfoKeySelector.java

@SuppressWarnings("unchecked")
@Override//from   w w w .  j av a  2s. co m
public KeySelectorResult select(KeyInfo keyInfo, Purpose purpose, AlgorithmMethod method,
        XMLCryptoContext context) throws KeySelectorException {
    LOG.debug("select key");
    if (null == keyInfo) {
        throw new KeySelectorException("no ds:KeyInfo present");
    }
    List<XMLStructure> keyInfoContent = keyInfo.getContent();
    this.certificate = null;
    for (XMLStructure keyInfoStructure : keyInfoContent) {
        if (false == (keyInfoStructure instanceof X509Data)) {
            continue;
        }
        X509Data x509Data = (X509Data) keyInfoStructure;
        List<Object> x509DataList = x509Data.getContent();
        for (Object x509DataObject : x509DataList) {
            if (false == (x509DataObject instanceof X509Certificate)) {
                continue;
            }
            X509Certificate certificate = (X509Certificate) x509DataObject;
            LOG.debug("certificate: " + certificate.getSubjectX500Principal());
            if (null == this.certificate) {
                /*
                 * The first certificate is presumably the signer.
                 */
                this.certificate = certificate;
                LOG.debug("signer certificate: " + certificate.getSubjectX500Principal());
            }
        }
        if (null != this.certificate) {
            return this;
        }
    }
    throw new KeySelectorException("No key found!");
}

From source file:be.fedict.eid.applet.service.signer.ooxml.OPCKeySelector.java

@Override
public KeySelectorResult select(KeyInfo keyInfo, Purpose purpose, AlgorithmMethod method,
        XMLCryptoContext context) throws KeySelectorException {
    try {/*w w  w  .ja  v a  2  s . c  om*/
        return super.select(keyInfo, purpose, method, context);
    } catch (KeySelectorException e) {
        LOG.debug("no key found via ds:KeyInfo key selector");
    }
    LOG.debug("signature resource name: " + this.signatureResourceName);
    String signatureSegment = this.signatureResourceName.substring(0,
            this.signatureResourceName.lastIndexOf("/"));
    LOG.debug("signature segment: " + signatureSegment);
    String signatureBase = this.signatureResourceName
            .substring(this.signatureResourceName.lastIndexOf("/") + 1);
    LOG.debug("signature base: " + signatureBase);
    String signatureRelationshipResourceName = signatureSegment + "/_rels/" + signatureBase + ".rels";
    LOG.debug("signature relationship resource name: " + signatureRelationshipResourceName);

    ZipArchiveInputStream zipInputStream;
    try {
        zipInputStream = new ZipArchiveInputStream(this.opcUrl.openStream(), "UTF8", true, true);
    } catch (IOException e) {
        throw new KeySelectorException(e);
    }
    ZipArchiveEntry zipEntry;
    try {
        while (null != (zipEntry = zipInputStream.getNextZipEntry())) {
            if (signatureRelationshipResourceName.equals(zipEntry.getName())) {
                break;
            }
        }
    } catch (IOException e) {
        throw new KeySelectorException(e);
    }
    if (null == zipEntry) {
        LOG.warn("relationship part not present: " + signatureRelationshipResourceName);
        throw new KeySelectorException("no key found");
    }
    LOG.debug("signature relationship part found");

    JAXBElement<CTRelationships> signatureRelationshipsElement;
    try {
        signatureRelationshipsElement = (JAXBElement<CTRelationships>) this.relationshipsUnmarshaller
                .unmarshal(zipInputStream);
    } catch (JAXBException e) {
        throw new KeySelectorException(e);
    }
    CTRelationships signatureRelationships = signatureRelationshipsElement.getValue();
    List<CTRelationship> signatureRelationshipList = signatureRelationships.getRelationship();
    List<String> certificateResourceNames = new LinkedList<String>();
    for (CTRelationship signatureRelationship : signatureRelationshipList) {
        if (DIGITAL_SIGNATURE_CERTIFICATE_REL_TYPE.equals(signatureRelationship.getType())) {
            String certificateResourceName = signatureRelationship.getTarget().substring(1);
            certificateResourceNames.add(certificateResourceName);
        }
    }

    X509Certificate endEntityCertificate = null;

    for (String certificateResourceName : certificateResourceNames) {
        try {
            zipInputStream = new ZipArchiveInputStream(this.opcUrl.openStream(), "UTF8", true, true);
        } catch (IOException e) {
            throw new KeySelectorException(e);
        }
        try {
            while (null != (zipEntry = zipInputStream.getNextZipEntry())) {
                if (certificateResourceName.equals(zipEntry.getName())) {
                    break;
                }
            }
        } catch (IOException e) {
            throw new KeySelectorException(e);
        }
        if (null == zipEntry) {
            LOG.warn("certificate part not present: " + certificateResourceName);
            continue;
        }
        X509Certificate certificate;
        try {
            certificate = (X509Certificate) this.certificateFactory.generateCertificate(zipInputStream);
        } catch (CertificateException e) {
            throw new KeySelectorException(e);
        }
        LOG.debug("certificate subject: " + certificate.getSubjectX500Principal());
        if (-1 != certificate.getBasicConstraints()) {
            LOG.debug("skipping CA certificate");
            continue;
        }
        if (null != endEntityCertificate) {
            throw new KeySelectorException("two possible end entity certificates");
        }
        endEntityCertificate = certificate;
    }
    if (null == endEntityCertificate) {
        throw new KeySelectorException("no key found");
    }
    this.certificate = endEntityCertificate;
    return this;
}

From source file:org.openehealth.coms.cc.web_frontend.consentcreator.service.DocumentFactory.java

public KeySelectorResult select(KeyInfo keyInfo, KeySelector.Purpose purpose, AlgorithmMethod method,
        XMLCryptoContext context) throws KeySelectorException {
    Iterator ki = keyInfo.getContent().iterator();
    while (ki.hasNext()) {
        XMLStructure info = (XMLStructure) ki.next();
        if (!(info instanceof X509Data))
            continue;
        X509Data x509Data = (X509Data) info;
        Iterator xi = x509Data.getContent().iterator();
        while (xi.hasNext()) {
            Object o = xi.next();
            if (!(o instanceof X509Certificate))
                continue;
            final PublicKey key = ((X509Certificate) o).getPublicKey();
            // Make sure the algorithm is compatible
            // with the method.
            if (algEquals(method.getAlgorithm(), key.getAlgorithm())) {
                return new KeySelectorResult() {
                    public Key getKey() {
                        return key;
                    }//from w w w .  jav a 2 s.  c om
                };
            }
        }
    }
    Logger.getLogger(this.getClass()).error("No Key found");
    throw new KeySelectorException("No key found!");
}