Search in sources :

Example 1 with OpenPgpElement

use of org.jivesoftware.smackx.ox.element.OpenPgpElement in project Smack by igniterealtime.

the class PainlessOpenPgpProvider method encrypt.

@Override
public OpenPgpElementAndMetadata encrypt(CryptElement element, OpenPgpSelf self, Collection<OpenPgpContact> recipients) throws IOException, PGPException {
    InputStream plainText = element.toInputStream();
    ByteArrayOutputStream cipherText = new ByteArrayOutputStream();
    EncryptionOptions encOpts = EncryptionOptions.encryptCommunications();
    for (OpenPgpContact contact : recipients) {
        PGPPublicKeyRingCollection keys = contact.getTrustedAnnouncedKeys();
        if (keys == null) {
            LOGGER.log(Level.WARNING, "There are no suitable keys for contact " + contact.getJid());
        }
        encOpts.addRecipients(keys);
    }
    encOpts.addRecipients(self.getTrustedAnnouncedKeys());
    EncryptionStream cipherStream = PGPainless.encryptAndOrSign().onOutputStream(cipherText).withOptions(ProducerOptions.encrypt(encOpts).setAsciiArmor(false));
    Streams.pipeAll(plainText, cipherStream);
    plainText.close();
    cipherStream.flush();
    cipherStream.close();
    cipherText.close();
    String base64 = Base64.encodeToString(cipherText.toByteArray());
    OpenPgpElement openPgpElement = new OpenPgpElement(base64);
    return new OpenPgpElementAndMetadata(openPgpElement, cipherStream.getResult());
}
Also used : PGPPublicKeyRingCollection(org.bouncycastle.openpgp.PGPPublicKeyRingCollection) InputStream(java.io.InputStream) OpenPgpElement(org.jivesoftware.smackx.ox.element.OpenPgpElement) ByteArrayOutputStream(java.io.ByteArrayOutputStream) EncryptionOptions(org.pgpainless.encryption_signing.EncryptionOptions) EncryptionStream(org.pgpainless.encryption_signing.EncryptionStream) OpenPgpContact(org.jivesoftware.smackx.ox.OpenPgpContact)

Example 2 with OpenPgpElement

use of org.jivesoftware.smackx.ox.element.OpenPgpElement in project Smack by igniterealtime.

the class PainlessOpenPgpProvider method signAndEncrypt.

@Override
public OpenPgpElementAndMetadata signAndEncrypt(SigncryptElement element, OpenPgpSelf self, Collection<OpenPgpContact> recipients) throws IOException, PGPException {
    InputStream plainText = element.toInputStream();
    ByteArrayOutputStream cipherText = new ByteArrayOutputStream();
    EncryptionOptions encOpts = EncryptionOptions.encryptCommunications();
    for (OpenPgpContact contact : recipients) {
        PGPPublicKeyRingCollection keys = contact.getTrustedAnnouncedKeys();
        if (keys == null) {
            LOGGER.log(Level.WARNING, "There are no suitable keys for contact " + contact.getJid());
        }
        encOpts.addRecipients(keys);
    }
    encOpts.addRecipients(self.getTrustedAnnouncedKeys());
    SigningOptions signOpts = new SigningOptions();
    signOpts.addInlineSignature(getStore().getKeyRingProtector(), self.getSigningKeyRing(), DocumentSignatureType.BINARY_DOCUMENT);
    EncryptionStream cipherStream = PGPainless.encryptAndOrSign().onOutputStream(cipherText).withOptions(ProducerOptions.signAndEncrypt(encOpts, signOpts).setAsciiArmor(false));
    Streams.pipeAll(plainText, cipherStream);
    plainText.close();
    cipherStream.flush();
    cipherStream.close();
    cipherText.close();
    String base64 = Base64.encodeToString(cipherText.toByteArray());
    OpenPgpElement openPgpElement = new OpenPgpElement(base64);
    return new OpenPgpElementAndMetadata(openPgpElement, cipherStream.getResult());
}
Also used : PGPPublicKeyRingCollection(org.bouncycastle.openpgp.PGPPublicKeyRingCollection) InputStream(java.io.InputStream) OpenPgpElement(org.jivesoftware.smackx.ox.element.OpenPgpElement) SigningOptions(org.pgpainless.encryption_signing.SigningOptions) ByteArrayOutputStream(java.io.ByteArrayOutputStream) EncryptionOptions(org.pgpainless.encryption_signing.EncryptionOptions) EncryptionStream(org.pgpainless.encryption_signing.EncryptionStream) OpenPgpContact(org.jivesoftware.smackx.ox.OpenPgpContact)

