From 340ef427367191cd7d1b51ff419d2f78b6951434 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Sat, 5 Jun 2021 21:27:47 +0200 Subject: [PATCH] Bump PGPainless to 0.2.0 --- smack-openpgp/build.gradle | 2 +- .../smackx/ox/OpenPgpContact.java | 10 +- .../smackx/ox/OpenPgpManager.java | 19 +- .../jivesoftware/smackx/ox/OpenPgpSelf.java | 2 - .../ox/crypto/OpenPgpElementAndMetadata.java | 9 +- .../ox/crypto/PainlessOpenPgpProvider.java | 81 +++++---- .../ox/selection_strategy/AnnouncedKeys.java | 45 ----- .../ox/selection_strategy/BareJidUserId.java | 58 ------- .../store/abstr/AbstractOpenPgpKeyStore.java | 30 ++-- .../ox/store/abstr/AbstractOpenPgpStore.java | 3 +- .../ox/store/definition/OpenPgpKeyStore.java | 5 +- .../smackx/ox/util/SecretKeyBackupHelper.java | 59 +++++-- .../ox_im/OXInstantMessagingManager.java | 15 +- .../smackx/ox/OpenPgpElementTest.java | 8 +- .../OpenPgpSecretKeyBackupPassphraseTest.java | 4 +- .../smackx/ox/OpenPgpStoreTest.java | 93 +++++----- .../ox/PainlessOpenPgpProviderTest.java | 45 ++--- .../smackx/ox/PubSubDelegateTest.java | 6 +- .../smackx/ox/PubkeyElementTest.java | 2 +- .../smackx/ox/PublicKeysListElementTest.java | 6 +- .../smackx/ox/SecretKeyBackupHelperTest.java | 18 +- .../smackx/ox/SecretkeyElementTest.java | 6 +- .../org/jivesoftware/smackx/ox/TestKeys.java | 162 ------------------ .../ox_im/OXInstantMessagingManagerTest.java | 12 +- 24 files changed, 228 insertions(+), 472 deletions(-) delete mode 100644 smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/selection_strategy/AnnouncedKeys.java delete mode 100644 smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/selection_strategy/BareJidUserId.java delete mode 100644 smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/TestKeys.java diff --git a/smack-openpgp/build.gradle b/smack-openpgp/build.gradle index 5568f6e13..08d104afa 100644 --- a/smack-openpgp/build.gradle +++ b/smack-openpgp/build.gradle @@ -8,7 +8,7 @@ dependencies { api project(':smack-extensions') api project(':smack-experimental') - api 'org.pgpainless:pgpainless-core:0.1.0' + api 'org.pgpainless:pgpainless-core:0.2.0' testImplementation "org.bouncycastle:bcprov-jdk15on:${bouncyCastleVersion}" diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpContact.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpContact.java index 0b3e09f1c..b7ffa0e71 100644 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpContact.java +++ b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpContact.java @@ -31,11 +31,9 @@ import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.util.stringencoder.Base64; - import org.jivesoftware.smackx.ox.element.PubkeyElement; import org.jivesoftware.smackx.ox.element.PublicKeysListElement; import org.jivesoftware.smackx.ox.exception.MissingUserIdOnKeyException; -import org.jivesoftware.smackx.ox.selection_strategy.BareJidUserId; import org.jivesoftware.smackx.ox.store.definition.OpenPgpStore; import org.jivesoftware.smackx.ox.store.definition.OpenPgpTrustStore; import org.jivesoftware.smackx.ox.util.OpenPgpPubSubUtil; @@ -48,7 +46,7 @@ import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; import org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator; import org.jxmpp.jid.BareJid; import org.pgpainless.key.OpenPgpV4Fingerprint; -import org.pgpainless.util.BCUtil; +import org.pgpainless.key.info.KeyRingInfo; /** * The OpenPgpContact is sort of a specialized view on the OpenPgpStore, which gives you access to the information @@ -108,17 +106,13 @@ public class OpenPgpContact { PGPPublicKeyRingCollection anyKeys = getAnyPublicKeys(); Map announced = store.getAnnouncedFingerprintsOf(jid); - BareJidUserId.PubRingSelectionStrategy userIdFilter = new BareJidUserId.PubRingSelectionStrategy(); - PGPPublicKeyRingCollection announcedKeysCollection = null; for (OpenPgpV4Fingerprint announcedFingerprint : announced.keySet()) { PGPPublicKeyRing ring = anyKeys.getPublicKeyRing(announcedFingerprint.getKeyId()); if (ring == null) continue; - ring = BCUtil.removeUnassociatedKeysFromKeyRing(ring, ring.getPublicKey(announcedFingerprint.getKeyId())); - - if (!userIdFilter.accept(getJid(), ring)) { + if (!new KeyRingInfo(ring).isUserIdValid("xmpp:" + getJid().toString())) { LOGGER.log(Level.WARNING, "Ignore key " + Long.toHexString(ring.getPublicKey().getKeyID()) + " as it lacks the user-id \"xmpp" + getJid().toString() + "\""); continue; diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpManager.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpManager.java index 0f90ed9c3..645f5bb66 100644 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpManager.java +++ b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpManager.java @@ -79,9 +79,8 @@ import org.bouncycastle.openpgp.PGPSecretKeyRingCollection; import org.jxmpp.jid.BareJid; import org.jxmpp.jid.EntityBareJid; import org.pgpainless.key.OpenPgpV4Fingerprint; -import org.pgpainless.key.collection.PGPKeyRing; import org.pgpainless.key.protection.SecretKeyRingProtector; -import org.pgpainless.util.BCUtil; +import org.pgpainless.key.util.KeyRingUtils; /** * Entry point for Smacks API for OpenPGP for XMPP. @@ -296,27 +295,27 @@ public final class OpenPgpManager extends Manager { throwIfNoProviderSet(); OpenPgpStore store = provider.getStore(); - PGPKeyRing keys = generateKeyRing(ourJid); + PGPSecretKeyRing keys = generateKeyRing(ourJid); importKeyRing(ourJid, keys); - OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(keys.getSecretKeys()); + OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(keys); store.setTrust(ourJid, fingerprint, OpenPgpTrustStore.Trust.trusted); return fingerprint; } - public PGPKeyRing generateKeyRing(BareJid ourJid) + public PGPSecretKeyRing generateKeyRing(BareJid ourJid) throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException { throwIfNoProviderSet(); - PGPKeyRing keys = provider.getStore().generateKeyRing(ourJid); + PGPSecretKeyRing keys = provider.getStore().generateKeyRing(ourJid); return keys; } - private void importKeyRing(BareJid ourJid, PGPKeyRing keyRing) throws IOException, PGPException { + private void importKeyRing(BareJid ourJid, PGPSecretKeyRing secretKeys) throws IOException, PGPException { try { - provider.getStore().importSecretKey(ourJid, keyRing.getSecretKeys()); - provider.getStore().importPublicKey(ourJid, keyRing.getPublicKeys()); + provider.getStore().importSecretKey(ourJid, secretKeys); + provider.getStore().importPublicKey(ourJid, KeyRingUtils.publicKeyRingFrom(secretKeys)); } catch (MissingUserIdOnKeyException e) { // This should never throw, since we set our jid literally one line above this comment. throw new AssertionError(e); @@ -515,7 +514,7 @@ public final class OpenPgpManager extends Manager { PGPSecretKeyRing secretKeys = SecretKeyBackupHelper.restoreSecretKeyBackup(backup, backupCode); OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(secretKeys); provider.getStore().importSecretKey(getJidOrThrow(), secretKeys); - provider.getStore().importPublicKey(getJidOrThrow(), BCUtil.publicKeyRingFromSecretKeyRing(secretKeys)); + provider.getStore().importPublicKey(getJidOrThrow(), KeyRingUtils.publicKeyRingFrom(secretKeys)); getOpenPgpSelf().trust(fingerprint); diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpSelf.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpSelf.java index eeed7966a..6ada87150 100644 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpSelf.java +++ b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpSelf.java @@ -28,7 +28,6 @@ import org.bouncycastle.openpgp.PGPSecretKeyRing; import org.bouncycastle.openpgp.PGPSecretKeyRingCollection; import org.jxmpp.jid.BareJid; import org.pgpainless.key.OpenPgpV4Fingerprint; -import org.pgpainless.util.BCUtil; /** * This class acts as our own OpenPGP identity. It can be seen as a special view on the {@link OpenPgpStore}, giving @@ -117,7 +116,6 @@ public class OpenPgpSelf extends OpenPgpContact { public PGPPublicKeyRingCollection getAnnouncedPublicKeys() throws IOException, PGPException { PGPSecretKeyRing secretKeys = getSigningKeyRing(); PGPPublicKeyRing publicKeys = getAnyPublicKeys().getPublicKeyRing(secretKeys.getPublicKey().getKeyID()); - publicKeys = BCUtil.removeUnassociatedKeysFromKeyRing(publicKeys, secretKeys.getPublicKey()); return new PGPPublicKeyRingCollection(Collections.singleton(publicKeys)); } } diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/crypto/OpenPgpElementAndMetadata.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/crypto/OpenPgpElementAndMetadata.java index 3586c9f08..6045e120d 100644 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/crypto/OpenPgpElementAndMetadata.java +++ b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/crypto/OpenPgpElementAndMetadata.java @@ -19,6 +19,7 @@ package org.jivesoftware.smackx.ox.crypto; import org.jivesoftware.smackx.ox.element.OpenPgpElement; import org.pgpainless.decryption_verification.OpenPgpMetadata; +import org.pgpainless.encryption_signing.EncryptionResult; /** * Bundle together an {@link OpenPgpElement} and {@link OpenPgpMetadata}. @@ -26,7 +27,7 @@ import org.pgpainless.decryption_verification.OpenPgpMetadata; public class OpenPgpElementAndMetadata { private final OpenPgpElement element; - private final OpenPgpMetadata metadata; + private final EncryptionResult metadata; /** * Constructor. @@ -34,7 +35,7 @@ public class OpenPgpElementAndMetadata { * @param element element * @param metadata metadata about the elements encryption */ - public OpenPgpElementAndMetadata(OpenPgpElement element, OpenPgpMetadata metadata) { + public OpenPgpElementAndMetadata(OpenPgpElement element, EncryptionResult metadata) { this.element = element; this.metadata = metadata; } @@ -49,11 +50,11 @@ public class OpenPgpElementAndMetadata { } /** - * Return {@link OpenPgpMetadata} about the {@link OpenPgpElement}s encryption/signatures. + * Return an {@link EncryptionResult} containing metadata about the {@link OpenPgpElement}s encryption/signatures. * * @return metadata TODO javadoc me please */ - public OpenPgpMetadata getMetadata() { + public EncryptionResult getMetadata() { return metadata; } } diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/crypto/PainlessOpenPgpProvider.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/crypto/PainlessOpenPgpProvider.java index ed542a59d..e8b99e3ef 100644 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/crypto/PainlessOpenPgpProvider.java +++ b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/crypto/PainlessOpenPgpProvider.java @@ -19,7 +19,6 @@ package org.jivesoftware.smackx.ox.crypto; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; -import java.util.ArrayList; import java.util.Collection; import java.util.logging.Level; import java.util.logging.Logger; @@ -43,14 +42,18 @@ import org.jivesoftware.smackx.pubsub.PubSubException.NotALeafNodeException; import org.jivesoftware.smackx.pubsub.PubSubException.NotAPubSubNodeException; import org.bouncycastle.openpgp.PGPException; -import org.bouncycastle.openpgp.PGPPublicKey; +import org.bouncycastle.openpgp.PGPPublicKeyRing; import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; import org.bouncycastle.util.io.Streams; import org.pgpainless.PGPainless; +import org.pgpainless.algorithm.DocumentSignatureType; import org.pgpainless.decryption_verification.DecryptionStream; import org.pgpainless.decryption_verification.MissingPublicKeyCallback; import org.pgpainless.decryption_verification.OpenPgpMetadata; +import org.pgpainless.encryption_signing.EncryptionOptions; import org.pgpainless.encryption_signing.EncryptionStream; +import org.pgpainless.encryption_signing.ProducerOptions; +import org.pgpainless.encryption_signing.SigningOptions; public class PainlessOpenPgpProvider implements OpenPgpProvider { @@ -73,23 +76,26 @@ public class PainlessOpenPgpProvider implements OpenPgpProvider { InputStream plainText = element.toInputStream(); ByteArrayOutputStream cipherText = new ByteArrayOutputStream(); - ArrayList recipientKeys = new ArrayList<>(); + EncryptionOptions encOpts = EncryptionOptions.encryptCommunications(); for (OpenPgpContact contact : recipients) { PGPPublicKeyRingCollection keys = contact.getTrustedAnnouncedKeys(); if (keys == null) { LOGGER.log(Level.WARNING, "There are no suitable keys for contact " + contact.getJid()); - } - - recipientKeys.add(keys); + encOpts.addRecipients(keys); } - EncryptionStream cipherStream = PGPainless.createEncryptor().onOutputStream(cipherText) - .toRecipients(recipientKeys.toArray(new PGPPublicKeyRingCollection[] {})) - .andToSelf(self.getTrustedAnnouncedKeys()) - .usingSecureAlgorithms() - .signWith(getStore().getKeyRingProtector(), self.getSigningKeyRing()) - .noArmor(); + encOpts.addRecipients(self.getTrustedAnnouncedKeys()); + + SigningOptions signOpts = new SigningOptions(); + signOpts.addInlineSignature(getStore().getKeyRingProtector(), self.getSigningKeyRing(), + "xmpp:" + self.getJid().toString(), DocumentSignatureType.BINARY_DOCUMENT); + + EncryptionStream cipherStream = PGPainless.encryptAndOrSign() + .onOutputStream(cipherText) + .withOptions(ProducerOptions + .signAndEncrypt(encOpts, signOpts) + .setAsciiArmor(false)); Streams.pipeAll(plainText, cipherStream); plainText.close(); @@ -109,10 +115,12 @@ public class PainlessOpenPgpProvider implements OpenPgpProvider { InputStream plainText = element.toInputStream(); ByteArrayOutputStream cipherText = new ByteArrayOutputStream(); - EncryptionStream cipherStream = PGPainless.createEncryptor().onOutputStream(cipherText) - .doNotEncrypt() - .signWith(getStore().getKeyRingProtector(), self.getSigningKeyRing()) - .noArmor(); + EncryptionStream cipherStream = PGPainless.encryptAndOrSign() + .onOutputStream(cipherText) + .withOptions(ProducerOptions.sign(new SigningOptions() + .addInlineSignature(getStore().getKeyRingProtector(), self.getSigningKeyRing(), + "xmpp:" + self.getJid().toString(), DocumentSignatureType.BINARY_DOCUMENT) + ).setAsciiArmor(false)); Streams.pipeAll(plainText, cipherStream); plainText.close(); @@ -132,22 +140,23 @@ public class PainlessOpenPgpProvider implements OpenPgpProvider { InputStream plainText = element.toInputStream(); ByteArrayOutputStream cipherText = new ByteArrayOutputStream(); - ArrayList recipientKeys = new ArrayList<>(); + EncryptionOptions encOpts = EncryptionOptions.encryptCommunications(); for (OpenPgpContact contact : recipients) { PGPPublicKeyRingCollection keys = contact.getTrustedAnnouncedKeys(); - if (keys != null) { - recipientKeys.add(keys); - } else { - LOGGER.log(Level.WARNING, "There are no suitable keys for contact " + contact.getJid().toString()); + if (keys == null) { + LOGGER.log(Level.WARNING, "There are no suitable keys for contact " + contact.getJid()); } + encOpts.addRecipients(keys); } - EncryptionStream cipherStream = PGPainless.createEncryptor().onOutputStream(cipherText) - .toRecipients(recipientKeys.toArray(new PGPPublicKeyRingCollection[] {})) - .andToSelf(self.getTrustedAnnouncedKeys()) - .usingSecureAlgorithms() - .doNotSign() - .noArmor(); + encOpts.addRecipients(self.getTrustedAnnouncedKeys()); + + EncryptionStream cipherStream = PGPainless.encryptAndOrSign() + .onOutputStream(cipherText) + .withOptions(ProducerOptions + .encrypt(encOpts) + .setAsciiArmor(false) + ); Streams.pipeAll(plainText, cipherStream); plainText.close(); @@ -172,26 +181,34 @@ public class PainlessOpenPgpProvider implements OpenPgpProvider { sender.updateKeys(connection); announcedPublicKeys = sender.getAnnouncedPublicKeys(); } catch (InterruptedException | NotALeafNodeException | NotAPubSubNodeException | NotConnectedException - | NoResponseException | XMPPErrorException e) { + | NoResponseException | XMPPErrorException e) { throw new PGPException("Abort decryption due to lack of keys", e); } } MissingPublicKeyCallback missingPublicKeyCallback = new MissingPublicKeyCallback() { + @Override - public PGPPublicKey onMissingPublicKeyEncountered(Long keyId) { + public PGPPublicKeyRing onMissingPublicKeyEncountered(Long keyId) { try { sender.updateKeys(connection); - return sender.getAnyPublicKeys().getPublicKey(keyId); + PGPPublicKeyRingCollection anyKeys = sender.getAnyPublicKeys(); + for (PGPPublicKeyRing ring : anyKeys) { + if (ring.getPublicKey(keyId) != null) { + return ring; + } + } + return null; } catch (InterruptedException | NotALeafNodeException | NotAPubSubNodeException | NotConnectedException - | NoResponseException | XMPPErrorException | IOException | PGPException e) { + | NoResponseException | XMPPErrorException | IOException | PGPException e) { LOGGER.log(Level.WARNING, "Cannot fetch missing key " + keyId, e); return null; } } }; - DecryptionStream cipherStream = PGPainless.createDecryptor().onInputStream(cipherText) + DecryptionStream cipherStream = PGPainless.decryptAndOrVerify() + .onInputStream(cipherText) .decryptWith(getStore().getKeyRingProtector(), self.getSecretKeys()) .verifyWith(announcedPublicKeys) .handleMissingPublicKeysWith(missingPublicKeyCallback) diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/selection_strategy/AnnouncedKeys.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/selection_strategy/AnnouncedKeys.java deleted file mode 100644 index 041d37947..000000000 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/selection_strategy/AnnouncedKeys.java +++ /dev/null @@ -1,45 +0,0 @@ -/** - * - * Copyright 2018 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.jivesoftware.smackx.ox.selection_strategy; - -import java.util.Date; -import java.util.Map; - -import org.bouncycastle.openpgp.PGPPublicKeyRing; -import org.bouncycastle.openpgp.PGPSecretKeyRing; -import org.pgpainless.key.OpenPgpV4Fingerprint; -import org.pgpainless.key.selection.keyring.PublicKeyRingSelectionStrategy; -import org.pgpainless.key.selection.keyring.SecretKeyRingSelectionStrategy; - -public class AnnouncedKeys { - - public static class PubKeyRingSelectionStrategy extends PublicKeyRingSelectionStrategy> { - - @Override - public boolean accept(Map announcedKeys, PGPPublicKeyRing publicKeys) { - return announcedKeys.keySet().contains(new OpenPgpV4Fingerprint(publicKeys)); - } - } - - public static class SecKeyRingSelectionStrategy extends SecretKeyRingSelectionStrategy> { - - @Override - public boolean accept(Map announcedKeys, PGPSecretKeyRing secretKeys) { - return announcedKeys.keySet().contains(new OpenPgpV4Fingerprint(secretKeys)); - } - } -} diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/selection_strategy/BareJidUserId.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/selection_strategy/BareJidUserId.java deleted file mode 100644 index 4603a0ed3..000000000 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/selection_strategy/BareJidUserId.java +++ /dev/null @@ -1,58 +0,0 @@ -/** - * - * Copyright 2018 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.jivesoftware.smackx.ox.selection_strategy; - -import java.util.Iterator; - -import org.bouncycastle.openpgp.PGPPublicKeyRing; -import org.bouncycastle.openpgp.PGPSecretKeyRing; -import org.jxmpp.jid.BareJid; -import org.pgpainless.key.selection.keyring.PublicKeyRingSelectionStrategy; -import org.pgpainless.key.selection.keyring.SecretKeyRingSelectionStrategy; - -public class BareJidUserId { - - public static class PubRingSelectionStrategy extends PublicKeyRingSelectionStrategy { - - @Override - public boolean accept(BareJid jid, PGPPublicKeyRing ring) { - Iterator userIds = ring.getPublicKey().getUserIDs(); - while (userIds.hasNext()) { - String userId = userIds.next(); - if (userId.equals("xmpp:" + jid.toString())) { - return true; - } - } - return false; - } - } - - public static class SecRingSelectionStrategy extends SecretKeyRingSelectionStrategy { - - @Override - public boolean accept(BareJid jid, PGPSecretKeyRing ring) { - Iterator userIds = ring.getPublicKey().getUserIDs(); - while (userIds.hasNext()) { - String userId = userIds.next(); - if (userId.equals("xmpp:" + jid.toString())) { - return true; - } - } - return false; - } - } -} diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/abstr/AbstractOpenPgpKeyStore.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/abstr/AbstractOpenPgpKeyStore.java index 426c6568d..0c261ccb6 100644 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/abstr/AbstractOpenPgpKeyStore.java +++ b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/abstr/AbstractOpenPgpKeyStore.java @@ -20,6 +20,7 @@ import java.io.IOException; import java.security.InvalidAlgorithmParameterException; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; +import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.Map; @@ -27,7 +28,6 @@ import java.util.logging.Level; import java.util.logging.Logger; import org.jivesoftware.smackx.ox.exception.MissingUserIdOnKeyException; -import org.jivesoftware.smackx.ox.selection_strategy.BareJidUserId; import org.jivesoftware.smackx.ox.store.definition.OpenPgpKeyStore; import org.bouncycastle.openpgp.PGPException; @@ -38,8 +38,7 @@ import org.bouncycastle.openpgp.PGPSecretKeyRingCollection; import org.jxmpp.jid.BareJid; import org.pgpainless.PGPainless; import org.pgpainless.key.OpenPgpV4Fingerprint; -import org.pgpainless.key.collection.PGPKeyRing; -import org.pgpainless.util.BCUtil; +import org.pgpainless.key.info.KeyRingInfo; public abstract class AbstractOpenPgpKeyStore implements OpenPgpKeyStore { @@ -157,22 +156,19 @@ public abstract class AbstractOpenPgpKeyStore implements OpenPgpKeyStore { public void importSecretKey(BareJid owner, PGPSecretKeyRing secretKeys) throws IOException, PGPException, MissingUserIdOnKeyException { - // TODO: Avoid 'new' use instance method. - if (!new BareJidUserId.SecRingSelectionStrategy().accept(owner, secretKeys)) { + if (!new KeyRingInfo(secretKeys).isUserIdValid("xmpp:" + owner.toString())) { throw new MissingUserIdOnKeyException(owner, new OpenPgpV4Fingerprint(secretKeys)); } - PGPSecretKeyRing importKeys = BCUtil.removeUnassociatedKeysFromKeyRing(secretKeys, secretKeys.getPublicKey()); - PGPSecretKeyRingCollection secretKeyRings = getSecretKeysOf(owner); try { if (secretKeyRings != null) { - secretKeyRings = PGPSecretKeyRingCollection.addSecretKeyRing(secretKeyRings, importKeys); + secretKeyRings = PGPSecretKeyRingCollection.addSecretKeyRing(secretKeyRings, secretKeys); } else { - secretKeyRings = BCUtil.keyRingsToKeyRingCollection(importKeys); + secretKeyRings = new PGPSecretKeyRingCollection(Collections.singleton(secretKeys)); } } catch (IllegalArgumentException e) { - LOGGER.log(Level.INFO, "Skipping secret key ring " + Long.toHexString(importKeys.getPublicKey().getKeyID()) + + LOGGER.log(Level.INFO, "Skipping secret key ring " + Long.toHexString(secretKeys.getPublicKey().getKeyID()) + " as it is already in the key ring of " + owner.toString()); } this.secretKeyRingCollections.put(owner, secretKeyRings); @@ -182,21 +178,19 @@ public abstract class AbstractOpenPgpKeyStore implements OpenPgpKeyStore { @Override public void importPublicKey(BareJid owner, PGPPublicKeyRing publicKeys) throws IOException, PGPException, MissingUserIdOnKeyException { - if (!new BareJidUserId.PubRingSelectionStrategy().accept(owner, publicKeys)) { + if (!new KeyRingInfo(publicKeys).isUserIdValid("xmpp:" + owner.toString())) { throw new MissingUserIdOnKeyException(owner, new OpenPgpV4Fingerprint(publicKeys)); } - PGPPublicKeyRing importKeys = BCUtil.removeUnassociatedKeysFromKeyRing(publicKeys, publicKeys.getPublicKey()); - PGPPublicKeyRingCollection publicKeyRings = getPublicKeysOf(owner); try { if (publicKeyRings != null) { - publicKeyRings = PGPPublicKeyRingCollection.addPublicKeyRing(publicKeyRings, importKeys); + publicKeyRings = PGPPublicKeyRingCollection.addPublicKeyRing(publicKeyRings, publicKeys); } else { - publicKeyRings = BCUtil.keyRingsToKeyRingCollection(importKeys); + publicKeyRings = new PGPPublicKeyRingCollection(Collections.singleton(publicKeys)); } } catch (IllegalArgumentException e) { - LOGGER.log(Level.FINE, "Skipping public key ring " + Long.toHexString(importKeys.getPublicKey().getKeyID()) + + LOGGER.log(Level.FINE, "Skipping public key ring " + Long.toHexString(publicKeys.getPublicKey().getKeyID()) + " as it is already in the key ring of " + owner.toString(), e); } this.publicKeyRingCollections.put(owner, publicKeyRings); @@ -252,8 +246,8 @@ public abstract class AbstractOpenPgpKeyStore implements OpenPgpKeyStore { } @Override - public PGPKeyRing generateKeyRing(BareJid owner) + public PGPSecretKeyRing generateKeyRing(BareJid owner) throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException { - return PGPainless.generateKeyRing().simpleEcKeyRing("xmpp:" + owner.toString()); + return PGPainless.generateKeyRing().modernKeyRing("xmpp:" + owner.toString(), null); } } diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/abstr/AbstractOpenPgpStore.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/abstr/AbstractOpenPgpStore.java index 83c46ebdf..24d5a2d44 100644 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/abstr/AbstractOpenPgpStore.java +++ b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/abstr/AbstractOpenPgpStore.java @@ -41,7 +41,6 @@ import org.bouncycastle.openpgp.PGPSecretKeyRing; import org.bouncycastle.openpgp.PGPSecretKeyRingCollection; import org.jxmpp.jid.BareJid; import org.pgpainless.key.OpenPgpV4Fingerprint; -import org.pgpainless.key.collection.PGPKeyRing; import org.pgpainless.key.protection.SecretKeyRingProtector; import org.pgpainless.key.protection.UnprotectedKeysProtector; @@ -123,7 +122,7 @@ public abstract class AbstractOpenPgpStore implements OpenPgpStore { } @Override - public PGPKeyRing generateKeyRing(BareJid owner) throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException { + public PGPSecretKeyRing generateKeyRing(BareJid owner) throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException { return keyStore.generateKeyRing(owner); } diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/definition/OpenPgpKeyStore.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/definition/OpenPgpKeyStore.java index 8d5600314..b67d07820 100644 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/definition/OpenPgpKeyStore.java +++ b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/definition/OpenPgpKeyStore.java @@ -32,7 +32,6 @@ import org.bouncycastle.openpgp.PGPSecretKeyRing; import org.bouncycastle.openpgp.PGPSecretKeyRingCollection; import org.jxmpp.jid.BareJid; import org.pgpainless.key.OpenPgpV4Fingerprint; -import org.pgpainless.key.collection.PGPKeyRing; public interface OpenPgpKeyStore { @@ -113,7 +112,7 @@ public interface OpenPgpKeyStore { void deleteSecretKeyRing(BareJid owner, OpenPgpV4Fingerprint fingerprint) throws IOException, PGPException; /** - * Generate a new {@link PGPKeyRing} for {@code owner}. + * Generate a new {@link PGPSecretKeyRing} for {@code owner}. * The key will have a user-id containing the users {@link BareJid} (eg. "xmpp:juliet@capulet.lit"). * This method MUST NOT return null. * @@ -126,7 +125,7 @@ public interface OpenPgpKeyStore { * @throws NoSuchProviderException in case there is no suitable {@link java.security.Provider} registered. * @throws InvalidAlgorithmParameterException in case an invalid algorithms configuration is used. */ - PGPKeyRing generateKeyRing(BareJid owner) throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException; + PGPSecretKeyRing generateKeyRing(BareJid owner) throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException; /** * Import a {@link PGPSecretKeyRing} of {@code owner}. diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/util/SecretKeyBackupHelper.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/util/SecretKeyBackupHelper.java index 978f22eb4..3c8483dfe 100644 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/util/SecretKeyBackupHelper.java +++ b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/util/SecretKeyBackupHelper.java @@ -16,8 +16,10 @@ */ package org.jivesoftware.smackx.ox.util; +import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.InputStream; import java.util.Set; import org.jivesoftware.smack.util.StringUtils; @@ -31,9 +33,15 @@ import org.jivesoftware.smackx.ox.exception.MissingOpenPgpKeyException; import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPSecretKeyRing; +import org.bouncycastle.util.io.Streams; import org.jxmpp.jid.BareJid; import org.pgpainless.PGPainless; import org.pgpainless.algorithm.SymmetricKeyAlgorithm; +import org.pgpainless.decryption_verification.DecryptionStream; +import org.pgpainless.encryption_signing.EncryptionOptions; +import org.pgpainless.encryption_signing.EncryptionStream; +import org.pgpainless.encryption_signing.ProducerOptions; +import org.pgpainless.exception.MissingDecryptionMethodException; import org.pgpainless.key.OpenPgpV4Fingerprint; import org.pgpainless.util.Passphrase; @@ -72,21 +80,21 @@ public class SecretKeyBackupHelper { * not accessible. */ public static SecretkeyElement createSecretkeyElement(OpenPgpProvider provider, - BareJid owner, - Set fingerprints, - OpenPgpSecretKeyBackupPassphrase backupCode) + BareJid owner, + Set fingerprints, + OpenPgpSecretKeyBackupPassphrase backupCode) throws PGPException, IOException, MissingOpenPgpKeyException { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); for (OpenPgpV4Fingerprint fingerprint : fingerprints) { - PGPSecretKeyRing key = provider.getStore().getSecretKeyRing(owner, fingerprint); - if (key == null) { - throw new MissingOpenPgpKeyException(owner, fingerprint); - } + PGPSecretKeyRing key = provider.getStore().getSecretKeyRing(owner, fingerprint); + if (key == null) { + throw new MissingOpenPgpKeyException(owner, fingerprint); + } - byte[] bytes = key.getEncoded(); - buffer.write(bytes); + byte[] bytes = key.getEncoded(); + buffer.write(bytes); } return createSecretkeyElement(buffer.toByteArray(), backupCode); } @@ -108,9 +116,21 @@ public class SecretKeyBackupHelper { public static SecretkeyElement createSecretkeyElement(byte[] keys, OpenPgpSecretKeyBackupPassphrase backupCode) throws PGPException, IOException { - byte[] encrypted = PGPainless.encryptWithPassword(keys, new Passphrase(backupCode.toString().toCharArray()), - SymmetricKeyAlgorithm.AES_256); - return new SecretkeyElement(Base64.encode(encrypted)); + InputStream keyStream = new ByteArrayInputStream(keys); + ByteArrayOutputStream cryptOut = new ByteArrayOutputStream(); + EncryptionOptions encOpts = new EncryptionOptions() + .addPassphrase(Passphrase.fromPassword(backupCode.toString())); + encOpts.overrideEncryptionAlgorithm(SymmetricKeyAlgorithm.AES_256); + + EncryptionStream encryptionStream = PGPainless.encryptAndOrSign() + .onOutputStream(cryptOut) + .withOptions(ProducerOptions.encrypt(encOpts) + .setAsciiArmor(false)); + + Streams.pipeAll(keyStream, encryptionStream); + encryptionStream.close(); + + return new SecretkeyElement(Base64.encode(cryptOut.toByteArray())); } /** @@ -127,14 +147,23 @@ public class SecretKeyBackupHelper { public static PGPSecretKeyRing restoreSecretKeyBackup(SecretkeyElement backup, OpenPgpSecretKeyBackupPassphrase backupCode) throws InvalidBackupCodeException, IOException, PGPException { byte[] encrypted = Base64.decode(backup.getB64Data()); + InputStream encryptedIn = new ByteArrayInputStream(encrypted); + ByteArrayOutputStream plaintextOut = new ByteArrayOutputStream(); - byte[] decrypted; try { - decrypted = PGPainless.decryptWithPassword(encrypted, new Passphrase(backupCode.toString().toCharArray())); - } catch (IOException | PGPException e) { + DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify() + .onInputStream(encryptedIn) + .decryptWith(Passphrase.fromPassword(backupCode.toString())) + .doNotVerify() + .build(); + + Streams.pipeAll(decryptionStream, plaintextOut); + decryptionStream.close(); + } catch (MissingDecryptionMethodException e) { throw new InvalidBackupCodeException("Could not decrypt secret key backup. Possibly wrong passphrase?", e); } + byte[] decrypted = plaintextOut.toByteArray(); return PGPainless.readKeyRing().secretKeyRing(decrypted); } } diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox_im/OXInstantMessagingManager.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox_im/OXInstantMessagingManager.java index c9fc021eb..db4a883d1 100644 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox_im/OXInstantMessagingManager.java +++ b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox_im/OXInstantMessagingManager.java @@ -49,6 +49,7 @@ import org.bouncycastle.openpgp.PGPException; import org.jxmpp.jid.BareJid; import org.jxmpp.jid.Jid; import org.pgpainless.decryption_verification.OpenPgpMetadata; +import org.pgpainless.encryption_signing.EncryptionResult; import org.pgpainless.key.OpenPgpV4Fingerprint; /** @@ -217,7 +218,7 @@ public final class OXInstantMessagingManager extends Manager { * @param contact contact capable of OpenPGP for XMPP: Instant Messaging. * @param body message body. * - * @return {@link OpenPgpMetadata} about the messages encryption + signatures. + * @return {@link EncryptionResult} containing metadata about the messages encryption + signatures. * * @throws InterruptedException if the thread is interrupted * @throws IOException IO is dangerous @@ -225,7 +226,7 @@ public final class OXInstantMessagingManager extends Manager { * @throws SmackException.NotLoggedInException if we are not logged in * @throws PGPException PGP is brittle */ - public OpenPgpMetadata sendOxMessage(OpenPgpContact contact, CharSequence body) + public EncryptionResult sendOxMessage(OpenPgpContact contact, CharSequence body) throws InterruptedException, IOException, SmackException.NotConnectedException, SmackException.NotLoggedInException, PGPException { MessageBuilder messageBuilder = connection() @@ -234,7 +235,7 @@ public final class OXInstantMessagingManager extends Manager { .to(contact.getJid()); Message.Body mBody = new Message.Body(null, body.toString()); - OpenPgpMetadata metadata = addOxMessage(messageBuilder, contact, Collections.singletonList(mBody)); + EncryptionResult metadata = addOxMessage(messageBuilder, contact, Collections.singletonList(mBody)); Message message = messageBuilder.build(); ChatManager.getInstanceFor(connection()).chatWith(contact.getJid().asEntityBareJidIfPossible()).send(message); @@ -249,13 +250,13 @@ public final class OXInstantMessagingManager extends Manager { * @param contact recipient of the message * @param payload payload which will be encrypted and signed * - * @return {@link OpenPgpMetadata} about the messages encryption + metadata. + * @return {@link EncryptionResult} containing metadata about the messages encryption + metadata. * * @throws SmackException.NotLoggedInException in case we are not logged in * @throws PGPException in case something goes wrong during encryption * @throws IOException IO is dangerous (we need to read keys) */ - public OpenPgpMetadata addOxMessage(MessageBuilder messageBuilder, OpenPgpContact contact, List payload) + public EncryptionResult addOxMessage(MessageBuilder messageBuilder, OpenPgpContact contact, List payload) throws SmackException.NotLoggedInException, PGPException, IOException { return addOxMessage(messageBuilder, Collections.singleton(contact), payload); } @@ -267,13 +268,13 @@ public final class OXInstantMessagingManager extends Manager { * @param recipients recipients of the message * @param payload payload which will be encrypted and signed * - * @return metadata about the messages encryption + signatures. + * @return {@link EncryptionResult} containing metadata about the messages encryption + signatures. * * @throws SmackException.NotLoggedInException in case we are not logged in * @throws PGPException in case something goes wrong during encryption * @throws IOException IO is dangerous (we need to read keys) */ - public OpenPgpMetadata addOxMessage(MessageBuilder messageBuilder, Set recipients, List payload) + public EncryptionResult addOxMessage(MessageBuilder messageBuilder, Set recipients, List payload) throws SmackException.NotLoggedInException, IOException, PGPException { OpenPgpElementAndMetadata openPgpElementAndMetadata = signAndEncrypt(recipients, payload); diff --git a/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/OpenPgpElementTest.java b/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/OpenPgpElementTest.java index 306037743..2c54a0d55 100644 --- a/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/OpenPgpElementTest.java +++ b/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/OpenPgpElementTest.java @@ -16,12 +16,11 @@ */ package org.jivesoftware.smackx.ox; -import static junit.framework.TestCase.assertEquals; -import static junit.framework.TestCase.assertNotNull; import static org.jivesoftware.smack.test.util.XmlAssertUtil.assertXmlSimilar; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; -import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.Date; @@ -35,7 +34,6 @@ import org.jivesoftware.smack.test.util.SmackTestSuite; import org.jivesoftware.smack.test.util.TestUtils; import org.jivesoftware.smack.xml.XmlPullParser; import org.jivesoftware.smack.xml.XmlPullParserException; - import org.jivesoftware.smackx.ox.element.CryptElement; import org.jivesoftware.smackx.ox.element.OpenPgpElement; import org.jivesoftware.smackx.ox.element.SignElement; @@ -180,7 +178,7 @@ public class OpenPgpElementTest extends SmackTestSuite { } @Test - public void openPgpContentElementProvider_invalidElementTest() throws IOException, XmlPullParserException { + public void openPgpContentElementProvider_invalidElementTest() { String invalidElementXML = "" + "This is a secret message." + ""; diff --git a/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/OpenPgpSecretKeyBackupPassphraseTest.java b/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/OpenPgpSecretKeyBackupPassphraseTest.java index 3c910a006..fd346812a 100644 --- a/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/OpenPgpSecretKeyBackupPassphraseTest.java +++ b/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/OpenPgpSecretKeyBackupPassphraseTest.java @@ -16,8 +16,8 @@ */ package org.jivesoftware.smackx.ox; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertThrows; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import org.jivesoftware.smackx.ox.util.SecretKeyBackupHelper; diff --git a/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/OpenPgpStoreTest.java b/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/OpenPgpStoreTest.java index 5ba6fd0e1..d64c1522c 100644 --- a/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/OpenPgpStoreTest.java +++ b/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/OpenPgpStoreTest.java @@ -16,12 +16,13 @@ */ package org.jivesoftware.smackx.ox; -import static junit.framework.TestCase.assertEquals; -import static junit.framework.TestCase.assertFalse; -import static junit.framework.TestCase.assertNotNull; -import static junit.framework.TestCase.assertNull; -import static junit.framework.TestCase.assertTrue; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.File; import java.io.IOException; @@ -36,7 +37,6 @@ import java.util.HashMap; import java.util.Map; import org.jivesoftware.smack.test.util.SmackTestSuite; - import org.jivesoftware.smackx.ox.callback.SecretKeyPassphraseCallback; import org.jivesoftware.smackx.ox.exception.MissingUserIdOnKeyException; import org.jivesoftware.smackx.ox.store.definition.OpenPgpStore; @@ -56,8 +56,8 @@ import org.junit.runners.Parameterized; import org.jxmpp.jid.BareJid; import org.jxmpp.jid.JidTestUtil; import org.pgpainless.key.OpenPgpV4Fingerprint; -import org.pgpainless.key.collection.PGPKeyRing; import org.pgpainless.key.protection.UnprotectedKeysProtector; +import org.pgpainless.key.util.KeyRingUtils; import org.pgpainless.util.Passphrase; @RunWith(Parameterized.class) @@ -128,15 +128,16 @@ public class OpenPgpStoreTest extends SmackTestSuite { assertNull(openPgpStoreInstance1.getSecretKeysOf(alice)); assertNull(openPgpStoreInstance1.getPublicKeysOf(alice)); - PGPKeyRing keys = openPgpStoreInstance1.generateKeyRing(alice); - openPgpStoreInstance1.importSecretKey(alice, keys.getSecretKeys()); - openPgpStoreInstance1.importPublicKey(alice, keys.getPublicKeys()); + PGPSecretKeyRing keys = openPgpStoreInstance1.generateKeyRing(alice); + OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(keys); + openPgpStoreInstance1.importSecretKey(alice, keys); + openPgpStoreInstance1.importPublicKey(alice, KeyRingUtils.publicKeyRingFrom(keys)); assertNotNull(openPgpStoreInstance1.getSecretKeysOf(alice)); assertNotNull(openPgpStoreInstance1.getPublicKeysOf(alice)); - openPgpStoreInstance1.deleteSecretKeyRing(alice, new OpenPgpV4Fingerprint(keys.getSecretKeys())); - openPgpStoreInstance1.deletePublicKeyRing(alice, new OpenPgpV4Fingerprint(keys.getSecretKeys())); + openPgpStoreInstance1.deleteSecretKeyRing(alice, fingerprint); + openPgpStoreInstance1.deletePublicKeyRing(alice, fingerprint); assertNull(openPgpStoreInstance1.getPublicKeysOf(alice)); assertNull(openPgpStoreInstance1.getSecretKeysOf(alice)); @@ -154,24 +155,23 @@ public class OpenPgpStoreTest extends SmackTestSuite { public void t02_key_importKeysTest() throws IOException, PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException, MissingUserIdOnKeyException { // Test for nullity of all possible values. - PGPKeyRing keys = openPgpStoreInstance1.generateKeyRing(alice); - - PGPSecretKeyRing secretKeys = keys.getSecretKeys(); - PGPPublicKeyRing publicKeys = keys.getPublicKeys(); + PGPSecretKeyRing secretKeys = openPgpStoreInstance1.generateKeyRing(alice); + PGPPublicKeyRing publicKeys = KeyRingUtils.publicKeyRingFrom(secretKeys); assertNotNull(secretKeys); assertNotNull(publicKeys); + OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(secretKeys); + OpenPgpContact cAlice = openPgpStoreInstance1.getOpenPgpContact(alice); assertNull(cAlice.getAnyPublicKeys()); - OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(publicKeys); assertEquals(fingerprint, new OpenPgpV4Fingerprint(secretKeys)); assertNull(openPgpStoreInstance1.getPublicKeysOf(alice)); assertNull(openPgpStoreInstance1.getSecretKeysOf(alice)); openPgpStoreInstance1.importPublicKey(alice, publicKeys); - assertTrue(Arrays.equals(publicKeys.getEncoded(), openPgpStoreInstance1.getPublicKeysOf(alice).getEncoded())); + assertArrayEquals(publicKeys.getEncoded(), openPgpStoreInstance1.getPublicKeysOf(alice).getEncoded()); assertNotNull(openPgpStoreInstance1.getPublicKeyRing(alice, fingerprint)); assertNull(openPgpStoreInstance1.getSecretKeysOf(alice)); @@ -180,17 +180,17 @@ public class OpenPgpStoreTest extends SmackTestSuite { // Import keys a second time -> No change expected. openPgpStoreInstance1.importPublicKey(alice, publicKeys); - assertTrue(Arrays.equals(publicKeys.getEncoded(), openPgpStoreInstance1.getPublicKeysOf(alice).getEncoded())); + assertArrayEquals(publicKeys.getEncoded(), openPgpStoreInstance1.getPublicKeysOf(alice).getEncoded()); openPgpStoreInstance1.importSecretKey(alice, secretKeys); - assertTrue(Arrays.equals(secretKeys.getEncoded(), openPgpStoreInstance1.getSecretKeysOf(alice).getEncoded())); + assertArrayEquals(secretKeys.getEncoded(), openPgpStoreInstance1.getSecretKeysOf(alice).getEncoded()); openPgpStoreInstance1.importSecretKey(alice, secretKeys); assertNotNull(openPgpStoreInstance1.getSecretKeysOf(alice)); - assertTrue(Arrays.equals(secretKeys.getEncoded(), openPgpStoreInstance1.getSecretKeysOf(alice).getEncoded())); + assertArrayEquals(secretKeys.getEncoded(), openPgpStoreInstance1.getSecretKeysOf(alice).getEncoded()); assertNotNull(openPgpStoreInstance1.getSecretKeyRing(alice, fingerprint)); - assertTrue(Arrays.equals(secretKeys.getEncoded(), openPgpStoreInstance1.getSecretKeyRing(alice, fingerprint).getEncoded())); - assertTrue(Arrays.equals(publicKeys.getEncoded(), openPgpStoreInstance1.getPublicKeyRing(alice, fingerprint).getEncoded())); + assertArrayEquals(secretKeys.getEncoded(), openPgpStoreInstance1.getSecretKeyRing(alice, fingerprint).getEncoded()); + assertArrayEquals(publicKeys.getEncoded(), openPgpStoreInstance1.getPublicKeyRing(alice, fingerprint).getEncoded()); // Clean up openPgpStoreInstance1.deletePublicKeyRing(alice, fingerprint); @@ -198,27 +198,27 @@ public class OpenPgpStoreTest extends SmackTestSuite { } @Test - public void t04_key_wrongBareJidOnSecretKeyImportTest() throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, IOException, MissingUserIdOnKeyException { - PGPSecretKeyRing secretKeys = openPgpStoreInstance1.generateKeyRing(alice).getSecretKeys(); + public void t04_key_wrongBareJidOnSecretKeyImportTest() throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException { + PGPSecretKeyRing secretKeys = openPgpStoreInstance1.generateKeyRing(alice); assertThrows(MissingUserIdOnKeyException.class, () -> - openPgpStoreInstance1.importSecretKey(bob, secretKeys)); + openPgpStoreInstance1.importSecretKey(bob, secretKeys)); } @Test - public void t05_key_wrongBareJidOnPublicKeyImportTest() throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, IOException, MissingUserIdOnKeyException { - PGPPublicKeyRing publicKeys = openPgpStoreInstance1.generateKeyRing(alice).getPublicKeys(); + public void t05_key_wrongBareJidOnPublicKeyImportTest() throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException { + PGPSecretKeyRing secretKeys = openPgpStoreInstance1.generateKeyRing(alice); + PGPPublicKeyRing publicKeys = KeyRingUtils.publicKeyRingFrom(secretKeys); assertThrows(MissingUserIdOnKeyException.class, () -> - openPgpStoreInstance1.importPublicKey(bob, publicKeys)); + openPgpStoreInstance1.importPublicKey(bob, publicKeys)); } @Test public void t06_key_keyReloadTest() throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, IOException, MissingUserIdOnKeyException { - PGPKeyRing keys = openPgpStoreInstance1.generateKeyRing(alice); - PGPSecretKeyRing secretKeys = keys.getSecretKeys(); + PGPSecretKeyRing secretKeys = openPgpStoreInstance1.generateKeyRing(alice); OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(secretKeys); - PGPPublicKeyRing publicKeys = keys.getPublicKeys(); + PGPPublicKeyRing publicKeys = KeyRingUtils.publicKeyRingFrom(secretKeys); openPgpStoreInstance1.importSecretKey(alice, secretKeys); openPgpStoreInstance1.importPublicKey(alice, publicKeys); @@ -235,24 +235,23 @@ public class OpenPgpStoreTest extends SmackTestSuite { @Test public void t07_multipleKeysTest() throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, IOException, MissingUserIdOnKeyException { - PGPKeyRing one = openPgpStoreInstance1.generateKeyRing(alice); - PGPKeyRing two = openPgpStoreInstance1.generateKeyRing(alice); + PGPSecretKeyRing one = openPgpStoreInstance1.generateKeyRing(alice); + PGPSecretKeyRing two = openPgpStoreInstance1.generateKeyRing(alice); - OpenPgpV4Fingerprint fingerprint1 = new OpenPgpV4Fingerprint(one.getSecretKeys()); - OpenPgpV4Fingerprint fingerprint2 = new OpenPgpV4Fingerprint(two.getSecretKeys()); + OpenPgpV4Fingerprint fingerprint1 = new OpenPgpV4Fingerprint(one); + OpenPgpV4Fingerprint fingerprint2 = new OpenPgpV4Fingerprint(two); - openPgpStoreInstance1.importSecretKey(alice, one.getSecretKeys()); - openPgpStoreInstance1.importSecretKey(alice, two.getSecretKeys()); - openPgpStoreInstance1.importPublicKey(alice, one.getPublicKeys()); - openPgpStoreInstance1.importPublicKey(alice, two.getPublicKeys()); + openPgpStoreInstance1.importSecretKey(alice, one); + openPgpStoreInstance1.importSecretKey(alice, two); + openPgpStoreInstance1.importPublicKey(alice, KeyRingUtils.publicKeyRingFrom(one)); + openPgpStoreInstance1.importPublicKey(alice, KeyRingUtils.publicKeyRingFrom(two)); - assertTrue(Arrays.equals(one.getSecretKeys().getEncoded(), openPgpStoreInstance1.getSecretKeyRing(alice, fingerprint1).getEncoded())); - assertTrue(Arrays.equals(two.getSecretKeys().getEncoded(), openPgpStoreInstance1.getSecretKeyRing(alice, fingerprint2).getEncoded())); + assertArrayEquals(one.getEncoded(), openPgpStoreInstance1.getSecretKeyRing(alice, fingerprint1).getEncoded()); + assertArrayEquals(two.getEncoded(), openPgpStoreInstance1.getSecretKeyRing(alice, fingerprint2).getEncoded()); - assertTrue(Arrays.equals(one.getSecretKeys().getEncoded(), openPgpStoreInstance1.getSecretKeysOf(alice).getSecretKeyRing(fingerprint1.getKeyId()).getEncoded())); + assertArrayEquals(one.getEncoded(), openPgpStoreInstance1.getSecretKeysOf(alice).getSecretKeyRing(fingerprint1.getKeyId()).getEncoded()); - assertTrue(Arrays.equals(one.getPublicKeys().getEncoded(), - openPgpStoreInstance1.getPublicKeyRing(alice, fingerprint1).getEncoded())); + assertArrayEquals(KeyRingUtils.publicKeyRingFrom(one).getEncoded(), openPgpStoreInstance1.getPublicKeyRing(alice, fingerprint1).getEncoded()); // Cleanup openPgpStoreInstance1.deletePublicKeyRing(alice, fingerprint1); @@ -320,8 +319,8 @@ public class OpenPgpStoreTest extends SmackTestSuite { assertFalse(openPgpStoreInstance2.getAnnouncedFingerprintsOf(alice).isEmpty()); assertEquals(map, openPgpStoreInstance2.getAnnouncedFingerprintsOf(alice)); - openPgpStoreInstance1.setAnnouncedFingerprintsOf(alice, Collections.emptyMap()); - openPgpStoreInstance2.setAnnouncedFingerprintsOf(alice, Collections.emptyMap()); + openPgpStoreInstance1.setAnnouncedFingerprintsOf(alice, Collections.emptyMap()); + openPgpStoreInstance2.setAnnouncedFingerprintsOf(alice, Collections.emptyMap()); } @Test diff --git a/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/PainlessOpenPgpProviderTest.java b/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/PainlessOpenPgpProviderTest.java index 903a2b827..f6b78493a 100644 --- a/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/PainlessOpenPgpProviderTest.java +++ b/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/PainlessOpenPgpProviderTest.java @@ -16,9 +16,9 @@ */ package org.jivesoftware.smackx.ox; -import static junit.framework.TestCase.assertEquals; -import static junit.framework.TestCase.assertNull; -import static junit.framework.TestCase.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.File; import java.io.IOException; @@ -35,7 +35,6 @@ import org.jivesoftware.smack.packet.ExtensionElement; import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.test.util.SmackTestSuite; import org.jivesoftware.smack.xml.XmlPullParserException; - import org.jivesoftware.smackx.ox.crypto.OpenPgpElementAndMetadata; import org.jivesoftware.smackx.ox.crypto.PainlessOpenPgpProvider; import org.jivesoftware.smackx.ox.element.CryptElement; @@ -46,6 +45,8 @@ import org.jivesoftware.smackx.ox.store.definition.OpenPgpStore; import org.jivesoftware.smackx.ox.store.filebased.FileBasedOpenPgpStore; import org.bouncycastle.openpgp.PGPException; +import org.bouncycastle.openpgp.PGPPublicKeyRing; +import org.bouncycastle.openpgp.PGPSecretKeyRing; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.jupiter.api.Test; @@ -53,8 +54,8 @@ import org.jxmpp.jid.BareJid; import org.jxmpp.jid.Jid; import org.jxmpp.jid.JidTestUtil; import org.pgpainless.key.OpenPgpV4Fingerprint; -import org.pgpainless.key.collection.PGPKeyRing; import org.pgpainless.key.protection.UnprotectedKeysProtector; +import org.pgpainless.key.util.KeyRingUtils; public class PainlessOpenPgpProviderTest extends SmackTestSuite { @@ -88,17 +89,19 @@ public class PainlessOpenPgpProviderTest extends SmackTestSuite { PainlessOpenPgpProvider aliceProvider = new PainlessOpenPgpProvider(aliceStore); PainlessOpenPgpProvider bobProvider = new PainlessOpenPgpProvider(bobStore); - PGPKeyRing aliceKeys = aliceStore.generateKeyRing(alice); - PGPKeyRing bobKeys = bobStore.generateKeyRing(bob); + PGPSecretKeyRing aliceKeys = aliceStore.generateKeyRing(alice); + PGPSecretKeyRing bobKeys = bobStore.generateKeyRing(bob); + PGPPublicKeyRing alicePubKeys = KeyRingUtils.publicKeyRingFrom(aliceKeys); + PGPPublicKeyRing bobPubKeys = KeyRingUtils.publicKeyRingFrom(bobKeys); - OpenPgpV4Fingerprint aliceFingerprint = new OpenPgpV4Fingerprint(aliceKeys.getPublicKeys()); - OpenPgpV4Fingerprint bobFingerprint = new OpenPgpV4Fingerprint(bobKeys.getPublicKeys()); + OpenPgpV4Fingerprint aliceFingerprint = new OpenPgpV4Fingerprint(aliceKeys); + OpenPgpV4Fingerprint bobFingerprint = new OpenPgpV4Fingerprint(bobKeys); - aliceStore.importSecretKey(alice, aliceKeys.getSecretKeys()); - bobStore.importSecretKey(bob, bobKeys.getSecretKeys()); + aliceStore.importSecretKey(alice, aliceKeys); + bobStore.importSecretKey(bob, bobKeys); - aliceStore.setAnnouncedFingerprintsOf(alice, Collections.singletonMap(new OpenPgpV4Fingerprint(aliceKeys.getPublicKeys()), new Date())); - bobStore.setAnnouncedFingerprintsOf(bob, Collections.singletonMap(new OpenPgpV4Fingerprint(bobKeys.getPublicKeys()), new Date())); + aliceStore.setAnnouncedFingerprintsOf(alice, Collections.singletonMap(aliceFingerprint, new Date())); + bobStore.setAnnouncedFingerprintsOf(bob, Collections.singletonMap(bobFingerprint, new Date())); OpenPgpSelf aliceSelf = new OpenPgpSelf(alice, aliceStore); aliceSelf.trust(aliceFingerprint); @@ -107,11 +110,11 @@ public class PainlessOpenPgpProviderTest extends SmackTestSuite { // Exchange keys - aliceStore.importPublicKey(bob, bobKeys.getPublicKeys()); - bobStore.importPublicKey(alice, aliceKeys.getPublicKeys()); + aliceStore.importPublicKey(bob, bobPubKeys); + bobStore.importPublicKey(alice, alicePubKeys); - aliceStore.setAnnouncedFingerprintsOf(bob, Collections.singletonMap(new OpenPgpV4Fingerprint(bobKeys.getPublicKeys()), new Date())); - bobStore.setAnnouncedFingerprintsOf(alice, Collections.singletonMap(new OpenPgpV4Fingerprint(aliceKeys.getPublicKeys()), new Date())); + aliceStore.setAnnouncedFingerprintsOf(bob, Collections.singletonMap(bobFingerprint, new Date())); + bobStore.setAnnouncedFingerprintsOf(alice, Collections.singletonMap(aliceFingerprint, new Date())); OpenPgpContact aliceForBob = new OpenPgpContact(alice, bobStore); aliceForBob.trust(aliceFingerprint); @@ -121,7 +124,7 @@ public class PainlessOpenPgpProviderTest extends SmackTestSuite { // Prepare message Message.Body body = new Message.Body(null, "Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi consequat. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."); - List payload = Collections.singletonList(body); + List payload = Collections.singletonList(body); OpenPgpElementAndMetadata encrypted; @@ -141,7 +144,7 @@ public class PainlessOpenPgpProviderTest extends SmackTestSuite { OpenPgpV4Fingerprint decryptionFingerprint = decrypted.getMetadata().getDecryptionFingerprint(); assertTrue(bobSelf.getSecretKeys().contains(decryptionFingerprint.getKeyId())); - assertTrue(decrypted.getMetadata().getVerifiedSignatureKeyFingerprints().contains(aliceFingerprint)); + assertTrue(decrypted.getMetadata().containsVerifiedSignatureFrom(alicePubKeys)); assertEquals(OpenPgpMessage.State.signcrypt, decrypted.getState()); SigncryptElement decryptedSignCrypt = (SigncryptElement) decrypted.getOpenPgpContentElement(); @@ -172,7 +175,7 @@ public class PainlessOpenPgpProviderTest extends SmackTestSuite { test sign */ - SignElement signElement = new SignElement(Collections.singleton(bob), new Date(), payload); + SignElement signElement = new SignElement(Collections.singleton(bob), new Date(), payload); // Sign encrypted = aliceProvider.sign(signElement, aliceSelf); @@ -180,7 +183,7 @@ public class PainlessOpenPgpProviderTest extends SmackTestSuite { decrypted = bobProvider.decryptAndOrVerify(bobConnection, encrypted.getElement(), bobSelf, aliceForBob); assertNull(decrypted.getMetadata().getDecryptionFingerprint()); - assertTrue(decrypted.getMetadata().getVerifiedSignatureKeyFingerprints().contains(aliceFingerprint)); + assertTrue(decrypted.getMetadata().containsVerifiedSignatureFrom(alicePubKeys)); assertEquals(OpenPgpMessage.State.sign, decrypted.getState()); SignElement decryptedSign = (SignElement) decrypted.getOpenPgpContentElement(); diff --git a/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/PubSubDelegateTest.java b/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/PubSubDelegateTest.java index 3a42a8d28..0bb120f8f 100644 --- a/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/PubSubDelegateTest.java +++ b/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/PubSubDelegateTest.java @@ -16,20 +16,18 @@ */ package org.jivesoftware.smackx.ox; -import static junit.framework.TestCase.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import org.jivesoftware.smack.test.util.SmackTestSuite; - import org.jivesoftware.smackx.ox.util.OpenPgpPubSubUtil; -import org.bouncycastle.openpgp.PGPException; import org.junit.jupiter.api.Test; import org.pgpainless.key.OpenPgpV4Fingerprint; public class PubSubDelegateTest extends SmackTestSuite { @Test - public void pubkeyNodeNameTest() throws PGPException { + public void pubkeyNodeNameTest() { OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint("486f7065207520646f6e2068617665204f43640a"); assertEquals("urn:xmpp:openpgp:0:public-keys:486F7065207520646F6E2068617665204F43640A", OpenPgpPubSubUtil.PEP_NODE_PUBLIC_KEY(fingerprint)); diff --git a/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/PubkeyElementTest.java b/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/PubkeyElementTest.java index 2e754650a..ac7d89c8c 100644 --- a/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/PubkeyElementTest.java +++ b/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/PubkeyElementTest.java @@ -16,8 +16,8 @@ */ package org.jivesoftware.smackx.ox; -import static junit.framework.TestCase.assertEquals; import static org.jivesoftware.smack.test.util.XmlAssertUtil.assertXmlSimilar; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.io.IOException; import java.text.ParseException; diff --git a/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/PublicKeysListElementTest.java b/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/PublicKeysListElementTest.java index f90015b9f..319c60bcf 100644 --- a/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/PublicKeysListElementTest.java +++ b/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/PublicKeysListElementTest.java @@ -16,19 +16,17 @@ */ package org.jivesoftware.smackx.ox; -import static junit.framework.TestCase.assertEquals; import static org.jivesoftware.smack.test.util.XmlAssertUtil.assertXmlSimilar; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.Date; import org.jivesoftware.smack.test.util.SmackTestSuite; import org.jivesoftware.smack.test.util.TestUtils; import org.jivesoftware.smack.xml.XmlPullParser; - import org.jivesoftware.smackx.ox.element.PublicKeysListElement; import org.jivesoftware.smackx.ox.provider.PublicKeysListElementProvider; -import org.bouncycastle.openpgp.PGPException; import org.junit.jupiter.api.Test; import org.jxmpp.util.XmppDateTime; import org.pgpainless.key.OpenPgpV4Fingerprint; @@ -72,7 +70,7 @@ public class PublicKeysListElementTest extends SmackTestSuite { } @Test - public void listBuilderRefusesDuplicatesTest() throws PGPException { + public void listBuilderRefusesDuplicatesTest() { PublicKeysListElement.Builder builder = PublicKeysListElement.builder(); String fp40 = "49545320414c4c2041424f555420444120484558"; Date oneDate = new Date(12337883234L); diff --git a/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/SecretKeyBackupHelperTest.java b/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/SecretKeyBackupHelperTest.java index ceaf50638..74f9471f8 100644 --- a/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/SecretKeyBackupHelperTest.java +++ b/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/SecretKeyBackupHelperTest.java @@ -16,15 +16,13 @@ */ package org.jivesoftware.smackx.ox; -import static junit.framework.TestCase.assertEquals; -import static junit.framework.TestCase.assertTrue; -import static org.junit.Assert.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.File; import java.io.IOException; import java.security.InvalidAlgorithmParameterException; import java.security.NoSuchAlgorithmException; -import java.security.NoSuchProviderException; import java.util.Collections; import org.jivesoftware.smack.test.util.SmackTestSuite; @@ -40,12 +38,12 @@ import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPSecretKeyRing; import org.junit.AfterClass; import org.junit.BeforeClass; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.jxmpp.jid.BareJid; import org.jxmpp.jid.impl.JidCreate; import org.pgpainless.PGPainless; import org.pgpainless.key.OpenPgpV4Fingerprint; -import org.pgpainless.key.collection.PGPKeyRing; public class SecretKeyBackupHelperTest extends SmackTestSuite { @@ -72,7 +70,7 @@ public class SecretKeyBackupHelperTest extends SmackTestSuite { @Test public void createAndDecryptSecretKeyElementTest() - throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, + throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, IOException, MissingUserIdOnKeyException, MissingOpenPgpKeyException, InvalidBackupCodeException { // Prepare store and provider and so on... @@ -80,18 +78,18 @@ public class SecretKeyBackupHelperTest extends SmackTestSuite { PainlessOpenPgpProvider provider = new PainlessOpenPgpProvider(store); // Generate and import key - PGPKeyRing keyRing = PGPainless.generateKeyRing().simpleEcKeyRing("xmpp:alice@wonderland.lit"); + PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing().simpleEcKeyRing("xmpp:alice@wonderland.lit"); BareJid jid = JidCreate.bareFrom("alice@wonderland.lit"); - provider.getStore().importSecretKey(jid, keyRing.getSecretKeys()); + provider.getStore().importSecretKey(jid, secretKeys); // Create encrypted backup OpenPgpSecretKeyBackupPassphrase backupCode = SecretKeyBackupHelper.generateBackupPassword(); SecretkeyElement element = SecretKeyBackupHelper.createSecretkeyElement(provider, jid, - Collections.singleton(new OpenPgpV4Fingerprint(keyRing.getSecretKeys())), backupCode); + Collections.singleton(new OpenPgpV4Fingerprint(secretKeys)), backupCode); // Decrypt backup and compare PGPSecretKeyRing secretKeyRing = SecretKeyBackupHelper.restoreSecretKeyBackup(element, backupCode); - assertArrayEquals(keyRing.getSecretKeys().getEncoded(), secretKeyRing.getEncoded()); + Assertions.assertArrayEquals(secretKeys.getEncoded(), secretKeyRing.getEncoded()); } @AfterClass diff --git a/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/SecretkeyElementTest.java b/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/SecretkeyElementTest.java index b9745bb20..9e8922cf2 100644 --- a/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/SecretkeyElementTest.java +++ b/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/SecretkeyElementTest.java @@ -16,16 +16,14 @@ */ package org.jivesoftware.smackx.ox; -import static junit.framework.TestCase.assertTrue; import static org.jivesoftware.smack.test.util.XmlAssertUtil.assertXmlSimilar; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import java.nio.charset.Charset; -import java.util.Arrays; import org.jivesoftware.smack.test.util.SmackTestSuite; import org.jivesoftware.smack.test.util.TestUtils; import org.jivesoftware.smack.xml.XmlPullParser; - import org.jivesoftware.smackx.ox.element.SecretkeyElement; import org.jivesoftware.smackx.ox.provider.SecretkeyElementProvider; @@ -48,6 +46,6 @@ public class SecretkeyElementTest extends SmackTestSuite { XmlPullParser parser = TestUtils.getParser(expected); SecretkeyElement parsed = SecretkeyElementProvider.TEST_INSTANCE.parse(parser); - assertTrue(Arrays.equals(element.getB64Data(), parsed.getB64Data())); + assertArrayEquals(element.getB64Data(), parsed.getB64Data()); } } diff --git a/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/TestKeys.java b/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/TestKeys.java deleted file mode 100644 index d6c28bf23..000000000 --- a/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox/TestKeys.java +++ /dev/null @@ -1,162 +0,0 @@ -/** - * - * Copyright 2018 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.jivesoftware.smackx.ox; - -import org.jxmpp.jid.BareJid; -import org.jxmpp.jid.impl.JidCreate; -import org.jxmpp.stringprep.XmppStringprepException; - -public class TestKeys { - - public TestKeys() { - - } - - public static final BareJid JULIET_JID; - public static final BareJid ROMEO_JID; - - static { - try { - JULIET_JID = JidCreate.bareFrom("juliet@capulet.lit"); - ROMEO_JID = JidCreate.bareFrom("romeo@montague.lit"); - } catch (XmppStringprepException e) { - throw new AssertionError(e); - } - } - - public static final String JULIET_UID = "xmpp:juliet@capulet.lit"; - - /** - * Public key of xmpp:juliet@capulet.lit. - */ - public static final String JULIET_PUB = - "-----BEGIN PGP PUBLIC KEY BLOCK-----\n" + - "\n" + - "mQENBFrxov4BCAChZwPrBxxIlwzpieR5T2pnaOZLWH0WqSON6rVjvfbJHWdDi3Th\n" + - "remHW4gg4IBSTXkVFDIeQNVcOvGNgMg3Oe/x0I6FK12jrw9prycmjFxQ7A0ix7ZG\n" + - "UkTF5jITgzJbkH100gYfXtZsfTyvgISSAT//6vvvQPZ3zCr09XvAG0CyQ1BhULsv\n" + - "mVRe4Oh5b0VK4kLdv+GiA/T+49UKZj6lne9Vdti16ZIj7teVCbicfdhpTzsjur42\n" + - "r8ptouKAuyFPw9KnGNwVlIiv5jt/Kit/LoOBenh74sitsCXq8IQ9kKp/eNt8TF4u\n" + - "D4IGpxnJfB8XCiixYHoFEajmQBVJXNYtvoPvABEBAAG0F3htcHA6anVsaWV0QGNh\n" + - "cHVsZXQubGl0iQFOBBMBCAA4FiEEHQGMdy34xe+GodzJtLUJy1k24D4FAlrxov4C\n" + - "Gy8FCwkIBwIGFQoJCAsCBBYCAwECHgECF4AACgkQtLUJy1k24D6H7AgAoTjx4ezc\n" + - "A83NeOY3tMHVQTM7hKuy0wMcSzQgVgJmhLYRZS8r+FocPZua/eke49GPhe2yozvl\n" + - "ByWHtotklQeJiwOKxuPKMzneVA1ZK3/9LdGvtZlHMcAkEKDhit8HIaEcsFd4Z1re\n" + - "EhF2lyvY/E+rrx9YxV0QjisSWV2dSptv6FeGSztr9e5E+Head6hEQhsugiTVRF+1\n" + - "6mG90te0WGQ9YNiJ2FJovx5kBLTTuhwUz8Oacqihd2+RDDI5p3wJoogVL31aNb4n\n" + - "c7dGo8ieJPHGlkBsOfmreSxijTodZz9MXsgcx7b//u0uQryViJoZHWbtnXOFjjNc\n" + - "GWBtS084NKWl9w==\n" + - "=ecwX\n" + - "-----END PGP PUBLIC KEY BLOCK-----"; - - /** - * Private key of xmpp:juliet@capulet.lit. - */ - public static final String JULIET_PRIV = - "-----BEGIN PGP PRIVATE KEY BLOCK-----\n" + - "\n" + - "lQOYBFrxov4BCAChZwPrBxxIlwzpieR5T2pnaOZLWH0WqSON6rVjvfbJHWdDi3Th\n" + - "remHW4gg4IBSTXkVFDIeQNVcOvGNgMg3Oe/x0I6FK12jrw9prycmjFxQ7A0ix7ZG\n" + - "UkTF5jITgzJbkH100gYfXtZsfTyvgISSAT//6vvvQPZ3zCr09XvAG0CyQ1BhULsv\n" + - "mVRe4Oh5b0VK4kLdv+GiA/T+49UKZj6lne9Vdti16ZIj7teVCbicfdhpTzsjur42\n" + - "r8ptouKAuyFPw9KnGNwVlIiv5jt/Kit/LoOBenh74sitsCXq8IQ9kKp/eNt8TF4u\n" + - "D4IGpxnJfB8XCiixYHoFEajmQBVJXNYtvoPvABEBAAEAB/4jMbXagW3q7DkOEZnm\n" + - "0+jVTLvu0QhRsScGEphj+++8sfMq+NVPQp9p+w0Hcjy49ZjB/mnhS+zaVCYI33yJ\n" + - "AlKubXYuVqLwBsO7HUzRrIiSwq4ol9jIo7bIWmYv+As6iRq6JvPb0k+6T2K0uDbw\n" + - "KWKduM0fwhAcVkJFsOO/o5GrbQaJc3oioFk8uFWTnO+FPBRTJ9oTlVG2M/tEatZK\n" + - "gl7I8Ukl0YYruCNUFKZ0tvO8HqulxBgUbGPBer1uOlfUD4RXdc8/PUiFKNo48XSu\n" + - "ZUEAZKGbFBjuX5Z8ha7+sUMEYEt70qlbkiLQxgHKAmpyridAk3q/SB3y2VB8Ik7I\n" + - "gpExBADInzLROYuUcXqmty+znVwm6nRIB75JBAy778zgIxx1v0O3QlVnR+YI8gJM\n" + - "mQ/9pD6LyP9hktWDmJxG8tX+kSuIp3wNJc5EMeXtCCmkUW0CP1gUhAbNW3MezKa5\n" + - "II5IhE9RgIsYqSU8ZgeIh72ON8XTp8i/wGipCXvJPggSAMXukQQAzfRmtLW+JHEK\n" + - "B8ETIYh8IUjXJ6TVlmuBwZ0eXjCpqy9arJi6tacesDJwnL3sqOMQWUmqGsCGSKA5\n" + - "cLITkVsxX/htIq8GFyludjg8t4Nr+fOGfChEq8QE0PHE2CgskQMHpfHvfIdnwKve\n" + - "Fg2Q8twoMw849O6PF3k/848Z65lDin8EAMDbuPWL7KU2sWeqvDEuoulS5K1gsq8X\n" + - "p3Od3+f0OG8YViMjKcVlSKHVvdlK4dlsccJrJJx6VzotV47LsmvVbzDwUE//MYq7\n" + - "QwwQetZbpdQZDysSGVqHMTuAg/1pr2u5rqh4cFqCYatgZwinEI2TQMXEqnSc+mj8\n" + - "xp/LNq5BZZQuO4y0F3htcHA6anVsaWV0QGNhcHVsZXQubGl0iQFOBBMBCAA4FiEE\n" + - "HQGMdy34xe+GodzJtLUJy1k24D4FAlrxov4CGy8FCwkIBwIGFQoJCAsCBBYCAwEC\n" + - "HgECF4AACgkQtLUJy1k24D6H7AgAoTjx4ezcA83NeOY3tMHVQTM7hKuy0wMcSzQg\n" + - "VgJmhLYRZS8r+FocPZua/eke49GPhe2yozvlByWHtotklQeJiwOKxuPKMzneVA1Z\n" + - "K3/9LdGvtZlHMcAkEKDhit8HIaEcsFd4Z1reEhF2lyvY/E+rrx9YxV0QjisSWV2d\n" + - "Sptv6FeGSztr9e5E+Head6hEQhsugiTVRF+16mG90te0WGQ9YNiJ2FJovx5kBLTT\n" + - "uhwUz8Oacqihd2+RDDI5p3wJoogVL31aNb4nc7dGo8ieJPHGlkBsOfmreSxijTod\n" + - "Zz9MXsgcx7b//u0uQryViJoZHWbtnXOFjjNcGWBtS084NKWl9w==\n" + - "=yPPE\n" + - "-----END PGP PRIVATE KEY BLOCK-----"; - - public static final String ROMEO_UID = "xmpp:romeo@montague.lit"; - - /** - * Public key of xmpp:romeo@montague.lit. - */ - public static final String ROMEO_PUB = - "-----BEGIN PGP PUBLIC KEY BLOCK-----\n" + - "\n" + - "mQENBFrxopkBCADiYg/+mEObXgxuMW6/LFKpEyaJK9pBMgutuxnYZ9PXWZmOhDIT\n" + - "Ugm9X9YJ3Qh94KaHge9F4uCeFASmM1vvUTRFTEb1W5RR9ZE/sy/cdAttnZ5JloPi\n" + - "CT3HDMIJAxIXhRJkeUR9GUb51ql27bMXl6lFh865VdNSXN/B8FzRQHENxv1Bq/6Z\n" + - "iQOViIETeRRgO+u6u2iZkYlHgYMaoMK7+YiNlHXanU9Atcuaz0ZCJS/XFNH89iqB\n" + - "Kvnv7KCQh4FhrNMLJRzNPXV8MY05nn0zF72qeEsniB16Xde18lMro8fQehg2mLwc\n" + - "XGtCwCKI6QbZVxYQt77r3ZACiwl66soFWijVABEBAAG0F3htcHA6cm9tZW9AbW9u\n" + - "dGFndWUubGl0iQFOBBMBCAA4FiEENdKZ0IovfYAjCwldBKMhguBeIfcFAlrxopkC\n" + - "Gy8FCwkIBwIGFQoJCAsCBBYCAwECHgECF4AACgkQBKMhguBeIfcj8AgAu1wubUwr\n" + - "2aQmDN3OqRM4M4yRL3oyYMkCKIjqD6KEeFsIXSSkXOuREJKEo8Mb1+ewV0SYmHCC\n" + - "K3bKKq3m71AQ7evDhKGshacPYesiDvMdHWQdQnjfaoHhyn9qIKl7H0Xv1yf/wyuG\n" + - "ANy1jYgtCEuYw7D+EsqNDdn8Xh+k/9s4aMI/6mfC0yGZgG8EyLTfbZkGPoS4aZfV\n" + - "AGFbuqryg48dXtnuzAPKcdgMTTMSnmR729YlfkjCffcFaldyXoe1VMbudUO7nkO9\n" + - "g65i5EXenkbc2h0TRDQ4lDFQyModqFTwYFYxAf/RA6tuhIQEoCnpCytFMvrRKMb3\n" + - "Bx5vYRDVmE3jeg==\n" + - "=2jSg\n" + - "-----END PGP PUBLIC KEY BLOCK-----"; - - /** - * Private key of xmpp:romeo@montague.lit. - */ - public static final String ROMEO_PRIV = - "-----BEGIN PGP PRIVATE KEY BLOCK-----\n" + - "\n" + - "lQOYBFrxopkBCADiYg/+mEObXgxuMW6/LFKpEyaJK9pBMgutuxnYZ9PXWZmOhDIT\n" + - "Ugm9X9YJ3Qh94KaHge9F4uCeFASmM1vvUTRFTEb1W5RR9ZE/sy/cdAttnZ5JloPi\n" + - "CT3HDMIJAxIXhRJkeUR9GUb51ql27bMXl6lFh865VdNSXN/B8FzRQHENxv1Bq/6Z\n" + - "iQOViIETeRRgO+u6u2iZkYlHgYMaoMK7+YiNlHXanU9Atcuaz0ZCJS/XFNH89iqB\n" + - "Kvnv7KCQh4FhrNMLJRzNPXV8MY05nn0zF72qeEsniB16Xde18lMro8fQehg2mLwc\n" + - "XGtCwCKI6QbZVxYQt77r3ZACiwl66soFWijVABEBAAEAB/4mu5p69/hRQ+UikWie\n" + - "Yun9rZ4hSBR+pR5kaifA4/rV1Km2PZ4HujiaYyRO6beDOgWkF7IlpezCfzBQc2ce\n" + - "ailkVemqHzIgV8CzQmhE8sHlzlr/wjXsXaJpRSCJxDG7PnRoJmt2b/W512WFSKQk\n" + - "vDklAVh4U1vlsqhCGWr4DmuJbJkRyDhcX01tplRwim283F7bGqRcMBmKMZHiMgVc\n" + - "0u84EYKKVizJ3YAaaVqZyHb4qdeKK2ak3fPNuGT/oGd2sxnkL+BZGjJpu3RGpTA1\n" + - "tbOvOQnJGHQtABFxE8n6H9dHPJGtgyz2+udjUhL/P/E3PDoXazZkXRq2oHZKgg0f\n" + - "AwOBBADsWncHgvz15rXPF7O6AivbGTJ5ctkgVy4U3Fu2sk9rf0fx0sryBSqtTBw1\n" + - "Uvn/p9RwTsKw6fng6Nf78xpZFlUDB00YCcuWkGodxvjTAyB0dtBmkhopeKi0dmHh\n" + - "ndnR6Pv0CsXu8nG7lUi+q6s3oc4h2OfDBhrqsyYY5M2gGit3dQQA9TNuinJD9XXv\n" + - "QRyauMnSJ5xRcfOu8QCxZlllCvffZjSGCPoVjUpJEe9qsVbXVj2GYCxjLCSXV0V+\n" + - "vlJfdPrl1BhZ3fmEpg0u7SyGDDOe8fe1ehk5sAeL8O0eFWlPSEaEccsjlpJ2FO0n\n" + - "P04SZdOeM6wmhDTEDzpFnjbPndQTH+ED/R1zNzr55DvxQodmrW/BvTmhGQ22rHtk\n" + - "IUfbeMaVfUvNLJA/JksrUIx3Gga9QCDZgfm1RsRhLUlHiqTQe23sPWgKOsbf5O1j\n" + - "XJZaCNZ7LloVQbkG7xFcnb/n1+JjBr4FxXjAA6cY/iRGlznjIIaasyklKm1/4LuQ\n" + - "hnH3QqTvCN3dOFS0F3htcHA6cm9tZW9AbW9udGFndWUubGl0iQFOBBMBCAA4FiEE\n" + - "NdKZ0IovfYAjCwldBKMhguBeIfcFAlrxopkCGy8FCwkIBwIGFQoJCAsCBBYCAwEC\n" + - "HgECF4AACgkQBKMhguBeIfcj8AgAu1wubUwr2aQmDN3OqRM4M4yRL3oyYMkCKIjq\n" + - "D6KEeFsIXSSkXOuREJKEo8Mb1+ewV0SYmHCCK3bKKq3m71AQ7evDhKGshacPYesi\n" + - "DvMdHWQdQnjfaoHhyn9qIKl7H0Xv1yf/wyuGANy1jYgtCEuYw7D+EsqNDdn8Xh+k\n" + - "/9s4aMI/6mfC0yGZgG8EyLTfbZkGPoS4aZfVAGFbuqryg48dXtnuzAPKcdgMTTMS\n" + - "nmR729YlfkjCffcFaldyXoe1VMbudUO7nkO9g65i5EXenkbc2h0TRDQ4lDFQyMod\n" + - "qFTwYFYxAf/RA6tuhIQEoCnpCytFMvrRKMb3Bx5vYRDVmE3jeg==\n" + - "=LZ1b\n" + - "-----END PGP PRIVATE KEY BLOCK-----"; -} diff --git a/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox_im/OXInstantMessagingManagerTest.java b/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox_im/OXInstantMessagingManagerTest.java index 9d74d50a0..8d9d74ba0 100644 --- a/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox_im/OXInstantMessagingManagerTest.java +++ b/smack-openpgp/src/test/java/org/jivesoftware/smackx/ox_im/OXInstantMessagingManagerTest.java @@ -16,10 +16,10 @@ */ package org.jivesoftware.smackx.ox_im; -import static junit.framework.TestCase.assertEquals; -import static junit.framework.TestCase.assertFalse; -import static junit.framework.TestCase.assertNotNull; -import static junit.framework.TestCase.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.File; import java.io.IOException; @@ -32,14 +32,12 @@ import java.util.Date; import org.jivesoftware.smack.DummyConnection; import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.XMPPException; -import org.jivesoftware.smack.packet.ExtensionElement; import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.MessageBuilder; import org.jivesoftware.smack.packet.StanzaBuilder; import org.jivesoftware.smack.test.util.SmackTestSuite; import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smack.xml.XmlPullParserException; - import org.jivesoftware.smackx.eme.element.ExplicitMessageEncryptionElement; import org.jivesoftware.smackx.ox.OpenPgpContact; import org.jivesoftware.smackx.ox.OpenPgpManager; @@ -137,7 +135,7 @@ public class OXInstantMessagingManagerTest extends SmackTestSuite { assertFalse(ExplicitMessageEncryptionElement.hasProtocol(messageBuilder.build(), ExplicitMessageEncryptionElement.ExplicitMessageEncryptionProtocol.openpgpV0)); aliceOxim.addOxMessage(messageBuilder, bobForAlice, - Collections.singletonList(new Message.Body(null, "Hello World!"))); + Collections.singletonList(new Message.Body(null, "Hello World!"))); Message message = messageBuilder.build(); assertTrue(ExplicitMessageEncryptionElement.hasProtocol(message, ExplicitMessageEncryptionElement.ExplicitMessageEncryptionProtocol.openpgpV0));