Skip to content
This repository has been archived by the owner on Aug 12, 2024. It is now read-only.

Commit

Permalink
Add key reader function impl to default keystore on Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
Cach30verfl0w committed Jun 14, 2024
1 parent 1fc0189 commit 496330c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import okio.Path
*/
class KeyStoreFactory<C: Any>(val name: String) {
private lateinit var initialize: () -> C
private var readKeyFromFile: ((C, Path) -> Key)? = null
private var readKeyFromFile: ((C, Path, String, UByte) -> Key)? = null

/**
* This method creates a new keystore with the delegate functions specified and initializes the
Expand All @@ -45,18 +45,9 @@ class KeyStoreFactory<C: Any>(val name: String) {
*/
fun createKeyStore(): KeyStore = object: KeyStore {
private val context = initialize()

/**
* This method opens the file with `okio` and reads the content as a bytearray. This content
* is used to extract information from the key and return this key to the user.
*
* @param path The path of the file
* @return The key derived from the file
*
* @author Cedric Hammes
* @since 14/06/2024
*/
override fun readKeyFromFile(path: Path): Key = readKeyFromFile!!.invoke(context, path)

override fun readKeyFromFile(path: Path, algorithm: String, purposes: UByte): Key =
readKeyFromFile!!.invoke(context, path, algorithm, purposes)
}

/**
Expand All @@ -75,7 +66,7 @@ class KeyStoreFactory<C: Any>(val name: String) {
* @author Cedric Hammes
* @since 14/06/2024
*/
fun readKeyFromFile(closure: (C, Path) -> Key) = this.apply {
fun readKeyFromFile(closure: (C, Path, String, UByte) -> Key) = this.apply {
if (readKeyFromFile != null)
throw IllegalStateException("Unable to set readKeyFromFile delegate twice")
readKeyFromFile = closure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ interface KeyStore {
* This method opens the file with `okio` and reads the content as a bytearray. This content is
* used to extract information from the key and return this key to the user.
*
* @param path The path of the file
* @return The key derived from the file
* @param path The path of the file
* @param algorithm The name of the key's algorithm
* @param purposes The purposes of the key
* @return The key derived from the file
*
* @author Cedric Hammes
* @since 14/06/2024
*/
fun readKeyFromFile(path: Path): Key
fun readKeyFromFile(path: Path, algorithm: String, purposes: UByte): Key

companion object {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class OpenSSLKey(private val secureHeap: SecureHeap,
): Key {
@InsecureCryptoApi
override val encoded: ByteArray = ByteArray(rawDataSize.toInt()) { rawDataPtr[it].toByte() }
override val format: KeyFormat = KeyFormat.DER // TODO: Derive from encoded content
override val format: KeyFormat = KeyFormat.DER // TODO: Derive from key

override fun close() {
secureHeap.free(rawDataSize, rawDataPtr)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package io.karma.advcrypto.linux.providers

import io.karma.advcrypto.AbstractProvider
import io.karma.advcrypto.Providers
import io.karma.advcrypto.linux.utils.KeyReaderHelper
import okio.FileSystem

class DefaultKeyStoreProvider: AbstractProvider(
Expand All @@ -30,12 +31,14 @@ class DefaultKeyStoreProvider: AbstractProvider(
initialize {
"Placeholder"
}
readKeyFromFile { context, path ->
readKeyFromFile { _, path, algorithm, purposes ->
FileSystem.SYSTEM.read(path) {
val data = readByteArray()
close()
return@readKeyFromFile KeyReaderHelper.tryParse(data, purposes, algorithm)?:
throw RuntimeException("Unable to parse key, no valid format!")

}
TODO("Read and derive key")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,11 @@ object KeyReaderHelper {
* @author Cedric Hammes
* @since 14/06/2024
*/
fun tryParse(array: ByteArray, purposes: UByte): Key? = array.usePinned {
tryParse(it.addressOf(0), array.size.toULong(), purposes)
fun tryParse(array: ByteArray, purposes: UByte, algorithm: String? = null): Key? = array.usePinned {
val parsedKey = tryParse(it.addressOf(0), array.size.toULong(), purposes)?: return null
if (parsedKey.algorithm != algorithm)
throw IllegalArgumentException("The algorithm '$algorithm' was specified, but key is '${parsedKey.algorithm}'")
return parsedKey
}

}

0 comments on commit 496330c

Please sign in to comment.