public PublicKey bin2publicKey(byte[] binary) throws EncryptionError { return new PublicKey(binary); }
public static NodeInfo initFrom(ResultSet rs) throws SQLException, EncryptionError { return new NodeInfo(new PublicKey(rs.getBytes("public_key")), rs.getInt("node_number"), rs.getString("node_name"), rs.getString("host"),rs.getString("public_host"), rs.getInt("udp_server_port"), rs.getInt("http_client_port"), rs.getInt("http_server_port")); } }
@Override public Object deserialize(Binder binder, BiDeserializer deserializer) { try { return new PublicKey(binder.getBinaryOrThrow("packed")); } catch (EncryptionError encryptionError) { return null; } }
public PublicKey getPublicKey() { if (cachedPublicKey == null) cachedPublicKey = new PublicKey(privateKey.getPublicKey()); return cachedPublicKey; }
public PublicKey base64toPublicKey(String s) throws EncryptionError { return new PublicKey(Base64.decodeLines(s)); }
private NodeRecord(Binder data) throws IOException { url = data.getStringOrThrow("url"); try { key = new PublicKey(data.getBinaryOrThrow("key")); } catch (EncryptionError encryptionError) { throw new IOException("failed to construct node public key", encryptionError); } }
public void setAuthorizedNameServiceCenterKeyData(Bytes authorizedNameServiceCenterKeyData) { try { this.authorizedNameServiceCenterKey = new PublicKey(authorizedNameServiceCenterKeyData.getData()); } catch (EncryptionError encryptionError) { encryptionError.printStackTrace(); } }
/** * Unpack the remote public key, check if it is trusted, if the {@link #remoteKey} is null, saves new value, * otherwise checks that it is the same key, or throws {@link IllegalArgumentException}. * <p> * Set the remote key and accurately check that it was not spoofed, e.g. only one key could be set event several * times. Because of the p2p nature of the connection we have no guarantee which way the remote key will arrive * first (in the "hello" command from the remote or from the hello answer from the remote), this method allows to * set the remote key in right mode. * * @param packedKey packed remoteKey * * @throws IllegalArgumentException if the {@link #remoteKey} is already set to a different key. */ public void setRemoteKey(@NonNull byte[] packedKey) throws EncryptionError { if (isTrustedKey != null && !isTrustedKey.test(packedKey)) throw new IllegalArgumentException("public key is not accepted"); PublicKey rk = new PublicKey(packedKey); synchronized (ready) { if (remoteKey == null) remoteKey = rk; else if (!rk.equals(remoteKey)) throw new IllegalArgumentException("remote key already set to a different value"); } }
@Override public byte[] encrypt(final byte[] bytes) throws EncryptionError { if (inUse.getAndSet(true)) { // our copy is in use synchronized (copyMutex) { if (copy == null) copy = new PublicKey(pack()); } return copy.encrypt(bytes); } else { try { return publicKey.encrypt(bytes); } finally { inUse.set(false); } } }
public AbstractKey unpackKey(byte[] data) throws EncryptionError { switch (algorythm) { case RSAPublic: return new PublicKey(data, this); case RSAPrivate: return new PrivateKey(data, this); case AES256: return new SymmetricKey(data, this); } throw new EncryptionError("can't unpack key: " + this); }
private void checkForSetUnlimit(Contract contract) { // check unlimit contract if (!contract.isUnlimitKeyContract(config)) return; // get key for setting unlimited requests PublicKey key; try { byte[] packedKey = contract.getTransactional().getData().getBinary("unlimited_key"); if (packedKey == null) return; key = new PublicKey(packedKey); } catch (Exception e) { return; } // setting unlimited requests for a key keyRequests.remove(key); keysUnlimited.remove(key); keysUnlimited.put(key, ZonedDateTime.now().plus(config.getUnlimitPeriod())); }
private static PublicKey readKeyAndGetPublic(String keyFilePath) throws IOException { PublicKey key; if (keyFilePath.endsWith(".private.unikey")) key = new PrivateKey(Do.read(keyFilePath)).getPublicKey(); else if (keyFilePath.endsWith(".public.unikey")) key = new PublicKey(Do.read(keyFilePath)); else { try { key = new PrivateKey(Do.read(keyFilePath)).getPublicKey(); } catch (EncryptionError e) { key = new PublicKey(Do.read(keyFilePath)); } } return key; }
private Binder onConnect(Binder params) throws ClientError { try { PublicKey clientKey = new PublicKey(params.getBinaryOrThrow("client_key")); return inSession(clientKey, session -> session.connect()); } catch (Exception e) { throw new ClientError(Errors.BAD_CLIENT_KEY, "client_key", "bad client key"); } }
@Ignore @Test public void checkPublicKeyMemoryLeak() throws Exception { byte[] bytes = Do.read("./src/test_contracts/keys/u_key.public.unikey"); for (int it = 0; it < 10000; it++) { PublicKey pk = new PublicKey(bytes); pk = null; System.gc(); } } @Before
/** * This is first step of creation and installation of the session. * @param session is {@link Session} in which sending is. */ private void sendHello(Session session) { try { report(logLabel, () -> "send hello to " + session.remoteNodeInfo.getNumber(), VerboseLevel.BASE); byte[] helloNonce = Do.randomBytes(64); Packet packet = new Packet(getNextPacketId(), myNodeInfo.getNumber(), session.remoteNodeInfo.getNumber(), PacketTypes.HELLO, new PublicKey(session.remoteNodeInfo.getPublicKey().pack()).encrypt(helloNonce)); sendPacket(session.remoteNodeInfo, packet); session.addPacketToRetransmitMap(packet.packetId, packet, helloNonce); } catch (EncryptionError e) { callErrorCallbacks("(sendHello) EncryptionError: " + e); } }
protected void sendDataGarbage(NodeInfo myNodeInfo, NodeInfo destination, UDPAdapter udpAdapter, DatagramSocket socket) throws Exception { byte[] data = new PublicKey(destination.getPublicKey().pack()).encrypt(Do.randomBytes(64)); byte[] crc32 = new Crc32().digest(data); byte[] payload = new byte[data.length + crc32.length]; System.arraycopy(data, 0, payload, 0, data.length); System.arraycopy(crc32, 0, payload, data.length, crc32.length); UDPAdapter.Packet packet = udpAdapter.createTestPacket( new Random().nextInt(Integer.MAX_VALUE), myNodeInfo.getNumber(), destination.getNumber(), UDPAdapter.PacketTypes.DATA, payload); sendBlock(packet, socket, destination); }
protected void sendWelcome(NodeInfo myNodeInfo, NodeInfo destination, UDPAdapter udpAdapter, DatagramSocket socket) throws Exception { byte[] payload = new PublicKey(destination.getPublicKey().pack()).encrypt(Do.randomBytes(64)); UDPAdapter.Packet packet = udpAdapter.createTestPacket( new Random().nextInt(Integer.MAX_VALUE), myNodeInfo.getNumber(), destination.getNumber(), UDPAdapter.PacketTypes.WELCOME, payload); sendBlock(packet, socket, destination); }
@Test public void testKeys() throws Exception { // Test vectors are for key #1 PrivateKey key = (PrivateKey) TestKeys.privateKey(1); byte[] encrypted = Bytes.fromBase64(encrypted64).toArray(); byte[] decrypted = key.decrypt(encrypted); assertEquals(plainText, new String(decrypted)); PublicKey publicKey = key.getPublicKey(); byte[] encrypted2 = publicKey.encrypt(plainText); assertEquals(plainText, new String(key.decrypt(encrypted2))); publicKey = new PublicKey(Do.decodeBase64(publicKey64)); encrypted2 = publicKey.encrypt(plainText); assertEquals(plainText, new String(key.decrypt(encrypted2))); }
@Test public void serializationTest() throws Exception { byte[] packedPublicKey = Do.decodeBase64(publicKey64); PublicKey publicKey = new PublicKey(packedPublicKey); byte[] packedPublicKey2 = publicKey.pack(); assertArrayEquals(packedPublicKey, packedPublicKey2); byte[] packedPrivateKey = Do.decodeBase64(TestKeys.binaryKeys[3]); PrivateKey privateKey = new PrivateKey(packedPrivateKey); byte[] packedPrivateKey2 = privateKey.pack(); assertArrayEquals(packedPrivateKey, packedPrivateKey2); }
/** * Someone who sent {@link PacketTypes#HELLO} typed {@link Packet}, * send us new KEY_REQ typed {@link Packet} - if all is ok we send session keys to. * SESSION's payload is more than 512 bytes, so used two parts here. * From now we ready to data exchange. * @param sessionReader is {@link SessionReader} in which sending is. */ private void sendSessionKey(SessionReader sessionReader) throws EncryptionError { report(logLabel, ()->"send session_key to "+sessionReader.remoteNodeInfo.getNumber(), VerboseLevel.BASE); List data = Arrays.asList(sessionReader.sessionKey.getKey(), sessionReader.remoteNonce); byte[] packed = Boss.pack(data); byte[] encrypted = new PublicKey(sessionReader.remoteNodeInfo.getPublicKey().pack()).encrypt(packed); byte[] sign = new PrivateKey(ownPrivateKey.pack()).sign(encrypted, HashType.SHA512); Packet packet1 = new Packet(getNextPacketId(), myNodeInfo.getNumber(), sessionReader.remoteNodeInfo.getNumber(), PacketTypes.SESSION_PART1, encrypted); Packet packet2 = new Packet(getNextPacketId(), myNodeInfo.getNumber(), sessionReader.remoteNodeInfo.getNumber(), PacketTypes.SESSION_PART2, sign); sendPacket(sessionReader.remoteNodeInfo, packet1); sendPacket(sessionReader.remoteNodeInfo, packet2); sessionReader.addPacketToRetransmitMap(packet1.packetId, packet1, encrypted); sessionReader.addPacketToRetransmitMap(packet2.packetId, packet2, sign); }