Search in sources :

Example 1 with PrivateKeyResolver

use of org.apache.xml.security.keys.keyresolver.implementations.PrivateKeyResolver in project santuario-java by apache.

the class KeyResolverTest method testKeyResolvers.

/**
 * Test key resolvers through a KeyInfo.
 */
@org.junit.Test
public void testKeyResolvers() throws Exception {
    // 
    if ("IBM Corporation".equals(System.getProperty("java.vendor"))) {
        return;
    }
    char[] pwd = "secret".toCharArray();
    KeyStore ks = KeyStore.getInstance("JCEKS");
    FileInputStream fis = null;
    if (BASEDIR != null && !"".equals(BASEDIR)) {
        fis = new FileInputStream(BASEDIR + SEP + "src/test/resources/test.jceks");
    } else {
        fis = new FileInputStream("src/test/resources/test.jceks");
    }
    ks.load(fis, pwd);
    X509Certificate cert = (X509Certificate) ks.getCertificate("rsakey");
    PublicKey publicKey = cert.getPublicKey();
    PrivateKey privateKey = (PrivateKey) ks.getKey("rsakey", pwd);
    SecretKey secretKey = (SecretKey) ks.getKey("des3key", pwd);
    StorageResolver storage = new StorageResolver(new KeyStoreResolver(ks));
    KeyResolverSpi privateKeyResolver = new PrivateKeyResolver(ks, pwd);
    KeyResolverSpi secretKeyResolver = new SecretKeyResolver(ks, pwd);
    DocumentBuilder db = XMLUtils.createDocumentBuilder(false);
    Document doc = db.newDocument();
    KeyInfo ki;
    X509Data x509data;
    // X509Certificate hint
    ki = new KeyInfo(doc);
    ki.addStorageResolver(storage);
    x509data = new X509Data(doc);
    x509data.add(new XMLX509Certificate(doc, cert));
    ki.add(x509data);
    assertEquals(publicKey, ki.getPublicKey());
    assertNull(ki.getPrivateKey());
    ki.registerInternalKeyResolver(privateKeyResolver);
    assertEquals(privateKey, ki.getPrivateKey());
    // Issuer/Serial hint
    ki = new KeyInfo(doc);
    ki.addStorageResolver(storage);
    x509data = new X509Data(doc);
    x509data.add(new XMLX509IssuerSerial(doc, cert.getIssuerX500Principal().getName(), cert.getSerialNumber()));
    ki.add(x509data);
    assertEquals(publicKey, ki.getPublicKey());
    ki.registerInternalKeyResolver(privateKeyResolver);
    assertEquals(privateKey, ki.getPrivateKey());
    // SubjectName hint
    ki = new KeyInfo(doc);
    ki.addStorageResolver(storage);
    x509data = new X509Data(doc);
    x509data.add(new XMLX509SubjectName(doc, cert.getSubjectX500Principal().getName()));
    ki.add(x509data);
    assertEquals(publicKey, ki.getPublicKey());
    ki.registerInternalKeyResolver(privateKeyResolver);
    assertEquals(privateKey, ki.getPrivateKey());
    // SKI hint
    ki = new KeyInfo(doc);
    ki.addStorageResolver(storage);
    x509data = new X509Data(doc);
    x509data.add(new XMLX509SKI(doc, cert));
    ki.add(x509data);
    assertEquals(publicKey, ki.getPublicKey());
    ki.registerInternalKeyResolver(privateKeyResolver);
    assertEquals(privateKey, ki.getPrivateKey());
    // KeyName hint
    String rsaKeyName = "rsakey";
    ki = new KeyInfo(doc);
    ki.addKeyName(rsaKeyName);
    ki.registerInternalKeyResolver(new SingleKeyResolver(rsaKeyName, publicKey));
    assertEquals(publicKey, ki.getPublicKey());
    ki = new KeyInfo(doc);
    ki.addKeyName(rsaKeyName);
    ki.registerInternalKeyResolver(privateKeyResolver);
    assertEquals(privateKey, ki.getPrivateKey());
    ki = new KeyInfo(doc);
    ki.addKeyName(rsaKeyName);
    ki.registerInternalKeyResolver(new SingleKeyResolver(rsaKeyName, privateKey));
    assertEquals(privateKey, ki.getPrivateKey());
    String des3KeyName = "des3key";
    ki = new KeyInfo(doc);
    ki.addKeyName(des3KeyName);
    ki.registerInternalKeyResolver(secretKeyResolver);
    assertEquals(secretKey, ki.getSecretKey());
    ki = new KeyInfo(doc);
    ki.addKeyName(des3KeyName);
    ki.registerInternalKeyResolver(new SingleKeyResolver(des3KeyName, secretKey));
    assertEquals(secretKey, ki.getSecretKey());
}
Also used : SingleKeyResolver(org.apache.xml.security.keys.keyresolver.implementations.SingleKeyResolver) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PrivateKey(java.security.PrivateKey) StorageResolver(org.apache.xml.security.keys.storage.StorageResolver) RSAPublicKey(java.security.interfaces.RSAPublicKey) PublicKey(java.security.PublicKey) PrivateKeyResolver(org.apache.xml.security.keys.keyresolver.implementations.PrivateKeyResolver) XMLX509IssuerSerial(org.apache.xml.security.keys.content.x509.XMLX509IssuerSerial) Document(org.w3c.dom.Document) KeyStore(java.security.KeyStore) X509Data(org.apache.xml.security.keys.content.X509Data) FileInputStream(java.io.FileInputStream) X509Certificate(java.security.cert.X509Certificate) XMLX509Certificate(org.apache.xml.security.keys.content.x509.XMLX509Certificate) XMLX509SubjectName(org.apache.xml.security.keys.content.x509.XMLX509SubjectName) XMLX509Certificate(org.apache.xml.security.keys.content.x509.XMLX509Certificate) SecretKey(javax.crypto.SecretKey) KeyStoreResolver(org.apache.xml.security.keys.storage.implementations.KeyStoreResolver) DocumentBuilder(javax.xml.parsers.DocumentBuilder) KeyInfo(org.apache.xml.security.keys.KeyInfo) XMLX509SKI(org.apache.xml.security.keys.content.x509.XMLX509SKI) SecretKeyResolver(org.apache.xml.security.keys.keyresolver.implementations.SecretKeyResolver) KeyResolverSpi(org.apache.xml.security.keys.keyresolver.KeyResolverSpi)

