Skip to content

Commit bbcf73a

Browse files
committed
Bug fix
1 parent 3927653 commit bbcf73a

File tree

3 files changed

+59
-56
lines changed

3 files changed

+59
-56
lines changed

src/main/kotlin/KotlinPGP.kt

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,14 @@ object KotlinPGP {
153153
ByteArrayInputStream(encrypted.toByteArray()).use {
154154
PGPUtil.getDecoderStream(it)
155155
}.use { inputStream ->
156-
157-
val encryptedDataKeyId = arrayListOf<Long>()
156+
val allKeys = privateKeyData.flatMap {
157+
OpenPGPUtils.getAllEncryptionPrivateKeys(
158+
getSecretKeyRingFromString(
159+
it.key,
160+
it.password
161+
), it.password.toCharArray()
162+
)
163+
}
158164
PGPObjectFactory(inputStream, jcaKeyFingerprintCalculator)
159165
.let {
160166
when (val obj = it.nextObject()) {
@@ -165,45 +171,28 @@ object KotlinPGP {
165171
it.encryptedDataObjects.iterator()
166172
}.forEach { data ->
167173
if (data is PGPPublicKeyEncryptedData) {
168-
encryptedDataKeyId.add(data.keyID)
169-
privateKeyData.forEach { privateKeyData ->
170-
val privKey = OpenPGPUtils.getMasterPrivateKey(getSecretKeyRingFromString(privateKeyData.key, privateKeyData.password), privateKeyData.password.toCharArray())
171-
if (data.keyID == 0L) {
174+
if (data.keyID == 0L) {
175+
for (key in allKeys) {
172176
kotlin.runCatching {
173-
data.getDataStream(BcPublicKeyDataDecryptorFactory(privKey)).use {
177+
data.getDataStream(BcPublicKeyDataDecryptorFactory(key)).use {
174178
PGPObjectFactory(it, jcaKeyFingerprintCalculator)
175179
}.let {
176-
return getDecryptResultFromFactory(it, encryptedDataKeyId)
180+
return getDecryptResultFromFactory(it, arrayListOf())
177181
}
178182
}.onFailure {
179183

180184
}
181-
} else if (data.keyID == privKey?.keyID) {
182-
data.getDataStream(BcPublicKeyDataDecryptorFactory(privKey)).use {
183-
PGPObjectFactory(it, jcaKeyFingerprintCalculator)
184-
}.let {
185-
return getDecryptResultFromFactory(it, encryptedDataKeyId)
186-
}
185+
}
186+
} else if (allKeys.any { key -> key.keyID == data.keyID }) {
187+
val key = allKeys.first { key -> key.keyID == data.keyID }
188+
data.getDataStream(BcPublicKeyDataDecryptorFactory(key)).use {
189+
PGPObjectFactory(it, jcaKeyFingerprintCalculator)
190+
}.let {
191+
return getDecryptResultFromFactory(it, arrayListOf())
187192
}
188193
}
189194
}
190195
}
191-
// .let {
192-
// OpenPGPUtils.getMasterPrivateKey(privateKeyRing)
193-
// var privKey: PGPPrivateKey? = null
194-
// var encryptedData: PGPPublicKeyEncryptedData? = null
195-
// while (it.hasNext()) {
196-
// val data = it.next() as PGPPublicKeyEncryptedData
197-
// encryptedDataKeyId.add(data.keyID)
198-
// if (privKey == null) {
199-
// encryptedData = data
200-
// privKey = OpenPGPUtils.getMasterPrivateKey(privateKeyRing, encryptedData.keyID, password.toCharArray())
201-
// }
202-
// }
203-
// encryptedData?.getDataStream(BcPublicKeyDataDecryptorFactory(privKey))
204-
// }?.use { clear ->
205-
// PGPObjectFactory(clear, jcaKeyFingerprintCalculator)
206-
// }
207196
}
208197
return null
209198
}
@@ -278,7 +267,11 @@ object KotlinPGP {
278267
encryptedDataKeyId.add(data.keyID)
279268
if (privKey == null) {
280269
encryptedData = data
281-
privKey = OpenPGPUtils.getMasterPrivateKey(privateKeyRing, encryptedData.keyID, password.toCharArray())
270+
privKey = OpenPGPUtils.getEncryptionPrivateKey(
271+
privateKeyRing,
272+
encryptedData.keyID,
273+
password.toCharArray()
274+
)
282275
}
283276
}
284277
encryptedData?.getDataStream(BcPublicKeyDataDecryptorFactory(privKey))
@@ -409,7 +402,7 @@ object KotlinPGP {
409402
PGPEncryptedDataGenerator(it)
410403
}.also {
411404
encryptParameter.publicKey.map {
412-
OpenPGPUtils.getEncryptionKey(getPublicKeyRingFromString(it.key)) to it
405+
OpenPGPUtils.getEncryptionPublicKey(getPublicKeyRingFromString(it.key)) to it
413406
}.forEach { data ->
414407
if (data.second.isHidden) {
415408
it.addMethod(KtHiddenPublicKeyKeyEncryptionMethodGenerator(data.first))
@@ -474,7 +467,6 @@ object KotlinPGP {
474467
}
475468

476469

477-
478470
fun verify(signatureData: SignatureData, publicKey: List<String>): VerifyResult {
479471
return if (signatureData.onePassSignatureList == null || signatureData.signatureList == null) {
480472
if (signatureData.signatureList != null) {

src/main/kotlin/utils/OpenPGPUtils.kt

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,45 @@ import java.io.ByteArrayOutputStream
77

88
internal object OpenPGPUtils {
99

10-
fun getEncryptionKey(publicKeyRing: PGPPublicKeyRing): PGPPublicKey {
11-
val iterator = publicKeyRing.publicKeys
12-
while (iterator.hasNext()) {
13-
val key = iterator.next() as PGPPublicKey
14-
if (key.isEncryptionKey) {
10+
fun getEncryptionPublicKey(publicKeyRing: PGPPublicKeyRing): PGPPublicKey {
11+
var masterEncryptionKey: PGPPublicKey? = null
12+
publicKeyRing.publicKeys.forEach { key ->
13+
val isMaster = key.isMasterKey
14+
val isEncryption = key.isEncryptionKey
15+
if (isMaster && isEncryption) {
16+
masterEncryptionKey = key
17+
} else if (!isMaster && isEncryption) {
1518
return key
1619
}
1720
}
21+
if (masterEncryptionKey != null) {
22+
return masterEncryptionKey!!
23+
}
1824
throw IllegalArgumentException(
1925
"Can't find encryption key in key ring."
2026
)
2127
}
2228

23-
fun getMasterPrivateKey(keyRing: PGPSecretKeyRing, keyID: Long, pass: CharArray): PGPPrivateKey? {
29+
fun getEncryptionPrivateKey(keyRing: PGPSecretKeyRing, keyID: Long, pass: CharArray): PGPPrivateKey? {
2430
val pgpSecKey = keyRing.getSecretKey(keyID)
2531
val decryptor = BcPBESecretKeyDecryptorBuilder(BcPGPDigestCalculatorProvider()).build(pass)
2632
return pgpSecKey?.extractPrivateKey(decryptor)
2733
}
2834

35+
fun getAllEncryptionPrivateKeys(keyRing: PGPSecretKeyRing, pass: CharArray): List<PGPPrivateKey> {
36+
val keys = arrayListOf<PGPPrivateKey>()
37+
keyRing.secretKeys.forEach { key ->
38+
if (key.publicKey.isEncryptionKey) {
39+
val decryptor = BcPBESecretKeyDecryptorBuilder(BcPGPDigestCalculatorProvider()).build(pass)
40+
keys.add(key.extractPrivateKey(decryptor))
41+
}
42+
}
43+
return keys
44+
}
2945

30-
fun getMasterPrivateKey(keyRing: PGPSecretKeyRing, pass: CharArray): PGPPrivateKey? {
31-
val iterator = keyRing.secretKeys
32-
while (iterator.hasNext()) {
33-
val key = iterator.next() as PGPSecretKey
34-
if (!key.isMasterKey) {
46+
fun getEncryptionPrivateKey(keyRing: PGPSecretKeyRing, pass: CharArray): PGPPrivateKey? {
47+
keyRing.secretKeys.forEach { key ->
48+
if (key.publicKey.isEncryptionKey) {
3549
val decryptor = BcPBESecretKeyDecryptorBuilder(BcPGPDigestCalculatorProvider()).build(pass)
3650
return key.extractPrivateKey(decryptor)
3751
}
@@ -55,9 +69,7 @@ internal object OpenPGPUtils {
5569
}
5670

5771
fun getMasterPublicKeyFromKeyRing(publicKeyRing: PGPPublicKeyRing): PGPPublicKey? {
58-
val iterator = publicKeyRing.publicKeys
59-
while (iterator.hasNext()) {
60-
val key = iterator.next() as PGPPublicKey
72+
publicKeyRing.publicKeys.forEach { key ->
6173
if (key.isMasterKey) {
6274
return key
6375
}
@@ -66,11 +78,10 @@ internal object OpenPGPUtils {
6678
}
6779

6880
fun getSignPrivateKey(securet: PGPSecretKeyRing): PGPSecretKey {
69-
val keyRingIter = securet.secretKeys
70-
while (keyRingIter.hasNext()) {
71-
when (val next = keyRingIter.next()) {
81+
securet.secretKeys.forEach {
82+
when (it) {
7283
is PGPSecretKeyRing -> {
73-
val keyIter = next.secretKeys
84+
val keyIter = it.secretKeys
7485
while (keyIter.hasNext()) {
7586
val key = keyIter.next() as PGPSecretKey
7687

@@ -81,8 +92,8 @@ internal object OpenPGPUtils {
8192
}
8293
is PGPSecretKey -> {
8394
// TODO: Do we need to check if is master key?
84-
if (next.isSigningKey) {
85-
return next
95+
if (it.isSigningKey) {
96+
return it
8697
}
8798
}
8899
}

src/test/kotlin/KotlinPGPTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ class KotlinPGPTest : FreeSpec({
217217
decryptResult.includedKeys.size shouldBe 3
218218
decryptResult.includedKeys.forAll { id ->
219219
publicKeyRings.any { keyRing ->
220-
OpenPGPUtils.getEncryptionKey(keyRing)?.keyID == id
220+
OpenPGPUtils.getEncryptionPublicKey(keyRing).keyID == id
221221
}
222222
}
223223
val signatureData = decryptResult.signatureData
@@ -232,7 +232,7 @@ class KotlinPGPTest : FreeSpec({
232232
decryptResult.includedKeys.size shouldBe 3
233233
decryptResult.includedKeys.forAll { id ->
234234
publicKeyRings.any { keyRing ->
235-
OpenPGPUtils.getEncryptionKey(keyRing)?.keyID == id
235+
OpenPGPUtils.getEncryptionPublicKey(keyRing).keyID == id
236236
}
237237
}
238238
val signatureData = decryptResult.signatureData
@@ -249,7 +249,7 @@ class KotlinPGPTest : FreeSpec({
249249
decryptResult.includedKeys.size shouldBe 0
250250
decryptResult.includedKeys.forAll { id ->
251251
publicKeyRings.any { keyRing ->
252-
OpenPGPUtils.getEncryptionKey(keyRing)?.keyID == id
252+
OpenPGPUtils.getEncryptionPublicKey(keyRing).keyID == id
253253
}
254254
}
255255
val signatureData = decryptResult.signatureData

0 commit comments

Comments
 (0)