use of java.security.PrivateKey in project robovm by robovm.
the class SignatureTest method testSign_SHA1withRSA_Key_EmptyKey_Failure.
public void testSign_SHA1withRSA_Key_EmptyKey_Failure() throws Exception {
KeyFactory kf = KeyFactory.getInstance("RSA");
RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(null, null);
// Failing on this key early is okay.
final PrivateKey privKey;
try {
privKey = kf.generatePrivate(keySpec);
} catch (NullPointerException e) {
return;
} catch (InvalidKeySpecException e) {
return;
}
Signature sig = Signature.getInstance("SHA1withRSA");
try {
sig.initSign(privKey);
fail("Should throw error when key is empty");
} catch (InvalidKeyException expected) {
}
}
use of java.security.PrivateKey in project robovm by robovm.
the class SignatureTest method testSign_SHA1withDSA_Key_Success.
public void testSign_SHA1withDSA_Key_Success() throws Exception {
KeyFactory kf = KeyFactory.getInstance("DSA");
DSAPrivateKeySpec keySpec = new DSAPrivateKeySpec(DSA_priv, DSA_P, DSA_Q, DSA_G);
PrivateKey privKey = kf.generatePrivate(keySpec);
Signature sig = Signature.getInstance("SHA1withDSA");
sig.initSign(privKey);
sig.update(Vector2Data);
byte[] signature = sig.sign();
assertNotNull("Signature must not be null", signature);
DSAPublicKeySpec pubKeySpec = new DSAPublicKeySpec(DSA_pub, DSA_P, DSA_Q, DSA_G);
PublicKey pubKey = kf.generatePublic(pubKeySpec);
sig.initVerify(pubKey);
sig.update(Vector2Data);
assertTrue("Signature must verify correctly", sig.verify(signature));
}
use of java.security.PrivateKey in project robovm by robovm.
the class SignatureTest method testSign_NONEwithRSA_Key_Success.
public void testSign_NONEwithRSA_Key_Success() throws Exception {
KeyFactory kf = KeyFactory.getInstance("RSA");
RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(RSA_2048_modulus, RSA_2048_privateExponent);
PrivateKey privKey = kf.generatePrivate(keySpec);
Signature sig = Signature.getInstance("NONEwithRSA");
sig.initSign(privKey);
sig.update(Vector1Data);
byte[] signature = sig.sign();
assertNotNull("Signature must not be null", signature);
assertTrue("Signature should match expected", Arrays.equals(signature, NONEwithRSA_Vector1Signature));
RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(RSA_2048_modulus, RSA_2048_publicExponent);
PublicKey pubKey = kf.generatePublic(pubKeySpec);
sig.initVerify(pubKey);
sig.update(Vector1Data);
assertTrue("Signature must verify correctly", sig.verify(signature));
}
use of java.security.PrivateKey in project robovm by robovm.
the class KeyManagerFactoryTest method test_X509KeyManager_alias.
private void test_X509KeyManager_alias(X509KeyManager km, String alias, String keyType, boolean many, boolean empty) throws Exception {
if (empty || (!many && (keyType == null || keyType.isEmpty()))) {
assertNull(keyType, alias);
assertNull(keyType, km.getCertificateChain(alias));
assertNull(keyType, km.getPrivateKey(alias));
return;
}
assertNotNull(keyType, alias);
X509Certificate[] certificateChain = km.getCertificateChain(alias);
PrivateKey privateKey = km.getPrivateKey(alias);
String keyAlgName;
String sigAlgName;
if (keyType == null) {
keyAlgName = privateKey.getAlgorithm();
sigAlgName = keyAlgName;
} else {
// potentially handle EC_EC or EC_RSA
keyAlgName = TestKeyStore.keyAlgorithm(keyType);
sigAlgName = TestKeyStore.signatureAlgorithm(keyType);
X509Certificate certificate = certificateChain[0];
assertEquals(keyType, keyAlgName, certificate.getPublicKey().getAlgorithm());
assertEquals(keyType, keyAlgName, privateKey.getAlgorithm());
// skip this for EC which could return EC_RSA case instead of EC_EC
if (!keyType.equals("EC")) {
String expectedSigAlgName = sigAlgName.toUpperCase();
String actualSigAlgName = certificate.getSigAlgName().toUpperCase();
String expected = actualSigAlgName + " contains " + expectedSigAlgName;
assertTrue(expected, actualSigAlgName.contains(expectedSigAlgName));
}
}
PrivateKeyEntry privateKeyEntry = getTestKeyStore().getPrivateKey(keyAlgName, sigAlgName);
if (!"EC".equals(keyAlgName)) {
assertEquals(keyType, Arrays.<Certificate>asList(privateKeyEntry.getCertificateChain()), Arrays.<Certificate>asList(certificateChain));
assertEquals(keyType, privateKeyEntry.getPrivateKey(), privateKey);
}
}
use of java.security.PrivateKey in project robovm by robovm.
the class CipherTest method testRSA_ECB_NoPadding_Private_OnlyDoFinalWithOffset_Success.
private void testRSA_ECB_NoPadding_Private_OnlyDoFinalWithOffset_Success(String provider) throws Exception {
KeyFactory kf = KeyFactory.getInstance("RSA");
RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(RSA_2048_modulus, RSA_2048_privateExponent);
final PrivateKey privKey = kf.generatePrivate(keySpec);
Cipher c = Cipher.getInstance("RSA/ECB/NoPadding", provider);
/*
* You're actually decrypting with private keys, but there is no
* distinction made here. It's all keyed off of what kind of key you're
* using. ENCRYPT_MODE and DECRYPT_MODE are the same.
*/
c.init(Cipher.ENCRYPT_MODE, privKey);
byte[] encrypted = new byte[RSA_Vector1_Encrypt_Private.length];
final int encryptLen = c.doFinal(RSA_2048_Vector1, 0, RSA_2048_Vector1.length, encrypted, 0);
assertEquals("Encrypted size should match expected", RSA_Vector1_Encrypt_Private.length, encryptLen);
assertTrue("Encrypted should match expected", Arrays.equals(RSA_Vector1_Encrypt_Private, encrypted));
c.init(Cipher.DECRYPT_MODE, privKey);
final int decryptLen = c.doFinal(RSA_2048_Vector1, 0, RSA_2048_Vector1.length, encrypted, 0);
assertEquals("Encrypted size should match expected", RSA_Vector1_Encrypt_Private.length, decryptLen);
assertTrue("Encrypted should match expected", Arrays.equals(RSA_Vector1_Encrypt_Private, encrypted));
}
Aggregations