Search in sources :

Example 1 with DSAPrivateKeySpec

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

the class RandomPrivateKeyX509ExtendedKeyManager method getPrivateKey.

@Override
public PrivateKey getPrivateKey(String alias) {
    PrivateKey originalPrivateKey = super.getPrivateKey(alias);
    if (originalPrivateKey == null) {
        return null;
    }
    PrivateKey result;
    String keyAlgorithm = originalPrivateKey.getAlgorithm();
    try {
        KeyFactory keyFactory = KeyFactory.getInstance(keyAlgorithm);
        if ("RSA".equals(keyAlgorithm)) {
            RSAPrivateKeySpec originalKeySpec = keyFactory.getKeySpec(originalPrivateKey, RSAPrivateKeySpec.class);
            int keyLengthBits = originalKeySpec.getModulus().bitLength();
            // Use a cache because RSA key generation is slow.
            String cacheKey = keyAlgorithm + "-" + keyLengthBits;
            result = cachedKeys.get(cacheKey);
            if (result == null) {
                KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(keyAlgorithm);
                keyPairGenerator.initialize(keyLengthBits);
                result = keyPairGenerator.generateKeyPair().getPrivate();
                cachedKeys.put(cacheKey, result);
            }
        } else if ("DSA".equals(keyAlgorithm)) {
            DSAPrivateKeySpec originalKeySpec = keyFactory.getKeySpec(originalPrivateKey, DSAPrivateKeySpec.class);
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(keyAlgorithm);
            keyPairGenerator.initialize(new DSAParameterSpec(originalKeySpec.getP(), originalKeySpec.getQ(), originalKeySpec.getG()));
            result = keyPairGenerator.generateKeyPair().getPrivate();
        } else {
            Assert.fail("Unsupported key algorithm: " + originalPrivateKey.getAlgorithm());
            result = null;
        }
    } catch (GeneralSecurityException e) {
        Assert.fail("Failed to generate private key: " + e);
        result = null;
    }
    return result;
}
Also used : DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) DSAParameterSpec(java.security.spec.DSAParameterSpec) PrivateKey(java.security.PrivateKey) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) GeneralSecurityException(java.security.GeneralSecurityException) KeyPairGenerator(java.security.KeyPairGenerator) KeyFactory(java.security.KeyFactory)

Example 2 with DSAPrivateKeySpec

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

the class DSAPrivateKeySpecTest method testGetX.

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

Example 3 with DSAPrivateKeySpec

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

the class DSAPrivateKeySpecTest method testGetG.

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

Example 4 with DSAPrivateKeySpec

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

the class SignatureTest method testSign_SHA1withDSA_Key_Success.

public void testSign_SHA1withDSA_Key_Success() throws Exception {
    KeyFactory kf = KeyFactory.getInstance("DSA");
    DSAPrivateKeySpec keySpec = new DSAPrivateKeySpec(DSA_priv, DSA_P, DSA_Q, DSA_G);
    PrivateKey privKey = kf.generatePrivate(keySpec);
    Signature sig = Signature.getInstance("SHA1withDSA");
    sig.initSign(privKey);
    sig.update(Vector2Data);
    byte[] signature = sig.sign();
    assertNotNull("Signature must not be null", signature);
    DSAPublicKeySpec pubKeySpec = new DSAPublicKeySpec(DSA_pub, DSA_P, DSA_Q, DSA_G);
    PublicKey pubKey = kf.generatePublic(pubKeySpec);
    sig.initVerify(pubKey);
    sig.update(Vector2Data);
    assertTrue("Signature must verify correctly", sig.verify(signature));
}
Also used : DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) Signature(java.security.Signature) KeyFactory(java.security.KeyFactory) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Example 5 with DSAPrivateKeySpec

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

the class KeyStore2Test method test_getCreationDate.

/**
     * java.security.KeyStore#getCreationDate(String)
     */
public void test_getCreationDate() throws Exception {
    String type = "DSA";
    KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
    try {
        keyTest.getCreationDate("anAlias");
        fail();
    } catch (KeyStoreException expected) {
    }
    keyTest.load(null, pssWord);
    assertNull(keyTest.getCreationDate(""));
    try {
        keyTest.getCreationDate(null);
        fail();
    } catch (NullPointerException expected) {
    }
    Certificate[] chain = { new MyCertificate(type, testEncoding), new MyCertificate(type, testEncoding) };
    PrivateKey privateKey1 = KeyFactory.getInstance(type).generatePrivate(new DSAPrivateKeySpec(new BigInteger("0"), new BigInteger("0"), new BigInteger("0"), new BigInteger("0")));
    KeyStore.PasswordProtection pp = new KeyStore.PasswordProtection(pssWord);
    KeyStore.PrivateKeyEntry pke = new KeyStore.PrivateKeyEntry(getPrivateKey(), chain);
    KeyStore.PrivateKeyEntry pke1 = new KeyStore.PrivateKeyEntry(privateKey1, chain);
    keyTest.setEntry("alias1", pke, pp);
    keyTest.setEntry("alias2", pke1, pp);
    Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
    int dayExpected = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
    int monthExpected = Calendar.getInstance().get(Calendar.MONTH);
    int yearExpected = Calendar.getInstance().get(Calendar.YEAR);
    int hourExpected = Calendar.getInstance().get(Calendar.HOUR);
    int minuteExpected = Calendar.getInstance().get(Calendar.MINUTE);
    Calendar.getInstance().setTimeInMillis(keyTest.getCreationDate("alias1").getTime());
    int dayActual1 = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
    int monthActual1 = Calendar.getInstance().get(Calendar.MONTH);
    int yearActual1 = Calendar.getInstance().get(Calendar.YEAR);
    int hourActual1 = Calendar.getInstance().get(Calendar.HOUR);
    int minuteActual1 = Calendar.getInstance().get(Calendar.MINUTE);
    assertEquals(dayExpected, dayActual1);
    assertEquals(monthExpected, monthActual1);
    assertEquals(yearExpected, yearActual1);
    assertEquals(hourExpected, hourActual1);
    assertEquals(minuteExpected, minuteActual1);
    Calendar.getInstance().setTimeInMillis(keyTest.getCreationDate("alias2").getTime());
    int dayActual2 = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
    int monthActual2 = Calendar.getInstance().get(Calendar.MONTH);
    int yearActual2 = Calendar.getInstance().get(Calendar.YEAR);
    int hourActual2 = Calendar.getInstance().get(Calendar.HOUR);
    int minuteActual2 = Calendar.getInstance().get(Calendar.MINUTE);
    assertEquals(dayExpected, dayActual2);
    assertEquals(monthExpected, monthActual2);
    assertEquals(yearExpected, yearActual2);
    assertEquals(hourExpected, hourActual2);
    assertEquals(minuteExpected, minuteActual2);
    try {
        keyTest.getCreationDate(null);
        fail();
    } catch (NullPointerException expected) {
    }
}
Also used : DSAPrivateKey(java.security.interfaces.DSAPrivateKey) PrivateKey(java.security.PrivateKey) KeyStoreException(java.security.KeyStoreException) KeyStore(java.security.KeyStore) DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) BigInteger(java.math.BigInteger) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

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