Search in sources :

Example 11 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 12 with DSAPublicKeySpec

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

the class DSAKeyValue method getPublicKey.

/** @inheritDoc */
public PublicKey getPublicKey() throws XMLSecurityException {
    try {
        DSAPublicKeySpec pkspec = new DSAPublicKeySpec(this.getBigIntegerFromChildElement(Constants._TAG_Y, Constants.SignatureSpecNS), this.getBigIntegerFromChildElement(Constants._TAG_P, Constants.SignatureSpecNS), this.getBigIntegerFromChildElement(Constants._TAG_Q, Constants.SignatureSpecNS), this.getBigIntegerFromChildElement(Constants._TAG_G, Constants.SignatureSpecNS));
        KeyFactory dsaFactory = KeyFactory.getInstance("DSA");
        PublicKey pk = dsaFactory.generatePublic(pkspec);
        return pk;
    } catch (NoSuchAlgorithmException ex) {
        throw new XMLSecurityException("empty", ex);
    } catch (InvalidKeySpecException ex) {
        throw new XMLSecurityException("empty", ex);
    }
}
Also used : PublicKey(java.security.PublicKey) DSAPublicKey(java.security.interfaces.DSAPublicKey) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory) XMLSecurityException(com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Example 13 with DSAPublicKeySpec

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

the class DSAKeyFactory method engineGetKeySpec.

/**
     * Returns a specification (key material) of the given key object
     * in the requested format.
     *
     * @param key the key
     *
     * @param keySpec the requested format in which the key material shall be
     * returned
     *
     * @return the underlying key specification (key material) in the
     * requested format
     *
     * @exception InvalidKeySpecException if the requested key specification is
     * inappropriate for the given key, or the given key cannot be processed
     * (e.g., the given key has an unrecognized algorithm or format).
     */
protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec) throws InvalidKeySpecException {
    DSAParams params;
    try {
        if (key instanceof java.security.interfaces.DSAPublicKey) {
            // Determine valid key specs
            Class<?> dsaPubKeySpec = Class.forName("java.security.spec.DSAPublicKeySpec");
            Class<?> x509KeySpec = Class.forName("java.security.spec.X509EncodedKeySpec");
            if (dsaPubKeySpec.isAssignableFrom(keySpec)) {
                java.security.interfaces.DSAPublicKey dsaPubKey = (java.security.interfaces.DSAPublicKey) key;
                params = dsaPubKey.getParams();
                return keySpec.cast(new DSAPublicKeySpec(dsaPubKey.getY(), params.getP(), params.getQ(), params.getG()));
            } else if (x509KeySpec.isAssignableFrom(keySpec)) {
                return keySpec.cast(new X509EncodedKeySpec(key.getEncoded()));
            } else {
                throw new InvalidKeySpecException("Inappropriate key specification");
            }
        } else if (key instanceof java.security.interfaces.DSAPrivateKey) {
            // Determine valid key specs
            Class<?> dsaPrivKeySpec = Class.forName("java.security.spec.DSAPrivateKeySpec");
            Class<?> pkcs8KeySpec = Class.forName("java.security.spec.PKCS8EncodedKeySpec");
            if (dsaPrivKeySpec.isAssignableFrom(keySpec)) {
                java.security.interfaces.DSAPrivateKey dsaPrivKey = (java.security.interfaces.DSAPrivateKey) key;
                params = dsaPrivKey.getParams();
                return keySpec.cast(new DSAPrivateKeySpec(dsaPrivKey.getX(), params.getP(), params.getQ(), params.getG()));
            } else if (pkcs8KeySpec.isAssignableFrom(keySpec)) {
                return keySpec.cast(new PKCS8EncodedKeySpec(key.getEncoded()));
            } else {
                throw new InvalidKeySpecException("Inappropriate key specification");
            }
        } else {
            throw new InvalidKeySpecException("Inappropriate key type");
        }
    } catch (ClassNotFoundException e) {
        throw new InvalidKeySpecException("Unsupported key specification: " + e.getMessage());
    }
}
Also used : X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) DSAParams(java.security.interfaces.DSAParams) DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Example 14 with DSAPublicKeySpec

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

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

DSAPublicKeySpec (java.security.spec.DSAPublicKeySpec)24 BigInteger (java.math.BigInteger)12 KeyFactory (java.security.KeyFactory)12 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)12 PublicKey (java.security.PublicKey)11 DSAParams (java.security.interfaces.DSAParams)11 DSAPublicKey (java.security.interfaces.DSAPublicKey)11 DSAPrivateKeySpec (java.security.spec.DSAPrivateKeySpec)11 PrivateKey (java.security.PrivateKey)6 DSAPrivateKey (java.security.interfaces.DSAPrivateKey)6 InvalidKeyException (java.security.InvalidKeyException)5 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)5 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)5 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)5 GeneralSecurityException (java.security.GeneralSecurityException)4 CertPathValidatorException (java.security.cert.CertPathValidatorException)4 KeySpec (java.security.spec.KeySpec)4 X509Certificate (java.security.cert.X509Certificate)3 IOException (java.io.IOException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2