2018-07-08 19:31:53 +02:00
|
|
|
/*
|
2018-07-08 18:05:22 +02:00
|
|
|
* Copyright 2018 Paul Schaub.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2018-07-18 18:23:06 +02:00
|
|
|
package org.pgpainless.key;
|
2018-07-08 18:05:22 +02:00
|
|
|
|
2020-04-21 17:34:21 +02:00
|
|
|
import java.nio.Buffer;
|
2018-07-08 18:05:22 +02:00
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
import java.nio.charset.Charset;
|
2020-01-10 18:46:31 +01:00
|
|
|
import javax.annotation.Nonnull;
|
2018-07-08 18:05:22 +02:00
|
|
|
|
|
|
|
import org.bouncycastle.openpgp.PGPPublicKey;
|
2018-07-08 18:14:36 +02:00
|
|
|
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
|
|
|
import org.bouncycastle.openpgp.PGPSecretKey;
|
|
|
|
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
2018-07-08 18:05:22 +02:00
|
|
|
import org.bouncycastle.util.encoders.Hex;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class represents an hex encoded, uppercase OpenPGP v4 fingerprint.
|
|
|
|
*/
|
|
|
|
public class OpenPgpV4Fingerprint implements CharSequence, Comparable<OpenPgpV4Fingerprint> {
|
|
|
|
|
2020-07-10 18:16:22 +02:00
|
|
|
private static final Charset utf8 = Charset.forName("UTF-8");
|
2018-07-08 18:05:22 +02:00
|
|
|
private final String fingerprint;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 hexadecimal representation of the fingerprint.
|
|
|
|
*/
|
2018-07-31 20:09:16 +02:00
|
|
|
public OpenPgpV4Fingerprint(@Nonnull String fingerprint) {
|
2018-07-08 18:14:36 +02:00
|
|
|
String fp = fingerprint.trim().toUpperCase();
|
2018-07-08 18:05:22 +02:00
|
|
|
if (!isValid(fp)) {
|
2018-07-09 16:40:02 +02:00
|
|
|
throw new IllegalArgumentException("Fingerprint " + fingerprint +
|
2018-07-08 18:05:22 +02:00
|
|
|
" does not appear to be a valid OpenPGP v4 fingerprint.");
|
|
|
|
}
|
|
|
|
this.fingerprint = fp;
|
|
|
|
}
|
|
|
|
|
2018-07-31 20:09:16 +02:00
|
|
|
public OpenPgpV4Fingerprint(@Nonnull byte[] bytes) {
|
2020-07-10 18:16:22 +02:00
|
|
|
this(new String(bytes, utf8));
|
2018-07-08 18:05:22 +02:00
|
|
|
}
|
|
|
|
|
2018-07-31 20:09:16 +02:00
|
|
|
public OpenPgpV4Fingerprint(@Nonnull PGPPublicKey key) {
|
2018-07-08 18:05:22 +02:00
|
|
|
this(Hex.encode(key.getFingerprint()));
|
2018-07-08 18:14:36 +02:00
|
|
|
if (key.getVersion() != 4) {
|
2018-07-09 16:40:02 +02:00
|
|
|
throw new IllegalArgumentException("Key is not a v4 OpenPgp key.");
|
2018-07-08 18:14:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-31 20:09:16 +02:00
|
|
|
public OpenPgpV4Fingerprint(@Nonnull PGPSecretKey key) {
|
2018-07-08 18:14:36 +02:00
|
|
|
this(key.getPublicKey());
|
|
|
|
}
|
|
|
|
|
2018-07-31 20:09:16 +02:00
|
|
|
public OpenPgpV4Fingerprint(@Nonnull PGPPublicKeyRing ring) {
|
2018-07-08 18:14:36 +02:00
|
|
|
this(ring.getPublicKey());
|
|
|
|
}
|
|
|
|
|
2018-07-31 20:09:16 +02:00
|
|
|
public OpenPgpV4Fingerprint(@Nonnull PGPSecretKeyRing ring) {
|
2018-07-08 18:14:36 +02:00
|
|
|
this(ring.getPublicKey());
|
2018-07-08 18:05:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check, whether the fingerprint consists of 40 valid hexadecimal characters.
|
|
|
|
* @param fp fingerprint to check.
|
|
|
|
* @return true if fingerprint is valid.
|
|
|
|
*/
|
2018-07-31 20:09:16 +02:00
|
|
|
private static boolean isValid(@Nonnull String fp) {
|
2018-07-08 18:05:22 +02:00
|
|
|
return fp.matches("[0-9A-F]{40}");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the key id of the OpenPGP public key this {@link OpenPgpV4Fingerprint} belongs to.
|
|
|
|
*
|
|
|
|
* @see <a href="https://tools.ietf.org/html/rfc4880#section-12.2">
|
|
|
|
* RFC-4880 §12.2: Key IDs and Fingerprints</a>
|
|
|
|
* @return key id
|
|
|
|
*/
|
|
|
|
public long getKeyId() {
|
2020-07-10 18:16:22 +02:00
|
|
|
byte[] bytes = Hex.decode(toString().getBytes(utf8));
|
2018-08-17 17:28:17 +02:00
|
|
|
ByteBuffer buf = ByteBuffer.wrap(bytes);
|
2018-07-08 19:31:53 +02:00
|
|
|
|
2020-04-21 17:34:21 +02:00
|
|
|
// 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();
|
2018-07-08 18:05:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@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 fingerprint.hashCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int length() {
|
|
|
|
return fingerprint.length();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public char charAt(int i) {
|
|
|
|
return fingerprint.charAt(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public CharSequence subSequence(int i, int i1) {
|
|
|
|
return fingerprint.subSequence(i, i1);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-01-09 20:00:28 +01:00
|
|
|
@Nonnull
|
2018-07-08 18:05:22 +02:00
|
|
|
public String toString() {
|
|
|
|
return fingerprint;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-07-31 20:09:16 +02:00
|
|
|
public int compareTo(@Nonnull OpenPgpV4Fingerprint openPgpV4Fingerprint) {
|
2018-07-08 18:05:22 +02:00
|
|
|
return fingerprint.compareTo(openPgpV4Fingerprint.fingerprint);
|
|
|
|
}
|
|
|
|
}
|