Search in sources :

Example 1 with KeySelectorException

use of javax.xml.crypto.KeySelectorException in project camel by apache.

the class DefaultKeySelector method select.

public KeySelectorResult select(KeyInfo keyInfo, KeySelector.Purpose purpose, AlgorithmMethod method, XMLCryptoContext context) throws KeySelectorException {
    if (keyStoreAndAlias.getKeyStore() == null) {
        return getNullKeyResult();
    }
    if (keyStoreAndAlias.getAlias() == null) {
        return getNullKeyResult();
    }
    if (KeySelector.Purpose.VERIFY.equals(purpose)) {
        Certificate cert;
        try {
            cert = keyStoreAndAlias.getKeyStore().getCertificate(keyStoreAndAlias.getAlias());
        } catch (KeyStoreException e) {
            throw new KeySelectorException(e);
        }
        if (cert == null) {
            return getNullKeyResult();
        }
        final Key key = cert.getPublicKey();
        return getKeySelectorResult(key);
    } else if (KeySelector.Purpose.SIGN.equals(purpose)) {
        if (keyStoreAndAlias.getPassword() == null) {
            return getNullKeyResult();
        }
        Key key;
        try {
            if (this.getCamelContext() != null && keyStoreAndAlias.getPassword() != null) {
                try {
                    String passwordProperty = this.getCamelContext().resolvePropertyPlaceholders(new String(keyStoreAndAlias.getPassword()));
                    key = keyStoreAndAlias.getKeyStore().getKey(keyStoreAndAlias.getAlias(), passwordProperty.toCharArray());
                } catch (Exception e) {
                    throw new RuntimeCamelException("Error parsing property value: " + new String(keyStoreAndAlias.getPassword()), e);
                }
            } else {
                key = keyStoreAndAlias.getKeyStore().getKey(keyStoreAndAlias.getAlias(), keyStoreAndAlias.getPassword());
            }
        } catch (UnrecoverableKeyException e) {
            throw new KeySelectorException(e);
        } catch (KeyStoreException e) {
            throw new KeySelectorException(e);
        } catch (NoSuchAlgorithmException e) {
            throw new KeySelectorException(e);
        }
        return getKeySelectorResult(key);
    } else {
        throw new IllegalStateException("Purpose " + purpose + " not supported");
    }
}
Also used : KeySelectorException(javax.xml.crypto.KeySelectorException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Key(java.security.Key) RuntimeCamelException(org.apache.camel.RuntimeCamelException) IOException(java.io.IOException) KeyStoreException(java.security.KeyStoreException) GeneralSecurityException(java.security.GeneralSecurityException) KeySelectorException(javax.xml.crypto.KeySelectorException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) Certificate(java.security.cert.Certificate)

Example 2 with KeySelectorException

use of javax.xml.crypto.KeySelectorException in project poi by apache.

the class KeyInfoKeySelector method select.

@SuppressWarnings("unchecked")
@Override
public KeySelectorResult select(KeyInfo keyInfo, Purpose purpose, AlgorithmMethod method, XMLCryptoContext context) throws KeySelectorException {
    LOG.log(POILogger.DEBUG, "select key");
    if (null == keyInfo) {
        throw new KeySelectorException("no ds:KeyInfo present");
    }
    List<XMLStructure> keyInfoContent = keyInfo.getContent();
    certChain.clear();
    for (XMLStructure keyInfoStructure : keyInfoContent) {
        if (!(keyInfoStructure instanceof X509Data)) {
            continue;
        }
        X509Data x509Data = (X509Data) keyInfoStructure;
        List<?> x509DataList = x509Data.getContent();
        for (Object x509DataObject : x509DataList) {
            if (!(x509DataObject instanceof X509Certificate)) {
                continue;
            }
            X509Certificate certificate = (X509Certificate) x509DataObject;
            LOG.log(POILogger.DEBUG, "certificate", certificate.getSubjectX500Principal());
            certChain.add(certificate);
        }
    }
    if (certChain.isEmpty()) {
        throw new KeySelectorException("No key found!");
    }
    return this;
}
Also used : KeySelectorException(javax.xml.crypto.KeySelectorException) XMLStructure(javax.xml.crypto.XMLStructure) X509Data(javax.xml.crypto.dsig.keyinfo.X509Data) X509Certificate(java.security.cert.X509Certificate)

Aggregations

KeySelectorException (javax.xml.crypto.KeySelectorException)2 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 Key (java.security.Key)1 KeyStoreException (java.security.KeyStoreException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 UnrecoverableKeyException (java.security.UnrecoverableKeyException)1 Certificate (java.security.cert.Certificate)1 X509Certificate (java.security.cert.X509Certificate)1 XMLStructure (javax.xml.crypto.XMLStructure)1 X509Data (javax.xml.crypto.dsig.keyinfo.X509Data)1 RuntimeCamelException (org.apache.camel.RuntimeCamelException)1