From 319f65ea15345ec47c21f55a128a8313e93e7195 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Mon, 9 Jul 2018 16:52:38 +0200 Subject: [PATCH] Fix checkstyle issues and android compatibility --- .../ox/crypto/PainlessOpenPgpProvider.java | 4 ++-- .../smackx/ox/crypto/package-info.java | 20 ++++++++++++++++++ .../ox/selection_strategy/AnnouncedKeys.java | 21 ++----------------- .../FileBasedOpenPgpMetadataStore.java | 17 ++++++++++----- .../filebased/FileBasedOpenPgpTrustStore.java | 15 ++++++++++--- 5 files changed, 48 insertions(+), 29 deletions(-) create mode 100644 smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/crypto/package-info.java diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/crypto/PainlessOpenPgpProvider.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/crypto/PainlessOpenPgpProvider.java index 3f96e9db8..f52276dab 100644 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/crypto/PainlessOpenPgpProvider.java +++ b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/crypto/PainlessOpenPgpProvider.java @@ -65,7 +65,7 @@ public class PainlessOpenPgpProvider implements OpenPgpProvider { } EncryptionStream cipherStream = PGPainless.createEncryptor().onOutputStream(cipherText) - .toRecipients(recipientKeys.toArray(new PGPPublicKeyRingCollection[]{})) + .toRecipients(recipientKeys.toArray(new PGPPublicKeyRingCollection[] {})) .andToSelf(self.getAnnouncedPublicKeys()) .usingSecureAlgorithms() .signWith(store.getKeyRingProtector(), self.getSigningKeyRing()) @@ -115,7 +115,7 @@ public class PainlessOpenPgpProvider implements OpenPgpProvider { } EncryptionStream cipherStream = PGPainless.createEncryptor().onOutputStream(cipherText) - .toRecipients(recipientKeys.toArray(new PGPPublicKeyRingCollection[]{})) + .toRecipients(recipientKeys.toArray(new PGPPublicKeyRingCollection[] {})) .andToSelf(self.getAnnouncedPublicKeys()) .usingSecureAlgorithms() .doNotSign() diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/crypto/package-info.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/crypto/package-info.java new file mode 100644 index 000000000..dadb473bf --- /dev/null +++ b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/crypto/package-info.java @@ -0,0 +1,20 @@ +/** + * + * 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. + */ +/** + * Crypto Providers for XEP-0373: OpenPGP for XMPP using Bouncycastle. + */ +package org.jivesoftware.smackx.ox.crypto; diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/selection_strategy/AnnouncedKeys.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/selection_strategy/AnnouncedKeys.java index 9c405b3ef..ea42ed1e7 100644 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/selection_strategy/AnnouncedKeys.java +++ b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/selection_strategy/AnnouncedKeys.java @@ -18,10 +18,7 @@ package org.jivesoftware.smackx.ox.selection_strategy; import java.util.Date; import java.util.Map; -import java.util.logging.Level; -import java.util.logging.Logger; -import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPPublicKeyRing; import org.bouncycastle.openpgp.PGPSecretKeyRing; import org.pgpainless.pgpainless.key.OpenPgpV4Fingerprint; @@ -32,31 +29,17 @@ public class AnnouncedKeys { public static class PubKeyRingSelectionStrategy extends PublicKeyRingSelectionStrategy> { - private static final Logger LOGGER = Logger.getLogger(PubKeyRingSelectionStrategy.class.getName()); - @Override public boolean accept(Map announcedKeys, PGPPublicKeyRing publicKeys) { - try { - return announcedKeys.keySet().contains(new OpenPgpV4Fingerprint(publicKeys)); - } catch (PGPException e) { - LOGGER.log(Level.WARNING, "Key might be damaged.", e); - return false; - } + return announcedKeys.keySet().contains(new OpenPgpV4Fingerprint(publicKeys)); } } public static class SecKeyRingSelectionStrategy extends SecretKeyRingSelectionStrategy> { - private static final Logger LOGGER = Logger.getLogger(SecKeyRingSelectionStrategy.class.getName()); - @Override public boolean accept(Map announcedKeys, PGPSecretKeyRing secretKeys) { - try { - return announcedKeys.keySet().contains(new OpenPgpV4Fingerprint(secretKeys)); - } catch (PGPException e) { - LOGGER.log(Level.WARNING, "Key might be damaged.", e); - return false; - } + return announcedKeys.keySet().contains(new OpenPgpV4Fingerprint(secretKeys)); } } } diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/filebased/FileBasedOpenPgpMetadataStore.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/filebased/FileBasedOpenPgpMetadataStore.java index f41cb8875..369d68616 100644 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/filebased/FileBasedOpenPgpMetadataStore.java +++ b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/filebased/FileBasedOpenPgpMetadataStore.java @@ -20,7 +20,10 @@ import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.IOException; -import java.nio.file.Files; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; import java.text.ParseException; import java.util.Date; import java.util.HashMap; @@ -29,9 +32,9 @@ import java.util.logging.Level; import java.util.logging.Logger; import org.jivesoftware.smackx.ox.store.abstr.AbstractOpenPgpMetadataStore; +import org.jivesoftware.smackx.ox.util.FileUtils; import org.jivesoftware.smackx.ox.util.Util; -import org.bouncycastle.openpgp.PGPException; import org.jxmpp.jid.BareJid; import org.jxmpp.util.XmppDateTime; import org.pgpainless.pgpainless.key.OpenPgpV4Fingerprint; @@ -68,7 +71,9 @@ public class FileBasedOpenPgpMetadataStore extends AbstractOpenPgpMetadataStore BufferedReader reader = null; try { - reader = Files.newBufferedReader(source.toPath(), Util.UTF8); + InputStream inputStream = FileUtils.prepareFileInputStream(source); + InputStreamReader isr = new InputStreamReader(inputStream, Util.UTF8); + reader = new BufferedReader(isr); Map fingerprintDateMap = new HashMap<>(); String line; int lineNr = 0; @@ -86,7 +91,7 @@ public class FileBasedOpenPgpMetadataStore extends AbstractOpenPgpMetadataStore OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(split[0]); Date date = XmppDateTime.parseXEP0082Date(split[1]); fingerprintDateMap.put(fingerprint, date); - } catch (PGPException | ParseException e) { + } catch (IllegalArgumentException | ParseException e) { LOGGER.log(Level.WARNING, "Error parsing fingerprint/date touple in line " + lineNr + " of file " + source.getAbsolutePath(), e); } @@ -127,7 +132,9 @@ public class FileBasedOpenPgpMetadataStore extends AbstractOpenPgpMetadataStore BufferedWriter writer = null; try { - writer = Files.newBufferedWriter(destination.toPath(), Util.UTF8); + OutputStream outputStream = FileUtils.prepareFileOutputStream(destination); + OutputStreamWriter osw = new OutputStreamWriter(outputStream, Util.UTF8); + writer = new BufferedWriter(osw); for (OpenPgpV4Fingerprint fingerprint : data.keySet()) { Date date = data.get(fingerprint); String line = fingerprint.toString() + " " + diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/filebased/FileBasedOpenPgpTrustStore.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/filebased/FileBasedOpenPgpTrustStore.java index f66922a82..fb3680553 100644 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/filebased/FileBasedOpenPgpTrustStore.java +++ b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/store/filebased/FileBasedOpenPgpTrustStore.java @@ -21,11 +21,15 @@ import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; -import java.nio.file.Files; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; import java.util.logging.Level; import java.util.logging.Logger; import org.jivesoftware.smackx.ox.store.abstr.AbstractOpenPgpTrustStore; +import org.jivesoftware.smackx.ox.util.FileUtils; import org.jivesoftware.smackx.ox.util.Util; import org.jxmpp.jid.BareJid; @@ -50,7 +54,9 @@ public class FileBasedOpenPgpTrustStore extends AbstractOpenPgpTrustStore { File file = getTrustPath(owner, fingerprint); BufferedReader reader = null; try { - reader = Files.newBufferedReader(file.toPath(), Util.UTF8); + InputStream inputStream = FileUtils.prepareFileInputStream(file); + InputStreamReader isr = new InputStreamReader(inputStream, Util.UTF8); + reader = new BufferedReader(isr); Trust trust = null; String line; int lineNr = 0; @@ -98,7 +104,10 @@ public class FileBasedOpenPgpTrustStore extends AbstractOpenPgpTrustStore { BufferedWriter writer = null; try { - writer = Files.newBufferedWriter(file.toPath(), Util.UTF8); + OutputStream outputStream = FileUtils.prepareFileOutputStream(file); + OutputStreamWriter osw = new OutputStreamWriter(outputStream, Util.UTF8); + writer = new BufferedWriter(osw); + writer.write(trust.toString()); writer.flush(); writer.close();