Skip to content

Commit

Permalink
Implement ISO mdoc request and response
Browse files Browse the repository at this point in the history
  • Loading branch information
nodh committed Jul 20, 2023
1 parent 7285678 commit 7007c21
Show file tree
Hide file tree
Showing 8 changed files with 725 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ enum class CoseAlgorithm(val value: Int) {

ES256(-7),
ES384(-35),
ES512(-36);
ES512(-36),
HMAC256_256(5);

val signatureValueLength
get() = when (this) {
ES256 -> 256 / 8
ES384 -> 384 / 8
ES512 -> 512 / 8
else -> throw IllegalArgumentException(this.toString())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,13 @@ data class CoseHeader(
}

override fun toString(): String {
return "CoseHeader(algorithm=$algorithm, criticalHeaders=$criticalHeaders, contentType=$contentType, kid=$kid, iv=${iv?.encodeBase16()}, partialIv=${partialIv?.encodeBase16()}, certificateChain=${certificateChain?.encodeBase16()})"
return "CoseHeader(algorithm=$algorithm," +
" criticalHeaders=$criticalHeaders," +
" contentType=$contentType," +
" kid=$kid," +
" iv=${iv?.encodeBase16()}," +
" partialIv=${partialIv?.encodeBase16()}," +
" certificateChain=${certificateChain?.encodeBase16()})"
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ data class CoseSigned(
val protectedHeader: ByteStringWrapper<CoseHeader>,
val unprotectedHeader: CoseHeader? = null,
@ByteString
val payload: ByteArray,
val payload: ByteArray? = null,
@ByteString
val signature: ByteArray,
) {
Expand All @@ -46,23 +46,28 @@ data class CoseSigned(

if (protectedHeader != other.protectedHeader) return false
if (unprotectedHeader != other.unprotectedHeader) return false
if (!payload.contentEquals(other.payload)) return false
if (payload != null) {
if (other.payload == null) return false
if (!payload.contentEquals(other.payload)) return false
} else if (other.payload != null) return false
return signature.contentEquals(other.signature)
}

override fun hashCode(): Int {
var result = protectedHeader.hashCode()
result = 31 * result + (unprotectedHeader?.hashCode() ?: 0)
result = 31 * result + payload.contentHashCode()
result = 31 * result + (payload?.contentHashCode() ?: 0)
result = 31 * result + signature.contentHashCode()
return result
}

override fun toString(): String {
return "CoseSigned(protectedHeader=$protectedHeader, unprotectedHeader=$unprotectedHeader, payload=${payload.encodeBase16()}, signature=${signature.encodeBase16()})"
return "CoseSigned(protectedHeader=${protectedHeader.value}," +
" unprotectedHeader=$unprotectedHeader," +
" payload=${payload?.encodeBase16()}," +
" signature=${signature.encodeBase16()})"
}


companion object {
fun deserialize(it: ByteArray) = kotlin.runCatching {
cborSerializer.decodeFromByteArray<CoseSigned>(it)
Expand All @@ -84,7 +89,7 @@ data class CoseSignatureInput(
@ByteString
val externalAad: ByteArray = byteArrayOf(),
@ByteString
val payload: ByteArray,
val payload: ByteArray? = null,
){
fun serialize() = cborSerializer.encodeToByteArray(this)

Expand All @@ -97,17 +102,30 @@ data class CoseSignatureInput(
if (contextString != other.contextString) return false
if (protectedHeader != other.protectedHeader) return false
if (!externalAad.contentEquals(other.externalAad)) return false
return payload.contentEquals(other.payload)
if (payload != null) {
if (other.payload == null) return false
if (!payload.contentEquals(other.payload)) return false
} else if (other.payload != null) return false

return true
}

override fun hashCode(): Int {
var result = contextString.hashCode()
result = 31 * result + protectedHeader.hashCode()
result = 31 * result + externalAad.contentHashCode()
result = 31 * result + payload.contentHashCode()
result = 31 * result + (payload?.contentHashCode() ?: 0)
return result
}

override fun toString(): String {
return "CoseSignatureInput(contextString='$contextString'," +
" protectedHeader=${protectedHeader.value}," +
" externalAad=${externalAad.encodeBase16()}," +
" payload=${payload?.encodeBase16()})"
}


companion object {
fun deserialize(it: ByteArray) = kotlin.runCatching {
cborSerializer.decodeFromByteArray<CoseSignatureInput>(it)
Expand All @@ -124,13 +142,13 @@ object ByteStringWrapperCoseHeaderSerializer : KSerializer<ByteStringWrapper<Cos
PrimitiveSerialDescriptor("ByteStringWrapperCoseHeaderSerializer", PrimitiveKind.STRING)

override fun serialize(encoder: Encoder, value: ByteStringWrapper<CoseHeader>) {
val bytes = Cbor.encodeToByteArray(value.value)
val bytes = cborSerializer.encodeToByteArray(value.value)
encoder.encodeSerializableValue(ByteArraySerializer(), bytes)
}

override fun deserialize(decoder: Decoder): ByteStringWrapper<CoseHeader> {
val bytes = decoder.decodeSerializableValue(ByteArraySerializer())
return ByteStringWrapper(Cbor.decodeFromByteArray(bytes), bytes)
return ByteStringWrapper(cborSerializer.decodeFromByteArray(bytes), bytes)
}

}
Loading

0 comments on commit 7007c21

Please sign in to comment.