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);
}
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);
}
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]);
}
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;
}
}
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));
}
}
Aggregations