Search in sources :

Example 1 with OpenPgpElementAndMetadata

use of org.jivesoftware.smackx.ox.crypto.OpenPgpElementAndMetadata in project Smack by igniterealtime.

the class PainlessOpenPgpProviderTest method encryptDecryptTest.

@Test
public void encryptDecryptTest() throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, IOException, MissingUserIdOnKeyException, XmlPullParserException {
    // Initialize
    OpenPgpStore aliceStore = new FileBasedOpenPgpStore(storagePath);
    OpenPgpStore bobStore = new FileBasedOpenPgpStore(storagePath);
    aliceStore.setKeyRingProtector(new UnprotectedKeysProtector());
    bobStore.setKeyRingProtector(new UnprotectedKeysProtector());
    XMPPConnection bobConnection = new DummyConnection();
    PainlessOpenPgpProvider aliceProvider = new PainlessOpenPgpProvider(aliceStore);
    PainlessOpenPgpProvider bobProvider = new PainlessOpenPgpProvider(bobStore);
    PGPSecretKeyRing aliceKeys = aliceStore.generateKeyRing(alice);
    PGPSecretKeyRing bobKeys = bobStore.generateKeyRing(bob);
    PGPPublicKeyRing alicePubKeys = KeyRingUtils.publicKeyRingFrom(aliceKeys);
    PGPPublicKeyRing bobPubKeys = KeyRingUtils.publicKeyRingFrom(bobKeys);
    OpenPgpV4Fingerprint aliceFingerprint = new OpenPgpV4Fingerprint(aliceKeys);
    OpenPgpV4Fingerprint bobFingerprint = new OpenPgpV4Fingerprint(bobKeys);
    aliceStore.importSecretKey(alice, aliceKeys);
    bobStore.importSecretKey(bob, bobKeys);
    aliceStore.setAnnouncedFingerprintsOf(alice, Collections.singletonMap(aliceFingerprint, new Date()));
    bobStore.setAnnouncedFingerprintsOf(bob, Collections.singletonMap(bobFingerprint, new Date()));
    OpenPgpSelf aliceSelf = new OpenPgpSelf(alice, aliceStore);
    aliceSelf.trust(aliceFingerprint);
    OpenPgpSelf bobSelf = new OpenPgpSelf(bob, bobStore);
    bobSelf.trust(bobFingerprint);
    // Exchange keys
    aliceStore.importPublicKey(bob, bobPubKeys);
    bobStore.importPublicKey(alice, alicePubKeys);
    aliceStore.setAnnouncedFingerprintsOf(bob, Collections.singletonMap(bobFingerprint, new Date()));
    bobStore.setAnnouncedFingerprintsOf(alice, Collections.singletonMap(aliceFingerprint, new Date()));
    OpenPgpContact aliceForBob = new OpenPgpContact(alice, bobStore);
    aliceForBob.trust(aliceFingerprint);
    OpenPgpContact bobForAlice = new OpenPgpContact(bob, aliceStore);
    bobForAlice.trust(bobFingerprint);
    // Prepare message
    Message.Body body = new Message.Body(null, "Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi consequat. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");
    List<ExtensionElement> payload = Collections.singletonList(body);
    OpenPgpElementAndMetadata encrypted;
    OpenPgpMessage decrypted;
    /*
        test signcrypt
         */
    SigncryptElement signcryptElement = new SigncryptElement(Collections.<Jid>singleton(bob), payload);
    // Encrypt and Sign
    encrypted = aliceProvider.signAndEncrypt(signcryptElement, aliceSelf, Collections.singleton(bobForAlice));
    // Decrypt and Verify
    decrypted = bobProvider.decryptAndOrVerify(bobConnection, encrypted.getElement(), bobSelf, aliceForBob);
    OpenPgpFingerprint decryptionFingerprint = decrypted.getMetadata().getDecryptionKey().getFingerprint();
    assertTrue(bobSelf.getSecretKeys().contains(decryptionFingerprint.getKeyId()));
    assertTrue(decrypted.getMetadata().containsVerifiedSignatureFrom(alicePubKeys));
    assertEquals(OpenPgpMessage.State.signcrypt, decrypted.getState());
    SigncryptElement decryptedSignCrypt = (SigncryptElement) decrypted.getOpenPgpContentElement();
    assertEquals(body.getMessage(), decryptedSignCrypt.<Message.Body>getExtension(Message.Body.ELEMENT, Message.Body.NAMESPACE).getMessage());
    /*
        test crypt
         */
    CryptElement cryptElement = new CryptElement(Collections.<Jid>singleton(bob), payload);
    // Encrypt
    encrypted = aliceProvider.encrypt(cryptElement, aliceSelf, Collections.singleton(bobForAlice));
    decrypted = bobProvider.decryptAndOrVerify(bobConnection, encrypted.getElement(), bobSelf, aliceForBob);
    decryptionFingerprint = decrypted.getMetadata().getDecryptionKey().getFingerprint();
    assertTrue(bobSelf.getSecretKeys().contains(decryptionFingerprint.getKeyId()));
    assertTrue(decrypted.getMetadata().getVerifiedSignatures().isEmpty());
    assertEquals(OpenPgpMessage.State.crypt, decrypted.getState());
    CryptElement decryptedCrypt = (CryptElement) decrypted.getOpenPgpContentElement();
    assertEquals(body.getMessage(), decryptedCrypt.<Message.Body>getExtension(Message.Body.ELEMENT, Message.Body.NAMESPACE).getMessage());
    /*
        test sign
         */
    SignElement signElement = new SignElement(Collections.singleton(bob), new Date(), payload);
    // Sign
    encrypted = aliceProvider.sign(signElement, aliceSelf);
    decrypted = bobProvider.decryptAndOrVerify(bobConnection, encrypted.getElement(), bobSelf, aliceForBob);
    assertNull(decrypted.getMetadata().getDecryptionKey());
    assertTrue(decrypted.getMetadata().containsVerifiedSignatureFrom(alicePubKeys));
    assertEquals(OpenPgpMessage.State.sign, decrypted.getState());
    SignElement decryptedSign = (SignElement) decrypted.getOpenPgpContentElement();
    assertEquals(body.getMessage(), decryptedSign.<Message.Body>getExtension(Message.Body.ELEMENT, Message.Body.NAMESPACE).getMessage());
}
Also used : PGPPublicKeyRing(org.bouncycastle.openpgp.PGPPublicKeyRing) SigncryptElement(org.jivesoftware.smackx.ox.element.SigncryptElement) Message(org.jivesoftware.smack.packet.Message) DummyConnection(org.jivesoftware.smack.DummyConnection) ExtensionElement(org.jivesoftware.smack.packet.ExtensionElement) XMPPConnection(org.jivesoftware.smack.XMPPConnection) Date(java.util.Date) CryptElement(org.jivesoftware.smackx.ox.element.CryptElement) OpenPgpElementAndMetadata(org.jivesoftware.smackx.ox.crypto.OpenPgpElementAndMetadata) FileBasedOpenPgpStore(org.jivesoftware.smackx.ox.store.filebased.FileBasedOpenPgpStore) UnprotectedKeysProtector(org.pgpainless.key.protection.UnprotectedKeysProtector) SignElement(org.jivesoftware.smackx.ox.element.SignElement) FileBasedOpenPgpStore(org.jivesoftware.smackx.ox.store.filebased.FileBasedOpenPgpStore) OpenPgpStore(org.jivesoftware.smackx.ox.store.definition.OpenPgpStore) OpenPgpV4Fingerprint(org.pgpainless.key.OpenPgpV4Fingerprint) OpenPgpFingerprint(org.pgpainless.key.OpenPgpFingerprint) PGPSecretKeyRing(org.bouncycastle.openpgp.PGPSecretKeyRing) PainlessOpenPgpProvider(org.jivesoftware.smackx.ox.crypto.PainlessOpenPgpProvider) Test(org.junit.jupiter.api.Test)

