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.assertEquals;
|
2018-06-11 01:33:49 +02:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.security.InvalidAlgorithmParameterException;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
|
|
|
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-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;
|
2021-01-03 16:26:53 +01:00
|
|
|
import org.pgpainless.algorithm.KeyFlag;
|
2018-07-18 18:23:06 +02:00
|
|
|
import org.pgpainless.key.generation.KeySpec;
|
2020-11-07 18:24:12 +01:00
|
|
|
import org.pgpainless.key.generation.type.KeyType;
|
2020-12-08 20:02:41 +01:00
|
|
|
import org.pgpainless.key.generation.type.rsa.RsaLength;
|
2020-12-08 19:14:52 +01:00
|
|
|
import org.pgpainless.key.util.KeyRingUtils;
|
2021-08-23 13:29:57 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
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
|
|
|
|
2021-08-23 13:29:57 +02:00
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(BCUtilTest.class);
|
2018-06-21 15:18:19 +02:00
|
|
|
|
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 {
|
2020-12-08 19:14:52 +01:00
|
|
|
PGPSecretKeyRing sec = PGPainless.generateKeyRing()
|
2021-09-20 12:30:03 +02:00
|
|
|
.setPrimaryKey(KeySpec.getBuilder(
|
2021-09-13 19:20:19 +02:00
|
|
|
KeyType.RSA(RsaLength._3072),
|
2021-09-20 12:30:03 +02:00
|
|
|
KeyFlag.CERTIFY_OTHER, KeyFlag.SIGN_DATA))
|
|
|
|
.addSubkey(KeySpec.getBuilder(KeyType.RSA(RsaLength._3072), KeyFlag.ENCRYPT_COMMS))
|
|
|
|
.addUserId("donald@duck.tails")
|
|
|
|
.build();
|
2020-12-08 19:14:52 +01:00
|
|
|
|
|
|
|
PGPPublicKeyRing pub = KeyRingUtils.publicKeyRingFrom(sec);
|
2018-06-11 01:33:49 +02:00
|
|
|
|
2021-08-23 13:29:57 +02:00
|
|
|
assertEquals(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();
|
2021-08-23 13:29:57 +02:00
|
|
|
LOGGER.debug("Index {}, keyId {}, isEncryptionKey={}, isPrimary={}", secSize, k.getKeyID(), k.isEncryptionKey(), k.isMasterKey());
|
2018-06-11 01:33:49 +02:00
|
|
|
secSize++;
|
|
|
|
}
|
|
|
|
|
2021-08-23 13:29:57 +02:00
|
|
|
LOGGER.debug("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();
|
2021-08-23 13:29:57 +02:00
|
|
|
LOGGER.debug("Index {}, keyId {}, isEncryptionKey={}, isPrimary={}", 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
|
|
|
|
2021-05-29 12:43:31 +02:00
|
|
|
PGPSecretKeyRingCollection secCol = KeyRingUtils.keyRingsToKeyRingCollection(sec);
|
2018-06-21 15:18:19 +02:00
|
|
|
|
|
|
|
int secColSize = 0;
|
|
|
|
Iterator<PGPSecretKeyRing> secColIt = secCol.getKeyRings();
|
|
|
|
while (secColIt.hasNext()) {
|
|
|
|
PGPSecretKeyRing r = secColIt.next();
|
2021-08-23 13:29:57 +02:00
|
|
|
LOGGER.debug("{}", r.getPublicKey().getKeyID());
|
2018-06-21 15:18:19 +02:00
|
|
|
secColSize++;
|
|
|
|
}
|
|
|
|
|
2021-05-29 12:43:31 +02:00
|
|
|
PGPPublicKeyRingCollection pubCol = KeyRingUtils.keyRingsToKeyRingCollection(pub);
|
2018-06-21 15:18:19 +02:00
|
|
|
|
|
|
|
int pubColSize = 0;
|
|
|
|
Iterator<PGPPublicKeyRing> pubColIt = pubCol.getKeyRings();
|
|
|
|
while (pubColIt.hasNext()) {
|
|
|
|
PGPPublicKeyRing r = pubColIt.next();
|
2021-08-23 13:29:57 +02:00
|
|
|
LOGGER.debug("{}", r.getPublicKey().getKeyID());
|
2018-06-21 15:18:19 +02:00
|
|
|
pubColSize++;
|
|
|
|
}
|
|
|
|
|
2021-08-23 13:29:57 +02:00
|
|
|
assertEquals(pubColSize, secColSize);
|
2018-06-11 01:33:49 +02:00
|
|
|
}
|
|
|
|
}
|