Skip to content

Commit 986cef8

Browse files
committed
add workarounds for mdoc to allow malformed strucure for now
Signed-off-by: Johannes Tuerk <johannes.tuerk@lissi.id>
1 parent 41a16ee commit 986cef8

File tree

4 files changed

+62
-11
lines changed

4 files changed

+62
-11
lines changed

src/WalletFramework.MdocLib/IssuerSigned.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@ private IssuerSigned(NameSpaces nameSpaces, IssuerAuth issuerAuth)
2222
private static IssuerSigned Create(NameSpaces nameSpaces, IssuerAuth issuerAuth) =>
2323
new(nameSpaces, issuerAuth);
2424

25-
internal static Validation<IssuerSigned> ValidIssuerSigned(CBORObject mdoc) =>
26-
mdoc.GetByLabel(IssuerSignedLabel).OnSuccess(issuerSigned =>
27-
Valid(Create)
28-
.Apply(ValidNameSpaces(issuerSigned))
29-
.Apply(ValidIssuerAuth(issuerSigned))
30-
);
25+
public static Validation<IssuerSigned> ValidIssuerSigned(CBORObject issuerSigned) =>
26+
Valid(Create)
27+
.Apply(ValidNameSpaces(issuerSigned))
28+
.Apply(ValidIssuerAuth(issuerSigned));
3129

3230
public CBORObject Encode()
3331
{
@@ -39,3 +37,13 @@ public CBORObject Encode()
3937
return cbor;
4038
}
4139
}
40+
41+
public static class IssuerSignedFun
42+
{
43+
// TODO: This is only a hack currently, the doctype of the mdoc and the mso must be validated normally
44+
public static Mdoc ToMdoc(this IssuerSigned issuerSigned)
45+
{
46+
var docType = issuerSigned.IssuerAuth.Payload.DocType;
47+
return new Mdoc(docType, issuerSigned);
48+
}
49+
}

src/WalletFramework.MdocLib/Mdoc.cs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public record Mdoc
2020
// TODO: mdoc authentication
2121
// public DeviceSigned DeviceSigned { get; }
2222

23-
private Mdoc(DocType docType, IssuerSigned issuerSigned)
23+
public Mdoc(DocType docType, IssuerSigned issuerSigned)
2424
{
2525
DocType = docType;
2626
IssuerSigned = issuerSigned;
@@ -68,12 +68,55 @@ public static Validation<Mdoc> ValidMdoc(string base64UrlencodedCborByteString)
6868
return
6969
from bytes in decodeBase64Url(base64UrlencodedCborByteString)
7070
from cbor in parseCborByteString(bytes)
71+
from issuerSigned in cbor.GetByLabel(IssuerSignedLabel)
7172
from mdoc in Valid(Create)
7273
.Apply(ValidDoctype(cbor))
73-
.Apply(ValidIssuerSigned(cbor))
74+
.Apply(ValidIssuerSigned(issuerSigned))
7475
from validMdoc in validateIntegrity(mdoc)
7576
select validMdoc;
7677
}
78+
79+
//TODO: Workaround because PId Issuer only implemented issuer signed, Delete this overload when PID Issuer is fixed!!
80+
public static Validation<Mdoc> FromIssuerSigned(string base64UrlencodedCborByteString)
81+
{
82+
var decodeBase64Url = new Func<string, Validation<byte[]>>(str =>
83+
{
84+
try
85+
{
86+
return Base64UrlEncoder.DecodeBytes(str)!;
87+
}
88+
catch (Exception e)
89+
{
90+
return new InvalidBase64UrlEncodingError(e);
91+
}
92+
});
93+
94+
var parseCborByteString = new Func<byte[], Validation<CBORObject>>(bytes =>
95+
{
96+
try
97+
{
98+
return CBORObject.DecodeFromBytes(bytes);
99+
}
100+
catch (Exception e)
101+
{
102+
return new InvalidCborByteStringError("mdocResponse", e);
103+
}
104+
});
105+
106+
var validateIntegrity = new List<Validator<Mdoc>>
107+
{
108+
MdocFun.DocTypeMatches,
109+
MdocFun.DigestsMatch
110+
}
111+
.AggregateValidators();
112+
113+
return
114+
from bytes in decodeBase64Url(base64UrlencodedCborByteString)
115+
from cbor in parseCborByteString(bytes)
116+
from issuerSigned in ValidIssuerSigned(cbor)
117+
from validMdoc in validateIntegrity(issuerSigned.ToMdoc())
118+
select validMdoc;
119+
}
77120

78121
public record InvalidBase64UrlEncodingError(Exception E) : Error("String is not Base64UrlEncoded", E);
79122
}

src/WalletFramework.MdocVc/MdocRecord.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ public static class MdocRecordFun
7575
public static MdocRecord DecodeFromJson(JObject json)
7676
{
7777
var id = json[nameof(RecordBase.Id)]!.ToString();
78-
78+
7979
var mdocStr = json[MdocJsonKey]!.ToString();
8080
var mdoc = Mdoc
8181
.ValidMdoc(mdocStr)
8282
.UnwrapOrThrow(new InvalidOperationException($"The MdocRecord with ID: {id} is corrupt"));
83-
83+
8484
var displays =
8585
from jToken in json.GetByKey(MdocDisplaysJsonKey).ToOption()
8686
from jArray in jToken.ToJArray().ToOption()

src/WalletFramework.Oid4Vc/Oid4Vci/CredResponse/Mdoc/EncodedMdoc.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static Validation<EncodedMdoc> ValidEncodedMdoc(JValue mdoc)
2525
var str = mdoc.ToString(CultureInfo.InvariantCulture);
2626

2727
return MdocLib.Mdoc
28-
.ValidMdoc(str)
28+
.FromIssuerSigned(str)
2929
.OnSuccess(mdoc1 => new EncodedMdoc(str, mdoc1));
3030
}
3131
}

0 commit comments

Comments
 (0)