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

Introduce OpenPgpv6Fingerprint

This commit is contained in:
Paul Schaub 2023-04-07 12:28:27 +02:00
parent e744668f5a
commit 46f7cfdb1a
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
7 changed files with 430 additions and 133 deletions

View file

@ -36,6 +36,9 @@ public abstract class OpenPgpFingerprint implements CharSequence, Comparable<Ope
if (key.getVersion() == 5) {
return new OpenPgpV5Fingerprint(key);
}
if (key.getVersion() == 6) {
return new OpenPgpV6Fingerprint(key);
}
throw new IllegalArgumentException("OpenPGP keys of version " + key.getVersion() + " are not supported.");
}
@ -52,10 +55,13 @@ public abstract class OpenPgpFingerprint implements CharSequence, Comparable<Ope
/**
* Try to parse an {@link OpenPgpFingerprint} from the given fingerprint string.
* If the trimmed fingerprint without whitespace is 64 characters long, it is either a v5 or v6 fingerprint.
* In this case, we return a {@link _64DigitFingerprint}. Since this is ambiguous, it is generally recommended
* to know the version of the key beforehand.
*
* @param fingerprint fingerprint
* @return parsed fingerprint
* @deprecated Use the parse() methods of the versioned fingerprint subclasses instead.
* @deprecated Use the constructor methods of the versioned fingerprint subclasses instead.
*/
@Deprecated
public static OpenPgpFingerprint parse(String fingerprint) {
@ -64,7 +70,8 @@ public abstract class OpenPgpFingerprint implements CharSequence, Comparable<Ope
return new OpenPgpV4Fingerprint(fp);
}
if (fp.matches("^[0-9A-F]{64}$")) {
return new OpenPgpV5Fingerprint(fp);
// Might be v5 or v6 :/
return new _64DigitFingerprint(fp);
}
throw new IllegalArgumentException("Fingerprint does not appear to match any known fingerprint patterns.");
}

View file

