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

Add feature-related utilities and tests

This commit is contained in:
Paul Schaub 2021-10-04 13:32:04 +02:00
parent 7113dd1d7e
commit bccf384dbf
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
3 changed files with 216 additions and 0 deletions

View file

@ -15,6 +15,8 @@
*/
package org.pgpainless.algorithm;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@ -34,6 +36,8 @@ public enum Feature {
* RFC-4880 §5.14: Modification Detection Code Packet</a>
*/
MODIFICATION_DETECTION(Features.FEATURE_MODIFICATION_DETECTION),
AEAD_ENCRYPTED_DATA(Features.FEATURE_AEAD_ENCRYPTED_DATA),
VERSION_5_PUBLIC_KEY(Features.FEATURE_VERSION_5_PUBLIC_KEY)
;
private static final Map<Byte, Feature> MAP = new ConcurrentHashMap<>();
@ -57,4 +61,34 @@ public enum Feature {
public byte getFeatureId() {
return featureId;
}
/**
* Convert a bitmask into a list of {@link KeyFlag KeyFlags}.
*
* @param bitmask bitmask
* @return list of key flags encoded by the bitmask
*/
public static List<Feature> fromBitmask(int bitmask) {
List<Feature> features = new ArrayList<>();
for (Feature f : Feature.values()) {
if ((bitmask & f.featureId) != 0) {
features.add(f);
}
}
return features;
}
/**
* Encode a list of {@link KeyFlag KeyFlags} into a bitmask.
*
* @param features list of flags
* @return bitmask
*/
public static byte toBitmask(Feature... features) {
byte mask = 0;
for (Feature f : features) {
mask |= f.featureId;
}
return mask;
}
}

View file

@ -49,6 +49,7 @@ import org.bouncycastle.openpgp.PGPSignatureList;
import org.bouncycastle.openpgp.PGPSignatureSubpacketVector;
import org.bouncycastle.util.encoders.Hex;
import org.pgpainless.algorithm.CompressionAlgorithm;
import org.pgpainless.algorithm.Feature;
import org.pgpainless.algorithm.HashAlgorithm;
import org.pgpainless.algorithm.KeyFlag;
import org.pgpainless.algorithm.SignatureSubpacket;
@ -356,6 +357,22 @@ public final class SignatureSubpacketsUtil {
return hashed(signature, SignatureSubpacket.features);
}
/**
* Parse out the features subpacket of a signature.
* If the signature has no features subpacket, return null.
* Otherwise, return the features as a feature set.
*
* @param signature signature
* @return features as set
*/
public static @Nullable Set<Feature> parseFeatures(PGPSignature signature) {
Features features = getFeatures(signature);
if (features == null) {
return null;
}
return new LinkedHashSet<>(Feature.fromBitmask(features.getData()[0]));
}
/**
* Return the signature target subpacket from the signature.
* We search for this subpacket in the hashed and unhashed area (in this order).