89 lines
3.6 KiB
Java
89 lines
3.6 KiB
Java
/*
|
|
* Copyright 2020 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.decryption_verification;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.IOException;
|
|
import java.nio.charset.Charset;
|
|
import java.util.Collections;
|
|
|
|
import org.bouncycastle.openpgp.PGPException;
|
|
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
|
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
|
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
|
|
import org.bouncycastle.util.io.Streams;
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.pgpainless.PGPainless;
|
|
import org.pgpainless.algorithm.CompressionAlgorithm;
|
|
import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
|
|
import org.pgpainless.key.TestKeys;
|
|
import org.pgpainless.key.protection.UnprotectedKeysProtector;
|
|
|
|
public class DecryptAndVerifyMessageTest {
|
|
|
|
// Don't use StandardCharsets.UTF8 because of Android API level.
|
|
private static final Charset UTF8 = Charset.forName("UTF-8");
|
|
|
|
private PGPSecretKeyRing juliet;
|
|
private PGPSecretKeyRing romeo;
|
|
|
|
@BeforeEach
|
|
public void loadKeys() throws IOException, PGPException {
|
|
juliet = TestKeys.getJulietSecretKeyRing();
|
|
romeo = TestKeys.getRomeoSecretKeyRing();
|
|
}
|
|
|
|
@Test
|
|
public void decryptMessageAndVerifySignatureTest() throws Exception {
|
|
String encryptedMessage = TestKeys.MSG_SIGN_CRYPT_JULIET_JULIET;
|
|
|
|
DecryptionStream decryptor = PGPainless.createDecryptor()
|
|
.onInputStream(new ByteArrayInputStream(encryptedMessage.getBytes()))
|
|
.decryptWith(new UnprotectedKeysProtector(), new PGPSecretKeyRingCollection(Collections.singleton(juliet)))
|
|
.verifyWith(Collections.singleton(new PGPPublicKeyRing(Collections.singletonList(juliet.getPublicKey()))))
|
|
.ignoreMissingPublicKeys()
|
|
.build();
|
|
|
|
ByteArrayOutputStream toPlain = new ByteArrayOutputStream();
|
|
Streams.pipeAll(decryptor, toPlain);
|
|
decryptor.close();
|
|
toPlain.close();
|
|
OpenPgpMetadata metadata = decryptor.getResult();
|
|
|
|
byte[] expected = TestKeys.TEST_MESSAGE_01_PLAIN.getBytes(UTF8);
|
|
byte[] actual = toPlain.toByteArray();
|
|
|
|
assertArrayEquals(expected, actual);
|
|
|
|
assertTrue(metadata.isIntegrityProtected());
|
|
assertTrue(metadata.isEncrypted());
|
|
assertTrue(metadata.isSigned());
|
|
assertTrue(metadata.isVerified());
|
|
assertEquals(CompressionAlgorithm.ZLIB, metadata.getCompressionAlgorithm());
|
|
assertEquals(SymmetricKeyAlgorithm.AES_256, metadata.getSymmetricKeyAlgorithm());
|
|
assertEquals(1, metadata.getSignatures().size());
|
|
assertEquals(1, metadata.getVerifiedSignatureKeyFingerprints().size());
|
|
assertTrue(metadata.containsVerifiedSignatureFrom(TestKeys.JULIET_FINGERPRINT));
|
|
assertEquals(TestKeys.JULIET_FINGERPRINT, metadata.getDecryptionFingerprint());
|
|
}
|
|
}
|