Search in sources :

Example 51 with DSAPrivateKey

use of java.security.interfaces.DSAPrivateKey in project robovm by robovm.

the class KeyPairGeneratorTest method testDSAGeneratorWithParams.

public void testDSAGeneratorWithParams() throws Exception {
    final DSAParameterSpec dsaSpec = new DSAParameterSpec(DSA_P, DSA_Q, DSA_G);
    boolean failure = false;
    final Provider[] providers = Security.getProviders();
    for (final Provider p : providers) {
        Service s = p.getService("KeyPairGenerator", "DSA");
        if (s == null) {
            continue;
        }
        final KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA", p);
        kpg.initialize(dsaSpec);
        KeyPair pair = kpg.generateKeyPair();
        DSAPrivateKey privKey = (DSAPrivateKey) pair.getPrivate();
        DSAPublicKey pubKey = (DSAPublicKey) pair.getPublic();
        DSAParams actualParams = privKey.getParams();
        assertNotNull("DSA params should not be null", actualParams);
        assertEquals("DSA P should be the same as supplied with provider " + p.getName(), DSA_P, actualParams.getP());
        assertEquals("DSA Q should be the same as supplied with provider " + p.getName(), DSA_Q, actualParams.getQ());
        assertEquals("DSA G should be the same as supplied with provider " + p.getName(), DSA_G, actualParams.getG());
        actualParams = pubKey.getParams();
        assertNotNull("DSA params should not be null", actualParams);
        assertEquals("DSA P should be the same as supplied with provider " + p.getName(), DSA_P, actualParams.getP());
        assertEquals("DSA Q should be the same as supplied with provider " + p.getName(), DSA_Q, actualParams.getQ());
        assertEquals("DSA G should be the same as supplied with provider " + p.getName(), DSA_G, actualParams.getG());
    }
}
Also used : DSAParameterSpec(java.security.spec.DSAParameterSpec) KeyPair(java.security.KeyPair) Service(java.security.Provider.Service) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) KeyPairGenerator(java.security.KeyPairGenerator) DSAParams(java.security.interfaces.DSAParams) Provider(java.security.Provider) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Example 52 with DSAPrivateKey

use of java.security.interfaces.DSAPrivateKey in project robovm by robovm.

the class KeyStore2Test method test_setEntry.

/**
     * java.security.KeyStore#setEntry(String, KeyStore.Entry,
     *        KeyStore.ProtectionParameter)
     */
