001// SPDX-FileCopyrightText: 2021 Paul Schaub <vanitasvitae@fsfe.org>
002//
003// SPDX-License-Identifier: Apache-2.0
004
005package org.pgpainless.signature.subpackets;
006
007import java.util.Date;
008import javax.annotation.Nonnull;
009
010import org.bouncycastle.bcpg.SignatureSubpacket;
011import org.bouncycastle.bcpg.SignatureSubpacketTags;
012import org.bouncycastle.openpgp.PGPSignatureSubpacketGenerator;
013
014/**
015 * Utility class that helps to deal with BCs SignatureSubpacketGenerator class.
016 */
017public final class SignatureSubpacketGeneratorUtil {
018
019    private SignatureSubpacketGeneratorUtil() {
020
021    }
022
023    /**
024     * Remove all packets of the given type from the {@link PGPSignatureSubpacketGenerator PGPSignatureSubpacketGenerators}
025     * internal set.
026     *
027     * @param subpacketType type of subpacket to remove
028     * @param subpacketGenerator subpacket generator
029     */
030    public static void removeAllPacketsOfType(org.pgpainless.algorithm.SignatureSubpacket subpacketType,
031                                              PGPSignatureSubpacketGenerator subpacketGenerator) {
032        removeAllPacketsOfType(subpacketType.getCode(), subpacketGenerator);
033    }
034
035    /**
036     * Remove all packets of the given type from the {@link PGPSignatureSubpacketGenerator PGPSignatureSubpacketGenerators}
037     * internal set.
038     *
039     * @param type type of subpacket to remove
040     * @param subpacketGenerator subpacket generator
041     */
042    public static void removeAllPacketsOfType(int type, PGPSignatureSubpacketGenerator subpacketGenerator) {
043        for (SignatureSubpacket subpacket : subpacketGenerator.getSubpackets(type)) {
044            subpacketGenerator.removePacket(subpacket);
045        }
046    }
047
048    /**
049     * Replace all occurrences of a signature creation time subpackets in the subpacket generator
050     * with a single new instance representing the provided date.
051     *
052     * @param date signature creation time
053     * @param subpacketGenerator subpacket generator
054     */
055    public static void setSignatureCreationTimeInSubpacketGenerator(Date date, PGPSignatureSubpacketGenerator subpacketGenerator) {
056        removeAllPacketsOfType(SignatureSubpacketTags.CREATION_TIME, subpacketGenerator);
057        subpacketGenerator.setSignatureCreationTime(false, date);
058    }
059
060    /**
061     * Replace all occurrences of key expiration time subpackets in the subpacket generator
062     * with a single instance representing the new expiration time.
063     *
064     * @param expirationDate expiration time as date or null for no expiration
065     * @param creationDate date on which the key was created
066     * @param subpacketGenerator subpacket generator
067     */
068    public static void setKeyExpirationDateInSubpacketGenerator(Date expirationDate,
069                                                                @Nonnull Date creationDate,
070                                                                PGPSignatureSubpacketGenerator subpacketGenerator) {
071        removeAllPacketsOfType(SignatureSubpacketTags.KEY_EXPIRE_TIME, subpacketGenerator);
072        long secondsToExpire = SignatureSubpacketsUtil.getKeyLifetimeInSeconds(expirationDate, creationDate);
073        subpacketGenerator.setKeyExpirationTime(true, secondsToExpire);
074    }
075}