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

Commit 0c8bed4

Browse files
committed
feat(crypto-pgpainless): run usability test when adding keys
1 parent 66a9c88 commit 0c8bed4

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

crypto-common/src/main/kotlin/app/passwordstore/crypto/errors/CryptoException.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ public object KeyDeletionFailedException : KeyManagerException("Couldn't delete
2222
public object InvalidKeyException :
2323
KeyManagerException("Given key cannot be parsed as a known key type")
2424

25+
/** Key failed the [app.passwordstore.crypto.KeyUtils.isKeyUsable] test. */
26+
public object UnusableKeyException :
27+
KeyManagerException("Given key is not usable for encryption - is it using AEAD?")
28+
2529
/** No key matching `keyId` could be found. */
2630
public class KeyNotFoundException(keyId: String) :
2731
KeyManagerException("No key found with id: $keyId")

crypto-pgpainless/src/main/kotlin/app/passwordstore/crypto/PGPKeyManager.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package app.passwordstore.crypto
88

99
import androidx.annotation.VisibleForTesting
10+
import app.passwordstore.crypto.KeyUtils.isKeyUsable
1011
import app.passwordstore.crypto.KeyUtils.tryGetId
1112
import app.passwordstore.crypto.KeyUtils.tryParseKeyring
1213
import app.passwordstore.crypto.errors.InvalidKeyException
@@ -15,6 +16,7 @@ import app.passwordstore.crypto.errors.KeyDeletionFailedException
1516
import app.passwordstore.crypto.errors.KeyDirectoryUnavailableException
1617
import app.passwordstore.crypto.errors.KeyNotFoundException
1718
import app.passwordstore.crypto.errors.NoKeysAvailableException
19+
import app.passwordstore.crypto.errors.UnusableKeyException
1820
import app.passwordstore.util.coroutines.runSuspendCatching
1921
import com.github.michaelbull.result.Result
2022
import com.github.michaelbull.result.unwrap
@@ -42,6 +44,7 @@ constructor(
4244
runSuspendCatching {
4345
if (!keyDirExists()) throw KeyDirectoryUnavailableException
4446
val incomingKeyRing = tryParseKeyring(key) ?: throw InvalidKeyException
47+
if (!isKeyUsable(key)) throw UnusableKeyException
4548
val keyFile = File(keyDir, "${tryGetId(key)}.$KEY_EXTENSION")
4649
if (keyFile.exists()) {
4750
val existingKeyBytes = keyFile.readBytes()

crypto-pgpainless/src/test/kotlin/app/passwordstore/crypto/PGPKeyManagerTest.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import app.passwordstore.crypto.PGPIdentifier.UserId
66
import app.passwordstore.crypto.errors.KeyAlreadyExistsException
77
import app.passwordstore.crypto.errors.KeyNotFoundException
88
import app.passwordstore.crypto.errors.NoKeysAvailableException
9+
import app.passwordstore.crypto.errors.UnusableKeyException
910
import com.github.michaelbull.result.Err
1011
import com.github.michaelbull.result.Ok
1112
import com.github.michaelbull.result.unwrap
@@ -70,6 +71,13 @@ class PGPKeyManagerTest {
7071
assertEquals(KeyId(CryptoConstants.KEY_ID), keyId)
7172
}
7273

74+
@Test
75+
fun addKeyWithUnusableKey() =
76+
runTest(dispatcher) {
77+
val error = keyManager.addKey(PGPKey(TestUtils.getAEADSecretKey())).unwrapError()
78+
assertEquals(UnusableKeyException, error)
79+
}
80+
7381
@Test
7482
fun removeKey() =
7583
runTest(dispatcher) {

0 commit comments

Comments
 (0)