mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-11-25 22:02:05 +01:00
Add support for SOP05 features
This commit is contained in:
parent
44608744c2
commit
e35287a666
4 changed files with 82 additions and 3 deletions
|
@ -8,18 +8,22 @@ import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.security.InvalidAlgorithmParameterException;
|
import java.security.InvalidAlgorithmParameterException;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.bouncycastle.bcpg.ArmoredOutputStream;
|
import org.bouncycastle.bcpg.ArmoredOutputStream;
|
||||||
import org.bouncycastle.openpgp.PGPException;
|
import org.bouncycastle.openpgp.PGPException;
|
||||||
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
||||||
import org.pgpainless.PGPainless;
|
import org.pgpainless.PGPainless;
|
||||||
|
import org.pgpainless.key.generation.type.rsa.RsaLength;
|
||||||
import org.pgpainless.key.modification.secretkeyring.SecretKeyRingEditorInterface;
|
import org.pgpainless.key.modification.secretkeyring.SecretKeyRingEditorInterface;
|
||||||
import org.pgpainless.key.protection.SecretKeyRingProtector;
|
import org.pgpainless.key.protection.SecretKeyRingProtector;
|
||||||
import org.pgpainless.util.ArmorUtils;
|
import org.pgpainless.util.ArmorUtils;
|
||||||
import org.pgpainless.util.Passphrase;
|
import org.pgpainless.util.Passphrase;
|
||||||
|
import sop.Profile;
|
||||||
import sop.Ready;
|
import sop.Ready;
|
||||||
import sop.exception.SOPGPException;
|
import sop.exception.SOPGPException;
|
||||||
import sop.operation.GenerateKey;
|
import sop.operation.GenerateKey;
|
||||||
|
@ -29,9 +33,16 @@ import sop.operation.GenerateKey;
|
||||||
*/
|
*/
|
||||||
public class GenerateKeyImpl implements 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 boolean armor = true;
|
||||||
private final Set<String> userIds = new LinkedHashSet<>();
|
private final Set<String> userIds = new LinkedHashSet<>();
|
||||||
private Passphrase passphrase = Passphrase.emptyPassphrase();
|
private Passphrase passphrase = Passphrase.emptyPassphrase();
|
||||||
|
private String profile = DEFAULT_PROFILE.getName();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public GenerateKey noArmor() {
|
public GenerateKey noArmor() {
|
||||||
|
@ -51,6 +62,18 @@ public class GenerateKeyImpl implements GenerateKey {
|
||||||
return this;
|
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
|
@Override
|
||||||
public Ready generate() throws SOPGPException.MissingArg, SOPGPException.UnsupportedAsymmetricAlgo {
|
public Ready generate() throws SOPGPException.MissingArg, SOPGPException.UnsupportedAsymmetricAlgo {
|
||||||
Iterator<String> userIdIterator = userIds.iterator();
|
Iterator<String> userIdIterator = userIds.iterator();
|
||||||
|
@ -58,8 +81,7 @@ public class GenerateKeyImpl implements GenerateKey {
|
||||||
PGPSecretKeyRing key;
|
PGPSecretKeyRing key;
|
||||||
try {
|
try {
|
||||||
String primaryUserId = userIdIterator.hasNext() ? userIdIterator.next() : null;
|
String primaryUserId = userIdIterator.hasNext() ? userIdIterator.next() : null;
|
||||||
key = PGPainless.generateKeyRing()
|
key = generateKeyWithProfile(profile, primaryUserId, passphrase);
|
||||||
.modernKeyRing(primaryUserId, passphrase);
|
|
||||||
|
|
||||||
if (userIdIterator.hasNext()) {
|
if (userIdIterator.hasNext()) {
|
||||||
SecretKeyRingEditorInterface editor = PGPainless.modifyKeyRing(key);
|
SecretKeyRingEditorInterface editor = PGPainless.modifyKeyRing(key);
|
||||||
|
@ -90,4 +112,26 @@ public class GenerateKeyImpl implements GenerateKey {
|
||||||
throw new RuntimeException(e);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -17,6 +17,7 @@ import sop.operation.ExtractCert;
|
||||||
import sop.operation.GenerateKey;
|
import sop.operation.GenerateKey;
|
||||||
import sop.operation.InlineSign;
|
import sop.operation.InlineSign;
|
||||||
import sop.operation.InlineVerify;
|
import sop.operation.InlineVerify;
|
||||||
|
import sop.operation.ListProfiles;
|
||||||
import sop.operation.Version;
|
import sop.operation.Version;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -96,6 +97,11 @@ public class SOPImpl implements SOP {
|
||||||
return new DearmorImpl();
|
return new DearmorImpl();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ListProfiles listProfiles() {
|
||||||
|
return new ListProfilesImpl();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public InlineDetach inlineDetach() {
|
public InlineDetach inlineDetach() {
|
||||||
return new InlineDetachImpl();
|
return new InlineDetachImpl();
|
||||||
|
|
|
@ -18,6 +18,6 @@ allprojects {
|
||||||
logbackVersion = '1.2.11'
|
logbackVersion = '1.2.11'
|
||||||
mockitoVersion = '4.5.1'
|
mockitoVersion = '4.5.1'
|
||||||
slf4jVersion = '1.7.36'
|
slf4jVersion = '1.7.36'
|
||||||
sopJavaVersion = '4.1.1'
|
sopJavaVersion = '5.0.0-SNAPSHOT'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue