diff --git a/besu/src/main/java/org/hyperledger/besu/chainimport/internal/TransactionData.java b/besu/src/main/java/org/hyperledger/besu/chainimport/internal/TransactionData.java index 6dba8fac76..1a46e337ad 100644 --- a/besu/src/main/java/org/hyperledger/besu/chainimport/internal/TransactionData.java +++ b/besu/src/main/java/org/hyperledger/besu/chainimport/internal/TransactionData.java @@ -56,7 +56,7 @@ public class TransactionData { } public Transaction getSignedTransaction(final NonceProvider nonceProvider) { - KeyPair keyPair = KeyPair.create(privateKey); + final KeyPair keyPair = KeyPair.create(privateKey); final Address fromAddress = Address.extract(keyPair.getPublicKey()); final long nonce = nonceProvider.get(fromAddress); diff --git a/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java b/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java index b47885e14f..18da99d3a6 100644 --- a/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java +++ b/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java @@ -1374,7 +1374,15 @@ public class BesuCommand implements DefaultCommandValues, Runnable { privacyParametersBuilder.setEnabled(true); privacyParametersBuilder.setEnclaveUrl(privacyUrl); if (privacyPublicKeyFile() != null) { - privacyParametersBuilder.setEnclavePublicKeyUsingFile(privacyPublicKeyFile()); + try { + privacyParametersBuilder.setEnclavePublicKeyUsingFile(privacyPublicKeyFile()); + } catch (final IOException e) { + throw new ParameterException( + commandLine, "Problem with privacy-public-key-file: " + e.getMessage(), e); + } catch (final IllegalArgumentException e) { + throw new ParameterException( + commandLine, "Contents of privacy-public-key-file invalid: " + e.getMessage(), e); + } } else { throw new ParameterException( commandLine, "Please specify Enclave public key file path to enable privacy"); diff --git a/besu/src/main/java/org/hyperledger/besu/cli/converter/FractionConverter.java b/besu/src/main/java/org/hyperledger/besu/cli/converter/FractionConverter.java index bed2586972..e3fc064891 100644 --- a/besu/src/main/java/org/hyperledger/besu/cli/converter/FractionConverter.java +++ b/besu/src/main/java/org/hyperledger/besu/cli/converter/FractionConverter.java @@ -25,7 +25,7 @@ public class FractionConverter implements CommandLine.ITypeConverter { public Float convert(final String value) throws FractionConversionException { try { return Fraction.fromString(value).getValue(); - } catch (NullPointerException | IllegalArgumentException e) { + } catch (final NullPointerException | IllegalArgumentException e) { throw new FractionConversionException(value); } } diff --git a/besu/src/main/java/org/hyperledger/besu/cli/util/CommandLineUtils.java b/besu/src/main/java/org/hyperledger/besu/cli/util/CommandLineUtils.java index 31ba838eaf..7c61d60fd8 100644 --- a/besu/src/main/java/org/hyperledger/besu/cli/util/CommandLineUtils.java +++ b/besu/src/main/java/org/hyperledger/besu/cli/util/CommandLineUtils.java @@ -48,7 +48,7 @@ public class CommandLineUtils { final boolean isMainOptionCondition, final List dependentOptionsNames) { if (isMainOptionCondition) { - String affectedOptions = + final String affectedOptions = commandLine.getCommandSpec().options().stream() .filter( option -> diff --git a/besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java b/besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java index 70b297b648..ed1ba0b5af 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java @@ -3138,4 +3138,35 @@ public class BesuCommandTest extends CommandTestAbstract { assertThat(commandOutput.toString()).isEmpty(); assertThat(commandErrorOutput.toString()).isEmpty(); } + + @Test + public void privEnclaveKeyFileDoesNotExist() { + parseCommand("--privacy-enabled=true", "--privacy-public-key-file", "/non/existent/file"); + + assertThat(commandOutput.toString()).isEmpty(); + assertThat(commandErrorOutput.toString()).startsWith("Problem with privacy-public-key-file"); + assertThat(commandErrorOutput.toString()).contains("No such file"); + } + + @Test + public void privEnclaveKeyFileInvalidContentTooShort() throws IOException { + final Path file = createTempFile("privacy.key", "lkjashdfiluhwelrk"); + parseCommand("--privacy-enabled=true", "--privacy-public-key-file", file.toString()); + + assertThat(commandOutput.toString()).isEmpty(); + assertThat(commandErrorOutput.toString()) + .startsWith("Contents of privacy-public-key-file invalid"); + assertThat(commandErrorOutput.toString()).contains("needs to be 44 characters long"); + } + + @Test + public void privEnclaveKeyFileInvalidContentNotValidBase64() throws IOException { + final Path file = createTempFile("privacy.key", "l*jashdfillk9ashdfillkjashdfillkjashdfilrtg="); + parseCommand("--privacy-enabled=true", "--privacy-public-key-file", file.toString()); + + assertThat(commandOutput.toString()).isEmpty(); + assertThat(commandErrorOutput.toString()) + .startsWith("Contents of privacy-public-key-file invalid"); + assertThat(commandErrorOutput.toString()).contains("Illegal base64 character"); + } } diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/PrivacyParameters.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/PrivacyParameters.java index d8d3c23e0f..5f37983386 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/PrivacyParameters.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/PrivacyParameters.java @@ -30,6 +30,7 @@ import java.io.File; import java.io.IOException; import java.net.URI; import java.nio.file.Path; +import java.util.Base64; import java.util.Optional; import com.google.common.io.Files; @@ -209,7 +210,17 @@ public class PrivacyParameters { public Builder setEnclavePublicKeyUsingFile(final File publicKeyFile) throws IOException { this.enclavePublicKeyFile = publicKeyFile; this.enclavePublicKey = Files.asCharSource(publicKeyFile, UTF_8).read(); + validatePublicKey(publicKeyFile); return this; } + + private void validatePublicKey(final File publicKeyFile) { + if (publicKeyFile.length() != 44) { + throw new IllegalArgumentException( + "Contents of enclave public key file needs to be 44 characters long to decode to a valid 32 byte public key."); + } + // throws exception if invalid base 64 + Base64.getDecoder().decode(this.enclavePublicKey); + } } }