Skip to content

Commit

Permalink
Add support for PreferredAEADCiphersuites subpacket
Browse files Browse the repository at this point in the history
Requires BC 1.77
See bcgit/bc-java#1464
  • Loading branch information
vanitasvitae committed Aug 1, 2023
1 parent 8cdb7ee commit 23e31a1
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@
import org.bouncycastle.bcpg.sig.Features;
import org.bouncycastle.bcpg.sig.KeyExpirationTime;
import org.bouncycastle.bcpg.sig.KeyFlags;
import org.bouncycastle.bcpg.sig.PreferredAEADCiphersuites;
import org.bouncycastle.bcpg.sig.PreferredAlgorithms;
import org.bouncycastle.bcpg.sig.PrimaryUserID;
import org.bouncycastle.bcpg.sig.RevocationKey;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.pgpainless.algorithm.AEADAlgorithm;
import org.pgpainless.algorithm.CompressionAlgorithm;
import org.pgpainless.algorithm.Feature;
import org.pgpainless.algorithm.HashAlgorithm;
import org.pgpainless.algorithm.KeyFlag;
import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
import org.pgpainless.util.Tuple;

public interface SelfSignatureSubpackets extends BaseSignatureSubpackets {

Expand Down Expand Up @@ -56,21 +59,21 @@ default SelfSignatureSubpackets setKeyFlags(List<KeyFlag> keyFlags) {

SelfSignatureSubpackets setKeyExpirationTime(@Nullable KeyExpirationTime keyExpirationTime);

SelfSignatureSubpackets setPreferredCompressionAlgorithms(CompressionAlgorithm... algorithms);
SelfSignatureSubpackets setPreferredAEADCiphersuites(Tuple<SymmetricKeyAlgorithm, AEADAlgorithm>... algorithms);

SelfSignatureSubpackets setPreferredCompressionAlgorithms(Set<CompressionAlgorithm> algorithms);
SelfSignatureSubpackets setPreferredAEADCiphersuites(Set<Tuple<SymmetricKeyAlgorithm, AEADAlgorithm>> algorithms);

SelfSignatureSubpackets setPreferredCompressionAlgorithms(boolean isCritical, Set<CompressionAlgorithm> algorithms);
SelfSignatureSubpackets setPreferredAEADCiphersuites(boolean isCritical, Set<Tuple<SymmetricKeyAlgorithm, AEADAlgorithm>> algorithms);

SelfSignatureSubpackets setPreferredCompressionAlgorithms(@Nullable PreferredAlgorithms algorithms);
SelfSignatureSubpackets setPreferredAEADCiphersuites(@Nullable PreferredAEADCiphersuites algorithms);

SelfSignatureSubpackets setPreferredSymmetricKeyAlgorithms(SymmetricKeyAlgorithm... algorithms);
SelfSignatureSubpackets setPreferredCompressionAlgorithms(CompressionAlgorithm... algorithms);

SelfSignatureSubpackets setPreferredSymmetricKeyAlgorithms(Set<SymmetricKeyAlgorithm> algorithms);
SelfSignatureSubpackets setPreferredCompressionAlgorithms(Set<CompressionAlgorithm> algorithms);

SelfSignatureSubpackets setPreferredSymmetricKeyAlgorithms(boolean isCritical, Set<SymmetricKeyAlgorithm> algorithms);
SelfSignatureSubpackets setPreferredCompressionAlgorithms(boolean isCritical, Set<CompressionAlgorithm> algorithms);

SelfSignatureSubpackets setPreferredSymmetricKeyAlgorithms(@Nullable PreferredAlgorithms algorithms);
SelfSignatureSubpackets setPreferredCompressionAlgorithms(@Nullable PreferredAlgorithms algorithms);

SelfSignatureSubpackets setPreferredHashAlgorithms(HashAlgorithm... algorithms);

Expand All @@ -80,6 +83,14 @@ default SelfSignatureSubpackets setKeyFlags(List<KeyFlag> keyFlags) {

SelfSignatureSubpackets setPreferredHashAlgorithms(@Nullable PreferredAlgorithms algorithms);

SelfSignatureSubpackets setPreferredSymmetricKeyAlgorithms(SymmetricKeyAlgorithm... algorithms);

SelfSignatureSubpackets setPreferredSymmetricKeyAlgorithms(Set<SymmetricKeyAlgorithm> algorithms);

SelfSignatureSubpackets setPreferredSymmetricKeyAlgorithms(boolean isCritical, Set<SymmetricKeyAlgorithm> algorithms);

SelfSignatureSubpackets setPreferredSymmetricKeyAlgorithms(@Nullable PreferredAlgorithms algorithms);

SelfSignatureSubpackets addRevocationKey(@Nonnull PGPPublicKey revocationKey);

SelfSignatureSubpackets addRevocationKey(boolean isCritical, @Nonnull PGPPublicKey revocationKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.bouncycastle.bcpg.sig.KeyFlags;
import org.bouncycastle.bcpg.sig.NotationData;
import org.bouncycastle.bcpg.sig.PolicyURI;
import org.bouncycastle.bcpg.sig.PreferredAEADCiphersuites;
import org.bouncycastle.bcpg.sig.PreferredAlgorithms;
import org.bouncycastle.bcpg.sig.PrimaryUserID;
import org.bouncycastle.bcpg.sig.RegularExpression;
Expand All @@ -42,13 +43,15 @@
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPSignature;
import org.bouncycastle.openpgp.PGPSignatureSubpacketVector;
import org.pgpainless.algorithm.AEADAlgorithm;
import org.pgpainless.algorithm.CompressionAlgorithm;
import org.pgpainless.algorithm.Feature;
import org.pgpainless.algorithm.HashAlgorithm;
import org.pgpainless.algorithm.KeyFlag;
import org.pgpainless.algorithm.PublicKeyAlgorithm;
import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
import org.pgpainless.key.util.RevocationAttributes;
import org.pgpainless.util.Tuple;

public class SignatureSubpackets
implements BaseSignatureSubpackets, SelfSignatureSubpackets, CertificationSubpackets, RevocationSignatureSubpackets {
Expand All @@ -68,6 +71,7 @@ public class SignatureSubpackets
private PreferredAlgorithms preferredCompressionAlgorithms;
private PreferredAlgorithms preferredSymmetricKeyAlgorithms;
private PreferredAlgorithms preferredHashAlgorithms;
private PreferredAEADCiphersuites preferredAEADCiphersuites;
private final List<EmbeddedSignature> embeddedSignatureList = new ArrayList<>();
private SignerUserID signerUserId;
private KeyExpirationTime keyExpirationTime;
Expand Down Expand Up @@ -313,6 +317,40 @@ public SignatureSubpackets setKeyExpirationTime(@Nullable KeyExpirationTime keyE
return this;
}

@Override
public SelfSignatureSubpackets setPreferredAEADCiphersuites(Tuple<SymmetricKeyAlgorithm, AEADAlgorithm>... algorithms) {
return setPreferredAEADCiphersuites(new LinkedHashSet<>(Arrays.asList(algorithms)));
}

@Override
public SelfSignatureSubpackets setPreferredAEADCiphersuites(Set<Tuple<SymmetricKeyAlgorithm, AEADAlgorithm>> algorithms) {
return setPreferredAEADCiphersuites(false, algorithms);
}

@Override
public SelfSignatureSubpackets setPreferredAEADCiphersuites(boolean isCritical, Set<Tuple<SymmetricKeyAlgorithm, AEADAlgorithm>> algorithms) {
List<PreferredAEADCiphersuites.Combination> combinations = new ArrayList<>();
Iterator<Tuple<SymmetricKeyAlgorithm, AEADAlgorithm>> iterator = algorithms.iterator();
while (iterator.hasNext()) {
Tuple<SymmetricKeyAlgorithm, AEADAlgorithm> tuple = iterator.next();
combinations.add(new PreferredAEADCiphersuites.Combination(
tuple.getA().getAlgorithmId(), tuple.getB().getAlgorithmId()));
}
PreferredAEADCiphersuites subpacket = new PreferredAEADCiphersuites(
isCritical, combinations.toArray(new PreferredAEADCiphersuites.Combination[0]));
return setPreferredAEADCiphersuites(subpacket);
}

@Override
public SelfSignatureSubpackets setPreferredAEADCiphersuites(@Nullable PreferredAEADCiphersuites algorithms) {
this.preferredAEADCiphersuites = algorithms;
return this;
}

public PreferredAEADCiphersuites getPreferredAEADCiphersuites() {
return preferredAEADCiphersuites;
}

public KeyExpirationTime getKeyExpirationTimeSubpacket() {
return keyExpirationTime;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.bouncycastle.bcpg.sig.KeyFlags;
import org.bouncycastle.bcpg.sig.NotationData;
import org.bouncycastle.bcpg.sig.PolicyURI;
import org.bouncycastle.bcpg.sig.PreferredAEADCiphersuites;
import org.bouncycastle.bcpg.sig.PreferredAlgorithms;
import org.bouncycastle.bcpg.sig.PrimaryUserID;
import org.bouncycastle.bcpg.sig.RegularExpression;
Expand Down Expand Up @@ -79,6 +80,9 @@ public static SignatureSubpackets applyFrom(PGPSignatureSubpacketVector vector,
case preferredCompressionAlgorithms:
subpackets.setPreferredCompressionAlgorithms((PreferredAlgorithms) subpacket);
break;
case preferredAEADAlgorithms:
subpackets.setPreferredAEADCiphersuites((PreferredAEADCiphersuites) subpacket);
break;
case primaryUserId:
PrimaryUserID primaryUserID = (PrimaryUserID) subpacket;
subpackets.setPrimaryUserId(primaryUserID);
Expand Down Expand Up @@ -128,7 +132,6 @@ public static SignatureSubpackets applyFrom(PGPSignatureSubpacketVector vector,
case keyServerPreferences:
case preferredKeyServers:
case placeholder:
case preferredAEADAlgorithms:
case attestedCertification:
subpackets.addResidualSubpacket(subpacket);
break;
Expand Down Expand Up @@ -161,6 +164,7 @@ public static PGPSignatureSubpacketGenerator applyTo(SignatureSubpackets subpack
addSubpacket(generator, subpackets.getPreferredCompressionAlgorithmsSubpacket());
addSubpacket(generator, subpackets.getPreferredSymmetricKeyAlgorithmsSubpacket());
addSubpacket(generator, subpackets.getPreferredHashAlgorithmsSubpacket());
addSubpacket(generator, subpackets.getPreferredAEADCiphersuites());
for (EmbeddedSignature embeddedSignature : subpackets.getEmbeddedSignatureSubpackets()) {
addSubpacket(generator, embeddedSignature);
}
Expand Down

0 comments on commit 23e31a1

Please sign in to comment.