Example 2 with OpenPgpElementAndMetadata

use of org.jivesoftware.smackx.ox.crypto.OpenPgpElementAndMetadata in project Smack by igniterealtime.

the class OXInstantMessagingManager method signAndEncrypt.

/**
 * Wrap some {@code payload} into a {@link SigncryptElement}, sign and encrypt it for {@code contacts} and ourselves.
 *
 * @param contacts recipients of the message
 * @param payload payload which will be encrypted and signed
 *
 * @return encrypted and signed {@link OpenPgpElement}, along with {@link OpenPgpMetadata} about the
 * encryption + signatures.
 *
 * @throws SmackException.NotLoggedInException in case we are not logged in
 * @throws IOException IO is dangerous (we need to read keys)
 * @throws PGPException in case encryption goes wrong
 */
public OpenPgpElementAndMetadata signAndEncrypt(Set<OpenPgpContact> contacts, List<ExtensionElement> payload) throws SmackException.NotLoggedInException, IOException, PGPException {
    Set<Jid> jids = new HashSet<>();
    for (OpenPgpContact contact : contacts) {
        jids.add(contact.getJid());
    }
    jids.add(openPgpManager.getOpenPgpSelf().getJid());
    SigncryptElement signcryptElement = new SigncryptElement(jids, payload);
    OpenPgpElementAndMetadata encrypted = openPgpManager.getOpenPgpProvider().signAndEncrypt(signcryptElement, openPgpManager.getOpenPgpSelf(), contacts);
    return encrypted;
}
Also used : SigncryptElement(org.jivesoftware.smackx.ox.element.SigncryptElement) Jid(org.jxmpp.jid.Jid) BareJid(org.jxmpp.jid.BareJid) HashSet(java.util.HashSet) OpenPgpContact(org.jivesoftware.smackx.ox.OpenPgpContact) OpenPgpElementAndMetadata(org.jivesoftware.smackx.ox.crypto.OpenPgpElementAndMetadata)

