1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-27 14:32:06 +01:00
This commit is contained in:
Paul Schaub 2018-07-09 15:01:15 +02:00
parent 8e4013fff3
commit 0031f7f5f3
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
8 changed files with 89 additions and 14 deletions

View file

@ -110,7 +110,7 @@ public class BasicOpenPgpInstantMessagingIntegrationTest extends AbstractOpenPgp
OpenPgpContact bobForAlice = aliceOpenPgp.getOpenPgpContact(bob.asEntityBareJidIfPossible()); OpenPgpContact bobForAlice = aliceOpenPgp.getOpenPgpContact(bob.asEntityBareJidIfPossible());
OpenPgpContact aliceForBob = bobOpenPgp.getOpenPgpContact(alice.asEntityBareJidIfPossible()); OpenPgpContact aliceForBob = bobOpenPgp.getOpenPgpContact(alice.asEntityBareJidIfPossible());
// TODO: Update keys bobForAlice.updateKeys(aliceConnection);
aliceInstantMessaging.sendOxMessage(bobForAlice, body); aliceInstantMessaging.sendOxMessage(bobForAlice, body);

View file

@ -18,17 +18,28 @@ package org.jivesoftware.smackx.ox;
import java.io.IOException; import java.io.IOException;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.util.stringencoder.Base64;
import org.jivesoftware.smackx.ox.element.PubkeyElement;
import org.jivesoftware.smackx.ox.element.PublicKeysListElement;
import org.jivesoftware.smackx.ox.exception.MissingUserIdOnKeyException;
import org.jivesoftware.smackx.ox.selection_strategy.AnnouncedKeys; import org.jivesoftware.smackx.ox.selection_strategy.AnnouncedKeys;
import org.jivesoftware.smackx.ox.selection_strategy.BareJidUserId; import org.jivesoftware.smackx.ox.selection_strategy.BareJidUserId;
import org.jivesoftware.smackx.ox.store.definition.OpenPgpStore; import org.jivesoftware.smackx.ox.store.definition.OpenPgpStore;
import org.jivesoftware.smackx.ox.util.PubSubDelegate;
import org.jivesoftware.smackx.pubsub.PubSubException;
import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKeyRing; import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
import org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator;
import org.jxmpp.jid.BareJid; import org.jxmpp.jid.BareJid;
import org.pgpainless.pgpainless.key.OpenPgpV4Fingerprint; import org.pgpainless.pgpainless.key.OpenPgpV4Fingerprint;
@ -79,4 +90,42 @@ public class OpenPgpContact {
} }
return announcedKeysCollection; return announcedKeysCollection;
} }
public void updateKeys(XMPPConnection connection) throws InterruptedException, SmackException.NotConnectedException,
SmackException.NoResponseException, XMPPException.XMPPErrorException, PubSubException.NotALeafNodeException,
PubSubException.NotAPubSubNodeException, IOException {
PublicKeysListElement metadata = PubSubDelegate.fetchPubkeysList(connection, getJid());
if (metadata == null) {
return;
}
Map<OpenPgpV4Fingerprint, Date> fingerprintsAndDates = new HashMap<>();
for (OpenPgpV4Fingerprint fingerprint : metadata.getMetadata().keySet()) {
fingerprintsAndDates.put(fingerprint, metadata.getMetadata().get(fingerprint).getDate());
}
store.setAnnouncedFingerprintsOf(getJid(), fingerprintsAndDates);
for (OpenPgpV4Fingerprint fingerprint : metadata.getMetadata().keySet()) {
try {
PubkeyElement key = PubSubDelegate.fetchPubkey(connection, getJid(), fingerprint);
if (key == null) {
LOGGER.log(Level.WARNING, "Public key " + Long.toHexString(fingerprint.getKeyId()) +
" can not be imported: Is null");
continue;
}
PGPPublicKeyRing keyRing = new PGPPublicKeyRing(Base64.decode(key.getDataElement().getB64Data()), new BcKeyFingerprintCalculator());
store.importPublicKey(getJid(), keyRing);
} catch (PubSubException.NotAPubSubNodeException | PubSubException.NotALeafNodeException |
XMPPException.XMPPErrorException e) {
LOGGER.log(Level.WARNING, "Error fetching public key " + Long.toHexString(fingerprint.getKeyId()), e);
} catch (PGPException | IOException e) {
LOGGER.log(Level.WARNING, "Public key " + Long.toHexString(fingerprint.getKeyId()) +
" can not be imported.", e);
} catch (MissingUserIdOnKeyException e) {
LOGGER.log(Level.WARNING, "Public key " + Long.toHexString(fingerprint.getKeyId()) +
" is missing the user-id \"xmpp:" + getJid() + "\". Refuse to import it.", e);
}
}
}
} }

