55 lines
2.3 KiB
Java
55 lines
2.3 KiB
Java
package org.mercury_im.messenger.core.crypto.ikey;
|
|
|
|
import org.bouncycastle.openpgp.PGPException;
|
|
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
|
import org.jivesoftware.smack.SmackException;
|
|
import org.jivesoftware.smack.XMPPException;
|
|
import org.jivesoftware.smackx.ikey.IkeyManager;
|
|
import org.jivesoftware.smackx.ikey_ox.OxIkeyManager;
|
|
import org.jivesoftware.smackx.ox.OpenPgpSecretKeyBackupPassphrase;
|
|
import org.jivesoftware.smackx.pubsub.PubSubException;
|
|
import org.mercury_im.messenger.core.SchedulersFacade;
|
|
import org.mercury_im.messenger.core.connection.MercuryConnection;
|
|
import org.pgpainless.PGPainless;
|
|
import org.pgpainless.key.collection.PGPKeyRing;
|
|
|
|
import java.io.IOException;
|
|
import java.security.InvalidAlgorithmParameterException;
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
import javax.inject.Inject;
|
|
|
|
public class MercuryIkeyManager {
|
|
|
|
private final IkeyRepository ikeyRepository;
|
|
private final SchedulersFacade schedulers;
|
|
|
|
@Inject
|
|
public MercuryIkeyManager(IkeyRepository ikeyRepository, SchedulersFacade schedulers) {
|
|
this.ikeyRepository = ikeyRepository;
|
|
this.schedulers = schedulers;
|
|
}
|
|
|
|
public void initFor(MercuryConnection connection)
|
|
throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException,
|
|
InterruptedException, SmackException.NoResponseException,
|
|
SmackException.NotConnectedException, SmackException.FeatureNotSupportedException,
|
|
XMPPException.XMPPErrorException, PubSubException.NotALeafNodeException, IOException {
|
|
IkeyManager ikeyManager = IkeyManager.getInstanceFor(connection.getConnection());
|
|
|
|
// bind repo to store
|
|
IkeyStoreAdapter store = new IkeyStoreAdapter(connection.getAccountId(), ikeyRepository, schedulers);
|
|
ikeyManager.setStore(store);
|
|
|
|
PGPSecretKeyRing ikey = store.loadSecretKey();
|
|
if (ikey == null) {
|
|
PGPKeyRing keyRing = PGPainless.generateKeyRing().simpleEcKeyRing("xmpp:" + connection.getAccount().getAddress());
|
|
store.storeSecretKey(keyRing.getSecretKeys());
|
|
}
|
|
|
|
OxIkeyManager oxIkeyManager = OxIkeyManager.getInstanceFor(connection.getConnection());
|
|
OpenPgpSecretKeyBackupPassphrase passphrase = oxIkeyManager.depositSecretIdentityKey(ikey);
|
|
|
|
}
|
|
}
|