2018-06-13 17:26:48 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2018 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.
|
|
|
|
*/
|
2020-01-11 13:11:14 +01:00
|
|
|
package org.pgpainless.util;
|
2018-06-11 01:33:49 +02:00
|
|
|
|
2020-11-13 16:31:59 +01:00
|
|
|
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
2018-06-11 01:33:49 +02:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.security.InvalidAlgorithmParameterException;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
import java.security.NoSuchProviderException;
|
|
|
|
import java.util.Iterator;
|
2018-06-21 15:18:19 +02:00
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
2018-06-11 01:33:49 +02:00
|
|
|
|
|
|
|
import org.bouncycastle.openpgp.PGPException;
|
|
|
|
import org.bouncycastle.openpgp.PGPPublicKey;
|
|
|
|
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
2018-06-21 15:18:19 +02:00
|
|
|
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
|
2018-06-28 15:45:30 +02:00
|
|
|
import org.bouncycastle.openpgp.PGPSecretKey;
|
2018-06-11 01:33:49 +02:00
|
|
|
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
2018-06-21 15:18:19 +02:00
|
|
|
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
|
2020-11-13 16:31:59 +01:00
|
|
|
import org.junit.jupiter.api.Test;
|
2020-01-11 13:11:14 +01:00
|
|
|
import org.pgpainless.PGPainless;
|
2018-07-18 18:23:06 +02:00
|
|
|
import org.pgpainless.key.collection.PGPKeyRing;
|
|
|
|
import org.pgpainless.key.generation.KeySpec;
|
2020-11-07 18:24:12 +01:00
|
|
|
import org.pgpainless.key.generation.type.KeyType;
|
2018-07-18 18:23:06 +02:00
|
|
|
import org.pgpainless.key.generation.type.length.RsaLength;
|
2018-06-11 01:33:49 +02:00
|
|
|
|
2020-01-10 19:03:04 +01:00
|
|
|
public class BCUtilTest {
|
2018-06-11 01:33:49 +02:00
|
|
|
|
2018-06-21 15:18:19 +02:00
|
|
|
private static final Logger LOGGER = Logger.getLogger(BCUtil.class.getName());
|
|
|
|
|
2018-06-11 01:33:49 +02:00
|
|
|
@Test
|
2018-06-28 15:45:30 +02:00
|
|
|
public void keyRingToCollectionTest()
|
2020-01-12 23:34:47 +01:00
|
|
|
throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException,
|
2018-06-21 15:18:19 +02:00
|
|
|
IOException {
|
2018-07-12 11:07:53 +02:00
|
|
|
PGPKeyRing ring = PGPainless.generateKeyRing()
|
2020-11-07 18:24:12 +01:00
|
|
|
.withSubKey(KeySpec.getBuilder(KeyType.RSA(RsaLength._3072)).withDefaultKeyFlags().withDefaultAlgorithms())
|
|
|
|
.withMasterKey(KeySpec.getBuilder(KeyType.RSA(RsaLength._3072)).withDefaultKeyFlags().withDefaultAlgorithms())
|
2018-07-12 11:07:53 +02:00
|
|
|
.withPrimaryUserId("donald@duck.tails").withoutPassphrase().build();
|
|
|
|
PGPSecretKeyRing sec = ring.getSecretKeys();
|
|
|
|
PGPPublicKeyRing pub = ring.getPublicKeys();
|
2018-06-11 01:33:49 +02:00
|
|
|
|
2019-04-18 08:19:19 +02:00
|
|
|
LOGGER.log(Level.FINER, "Main ID: " + sec.getPublicKey().getKeyID() + " " + pub.getPublicKey().getKeyID());
|
2018-06-21 15:18:19 +02:00
|
|
|
|
2018-07-12 11:07:53 +02:00
|
|
|
int secSize = 1;
|
2018-06-11 01:33:49 +02:00
|
|
|
Iterator<PGPPublicKey> secPubIt = sec.getPublicKeys();
|
|
|
|
while (secPubIt.hasNext()) {
|
2018-06-21 15:18:19 +02:00
|
|
|
PGPPublicKey k = secPubIt.next();
|
2019-04-18 08:19:19 +02:00
|
|
|
LOGGER.log(Level.FINER, secSize + " " + k.getKeyID() + " " + k.isEncryptionKey() + " " + k.isMasterKey());
|
2018-06-11 01:33:49 +02:00
|
|
|
secSize++;
|
|
|
|
}
|
|
|
|
|
2019-04-18 08:19:19 +02:00
|
|
|
LOGGER.log(Level.FINER, "After BCUtil.publicKeyRingFromSecretKeyRing()");
|
2018-07-12 11:07:53 +02:00
|
|
|
int pubSize = 1;
|
2018-06-11 01:33:49 +02:00
|
|
|
Iterator<PGPPublicKey> pubPubIt = pub.getPublicKeys();
|
|
|
|
while (pubPubIt.hasNext()) {
|
2018-06-21 15:18:19 +02:00
|
|
|
PGPPublicKey k = pubPubIt.next();
|
2019-04-18 08:19:19 +02:00
|
|
|
LOGGER.log(Level.FINER, pubSize + " " + k.getKeyID() + " " + k.isEncryptionKey() + " " + k.isMasterKey());
|
2018-06-11 01:33:49 +02:00
|
|
|
pubSize++;
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals(secSize, pubSize);
|
2018-06-21 15:18:19 +02:00
|
|
|
|
|
|
|
PGPSecretKeyRingCollection secCol = BCUtil.keyRingsToKeyRingCollection(sec);
|
|
|
|
|
|
|
|
int secColSize = 0;
|
|
|
|
Iterator<PGPSecretKeyRing> secColIt = secCol.getKeyRings();
|
|
|
|
while (secColIt.hasNext()) {
|
|
|
|
PGPSecretKeyRing r = secColIt.next();
|
2019-04-18 08:19:19 +02:00
|
|
|
LOGGER.log(Level.FINER, "" + r.getPublicKey().getKeyID());
|
2018-06-21 15:18:19 +02:00
|
|
|
secColSize++;
|
|
|
|
}
|
|
|
|
|
2019-04-18 08:19:19 +02:00
|
|
|
LOGGER.log(Level.FINER, "SecCol: " + secColSize);
|
2018-06-21 15:18:19 +02:00
|
|
|
|
|
|
|
PGPPublicKeyRingCollection pubCol = BCUtil.keyRingsToKeyRingCollection(pub);
|
|
|
|
|
|
|
|
int pubColSize = 0;
|
|
|
|
Iterator<PGPPublicKeyRing> pubColIt = pubCol.getKeyRings();
|
|
|
|
while (pubColIt.hasNext()) {
|
|
|
|
PGPPublicKeyRing r = pubColIt.next();
|
2019-04-18 08:19:19 +02:00
|
|
|
LOGGER.log(Level.FINER, "" + r.getPublicKey().getKeyID());
|
2018-06-21 15:18:19 +02:00
|
|
|
pubColSize++;
|
|
|
|
}
|
|
|
|
|
2019-04-18 08:19:19 +02:00
|
|
|
LOGGER.log(Level.FINER, "PubCol: " + pubColSize);
|
2018-06-11 01:33:49 +02:00
|
|
|
}
|
2018-06-28 15:45:30 +02:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void removeUnsignedKeysTest()
|
2020-01-12 23:34:47 +01:00
|
|
|
throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException {
|
2018-08-05 12:23:09 +02:00
|
|
|
@SuppressWarnings("deprecation")
|
2018-07-12 11:07:53 +02:00
|
|
|
PGPKeyRing alice = PGPainless.generateKeyRing().simpleRsaKeyRing("alice@wonderland.lit", RsaLength._1024);
|
|
|
|
PGPKeyRing mallory = PGPainless.generateKeyRing().simpleEcKeyRing("mallory@mall.ory");
|
2018-06-28 15:45:30 +02:00
|
|
|
|
|
|
|
PGPSecretKey subKey = null;
|
2018-07-12 11:07:53 +02:00
|
|
|
Iterator<PGPSecretKey> sit = mallory.getSecretKeys().getSecretKeys();
|
2018-06-28 15:45:30 +02:00
|
|
|
while (sit.hasNext()) {
|
|
|
|
PGPSecretKey s = sit.next();
|
|
|
|
if (!s.isMasterKey()) {
|
|
|
|
subKey = s;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-13 16:31:59 +01:00
|
|
|
assertNotNull(subKey);
|
2018-06-28 15:45:30 +02:00
|
|
|
|
2018-07-12 11:07:53 +02:00
|
|
|
PGPSecretKeyRing alice_mallory = PGPSecretKeyRing.insertSecretKey(alice.getSecretKeys(), subKey);
|
2018-06-28 15:45:30 +02:00
|
|
|
|
|
|
|
// Check, if alice_mallory contains mallory's key
|
2020-11-13 16:31:59 +01:00
|
|
|
assertNotNull(alice_mallory.getSecretKey(subKey.getKeyID()));
|
2018-06-28 15:45:30 +02:00
|
|
|
|
2018-07-12 11:07:53 +02:00
|
|
|
PGPSecretKeyRing cleaned = BCUtil.removeUnassociatedKeysFromKeyRing(alice_mallory, alice.getPublicKeys().getPublicKey());
|
2020-11-13 16:31:59 +01:00
|
|
|
assertNull(cleaned.getSecretKey(subKey.getKeyID()));
|
2018-06-28 15:45:30 +02:00
|
|
|
}
|
2018-07-11 14:17:04 +02:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void removeUnsignedKeysECTest()
|
|
|
|
throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException,
|
|
|
|
IOException {
|
2018-07-12 11:07:53 +02:00
|
|
|
PGPKeyRing ring = PGPainless.generateKeyRing().simpleEcKeyRing("alice@wonderland.lit");
|
|
|
|
PGPPublicKeyRing publicKeys = ring.getPublicKeys();
|
|
|
|
PGPSecretKeyRing secretKeys = ring.getSecretKeys();
|
|
|
|
PGPSecretKeyRing secCleaned = ring.getSecretKeys();
|
2018-07-11 14:17:04 +02:00
|
|
|
|
2020-01-12 23:34:47 +01:00
|
|
|
assertArrayEquals(secretKeys.getEncoded(), secCleaned.getEncoded());
|
2018-07-11 19:01:13 +02:00
|
|
|
|
|
|
|
PGPPublicKeyRing pubCleaned = BCUtil.removeUnassociatedKeysFromKeyRing(publicKeys, publicKeys.getPublicKey());
|
|
|
|
|
2020-01-12 23:34:47 +01:00
|
|
|
assertArrayEquals(publicKeys.getEncoded(), pubCleaned.getEncoded());
|
2018-07-11 14:17:04 +02:00
|
|
|
|
|
|
|
}
|
2018-06-11 01:33:49 +02:00
|
|
|
}
|