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

Commit

Permalink
Add bytearray wrapper for tryParse and modify tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Cach30verfl0w committed Jun 14, 2024
1 parent f29baff commit c0ca888
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import io.karma.advcrypto.linux.keys.OpenSSLPKey
import kotlinx.cinterop.ByteVar
import kotlinx.cinterop.CPointer
import kotlinx.cinterop.ExperimentalForeignApi
import kotlinx.cinterop.addressOf
import kotlinx.cinterop.usePinned
import libssl.BIO
import libssl.BIO_free
import libssl.BIO_new
Expand Down Expand Up @@ -159,7 +161,7 @@ object KeyReaderHelper {
}

/**
* This method tries to parse the data in the specified pointer in a key. This method supports
* This method tries to parse the data of the specified pointer in a key. This method supports
* PEM and DER. If no supported format worked, this method simply returns null.
*
* @author Cedric Hammes
Expand All @@ -182,4 +184,15 @@ object KeyReaderHelper {
return null
}

/**
* This method tries to parse the data of the specified array in a key. This method supports PEM
* and DER. If no supported format worked, this method simply returns null.
*
* @author Cedric Hammes
* @since 14/06/2024
*/
fun tryParse(array: ByteArray, purposes: UByte): Key? = array.usePinned {
tryParse(it.addressOf(0), array.size.toULong(), purposes)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,68 +4,50 @@ import io.karma.advcrypto.keys.Key
import io.karma.advcrypto.keys.enum.KeyFormat
import io.karma.advcrypto.keys.enum.KeyType
import io.karma.advcrypto.linux.utils.KeyReaderHelper
import kotlinx.cinterop.ExperimentalForeignApi
import kotlinx.cinterop.addressOf
import kotlinx.cinterop.usePinned
import okio.FileSystem
import okio.Path.Companion.toPath
import kotlin.experimental.ExperimentalNativeApi
import kotlin.test.Test

@OptIn(ExperimentalForeignApi::class, ExperimentalNativeApi::class)
@OptIn(ExperimentalNativeApi::class)
class KeyReaderHelperTests {
private val fileSystem = FileSystem.SYSTEM

@Test
fun testPEM() {
fileSystem.read("./testkeys/rsa-private-key.pem".toPath()) {
val byteArray = readByteArray()
val size = byteArray.size.toULong()
byteArray.usePinned { array ->
val key = KeyReaderHelper.tryParse(array.addressOf(0), size, Key.PURPOSES_ALL)!!
assert(key.algorithm == "RSA")
assert(key.format == KeyFormat.PEM)
assert(key.type == KeyType.PRIVATE)
}
val key = KeyReaderHelper.tryParse(readByteArray(), Key.PURPOSES_ALL)!!
close()
assert(key.algorithm == "RSA")
assert(key.format == KeyFormat.PEM)
assert(key.type == KeyType.PRIVATE)
}

fileSystem.read("./testkeys/rsa-public-key.pem".toPath()) {
val byteArray = readByteArray()
val size = byteArray.size.toULong()
byteArray.usePinned { array ->
val key = KeyReaderHelper.tryParse(array.addressOf(0), size, Key.PURPOSES_ALL)!!
assert(key.algorithm == "RSA")
assert(key.format == KeyFormat.PEM)
assert(key.type == KeyType.PUBLIC)
}
val key = KeyReaderHelper.tryParse(readByteArray(), Key.PURPOSES_ALL)!!
close()
assert(key.algorithm == "RSA")
assert(key.format == KeyFormat.PEM)
assert(key.type == KeyType.PUBLIC)
}
}

@Test
fun testDER() {
fileSystem.read("./testkeys/rsa-private-key.der".toPath()) {
val byteArray = readByteArray()
val size = byteArray.size.toULong()
byteArray.usePinned { array ->
val key = KeyReaderHelper.tryParse(array.addressOf(0), size, Key.PURPOSES_ALL)!!
assert(key.algorithm == "RSA")
assert(key.format == KeyFormat.DER)
assert(key.type == KeyType.PRIVATE)
}
val key = KeyReaderHelper.tryParse(readByteArray(), Key.PURPOSES_ALL)!!
close()
assert(key.algorithm == "RSA")
assert(key.format == KeyFormat.DER)
assert(key.type == KeyType.PRIVATE)
}

fileSystem.read("./testkeys/rsa-public-key.der".toPath()) {
val byteArray = readByteArray()
val size = byteArray.size.toULong()
byteArray.usePinned { array ->
val key = KeyReaderHelper.tryParse(array.addressOf(0), size, Key.PURPOSES_ALL)!!
assert(key.algorithm == "RSA")
assert(key.format == KeyFormat.DER)
assert(key.type == KeyType.PUBLIC)
}
val key = KeyReaderHelper.tryParse(readByteArray(), Key.PURPOSES_ALL)!!
close()
assert(key.algorithm == "RSA")
assert(key.format == KeyFormat.DER)
assert(key.type == KeyType.PUBLIC)
close()
}
}
Expand Down

0 comments on commit c0ca888

Please sign in to comment.