Example 3 with OpenPgpElementAndMetadata

use of org.jivesoftware.smackx.ox.crypto.OpenPgpElementAndMetadata in project Smack by igniterealtime.

the class OXInstantMessagingManager method addOxMessage.

/**
 * Add an OX-IM message element to a message.
 *
 * @param messageBuilder message
 * @param recipients recipients of the message
 * @param payload payload which will be encrypted and signed
 *
 * @return {@link EncryptionResult} containing metadata about the messages encryption + signatures.
 *
 * @throws SmackException.NotLoggedInException in case we are not logged in
 * @throws PGPException in case something goes wrong during encryption
 * @throws IOException IO is dangerous (we need to read keys)
 */
public EncryptionResult addOxMessage(MessageBuilder messageBuilder, Set<OpenPgpContact> recipients, List<ExtensionElement> payload) throws SmackException.NotLoggedInException, IOException, PGPException {
    OpenPgpElementAndMetadata openPgpElementAndMetadata = signAndEncrypt(recipients, payload);
    messageBuilder.addExtension(openPgpElementAndMetadata.getElement());
    // Set hints on message
    ExplicitMessageEncryptionElement.set(messageBuilder, ExplicitMessageEncryptionElement.ExplicitMessageEncryptionProtocol.openpgpV0);
    StoreHint.set(messageBuilder);
    setOXBodyHint(messageBuilder);
    return openPgpElementAndMetadata.getMetadata();
}
Also used : OpenPgpElementAndMetadata(org.jivesoftware.smackx.ox.crypto.OpenPgpElementAndMetadata)

Aggregations

OpenPgpElementAndMetadata (org.jivesoftware.smackx.ox.crypto.OpenPgpElementAndMetadata)3 SigncryptElement (org.jivesoftware.smackx.ox.element.SigncryptElement)2 Date (java.util.Date)1 HashSet (java.util.HashSet)1 PGPPublicKeyRing (org.bouncycastle.openpgp.PGPPublicKeyRing)1 PGPSecretKeyRing (org.bouncycastle.openpgp.PGPSecretKeyRing)1 DummyConnection (org.jivesoftware.smack.DummyConnection)1 XMPPConnection (org.jivesoftware.smack.XMPPConnection)1 ExtensionElement (org.jivesoftware.smack.packet.ExtensionElement)1 Message (org.jivesoftware.smack.packet.Message)1 OpenPgpContact (org.jivesoftware.smackx.ox.OpenPgpContact)1 PainlessOpenPgpProvider (org.jivesoftware.smackx.ox.crypto.PainlessOpenPgpProvider)1 CryptElement (org.jivesoftware.smackx.ox.element.CryptElement)1 SignElement (org.jivesoftware.smackx.ox.element.SignElement)1 OpenPgpStore (org.jivesoftware.smackx.ox.store.definition.OpenPgpStore)1 FileBasedOpenPgpStore (org.jivesoftware.smackx.ox.store.filebased.FileBasedOpenPgpStore)1 Test (org.junit.jupiter.api.Test)1 BareJid (org.jxmpp.jid.BareJid)1 Jid (org.jxmpp.jid.Jid)1 OpenPgpFingerprint (org.pgpainless.key.OpenPgpFingerprint)1