Search in sources :

Example 31 with DSAParams

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

the class DSAKeyPairGeneratorTest method test_DSAKeyPairGenerator01.

/**
     * java.security.interfaces.DSAKeyPairGenerator
     * #initialize(DSAParams params, SecureRandom random)
     */
public void test_DSAKeyPairGenerator01() {
    DSAParams dsaParams = new DSAParameterSpec(p, q, g);
    SecureRandom random = null;
    MyDSA dsa = new MyDSA(dsaParams);
    try {
        random = SecureRandom.getInstance("SHA1PRNG");
    } catch (Exception e) {
        fail("Unexpected exception for SecureRandom: " + e);
    }
    try {
        dsa.initialize(dsaParams, random);
    } catch (Exception e) {
        fail("Unexpected exception: " + e);
    }
    try {
        dsa.initialize(dsaParams, null);
        fail("InvalidParameterException was not thrown");
    } catch (InvalidParameterException ipe) {
    //expected
    } catch (Exception e) {
        fail(e + " was thrown instead of InvalidParameterException");
    }
    try {
        dsa.initialize(null, random);
        fail("InvalidParameterException was not thrown");
    } catch (InvalidParameterException ipe) {
    //expected
    } catch (Exception e) {
        fail(e + " was thrown instead of InvalidParameterException");
    }
}
Also used : DSAParameterSpec(java.security.spec.DSAParameterSpec) InvalidParameterException(java.security.InvalidParameterException) SecureRandom(java.security.SecureRandom) DSAParams(java.security.interfaces.DSAParams) InvalidParameterException(java.security.InvalidParameterException)

Example 32 with DSAParams

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

the class DSAKeyPairGeneratorTest method test_DSAKeyPairGenerator02.

/**
     * java.security.interfaces.DSAKeyPairGenerator
     * #initialize(int modlen, boolean genParams, SecureRandom randomm)
     */
public void test_DSAKeyPairGenerator02() {
    int[] invalidLen = { -1, 0, 511, 513, 650, 1023, 1025 };
    DSAParams dsaParams = new DSAParameterSpec(p, q, g);
    SecureRandom random = null;
    MyDSA dsa = new MyDSA(null);
    try {
        random = SecureRandom.getInstance("SHA1PRNG");
    } catch (Exception e) {
        fail("Unexpected exception for SecureRandom: " + e);
    }
    //exception case
    try {
        dsa.initialize(520, false, random);
        fail("InvalidParameterException was not thrown");
    } catch (InvalidParameterException ipe) {
        String str = ipe.getMessage();
        if (!str.equals("there are not precomputed parameters")) {
            fail("Incorrect exception's message: " + str);
        }
    } catch (Exception e) {
        fail(e + " was thrown instead of InvalidParameterException");
    }
    //exception case
    for (int i = 0; i < invalidLen.length; i++) {
        try {
            dsa.initialize(invalidLen[i], true, random);
            fail("InvalidParameterException was not thrown");
        } catch (InvalidParameterException ipe) {
            String str = ipe.getMessage();
            if (!str.equals("Incorrect modlen")) {
                fail("Incorrect exception's message: " + str);
            }
        } catch (Exception e) {
            fail(e + " was thrown instead of InvalidParameterException");
        }
    }
    //positive case
    dsa = new MyDSA(dsaParams);
    try {
        dsa.initialize(520, true, random);
    } catch (Exception e) {
        fail(e + " was thrown for subcase 1");
    }
    //positive case
    try {
        dsa.initialize(520, false, random);
    } catch (Exception e) {
        fail(e + " was thrown for subcase 1");
    }
}
Also used : DSAParameterSpec(java.security.spec.DSAParameterSpec) InvalidParameterException(java.security.InvalidParameterException) SecureRandom(java.security.SecureRandom) DSAParams(java.security.interfaces.DSAParams) InvalidParameterException(java.security.InvalidParameterException)

Example 33 with DSAParams

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

the class DSAKeyTest method test_getParams.

/**
     * java.security.interfaces.DSAKey
     * #getParams()
     * test covers following use cases
     *   Case 1: check private key
     *   Case 2: check public key
     */
public void test_getParams() throws Exception {
    DSAParams param = new DSAParameterSpec(Util.P, Util.Q, Util.G);
    KeyPairGenerator gen = KeyPairGenerator.getInstance("DSA");
    gen.initialize((DSAParameterSpec) param);
    DSAKey key = null;
    // Case 1: check private key
    key = (DSAKey) gen.generateKeyPair().getPrivate();
    assertDSAParamsEquals(param, key.getParams());
    // Case 2: check public key
    key = (DSAKey) gen.generateKeyPair().getPublic();
    assertDSAParamsEquals(param, key.getParams());
}
Also used : DSAParameterSpec(java.security.spec.DSAParameterSpec) DSAKey(java.security.interfaces.DSAKey) DSAParams(java.security.interfaces.DSAParams) KeyPairGenerator(java.security.KeyPairGenerator)

Example 34 with DSAParams

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

the class DSAParamsTest method test_getG.

/**
     * java.security.interfaces.DSAParams
     * #getG()
     */
