Search in sources :

Example 86 with PublicKey

use of java.security.PublicKey in project karaf by apache.

the class ServerKeyVerifierImplTest method testNewKey.

@Test
public void testNewKey() throws NoSuchAlgorithmException, InvalidKeySpecException {
    SocketAddress address = LOCALHOST;
    PublicKey validServerKey = createPubKey();
    KnownHostsManager knowHostsManager = EasyMock.createMock(KnownHostsManager.class);
    EasyMock.expect(knowHostsManager.getKnownKey(address, ALGORITHM)).andReturn(null);
    knowHostsManager.storeKeyForHost(address, validServerKey);
    EasyMock.expectLastCall();
    EasyMock.replay(knowHostsManager);
    ServerKeyVerifierImpl verifier = new ServerKeyVerifierImpl(knowHostsManager, true);
    boolean verified = verifier.verifyServerKey(null, address, validServerKey);
    Assert.assertTrue("Key should be verified as the key is new", verified);
}
Also used : PublicKey(java.security.PublicKey) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 87 with PublicKey

use of java.security.PublicKey in project karaf by apache.

the class ServerKeyVerifierImplTest method testKnownAndIncorrectKey.

@Test
public void testKnownAndIncorrectKey() throws NoSuchAlgorithmException, InvalidKeySpecException {
    SocketAddress address = LOCALHOST;
    PublicKey validServerKey = createPubKey();
    PublicKey otherServerKey = createPubKey();
    KnownHostsManager knowHostsManager = EasyMock.createMock(KnownHostsManager.class);
    EasyMock.expect(knowHostsManager.getKnownKey(address, ALGORITHM)).andReturn(otherServerKey);
    EasyMock.replay(knowHostsManager);
    ServerKeyVerifierImpl verifier = new ServerKeyVerifierImpl(knowHostsManager, true);
    boolean verified = verifier.verifyServerKey(null, address, validServerKey);
    Assert.assertFalse("Key should not be verified as the key is known and does not match the key we verify", verified);
}
Also used : PublicKey(java.security.PublicKey) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 88 with PublicKey

use of java.security.PublicKey in project robovm by robovm.

the class KeyStore2Test method test_getCertificateLjava_lang_String.

/**
     * java.security.KeyStore#getCertificate(java.lang.String)
     */
public void test_getCertificateLjava_lang_String() throws Exception {
    // Test for method java.security.cert.Certificate
    // java.security.KeyStore.getCertificate(java.lang.String)
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    X509Certificate[] cert = new X509Certificate[2];
    cert[0] = (X509Certificate) cf.generateCertificate(certArray);
    cert[1] = (X509Certificate) cf.generateCertificate(certArray2);
    KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
    try {
        keyTest.getCertificate("anAlias");
        fail();
    } catch (KeyStoreException expected) {
    }
    keyTest.load(null, null);
    // alias 1
    PublicKey pub = cert[0].getPublicKey();
    keyTest.setCertificateEntry("alias1", cert[0]);
    Certificate certRes = keyTest.getCertificate("alias1");
    assertEquals("the public key of the certificate from getCertificate() " + "did not equal the original certificate", pub, certRes.getPublicKey());
    // alias 2
    keyTest.setCertificateEntry("alias2", cert[0]);
    // testing for a certificate chain
    Certificate cert2 = keyTest.getCertificate("alias2");
    assertEquals("the certificate for alias2 is supposed to exist", cert2, cert[0]);
}
Also used : PublicKey(java.security.PublicKey) KeyStoreException(java.security.KeyStoreException) CertificateFactory(java.security.cert.CertificateFactory) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 89 with PublicKey

use of java.security.PublicKey in project spring-boot by spring-projects.

the class TokenValidator method hasValidSignature.

private boolean hasValidSignature(Token token, String key) {
    try {
        PublicKey publicKey = getPublicKey(key);
        Signature signature = Signature.getInstance("SHA256withRSA");
        signature.initVerify(publicKey);
        signature.update(token.getContent());
        return signature.verify(token.getSignature());
    } catch (GeneralSecurityException ex) {
        return false;
    }
}
Also used : PublicKey(java.security.PublicKey) Signature(java.security.Signature) GeneralSecurityException(java.security.GeneralSecurityException)

Example 90 with PublicKey

use of java.security.PublicKey in project robovm by robovm.

the class KeyFactory2Test method test_getKeySpecLjava_security_KeyLjava_lang_Class.

public void test_getKeySpecLjava_security_KeyLjava_lang_Class() throws Exception {
    // java.lang.Class)
    for (int i = 0; i < keyfactAlgs.length; i++) {
        KeyFactory fact = KeyFactory.getInstance(keyfactAlgs[i], providerName);
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance(keyfactAlgs[i]);
        // We don't use getInstance
        SecureRandom random = new SecureRandom();
        keyGen.initialize(StandardNames.getMinimumKeySize(keyfactAlgs[i]), random);
        KeepAlive keepalive = createKeepAlive(keyfactAlgs[i]);
        KeyPair keys = keyGen.generateKeyPair();
        if (keepalive != null) {
            keepalive.interrupt();
        }
        KeySpec privateKeySpec = fact.getKeySpec(keys.getPrivate(), StandardNames.getPrivateKeySpecClass(keyfactAlgs[i]));
        KeySpec publicKeySpec = fact.getKeySpec(keys.getPublic(), StandardNames.getPublicKeySpecClass(keyfactAlgs[i]));
        PrivateKey privateKey = fact.generatePrivate(privateKeySpec);
        PublicKey publicKey = fact.generatePublic(publicKeySpec);
        assertEquals("generatePrivate generated different key for algorithm " + keyfactAlgs[i] + " (provider=" + fact.getProvider().getName() + ")", Arrays.toString(keys.getPrivate().getEncoded()), Arrays.toString(privateKey.getEncoded()));
        assertEquals("generatePublic generated different key for algorithm " + keyfactAlgs[i] + " (provider=" + fact.getProvider().getName() + ")", Arrays.toString(keys.getPublic().getEncoded()), Arrays.toString(publicKey.getEncoded()));
        KeySpec encodedSpec = fact.getKeySpec(keys.getPublic(), X509EncodedKeySpec.class);
        assertTrue("improper key spec for encoded public key", encodedSpec.getClass().equals(X509EncodedKeySpec.class));
        encodedSpec = fact.getKeySpec(keys.getPrivate(), PKCS8EncodedKeySpec.class);
        assertTrue("improper key spec for encoded private key", encodedSpec.getClass().equals(PKCS8EncodedKeySpec.class));
    }
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) KeySpec(java.security.spec.KeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) SecureRandom(java.security.SecureRandom) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) KeyPairGenerator(java.security.KeyPairGenerator) KeyFactory(java.security.KeyFactory)

Aggregations

PublicKey (java.security.PublicKey)1113 PrivateKey (java.security.PrivateKey)278 KeyFactory (java.security.KeyFactory)184 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)170 KeyPair (java.security.KeyPair)167 X509Certificate (java.security.cert.X509Certificate)165 IOException (java.io.IOException)151 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)134 RSAPublicKey (java.security.interfaces.RSAPublicKey)123 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)110 Signature (java.security.Signature)108 InvalidKeyException (java.security.InvalidKeyException)96 ArraySet (android.util.ArraySet)94 Test (org.junit.Test)92 ByteArrayInputStream (java.io.ByteArrayInputStream)77 BigInteger (java.math.BigInteger)75 CertificateException (java.security.cert.CertificateException)71 Cipher (javax.crypto.Cipher)68 KeyPairGenerator (java.security.KeyPairGenerator)65 SignatureException (java.security.SignatureException)65