public void test_setEntry() throws Exception {
    String type = "DSA";
    KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
    keyTest.load(null, pssWord);
    Certificate[] chain = { new MyCertificate(type, testEncoding), new MyCertificate(type, testEncoding) };
    DSAPrivateKey privateKey1 = (DSAPrivateKey) KeyFactory.getInstance(type).generatePrivate(new DSAPrivateKeySpec(new BigInteger("1"), new BigInteger("2"), new BigInteger("3"), new BigInteger("4")));
    KeyStore.PasswordProtection pp = new KeyStore.PasswordProtection(pssWord);
    KeyStore.PrivateKeyEntry pke = new KeyStore.PrivateKeyEntry(getPrivateKey(), chain);
    KeyStore.PrivateKeyEntry pke1 = new KeyStore.PrivateKeyEntry(privateKey1, chain);
    try {
        keyTest.setEntry("alias", pke, null);
        // BKS KeyStore does not require a password
        assertFalse(StandardNames.IS_RI);
    } catch (KeyStoreException e) {
        // JKS KeyStore requires a password
        assertTrue(StandardNames.IS_RI);
    }
    keyTest.setEntry("alias", pke, pp);
    KeyStore.PrivateKeyEntry pkeActual = (KeyStore.PrivateKeyEntry) keyTest.getEntry("alias", pp);
    assertTrue(Arrays.equals(chain, pkeActual.getCertificateChain()));
    assertEquals(getPrivateKey(), pkeActual.getPrivateKey());
    assertEquals(new MyCertificate(type, testEncoding), pkeActual.getCertificate());
    assertTrue(keyTest.entryInstanceOf("alias", KeyStore.PrivateKeyEntry.class));
    keyTest.setEntry("alias", pke1, pp);
    pkeActual = (KeyStore.PrivateKeyEntry) keyTest.getEntry("alias", pp);
    assertTrue(Arrays.equals(chain, pkeActual.getCertificateChain()));
    DSAPrivateKey actualPrivateKey = (DSAPrivateKey) pkeActual.getPrivateKey();
    assertEquals(privateKey1.getX(), actualPrivateKey.getX());
    assertEquals(privateKey1.getParams().getG(), actualPrivateKey.getParams().getG());
    assertEquals(privateKey1.getParams().getP(), actualPrivateKey.getParams().getP());
    assertEquals(privateKey1.getParams().getQ(), actualPrivateKey.getParams().getQ());
    assertEquals(new MyCertificate(type, testEncoding), pkeActual.getCertificate());
    assertTrue(keyTest.entryInstanceOf("alias", KeyStore.PrivateKeyEntry.class));
    keyTest.setEntry("alias2", pke1, pp);
    pkeActual = (KeyStore.PrivateKeyEntry) keyTest.getEntry("alias2", pp);
    assertTrue(Arrays.equals(chain, pkeActual.getCertificateChain()));
    actualPrivateKey = (DSAPrivateKey) pkeActual.getPrivateKey();
    assertEquals(privateKey1.getX(), actualPrivateKey.getX());
    assertEquals(privateKey1.getParams().getG(), actualPrivateKey.getParams().getG());
    assertEquals(privateKey1.getParams().getP(), actualPrivateKey.getParams().getP());
    assertEquals(privateKey1.getParams().getQ(), actualPrivateKey.getParams().getQ());
    assertEquals(new MyCertificate(type, testEncoding), pkeActual.getCertificate());
    assertTrue(keyTest.entryInstanceOf("alias2", KeyStore.PrivateKeyEntry.class));
    try {
        keyTest.setEntry(null, null, null);
        fail();
    } catch (NullPointerException expected) {
    }
}
Also used : KeyStoreException(java.security.KeyStoreException) KeyStore(java.security.KeyStore) DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) BigInteger(java.math.BigInteger) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 53 with DSAPrivateKey

use of java.security.interfaces.DSAPrivateKey in project robovm by robovm.

the class KeyStore2Test method test_getEntry.

/**
     * java.security.KeyStore#getEntry(String,
     *        KeyStore.ProtectionParameter)
     */
