Search in sources :

Example 1 with XMLX509Certificate

use of org.apache.xml.security.keys.content.x509.XMLX509Certificate in project jdk8u_jdk by JetBrains.

the class X509CertificateResolver method engineLookupResolveX509Certificate.

/**
     * Method engineResolveX509Certificate
     * @inheritDoc
     * @param element
     * @param BaseURI
     * @param storage
     *
     * @throws KeyResolverException
     */
public X509Certificate engineLookupResolveX509Certificate(Element element, String BaseURI, StorageResolver storage) throws KeyResolverException {
    try {
        Element[] els = XMLUtils.selectDsNodes(element.getFirstChild(), Constants._TAG_X509CERTIFICATE);
        if ((els == null) || (els.length == 0)) {
            Element el = XMLUtils.selectDsNode(element.getFirstChild(), Constants._TAG_X509DATA, 0);
            if (el != null) {
                return engineLookupResolveX509Certificate(el, BaseURI, storage);
            }
            return null;
        }
        // populate Object array
        for (int i = 0; i < els.length; i++) {
            XMLX509Certificate xmlCert = new XMLX509Certificate(els[i], BaseURI);
            X509Certificate cert = xmlCert.getX509Certificate();
            if (cert != null) {
                return cert;
            }
        }
        return null;
    } catch (XMLSecurityException ex) {
        if (log.isLoggable(java.util.logging.Level.FINE)) {
            log.log(java.util.logging.Level.FINE, "XMLSecurityException", ex);
        }
        throw new KeyResolverException("generic.EmptyMessage", ex);
    }
}
Also used : XMLX509Certificate(com.sun.org.apache.xml.internal.security.keys.content.x509.XMLX509Certificate) Element(org.w3c.dom.Element) KeyResolverException(com.sun.org.apache.xml.internal.security.keys.keyresolver.KeyResolverException) X509Certificate(java.security.cert.X509Certificate) XMLX509Certificate(com.sun.org.apache.xml.internal.security.keys.content.x509.XMLX509Certificate) XMLSecurityException(com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException)

Example 2 with XMLX509Certificate

use of org.apache.xml.security.keys.content.x509.XMLX509Certificate in project jdk8u_jdk by JetBrains.

the class PrivateKeyResolver method resolveX509Data.

private PrivateKey resolveX509Data(Element element, String baseURI) {
    log.log(java.util.logging.Level.FINE, "Can I resolve X509Data?");
    try {
        X509Data x509Data = new X509Data(element, baseURI);
        int len = x509Data.lengthSKI();
        for (int i = 0; i < len; i++) {
            XMLX509SKI x509SKI = x509Data.itemSKI(i);
            PrivateKey privKey = resolveX509SKI(x509SKI);
            if (privKey != null) {
                return privKey;
            }
        }
        len = x509Data.lengthIssuerSerial();
        for (int i = 0; i < len; i++) {
            XMLX509IssuerSerial x509Serial = x509Data.itemIssuerSerial(i);
            PrivateKey privKey = resolveX509IssuerSerial(x509Serial);
            if (privKey != null) {
                return privKey;
            }
        }
        len = x509Data.lengthSubjectName();
        for (int i = 0; i < len; i++) {
            XMLX509SubjectName x509SubjectName = x509Data.itemSubjectName(i);
            PrivateKey privKey = resolveX509SubjectName(x509SubjectName);
            if (privKey != null) {
                return privKey;
            }
        }
        len = x509Data.lengthCertificate();
        for (int i = 0; i < len; i++) {
            XMLX509Certificate x509Cert = x509Data.itemCertificate(i);
            PrivateKey privKey = resolveX509Certificate(x509Cert);
            if (privKey != null) {
                return privKey;
            }
        }
    } catch (XMLSecurityException e) {
        log.log(java.util.logging.Level.FINE, "XMLSecurityException", e);
    } catch (KeyStoreException e) {
        log.log(java.util.logging.Level.FINE, "KeyStoreException", e);
    }
    return null;
}
Also used : XMLX509Certificate(com.sun.org.apache.xml.internal.security.keys.content.x509.XMLX509Certificate) PrivateKey(java.security.PrivateKey) XMLX509SKI(com.sun.org.apache.xml.internal.security.keys.content.x509.XMLX509SKI) XMLX509IssuerSerial(com.sun.org.apache.xml.internal.security.keys.content.x509.XMLX509IssuerSerial) KeyStoreException(java.security.KeyStoreException) X509Data(com.sun.org.apache.xml.internal.security.keys.content.X509Data) XMLX509SubjectName(com.sun.org.apache.xml.internal.security.keys.content.x509.XMLX509SubjectName) XMLSecurityException(com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException)

