feat(crypto-pgpainless): run usability test when adding keys

This commit is contained in:
Harsh Shandilya 2023-07-05 02:40:31 +05:30
parent 66a9c88448
commit 0c8bed4e54
No known key found for this signature in database
3 changed files with 15 additions and 0 deletions

View file

@ -22,6 +22,10 @@ public object KeyDeletionFailedException : KeyManagerException("Couldn't delete
public object InvalidKeyException :
KeyManagerException("Given key cannot be parsed as a known key type")
/** Key failed the [app.passwordstore.crypto.KeyUtils.isKeyUsable] test. */
public object UnusableKeyException :
KeyManagerException("Given key is not usable for encryption - is it using AEAD?")
/** No key matching `keyId` could be found. */
public class KeyNotFoundException(keyId: String) :
KeyManagerException("No key found with id: $keyId")

View file

@ -7,6 +7,7 @@
package app.passwordstore.crypto
import androidx.annotation.VisibleForTesting
import app.passwordstore.crypto.KeyUtils.isKeyUsable
import app.passwordstore.crypto.KeyUtils.tryGetId
import app.passwordstore.crypto.KeyUtils.tryParseKeyring
import app.passwordstore.crypto.errors.InvalidKeyException
@ -15,6 +16,7 @@ import app.passwordstore.crypto.errors.KeyDeletionFailedException
import app.passwordstore.crypto.errors.KeyDirectoryUnavailableException
import app.passwordstore.crypto.errors.KeyNotFoundException
import app.passwordstore.crypto.errors.NoKeysAvailableException
import app.passwordstore.crypto.errors.UnusableKeyException
import app.passwordstore.util.coroutines.runSuspendCatching
import com.github.michaelbull.result.Result
import com.github.michaelbull.result.unwrap
@ -42,6 +44,7 @@ constructor(
runSuspendCatching {
if (!keyDirExists()) throw KeyDirectoryUnavailableException
val incomingKeyRing = tryParseKeyring(key) ?: throw InvalidKeyException
if (!isKeyUsable(key)) throw UnusableKeyException
val keyFile = File(keyDir, "${tryGetId(key)}.$KEY_EXTENSION")
if (keyFile.exists()) {
val existingKeyBytes = keyFile.readBytes()

View file

@ -6,6 +6,7 @@ import app.passwordstore.crypto.PGPIdentifier.UserId
import app.passwordstore.crypto.errors.KeyAlreadyExistsException
import app.passwordstore.crypto.errors.KeyNotFoundException
import app.passwordstore.crypto.errors.NoKeysAvailableException
import app.passwordstore.crypto.errors.UnusableKeyException
import com.github.michaelbull.result.Err
import com.github.michaelbull.result.Ok
import com.github.michaelbull.result.unwrap
@ -70,6 +71,13 @@ class PGPKeyManagerTest {
assertEquals(KeyId(CryptoConstants.KEY_ID), keyId)
}
@Test
fun addKeyWithUnusableKey() =
runTest(dispatcher) {
val error = keyManager.addKey(PGPKey(TestUtils.getAEADSecretKey())).unwrapError()
assertEquals(UnusableKeyException, error)
}
@Test
fun removeKey() =
runTest(dispatcher) {