public void test_getEntry() throws Exception {
    String type = "DSA";
    KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
    try {
        keyTest.getEntry("anAlias", new KeyStore.PasswordProtection(new char[] {}));
        fail();
    } catch (KeyStoreException expected) {
    }
    keyTest.load(null, pssWord);
    try {
        keyTest.getEntry(null, new KeyStore.PasswordProtection(new char[] {}));
        fail();
    } catch (NullPointerException expected) {
    }
    keyTest.getEntry("anAlias", null);
    try {
        keyTest.getEntry(null, null);
        fail();
    } catch (NullPointerException expected) {
    }
    assertNull(keyTest.getEntry("alias", null));
    Certificate[] chain = { new MyCertificate(type, testEncoding), new MyCertificate(type, testEncoding) };
    DSAPrivateKey privateKey1 = (DSAPrivateKey) KeyFactory.getInstance(type).generatePrivate(new DSAPrivateKeySpec(new BigInteger("1"), new BigInteger("2"), new BigInteger("3"), new BigInteger("4")));
    KeyStore.PasswordProtection pp = new KeyStore.PasswordProtection(pssWord);
    assertNull(keyTest.getEntry("alias", pp));
    KeyStore.PrivateKeyEntry pke1 = new KeyStore.PrivateKeyEntry(getPrivateKey(), chain);
    KeyStore.PrivateKeyEntry pke2 = new KeyStore.PrivateKeyEntry(privateKey1, chain);
    keyTest.setEntry("alias1", pke1, pp);
    keyTest.setEntry("alias2", pke2, pp);
    assertNull(keyTest.getEntry("alias", pp));
    KeyStore.PrivateKeyEntry pkeActual1 = (KeyStore.PrivateKeyEntry) keyTest.getEntry("alias1", pp);
    KeyStore.PrivateKeyEntry pkeActual2 = (KeyStore.PrivateKeyEntry) keyTest.getEntry("alias2", pp);
    assertTrue(Arrays.equals(chain, pkeActual1.getCertificateChain()));
    assertEquals(getPrivateKey(), pkeActual1.getPrivateKey());
    assertEquals(new MyCertificate(type, testEncoding), pkeActual1.getCertificate());
    assertTrue(keyTest.entryInstanceOf("alias1", KeyStore.PrivateKeyEntry.class));
    assertTrue(Arrays.equals(chain, pkeActual2.getCertificateChain()));
    DSAPrivateKey entryPrivateKey = (DSAPrivateKey) pkeActual2.getPrivateKey();
    assertEquals(privateKey1.getX(), entryPrivateKey.getX());
    assertEquals(privateKey1.getParams().getG(), entryPrivateKey.getParams().getG());
    assertEquals(privateKey1.getParams().getP(), entryPrivateKey.getParams().getP());
    assertEquals(privateKey1.getParams().getQ(), entryPrivateKey.getParams().getQ());
    assertEquals(new MyCertificate(type, testEncoding), pkeActual2.getCertificate());
    assertTrue(keyTest.entryInstanceOf("alias2", KeyStore.PrivateKeyEntry.class));
}
Also used : KeyStoreException(java.security.KeyStoreException) KeyStore(java.security.KeyStore) DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) BigInteger(java.math.BigInteger) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 54 with DSAPrivateKey

use of java.security.interfaces.DSAPrivateKey in project robovm by robovm.

the class DSAPrivateKeyTest method test_getX.

/**
     * java.security.interfaces.DSAPrivateKey
     * #getX()
     */
public void test_getX() throws Exception {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
    keyGen.initialize(new DSAParameterSpec(Util.P, Util.Q, Util.G), new SecureRandom(new MySecureRandomSpi(), null) {
    });
    KeyPair keyPair = keyGen.generateKeyPair();
    DSAPrivateKey key = (DSAPrivateKey) keyPair.getPrivate();
    assertNotNull("Invalid X value", key.getX());
}
Also used : DSAParameterSpec(java.security.spec.DSAParameterSpec) KeyPair(java.security.KeyPair) SecureRandom(java.security.SecureRandom) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) KeyPairGenerator(java.security.KeyPairGenerator)

Example 55 with DSAPrivateKey

use of java.security.interfaces.DSAPrivateKey in project robovm by robovm.

the class OpenSSLDSAKeyFactory method engineGetKeySpec.

