Bump to latest Smack changes

This commit is contained in:
Paul Schaub 2018-06-21 15:21:06 +02:00
parent 7d3788df54
commit 3cd327471a
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
2 changed files with 38 additions and 22 deletions

View file

@ -1,6 +1,7 @@
package de.vanitasvitae.oxclient; package de.vanitasvitae.oxclient;
import java.io.File; import java.io.File;
import java.security.Security;
import java.util.Scanner; import java.util.Scanner;
import java.util.Set; import java.util.Set;
@ -12,14 +13,14 @@ import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smackx.ox.OXInstantMessagingManager; import org.jivesoftware.smackx.ox.OXInstantMessagingManager;
import org.jivesoftware.smackx.ox.OpenPgpManager; import org.jivesoftware.smackx.ox.OpenPgpManager;
import org.jivesoftware.smackx.ox.OpenPgpV4Fingerprint; import org.jivesoftware.smackx.ox.OpenPgpV4Fingerprint;
import org.jivesoftware.smackx.ox.PubSubDelegate; import org.jivesoftware.smackx.ox.bouncycastle.FileBasedPainlessOpenPgpStore;
import org.jivesoftware.smackx.ox.bouncycastle.BCOpenPgpProvider; import org.jivesoftware.smackx.ox.bouncycastle.PainlessOpenPgpProvider;
import org.jivesoftware.smackx.ox.bouncycastle.FileBasedBcOpenPgpStore; import org.jivesoftware.smackx.ox.util.KeyBytesAndFingerprint;
import org.jivesoftware.smackx.ox.callback.SecretKeyBackupSelectionCallback; import org.jivesoftware.smackx.ox.util.PubSubDelegate;
import org.jivesoftware.smackx.ox.callback.SecretKeyRestoreSelectionCallback;
import name.neuhalfen.projects.crypto.bouncycastle.openpgp.BouncyGPG; import de.vanitasvitae.crypto.pgpainless.key.SecretKeyRingProtector;
import name.neuhalfen.projects.crypto.bouncycastle.openpgp.keys.callbacks.KeyringConfigCallbacks; import de.vanitasvitae.crypto.pgpainless.key.UnprotectedKeysProtector;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.jxmpp.jid.BareJid; import org.jxmpp.jid.BareJid;
import org.jxmpp.jid.EntityBareJid; import org.jxmpp.jid.EntityBareJid;
import org.jxmpp.jid.impl.JidCreate; import org.jxmpp.jid.impl.JidCreate;
@ -28,26 +29,33 @@ import org.jxmpp.stringprep.XmppStringprepException;
public class Client { public class Client {
private final XMPPTCPConnection connection; private final XMPPTCPConnection connection;
private final String username;
public Client(String username, String password) throws XmppStringprepException { public Client(String username, String password) throws XmppStringprepException {
this.connection = new XMPPTCPConnection(username, password); this.connection = new XMPPTCPConnection(username, password);
this.username = username;
} }
public void start() throws Exception { public void start() throws Exception {
BouncyGPG.registerProvider(); Security.removeProvider("BC");
Security.addProvider(new BouncyCastleProvider());
connection.connect().login(); connection.connect().login();
Scanner scanner = new Scanner(System.in); Scanner scanner = new Scanner(System.in);
BareJid user = connection.getUser().asBareJid(); BareJid user = connection.getUser().asBareJid();
OpenPgpManager manager = OpenPgpManager.getInstanceFor(connection); OpenPgpManager manager = OpenPgpManager.getInstanceFor(connection);
BCOpenPgpProvider provider = new BCOpenPgpProvider(user); SecretKeyRingProtector protector = new UnprotectedKeysProtector();
FileBasedBcOpenPgpStore store = new FileBasedBcOpenPgpStore(new File("oxstore"), user, FileBasedPainlessOpenPgpStore store = new FileBasedPainlessOpenPgpStore(new File("oxstore", username), protector);
KeyringConfigCallbacks.withUnprotectedKeys()); PainlessOpenPgpProvider provider = new PainlessOpenPgpProvider(user, store);
provider.setStore(store);
manager.setOpenPgpProvider(provider); manager.setOpenPgpProvider(provider);
Set<OpenPgpV4Fingerprint> av = store.getAvailableKeyPairFingerprints(user);
if (av.size() == 1) {
store.setPrimaryOpenPgpKeyPairFingerprint(av.iterator().next());
}
OXInstantMessagingManager instantManager = OXInstantMessagingManager.getInstanceFor(connection); OXInstantMessagingManager instantManager = OXInstantMessagingManager.getInstanceFor(connection);
instantManager.addOpenPgpEncryptedMessageListener((from, originalMessage, decryptedPayload, chat) -> System.out.println("Received OX chat message from " + from + ":\n" + instantManager.addOxMessageListener((chat, originalMessage, decryptedPayload) -> System.out.println("Received OX chat message from " + chat.getJid() + ":\n" +
decryptedPayload.<Message.Body>getExtension( decryptedPayload.<Message.Body>getExtension(
Message.Body.ELEMENT, Message.Body.NAMESPACE) Message.Body.ELEMENT, Message.Body.NAMESPACE)
.getMessage())); .getMessage()));
@ -62,7 +70,7 @@ public class Client {
String cmd = scanner.nextLine(); String cmd = scanner.nextLine();
switch (cmd) { switch (cmd) {
case "publishKeys": case "publishKeys":
if (provider.primaryOpenPgpKeyPairFingerprint() == null) { if (provider.getStore().getPrimaryOpenPgpKeyPairFingerprint() == null) {
System.out.println("No private key available. Try to generate one using \"generateKey\""); System.out.println("No private key available. Try to generate one using \"generateKey\"");
break; break;
} }
@ -95,7 +103,9 @@ public class Client {
break outerloop; break outerloop;
case "generateKey": case "generateKey":
provider.createOpenPgpKeyPair(); KeyBytesAndFingerprint kf = provider.generateOpenPgpKeyPair(user);
provider.importSecretKey(user, kf.getBytes());
store.setPrimaryOpenPgpKeyPairFingerprint(kf.getFingerprint());
System.out.println("Key generated."); System.out.println("Key generated.");
System.out.println(manager.getOurFingerprint()); System.out.println(manager.getOurFingerprint());
break; break;
@ -110,17 +120,17 @@ public class Client {
String l = scanner.nextLine(); String l = scanner.nextLine();
if (l.isEmpty()) { if (l.isEmpty()) {
System.out.println(provider.primaryOpenPgpKeyPairFingerprint()); System.out.println(store.getPrimaryOpenPgpKeyPairFingerprint());
} else { } else {
jid = JidCreate.bareFrom(l); jid = JidCreate.bareFrom(l);
for (OpenPgpV4Fingerprint f : provider.availableOpenPgpPublicKeysFingerprints(jid)) { for (OpenPgpV4Fingerprint f : store.getAvailableKeysFingerprints(jid).keySet()) {
System.out.println(f); System.out.println(f);
} }
} }
break; break;
case "encrypt": case "encrypt":
if (provider.primaryOpenPgpKeyPairFingerprint() == null) { if (store.getPrimaryOpenPgpKeyPairFingerprint() == null) {
System.out.println("No private key available. Try to generate one using \"generateKey\""); System.out.println("No private key available. Try to generate one using \"generateKey\"");
break; break;
} }
@ -129,13 +139,13 @@ public class Client {
System.out.println("Enter a message:"); System.out.println("Enter a message:");
String message = scanner.nextLine(); String message = scanner.nextLine();
instantManager.chatWith(((EntityBareJid) jid).asEntityBareJid()) manager.getOpenPgpContact(((EntityBareJid) jid).asEntityBareJid())
.send(new Message(jid), message); .send(connection, new Message(jid), message);
System.out.println("Message sent."); System.out.println("Message sent.");
break; break;
case "backup": case "backup":
if (provider.primaryOpenPgpKeyPairFingerprint() == null) { if (store.getPrimaryOpenPgpKeyPairFingerprint() == null) {
System.out.println("No private key available. Try to generate one using \"generateKey\""); System.out.println("No private key available. Try to generate one using \"generateKey\"");
break; break;
} }

View file

@ -1,6 +1,9 @@
package de.vanitasvitae.oxclient; package de.vanitasvitae.oxclient;
import java.util.Scanner; import java.util.Scanner;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.SmackConfiguration; import org.jivesoftware.smack.SmackConfiguration;
@ -8,8 +11,11 @@ public class Main {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
SmackConfiguration.DEBUG = false; SmackConfiguration.DEBUG = true;
SmackConfiguration.setDefaultReplyTimeout(10 * 1000); SmackConfiguration.setDefaultReplyTimeout(10 * 1000);
ConsoleHandler handler = new ConsoleHandler();
handler.setLevel(Level.FINER);
Logger.getGlobal().addHandler(handler);
String username, password; String username, password;
Scanner scanner = new Scanner(System.in); Scanner scanner = new Scanner(System.in);