Skip to content

Commit

Permalink
start adding KmmResult to key conversion functions
Browse files Browse the repository at this point in the history
  • Loading branch information
JesusMcCloud committed Oct 30, 2023
1 parent db98183 commit fdde62b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package at.asitplus.crypto.datatypes.cose

import at.asitplus.KmmResult
import at.asitplus.crypto.datatypes.CryptoPublicKey
import at.asitplus.crypto.datatypes.EcCurve
import at.asitplus.crypto.datatypes.asn1.encodeToByteArray
Expand Down Expand Up @@ -102,7 +103,7 @@ data class CoseKey(
curve: CoseEllipticCurve,
x: ByteArray,
y: ByteArray
): CoseKey? = CryptoPublicKey.Ec.fromCoordinates(curve.toJwkCurve(), x, y).toCoseKey()
): CoseKey? = CryptoPublicKey.Ec.fromCoordinates(curve.toJwkCurve(), x, y).toCoseKey().getOrNull()

}
}
Expand All @@ -111,42 +112,44 @@ data class CoseKey(
* Converts CryptoPublicKey into CoseKey
* If [algorithm] is not set then key can be used for any algorithm with same kty (RFC 8152), returns null for invalid kty/algorithm pairs
*/
fun CryptoPublicKey.toCoseKey(algorithm: CoseAlgorithm? = null): CoseKey? =
fun CryptoPublicKey.toCoseKey(algorithm: CoseAlgorithm? = null): KmmResult<CoseKey> =
when (this) {
is CryptoPublicKey.Ec ->
if ((algorithm != null) && (algorithm != when (curve) {
EcCurve.SECP_256_R_1 -> CoseAlgorithm.ES256
EcCurve.SECP_384_R_1 -> CoseAlgorithm.ES384
EcCurve.SECP_521_R_1 -> CoseAlgorithm.ES512
})
)
null
else CoseKey(
keyParams = CoseKeyParams.EcYByteArrayParams(
curve = curve.toCoseCurve(),
x = x,
y = y
),
type = CoseKeyType.EC2,
keyId = keyId.encodeToByteArray(),
algorithm = algorithm
) KmmResult.failure(IllegalArgumentException("Algorithm and Key Type mismatch!"))
else KmmResult.success(
CoseKey(
keyParams = CoseKeyParams.EcYByteArrayParams(
curve = curve.toCoseCurve(),
x = x,
y = y
),
type = CoseKeyType.EC2,
keyId = keyId.encodeToByteArray(),
algorithm = algorithm
)
)

is CryptoPublicKey.Rsa ->
if ((algorithm != null) && (algorithm !in listOf(
CoseAlgorithm.PS256, CoseAlgorithm.PS384, CoseAlgorithm.PS512,
CoseAlgorithm.RS256, CoseAlgorithm.RS384, CoseAlgorithm.RS512
))
)
null
else CoseKey(
keyParams = CoseKeyParams.RsaParams(
n = n,
e = e.encodeToByteArray()
),
type = CoseKeyType.RSA,
keyId = keyId.encodeToByteArray(),
algorithm = algorithm
) KmmResult.failure(IllegalArgumentException("Algorithm and Key Type mismatch!"))
else KmmResult.success(
CoseKey(
keyParams = CoseKeyParams.RsaParams(
n = n,
e = e.encodeToByteArray()
),
type = CoseKeyType.RSA,
keyId = keyId.encodeToByteArray(),
algorithm = algorithm
)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ interface Asn1Decodable<A : Asn1Element, T : Asn1Encodable<A>> {
interface Asn1TagVerifyingDecodable<T:Asn1Encodable<Asn1Primitive>> : Asn1Decodable<Asn1Primitive, T> {

/**
* Same as [Asn1Decodable.decodeFromTlv], but allows overriding the tag, shoudl the implementing class verify it.
* Same as [Asn1Decodable.decodeFromTlv], but allows overriding the tag, should the implementing class verify it.
* Useful for implicit tagging.
*/
@Throws(Throwable::class)
fun decodeFromTlv(src: Asn1Primitive, tagOverride: UByte?): T

/**
* Same as [Asn1Decodable.derDecode], but allows overriding the tag, shoudl the implementing class verify it.
* Same as [Asn1Decodable.derDecode], but allows overriding the tag, should the implementing class verify it.
* Useful for implicit tagging.
*/
@Throws(Throwable::class)
Expand Down

0 comments on commit fdde62b

Please sign in to comment.