Search in sources :

Example 46 with PrivateKey

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);
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey)

Example 47 with PrivateKey

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);
}
Also used : PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) Signature(java.security.Signature)

Example 48 with PrivateKey

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);
    }
}
Also used : PrivateKey(java.security.PrivateKey) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException)

Example 49 with PrivateKey

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];
}
Also used : PrivateKey(java.security.PrivateKey) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) CertificateFactory(java.security.cert.CertificateFactory) KeyFactory(java.security.KeyFactory) X509Certificate(java.security.cert.X509Certificate) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 50 with PrivateKey

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));
}
Also used : PrivateKey(java.security.PrivateKey) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) PublicKey(java.security.PublicKey) Signature(java.security.Signature) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) KeyFactory(java.security.KeyFactory)

Aggregations

PrivateKey (java.security.PrivateKey)517 X509Certificate (java.security.cert.X509Certificate)217 KeyFactory (java.security.KeyFactory)169 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)144 Certificate (java.security.cert.Certificate)127 PublicKey (java.security.PublicKey)120 ByteArrayInputStream (java.io.ByteArrayInputStream)118 KeyStore (java.security.KeyStore)93 CertificateFactory (java.security.cert.CertificateFactory)92 IOException (java.io.IOException)81 Key (java.security.Key)74 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)73 PrivateKeyEntry (java.security.KeyStore.PrivateKeyEntry)70 Entry (java.security.KeyStore.Entry)60 TrustedCertificateEntry (java.security.KeyStore.TrustedCertificateEntry)60 KeyPair (java.security.KeyPair)59 SecretKey (javax.crypto.SecretKey)48 InvalidKeyException (java.security.InvalidKeyException)47 KeyStoreException (java.security.KeyStoreException)46 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)46