mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-12-08 03:51:09 +01:00
Refactor API
This commit is contained in:
parent
e23cf88082
commit
ffbfae9856
14 changed files with 521 additions and 138 deletions
|
|
@ -126,7 +126,7 @@ public class PainlessOpenPgpProvider implements OpenPgpProvider {
|
|||
try {
|
||||
toEncrypted = PGPainless.createEncryptor()
|
||||
.onOutputStream(encryptedBytes)
|
||||
.toRecipients(new ArrayList<>(encryptionKeys.values()).toArray(new PGPPublicKeyRing[]{}))
|
||||
.toRecipients(new ArrayList<>(encryptionKeys.values()).toArray(new PGPPublicKeyRing[] {}))
|
||||
.usingSecureAlgorithms()
|
||||
.signWith(secretKeyRingProtector, signingKey)
|
||||
.noArmor();
|
||||
|
|
@ -201,12 +201,12 @@ public class PainlessOpenPgpProvider implements OpenPgpProvider {
|
|||
|
||||
@Override
|
||||
public DecryptedBytesAndMetadata decrypt(byte[] bytes, BareJid sender, final SmackMissingOpenPgpPublicKeyCallback missingPublicKeyCallback)
|
||||
throws MissingOpenPgpKeyPairException, SmackOpenPgpException, IOException {
|
||||
throws MissingOpenPgpKeyPairException, SmackOpenPgpException {
|
||||
|
||||
PGPSecretKeyRingCollection secretKeyRings;
|
||||
try {
|
||||
secretKeyRings = getStore().getSecretKeyRings(owner);
|
||||
} catch (PGPException e) {
|
||||
} catch (PGPException | IOException e) {
|
||||
LOGGER.log(Level.INFO, "Could not get secret keys of user " + owner);
|
||||
throw new MissingOpenPgpKeyPairException(owner, getStore().getPrimaryOpenPgpKeyPairFingerprint());
|
||||
}
|
||||
|
|
@ -222,7 +222,7 @@ public class PainlessOpenPgpProvider implements OpenPgpProvider {
|
|||
PGPPublicKeyRingCollection publicKeyRings;
|
||||
try {
|
||||
publicKeyRings = getStore().getPublicKeyRings(sender);
|
||||
} catch (PGPException e) {
|
||||
} catch (PGPException | IOException e) {
|
||||
LOGGER.log(Level.INFO, "Could not get public keys of sender " + sender.toString(), e);
|
||||
if (missingPublicKeyCallback != null) {
|
||||
// TODO: Handle missing key
|
||||
|
|
@ -239,7 +239,11 @@ public class PainlessOpenPgpProvider implements OpenPgpProvider {
|
|||
}
|
||||
}
|
||||
|
||||
return decryptImpl(bytes, secretKeyRings, protector, trustedKeys);
|
||||
try {
|
||||
return decryptImpl(bytes, secretKeyRings, protector, trustedKeys);
|
||||
} catch (IOException e) {
|
||||
throw new SmackOpenPgpException(e);
|
||||
}
|
||||
}
|
||||
|
||||
DecryptedBytesAndMetadata decryptImpl(byte[] bytes, PGPSecretKeyRingCollection decryptionKeys,
|
||||
|
|
|
|||
|
|
@ -0,0 +1,91 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2018 Paul Schaub.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smackx.ox.bouncycastle;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.security.InvalidAlgorithmParameterException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.NoSuchProviderException;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
|
||||
import org.jivesoftware.smack.util.stringencoder.Base64;
|
||||
import org.jivesoftware.smackx.ox.element.PubkeyElement;
|
||||
import org.jivesoftware.smackx.ox.exception.MissingOpenPgpPublicKeyException;
|
||||
import org.jivesoftware.smackx.ox.exception.MissingUserIdOnKeyException;
|
||||
import org.jivesoftware.smackx.ox.exception.SmackOpenPgpException;
|
||||
import org.jivesoftware.smackx.ox.util.KeyBytesAndFingerprint;
|
||||
|
||||
import de.vanitasvitae.crypto.pgpainless.key.UnprotectedKeysProtector;
|
||||
import org.bouncycastle.openpgp.PGPException;
|
||||
import org.junit.Test;
|
||||
import org.jxmpp.jid.BareJid;
|
||||
import org.jxmpp.jid.JidTestUtil;
|
||||
|
||||
public class DryOxEncryptionTest extends OxTestSuite {
|
||||
|
||||
private static File getTempDir(String suffix) {
|
||||
String temp = System.getProperty("java.io.tmpdir");
|
||||
if (temp == null) {
|
||||
temp = "tmp";
|
||||
}
|
||||
|
||||
if (suffix == null) {
|
||||
return new File(temp);
|
||||
} else {
|
||||
return new File(temp, suffix);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dryEncryptionTest()
|
||||
throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException,
|
||||
IOException, SmackOpenPgpException, MissingUserIdOnKeyException, MissingOpenPgpPublicKeyException {
|
||||
BareJid alice = JidTestUtil.BARE_JID_1;
|
||||
BareJid bob = JidTestUtil.BARE_JID_2;
|
||||
|
||||
File alicePath = getTempDir("ox-alice");
|
||||
File bobPath = getTempDir("ox-bob");
|
||||
FileBasedPainlessOpenPgpStore aliceStore = new FileBasedPainlessOpenPgpStore(alicePath, new UnprotectedKeysProtector());
|
||||
FileBasedPainlessOpenPgpStore bobStore = new FileBasedPainlessOpenPgpStore(bobPath, new UnprotectedKeysProtector());
|
||||
|
||||
PainlessOpenPgpProvider aliceProvider = new PainlessOpenPgpProvider(alice, aliceStore);
|
||||
PainlessOpenPgpProvider bobProvider = new PainlessOpenPgpProvider(bob, bobStore);
|
||||
|
||||
KeyBytesAndFingerprint aliceKey = aliceProvider.generateOpenPgpKeyPair(alice);
|
||||
KeyBytesAndFingerprint bobKey = bobProvider.generateOpenPgpKeyPair(bob);
|
||||
|
||||
aliceProvider.importSecretKey(alice, aliceKey.getBytes());
|
||||
bobProvider.importSecretKey(bob, bobKey.getBytes());
|
||||
|
||||
PubkeyElement alicePub = new PubkeyElement(new PubkeyElement.PubkeyDataElement(
|
||||
Base64.encode(aliceStore.getPublicKeyRingBytes(alice, aliceKey.getFingerprint()))),
|
||||
new Date());
|
||||
PubkeyElement bobPub = new PubkeyElement(new PubkeyElement.PubkeyDataElement(
|
||||
Base64.encode(bobStore.getPublicKeyRingBytes(bob, bobKey.getFingerprint()))),
|
||||
new Date());
|
||||
|
||||
aliceProvider.importPublicKey(bob, Base64.decode(bobPub.getDataElement().getB64Data()));
|
||||
bobProvider.importPublicKey(alice, Base64.decode(alicePub.getDataElement().getB64Data()));
|
||||
|
||||
aliceStore.setAnnouncedKeysFingerprints(bob, Collections.singletonMap(bobKey.getFingerprint(), new Date()));
|
||||
bobStore.setAnnouncedKeysFingerprints(alice, Collections.singletonMap(aliceKey.getFingerprint(), new Date()));
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,22 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2018 Paul Schaub.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smackx.ox.bouncycastle;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
import static junit.framework.TestCase.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
|
|
@ -7,30 +24,35 @@ import java.io.IOException;
|
|||
import java.security.InvalidAlgorithmParameterException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.NoSuchProviderException;
|
||||
import java.security.Security;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.jivesoftware.smack.test.util.SmackTestSuite;
|
||||
import org.jivesoftware.smack.util.FileUtils;
|
||||
|
||||
import de.vanitasvitae.crypto.pgpainless.PGPainless;
|
||||
import de.vanitasvitae.crypto.pgpainless.key.UnprotectedKeysProtector;
|
||||
import de.vanitasvitae.crypto.pgpainless.key.generation.type.length.RsaLength;
|
||||
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||||
import de.vanitasvitae.crypto.pgpainless.util.BCUtil;
|
||||
import org.bouncycastle.openpgp.PGPException;
|
||||
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
||||
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
|
||||
import org.bouncycastle.openpgp.PGPSecretKey;
|
||||
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
||||
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.jxmpp.jid.BareJid;
|
||||
import org.jxmpp.jid.impl.JidCreate;
|
||||
import org.jxmpp.stringprep.XmppStringprepException;
|
||||
|
||||
public class FileBasedPainlessOpenPgpStoreTest extends SmackTestSuite {
|
||||
public class FileBasedPainlessOpenPgpStoreTest extends OxTestSuite {
|
||||
|
||||
private static final File basePath;
|
||||
private static final BareJid alice;
|
||||
private static final BareJid bob;
|
||||
|
||||
private FileBasedPainlessOpenPgpStore store;
|
||||
|
||||
static {
|
||||
String userHome = System.getProperty("user.home");
|
||||
if (userHome != null) {
|
||||
|
|
@ -46,22 +68,45 @@ public class FileBasedPainlessOpenPgpStoreTest extends SmackTestSuite {
|
|||
} catch (XmppStringprepException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
Security.addProvider(new BouncyCastleProvider());
|
||||
@Before
|
||||
public void deleteStore() {
|
||||
FileUtils.deleteDirectory(basePath);
|
||||
this.store = new FileBasedPainlessOpenPgpStore(basePath, new UnprotectedKeysProtector());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void storeSecretKeyRingsTest()
|
||||
throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException,
|
||||
IOException {
|
||||
FileBasedPainlessOpenPgpStore store = new FileBasedPainlessOpenPgpStore(basePath, new UnprotectedKeysProtector());
|
||||
|
||||
PGPSecretKeyRing secretKey = PGPainless.generateKeyRing().simpleRsaKeyRing("xmpp:" + alice.toString(), RsaLength._3072);
|
||||
PGPSecretKeyRingCollection saving = new PGPSecretKeyRingCollection(Collections.singleton(secretKey));
|
||||
store.storeSecretKeyRing(alice, saving);
|
||||
|
||||
PGPSecretKeyRingCollection restored = store.getSecretKeyRings(alice);
|
||||
FileBasedPainlessOpenPgpStore store2 = new FileBasedPainlessOpenPgpStore(basePath, new UnprotectedKeysProtector());
|
||||
PGPSecretKeyRingCollection restored = store2.getSecretKeyRings(alice);
|
||||
|
||||
assertTrue(Arrays.equals(saving.getEncoded(), restored.getEncoded()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void storePublicKeyRingTest()
|
||||
throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException,
|
||||
IOException {
|
||||
PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing().simpleRsaKeyRing("xmpp:" + alice.toString(), RsaLength._3072);
|
||||
|
||||
PGPPublicKeyRing publicKeys = BCUtil.publicKeyRingFromSecretKeyRing(secretKeys);
|
||||
for (PGPSecretKey k : secretKeys) {
|
||||
assertEquals(publicKeys.getPublicKey(k.getKeyID()), k.getPublicKey());
|
||||
}
|
||||
|
||||
PGPPublicKeyRingCollection saving = new PGPPublicKeyRingCollection(Collections.singleton(publicKeys));
|
||||
store.storePublicKeyRing(alice, saving);
|
||||
|
||||
FileBasedPainlessOpenPgpStore store2 = new FileBasedPainlessOpenPgpStore(basePath, new UnprotectedKeysProtector());
|
||||
|
||||
PGPPublicKeyRingCollection restored = store2.getPublicKeyRings(alice);
|
||||
assertTrue(Arrays.equals(saving.getEncoded(), restored.getEncoded()));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,33 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2018 Paul Schaub.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smackx.ox.bouncycastle;
|
||||
|
||||
import java.security.Security;
|
||||
|
||||
import org.jivesoftware.smack.test.util.SmackTestSuite;
|
||||
|
||||
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||||
import org.junit.BeforeClass;
|
||||
|
||||
public abstract class OxTestSuite extends SmackTestSuite {
|
||||
|
||||
@BeforeClass
|
||||
public static void registerProvider() {
|
||||
Security.removeProvider("BC");
|
||||
Security.addProvider(new BouncyCastleProvider());
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue