use of java.security.PrivateKey in project robovm by robovm.
the class OpenSSLRSAKeyPairGenerator method generateKeyPair.
@Override
public KeyPair generateKeyPair() {
final OpenSSLKey key = new OpenSSLKey(NativeCrypto.RSA_generate_key_ex(modulusBits, publicExponent));
PrivateKey privKey = OpenSSLRSAPrivateKey.getInstance(key);
PublicKey pubKey = new OpenSSLRSAPublicKey(key);
return new KeyPair(pubKey, privKey);
}
use of java.security.PrivateKey in project robovm by robovm.
the class SignatureSpiTest method testEngineGetParameters.
public void testEngineGetParameters() {
// or rather test that no UnsupportedOperationException is thrown?
@SuppressWarnings("unused") Signature s = new Signature("dummy") {
protected AlgorithmParameters engineGetParameters() {
engineGetParametersCalled = true;
try {
super.engineGetParameters();
} catch (UnsupportedOperationException e) {
engineGetParametersExceptionOcurred = true;
}
return null;
}
@Override
protected Object engineGetParameter(String param) throws InvalidParameterException {
return null;
}
@Override
protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException {
}
@Override
protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException {
}
@Override
protected void engineSetParameter(String param, Object value) throws InvalidParameterException {
}
@Override
protected byte[] engineSign() throws SignatureException {
return null;
}
@Override
protected void engineUpdate(byte b) throws SignatureException {
}
@Override
protected void engineUpdate(byte[] b, int off, int len) throws SignatureException {
}
@Override
protected boolean engineVerify(byte[] sigBytes) throws SignatureException {
return false;
}
};
// must call engineGetParameters
s.getParameters();
assertTrue(engineGetParametersCalled);
assertTrue(engineGetParametersExceptionOcurred);
}
use of java.security.PrivateKey in project robovm by robovm.
the class SignatureSpiTest method testEngineSign_BII.
public void testEngineSign_BII() {
MySignature signature = new MySignature("dummy");
try {
signature.initSign(new PrivateKey() {
public String getFormat() {
return null;
}
public byte[] getEncoded() {
return null;
}
public String getAlgorithm() {
return null;
}
});
} catch (InvalidKeyException e) {
fail("unexpected exception: " + e);
}
byte[] buf = new byte[10];
try {
signature.sign(buf, 2, 1);
assertTrue("SPI method not called", signature.wasMethodCalled("engineSign_[BII"));
} catch (SignatureException e) {
fail("unexpected exception: " + e);
}
}
use of java.security.PrivateKey in project robovm by robovm.
the class X509KeyManagerTest method init.
void init(String name) {
keyType = name;
try {
CertificateFactory cf = CertificateFactory.getInstance("X.509");
KeyFactory kf = KeyFactory.getInstance("RSA");
keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
keyTest.load(null, "1234".toCharArray());
if (keyType.equals(CLIENT)) {
keys = new PrivateKey[3];
keys[0] = kf.generatePrivate(new PKCS8EncodedKeySpec(keyBytes));
keys[1] = kf.generatePrivate(new PKCS8EncodedKeySpec(key2Bytes));
keys[2] = kf.generatePrivate(new PKCS8EncodedKeySpec(key3Bytes));
cert = new X509Certificate[3];
cert[0] = (X509Certificate) cf.generateCertificate(certArray);
cert[1] = (X509Certificate) cf.generateCertificate(certArray2);
cert[2] = (X509Certificate) cf.generateCertificate(certArray3);
keyTest.setKeyEntry("clientKey_01", keys[0], PASSWORD, new X509Certificate[] { cert[0] });
keyTest.setKeyEntry("clientKey_02", keys[1], PASSWORD, new X509Certificate[] { cert[0], cert[1] });
keyTest.setKeyEntry("clientKey_03", keys[2], PASSWORD, new X509Certificate[] { cert[0], cert[2] });
keyTest.setCertificateEntry("clientAlias_01", cert[0]);
keyTest.setCertificateEntry("clientAlias_02", cert[0]);
keyTest.setCertificateEntry("clientAlias_03", cert[1]);
} else if (keyType.equals(SERVER)) {
keys = new PrivateKey[1];
keys[0] = kf.generatePrivate(new PKCS8EncodedKeySpec(keyBytes));
cert = new X509Certificate[1];
cert[0] = (X509Certificate) cf.generateCertificate(certArray3);
keyTest.setKeyEntry("serverKey_00", keys[0], PASSWORD, new X509Certificate[] { cert[0] });
keyTest.setCertificateEntry("serverAlias_00", cert[0]);
}
} catch (Exception ex) {
ex.printStackTrace();
throw new IllegalArgumentException(ex.getMessage());
}
try {
factory.init(keyTest, "1234".toCharArray());
} catch (Exception e) {
fail("Could't init the KeyManagerFactory");
}
manager = (X509KeyManager) factory.getKeyManagers()[0];
}
use of java.security.PrivateKey in project robovm by robovm.
the class SignatureTest method testSign_SHA384withRSA_Key_Success.
public void testSign_SHA384withRSA_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("SHA384withRSA");
sig.initSign(privKey);
sig.update(Vector2Data);
byte[] signature = sig.sign();
assertNotNull("Signature must not be null", signature);
assertTrue("Signature should match expected", Arrays.equals(signature, SHA384withRSA_Vector2Signature));
RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(RSA_2048_modulus, RSA_2048_publicExponent);
PublicKey pubKey = kf.generatePublic(pubKeySpec);
sig.initVerify(pubKey);
sig.update(Vector2Data);
assertTrue("Signature must verify correctly", sig.verify(signature));
}
Aggregations