2021-04-26 13:38:12 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2021 Paul Schaub.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
package org.bouncycastle;
|
|
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
2021-05-06 00:04:03 +02:00
|
|
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
2021-04-26 13:38:12 +02:00
|
|
|
|
|
|
|
import java.security.InvalidAlgorithmParameterException;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
import java.util.Iterator;
|
2021-05-06 00:04:03 +02:00
|
|
|
import java.util.List;
|
2021-04-26 13:38:12 +02:00
|
|
|
|
|
|
|
import org.bouncycastle.openpgp.PGPException;
|
|
|
|
import org.bouncycastle.openpgp.PGPPublicKey;
|
|
|
|
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
|
|
|
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import org.pgpainless.PGPainless;
|
|
|
|
import org.pgpainless.key.util.KeyRingUtils;
|
2021-05-06 00:04:03 +02:00
|
|
|
import org.pgpainless.util.CollectionUtils;
|
2021-04-26 13:38:12 +02:00
|
|
|
|
|
|
|
public class PGPPublicKeyRingTest {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Learning test to see if BC also makes userids available on subkeys.
|
|
|
|
* It does not.
|
|
|
|
*
|
|
|
|
* see also https://security.stackexchange.com/questions/92635/is-it-possible-to-assign-different-uids-to-subkeys-for-the-purpose-of-having-mul
|
|
|
|
*/
|
|
|
|
@Test
|
|
|
|
public void subkeysDoNotHaveUserIDsTest() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException {
|
|
|
|
PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing().simpleEcKeyRing("primary@user.id");
|
|
|
|
PGPPublicKeyRing publicKeys = KeyRingUtils.publicKeyRingFrom(secretKeys);
|
|
|
|
PGPPublicKey primaryKey = publicKeys.getPublicKey();
|
|
|
|
for (PGPPublicKey subkey : publicKeys) {
|
|
|
|
Iterator<String> userIds = subkey.getUserIDs();
|
|
|
|
if (primaryKey == subkey) {
|
|
|
|
assertEquals("primary@user.id", userIds.next());
|
|
|
|
}
|
2021-06-10 12:42:48 +02:00
|
|
|
assertFalse(userIds.hasNext());
|
2021-04-26 13:38:12 +02:00
|
|
|
}
|
|
|
|
}
|
2021-05-06 00:04:03 +02:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void removeUserIdTest() throws PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException {
|
|
|
|
String userId = "alice@wonderland.lit";
|
|
|
|
PGPSecretKeyRing secretKeyRing = PGPainless.generateKeyRing().simpleEcKeyRing(userId);
|
|
|
|
PGPPublicKeyRing publicKeys = KeyRingUtils.publicKeyRingFrom(secretKeyRing);
|
|
|
|
|
|
|
|
List<String> userIds = CollectionUtils.iteratorToList(publicKeys.getPublicKey().getUserIDs());
|
|
|
|
assertTrue(userIds.contains(userId));
|
|
|
|
|
|
|
|
PGPPublicKey publicKey = publicKeys.getPublicKey();
|
2021-05-20 13:42:52 +02:00
|
|
|
publicKey = PGPPublicKey.removeCertification(publicKey, userId);
|
2021-05-06 00:04:03 +02:00
|
|
|
|
|
|
|
userIds = CollectionUtils.iteratorToList(publicKey.getUserIDs());
|
|
|
|
assertFalse(userIds.contains(userId));
|
|
|
|
}
|
2021-04-26 13:38:12 +02:00
|
|
|
}
|