public void test_getG() {
    DSAParams params = new DSAParameterSpec(p, q, g);
    assertEquals("Invalid G", g, params.getG());
}
Also used : DSAParameterSpec(java.security.spec.DSAParameterSpec) DSAParams(java.security.interfaces.DSAParams)

Example 35 with DSAParams

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

the class CertPathValidatorUtilities method getNextWorkingKey.

/**
     * Return the next working key inheriting DSA parameters if necessary.
     * <p>
     * This methods inherits DSA parameters from the indexed certificate or
     * previous certificates in the certificate chain to the returned
     * <code>PublicKey</code>. The list is searched upwards, meaning the end
     * certificate is at position 0 and previous certificates are following.
     * </p>
     * <p>
     * If the indexed certificate does not contain a DSA key this method simply
     * returns the public key. If the DSA key already contains DSA parameters
     * the key is also only returned.
     * </p>
     *
     * @param certs The certification path.
     * @param index The index of the certificate which contains the public key
     *              which should be extended with DSA parameters.
     * @return The public key of the certificate in list position
     *         <code>index</code> extended with DSA parameters if applicable.
     * @throws AnnotatedException if DSA parameters cannot be inherited.
     */
protected static PublicKey getNextWorkingKey(List certs, int index) throws CertPathValidatorException {
    Certificate cert = (Certificate) certs.get(index);
    PublicKey pubKey = cert.getPublicKey();
    if (!(pubKey instanceof DSAPublicKey)) {
        return pubKey;
    }
    DSAPublicKey dsaPubKey = (DSAPublicKey) pubKey;
    if (dsaPubKey.getParams() != null) {
        return dsaPubKey;
    }
    for (int i = index + 1; i < certs.size(); i++) {
        X509Certificate parentCert = (X509Certificate) certs.get(i);
        pubKey = parentCert.getPublicKey();
        if (!(pubKey instanceof DSAPublicKey)) {
            throw new CertPathValidatorException("DSA parameters cannot be inherited from previous certificate.");
        }
        DSAPublicKey prevDSAPubKey = (DSAPublicKey) pubKey;
        if (prevDSAPubKey.getParams() == null) {
            continue;
        }
        DSAParams dsaParams = prevDSAPubKey.getParams();
        DSAPublicKeySpec dsaPubKeySpec = new DSAPublicKeySpec(dsaPubKey.getY(), dsaParams.getP(), dsaParams.getQ(), dsaParams.getG());
        try {
            KeyFactory keyFactory = KeyFactory.getInstance("DSA", BouncyCastleProvider.PROVIDER_NAME);
            return keyFactory.generatePublic(dsaPubKeySpec);
        } catch (Exception exception) {
            throw new RuntimeException(exception.getMessage());
        }
    }
    throw new CertPathValidatorException("DSA parameters cannot be inherited from previous certificate.");
}
Also used : CertPathValidatorException(java.security.cert.CertPathValidatorException) ExtCertPathValidatorException(org.bouncycastle.jce.exception.ExtCertPathValidatorException) PublicKey(java.security.PublicKey) DSAPublicKey(java.security.interfaces.DSAPublicKey) DSAParams(java.security.interfaces.DSAParams) CRLDistPoint(org.bouncycastle.asn1.x509.CRLDistPoint) DistributionPoint(org.bouncycastle.asn1.x509.DistributionPoint) X509Certificate(java.security.cert.X509Certificate) KeyFactory(java.security.KeyFactory) GeneralSecurityException(java.security.GeneralSecurityException) CertPathValidatorException(java.security.cert.CertPathValidatorException) ParseException(java.text.ParseException) ExtCertPathValidatorException(org.bouncycastle.jce.exception.ExtCertPathValidatorException) CertStoreException(java.security.cert.CertStoreException) CRLException(java.security.cert.CRLException) CertificateParsingException(java.security.cert.CertificateParsingException) StoreException(org.bouncycastle.util.StoreException) IOException(java.io.IOException) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate) X509AttributeCertificate(org.bouncycastle.x509.X509AttributeCertificate) DSAPublicKey(java.security.interfaces.DSAPublicKey) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Aggregations

DSAParams (java.security.interfaces.DSAParams)40 DSAPublicKey (java.security.interfaces.DSAPublicKey)19 BigInteger (java.math.BigInteger)16 DSAPrivateKey (java.security.interfaces.DSAPrivateKey)13 DSAParameterSpec (java.security.spec.DSAParameterSpec)11 DSAPublicKeySpec (java.security.spec.DSAPublicKeySpec)11 InvalidKeyException (java.security.InvalidKeyException)8 PublicKey (java.security.PublicKey)7 DSAPrivateKeySpec (java.security.spec.DSAPrivateKeySpec)7 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)7 KeyPairGenerator (java.security.KeyPairGenerator)6 SecureRandom (java.security.SecureRandom)5 X509Certificate (java.security.cert.X509Certificate)5 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)5 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)5 GeneralSecurityException (java.security.GeneralSecurityException)4 KeyFactory (java.security.KeyFactory)4 KeyPair (java.security.KeyPair)4 CertPathValidatorException (java.security.cert.CertPathValidatorException)4 IOException (java.io.IOException)3