2020-06-15 17:41:13 +02:00
|
|
|
package org.mercury_im.messenger.core.crypto;
|
|
|
|
|
|
|
|
import org.jivesoftware.smack.AbstractConnectionListener;
|
|
|
|
import org.jivesoftware.smack.XMPPConnection;
|
|
|
|
import org.jivesoftware.smackx.ox.OpenPgpManager;
|
|
|
|
import org.jivesoftware.smackx.ox.crypto.OpenPgpProvider;
|
2020-06-18 21:52:06 +02:00
|
|
|
import org.jivesoftware.smackx.ox.crypto.PainlessOpenPgpProvider;
|
2020-06-15 17:41:13 +02:00
|
|
|
import org.jivesoftware.smackx.ox.exception.InvalidBackupCodeException;
|
|
|
|
import org.jivesoftware.smackx.ox.exception.NoBackupFoundException;
|
2020-06-18 21:52:06 +02:00
|
|
|
import org.jivesoftware.smackx.ox.store.definition.OpenPgpStore;
|
2020-06-24 22:50:26 +02:00
|
|
|
import org.jivesoftware.smackx.ox.util.SecretKeyBackupHelper;
|
2020-06-15 17:41:13 +02:00
|
|
|
import org.jivesoftware.smackx.ox_im.OXInstantMessagingManager;
|
|
|
|
import org.jivesoftware.smackx.pubsub.PubSubException;
|
2020-06-18 21:52:06 +02:00
|
|
|
import org.mercury_im.messenger.core.SchedulersFacade;
|
|
|
|
import org.mercury_im.messenger.core.data.repository.OpenPgpRepository;
|
2020-06-24 22:50:26 +02:00
|
|
|
import org.mercury_im.messenger.core.data.repository.Repositories;
|
2020-06-18 21:52:06 +02:00
|
|
|
import org.mercury_im.messenger.core.store.crypto.MercuryOpenPgpStore;
|
2020-06-24 22:50:26 +02:00
|
|
|
import org.mercury_im.messenger.core.store.message.MercuryMessageStore;
|
2020-06-15 17:41:13 +02:00
|
|
|
import org.mercury_im.messenger.core.xmpp.MercuryConnection;
|
|
|
|
|
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
|
|
|
public class MercuryOpenPgpManager {
|
|
|
|
|
|
|
|
private static final Logger LOGGER = Logger.getLogger(MercuryOpenPgpManager.class.getName());
|
|
|
|
|
2020-06-24 22:50:26 +02:00
|
|
|
private final Repositories repositories;
|
2020-06-18 21:52:06 +02:00
|
|
|
private final OpenPgpRepository openPgpRepository;
|
|
|
|
private final SchedulersFacade schedulers;
|
2020-06-15 17:41:13 +02:00
|
|
|
|
|
|
|
@Inject
|
2020-06-24 22:50:26 +02:00
|
|
|
public MercuryOpenPgpManager(OpenPgpRepository openPgpRepository,
|
|
|
|
SchedulersFacade schedulers,
|
|
|
|
Repositories repositories) {
|
2020-06-18 21:52:06 +02:00
|
|
|
this.openPgpRepository = openPgpRepository;
|
|
|
|
this.schedulers = schedulers;
|
2020-06-24 22:50:26 +02:00
|
|
|
this.repositories = repositories;
|
2020-06-15 17:41:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void initialize(MercuryConnection connection) {
|
|
|
|
if (connection.getConnection().isAuthenticated()) {
|
|
|
|
setup(connection);
|
|
|
|
} else {
|
|
|
|
connection.getConnection().addConnectionListener(new AbstractConnectionListener() {
|
|
|
|
@Override
|
|
|
|
public void authenticated(XMPPConnection con, boolean resumed) {
|
|
|
|
if (!resumed) {
|
|
|
|
setup(connection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setup(MercuryConnection connection) {
|
2020-06-18 21:52:06 +02:00
|
|
|
OpenPgpStore store = new MercuryOpenPgpStore(connection.getAccountId(), openPgpRepository, schedulers);
|
|
|
|
OpenPgpProvider provider = new PainlessOpenPgpProvider(store);
|
2020-06-24 22:50:26 +02:00
|
|
|
OpenPgpManager oxManager = OpenPgpManager.getInstanceFor(connection.getConnection());
|
|
|
|
oxManager.setOpenPgpProvider(provider);
|
|
|
|
SecretKeyBackupHelper.setBackupCodeGenerator(
|
|
|
|
() -> "71ZA-Y416-UA7A-7NCE-3SNM-88EF"
|
|
|
|
);
|
2020-06-15 17:41:13 +02:00
|
|
|
try {
|
|
|
|
if (!oxManager.hasSecretKeysAvailable()) {
|
|
|
|
try {
|
|
|
|
oxManager.restoreSecretKeyServerBackup(
|
|
|
|
//() -> "RW8X-367S-A2C3-QYAL-VG6E-Z2IM");
|
2020-06-18 21:52:06 +02:00
|
|
|
//() -> "KISJ-5Z1T-FGDW-WMDK-SC2U-SQUA");
|
|
|
|
() -> "71ZA-Y416-UA7A-7NCE-3SNM-88EF");
|
2020-06-15 17:41:13 +02:00
|
|
|
LOGGER.log(Level.INFO, "Successfully restored secret key backup!");
|
|
|
|
} catch (NoBackupFoundException | PubSubException.NotALeafNodeException | InvalidBackupCodeException e) {
|
|
|
|
LOGGER.log(Level.INFO, "Error restoring secret key backup.", e);
|
|
|
|
oxManager.generateAndImportKeyPair(connection.getAccount().getJid());
|
|
|
|
oxManager.backupSecretKeyToServer(
|
|
|
|
backupCode -> LOGGER.log(Level.INFO, "OpenPGP Backup Code: " + backupCode),
|
|
|
|
availableSecretKeys -> availableSecretKeys);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
oxManager.announceSupportAndPublish();
|
|
|
|
OXInstantMessagingManager oximManager = OXInstantMessagingManager.getInstanceFor(connection.getConnection());
|
2020-06-26 16:00:47 +02:00
|
|
|
oximManager.addOxMessageListener(new MercuryMessageStore(connection.getAccount(),
|
|
|
|
repositories.getPeerRepository(), repositories.getDirectChatRepository(),
|
|
|
|
repositories.getMessageRepository(), schedulers));
|
2020-06-15 17:41:13 +02:00
|
|
|
oximManager.announceSupportForOxInstantMessaging();
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|