Aggregations

FileInputStream (java.io.FileInputStream)1 KeyStore (java.security.KeyStore)1 PrivateKey (java.security.PrivateKey)1 PublicKey (java.security.PublicKey)1 X509Certificate (java.security.cert.X509Certificate)1 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)1 RSAPublicKey (java.security.interfaces.RSAPublicKey)1 SecretKey (javax.crypto.SecretKey)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 KeyInfo (org.apache.xml.security.keys.KeyInfo)1 X509Data (org.apache.xml.security.keys.content.X509Data)1 XMLX509Certificate (org.apache.xml.security.keys.content.x509.XMLX509Certificate)1 XMLX509IssuerSerial (org.apache.xml.security.keys.content.x509.XMLX509IssuerSerial)1 XMLX509SKI (org.apache.xml.security.keys.content.x509.XMLX509SKI)1 XMLX509SubjectName (org.apache.xml.security.keys.content.x509.XMLX509SubjectName)1 KeyResolverSpi (org.apache.xml.security.keys.keyresolver.KeyResolverSpi)1 PrivateKeyResolver (org.apache.xml.security.keys.keyresolver.implementations.PrivateKeyResolver)1 SecretKeyResolver (org.apache.xml.security.keys.keyresolver.implementations.SecretKeyResolver)1 SingleKeyResolver (org.apache.xml.security.keys.keyresolver.implementations.SingleKeyResolver)1 StorageResolver (org.apache.xml.security.keys.storage.StorageResolver)1