mirror of
https://github.com/pgpainless/pgpainless.git
synced 2025-01-08 19:27:57 +01:00
Add tests for pgpainless-sop
This commit is contained in:
parent
97c8ff8312
commit
f68779d8a5
4 changed files with 349 additions and 3 deletions
|
@ -105,11 +105,11 @@ public class PGPKeyRingCollection {
|
|||
pgpPublicKeyRingCollection = new PGPPublicKeyRingCollection(publicKeyRings);
|
||||
}
|
||||
|
||||
public PGPSecretKeyRingCollection getPGPSecretKeyRingCollection() {
|
||||
public @Nonnull PGPSecretKeyRingCollection getPGPSecretKeyRingCollection() {
|
||||
return pgpSecretKeyRingCollection;
|
||||
}
|
||||
|
||||
public PGPPublicKeyRingCollection getPgpPublicKeyRingCollection() {
|
||||
public @Nonnull PGPPublicKeyRingCollection getPgpPublicKeyRingCollection() {
|
||||
return pgpPublicKeyRingCollection;
|
||||
}
|
||||
|
||||
|
|
|
@ -64,7 +64,7 @@ public class DecryptImpl implements Decrypt {
|
|||
try {
|
||||
PGPPublicKeyRingCollection certs = PGPainless.readKeyRing().keyRingCollection(certIn, false)
|
||||
.getPgpPublicKeyRingCollection();
|
||||
if (certs == null) {
|
||||
if (certs.size() == 0) {
|
||||
throw new SOPGPException.BadData(new PGPException("No certificates provided."));
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,247 @@
|
|||
/*
|
||||
* 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.pgpainless.sop;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import sop.ByteArrayAndResult;
|
||||
import sop.DecryptionResult;
|
||||
import sop.SOP;
|
||||
import sop.exception.SOPGPException;
|
||||
|
||||
public class EncryptDecryptRoundTripTest {
|
||||
|
||||
private static SOP sop;
|
||||
private static byte[] aliceKey;
|
||||
private static byte[] aliceCert;
|
||||
private static byte[] bobKey;
|
||||
private static byte[] bobCert;
|
||||
private static byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
@BeforeAll
|
||||
public static void setup() throws IOException {
|
||||
sop = new SOPImpl();
|
||||
aliceKey = sop.generateKey()
|
||||
.userId("Alice <alice@pgpainless.org>")
|
||||
.generate()
|
||||
.getBytes();
|
||||
aliceCert = sop.extractCert()
|
||||
.key(new ByteArrayInputStream(aliceKey))
|
||||
.getBytes();
|
||||
bobKey = sop.generateKey()
|
||||
.userId("Bob <bob@pgpainless.org>")
|
||||
.generate()
|
||||
.getBytes();
|
||||
bobCert = sop.extractCert()
|
||||
.key(new ByteArrayInputStream(bobKey))
|
||||
.getBytes();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void basicRoundTripWithKey() throws IOException, SOPGPException.CertCannotSign {
|
||||
byte[] encrypted = sop.encrypt()
|
||||
.signWith(new ByteArrayInputStream(aliceKey))
|
||||
.withCert(new ByteArrayInputStream(aliceCert))
|
||||
.withCert(new ByteArrayInputStream(bobCert))
|
||||
.plaintext(new ByteArrayInputStream(message))
|
||||
.getBytes();
|
||||
|
||||
ByteArrayAndResult<DecryptionResult> bytesAndResult = sop.decrypt()
|
||||
.withKey(new ByteArrayInputStream(bobKey))
|
||||
.verifyWithCert(new ByteArrayInputStream(aliceCert))
|
||||
.ciphertext(new ByteArrayInputStream(encrypted))
|
||||
.toBytes();
|
||||
|
||||
byte[] decrypted = bytesAndResult.getBytes();
|
||||
assertArrayEquals(message, decrypted);
|
||||
|
||||
DecryptionResult result = bytesAndResult.getResult();
|
||||
assertEquals(1, result.getVerifications().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void basicRoundTripWithoutArmorUsingKey() throws IOException, SOPGPException.CertCannotSign {
|
||||
byte[] aliceKeyNoArmor = sop.generateKey()
|
||||
.userId("Alice <alice@unarmored.org>")
|
||||
.noArmor()
|
||||
.generate()
|
||||
.getBytes();
|
||||
byte[] aliceCertNoArmor = sop.extractCert()
|
||||
.noArmor()
|
||||
.key(new ByteArrayInputStream(aliceKeyNoArmor))
|
||||
.getBytes();
|
||||
byte[] encrypted = sop.encrypt()
|
||||
.signWith(new ByteArrayInputStream(aliceKeyNoArmor))
|
||||
.withCert(new ByteArrayInputStream(aliceCertNoArmor))
|
||||
.noArmor()
|
||||
.plaintext(new ByteArrayInputStream(message))
|
||||
.getBytes();
|
||||
|
||||
ByteArrayAndResult<DecryptionResult> bytesAndResult = sop.decrypt()
|
||||
.withKey(new ByteArrayInputStream(aliceKeyNoArmor))
|
||||
.verifyWithCert(new ByteArrayInputStream(aliceCertNoArmor))
|
||||
.ciphertext(new ByteArrayInputStream(encrypted))
|
||||
.toBytes();
|
||||
|
||||
byte[] decrypted = bytesAndResult.getBytes();
|
||||
assertArrayEquals(message, decrypted);
|
||||
|
||||
DecryptionResult result = bytesAndResult.getResult();
|
||||
assertEquals(1, result.getVerifications().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void basicRoundTripWithPassword() throws IOException {
|
||||
byte[] encrypted = sop.encrypt()
|
||||
.withPassword("passphr4s3")
|
||||
.plaintext(new ByteArrayInputStream(message))
|
||||
.getBytes();
|
||||
|
||||
ByteArrayAndResult<DecryptionResult> bytesAndResult = sop.decrypt()
|
||||
.withPassword("passphr4s3")
|
||||
.ciphertext(new ByteArrayInputStream(encrypted))
|
||||
.toBytes();
|
||||
|
||||
byte[] decrypted = bytesAndResult.getBytes();
|
||||
assertArrayEquals(message, decrypted);
|
||||
|
||||
DecryptionResult result = bytesAndResult.getResult();
|
||||
assertEquals(0, result.getVerifications().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void roundTripWithDecryptionPasswordContainingWhitespace() throws IOException {
|
||||
byte[] encrypted = sop.encrypt()
|
||||
.withPassword("passphr4s3")
|
||||
.plaintext(new ByteArrayInputStream(message))
|
||||
.getBytes();
|
||||
|
||||
ByteArrayAndResult<DecryptionResult> bytesAndResult = sop.decrypt()
|
||||
.withPassword("passphr4s3 ") // whitespace is removed
|
||||
.ciphertext(new ByteArrayInputStream(encrypted))
|
||||
.toBytes();
|
||||
|
||||
byte[] decrypted = bytesAndResult.getBytes();
|
||||
assertArrayEquals(message, decrypted);
|
||||
|
||||
DecryptionResult result = bytesAndResult.getResult();
|
||||
assertEquals(0, result.getVerifications().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void roundTripWithEncryptionPasswordContainingWhitespace() throws IOException {
|
||||
byte[] encrypted = sop.encrypt()
|
||||
.withPassword("passphr4s3 ")
|
||||
.plaintext(new ByteArrayInputStream(message))
|
||||
.getBytes();
|
||||
|
||||
ByteArrayAndResult<DecryptionResult> bytesAndResult = sop.decrypt()
|
||||
.withPassword("passphr4s3 ")
|
||||
.ciphertext(new ByteArrayInputStream(encrypted))
|
||||
.toBytes();
|
||||
|
||||
byte[] decrypted = bytesAndResult.getBytes();
|
||||
assertArrayEquals(message, decrypted);
|
||||
|
||||
DecryptionResult result = bytesAndResult.getResult();
|
||||
assertEquals(0, result.getVerifications().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encrypt_decryptAndVerifyYieldsNoSignatureException() throws IOException {
|
||||
byte[] encrypted = sop.encrypt()
|
||||
.withCert(new ByteArrayInputStream(bobCert))
|
||||
.plaintext(new ByteArrayInputStream(message))
|
||||
.getBytes();
|
||||
|
||||
assertThrows(SOPGPException.NoSignature.class, () -> sop
|
||||
.decrypt()
|
||||
.withKey(new ByteArrayInputStream(bobKey))
|
||||
.verifyWithCert(new ByteArrayInputStream(aliceCert))
|
||||
.ciphertext(new ByteArrayInputStream(encrypted))
|
||||
.toBytes());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encrypt_decryptWithoutKeyOrPassphraseYieldsMissingArgException() throws IOException {
|
||||
byte[] encrypted = sop.encrypt()
|
||||
.withCert(new ByteArrayInputStream(bobCert))
|
||||
.plaintext(new ByteArrayInputStream(message))
|
||||
.getBytes();
|
||||
|
||||
assertThrows(SOPGPException.MissingArg.class, () -> sop
|
||||
.decrypt()
|
||||
.ciphertext(new ByteArrayInputStream(encrypted))
|
||||
.toBytes());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decrypt_withKeyWithMultipleKeysFails() {
|
||||
byte[] keys = new byte[aliceKey.length + bobKey.length];
|
||||
System.arraycopy(aliceKey, 0, keys, 0 , aliceKey.length);
|
||||
System.arraycopy(bobKey, 0, keys, aliceKey.length, bobKey.length);
|
||||
|
||||
assertThrows(SOPGPException.BadData.class, () -> sop.decrypt()
|
||||
.withKey(new ByteArrayInputStream(keys)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decrypt_withKeyWithPasswordProtectionFails() {
|
||||
String passwordProtectedKey = "-----BEGIN PGP PRIVATE KEY BLOCK-----\n" +
|
||||
"Version: PGPainless\n" +
|
||||
"Comment: 1777 B08F B9FE 25B8 E32C E2E2 5974 2F3E DEC0 1772\n" +
|
||||
"Comment: Protected <protected@pgpainless.org>\n" +
|
||||
"\n" +
|
||||
"lIYEYSztkRYJKwYBBAHaRw8BAQdAMAcay1YOujgMVAXQsAz/QwL2PHc8EBroTQpe\n" +
|
||||
"Skizmyf+CQMC03aJCFqkseNgXBlZLknishdIO2iMz5YT2ptOjDkYLenEc71LqDTZ\n" +
|
||||
"abhG9vjfaMtfUvfF6qZurbvVAC0QBr/cSVREuEqDCxpbmYKeFrMz6L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|
||||
"99Ts4FiwRLQJAAoJEFl0Lz7ewBdyaysA/jk61StphMpfuRsuQwznH7L7SddNcZ1k\n" +
|
||||
"l1wHK5kRJXyCAP0WUgkpEcM2bpwb4sxkCDxrfk/ixc47hGa68MPWwOJkAQ==\n" +
|
||||
"=MUYS\n" +
|
||||
"-----END PGP PRIVATE KEY BLOCK-----";
|
||||
|
||||
assertThrows(SOPGPException.KeyIsProtected.class, () -> sop.decrypt()
|
||||
.withKey(new ByteArrayInputStream(passwordProtectedKey.getBytes(StandardCharsets.UTF_8))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void verifyWith_noDataThrowsBadData() {
|
||||
assertThrows(SOPGPException.BadData.class, () -> sop.decrypt()
|
||||
.verifyWithCert(new ByteArrayInputStream(new byte[0])));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,99 @@
|
|||
/*
|
||||
* 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.pgpainless.sop;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import sop.SOP;
|
||||
import sop.exception.SOPGPException;
|
||||
|
||||
public class ExtractCertTest {
|
||||
|
||||
public static final String key = "-----BEGIN PGP PRIVATE KEY BLOCK-----\n" +
|
||||
"Version: PGPainless\n" +
|
||||
"Comment: A8D9 9FF4 C8DD BBA6 C610 A6B7 9ACB 2195 A9BC DF5B\n" +
|
||||
"Comment: Alice <alice@pgpainless.org>\n" +
|
||||
"\n" +
|
||||
"lFgEYSzwMhYJKwYBBAHaRw8BAQdA60pbfTLh5MB1Ka5KfZqUzMhHzJHYBvXF68mW\n" +
|
||||
"BMzgupIAAPsHWal9lDZzNXUE8Xnt00IUFYhOC5P73FMLGqdpsA+fQw51tBxBbGlj\n" +
|
||||
"ZSA8YWxpY2VAcGdwYWlubGVzcy5vcmc+iHgEExYKACAFAmEs8DICGwEFFgIDAQAE\n" +
|
||||
"CwkIBwUVCgkICwIeAQIZAQAKCRCayyGVqbzfW9EWAQDQbiKUpftQsK4IXJJ1d40H\n" +
|
||||
"fe7Djhm0P08Oo73GeE8vLwEA0ArOcyBbOETBBIefigVWWay6JIt57DGxR6KWQABk\n" +
|
||||
"AwKcXQRhLPAyEgorBgEEAZdVAQUBAQdAa3lioBiWVujoFINa2wVNPLjf/hc1aIPK\n" +
|
||||
"sbAcs83uRysDAQgHAAD/QJGlp9SjIzT9o2e+x9jyndOhyMPSmlLljW9ZtSuzmrgU\n" +
|
||||
"uYh1BBgWCgAdBQJhLPAyAhsMBRYCAwEABAsJCAcFFQoJCAsCHgEACgkQmsshlam8\n" +
|
||||
"31u3awEA0b/hCRQNrtsITuQGS1ikzonhJITpbmrg/ZVOvBn+3jYBAIC5d4Hozn1O\n" +
|
||||
"aBBl1ZiY3Bl2qIFYWzVR9vFOm+Va3lkEnFgEYSzwMhYJKwYBBAHaRw8BAQdAFfJi\n" +
|
||||
"f64K8E2ZlBqAAxO0eG7nIlxRYlOvbN/1vP8grW8AAP0cKOpo2uFqLzTnmzJ+rpmV\n" +
|
||||
"gwUW1FiHGpM/awfg+zzCgBAxiNUEGBYKAH0FAmEs8DICGwIFFgIDAQAECwkIBwUV\n" +
|
||||
"CgkICwIeAV8gBBkWCgAGBQJhLPAyAAoJEOVqq3ZdDwXc3WAA/2UaO79+srF3p/f9\n" +
|
||||
"scsmj7Rax8uXKw8sJPdgPMjmo404AQDor96bTeBiGOwPq0UfY4GGRmdkH8Z95PE+\n" +
|
||||
"fKyEbkjFAgAKCRCayyGVqbzfWwmUAP9AtTcZZPHa/gMYr5KXI+L7VRie9iolKII7\n" +
|
||||
"glyfG0/RUwEA3hTvAPRPAFG5WFNYaQprBAnAsefmdqwdDJGPfR7uGg0=\n" +
|
||||
"=BVSY\n" +
|
||||
"-----END PGP PRIVATE KEY BLOCK-----\n";
|
||||
|
||||
public static final String cert = "-----BEGIN PGP PUBLIC KEY BLOCK-----\n" +
|
||||
"Version: PGPainless\n" +
|
||||
"Comment: A8D9 9FF4 C8DD BBA6 C610 A6B7 9ACB 2195 A9BC DF5B\n" +
|
||||
"Comment: Alice <alice@pgpainless.org>\n" +
|
||||
"\n" +
|
||||
"mDMEYSzwMhYJKwYBBAHaRw8BAQdA60pbfTLh5MB1Ka5KfZqUzMhHzJHYBvXF68mW\n" +
|
||||
"BMzgupK0HEFsaWNlIDxhbGljZUBwZ3BhaW5sZXNzLm9yZz6IeAQTFgoAIAUCYSzw\n" +
|
||||
"MgIbAQUWAgMBAAQLCQgHBRUKCQgLAh4BAhkBAAoJEJrLIZWpvN9b0RYBANBuIpSl\n" +
|
||||
"+1CwrghcknV3jQd97sOOGbQ/Tw6jvcZ4Ty8vAQDQCs5zIFs4RMEEh5+KBVZZrLok\n" +
|
||||
"i3nsMbFHopZAAGQDArg4BGEs8DISCisGAQQBl1UBBQEBB0BreWKgGJZW6OgUg1rb\n" +
|
||||
"BU08uN/+FzVog8qxsByzze5HKwMBCAeIdQQYFgoAHQUCYSzwMgIbDAUWAgMBAAQL\n" +
|
||||
"CQgHBRUKCQgLAh4BAAoJEJrLIZWpvN9bt2sBANG/4QkUDa7bCE7kBktYpM6J4SSE\n" +
|
||||
"6W5q4P2VTrwZ/t42AQCAuXeB6M59TmgQZdWYmNwZdqiBWFs1UfbxTpvlWt5ZBLgz\n" +
|
||||
"BGEs8DIWCSsGAQQB2kcPAQEHQBXyYn+uCvBNmZQagAMTtHhu5yJcUWJTr2zf9bz/\n" +
|
||||
"IK1viNUEGBYKAH0FAmEs8DICGwIFFgIDAQAECwkIBwUVCgkICwIeAV8gBBkWCgAG\n" +
|
||||
"BQJhLPAyAAoJEOVqq3ZdDwXc3WAA/2UaO79+srF3p/f9scsmj7Rax8uXKw8sJPdg\n" +
|
||||
"PMjmo404AQDor96bTeBiGOwPq0UfY4GGRmdkH8Z95PE+fKyEbkjFAgAKCRCayyGV\n" +
|
||||
"qbzfWwmUAP9AtTcZZPHa/gMYr5KXI+L7VRie9iolKII7glyfG0/RUwEA3hTvAPRP\n" +
|
||||
"AFG5WFNYaQprBAnAsefmdqwdDJGPfR7uGg0=\n" +
|
||||
"=9qam\n" +
|
||||
"-----END PGP PUBLIC KEY BLOCK-----\n";
|
||||
|
||||
private static SOP sop;
|
||||
|
||||
@BeforeAll
|
||||
public static void setup() {
|
||||
sop = new SOPImpl();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void basicExtractCert() throws IOException {
|
||||
assertArrayEquals(
|
||||
cert.getBytes(StandardCharsets.UTF_8),
|
||||
sop.extractCert()
|
||||
.key(new ByteArrayInputStream(key.getBytes(StandardCharsets.UTF_8)))
|
||||
.getBytes());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void emptyKeyDataYieldsBadData() {
|
||||
assertThrows(SOPGPException.BadData.class, () -> sop.extractCert()
|
||||
.key(new ByteArrayInputStream(new byte[0])));
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue