2018-05-08 18:06:30 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* 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.assertTrue;
|
|
|
|
import static org.jivesoftware.smackx.ox.TestKeys.JULIET_UID;
|
|
|
|
import static org.jivesoftware.smackx.ox.TestKeys.ROMEO_UID;
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.nio.charset.Charset;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
import java.security.NoSuchProviderException;
|
|
|
|
import java.security.SignatureException;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Date;
|
|
|
|
|
|
|
|
import org.jivesoftware.smack.test.util.SmackTestSuite;
|
|
|
|
import org.jivesoftware.smackx.ox.TestKeys;
|
|
|
|
|
|
|
|
import name.neuhalfen.projects.crypto.bouncycastle.openpgp.BouncyGPG;
|
2018-05-10 16:00:27 +02:00
|
|
|
import name.neuhalfen.projects.crypto.bouncycastle.openpgp.keys.callbacks.KeySelectionStrategy;
|
2018-05-08 18:06:30 +02:00
|
|
|
import name.neuhalfen.projects.crypto.bouncycastle.openpgp.keys.callbacks.KeyringConfigCallbacks;
|
|
|
|
import name.neuhalfen.projects.crypto.bouncycastle.openpgp.keys.callbacks.Xep0373KeySelectionStrategy;
|
|
|
|
import name.neuhalfen.projects.crypto.bouncycastle.openpgp.keys.keyrings.InMemoryKeyring;
|
|
|
|
import name.neuhalfen.projects.crypto.bouncycastle.openpgp.keys.keyrings.KeyringConfig;
|
|
|
|
import name.neuhalfen.projects.crypto.bouncycastle.openpgp.keys.keyrings.KeyringConfigs;
|
|
|
|
import org.bouncycastle.openpgp.PGPException;
|
|
|
|
import org.bouncycastle.util.io.Streams;
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
public class BasicEncryptionTest extends SmackTestSuite {
|
|
|
|
|
|
|
|
private static final Charset UTF8 = Charset.forName("UTF-8");
|
|
|
|
private final KeyringConfig keyringJuliet;
|
|
|
|
private final KeyringConfig keyringRomeo;
|
|
|
|
|
|
|
|
public BasicEncryptionTest() throws IOException, PGPException {
|
|
|
|
super();
|
|
|
|
|
|
|
|
// Prepare Juliets keyring
|
|
|
|
keyringJuliet = KeyringConfigs.forGpgExportedKeys(KeyringConfigCallbacks.withUnprotectedKeys());
|
|
|
|
((InMemoryKeyring) keyringJuliet).addSecretKey(TestKeys.JULIET_PRIV.getBytes(UTF8));
|
|
|
|
((InMemoryKeyring) keyringJuliet).addPublicKey(TestKeys.JULIET_PUB.getBytes(UTF8));
|
|
|
|
((InMemoryKeyring) keyringJuliet).addPublicKey(TestKeys.ROMEO_PUB.getBytes(UTF8));
|
|
|
|
|
|
|
|
// Prepare Romeos keyring
|
|
|
|
keyringRomeo = KeyringConfigs.forGpgExportedKeys(KeyringConfigCallbacks.withUnprotectedKeys());
|
|
|
|
((InMemoryKeyring) keyringRomeo).addSecretKey(TestKeys.ROMEO_PRIV.getBytes(UTF8));
|
|
|
|
((InMemoryKeyring) keyringRomeo).addPublicKey(TestKeys.ROMEO_PUB.getBytes(UTF8));
|
|
|
|
((InMemoryKeyring) keyringRomeo).addPublicKey(TestKeys.JULIET_PUB.getBytes(UTF8));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void encryptionTest()
|
|
|
|
throws IOException, PGPException, NoSuchAlgorithmException, SignatureException, NoSuchProviderException {
|
|
|
|
|
|
|
|
|
|
|
|
ByteArrayOutputStream result = new ByteArrayOutputStream();
|
2018-05-10 16:00:27 +02:00
|
|
|
KeySelectionStrategy selectionStrategy = new Xep0373KeySelectionStrategy(new Date());
|
2018-05-08 18:06:30 +02:00
|
|
|
|
|
|
|
byte[] message = "Hello World!!!!".getBytes(UTF8);
|
|
|
|
|
|
|
|
// Encrypt
|
|
|
|
OutputStream out = BouncyGPG.encryptToStream()
|
|
|
|
.withConfig(keyringJuliet)
|
2018-05-10 16:00:27 +02:00
|
|
|
.withKeySelectionStrategy(selectionStrategy)
|
|
|
|
.withOxAlgorithms()
|
2018-05-08 18:06:30 +02:00
|
|
|
.toRecipients(ROMEO_UID, JULIET_UID)
|
|
|
|
.andSignWith(JULIET_UID)
|
|
|
|
.binaryOutput()
|
|
|
|
.andWriteTo(result);
|
|
|
|
|
|
|
|
out.write(message);
|
|
|
|
out.close();
|
|
|
|
|
|
|
|
byte[] encrypted = result.toByteArray();
|
|
|
|
|
|
|
|
// Decrypt
|
|
|
|
ByteArrayInputStream encIn = new ByteArrayInputStream(encrypted);
|
|
|
|
InputStream in = BouncyGPG.decryptAndVerifyStream()
|
|
|
|
.withConfig(keyringRomeo)
|
2018-05-10 16:00:27 +02:00
|
|
|
.withKeySelectionStrategy(selectionStrategy)
|
2018-05-08 18:06:30 +02:00
|
|
|
.andRequireSignatureFromAllKeys(JULIET_UID)
|
|
|
|
.fromEncryptedInputStream(encIn);
|
|
|
|
|
|
|
|
ByteArrayOutputStream decrypted = new ByteArrayOutputStream();
|
|
|
|
Streams.pipeAll(in, decrypted);
|
|
|
|
|
|
|
|
byte[] message2 = decrypted.toByteArray();
|
|
|
|
|
|
|
|
assertTrue(Arrays.equals(message, message2));
|
|
|
|
}
|
|
|
|
}
|