use of com.icodici.crypto.SymmetricKey in project universa by UniversaBlockchain.
the class DatagramAdapterTest method create5NodesSend10Times.
public void create5NodesSend10Times() throws Exception {
NodeInfo node1 = new NodeInfo(TestKeys.publicKey(0), 10, "test_node_10", "localhost", 16201, 16202, 16301);
NodeInfo node2 = new NodeInfo(TestKeys.publicKey(1), 11, "test_node_11", "localhost", 16203, 16204, 16302);
NodeInfo node3 = new NodeInfo(TestKeys.publicKey(2), 12, "test_node_12", "localhost", 16204, 16205, 16303);
NodeInfo node4 = new NodeInfo(TestKeys.publicKey(0), 13, "test_node_13", "localhost", 16205, 16206, 16304);
NodeInfo node5 = new NodeInfo(TestKeys.publicKey(1), 14, "test_node_14", "localhost", 16206, 16207, 16305);
List<NodeInfo> nodes = new ArrayList<>();
nodes.add(node1);
nodes.add(node2);
nodes.add(node3);
nodes.add(node4);
nodes.add(node5);
NetConfig nc = new NetConfig(nodes);
SymmetricKey symmetricKey1 = new SymmetricKey();
SymmetricKey symmetricKey2 = new SymmetricKey();
SymmetricKey symmetricKey3 = new SymmetricKey();
SymmetricKey symmetricKey4 = new SymmetricKey();
SymmetricKey symmetricKey5 = new SymmetricKey();
// create implemented class with node1
DatagramAdapter d1 = new UDPAdapter(TestKeys.privateKey(0), symmetricKey1, node1, nc);
// create implemented class with node1
DatagramAdapter d2 = new UDPAdapter(TestKeys.privateKey(1), symmetricKey2, node2, nc);
// create implemented class with node1
DatagramAdapter d3 = new UDPAdapter(TestKeys.privateKey(2), symmetricKey3, node3, nc);
// create implemented class with node1
DatagramAdapter d4 = new UDPAdapter(TestKeys.privateKey(0), symmetricKey4, node4, nc);
// create implemented class with node1
DatagramAdapter d5 = new UDPAdapter(TestKeys.privateKey(1), symmetricKey5, node5, nc);
// d1.setVerboseLevel(DatagramAdapter.VerboseLevel.BASE);
// d2.setVerboseLevel(DatagramAdapter.VerboseLevel.BASE);
// d3.setVerboseLevel(DatagramAdapter.VerboseLevel.BASE);
// d4.setVerboseLevel(DatagramAdapter.VerboseLevel.BASE);
// d5.setVerboseLevel(DatagramAdapter.VerboseLevel.BASE);
// d1.setTestMode(DatagramAdapter.TestModes.LOST_PACKETS);
// d2.setTestMode(DatagramAdapter.TestModes.LOST_PACKETS);
// d3.setTestMode(DatagramAdapter.TestModes.LOST_PACKETS);
// d4.setTestMode(DatagramAdapter.TestModes.LOST_PACKETS);
// d5.setTestMode(DatagramAdapter.TestModes.LOST_PACKETS);
//
// d1.setLostPacketsPercentInTestMode(75);
// d2.setLostPacketsPercentInTestMode(75);
// d3.setLostPacketsPercentInTestMode(75);
// d4.setLostPacketsPercentInTestMode(75);
// d5.setLostPacketsPercentInTestMode(75);
List symmetricKeyErrors = new ArrayList();
d1.addErrorsCallback(m -> {
System.err.println(m);
if (m.indexOf("SymmetricKey.AuthenticationFailed") >= 0)
symmetricKeyErrors.add(m);
return m;
});
d2.addErrorsCallback(m -> {
System.err.println(m);
if (m.indexOf("SymmetricKey.AuthenticationFailed") >= 0)
symmetricKeyErrors.add(m);
return m;
});
d3.addErrorsCallback(m -> {
System.err.println(m);
if (m.indexOf("SymmetricKey.AuthenticationFailed") >= 0)
symmetricKeyErrors.add(m);
return m;
});
d4.addErrorsCallback(m -> {
System.err.println(m);
if (m.indexOf("SymmetricKey.AuthenticationFailed") >= 0)
symmetricKeyErrors.add(m);
return m;
});
d5.addErrorsCallback(m -> {
System.err.println(m);
if (m.indexOf("SymmetricKey.AuthenticationFailed") >= 0)
symmetricKeyErrors.add(m);
return m;
});
byte[] payload1 = "test data set 1".getBytes();
byte[] payload2 = "test data set 2".getBytes();
byte[] payload3 = "test data set 3".getBytes();
int attempts = 100;
int numSends = 10;
ArrayList<byte[]> receviedFor1 = new ArrayList<>();
ArrayList<byte[]> receviedFor2 = new ArrayList<>();
ArrayList<byte[]> receviedFor3 = new ArrayList<>();
ArrayList<byte[]> receviedFor4 = new ArrayList<>();
ArrayList<byte[]> receviedFor5 = new ArrayList<>();
AsyncEvent<Void> ae = new AsyncEvent<>();
d1.receive(d -> {
receviedFor1.add(d);
if ((receviedFor1.size() + receviedFor2.size() + receviedFor3.size() + receviedFor4.size() + receviedFor5.size()) == attempts * numSends)
ae.fire();
});
d2.receive(d -> {
receviedFor2.add(d);
if ((receviedFor1.size() + receviedFor2.size() + receviedFor3.size() + receviedFor4.size() + receviedFor5.size()) == attempts * numSends)
ae.fire();
});
d3.receive(d -> {
receviedFor3.add(d);
if ((receviedFor1.size() + receviedFor2.size() + receviedFor3.size() + receviedFor4.size() + receviedFor5.size()) == attempts * numSends)
ae.fire();
});
d4.receive(d -> {
receviedFor4.add(d);
if ((receviedFor1.size() + receviedFor2.size() + receviedFor3.size() + receviedFor4.size() + receviedFor5.size()) == attempts * numSends)
ae.fire();
});
d5.receive(d -> {
receviedFor5.add(d);
if ((receviedFor1.size() + receviedFor2.size() + receviedFor3.size() + receviedFor4.size() + receviedFor5.size()) == attempts * numSends)
ae.fire();
});
for (int i = 0; i < attempts; i++) {
System.out.println("Send part: " + i);
// send from adapter d1, to d2 as it is connected with node2 credentials:
for (int j = 0; j < numSends; j++) {
int rnd1 = new Random().nextInt(3);
int rnd2 = 0;
int rnd3 = 0;
while (rnd2 == rnd3) {
rnd2 = new Random().nextInt(5);
rnd3 = new Random().nextInt(5);
}
byte[] payload;
DatagramAdapter sender;
NodeInfo receiverNode;
if (rnd1 == 0)
payload = payload1;
else if (rnd1 == 1)
payload = payload2;
else
payload = payload3;
if (rnd2 == 0)
sender = d1;
else if (rnd2 == 1)
sender = d2;
else if (rnd2 == 2)
sender = d3;
else if (rnd2 == 3)
sender = d4;
else
sender = d5;
if (rnd3 == 0)
receiverNode = node1;
else if (rnd3 == 1)
receiverNode = node2;
else if (rnd3 == 2)
receiverNode = node3;
else if (rnd3 == 3)
receiverNode = node4;
else
receiverNode = node5;
sender.send(receiverNode, payload);
}
Thread.sleep(new Random().nextInt(200));
// if(new Random().nextBoolean()) ((UDPAdapter)d1).brakeSessions();
// if(new Random().nextBoolean()) ((UDPAdapter)d2).brakeSessions();
// if(new Random().nextBoolean()) ((UDPAdapter)d3).brakeSessions();
// if(new Random().nextBoolean()) ((UDPAdapter)d4).brakeSessions();
// if(new Random().nextBoolean()) ((UDPAdapter)d5).brakeSessions();
}
try {
ae.await(5000);
} catch (TimeoutException e) {
System.out.println("time is up");
}
System.out.println("receviedFor1 got: " + (receviedFor1.size()));
System.out.println("receviedFor2 got: " + (receviedFor2.size()));
System.out.println("receviedFor3 got: " + (receviedFor3.size()));
System.out.println("receviedFor4 got: " + (receviedFor4.size()));
System.out.println("receviedFor5 got: " + (receviedFor5.size()));
System.out.println("all got: " + (receviedFor1.size() + receviedFor2.size() + receviedFor3.size() + receviedFor4.size() + receviedFor5.size()));
// assertEquals(numSends * attempts, receviedFor1.size() + receviedFor2.size() + receviedFor3.size() + receviedFor4.size() + receviedFor5.size());
assertEquals(0, symmetricKeyErrors.size());
d1.shutdown();
d2.shutdown();
d3.shutdown();
d4.shutdown();
d5.shutdown();
}
use of com.icodici.crypto.SymmetricKey in project universa by UniversaBlockchain.
the class DatagramAdapterTest method shufflePackets.
@Test
public void shufflePackets() throws Exception {
// create pair of connected adapters
// and simulate packets received in random order
NodeInfo node1 = new NodeInfo(TestKeys.publicKey(0), 10, "test_node_10", "localhost", 16201, 16202, 16301);
NodeInfo node2 = new NodeInfo(TestKeys.publicKey(1), 11, "test_node_11", "localhost", 16203, 16204, 16302);
List<NodeInfo> nodes = new ArrayList<>();
nodes.add(node1);
nodes.add(node2);
NetConfig nc = new NetConfig(nodes);
// create implemented class with node1
DatagramAdapter d1 = new UDPAdapter(TestKeys.privateKey(0), new SymmetricKey(), node1, nc);
// create implemented class with node1
DatagramAdapter d2 = new UDPAdapter(TestKeys.privateKey(1), new SymmetricKey(), node2, nc);
// d1.setVerboseLevel(DatagramAdapter.VerboseLevel.BASE);
// d2.setVerboseLevel(DatagramAdapter.VerboseLevel.BASE);
d1.setTestMode(DatagramAdapter.TestModes.SHUFFLE_PACKETS);
d2.setTestMode(DatagramAdapter.TestModes.SHUFFLE_PACKETS);
byte[] payload1 = "test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 test data set 1 ".getBytes();
ArrayList<byte[]> receviedFor1 = new ArrayList<>();
ArrayList<byte[]> receviedFor2 = new ArrayList<>();
BlockingQueue<String> waitStatusQueue = new ArrayBlockingQueue<String>(1, true);
d2.receive(d -> {
receviedFor2.add(d);
try {
waitStatusQueue.put("DONE");
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("DONE error");
}
});
// send from adapter d1, to d2 as it is connected with node2 credentials:
d1.send(node2, payload1);
while (!((waitStatusQueue.take()).equals("DONE"))) {
// wait until it is delivered
}
assertEquals(1, receviedFor2.size());
byte[] data = receviedFor2.get(0);
// receiver must s
assertArrayEquals(payload1, data);
d1.shutdown();
d2.shutdown();
}
use of com.icodici.crypto.SymmetricKey in project universa by UniversaBlockchain.
the class CLIMain method breakSession.
/**
* Only for test purposes
*
* @param nodeNumber
* @throws IOException
*/
public static void breakSession(int nodeNumber) throws IOException {
BasicHttpClientSession s = getSession(nodeNumber);
s.setSessionId(666);
s.setSessionKey(new SymmetricKey());
Path keysDir = Paths.get(System.getProperty("user.home") + "/.universa");
if (!Files.exists(keysDir)) {
reporter.verbose("creating new keys directory: " + keysDir.toString());
final Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rwx------");
final FileAttribute<Set<PosixFilePermission>> ownerOnly = PosixFilePermissions.asFileAttribute(perms);
Files.createDirectory(keysDir, ownerOnly);
}
Path sessionFile = keysDir.resolve("node_" + nodeNumber + ".session");
try (OutputStream out = Files.newOutputStream(sessionFile)) {
out.write(Boss.pack(s.asBinder()));
}
final Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rw-------");
Files.setPosixFilePermissions(sessionFile, perms);
prefs.put("session_" + nodeNumber, sessionFile.toString());
reporter.verbose("Broken session has been stored to the " + keysDir + "/" + sessionFile);
}
use of com.icodici.crypto.SymmetricKey in project universa by UniversaBlockchain.
the class BasicHttpClient method start.
/**
* Authenticate self to the remote party. Blocks until the handshake is done. It is important to start() connection
* before any use.
*
* @param privateKey is client's {@link PrivateKey}
* @param nodePublicKey is Node's {@link PublicKey}
* @param session is {@link BasicHttpClientSession} object, can be null
* @return created or already using {@link BasicHttpClientSession}
* @throws IOException is something went wrong
*/
public BasicHttpClientSession start(PrivateKey privateKey, PublicKey nodePublicKey, BasicHttpClientSession session) throws IOException {
synchronized (this) {
if (session != null) {
this.session = session;
this.session.setNodePublicKey(nodePublicKey);
this.session.setPrivateKey(privateKey);
// this.session.setNodePublicKey(nodePublicKey);
//
// this.session.setPrivateKey(privateKey);
// Answer a = requestOrThrow("connect", "client_key", privateKey.getPublicKey().pack());
//
// this.session.setSessionId(a.data.getLongOrThrow("session_id"));
// byte[] server_nonce = a.data.getBinaryOrThrow("server_nonce");
// byte[] client_nonce = Do.randomBytes(47);
// byte[] data = Boss.pack(Binder.fromKeysValues(
// "client_nonce", client_nonce,
// "server_nonce", server_nonce
// ));
//
// a = requestOrThrow("get_token",
// "signature", privateKey.sign(data, HashType.SHA512),
// "data", data,
// "session_id", this.session.getSessionId()
// );
//
// data = a.data.getBinaryOrThrow("data");
//
// if (!nodePublicKey.verify(data, a.data.getBinaryOrThrow("signature"), HashType.SHA512))
// throw new IOException("node signature failed");
//
// Binder params = Boss.unpack(data);
//
// if (!Arrays.equals(client_nonce, params.getBinaryOrThrow("client_nonce")))
// throw new IOException("client nonce mismatch");
//
// byte[] key = Boss.unpack(
// privateKey.decrypt(
// params.getBinaryOrThrow("encrypted_token")
// )
// )
// .getBinaryOrThrow("sk");
// this.session.sessionKey = new SymmetricKey(key);
Binder result = command("hello");
this.session.setConnectMessage(result.getStringOrThrow("message"));
if (!result.getStringOrThrow("status").equals("OK"))
throw new ConnectionFailedException("" + result);
// throw new IllegalStateException("session already started");
} else {
this.session = new BasicHttpClientSession();
this.session.setNodePublicKey(nodePublicKey);
this.session.setPrivateKey(privateKey);
Answer a = requestOrThrow("connect", "client_key", privateKey.getPublicKey().pack());
this.session.setSessionId(a.data.getLongOrThrow("session_id"));
byte[] server_nonce = a.data.getBinaryOrThrow("server_nonce");
byte[] client_nonce = Do.randomBytes(47);
byte[] data = Boss.pack(Binder.fromKeysValues("client_nonce", client_nonce, "server_nonce", server_nonce));
a = requestOrThrow("get_token", "signature", privateKey.sign(data, HashType.SHA512), "data", data, "session_id", this.session.getSessionId());
data = a.data.getBinaryOrThrow("data");
if (!nodePublicKey.verify(data, a.data.getBinaryOrThrow("signature"), HashType.SHA512))
throw new IOException("node signature failed");
Binder params = Boss.unpack(data);
if (!Arrays.equals(client_nonce, params.getBinaryOrThrow("client_nonce")))
throw new IOException("client nonce mismatch");
byte[] key = Boss.unpack(privateKey.decrypt(params.getBinaryOrThrow("encrypted_token"))).getBinaryOrThrow("sk");
this.session.setSessionKey(new SymmetricKey(key));
Binder result = command("hello");
this.session.setConnectMessage(result.getStringOrThrow("message"));
if (!result.getStringOrThrow("status").equals("OK"))
throw new ConnectionFailedException("" + result);
}
return this.session;
}
}
use of com.icodici.crypto.SymmetricKey in project universa by UniversaBlockchain.
the class BasicHttpClientSession method reconstructSession.
public static BasicHttpClientSession reconstructSession(Binder binder) {
BasicHttpClientSession restoringSession = new BasicHttpClientSession();
restoringSession.setConnectMessage(binder.getOrThrow("connectMessage"));
// restoringSession.privateKey = binder.getBinaryOrThrowgetBinaryOrThrowgetBinaryOrThrow("privateKey");
restoringSession.setSessionKey(new SymmetricKey(binder.getBinaryOrThrow("sessionKey")));
restoringSession.setSessionId(binder.getLongOrThrow("sessionId"));
// restoringSession.nodePublicKey = binder.getBinaryOrThrow("nodePublicKey");
return restoringSession;
}
Aggregations