1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-09-10 10:49:39 +02:00

Wip: SOP 4

This commit is contained in:
Paul Schaub 2022-06-07 08:55:10 +02:00
parent 9cdea63ec4
commit 9a545a2936
16 changed files with 429 additions and 58 deletions

View file

@ -28,7 +28,7 @@ public class ArmorTest {
@Test
public void armor() throws PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, IOException {
byte[] data = PGPainless.generateKeyRing().modernKeyRing("Alice", null).getEncoded();
byte[] data = PGPainless.generateKeyRing().modernKeyRing("Alice").getEncoded();
byte[] knownGoodArmor = ArmorUtils.toAsciiArmoredString(data).getBytes(StandardCharsets.UTF_8);
byte[] armored = new SOPImpl()
.armor()

View file

@ -58,7 +58,7 @@ public class DetachInbandSignatureAndMessageTest {
signingStream.close();
// actually detach the message
ByteArrayAndResult<Signatures> detachedMsg = sop.detachInbandSignatureAndMessage()
ByteArrayAndResult<Signatures> detachedMsg = sop.inlineDetach()
.message(out.toByteArray())
.toByteArrayAndResult();

View file

@ -11,6 +11,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.bouncycastle.util.io.Streams;
@ -24,12 +25,13 @@ import sop.exception.SOPGPException;
public class EncryptDecryptRoundTripTest {
private static final Charset utf8 = Charset.forName("UTF8");
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);
private static byte[] message = "Hello, World!\n".getBytes(utf8);
@BeforeAll
public static void setup() throws IOException {
@ -218,8 +220,119 @@ public class EncryptDecryptRoundTripTest {
"=MUYS\n" +
"-----END PGP PRIVATE KEY BLOCK-----";
String msg = "-----BEGIN PGP MESSAGE-----\n" +
"Version: PGPainless\n" +
"\n" +
"hF4Doj0CaB2GRvISAQdAhV5sjUCxanM68jG9qaq2rep1KKQx2o+9yrK0Rsrtqkww\n" +
"mb4uVv/SD3ixDztUSgUset0jeUeZHZAWfTB9cWawX4fiB2BdbcxhxFqQR8VPJ2SZ\n" +
"0jcB+wH1gq05AkMaCfoEIio3o3QcZq2In8tqj69U3AFRQApoH/p+ZLDz2pcnFBn+\n" +
"x1Y+C6wNg/3g\n" +
"=6vge\n" +
"-----END PGP MESSAGE-----";
assertThrows(SOPGPException.KeyIsProtected.class, () -> sop.decrypt()
.withKey(passwordProtectedKey.getBytes(StandardCharsets.UTF_8)));
.withKey(passwordProtectedKey.getBytes(StandardCharsets.UTF_8))
.ciphertext(msg.getBytes(utf8)));
}
@Test
public void encryptDecryptRoundTripWithProtectedKey() throws IOException {
byte[] passphrase = "sw0rdf1sh".getBytes(utf8);
byte[] key = sop.generateKey()
.userId("Alice <alice@pgpainless.org>")
.withKeyPassword(passphrase)
.generate().getBytes();
byte[] cert = sop.extractCert()
.key(key)
.getBytes();
byte[] plaintext = "Hello, World!\n".getBytes(utf8);
byte[] ciphertext = sop.encrypt()
.withCert(cert)
.plaintext(plaintext)
.getBytes();
byte[] decrypted = sop.decrypt()
.withKeyPassword(passphrase)
.withKey(key)
.ciphertext(ciphertext)
.toByteArrayAndResult()
.getBytes();
assertArrayEquals(plaintext, decrypted);
}
@Test
public void encryptDecryptRoundTripWithTwoProtectedKeysAndOnePassphrase() throws IOException {
byte[] passphrase1 = "sw0rdf1sh".getBytes(utf8);
byte[] key1 = sop.generateKey()
.userId("Alice <alice@pgpainless.org>")
.withKeyPassword(passphrase1)
.generate().getBytes();
byte[] cert1 = sop.extractCert()
.key(key1)
.getBytes();
byte[] passphrase2 = "fooBar".getBytes(utf8);
byte[] key2 = sop.generateKey()
.userId("Bob <bob@pgpainless.org>")
.withKeyPassword(passphrase2)
.generate().getBytes();
byte[] cert2 = sop.extractCert()
.key(key2)
.getBytes();
byte[] plaintext = "Hello, World!\n".getBytes(utf8);
byte[] ciphertext = sop.encrypt()
.withCert(cert1)
.withCert(cert2)
.plaintext(plaintext)
.getBytes();
byte[] decrypted = sop.decrypt()
.withKey(key1)
.withKey(key2)
.withKeyPassword(passphrase2)
.ciphertext(ciphertext)
.toByteArrayAndResult()
.getBytes();
assertArrayEquals(plaintext, decrypted);
}
@Test
public void encryptDecryptRoundTripFailsWithProtectedKeyAndWrongPassphrase() throws IOException {
byte[] passphrase = "sw0rdf1sh".getBytes(utf8);
byte[] key = sop.generateKey()
.userId("Alice <alice@pgpainless.org>")
.withKeyPassword(passphrase)
.generate().getBytes();
byte[] cert = sop.extractCert()
.key(key)
.getBytes();
byte[] plaintext = "Hello, World!\n".getBytes(utf8);
byte[] ciphertext = sop.encrypt()
.withCert(cert)
.plaintext(plaintext)
.getBytes();
assertThrows(SOPGPException.KeyIsProtected.class,
() -> sop.decrypt()
.withKeyPassword("foobar")
.withKey(key)
.ciphertext(ciphertext));
}
@Test