1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-26 22:32:07 +01:00

Add support for OpenPGP v5 fingerprints.

Obviously we need support for key.getFingerprint() in BC, but once
that is there, this should magically start working.
This commit is contained in:
Paul Schaub 2022-03-10 12:01:12 +01:00
parent 0824bbd37c
commit 8f473b513f
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
4 changed files with 185 additions and 40 deletions

View file

@ -33,6 +33,9 @@ public abstract class OpenPgpFingerprint implements CharSequence, Comparable<Ope
if (key.getVersion() == 4) { if (key.getVersion() == 4) {
return new OpenPgpV4Fingerprint(key); return new OpenPgpV4Fingerprint(key);
} }
if (key.getVersion() == 5) {
return new OpenPgpV5Fingerprint(key);
}
throw new IllegalArgumentException("OpenPGP keys of version " + key.getVersion() + " are not supported."); throw new IllegalArgumentException("OpenPGP keys of version " + key.getVersion() + " are not supported.");
} }

View file

@ -26,51 +26,13 @@ public class OpenPgpV4Fingerprint extends OpenPgpFingerprint {
/** /**
* Create an {@link OpenPgpV4Fingerprint}. * Create an {@link OpenPgpV4Fingerprint}.
* @see <a href="https://xmpp.org/extensions/xep-0373.html#annoucning-pubkey"> *
* XEP-0373 §4.1: The OpenPGP Public-Key Data Node about how to obtain the fingerprint</a> * @param fingerprint uppercase hexadecimal fingerprint of length 40
* @param fingerprint hexadecimal representation of the fingerprint.
*/ */
public OpenPgpV4Fingerprint(@Nonnull String fingerprint) { public OpenPgpV4Fingerprint(@Nonnull String fingerprint) {
super(fingerprint); super(fingerprint);
} }
@Override
public int getVersion() {
return 4;
}
@Override
protected boolean isValid(@Nonnull String fp) {
return fp.matches("[0-9A-F]{40}");
}
@Override
public long getKeyId() {
byte[] bytes = Hex.decode(toString().getBytes(utf8));
ByteBuffer buf = ByteBuffer.wrap(bytes);
// We have to cast here in order to be compatible with java 8
// https://github.com/eclipse/jetty.project/issues/3244
((Buffer) buf).position(12);
return buf.getLong();
}
@Override
public String prettyPrint() {
String fp = toString();
StringBuilder pretty = new StringBuilder();
for (int i = 0; i < 5; i++) {
pretty.append(fp, i * 4, (i + 1) * 4).append(' ');
}
pretty.append(' ');
for (int i = 5; i < 9; i++) {
pretty.append(fp, i * 4, (i + 1) * 4).append(' ');
}
pretty.append(fp, 36, 40);
return pretty.toString();
}
public OpenPgpV4Fingerprint(@Nonnull byte[] bytes) { public OpenPgpV4Fingerprint(@Nonnull byte[] bytes) {
super(Hex.encode(bytes)); super(Hex.encode(bytes));
} }
@ -95,6 +57,44 @@ public class OpenPgpV4Fingerprint extends OpenPgpFingerprint {
super(ring); super(ring);
} }
@Override
public int getVersion() {
return 4;
}
@Override
protected boolean isValid(@Nonnull String fp) {
return fp.matches("^[0-9A-F]{40}$");
}
@Override
public long getKeyId() {
byte[] bytes = Hex.decode(toString().getBytes(utf8));
ByteBuffer buf = ByteBuffer.wrap(bytes);
// The key id is the right-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(12); // 20 - 8 bytes = offset 12
return buf.getLong();
}
@Override
public String prettyPrint() {
String fp = toString();
StringBuilder pretty = new StringBuilder();
for (int i = 0; i < 5; i++) {
pretty.append(fp, i * 4, (i + 1) * 4).append(' ');
}
pretty.append(' ');
for (int i = 5; i < 9; i++) {
pretty.append(fp, i * 4, (i + 1) * 4).append(' ');
}
pretty.append(fp, 36, 40);
return pretty.toString();
}
@Override @Override
public boolean equals(Object other) { public boolean equals(Object other) {
if (other == null) { if (other == null) {

View file

@ -0,0 +1,117 @@
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.pgpainless.key;
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 {
/**
* Create an {@link OpenPgpV5Fingerprint}.
*
* @param fingerprint uppercase hexadecimal fingerprint of length 64
*/
public OpenPgpV5Fingerprint(@Nonnull String fingerprint) {
super(fingerprint);
}
public OpenPgpV5Fingerprint(@Nonnull byte[] bytes) {
super(Hex.encode(bytes));
}
public OpenPgpV5Fingerprint(@Nonnull PGPPublicKey key) {
super(key);
}
public OpenPgpV5Fingerprint(@Nonnull PGPSecretKey key) {
this(key.getPublicKey());
}
public OpenPgpV5Fingerprint(@Nonnull PGPPublicKeyRing ring) {
super(ring);
}
public OpenPgpV5Fingerprint(@Nonnull PGPSecretKeyRing ring) {
super(ring);
}
public OpenPgpV5Fingerprint(@Nonnull PGPKeyRing ring) {
super(ring);
}
@Override
public int getVersion() {
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,25 @@
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.pgpainless.key;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class OpenPgpV5FingerprintTest {
@Test
public void testFingerprintFormatting() {
String pretty = "76543210 ABCDEFAB 01AB23CD 1C0FFEE1 1EEFF0C1 DC32BA10 BAFEDCBA 01234567";
String fp = pretty.replace(" ", "");
OpenPgpV5Fingerprint fingerprint = new OpenPgpV5Fingerprint(fp);
assertEquals(fp, fingerprint.toString());
assertEquals(pretty, fingerprint.prettyPrint());
long id = fingerprint.getKeyId();
assertEquals("76543210abcdefab", Long.toHexString(id));
}
}