Skip to content

Commit

Permalink
Address sonar cloud issues
Browse files Browse the repository at this point in the history
  • Loading branch information
TharmiganK committed Oct 9, 2024
1 parent 9d529a4 commit 6902dbe
Showing 1 changed file with 15 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ private Optional<PGPPrivateKey> findSecretKey(long keyID) throws PGPException {

private void decryptStream(InputStream encryptedIn, OutputStream clearOut)
throws PGPException, IOException {
KeyEncryptedResult keyEncryptedResult = getKeyEncryptedResult(encryptedIn);
decrypt(clearOut, keyEncryptedResult.pgpPrivateKey(), keyEncryptedResult.publicKeyEncryptedData());
}

private KeyEncryptedResult getKeyEncryptedResult(InputStream encryptedIn) throws IOException, PGPException {
// Remove armour and return the underlying binary encrypted stream
encryptedIn = PGPUtil.getDecoderStream(encryptedIn);
JcaPGPObjectFactory pgpObjectFactory = new JcaPGPObjectFactory(encryptedIn);
Expand All @@ -116,36 +121,15 @@ private void decryptStream(InputStream encryptedIn, OutputStream clearOut)
if (pgpPrivateKey.isEmpty()) {
throw new PGPException("Could not Extract private key");
}
decrypt(clearOut, pgpPrivateKey.get(), publicKeyEncryptedData);
return new KeyEncryptedResult(pgpPrivateKey.get(), publicKeyEncryptedData);
}

public void decryptStream(InputStream encryptedIn, BObject iteratorObj) throws PGPException, IOException {
// Remove armour and return the underlying binary encrypted stream
encryptedIn = PGPUtil.getDecoderStream(encryptedIn);
JcaPGPObjectFactory pgpObjectFactory = new JcaPGPObjectFactory(encryptedIn);

Object obj = pgpObjectFactory.nextObject();
// Verify the marker packet
PGPEncryptedDataList pgpEncryptedDataList = (obj instanceof PGPEncryptedDataList)
? (PGPEncryptedDataList) obj : (PGPEncryptedDataList) pgpObjectFactory.nextObject();

Optional<PGPPrivateKey> pgpPrivateKey = Optional.empty();
PGPPublicKeyEncryptedData publicKeyEncryptedData = null;

Iterator<PGPEncryptedData> encryptedDataItr = pgpEncryptedDataList.getEncryptedDataObjects();
while (pgpPrivateKey.isEmpty() && encryptedDataItr.hasNext()) {
publicKeyEncryptedData = (PGPPublicKeyEncryptedData) encryptedDataItr.next();
pgpPrivateKey = findSecretKey(publicKeyEncryptedData.getKeyID());
}

if (Objects.isNull(publicKeyEncryptedData)) {
throw new PGPException("Could not generate PGPPublicKeyEncryptedData object");
}
private record KeyEncryptedResult(PGPPrivateKey pgpPrivateKey, PGPPublicKeyEncryptedData publicKeyEncryptedData) {
}

if (pgpPrivateKey.isEmpty()) {
throw new PGPException("Could not Extract private key");
}
decrypt(pgpPrivateKey.get(), publicKeyEncryptedData, iteratorObj);
public void decryptStream(InputStream encryptedIn, BObject iteratorObj) throws PGPException, IOException {
KeyEncryptedResult keyEncryptedResult = getKeyEncryptedResult(encryptedIn);
decrypt(keyEncryptedResult.pgpPrivateKey, keyEncryptedResult.publicKeyEncryptedData, iteratorObj);
}

// Decrypts the given byte array of encrypted data using PGP decryption.
Expand Down Expand Up @@ -187,11 +171,10 @@ private static void decrypt(OutputStream clearOut, PGPPrivateKey pgpPrivateKey,
}
}
// Perform the integrity check
if (publicKeyEncryptedData.isIntegrityProtected()) {
if (!publicKeyEncryptedData.verify()) {
if (publicKeyEncryptedData.isIntegrityProtected() && !publicKeyEncryptedData.verify()) {
throw new PGPException("Message failed integrity check");

Check warning on line 175 in native/src/main/java/io/ballerina/stdlib/crypto/PgpDecryptionGenerator.java

View check run for this annotation

Codecov / codecov/patch

native/src/main/java/io/ballerina/stdlib/crypto/PgpDecryptionGenerator.java#L175

Added line #L175 was not covered by tests
}
}

}

private static void decrypt(PGPPrivateKey pgpPrivateKey, PGPPublicKeyEncryptedData publicKeyEncryptedData,
Expand All @@ -209,11 +192,10 @@ private static void decrypt(PGPPrivateKey pgpPrivateKey, PGPPublicKeyEncryptedDa

if (message instanceof PGPLiteralData pgpLiteralData) {
// Perform the integrity check
if (publicKeyEncryptedData.isIntegrityProtected()) {
if (!publicKeyEncryptedData.verify()) {
if (publicKeyEncryptedData.isIntegrityProtected() && !publicKeyEncryptedData.verify()) {
throw new PGPException("Message failed integrity check");
}
}

iteratorObj.addNativeData(TARGET_STREAM, pgpLiteralData.getDataStream());
iteratorObj.addNativeData(COMPRESSED_DATA_STREAM, compressedDataStream);
iteratorObj.addNativeData(DATA_STREAM, decryptedCompressedIn);
Expand Down

0 comments on commit 6902dbe

Please sign in to comment.