mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-11-26 14:22:05 +01:00
Improve tests and add signatures to result
This commit is contained in:
parent
47300a0694
commit
530a22ba0e
8 changed files with 146 additions and 86 deletions
|
@ -21,6 +21,7 @@ import java.io.InputStream;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
import org.bouncycastle.openpgp.PGPException;
|
import org.bouncycastle.openpgp.PGPException;
|
||||||
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
||||||
|
@ -74,15 +75,17 @@ public class DecryptionBuilder implements DecryptionBuilderInterface {
|
||||||
@Override
|
@Override
|
||||||
public HandleMissingPublicKeys verifyWith(@Nonnull Set<OpenPgpV4Fingerprint> trustedKeyIds,
|
public HandleMissingPublicKeys verifyWith(@Nonnull Set<OpenPgpV4Fingerprint> trustedKeyIds,
|
||||||
@Nonnull PGPPublicKeyRingCollection publicKeyRingCollection) {
|
@Nonnull PGPPublicKeyRingCollection publicKeyRingCollection) {
|
||||||
|
Set<PGPPublicKeyRing> publicKeyRings = keyRingCollectionToSet(publicKeyRingCollection);
|
||||||
|
publicKeyRings.removeIf(p -> !trustedKeyIds.contains(new OpenPgpV4Fingerprint(p)));
|
||||||
|
return verifyWith(publicKeyRings);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Set<PGPPublicKeyRing> keyRingCollectionToSet(PGPPublicKeyRingCollection publicKeyRingCollection) {
|
||||||
Set<PGPPublicKeyRing> publicKeyRings = new HashSet<>();
|
Set<PGPPublicKeyRing> publicKeyRings = new HashSet<>();
|
||||||
for (Iterator<PGPPublicKeyRing> i = publicKeyRingCollection.getKeyRings(); i.hasNext(); ) {
|
for (Iterator<PGPPublicKeyRing> i = publicKeyRingCollection.getKeyRings(); i.hasNext(); ) {
|
||||||
PGPPublicKeyRing p = i.next();
|
publicKeyRings.add(i.next());
|
||||||
OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(p);
|
|
||||||
if (trustedKeyIds.contains(fingerprint)) {
|
|
||||||
publicKeyRings.add(p);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return verifyWith(publicKeyRings);
|
return publicKeyRings;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -198,8 +198,12 @@ public final class DecryptionStreamFactory {
|
||||||
throw new PGPException("Verification failed - No OnePassSignatures found");
|
throw new PGPException("Verification failed - No OnePassSignatures found");
|
||||||
}
|
}
|
||||||
|
|
||||||
while (iterator.hasNext()) {
|
processOnePassSignatures(iterator);
|
||||||
PGPOnePassSignature signature = iterator.next();
|
}
|
||||||
|
|
||||||
|
private void processOnePassSignatures(Iterator<PGPOnePassSignature> signatures) throws PGPException {
|
||||||
|
while (signatures.hasNext()) {
|
||||||
|
PGPOnePassSignature signature = signatures.next();
|
||||||
final long keyId = signature.getKeyID();
|
final long keyId = signature.getKeyID();
|
||||||
resultBuilder.addUnverifiedSignatureKeyId(keyId);
|
resultBuilder.addUnverifiedSignatureKeyId(keyId);
|
||||||
|
|
||||||
|
|
|
@ -17,10 +17,13 @@ package org.pgpainless.decryption_verification;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
import org.bouncycastle.openpgp.PGPPublicKey;
|
import org.bouncycastle.openpgp.PGPPublicKey;
|
||||||
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
||||||
|
import org.bouncycastle.openpgp.PGPSignature;
|
||||||
import org.pgpainless.algorithm.CompressionAlgorithm;
|
import org.pgpainless.algorithm.CompressionAlgorithm;
|
||||||
import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
|
import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
|
||||||
import org.pgpainless.key.OpenPgpV4Fingerprint;
|
import org.pgpainless.key.OpenPgpV4Fingerprint;
|
||||||
|
@ -29,7 +32,9 @@ public class OpenPgpMetadata {
|
||||||
|
|
||||||
private final Set<Long> recipientKeyIds;
|
private final Set<Long> recipientKeyIds;
|
||||||
private final OpenPgpV4Fingerprint decryptionFingerprint;
|
private final OpenPgpV4Fingerprint decryptionFingerprint;
|
||||||
private final Set<Long> unverifiedSignatureKeyIds;
|
private final Set<PGPSignature> signatures;
|
||||||
|
private final Set<Long> signatureKeyIds;
|
||||||
|
private final Map<OpenPgpV4Fingerprint, PGPSignature> verifiedSignatures;
|
||||||
private final Set<OpenPgpV4Fingerprint> verifiedSignaturesFingerprints;
|
private final Set<OpenPgpV4Fingerprint> verifiedSignaturesFingerprints;
|
||||||
|
|
||||||
private final SymmetricKeyAlgorithm symmetricKeyAlgorithm;
|
private final SymmetricKeyAlgorithm symmetricKeyAlgorithm;
|
||||||
|
@ -41,7 +46,9 @@ public class OpenPgpMetadata {
|
||||||
SymmetricKeyAlgorithm symmetricKeyAlgorithm,
|
SymmetricKeyAlgorithm symmetricKeyAlgorithm,
|
||||||
CompressionAlgorithm algorithm,
|
CompressionAlgorithm algorithm,
|
||||||
boolean integrityProtected,
|
boolean integrityProtected,
|
||||||
Set<Long> unverifiedSignatureKeyIds,
|
Set<PGPSignature> signatures,
|
||||||
|
Set<Long> signatureKeyIds,
|
||||||
|
Map<OpenPgpV4Fingerprint, PGPSignature> verifiedSignatures,
|
||||||
Set<OpenPgpV4Fingerprint> verifiedSignaturesFingerprints) {
|
Set<OpenPgpV4Fingerprint> verifiedSignaturesFingerprints) {
|
||||||
|
|
||||||
this.recipientKeyIds = Collections.unmodifiableSet(recipientKeyIds);
|
this.recipientKeyIds = Collections.unmodifiableSet(recipientKeyIds);
|
||||||
|
@ -49,7 +56,9 @@ public class OpenPgpMetadata {
|
||||||
this.symmetricKeyAlgorithm = symmetricKeyAlgorithm;
|
this.symmetricKeyAlgorithm = symmetricKeyAlgorithm;
|
||||||
this.compressionAlgorithm = algorithm;
|
this.compressionAlgorithm = algorithm;
|
||||||
this.integrityProtected = integrityProtected;
|
this.integrityProtected = integrityProtected;
|
||||||
this.unverifiedSignatureKeyIds = Collections.unmodifiableSet(unverifiedSignatureKeyIds);
|
this.signatures = Collections.unmodifiableSet(signatures);
|
||||||
|
this.signatureKeyIds = Collections.unmodifiableSet(signatureKeyIds);
|
||||||
|
this.verifiedSignatures = Collections.unmodifiableMap(verifiedSignatures);
|
||||||
this.verifiedSignaturesFingerprints = Collections.unmodifiableSet(verifiedSignaturesFingerprints);
|
this.verifiedSignaturesFingerprints = Collections.unmodifiableSet(verifiedSignaturesFingerprints);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,15 +86,23 @@ public class OpenPgpMetadata {
|
||||||
return integrityProtected;
|
return integrityProtected;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<Long> getAllSignatureKeyFingerprints() {
|
public Set<PGPSignature> getSignatures() {
|
||||||
return unverifiedSignatureKeyIds;
|
return signatures;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<Long> getSignatureKeyIDs() {
|
||||||
|
return signatureKeyIds;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isSigned() {
|
public boolean isSigned() {
|
||||||
return !unverifiedSignatureKeyIds.isEmpty();
|
return !signatureKeyIds.isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<OpenPgpV4Fingerprint> getVerifiedSignaturesFingerprints() {
|
public Map<OpenPgpV4Fingerprint, PGPSignature> getVerifiedSignatures() {
|
||||||
|
return verifiedSignatures;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<OpenPgpV4Fingerprint> getVerifiedSignatureKeyFingerprints() {
|
||||||
return verifiedSignaturesFingerprints;
|
return verifiedSignaturesFingerprints;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,16 +124,18 @@ public class OpenPgpMetadata {
|
||||||
return verifiedSignaturesFingerprints.contains(fingerprint);
|
return verifiedSignaturesFingerprints.contains(fingerprint);
|
||||||
}
|
}
|
||||||
|
|
||||||
static Builder getBuilder() {
|
public static Builder getBuilder() {
|
||||||
return new Builder();
|
return new Builder();
|
||||||
}
|
}
|
||||||
|
|
||||||
static class Builder {
|
public static class Builder {
|
||||||
|
|
||||||
private final Set<Long> recipientFingerprints = new HashSet<>();
|
private final Set<Long> recipientFingerprints = new HashSet<>();
|
||||||
private OpenPgpV4Fingerprint decryptionFingerprint;
|
private OpenPgpV4Fingerprint decryptionFingerprint;
|
||||||
private final Set<Long> unverifiedSignatureKeyIds = new HashSet<>();
|
private final Set<PGPSignature> signatures = new HashSet<>();
|
||||||
private final Set<OpenPgpV4Fingerprint> verifiedSignatureFingerprints = new HashSet<>();
|
private final Set<Long> signatureKeyIds = new HashSet<>();
|
||||||
|
private final Map<OpenPgpV4Fingerprint, PGPSignature> verifiedSignatures = new ConcurrentHashMap<>();
|
||||||
|
private final Set<OpenPgpV4Fingerprint> verifiedSignatureKeyFingerprints = new HashSet<>();
|
||||||
private SymmetricKeyAlgorithm symmetricKeyAlgorithm = SymmetricKeyAlgorithm.NULL;
|
private SymmetricKeyAlgorithm symmetricKeyAlgorithm = SymmetricKeyAlgorithm.NULL;
|
||||||
private CompressionAlgorithm compressionAlgorithm = CompressionAlgorithm.UNCOMPRESSED;
|
private CompressionAlgorithm compressionAlgorithm = CompressionAlgorithm.UNCOMPRESSED;
|
||||||
private boolean integrityProtected = false;
|
private boolean integrityProtected = false;
|
||||||
|
@ -136,13 +155,23 @@ public class OpenPgpMetadata {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Builder addSignature(PGPSignature signature) {
|
||||||
|
signatures.add(signature);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public Builder addUnverifiedSignatureKeyId(Long keyId) {
|
public Builder addUnverifiedSignatureKeyId(Long keyId) {
|
||||||
this.unverifiedSignatureKeyIds.add(keyId);
|
this.signatureKeyIds.add(keyId);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder putVerifiedSignature(OpenPgpV4Fingerprint fingerprint, PGPSignature verifiedSignature) {
|
||||||
|
verifiedSignatures.put(fingerprint, verifiedSignature);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Builder addVerifiedSignatureFingerprint(OpenPgpV4Fingerprint fingerprint) {
|
public Builder addVerifiedSignatureFingerprint(OpenPgpV4Fingerprint fingerprint) {
|
||||||
this.verifiedSignatureFingerprints.add(fingerprint);
|
this.verifiedSignatureKeyFingerprints.add(fingerprint);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -157,7 +186,10 @@ public class OpenPgpMetadata {
|
||||||
}
|
}
|
||||||
|
|
||||||
public OpenPgpMetadata build() {
|
public OpenPgpMetadata build() {
|
||||||
return new OpenPgpMetadata(recipientFingerprints, decryptionFingerprint, symmetricKeyAlgorithm, compressionAlgorithm, integrityProtected, unverifiedSignatureKeyIds, verifiedSignatureFingerprints);
|
return new OpenPgpMetadata(recipientFingerprints, decryptionFingerprint,
|
||||||
|
symmetricKeyAlgorithm, compressionAlgorithm, integrityProtected,
|
||||||
|
signatures, signatureKeyIds,
|
||||||
|
verifiedSignatures, verifiedSignatureKeyFingerprints);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -96,6 +96,7 @@ public class SignatureVerifyingInputStream extends FilterInputStream {
|
||||||
}
|
}
|
||||||
|
|
||||||
for (PGPSignature signature : signatureList) {
|
for (PGPSignature signature : signatureList) {
|
||||||
|
resultBuilder.addSignature(signature);
|
||||||
OpenPgpV4Fingerprint fingerprint = null;
|
OpenPgpV4Fingerprint fingerprint = null;
|
||||||
for (OpenPgpV4Fingerprint f : onePassSignatures.keySet()) {
|
for (OpenPgpV4Fingerprint f : onePassSignatures.keySet()) {
|
||||||
if (f.getKeyId() == signature.getKeyID()) {
|
if (f.getKeyId() == signature.getKeyID()) {
|
||||||
|
@ -114,6 +115,7 @@ public class SignatureVerifyingInputStream extends FilterInputStream {
|
||||||
throw new SignatureException("Bad Signature of key " + signature.getKeyID());
|
throw new SignatureException("Bad Signature of key " + signature.getKeyID());
|
||||||
} else {
|
} else {
|
||||||
LOGGER.log(LEVEL, "Verified signature of key " + Long.toHexString(signature.getKeyID()));
|
LOGGER.log(LEVEL, "Verified signature of key " + Long.toHexString(signature.getKeyID()));
|
||||||
|
resultBuilder.putVerifiedSignature(fingerprint, signature);
|
||||||
resultBuilder.addVerifiedSignatureFingerprint(fingerprint);
|
resultBuilder.addVerifiedSignatureFingerprint(fingerprint);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,7 +57,7 @@ public final class EncryptionStream extends OutputStream {
|
||||||
|
|
||||||
private static final int BUFFER_SIZE = 1 << 8;
|
private static final int BUFFER_SIZE = 1 << 8;
|
||||||
|
|
||||||
private final OpenPgpMetadata result;
|
private final OpenPgpMetadata.Builder resultBuilder = OpenPgpMetadata.getBuilder();
|
||||||
|
|
||||||
private List<PGPSignatureGenerator> signatureGenerators = new ArrayList<>();
|
private List<PGPSignatureGenerator> signatureGenerators = new ArrayList<>();
|
||||||
private boolean closed = false;
|
private boolean closed = false;
|
||||||
|
@ -147,21 +147,11 @@ public final class EncryptionStream extends OutputStream {
|
||||||
PGPLiteralData.BINARY, PGPLiteralData.CONSOLE, new Date(), new byte[BUFFER_SIZE]);
|
PGPLiteralData.BINARY, PGPLiteralData.CONSOLE, new Date(), new byte[BUFFER_SIZE]);
|
||||||
|
|
||||||
// Prepare result
|
// Prepare result
|
||||||
Set<Long> recipientKeyIds = new HashSet<>();
|
|
||||||
for (PGPPublicKey recipient : encryptionKeys) {
|
for (PGPPublicKey recipient : encryptionKeys) {
|
||||||
recipientKeyIds.add(recipient.getKeyID());
|
resultBuilder.addRecipientKeyId(recipient.getKeyID());
|
||||||
}
|
}
|
||||||
|
resultBuilder.setSymmetricKeyAlgorithm(symmetricKeyAlgorithm);
|
||||||
Set<Long> signingKeyIds = new HashSet<>();
|
resultBuilder.setCompressionAlgorithm(compressionAlgorithm);
|
||||||
for (PGPPrivateKey signer : signingKeys) {
|
|
||||||
signingKeyIds.add(signer.getKeyID());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
this.result = new OpenPgpMetadata(recipientKeyIds,
|
|
||||||
null, symmetricKeyAlgorithm,
|
|
||||||
compressionAlgorithm, true,
|
|
||||||
signingKeyIds, Collections.emptySet());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -205,7 +195,10 @@ public final class EncryptionStream extends OutputStream {
|
||||||
// Signing
|
// Signing
|
||||||
for (PGPSignatureGenerator signatureGenerator : signatureGenerators) {
|
for (PGPSignatureGenerator signatureGenerator : signatureGenerators) {
|
||||||
try {
|
try {
|
||||||
signatureGenerator.generate().encode(basicCompressionStream);
|
PGPSignature signature = signatureGenerator.generate();
|
||||||
|
signature.encode(basicCompressionStream);
|
||||||
|
resultBuilder.addSignature(signature);
|
||||||
|
resultBuilder.addUnverifiedSignatureKeyId(signature.getKeyID());
|
||||||
} catch (PGPException e) {
|
} catch (PGPException e) {
|
||||||
throw new IOException(e);
|
throw new IOException(e);
|
||||||
}
|
}
|
||||||
|
@ -230,6 +223,9 @@ public final class EncryptionStream extends OutputStream {
|
||||||
}
|
}
|
||||||
|
|
||||||
public OpenPgpMetadata getResult() {
|
public OpenPgpMetadata getResult() {
|
||||||
return result;
|
if (!closed) {
|
||||||
|
throw new IllegalStateException("EncryptionStream must be closed before accessing the Result.");
|
||||||
|
}
|
||||||
|
return resultBuilder.build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -142,10 +142,18 @@ public class EncryptDecryptTest extends AbstractPGPainlessTest {
|
||||||
.signWith(keyDecryptor, senderSec)
|
.signWith(keyDecryptor, senderSec)
|
||||||
.noArmor();
|
.noArmor();
|
||||||
|
|
||||||
|
Streams.pipeAll(new ByteArrayInputStream(secretMessage), encryptor);
|
||||||
|
encryptor.close();
|
||||||
|
byte[] encryptedSecretMessage = envelope.toByteArray();
|
||||||
|
|
||||||
|
LOGGER.log(Level.INFO, "Sender: " + PublicKeyAlgorithm.fromId(senderPub.getPublicKey().getAlgorithm()) +
|
||||||
|
" Receiver: " + PublicKeyAlgorithm.fromId(recipientPub.getPublicKey().getAlgorithm()) +
|
||||||
|
" Encrypted Length: " + encryptedSecretMessage.length);
|
||||||
|
|
||||||
OpenPgpMetadata encryptionResult = encryptor.getResult();
|
OpenPgpMetadata encryptionResult = encryptor.getResult();
|
||||||
|
|
||||||
assertFalse(encryptionResult.getAllSignatureKeyFingerprints().isEmpty());
|
assertFalse(encryptionResult.getSignatureKeyIDs().isEmpty());
|
||||||
for (long keyId : encryptionResult.getAllSignatureKeyFingerprints()) {
|
for (long keyId : encryptionResult.getSignatureKeyIDs()) {
|
||||||
assertTrue(BCUtil.keyRingContainsKeyWithId(senderPub, keyId));
|
assertTrue(BCUtil.keyRingContainsKeyWithId(senderPub, keyId));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -156,14 +164,6 @@ public class EncryptDecryptTest extends AbstractPGPainlessTest {
|
||||||
|
|
||||||
assertEquals(SymmetricKeyAlgorithm.AES_256, encryptionResult.getSymmetricKeyAlgorithm());
|
assertEquals(SymmetricKeyAlgorithm.AES_256, encryptionResult.getSymmetricKeyAlgorithm());
|
||||||
|
|
||||||
Streams.pipeAll(new ByteArrayInputStream(secretMessage), encryptor);
|
|
||||||
encryptor.close();
|
|
||||||
byte[] encryptedSecretMessage = envelope.toByteArray();
|
|
||||||
|
|
||||||
LOGGER.log(Level.INFO, "Sender: " + PublicKeyAlgorithm.fromId(senderPub.getPublicKey().getAlgorithm()) +
|
|
||||||
" Receiver: " + PublicKeyAlgorithm.fromId(recipientPub.getPublicKey().getAlgorithm()) +
|
|
||||||
" Encrypted Length: " + encryptedSecretMessage.length);
|
|
||||||
|
|
||||||
// Juliet trieth to comprehend Romeos words
|
// Juliet trieth to comprehend Romeos words
|
||||||
|
|
||||||
ByteArrayInputStream envelopeIn = new ByteArrayInputStream(encryptedSecretMessage);
|
ByteArrayInputStream envelopeIn = new ByteArrayInputStream(encryptedSecretMessage);
|
||||||
|
|
|
@ -26,6 +26,7 @@ import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
|
||||||
import org.bouncycastle.openpgp.PGPUtil;
|
import org.bouncycastle.openpgp.PGPUtil;
|
||||||
import org.bouncycastle.openpgp.operator.KeyFingerPrintCalculator;
|
import org.bouncycastle.openpgp.operator.KeyFingerPrintCalculator;
|
||||||
import org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator;
|
import org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator;
|
||||||
|
import org.pgpainless.key.OpenPgpV4Fingerprint;
|
||||||
|
|
||||||
public class TestKeys extends AbstractPGPainlessTest {
|
public class TestKeys extends AbstractPGPainlessTest {
|
||||||
|
|
||||||
|
@ -42,6 +43,8 @@ public class TestKeys extends AbstractPGPainlessTest {
|
||||||
|
|
||||||
public static final String JULIET_UID = "xmpp:juliet@capulet.lit";
|
public static final String JULIET_UID = "xmpp:juliet@capulet.lit";
|
||||||
public static final long JULIET_KEY_ID = -5425419407118114754L;
|
public static final long JULIET_KEY_ID = -5425419407118114754L;
|
||||||
|
public static final String JULIET_FINGERPRINT_STRING = "1D018C772DF8C5EF86A1DCC9B4B509CB5936E03E";
|
||||||
|
public static final OpenPgpV4Fingerprint JULIET_FINGERPRINT = new OpenPgpV4Fingerprint(JULIET_FINGERPRINT_STRING);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Public key of xmpp:juliet@capulet.lit.
|
* Public key of xmpp:juliet@capulet.lit.
|
||||||
|
@ -104,6 +107,8 @@ public class TestKeys extends AbstractPGPainlessTest {
|
||||||
|
|
||||||
public static final String ROMEO_UID = "xmpp:romeo@montague.lit";
|
public static final String ROMEO_UID = "xmpp:romeo@montague.lit";
|
||||||
public static final long ROMEO_KEY_ID = 334147643349279223L;
|
public static final long ROMEO_KEY_ID = 334147643349279223L;
|
||||||
|
public static final String ROMEO_FINGERPRINT_STRING = "35D299D08A2F7D80230B095D04A32182E05E21F7";
|
||||||
|
public static final OpenPgpV4Fingerprint ROMEO_FINGERPRINT = new OpenPgpV4Fingerprint(ROMEO_FINGERPRINT_STRING);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Public key of xmpp:romeo@montague.lit.
|
* Public key of xmpp:romeo@montague.lit.
|
||||||
|
@ -233,35 +238,24 @@ public class TestKeys extends AbstractPGPainlessTest {
|
||||||
/**
|
/**
|
||||||
* Test Message signed with {@link #JULIET_SEC} and encrypted for {@link #JULIET_PUB}.
|
* Test Message signed with {@link #JULIET_SEC} and encrypted for {@link #JULIET_PUB}.
|
||||||
*/
|
*/
|
||||||
public static final String TEST_MESSAGE_01 = "-----BEGIN PGP MESSAGE-----\n" +
|
public static final String MSG_SIGN_CRYPT_JULIET_JULIET =
|
||||||
|
"-----BEGIN PGP MESSAGE-----\n" +
|
||||||
"\n" +
|
"\n" +
|
||||||
"hQGMAwAAAAAAAAAAAQwAoJtfpcBPCwhUzzHuVIcBzBLyfIWT/EJ527neb46lN56S\n" +
|
"hQEMA7S1CctZNuA+AQf/SMX7NTOaAynogTVKE9BMWSj5fgK+7sFrCKiLYbungJEu\n" +
|
||||||
"B05BTIRudIeCsPYz81jwiFi/k0MBecRfozZ1xCPByo8ohSvRgzEHEkCNgObQ1bz0\n" +
|
"RA/fYqaJNfZN3GARqsHcGaGihQDXr0thnx71+37NhV2cHVeFkeMsHmJf/74lRrHk\n" +
|
||||||
"iB+Xb76OEzFOCPUebTaVscLNf8ak/GSzaW7jDc+5vnvDf7cV0x26pe4odpS/U5Tr\n" +
|
"QB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DtLUJy1k24D4BB/0brqR0UN1LtO+Lc/vN6X/Um2CZ\n" +
|
|
||||||
"CM6MRhPnXP63Q9HHkGJ2S8zGWvQLwWL9Y14CFCgm6rACLBSIyPbihhC2OC8afhSy\n" +
|
|
||||||
"apGkdHtdghS2egs2U8qlJ2Y32IAG9CcUtNkRjxp+/RWSrmZeuL4l7DXCyH5lUadx\n" +
|
|
||||||
"5bPZhAHqW9408q2rQd9dBg2o7ciGXTJSKVahjuiB/O0gchOnbqnlYJbKbCkntXUo\n" +
|
|
||||||
"c7h4w1e8MutisSJorh7kbxgxUJSboZzEkiUfnoacPTz6bL+re9tmnpvlee70sIyM\n" +
|
|
||||||
"BiYRCyPw7Ice4R3XyWtsMTjT/wjZ//whMpWdy2drcJSyhh+GQMbekTVsNWod0lQB\n" +
|
|
||||||
"JTPUfti2VU7PMB3LjJA+l/T9iWPPx8lirnLhXOOerWKH9I5Wo4Kqv/47aJhfMO6+\n" +
|
|
||||||
"jmLekAOylq+9DizrslW/EUgQyjIbcWfmyMiV6E2RwbI93tE=\n" +
|
|
||||||
"=GAhR\n" +
|
|
||||||
"-----END PGP MESSAGE-----";
|
"-----END PGP MESSAGE-----";
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,23 +15,27 @@
|
||||||
*/
|
*/
|
||||||
package org.pgpainless;
|
package org.pgpainless;
|
||||||
|
|
||||||
|
import static junit.framework.TestCase.assertEquals;
|
||||||
import static junit.framework.TestCase.assertTrue;
|
import static junit.framework.TestCase.assertTrue;
|
||||||
|
import static org.junit.Assert.assertArrayEquals;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
|
||||||
import org.bouncycastle.openpgp.PGPException;
|
import org.bouncycastle.openpgp.PGPException;
|
||||||
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
||||||
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
||||||
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
|
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
|
||||||
import org.bouncycastle.util.io.Streams;
|
import org.bouncycastle.util.io.Streams;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.pgpainless.algorithm.CompressionAlgorithm;
|
||||||
|
import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
|
||||||
import org.pgpainless.decryption_verification.DecryptionStream;
|
import org.pgpainless.decryption_verification.DecryptionStream;
|
||||||
|
import org.pgpainless.decryption_verification.OpenPgpMetadata;
|
||||||
|
import org.pgpainless.key.OpenPgpV4Fingerprint;
|
||||||
import org.pgpainless.key.protection.UnprotectedKeysProtector;
|
import org.pgpainless.key.protection.UnprotectedKeysProtector;
|
||||||
|
|
||||||
public class TestKeysTest extends AbstractPGPainlessTest {
|
public class TestKeysTest extends AbstractPGPainlessTest {
|
||||||
|
@ -45,14 +49,26 @@ public class TestKeysTest extends AbstractPGPainlessTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void keyIdTest() {
|
public void julietKeyTest() {
|
||||||
TestCase.assertEquals(TestKeys.JULIET_KEY_ID, juliet.getSecretKey().getKeyID());
|
assertEquals(TestKeys.JULIET_KEY_ID, juliet.getSecretKey().getKeyID());
|
||||||
TestCase.assertEquals(TestKeys.ROMEO_KEY_ID, romeo.getSecretKey().getKeyID());
|
assertEquals(TestKeys.JULIET_FINGERPRINT, new OpenPgpV4Fingerprint(juliet));
|
||||||
|
assertEquals(TestKeys.JULIET_FINGERPRINT, new OpenPgpV4Fingerprint(juliet.getPublicKey()));
|
||||||
|
assertEquals(TestKeys.JULIET_FINGERPRINT, new OpenPgpV4Fingerprint(juliet.getSecretKey()));
|
||||||
|
assertEquals(TestKeys.JULIET_KEY_ID, TestKeys.JULIET_FINGERPRINT.getKeyId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void romeoKeyTest() {
|
||||||
|
assertEquals(TestKeys.ROMEO_KEY_ID, romeo.getSecretKey().getKeyID());
|
||||||
|
assertEquals(TestKeys.ROMEO_FINGERPRINT, new OpenPgpV4Fingerprint(romeo));
|
||||||
|
assertEquals(TestKeys.ROMEO_FINGERPRINT, new OpenPgpV4Fingerprint(romeo.getPublicKey()));
|
||||||
|
assertEquals(TestKeys.ROMEO_FINGERPRINT, new OpenPgpV4Fingerprint(romeo.getSecretKey()));
|
||||||
|
assertEquals(TestKeys.ROMEO_KEY_ID, TestKeys.ROMEO_FINGERPRINT.getKeyId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void decryptVerifyTest() throws Exception {
|
public void decryptVerifyTest() throws Exception {
|
||||||
String encryptedMessage = TestKeys.TEST_MESSAGE_01;
|
String encryptedMessage = TestKeys.MSG_SIGN_CRYPT_JULIET_JULIET;
|
||||||
|
|
||||||
DecryptionStream decryptor = PGPainless.createDecryptor()
|
DecryptionStream decryptor = PGPainless.createDecryptor()
|
||||||
.onInputStream(new ByteArrayInputStream(encryptedMessage.getBytes()))
|
.onInputStream(new ByteArrayInputStream(encryptedMessage.getBytes()))
|
||||||
|
@ -65,10 +81,23 @@ public class TestKeysTest extends AbstractPGPainlessTest {
|
||||||
Streams.pipeAll(decryptor, toPlain);
|
Streams.pipeAll(decryptor, toPlain);
|
||||||
decryptor.close();
|
decryptor.close();
|
||||||
toPlain.close();
|
toPlain.close();
|
||||||
|
OpenPgpMetadata metadata = decryptor.getResult();
|
||||||
|
|
||||||
byte[] expected = TestKeys.TEST_MESSAGE_01_PLAIN.getBytes(Charset.forName("UTF-8"));
|
byte[] expected = TestKeys.TEST_MESSAGE_01_PLAIN.getBytes(StandardCharsets.UTF_8);
|
||||||
byte[] actual = toPlain.toByteArray();
|
byte[] actual = toPlain.toByteArray();
|
||||||
|
|
||||||
assertTrue(Arrays.equals(expected, actual));
|
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.getSignatureKeyIDs().size());
|
||||||
|
assertEquals(1, metadata.getVerifiedSignatureKeyFingerprints().size());
|
||||||
|
assertTrue(metadata.containsVerifiedSignatureFrom(TestKeys.JULIET_FINGERPRINT));
|
||||||
|
assertEquals(TestKeys.JULIET_FINGERPRINT, metadata.getDecryptionFingerprint());
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue