Search in sources :

Example 26 with DSAPrivateKey

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

the class DSAKeyFactoryImpl method engineGetKeySpec.

/**
     * This method returns a specification for the supplied key.
     *
     * The specification will be returned in the form of an object of the type
     * specified by keySpec.
     *
     * @param key -
     *            either DSAPrivateKey or DSAPublicKey
     * @param keySpec -
     *            either DSAPrivateKeySpec.class or DSAPublicKeySpec.class
     *
     * @return either a DSAPrivateKeySpec or a DSAPublicKeySpec
     *
     * @throws InvalidKeySpecException
     *             if "keySpec" is not a specification for DSAPublicKey or
     *             DSAPrivateKey
     */
protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec) throws InvalidKeySpecException {
    BigInteger p, q, g, x, y;
    if (key != null) {
        if (keySpec == null) {
            throw new NullPointerException("keySpec == null");
        }
        if (key instanceof DSAPrivateKey) {
            DSAPrivateKey privateKey = (DSAPrivateKey) key;
            if (keySpec.equals(DSAPrivateKeySpec.class)) {
                x = privateKey.getX();
                DSAParams params = privateKey.getParams();
                p = params.getP();
                q = params.getQ();
                g = params.getG();
                return (T) (new DSAPrivateKeySpec(x, p, q, g));
            }
            if (keySpec.equals(PKCS8EncodedKeySpec.class)) {
                return (T) (new PKCS8EncodedKeySpec(key.getEncoded()));
            }
            throw new InvalidKeySpecException("'keySpec' is neither DSAPrivateKeySpec nor PKCS8EncodedKeySpec");
        }
        if (key instanceof DSAPublicKey) {
            DSAPublicKey publicKey = (DSAPublicKey) key;
            if (keySpec.equals(DSAPublicKeySpec.class)) {
                y = publicKey.getY();
                DSAParams params = publicKey.getParams();
                p = params.getP();
                q = params.getQ();
                g = params.getG();
                return (T) (new DSAPublicKeySpec(y, p, q, g));
            }
            if (keySpec.equals(X509EncodedKeySpec.class)) {
                return (T) (new X509EncodedKeySpec(key.getEncoded()));
            }
            throw new InvalidKeySpecException("'keySpec' is neither DSAPublicKeySpec nor X509EncodedKeySpec");
        }
    }
    throw new InvalidKeySpecException("'key' is neither DSAPublicKey nor DSAPrivateKey");
}
Also used : DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) BigInteger(java.math.BigInteger) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) DSAParams(java.security.interfaces.DSAParams) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) DSAPublicKey(java.security.interfaces.DSAPublicKey) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Example 27 with DSAPrivateKey

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

the class DSAKeyFactoryImpl method engineTranslateKey.

/**
     * The method generates a DSAPublicKey object from the provided key.
     *
     * @param
     *    key - a DSAPublicKey object or DSAPrivateKey object.
     *
     * @return
     *    object of the same type as the "key" argument
     *
     * @throws InvalidKeyException
     *     if "key" is neither DSAPublicKey nor DSAPrivateKey
     */
protected Key engineTranslateKey(Key key) throws InvalidKeyException {
    if (key != null) {
        if (key instanceof DSAPrivateKey) {
            DSAPrivateKey privateKey = (DSAPrivateKey) key;
            DSAParams params = privateKey.getParams();
            try {
                return engineGeneratePrivate(new DSAPrivateKeySpec(privateKey.getX(), params.getP(), params.getQ(), params.getG()));
            } catch (InvalidKeySpecException e) {
                // Actually this exception shouldn't be thrown
                throw new InvalidKeyException("ATTENTION: InvalidKeySpecException: " + e);
            }
        }
        if (key instanceof DSAPublicKey) {
            DSAPublicKey publicKey = (DSAPublicKey) key;
            DSAParams params = publicKey.getParams();
            try {
                return engineGeneratePublic(new DSAPublicKeySpec(publicKey.getY(), params.getP(), params.getQ(), params.getG()));
            } catch (InvalidKeySpecException e) {
                // Actually this exception shouldn't be thrown
                throw new InvalidKeyException("ATTENTION: InvalidKeySpecException: " + e);
            }
        }
    }
    throw new InvalidKeyException("'key' is neither DSAPublicKey nor DSAPrivateKey");
}
Also used : DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) DSAParams(java.security.interfaces.DSAParams) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidKeyException(java.security.InvalidKeyException) DSAPublicKey(java.security.interfaces.DSAPublicKey) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Example 28 with DSAPrivateKey

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

the class SHA1withDSA_SignatureImpl method engineInitSign.

/**
     * Initializes this signature object with PrivateKey object
     * passed as argument to the method.
     *
     * @params
     *    privateKey DSAPrivateKey object
     * @throws
     *    InvalidKeyException if privateKey is not DSAPrivateKey object
     */
protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException {
    DSAParams params;
    // parameters and private key
    BigInteger p, q, x;
    int n;
    if (privateKey == null || !(privateKey instanceof DSAPrivateKey)) {
        throw new InvalidKeyException();
    }
    params = ((DSAPrivateKey) privateKey).getParams();
    p = params.getP();
    q = params.getQ();
    x = ((DSAPrivateKey) privateKey).getX();
    // checks described in DSA standard
    n = p.bitLength();
    if (p.compareTo(BigInteger.valueOf(1)) != 1 || n < 512 || n > 1024 || (n & 077) != 0) {
        throw new InvalidKeyException("bad p");
    }
    if (q.signum() != 1 && q.bitLength() != 160) {
        throw new InvalidKeyException("bad q");
    }
    if (x.signum() != 1 || x.compareTo(q) != -1) {
        throw new InvalidKeyException("x <= 0 || x >= q");
    }
    dsaKey = (DSAKey) privateKey;
    msgDigest.reset();
}
Also used : BigInteger(java.math.BigInteger) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) DSAParams(java.security.interfaces.DSAParams) InvalidKeyException(java.security.InvalidKeyException)

Example 29 with DSAPrivateKey

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

the class EncodedKeySpec2Test method isEqual.

private boolean isEqual(Key key1, Key key2) {
    if (key1 instanceof DSAPublicKey && key2 instanceof DSAPublicKey) {
        DSAPublicKey dsa1 = ((DSAPublicKey) key1);
        DSAPublicKey dsa2 = ((DSAPublicKey) key2);
        return dsa1.getY().equals(dsa2.getY()) && dsa1.getParams().getG().equals(dsa2.getParams().getG()) && dsa1.getParams().getP().equals(dsa2.getParams().getP()) && dsa1.getParams().getQ().equals(dsa2.getParams().getQ());
    } else if (key1 instanceof DSAPrivateKey && key2 instanceof DSAPrivateKey) {
        DSAPrivateKey dsa1 = ((DSAPrivateKey) key1);
        DSAPrivateKey dsa2 = ((DSAPrivateKey) key2);
        return dsa1.getX().equals(dsa2.getX()) && dsa1.getParams().getG().equals(dsa2.getParams().getG()) && dsa1.getParams().getP().equals(dsa2.getParams().getP()) && dsa1.getParams().getQ().equals(dsa2.getParams().getQ());
    } else {
        return false;
    }
}
Also used : DSAPrivateKey(java.security.interfaces.DSAPrivateKey) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Example 30 with DSAPrivateKey

use of java.security.interfaces.DSAPrivateKey in project XobotOS by xamarin.

the class SHA1withDSA_SignatureImpl method engineInitSign.

/**
     * Initializes this signature object with PrivateKey object
     * passed as argument to the method.
     *
     * @params
     *    privateKey DSAPrivateKey object
     * @throws
     *    InvalidKeyException if privateKey is not DSAPrivateKey object
     */
protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException {
    DSAParams params;
    // parameters and private key
    BigInteger p, q, x;
    int n;
    if (privateKey == null || !(privateKey instanceof DSAPrivateKey)) {
        throw new InvalidKeyException();
    }
    params = ((DSAPrivateKey) privateKey).getParams();
    p = params.getP();
    q = params.getQ();
    x = ((DSAPrivateKey) privateKey).getX();
    // checks described in DSA standard
    n = p.bitLength();
    if (p.compareTo(BigInteger.valueOf(1)) != 1 || n < 512 || n > 1024 || (n & 077) != 0) {
        throw new InvalidKeyException("bad p");
    }
    if (q.signum() != 1 && q.bitLength() != 160) {
        throw new InvalidKeyException("bad q");
    }
    if (x.signum() != 1 || x.compareTo(q) != -1) {
        throw new InvalidKeyException("x <= 0 || x >= q");
    }
    dsaKey = (DSAKey) privateKey;
    msgDigest.reset();
}
Also used : BigInteger(java.math.BigInteger) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) DSAParams(java.security.interfaces.DSAParams) InvalidKeyException(java.security.InvalidKeyException)

Aggregations

DSAPrivateKey (java.security.interfaces.DSAPrivateKey)48 BigInteger (java.math.BigInteger)23 DSAParams (java.security.interfaces.DSAParams)18 DSAPublicKey (java.security.interfaces.DSAPublicKey)15 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)14 KeyPair (java.security.KeyPair)12 RSAPrivateCrtKey (java.security.interfaces.RSAPrivateCrtKey)12 DSAPrivateKeySpec (java.security.spec.DSAPrivateKeySpec)11 IOException (java.io.IOException)10 KeyPairGenerator (java.security.KeyPairGenerator)8 DSAPublicKeySpec (java.security.spec.DSAPublicKeySpec)8 InvalidKeyException (java.security.InvalidKeyException)7 ECPrivateKey (java.security.interfaces.ECPrivateKey)7 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)7 Signature (java.security.Signature)6 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)6 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)6 PrivateKey (java.security.PrivateKey)5 SlowTest (com.google.security.wycheproof.WycheproofRunner.SlowTest)4 Test (org.junit.Test)4