Refactorings, add decryption to crypt test at startup

This commit is contained in:
Manfred Karrer 2016-03-16 23:59:08 +01:00
parent 8037c33e79
commit 6721131ccf
9 changed files with 30 additions and 19 deletions

View file

@ -32,12 +32,13 @@ public final class DepositTxPublishedMessage extends TradeMessage implements Mai
public final byte[] depositTx;
private final NodeAddress senderNodeAddress;
private final String uid = UUID.randomUUID().toString();
private final String uid;
public DepositTxPublishedMessage(String tradeId, byte[] depositTx, NodeAddress senderNodeAddress) {
super(tradeId);
this.depositTx = depositTx;
this.senderNodeAddress = senderNodeAddress;
uid = UUID.randomUUID().toString();
}
@Override

View file

@ -31,12 +31,13 @@ public final class FiatTransferStartedMessage extends TradeMessage implements Ma
public final String buyerPayoutAddress;
private final NodeAddress senderNodeAddress;
private final String uid = UUID.randomUUID().toString();
private final String uid;
public FiatTransferStartedMessage(String tradeId, String buyerPayoutAddress, NodeAddress senderNodeAddress) {
super(tradeId);
this.buyerPayoutAddress = buyerPayoutAddress;
this.senderNodeAddress = senderNodeAddress;
uid = UUID.randomUUID().toString();
}
@Override

View file

@ -34,7 +34,7 @@ public final class FinalizePayoutTxRequest extends TradeMessage implements Mailb
public final String sellerPayoutAddress;
public final long lockTimeAsBlockHeight;
private final NodeAddress senderNodeAddress;
private final String uid = UUID.randomUUID().toString();
private final String uid;
public FinalizePayoutTxRequest(String tradeId,
byte[] sellerSignature,
@ -46,6 +46,7 @@ public final class FinalizePayoutTxRequest extends TradeMessage implements Mailb
this.sellerPayoutAddress = sellerPayoutAddress;
this.lockTimeAsBlockHeight = lockTimeAsBlockHeight;
this.senderNodeAddress = senderNodeAddress;
uid = UUID.randomUUID().toString();
}
@Override

View file

@ -47,7 +47,7 @@ public final class PayDepositRequest extends TradeMessage implements MailboxMess
public final ArrayList<NodeAddress> acceptedArbitratorNodeAddresses;
public final NodeAddress arbitratorNodeAddress;
private final NodeAddress senderNodeAddress;
private final String uid = UUID.randomUUID().toString();
private final String uid;
public PayDepositRequest(NodeAddress senderNodeAddress,
String tradeId,
@ -77,6 +77,7 @@ public final class PayDepositRequest extends TradeMessage implements MailboxMess
this.takeOfferFeeTxId = takeOfferFeeTxId;
this.acceptedArbitratorNodeAddresses = acceptedArbitratorNodeAddresses;
this.arbitratorNodeAddress = arbitratorNodeAddress;
uid = UUID.randomUUID().toString();
}
@Override

View file

@ -32,12 +32,13 @@ public final class PayoutTxFinalizedMessage extends TradeMessage implements Mail
public final byte[] payoutTx;
private final NodeAddress senderNodeAddress;
private final String uid = UUID.randomUUID().toString();
private final String uid;
public PayoutTxFinalizedMessage(String tradeId, byte[] payoutTx, NodeAddress senderNodeAddress) {
super(tradeId);
this.payoutTx = payoutTx;
this.senderNodeAddress = senderNodeAddress;
uid = UUID.randomUUID().toString();
}
@Override

View file

