1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-25 13:52:06 +01:00

Add support for SOP05 features

This commit is contained in:
Paul Schaub 2023-04-14 14:31:48 +02:00
parent 44608744c2
commit e35287a666
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
4 changed files with 82 additions and 3 deletions

View file

@ -8,18 +8,22 @@ import java.io.IOException;
import java.io.OutputStream;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import org.bouncycastle.bcpg.ArmoredOutputStream;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.pgpainless.PGPainless;
import org.pgpainless.key.generation.type.rsa.RsaLength;
import org.pgpainless.key.modification.secretkeyring.SecretKeyRingEditorInterface;
import org.pgpainless.key.protection.SecretKeyRingProtector;
import org.pgpainless.util.ArmorUtils;
import org.pgpainless.util.Passphrase;
import sop.Profile;
import sop.Ready;
import sop.exception.SOPGPException;
import sop.operation.GenerateKey;
@ -29,9 +33,16 @@ import sop.operation.GenerateKey;
*/
public class GenerateKeyImpl implements GenerateKey {
public static final Profile DEFAULT_PROFILE = new Profile("default", "Generate keys based on XDH and EdDSA");
public static final Profile RSA3072_PROFILE = new Profile("rfc4880-rsa3072@pgpainless.org", "Generate 3072-bit RSA keys");
public static final Profile RSA4096_PROFILE = new Profile("rfc4880-rsa4096@pgpainless.org", "Generate 4096-bit RSA keys");
public static final List<Profile> SUPPORTED_PROFILES = Arrays.asList(DEFAULT_PROFILE, RSA3072_PROFILE, RSA4096_PROFILE);
private boolean armor = true;
private final Set<String> userIds = new LinkedHashSet<>();
private Passphrase passphrase = Passphrase.emptyPassphrase();
private String profile = DEFAULT_PROFILE.getName();
@Override
public GenerateKey noArmor() {
@ -51,6 +62,18 @@ public class GenerateKeyImpl implements GenerateKey {
return this;
}
@Override
public GenerateKey profile(String profileName) {
for (Profile profile : SUPPORTED_PROFILES) {
if (profile.getName().equals(profileName)) {
this.profile = profileName;
return this;
}
}
throw new SOPGPException.UnsupportedProfile("generate-key", profileName);
}
@Override
public Ready generate() throws SOPGPException.MissingArg, SOPGPException.UnsupportedAsymmetricAlgo {
Iterator<String> userIdIterator = userIds.iterator();
@ -58,8 +81,7 @@ public class GenerateKeyImpl implements GenerateKey {
PGPSecretKeyRing key;
try {
String primaryUserId = userIdIterator.hasNext() ? userIdIterator.next() : null;
key = PGPainless.generateKeyRing()
.modernKeyRing(primaryUserId, passphrase);
key = generateKeyWithProfile(profile, primaryUserId, passphrase);
if (userIdIterator.hasNext()) {
SecretKeyRingEditorInterface editor = PGPainless.modifyKeyRing(key);
@ -90,4 +112,26 @@ public class GenerateKeyImpl implements GenerateKey {
throw new RuntimeException(e);
}
}
private PGPSecretKeyRing generateKeyWithProfile(String profile, String primaryUserId, Passphrase passphrase)
throws PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException {
PGPSecretKeyRing key;
// XDH + EdDSA
if (profile.equals(DEFAULT_PROFILE.getName())) {
key = PGPainless.generateKeyRing()
.modernKeyRing(primaryUserId, passphrase);
}
else if (profile.equals(RSA3072_PROFILE.getName())) {
key = PGPainless.generateKeyRing()
.simpleRsaKeyRing(primaryUserId, RsaLength._3072, passphrase);
}
else if (profile.equals(RSA4096_PROFILE.getName())) {
key = PGPainless.generateKeyRing()
.simpleRsaKeyRing(primaryUserId, RsaLength._4096, passphrase);
}
else {
throw new SOPGPException.UnsupportedProfile("generate-key", profile);
}
return key;
}
}

View file

@ -0,0 +1,29 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.pgpainless.sop;
import java.util.List;
import sop.Profile;
import sop.exception.SOPGPException;
import sop.operation.ListProfiles;
public class ListProfilesImpl implements ListProfiles {
@Override
public List<Profile> subcommand(String command) {
if (command == null) {
throw new SOPGPException.UnsupportedProfile("null");
}
switch (command) {
case "generate-key":
return GenerateKeyImpl.SUPPORTED_PROFILES;
default:
throw new SOPGPException.UnsupportedProfile(command);
}
}
}

View file

@ -17,6 +17,7 @@ import sop.operation.ExtractCert;
import sop.operation.GenerateKey;
import sop.operation.InlineSign;
import sop.operation.InlineVerify;
import sop.operation.ListProfiles;
import sop.operation.Version;
/**
@ -96,6 +97,11 @@ public class SOPImpl implements SOP {
return new DearmorImpl();
}
@Override
public ListProfiles listProfiles() {
return new ListProfilesImpl();
}
@Override
public InlineDetach inlineDetach() {
return new InlineDetachImpl();

View file

@ -18,6 +18,6 @@ allprojects {
logbackVersion = '1.2.11'
mockitoVersion = '4.5.1'
slf4jVersion = '1.7.36'
sopJavaVersion = '4.1.1'
sopJavaVersion = '5.0.0-SNAPSHOT'
}
}