Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

Commit

Permalink
Merge pull request #21 from pohlm01/8-implement-logging-in-a-csv
Browse files Browse the repository at this point in the history
Add logging to a CSV file
  • Loading branch information
Bart73-v committed Jun 18, 2023
2 parents 4f34a9f + dd6b783 commit 3719a8b
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ reload_private.pem
reload_public.pem
reload_signature
CRL
*.csv
52 changes: 50 additions & 2 deletions terminals/src/main/java/nl/ru/sec_protocol/group5/Handle.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import javax.smartcardio.CardException;
import javax.smartcardio.CommandAPDU;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
Expand All @@ -12,6 +13,7 @@
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.time.OffsetDateTime;
import java.util.Base64;
import java.util.Date;

import static nl.ru.sec_protocol.group5.Utils.*;
Expand All @@ -34,6 +36,8 @@ public abstract class Handle {

protected int cardId;
protected int cardCounter;

protected byte[] cardBackendSignature;
protected OffsetDateTime cardExpirationDate;
protected RSAPublicKey cardPubKey;
protected int timeStamp;
Expand Down Expand Up @@ -151,8 +155,11 @@ private void exchangeBackendSignatures(CardChannel channel, byte terminalType) t
var response = channel.transmit(apdu);
System.out.printf("receive card signature: %s\n", response);

// Step 15 + 16 - Mutual authentication
var cardPassivelyVerified = verifyCardMetadata(response.getData());
// Step 15 - Mutual authentication
cardBackendSignature = response.getData();

// Step 16 - Mutual authentication
var cardPassivelyVerified = verifyCardMetadata(cardBackendSignature);
System.out.printf("card passively verified: %s\n", cardPassivelyVerified);
}

Expand Down Expand Up @@ -281,4 +288,45 @@ private void blockCard(CardChannel channel) throws NoSuchAlgorithmException, Sig
// TODO verify signature and log the successful blocking
}

/**
* Logs the transaction details to the given filename as comma seperated CSV
*
* @param filename file to log the details to
* @param amount amount transferred in the transaction
* @param terminalType terminal type. Should be either Pos or Reload.
* @param transactionSignature signature of the transaction produced by the card
* @author Maximilian Pohl
*/
public void logPaymentDetails(String filename, int amount, Backend.TerminalType terminalType, byte[] transactionSignature) {
if (!new File(filename).isFile()) {
try (var outputStream = new FileOutputStream(filename, true)) {
outputStream.write("timestamp,cardID,cardCounter,amount,terminalType,cardPubKey,cardBackendSignature,transactionSignature\n".getBytes());
} catch (Exception e) {
System.out.println("Error writing to log file");
System.exit(1);
}
}

try (var outputStream = new FileOutputStream(filename, true)) {
outputStream.write(String.valueOf(timeStamp).getBytes());
outputStream.write(',');
outputStream.write(String.valueOf(cardId).getBytes());
outputStream.write(',');
outputStream.write(String.valueOf(cardCounter).getBytes());
outputStream.write(',');
outputStream.write(String.valueOf(amount).getBytes());
outputStream.write(',');
outputStream.write(terminalType.name().getBytes());
outputStream.write(',');
outputStream.write(Base64.getEncoder().encode(cardPubKey.getEncoded()));
outputStream.write(',');
outputStream.write(Base64.getEncoder().encode(cardBackendSignature));
outputStream.write(',');
outputStream.write(Base64.getEncoder().encode(transactionSignature));
outputStream.write('\n');
} catch (Exception e) {
System.out.println("Error writing to log file");
System.exit(1);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,16 @@ private void communicateAmount(CardChannel channel, int amount) throws CardExcep
response = channel.transmit(apdu);
System.out.printf("receive amount signature: %s\n", response);

// Step 10 - Payment protocol
var transactionSignature = response.getData();

// verify signature
// Step 10 + 11 - Payment protocol
var signatureVerified = verifyAmountSignature(response.getData(), terminal.id, cardCounter, amount, cardId, timeStamp, cardPubKey);
// Step 11 - Payment protocol
var signatureVerified = verifyAmountSignature(transactionSignature, terminal.id, cardCounter, amount, cardId, timeStamp, cardPubKey);
System.out.printf("signatures verified: %s\n", signatureVerified);

// Step 12 - Payment protocol
// TODO log the transaction details
logPaymentDetails("payment_log.csv", amount, Backend.TerminalType.Pos, transactionSignature);

System.out.printf("Successfully payed %s\n\n", amount);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,17 @@ private void communicateAmount(CardChannel channel, int amount) throws NoSuchAlg
apdu = new CommandAPDU((byte) 0x00, SEND_RELOAD_AMOUNT_SIGNATURE_APDU_INS, signatureAmount[0], (byte) 0x00, signatureAmount, 1, SIGNATURE_SIZE - 1, SIGNATURE_SIZE);
response = channel.transmit(apdu);

// Step 9 + 10 - Reload protocol
if (!verifyAmountSignature(response.getData(), terminal.id, cardCounter, amount, cardId, timeStamp, cardPubKey)) {
// Step 9 - Reload protocol
var transactionSignature = response.getData();

// Step 10 - Reload protocol
if (!verifyAmountSignature(transactionSignature, terminal.id, cardCounter, amount, cardId, timeStamp, cardPubKey)) {
System.out.println("An error occurred while verifying the amount");
System.exit(1);
}

// Step 11 - Reload protocol
logPaymentDetails("reload_log.csv", amount, Backend.TerminalType.Reload, transactionSignature);
}

/**
Expand All @@ -76,9 +82,6 @@ private void communicateAmount(CardChannel channel, int amount) throws NoSuchAlg
* @author Bart Veldman
*/
private void finalizeReload(CardChannel channel, int amount) throws NoSuchAlgorithmException, SignatureException, InvalidKeyException, CardException {
// Step 11 - Reload protocol
// TODO log the transaction here

// Step 12 - Reload protocol
var data = new byte[COUNTER_SIZE + AMOUNT_SIZE + ID_SIZE];
cardCounter += 1;
Expand Down

0 comments on commit 3719a8b

Please sign in to comment.