From fb1c0d6ff3c61619beca9c3346ec54fb416a73d4 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Fri, 5 Mar 2021 13:32:04 +0100 Subject: [PATCH] SOP: Allow for multiple user-ids during key generation --- .../pgpainless/sop/commands/GenerateKey.java | 42 ++++++++++++++++++- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/pgpainless-sop/src/main/java/org/pgpainless/sop/commands/GenerateKey.java b/pgpainless-sop/src/main/java/org/pgpainless/sop/commands/GenerateKey.java index 66dae486..99618235 100644 --- a/pgpainless-sop/src/main/java/org/pgpainless/sop/commands/GenerateKey.java +++ b/pgpainless-sop/src/main/java/org/pgpainless/sop/commands/GenerateKey.java @@ -18,10 +18,20 @@ package org.pgpainless.sop.commands; import java.io.IOException; import java.security.InvalidAlgorithmParameterException; import java.security.NoSuchAlgorithmException; +import java.util.Calendar; +import java.util.Date; +import java.util.List; +import java.util.TimeZone; import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPSecretKeyRing; import org.pgpainless.PGPainless; +import org.pgpainless.algorithm.KeyFlag; +import org.pgpainless.key.generation.KeyRingBuilderInterface; +import org.pgpainless.key.generation.KeySpec; +import org.pgpainless.key.generation.type.KeyType; +import org.pgpainless.key.generation.type.eddsa.EdDSACurve; +import org.pgpainless.key.generation.type.xdh.XDHCurve; import org.pgpainless.sop.Print; import picocli.CommandLine; @@ -37,12 +47,40 @@ public class GenerateKey implements Runnable { boolean armor = true; @CommandLine.Parameters(description = "User-ID, eg. \"Alice \"") - String userId; + List userId; @Override public void run() { try { - PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing().simpleEcKeyRing(userId); + KeyRingBuilderInterface.WithAdditionalUserIdOrPassphrase builder = PGPainless.generateKeyRing() + .withSubKey(KeySpec.getBuilder(KeyType.EDDSA(EdDSACurve._Ed25519)) + .withKeyFlags(KeyFlag.SIGN_DATA) + .withDefaultAlgorithms()) + .withSubKey(KeySpec.getBuilder(KeyType.XDH(XDHCurve._X25519)) + .withKeyFlags(KeyFlag.ENCRYPT_COMMS, KeyFlag.ENCRYPT_STORAGE) + .withDefaultAlgorithms()) + .withPrimaryKey(KeySpec.getBuilder(KeyType.EDDSA(EdDSACurve._Ed25519)) + .withKeyFlags(KeyFlag.CERTIFY_OTHER) + .withDefaultAlgorithms()) + .withPrimaryUserId(userId.get(0)); + + if (userId.isEmpty()) { + print_ln("At least one user-id expected."); + System.exit(1); + return; + } + + for (int i = 1; i < userId.size(); i++) { + builder.withAdditionalUserId(userId.get(i)); + } + + Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); + calendar.add(Calendar.YEAR, 3); + Date expiration = calendar.getTime(); + + PGPSecretKeyRing secretKeys = builder.setExpirationDate(expiration) + .withoutPassphrase() + .build(); print_ln(Print.toString(secretKeys.getEncoded(), armor));