Search in sources :

Example 56 with InvalidKeyException

use of java.security.InvalidKeyException in project robovm by robovm.

the class PKCS10CertificationRequest method verify.

/**
     * verify the request using the passed in public key and the provider..
     */
public boolean verify(PublicKey pubKey, String provider) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException, SignatureException {
    Signature sig;
    try {
        if (provider == null) {
            sig = Signature.getInstance(getSignatureName(sigAlgId));
        } else {
            sig = Signature.getInstance(getSignatureName(sigAlgId), provider);
        }
    } catch (NoSuchAlgorithmException e) {
        //
        if (oids.get(sigAlgId.getObjectId()) != null) {
            String signatureAlgorithm = (String) oids.get(sigAlgId.getObjectId());
            if (provider == null) {
                sig = Signature.getInstance(signatureAlgorithm);
            } else {
                sig = Signature.getInstance(signatureAlgorithm, provider);
            }
        } else {
            throw e;
        }
    }
    setSignatureParameters(sig, sigAlgId.getParameters());
    sig.initVerify(pubKey);
    try {
        sig.update(reqInfo.getEncoded(ASN1Encoding.DER));
    } catch (Exception e) {
        throw new SignatureException("exception encoding TBS cert request - " + e);
    }
    return sig.verify(sigBits.getBytes());
}
Also used : Signature(java.security.Signature) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) DERBitString(org.bouncycastle.asn1.DERBitString) SignatureException(java.security.SignatureException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) GeneralSecurityException(java.security.GeneralSecurityException) SignatureException(java.security.SignatureException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) NoSuchProviderException(java.security.NoSuchProviderException)

Example 57 with InvalidKeyException

use of java.security.InvalidKeyException in project robovm by robovm.

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 58 with InvalidKeyException

use of java.security.InvalidKeyException in project robovm by robovm.

the class OpenSSLSignature method engineInitVerify.

@Override
protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException {
    // If we had an existing context, destroy it first.
    destroyContextIfExists();
    if (publicKey instanceof OpenSSLKeyHolder) {
        OpenSSLKey pkey = ((OpenSSLKeyHolder) publicKey).getOpenSSLKey();
        checkEngineType(pkey);
        key = pkey;
    } else if (publicKey instanceof RSAPublicKey) {
        if (engineType != EngineType.RSA) {
            throw new InvalidKeyException("Signature not initialized as RSA");
        }
        RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey;
        key = OpenSSLRSAPublicKey.getInstance(rsaPublicKey);
    } else if (publicKey instanceof DSAPublicKey) {
        if (engineType != EngineType.DSA) {
            throw new InvalidKeyException("Signature not initialized as DSA");
        }
        DSAPublicKey dsaPublicKey = (DSAPublicKey) publicKey;
        key = OpenSSLDSAPublicKey.getInstance(dsaPublicKey);
    } else if (publicKey instanceof ECPublicKey) {
        if (engineType != EngineType.EC) {
            throw new InvalidKeyException("Signature not initialized as EC");
        }
        ECPublicKey ecPublicKey = (ECPublicKey) publicKey;
        key = OpenSSLECPublicKey.getInstance(ecPublicKey);
    } else {
        throw new InvalidKeyException("Need DSA or RSA or EC public key");
    }
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) ECPublicKey(java.security.interfaces.ECPublicKey) InvalidKeyException(java.security.InvalidKeyException) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Example 59 with InvalidKeyException

use of java.security.InvalidKeyException in project robovm by robovm.

the class OpenSSLSignature method engineInitSign.

@Override
protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException {
    destroyContextIfExists();
    if (privateKey instanceof OpenSSLKeyHolder) {
        OpenSSLKey pkey = ((OpenSSLKeyHolder) privateKey).getOpenSSLKey();
        checkEngineType(pkey);
        key = pkey;
    } else if (privateKey instanceof RSAPrivateCrtKey) {
        if (engineType != EngineType.RSA) {
            throw new InvalidKeyException("Signature not initialized as RSA");
        }
        RSAPrivateCrtKey rsaPrivateKey = (RSAPrivateCrtKey) privateKey;
        key = OpenSSLRSAPrivateCrtKey.getInstance(rsaPrivateKey);
    } else if (privateKey instanceof RSAPrivateKey) {
        if (engineType != EngineType.RSA) {
            throw new InvalidKeyException("Signature not initialized as RSA");
        }
        RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) privateKey;
        key = OpenSSLRSAPrivateKey.getInstance(rsaPrivateKey);
    } else if (privateKey instanceof DSAPrivateKey) {
        if (engineType != EngineType.DSA) {
            throw new InvalidKeyException("Signature not initialized as DSA");
        }
        DSAPrivateKey dsaPrivateKey = (DSAPrivateKey) privateKey;
        key = OpenSSLDSAPrivateKey.getInstance(dsaPrivateKey);
    } else if (privateKey instanceof ECPrivateKey) {
        if (engineType != EngineType.EC) {
            throw new InvalidKeyException("Signature not initialized as EC");
        }
        ECPrivateKey ecPrivateKey = (ECPrivateKey) privateKey;
        key = OpenSSLECPrivateKey.getInstance(ecPrivateKey);
    } else {
        throw new InvalidKeyException("Need DSA or RSA or EC private key");
    }
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) InvalidKeyException(java.security.InvalidKeyException) RSAPrivateKey(java.security.interfaces.RSAPrivateKey)

Example 60 with InvalidKeyException

use of java.security.InvalidKeyException in project robovm by robovm.

the class OpenSSLSignatureRawRSA method engineInitSign.

@Override
protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException {
    if (privateKey instanceof OpenSSLRSAPrivateKey) {
        OpenSSLRSAPrivateKey rsaPrivateKey = (OpenSSLRSAPrivateKey) privateKey;
        key = rsaPrivateKey.getOpenSSLKey();
    } else if (privateKey instanceof RSAPrivateCrtKey) {
        RSAPrivateCrtKey rsaPrivateKey = (RSAPrivateCrtKey) privateKey;
        key = OpenSSLRSAPrivateCrtKey.getInstance(rsaPrivateKey);
    } else if (privateKey instanceof RSAPrivateKey) {
        RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) privateKey;
        key = OpenSSLRSAPrivateKey.getInstance(rsaPrivateKey);
    } else {
        throw new InvalidKeyException("Need RSA private key");
    }
    // Allocate buffer according to RSA modulus size.
    int maxSize = NativeCrypto.RSA_size(key.getPkeyContext());
    inputBuffer = new byte[maxSize];
    inputOffset = 0;
}
Also used : RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) InvalidKeyException(java.security.InvalidKeyException) RSAPrivateKey(java.security.interfaces.RSAPrivateKey)

Aggregations

InvalidKeyException (java.security.InvalidKeyException)499 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)263 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)124 SignatureException (java.security.SignatureException)95 IOException (java.io.IOException)94 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)93 BadPaddingException (javax.crypto.BadPaddingException)89 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)87 Cipher (javax.crypto.Cipher)77 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)63 SecretKeySpec (javax.crypto.spec.SecretKeySpec)63 Signature (java.security.Signature)58 SecretKey (javax.crypto.SecretKey)50 PublicKey (java.security.PublicKey)49 PrivateKey (java.security.PrivateKey)47 CertificateException (java.security.cert.CertificateException)46 Mac (javax.crypto.Mac)44 IvParameterSpec (javax.crypto.spec.IvParameterSpec)41 NoSuchProviderException (java.security.NoSuchProviderException)39 KeyStoreException (java.security.KeyStoreException)33