mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-12-09 04:21:08 +01:00
Temp commit
This commit is contained in:
parent
6e7145801e
commit
a234760233
14 changed files with 258 additions and 82 deletions
|
|
@ -1,3 +1,19 @@
|
|||
/**
|
||||
*
|
||||
* 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.FileNotFoundException;
|
||||
|
|
|
|||
|
|
@ -33,6 +33,7 @@ import org.jivesoftware.smackx.ox.OpenPgpMessage;
|
|||
import org.jivesoftware.smackx.ox.OpenPgpProvider;
|
||||
import org.jivesoftware.smackx.ox.element.OpenPgpElement;
|
||||
import org.jivesoftware.smackx.ox.element.PubkeyElement;
|
||||
import org.jivesoftware.smackx.ox.element.PublicKeysListElement;
|
||||
|
||||
import name.neuhalfen.projects.crypto.bouncycastle.openpgp.BouncyGPG;
|
||||
import name.neuhalfen.projects.crypto.bouncycastle.openpgp.algorithms.PublicKeySize;
|
||||
|
|
@ -50,14 +51,14 @@ import org.bouncycastle.util.encoders.Hex;
|
|||
import org.bouncycastle.util.io.Streams;
|
||||
import org.jxmpp.jid.BareJid;
|
||||
|
||||
public class BouncycastleOpenPgpProvider implements OpenPgpProvider {
|
||||
public class BouncyCastleOpenPgpProvider implements OpenPgpProvider {
|
||||
|
||||
private final BareJid ourJid;
|
||||
private final InMemoryKeyring ourKeys;
|
||||
private final Long ourKeyId;
|
||||
private final Map<BareJid, InMemoryKeyring> theirKeys = new HashMap<>();
|
||||
|
||||
public BouncycastleOpenPgpProvider(BareJid ourJid) throws IOException, PGPException, NoSuchAlgorithmException {
|
||||
public BouncyCastleOpenPgpProvider(BareJid ourJid) throws IOException, PGPException, NoSuchAlgorithmException {
|
||||
this.ourJid = ourJid;
|
||||
PGPSecretKeyRing ourKey = generateKey(ourJid).generateSecretKeyRing();
|
||||
ourKeyId = ourKey.getPublicKey().getKeyID();
|
||||
|
|
@ -87,6 +88,11 @@ public class BouncycastleOpenPgpProvider implements OpenPgpProvider {
|
|||
contactsKeyring.addPublicKey(decoded);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processPublicKeysListElement(PublicKeysListElement listElement, BareJid from) throws Exception {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public OpenPgpElement signAndEncrypt(InputStream inputStream, Set<BareJid> recipients)
|
||||
throws Exception {
|
||||
|
|
@ -138,6 +144,16 @@ public class BouncycastleOpenPgpProvider implements OpenPgpProvider {
|
|||
return new OpenPgpElement(base64);
|
||||
}
|
||||
|
||||
@Override
|
||||
public OpenPgpElement sign(InputStream inputStream) throws Exception {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public OpenPgpElement encrypt(InputStream inputStream, Set<BareJid> recipients) throws Exception {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public OpenPgpMessage decryptAndVerify(OpenPgpElement element, BareJid sender) throws Exception {
|
||||
InMemoryKeyring decryptionConfig = KeyringConfigs.forGpgExportedKeys(KeyringConfigCallbacks.withUnprotectedKeys());
|
||||
|
|
@ -168,7 +184,7 @@ public class BouncycastleOpenPgpProvider implements OpenPgpProvider {
|
|||
|
||||
Streams.pipeAll(decrypted, decryptedOut);
|
||||
|
||||
return new OpenPgpMessage(null, new String(decryptedOut.toByteArray(), Charset.forName("UTF-8")));
|
||||
return new OpenPgpMessage(new String(decryptedOut.toByteArray(), Charset.forName("UTF-8")));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -1,3 +1,19 @@
|
|||
/**
|
||||
*
|
||||
* 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.DataInputStream;
|
||||
|
|
@ -56,6 +72,10 @@ public class FileBasedBouncyCastleIdentityStore implements BouncyCastleIdentityS
|
|||
public PublicKeysListElement loadPubkeyList(BareJid jid) throws IOException {
|
||||
File contactsDir = contactsDir(jid);
|
||||
File source = new File(contactsDir, "pubkey_list");
|
||||
if (!source.exists()) {
|
||||
LOGGER.log(Level.FINE, "File " + source.getAbsolutePath() + " does not exist. Returning null.");
|
||||
return null;
|
||||
}
|
||||
DataInputStream dataIn = new DataInputStream(new FileInputStream(source));
|
||||
|
||||
PublicKeysListElement.Builder builder = PublicKeysListElement.builder();
|
||||
|
|
|
|||
|
|
@ -116,8 +116,8 @@ public class BasicEncryptionTest extends SmackTestSuite {
|
|||
throws IOException, PGPException, NoSuchAlgorithmException, NoSuchProviderException, SignatureException {
|
||||
final String alice = "alice@wonderland.lit";
|
||||
final String bob = "bob@builder.tv";
|
||||
PGPKeyRingGenerator g1 = BouncycastleOpenPgpProvider.generateKey(JidCreate.bareFrom(alice));
|
||||
PGPKeyRingGenerator g2 = BouncycastleOpenPgpProvider.generateKey(JidCreate.bareFrom(bob));
|
||||
PGPKeyRingGenerator g1 = BouncyCastleOpenPgpProvider.generateKey(JidCreate.bareFrom(alice));
|
||||
PGPKeyRingGenerator g2 = BouncyCastleOpenPgpProvider.generateKey(JidCreate.bareFrom(bob));
|
||||
PGPSecretKey s1 = g1.generateSecretKeyRing().getSecretKey();
|
||||
PGPSecretKey s2 = g2.generateSecretKeyRing().getSecretKey();
|
||||
PGPPublicKey p1 = g1.generatePublicKeyRing().getPublicKey();
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ import org.jxmpp.jid.BareJid;
|
|||
import org.jxmpp.jid.Jid;
|
||||
import org.jxmpp.jid.impl.JidCreate;
|
||||
|
||||
public class BouncycastleOpenPgpProviderTest extends SmackTestSuite {
|
||||
public class BouncyCastleOpenPgpProviderTest extends SmackTestSuite {
|
||||
|
||||
@Test
|
||||
public void encryptAndSign_decryptAndVerifyElementTest() throws Exception {
|
||||
|
|
@ -46,8 +46,8 @@ public class BouncycastleOpenPgpProviderTest extends SmackTestSuite {
|
|||
// Create providers for alice and the cat
|
||||
BareJid alice = JidCreate.bareFrom("alice@wonderland.lit");
|
||||
BareJid cheshire = JidCreate.bareFrom("cheshire@wonderland.lit");
|
||||
BouncycastleOpenPgpProvider aliceProvider = new BouncycastleOpenPgpProvider(alice);
|
||||
BouncycastleOpenPgpProvider cheshireProvider = new BouncycastleOpenPgpProvider(cheshire);
|
||||
BouncyCastleOpenPgpProvider aliceProvider = new BouncyCastleOpenPgpProvider(alice);
|
||||
BouncyCastleOpenPgpProvider cheshireProvider = new BouncyCastleOpenPgpProvider(cheshire);
|
||||
|
||||
// dry exchange keys
|
||||
PubkeyElement aliceKeys = aliceProvider.createPubkeyElement();
|
||||
|
|
@ -1,14 +1,31 @@
|
|||
/**
|
||||
*
|
||||
* 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.assertNull;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.text.ParseException;
|
||||
import java.util.Date;
|
||||
import java.util.Stack;
|
||||
|
||||
import org.jivesoftware.smack.test.util.SmackTestSuite;
|
||||
import org.jivesoftware.smack.util.FileUtils;
|
||||
import org.jivesoftware.smackx.ox.element.PublicKeysListElement;
|
||||
|
||||
import org.junit.After;
|
||||
|
|
@ -34,36 +51,6 @@ public class FileBasedBouncyCastleIdentityStoreTest extends SmackTestSuite {
|
|||
}
|
||||
}
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
deleteStore();
|
||||
}
|
||||
|
||||
@After
|
||||
public void after() {
|
||||
deleteStore();
|
||||
}
|
||||
|
||||
public void deleteStore() {
|
||||
File[] currList;
|
||||
Stack<File> stack = new Stack<>();
|
||||
stack.push(storePath);
|
||||
while (!stack.isEmpty()) {
|
||||
if (stack.lastElement().isDirectory()) {
|
||||
currList = stack.lastElement().listFiles();
|
||||
if (currList != null && currList.length > 0) {
|
||||
for (File curr : currList) {
|
||||
stack.push(curr);
|
||||
}
|
||||
} else {
|
||||
stack.pop().delete();
|
||||
}
|
||||
} else {
|
||||
stack.pop().delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeReadPublicKeysLists() throws ParseException, IOException {
|
||||
BareJid jid = JidCreate.bareFrom("edward@snowden.org");
|
||||
|
|
@ -78,9 +65,26 @@ public class FileBasedBouncyCastleIdentityStoreTest extends SmackTestSuite {
|
|||
.build();
|
||||
|
||||
FileBasedBouncyCastleIdentityStore store = new FileBasedBouncyCastleIdentityStore(storePath);
|
||||
|
||||
PublicKeysListElement shouldBeNull = store.loadPubkeyList(jid);
|
||||
assertNull(shouldBeNull);
|
||||
store.storePubkeyList(jid, list);
|
||||
|
||||
PublicKeysListElement retrieved = store.loadPubkeyList(jid);
|
||||
assertEquals(list.getMetadata(), retrieved.getMetadata());
|
||||
}
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
deleteStore();
|
||||
}
|
||||
|
||||
@After
|
||||
public void after() {
|
||||
deleteStore();
|
||||
}
|
||||
|
||||
public void deleteStore() {
|
||||
FileUtils.deleteDirectory(storePath);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ public class KeyGenerationTest {
|
|||
|
||||
@Test
|
||||
public void createSecretKey() throws Exception {
|
||||
PGPSecretKey secretKey = BouncycastleOpenPgpProvider
|
||||
PGPSecretKey secretKey = BouncyCastleOpenPgpProvider
|
||||
.generateKey(JidCreate.bareFrom("alice@wonderland.lit"))
|
||||
.generateSecretKeyRing()
|
||||
.getSecretKey();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue