aboutsummaryrefslogtreecommitdiffstats
path: root/crypto-pgpainless/src/main/kotlin/dev/msfjarvis/aps/crypto/PGPKeyManager.kt
diff options
context:
space:
mode:
authorHarsh Shandilya <me@msfjarvis.dev>2022-01-18 17:40:16 +0530
committerGitHub <noreply@github.com>2022-01-18 17:40:16 +0530
commit5509558eedbcd90f130b32d4e6c41632da291b15 (patch)
tree7b6839928889698ebe0da48a83b2e1532ed6d094 /crypto-pgpainless/src/main/kotlin/dev/msfjarvis/aps/crypto/PGPKeyManager.kt
parent3ead6596ba9c2b65434f6cd948bf6ebf41a230eb (diff)
downloadHardenedPass-5509558eedbcd90f130b32d4e6c41632da291b15.tar.gz
HardenedPass-5509558eedbcd90f130b32d4e6c41632da291b15.zip
Parameterize key and key identifier types for KeyManager (#1669)
Diffstat (limited to 'crypto-pgpainless/src/main/kotlin/dev/msfjarvis/aps/crypto/PGPKeyManager.kt')
-rw-r--r--crypto-pgpainless/src/main/kotlin/dev/msfjarvis/aps/crypto/PGPKeyManager.kt57
1 files changed, 30 insertions, 27 deletions
diff --git a/crypto-pgpainless/src/main/kotlin/dev/msfjarvis/aps/crypto/PGPKeyManager.kt b/crypto-pgpainless/src/main/kotlin/dev/msfjarvis/aps/crypto/PGPKeyManager.kt
index 2053ecd7..adcce22b 100644
--- a/crypto-pgpainless/src/main/kotlin/dev/msfjarvis/aps/crypto/PGPKeyManager.kt
+++ b/crypto-pgpainless/src/main/kotlin/dev/msfjarvis/aps/crypto/PGPKeyManager.kt
@@ -22,11 +22,11 @@ public class PGPKeyManager
constructor(
filesDir: String,
private val dispatcher: CoroutineDispatcher,
-) : KeyManager {
+) : KeyManager<PGPKey, GpgIdentifier> {
private val keyDir = File(filesDir, KEY_DIR_NAME)
- override suspend fun addKey(key: Key, replace: Boolean): Result<Key, Throwable> =
+ override suspend fun addKey(key: PGPKey, replace: Boolean): Result<PGPKey, Throwable> =
withContext(dispatcher) {
runCatching {
if (!keyDirExists()) throw KeyManagerException.KeyDirectoryUnavailableException
@@ -36,7 +36,7 @@ constructor(
// Check for replace flag first and if it is false, throw an error
if (!replace)
throw KeyManagerException.KeyAlreadyExistsException(
- tryGetId(key) ?: "Failed to retrieve key ID"
+ tryGetId(key)?.toString() ?: "Failed to retrieve key ID"
)
if (!keyFile.delete()) throw KeyManagerException.KeyDeletionFailedException
}
@@ -47,7 +47,7 @@ constructor(
}
}
- override suspend fun removeKey(key: Key): Result<Key, Throwable> =
+ override suspend fun removeKey(key: PGPKey): Result<PGPKey, Throwable> =
withContext(dispatcher) {
runCatching {
if (!keyDirExists()) throw KeyManagerException.KeyDirectoryUnavailableException
@@ -61,49 +61,52 @@ constructor(
}
}
- override suspend fun getKeyById(id: String): Result<Key, Throwable> =
+ override suspend fun getKeyById(id: GpgIdentifier): Result<PGPKey, Throwable> =
withContext(dispatcher) {
runCatching {
if (!keyDirExists()) throw KeyManagerException.KeyDirectoryUnavailableException
val keyFiles = keyDir.listFiles()
if (keyFiles.isNullOrEmpty()) throw KeyManagerException.NoKeysAvailableException
-
- val keys = keyFiles.map { file -> Key(file.readBytes()) }
- // Try to parse the key ID as an email
- val selector = SelectUserId.byEmail(id)
- val userIdMatch =
- keys.map { key -> key to tryParseKeyring(key) }.firstOrNull { (_, keyRing) ->
- selector.firstMatch(keyRing) != null
- }
-
- if (userIdMatch != null) {
- return@runCatching userIdMatch.first
- }
-
- val keyIdMatch =
- keys.map { key -> key to tryGetId(key) }.firstOrNull { (_, keyId) ->
- keyId == id || keyId == "0x$id"
+ val keys = keyFiles.map { file -> PGPKey(file.readBytes()) }
+
+ val matchResult =
+ when (id) {
+ is GpgIdentifier.KeyId -> {
+ val keyIdMatch =
+ keys.map { key -> key to tryGetId(key) }.firstOrNull { (_, keyId) ->
+ keyId?.id == id.id
+ }
+ keyIdMatch?.first
+ }
+ is GpgIdentifier.UserId -> {
+ val selector = SelectUserId.byEmail(id.email)
+ val userIdMatch =
+ keys.map { key -> key to tryParseKeyring(key) }.firstOrNull { (_, keyRing) ->
+ selector.firstMatch(keyRing) != null
+ }
+ userIdMatch?.first
+ }
}
- if (keyIdMatch != null) {
- return@runCatching keyIdMatch.first
+ if (matchResult != null) {
+ return@runCatching matchResult
}
- throw KeyManagerException.KeyNotFoundException(id)
+ throw KeyManagerException.KeyNotFoundException("$id")
}
}
- override suspend fun getAllKeys(): Result<List<Key>, Throwable> =
+ override suspend fun getAllKeys(): Result<List<PGPKey>, Throwable> =
withContext(dispatcher) {
runCatching {
if (!keyDirExists()) throw KeyManagerException.KeyDirectoryUnavailableException
val keyFiles = keyDir.listFiles()
if (keyFiles.isNullOrEmpty()) return@runCatching emptyList()
- keyFiles.map { keyFile -> Key(keyFile.readBytes()) }.toList()
+ keyFiles.map { keyFile -> PGPKey(keyFile.readBytes()) }.toList()
}
}
- override suspend fun getKeyId(key: Key): String? = tryGetId(key)
+ override suspend fun getKeyId(key: PGPKey): GpgIdentifier? = tryGetId(key)
// TODO: This is a temp hack for now and in future it should check that the GPGKeyManager can
// decrypt the file.