2023-01-12 16:55:47 +01:00
|
|
|
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package sop.external;
|
|
|
|
|
2023-01-22 16:53:50 +01:00
|
|
|
import org.junit.jupiter.api.condition.EnabledIf;
|
2023-01-22 16:47:44 +01:00
|
|
|
import org.junit.jupiter.params.ParameterizedTest;
|
|
|
|
import org.junit.jupiter.params.provider.MethodSource;
|
2023-01-12 16:55:47 +01:00
|
|
|
import sop.ByteArrayAndResult;
|
2023-01-22 16:47:44 +01:00
|
|
|
import sop.SOP;
|
2023-01-12 16:55:47 +01:00
|
|
|
import sop.Verification;
|
|
|
|
import sop.enums.InlineSignAs;
|
2023-01-13 18:50:19 +01:00
|
|
|
import sop.exception.SOPGPException;
|
2023-01-27 00:35:38 +01:00
|
|
|
import sop.testing.JUtils;
|
|
|
|
import sop.testing.TestData;
|
2023-01-12 16:55:47 +01:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
2023-01-13 18:50:19 +01:00
|
|
|
import java.util.Date;
|
2023-01-12 16:55:47 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
2023-01-13 18:50:19 +01:00
|
|
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
2023-01-27 00:35:38 +01:00
|
|
|
import static sop.testing.JUtils.assertSignedBy;
|
|
|
|
import static sop.testing.TestData.ALICE_CERT;
|
|
|
|
import static sop.testing.TestData.ALICE_KEY;
|
|
|
|
import static sop.testing.TestData.ALICE_PRIMARY_FINGERPRINT;
|
|
|
|
import static sop.testing.TestData.ALICE_SIGNING_FINGERPRINT;
|
|
|
|
import static sop.testing.TestData.BEGIN_PGP_MESSAGE;
|
|
|
|
import static sop.testing.TestData.BEGIN_PGP_SIGNED_MESSAGE;
|
|
|
|
import static sop.testing.TestData.PLAINTEXT;
|
2023-01-12 16:55:47 +01:00
|
|
|
|
2023-01-22 16:53:50 +01:00
|
|
|
@EnabledIf("sop.external.AbstractExternalSOPTest#hasBackends")
|
2023-01-12 16:55:47 +01:00
|
|
|
public class ExternalInlineSignVerifyTest extends AbstractExternalSOPTest {
|
|
|
|
|
2023-01-22 16:47:44 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
|
|
|
|
public void inlineSignVerifyAlice(SOP sop) throws IOException {
|
2023-01-27 00:35:38 +01:00
|
|
|
byte[] message = PLAINTEXT.getBytes(StandardCharsets.UTF_8);
|
2023-01-12 16:55:47 +01:00
|
|
|
|
2023-01-22 16:47:44 +01:00
|
|
|
byte[] inlineSigned = sop.inlineSign()
|
2023-01-27 00:35:38 +01:00
|
|
|
.key(ALICE_KEY.getBytes(StandardCharsets.UTF_8))
|
2023-01-12 16:55:47 +01:00
|
|
|
.data(message)
|
|
|
|
.getBytes();
|
|
|
|
|
2023-01-27 00:35:38 +01:00
|
|
|
JUtils.assertArrayStartsWith(inlineSigned, BEGIN_PGP_MESSAGE);
|
2023-01-12 16:55:47 +01:00
|
|
|
|
2023-01-22 16:47:44 +01:00
|
|
|
ByteArrayAndResult<List<Verification>> bytesAndResult = sop.inlineVerify()
|
2023-01-27 00:35:38 +01:00
|
|
|
.cert(ALICE_CERT.getBytes(StandardCharsets.UTF_8))
|
2023-01-12 16:55:47 +01:00
|
|
|
.data(inlineSigned)
|
|
|
|
.toByteArrayAndResult();
|
|
|
|
|
|
|
|
assertArrayEquals(message, bytesAndResult.getBytes());
|
2023-01-21 20:31:49 +01:00
|
|
|
List<Verification> verificationList = bytesAndResult.getResult();
|
2023-01-27 00:35:38 +01:00
|
|
|
assertSignedBy(verificationList, ALICE_SIGNING_FINGERPRINT, ALICE_PRIMARY_FINGERPRINT);
|
2023-01-12 16:55:47 +01:00
|
|
|
}
|
|
|
|
|
2023-01-22 16:47:44 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
|
|
|
|
public void inlineSignVerifyAliceNoArmor(SOP sop) throws IOException {
|
2023-01-27 00:35:38 +01:00
|
|
|
byte[] message = PLAINTEXT.getBytes(StandardCharsets.UTF_8);
|
2023-01-12 16:55:47 +01:00
|
|
|
|
2023-01-22 16:47:44 +01:00
|
|
|
byte[] inlineSigned = sop.inlineSign()
|
2023-01-27 00:35:38 +01:00
|
|
|
.key(ALICE_KEY.getBytes(StandardCharsets.UTF_8))
|
2023-01-12 16:55:47 +01:00
|
|
|
.noArmor()
|
|
|
|
.data(message)
|
|
|
|
.getBytes();
|
|
|
|
|
2023-01-27 00:35:38 +01:00
|
|
|
assertFalse(JUtils.arrayStartsWith(inlineSigned, BEGIN_PGP_MESSAGE));
|
2023-01-12 16:55:47 +01:00
|
|
|
|
2023-01-22 16:47:44 +01:00
|
|
|
ByteArrayAndResult<List<Verification>> bytesAndResult = sop.inlineVerify()
|
2023-01-27 00:35:38 +01:00
|
|
|
.cert(ALICE_CERT.getBytes(StandardCharsets.UTF_8))
|
2023-01-12 16:55:47 +01:00
|
|
|
.data(inlineSigned)
|
|
|
|
.toByteArrayAndResult();
|
|
|
|
|
|
|
|
assertArrayEquals(message, bytesAndResult.getBytes());
|
2023-01-21 20:31:49 +01:00
|
|
|
List<Verification> verificationList = bytesAndResult.getResult();
|
2023-01-27 00:35:38 +01:00
|
|
|
assertSignedBy(verificationList, ALICE_SIGNING_FINGERPRINT, ALICE_PRIMARY_FINGERPRINT);
|
2023-01-12 16:55:47 +01:00
|
|
|
}
|
|
|
|
|
2023-01-22 16:47:44 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
|
|
|
|
public void clearsignVerifyAlice(SOP sop) throws IOException {
|
2023-01-27 00:35:38 +01:00
|
|
|
byte[] message = PLAINTEXT.getBytes(StandardCharsets.UTF_8);
|
2023-01-12 16:55:47 +01:00
|
|
|
|
2023-01-22 16:47:44 +01:00
|
|
|
byte[] clearsigned = sop.inlineSign()
|
2023-01-27 00:35:38 +01:00
|
|
|
.key(ALICE_KEY.getBytes(StandardCharsets.UTF_8))
|
2023-01-12 16:55:47 +01:00
|
|
|
.mode(InlineSignAs.clearsigned)
|
|
|
|
.data(message)
|
|
|
|
.getBytes();
|
|
|
|
|
2023-01-27 00:35:38 +01:00
|
|
|
JUtils.assertArrayStartsWith(clearsigned, BEGIN_PGP_SIGNED_MESSAGE);
|
2023-01-12 16:55:47 +01:00
|
|
|
|
2023-01-22 16:47:44 +01:00
|
|
|
ByteArrayAndResult<List<Verification>> bytesAndResult = sop.inlineVerify()
|
2023-01-27 00:35:38 +01:00
|
|
|
.cert(ALICE_CERT.getBytes(StandardCharsets.UTF_8))
|
2023-01-12 16:55:47 +01:00
|
|
|
.data(clearsigned)
|
|
|
|
.toByteArrayAndResult();
|
|
|
|
|
|
|
|
assertArrayEquals(message, bytesAndResult.getBytes());
|
2023-01-21 20:31:49 +01:00
|
|
|
List<Verification> verificationList = bytesAndResult.getResult();
|
2023-01-27 00:35:38 +01:00
|
|
|
assertSignedBy(verificationList, ALICE_SIGNING_FINGERPRINT, ALICE_PRIMARY_FINGERPRINT);
|
2023-01-21 20:31:49 +01:00
|
|
|
}
|
|
|
|
|
2023-01-22 16:47:44 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
|
|
|
|
public void inlineVerifyCompareSignatureDate(SOP sop) throws IOException {
|
2023-01-21 20:31:49 +01:00
|
|
|
byte[] message = TestData.ALICE_INLINE_SIGNED_MESSAGE.getBytes(StandardCharsets.UTF_8);
|
|
|
|
Date signatureDate = TestData.ALICE_INLINE_SIGNED_MESSAGE_DATE;
|
|
|
|
|
2023-01-22 16:47:44 +01:00
|
|
|
ByteArrayAndResult<List<Verification>> bytesAndResult = sop.inlineVerify()
|
2023-01-27 00:35:38 +01:00
|
|
|
.cert(ALICE_CERT.getBytes(StandardCharsets.UTF_8))
|
2023-01-21 20:31:49 +01:00
|
|
|
.data(message)
|
|
|
|
.toByteArrayAndResult();
|
|
|
|
List<Verification> verificationList = bytesAndResult.getResult();
|
2023-01-27 00:35:38 +01:00
|
|
|
assertSignedBy(verificationList, ALICE_SIGNING_FINGERPRINT, ALICE_PRIMARY_FINGERPRINT, signatureDate);
|
2023-01-12 16:55:47 +01:00
|
|
|
}
|
2023-01-13 18:50:19 +01:00
|
|
|
|
2023-01-22 16:47:44 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
|
|
|
|
public void assertNotBeforeThrowsNoSignature(SOP sop) {
|
2023-01-21 20:31:49 +01:00
|
|
|
byte[] message = TestData.ALICE_INLINE_SIGNED_MESSAGE.getBytes(StandardCharsets.UTF_8);
|
|
|
|
Date signatureDate = TestData.ALICE_INLINE_SIGNED_MESSAGE_DATE;
|
2023-01-13 18:50:19 +01:00
|
|
|
Date afterSignature = new Date(signatureDate.getTime() + 1000); // 1 sec before sig
|
|
|
|
|
2023-01-22 16:47:44 +01:00
|
|
|
assertThrows(SOPGPException.NoSignature.class, () -> sop.inlineVerify()
|
2023-01-13 18:50:19 +01:00
|
|
|
.notBefore(afterSignature)
|
2023-01-27 00:35:38 +01:00
|
|
|
.cert(ALICE_CERT.getBytes(StandardCharsets.UTF_8))
|
2023-01-13 18:50:19 +01:00
|
|
|
.data(message)
|
|
|
|
.toByteArrayAndResult());
|
|
|
|
}
|
|
|
|
|
2023-01-22 16:47:44 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
|
|
|
|
public void assertNotAfterThrowsNoSignature(SOP sop) {
|
2023-01-21 20:31:49 +01:00
|
|
|
byte[] message = TestData.ALICE_INLINE_SIGNED_MESSAGE.getBytes(StandardCharsets.UTF_8);
|
|
|
|
Date signatureDate = TestData.ALICE_INLINE_SIGNED_MESSAGE_DATE;
|
2023-01-13 18:50:19 +01:00
|
|
|
Date beforeSignature = new Date(signatureDate.getTime() - 1000); // 1 sec before sig
|
|
|
|
|
2023-01-22 16:47:44 +01:00
|
|
|
assertThrows(SOPGPException.NoSignature.class, () -> sop.inlineVerify()
|
2023-01-13 18:50:19 +01:00
|
|
|
.notAfter(beforeSignature)
|
2023-01-27 00:35:38 +01:00
|
|
|
.cert(ALICE_CERT.getBytes(StandardCharsets.UTF_8))
|
2023-01-13 18:50:19 +01:00
|
|
|
.data(message)
|
|
|
|
.toByteArrayAndResult());
|
|
|
|
}
|
|
|
|
|
2023-01-22 16:47:44 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
|
|
|
|
public void inlineSignVerifyBob(SOP sop) throws IOException {
|
2023-01-27 00:35:38 +01:00
|
|
|
byte[] message = PLAINTEXT.getBytes(StandardCharsets.UTF_8);
|
2023-01-21 20:31:49 +01:00
|
|
|
|
2023-01-22 16:47:44 +01:00
|
|
|
byte[] inlineSigned = sop.inlineSign()
|
2023-01-21 20:31:49 +01:00
|
|
|
.key(TestData.BOB_KEY.getBytes(StandardCharsets.UTF_8))
|
|
|
|
.data(message)
|
2023-01-13 18:50:19 +01:00
|
|
|
.getBytes();
|
2023-01-21 20:31:49 +01:00
|
|
|
|
2023-01-27 00:35:38 +01:00
|
|
|
JUtils.assertArrayStartsWith(inlineSigned, BEGIN_PGP_MESSAGE);
|
2023-01-21 20:31:49 +01:00
|
|
|
|
2023-01-22 16:47:44 +01:00
|
|
|
ByteArrayAndResult<List<Verification>> bytesAndResult = sop.inlineVerify()
|
2023-01-21 20:31:49 +01:00
|
|
|
.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);
|
|
|
|
}
|
|
|
|
|
2023-01-22 16:47:44 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
|
|
|
|
public void inlineSignVerifyCarol(SOP sop) throws IOException {
|
2023-01-27 00:35:38 +01:00
|
|
|
byte[] message = PLAINTEXT.getBytes(StandardCharsets.UTF_8);
|
2023-01-21 20:31:49 +01:00
|
|
|
|
2023-01-22 16:47:44 +01:00
|
|
|
byte[] inlineSigned = sop.inlineSign()
|
2023-01-21 20:31:49 +01:00
|
|
|
.key(TestData.CAROL_KEY.getBytes(StandardCharsets.UTF_8))
|
|
|
|
.data(message)
|
2023-01-13 18:50:19 +01:00
|
|
|
.getBytes();
|
|
|
|
|
2023-01-27 00:35:38 +01:00
|
|
|
JUtils.assertArrayStartsWith(inlineSigned, BEGIN_PGP_MESSAGE);
|
2023-01-21 20:31:49 +01:00
|
|
|
|
2023-01-22 16:47:44 +01:00
|
|
|
ByteArrayAndResult<List<Verification>> bytesAndResult = sop.inlineVerify()
|
2023-01-21 20:31:49 +01:00
|
|
|
.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);
|
|
|
|
}
|
|
|
|
|
2023-01-22 16:47:44 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
|
|
|
|
public void inlineSignVerifyProtectedKey(SOP sop) throws IOException {
|
2023-01-27 00:35:38 +01:00
|
|
|
byte[] message = PLAINTEXT.getBytes(StandardCharsets.UTF_8);
|
2023-01-21 20:31:49 +01:00
|
|
|
|
2023-01-22 16:47:44 +01:00
|
|
|
byte[] inlineSigned = sop.inlineSign()
|
2023-01-21 20:31:49 +01:00
|
|
|
.withKeyPassword(TestData.PASSWORD)
|
|
|
|
.key(TestData.PASSWORD_PROTECTED_KEY.getBytes(StandardCharsets.UTF_8))
|
2023-01-13 18:50:19 +01:00
|
|
|
.mode(InlineSignAs.binary)
|
|
|
|
.data(message)
|
|
|
|
.getBytes();
|
|
|
|
|
2023-01-22 16:47:44 +01:00
|
|
|
ByteArrayAndResult<List<Verification>> bytesAndResult = sop.inlineVerify()
|
2023-01-21 20:31:49 +01:00
|
|
|
.cert(TestData.PASSWORD_PROTECTED_CERT.getBytes(StandardCharsets.UTF_8))
|
2023-01-13 18:50:19 +01:00
|
|
|
.data(inlineSigned)
|
2023-01-21 20:31:49 +01:00
|
|
|
.toByteArrayAndResult();
|
|
|
|
|
|
|
|
List<Verification> verificationList = bytesAndResult.getResult();
|
|
|
|
assertSignedBy(verificationList, TestData.PASSWORD_PROTECTED_SIGNING_FINGERPRINT, TestData.PASSWORD_PROTECTED_PRIMARY_FINGERPRINT);
|
2023-01-13 18:50:19 +01:00
|
|
|
}
|
2023-01-21 20:31:49 +01:00
|
|
|
|
2023-01-12 16:55:47 +01:00
|
|
|
}
|