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;
|
2021-01-18 17:08:52 +01:00
|
|
|
|
|
|
|
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-01-18 17:08:52 +01:00
|
|
|
*/
|
2021-08-15 15:24:19 +02:00
|
|
|
public final class SignatureSubpacketGeneratorUtil {
|
|
|
|
|
|
|
|
private SignatureSubpacketGeneratorUtil() {
|
|
|
|
|
|
|
|
}
|
2021-01-18 17:08:52 +01:00
|
|
|
|
2021-05-03 13:37:47 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2021-01-18 17:08:52 +01:00
|
|
|
public static void removeAllPacketsOfType(org.pgpainless.algorithm.SignatureSubpacket subpacketType,
|
|
|
|
PGPSignatureSubpacketGenerator subpacketGenerator) {
|
|
|
|
removeAllPacketsOfType(subpacketType.getCode(), subpacketGenerator);
|
|
|
|
}
|
|
|
|
|
2021-05-03 13:37:47 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2021-01-18 17:08:52 +01:00
|
|
|
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
|
|
|
|
*/
|
2021-11-06 17:39:04 +01:00
|
|
|
public static void setKeyExpirationDateInSubpacketGenerator(Date expirationDate,
|
|
|
|
@Nonnull Date creationDate,
|
|
|
|
PGPSignatureSubpacketGenerator subpacketGenerator) {
|
2021-01-18 17:08:52 +01:00
|
|
|
removeAllPacketsOfType(SignatureSubpacketTags.KEY_EXPIRE_TIME, subpacketGenerator);
|
2021-11-28 13:09:10 +01:00
|
|
|
long secondsToExpire = SignatureSubpacketsUtil.getKeyLifetimeInSeconds(expirationDate, creationDate);
|
2021-01-18 17:08:52 +01:00
|
|
|
subpacketGenerator.setKeyExpirationTime(true, secondsToExpire);
|
|
|
|
}
|
|
|
|
}
|