View file

@ -68,7 +68,7 @@ public class PainlessOpenPgpProvider implements OpenPgpProvider {
.toRecipients(recipientKeys.toArray(new PGPPublicKeyRingCollection[]{})) .toRecipients(recipientKeys.toArray(new PGPPublicKeyRingCollection[]{}))
.andToSelf(self.getAnnouncedPublicKeys()) .andToSelf(self.getAnnouncedPublicKeys())
.usingSecureAlgorithms() .usingSecureAlgorithms()
.signWith(null, self.getSigningKeyRing()) .signWith(store.getKeyRingProtector(), self.getSigningKeyRing())
.noArmor(); .noArmor();
Streams.pipeAll(plainText, cipherStream); Streams.pipeAll(plainText, cipherStream);
@ -90,7 +90,7 @@ public class PainlessOpenPgpProvider implements OpenPgpProvider {
EncryptionStream cipherStream = PGPainless.createEncryptor().onOutputStream(cipherText) EncryptionStream cipherStream = PGPainless.createEncryptor().onOutputStream(cipherText)
.doNotEncrypt() .doNotEncrypt()
.signWith(null, self.getSigningKeyRing()) .signWith(store.getKeyRingProtector(), self.getSigningKeyRing())
.noArmor(); .noArmor();
Streams.pipeAll(plainText, cipherStream); Streams.pipeAll(plainText, cipherStream);
@ -138,7 +138,7 @@ public class PainlessOpenPgpProvider implements OpenPgpProvider {
InputStream cipherText = element.toInputStream(); InputStream cipherText = element.toInputStream();
DecryptionStream cipherStream = PGPainless.createDecryptor().onInputStream(cipherText) DecryptionStream cipherStream = PGPainless.createDecryptor().onInputStream(cipherText)
.decryptWith(null, self.getSecretKeys()) .decryptWith(store.getKeyRingProtector(), self.getSecretKeys())
.verifyWith(sender.getAnnouncedPublicKeys()) .verifyWith(sender.getAnnouncedPublicKeys())
.ignoreMissingPublicKeys() .ignoreMissingPublicKeys()
.build(); .build();

View file

@ -38,6 +38,7 @@ import org.jxmpp.jid.BareJid;
import org.pgpainless.pgpainless.PGPainless; import org.pgpainless.pgpainless.PGPainless;
import org.pgpainless.pgpainless.key.OpenPgpV4Fingerprint; import org.pgpainless.pgpainless.key.OpenPgpV4Fingerprint;
import org.pgpainless.pgpainless.key.generation.type.length.RsaLength; import org.pgpainless.pgpainless.key.generation.type.length.RsaLength;
import org.pgpainless.pgpainless.util.BCUtil;
public abstract class AbstractOpenPgpKeyStore implements OpenPgpKeyStore { public abstract class AbstractOpenPgpKeyStore implements OpenPgpKeyStore {
@ -88,12 +89,17 @@ public abstract class AbstractOpenPgpKeyStore implements OpenPgpKeyStore {
PGPSecretKeyRingCollection secretKeyRings = getSecretKeysOf(owner); PGPSecretKeyRingCollection secretKeyRings = getSecretKeysOf(owner);
try { try {
if (secretKeyRings != null) {
secretKeyRings = PGPSecretKeyRingCollection.addSecretKeyRing(secretKeyRings, secretKeys); secretKeyRings = PGPSecretKeyRingCollection.addSecretKeyRing(secretKeyRings, secretKeys);
} else {
secretKeyRings = BCUtil.keyRingsToKeyRingCollection(secretKeys);
}
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
LOGGER.log(Level.INFO, "Skipping secret key ring " + Long.toHexString(secretKeys.getPublicKey().getKeyID()) + LOGGER.log(Level.INFO, "Skipping secret key ring " + Long.toHexString(secretKeys.getPublicKey().getKeyID()) +
" as it is already in the key ring of " + owner.toString()); " as it is already in the key ring of " + owner.toString());
} }
this.secretKeyRingCollections.put(owner, secretKeyRings); this.secretKeyRingCollections.put(owner, secretKeyRings);
importPublicKey(owner, BCUtil.publicKeyRingFromSecretKeyRing(secretKeys));
writeSecretKeysOf(owner, secretKeyRings); writeSecretKeysOf(owner, secretKeyRings);
} }
@ -106,7 +112,11 @@ public abstract class AbstractOpenPgpKeyStore implements OpenPgpKeyStore {
PGPPublicKeyRingCollection publicKeyRings = getPublicKeysOf(owner); PGPPublicKeyRingCollection publicKeyRings = getPublicKeysOf(owner);
try { try {
if (publicKeyRings != null) {
publicKeyRings = PGPPublicKeyRingCollection.addPublicKeyRing(publicKeyRings, publicKeys); publicKeyRings = PGPPublicKeyRingCollection.addPublicKeyRing(publicKeyRings, publicKeys);
} else {
publicKeyRings = BCUtil.keyRingsToKeyRingCollection(publicKeys);
}
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
LOGGER.log(Level.INFO, "Skipping public key ring " + Long.toHexString(publicKeys.getPublicKey().getKeyID()) + LOGGER.log(Level.INFO, "Skipping public key ring " + Long.toHexString(publicKeys.getPublicKey().getKeyID()) +
" as it is already in the key ring of " + owner.toString()); " as it is already in the key ring of " + owner.toString());

View file

@ -64,12 +64,11 @@ public abstract class AbstractOpenPgpStore extends Observable implements OpenPgp
@Override @Override
public OpenPgpContact getOpenPgpContact(BareJid jid) { public OpenPgpContact getOpenPgpContact(BareJid jid) {
OpenPgpContact contact = contacts.get(jid); OpenPgpContact contact = contacts.get(jid);
if (contact != null) { if (contact == null) {
return contact; contact = new OpenPgpContact(jid, this);
contacts.put(jid, contact);
} }
return contact;
// TODO
return null;
} }
@Override @Override

View file

@ -33,8 +33,6 @@ import org.pgpainless.pgpainless.key.OpenPgpV4Fingerprint;
public interface OpenPgpKeyStore { public interface OpenPgpKeyStore {
PGPPublicKeyRingCollection getPublicKeysOf(BareJid owner) throws IOException, PGPException; PGPPublicKeyRingCollection getPublicKeysOf(BareJid owner) throws IOException, PGPException;
PGPSecretKeyRingCollection getSecretKeysOf(BareJid owner) throws IOException, PGPException; PGPSecretKeyRingCollection getSecretKeysOf(BareJid owner) throws IOException, PGPException;

View file

@ -62,6 +62,10 @@ public class FileBasedOpenPgpMetadataStore extends AbstractOpenPgpMetadataStore
} }
private Map<OpenPgpV4Fingerprint, Date> readFingerprintsAndDates(File source) throws IOException { private Map<OpenPgpV4Fingerprint, Date> readFingerprintsAndDates(File source) throws IOException {
if (!source.exists() || source.isDirectory()) {
return new HashMap<>();
}
BufferedReader reader = null; BufferedReader reader = null;
try { try {
reader = Files.newBufferedReader(source.toPath(), Util.UTF8); reader = Files.newBufferedReader(source.toPath(), Util.UTF8);
@ -106,6 +110,21 @@ public class FileBasedOpenPgpMetadataStore extends AbstractOpenPgpMetadataStore
private void writeFingerprintsAndDates(Map<OpenPgpV4Fingerprint, Date> data, File destination) private void writeFingerprintsAndDates(Map<OpenPgpV4Fingerprint, Date> data, File destination)
throws IOException { throws IOException {
if (!destination.exists()) {
File parent = destination.getParentFile();
if (!parent.exists() && !parent.mkdirs()) {
throw new IOException("Cannot create directory " + parent.getAbsolutePath());
}
if (!destination.createNewFile()) {
throw new IOException("Cannot create file " + destination.getAbsolutePath());
}
}
if (destination.isDirectory()) {
throw new IOException("File " + destination.getAbsolutePath() + " is a directory.");
}
BufferedWriter writer = null; BufferedWriter writer = null;
try { try {
writer = Files.newBufferedWriter(destination.toPath(), Util.UTF8); writer = Files.newBufferedWriter(destination.toPath(), Util.UTF8);

View file

@ -29,7 +29,7 @@ public class FileUtils {
// Create parent directory // Create parent directory
File parent = file.getParentFile(); File parent = file.getParentFile();
if (!parent.mkdirs()) { if (!parent.exists() && !parent.mkdirs()) {
throw new IOException("Cannot create directory " + parent.getAbsolutePath()); throw new IOException("Cannot create directory " + parent.getAbsolutePath());
} }