1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-09-28 10:39:33 +02:00

Improve OpenPgpV4Fingerprint and decryption API

This commit is contained in:
Paul Schaub 2018-07-08 18:14:36 +02:00
parent 626c4b14eb
commit 88d59d04d7
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
3 changed files with 36 additions and 11 deletions

View file

@ -25,6 +25,7 @@ import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKeyRing; import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection; import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
import org.pgpainless.pgpainless.key.OpenPgpV4Fingerprint;
import org.pgpainless.pgpainless.key.protection.SecretKeyRingProtector; import org.pgpainless.pgpainless.key.protection.SecretKeyRingProtector;
public class DecryptionBuilder implements DecryptionBuilderInterface { public class DecryptionBuilder implements DecryptionBuilderInterface {
@ -61,12 +62,18 @@ public class DecryptionBuilder implements DecryptionBuilderInterface {
class VerifyWithImpl implements VerifyWith { class VerifyWithImpl implements VerifyWith {
@Override @Override
public HandleMissingPublicKeys verifyWith(Set<Long> trustedKeyIds, public HandleMissingPublicKeys verifyWith(Set<OpenPgpV4Fingerprint> trustedKeyIds,
PGPPublicKeyRingCollection publicKeyRingCollection) { PGPPublicKeyRingCollection publicKeyRingCollection) {
Set<PGPPublicKeyRing> publicKeyRings = new HashSet<>(); Set<PGPPublicKeyRing> publicKeyRings = new HashSet<>();
for (Iterator<PGPPublicKeyRing> i = publicKeyRingCollection.getKeyRings(); i.hasNext(); ) { for (Iterator<PGPPublicKeyRing> i = publicKeyRingCollection.getKeyRings(); i.hasNext(); ) {
PGPPublicKeyRing p = i.next(); PGPPublicKeyRing p = i.next();
if (trustedKeyIds.contains(p.getPublicKey().getKeyID())) { OpenPgpV4Fingerprint fingerprint;
try {
fingerprint = new OpenPgpV4Fingerprint(p);
} catch (PGPException e) {
throw new IllegalArgumentException(e);
}
if (trustedKeyIds.contains(fingerprint)) {
publicKeyRings.add(p); publicKeyRings.add(p);
} }
} }
@ -89,7 +96,7 @@ public class DecryptionBuilder implements DecryptionBuilderInterface {
class HandleMissingPublicKeysImpl implements HandleMissingPublicKeys { class HandleMissingPublicKeysImpl implements HandleMissingPublicKeys {
@Override @Override
public Build handleMissingPublicKeysWith(org.pgpainless.pgpainless.decryption_verification.MissingPublicKeyCallback callback) { public Build handleMissingPublicKeysWith(MissingPublicKeyCallback callback) {
DecryptionBuilder.this.missingPublicKeyCallback = callback; DecryptionBuilder.this.missingPublicKeyCallback = callback;
return new BuildImpl(); return new BuildImpl();
} }

View file

@ -23,6 +23,7 @@ import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKeyRing; import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection; import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
import org.pgpainless.pgpainless.key.OpenPgpV4Fingerprint;
import org.pgpainless.pgpainless.key.protection.SecretKeyRingProtector; import org.pgpainless.pgpainless.key.protection.SecretKeyRingProtector;
public interface DecryptionBuilderInterface { public interface DecryptionBuilderInterface {
@ -39,7 +40,7 @@ public interface DecryptionBuilderInterface {
interface VerifyWith { interface VerifyWith {
HandleMissingPublicKeys verifyWith(Set<Long> trustedFingerprints, PGPPublicKeyRingCollection publicKeyRings); HandleMissingPublicKeys verifyWith(Set<OpenPgpV4Fingerprint> trustedFingerprints, PGPPublicKeyRingCollection publicKeyRings);
HandleMissingPublicKeys verifyWith(Set<PGPPublicKeyRing> publicKeyRings); HandleMissingPublicKeys verifyWith(Set<PGPPublicKeyRing> publicKeyRings);
@ -49,7 +50,7 @@ public interface DecryptionBuilderInterface {
interface HandleMissingPublicKeys { interface HandleMissingPublicKeys {
Build handleMissingPublicKeysWith(org.pgpainless.pgpainless.decryption_verification.MissingPublicKeyCallback callback); Build handleMissingPublicKeysWith(MissingPublicKeyCallback callback);
Build ignoreMissingPublicKeys(); Build ignoreMissingPublicKeys();
} }

View file

@ -14,17 +14,18 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.jivesoftware.smackx.ox; package org.pgpainless.pgpainless.key;
import javax.xml.bind.DatatypeConverter; import javax.xml.bind.DatatypeConverter;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.Arrays; import java.util.Arrays;
import org.jivesoftware.smack.util.Objects;
import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKey; import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.util.encoders.Hex; import org.bouncycastle.util.encoders.Hex;
/** /**
@ -41,9 +42,10 @@ public class OpenPgpV4Fingerprint implements CharSequence, Comparable<OpenPgpV4F
* @param fingerprint hexadecimal representation of the fingerprint. * @param fingerprint hexadecimal representation of the fingerprint.
*/ */
public OpenPgpV4Fingerprint(String fingerprint) throws PGPException { public OpenPgpV4Fingerprint(String fingerprint) throws PGPException {
String fp = Objects.requireNonNull(fingerprint) if (fingerprint == null) {
.trim() throw new NullPointerException("Fingerprint MUST NOT be null.");
.toUpperCase(); }
String fp = fingerprint.trim().toUpperCase();
if (!isValid(fp)) { if (!isValid(fp)) {
throw new PGPException("Fingerprint " + fingerprint + throw new PGPException("Fingerprint " + fingerprint +
" does not appear to be a valid OpenPGP v4 fingerprint."); " does not appear to be a valid OpenPGP v4 fingerprint.");
@ -57,6 +59,21 @@ public class OpenPgpV4Fingerprint implements CharSequence, Comparable<OpenPgpV4F
public OpenPgpV4Fingerprint(PGPPublicKey key) throws PGPException { public OpenPgpV4Fingerprint(PGPPublicKey key) throws PGPException {
this(Hex.encode(key.getFingerprint())); this(Hex.encode(key.getFingerprint()));
if (key.getVersion() != 4) {
throw new PGPException("Key is not a v4 OpenPgp key.");
}
}
public OpenPgpV4Fingerprint(PGPSecretKey key) throws PGPException {
this(key.getPublicKey());
}
public OpenPgpV4Fingerprint(PGPPublicKeyRing ring) throws PGPException {
this(ring.getPublicKey());
}
public OpenPgpV4Fingerprint(PGPSecretKeyRing ring) throws PGPException {
this(ring.getPublicKey());
} }
/** /**