From 9b3bffce5279d570c095ae0f9aa6f2862f71fa07 Mon Sep 17 00:00:00 2001 From: 5HT Date: Thu, 21 Nov 2024 04:01:28 +0200 Subject: [PATCH] decode --- lib/eudi/mdoc.ex | 49 +++++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/lib/eudi/mdoc.ex b/lib/eudi/mdoc.ex index bb9434b..411dba9 100644 --- a/lib/eudi/mdoc.ex +++ b/lib/eudi/mdoc.ex @@ -11,11 +11,12 @@ defmodule CA.MDoc do def parseMDocHex(x) do {:ok, hex} = :file.read_file x ; decode(:oid.unhex(hex)) end def testMDoc(x) do + x = :erlang.iolist_to_binary(x) {:ok, bin} = :file.read_file x case :filename.extension(x) do - '.b64' -> [bin: byte_size(:erlang.term_to_binary(:base64.decode(bin))), name: x] - '.b64u' -> [bin: byte_size(:erlang.term_to_binary(:base64.decode(replace(replace(bin,"_","/"),"-","+")))), name: x] - '.hex' -> [bin: byte_size(:erlang.term_to_binary(:oid.unhex(bin))), name: x] + ".b64" -> [bin: byte_size(:erlang.term_to_binary(:base64.decode(bin))), name: x] + ".b64u" -> [bin: byte_size(:erlang.term_to_binary(:base64.decode(replace(replace(bin,"_","/"),"-","+")))), name: x] + ".hex" -> [bin: byte_size(:erlang.term_to_binary(:oid.unhex(bin))), name: x] end end @@ -40,14 +41,17 @@ defmodule CA.MDoc do version = :maps.get("version", map, []) validityInfo = :maps.get("validityInfo", map, []) valueDigests = :maps.get("valueDigests", map, []) - [{_,nameSpacesList}] = :maps.to_list(valueDigests) + digests = case :maps.to_list(valueDigests) do + x when is_list(x) -> :lists.map(fn a -> :lists.map(fn {_,b} -> parseTag(b) end, :maps.to_list(a)) end, x) + x -> x + end [ docType: docType, digestAlgorithm: da, deviceKeyInfo: dki, validityInfo: validityInfo, status: status, - valueDigests: :lists.map(fn x -> parseTag(x) end, nameSpacesList), + valueDigests: digests, version: version, ] end @@ -87,26 +91,28 @@ defmodule CA.MDoc do def parseTag(%CBOR.Tag{tag: type1, value: %CBOR.Tag{tag: type2, value: bytes}}) when is_binary(bytes) do {:ok, cbor, _} = decode(bytes) - [tag: {type1, type2}, decoded: true, value: parseTag(cbor)] + [tag: {type1, type2}, decoded: true, value: parseMapValue(cbor)] end def parseTag(%CBOR.Tag{tag: type, value: bytes}) when is_binary(bytes) do {:ok, cbor, _} = decode(bytes) - [tag: type, decoded: true, value: parseTag(cbor)] + [tag: type, decoded: true, value: cbor] end - def parseTag(map) when is_map(map) do - case :maps.get("docType", map, []) do - [] -> case :maps.get("elementIdentifier", map, []) do - [] -> map - _ -> parseDigest(map) - end - _ -> parseDocType(map) - end - end + def parseTag(value) when is_binary(value) do :base64.encode(value) end def parseTag(value) do value end + def parseMapValue(map) when is_map(map) do + case :maps.get("elementIdentifier", map, []) do + [] -> case :maps.get("deviceKeyInfo", map, []) do + [] -> map + _ -> parseDocType(map) + end + _ -> parseDigest(map) + end + end + def parseMDoc(%{"deviceAuth"=> %{"deviceSignature" => tags}, "nameSpaces"=> ns}) do [ deviceAuth: [deviceSignature: :lists.map(fn x -> parseTag(x) end, tags)], @@ -133,7 +139,7 @@ defmodule CA.MDoc do ] end - def parseMDoc(%{"documents"=>documents}) do + def parseMDoc(%{"documents" => documents}) do :lists.map(fn x -> parseMDoc(x) end, documents) end @@ -142,16 +148,17 @@ defmodule CA.MDoc do end def parseMDocEnvelop(x) do + x = :erlang.iolist_to_binary(x) {:ok, bin} = :file.read_file x case :filename.extension(x) do - ".b64" -> {:ok, mDoc, _} = parseMDocB64(x) ; parseMDoc(mDoc) + ".b64" -> {:ok, mDoc, _} = parseMDocB64(x) ; parseMDoc(mDoc) ".b64u" -> {:ok, mDoc, _} = parseMDocB64U(x) ; parseMDoc(mDoc) - ".hex" -> {:ok, mDoc, _} = parseMDocHex(x) ; parseMDoc(mDoc) + ".hex" -> {:ok, mDoc, _} = parseMDocHex(x) ; parseMDoc(mDoc) end end - def test(folder \\ "cbor") do - :lists.map(fn x -> parseMDoc(x) end, :filelib.wildcard ['test/#{folder}/*']) + def test(folder \\ "mdoc") do + :lists.map(fn x -> testMDoc(x) end, :filelib.wildcard ['test/#{folder}/*']) end end