Search in sources :

Example 21 with DSAPublicKey

use of java.security.interfaces.DSAPublicKey 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 22 with DSAPublicKey

use of java.security.interfaces.DSAPublicKey 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 23 with DSAPublicKey

use of java.security.interfaces.DSAPublicKey in project Conversations by siacs.

the class Account method getOtrFingerprint.

public String getOtrFingerprint() {
    if (this.otrFingerprint == null) {
        try {
            if (this.mOtrService == null) {
                return null;
            }
            final PublicKey publicKey = this.mOtrService.getPublicKey();
            if (publicKey == null || !(publicKey instanceof DSAPublicKey)) {
                return null;
            }
            this.otrFingerprint = new OtrCryptoEngineImpl().getFingerprint(publicKey).toLowerCase(Locale.US);
            return this.otrFingerprint;
        } catch (final OtrCryptoException ignored) {
            return null;
        }
    } else {
        return this.otrFingerprint;
    }
}
Also used : OtrCryptoException(net.java.otr4j.crypto.OtrCryptoException) PublicKey(java.security.PublicKey) DSAPublicKey(java.security.interfaces.DSAPublicKey) OtrCryptoEngineImpl(net.java.otr4j.crypto.OtrCryptoEngineImpl) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Example 24 with DSAPublicKey

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

the class SHA1withDSA_SignatureImpl method engineInitVerify.

/**
     * Initializes this signature object with PublicKey object
     * passed as argument to the method.
     *
     * @params
     *    publicKey DSAPublicKey object
     * @throws
     *    InvalidKeyException if publicKey is not DSAPublicKey object
     */
protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException {
    // parameters and public key
    BigInteger p, q, y;
    int n1;
    if (publicKey == null || !(publicKey instanceof DSAPublicKey)) {
        throw new InvalidKeyException("publicKey is not an instance of DSAPublicKey");
    }
    DSAParams params = ((DSAPublicKey) publicKey).getParams();
    p = params.getP();
    q = params.getQ();
    y = ((DSAPublicKey) publicKey).getY();
    // checks described in DSA standard
    n1 = p.bitLength();
    if (p.compareTo(BigInteger.valueOf(1)) != 1 || n1 < 512 || n1 > 1024 || (n1 & 077) != 0) {
        throw new InvalidKeyException("bad p");
    }
    if (q.signum() != 1 || q.bitLength() != 160) {
        throw new InvalidKeyException("bad q");
    }
    if (y.signum() != 1) {
        throw new InvalidKeyException("y <= 0");
    }
    dsaKey = (DSAKey) publicKey;
    msgDigest.reset();
}
Also used : BigInteger(java.math.BigInteger) DSAParams(java.security.interfaces.DSAParams) InvalidKeyException(java.security.InvalidKeyException) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Example 25 with DSAPublicKey

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

the class OpenSSLSignature method engineInitVerify.

@Override
protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException {
    if (publicKey instanceof DSAPublicKey) {
        try {
            DSAPublicKey dsaPublicKey = (DSAPublicKey) publicKey;
            DSAParams dsaParams = dsaPublicKey.getParams();
            dsa = NativeCrypto.EVP_PKEY_new_DSA(dsaParams.getP().toByteArray(), dsaParams.getQ().toByteArray(), dsaParams.getG().toByteArray(), dsaPublicKey.getY().toByteArray(), null);
        } catch (Exception e) {
            throw new InvalidKeyException(e);
        }
    } else if (publicKey instanceof RSAPublicKey) {
        try {
            RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey;
            rsa = NativeCrypto.EVP_PKEY_new_RSA(rsaPublicKey.getModulus().toByteArray(), rsaPublicKey.getPublicExponent().toByteArray(), null, null, null);
        } catch (Exception e) {
            throw new InvalidKeyException(e);
        }
    } else {
        throw new InvalidKeyException("Need DSA or RSA public key");
    }
    try {
        ctx = NativeCrypto.EVP_VerifyInit(evpAlgorithm);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) DSAParams(java.security.interfaces.DSAParams) InvalidKeyException(java.security.InvalidKeyException) InvalidParameterException(java.security.InvalidParameterException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Aggregations

DSAPublicKey (java.security.interfaces.DSAPublicKey)31 DSAParams (java.security.interfaces.DSAParams)19 DSAPrivateKey (java.security.interfaces.DSAPrivateKey)11 PublicKey (java.security.PublicKey)10 DSAPublicKeySpec (java.security.spec.DSAPublicKeySpec)10 BigInteger (java.math.BigInteger)9 RSAPublicKey (java.security.interfaces.RSAPublicKey)8 InvalidKeyException (java.security.InvalidKeyException)7 DSAPrivateKeySpec (java.security.spec.DSAPrivateKeySpec)6 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)6 IOException (java.io.IOException)5 GeneralSecurityException (java.security.GeneralSecurityException)5 KeyPairGenerator (java.security.KeyPairGenerator)5 X509Certificate (java.security.cert.X509Certificate)5 KeyFactory (java.security.KeyFactory)4 KeyPair (java.security.KeyPair)4 CertPathValidatorException (java.security.cert.CertPathValidatorException)4 DSAParameterSpec (java.security.spec.DSAParameterSpec)4 CertificateException (java.security.cert.CertificateException)3 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)3