1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-01-09 19:57:57 +01:00

Prevent additional user-id from being equal to primary user-id

This commit is contained in:
Paul Schaub 2020-10-20 22:14:56 +02:00
parent 2f85c9a8d0
commit 9670e5ecb9
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
2 changed files with 9 additions and 5 deletions

View file

@ -24,11 +24,12 @@ import java.security.NoSuchAlgorithmException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.Set;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import org.bouncycastle.asn1.ocsp.Signature;
import org.bouncycastle.bcpg.SignatureSubpacket;
import org.bouncycastle.openpgp.PGPEncryptedData; import org.bouncycastle.openpgp.PGPEncryptedData;
import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPKeyPair; import org.bouncycastle.openpgp.PGPKeyPair;
@ -45,7 +46,6 @@ import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor;
import org.bouncycastle.openpgp.operator.PBESecretKeyEncryptor; import org.bouncycastle.openpgp.operator.PBESecretKeyEncryptor;
import org.bouncycastle.openpgp.operator.PGPContentSignerBuilder; import org.bouncycastle.openpgp.operator.PGPContentSignerBuilder;
import org.bouncycastle.openpgp.operator.PGPDigestCalculator; import org.bouncycastle.openpgp.operator.PGPDigestCalculator;
import org.bouncycastle.openpgp.operator.bc.BcPGPContentSignerBuilder;
import org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder; import org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder;
import org.bouncycastle.openpgp.operator.jcajce.JcaPGPDigestCalculatorProviderBuilder; import org.bouncycastle.openpgp.operator.jcajce.JcaPGPDigestCalculatorProviderBuilder;
import org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyPair; import org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyPair;
@ -53,7 +53,6 @@ import org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyDecryptorBuilder;
import org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyEncryptorBuilder; import org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyEncryptorBuilder;
import org.pgpainless.algorithm.HashAlgorithm; import org.pgpainless.algorithm.HashAlgorithm;
import org.pgpainless.algorithm.KeyFlag; import org.pgpainless.algorithm.KeyFlag;
import org.pgpainless.decryption_verification.OpenPgpMetadata;
import org.pgpainless.key.collection.PGPKeyRing; import org.pgpainless.key.collection.PGPKeyRing;
import org.pgpainless.key.generation.type.ECDH; import org.pgpainless.key.generation.type.ECDH;
import org.pgpainless.key.generation.type.ECDSA; import org.pgpainless.key.generation.type.ECDSA;
@ -70,7 +69,7 @@ public class KeyRingBuilder implements KeyRingBuilderInterface {
private List<KeySpec> keySpecs = new ArrayList<>(); private List<KeySpec> keySpecs = new ArrayList<>();
private String userId; private String userId;
private List<String> additionalUserIds = new ArrayList<>(); private Set<String> additionalUserIds = new LinkedHashSet<>();
private Passphrase passphrase; private Passphrase passphrase;
/** /**
@ -216,6 +215,9 @@ public class KeyRingBuilder implements KeyRingBuilderInterface {
@Override @Override
public WithAdditionalUserIdOrPassphrase withAdditionalUserId(@Nonnull String userId) { public WithAdditionalUserIdOrPassphrase withAdditionalUserId(@Nonnull String userId) {
if (Objects.equals(KeyRingBuilder.this.userId, userId)) {
throw new IllegalArgumentException("Additional user-id MUST NOT be equal to primary user-id.");
}
KeyRingBuilder.this.additionalUserIds.add(userId); KeyRingBuilder.this.additionalUserIds.add(userId);
return this; return this;
} }

View file

@ -27,12 +27,14 @@ public class GenerateKeyWithAdditionalUserIdTest {
.withDefaultAlgorithms()) .withDefaultAlgorithms())
.withPrimaryUserId("primary@user.id") .withPrimaryUserId("primary@user.id")
.withAdditionalUserId("additional@user.id") .withAdditionalUserId("additional@user.id")
.withAdditionalUserId("additional2@user.id")
.withoutPassphrase() .withoutPassphrase()
.build(); .build();
Iterator<String> userIds = keyRing.getPublicKeys().getPublicKey().getUserIDs(); Iterator<String> userIds = keyRing.getPublicKeys().getPublicKey().getUserIDs();
assertEquals("primary@user.id", userIds.next()); assertEquals("primary@user.id", userIds.next());
assertEquals("additional@user.id", userIds.next()); assertEquals("additional@user.id", userIds.next());
assertEquals("additional2@user.id", userIds.next());
assertFalse(userIds.hasNext()); assertFalse(userIds.hasNext());
ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); ByteArrayOutputStream byteOut = new ByteArrayOutputStream();