Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

fix null pointer exception when using public only keys (#3143) #3144

Merged
merged 1 commit into from
Jul 30, 2024

Conversation

gregrenda
Copy link
Contributor

Don't call PGPSecretKeyRingCollection for public only keys

@msfjarvis msfjarvis added C-bug Category: This is a bug P-high Priority: high, must be resolved before next major release S-waiting-on-author Status: This PR is incomplete or needs to address review comments A-PGPainless Area: PGPainless-backed PGP labels Jul 30, 2024
@msfjarvis msfjarvis added this to the v2.0.0 milestone Jul 30, 2024
@msfjarvis msfjarvis linked an issue Jul 30, 2024 that may be closed by this pull request
@msfjarvis msfjarvis self-assigned this Jul 30, 2024
@msfjarvis
Copy link
Member

I had some time so I tried to adapt an existing test to reproduce this but couldn't get it to fail without your fix, feel free to build upon this or write your own test. It should fail on develop and pass on this branch.

  @Ignore("Should fail when decrypting but completes successfully")
  @Test
  fun decryptWithPublicKeys() {
    val aliceSecKeyRing =
      PGPainless.generateKeyRing().modernKeyRing("Alice <[email protected]>", KEY_PASSPHRASE)
    val bobSecKeyRing = PGPainless.generateKeyRing().modernKeyRing("Bob <[email protected]>", KEY_PASSPHRASE)
    val aliceCert = PGPainless.extractCertificate(aliceSecKeyRing)
    val aliceKey = PGPKey(PGPainless.asciiArmor(aliceCert).encodeToByteArray())
    val bobKey = PGPKey(PGPainless.asciiArmor(bobSecKeyRing).encodeToByteArray())

    val ciphertextStream = ByteArrayOutputStream()
    val encryptRes =
      cryptoHandler.encrypt(
        listOf(aliceKey, bobKey),
        PLAIN_TEXT.byteInputStream(Charsets.UTF_8),
        ciphertextStream,
        PGPEncryptOptions.Builder().withAsciiArmor(true).build(),
      )
    assertTrue(encryptRes.isOk)

    val message = ciphertextStream.toByteArray().decodeToString()
    val info = MessageInspector.determineEncryptionInfoForMessage(message)
    assertTrue(info.isEncrypted)
    assertEquals(2, info.keyIds.size)
    assertFalse(info.isSignedOnly)

    assertFails {
      val ciphertextStreamCopy = message.byteInputStream()
      val plaintextStream = ByteArrayOutputStream()
      cryptoHandler.decrypt(
        listOf(aliceKey, bobKey),
        KEY_PASSPHRASE,
        ciphertextStreamCopy,
        plaintextStream,
        PGPDecryptOptions.Builder().build(),
      )
    }
  }

@gregrenda
Copy link
Contributor Author

Added a unit test. You have to pass both private and public keys to decrypt simultaneously to trigger the bug. You probably shouldn't be passing public keys to decrypt but that's likely a bigger change.

Copy link
Member

@msfjarvis msfjarvis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick turnaround!

@msfjarvis msfjarvis added this pull request to the merge queue Jul 30, 2024
Merged via the queue into android-password-store:develop with commit b5b7f74 Jul 30, 2024
6 checks passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A-PGPainless Area: PGPainless-backed PGP C-bug Category: This is a bug P-high Priority: high, must be resolved before next major release S-waiting-on-author Status: This PR is incomplete or needs to address review comments
Projects
None yet
Development

Successfully merging this pull request may close these issues.

public only keys cause null pointer exception
2 participants