@Override
protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec) throws InvalidKeySpecException {
    if (key == null) {
        throw new InvalidKeySpecException("key == null");
    }
    if (keySpec == null) {
        throw new InvalidKeySpecException("keySpec == null");
    }
    if (!"DSA".equals(key.getAlgorithm())) {
        throw new InvalidKeySpecException("Key must be a DSA key");
    }
    if (key instanceof DSAPublicKey && DSAPublicKeySpec.class.isAssignableFrom(keySpec)) {
        DSAPublicKey dsaKey = (DSAPublicKey) key;
        DSAParams params = dsaKey.getParams();
        return (T) new DSAPublicKeySpec(dsaKey.getY(), params.getP(), params.getQ(), params.getG());
    } else if (key instanceof PublicKey && DSAPublicKeySpec.class.isAssignableFrom(keySpec)) {
        final byte[] encoded = key.getEncoded();
        if (!"X.509".equals(key.getFormat()) || encoded == null) {
            throw new InvalidKeySpecException("Not a valid X.509 encoding");
        }
        DSAPublicKey dsaKey = (DSAPublicKey) engineGeneratePublic(new X509EncodedKeySpec(encoded));
        DSAParams params = dsaKey.getParams();
        return (T) new DSAPublicKeySpec(dsaKey.getY(), params.getP(), params.getQ(), params.getG());
    } else if (key instanceof DSAPrivateKey && DSAPrivateKeySpec.class.isAssignableFrom(keySpec)) {
        DSAPrivateKey dsaKey = (DSAPrivateKey) key;
        DSAParams params = dsaKey.getParams();
        return (T) new DSAPrivateKeySpec(dsaKey.getX(), params.getP(), params.getQ(), params.getG());
    } else if (key instanceof PrivateKey && DSAPrivateKeySpec.class.isAssignableFrom(keySpec)) {
        final byte[] encoded = key.getEncoded();
        if (!"PKCS#8".equals(key.getFormat()) || encoded == null) {
            throw new InvalidKeySpecException("Not a valid PKCS#8 encoding");
        }
        DSAPrivateKey dsaKey = (DSAPrivateKey) engineGeneratePrivate(new PKCS8EncodedKeySpec(encoded));
        DSAParams params = dsaKey.getParams();
        return (T) new DSAPrivateKeySpec(dsaKey.getX(), params.getP(), params.getQ(), params.getG());
    } else if (key instanceof PrivateKey && PKCS8EncodedKeySpec.class.isAssignableFrom(keySpec)) {
        final byte[] encoded = key.getEncoded();
        if (!"PKCS#8".equals(key.getFormat())) {
            throw new InvalidKeySpecException("Encoding type must be PKCS#8; was " + key.getFormat());
        } else if (encoded == null) {
            throw new InvalidKeySpecException("Key is not encodable");
        }
        return (T) new PKCS8EncodedKeySpec(encoded);
    } else if (key instanceof PublicKey && X509EncodedKeySpec.class.isAssignableFrom(keySpec)) {
        final byte[] encoded = key.getEncoded();
        if (!"X.509".equals(key.getFormat())) {
            throw new InvalidKeySpecException("Encoding type must be X.509; was " + key.getFormat());
        } else if (encoded == null) {
            throw new InvalidKeySpecException("Key is not encodable");
        }
        return (T) new X509EncodedKeySpec(encoded);
    } else {
        throw new InvalidKeySpecException("Unsupported key type and key spec combination; key=" + key.getClass().getName() + ", keySpec=" + keySpec.getName());
    }
}
Also used : DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) DSAPublicKey(java.security.interfaces.DSAPublicKey) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) DSAParams(java.security.interfaces.DSAParams) DSAPublicKey(java.security.interfaces.DSAPublicKey) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Aggregations

DSAPrivateKey (java.security.interfaces.DSAPrivateKey)86 BigInteger (java.math.BigInteger)35 DSAPublicKey (java.security.interfaces.DSAPublicKey)31 DSAParams (java.security.interfaces.DSAParams)26 RSAPrivateCrtKey (java.security.interfaces.RSAPrivateCrtKey)25 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)25 ECPrivateKey (java.security.interfaces.ECPrivateKey)23 IOException (java.io.IOException)18 KeyPair (java.security.KeyPair)18 DSAPrivateKeySpec (java.security.spec.DSAPrivateKeySpec)15 PrivateKey (java.security.PrivateKey)14 DSAPublicKeySpec (java.security.spec.DSAPublicKeySpec)14 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)13 KeyFactory (java.security.KeyFactory)12 RSAPublicKey (java.security.interfaces.RSAPublicKey)12 PublicKey (java.security.PublicKey)11 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)10 InvalidKeyException (java.security.InvalidKeyException)9 KeyPairGenerator (java.security.KeyPairGenerator)9 ECPublicKey (java.security.interfaces.ECPublicKey)9