Example 3 with OpenPgpElement

use of org.jivesoftware.smackx.ox.element.OpenPgpElement in project Smack by igniterealtime.

the class PainlessOpenPgpProvider method sign.

@Override
public OpenPgpElementAndMetadata sign(SignElement element, OpenPgpSelf self) throws IOException, PGPException {
    InputStream plainText = element.toInputStream();
    ByteArrayOutputStream cipherText = new ByteArrayOutputStream();
    EncryptionStream cipherStream = PGPainless.encryptAndOrSign().onOutputStream(cipherText).withOptions(ProducerOptions.sign(new SigningOptions().addInlineSignature(getStore().getKeyRingProtector(), self.getSigningKeyRing(), "xmpp:" + self.getJid().toString(), DocumentSignatureType.BINARY_DOCUMENT)).setAsciiArmor(false));
    Streams.pipeAll(plainText, cipherStream);
    plainText.close();
    cipherStream.flush();
    cipherStream.close();
    cipherText.close();
    String base64 = Base64.encodeToString(cipherText.toByteArray());
    OpenPgpElement openPgpElement = new OpenPgpElement(base64);
    return new OpenPgpElementAndMetadata(openPgpElement, cipherStream.getResult());
}
Also used : InputStream(java.io.InputStream) OpenPgpElement(org.jivesoftware.smackx.ox.element.OpenPgpElement) SigningOptions(org.pgpainless.encryption_signing.SigningOptions) ByteArrayOutputStream(java.io.ByteArrayOutputStream) EncryptionStream(org.pgpainless.encryption_signing.EncryptionStream)

Example 4 with OpenPgpElement

use of org.jivesoftware.smackx.ox.element.OpenPgpElement in project Smack by igniterealtime.

the class OpenPgpElementTest method providerTest.

@Test
public void providerTest() throws Exception {
    String expected = "<openpgp xmlns='urn:xmpp:openpgp:0'>" + "BASE64_OPENPGP_MESSAGE" + "</openpgp>";
    OpenPgpElement element = new OpenPgpElement("BASE64_OPENPGP_MESSAGE");
    assertXmlSimilar(expected, element.toXML().toString());
    XmlPullParser parser = TestUtils.getParser(expected);
    OpenPgpElement parsed = OpenPgpElementProvider.TEST_INSTANCE.parse(parser);
    assertEquals(element.getEncryptedBase64MessageContent(), parsed.getEncryptedBase64MessageContent());
}
Also used : OpenPgpElement(org.jivesoftware.smackx.ox.element.OpenPgpElement) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) Test(org.junit.jupiter.api.Test)

Aggregations

OpenPgpElement (org.jivesoftware.smackx.ox.element.OpenPgpElement)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 InputStream (java.io.InputStream)3 EncryptionStream (org.pgpainless.encryption_signing.EncryptionStream)3 PGPPublicKeyRingCollection (org.bouncycastle.openpgp.PGPPublicKeyRingCollection)2 OpenPgpContact (org.jivesoftware.smackx.ox.OpenPgpContact)2 EncryptionOptions (org.pgpainless.encryption_signing.EncryptionOptions)2 SigningOptions (org.pgpainless.encryption_signing.SigningOptions)2 XmlPullParser (org.jivesoftware.smack.xml.XmlPullParser)1 Test (org.junit.jupiter.api.Test)1