Skip to content

Commit d88202f

Browse files
committed
Implement proper methods for setting/getting preferred AEAD algorithms for OpenPGP and LibrePGP
1 parent e757540 commit d88202f

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

pg/src/main/java/org/bouncycastle/openpgp/PGPSignatureSubpacketGenerator.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
import org.bouncycastle.bcpg.sig.IssuerKeyID;
1616
import org.bouncycastle.bcpg.sig.KeyExpirationTime;
1717
import org.bouncycastle.bcpg.sig.KeyFlags;
18+
import org.bouncycastle.bcpg.sig.LibrePGPPreferredEncryptionModes;
1819
import org.bouncycastle.bcpg.sig.NotationData;
1920
import org.bouncycastle.bcpg.sig.PolicyURI;
21+
import org.bouncycastle.bcpg.sig.PreferredAEADCiphersuites;
2022
import org.bouncycastle.bcpg.sig.PreferredAlgorithms;
2123
import org.bouncycastle.bcpg.sig.PrimaryUserID;
2224
import org.bouncycastle.bcpg.sig.RegularExpression;
@@ -202,6 +204,54 @@ public void setPreferredAEADAlgorithms(boolean isCritical, int[] algorithms)
202204
algorithms));
203205
}
204206

207+
/**
208+
* Specify the preferred OpenPGP AEAD ciphersuites of this key.
209+
*
210+
* @see <a href="https://www.rfc-editor.org/rfc/rfc9580.html#name-preferred-aead-ciphersuites">
211+
* RFC9580: Preferred AEAD Ciphersuites</a>
212+
*
213+
* @param isCritical true, if this packet should be treated as critical, false otherwise.
214+
* @param algorithms array of algorithms in descending preference
215+
*/
216+
public void setPreferredAEADCiphersuites(boolean isCritical, PreferredAEADCiphersuites.Combination[] algorithms)
217+
{
218+
packets.add(new PreferredAEADCiphersuites(isCritical, algorithms));
219+
}
220+
221+
/**
222+
* Specify the preferred OpenPGP AEAD ciphersuites of this key.
223+
*
224+
* @see <a href="https://www.rfc-editor.org/rfc/rfc9580.html#name-preferred-aead-ciphersuites">
225+
* RFC9580: Preferred AEAD Ciphersuites</a>
226+
*
227+
* @param builder builder to build the ciphersuites packet from
228+
*/
229+
public void setPreferredAEADCiphersuites(PreferredAEADCiphersuites.Builder builder)
230+
{
231+
packets.add(builder.build());
232+
}
233+
234+
/**
235+
* Set the preferred encryption modes for LibrePGP keys.
236+
* Note: LibrePGP is not OpenPGP. An application strictly compliant to only the OpenPGP standard will not
237+
* know how to handle LibrePGP encryption modes.
238+
* The LibrePGP spec states that this subpacket shall be ignored and the application shall instead assume
239+
* {@link org.bouncycastle.bcpg.AEADAlgorithmTags#OCB}.
240+
*
241+
* @see <a href="https://www.ietf.org/archive/id/draft-koch-librepgp-01.html#name-preferred-encryption-modes">
242+
* LibrePGP: Preferred Encryption Modes</a>
243+
* @see org.bouncycastle.bcpg.AEADAlgorithmTags for possible algorithms
244+
*
245+
* @param isCritical whether the packet is critical
246+
* @param algorithms list of algorithms
247+
* @deprecated the use of this subpacket is deprecated in LibrePGP
248+
*/
249+
@Deprecated
250+
public void setPreferredLibrePgpEncryptionModes(boolean isCritical, int[] algorithms)
251+
{
252+
packets.add(new LibrePGPPreferredEncryptionModes(isCritical, algorithms));
253+
}
254+
205255
public void addPolicyURI(boolean isCritical, String policyUri)
206256
{
207257
packets.add(new PolicyURI(isCritical, policyUri));

pg/src/main/java/org/bouncycastle/openpgp/PGPSignatureSubpacketVector.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
import org.bouncycastle.bcpg.sig.IssuerKeyID;
1717
import org.bouncycastle.bcpg.sig.KeyExpirationTime;
1818
import org.bouncycastle.bcpg.sig.KeyFlags;
19+
import org.bouncycastle.bcpg.sig.LibrePGPPreferredEncryptionModes;
1920
import org.bouncycastle.bcpg.sig.NotationData;
2021
import org.bouncycastle.bcpg.sig.PolicyURI;
22+
import org.bouncycastle.bcpg.sig.PreferredAEADCiphersuites;
2123
import org.bouncycastle.bcpg.sig.PreferredAlgorithms;
2224
import org.bouncycastle.bcpg.sig.PrimaryUserID;
2325
import org.bouncycastle.bcpg.sig.RegularExpression;
@@ -297,6 +299,40 @@ public int[] getPreferredAEADAlgorithms()
297299
return ((PreferredAlgorithms)p).getPreferences();
298300
}
299301

302+
/**
303+
* Return the preferred AEAD ciphersuites denoted in the signature.
304+
*
305+
* @return OpenPGP AEAD ciphersuites
306+
*/
307+
public PreferredAEADCiphersuites getPreferredAEADCiphersuites()
308+
{
309+
SignatureSubpacket p = this.getSubpacket(SignatureSubpacketTags.PREFERRED_AEAD_ALGORITHMS);
310+
311+
if (p == null)
312+
{
313+
return null;
314+
}
315+
return (PreferredAEADCiphersuites) p;
316+
}
317+
318+
/**
319+
* Return the preferred LibrePGP encryption modes denoted in the signature.
320+
* Note: The LibrePGP spec states that this subpacket shall be ignored and the application
321+
* shall instead assume {@link org.bouncycastle.bcpg.AEADAlgorithmTags#OCB}.
322+
*
323+
* @return LibrePGP encryption modes
324+
*/
325+
public int[] getPreferredLibrePgpEncryptionModes()
326+
{
327+
SignatureSubpacket p = this.getSubpacket(SignatureSubpacketTags.PREFERRED_AEAD_ALGORITHMS);
328+
329+
if (p == null)
330+
{
331+
return null;
332+
}
333+
return ((LibrePGPPreferredEncryptionModes) p).getPreferences();
334+
}
335+
300336
public int getKeyFlags()
301337
{
302338
SignatureSubpacket p = this.getSubpacket(SignatureSubpacketTags.KEY_FLAGS);

0 commit comments

Comments
 (0)