Fix message sending, filebased store storage

This commit is contained in:
Paul Schaub 2018-06-01 15:36:50 +02:00
parent ffc8cbbf79
commit 48862962db
8 changed files with 54 additions and 7 deletions

View File

@ -143,10 +143,7 @@ public class BCOpenPgpProvider implements OpenPgpProvider {
Streams.pipeAll(decrypted, decryptedOut); Streams.pipeAll(decrypted, decryptedOut);
return new OpenPgpMessage(OpenPgpMessage.State.signcrypt, new String(decryptedOut.toByteArray(), Charset.forName("UTF-8"))); return new OpenPgpMessage(OpenPgpMessage.State.signcrypt, new String(decryptedOut.toByteArray(), Charset.forName("UTF-8")));
} catch (IOException e) { } catch (IOException | NoSuchProviderException e) {
// TODO: Hm...
return null;
} catch (NoSuchProviderException e) {
throw new AssertionError(e); throw new AssertionError(e);
} }
} }

View File

@ -346,6 +346,7 @@ public class FileBasedBcOpenPgpStore implements BCOpenPgpStore {
} }
primaryKeyFingerprint = selectedKey; primaryKeyFingerprint = selectedKey;
writePrivateKeysToFile(keyringConfig, secretKeyringPath()); writePrivateKeysToFile(keyringConfig, secretKeyringPath());
writePublicKeysToFile(keyring, publicKeyringPath());
} }
} catch (PGPException | IOException e) { } catch (PGPException | IOException e) {
throw new SmackOpenPgpException(e); throw new SmackOpenPgpException(e);
@ -359,6 +360,8 @@ public class FileBasedBcOpenPgpStore implements BCOpenPgpStore {
PGPSecretKeyRing ourKey = BCOpenPgpProvider.generateKey(user).generateSecretKeyRing(); PGPSecretKeyRing ourKey = BCOpenPgpProvider.generateKey(user).generateSecretKeyRing();
keyringConfig.addSecretKey(ourKey.getSecretKey().getEncoded()); keyringConfig.addSecretKey(ourKey.getSecretKey().getEncoded());
keyringConfig.addPublicKey(ourKey.getPublicKey().getEncoded()); keyringConfig.addPublicKey(ourKey.getPublicKey().getEncoded());
writePrivateKeysToFile(keyringConfig, secretKeyringPath());
writePublicKeysToFile(keyringConfig, publicKeyringPath());
primaryKeyFingerprint = BCOpenPgpProvider.getFingerprint(ourKey.getPublicKey()); primaryKeyFingerprint = BCOpenPgpProvider.getFingerprint(ourKey.getPublicKey());
return primaryKeyFingerprint; return primaryKeyFingerprint;
} catch (PGPException | IOException e) { } catch (PGPException | IOException e) {
@ -456,7 +459,8 @@ public class FileBasedBcOpenPgpStore implements BCOpenPgpStore {
BufferedOutputStream bufferedStream = new BufferedOutputStream(outputStream); BufferedOutputStream bufferedStream = new BufferedOutputStream(outputStream);
bufferedStream.write(bytes); bufferedStream.write(bytes);
outputStream.close(); bufferedStream.flush();
bufferedStream.close();
} catch (IOException e) { } catch (IOException e) {
if (outputStream != null) { if (outputStream != null) {
outputStream.close(); outputStream.close();

View File

@ -79,6 +79,11 @@ public final class OXInstantMessagingManager extends Manager {
return 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. * Determine, whether a contact announces support for XEP-0374: OpenPGP for XMPP: Instant Messaging.
* *

View File

@ -22,6 +22,7 @@ import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.chat2.Chat; import org.jivesoftware.smack.chat2.Chat;
import org.jivesoftware.smack.packet.ExtensionElement; import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.Message;
@ -54,7 +55,7 @@ public class OpenPgpEncryptedChat {
} }
public void send(Message message, List<ExtensionElement> payload) public void send(Message message, List<ExtensionElement> payload)
throws MissingOpenPgpKeyPairException { throws MissingOpenPgpKeyPairException, SmackException.NotConnectedException, InterruptedException {
Set<OpenPgpV4Fingerprint> encryptionFingerprints = new HashSet<>(contactsFingerprints.getActiveKeys()); Set<OpenPgpV4Fingerprint> encryptionFingerprints = new HashSet<>(contactsFingerprints.getActiveKeys());
encryptionFingerprints.addAll(ourFingerprints.getActiveKeys()); encryptionFingerprints.addAll(ourFingerprints.getActiveKeys());
@ -81,10 +82,12 @@ public class OpenPgpEncryptedChat {
ExplicitMessageEncryptionElement.ExplicitMessageEncryptionProtocol.openpgpV0)); ExplicitMessageEncryptionElement.ExplicitMessageEncryptionProtocol.openpgpV0));
StoreHint.set(message); StoreHint.set(message);
message.setBody("This message is encrypted using XEP-0374: OpenPGP for XMPP: Instant Messaging."); message.setBody("This message is encrypted using XEP-0374: OpenPGP for XMPP: Instant Messaging.");
chat.send(message);
} }
public void send(Message message, CharSequence body) public void send(Message message, CharSequence body)
throws MissingOpenPgpKeyPairException { throws MissingOpenPgpKeyPairException, SmackException.NotConnectedException, InterruptedException {
List<ExtensionElement> payload = new ArrayList<>(); List<ExtensionElement> payload = new ArrayList<>();
payload.add(new Message.Body(null, body.toString())); payload.add(new Message.Body(null, body.toString()));
send(message, payload); send(message, payload);

View File

@ -17,5 +17,15 @@
package org.jivesoftware.smackx.ox.callback; package org.jivesoftware.smackx.ox.callback;
public interface AskForBackupCodeCallback { 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 <a href="https://xmpp.org/extensions/xep-0373.html#sect-idm139662753819792">
* XEP-0373 §5.3 about the format of the backup code</a>
*
* @return backup code provided by the user.
*/
String askForBackupCode(); String askForBackupCode();
} }

View File

@ -17,5 +17,16 @@
package org.jivesoftware.smackx.ox.callback; package org.jivesoftware.smackx.ox.callback;
public interface DisplayBackupCodeCallback { 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 <a href="https://xmpp.org/extensions/xep-0373.html#sect-idm139662753819792">
* XEP-0373 §5.3 about the format of the backup code</a>
*
* @param backupCode
*/
void displayBackupCode(String backupCode); void displayBackupCode(String backupCode);
} }

View File

@ -172,6 +172,11 @@ public abstract class OpenPgpContentElement implements ExtensionElement {
xml.closeElement(ELEM_PAYLOAD); 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() { public InputStream toInputStream() {
byte[] encoded = toXML(null).toString().getBytes(Charset.forName("UTF-8")); byte[] encoded = toXML(null).toString().getBytes(Charset.forName("UTF-8"));
return new ByteArrayInputStream(encoded); return new ByteArrayInputStream(encoded);

View File

@ -18,12 +18,24 @@ package org.jivesoftware.smackx.ox.listener;
import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smackx.ox.OpenPgpEncryptedChat; import org.jivesoftware.smackx.ox.OpenPgpEncryptedChat;
import org.jivesoftware.smackx.ox.element.OpenPgpElement;
import org.jivesoftware.smackx.ox.element.SigncryptElement; import org.jivesoftware.smackx.ox.element.SigncryptElement;
import org.jxmpp.jid.EntityBareJid; import org.jxmpp.jid.EntityBareJid;
public interface OpenPgpEncryptedMessageListener { public interface OpenPgpEncryptedMessageListener {
/**
* This method gets invoked, whenever an OX-IM encrypted message gets received.
*
* @see <a href="https://xmpp.org/extensions/xep-0374.html">
* XEP-0374: OpenPGP for XMPP: Instant Messaging (OX-IM)</a>
*
* @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, void newIncomingEncryptedMessage(EntityBareJid from,
Message originalMessage, Message originalMessage,
SigncryptElement decryptedPayload, SigncryptElement decryptedPayload,