Search in sources :

Example 1 with DSAPublicKeySpec

use of java.security.spec.DSAPublicKeySpec in project XobotOS by xamarin.

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) 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)

Example 2 with DSAPublicKeySpec

use of java.security.spec.DSAPublicKeySpec in project jdk8u_jdk by JetBrains.

the class GenerationTests method getPublicKey.

private static PublicKey getPublicKey(String algo, int keysize) throws Exception {
    KeyFactory kf = KeyFactory.getInstance(algo);
    KeySpec kspec;
    if (algo.equalsIgnoreCase("DSA")) {
        if (keysize == 1024) {
            kspec = new DSAPublicKeySpec(new BigInteger(DSA_Y), new BigInteger(DSA_P), new BigInteger(DSA_Q), new BigInteger(DSA_G));
        } else if (keysize == 2048) {
            kspec = new DSAPublicKeySpec(new BigInteger(DSA_2048_Y), new BigInteger(DSA_2048_P), new BigInteger(DSA_2048_Q), new BigInteger(DSA_2048_G));
        } else
            throw new RuntimeException("Unsupported keysize:" + keysize);
    } else if (algo.equalsIgnoreCase("RSA")) {
        if (keysize == 512) {
            kspec = new RSAPublicKeySpec(new BigInteger(RSA_MOD), new BigInteger(RSA_PUB));
        } else if (keysize == 1024) {
            kspec = new RSAPublicKeySpec(new BigInteger(RSA_1024_MOD), new BigInteger(RSA_PUB));
        } else
            throw new RuntimeException("Unsupported keysize:" + keysize);
    } else
        throw new RuntimeException("Unsupported key algorithm " + algo);
    return kf.generatePublic(kspec);
}
Also used : KeySpec(java.security.spec.KeySpec) DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) BigInteger(java.math.BigInteger) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) KeyFactory(java.security.KeyFactory) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Example 3 with DSAPublicKeySpec

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

the class KeyFactoryTest method testGeneratePublic.

@SuppressWarnings("unchecked")
public void testGeneratePublic() {
    KeyFactory factory = null;
    try {
        factory = KeyFactory.getInstance(TEST_KEYFACTORY_NAME);
    } catch (NoSuchAlgorithmException e) {
        fail("unexpected exception: " + e);
    }
    assertNotNull(factory);
    try {
        TestPublicKey key = new TestPublicKey();
        TestPublicKeySpec keySpec = new TestPublicKeySpec(key);
        PublicKey publicKey = factory.generatePublic(keySpec);
        assertNotNull(publicKey);
        assertTrue(Arrays.equals(key.encoded, publicKey.getEncoded()));
    } catch (InvalidKeySpecException e) {
        fail("unexpected exception: " + e);
    }
    KeySpec[] keySpecs = { new TestPrivateKeySpec(new TestPrivateKey()), null, new DSAPublicKeySpec(null, null, null, null) };
    Class[] exceptions = { InvalidKeySpecException.class, NullPointerException.class, InvalidKeySpecException.class };
    for (int i = 0; i < keySpecs.length; i++) {
        KeySpec keySpec = keySpecs[i];
        String message = "generatePublic(" + (keySpec == null ? "null" : keySpec.toString()) + ")";
        try {
            PublicKey generatePublic = factory.generatePublic(keySpec);
            assertNotNull(generatePublic);
        } catch (Exception e) {
            checkException(message, e, exceptions[i]);
        } finally {
            checkException(message, null, exceptions[i]);
        }
    }
}
Also used : PublicKey(java.security.PublicKey) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) KeySpec(java.security.spec.KeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvocationTargetException(java.lang.reflect.InvocationTargetException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) NoSuchProviderException(java.security.NoSuchProviderException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Example 4 with DSAPublicKeySpec

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

the class SignatureTest method testVerify_SHA1withDSA_Key_Success.

public void testVerify_SHA1withDSA_Key_Success() throws Exception {
    KeyFactory kf = KeyFactory.getInstance("DSA");
    DSAPublicKeySpec pubKeySpec = new DSAPublicKeySpec(DSA_pub, DSA_P, DSA_Q, DSA_G);
    PublicKey pubKey = kf.generatePublic(pubKeySpec);
    Signature sig = Signature.getInstance("SHA1withDSA");
    sig.initVerify(pubKey);
    sig.update(Vector2Data);
    assertTrue("Signature must verify correctly", sig.verify(SHA1withDSA_Vector2Signature));
}
Also used : PublicKey(java.security.PublicKey) Signature(java.security.Signature) KeyFactory(java.security.KeyFactory) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Example 5 with DSAPublicKeySpec

use of java.security.spec.DSAPublicKeySpec 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)

Aggregations

DSAPublicKeySpec (java.security.spec.DSAPublicKeySpec)57 BigInteger (java.math.BigInteger)39 KeyFactory (java.security.KeyFactory)31 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)22 DSAPublicKey (java.security.interfaces.DSAPublicKey)19 DSAPrivateKeySpec (java.security.spec.DSAPrivateKeySpec)19 PublicKey (java.security.PublicKey)18 DSAParams (java.security.interfaces.DSAParams)17 KeySpec (java.security.spec.KeySpec)14 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)12 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)10 PrivateKey (java.security.PrivateKey)10 GeneralSecurityException (java.security.GeneralSecurityException)9 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)9 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)9 DSAPrivateKey (java.security.interfaces.DSAPrivateKey)8 CertPathValidatorException (java.security.cert.CertPathValidatorException)7 IOException (java.io.IOException)6 InvalidKeyException (java.security.InvalidKeyException)5 KeyPair (java.security.KeyPair)5