From ee1524c186775fd2ab132a75771863649aebc2f1 Mon Sep 17 00:00:00 2001 From: Ameziane H Date: Mon, 10 Oct 2022 13:27:30 +0200 Subject: [PATCH 1/4] During handshake, flip the encrypted message decryption by starting with the new format (EIP-8), and if there is an exception, try the old format. This will reduce the number of exceptions and unnecessary executions. Signed-off-by: Ameziane H --- CHANGELOG.md | 1 + .../p2p/rlpx/handshake/ecies/ECIESHandshaker.java | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d5028bdbb72..86729ab5d9e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * For the EC encryptor, the encoded public key length is 91 ### Additions and Improvements +- Reduce the number of runtime exceptions (SecurityModuleException) and unnecessary executions during ECIES handshake. ### Bug Fixes - Corrects emission of blockadded events when rewinding during a re-org. Fix for [#4495](https://github.com/hyperledger/besu/issues/4495) diff --git a/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/rlpx/handshake/ecies/ECIESHandshaker.java b/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/rlpx/handshake/ecies/ECIESHandshaker.java index 5282ed62b07..d5620e9fe3c 100644 --- a/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/rlpx/handshake/ecies/ECIESHandshaker.java +++ b/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/rlpx/handshake/ecies/ECIESHandshaker.java @@ -189,9 +189,6 @@ public Optional handleMessage(final ByteBuf buf) throws HandshakeExcept try { // Decrypt the message with our private key. try { - bytes = EncryptedMessage.decryptMsg(bytes, nodeKey); - version4 = false; - } catch (final Exception ex) { // Assume new format final int size = bufferedBytes.readUnsignedShort(); if (buf.writerIndex() >= size) { @@ -203,8 +200,11 @@ public Optional handleMessage(final ByteBuf buf) throws HandshakeExcept bytes = EncryptedMessage.decryptMsgEIP8(encryptedMsg, nodeKey); version4 = true; } else { - throw new HandshakeException("Failed to decrypt handshake message", ex); + throw new HandshakeException("Failed to decrypt handshake message"); } + } catch (final Exception ex) { + bytes = EncryptedMessage.decryptMsg(bytes, nodeKey); + version4 = false; } } catch (final InvalidCipherTextException e) { status.set(Handshaker.HandshakeStatus.FAILED); From e4b1101629c71950ff79a25585cca3691902933e Mon Sep 17 00:00:00 2001 From: Ameziane H Date: Wed, 12 Oct 2022 10:22:26 +0200 Subject: [PATCH 2/4] update CHANGELOG.md to give more context on this PR. Signed-off-by: Ameziane H --- .../besu/ethereum/core/encoding/TransactionDecoder.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/encoding/TransactionDecoder.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/encoding/TransactionDecoder.java index 0f203ce331a..d38c2d381ac 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/encoding/TransactionDecoder.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/encoding/TransactionDecoder.java @@ -42,8 +42,11 @@ import com.google.common.base.Suppliers; import com.google.common.collect.ImmutableMap; import org.apache.tuweni.bytes.Bytes; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class TransactionDecoder { + private static final Logger LOG = LoggerFactory.getLogger(TransactionDecoder.class); @FunctionalInterface interface Decoder { From 49117969173fb570eb691279d9adb3e4173158fe Mon Sep 17 00:00:00 2001 From: Ameziane H Date: Wed, 12 Oct 2022 10:36:55 +0200 Subject: [PATCH 3/4] update CHANGELOG.md to give more context on this PR. Signed-off-by: Ameziane H --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 86729ab5d9e..dcd9974ee61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ * For the EC encryptor, the encoded public key length is 91 ### Additions and Improvements -- Reduce the number of runtime exceptions (SecurityModuleException) and unnecessary executions during ECIES handshake. +- Reduce the number of runtime exceptions (SecurityModuleException) and unnecessary executions during ECIES handshake, by trying to decrypt EIP-8 formatted messages first. ### Bug Fixes - Corrects emission of blockadded events when rewinding during a re-org. Fix for [#4495](https://github.com/hyperledger/besu/issues/4495) From e5d975d4397b9c86d08eaad884ebd85768f81573 Mon Sep 17 00:00:00 2001 From: Ameziane H Date: Wed, 12 Oct 2022 15:50:00 +0200 Subject: [PATCH 4/4] Delete some debug code committed by error Signed-off-by: Ameziane H --- .../besu/ethereum/core/encoding/TransactionDecoder.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/encoding/TransactionDecoder.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/encoding/TransactionDecoder.java index d38c2d381ac..0f203ce331a 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/encoding/TransactionDecoder.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/encoding/TransactionDecoder.java @@ -42,11 +42,8 @@ import com.google.common.base.Suppliers; import com.google.common.collect.ImmutableMap; import org.apache.tuweni.bytes.Bytes; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; public class TransactionDecoder { - private static final Logger LOG = LoggerFactory.getLogger(TransactionDecoder.class); @FunctionalInterface interface Decoder {