183 lines
8.8 KiB
Java
183 lines
8.8 KiB
Java
/*
|
|
* Copyright 2020 Paul Schaub. Copyright 2021 Flowcrypt a.s.
|
|
*
|
|
* 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.pgpainless.key.info;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
|
import java.io.IOException;
|
|
import java.util.Collections;
|
|
import java.util.Date;
|
|
|
|
import org.bouncycastle.openpgp.PGPException;
|
|
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
|
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.pgpainless.PGPainless;
|
|
import org.pgpainless.algorithm.PublicKeyAlgorithm;
|
|
import org.pgpainless.key.TestKeys;
|
|
import org.pgpainless.key.protection.UnprotectedKeysProtector;
|
|
import org.pgpainless.key.util.KeyRingUtils;
|
|
import org.pgpainless.util.ArmorUtils;
|
|
import org.pgpainless.util.Passphrase;
|
|
|
|
public class KeyRingInfoTest {
|
|
|
|
@Test
|
|
public void testWithEmilsKeys() throws IOException, PGPException {
|
|
PGPSecretKeyRing secretKeys = TestKeys.getEmilSecretKeyRing();
|
|
PGPPublicKeyRing publicKeys = TestKeys.getEmilPublicKeyRing();
|
|
KeyRingInfo sInfo = PGPainless.inspectKeyRing(secretKeys);
|
|
KeyRingInfo pInfo = PGPainless.inspectKeyRing(publicKeys);
|
|
|
|
assertEquals(TestKeys.EMIL_KEY_ID, sInfo.getKeyId());
|
|
assertEquals(TestKeys.EMIL_KEY_ID, pInfo.getKeyId());
|
|
assertEquals(TestKeys.EMIL_FINGERPRINT, sInfo.getFingerprint());
|
|
assertEquals(TestKeys.EMIL_FINGERPRINT, pInfo.getFingerprint());
|
|
assertEquals(PublicKeyAlgorithm.ECDSA, sInfo.getAlgorithm());
|
|
assertEquals(PublicKeyAlgorithm.ECDSA, pInfo.getAlgorithm());
|
|
|
|
assertEquals(2, sInfo.getPublicKeys().size());
|
|
assertEquals(2, pInfo.getPublicKeys().size());
|
|
|
|
assertEquals(Collections.singletonList("<emil@email.user>"), sInfo.getUserIds());
|
|
assertEquals(Collections.singletonList("<emil@email.user>"), pInfo.getUserIds());
|
|
assertEquals(Collections.singletonList("emil@email.user"), sInfo.getEmailAddresses());
|
|
assertEquals(Collections.singletonList("emil@email.user"), pInfo.getEmailAddresses());
|
|
|
|
assertTrue(sInfo.isSecretKey());
|
|
assertFalse(pInfo.isSecretKey());
|
|
assertTrue(sInfo.isFullyDecrypted());
|
|
assertTrue(pInfo.isFullyDecrypted());
|
|
|
|
assertEquals(TestKeys.EMIL_CREATION_DATE, sInfo.getCreationDate());
|
|
assertEquals(TestKeys.EMIL_CREATION_DATE, pInfo.getCreationDate());
|
|
assertNull(sInfo.getPrimaryKeyExpirationDate());
|
|
assertNull(pInfo.getPrimaryKeyExpirationDate());
|
|
assertEquals(TestKeys.EMIL_CREATION_DATE.getTime(), sInfo.getLastModified().getTime(), 50);
|
|
assertEquals(TestKeys.EMIL_CREATION_DATE.getTime(), pInfo.getLastModified().getTime(), 50);
|
|
|
|
assertNull(sInfo.getRevocationDate());
|
|
assertNull(pInfo.getRevocationDate());
|
|
Date revocationDate = new Date();
|
|
PGPSecretKeyRing revoked = PGPainless.modifyKeyRing(secretKeys).revoke(new UnprotectedKeysProtector()).done();
|
|
// CHECKSTYLE:OFF
|
|
System.out.println(ArmorUtils.toAsciiArmoredString(revoked));
|
|
// CHECKSTYLE:ON
|
|
KeyRingInfo rInfo = PGPainless.inspectKeyRing(revoked);
|
|
assertNotNull(rInfo.getRevocationDate());
|
|
assertEquals(revocationDate.getTime(), rInfo.getRevocationDate().getTime(), 1000);
|
|
assertEquals(revocationDate.getTime(), rInfo.getLastModified().getTime(), 1000);
|
|
}
|
|
|
|
@Test
|
|
public void testIsFullyDecrypted() throws IOException, PGPException {
|
|
PGPSecretKeyRing secretKeys = TestKeys.getEmilSecretKeyRing();
|
|
KeyRingInfo info = PGPainless.inspectKeyRing(secretKeys);
|
|
|
|
assertTrue(info.isFullyDecrypted());
|
|
|
|
secretKeys = encryptSecretKeys(secretKeys);
|
|
info = PGPainless.inspectKeyRing(secretKeys);
|
|
|
|
assertFalse(info.isFullyDecrypted());
|
|
}
|
|
|
|
@Test
|
|
public void testIsFullyEncrypted() throws IOException, PGPException {
|
|
PGPSecretKeyRing secretKeys = TestKeys.getEmilSecretKeyRing();
|
|
KeyRingInfo info = PGPainless.inspectKeyRing(secretKeys);
|
|
|
|
assertFalse(info.isFullyEncrypted());
|
|
|
|
secretKeys = encryptSecretKeys(secretKeys);
|
|
info = PGPainless.inspectKeyRing(secretKeys);
|
|
|
|
assertTrue(info.isFullyEncrypted());
|
|
}
|
|
|
|
private static PGPSecretKeyRing encryptSecretKeys(PGPSecretKeyRing secretKeys) throws PGPException {
|
|
return PGPainless.modifyKeyRing(secretKeys)
|
|
.changePassphraseFromOldPassphrase(null)
|
|
.withSecureDefaultSettings()
|
|
.toNewPassphrase(Passphrase.fromPassword("sw0rdf1sh"))
|
|
.done();
|
|
}
|
|
|
|
|
|
@Test
|
|
public void testGetSecretKey() throws IOException, PGPException {
|
|
PGPSecretKeyRing secretKeys = TestKeys.getCryptieSecretKeyRing();
|
|
PGPPublicKeyRing publicKeys = KeyRingUtils.publicKeyRingFrom(secretKeys);
|
|
|
|
KeyRingInfo info = PGPainless.inspectKeyRing(secretKeys);
|
|
assertEquals(KeyRingUtils.requirePrimarySecretKeyFrom(secretKeys), info.getSecretKey());
|
|
|
|
info = PGPainless.inspectKeyRing(publicKeys);
|
|
assertNull(info.getSecretKey());
|
|
}
|
|
|
|
@Test
|
|
public void testGetPublicKey() throws IOException, PGPException {
|
|
PGPSecretKeyRing secretKeys = TestKeys.getCryptieSecretKeyRing();
|
|
|
|
KeyRingInfo info = PGPainless.inspectKeyRing(secretKeys);
|
|
assertEquals(KeyRingUtils.requirePrimaryPublicKeyFrom(secretKeys), info.getPublicKey());
|
|
|
|
assertEquals(KeyRingUtils.requirePrimarySecretKeyFrom(secretKeys),
|
|
KeyRingUtils.requireSecretKeyFrom(secretKeys, secretKeys.getPublicKey().getKeyID()));
|
|
}
|
|
|
|
@Test
|
|
public void dummyS2KTest() throws PGPException, IOException {
|
|
String withDummyS2K = "-----BEGIN PGP PRIVATE KEY BLOCK-----\n" +
|
|
"\n" +
|
|
"lQCVBFZuSwwBBAC04VdUUq2REb7+IF/x21yOV3kIn798XRl7A7RiGcE9VpBjT5xM\n" +
|
|
"xtghWhH1mxyT+nrS36OJxdvtgJb3NB6hhh3qBQC6DmCGbWe61tT6TfyFbN6OvzZK\n" +
|
|
"MEa6RMunyd+2ErX4RLOcO+9X7a0weVASH5wRYjjqQtvPvt1/k25sloPnZQARAQAB\n" +
|
|
"/gNlAkdOVQG0EyA8dGVzdEBleGFtcGxlLmNvbT6IuAQTAQIAIgUCVm5LDAIbLwYL\n" +
|
|
"CQgHAwIGFQgCCQoLBBYCAwECHgECF4AACgkQwaXYFkPfLEVROQP/RF4GXi/yGm6y\n" +
|
|
"QoDNXFkFiwNhJndayfZxf5Qa+JWz1ltLyal7Dm1c+U6/R/7D25gmEslI+5YrHpbE\n" +
|
|
"xWXyfG8DbX/5Ef9Be04e9IvjoZboeRpxmyb8IflEw90tJGL8YAK2xWohvayigPnj\n" +
|
|
"jhycZQPMuMK9X35o89oJs+p1MxcC9EOwAgA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|
"LkHfQMWMR11sA/0YQl4wf3knjk83DVVhFK5fT2lW4hmSO74tuCAA4V71C8B5rJzV\n" +
|
|
"q2vy1L2bGHAroe+LtX30LtZM5qWKzZzK7jjo1/eaXimOkJcnnpg6jmUP7TMkWpU7\n" +
|
|
"hlOQ3ZHjS2K5xJYJqBwP86TWPtDLxYD3mTlYtp2dDT8ogV/sEPPd44yWlrACAAA=\n" +
|
|
"=gU+0\n" +
|
|
"-----END PGP PRIVATE KEY BLOCK-----\n";
|
|
|
|
PGPSecretKeyRing secretKeys = PGPainless.readKeyRing().secretKeyRing(withDummyS2K);
|
|
assertTrue(new KeyInfo(secretKeys.getSecretKey()).hasDummyS2K());
|
|
}
|
|
}
|