2019-03-09 18:00:28 +00:00
|
|
|
#!/usr/bin/env python3
|
2019-04-18 14:27:27 +00:00
|
|
|
import json
|
2019-03-09 18:00:28 +00:00
|
|
|
import sys
|
|
|
|
from glob import glob
|
|
|
|
from hashlib import sha256
|
|
|
|
|
|
|
|
try:
|
|
|
|
opt = sys.argv[1]
|
2019-04-18 14:27:27 +00:00
|
|
|
except IndexError:
|
2019-03-09 18:00:28 +00:00
|
|
|
print("Usage: gen.py [core|mcu|check])")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
def c_bytes(h):
|
|
|
|
return "{ " + ", ".join(["0x%02x" % x for x in h]) + " }"
|
|
|
|
|
|
|
|
|
|
|
|
def gen_core(data):
|
2019-09-26 15:42:35 +00:00
|
|
|
print("# contents generated via script in")
|
|
|
|
print("# trezor-common/defs/webauthn/gen.py")
|
|
|
|
print("# do not edit manually")
|
|
|
|
print()
|
|
|
|
print("knownapps = {")
|
2019-03-09 18:00:28 +00:00
|
|
|
print(" # U2F")
|
|
|
|
for d in data:
|
2019-09-26 15:42:35 +00:00
|
|
|
for appid in d.get("u2f", []):
|
2019-09-26 14:18:47 +00:00
|
|
|
label = d["label"]
|
2019-09-26 15:42:35 +00:00
|
|
|
h = bytes.fromhex(appid)
|
|
|
|
print(" %s: {" % h)
|
|
|
|
print(' "label": "%s",' % label)
|
|
|
|
print(' "use_sign_count": True,')
|
|
|
|
print(" },")
|
2019-03-09 18:00:28 +00:00
|
|
|
print(" # WebAuthn")
|
|
|
|
for d in data:
|
2019-09-26 14:18:47 +00:00
|
|
|
for origin in d.get("webauthn", []):
|
2019-09-26 15:42:35 +00:00
|
|
|
h = sha256(origin.encode()).digest()
|
2019-09-26 14:18:47 +00:00
|
|
|
label, use_sign_count = (d["label"], d.get("use_sign_count", None))
|
2019-09-26 15:42:35 +00:00
|
|
|
print(" %s: {" % h)
|
|
|
|
print(' "label": "%s",' % label)
|
|
|
|
if use_sign_count is not None:
|
|
|
|
print(' "use_sign_count": %s,' % use_sign_count)
|
|
|
|
print(" },")
|
2019-03-09 18:00:28 +00:00
|
|
|
print("}")
|
|
|
|
|
|
|
|
|
|
|
|
def gen_mcu(data):
|
|
|
|
for d in data:
|
2019-09-26 15:42:35 +00:00
|
|
|
for appid in d.get("u2f", []):
|
2019-09-26 14:18:47 +00:00
|
|
|
label = d["label"]
|
2019-09-26 15:42:35 +00:00
|
|
|
h = bytes.fromhex(appid)
|
|
|
|
print('\t{\n\t\t// U2F\n\t\t%s,\n\t\t"%s"\n\t},' % (c_bytes(h), label))
|
2019-09-26 14:18:47 +00:00
|
|
|
for origin in d.get("webauthn", []):
|
|
|
|
label = d["label"]
|
2019-03-09 18:00:28 +00:00
|
|
|
h = sha256(origin.encode()).digest()
|
2019-04-18 14:27:27 +00:00
|
|
|
print(
|
|
|
|
'\t{\n\t\t// WebAuthn: %s\n\t\t%s,\n\t\t"%s"\n\t},'
|
|
|
|
% (origin, c_bytes(h), label)
|
|
|
|
)
|
2019-03-09 18:00:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
data = []
|
|
|
|
for fn in sorted(glob("apps/*.json")):
|
|
|
|
d = json.load(open(fn, "rt"))
|
|
|
|
data.append(d)
|
|
|
|
|
|
|
|
if opt == "core":
|
|
|
|
gen_core(data)
|
|
|
|
elif opt == "mcu":
|
|
|
|
gen_mcu(data)
|