/** * 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); }
privateKeyInput = storage.openForRead(clientPrivateKeyFileLocation); PublicKey publicKey = KeyUtil.getPublic(publicKeyInput); PrivateKey privateKey = KeyUtil.getPrivate(privateKeyInput); if (!KeyUtil.validateKeyPair(keyPair)) { throw new InvalidKeyException(); privateKeyOutput = storage.openForWrite(clientPrivateKeyFileLocation); publicKeyOutput = storage.openForWrite(clientPublicKeyFileLocation); keyPair = KeyUtil.generateKeyPair(privateKeyOutput, publicKeyOutput); } catch (IOException ex) { LOG.error("Error generating Client Key pair", ex);
/** * Saves public and private keys to specified files. * * @param keyPair the key pair * @param privateKeyFile the private key file * @param publicKeyFile the public key file * @throws IOException Signals that an I/O exception has occurred. */ public static void saveKeyPair(KeyPair keyPair, String privateKeyFile, String publicKeyFile) throws IOException { File privateFile = makeDirs(privateKeyFile); File publicFile = makeDirs(publicKeyFile); OutputStream privateKeyOutput = null; OutputStream publicKeyOutput = null; try { privateKeyOutput = new FileOutputStream(privateFile); publicKeyOutput = new FileOutputStream(publicFile); saveKeyPair(keyPair, privateKeyOutput, publicKeyOutput); } finally { IOUtils.closeQuietly(privateKeyOutput); IOUtils.closeQuietly(publicKeyOutput); } }
/** * 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; }
if (file.exists()) { try { privateKey = KeyUtil.getPrivate(file); } catch (Exception ex) { LOG.debug("Error loading private key", ex); if (file.exists()) { try { publicKey = KeyUtil.getPublic(file); } catch (Exception ex) { LOG.debug("Error loading public key", ex);
/** * Gets the public key from input stream. * * @param input the input stream * @return the public * @throws IOException the i/o exception * @throws InvalidKeyException invalid key exception */ public static PublicKey getPublic(InputStream input) throws IOException, InvalidKeyException { ByteArrayOutputStream output = new ByteArrayOutputStream(); byte[] buffer = new byte[4096]; int len = 0; while (-1 != (len = input.read(buffer))) { output.write(buffer, 0, len); } byte[] keyBytes = output.toByteArray(); return getPublic(keyBytes); }
/** * Gets the private key from input stream. * * @param input the input stream * @return the private * @throws IOException the i/o exception * @throws InvalidKeyException invalid key exception */ public static PrivateKey getPrivate(InputStream input) throws IOException, InvalidKeyException { ByteArrayOutputStream output = new ByteArrayOutputStream(); byte[] buffer = new byte[4096]; int len = 0; while (-1 != (len = input.read(buffer))) { output.write(buffer, 0, len); } byte[] keyBytes = output.toByteArray(); return getPrivate(keyBytes); }
@Test public void validateKeyPairTest() throws Exception { KeyPairGenerator clientKeyGen = KeyPairGenerator.getInstance("RSA"); clientKeyGen.initialize(2048); KeyPair kp = clientKeyGen.genKeyPair(); PublicKey clientPublic = kp.getPublic(); PrivateKey clientPrivate = kp.getPrivate(); Assert.assertTrue(KeyUtil.validateKeyPair(new KeyPair(clientPublic, clientPrivate))); } }
if (file.exists()) { try { privateKey = KeyUtil.getPrivate(file); } catch (Exception ex) { LOG.debug("Error loading Private Key", ex); if (file.exists()) { try { publicKey = KeyUtil.getPublic(file); } catch (Exception ex) { LOG.debug("Error loading Public Key", ex);
/** * 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 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; }
/** * Gets the private key from file. * * @param file the file * @return the private * @throws IOException the i/o exception * @throws InvalidKeyException invalid key exception */ public static PrivateKey getPrivate(File file) throws IOException, InvalidKeyException { DataInputStream dis = null; try { FileInputStream fis = new FileInputStream(file); dis = new DataInputStream(fis); byte[] keyBytes = new byte[(int) file.length()]; dis.readFully(keyBytes); return getPrivate(keyBytes); } finally { IOUtils.closeQuietly(dis); } }
/** * 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; }
@Test public void testRecreateKeys() throws IOException, InvalidKeyException { KaaClientState state = new KaaClientPropertiesState(new FilePersistentStorage(), CommonsBase64.getInstance(), getProperties(), true); state.getPublicKey(); state.getPrivateKey(); File pub = new File(WORK_DIR + KEY_PUBLIC); File priv = new File(WORK_DIR + KEY_PRIVATE); assertArrayEquals(KeyUtil.getPrivate(priv).getEncoded(), state.getPrivateKey().getEncoded()); assertArrayEquals(KeyUtil.getPublic(pub).getEncoded(), state.getPublicKey().getEncoded()); pub.delete(); priv.delete(); assertNotNull(state.getPublicKey()); assertNotNull(state.getPrivateKey()); }
/** * Gets the public key from file. * * @param file the file * @return the public * @throws IOException the i/o exception * @throws InvalidKeyException invalid key exception */ public static PublicKey getPublic(File file) throws IOException, InvalidKeyException { DataInputStream dis = null; try { FileInputStream fis = new FileInputStream(file); dis = new DataInputStream(fis); byte[] keyBytes = new byte[(int) file.length()]; dis.readFully(keyBytes); return getPublic(keyBytes); } finally { IOUtils.closeQuietly(dis); } }
public DefaultOperationTcpChannelTest() throws Exception { clientKeys = KeyUtil.generateKeyPair(); }
/** * All-args constructor. */ public IpTransportInfo(TransportConnectionInfo parent) { super(parent.getServerType(), new ProtocolMetaData(parent.getAccessPointId(), new ProtocolVersionPair(parent.getTransportId() .getProtocolId(), parent.getTransportId().getProtocolVersion()), ByteBuffer.wrap(parent.getConnectionInfo()))); ByteBuffer buf = md.getConnectionInfo().duplicate(); byte[] publicKeyData = new byte[buf.getInt()]; buf.get(publicKeyData); try { this.publicKey = KeyUtil.getPublic(publicKeyData); } catch (InvalidKeyException ex) { LOG.error("Can't initialize public key", ex); throw new RuntimeException(ex); } byte[] hostData = new byte[buf.getInt()]; buf.get(hostData); this.host = new String(hostData, UTF8); this.port = buf.getInt(); }
private Map<TransportProtocolId, List<TransportConnectionInfo>> getDefaultBootstrapServers() throws NoSuchAlgorithmException { Map<TransportProtocolId, List<TransportConnectionInfo>> bootststrapServers = new HashMap<>(); TransportConnectionInfo server = IpTransportInfoTest.createTestServerInfo( ServerType.BOOTSTRAP, TransportProtocolIdConstants.HTTP_TRANSPORT_ID, "localhost", 9889, KeyUtil.generateKeyPair().getPublic()); bootststrapServers.put(TransportProtocolIdConstants.HTTP_TRANSPORT_ID, Collections.singletonList(server)); return bootststrapServers; }
private PublicKey getPublicKey(ClientSync request) throws GeneralSecurityException { PublicKey endpointKey = null; if (request.getProfileSync() != null && request.getProfileSync().getEndpointPublicKey() != null) { byte[] publicKeySrc = request.getProfileSync().getEndpointPublicKey().array(); endpointKey = KeyUtil.getPublic(publicKeySrc); } if (endpointKey == null) { EndpointObjectHash hash = getEndpointObjectHash(request); endpointKey = cacheService.getEndpointKey(hash); } return endpointKey; }
protected void initStorageMock(PersistentStorage storage) throws NoSuchAlgorithmException, IOException { KeyPair kp = KeyUtil.generateKeyPair(); Mockito.when(storage.exists(KaaClientProperties.CLIENT_PUBLIC_KEY_NAME_DEFAULT)).thenReturn(true); Mockito.when(storage.exists(KaaClientProperties.CLIENT_PRIVATE_KEY_NAME_DEFAULT)).thenReturn(true); Mockito.when(storage.openForRead(KaaClientProperties.CLIENT_PUBLIC_KEY_NAME_DEFAULT)).thenReturn( new ByteArrayInputStream(kp.getPublic().getEncoded())); Mockito.when(storage.openForRead(KaaClientProperties.CLIENT_PRIVATE_KEY_NAME_DEFAULT)).thenReturn( new ByteArrayInputStream(kp.getPrivate().getEncoded())); Mockito.when(storage.openForWrite(Mockito.anyString())).thenReturn(Mockito.mock(OutputStream.class)); }