From 32c0d9eb1cce9fdf97137617f32c011fa7851363 Mon Sep 17 00:00:00 2001 From: Tor Brede Vekterli Date: Tue, 1 Nov 2022 16:22:21 +0100 Subject: Add simple token info dumping tool Dumps key version, ID and HPKE components --- .../java/com/yahoo/vespa/security/tool/Main.java | 3 +- .../vespa/security/tool/crypto/KeygenTool.java | 11 ++++- .../vespa/security/tool/crypto/TokenInfoTool.java | 56 ++++++++++++++++++++++ 3 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 vespaclient-java/src/main/java/com/yahoo/vespa/security/tool/crypto/TokenInfoTool.java (limited to 'vespaclient-java/src/main/java/com/yahoo') diff --git a/vespaclient-java/src/main/java/com/yahoo/vespa/security/tool/Main.java b/vespaclient-java/src/main/java/com/yahoo/vespa/security/tool/Main.java index 7ca98e4b9ba..11bd8815d77 100644 --- a/vespaclient-java/src/main/java/com/yahoo/vespa/security/tool/Main.java +++ b/vespaclient-java/src/main/java/com/yahoo/vespa/security/tool/Main.java @@ -4,6 +4,7 @@ package com.yahoo.vespa.security.tool; import com.yahoo.vespa.security.tool.crypto.DecryptTool; import com.yahoo.vespa.security.tool.crypto.EncryptTool; import com.yahoo.vespa.security.tool.crypto.KeygenTool; +import com.yahoo.vespa.security.tool.crypto.TokenInfoTool; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.DefaultParser; @@ -44,7 +45,7 @@ public class Main { } private static final List TOOLS = List.of( - new KeygenTool(), new EncryptTool(), new DecryptTool()); + new KeygenTool(), new EncryptTool(), new DecryptTool(), new TokenInfoTool()); private static Optional toolFromCliArgs(String[] args) { if (args.length == 0) { diff --git a/vespaclient-java/src/main/java/com/yahoo/vespa/security/tool/crypto/KeygenTool.java b/vespaclient-java/src/main/java/com/yahoo/vespa/security/tool/crypto/KeygenTool.java index a0b9cce710b..d7885dc6455 100644 --- a/vespaclient-java/src/main/java/com/yahoo/vespa/security/tool/crypto/KeygenTool.java +++ b/vespaclient-java/src/main/java/com/yahoo/vespa/security/tool/crypto/KeygenTool.java @@ -64,8 +64,14 @@ public class KeygenTool implements Tool { OPTIONS); } - private void handleExistingFileIfAny(Path filePath, boolean allowOverwrite) throws IOException { - if (filePath.toFile().exists()) { + private static void verifyNotSameKeyPaths(Path privPath, Path pubPath) { + if (privPath.equals(pubPath)) { + throw new IllegalArgumentException("Private and public key output files must be different"); + } + } + + private static void handleExistingFileIfAny(Path filePath, boolean allowOverwrite) throws IOException { + if (Files.exists(filePath)) { if (!allowOverwrite) { throw new IllegalArgumentException(("Output file '%s' already exists. No keys written. " + "If you want to overwrite existing files, specify --%s.") @@ -83,6 +89,7 @@ public class KeygenTool implements Tool { var arguments = invocation.arguments(); var privOutPath = Paths.get(CliUtils.optionOrThrow(arguments, PRIVATE_OUT_FILE_OPTION)); var pubOutPath = Paths.get(CliUtils.optionOrThrow(arguments, PUBLIC_OUT_FILE_OPTION)); + verifyNotSameKeyPaths(privOutPath, pubOutPath); boolean allowOverwrite = arguments.hasOption(OVERWRITE_EXISTING_OPTION); handleExistingFileIfAny(privOutPath, allowOverwrite); diff --git a/vespaclient-java/src/main/java/com/yahoo/vespa/security/tool/crypto/TokenInfoTool.java b/vespaclient-java/src/main/java/com/yahoo/vespa/security/tool/crypto/TokenInfoTool.java new file mode 100644 index 00000000000..dc597e9301f --- /dev/null +++ b/vespaclient-java/src/main/java/com/yahoo/vespa/security/tool/crypto/TokenInfoTool.java @@ -0,0 +1,56 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.vespa.security.tool.crypto; + +import com.yahoo.security.SealedSharedKey; +import com.yahoo.text.StringUtilities; +import com.yahoo.vespa.security.tool.Tool; +import com.yahoo.vespa.security.tool.ToolDescription; +import com.yahoo.vespa.security.tool.ToolInvocation; +import org.apache.commons.cli.Option; + +import java.util.List; + +import static com.yahoo.security.ArrayUtils.fromUtf8Bytes; +import static com.yahoo.security.ArrayUtils.hex; + +/** + * Tooling to dump the various components of a decryption token + * + * @author vekterli + */ +public class TokenInfoTool implements Tool { + + private static final List