@Test public void testVerification() { KeyPair rsaKeyPair = Connection.generateRSAKeyPair(); Message message = Connection.generatePublicKeyMessage(rsaKeyPair.getPublic()); InetAddress address = InetAddress.getLoopbackAddress(); Map<InetAddress, Key> keys = new HashMap<>(10); keys.put(address, rsaKeyPair.getPublic()); Connection connection = mock(Connection.class); MessageInputStream in = mock(MessageInputStream.class); MessageOutputStream out = mock(MessageOutputStream.class); MessageIO messageIO = new MessageIO(in, out, address); when(connection.getMessageIO()).thenReturn(messageIO); when(connection.getVerifiedKeys()).thenReturn(keys); ExecutablePublicKeyMessage executable = new ExecutablePublicKeyMessage(connection, message); executable.runImmediate(); verify(out).setPublicKey(rsaKeyPair.getPublic()); }
@Override public void runImmediate() { byte[] keyBytes = (byte[]) getMessage().getArgument("publicKey"); try { PublicKey suppliedKey = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(keyBytes)); if (isKeyValid(suppliedKey)) { connection.getMessageIO().getOut().setPublicKey(suppliedKey); } } catch (InvalidKeySpecException | NoSuchAlgorithmException e) { LOGGER.log(Level.INFO, "Unable to instantiate public key; messages will not be encrypted!", e); } }
ExecutablePublicKeyMessage extends ExecutableMessage { @Override public void runImmediate() { byte[] keyBytes = (byte[]) getMessage().getArgument("publicKey"); try { PublicKey suppliedKey = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(keyBytes)); if (isKeyValid(suppliedKey)) { connection.getMessageIO().getOut().setPublicKey(suppliedKey); } } catch (InvalidKeySpecException | NoSuchAlgorithmException e) { LOGGER.log(Level.INFO, "Unable to instantiate public key; messages will not be encrypted!", e); } } }
ExecutablePublicKeyMessage extends ExecutableMessage { @Override public void runImmediate() { byte[] keyBytes = (byte[]) getMessage().getArgument("publicKey"); try { PublicKey suppliedKey = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(keyBytes)); if (isKeyValid(suppliedKey)) { connection.getMessageIO().getOut().setPublicKey(suppliedKey); } } catch (InvalidKeySpecException | NoSuchAlgorithmException e) { LOGGER.log(Level.INFO, "Unable to instantiate public key; messages will not be encrypted!", e); } } @SuppressWarnings("WeakerAccess")  ExecutablePublicKeyMessage(Connection connection, Message message); }
ExecutablePublicKeyMessage extends ExecutableMessage { @Override public void runImmediate() { byte[] keyBytes = (byte[]) getMessage().getArgument("publicKey"); try { PublicKey suppliedKey = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(keyBytes)); if (isKeyValid(suppliedKey)) { connection.getMessageIO().getOut().setPublicKey(suppliedKey); } } catch (InvalidKeySpecException | NoSuchAlgorithmException e) { LOGGER.log(Level.INFO, "Unable to instantiate public key; messages will not be encrypted!", e); } } @SuppressWarnings("WeakerAccess")  ExecutablePublicKeyMessage(Connection connection, Message message); @Override void runDelayed(); @Override void runImmediate(); }
ExecutablePublicKeyMessage extends ExecutableMessage { @Override public void runImmediate() { byte[] keyBytes = (byte[]) getMessage().getArgument("publicKey"); try { PublicKey suppliedKey = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(keyBytes)); if (isKeyValid(suppliedKey)) { connection.getMessageIO().getOut().setPublicKey(suppliedKey); } } catch (InvalidKeySpecException | NoSuchAlgorithmException e) { LOGGER.log(Level.INFO, "Unable to instantiate public key; messages will not be encrypted!", e); } } @SuppressWarnings("WeakerAccess")  ExecutablePublicKeyMessage(Connection connection, Message message); @Override void runDelayed(); @Override void runImmediate();  }