Search in sources :

Example 41 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 42 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 43 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)

Example 44 with DSAParams

use of java.security.interfaces.DSAParams 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)

Example 45 with DSAParams

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

the class OpenSSLDSAKeyFactory method engineTranslateKey.

@Override
protected Key engineTranslateKey(Key key) throws InvalidKeyException {
    if (key == null) {
        throw new InvalidKeyException("key == null");
    }
    if ((key instanceof OpenSSLDSAPublicKey) || (key instanceof OpenSSLDSAPrivateKey)) {
        return key;
    } else if (key instanceof DSAPublicKey) {
        DSAPublicKey dsaKey = (DSAPublicKey) key;
        BigInteger y = dsaKey.getY();
        DSAParams params = dsaKey.getParams();
        BigInteger p = params.getP();
        BigInteger q = params.getQ();
        BigInteger g = params.getG();
        try {
            return engineGeneratePublic(new DSAPublicKeySpec(y, p, q, g));
        } catch (InvalidKeySpecException e) {
            throw new InvalidKeyException(e);
        }
    } else if (key instanceof DSAPrivateKey) {
        DSAPrivateKey dsaKey = (DSAPrivateKey) key;
        BigInteger x = dsaKey.getX();
        DSAParams params = dsaKey.getParams();
        BigInteger p = params.getP();
        BigInteger q = params.getQ();
        BigInteger g = params.getG();
        try {
            return engineGeneratePrivate(new DSAPrivateKeySpec(x, p, q, g));
        } catch (InvalidKeySpecException e) {
            throw new InvalidKeyException(e);
        }
    } else if ((key instanceof PrivateKey) && ("PKCS#8".equals(key.getFormat()))) {
        byte[] encoded = key.getEncoded();
        if (encoded == null) {
            throw new InvalidKeyException("Key does not support encoding");
        }
        try {
            return engineGeneratePrivate(new PKCS8EncodedKeySpec(encoded));
        } catch (InvalidKeySpecException e) {
            throw new InvalidKeyException(e);
        }
    } else if ((key instanceof PublicKey) && ("X.509".equals(key.getFormat()))) {
        byte[] encoded = key.getEncoded();
        if (encoded == null) {
            throw new InvalidKeyException("Key does not support encoding");
        }
        try {
            return engineGeneratePublic(new X509EncodedKeySpec(encoded));
        } catch (InvalidKeySpecException e) {
            throw new InvalidKeyException(e);
        }
    } else {
        throw new InvalidKeyException("Key must be DSA public or private key; was " + key.getClass().getName());
    }
}
Also used : DSAPrivateKey(java.security.interfaces.DSAPrivateKey) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) DSAPublicKey(java.security.interfaces.DSAPublicKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) DSAParams(java.security.interfaces.DSAParams) InvalidKeyException(java.security.InvalidKeyException) DSAPublicKey(java.security.interfaces.DSAPublicKey) DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) BigInteger(java.math.BigInteger) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Aggregations

DSAParams (java.security.interfaces.DSAParams)66 DSAPublicKey (java.security.interfaces.DSAPublicKey)30 BigInteger (java.math.BigInteger)29 DSAPrivateKey (java.security.interfaces.DSAPrivateKey)18 DSAPublicKeySpec (java.security.spec.DSAPublicKeySpec)17 DSAParameterSpec (java.security.spec.DSAParameterSpec)16 PublicKey (java.security.PublicKey)10 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)9 InvalidKeyException (java.security.InvalidKeyException)8 KeyFactory (java.security.KeyFactory)8 DSAPrivateKeySpec (java.security.spec.DSAPrivateKeySpec)8 IOException (java.io.IOException)7 GeneralSecurityException (java.security.GeneralSecurityException)7 CertPathValidatorException (java.security.cert.CertPathValidatorException)7 RSAPrivateCrtKey (java.security.interfaces.RSAPrivateCrtKey)7 RSAPublicKey (java.security.interfaces.RSAPublicKey)7 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)7 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)7 AlgorithmParameters (java.security.AlgorithmParameters)6 X509Certificate (java.security.cert.X509Certificate)6