1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-06-16 16:44:50 +02:00
pgpainless/pgpainless-core/src/main/java/org/pgpainless/signature/subpackets/SignatureSubpacketGeneratorUtil.java

76 lines
3.1 KiB
Java
Raw Normal View History

2021-10-07 15:48:52 +02:00
// SPDX-FileCopyrightText: 2021 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
2021-04-26 13:38:12 +02:00
package org.pgpainless.signature.subpackets;
import java.util.Date;
import javax.annotation.Nonnull;
import org.bouncycastle.bcpg.SignatureSubpacket;
import org.bouncycastle.bcpg.SignatureSubpacketTags;
import org.bouncycastle.openpgp.PGPSignatureSubpacketGenerator;
/**
2021-12-28 13:53:25 +01:00
* Utility class that helps to deal with BCs SignatureSubpacketGenerator class.
*/
2021-08-15 15:24:19 +02:00
public final class SignatureSubpacketGeneratorUtil {
private SignatureSubpacketGeneratorUtil() {
}
/**
* Remove all packets of the given type from the {@link PGPSignatureSubpacketGenerator PGPSignatureSubpacketGenerators}
* internal set.
*
* @param subpacketType type of subpacket to remove
* @param subpacketGenerator subpacket generator
*/
public static void removeAllPacketsOfType(org.pgpainless.algorithm.SignatureSubpacket subpacketType,
PGPSignatureSubpacketGenerator subpacketGenerator) {
removeAllPacketsOfType(subpacketType.getCode(), subpacketGenerator);
}
/**
* Remove all packets of the given type from the {@link PGPSignatureSubpacketGenerator PGPSignatureSubpacketGenerators}
* internal set.
*
* @param type type of subpacket to remove
* @param subpacketGenerator subpacket generator
*/
public static void removeAllPacketsOfType(int type, PGPSignatureSubpacketGenerator subpacketGenerator) {
for (SignatureSubpacket subpacket : subpacketGenerator.getSubpackets(type)) {
subpacketGenerator.removePacket(subpacket);
}
}
/**
* Replace all occurrences of a signature creation time subpackets in the subpacket generator
* with a single new instance representing the provided date.
*
* @param date signature creation time
* @param subpacketGenerator subpacket generator
*/
public static void setSignatureCreationTimeInSubpacketGenerator(Date date, PGPSignatureSubpacketGenerator subpacketGenerator) {
removeAllPacketsOfType(SignatureSubpacketTags.CREATION_TIME, subpacketGenerator);
subpacketGenerator.setSignatureCreationTime(false, date);
}
/**
* Replace all occurrences of key expiration time subpackets in the subpacket generator
* with a single instance representing the new expiration time.
*
* @param expirationDate expiration time as date or null for no expiration
* @param creationDate date on which the key was created
* @param subpacketGenerator subpacket generator
*/
public static void setKeyExpirationDateInSubpacketGenerator(Date expirationDate,
@Nonnull Date creationDate,
PGPSignatureSubpacketGenerator subpacketGenerator) {
removeAllPacketsOfType(SignatureSubpacketTags.KEY_EXPIRE_TIME, subpacketGenerator);
long secondsToExpire = SignatureSubpacketsUtil.getKeyLifetimeInSeconds(expirationDate, creationDate);
subpacketGenerator.setKeyExpirationTime(true, secondsToExpire);
}
}