1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-24 04:52:05 +01:00

Fix decrypting messages

This commit is contained in:
Paul Schaub 2018-05-24 16:01:33 +02:00
parent 653f318d37
commit 365a4d20d0
3 changed files with 30 additions and 9 deletions

View file

@ -77,11 +77,6 @@ public class BouncyCastleOpenPgpProvider implements OpenPgpProvider {
public BouncyCastleOpenPgpProvider(BareJid ourJid) throws IOException, PGPException, NoSuchAlgorithmException { public BouncyCastleOpenPgpProvider(BareJid ourJid) throws IOException, PGPException, NoSuchAlgorithmException {
this.ourJid = ourJid; this.ourJid = ourJid;
PGPSecretKeyRing ourKey = generateKey(ourJid).generateSecretKeyRing();
ourKeyId = ourKey.getPublicKey().getKeyID();
ourKeys = KeyringConfigs.forGpgExportedKeys(KeyringConfigCallbacks.withUnprotectedKeys());
ourKeys.addSecretKey(ourKey.getSecretKey().getEncoded());
ourKeys.addPublicKey(ourKey.getPublicKey().getEncoded());
} }
@Override @Override
@ -177,14 +172,20 @@ public class BouncyCastleOpenPgpProvider implements OpenPgpProvider {
InMemoryKeyring newKeyring = KeyringConfigs.forGpgExportedKeys( InMemoryKeyring newKeyring = KeyringConfigs.forGpgExportedKeys(
KeyringConfigCallbacks.withUnprotectedKeys()); KeyringConfigCallbacks.withUnprotectedKeys());
newKeyring.addSecretKey(secretKey.getEncoded());
newKeyring.addPublicKey(secretKey.getPublicKey().getEncoded()); newKeyring.addPublicKey(secretKey.getPublicKey().getEncoded());
newKeyring.addSecretKey(secretKey.getEncoded());
ourKeys = newKeyring; ourKeys = newKeyring;
ourKeyId = secretKey.getKeyID(); ourKeyId = secretKey.getKeyID();
InMemoryKeyring theirKeyRing = KeyringConfigs.forGpgExportedKeys(
KeyringConfigCallbacks.withUnprotectedKeys());
theirKeyRing.addPublicKey(secretKey.getPublicKey().getEncoded());
theirKeys.put(ourJid, theirKeyRing);
} }
} catch (PGPException | IOException e) { } catch (PGPException | IOException e) {
e.printStackTrace(); throw new CorruptedOpenPgpKeyException(e);
} }
} }
@ -358,8 +359,9 @@ public class BouncyCastleOpenPgpProvider implements OpenPgpProvider {
decryptionConfig.addPublicKey(p.getPublicKey().getEncoded()); decryptionConfig.addPublicKey(p.getPublicKey().getEncoded());
} }
ByteArrayInputStream encryptedIn = new ByteArrayInputStream( byte[] b64decoded = Base64.decode(element.getEncryptedBase64MessageContent());
element.getEncryptedBase64MessageContent().getBytes(Charset.forName("UTF-8")));
ByteArrayInputStream encryptedIn = new ByteArrayInputStream(b64decoded);
InputStream decrypted = BouncyGPG.decryptAndVerifyStream() InputStream decrypted = BouncyGPG.decryptAndVerifyStream()
.withConfig(decryptionConfig) .withConfig(decryptionConfig)
@ -385,6 +387,19 @@ public class BouncyCastleOpenPgpProvider implements OpenPgpProvider {
} }
} }
@Override
public void createAndUseKey() throws CorruptedOpenPgpKeyException, NoSuchAlgorithmException {
try {
PGPSecretKeyRing ourKey = generateKey(ourJid).generateSecretKeyRing();
ourKeyId = ourKey.getPublicKey().getKeyID();
ourKeys = KeyringConfigs.forGpgExportedKeys(KeyringConfigCallbacks.withUnprotectedKeys());
ourKeys.addSecretKey(ourKey.getSecretKey().getEncoded());
ourKeys.addPublicKey(ourKey.getPublicKey().getEncoded());
} catch (PGPException | IOException e) {
throw new CorruptedOpenPgpKeyException(e);
}
}
public static PGPKeyRingGenerator generateKey(BareJid owner) throws NoSuchAlgorithmException, PGPException { public static PGPKeyRingGenerator generateKey(BareJid owner) throws NoSuchAlgorithmException, PGPException {
PGPKeyRingGenerator generator = BouncyGPG.createKeyPair() PGPKeyRingGenerator generator = BouncyGPG.createKeyPair()
.withRSAKeys() .withRSAKeys()

View file

@ -49,6 +49,9 @@ public class BouncyCastleOpenPgpProviderTest extends SmackTestSuite {
BouncyCastleOpenPgpProvider aliceProvider = new BouncyCastleOpenPgpProvider(alice); BouncyCastleOpenPgpProvider aliceProvider = new BouncyCastleOpenPgpProvider(alice);
BouncyCastleOpenPgpProvider cheshireProvider = new BouncyCastleOpenPgpProvider(cheshire); BouncyCastleOpenPgpProvider cheshireProvider = new BouncyCastleOpenPgpProvider(cheshire);
aliceProvider.createAndUseKey();
cheshireProvider.createAndUseKey();
// dry exchange keys // dry exchange keys
PubkeyElement aliceKeys = aliceProvider.createPubkeyElement(); PubkeyElement aliceKeys = aliceProvider.createPubkeyElement();
PubkeyElement cheshireKeys = cheshireProvider.createPubkeyElement(); PubkeyElement cheshireKeys = cheshireProvider.createPubkeyElement();

View file

@ -16,6 +16,7 @@
*/ */
package org.jivesoftware.smackx.ox; package org.jivesoftware.smackx.ox;
import java.security.NoSuchAlgorithmException;
import java.util.Set; import java.util.Set;
import org.jivesoftware.smackx.ox.element.CryptElement; import org.jivesoftware.smackx.ox.element.CryptElement;
@ -156,4 +157,6 @@ public interface OpenPgpProvider {
SecretkeyElement createSecretkeyElement(String password) throws CorruptedOpenPgpKeyException; SecretkeyElement createSecretkeyElement(String password) throws CorruptedOpenPgpKeyException;
void restoreSecretKeyElement(SecretkeyElement secretkeyElement, String password) throws CorruptedOpenPgpKeyException; void restoreSecretKeyElement(SecretkeyElement secretkeyElement, String password) throws CorruptedOpenPgpKeyException;
void createAndUseKey() throws CorruptedOpenPgpKeyException, NoSuchAlgorithmException;
} }