2021-11-08 22:45:08 +01:00
|
|
|
// SPDX-FileCopyrightText: 2021 Paul Schaub <vanitasvitae@fsfe.org>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package org.pgpainless.key.modification;
|
|
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
2023-05-03 17:26:21 +02:00
|
|
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
2021-11-08 22:45:08 +01:00
|
|
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.security.InvalidAlgorithmParameterException;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import org.bouncycastle.bcpg.sig.NotationData;
|
|
|
|
import org.bouncycastle.openpgp.PGPException;
|
2021-11-20 16:07:27 +01:00
|
|
|
import org.bouncycastle.openpgp.PGPKeyPair;
|
2021-11-08 22:45:08 +01:00
|
|
|
import org.bouncycastle.openpgp.PGPPublicKey;
|
|
|
|
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
|
|
|
import org.bouncycastle.openpgp.PGPSignature;
|
|
|
|
import org.junit.JUtils;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import org.pgpainless.PGPainless;
|
|
|
|
import org.pgpainless.algorithm.KeyFlag;
|
|
|
|
import org.pgpainless.key.OpenPgpV4Fingerprint;
|
|
|
|
import org.pgpainless.key.generation.KeyRingBuilder;
|
|
|
|
import org.pgpainless.key.generation.KeySpec;
|
|
|
|
import org.pgpainless.key.generation.type.KeyType;
|
2024-02-21 14:57:02 +01:00
|
|
|
import org.pgpainless.key.generation.type.eddsa_legacy.EdDSALegacyCurve;
|
2021-11-08 22:45:08 +01:00
|
|
|
import org.pgpainless.key.info.KeyRingInfo;
|
|
|
|
import org.pgpainless.key.protection.SecretKeyRingProtector;
|
|
|
|
import org.pgpainless.signature.subpackets.SelfSignatureSubpackets;
|
|
|
|
import org.pgpainless.signature.subpackets.SignatureSubpacketsUtil;
|
|
|
|
|
|
|
|
public class AddSubkeyWithModifiedBindingSignatureSubpackets {
|
|
|
|
|
2021-12-28 13:32:50 +01:00
|
|
|
public static final long MILLIS_IN_SEC = 1000;
|
2021-11-20 16:07:27 +01:00
|
|
|
|
2021-11-08 22:45:08 +01:00
|
|
|
@Test
|
|
|
|
public void bindEncryptionSubkeyAndModifyBindingSignatureHashedSubpackets() throws PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, IOException {
|
|
|
|
SecretKeyRingProtector protector = SecretKeyRingProtector.unprotectedKeys();
|
|
|
|
PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing()
|
2022-06-09 00:42:06 +02:00
|
|
|
.modernKeyRing("Alice <alice@pgpainless.org>");
|
2021-11-08 22:45:08 +01:00
|
|
|
KeyRingInfo before = PGPainless.inspectKeyRing(secretKeys);
|
|
|
|
|
2021-11-20 16:07:27 +01:00
|
|
|
PGPKeyPair secretSubkey = KeyRingBuilder.generateKeyPair(
|
2024-02-21 14:57:02 +01:00
|
|
|
KeySpec.getBuilder(KeyType.EDDSA_LEGACY(EdDSALegacyCurve._Ed25519), KeyFlag.SIGN_DATA).build());
|
2021-11-08 22:45:08 +01:00
|
|
|
|
2021-11-20 16:07:27 +01:00
|
|
|
long secondsUntilExpiration = 1000;
|
2021-11-08 22:45:08 +01:00
|
|
|
secretKeys = PGPainless.modifyKeyRing(secretKeys)
|
|
|
|
.addSubKey(secretSubkey, new SelfSignatureSubpackets.Callback() {
|
2021-11-20 16:07:27 +01:00
|
|
|
@Override
|
|
|
|
public void modifyHashedSubpackets(SelfSignatureSubpackets hashedSubpackets) {
|
|
|
|
hashedSubpackets.setKeyExpirationTime(true, secondsUntilExpiration);
|
|
|
|
hashedSubpackets.addNotationData(false, "test@test.test", "test");
|
|
|
|
}
|
|
|
|
}, SecretKeyRingProtector.unprotectedKeys(), protector, KeyFlag.SIGN_DATA)
|
2021-11-08 22:45:08 +01:00
|
|
|
.done();
|
|
|
|
|
|
|
|
KeyRingInfo after = PGPainless.inspectKeyRing(secretKeys);
|
2021-11-20 16:07:27 +01:00
|
|
|
List<PGPPublicKey> signingKeys = after.getSigningSubkeys();
|
|
|
|
signingKeys.removeAll(before.getSigningSubkeys());
|
|
|
|
assertFalse(signingKeys.isEmpty());
|
2021-11-08 22:45:08 +01:00
|
|
|
|
2021-11-20 16:07:27 +01:00
|
|
|
PGPPublicKey newKey = signingKeys.get(0);
|
2023-05-03 17:26:21 +02:00
|
|
|
Date newExpirationDate = after.getSubkeyExpirationDate(new OpenPgpV4Fingerprint(newKey));
|
|
|
|
assertNotNull(newExpirationDate);
|
2021-11-20 16:07:27 +01:00
|
|
|
Date now = new Date();
|
|
|
|
JUtils.assertEquals(
|
|
|
|
now.getTime() + MILLIS_IN_SEC * secondsUntilExpiration,
|
2023-05-03 17:26:21 +02:00
|
|
|
newExpirationDate.getTime(), 2 * MILLIS_IN_SEC);
|
2021-11-08 22:45:08 +01:00
|
|
|
assertTrue(newKey.getSignatures().hasNext());
|
|
|
|
PGPSignature binding = newKey.getSignatures().next();
|
|
|
|
List<NotationData> notations = SignatureSubpacketsUtil.getHashedNotationData(binding);
|
|
|
|
assertEquals(1, notations.size());
|
|
|
|
assertEquals("test@test.test", notations.get(0).getNotationName());
|
|
|
|
}
|
|
|
|
}
|