@ -4,21 +4,18 @@
package org.pgpainless.key;
import javax.annotation.Nonnull;
import org.bouncycastle.openpgp.PGPKeyRing;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.util.encoders.Hex;
import javax.annotation.Nonnull;
import java.nio.Buffer;
import java.nio.ByteBuffer;
/**
* This class represents a hex encoded, upper case OpenPGP v5 fingerprint.
*/
public class OpenPgpV5Fingerprint extends OpenPgpFingerprint {
public class OpenPgpV5Fingerprint extends _64DigitFingerprint {
/**
* Create an {@link OpenPgpV5Fingerprint}.
@ -30,7 +27,7 @@ public class OpenPgpV5Fingerprint extends OpenPgpFingerprint {
}
public OpenPgpV5Fingerprint(@Nonnull byte[] bytes) {
super(Hex.encode(bytes));
super(bytes);
}
public OpenPgpV5Fingerprint(@Nonnull PGPPublicKey key) {
@ -58,60 +55,4 @@ public class OpenPgpV5Fingerprint extends OpenPgpFingerprint {
return 5;
}
@Override
protected boolean isValid(@Nonnull String fp) {
return fp.matches("^[0-9A-F]{64}$");
}
@Override
public long getKeyId() {
byte[] bytes = Hex.decode(toString().getBytes(utf8));
ByteBuffer buf = ByteBuffer.wrap(bytes);
// The key id is the left-most 8 bytes (conveniently a long).
// We have to cast here in order to be compatible with java 8
// https://github.com/eclipse/jetty.project/issues/3244
((Buffer) buf).position(0);
return buf.getLong();
}
@Override
public String prettyPrint() {
String fp = toString();
StringBuilder pretty = new StringBuilder();
for (int i = 0; i < 4; i++) {
pretty.append(fp, i * 8, (i + 1) * 8).append(' ');
}
pretty.append(' ');
for (int i = 4; i < 7; i++) {
pretty.append(fp, i * 8, (i + 1) * 8).append(' ');
}
pretty.append(fp, 56, 64);
return pretty.toString();
}
@Override
public boolean equals(Object other) {
if (other == null) {
return false;
}
if (!(other instanceof CharSequence)) {
return false;
}
return this.toString().equals(other.toString());
}
@Override
public int hashCode() {
return toString().hashCode();
}
@Override
public int compareTo(OpenPgpFingerprint openPgpFingerprint) {
return toString().compareTo(openPgpFingerprint.toString());
}
}

View file

@ -0,0 +1,58 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.pgpainless.key;
import javax.annotation.Nonnull;
import org.bouncycastle.openpgp.PGPKeyRing;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
/**
* This class represents a hex encoded, upper case OpenPGP v6 fingerprint.
*/
public class OpenPgpV6Fingerprint extends _64DigitFingerprint {
/**
* Create an {@link OpenPgpV6Fingerprint}.
*
* @param fingerprint uppercase hexadecimal fingerprint of length 64
*/
public OpenPgpV6Fingerprint(@Nonnull String fingerprint) {
super(fingerprint);
}
public OpenPgpV6Fingerprint(@Nonnull byte[] bytes) {
super(bytes);
}
public OpenPgpV6Fingerprint(@Nonnull PGPPublicKey key) {
super(key);
}
public OpenPgpV6Fingerprint(@Nonnull PGPSecretKey key) {
this(key.getPublicKey());
}
public OpenPgpV6Fingerprint(@Nonnull PGPPublicKeyRing ring) {
super(ring);
}
public OpenPgpV6Fingerprint(@Nonnull PGPSecretKeyRing ring) {
super(ring);
}
public OpenPgpV6Fingerprint(@Nonnull PGPKeyRing ring) {
super(ring);
}
@Override
public int getVersion() {
return 6;
}
}

View file

@ -0,0 +1,119 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.pgpainless.key;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import javax.annotation.Nonnull;
import org.bouncycastle.openpgp.PGPKeyRing;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.util.encoders.Hex;
/**
* This class represents a hex encoded, upper case OpenPGP v5 or v6 fingerprint.
* Since both fingerprints use the same format, this class is used when parsing the fingerprint without knowing the
* key version.
*/
public class _64DigitFingerprint extends OpenPgpFingerprint {
/**
* Create an {@link _64DigitFingerprint}.
*
* @param fingerprint uppercase hexadecimal fingerprint of length 64
*/
protected _64DigitFingerprint(@Nonnull String fingerprint) {
super(fingerprint);
}
protected _64DigitFingerprint(@Nonnull byte[] bytes) {
super(Hex.encode(bytes));
}
protected _64DigitFingerprint(@Nonnull PGPPublicKey key) {
super(key);
}
protected _64DigitFingerprint(@Nonnull PGPSecretKey key) {
this(key.getPublicKey());
}
protected _64DigitFingerprint(@Nonnull PGPPublicKeyRing ring) {
super(ring);
}
protected _64DigitFingerprint(@Nonnull PGPSecretKeyRing ring) {
super(ring);
}
protected _64DigitFingerprint(@Nonnull PGPKeyRing ring) {
super(ring);
}
@Override
public int getVersion() {
return -1; // might be v5 or v6
}
@Override
protected boolean isValid(@Nonnull String fp) {
return fp.matches("^[0-9A-F]{64}$");
}
@Override
public long getKeyId() {
byte[] bytes = Hex.decode(toString().getBytes(utf8));
ByteBuffer buf = ByteBuffer.wrap(bytes);
// The key id is the left-most 8 bytes (conveniently a long).
// We have to cast here in order to be compatible with java 8
// https://github.com/eclipse/jetty.project/issues/3244
((Buffer) buf).position(0);
return buf.getLong();
}
@Override
public String prettyPrint() {
String fp = toString();
StringBuilder pretty = new StringBuilder();
for (int i = 0; i < 4; i++) {
pretty.append(fp, i * 8, (i + 1) * 8).append(' ');
}
pretty.append(' ');
for (int i = 4; i < 7; i++) {
pretty.append(fp, i * 8, (i + 1) * 8).append(' ');
}
pretty.append(fp, 56, 64);
return pretty.toString();
}
@Override
public boolean equals(Object other) {
if (other == null) {
return false;
}
if (!(other instanceof CharSequence)) {
return false;
}
return this.toString().equals(other.toString());
}
@Override
public int hashCode() {
return toString().hashCode();
}
@Override
public int compareTo(OpenPgpFingerprint openPgpFingerprint) {
return toString().compareTo(openPgpFingerprint.toString());
}
}