diff --git a/smack-openpgp-bouncycastle/src/main/java/org/jivesoftware/smackx/ox/bouncycastle/BCOpenPgpProvider.java b/smack-openpgp-bouncycastle/src/main/java/org/jivesoftware/smackx/ox/bouncycastle/BCOpenPgpProvider.java index 5fde46300..f81b382e9 100644 --- a/smack-openpgp-bouncycastle/src/main/java/org/jivesoftware/smackx/ox/bouncycastle/BCOpenPgpProvider.java +++ b/smack-openpgp-bouncycastle/src/main/java/org/jivesoftware/smackx/ox/bouncycastle/BCOpenPgpProvider.java @@ -143,10 +143,7 @@ public class BCOpenPgpProvider implements OpenPgpProvider { Streams.pipeAll(decrypted, decryptedOut); return new OpenPgpMessage(OpenPgpMessage.State.signcrypt, new String(decryptedOut.toByteArray(), Charset.forName("UTF-8"))); - } catch (IOException e) { - // TODO: Hm... - return null; - } catch (NoSuchProviderException e) { + } catch (IOException | NoSuchProviderException e) { throw new AssertionError(e); } } diff --git a/smack-openpgp-bouncycastle/src/main/java/org/jivesoftware/smackx/ox/bouncycastle/FileBasedBcOpenPgpStore.java b/smack-openpgp-bouncycastle/src/main/java/org/jivesoftware/smackx/ox/bouncycastle/FileBasedBcOpenPgpStore.java index 372d94e5b..262b91b12 100644 --- a/smack-openpgp-bouncycastle/src/main/java/org/jivesoftware/smackx/ox/bouncycastle/FileBasedBcOpenPgpStore.java +++ b/smack-openpgp-bouncycastle/src/main/java/org/jivesoftware/smackx/ox/bouncycastle/FileBasedBcOpenPgpStore.java @@ -346,6 +346,7 @@ public class FileBasedBcOpenPgpStore implements BCOpenPgpStore { } primaryKeyFingerprint = selectedKey; writePrivateKeysToFile(keyringConfig, secretKeyringPath()); + writePublicKeysToFile(keyring, publicKeyringPath()); } } catch (PGPException | IOException e) { throw new SmackOpenPgpException(e); @@ -359,6 +360,8 @@ public class FileBasedBcOpenPgpStore implements BCOpenPgpStore { PGPSecretKeyRing ourKey = BCOpenPgpProvider.generateKey(user).generateSecretKeyRing(); keyringConfig.addSecretKey(ourKey.getSecretKey().getEncoded()); keyringConfig.addPublicKey(ourKey.getPublicKey().getEncoded()); + writePrivateKeysToFile(keyringConfig, secretKeyringPath()); + writePublicKeysToFile(keyringConfig, publicKeyringPath()); primaryKeyFingerprint = BCOpenPgpProvider.getFingerprint(ourKey.getPublicKey()); return primaryKeyFingerprint; } catch (PGPException | IOException e) { @@ -456,7 +459,8 @@ public class FileBasedBcOpenPgpStore implements BCOpenPgpStore { BufferedOutputStream bufferedStream = new BufferedOutputStream(outputStream); bufferedStream.write(bytes); - outputStream.close(); + bufferedStream.flush(); + bufferedStream.close(); } catch (IOException e) { if (outputStream != null) { outputStream.close(); diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OXInstantMessagingManager.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OXInstantMessagingManager.java index 866963fd6..718841a4b 100644 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OXInstantMessagingManager.java +++ b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OXInstantMessagingManager.java @@ -79,6 +79,11 @@ public final class OXInstantMessagingManager extends Manager { return manager; } + public void announceSupportForOxInstantMessaging() { + ServiceDiscoveryManager.getInstanceFor(connection()) + .addFeature(NAMESPACE_0); + } + /** * Determine, whether a contact announces support for XEP-0374: OpenPGP for XMPP: Instant Messaging. * diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpEncryptedChat.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpEncryptedChat.java index 130e8ea61..956b02fef 100644 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpEncryptedChat.java +++ b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpEncryptedChat.java @@ -22,6 +22,7 @@ import java.util.HashSet; import java.util.List; import java.util.Set; +import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.chat2.Chat; import org.jivesoftware.smack.packet.ExtensionElement; import org.jivesoftware.smack.packet.Message; @@ -54,7 +55,7 @@ public class OpenPgpEncryptedChat { } public void send(Message message, List payload) - throws MissingOpenPgpKeyPairException { + throws MissingOpenPgpKeyPairException, SmackException.NotConnectedException, InterruptedException { Set encryptionFingerprints = new HashSet<>(contactsFingerprints.getActiveKeys()); encryptionFingerprints.addAll(ourFingerprints.getActiveKeys()); @@ -81,10 +82,12 @@ public class OpenPgpEncryptedChat { ExplicitMessageEncryptionElement.ExplicitMessageEncryptionProtocol.openpgpV0)); StoreHint.set(message); message.setBody("This message is encrypted using XEP-0374: OpenPGP for XMPP: Instant Messaging."); + + chat.send(message); } public void send(Message message, CharSequence body) - throws MissingOpenPgpKeyPairException { + throws MissingOpenPgpKeyPairException, SmackException.NotConnectedException, InterruptedException { List payload = new ArrayList<>(); payload.add(new Message.Body(null, body.toString())); send(message, payload); diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/callback/AskForBackupCodeCallback.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/callback/AskForBackupCodeCallback.java index d1a536b69..e37200b60 100644 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/callback/AskForBackupCodeCallback.java +++ b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/callback/AskForBackupCodeCallback.java @@ -17,5 +17,15 @@ package org.jivesoftware.smackx.ox.callback; public interface AskForBackupCodeCallback { + + /** + * This callback is used to ask the user to provide a backup code. + * The backup code must follow the format described in XEP-0373 §5.3 + * + * @see + * XEP-0373 §5.3 about the format of the backup code + * + * @return backup code provided by the user. + */ String askForBackupCode(); } diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/callback/DisplayBackupCodeCallback.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/callback/DisplayBackupCodeCallback.java index 5eb8889dc..ed1ef7595 100644 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/callback/DisplayBackupCodeCallback.java +++ b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/callback/DisplayBackupCodeCallback.java @@ -17,5 +17,16 @@ package org.jivesoftware.smackx.ox.callback; public interface DisplayBackupCodeCallback { + + /** + * This method is used to provide a client access to the generated backup code. + * The client can then go ahead and display the code to the user. + * The backup code follows the format described in XEP-0373 §5.3 + * + * @see + * XEP-0373 §5.3 about the format of the backup code + * + * @param backupCode + */ void displayBackupCode(String backupCode); } diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/element/OpenPgpContentElement.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/element/OpenPgpContentElement.java index e48e29279..a0c28a848 100644 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/element/OpenPgpContentElement.java +++ b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/element/OpenPgpContentElement.java @@ -172,6 +172,11 @@ public abstract class OpenPgpContentElement implements ExtensionElement { xml.closeElement(ELEM_PAYLOAD); } + /** + * Return a {@link ByteArrayInputStream} that reads the bytes of the XML representation of this element. + * + * @return InputStream over xml. + */ public InputStream toInputStream() { byte[] encoded = toXML(null).toString().getBytes(Charset.forName("UTF-8")); return new ByteArrayInputStream(encoded); diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/listener/OpenPgpEncryptedMessageListener.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/listener/OpenPgpEncryptedMessageListener.java index 05384a798..654d3c0df 100644 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/listener/OpenPgpEncryptedMessageListener.java +++ b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/listener/OpenPgpEncryptedMessageListener.java @@ -18,12 +18,24 @@ package org.jivesoftware.smackx.ox.listener; import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smackx.ox.OpenPgpEncryptedChat; +import org.jivesoftware.smackx.ox.element.OpenPgpElement; import org.jivesoftware.smackx.ox.element.SigncryptElement; import org.jxmpp.jid.EntityBareJid; public interface OpenPgpEncryptedMessageListener { + /** + * This method gets invoked, whenever an OX-IM encrypted message gets received. + * + * @see + * XEP-0374: OpenPGP for XMPP: Instant Messaging (OX-IM) + * + * @param from sender of the message. + * @param originalMessage the received message that is carrying the encrypted {@link OpenPgpElement}. + * @param decryptedPayload decrypted {@link SigncryptElement} which is carrying the payload. + * @param chat {@link OpenPgpEncryptedChat} which is the context of the message. + */ void newIncomingEncryptedMessage(EntityBareJid from, Message originalMessage, SigncryptElement decryptedPayload,