Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/com/rubix/AuthenticateNode/Authenticate.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.ipfs.api.IPFS;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

Expand All @@ -11,11 +12,13 @@
import java.io.File;
import java.io.IOException;

import static com.rubix.Resources.APIHandler.networkInfo;
import static com.rubix.Resources.Functions.*;


public class Authenticate {
public static Logger AuthenticateLogger = Logger.getLogger(Authenticate.class);
public static int verifyCount = 0;

/**
* This method is used to authenticate a node in Rubix implementing text based two level NLSS.
Expand All @@ -29,8 +32,8 @@ public class Authenticate {

public static boolean verifySignature(String detailString) throws IOException, JSONException {
PropertyConfigurator.configure(LOGGER_PATH + "log4jWallet.properties");
verifyCount++;
IPFS ipfs = new IPFS("/ip4/127.0.0.1/tcp/" + IPFS_PORT);
System.out.println(IPFS_PORT);
JSONObject details = new JSONObject(detailString);
String decentralizedID = details.getString("did");
String hash = details.getString("hash");
Expand Down Expand Up @@ -61,7 +64,6 @@ public static boolean verifySignature(String detailString) throws IOException, J
StringBuilder decentralizedIDForAuth = new StringBuilder();
for (int value : positionsLevelZero) decentralizedIDForAuth.append(senderDIDBin.charAt(value));
if (recombinedResult.equals(decentralizedIDForAuth.toString())) {
AuthenticateLogger.info("Verification True");
return true;
} else {
AuthenticateLogger.info("Verification Failed");
Expand Down
106 changes: 80 additions & 26 deletions src/com/rubix/Consensus/InitiatorConsensus.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,23 @@ public class InitiatorConsensus {
private static final Object countLock = new Object();
private static final Object signLock = new Object();
public static ArrayList<String> quorumWithShares = new ArrayList<>();
public static volatile int[] quorumResponse = {0,0,0};
public static volatile int[] quorumResponse = {0, 0, 0};
public static volatile JSONArray finalQuorumSignsArray = new JSONArray();

/**
* This method increments the quorumResponse variable
*/
private static synchronized boolean voteNCount(int i,int quorumSize) {
private static synchronized boolean voteNCount(int i, int quorumSize) {
boolean status;
PropertyConfigurator.configure(LOGGER_PATH + "log4jWallet.properties");
synchronized (countLock) {
if(quorumResponse[i]<minQuorum(quorumSize)) {
if (quorumResponse[i] < minQuorum(quorumSize)) {
quorumResponse[i]++;
InitiatorConsensusLogger.debug("quorum response added index "+i+ " is "+quorumResponse[i]+" quorumsize " + minQuorum(quorumSize));
status=true;
}
else
{
status=false;
InitiatorConsensusLogger.debug("Consensus Reached for index "+ i);
InitiatorConsensusLogger.debug("quorum response added index " + i + " is " + quorumResponse[i] + " quorumsize " + minQuorum(quorumSize));
status = true;
} else {
status = false;
InitiatorConsensusLogger.debug("Consensus Reached for index " + i);
}
}
return status;
Expand All @@ -51,20 +50,23 @@ private static synchronized boolean voteNCount(int i,int quorumSize) {

/**
* This method stores all the quorum signatures until required count for consensus
* @param quorumDID DID of the Quorum
*
* @param quorumDID DID of the Quorum
* @param quorumSignResponse Signature of the Quorum
*/
private static synchronized void quorumSign(String quorumDID, String quorumSignResponse,int index,int quorumSize,int alphaSize) {
private static synchronized void quorumSign(String quorumDID, String hash, String quorumSignResponse, int index, int quorumSize, int alphaSize) {
PropertyConfigurator.configure(LOGGER_PATH + "log4jWallet.properties");
synchronized (signLock) {
try {
if(quorumSignature.length()<(minQuorum(alphaSize)+2*minQuorum(7))&& quorumResponse[index]<=minQuorum(quorumSize)) {
if (quorumSignature.length() < (minQuorum(alphaSize) + 2 * minQuorum(7)) && quorumResponse[index] <= minQuorum(quorumSize)) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("did", quorumDID);
jsonObject.put("sign", quorumSignResponse);
jsonObject.put("hash", hash);
finalQuorumSignsArray.put(jsonObject);
quorumSignature.put(quorumDID, quorumSignResponse);
InitiatorConsensusLogger.debug("quorum sign added current length " + quorumSignature.length());
}
else
{
InitiatorConsensusLogger.debug("quorum already reached consensus "+quorumSignature.length());
} else {
InitiatorConsensusLogger.debug("quorum already reached consensus " + quorumSignature.length());
}
} catch (JSONException e) {
InitiatorConsensusLogger.error("JSON Exception Occurred", e);
Expand All @@ -79,12 +81,13 @@ private static synchronized void quorumSign(String quorumDID, String quorumSignR
* 1. Contact quorum with sender signatures and details
* 2. Verify quorum signatures
* 3. If consensus reached , sends shares to Quorum
*
* @param ipfs IPFS instance
* @param PORT Port for forwarding to Quorum
*/
public static JSONObject start(String data, IPFS ipfs, int PORT, int index,String role,JSONArray quorumPeersObject,int alphaSize,int quorumSize) throws JSONException {
public static JSONObject start(String data, IPFS ipfs, int PORT, int index, String role, JSONArray quorumPeersObject, int alphaSize, int quorumSize) throws JSONException {
String[] qResponse = new String[QUORUM_COUNT];
// int quorumSize = quorumPeersObject.length();
String[] qVerification = new String[QUORUM_COUNT];
Socket[] qSocket = new Socket[QUORUM_COUNT];
PrintStream[] qOut = new PrintStream[QUORUM_COUNT];
BufferedReader[] qIn = new BufferedReader[QUORUM_COUNT];
Expand Down Expand Up @@ -127,6 +130,58 @@ public static JSONObject start(String data, IPFS ipfs, int PORT, int index,Stri
qSocket[j] = new Socket("127.0.0.1", PORT+j);
qIn[j] = new BufferedReader(new InputStreamReader(qSocket[j].getInputStream()));
qOut[j] = new PrintStream(qSocket[j].getOutputStream());
qOut[j].println("qstcmrequest");
qVerification[j] = qIn[j].readLine();
JSONObject quorumDetails = new JSONObject(qVerification[j]);
String data1 = quorumDetails.getString("CreditMapping");
JSONArray cmContent = new JSONArray(data1);


String data2 = quorumDetails.getString("Credits");
JSONArray creditsArray = new JSONArray(data2);
if(creditsArray.length() > 0) {
InitiatorConsensusLogger.debug("Entering Credits Security");
}else
InitiatorConsensusLogger.debug("Old Credits file");


// if (qstContent.length() == 0 && role == "alpha") {
// InitiatorConsensusLogger.warn("Alpha quorum (" + quorumID[j] + ") has no credits");
// }
// if (cmContent.length() == 0 && role == "alpha") {
// InitiatorConsensusLogger.warn("Alpha quorum (" + quorumID[j] + ") has no credits in credit mapping data");
// }

if(creditsArray.length() != 0) {
for (int k = 0; k < creditsArray.length(); k++) {
JSONObject object = creditsArray.getJSONObject(k);
String did = object.getString("did");
String sign = object.getString("sign");
String signHash = object.getString("hash");

JSONObject hashedCredObject = new JSONObject();
hashedCredObject.put("did", did);
hashedCredObject.put("hash", signHash);
hashedCredObject.put("signature", sign);


if (!(Authenticate.verifySignature(hashedCredObject.toString())))
InitiatorConsensusLogger.warn("Credit verification failed for Alpha quorum (" + quorumID[j] + ") credits");


if (cmContent != null) {
for (int l = 0; l < cmContent.length(); l++) {
if ((cmContent.getJSONObject(l).getString("hash") == signHash)) {
InitiatorConsensusLogger.warn("Credit verification failed for Alpha quorum (" + quorumID[j] + ") credits - Hash matched in Credits Mapping file");
}
}
}

}
}



qOut[j].println(detailsToken);
qResponse[j] = qIn[j].readLine();
if (qResponse[j].equals("Auth_Failed")) {
Expand All @@ -150,11 +205,11 @@ public static JSONObject start(String data, IPFS ipfs, int PORT, int index,Stri
InitiatorConsensusLogger.debug("waiting for " +quorumSize +" +signs " + role);
while (quorumResponse[index] < minQuorum(quorumSize)) {}
InitiatorConsensusLogger.debug("between Q1- to Q"+quorumSize+" for index " + index);
quorumSign(didHash, qResponse[j], index,quorumSize,alphaSize);
quorumSign(didHash,hash, qResponse[j], index,quorumSize,alphaSize);
quorumWithShares.add(quorumPeersObject.getString(j));
while (quorumSignature.length() < (minQuorum(alphaSize) + 2* minQuorum(7))) {}
InitiatorConsensusLogger.debug("sending Qsign of length " + quorumSignature.length() + "at index " + index);
qOut[j].println(quorumSignature);
qOut[j].println(finalQuorumSignsArray.toString());
IPFSNetwork.executeIPFSCommands("ipfs p2p close -t /p2p/" + quorumID[j]);
}
else {
Expand All @@ -165,22 +220,21 @@ public static JSONObject start(String data, IPFS ipfs, int PORT, int index,Stri
InitiatorConsensusLogger.debug("Quorum Count : " + quorumResponse + "Signature count : " + quorumSignature.length());
} else {
InitiatorConsensusLogger.debug("node failed authentication with index " + index + " with role " + role + " with did " + didHash + " and data to verify " + detailsToVerify);
InitiatorConsensusLogger.debug("recheck result is " + Authenticate.verifySignature(detailsToVerify.toString()));
IPFSNetwork.executeIPFSCommands("ipfs p2p close -t /p2p/" + quorumID[j]);
}
}
}
} catch (IOException | JSONException e) {
IPFSNetwork.executeIPFSCommands("ipfs p2p close -t /p2p/"+ quorumID[j]);
InitiatorConsensusLogger.error("IOException Occurred", e);
IPFSNetwork.executeIPFSCommands("ipfs p2p close -t /p2p/" + quorumID[j]);
InitiatorConsensusLogger.error("IOException Occurred");
e.printStackTrace();
}
});
quorumThreads[j].start();
}

while(quorumResponse[index] < minQuorum(quorumSize) || quorumSignature.length() < (minQuorum(alphaSize) + 2* minQuorum(7))){}

repo(ipfs);
} catch (JSONException e) {
InitiatorConsensusLogger.error("JSON Exception Occurred", e);
e.printStackTrace();
Expand Down
3 changes: 2 additions & 1 deletion src/com/rubix/Consensus/InitiatorProcedure.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static void consensusSetUp(String data,IPFS ipfs, int PORT,int alphaSize)
JSONArray betaList = dataObject.getJSONArray("betaList");
JSONArray gammaList = dataObject.getJSONArray("gammaList");
String authSenderByQuorumHash="", authQuorumHash="";
authSenderByQuorumHash = message;
authSenderByQuorumHash = calculateHash(message , "SHA3-256");
authQuorumHash = calculateHash(authSenderByQuorumHash.concat(receiverDidIpfs), "SHA3-256");
InitiatorProcedureLogger.debug("Sender by Quorum Hash" + authSenderByQuorumHash);
InitiatorProcedureLogger.debug("Quorum Auth Hash" + authQuorumHash);
Expand Down Expand Up @@ -122,6 +122,7 @@ public static void consensusSetUp(String data,IPFS ipfs, int PORT,int alphaSize)
});

InitiatorConsensus.quorumSignature=new JSONObject();
InitiatorConsensus.finalQuorumSignsArray = new JSONArray();
alphaThread.start();
betaThread.start();
gammaThread.start();
Expand Down
Loading