Skip to content

Commit

Permalink
Change: Move public key from CoseSigned to CoseHeader
Browse files Browse the repository at this point in the history
  • Loading branch information
acrusage-iaik committed Dec 12, 2024
1 parent 27b32a2 commit 0a3c25a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package at.asitplus.signum.indispensable.cosef
import at.asitplus.catching
import at.asitplus.signum.indispensable.cosef.io.Base16Strict
import at.asitplus.signum.indispensable.cosef.io.coseCompliantSerializer
import at.asitplus.signum.indispensable.pki.X509Certificate
import io.matthewnelson.encoding.core.Encoder.Companion.encodeToString
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.SerialName
Expand Down Expand Up @@ -131,6 +132,23 @@ data class CoseHeader(
@SerialName("typ")
val type: String? = null,
) {
/**
* Tries to compute a public key in descending order from [jsonWebKey], [keyId],
* or [certificateChain], and takes the first success or null.
*/

/**
* Tries to compute a public key in descending order from [coseKey] or [certificateChain],
* and takes the first success or null.
*/
val publicKey: CoseKey?
get() = coseKey?.let { CoseKey.deserialize(it).getOrNull() }
?: kid?.let { CoseKey.fromDid(it.decodeToString()) }?.getOrNull()
?: certificateChain?.let {
runCatching {
X509Certificate.decodeFromDer(it)
}.getOrNull()?.publicKey?.toCoseKey()?.getOrThrow()
}

fun serialize() = coseCompliantSerializer.encodeToByteArray(this)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,6 @@ data class CoseSigned<P : Any?> internal constructor(
)
}

/**
* Tries to compute a public key in descending order from [coseKey] or [certificateChain],
* and takes the first success or null.
*/
val publicKey: CoseKey?
get() = combinedCoseHeader.run {
coseKey?.let { CoseKey.deserialize(it).getOrNull() }
?: kid?.let { CoseKey.fromDid(it.decodeToString()) }?.getOrNull()
?: certificateChain?.let {
runCatching {
X509Certificate.decodeFromDer(it)
}.getOrNull()?.publicKey?.toCoseKey()?.getOrThrow()
}
}

companion object {
fun <P : Any> deserialize(
parameterSerializer: KSerializer<P>,
Expand Down

0 comments on commit 0a3c25a

Please sign in to comment.