mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-11-26 22:32:07 +01:00
Refactor SignatureVerifyingInputStream
This commit is contained in:
parent
ad070d0c34
commit
be33206d03
1 changed files with 52 additions and 33 deletions
|
@ -67,12 +67,9 @@ public class SignatureVerifyingInputStream extends FilterInputStream {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void validateOnePassSignatures() throws IOException {
|
private void validateOnePassSignatures() throws IOException {
|
||||||
|
|
||||||
if (validated) {
|
if (validated) {
|
||||||
LOGGER.log(LEVEL, "Validated signatures already. Skip");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
validated = true;
|
validated = true;
|
||||||
|
|
||||||
if (onePassSignatures.isEmpty()) {
|
if (onePassSignatures.isEmpty()) {
|
||||||
|
@ -80,14 +77,52 @@ public class SignatureVerifyingInputStream extends FilterInputStream {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PGPSignatureList signatureList = findPgpSignatureList();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
PGPSignatureList signatureList = null;
|
for (PGPSignature signature : signatureList) {
|
||||||
Object obj = objectFactory.nextObject();
|
resultBuilder.addSignature(signature);
|
||||||
while (obj != null && signatureList == null) {
|
|
||||||
if (obj instanceof PGPSignatureList) {
|
OpenPgpV4Fingerprint fingerprint = findFingerprintForSignature(signature);
|
||||||
signatureList = (PGPSignatureList) obj;
|
PGPOnePassSignature onePassSignature = findOnePassSignature(fingerprint);
|
||||||
|
if (onePassSignature == null) {
|
||||||
|
LOGGER.log(LEVEL, "Found Signature without respective OnePassSignature packet -> skip");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
verifySignatureOrThrowSignatureException(signature, fingerprint, onePassSignature);
|
||||||
|
}
|
||||||
|
} catch (PGPException | SignatureException e) {
|
||||||
|
throw new IOException(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void verifySignatureOrThrowSignatureException(PGPSignature signature, OpenPgpV4Fingerprint fingerprint, PGPOnePassSignature onePassSignature) throws PGPException, SignatureException {
|
||||||
|
if (onePassSignature.verify(signature)) {
|
||||||
|
LOGGER.log(LEVEL, "Verified signature of key " + Long.toHexString(signature.getKeyID()));
|
||||||
|
resultBuilder.putVerifiedSignature(fingerprint, signature);
|
||||||
|
resultBuilder.addVerifiedSignatureFingerprint(fingerprint);
|
||||||
} else {
|
} else {
|
||||||
obj = objectFactory.nextObject();
|
throw new SignatureException("Bad Signature of key " + signature.getKeyID());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private PGPOnePassSignature findOnePassSignature(OpenPgpV4Fingerprint fingerprint) {
|
||||||
|
if (fingerprint != null) {
|
||||||
|
return onePassSignatures.get(fingerprint);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private PGPSignatureList findPgpSignatureList() throws IOException {
|
||||||
|
PGPSignatureList signatureList = null;
|
||||||
|
Object pgpObject = objectFactory.nextObject();
|
||||||
|
while (pgpObject != null && signatureList == null) {
|
||||||
|
if (pgpObject instanceof PGPSignatureList) {
|
||||||
|
signatureList = (PGPSignatureList) pgpObject;
|
||||||
|
} else {
|
||||||
|
pgpObject = objectFactory.nextObject();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,8 +130,10 @@ public class SignatureVerifyingInputStream extends FilterInputStream {
|
||||||
throw new IOException("Verification failed - No Signatures found");
|
throw new IOException("Verification failed - No Signatures found");
|
||||||
}
|
}
|
||||||
|
|
||||||
for (PGPSignature signature : signatureList) {
|
return signatureList;
|
||||||
resultBuilder.addSignature(signature);
|
}
|
||||||
|
|
||||||
|
private OpenPgpV4Fingerprint findFingerprintForSignature(PGPSignature signature) {
|
||||||
OpenPgpV4Fingerprint fingerprint = null;
|
OpenPgpV4Fingerprint fingerprint = null;
|
||||||
for (OpenPgpV4Fingerprint f : onePassSignatures.keySet()) {
|
for (OpenPgpV4Fingerprint f : onePassSignatures.keySet()) {
|
||||||
if (f.getKeyId() == signature.getKeyID()) {
|
if (f.getKeyId() == signature.getKeyID()) {
|
||||||
|
@ -104,25 +141,7 @@ public class SignatureVerifyingInputStream extends FilterInputStream {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return fingerprint;
|
||||||
PGPOnePassSignature onePassSignature;
|
|
||||||
if (fingerprint == null || (onePassSignature = onePassSignatures.get(fingerprint)) == null) {
|
|
||||||
LOGGER.log(LEVEL, "Found Signature without respective OnePassSignature packet -> skip");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!onePassSignature.verify(signature)) {
|
|
||||||
throw new SignatureException("Bad Signature of key " + signature.getKeyID());
|
|
||||||
} else {
|
|
||||||
LOGGER.log(LEVEL, "Verified signature of key " + Long.toHexString(signature.getKeyID()));
|
|
||||||
resultBuilder.putVerifiedSignature(fingerprint, signature);
|
|
||||||
resultBuilder.addVerifiedSignatureFingerprint(fingerprint);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (PGPException | SignatureException e) {
|
|
||||||
throw new IOException(e.getMessage(), e);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in a new issue