Commit 1bdb0818 authored by Miguel Mateo Montero's avatar Miguel Mateo Montero
Browse files

final release

parent 70b9915b
......@@ -74,7 +74,7 @@
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.10.0</version>
<version>4.9.0</version>
</dependency>
<dependency>
<groupId>eu.ari.fentec</groupId>
......
......@@ -33,11 +33,11 @@ import okhttp3.Response;
import java.util.ArrayList;
import org.apache.log4j.Logger;
import org.bouncycastle.util.encoders.Base64;
/**
* @author A101866
* connector ot the Exchange server
* connector to the Exchange server
*
*/
public class ExchangeConnector {
......@@ -50,7 +50,7 @@ public class ExchangeConnector {
* @param eCoinTypeName
* @return
*/
public boolean wireTransferRequest(int funds, byte[] walletID, ArrayList<String> eCoinTypeName) {
public boolean wireTransferRequest(int funds, String walletID, ArrayList<String> eCoinTypeName) {
try {
String jsonS = getJsonTransfer(funds, walletID, eCoinTypeName);
......@@ -63,10 +63,12 @@ public class ExchangeConnector {
.build();
Response response;
OkHttpClient mClient = new OkHttpClient();
OkHttpClient mClient = OKHttpClientST.getInstance().getClient();
response = mClient.newCall(request).execute();
if (response.code() == 200) {
try {
if (response.code() == 200) {
return true;
} else {
logger.error("error while creating wiring transfer for merchant account to exchange.");
......@@ -74,6 +76,12 @@ public class ExchangeConnector {
logger.error(" exchange msg: " + response.message());
return false;
}
}catch (Exception i) {
logger.error(i,i);
return false;
}finally {
response.body().close();
}
} catch (Exception e) {
logger.error(e, e);
......@@ -87,10 +95,10 @@ public class ExchangeConnector {
* @param eCoinTypeName
* @return Body of the request to Exchange
*/
private String getJsonTransfer(int funds, byte[] walletID, ArrayList<String> eCoinTypeName) {
private String getJsonTransfer(int funds, String walletID, ArrayList<String> eCoinTypeName) {
JSONObject jo = new JSONObject();
jo.put("funds", funds);
jo.put("walletID", Base64.toBase64String(walletID));
jo.put("walletID", walletID);
for (String s : eCoinTypeName)
jo.append("ect", s);
......@@ -104,15 +112,15 @@ public class ExchangeConnector {
* @param walletID
* @return
*/
public boolean ecoinVerification(Econtract econtract, byte[] walletID) {
public String ecoinVerification(Econtract econtract, String walletId) {
try {
String contractS = new Utils().serializeEcontract(econtract);
String id = Base64.toBase64String(walletID);
JSONObject jo = new JSONObject();
jo.put("econtract", contractS);
jo.put("id", id);
jo.put("id", walletId);
String jsonS = jo.toString(4);
RequestBody body = RequestBody.create(MediaType.parse("Application/Json"), jsonS);
......@@ -122,21 +130,34 @@ public class ExchangeConnector {
.url(Configuration.getConfiguration().getProperty("EXCHANGE_END_POINT") + "ecoinVerify").post(body)
.build();
Response response;
OkHttpClient mClient = new OkHttpClient();
OkHttpClient mClient = OKHttpClientST.getInstance().getClient();
response = mClient.newCall(request).execute();
try {
if (response.code() == 200) {
return true;
return null;
} else {
String m = response.body().string();
logger.error("ecoin Verification fail");
logger.error(" exchange response: " + response.code());
logger.error(" exchange msg: " + response.message());
return false;
logger.error(" exchange msg: " + m);
return m;
}
}catch (Exception i) {
logger.error(i,i);
return "internal server error";
}finally {
response.body().close();
}
} catch (Exception e) {
logger.error(e, e);
return false;
return "internal server error";
}
}
}
/**
*
*FENTEC Functional Encryption Technologies
*Privacy-preserving and Auditable Digital Currency Use Case
*Copyright © 2019 Atos Spain SA
*
*This program is free software: you can redistribute it and/or modify
*it under the terms of the GNU General Public License as published by
*the Free Software Foundation, either version 3 of the License, or
*(at your option) any later version.
*
*This program is distributed in the hope that it will be useful,
*but WITHOUT ANY WARRANTY; without even the implied warranty of
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
*GNU General Public License for more details.
*
*You should have received a copy of the GNU General Public License
*along with this program. If not, see <http://www.gnu.org/licenses/>.
**/
package eu.ari.fentec.connector;
import java.util.List;
import okhttp3.OkHttpClient;
/**
* class to manage OKHttpClient as singleton
* @author A101866
*
*/
public class OKHttpClientST {
private static OKHttpClientST singletonInstance;
private OkHttpClient client;
/**
* private constructor of the class
*/
private OKHttpClientST() {
client = new OkHttpClient.Builder()
.retryOnConnectionFailure(true)
.build();
}
/**
* getter of the singleton
* @return
*/
public static OKHttpClientST getInstance() {
if (singletonInstance == null) {
singletonInstance = new OKHttpClientST();
}
return singletonInstance;
}
/**
* @return
*/
public OkHttpClient getClient() {
return client;
}
/**
* close connections
*/
public void closeConnections() {
client.dispatcher().cancelAll();
}
}
......@@ -61,9 +61,10 @@ public class TrustedAuthConnector {
.build();
Response response;
OkHttpClient mClient = new OkHttpClient();
OkHttpClient mClient = OKHttpClientST.getInstance().getClient();
response = mClient.newCall(request).execute();
try {
if (response.code() == 200) {
String id = response.message();
return id;
......@@ -73,7 +74,13 @@ public class TrustedAuthConnector {
logger.error("trusted authority msg: " + response.message());
return null;
}
}catch (Exception i) {
logger.error(i,i);
return null;
}finally {
response.body().close();
}
}catch (Exception e) {
logger.error(e,e);
return null;
......
/**
*
*FENTEC Functional Encryption Technologies
*Privacy-preserving and Auditable Digital Currency Use Case
*Copyright © 2019 Atos Spain SA
*
*This program is free software: you can redistribute it and/or modify
*it under the terms of the GNU General Public License as published by
*the Free Software Foundation, either version 3 of the License, or
*(at your option) any later version.
*
*This program is distributed in the hope that it will be useful,
*but WITHOUT ANY WARRANTY; without even the implied warranty of
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
*GNU General Public License for more details.
*
*You should have received a copy of the GNU General Public License
*along with this program. If not, see <http://www.gnu.org/licenses/>.
**/
*
*FENTEC Functional Encryption Technologies
*Privacy-preserving and Auditable Digital Currency Use Case
*Copyright © 2019 Atos Spain SA
*
*This program is free software: you can redistribute it and/or modify
*it under the terms of the GNU General Public License as published by
*the Free Software Foundation, either version 3 of the License, or
*(at your option) any later version.
*
*This program is distributed in the hope that it will be useful,
*but WITHOUT ANY WARRANTY; without even the implied warranty of
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
*GNU General Public License for more details.
*
*You should have received a copy of the GNU General Public License
*along with this program. If not, see <http://www.gnu.org/licenses/>.
**/
package eu.ari.fentec.entities.merchant;
......@@ -27,7 +27,6 @@ import java.util.ArrayList;
import org.apache.log4j.Logger;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Base64;
import org.bouncycastle.util.encoders.Hex;
import org.bson.Document;
......@@ -60,10 +59,10 @@ import eu.ari.fentec.wrapper.KPabe_Wrapper;
*
*/
public class Merchant implements MerchantIf{
private static Logger logger = Logger.getLogger(Merchant.class);
private static ConnectorMongo mongo = new ConnectorMongo("merchant", "merch", "merchantDB");
MerchantMetadata mmd = null;
Wallet wallet = null;
......@@ -71,9 +70,9 @@ public class Merchant implements MerchantIf{
* Constructor
* @param mmd MerchantMetadata
*/
public Merchant(MerchantMetadata mmd) {
public Merchant(MerchantMetadata mmd, String walletId) {
this.mmd = mmd;
wallet = new Wallet ();
wallet = new Wallet (mmd.ectAL, walletId);
wallet.setFunds(0);
ExchangeConnector exchange = new ExchangeConnector();
ArrayList<String> ectN = new ArrayList<>();
......@@ -81,9 +80,9 @@ public class Merchant implements MerchantIf{
boolean success = exchange.wireTransferRequest(0, wallet.getId(), ectN);
logger.debug("wireTransfer to Exchange result for ("+this.getName()+"): "+ success);
}
/* (non-Javadoc)
* @see eu.ari.fentec.entities.merchant.MerchantIf#econtractRequest(java.lang.String)
......@@ -91,7 +90,7 @@ public class Merchant implements MerchantIf{
public String econtractRequest(String productS) {
Econtract econtract = null;
//writeOnTextArea("econtractRequest: INIT \n");
ItemList pl = new Utils().deserializeItemList(productS);
ArrayList<Item> productsList = pl.getItemList();
try {
......@@ -103,11 +102,11 @@ public class Merchant implements MerchantIf{
cost = cost + (p.getPrice() * it.getAmount());
}
}
}
Contract contract = new Contract( mmd.name , productsList, cost);
//Sign the contract
Gson gson = new Gson();
String contractJSON = gson.toJson(contract);
......@@ -117,57 +116,58 @@ public class Merchant implements MerchantIf{
ecdsaSignature.update(contractJSON.getBytes("UTF-8"));
econtract = new Econtract(contract, mmd.ecdsaPublicKey, ecdsaSignature.sign());
String econtractJSON = gson.toJson(econtract);
logger.debug ("El contrato en formato json: " + econtractJSON);
mongo.insertDocument(this.getName(), Const.COLLECTION_NAME, econtract, Const.PROPOSED);
mongo.insertEContract(this.getName(), Const.COLLECTION_NAME, econtract, Const.PROPOSED);
String econtractS = new Utils().serializeEcontract(econtract);
return econtractS;
} catch (Exception e) {
logger.error(e,e);
}
return null;
}
/* (non-Javadoc)
* @see eu.ari.fentec.entities.MerchantIf#econtractSendBack(eu.ari.fentec.payment.Econtract, eu.ari.fentec.entities.Exchange)
* @see eu.ari.fentec.entities.merchant.MerchantIf#econtractSendBack(java.lang.String)
*/
public boolean econtractSendBack(String econtractS) {
//writeOnTextArea("econtractSendBack:INIT \n");
//if (Configuration.DEBUG) System.out.println("econtractSendBack:INIT number of ecoinSignature:"+econtract.getEcoinsSignatures().size());
//Extract/Decrypt the Issur signature of its part of the ecoin id
public String econtractSendBack(String econtractS) {
try {
logger.debug("Meachant processing send Back");
Econtract econtract = new Utils().deserializeEcontract(econtractS);
//extract vt signature
logger.debug("contract deserialized");
for(Pair<EcoinPaid, byte[]> pair : econtract.getEcoinsSignatures()) {
logger.debug("processing coin");
EcoinPaid ecoin = pair.getKey();
StringBuffer sb = new StringBuffer();
sb.append("\n\tEcoin id: "+ Base64.toBase64String(ecoin.getID()));
sb.append("\n\tEcoin id: "+ (ecoin.getID()));
logger.debug("key obtained, going to decrypt vt");
KPabe_Wrapper wrapper = new KPabe_Wrapper (Configuration.getKPABELIB(), Configuration.getConfiguration().getProperty(ConfPnames.GOLANG_DEBUG));
//String path = Configuration.getKEYPATH()+"/"+ecoin.getECoinTypeName()+"/master";
String path = Configuration.getKEYPATH()+"/"+"kudo"+"/master";
String pathuser = path+"/"+this.getName();
//String encodedSignature = wrapper.Decrypt(path, this.getName(), ecoin.getFEIssuerSignature());
Long startTime = System.nanoTime();
String encodedSignature = wrapper.Decrypt(path, pathuser, ecoin.getFEIssuerSignature());
Long endTime = System.nanoTime();
Long timeElapsed = endTime - startTime;
logger.error("MAMM;TIME;DECRYPTVT;"+econtract.getContract().getContractNumber()+";"+timeElapsed);
logger.error("MAMM;TIME;DECRYPTVT;"+econtract.getContract().getContractNumber()+";"+timeElapsed/1000000);
logger.debug("vt decrypted");
if (Configuration.DEBUG) System.out.println("encodedSignature:"+encodedSignature);
......@@ -180,12 +180,18 @@ public class Merchant implements MerchantIf{
sb.append("\n\tDecripted signature of Validation ID: "+ ecoin.getIssuerSignature());
logger.debug("RCORT;validation token signature;"+ecoin.getIssuerSignature().length);
logger.debug("RCORT;validation token signature FE encdrypted;"+ecoin.getFEIssuerSignature().getBytes().length);
if (Configuration.DEBUG) System.out.println(sb.toString());
logger.debug(sb.toString());
}
//verify ecoins
ExchangeConnector exchange = new ExchangeConnector();
if(!this.econtractVerification(econtract) || !exchange.ecoinVerification(econtract, wallet.getId())) {
mongo.udateDocument(this.getName(), Const.COLLECTION_NAME, econtract, Const.REJECTED);
if(!this.econtractSignaturesVerification(econtract)) {
mongo.updateEContract(this.getName(), Const.COLLECTION_NAME, econtract, Const.REJECTED);
return ("contract signatures not valid");
}
String msg = exchange.ecoinVerification(econtract, wallet.getId());
if (msg!=null) {
mongo.updateEContract(this.getName(), Const.COLLECTION_NAME, econtract, Const.REJECTED);
return (msg);
}
else {
......@@ -194,70 +200,33 @@ public class Merchant implements MerchantIf{
wallet.updateFunds(wallet.getFunds() + econtract.getContract().getCost());
}
//Update the e-contract
Econtract ec = new Utils().deserializeEcontract(mongo.getDocument(econtract.getId(), Const.COLLECTION_NAME).getString("contract"));
Econtract ec = new Utils().deserializeEcontract(mongo.getEcontractByContractID(econtract.getId(), Const.COLLECTION_NAME).getString("contract"));
ec.getEcoinsSignatures().addAll(econtract.getEcoinsSignatures());
mongo.udateDocument(this.getName(), Const.COLLECTION_NAME, ec, Const.PAID);
return true;
mongo.updateEContract(this.getName(), Const.COLLECTION_NAME, ec, Const.PAID);
return null;
}
}catch (Exception e) {
if (Configuration.DEBUG) System.out.println(e.getMessage());
e.printStackTrace();
if (Configuration.DEBUG) System.out.println("---------------------------->eCoins could not be verified, payment rejected;");
logger.error(e,e);
return "internal server error";
}
return false;
}
/* (non-Javadoc)
* @see eu.ari.fentec.entities.MerchantIf#printEContracts()
*/
public void printEContracts() {
int i = 1;
if (Configuration.DEBUG) System.out.println("\n---------- THE MERCHANT'S E-CONTRACTS ----------");
if (Configuration.DEBUG) System.out.println("_____________________________________#");
MongoCursor<Document> mc = mongo.getAllDocuments(Const.COLLECTION_NAME);
while (mc.hasNext()) {
Document doc = mc.next();
String econt = (String) doc.get("contract");
Econtract econtract = new Utils().deserializeEcontract(econt);
if (Configuration.DEBUG) System.out.println("E-CONTRACT " + i);
if (Configuration.DEBUG) System.out.println("\tBase64-ID: " + econtract.getId());
Contract contract = econtract.getContract();
Gson gson = new Gson();
String contractJSON = gson.toJson(contract);
if (Configuration.DEBUG) System.out.println("\tContract: " + contractJSON);
if(econtract.getIssuerSignature().getValue() != null) if (Configuration.DEBUG) System.out.println("\tMerchant's signature: " + Base64.toBase64String(econtract.getIssuerSignature().getValue()));
else if (Configuration.DEBUG) System.out.println("\tMerchant's signature: NULL");
int j = 1;
if (econtract.getPayment() != null) {
for(Pair<EcoinPaid, byte[]> pair : econtract.getEcoinsSignatures()) {
if (Configuration.DEBUG) System.out.println("\tE-Coins' signatures " + j);
if(pair.getValue() != null) if (Configuration.DEBUG) System.out.println("\t\tE-Coin's signature: " + Base64.toBase64String(pair.getValue()));
else if (Configuration.DEBUG) System.out.println("\t\tE-Coin's signature: NULL");
j++;
}
}
i++;
}
if (Configuration.DEBUG) System.out.println("_____________________________________#\n");
}
/**
* @param econtract
* @return result of the verification
*/
private boolean econtractVerification(Econtract econtract) {
private boolean econtractSignaturesVerification(Econtract econtract) {
if(econtract != null) {
for(Pair<EcoinPaid, byte[]> pair : econtract.getEcoinsSignatures()) {
//Verify the e-coin's ECDSA signature over the e-contract, and the Exchange's ECDSA signature over the e-coin
......@@ -294,22 +263,14 @@ public class Merchant implements MerchantIf{
return ecdsaVerification.verify(ecoinSignature);
}catch (Exception e) {
System.err.println("Error when verifying the contract's signature");
e.printStackTrace();
logger.error("Error when verifying the contract's signature");
}
return false;
}
///
/// Added RCC
///
/**
* @return array list of products
*/
......@@ -331,7 +292,7 @@ public class Merchant implements MerchantIf{
public void setWallet(Wallet wallet) {
wallet = wallet;
}
/**
* @return name
*/
......@@ -346,8 +307,5 @@ public class Merchant implements MerchantIf{
mmd.name = name;
}
}
......@@ -26,6 +26,7 @@ import java.util.Map.Entry;
/**
* Container of Merchants
* @author A101866
*
*/
......@@ -35,10 +36,16 @@ public class MerchantContainer {
private static MerchantContainer merchantContainer = null;
/**
* Class constructor
*/
private MerchantContainer () {
this.merchantHM = new HashMap<>();
}
/**
* @return
*/
public static MerchantContainer getMerchantContainer() {
if (merchantContainer == null)
merchantContainer = new MerchantContainer();
......@@ -46,14 +53,24 @@ public class MerchantContainer {
return merchantContainer;
}
/**
* @param name
* @return