-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvalidate_hcert.py
60 lines (53 loc) · 1.89 KB
/
validate_hcert.py
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
#!/bin/env python3.9
import sys
from base45 import b45decode
import zlib
from cbor2 import loads
from cose.messages import Sign1Message
from classes.SchemaValidator import SchemaValidator
from classes.SignatureValidator import SignatureValidator
from classes.TrustList import TrustList, UnknownKidError
# Initialize components
SCHEMA_VALIDATOR = SchemaValidator.create()
SIGNATURE_VALIDATOR = SignatureValidator(TrustList.load("trustlist.json"))
sys.stdin.reconfigure(encoding='utf-8')
def unpack_qr(qr_text):
compressed_bytes = b45decode(qr_text[4:])
print("..b45-decode OK")
cose_bytes = zlib.decompress(compressed_bytes)
print("..zlib.decompress OK")
cose_message = Sign1Message.decode(cose_bytes)
print("..cose.decode OK")
cbor_message = loads(cose_message.payload)
print("..cbor.load OK")
print(cbor_message)
return {
"COSE": cose_bytes.hex(),
"JSON": cbor_message[-260][1]
}
for line in sys.stdin:
data = line.rstrip("\r\n").rstrip("\n")
print()
print(f"Validating: [{data}]")
try:
json = unpack_qr(data)
# Signature
result = SIGNATURE_VALIDATOR.validate(bytes.fromhex(json["COSE"]))
if result["valid"]:
print("Successfully validated signature!")
else:
print("Signature validation failed!")
# Schema
json_payload = json["JSON"]
schema_ver = json_payload['ver']
result = SCHEMA_VALIDATOR.validate(json_payload)
if result["valid"]:
print(f"Successfully validated schema! The file conforms to schema { schema_ver }")
else:
print(f"Schema validation failed! The file does not conform to schema { schema_ver }")
except UnknownKidError as error:
print("Error! KID not found")
print(error)
except Exception as error:
print("Error! Something went very wrong!")
print(error)