use of org.bouncycastle.openpgp.PGPException in project bisq-desktop by bisq-network.
the class BisqInstaller method verifySignature.
/**
* Verifies detached PGP signatures against GPG/openPGP RSA public keys. Does currently not work with openssl or JCA/JCE keys.
*
* @param pubKeyFile Path to file providing the public key to use
* @param sigFile Path to detached signature file
* @param dataFile Path to signed data file
* @return {@code true} if signature is valid, {@code false} if signature is not valid
* @throws Exception throws various exceptions in case something went wrong. Main reason should be that key or
* signature could be extracted from the provided files due to a "bad" format.<br>
* <code>FileNotFoundException, IOException, SignatureException, PGPException</code>
*/
public static VerifyStatusEnum verifySignature(File pubKeyFile, File sigFile, File dataFile) throws Exception {
InputStream inputStream;
int bytesRead;
PGPPublicKey publicKey;
PGPSignature pgpSignature;
boolean result;
// Read keys from file
inputStream = PGPUtil.getDecoderStream(new FileInputStream(pubKeyFile));
PGPPublicKeyRingCollection publicKeyRingCollection = new PGPPublicKeyRingCollection(inputStream, new JcaKeyFingerprintCalculator());
inputStream.close();
Iterator<PGPPublicKeyRing> iterator = publicKeyRingCollection.getKeyRings();
PGPPublicKeyRing pgpPublicKeyRing;
if (iterator.hasNext()) {
pgpPublicKeyRing = iterator.next();
} else {
throw new PGPException("Could not find public keyring in provided key file");
}
// Would be the solution for multiple keys in one file
// Iterator<PGPPublicKey> kIt;
// kIt = pgpPublicKeyRing.getPublicKeys();
// publicKey = pgpPublicKeyRing.getPublicKey(0xF5B84436F379A1C6L);
// Read signature from file
inputStream = PGPUtil.getDecoderStream(new FileInputStream(sigFile));
PGPObjectFactory pgpObjectFactory = new PGPObjectFactory(inputStream, new JcaKeyFingerprintCalculator());
Object o = pgpObjectFactory.nextObject();
if (o instanceof PGPSignatureList) {
PGPSignatureList signatureList = (PGPSignatureList) o;
checkArgument(!signatureList.isEmpty(), "signatureList must not be empty");
pgpSignature = signatureList.get(0);
} else if (o instanceof PGPSignature) {
pgpSignature = (PGPSignature) o;
} else {
throw new SignatureException("Could not find signature in provided signature file");
}
inputStream.close();
log.debug("KeyID used in signature: %X\n", pgpSignature.getKeyID());
publicKey = pgpPublicKeyRing.getPublicKey(pgpSignature.getKeyID());
// If signature is not matching the key used for signing we fail
if (publicKey == null)
return VerifyStatusEnum.FAIL;
log.debug("The ID of the selected key is %X\n", publicKey.getKeyID());
pgpSignature.init(new BcPGPContentVerifierBuilderProvider(), publicKey);
// Read file to verify
byte[] data = new byte[1024];
inputStream = new DataInputStream(new BufferedInputStream(new FileInputStream(dataFile)));
while (true) {
bytesRead = inputStream.read(data, 0, 1024);
if (bytesRead == -1)
break;
pgpSignature.update(data, 0, bytesRead);
}
inputStream.close();
// Verify the signature
result = pgpSignature.verify();
return result ? VerifyStatusEnum.OK : VerifyStatusEnum.FAIL;
}
use of org.bouncycastle.openpgp.PGPException in project gradle by gradle.
the class PgpSignatory method createSignatureGenerator.
public PGPSignatureGenerator createSignatureGenerator() {
try {
PGPSignatureGenerator generator = new PGPSignatureGenerator(new BcPGPContentSignerBuilder(secretKey.getPublicKey().getAlgorithm(), PGPUtil.SHA512));
generator.init(PGPSignature.BINARY_DOCUMENT, privateKey);
return generator;
} catch (PGPException e) {
throw UncheckedException.throwAsUncheckedException(e);
}
}
use of org.bouncycastle.openpgp.PGPException in project gerrit by GerritCodeReview.
the class PushCertificateChecker method check.
/**
* Check a push certificate.
*
* @return result of the check.
*/
public final Result check(PushCertificate cert) {
if (checkNonce && cert.getNonceStatus() != NonceStatus.OK) {
return new Result(null, CheckResult.bad("Invalid nonce"));
}
List<CheckResult> results = new ArrayList<>(2);
Result sigResult = null;
try {
PGPSignature sig = readSignature(cert);
if (sig != null) {
@SuppressWarnings("resource") Repository repo = getRepository();
try (PublicKeyStore store = new PublicKeyStore(repo)) {
sigResult = checkSignature(sig, cert, store);
results.add(checkCustom(repo));
} finally {
if (shouldClose(repo)) {
repo.close();
}
}
} else {
results.add(CheckResult.bad("Invalid signature format"));
}
} catch (PGPException | IOException e) {
String msg = "Internal error checking push certificate";
logger.atSevere().withCause(e).log("%s", msg);
results.add(CheckResult.bad(msg));
}
return combine(sigResult, results);
}
use of org.bouncycastle.openpgp.PGPException in project gerrit by GerritCodeReview.
the class GpgApiAdapterImpl method putGpgKeys.
@Override
public Map<String, GpgKeyInfo> putGpgKeys(AccountResource account, List<String> add, List<String> delete) throws RestApiException, GpgException {
GpgKeysInput in = new GpgKeysInput();
in.add = add;
in.delete = delete;
try {
return postGpgKeys.get().apply(account, in).value();
} catch (PGPException | IOException | ConfigInvalidException e) {
throw new GpgException(e);
} catch (Exception e) {
throw asRestApiException("Cannot put GPG keys", e);
}
}
use of org.bouncycastle.openpgp.PGPException in project gerrit by GerritCodeReview.
the class PublicKeyChecker method getSigner.
private static PGPPublicKey getSigner(PublicKeyStore store, PGPSignature sig, String userId, PGPPublicKey key, List<CheckResult> results) {
try {
PGPPublicKeyRingCollection signers = store.get(sig.getKeyID());
if (!signers.getKeyRings().hasNext()) {
results.add(CheckResult.ok("Key " + keyIdToString(sig.getKeyID()) + " used for certification is not in store"));
return null;
}
PGPPublicKey signer = PublicKeyStore.getSigner(signers, sig, userId, key);
if (signer == null) {
results.add(CheckResult.ok("Certification by " + keyIdToString(sig.getKeyID()) + " is not valid"));
return null;
}
return signer;
} catch (PGPException | IOException e) {
results.add(CheckResult.ok("Error checking certification by " + keyIdToString(sig.getKeyID())));
return null;
}
}
Aggregations