@Override protected Cipher initialValue() { return cipherForAlgorithm(RSA); } };
@Override protected Signature initialValue() { return signatureForAlgorithm(SHA1WITH_RSA); } };
public AbstractHttpClient(String url, PrivateKey privateKey, PublicKey publicKey, PublicKey remotePublicKey) { this.url = url; this.messageEncDec = new MessageEncoderDecoder(privateKey, publicKey, remotePublicKey); }
/** * Sets the remote public key. * * @param remotePublicKey the new remote public key * @throws GeneralSecurityException the general security exception */ public void setRemotePublicKey(byte[] remotePublicKey) throws GeneralSecurityException { this.remotePublicKey = KeyUtil.getPublic(remotePublicKey); if (LOG.isTraceEnabled()) { LOG.trace("RemotePublicKey {}", this.remotePublicKey != null ? bytesToHex(this.remotePublicKey.getEncoded()) : "empty"); } }
/** * Generate key pair. * * @param privateKeyLocation the private key location * @param publicKeyLocation the public key location * @return the key pair */ private KeyPair generateKeyPair(String privateKeyLocation, String publicKeyLocation) { LOG.debug("Generating key pair (private at {}; public at {})", privateKeyLocation, publicKeyLocation); return KeyUtil.generateKeyPair(privateKeyLocation, publicKeyLocation); }
/** * Generate key pair and saves it to specified files. * * @param privateKeyLocation the private key location * @param publicKeyLocation the public key location * @return the key pair */ public static KeyPair generateKeyPair(String privateKeyLocation, String publicKeyLocation) { try { KeyPair clientKeyPair = generateKeyPair(); saveKeyPair(clientKeyPair, privateKeyLocation, publicKeyLocation); return clientKeyPair; } catch (Exception ex) { LOG.error("Error generating client key pair", ex); } return null; }
/** * Decode data using session key then is decoded using private key. * * @param message the message * @param encodedKey the encoded key * @return the byte[] * @throws GeneralSecurityException the general security exception */ public byte[] decodeData(byte[] message, byte[] encodedKey) throws GeneralSecurityException { sessionCipherPair = null; decodeSessionKey(encodedKey); return decodeData(message); }
protected byte[] verifyResponse(byte[] body, byte[] signature) throws GeneralSecurityException { if (!verificationEnabled || messageEncDec.verify(body, signature)) { return body; } else { throw new SecurityException("message can't be verified"); } }
@Override protected KeyGenerator initialValue() { return keyGeneratorForAlgorithm(SESSION_KEY_ALGORITHM, SESSION_KEY_SIZE); } };
/** * Decode data using session key. * * @param message the message * @return the byte[] * @throws GeneralSecurityException the general security exception */ public byte[] decodeData(byte[] message) throws GeneralSecurityException { if (sessionCipherPair == null) { sessionCipherPair = new CipherPair(SESSION_CRYPT_ALGORITHM, getSessionKey()); } return sessionCipherPair.decCipher.doFinal(message); }
/** * Generate key pair. * * @param privateKeyLocation the private key location * @param publicKeyLocation the public key location * @return the key pair */ private KeyPair generateKeyPair(String privateKeyLocation, String publicKeyLocation) { LOG.debug("Generating Key pair"); KeyPair kp = KeyUtil.generateKeyPair(privateKeyLocation, publicKeyLocation); LOG.debug("Private key location: {}", privateKeyLocation); LOG.debug("Public key location: {}", publicKeyLocation); return kp; }
/** * Generate key pair and saves it to specified streams. * * @param privateKeyOutput the private key output stream * @param publicKeyOutput the public key output stream * @return the key pair */ public static KeyPair generateKeyPair( OutputStream privateKeyOutput, OutputStream publicKeyOutput) { try { KeyPair clientKeyPair = generateKeyPair(); saveKeyPair(clientKeyPair, privateKeyOutput, publicKeyOutput); return clientKeyPair; } catch (Exception ex) { LOG.error("Error generating client key pair", ex); } return null; }
private MessageEncoderDecoder getOrInitCrypt() { if (crypt.get() == null) { crypt.set(new MessageEncoderDecoder(keyPair.getPrivate(), keyPair.getPublic())); } return crypt.get(); }
/** * Creates enc/dec ciphers based on cipher algorithm and secret key. * * @param algorithm - Cipher algorithm * @param secretKey - Secret key */ private CipherPair(String algorithm, SecretKey secretKey) throws InvalidKeyException { this.decCipher = cipherForAlgorithm(algorithm); this.decCipher.init(Cipher.DECRYPT_MODE, secretKey); this.encCipher = cipherForAlgorithm(algorithm); this.encCipher.init(Cipher.ENCRYPT_MODE, secretKey); } }
@Test public void testNotExistingSignatoreAlgorithm() { Assert.assertNull(MessageEncoderDecoder.signatureForAlgorithm("42")); } }
@Test public void testExistingKeyGeneratorAlgorithm() { Assert.assertNotNull(MessageEncoderDecoder.keyGeneratorForAlgorithm("AES", 128)); }
/** * Encode data using sessionKey. * * @param message the message * @return the byte[] * @throws GeneralSecurityException the general security exception */ public byte[] encodeData(byte[] message) throws GeneralSecurityException { if (sessionCipherPair == null) { sessionCipherPair = new CipherPair(SESSION_CRYPT_ALGORITHM, getSessionKey()); } return sessionCipherPair.encCipher.doFinal(message); }
public DefaultOperationTcpChannelTest() throws Exception { clientKeys = KeyUtil.generateKeyPair(); }
@Test public void testExistingCipherAlgorithm() { Assert.assertNotNull(MessageEncoderDecoder.cipherForAlgorithm("RSA")); }
@Test public void testExistingSignatoreAlgorithm() { Assert.assertNotNull(MessageEncoderDecoder.signatureForAlgorithm("SHA1withRSA")); }