Commit eb118e37 authored by Miguel Mateo Montero's avatar Miguel Mateo Montero
Browse files

final release

parent 5d04a1ac
......@@ -73,7 +73,7 @@
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.10.0</version>
<version>4.9.0</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
......
/**
*
*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 org.apache.log4j.Logger;
import org.json.JSONObject;
import eu.ari.fentec.utils.conf.Configuration;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
/**
* Connector to Customer
* @author A101866
*
*/
public class CustomerConnector {
private Logger logger = Logger.getLogger(CustomerConnector.class);
/**
* @param walletId
* @return
*/
public boolean blockWallet (String walletId) {
try {
JSONObject jo = new JSONObject();
jo.put("walletId", walletId);
jo.put("reasson", "Double spending");
RequestBody body = RequestBody.create(MediaType.parse("application/json"), jo.toString());
Request request = new Request.Builder().url(Configuration.getConfiguration().getProperty("CUSTOMER_END_POINT") + "wallet/block")
.post(body)
.build();
logger.debug("block wallet body: " + jo.toString());
logger.debug("block wallet url: " + Configuration.getConfiguration().getProperty("CUSTOMER_END_POINT") + "wallet/block");
Response response;
OkHttpClient mClient = OKHttpClientST.getInstance().getClient();
response = mClient.newCall(request).execute();
try {
if (response.code() == 200) {
logger.debug("wallet id blocked: "+walletId);
return true;
}else {
logger.debug("block_code answer: " + response.code());
return false;
}
}catch (Exception i) {
logger.error(i,i);
return false;
}finally {
response.body().close();
}
}catch (Exception f) {
logger.error(f,f);
return false;
}
}
}
\ No newline at end of file
......@@ -64,9 +64,9 @@ public class ExchangeConnector {
.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;
}else {
......@@ -75,7 +75,13 @@ 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);
return false;
......@@ -84,7 +90,32 @@ public class ExchangeConnector {
}
/**
* @param walletId
* @return
*/
public String geteInvoices (String walletId) {
try {
Request request = new Request.Builder().url(Configuration.getConfiguration().getProperty("EXCHANGE_END_POINT") + "eInvoices/"+ walletId)
.get()
.build();
Response response;
OkHttpClient mClient = OKHttpClientST.getInstance().getClient();
response = mClient.newCall(request).execute();
try {
String body = response.body().string();
logger.debug("list of eInvoices to audit:\n"+body);
return body;
}catch (Exception i) {
logger.error(i,i);
return null;
}finally {
response.body().close();
}
}catch (Exception f) {
logger.error(f,f);
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/>.
**/
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();
}
}
......@@ -19,18 +19,24 @@
**/
package eu.ari.fentec.entities;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.security.Signature;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import org.apache.log4j.Logger;
import org.bouncycastle.util.encoders.Hex;
import eu.ari.fentec.connector.CustomerConnector;
import eu.ari.fentec.connector.ExchangeConnector;
import eu.ari.fentec.db.ConnectorMongo;
import eu.ari.fentec.model.AccountContract;
import eu.ari.fentec.model.AccountContractsContainer;
import eu.ari.fentec.model.EcoinIDContainer;
import eu.ari.fentec.model.EcoinMetaData;
import eu.ari.fentec.model.EcoinMetadataContainer;
import eu.ari.fentec.payment.ValidationToken;
......@@ -49,6 +55,7 @@ public class TrustedAuthority implements TrustedAuthorityIf {
private static boolean policyKeysCreated = false;
private static boolean masterKeyCreated = false;
private static ConnectorMongo mongo = new ConnectorMongo("trustedA", "trusted", "TrustAuthDB");
private Logger logger = Logger.getLogger(TrustedAuthority.class);
......@@ -95,7 +102,7 @@ public class TrustedAuthority implements TrustedAuthorityIf {
wrapper.GeneratePolicyK(path, pathuser,policy);
long endTime = System.nanoTime();
long timeElapsed = endTime - startTime;
logger.error("MAMM;TIME;GENPOLICYKEY;"+name+";"+timeElapsed);
logger.error("MAMM;TIME;GENPOLICYKEY;"+name+";"+timeElapsed/1000000);
policyKeysCreated = true;
......@@ -110,7 +117,7 @@ public class TrustedAuthority implements TrustedAuthorityIf {
account.setPolicy(policy);
return account.getIDasString();
return account.getID();
}catch (Exception e) {
logger.debug(e,e);
return null;
......@@ -121,16 +128,18 @@ public class TrustedAuthority implements TrustedAuthorityIf {
/* (non-Javadoc)
* @see eu.ari.fentec.entities.TrustedAuthorityIf#getEcoinValidationToken(java.lang.String, boolean)
*/
public ValidationToken getEcoinValidationToken(String ecoinTypeName, boolean twoMonths) {
byte[] id = new Utils().getRandomBytes(16);
EcoinIDContainer.getContainer().put(id, id);
public ValidationToken getEcoinValidationToken(String ecoinTypeName, boolean twoMonths, String walletId) {
byte[] idA = new Utils().getRandomBytes(16);
String idS = Hex.toHexString(idA);
try{
Signature ecdsaSignature = Signature.getInstance("SHA256withECDSA", "BC");
ecdsaSignature.initSign(tamd.ecdsaPrivateKey);
ecdsaSignature.update(id);
ecdsaSignature.update(idA);
byte[] signature = ecdsaSignature.sign();
if (!mongo.insertValidationToken(walletId, idS, "validationTokenColl", new String(Hex.encode(signature))))
return null;
KPabe_Wrapper wrapper = new KPabe_Wrapper(Configuration.getKPABELIB(), Configuration.getConfiguration().getProperty(ConfPnames.GOLANG_DEBUG));
......@@ -145,20 +154,21 @@ public class TrustedAuthority implements TrustedAuthorityIf {
String feSignature = wrapper.encrp(path, bl, g);
Long endTime = System.nanoTime();
Long timeElapsed = endTime - startTime;
logger.error("MAMM;TIME;ENCRYPTVALTOK;"+";"+timeElapsed);
logger.error("MAMM;TIME;ENCRYPTVALTOK;"+";"+timeElapsed/1000000);
logger.debug("VT created, ID: " + idS);
logger.debug("VT created SIG: " + Hex.toHexString(signature));
//String feSignature = wrapper.encrp(Configuration.getKEYPATH(), ecoinTypeName, "", bl, g);
return new ValidationToken(id, feSignature);
return new ValidationToken(idS, feSignature);
}catch(Exception e) {
if (Configuration.DEBUG) System.out.println(e.getMessage());
if (Configuration.DEBUG) System.out.println(e.toString());
logger.error(e,e);
return null;
}
return null;
}
/* (non-Javadoc)
......@@ -184,7 +194,7 @@ public class TrustedAuthority implements TrustedAuthorityIf {
wrapper.genMaster( path, ec.getNumParams()+1);
long endTime = System.nanoTime();
long timeElapsed = endTime - startTime;
logger.error("MAMM;TIME;GENMASTER;"+eCoinTypeName+";"+timeElapsed);
logger.error("MAMM;TIME;GENMASTER;"+eCoinTypeName+";"+timeElapsed/1000000);
masterKeyCreated = true;
}
......@@ -236,6 +246,48 @@ public class TrustedAuthority implements TrustedAuthorityIf {
}
}
/**
* @param vtID
* @param eInvoiceID
* @return
*/
public boolean auditEC (String vtID, String eInvoiceID) {
String walletID = mongo.getWalletIdByVTId(vtID, "validationTokenColl");
logger.debug("WalletID to be blocked: " + walletID);
CustomerConnector customerConnector = new CustomerConnector();
if (!customerConnector.blockWallet(walletID))
logger.error("there was an error blocking the wallet");
String eInvoiceList = "";
if (walletID != null) {
eInvoiceList = new ExchangeConnector().geteInvoices(walletID);
}
//generate report
StringBuffer report = new StringBuffer();
report.append(" suspicious vtid: " + Hex.toHexString(vtID.getBytes()) + "\n");
report.append(" owner of vtid (eCoin) wallet id: "+ walletID + "\n");
report.append(" list of eInvoices of this wallet id: \n" + eInvoiceList);
report.append("_________");
report.append(" vtid has been already spent in eInvoice: "+ eInvoiceID);
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'", Locale.ENGLISH);
Date date = new Date();
String formatedDate = format.format(date);
BufferedWriter bwr;
try {
bwr = new BufferedWriter(new FileWriter(new File(Configuration.getKEYPATH()+"../"+Hex.toHexString(vtID.getBytes())+"_"+formatedDate+".report")));
bwr.write(report.toString());
bwr.flush();
bwr.close();
} catch (IOException e) {
logger.error(e,e);
return false;
}
return true;
}
}
......@@ -29,14 +29,11 @@ public interface TrustedAuthorityIf {
* @param eCoinTypeName
* @param numParams
* @param values
* @param gammaC
* @param gammaM
* @param exchange
* @param ectAttrib
* @return
*/
// public boolean createEcoinMetadata (String eCoinTypeName, int numParams, int[] values, long[] gammaC, long[] gammaM, Exchange exchange);
// public boolean createEcoinMetadata (String eCoinTypeName, int numParams, String values, String gammaC, String gammaM);
public boolean createEcoinMetadata (String eCoinTypeName, int numParams, String values, String ectAttrib);
/**
* creates an account of a user of the platform, the role played will depend on the attributes
*
......@@ -44,17 +41,19 @@ public interface TrustedAuthorityIf {
* @param eCoinTypes
* @param attribs
* @param policy
* @param type
* @return
*/
//public String createAccount (String name, String[] eCoinTypes, long[] attribs, String policy);
public String createAccount(String name, String[] eCoinTypes, long[] attribs, String policy, String type);
/**
* generates a validation token for an ecoin of the type given
*
* @param ecoinTypeName
* @param twoMonths
* @param walletId
* @return
*/
//public ValidationToken getEcoinValidationToken (String ecoinTypeName);
public ValidationToken getEcoinValidationToken (String ecoinTypeName, boolean twoMonths);
public ValidationToken getEcoinValidationToken (String ecoinTypeName, boolean twoMonths, String walletId);
}
\ No newline at end of file
......@@ -23,8 +23,14 @@ import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import org.apache.log4j.Logger;
import eu.ari.fentec.utils.crypto.Utils;
/**
* @author A101866
*
*/
public class TrustedAuthorityMetadata {
public byte[] id;
......@@ -35,6 +41,8 @@ public class TrustedAuthorityMetadata {
private static TrustedAuthorityMetadata tamd = null;
static Logger logger = Logger.getLogger(TrustedAuthorityMetadata.class);
/**
* constructor
*/
......@@ -58,4 +66,18 @@ public class TrustedAuthorityMetadata {
return tamd;
}
/**
* @return
*/
public byte[] getPublicKey() {
try {
byte[] encoded = ecdsaPublicKey.getEncoded();
logger.debug("getPublicKey, encoded: " + encoded);
return encoded;
}catch(Exception e) {
logger.error (e,e);
return null;
}
}
}
......@@ -21,8 +21,8 @@
//package eu.ari.fentec.entities.issuer;
package eu.ari.fentec.model;
//import java.util.HashMap;
import org.apache.log4j.Logger;
import org.bouncycastle.util.encoders.Hex;
import eu.ari.fentec.utils.crypto.Utils;
......@@ -35,7 +35,7 @@ import eu.ari.fentec.utils.crypto.Utils;
*/
public class AccountContract {
private byte[] id;
private String id;
private String name;
private String[] authorizedECoinTypesName;
......@@ -72,7 +72,12 @@ public class AccountContract {
this.authorizedECoinTypesName = authEcoins;
this.attributes = attributes;
this.id = new Utils().getRandomBytes(16); //Create a 128-bit globally unique ID
byte[] idA = new Utils().getRandomBytes(16); //Create a 128-bit globally unique ID
id = new String (Hex.encode(idA));
Logger.getLogger(AccountContract.class).debug("AccountContract: " + id);
}
/**
......@@ -106,17 +111,11 @@ public class AccountContract {
/**
* @return
*/
public byte[] getID() {
public String getID() {
return this.id;
}
/**
* @return
*/
public String getIDasString() {
return new String(Hex.encode(id));
}
/**
* @return
*/
......
......@@ -22,6 +22,7 @@ package eu.ari.fentec.model;
import java.util.HashMap;
/**
* Acounts container
* @author A101866
*
*/
......
......@@ -67,21 +67,6 @@ public class EcoinMetaData {
return this.numParams;
}
/**
* @return
*/
// public long[] getGammaC() {
// return gammaC;
// }
//
// /**
// * @return
// */
// public long[] getGammaM() {
// return gammaM;
// }
/**
* @return attribute to identify teh ecoin type
......
/**
*
*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.resource;