2023-01-09 19:48:25 +01:00
|
|
|
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package sop.external;
|
|
|
|
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import org.junit.jupiter.api.condition.EnabledIf;
|
2023-01-12 14:40:00 +01:00
|
|
|
import sop.ByteArrayAndResult;
|
|
|
|
import sop.DecryptionResult;
|
|
|
|
import sop.Verification;
|
2023-01-09 19:48:25 +01:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
2023-01-12 14:40:00 +01:00
|
|
|
import java.util.List;
|
2023-01-09 19:48:25 +01:00
|
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
2023-01-12 14:40:00 +01:00
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
2023-01-09 19:48:25 +01:00
|
|
|
|
|
|
|
@EnabledIf("sop.external.AbstractExternalSOPTest#isExternalSopInstalled")
|
|
|
|
public class EncryptDecryptRoundTripTest extends AbstractExternalSOPTest {
|
|
|
|
|
|
|
|
@Test
|
2023-01-12 14:40:00 +01:00
|
|
|
public void encryptDecryptRoundTripPasswordTest() throws IOException {
|
2023-01-09 19:48:25 +01:00
|
|
|
byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8);
|
|
|
|
byte[] ciphertext = getSop().encrypt()
|
2023-01-12 14:40:00 +01:00
|
|
|
.withPassword("sw0rdf1sh")
|
2023-01-09 19:48:25 +01:00
|
|
|
.plaintext(message)
|
|
|
|
.getBytes();
|
|
|
|
|
|
|
|
byte[] plaintext = getSop().decrypt()
|
2023-01-12 14:40:00 +01:00
|
|
|
.withPassword("sw0rdf1sh")
|
2023-01-09 19:48:25 +01:00
|
|
|
.ciphertext(ciphertext)
|
|
|
|
.toByteArrayAndResult()
|
|
|
|
.getBytes();
|
|
|
|
|
|
|
|
assertArrayEquals(message, plaintext);
|
|
|
|
}
|
|
|
|
|
2023-01-12 14:40:00 +01:00
|
|
|
@Test
|
|
|
|
public void encryptDecryptRoundTripAliceTest() throws IOException {
|
|
|
|
byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8);
|
|
|
|
byte[] ciphertext = getSop().encrypt()
|
|
|
|
.withCert(TestKeys.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
|
|
|
|
.plaintext(message)
|
|
|
|
.getBytes();
|
|
|
|
|
|
|
|
ByteArrayAndResult<DecryptionResult> bytesAndResult = getSop().decrypt()
|
|
|
|
.withKey(TestKeys.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
|
|
|
|
.ciphertext(ciphertext)
|
|
|
|
.toByteArrayAndResult();
|
|
|
|
|
|
|
|
byte[] plaintext = bytesAndResult.getBytes();
|
|
|
|
assertArrayEquals(message, plaintext);
|
|
|
|
|
|
|
|
DecryptionResult result = bytesAndResult.getResult();
|
|
|
|
assertNotNull(result.getSessionKey().get());
|
|
|
|
}
|
|
|
|
|
2023-01-09 19:48:25 +01:00
|
|
|
@Test
|
|
|
|
public void encryptDecryptRoundTripBobTest() throws IOException {
|
|
|
|
byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8);
|
|
|
|
byte[] ciphertext = getSop().encrypt()
|
|
|
|
.withCert(TestKeys.BOB_CERT.getBytes(StandardCharsets.UTF_8))
|
|
|
|
.plaintext(message)
|
|
|
|
.getBytes();
|
|
|
|
|
|
|
|
byte[] plaintext = getSop().decrypt()
|
|
|
|
.withKey(TestKeys.BOB_KEY.getBytes(StandardCharsets.UTF_8))
|
|
|
|
.ciphertext(ciphertext)
|
|
|
|
.toByteArrayAndResult()
|
|
|
|
.getBytes();
|
|
|
|
|
|
|
|
assertArrayEquals(message, plaintext);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void encryptDecryptRoundTripCarolTest() throws IOException {
|
|
|
|
ignoreIf("sqop", Is.geq, "0.0.0"); // sqop reports cert not encryption capable
|
|
|
|
|
|
|
|
byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8);
|
|
|
|
byte[] ciphertext = getSop().encrypt()
|
|
|
|
.withCert(TestKeys.CAROL_CERT.getBytes(StandardCharsets.UTF_8))
|
|
|
|
.plaintext(message)
|
|
|
|
.getBytes();
|
|
|
|
|
|
|
|
byte[] plaintext = getSop().decrypt()
|
|
|
|
.withKey(TestKeys.CAROL_KEY.getBytes(StandardCharsets.UTF_8))
|
|
|
|
.ciphertext(ciphertext)
|
|
|
|
.toByteArrayAndResult()
|
|
|
|
.getBytes();
|
|
|
|
|
|
|
|
assertArrayEquals(message, plaintext);
|
|
|
|
}
|
2023-01-12 14:40:00 +01:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void encryptSignDecryptVerifyRoundTripAliceTest() throws IOException {
|
|
|
|
byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8);
|
|
|
|
byte[] ciphertext = getSop().encrypt()
|
|
|
|
.withCert(TestKeys.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
|
|
|
|
.signWith(TestKeys.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
|
|
|
|
.plaintext(message)
|
|
|
|
.getBytes();
|
|
|
|
|
|
|
|
ByteArrayAndResult<DecryptionResult> bytesAndResult = getSop().decrypt()
|
|
|
|
.withKey(TestKeys.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
|
|
|
|
.verifyWithCert(TestKeys.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
|
|
|
|
.ciphertext(ciphertext)
|
|
|
|
.toByteArrayAndResult();
|
|
|
|
|
|
|
|
byte[] plaintext = bytesAndResult.getBytes();
|
|
|
|
assertArrayEquals(message, plaintext);
|
|
|
|
|
|
|
|
DecryptionResult result = bytesAndResult.getResult();
|
|
|
|
assertNotNull(result.getSessionKey().get());
|
|
|
|
List<Verification> verificationList = result.getVerifications();
|
|
|
|
assertEquals(1, verificationList.size());
|
|
|
|
assertTrue(verificationList.get(0).toString().contains("EB85BB5FA33A75E15E944E63F231550C4F47E38E EB85BB5FA33A75E15E944E63F231550C4F47E38E"));
|
|
|
|
}
|
2023-01-09 19:48:25 +01:00
|
|
|
}
|