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());
}
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();
}
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");
}
}
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");
}
}
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;
}
Aggregations