mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-11-23 04:42:06 +01:00
Improve OpenPgpV4Fingerprint and decryption API
This commit is contained in:
parent
626c4b14eb
commit
88d59d04d7
3 changed files with 36 additions and 11 deletions
|
@ -25,6 +25,7 @@ import org.bouncycastle.openpgp.PGPException;
|
|||
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
||||
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
|
||||
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
|
||||
import org.pgpainless.pgpainless.key.OpenPgpV4Fingerprint;
|
||||
import org.pgpainless.pgpainless.key.protection.SecretKeyRingProtector;
|
||||
|
||||
public class DecryptionBuilder implements DecryptionBuilderInterface {
|
||||
|
@ -61,12 +62,18 @@ public class DecryptionBuilder implements DecryptionBuilderInterface {
|
|||
class VerifyWithImpl implements VerifyWith {
|
||||
|
||||
@Override
|
||||
public HandleMissingPublicKeys verifyWith(Set<Long> trustedKeyIds,
|
||||
public HandleMissingPublicKeys verifyWith(Set<OpenPgpV4Fingerprint> trustedKeyIds,
|
||||
PGPPublicKeyRingCollection publicKeyRingCollection) {
|
||||
Set<PGPPublicKeyRing> publicKeyRings = new HashSet<>();
|
||||
for (Iterator<PGPPublicKeyRing> i = publicKeyRingCollection.getKeyRings(); i.hasNext(); ) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -89,7 +96,7 @@ public class DecryptionBuilder implements DecryptionBuilderInterface {
|
|||
class HandleMissingPublicKeysImpl implements HandleMissingPublicKeys {
|
||||
|
||||
@Override
|
||||
public Build handleMissingPublicKeysWith(org.pgpainless.pgpainless.decryption_verification.MissingPublicKeyCallback callback) {
|
||||
public Build handleMissingPublicKeysWith(MissingPublicKeyCallback callback) {
|
||||
DecryptionBuilder.this.missingPublicKeyCallback = callback;
|
||||
return new BuildImpl();
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@ import org.bouncycastle.openpgp.PGPException;
|
|||
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
||||
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
|
||||
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
|
||||
import org.pgpainless.pgpainless.key.OpenPgpV4Fingerprint;
|
||||
import org.pgpainless.pgpainless.key.protection.SecretKeyRingProtector;
|
||||
|
||||
public interface DecryptionBuilderInterface {
|
||||
|
@ -39,7 +40,7 @@ public interface DecryptionBuilderInterface {
|
|||
|
||||
interface VerifyWith {
|
||||
|
||||
HandleMissingPublicKeys verifyWith(Set<Long> trustedFingerprints, PGPPublicKeyRingCollection publicKeyRings);
|
||||
HandleMissingPublicKeys verifyWith(Set<OpenPgpV4Fingerprint> trustedFingerprints, PGPPublicKeyRingCollection publicKeyRings);
|
||||
|
||||
HandleMissingPublicKeys verifyWith(Set<PGPPublicKeyRing> publicKeyRings);
|
||||
|
||||
|
@ -49,7 +50,7 @@ public interface DecryptionBuilderInterface {
|
|||
|
||||
interface HandleMissingPublicKeys {
|
||||
|
||||
Build handleMissingPublicKeysWith(org.pgpainless.pgpainless.decryption_verification.MissingPublicKeyCallback callback);
|
||||
Build handleMissingPublicKeysWith(MissingPublicKeyCallback callback);
|
||||
|
||||
Build ignoreMissingPublicKeys();
|
||||
}
|
||||
|
|
|
@ -14,17 +14,18 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smackx.ox;
|
||||
package org.pgpainless.pgpainless.key;
|
||||
|
||||
import javax.xml.bind.DatatypeConverter;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.jivesoftware.smack.util.Objects;
|
||||
|
||||
import org.bouncycastle.openpgp.PGPException;
|
||||
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;
|
||||
|
||||
/**
|
||||
|
@ -41,9 +42,10 @@ public class OpenPgpV4Fingerprint implements CharSequence, Comparable<OpenPgpV4F
|
|||
* @param fingerprint hexadecimal representation of the fingerprint.
|
||||
*/
|
||||
public OpenPgpV4Fingerprint(String fingerprint) throws PGPException {
|
||||
String fp = Objects.requireNonNull(fingerprint)
|
||||
.trim()
|
||||
.toUpperCase();
|
||||
if (fingerprint == null) {
|
||||
throw new NullPointerException("Fingerprint MUST NOT be null.");
|
||||
}
|
||||
String fp = fingerprint.trim().toUpperCase();
|
||||
if (!isValid(fp)) {
|
||||
throw new PGPException("Fingerprint " + 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 {
|
||||
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());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue