Search in sources :

Example 16 with DSAPrivateKeySpec

use of java.security.spec.DSAPrivateKeySpec 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 17 with DSAPrivateKeySpec

use of java.security.spec.DSAPrivateKeySpec 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 18 with DSAPrivateKeySpec

use of java.security.spec.DSAPrivateKeySpec in project robovm by robovm.

the class DSAPrivateKeySpecTest method testDSAPrivateKeySpec.

/**
     * Test for constructor
     */
public final void testDSAPrivateKeySpec() {
    KeySpec ks = new DSAPrivateKeySpec(new BigInteger("1"), new BigInteger("2"), new BigInteger("3"), new BigInteger("4"));
    assertTrue(ks instanceof DSAPrivateKeySpec);
}
Also used : DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) KeySpec(java.security.spec.KeySpec) DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) BigInteger(java.math.BigInteger)

Example 19 with DSAPrivateKeySpec

use of java.security.spec.DSAPrivateKeySpec in project robovm by robovm.

the class DSAPrivateKeySpecTest method testGetQ.

/**
     * getQ() test
     */
public final void testGetQ() {
    DSAPrivateKeySpec dpks = new DSAPrivateKeySpec(new BigInteger("1"), new BigInteger("2"), new BigInteger("3"), new BigInteger("4"));
    assertEquals(3, dpks.getQ().intValue());
}
Also used : DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) BigInteger(java.math.BigInteger)

Example 20 with DSAPrivateKeySpec

use of java.security.spec.DSAPrivateKeySpec in project robovm by robovm.

the class DSAPrivateKeySpecTest method testGetP.

/**
     * getP() test
     */
public final void testGetP() {
    DSAPrivateKeySpec dpks = new DSAPrivateKeySpec(new BigInteger("1"), new BigInteger("2"), new BigInteger("3"), new BigInteger("4"));
    assertEquals(2, dpks.getP().intValue());
}
Also used : DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) BigInteger(java.math.BigInteger)

Aggregations

DSAPrivateKeySpec (java.security.spec.DSAPrivateKeySpec)21 BigInteger (java.math.BigInteger)14 DSAPublicKeySpec (java.security.spec.DSAPublicKeySpec)11 DSAPrivateKey (java.security.interfaces.DSAPrivateKey)10 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)9 PrivateKey (java.security.PrivateKey)7 DSAParams (java.security.interfaces.DSAParams)7 KeyFactory (java.security.KeyFactory)6 DSAPublicKey (java.security.interfaces.DSAPublicKey)6 PublicKey (java.security.PublicKey)5 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)5 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)5 InvalidKeyException (java.security.InvalidKeyException)3 KeyStore (java.security.KeyStore)3 KeyStoreException (java.security.KeyStoreException)3 Certificate (java.security.cert.Certificate)3 X509Certificate (java.security.cert.X509Certificate)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 Signature (java.security.Signature)2 KeySpec (java.security.spec.KeySpec)2