From c8bd5938cf4022acaded94dcfb9315d1fcc2d846 Mon Sep 17 00:00:00 2001 From: cach30verfl0w Date: Fri, 14 Jun 2024 15:51:32 +0200 Subject: [PATCH] Add type field to Key --- .../io/karma/advcrypto/android/AndroidUtils.kt | 13 ++++++++++--- .../karma/advcrypto/android/keys/AndroidKey.kt | 3 ++- .../kotlin/io/karma/advcrypto/keys/Key.kt | 11 +++++++++++ .../io/karma/advcrypto/linux/keys/OpenSSLKey.kt | 14 +++++++++++--- .../io/karma/advcrypto/linux/keys/OpenSSLPKey.kt | 4 +++- .../linux/providers/OpenSSLCryptoProvider.kt | 16 +++++++++++++--- 6 files changed, 50 insertions(+), 11 deletions(-) diff --git a/kmp-advcrypto/src/androidMain/kotlin/io/karma/advcrypto/android/AndroidUtils.kt b/kmp-advcrypto/src/androidMain/kotlin/io/karma/advcrypto/android/AndroidUtils.kt index 1d7e47c..94ed5cb 100644 --- a/kmp-advcrypto/src/androidMain/kotlin/io/karma/advcrypto/android/AndroidUtils.kt +++ b/kmp-advcrypto/src/androidMain/kotlin/io/karma/advcrypto/android/AndroidUtils.kt @@ -16,6 +16,7 @@ package io.karma.advcrypto.android +import android.annotation.SuppressLint import android.security.keystore.KeyGenParameterSpec import android.security.keystore.KeyProperties import io.karma.advcrypto.algorithm.Algorithm @@ -25,6 +26,7 @@ import io.karma.advcrypto.algorithm.delegates.SignatureDelegate import io.karma.advcrypto.android.keys.AndroidKey import io.karma.advcrypto.keys.Key import io.karma.advcrypto.keys.KeyPair +import io.karma.advcrypto.keys.enum.KeyType import java.security.KeyPairGenerator import java.security.MessageDigest import java.security.PrivateKey @@ -59,16 +61,19 @@ fun KeyGeneratorDelegate.androidKeyPairGenerator() { KeyPair( AndroidKey( keyPair.public, - purposes and Key.PURPOSE_SIGNING.inv() + purposes and Key.PURPOSE_SIGNING.inv(), + KeyType.PUBLIC ), AndroidKey( keyPair.private, - purposes and Key.PURPOSE_VERIFY.inv() + purposes and Key.PURPOSE_VERIFY.inv(), + KeyType.PRIVATE ) ) } } +@SuppressLint("WrongConstant") fun KeyGeneratorDelegate.androidKeyPair(defaultBlockMode: BlockMode, algorithm: String) { initializer { initSpec -> val purposes = purposesToAndroid(initSpec.purposes) @@ -88,6 +93,7 @@ fun KeyGeneratorDelegate.androidKeyPair(defaultBlockMode: Bloc androidKeyPairGenerator() } +@SuppressLint("WrongConstant") fun KeyGeneratorDelegate.androidKey(defaultBlockMode: BlockMode, algorithm: String) { initializer { initSpec -> val purposes = purposesToAndroid(initSpec.purposes) @@ -107,7 +113,8 @@ fun KeyGeneratorDelegate.androidKey(defaultBlockMode: BlockMode, a generateKey { context -> AndroidKey( context.internalContext.generateKey(), - context.generatorSpec.purposes + context.generatorSpec.purposes, + KeyType.SECRET ) } } diff --git a/kmp-advcrypto/src/androidMain/kotlin/io/karma/advcrypto/android/keys/AndroidKey.kt b/kmp-advcrypto/src/androidMain/kotlin/io/karma/advcrypto/android/keys/AndroidKey.kt index a19a29f..f3d795b 100644 --- a/kmp-advcrypto/src/androidMain/kotlin/io/karma/advcrypto/android/keys/AndroidKey.kt +++ b/kmp-advcrypto/src/androidMain/kotlin/io/karma/advcrypto/android/keys/AndroidKey.kt @@ -17,10 +17,11 @@ package io.karma.advcrypto.android.keys import io.karma.advcrypto.keys.Key +import io.karma.advcrypto.keys.enum.KeyType typealias RawKey = java.security.Key -class AndroidKey(val raw: RawKey, override val purposes: UByte): Key { +class AndroidKey(val raw: RawKey, override val purposes: UByte, override val type: KeyType): Key { override val algorithm: String = raw.algorithm override fun toString(): String { diff --git a/kmp-advcrypto/src/commonMain/kotlin/io/karma/advcrypto/keys/Key.kt b/kmp-advcrypto/src/commonMain/kotlin/io/karma/advcrypto/keys/Key.kt index 4f24ccb..737eee0 100644 --- a/kmp-advcrypto/src/commonMain/kotlin/io/karma/advcrypto/keys/Key.kt +++ b/kmp-advcrypto/src/commonMain/kotlin/io/karma/advcrypto/keys/Key.kt @@ -16,6 +16,8 @@ package io.karma.advcrypto.keys +import io.karma.advcrypto.keys.enum.KeyType + /** * This interface represents every single key which can be generated by this library. These keys are * used to perform operations like signing or encrypting data etc. @@ -46,6 +48,15 @@ interface Key: AutoCloseable { */ val purposes: UByte + /** + * This value represents the type of this specific key. This type identifies a public, private + * or secret key. + * + * @author Cedric Hammes + * @since 14/06/2024 + */ + val type: KeyType + companion object { const val PURPOSES_ALL: UByte = 0b0000_1111U const val PURPOSES_SYMMETRIC: UByte = 0b0000_1100U diff --git a/kmp-advcrypto/src/linuxX64Main/kotlin/io/karma/advcrypto/linux/keys/OpenSSLKey.kt b/kmp-advcrypto/src/linuxX64Main/kotlin/io/karma/advcrypto/linux/keys/OpenSSLKey.kt index a658fbc..32edce5 100644 --- a/kmp-advcrypto/src/linuxX64Main/kotlin/io/karma/advcrypto/linux/keys/OpenSSLKey.kt +++ b/kmp-advcrypto/src/linuxX64Main/kotlin/io/karma/advcrypto/linux/keys/OpenSSLKey.kt @@ -17,6 +17,7 @@ package io.karma.advcrypto.linux.keys import io.karma.advcrypto.keys.Key +import io.karma.advcrypto.keys.enum.KeyType import io.karma.advcrypto.linux.utils.SecureHeap import kotlinx.cinterop.CPointer import kotlinx.cinterop.ExperimentalForeignApi @@ -32,7 +33,8 @@ class OpenSSLKey(private val secureHeap: SecureHeap, override val purposes: UByte, override val algorithm: String, private val rawDataPtr: CPointer, - private val rawDataSize: ULong + private val rawDataSize: ULong, + override val type: KeyType ): Key { override fun close() { @@ -40,14 +42,20 @@ class OpenSSLKey(private val secureHeap: SecureHeap, } companion object { - fun generateRandom(secureHeap: SecureHeap, keySize: Int, purposes: UByte, algorithm: String): OpenSSLKey { + fun generateRandom( + secureHeap: SecureHeap, + keySize: Int, + purposes: UByte, + algorithm: String, + type: KeyType + ): OpenSSLKey { val dataSize = (keySize / 8).toULong() val rawDataPtr = secureHeap.allocate((keySize / 8).toULong()).reinterpret() if (RAND_bytes(rawDataPtr, 1) != 1) { throw Exception(ERR_func_error_string(ERR_get_error())?.toKString()) } - return OpenSSLKey(secureHeap, purposes, algorithm, rawDataPtr, dataSize) + return OpenSSLKey(secureHeap, purposes, algorithm, rawDataPtr, dataSize, type) } } diff --git a/kmp-advcrypto/src/linuxX64Main/kotlin/io/karma/advcrypto/linux/keys/OpenSSLPKey.kt b/kmp-advcrypto/src/linuxX64Main/kotlin/io/karma/advcrypto/linux/keys/OpenSSLPKey.kt index 600310e..71b660d 100644 --- a/kmp-advcrypto/src/linuxX64Main/kotlin/io/karma/advcrypto/linux/keys/OpenSSLPKey.kt +++ b/kmp-advcrypto/src/linuxX64Main/kotlin/io/karma/advcrypto/linux/keys/OpenSSLPKey.kt @@ -17,6 +17,7 @@ package io.karma.advcrypto.linux.keys import io.karma.advcrypto.keys.Key +import io.karma.advcrypto.keys.enum.KeyType import kotlinx.cinterop.CPointer import kotlinx.cinterop.ExperimentalForeignApi import kotlinx.cinterop.toKString @@ -28,7 +29,8 @@ import libssl.EVP_PKEY_get_base_id import libssl.OBJ_nid2sn @OptIn(ExperimentalForeignApi::class) -class OpenSSLPKey(private val rawKey: CPointer, override val purposes: UByte): Key { +class OpenSSLPKey(private val rawKey: CPointer, override val purposes: UByte, + override val type: KeyType): Key { override val algorithm: String = when(val baseId = EVP_PKEY_get_base_id(rawKey)) { EVP_PKEY_RSA -> "RSA" EVP_PKEY_ED25519 -> "ED25519" diff --git a/kmp-advcrypto/src/linuxX64Main/kotlin/io/karma/advcrypto/linux/providers/OpenSSLCryptoProvider.kt b/kmp-advcrypto/src/linuxX64Main/kotlin/io/karma/advcrypto/linux/providers/OpenSSLCryptoProvider.kt index bdfe0a7..01f9928 100644 --- a/kmp-advcrypto/src/linuxX64Main/kotlin/io/karma/advcrypto/linux/providers/OpenSSLCryptoProvider.kt +++ b/kmp-advcrypto/src/linuxX64Main/kotlin/io/karma/advcrypto/linux/providers/OpenSSLCryptoProvider.kt @@ -21,6 +21,7 @@ import io.karma.advcrypto.Providers import io.karma.advcrypto.algorithm.delegates.KeyGenContext import io.karma.advcrypto.keys.Key import io.karma.advcrypto.keys.KeyPair +import io.karma.advcrypto.keys.enum.KeyType import io.karma.advcrypto.linux.keys.OpenSSLKey import io.karma.advcrypto.linux.keys.OpenSSLPKey import io.karma.advcrypto.linux.utils.SecureHeap @@ -88,7 +89,8 @@ class OpenSSLCryptoProvider: AbstractProvider( secureHeap, context.generatorSpec.keySize?: defaultKeySize, context.generatorSpec.purposes, - "AES" + "AES", + KeyType.SECRET ) } } @@ -135,8 +137,16 @@ class OpenSSLCryptoProvider: AbstractProvider( // Return key pair KeyPair( - OpenSSLPKey(publicKey!!, (purposes and (Key.PURPOSE_ENCRYPT or Key.PURPOSE_VERIFY))), - OpenSSLPKey(privateKey!!, (purposes and (Key.PURPOSE_DECRYPT or Key.PURPOSE_SIGNING))) + OpenSSLPKey( + publicKey!!, + (purposes and (Key.PURPOSE_ENCRYPT or Key.PURPOSE_VERIFY)), + KeyType.PUBLIC + ), + OpenSSLPKey( + privateKey!!, + (purposes and (Key.PURPOSE_DECRYPT or Key.PURPOSE_SIGNING)), + KeyType.PRIVATE + ) ) }