2020-10-20 22:58:45 +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-10-16 12:46:58 +02:00
|
|
|
package org.pgpainless.key.generation;
|
|
|
|
|
2020-11-13 16:31:59 +01:00
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
2020-10-16 13:18:07 +02:00
|
|
|
|
2020-10-16 12:46:58 +02:00
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.security.InvalidAlgorithmParameterException;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
2021-02-13 12:22:28 +01:00
|
|
|
import java.util.Date;
|
2020-10-16 13:18:07 +02:00
|
|
|
import java.util.Iterator;
|
2020-10-16 12:46:58 +02:00
|
|
|
|
|
|
|
import org.bouncycastle.bcpg.ArmoredOutputStream;
|
|
|
|
import org.bouncycastle.openpgp.PGPException;
|
2020-12-08 19:14:52 +01:00
|
|
|
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
|
|
|
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
2021-02-20 01:05:29 +01:00
|
|
|
import org.junit.JUtils;
|
2021-02-25 23:27:08 +01:00
|
|
|
import org.junit.jupiter.params.ParameterizedTest;
|
|
|
|
import org.junit.jupiter.params.provider.MethodSource;
|
2020-10-16 12:46:58 +02:00
|
|
|
import org.pgpainless.PGPainless;
|
2021-01-03 16:26:53 +01:00
|
|
|
import org.pgpainless.algorithm.KeyFlag;
|
2021-02-25 23:27:08 +01:00
|
|
|
import org.pgpainless.implementation.ImplementationFactory;
|
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-02-19 19:50:36 +01:00
|
|
|
import org.pgpainless.util.ArmoredOutputStreamFactory;
|
2020-10-16 12:46:58 +02:00
|
|
|
|
|
|
|
public class GenerateKeyWithAdditionalUserIdTest {
|
|
|
|
|
2021-02-25 23:27:08 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("org.pgpainless.util.TestUtil#provideImplementationFactories")
|
|
|
|
public void test(ImplementationFactory implementationFactory) throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException, IOException {
|
|
|
|
ImplementationFactory.setFactoryImplementation(implementationFactory);
|
2021-02-13 12:22:28 +01:00
|
|
|
Date now = new Date();
|
2021-02-20 01:05:29 +01:00
|
|
|
Date expiration = new Date(now.getTime() + 1000 * 60);
|
2020-12-08 19:14:52 +01:00
|
|
|
PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing()
|
2021-02-11 17:18:59 +01:00
|
|
|
.withPrimaryKey(KeySpec.getBuilder(KeyType.RSA(RsaLength._3072))
|
2021-01-03 16:26:53 +01:00
|
|
|
.withKeyFlags(KeyFlag.CERTIFY_OTHER, KeyFlag.SIGN_DATA, KeyFlag.ENCRYPT_COMMS)
|
2020-10-16 12:46:58 +02:00
|
|
|
.withDefaultAlgorithms())
|
|
|
|
.withPrimaryUserId("primary@user.id")
|
|
|
|
.withAdditionalUserId("additional@user.id")
|
2020-10-20 22:14:56 +02:00
|
|
|
.withAdditionalUserId("additional2@user.id")
|
2020-10-20 23:23:25 +02:00
|
|
|
.withAdditionalUserId("\ttrimThis@user.id ")
|
2021-02-13 12:22:28 +01:00
|
|
|
.setExpirationDate(expiration)
|
2020-10-16 12:46:58 +02:00
|
|
|
.withoutPassphrase()
|
|
|
|
.build();
|
2020-12-08 19:14:52 +01:00
|
|
|
PGPPublicKeyRing publicKeys = KeyRingUtils.publicKeyRingFrom(secretKeys);
|
2020-10-16 12:46:58 +02:00
|
|
|
|
2021-04-26 13:38:12 +02:00
|
|
|
JUtils.assertEquals(expiration.getTime(), PGPainless.inspectKeyRing(publicKeys).getPrimaryKeyExpirationDate().getTime(),2000);
|
2021-02-13 12:22:28 +01:00
|
|
|
|
2020-12-08 19:14:52 +01:00
|
|
|
Iterator<String> userIds = publicKeys.getPublicKey().getUserIDs();
|
2020-10-16 13:18:07 +02:00
|
|
|
assertEquals("primary@user.id", userIds.next());
|
|
|
|
assertEquals("additional@user.id", userIds.next());
|
2020-10-20 22:14:56 +02:00
|
|
|
assertEquals("additional2@user.id", userIds.next());
|
2020-10-20 23:23:25 +02:00
|
|
|
assertEquals("trimThis@user.id", userIds.next());
|
2020-10-16 13:18:07 +02:00
|
|
|
assertFalse(userIds.hasNext());
|
|
|
|
|
2020-10-16 12:46:58 +02:00
|
|
|
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
|
2021-02-19 19:50:36 +01:00
|
|
|
ArmoredOutputStream armor = ArmoredOutputStreamFactory.get(byteOut);
|
2020-12-08 19:14:52 +01:00
|
|
|
secretKeys.encode(armor);
|
2020-10-16 12:46:58 +02:00
|
|
|
armor.close();
|
|
|
|
|
2020-10-16 13:18:07 +02:00
|
|
|
// echo this | gpg --list-packets
|
2020-10-20 22:58:45 +02:00
|
|
|
// CHECKSTYLE:OFF
|
2020-10-16 12:46:58 +02:00
|
|
|
System.out.println(byteOut.toString("UTF-8"));
|
2020-10-20 22:58:45 +02:00
|
|
|
// CHECKSTYLE:ON
|
2020-10-16 12:46:58 +02:00
|
|
|
}
|
|
|
|
}
|