-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathverifier.py
More file actions
209 lines (159 loc) · 6.03 KB
/
verifier.py
File metadata and controls
209 lines (159 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import binascii
import cbor2
import logging
from typing import List
from pymdoccbor.exceptions import InvalidMdoc
from pymdoccbor.mdoc.issuersigned import IssuerSigned
from pymdoccbor.mdoc.exceptions import NoDocumentTypeProvided, NoSignedDocumentProvided
logger = logging.getLogger('pymdoccbor')
class MobileDocument:
"""
MobileDocument class to handle the Mobile Document
"""
_states = {
True: "valid",
False: "failed",
}
def __init__(self, docType: str, issuerSigned: dict, deviceSigned: dict = {}) -> None:
"""
Initialize the MobileDocument object
:param docType: str: the document type
:param issuerSigned: dict: the issuerSigned info
:param deviceSigned: dict: the deviceSigned info
"""
if not docType:
raise NoDocumentTypeProvided("You must provide a document type")
self.doctype: str = docType # eg: 'org.iso.18013.5.1.mDL'
if not issuerSigned:
raise NoSignedDocumentProvided("You must provide a signed document")
self.issuersigned: List[IssuerSigned] = IssuerSigned(**issuerSigned)
self.is_valid = False
self.devicesigned: dict = deviceSigned
def dump(self) -> dict:
"""
It returns the document as a dict
:return: dict: the document as a dict
"""
return {
'docType': self.doctype,
'issuerSigned': self.issuersigned.dump()
}
def dumps(self) -> bytes:
"""
It returns the AF binary repr as bytes
:return: bytes: the document as bytes
"""
return binascii.hexlify(self.dump())
def dump(self) -> bytes:
"""
It returns the document as bytes
:return: dict: the document as bytes
"""
return cbor2.dumps(
cbor2.CBORTag(
24,
value={
'docType': self.doctype,
'issuerSigned': self.issuersigned.dumps()
}
)
)
def verify(self) -> bool:
"""
Verify the document signature
:return: bool: True if the signature is valid, False otherwise
"""
self.is_valid = self.issuersigned.issuer_auth.verify_signature()
return self.is_valid
def __repr__(self) -> str:
return f"{self.__module__}.{self.__class__.__name__} [{self._states[self.is_valid]}]"
class MdocCbor:
"""
MdocCbor class to handle the Mobile Document
"""
def __init__(self) -> None:
"""
Initialize the MdocCbor object
"""
self.data_as_bytes: bytes = b""
self.data_as_cbor_dict: dict = {}
self.documents: List[MobileDocument] = []
self.documents_invalid: list = []
self.disclosure_map: dict = {}
def loads(self, data: str) -> None:
"""
Load the data from a AF Binary string
:param data: str: the AF binary string
"""
if isinstance(data, bytes):
data = binascii.hexlify(data)
self.data_as_bytes = binascii.unhexlify(data)
self.data_as_cbor_dict = cbor2.loads(self.data_as_bytes)
def dump(self) -> bytes:
"""
Returns the CBOR representation of the mdoc as bytes
"""
return self.data_as_bytes
def dumps(self) -> bytes:
"""
Returns the AF binary representation of the mdoc as bytes
:return: bytes: the AF binary representation of the mdoc
"""
return binascii.hexlify(self.data_as_bytes)
@property
def data_as_string(self) -> str:
return self.dumps().decode()
def _decode_claims(self, claims: list[dict]) -> dict:
decoded_claims = {}
for claim in claims:
decoded = cbor2.loads(claim.value)
if isinstance(decoded['elementValue'], cbor2.CBORTag):
decoded_claims[decoded['elementIdentifier']] = decoded['elementValue'].value
elif isinstance(decoded['elementValue'], list):
claims_list = []
for element in decoded['elementValue']:
claims_dict = {}
for key, value in element.items():
if isinstance(value, cbor2.CBORTag):
claims_dict[key] = value.value
else:
claims_dict[key] = value
claims_list.append(claims_dict)
decoded_claims[decoded['elementIdentifier']] = claims_list
else:
decoded_claims[decoded['elementIdentifier']] = decoded['elementValue']
return decoded_claims
def verify(self) -> bool:
""""
Verify signatures of all documents contained in the mdoc
:return: bool: True if all signatures are valid, False otherwise
"""
cdict = self.data_as_cbor_dict
for i in ('version', 'documents'):
if i not in cdict:
raise InvalidMdoc(
f"Mdoc is invalid since it doesn't contain the '{i}' element"
)
doc_cnt = 1
for doc in cdict['documents']:
mso = MobileDocument(**doc)
try:
if mso.verify():
self.documents.append(mso)
else:
self.documents_invalid.append(mso)
for namespace, claims in mso.issuersigned.namespaces.items():
self.disclosure_map[namespace] = self._decode_claims(claims)
except Exception as e:
logger.error(
f"COSE Sign1 validation failed to the document number #{doc_cnt}. "
f"Then it is appended to self.documents_invalid: {e}"
)
self.documents_invalid.append(doc)
doc_cnt += 1
return False if self.documents_invalid else True
def __repr__(self) -> str:
return (
f"{self.__module__}.{self.__class__.__name__} "
f"[{len(self.documents)} valid documents]"
)