More tests

This commit is contained in:
Paul Schaub 2023-01-21 20:31:49 +01:00
parent d09626782d
commit 9cf6301b8c
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
9 changed files with 319 additions and 134 deletions

View file

@ -30,7 +30,7 @@ public class ExternalArmorDearmorRoundTripTest extends AbstractExternalSOPTest {
@Test @Test
public void dearmorArmorAliceKey() throws IOException { public void dearmorArmorAliceKey() throws IOException {
byte[] aliceKey = TestKeys.ALICE_KEY.getBytes(StandardCharsets.UTF_8); byte[] aliceKey = TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8);
byte[] dearmored = getSop().dearmor() byte[] dearmored = getSop().dearmor()
.data(aliceKey) .data(aliceKey)
@ -48,7 +48,7 @@ public class ExternalArmorDearmorRoundTripTest extends AbstractExternalSOPTest {
@Test @Test
public void dearmorArmorAliceCert() throws IOException { public void dearmorArmorAliceCert() throws IOException {
byte[] aliceCert = TestKeys.ALICE_CERT.getBytes(StandardCharsets.UTF_8); byte[] aliceCert = TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8);
byte[] dearmored = getSop().dearmor() byte[] dearmored = getSop().dearmor()
.data(aliceCert) .data(aliceCert)
@ -66,7 +66,7 @@ public class ExternalArmorDearmorRoundTripTest extends AbstractExternalSOPTest {
@Test @Test
public void dearmorArmorBobKey() throws IOException { public void dearmorArmorBobKey() throws IOException {
byte[] bobKey = TestKeys.BOB_KEY.getBytes(StandardCharsets.UTF_8); byte[] bobKey = TestData.BOB_KEY.getBytes(StandardCharsets.UTF_8);
byte[] dearmored = getSop().dearmor() byte[] dearmored = getSop().dearmor()
.data(bobKey) .data(bobKey)
@ -84,7 +84,7 @@ public class ExternalArmorDearmorRoundTripTest extends AbstractExternalSOPTest {
@Test @Test
public void dearmorArmorBobCert() throws IOException { public void dearmorArmorBobCert() throws IOException {
byte[] bobCert = TestKeys.BOB_CERT.getBytes(StandardCharsets.UTF_8); byte[] bobCert = TestData.BOB_CERT.getBytes(StandardCharsets.UTF_8);
byte[] dearmored = getSop().dearmor() byte[] dearmored = getSop().dearmor()
.data(bobCert) .data(bobCert)
@ -102,7 +102,7 @@ public class ExternalArmorDearmorRoundTripTest extends AbstractExternalSOPTest {
@Test @Test
public void dearmorArmorCarolKey() throws IOException { public void dearmorArmorCarolKey() throws IOException {
byte[] carolKey = TestKeys.CAROL_KEY.getBytes(StandardCharsets.UTF_8); byte[] carolKey = TestData.CAROL_KEY.getBytes(StandardCharsets.UTF_8);
byte[] dearmored = getSop().dearmor() byte[] dearmored = getSop().dearmor()
.data(carolKey) .data(carolKey)
@ -120,7 +120,7 @@ public class ExternalArmorDearmorRoundTripTest extends AbstractExternalSOPTest {
@Test @Test
public void dearmorArmorCarolCert() throws IOException { public void dearmorArmorCarolCert() throws IOException {
byte[] carolCert = TestKeys.CAROL_CERT.getBytes(StandardCharsets.UTF_8); byte[] carolCert = TestData.CAROL_CERT.getBytes(StandardCharsets.UTF_8);
byte[] dearmored = getSop().dearmor() byte[] dearmored = getSop().dearmor()
.data(carolCert) .data(carolCert)
@ -191,7 +191,7 @@ public class ExternalArmorDearmorRoundTripTest extends AbstractExternalSOPTest {
ignoreIf("sqop", Is.eq, "0.27.2"); // IO error because: EOF ignoreIf("sqop", Is.eq, "0.27.2"); // IO error because: EOF
byte[] dearmored = getSop().dearmor() byte[] dearmored = getSop().dearmor()
.data(TestKeys.ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .data(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.getBytes(); .getBytes();
byte[] dearmoredAgain = getSop().dearmor() byte[] dearmoredAgain = getSop().dearmor()

View file

@ -32,7 +32,7 @@ public class ExternalDecryptWithSessionKeyTest extends AbstractExternalSOPTest {
@Test @Test
public void testDecryptAndExtractSessionKey() throws IOException { public void testDecryptAndExtractSessionKey() throws IOException {
ByteArrayAndResult<DecryptionResult> bytesAndResult = getSop().decrypt() ByteArrayAndResult<DecryptionResult> bytesAndResult = getSop().decrypt()
.withKey(TestKeys.ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .withKey(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.ciphertext(CIPHERTEXT.getBytes(StandardCharsets.UTF_8)) .ciphertext(CIPHERTEXT.getBytes(StandardCharsets.UTF_8))
.toByteArrayAndResult(); .toByteArrayAndResult();

View file

@ -9,7 +9,6 @@ import org.junit.jupiter.api.condition.EnabledIf;
import sop.Verification; import sop.Verification;
import sop.enums.SignAs; import sop.enums.SignAs;
import sop.exception.SOPGPException; import sop.exception.SOPGPException;
import sop.util.UTCUtil;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@ -18,8 +17,8 @@ import java.util.List;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static sop.external.JUtils.assertArrayStartsWith; import static sop.external.JUtils.assertArrayStartsWith;
import static sop.external.JUtils.assertSignedBy;
@EnabledIf("sop.external.AbstractExternalSOPTest#isExternalSopInstalled") @EnabledIf("sop.external.AbstractExternalSOPTest#isExternalSopInstalled")
public class ExternalDetachedSignVerifyRoundTripTest extends AbstractExternalSOPTest { public class ExternalDetachedSignVerifyRoundTripTest extends AbstractExternalSOPTest {
@ -29,50 +28,102 @@ public class ExternalDetachedSignVerifyRoundTripTest extends AbstractExternalSOP
@Test @Test
public void signVerifyWithAliceKey() throws IOException { public void signVerifyWithAliceKey() throws IOException {
byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] signature = getSop().detachedSign() byte[] signature = getSop().detachedSign()
.key(TestKeys.ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .key(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.data(message) .data(message)
.toByteArrayAndResult() .toByteArrayAndResult()
.getBytes(); .getBytes();
List<Verification> verificationList = getSop().detachedVerify() List<Verification> verificationList = getSop().detachedVerify()
.cert(TestKeys.ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.signatures(signature) .signatures(signature)
.data(message); .data(message);
assertFalse(verificationList.isEmpty()); assertFalse(verificationList.isEmpty());
assertTrue(verificationList.get(0).toString().contains("EB85BB5FA33A75E15E944E63F231550C4F47E38E EB85BB5FA33A75E15E944E63F231550C4F47E38E")); assertSignedBy(verificationList, TestData.ALICE_SIGNING_FINGERPRINT, TestData.ALICE_PRIMARY_FINGERPRINT);
} }
@Test @Test
public void signVerifyTextModeWithAliceKey() throws IOException { public void signVerifyTextModeWithAliceKey() throws IOException {
byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] signature = getSop().detachedSign() byte[] signature = getSop().detachedSign()
.key(TestKeys.ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .key(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.mode(SignAs.Text) .mode(SignAs.Text)
.data(message) .data(message)
.toByteArrayAndResult() .toByteArrayAndResult()
.getBytes(); .getBytes();
List<Verification> verificationList = getSop().detachedVerify() List<Verification> verificationList = getSop().detachedVerify()
.cert(TestKeys.ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.signatures(signature) .signatures(signature)
.data(message); .data(message);
assertFalse(verificationList.isEmpty()); assertFalse(verificationList.isEmpty());
assertTrue(verificationList.get(0).toString().contains("EB85BB5FA33A75E15E944E63F231550C4F47E38E EB85BB5FA33A75E15E944E63F231550C4F47E38E")); assertSignedBy(verificationList, TestData.ALICE_SIGNING_FINGERPRINT, TestData.ALICE_PRIMARY_FINGERPRINT);
}
@Test
public void verifyKnownMessageWithAliceCert() throws IOException {
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] signature = TestData.ALICE_DETACHED_SIGNED_MESSAGE.getBytes(StandardCharsets.UTF_8);
List<Verification> verificationList = getSop().detachedVerify()
.cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.signatures(signature)
.data(message);
assertFalse(verificationList.isEmpty());
assertSignedBy(verificationList, TestData.ALICE_SIGNING_FINGERPRINT, TestData.ALICE_PRIMARY_FINGERPRINT, TestData.ALICE_DETACHED_SIGNED_MESSAGE_DATE);
}
@Test
public void signVerifyWithBobKey() throws IOException {
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] signature = getSop().detachedSign()
.key(TestData.BOB_KEY.getBytes(StandardCharsets.UTF_8))
.data(message)
.toByteArrayAndResult()
.getBytes();
List<Verification> verificationList = getSop().detachedVerify()
.cert(TestData.BOB_CERT.getBytes(StandardCharsets.UTF_8))
.signatures(signature)
.data(message);
assertFalse(verificationList.isEmpty());
assertSignedBy(verificationList, TestData.BOB_SIGNING_FINGERPRINT, TestData.BOB_PRIMARY_FINGERPRINT);
}
@Test
public void signVerifyWithCarolKey() throws IOException {
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] signature = getSop().detachedSign()
.key(TestData.CAROL_KEY.getBytes(StandardCharsets.UTF_8))
.data(message)
.toByteArrayAndResult()
.getBytes();
List<Verification> verificationList = getSop().detachedVerify()
.cert(TestData.CAROL_CERT.getBytes(StandardCharsets.UTF_8))
.signatures(signature)
.data(message);
assertFalse(verificationList.isEmpty());
assertSignedBy(verificationList, TestData.CAROL_SIGNING_FINGERPRINT, TestData.CAROL_PRIMARY_FINGERPRINT);
} }
@Test @Test
public void signVerifyWithEncryptedKey() throws IOException { public void signVerifyWithEncryptedKey() throws IOException {
byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] signature = getSop().detachedSign() byte[] signature = getSop().detachedSign()
.key(TestKeys.PASSWORD_PROTECTED_KEY.getBytes(StandardCharsets.UTF_8)) .key(TestData.PASSWORD_PROTECTED_KEY.getBytes(StandardCharsets.UTF_8))
.withKeyPassword(TestKeys.PASSWORD) .withKeyPassword(TestData.PASSWORD)
.data(message) .data(message)
.toByteArrayAndResult() .toByteArrayAndResult()
.getBytes(); .getBytes();
@ -80,7 +131,7 @@ public class ExternalDetachedSignVerifyRoundTripTest extends AbstractExternalSOP
assertArrayStartsWith(signature, BEGIN_PGP_SIGNATURE_BYTES); assertArrayStartsWith(signature, BEGIN_PGP_SIGNATURE_BYTES);
List<Verification> verificationList = getSop().detachedVerify() List<Verification> verificationList = getSop().detachedVerify()
.cert(TestKeys.PASSWORD_PROTECTED_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.PASSWORD_PROTECTED_CERT.getBytes(StandardCharsets.UTF_8))
.signatures(signature) .signatures(signature)
.data(message); .data(message);
@ -89,10 +140,10 @@ public class ExternalDetachedSignVerifyRoundTripTest extends AbstractExternalSOP
@Test @Test
public void signArmorVerifyWithBobKey() throws IOException { public void signArmorVerifyWithBobKey() throws IOException {
byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] signature = getSop().detachedSign() byte[] signature = getSop().detachedSign()
.key(TestKeys.BOB_KEY.getBytes(StandardCharsets.UTF_8)) .key(TestData.BOB_KEY.getBytes(StandardCharsets.UTF_8))
.noArmor() .noArmor()
.data(message) .data(message)
.toByteArrayAndResult() .toByteArrayAndResult()
@ -103,31 +154,25 @@ public class ExternalDetachedSignVerifyRoundTripTest extends AbstractExternalSOP
.getBytes(); .getBytes();
List<Verification> verificationList = getSop().detachedVerify() List<Verification> verificationList = getSop().detachedVerify()
.cert(TestKeys.BOB_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.BOB_CERT.getBytes(StandardCharsets.UTF_8))
.signatures(armored) .signatures(armored)
.data(message); .data(message);
assertFalse(verificationList.isEmpty()); assertFalse(verificationList.isEmpty());
assertTrue(verificationList.get(0).toString().contains("D1A66E1A23B182C9980F788CFBFCC82A015E7330 D1A66E1A23B182C9980F788CFBFCC82A015E7330")); assertSignedBy(verificationList, TestData.BOB_SIGNING_FINGERPRINT, TestData.BOB_PRIMARY_FINGERPRINT);
} }
@Test @Test
public void verifyNotAfterThrowsNoSignature() { public void verifyNotAfterThrowsNoSignature() {
ignoreIf("sqop", Is.leq, "0.27.2"); // returns 1 instead of 3 (NO_SIGNATURE) ignoreIf("sqop", Is.leq, "0.27.2"); // returns 1 instead of 3 (NO_SIGNATURE)
byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] signature = ("-----BEGIN PGP SIGNATURE-----\n" + byte[] signature = TestData.ALICE_DETACHED_SIGNED_MESSAGE.getBytes(StandardCharsets.UTF_8);
"\n" + Date signatureDate = TestData.ALICE_DETACHED_SIGNED_MESSAGE_DATE;
"iHUEABYKACcFAmPBjZUJEPIxVQxPR+OOFiEE64W7X6M6deFelE5j8jFVDE9H444A\n" +
"ADI/AQC6Bux6WpGYf7HO+QPV/D5iIrqZt9xPLgfUVoNJBmMZZwD+Ib+tn5pSyWUw\n" +
"0K1UgT5roym9Fln8U5W8R03TSbfNiwE=\n" +
"=bxPN\n" +
"-----END PGP SIGNATURE-----").getBytes(StandardCharsets.UTF_8);
Date signatureDate = UTCUtil.parseUTCDate("2023-01-13T16:57:57Z");
Date beforeSignature = new Date(signatureDate.getTime() - 1000); // 1 sec before sig Date beforeSignature = new Date(signatureDate.getTime() - 1000); // 1 sec before sig
assertThrows(SOPGPException.NoSignature.class, () -> getSop().detachedVerify() assertThrows(SOPGPException.NoSignature.class, () -> getSop().detachedVerify()
.cert(TestKeys.ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.notAfter(beforeSignature) .notAfter(beforeSignature)
.signatures(signature) .signatures(signature)
.data(message)); .data(message));
@ -137,19 +182,13 @@ public class ExternalDetachedSignVerifyRoundTripTest extends AbstractExternalSOP
public void verifyNotBeforeThrowsNoSignature() { public void verifyNotBeforeThrowsNoSignature() {
ignoreIf("sqop", Is.leq, "0.27.2"); // returns 1 instead of 3 (NO_SIGNATURE) ignoreIf("sqop", Is.leq, "0.27.2"); // returns 1 instead of 3 (NO_SIGNATURE)
byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] signature = ("-----BEGIN PGP SIGNATURE-----\n" + byte[] signature = TestData.ALICE_DETACHED_SIGNED_MESSAGE.getBytes(StandardCharsets.UTF_8);
"\n" + Date signatureDate = TestData.ALICE_DETACHED_SIGNED_MESSAGE_DATE;
"iHUEABYKACcFAmPBjZUJEPIxVQxPR+OOFiEE64W7X6M6deFelE5j8jFVDE9H444A\n" +
"ADI/AQC6Bux6WpGYf7HO+QPV/D5iIrqZt9xPLgfUVoNJBmMZZwD+Ib+tn5pSyWUw\n" +
"0K1UgT5roym9Fln8U5W8R03TSbfNiwE=\n" +
"=bxPN\n" +
"-----END PGP SIGNATURE-----").getBytes(StandardCharsets.UTF_8);
Date signatureDate = UTCUtil.parseUTCDate("2023-01-13T16:57:57Z");
Date afterSignature = new Date(signatureDate.getTime() + 1000); // 1 sec after sig Date afterSignature = new Date(signatureDate.getTime() + 1000); // 1 sec after sig
assertThrows(SOPGPException.NoSignature.class, () -> getSop().detachedVerify() assertThrows(SOPGPException.NoSignature.class, () -> getSop().detachedVerify()
.cert(TestKeys.ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.notBefore(afterSignature) .notBefore(afterSignature)
.signatures(signature) .signatures(signature)
.data(message)); .data(message));
@ -162,8 +201,8 @@ public class ExternalDetachedSignVerifyRoundTripTest extends AbstractExternalSOP
assertThrows(SOPGPException.KeyIsProtected.class, () -> assertThrows(SOPGPException.KeyIsProtected.class, () ->
getSop().detachedSign() getSop().detachedSign()
.key(TestKeys.PASSWORD_PROTECTED_KEY.getBytes(StandardCharsets.UTF_8)) .key(TestData.PASSWORD_PROTECTED_KEY.getBytes(StandardCharsets.UTF_8))
.data("Hello, World!\n".getBytes(StandardCharsets.UTF_8)) .data(TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8))
.toByteArrayAndResult() .toByteArrayAndResult()
.getBytes()); .getBytes());
} }
@ -171,19 +210,19 @@ public class ExternalDetachedSignVerifyRoundTripTest extends AbstractExternalSOP
@Test @Test
public void signWithProtectedKeyAndMultiplePassphrasesTest() public void signWithProtectedKeyAndMultiplePassphrasesTest()
throws IOException { throws IOException {
byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] signature = getSop().sign() byte[] signature = getSop().sign()
.key(TestKeys.PASSWORD_PROTECTED_KEY.getBytes(StandardCharsets.UTF_8)) .key(TestData.PASSWORD_PROTECTED_KEY.getBytes(StandardCharsets.UTF_8))
.withKeyPassword("wrong") .withKeyPassword("wrong")
.withKeyPassword(TestKeys.PASSWORD) // correct .withKeyPassword(TestData.PASSWORD) // correct
.withKeyPassword("wrong2") .withKeyPassword("wrong2")
.data(message) .data(message)
.toByteArrayAndResult() .toByteArrayAndResult()
.getBytes(); .getBytes();
assertFalse(getSop().verify() assertFalse(getSop().verify()
.cert(TestKeys.PASSWORD_PROTECTED_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.PASSWORD_PROTECTED_CERT.getBytes(StandardCharsets.UTF_8))
.signatures(signature) .signatures(signature)
.data(message) .data(message)
.isEmpty()); .isEmpty());

View file

@ -49,12 +49,12 @@ public class ExternalEncryptDecryptRoundTripTest extends AbstractExternalSOPTest
public void encryptDecryptRoundTripAliceTest() throws IOException { public void encryptDecryptRoundTripAliceTest() throws IOException {
byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8); byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8);
byte[] ciphertext = getSop().encrypt() byte[] ciphertext = getSop().encrypt()
.withCert(TestKeys.ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .withCert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.plaintext(message) .plaintext(message)
.getBytes(); .getBytes();
ByteArrayAndResult<DecryptionResult> bytesAndResult = getSop().decrypt() ByteArrayAndResult<DecryptionResult> bytesAndResult = getSop().decrypt()
.withKey(TestKeys.ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .withKey(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.ciphertext(ciphertext) .ciphertext(ciphertext)
.toByteArrayAndResult(); .toByteArrayAndResult();
@ -69,12 +69,12 @@ public class ExternalEncryptDecryptRoundTripTest extends AbstractExternalSOPTest
public void encryptDecryptRoundTripBobTest() throws IOException { public void encryptDecryptRoundTripBobTest() throws IOException {
byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8); byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8);
byte[] ciphertext = getSop().encrypt() byte[] ciphertext = getSop().encrypt()
.withCert(TestKeys.BOB_CERT.getBytes(StandardCharsets.UTF_8)) .withCert(TestData.BOB_CERT.getBytes(StandardCharsets.UTF_8))
.plaintext(message) .plaintext(message)
.getBytes(); .getBytes();
byte[] plaintext = getSop().decrypt() byte[] plaintext = getSop().decrypt()
.withKey(TestKeys.BOB_KEY.getBytes(StandardCharsets.UTF_8)) .withKey(TestData.BOB_KEY.getBytes(StandardCharsets.UTF_8))
.ciphertext(ciphertext) .ciphertext(ciphertext)
.toByteArrayAndResult() .toByteArrayAndResult()
.getBytes(); .getBytes();
@ -88,12 +88,12 @@ public class ExternalEncryptDecryptRoundTripTest extends AbstractExternalSOPTest
byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8); byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8);
byte[] ciphertext = getSop().encrypt() byte[] ciphertext = getSop().encrypt()
.withCert(TestKeys.CAROL_CERT.getBytes(StandardCharsets.UTF_8)) .withCert(TestData.CAROL_CERT.getBytes(StandardCharsets.UTF_8))
.plaintext(message) .plaintext(message)
.getBytes(); .getBytes();
byte[] plaintext = getSop().decrypt() byte[] plaintext = getSop().decrypt()
.withKey(TestKeys.CAROL_KEY.getBytes(StandardCharsets.UTF_8)) .withKey(TestData.CAROL_KEY.getBytes(StandardCharsets.UTF_8))
.ciphertext(ciphertext) .ciphertext(ciphertext)
.toByteArrayAndResult() .toByteArrayAndResult()
.getBytes(); .getBytes();
@ -107,7 +107,7 @@ public class ExternalEncryptDecryptRoundTripTest extends AbstractExternalSOPTest
byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8); byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8);
byte[] ciphertext = getSop().encrypt() byte[] ciphertext = getSop().encrypt()
.withCert(TestKeys.ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .withCert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.noArmor() .noArmor()
.plaintext(message) .plaintext(message)
.getBytes(); .getBytes();
@ -117,7 +117,7 @@ public class ExternalEncryptDecryptRoundTripTest extends AbstractExternalSOPTest
.getBytes(); .getBytes();
ByteArrayAndResult<DecryptionResult> bytesAndResult = getSop().decrypt() ByteArrayAndResult<DecryptionResult> bytesAndResult = getSop().decrypt()
.withKey(TestKeys.ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .withKey(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.ciphertext(armored) .ciphertext(armored)
.toByteArrayAndResult(); .toByteArrayAndResult();
@ -129,14 +129,14 @@ public class ExternalEncryptDecryptRoundTripTest extends AbstractExternalSOPTest
public void encryptSignDecryptVerifyRoundTripAliceTest() throws IOException { public void encryptSignDecryptVerifyRoundTripAliceTest() throws IOException {
byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8); byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8);
byte[] ciphertext = getSop().encrypt() byte[] ciphertext = getSop().encrypt()
.withCert(TestKeys.ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .withCert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.signWith(TestKeys.ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .signWith(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.plaintext(message) .plaintext(message)
.getBytes(); .getBytes();
ByteArrayAndResult<DecryptionResult> bytesAndResult = getSop().decrypt() ByteArrayAndResult<DecryptionResult> bytesAndResult = getSop().decrypt()
.withKey(TestKeys.ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .withKey(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.verifyWithCert(TestKeys.ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .verifyWithCert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.ciphertext(ciphertext) .ciphertext(ciphertext)
.toByteArrayAndResult(); .toByteArrayAndResult();
@ -154,15 +154,15 @@ public class ExternalEncryptDecryptRoundTripTest extends AbstractExternalSOPTest
public void encryptSignAsTextDecryptVerifyRoundTripAliceTest() throws IOException { public void encryptSignAsTextDecryptVerifyRoundTripAliceTest() throws IOException {
byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8); byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8);
byte[] ciphertext = getSop().encrypt() byte[] ciphertext = getSop().encrypt()
.withCert(TestKeys.ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .withCert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.signWith(TestKeys.ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .signWith(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.mode(EncryptAs.Text) .mode(EncryptAs.Text)
.plaintext(message) .plaintext(message)
.getBytes(); .getBytes();
ByteArrayAndResult<DecryptionResult> bytesAndResult = getSop().decrypt() ByteArrayAndResult<DecryptionResult> bytesAndResult = getSop().decrypt()
.withKey(TestKeys.ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .withKey(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.verifyWithCert(TestKeys.ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .verifyWithCert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.ciphertext(ciphertext) .ciphertext(ciphertext)
.toByteArrayAndResult(); .toByteArrayAndResult();
@ -232,8 +232,8 @@ public class ExternalEncryptDecryptRoundTripTest extends AbstractExternalSOPTest
assertThrows(SOPGPException.NoSignature.class, () -> { assertThrows(SOPGPException.NoSignature.class, () -> {
ByteArrayAndResult<DecryptionResult> bytesAndResult = getSop().decrypt() ByteArrayAndResult<DecryptionResult> bytesAndResult = getSop().decrypt()
.withKey(TestKeys.ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .withKey(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.verifyWithCert(TestKeys.ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .verifyWithCert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.verifyNotAfter(beforeSignature) .verifyNotAfter(beforeSignature)
.ciphertext(message) .ciphertext(message)
.toByteArrayAndResult(); .toByteArrayAndResult();
@ -267,8 +267,8 @@ public class ExternalEncryptDecryptRoundTripTest extends AbstractExternalSOPTest
assertThrows(SOPGPException.NoSignature.class, () -> { assertThrows(SOPGPException.NoSignature.class, () -> {
ByteArrayAndResult<DecryptionResult> bytesAndResult = getSop().decrypt() ByteArrayAndResult<DecryptionResult> bytesAndResult = getSop().decrypt()
.withKey(TestKeys.ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .withKey(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.verifyWithCert(TestKeys.ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .verifyWithCert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.verifyNotBefore(afterSignature) .verifyNotBefore(afterSignature)
.ciphertext(message) .ciphertext(message)
.toByteArrayAndResult(); .toByteArrayAndResult();

View file

@ -36,25 +36,25 @@ public class ExternalExtractCertTest extends AbstractExternalSOPTest {
@Test @Test
public void extractAliceCertFromAliceKeyTest() throws IOException { public void extractAliceCertFromAliceKeyTest() throws IOException {
byte[] armoredCert = getSop().extractCert() byte[] armoredCert = getSop().extractCert()
.key(TestKeys.ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .key(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.getBytes(); .getBytes();
assertAsciiArmorEquals(TestKeys.ALICE_CERT.getBytes(StandardCharsets.UTF_8), armoredCert); assertAsciiArmorEquals(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8), armoredCert);
} }
@Test @Test
public void extractBobsCertFromBobsKeyTest() throws IOException { public void extractBobsCertFromBobsKeyTest() throws IOException {
byte[] armoredCert = getSop().extractCert() byte[] armoredCert = getSop().extractCert()
.key(TestKeys.BOB_KEY.getBytes(StandardCharsets.UTF_8)) .key(TestData.BOB_KEY.getBytes(StandardCharsets.UTF_8))
.getBytes(); .getBytes();
assertAsciiArmorEquals(TestKeys.BOB_CERT.getBytes(StandardCharsets.UTF_8), armoredCert); assertAsciiArmorEquals(TestData.BOB_CERT.getBytes(StandardCharsets.UTF_8), armoredCert);
} }
@Test @Test
public void extractCarolsCertFromCarolsKeyTest() throws IOException { public void extractCarolsCertFromCarolsKeyTest() throws IOException {
byte[] armoredCert = getSop().extractCert() byte[] armoredCert = getSop().extractCert()
.key(TestKeys.CAROL_KEY.getBytes(StandardCharsets.UTF_8)) .key(TestData.CAROL_KEY.getBytes(StandardCharsets.UTF_8))
.getBytes(); .getBytes();
assertAsciiArmorEquals(TestKeys.CAROL_CERT.getBytes(StandardCharsets.UTF_8), armoredCert); assertAsciiArmorEquals(TestData.CAROL_CERT.getBytes(StandardCharsets.UTF_8), armoredCert);
} }
@Test @Test

View file

@ -31,7 +31,7 @@ public class ExternalInlineSignDetachVerifyRoundTripTest extends AbstractExterna
byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8); byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8);
byte[] inlineSigned = getSop().inlineSign() byte[] inlineSigned = getSop().inlineSign()
.key(TestKeys.ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .key(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.data(message) .data(message)
.getBytes(); .getBytes();
@ -46,7 +46,7 @@ public class ExternalInlineSignDetachVerifyRoundTripTest extends AbstractExterna
.getBytes(); .getBytes();
List<Verification> verifications = getSop().detachedVerify() List<Verification> verifications = getSop().detachedVerify()
.cert(TestKeys.ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.signatures(signatures) .signatures(signatures)
.data(plaintext); .data(plaintext);
@ -60,7 +60,7 @@ public class ExternalInlineSignDetachVerifyRoundTripTest extends AbstractExterna
byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8); byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8);
byte[] inlineSigned = getSop().inlineSign() byte[] inlineSigned = getSop().inlineSign()
.key(TestKeys.ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .key(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.data(message) .data(message)
.getBytes(); .getBytes();
@ -82,7 +82,7 @@ public class ExternalInlineSignDetachVerifyRoundTripTest extends AbstractExterna
assertArrayStartsWith(armored, BEGIN_PGP_SIGNATURE); assertArrayStartsWith(armored, BEGIN_PGP_SIGNATURE);
List<Verification> verifications = getSop().detachedVerify() List<Verification> verifications = getSop().detachedVerify()
.cert(TestKeys.ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.signatures(armored) .signatures(armored)
.data(plaintext); .data(plaintext);

View file

@ -10,7 +10,6 @@ import sop.ByteArrayAndResult;
import sop.Verification; import sop.Verification;
import sop.enums.InlineSignAs; import sop.enums.InlineSignAs;
import sop.exception.SOPGPException; import sop.exception.SOPGPException;
import sop.util.UTCUtil;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@ -20,6 +19,7 @@ import java.util.List;
import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static sop.external.JUtils.assertSignedBy;
@EnabledIf("sop.external.AbstractExternalSOPTest#isExternalSopInstalled") @EnabledIf("sop.external.AbstractExternalSOPTest#isExternalSopInstalled")
public class ExternalInlineSignVerifyTest extends AbstractExternalSOPTest { public class ExternalInlineSignVerifyTest extends AbstractExternalSOPTest {
@ -33,32 +33,33 @@ public class ExternalInlineSignVerifyTest extends AbstractExternalSOPTest {
public void inlineSignVerifyAlice() throws IOException { public void inlineSignVerifyAlice() throws IOException {
ignoreIf("sqop", Is.leq, "0.26.1"); // inline-sign not supported ignoreIf("sqop", Is.leq, "0.26.1"); // inline-sign not supported
byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] inlineSigned = getSop().inlineSign() byte[] inlineSigned = getSop().inlineSign()
.key(TestKeys.ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .key(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.data(message) .data(message)
.getBytes(); .getBytes();
JUtils.assertArrayStartsWith(inlineSigned, BEGIN_PGP_MESSAGE_BYTES); JUtils.assertArrayStartsWith(inlineSigned, BEGIN_PGP_MESSAGE_BYTES);
ByteArrayAndResult<List<Verification>> bytesAndResult = getSop().inlineVerify() ByteArrayAndResult<List<Verification>> bytesAndResult = getSop().inlineVerify()
.cert(TestKeys.ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.data(inlineSigned) .data(inlineSigned)
.toByteArrayAndResult(); .toByteArrayAndResult();
assertArrayEquals(message, bytesAndResult.getBytes()); assertArrayEquals(message, bytesAndResult.getBytes());
assertFalse(bytesAndResult.getResult().isEmpty()); List<Verification> verificationList = bytesAndResult.getResult();
assertSignedBy(verificationList, TestData.ALICE_SIGNING_FINGERPRINT, TestData.ALICE_PRIMARY_FINGERPRINT);
} }
@Test @Test
public void inlineSignVerifyAliceNoArmor() throws IOException { public void inlineSignVerifyAliceNoArmor() throws IOException {
ignoreIf("sqop", Is.leq, "0.26.1"); // inline-sign not supported ignoreIf("sqop", Is.leq, "0.26.1"); // inline-sign not supported
byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] inlineSigned = getSop().inlineSign() byte[] inlineSigned = getSop().inlineSign()
.key(TestKeys.ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .key(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.noArmor() .noArmor()
.data(message) .data(message)
.getBytes(); .getBytes();
@ -66,22 +67,23 @@ public class ExternalInlineSignVerifyTest extends AbstractExternalSOPTest {
assertFalse(JUtils.arrayStartsWith(inlineSigned, BEGIN_PGP_MESSAGE_BYTES)); assertFalse(JUtils.arrayStartsWith(inlineSigned, BEGIN_PGP_MESSAGE_BYTES));
ByteArrayAndResult<List<Verification>> bytesAndResult = getSop().inlineVerify() ByteArrayAndResult<List<Verification>> bytesAndResult = getSop().inlineVerify()
.cert(TestKeys.ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.data(inlineSigned) .data(inlineSigned)
.toByteArrayAndResult(); .toByteArrayAndResult();
assertArrayEquals(message, bytesAndResult.getBytes()); assertArrayEquals(message, bytesAndResult.getBytes());
assertFalse(bytesAndResult.getResult().isEmpty()); List<Verification> verificationList = bytesAndResult.getResult();
assertSignedBy(verificationList, TestData.ALICE_SIGNING_FINGERPRINT, TestData.ALICE_PRIMARY_FINGERPRINT);
} }
@Test @Test
public void clearsignVerifyAlice() throws IOException { public void clearsignVerifyAlice() throws IOException {
ignoreIf("sqop", Is.leq, "0.26.1"); // inline-sign not supported ignoreIf("sqop", Is.leq, "0.26.1"); // inline-sign not supported
byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] clearsigned = getSop().inlineSign() byte[] clearsigned = getSop().inlineSign()
.key(TestKeys.ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .key(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.mode(InlineSignAs.clearsigned) .mode(InlineSignAs.clearsigned)
.data(message) .data(message)
.getBytes(); .getBytes();
@ -89,12 +91,29 @@ public class ExternalInlineSignVerifyTest extends AbstractExternalSOPTest {
JUtils.assertArrayStartsWith(clearsigned, BEGIN_PGP_SIGNED_MESSAGE_BYTES); JUtils.assertArrayStartsWith(clearsigned, BEGIN_PGP_SIGNED_MESSAGE_BYTES);
ByteArrayAndResult<List<Verification>> bytesAndResult = getSop().inlineVerify() ByteArrayAndResult<List<Verification>> bytesAndResult = getSop().inlineVerify()
.cert(TestKeys.ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.data(clearsigned) .data(clearsigned)
.toByteArrayAndResult(); .toByteArrayAndResult();
assertArrayEquals(message, bytesAndResult.getBytes()); assertArrayEquals(message, bytesAndResult.getBytes());
assertFalse(bytesAndResult.getResult().isEmpty()); List<Verification> verificationList = bytesAndResult.getResult();
assertSignedBy(verificationList, TestData.ALICE_SIGNING_FINGERPRINT, TestData.ALICE_PRIMARY_FINGERPRINT);
}
@Test
public void inlineVerifyCompareSignatureDate() throws IOException {
ignoreIf("sqop", Is.leq, "0.26.1"); // inline-sign not supported
ignoreIf("sqop", Is.leq, "0.27.2"); // returns 1 instead of 3 (NO_SIGNATURE)
byte[] message = TestData.ALICE_INLINE_SIGNED_MESSAGE.getBytes(StandardCharsets.UTF_8);
Date signatureDate = TestData.ALICE_INLINE_SIGNED_MESSAGE_DATE;
ByteArrayAndResult<List<Verification>> bytesAndResult = getSop().inlineVerify()
.cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.data(message)
.toByteArrayAndResult();
List<Verification> verificationList = bytesAndResult.getResult();
assertSignedBy(verificationList, TestData.ALICE_SIGNING_FINGERPRINT, TestData.ALICE_PRIMARY_FINGERPRINT, signatureDate);
} }
@Test @Test
@ -102,20 +121,13 @@ public class ExternalInlineSignVerifyTest extends AbstractExternalSOPTest {
ignoreIf("sqop", Is.leq, "0.26.1"); // inline-sign not supported ignoreIf("sqop", Is.leq, "0.26.1"); // inline-sign not supported
ignoreIf("sqop", Is.leq, "0.27.2"); // returns 1 instead of 3 (NO_SIGNATURE) ignoreIf("sqop", Is.leq, "0.27.2"); // returns 1 instead of 3 (NO_SIGNATURE)
byte[] message = ("-----BEGIN PGP MESSAGE-----\n" + byte[] message = TestData.ALICE_INLINE_SIGNED_MESSAGE.getBytes(StandardCharsets.UTF_8);
"\n" + Date signatureDate = TestData.ALICE_INLINE_SIGNED_MESSAGE_DATE;
"owGbwMvMwCX2yTCUx9/9cR/jaZEkBhDwSM3JyddRCM8vyklR5OooZWEQ42JQZ2VK\n" +
"PjjpPacATLmYIsvr1t3xi61KH8ZN8UuGCTMwpPcw/E9jS+vcvPu2gmp4jcRbcSNP\n" +
"FYmW8hmLJdUVrdt1V8w6GM/IMEvN0tP339sNGX4swq8T5p62q3jUfLjpstmcI6Ie\n" +
"sfcfswMA\n" +
"=RDAo\n" +
"-----END PGP MESSAGE-----").getBytes(StandardCharsets.UTF_8);
Date signatureDate = UTCUtil.parseUTCDate("2023-01-13T17:20:47Z");
Date afterSignature = new Date(signatureDate.getTime() + 1000); // 1 sec before sig Date afterSignature = new Date(signatureDate.getTime() + 1000); // 1 sec before sig
assertThrows(SOPGPException.NoSignature.class, () -> getSop().inlineVerify() assertThrows(SOPGPException.NoSignature.class, () -> getSop().inlineVerify()
.notBefore(afterSignature) .notBefore(afterSignature)
.cert(TestKeys.ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.data(message) .data(message)
.toByteArrayAndResult()); .toByteArrayAndResult());
} }
@ -125,51 +137,83 @@ public class ExternalInlineSignVerifyTest extends AbstractExternalSOPTest {
ignoreIf("sqop", Is.leq, "0.26.1"); // inline-sign not supported ignoreIf("sqop", Is.leq, "0.26.1"); // inline-sign not supported
ignoreIf("sqop", Is.leq, "0.27.2"); // returns 1 instead of 3 (NO_SIGNATURE) ignoreIf("sqop", Is.leq, "0.27.2"); // returns 1 instead of 3 (NO_SIGNATURE)
byte[] message = ("-----BEGIN PGP MESSAGE-----\n" + byte[] message = TestData.ALICE_INLINE_SIGNED_MESSAGE.getBytes(StandardCharsets.UTF_8);
"\n" + Date signatureDate = TestData.ALICE_INLINE_SIGNED_MESSAGE_DATE;
"owGbwMvMwCX2yTCUx9/9cR/jaZEkBhDwSM3JyddRCM8vyklR5OooZWEQ42JQZ2VK\n" +
"PjjpPacATLmYIsvr1t3xi61KH8ZN8UuGCTMwpPcw/E9jS+vcvPu2gmp4jcRbcSNP\n" +
"FYmW8hmLJdUVrdt1V8w6GM/IMEvN0tP339sNGX4swq8T5p62q3jUfLjpstmcI6Ie\n" +
"sfcfswMA\n" +
"=RDAo\n" +
"-----END PGP MESSAGE-----").getBytes(StandardCharsets.UTF_8);
Date signatureDate = UTCUtil.parseUTCDate("2023-01-13T17:20:47Z");
Date beforeSignature = new Date(signatureDate.getTime() - 1000); // 1 sec before sig Date beforeSignature = new Date(signatureDate.getTime() - 1000); // 1 sec before sig
assertThrows(SOPGPException.NoSignature.class, () -> getSop().inlineVerify() assertThrows(SOPGPException.NoSignature.class, () -> getSop().inlineVerify()
.notAfter(beforeSignature) .notAfter(beforeSignature)
.cert(TestKeys.ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.data(message) .data(message)
.toByteArrayAndResult()); .toByteArrayAndResult());
} }
@Test @Test
public void signVerifyWithPasswordProtectedKey() throws IOException { public void inlineSignVerifyBob() throws IOException {
ignoreIf("sqop", Is.leq, "0.26.1"); // inline-sign not supported ignoreIf("sqop", Is.leq, "0.26.1"); // inline-sign not supported
byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] keyPassword = "sw0rdf1sh".getBytes(StandardCharsets.UTF_8);
byte[] key = getSop().generateKey()
.userId("Alice <alice@openpgp.org>")
.withKeyPassword(keyPassword)
.generate()
.getBytes();
byte[] cert = getSop().extractCert()
.key(key)
.getBytes();
byte[] inlineSigned = getSop().inlineSign() byte[] inlineSigned = getSop().inlineSign()
.withKeyPassword(keyPassword) .key(TestData.BOB_KEY.getBytes(StandardCharsets.UTF_8))
.key(key) .data(message)
.getBytes();
JUtils.assertArrayStartsWith(inlineSigned, BEGIN_PGP_MESSAGE_BYTES);
ByteArrayAndResult<List<Verification>> bytesAndResult = getSop().inlineVerify()
.cert(TestData.BOB_CERT.getBytes(StandardCharsets.UTF_8))
.data(inlineSigned)
.toByteArrayAndResult();
assertArrayEquals(message, bytesAndResult.getBytes());
List<Verification> verificationList = bytesAndResult.getResult();
assertSignedBy(verificationList, TestData.BOB_SIGNING_FINGERPRINT, TestData.BOB_PRIMARY_FINGERPRINT);
}
@Test
public void inlineSignVerifyCarol() throws IOException {
ignoreIf("sqop", Is.leq, "0.26.1"); // inline-sign not supported
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] inlineSigned = getSop().inlineSign()
.key(TestData.CAROL_KEY.getBytes(StandardCharsets.UTF_8))
.data(message)
.getBytes();
JUtils.assertArrayStartsWith(inlineSigned, BEGIN_PGP_MESSAGE_BYTES);
ByteArrayAndResult<List<Verification>> bytesAndResult = getSop().inlineVerify()
.cert(TestData.CAROL_CERT.getBytes(StandardCharsets.UTF_8))
.data(inlineSigned)
.toByteArrayAndResult();
assertArrayEquals(message, bytesAndResult.getBytes());
List<Verification> verificationList = bytesAndResult.getResult();
assertSignedBy(verificationList, TestData.CAROL_SIGNING_FINGERPRINT, TestData.CAROL_PRIMARY_FINGERPRINT);
}
@Test
public void inlineSignVerifyProtectedKey() throws IOException {
ignoreIf("sqop", Is.leq, "0.26.1"); // inline-sign not supported
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] inlineSigned = getSop().inlineSign()
.withKeyPassword(TestData.PASSWORD)
.key(TestData.PASSWORD_PROTECTED_KEY.getBytes(StandardCharsets.UTF_8))
.mode(InlineSignAs.binary) .mode(InlineSignAs.binary)
.data(message) .data(message)
.getBytes(); .getBytes();
assertFalse(getSop().inlineVerify() ByteArrayAndResult<List<Verification>> bytesAndResult = getSop().inlineVerify()
.cert(cert) .cert(TestData.PASSWORD_PROTECTED_CERT.getBytes(StandardCharsets.UTF_8))
.data(inlineSigned) .data(inlineSigned)
.toByteArrayAndResult() .toByteArrayAndResult();
.getResult()
.isEmpty()); List<Verification> verificationList = bytesAndResult.getResult();
assertSignedBy(verificationList, TestData.PASSWORD_PROTECTED_SIGNING_FINGERPRINT, TestData.PASSWORD_PROTECTED_PRIMARY_FINGERPRINT);
} }
} }

View file

@ -4,8 +4,13 @@
package sop.external; package sop.external;
import sop.Verification;
import sop.util.UTCUtil;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.fail;
@ -110,4 +115,67 @@ public class JUtils {
.replaceAll("Charset: .+\\R", ""); .replaceAll("Charset: .+\\R", "");
return string.getBytes(StandardCharsets.UTF_8); return string.getBytes(StandardCharsets.UTF_8);
} }
public static void assertSignedBy(List<Verification> verifications, String primaryFingerprint) {
for (Verification verification : verifications) {
if (verification.getSigningCertFingerprint().equals(primaryFingerprint)) {
return;
}
}
if (verifications.isEmpty()) {
fail("Verification list is empty.");
}
fail("Verification list does not contain verification by cert " + primaryFingerprint + ":\n" +
Arrays.toString(verifications.toArray(new Verification[0])));
}
public static void assertSignedBy(List<Verification> verifications, String signingFingerprint, String primaryFingerprint) {
for (Verification verification : verifications) {
if (verification.getSigningCertFingerprint().equals(primaryFingerprint) && verification.getSigningKeyFingerprint().equals(signingFingerprint)) {
return;
}
}
if (verifications.isEmpty()) {
fail("Verification list is empty.");
}
fail("Verification list does not contain verification by key " + signingFingerprint + " on cert " + primaryFingerprint + ":\n" +
Arrays.toString(verifications.toArray(new Verification[0])));
}
public static void assertSignedBy(List<Verification> verifications, String primaryFingerprint, Date signatureDate) {
for (Verification verification : verifications) {
if (verification.getSigningCertFingerprint().equals(primaryFingerprint) &&
verification.getCreationTime().equals(signatureDate)) {
return;
}
}
if (verifications.isEmpty()) {
fail("Verification list is empty.");
}
fail("Verification list does not contain verification by cert " + primaryFingerprint + " made at " + UTCUtil.formatUTCDate(signatureDate) + ":\n" +
Arrays.toString(verifications.toArray(new Verification[0])));
}
public static void assertSignedBy(List<Verification> verifications, String signingFingerprint, String primaryFingerprint, Date signatureDate) {
for (Verification verification : verifications) {
if (verification.getSigningCertFingerprint().equals(primaryFingerprint) &&
verification.getSigningKeyFingerprint().equals(signingFingerprint) &&
verification.getCreationTime().equals(signatureDate)) {
return;
}
}
if (verifications.isEmpty()) {
fail("Verification list is empty.");
}
fail("Verification list does not contain verification by key" + signingFingerprint + " on cert " + primaryFingerprint + " made at " + UTCUtil.formatUTCDate(signatureDate) + ":\n" +
Arrays.toString(verifications.toArray(new Verification[0])));
}
} }

View file

@ -4,7 +4,14 @@
package sop.external; package sop.external;
public class TestKeys { import sop.util.UTCUtil;
import java.util.Date;
public class TestData {
public static final String PLAINTEXT = "Hello, World!\n";
// 'Alice' key from draft-bre-openpgp-samples-00 // 'Alice' key from draft-bre-openpgp-samples-00
public static final String ALICE_CERT = "-----BEGIN PGP PUBLIC KEY BLOCK-----\n" + public static final String ALICE_CERT = "-----BEGIN PGP PUBLIC KEY BLOCK-----\n" +
@ -40,6 +47,27 @@ public class TestKeys {
"Pnn+We1aTBhaGa86AQ==\n" + "Pnn+We1aTBhaGa86AQ==\n" +
"=3GfK\n" + "=3GfK\n" +
"-----END PGP PRIVATE KEY BLOCK-----\n"; "-----END PGP PRIVATE KEY BLOCK-----\n";
public static final String ALICE_PRIMARY_FINGERPRINT = "EB85BB5FA33A75E15E944E63F231550C4F47E38E";
public static final String ALICE_SIGNING_FINGERPRINT = "EB85BB5FA33A75E15E944E63F231550C4F47E38E";
public static final String ALICE_INLINE_SIGNED_MESSAGE = "-----BEGIN PGP MESSAGE-----\n" +
"\n" +
"owGbwMvMwCX2yTCUx9/9cR/jaZEkBhDwSM3JyddRCM8vyklR5OooZWEQ42JQZ2VK\n" +
"PjjpPacATLmYIsvr1t3xi61KH8ZN8UuGCTMwpPcw/E9jS+vcvPu2gmp4jcRbcSNP\n" +
"FYmW8hmLJdUVrdt1V8w6GM/IMEvN0tP339sNGX4swq8T5p62q3jUfLjpstmcI6Ie\n" +
"sfcfswMA\n" +
"=RDAo\n" +
"-----END PGP MESSAGE-----";
public static final Date ALICE_INLINE_SIGNED_MESSAGE_DATE = UTCUtil.parseUTCDate("2023-01-13T17:20:47Z");
// signature over PLAINTEXT
public static final String ALICE_DETACHED_SIGNED_MESSAGE = "-----BEGIN PGP SIGNATURE-----\n" +
"\n" +
"iHUEABYKACcFAmPBjZUJEPIxVQxPR+OOFiEE64W7X6M6deFelE5j8jFVDE9H444A\n" +
"ADI/AQC6Bux6WpGYf7HO+QPV/D5iIrqZt9xPLgfUVoNJBmMZZwD+Ib+tn5pSyWUw\n" +
"0K1UgT5roym9Fln8U5W8R03TSbfNiwE=\n" +
"=bxPN\n" +
"-----END PGP SIGNATURE-----";
public static final Date ALICE_DETACHED_SIGNED_MESSAGE_DATE = UTCUtil.parseUTCDate("2023-01-13T16:57:57Z");
// 'Bob' key from draft-bre-openpgp-samples-00 // 'Bob' key from draft-bre-openpgp-samples-00
public static final String BOB_CERT = "-----BEGIN PGP PUBLIC KEY BLOCK-----\n" + public static final String BOB_CERT = "-----BEGIN PGP PUBLIC KEY BLOCK-----\n" +
@ -169,6 +197,8 @@ public class TestKeys {
"xqAY9Bwizt4FWgXuLm1a4+So4V9j1TRCXd12Uc2l2RNmgDE=\n" + "xqAY9Bwizt4FWgXuLm1a4+So4V9j1TRCXd12Uc2l2RNmgDE=\n" +
"=FAzO\n" + "=FAzO\n" +
"-----END PGP PRIVATE KEY BLOCK-----\n"; "-----END PGP PRIVATE KEY BLOCK-----\n";
public static final String BOB_PRIMARY_FINGERPRINT = "D1A66E1A23B182C9980F788CFBFCC82A015E7330";
public static final String BOB_SIGNING_FINGERPRINT = "D1A66E1A23B182C9980F788CFBFCC82A015E7330";
// 'Carol' key from draft-bre-openpgp-samples-00 // 'Carol' key from draft-bre-openpgp-samples-00
public static final String CAROL_CERT = "-----BEGIN PGP PUBLIC KEY BLOCK-----\n" + public static final String CAROL_CERT = "-----BEGIN PGP PUBLIC KEY BLOCK-----\n" +
@ -304,6 +334,8 @@ public class TestKeys {
"1FkOSekLi8WNMdUx3XMyvP8nJ65P2Q==\n" + "1FkOSekLi8WNMdUx3XMyvP8nJ65P2Q==\n" +
"=Xj8h\n" + "=Xj8h\n" +
"-----END PGP PRIVATE KEY BLOCK-----\n"; "-----END PGP PRIVATE KEY BLOCK-----\n";
public static final String CAROL_PRIMARY_FINGERPRINT = "71FFDA004409E5DDB0C3E8F19BA789DC76D6849A";
public static final String CAROL_SIGNING_FINGERPRINT = "71FFDA004409E5DDB0C3E8F19BA789DC76D6849A";
public static final String PASSWORD_PROTECTED_KEY = "-----BEGIN PGP PRIVATE KEY BLOCK-----\n" + public static final String PASSWORD_PROTECTED_KEY = "-----BEGIN PGP PRIVATE KEY BLOCK-----\n" +
"Comment: FC63 688A 5E69 8C29 40AF 7029 7C62 2B00 D459 2657\n" + "Comment: FC63 688A 5E69 8C29 40AF 7029 7C62 2B00 D459 2657\n" +
@ -375,4 +407,6 @@ public class TestKeys {
"=xlgc\n" + "=xlgc\n" +
"-----END PGP PUBLIC KEY BLOCK-----\n"; "-----END PGP PUBLIC KEY BLOCK-----\n";
public static final String PASSWORD = "sw0rdf1sh"; public static final String PASSWORD = "sw0rdf1sh";
public static final String PASSWORD_PROTECTED_PRIMARY_FINGERPRINT = "FC63688A5E698C2940AF70297C622B00D4592657";
public static final String PASSWORD_PROTECTED_SIGNING_FINGERPRINT = "D8F1CBC2613350D1A766D35F68862FB90F07165B";
} }