Example 3 with XMLX509Certificate

use of org.apache.xml.security.keys.content.x509.XMLX509Certificate in project xades4j by luisgoncalves.

the class KeyInfoBuilderTest method testIncludeCertAndKey.

@Test
public void testIncludeCertAndKey() throws Exception {
    System.out.println("includeCertAndKey");
    KeyInfoBuilder keyInfoBuilder = new KeyInfoBuilder(new TestBasicSignatureOptionsProvider(true, true, false), new TestAlgorithmsProvider(), new TestAlgorithmsParametersMarshallingProvider());
    XMLSignature xmlSignature = getTestSignature();
    keyInfoBuilder.buildKeyInfo(testCertificate, xmlSignature);
    Assert.assertEquals(0, xmlSignature.getSignedInfo().getLength());
    KeyValue kv = xmlSignature.getKeyInfo().itemKeyValue(0);
    Assert.assertTrue(kv.getPublicKey().getAlgorithm().startsWith("RSA"));
    XMLX509Certificate x509Certificate = xmlSignature.getKeyInfo().itemX509Data(0).itemCertificate(0);
    Assert.assertEquals(testCertificate, x509Certificate.getX509Certificate());
}
Also used : XMLX509Certificate(org.apache.xml.security.keys.content.x509.XMLX509Certificate) KeyValue(org.apache.xml.security.keys.content.KeyValue) XMLSignature(org.apache.xml.security.signature.XMLSignature) Test(org.junit.Test)

Example 4 with XMLX509Certificate

use of org.apache.xml.security.keys.content.x509.XMLX509Certificate in project santuario-java by apache.

the class PrivateKeyResolver method resolveX509Data.

private PrivateKey resolveX509Data(Element element, String baseURI) {
    LOG.debug("Can I resolve X509Data?");
    try {
        X509Data x509Data = new X509Data(element, baseURI);
        int len = x509Data.lengthSKI();
        for (int i = 0; i < len; i++) {
            XMLX509SKI x509SKI = x509Data.itemSKI(i);
            PrivateKey privKey = resolveX509SKI(x509SKI);
            if (privKey != null) {
                return privKey;
            }
        }
        len = x509Data.lengthIssuerSerial();
        for (int i = 0; i < len; i++) {
            XMLX509IssuerSerial x509Serial = x509Data.itemIssuerSerial(i);
            PrivateKey privKey = resolveX509IssuerSerial(x509Serial);
            if (privKey != null) {
                return privKey;
            }
        }
        len = x509Data.lengthSubjectName();
        for (int i = 0; i < len; i++) {
            XMLX509SubjectName x509SubjectName = x509Data.itemSubjectName(i);
            PrivateKey privKey = resolveX509SubjectName(x509SubjectName);
            if (privKey != null) {
                return privKey;
            }
        }
        len = x509Data.lengthCertificate();
        for (int i = 0; i < len; i++) {
            XMLX509Certificate x509Cert = x509Data.itemCertificate(i);
            PrivateKey privKey = resolveX509Certificate(x509Cert);
            if (privKey != null) {
                return privKey;
            }
        }
    } catch (XMLSecurityException e) {
        LOG.debug("XMLSecurityException", e);
    } catch (KeyStoreException e) {
        LOG.debug("KeyStoreException", e);
    }
    return null;
}
Also used : XMLX509Certificate(org.apache.xml.security.keys.content.x509.XMLX509Certificate) PrivateKey(java.security.PrivateKey) XMLX509SKI(org.apache.xml.security.keys.content.x509.XMLX509SKI) XMLX509IssuerSerial(org.apache.xml.security.keys.content.x509.XMLX509IssuerSerial) KeyStoreException(java.security.KeyStoreException) X509Data(org.apache.xml.security.keys.content.X509Data) XMLX509SubjectName(org.apache.xml.security.keys.content.x509.XMLX509SubjectName) XMLSecurityException(org.apache.xml.security.exceptions.XMLSecurityException)

Example 5 with XMLX509Certificate

use of org.apache.xml.security.keys.content.x509.XMLX509Certificate in project santuario-java by apache.

the class BaltimoreEncTest method findKey.

/**
 * Method findKey
 *
 * Given an encryptedData structure, return the key that will decrypt
 * it
 *
 * @param encryptedData EncryptedData to get key for
 */
private Key findKey(EncryptedData encryptedData) throws Exception {
    KeyInfo ki = encryptedData.getKeyInfo();
    Key key = null;
    Key kek = null;
    if (ki == null) {
        return null;
    }
    // First check for a known key name
    KeyName keyName = ki.itemKeyName(0);
    if (keyName != null) {
        return mapKeyName(keyName.getKeyName());
    }
    // Decrypt any encryptedKey structures
    EncryptedKey encryptedKey = ki.itemEncryptedKey(0);
    if (encryptedKey == null) {
        return null;
    }
    KeyInfo kiek = encryptedKey.getKeyInfo();
    if (kiek == null) {
        return null;
    }
    KeyName kekKeyName = kiek.itemKeyName(0);
    if (kekKeyName != null) {
        kek = mapKeyName(kekKeyName.getKeyName());
    } else {
        X509Data certData = kiek.itemX509Data(0);
        XMLX509Certificate xcert = certData.itemCertificate(0);
        X509Certificate cert = xcert.getX509Certificate();
        if (cert != null && cert.getSerialNumber().toString().equals(rsaCertSerialNumber)) {
            kek = rsaKey;
        }
    }
    if (kek != null) {
        XMLCipher cipher = XMLCipher.getInstance();
        cipher.init(XMLCipher.UNWRAP_MODE, kek);
        key = cipher.decryptKey(encryptedKey, encryptedData.getEncryptionMethod().getAlgorithm());
    }
    return key;
}
Also used : KeyName(org.apache.xml.security.keys.content.KeyName) XMLX509Certificate(org.apache.xml.security.keys.content.x509.XMLX509Certificate) KeyInfo(org.apache.xml.security.keys.KeyInfo) EncryptedKey(org.apache.xml.security.encryption.EncryptedKey) XMLCipher(org.apache.xml.security.encryption.XMLCipher) X509Data(org.apache.xml.security.keys.content.X509Data) EncryptedKey(org.apache.xml.security.encryption.EncryptedKey) Key(java.security.Key) PrivateKey(java.security.PrivateKey) SecretKey(javax.crypto.SecretKey) X509Certificate(java.security.cert.X509Certificate) XMLX509Certificate(org.apache.xml.security.keys.content.x509.XMLX509Certificate)

Aggregations

XMLX509Certificate (org.apache.xml.security.keys.content.x509.XMLX509Certificate)9 X509Certificate (java.security.cert.X509Certificate)8 PrivateKey (java.security.PrivateKey)7 X509Data (org.apache.xml.security.keys.content.X509Data)5 Key (java.security.Key)4 KeyStoreException (java.security.KeyStoreException)4 SecretKey (javax.crypto.SecretKey)4 XMLSecurityException (org.apache.xml.security.exceptions.XMLSecurityException)4 Document (org.w3c.dom.Document)4 Element (org.w3c.dom.Element)4 XMLSecurityException (com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException)3 XMLX509Certificate (com.sun.org.apache.xml.internal.security.keys.content.x509.XMLX509Certificate)3 PublicKey (java.security.PublicKey)3 KeyInfo (org.apache.xml.security.keys.KeyInfo)3 KeyResolverException (com.sun.org.apache.xml.internal.security.keys.keyresolver.KeyResolverException)2 FileInputStream (java.io.FileInputStream)2 Certificate (java.security.cert.Certificate)2 CertificateEncodingException (java.security.cert.CertificateEncodingException)2 EncryptedKey (org.apache.xml.security.encryption.EncryptedKey)2 XMLCipher (org.apache.xml.security.encryption.XMLCipher)2