1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-23 04:42:06 +01:00

SubkeyIdentifier: Throw NoSuchElementException for non-existent subkey

This commit is contained in:
Paul Schaub 2021-08-01 17:23:17 +02:00
parent 1327e08ac3
commit e4fdc3bc1e
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
2 changed files with 15 additions and 1 deletions

View file

@ -15,9 +15,11 @@
*/
package org.pgpainless.key;
import java.util.NoSuchElementException;
import javax.annotation.Nonnull;
import org.bouncycastle.openpgp.PGPKeyRing;
import org.bouncycastle.openpgp.PGPPublicKey;
/**
* Tuple class used to identify a subkey by fingerprints of the primary key of the subkeys key ring,
@ -47,7 +49,12 @@ public class SubkeyIdentifier {
* @param keyId keyid of the subkey
*/
public SubkeyIdentifier(@Nonnull PGPKeyRing keyRing, long keyId) {
this(new OpenPgpV4Fingerprint(keyRing.getPublicKey()), new OpenPgpV4Fingerprint(keyRing.getPublicKey(keyId)));
PGPPublicKey subkey = keyRing.getPublicKey(keyId);
if (subkey == null) {
throw new NoSuchElementException("Key ring does not contain subkey with id " + Long.toHexString(keyId));
}
this.primaryKeyFingerprint = new OpenPgpV4Fingerprint(keyRing);
this.subkeyFingerprint = new OpenPgpV4Fingerprint(subkey);
}
public SubkeyIdentifier(@Nonnull PGPKeyRing keyRing, @Nonnull OpenPgpV4Fingerprint subkeyFingerprint) {

View file

@ -17,8 +17,10 @@ package org.pgpainless.key;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.IOException;
import java.util.NoSuchElementException;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.junit.jupiter.api.BeforeAll;
@ -110,4 +112,9 @@ public class SubkeyIdentifierTest {
assertNotEquals(id1, PRIMARY_FP);
assertNotEquals(id1, null);
}
@Test
public void nonExistentSubkeyThrowsNoSuchElementException() {
assertThrows(NoSuchElementException.class, () -> new SubkeyIdentifier(CERT, 123));
}
}