pgpainless/pgpainless-core/src/test/java/org/pgpainless/key/modification/AddSubKeyTest.java

75 lines
3.0 KiB
Java
Raw Normal View History

2021-10-07 15:48:52 +02:00
// SPDX-FileCopyrightText: 2020 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
2020-11-10 17:25:35 +01:00
package org.pgpainless.key.modification;
import static org.junit.jupiter.api.Assertions.assertEquals;
2020-11-13 16:31:59 +01:00
import static org.junit.jupiter.api.Assertions.assertNotEquals;
2020-11-10 17:25:35 +01:00
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
2020-11-10 17:25:35 +01:00
import java.util.Iterator;
import java.util.List;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
2020-11-10 17:25:35 +01:00
import org.pgpainless.PGPainless;
import org.pgpainless.algorithm.KeyFlag;
import org.pgpainless.key.TestKeys;
import org.pgpainless.key.generation.KeySpec;
2020-12-11 18:16:31 +01:00
import org.pgpainless.key.generation.type.ecc.EllipticCurve;
import org.pgpainless.key.generation.type.ecc.ecdsa.ECDSA;
import org.pgpainless.key.info.KeyRingInfo;
2020-11-10 17:25:35 +01:00
import org.pgpainless.key.protection.PasswordBasedSecretKeyRingProtector;
import org.pgpainless.key.protection.SecretKeyRingProtector;
import org.pgpainless.key.protection.UnlockSecretKey;
2020-11-10 17:25:35 +01:00
import org.pgpainless.util.Passphrase;
2021-12-28 13:32:50 +01:00
import org.pgpainless.util.TestAllImplementations;
2020-11-10 17:25:35 +01:00
public class AddSubKeyTest {
@TestTemplate
@ExtendWith(TestAllImplementations.class)
public void testAddSubKey()
throws IOException, PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException {
2020-11-10 17:25:35 +01:00
PGPSecretKeyRing secretKeys = TestKeys.getCryptieSecretKeyRing();
List<Long> keyIdsBefore = new ArrayList<>();
for (Iterator<PGPPublicKey> it = secretKeys.getPublicKeys(); it.hasNext(); ) {
keyIdsBefore.add(it.next().getKeyID());
}
secretKeys = PGPainless.modifyKeyRing(secretKeys)
.addSubKey(
2021-09-13 19:20:19 +02:00
KeySpec.getBuilder(ECDSA.fromCurve(EllipticCurve._P256), KeyFlag.SIGN_DATA).build(),
2020-11-10 17:25:35 +01:00
Passphrase.fromPassword("subKeyPassphrase"),
PasswordBasedSecretKeyRingProtector.forKey(secretKeys, Passphrase.fromPassword("password123")))
.done();
List<Long> keyIdsAfter = new ArrayList<>();
for (Iterator<PGPPublicKey> it = secretKeys.getPublicKeys(); it.hasNext(); ) {
keyIdsAfter.add(it.next().getKeyID());
}
assertNotEquals(keyIdsAfter, keyIdsBefore);
keyIdsAfter.removeAll(keyIdsBefore);
long subKeyId = keyIdsAfter.get(0);
PGPSecretKey subKey = secretKeys.getSecretKey(subKeyId);
2021-11-20 20:19:22 +01:00
SecretKeyRingProtector protector = SecretKeyRingProtector.unlockEachKeyWith(
Passphrase.fromPassword("subKeyPassphrase"), secretKeys);
2021-12-28 13:32:50 +01:00
UnlockSecretKey.unlockSecretKey(subKey, protector);
KeyRingInfo info = new KeyRingInfo(secretKeys);
assertEquals(Collections.singletonList(KeyFlag.SIGN_DATA), info.getKeyFlagsOf(subKeyId));
2020-11-10 17:25:35 +01:00
}
}