2018-07-29 18:52:45 +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.crypto;
|
|
|
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
2021-05-02 18:44:25 +02:00
|
|
|
import org.jivesoftware.smack.SmackException.NoResponseException;
|
|
|
|
import org.jivesoftware.smack.SmackException.NotConnectedException;
|
2018-07-29 18:52:45 +02:00
|
|
|
import org.jivesoftware.smack.XMPPConnection;
|
2021-05-02 18:44:25 +02:00
|
|
|
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
|
2018-07-29 18:52:45 +02:00
|
|
|
import org.jivesoftware.smack.util.Objects;
|
|
|
|
import org.jivesoftware.smack.util.stringencoder.Base64;
|
2019-05-06 22:06:13 +02:00
|
|
|
|
2018-07-29 18:52:45 +02:00
|
|
|
import org.jivesoftware.smackx.ox.OpenPgpContact;
|
|
|
|
import org.jivesoftware.smackx.ox.OpenPgpMessage;
|
|
|
|
import org.jivesoftware.smackx.ox.OpenPgpSelf;
|
|
|
|
import org.jivesoftware.smackx.ox.element.CryptElement;
|
|
|
|
import org.jivesoftware.smackx.ox.element.OpenPgpElement;
|
|
|
|
import org.jivesoftware.smackx.ox.element.SignElement;
|
|
|
|
import org.jivesoftware.smackx.ox.element.SigncryptElement;
|
|
|
|
import org.jivesoftware.smackx.ox.store.definition.OpenPgpStore;
|
2021-05-02 18:44:25 +02:00
|
|
|
import org.jivesoftware.smackx.pubsub.PubSubException.NotALeafNodeException;
|
|
|
|
import org.jivesoftware.smackx.pubsub.PubSubException.NotAPubSubNodeException;
|
2018-07-29 18:52:45 +02:00
|
|
|
|
|
|
|
import org.bouncycastle.openpgp.PGPException;
|
2021-06-05 21:27:47 +02:00
|
|
|
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
2018-07-29 18:52:45 +02:00
|
|
|
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
|
|
|
|
import org.bouncycastle.util.io.Streams;
|
|
|
|
import org.pgpainless.PGPainless;
|
2021-06-05 21:27:47 +02:00
|
|
|
import org.pgpainless.algorithm.DocumentSignatureType;
|
2021-08-10 15:23:49 +02:00
|
|
|
import org.pgpainless.decryption_verification.ConsumerOptions;
|
2018-07-29 18:52:45 +02:00
|
|
|
import org.pgpainless.decryption_verification.DecryptionStream;
|
|
|
|
import org.pgpainless.decryption_verification.MissingPublicKeyCallback;
|
|
|
|
import org.pgpainless.decryption_verification.OpenPgpMetadata;
|
2021-06-05 21:27:47 +02:00
|
|
|
import org.pgpainless.encryption_signing.EncryptionOptions;
|
2018-07-29 18:52:45 +02:00
|
|
|
import org.pgpainless.encryption_signing.EncryptionStream;
|
2021-06-05 21:27:47 +02:00
|
|
|
import org.pgpainless.encryption_signing.ProducerOptions;
|
|
|
|
import org.pgpainless.encryption_signing.SigningOptions;
|
2018-07-29 18:52:45 +02:00
|
|
|
|
|
|
|
public class PainlessOpenPgpProvider implements OpenPgpProvider {
|
|
|
|
|
|
|
|
private static final Logger LOGGER = Logger.getLogger(PainlessOpenPgpProvider.class.getName());
|
|
|
|
|
|
|
|
private final OpenPgpStore store;
|
|
|
|
|
2020-06-15 14:02:18 +02:00
|
|
|
public PainlessOpenPgpProvider(OpenPgpStore store) {
|
2018-07-29 18:52:45 +02:00
|
|
|
this.store = Objects.requireNonNull(store);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public OpenPgpStore getStore() {
|
|
|
|
return store;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public OpenPgpElementAndMetadata signAndEncrypt(SigncryptElement element, OpenPgpSelf self, Collection<OpenPgpContact> recipients)
|
|
|
|
throws IOException, PGPException {
|
|
|
|
InputStream plainText = element.toInputStream();
|
|
|
|
ByteArrayOutputStream cipherText = new ByteArrayOutputStream();
|
|
|
|
|
2021-06-05 21:27:47 +02:00
|
|
|
EncryptionOptions encOpts = EncryptionOptions.encryptCommunications();
|
2018-07-29 18:52:45 +02:00
|
|
|
for (OpenPgpContact contact : recipients) {
|
|
|
|
PGPPublicKeyRingCollection keys = contact.getTrustedAnnouncedKeys();
|
2021-05-02 18:43:41 +02:00
|
|
|
if (keys == null) {
|
|
|
|
LOGGER.log(Level.WARNING, "There are no suitable keys for contact " + contact.getJid());
|
2018-07-29 18:52:45 +02:00
|
|
|
}
|
2021-06-05 21:27:47 +02:00
|
|
|
encOpts.addRecipients(keys);
|
2018-07-29 18:52:45 +02:00
|
|
|
}
|
|
|
|
|
2021-06-05 21:27:47 +02:00
|
|
|
encOpts.addRecipients(self.getTrustedAnnouncedKeys());
|
|
|
|
|
|
|
|
SigningOptions signOpts = new SigningOptions();
|
|
|
|
signOpts.addInlineSignature(getStore().getKeyRingProtector(), self.getSigningKeyRing(),
|
|
|
|
"xmpp:" + self.getJid().toString(), DocumentSignatureType.BINARY_DOCUMENT);
|
|
|
|
|
|
|
|
EncryptionStream cipherStream = PGPainless.encryptAndOrSign()
|
|
|
|
.onOutputStream(cipherText)
|
|
|
|
.withOptions(ProducerOptions
|
|
|
|
.signAndEncrypt(encOpts, signOpts)
|
|
|
|
.setAsciiArmor(false));
|
2018-07-29 18:52:45 +02:00
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public OpenPgpElementAndMetadata sign(SignElement element, OpenPgpSelf self)
|
|
|
|
throws IOException, PGPException {
|
|
|
|
InputStream plainText = element.toInputStream();
|
|
|
|
ByteArrayOutputStream cipherText = new ByteArrayOutputStream();
|
|
|
|
|
2021-06-05 21:27:47 +02:00
|
|
|
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));
|
2018-07-29 18:52:45 +02:00
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public OpenPgpElementAndMetadata encrypt(CryptElement element, OpenPgpSelf self, Collection<OpenPgpContact> recipients)
|
|
|
|
throws IOException, PGPException {
|
|
|
|
InputStream plainText = element.toInputStream();
|
|
|
|
ByteArrayOutputStream cipherText = new ByteArrayOutputStream();
|
|
|
|
|
2021-06-05 21:27:47 +02:00
|
|
|
EncryptionOptions encOpts = EncryptionOptions.encryptCommunications();
|
2018-07-29 18:52:45 +02:00
|
|
|
for (OpenPgpContact contact : recipients) {
|
|
|
|
PGPPublicKeyRingCollection keys = contact.getTrustedAnnouncedKeys();
|
2021-06-05 21:27:47 +02:00
|
|
|
if (keys == null) {
|
|
|
|
LOGGER.log(Level.WARNING, "There are no suitable keys for contact " + contact.getJid());
|
2018-07-29 18:52:45 +02:00
|
|
|
}
|
2021-06-05 21:27:47 +02:00
|
|
|
encOpts.addRecipients(keys);
|
2018-07-29 18:52:45 +02:00
|
|
|
}
|
|
|
|
|
2021-06-05 21:27:47 +02:00
|
|
|
encOpts.addRecipients(self.getTrustedAnnouncedKeys());
|
|
|
|
|
|
|
|
EncryptionStream cipherStream = PGPainless.encryptAndOrSign()
|
|
|
|
.onOutputStream(cipherText)
|
|
|
|
.withOptions(ProducerOptions
|
|
|
|
.encrypt(encOpts)
|
|
|
|
.setAsciiArmor(false)
|
|
|
|
);
|
2018-07-29 18:52:45 +02:00
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-06-15 14:02:18 +02:00
|
|
|
public OpenPgpMessage decryptAndOrVerify(XMPPConnection connection, OpenPgpElement element, final OpenPgpSelf self, final OpenPgpContact sender) throws IOException, PGPException {
|
2018-07-29 18:52:45 +02:00
|
|
|
ByteArrayOutputStream plainText = new ByteArrayOutputStream();
|
|
|
|
InputStream cipherText = element.toInputStream();
|
|
|
|
|
|
|
|
PGPPublicKeyRingCollection announcedPublicKeys = sender.getAnnouncedPublicKeys();
|
|
|
|
if (announcedPublicKeys == null) {
|
|
|
|
try {
|
|
|
|
sender.updateKeys(connection);
|
|
|
|
announcedPublicKeys = sender.getAnnouncedPublicKeys();
|
2021-05-02 18:44:25 +02:00
|
|
|
} catch (InterruptedException | NotALeafNodeException | NotAPubSubNodeException | NotConnectedException
|
2021-06-05 21:27:47 +02:00
|
|
|
| NoResponseException | XMPPErrorException e) {
|
2021-05-02 18:44:25 +02:00
|
|
|
throw new PGPException("Abort decryption due to lack of keys", e);
|
2018-07-29 18:52:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MissingPublicKeyCallback missingPublicKeyCallback = new MissingPublicKeyCallback() {
|
2021-06-05 21:27:47 +02:00
|
|
|
|
2018-07-29 18:52:45 +02:00
|
|
|
@Override
|
2021-06-05 21:27:47 +02:00
|
|
|
public PGPPublicKeyRing onMissingPublicKeyEncountered(Long keyId) {
|
2018-07-29 18:52:45 +02:00
|
|
|
try {
|
|
|
|
sender.updateKeys(connection);
|
2021-06-05 21:27:47 +02:00
|
|
|
PGPPublicKeyRingCollection anyKeys = sender.getAnyPublicKeys();
|
|
|
|
for (PGPPublicKeyRing ring : anyKeys) {
|
|
|
|
if (ring.getPublicKey(keyId) != null) {
|
|
|
|
return ring;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2021-05-02 18:44:25 +02:00
|
|
|
} catch (InterruptedException | NotALeafNodeException | NotAPubSubNodeException | NotConnectedException
|
2021-06-05 21:27:47 +02:00
|
|
|
| NoResponseException | XMPPErrorException | IOException | PGPException e) {
|
2018-07-29 18:52:45 +02:00
|
|
|
LOGGER.log(Level.WARNING, "Cannot fetch missing key " + keyId, e);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-06-05 21:27:47 +02:00
|
|
|
DecryptionStream cipherStream = PGPainless.decryptAndOrVerify()
|
|
|
|
.onInputStream(cipherText)
|
2021-08-10 15:23:49 +02:00
|
|
|
.withOptions(new ConsumerOptions()
|
|
|
|
.addDecryptionKeys(self.getSecretKeys(), getStore().getKeyRingProtector())
|
|
|
|
.addVerificationCerts(announcedPublicKeys)
|
|
|
|
.setMissingCertificateCallback(missingPublicKeyCallback));
|
2018-07-29 18:52:45 +02:00
|
|
|
|
|
|
|
Streams.pipeAll(cipherStream, plainText);
|
|
|
|
|
|
|
|
cipherText.close();
|
|
|
|
cipherStream.close();
|
|
|
|
plainText.close();
|
|
|
|
|
|
|
|
OpenPgpMetadata info = cipherStream.getResult();
|
|
|
|
|
|
|
|
OpenPgpMessage.State state;
|
|
|
|
if (info.isSigned()) {
|
|
|
|
if (info.isEncrypted()) {
|
|
|
|
state = OpenPgpMessage.State.signcrypt;
|
|
|
|
} else {
|
|
|
|
state = OpenPgpMessage.State.sign;
|
|
|
|
}
|
|
|
|
} else if (info.isEncrypted()) {
|
|
|
|
state = OpenPgpMessage.State.crypt;
|
|
|
|
} else {
|
|
|
|
throw new PGPException("Received message appears to be neither encrypted, nor signed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return new OpenPgpMessage(plainText.toByteArray(), state, info);
|
|
|
|
}
|
|
|
|
}
|