@ -35,9 +35,7 @@ import io.bitsquare.btc.pricefeed.PriceFeed;
import io.bitsquare.common.Clock;
import io.bitsquare.common.Timer;
import io.bitsquare.common.UserThread;
import io.bitsquare.common.crypto.CryptoException;
import io.bitsquare.common.crypto.Encryption;
import io.bitsquare.common.crypto.KeyRing;
import io.bitsquare.common.crypto.*;
import io.bitsquare.common.util.Utilities;
import io.bitsquare.gui.Navigation;
import io.bitsquare.gui.common.model.ViewModel;
@ -56,6 +54,7 @@ import io.bitsquare.p2p.P2PServiceListener;
import io.bitsquare.p2p.network.CloseConnectionReason;
import io.bitsquare.p2p.network.Connection;
import io.bitsquare.p2p.network.ConnectionListener;
import io.bitsquare.p2p.peers.keepalive.messages.Ping;
import io.bitsquare.payment.OKPayAccount;
import io.bitsquare.trade.Trade;
import io.bitsquare.trade.TradeManager;
@ -467,9 +466,16 @@ public class MainViewModel implements ViewModel {
try {
log.trace("Run crypto test");
// just use any simple dummy msg
Encryption.encryptHybridWithSignature(new io.bitsquare.p2p.peers.keepalive.messages.Ping(0, 0),
io.bitsquare.p2p.peers.keepalive.messages.Ping payload = new Ping(1, 1);
SealedAndSigned sealedAndSigned = Encryption.encryptHybridWithSignature(payload,
keyRing.getSignatureKeyPair(), keyRing.getPubKeyRing().getEncryptionPubKey());
DecryptedDataTuple tuple = Encryption.decryptHybridWithSignature(sealedAndSigned, keyRing.getEncryptionKeyPair().getPrivate());
if (tuple.payload instanceof Ping &&
((Ping) tuple.payload).nonce == payload.nonce &&
((Ping) tuple.payload).lastRoundTripTime == payload.lastRoundTripTime)
log.trace("Crypto test succeeded");
else
throw new CryptoException("Payload not correct after decryption");
} catch (CryptoException e) {
e.printStackTrace();
String msg = "Seems that you use a self compiled binary and have not following the build " +

View file

@ -21,7 +21,6 @@ import io.bitsquare.common.crypto.*;
import io.bitsquare.p2p.Message;
import javax.inject.Inject;
import java.security.KeyPair;
public class EncryptionService {
private final KeyRing keyRing;
@ -32,8 +31,7 @@ public class EncryptionService {
}
public SealedAndSigned encryptAndSign(PubKeyRing pubKeyRing, Message message) throws CryptoException {
KeyPair signatureKeyPair = keyRing.getSignatureKeyPair();
return Encryption.encryptHybridWithSignature(message, signatureKeyPair, pubKeyRing.getEncryptionPubKey());
return Encryption.encryptHybridWithSignature(message, keyRing.getSignatureKeyPair(), pubKeyRing.getEncryptionPubKey());
}
public DecryptedMsgWithPubKey decryptAndVerify(SealedAndSigned sealedAndSigned) throws CryptoException {

View file

@ -348,9 +348,9 @@ public class P2PService implements SetupListener, MessageListener, ConnectionLis
log.info("Wrong receiverAddressMaskHash. The message is not intended for us.");
}
} catch (CryptoException e) {
log.warn(message.toString());
log.warn(e.toString());
log.warn("Decryption of SealedAndSignedMessage failed. " +
log.info(message.toString());
log.info(e.toString());
log.info("Decryption of prefixedSealedAndSignedMessage.sealedAndSigned failed. " +
"That is expected if the message is not intended for us.");
}
}
@ -416,6 +416,8 @@ public class P2PService implements SetupListener, MessageListener, ConnectionLis
});
} catch (CryptoException e) {
e.printStackTrace();
log.error(message.toString());
log.error(e.toString());
sendDirectMessageListener.onFault();
}
}
@ -450,8 +452,9 @@ public class P2PService implements SetupListener, MessageListener, ConnectionLis
"decryptedMsgWithPubKey.message=", decryptedMsgWithPubKey.message);
}
} catch (CryptoException e) {
log.trace("Decryption of SealedAndSignedMessage failed. " +
"That is expected if the message is not intended for us. " + e.getMessage());
log.info(e.toString());
log.info("Decryption of prefixedSealedAndSignedMessage.sealedAndSigned failed. " +
"That is expected if the message is not intended for us.");
}
} else {
log.info("Wrong blurredAddressHash. The message is not intended for us.");

View file

@ -20,5 +20,4 @@ package io.bitsquare.p2p.messaging;
import io.bitsquare.p2p.Message;
public interface DirectMessage extends Message {
}