Use KeyRingUtils#publicKeyRingCollectionFrom to extract public keys (#2009)

This commit is contained in:
Harsh Shandilya 2022-07-16 21:25:04 +05:30 committed by GitHub
parent 205a1d942e
commit db01ed2a2e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 15 deletions

View file

@ -25,6 +25,7 @@ import org.pgpainless.encryption_signing.EncryptionOptions
import org.pgpainless.encryption_signing.ProducerOptions import org.pgpainless.encryption_signing.ProducerOptions
import org.pgpainless.exception.WrongPassphraseException import org.pgpainless.exception.WrongPassphraseException
import org.pgpainless.key.protection.PasswordBasedSecretKeyRingProtector import org.pgpainless.key.protection.PasswordBasedSecretKeyRingProtector
import org.pgpainless.key.util.KeyRingUtils
import org.pgpainless.util.Passphrase import org.pgpainless.util.Passphrase
public class PGPainlessCryptoHandler @Inject constructor() : CryptoHandler<PGPKey> { public class PGPainlessCryptoHandler @Inject constructor() : CryptoHandler<PGPKey> {
@ -71,16 +72,15 @@ public class PGPainlessCryptoHandler @Inject constructor() : CryptoHandler<PGPKe
val armoredKeys = val armoredKeys =
keys.joinToString("\n") { key -> key.contents.decodeToString() }.toByteArray() keys.joinToString("\n") { key -> key.contents.decodeToString() }.toByteArray()
val secKeysStream = ByteArrayInputStream(armoredKeys) val secKeysStream = ByteArrayInputStream(armoredKeys)
val secretKeyRingCollection = publicKeyRings.addAll(
PGPainless.readKeyRing().secretKeyRingCollection(secKeysStream) KeyRingUtils.publicKeyRingCollectionFrom(
secretKeyRingCollection.forEach { secretKeyRing -> PGPainless.readKeyRing().secretKeyRingCollection(secKeysStream)
publicKeyRings.add(PGPainless.extractCertificate(secretKeyRing)) )
} )
if (publicKeyRings.isEmpty()) { val pubKeysStream = ByteArrayInputStream(armoredKeys)
val pubKeysStream = ByteArrayInputStream(armoredKeys) publicKeyRings.addAll(PGPainless.readKeyRing().publicKeyRingCollection(pubKeysStream))
val publicKeyRingCollection = require(keys.size == publicKeyRings.size) {
PGPainless.readKeyRing().publicKeyRingCollection(pubKeysStream) "Failed to parse all keys: keys=${keys.size},parsed=${publicKeyRings.size}"
publicKeyRings.addAll(publicKeyRingCollection)
} }
require(publicKeyRings.isNotEmpty()) { "No public keys to encrypt message to" } require(publicKeyRings.isNotEmpty()) { "No public keys to encrypt message to" }
val publicKeyRingCollection = PGPPublicKeyRingCollection(publicKeyRings) val publicKeyRingCollection = PGPPublicKeyRingCollection(publicKeyRings)

View file

@ -19,9 +19,10 @@ import kotlin.test.assertTrue
import org.junit.runner.RunWith import org.junit.runner.RunWith
@Suppress("Unused") // Test runner handles it internally @Suppress("Unused") // Test runner handles it internally
enum class EncryptionKey(val key: PGPKey) { enum class EncryptionKey(val keySet: List<PGPKey>) {
PUBLIC(PGPKey(TestUtils.getArmoredPublicKey())), PUBLIC(listOf(PGPKey(TestUtils.getArmoredPublicKey()))),
SECRET(PGPKey(TestUtils.getArmoredPrivateKey())), SECRET(listOf(PGPKey(TestUtils.getArmoredPrivateKey()))),
ALL(listOf(PGPKey(TestUtils.getArmoredPublicKey()), PGPKey(TestUtils.getArmoredPrivateKey()))),
} }
@RunWith(TestParameterInjector::class) @RunWith(TestParameterInjector::class)
@ -35,7 +36,7 @@ class PGPainlessCryptoHandlerTest {
fun encryptAndDecrypt() { fun encryptAndDecrypt() {
val ciphertextStream = ByteArrayOutputStream() val ciphertextStream = ByteArrayOutputStream()
cryptoHandler.encrypt( cryptoHandler.encrypt(
listOf(encryptionKey.key), encryptionKey.keySet,
CryptoConstants.PLAIN_TEXT.byteInputStream(Charsets.UTF_8), CryptoConstants.PLAIN_TEXT.byteInputStream(Charsets.UTF_8),
ciphertextStream, ciphertextStream,
) )
@ -53,7 +54,7 @@ class PGPainlessCryptoHandlerTest {
fun decryptWithWrongPassphrase() { fun decryptWithWrongPassphrase() {
val ciphertextStream = ByteArrayOutputStream() val ciphertextStream = ByteArrayOutputStream()
cryptoHandler.encrypt( cryptoHandler.encrypt(
listOf(encryptionKey.key), encryptionKey.keySet,
CryptoConstants.PLAIN_TEXT.byteInputStream(Charsets.UTF_8), CryptoConstants.PLAIN_TEXT.byteInputStream(Charsets.UTF_8),
ciphertextStream, ciphertextStream,
) )