use of java.security.InvalidKeyException in project platform_frameworks_base by android.
the class AndroidKeyStoreSignatureSpiBase method engineSign.
@Override
protected final byte[] engineSign() throws SignatureException {
if (mCachedException != null) {
throw new SignatureException(mCachedException);
}
byte[] signature;
try {
ensureKeystoreOperationInitialized();
byte[] additionalEntropy = KeyStoreCryptoOperationUtils.getRandomBytesToMixIntoKeystoreRng(appRandom, getAdditionalEntropyAmountForSign());
signature = mMessageStreamer.doFinal(EmptyArray.BYTE, 0, 0, // no signature provided -- it'll be generated by this invocation
null, additionalEntropy);
} catch (InvalidKeyException | KeyStoreException e) {
throw new SignatureException(e);
}
resetWhilePreservingInitState();
return signature;
}
use of java.security.InvalidKeyException in project wycheproof by google.
the class EcdhTest method testDistinctCurves.
@SuppressWarnings("InsecureCryptoUsage")
public void testDistinctCurves(String algorithm, ECPrivateKey priv, ECPublicKey pub) throws Exception {
KeyAgreement kaA;
try {
kaA = KeyAgreement.getInstance(algorithm);
} catch (NoSuchAlgorithmException ex) {
System.out.println("Algorithm not supported: " + algorithm);
return;
}
byte[] shared;
try {
kaA.init(priv);
kaA.doPhase(pub, true);
shared = kaA.generateSecret();
} catch (InvalidKeyException ex) {
// This is expected.
return;
}
// Printing some information to determine what might have gone wrong:
// E.g., if the generated secret is the same as the x-coordinate of the public key
// then it is likely that the ECDH computation was using a fake group with small order.
// Such a situation is probably exploitable.
// This probably is exploitable. If the curve of the private key was used for the ECDH
// then the generated secret and the x-coordinate of the public key are likely
// distinct.
EllipticCurve pubCurve = pub.getParams().getCurve();
EllipticCurve privCurve = priv.getParams().getCurve();
ECPoint pubW = pub.getW();
System.out.println("testDistinctCurves: algorithm=" + algorithm);
System.out.println("Private key: a=" + privCurve.getA() + " b=" + privCurve.getB() + " p" + EcUtil.getModulus(privCurve));
System.out.println(" s =" + priv.getS());
System.out.println("Public key: a=" + pubCurve.getA() + " b=" + pubCurve.getB() + " p" + EcUtil.getModulus(pubCurve));
System.out.println(" w = (" + pubW.getAffineX() + ", " + pubW.getAffineY() + ")");
System.out.println(" = (" + pubW.getAffineX().toString(16) + ", " + pubW.getAffineY().toString(16) + ")");
System.out.println("generated shared secret:" + TestUtil.bytesToHex(shared));
fail("Generated secret with distinct Curves using " + algorithm);
}
use of java.security.InvalidKeyException in project j2objc by google.
the class X509Key method decode.
/**
* Initialize an X509Key object from an input stream. The data on that
* input stream must be encoded using DER, obeying the X.509
* <code>SubjectPublicKeyInfo</code> format. That is, the data is a
* sequence consisting of an algorithm ID and a bit string which holds
* the key. (That bit string is often used to encapsulate another DER
* encoded sequence.)
*
* <P>Subclasses should not normally redefine this method; they should
* instead provide a <code>parseKeyBits</code> method to parse any
* fields inside the <code>key</code> member.
*
* <P>The exception to this rule is that since private keys need not
* be encoded using the X.509 <code>SubjectPublicKeyInfo</code> format,
* private keys may override this method, <code>encode</code>, and
* of course <code>getFormat</code>.
*
* @param in an input stream with a DER-encoded X.509
* SubjectPublicKeyInfo value
* @exception InvalidKeyException on parsing errors.
*/
public void decode(InputStream in) throws InvalidKeyException {
DerValue val;
try {
val = new DerValue(in);
if (val.tag != DerValue.tag_Sequence)
throw new InvalidKeyException("invalid key format");
algid = AlgorithmId.parse(val.data.getDerValue());
setKey(val.data.getUnalignedBitString());
parseKeyBits();
if (val.data.available() != 0)
throw new InvalidKeyException("excess key data");
} catch (IOException e) {
// e.printStackTrace ();
throw new InvalidKeyException("IOException: " + e.getMessage());
}
}
use of java.security.InvalidKeyException in project j2objc by google.
the class X509Key method getEncodedInternal.
public byte[] getEncodedInternal() throws InvalidKeyException {
byte[] encoded = encodedKey;
if (encoded == null) {
try {
DerOutputStream out = new DerOutputStream();
encode(out);
encoded = out.toByteArray();
} catch (IOException e) {
throw new InvalidKeyException("IOException : " + e.getMessage());
}
encodedKey = encoded;
}
return encoded;
}
use of java.security.InvalidKeyException in project j2objc by google.
the class X509Key method buildX509Key.
/*
* Factory interface, building the kind of key associated with this
* specific algorithm ID or else returning this generic base class.
* See the description above.
*/
static PublicKey buildX509Key(AlgorithmId algid, BitArray key) throws IOException, InvalidKeyException {
/*
* Use the algid and key parameters to produce the ASN.1 encoding
* of the key, which will then be used as the input to the
* key factory.
*/
DerOutputStream x509EncodedKeyStream = new DerOutputStream();
encode(x509EncodedKeyStream, algid, key);
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(x509EncodedKeyStream.toByteArray());
try {
// Instantiate the key factory of the appropriate algorithm
KeyFactory keyFac = KeyFactory.getInstance(algid.getName());
// Generate the public key
return keyFac.generatePublic(x509KeySpec);
} catch (NoSuchAlgorithmException e) {
// Return generic X509Key with opaque key data (see below)
} catch (InvalidKeySpecException e) {
throw new InvalidKeyException(e.getMessage(), e);
}
/*
* Try again using JDK1.1-style for backwards compatibility.
*/
String classname = "";
try {
Properties props;
String keytype;
Provider sunProvider;
sunProvider = Security.getProvider("SUN");
if (sunProvider == null)
throw new InstantiationException();
classname = sunProvider.getProperty("PublicKey.X.509." + algid.getName());
if (classname == null) {
throw new InstantiationException();
}
Class keyClass = null;
try {
keyClass = Class.forName(classname);
} catch (ClassNotFoundException e) {
ClassLoader cl = ClassLoader.getSystemClassLoader();
if (cl != null) {
keyClass = cl.loadClass(classname);
}
}
Object inst = null;
X509Key result;
if (keyClass != null)
inst = keyClass.newInstance();
if (inst instanceof X509Key) {
result = (X509Key) inst;
result.algid = algid;
result.setKey(key);
result.parseKeyBits();
return result;
}
} catch (ClassNotFoundException e) {
} catch (InstantiationException e) {
} catch (IllegalAccessException e) {
// this should not happen.
throw new IOException(classname + " [internal error]");
}
X509Key result = new X509Key(algid, key);
return result;
}
Aggregations