| |
| |
| |
| |
| |
| import sys, os, json, glob, hashlib, base64 |
| F = ("phoenix", "subject", "observation", "claim", "evidence") |
| def canon(o): return json.dumps({k: o[k] for k in F}, sort_keys=True, separators=(",", ":"), ensure_ascii=False).encode() |
| def find_ev(edir, uri): |
| for p in (os.path.join(edir, uri), os.path.join(edir, os.path.basename(uri))): |
| if os.path.isfile(p): return p |
| def check(path, key, edir): |
| try: |
| obj = json.load(open(path)) |
| if not isinstance(obj, dict): return "INVALID", ["objet non-dict"] |
| miss = [k for k in F + ("attestation",) if k not in obj] |
| if miss: return "INVALID", ["champs manquants: " + ",".join(miss)] |
| c, att, ev = obj["claim"], obj["attestation"], obj["evidence"] |
| if not (isinstance(c, dict) and isinstance(att, dict) and isinstance(ev, list)): return "INVALID", ["structure invalide (claim/attestation/evidence)"] |
| print("\n%s | %s" % (os.path.basename(path), str(c.get("text", ""))[:100])) |
| fail, d = [], hashlib.sha256(canon(obj)).hexdigest() |
| if att.get("digest") != d: fail.append("empreinte canonique differente") |
| kb = base64.b64decode(key.get("key", "") or "") |
| if key.get("alg") != "Ed25519" or len(kb) != 32: fail.append("cle attendue invalide") |
| if att.get("public_key_b64") != key.get("key"): fail.append("signe par une autre cle que la cle attendue") |
| try: |
| from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey |
| try: Ed25519PublicKey.from_public_bytes(kb).verify(base64.b64decode(att.get("signature", "")), ("PHOENIX_V0.1|" + d).encode()); print(" signature Ed25519: OK") |
| except Exception: fail.append("signature invalide") |
| except ImportError: fail.append("paquet cryptography absent (pip install cryptography)") |
| missing = 0 |
| for e in ev: |
| if not isinstance(e, dict): fail.append("preuve mal formee"); continue |
| p = find_ev(edir, str(e.get("uri", ""))) if edir else None |
| if not p: missing += 1; print(" preuve non fournie:", e.get("uri")); continue |
| ok = hashlib.sha256(open(p, "rb").read()).hexdigest() == e.get("sha256") |
| print(" preuve %s %s" % (os.path.basename(p), "OK" if ok else "ALTEREE")) |
| if not ok: fail.append("preuve alteree: " + os.path.basename(p)) |
| if fail: return "INVALID", fail |
| return ("VALID_SIGNATURE_ONLY" if missing else "VALID_FULL"), [] |
| except Exception as x: return "INVALID", ["entree illisible: %s" % type(x).__name__] |
| def main(): |
| a = sys.argv[1:] |
| if a: objs, kp, edir = [a[0]], (a[1] if len(a) > 1 else "legion-receipt-key.json"), (a[2] if len(a) > 2 else None) |
| else: |
| objs = sorted(glob.glob("objects/*.json")) or sorted(f for f in glob.glob("*.json") if f != "legion-receipt-key.json") |
| kp, edir = "legion-receipt-key.json", ("evidence" if os.path.isdir("evidence") else None) |
| try: key = json.load(open(kp)); assert isinstance(key, dict) |
| except Exception: print("usage: python3 phoenix_verify.py [objet.json cle.json [dossier_preuves]] (cle introuvable ou illisible)"); return 2 |
| if not objs: print("aucun objet trouve"); return 2 |
| n = {"VALID_FULL": 0, "VALID_SIGNATURE_ONLY": 0, "INVALID": 0} |
| for o in objs: |
| v, f = check(o, key, edir); n[v] += 1; print(" ->", v + ("" if not f else ": " + "; ".join(f))) |
| print("\nBILAN: %d objet(s) | VALID_FULL %d | VALID_SIGNATURE_ONLY %d | INVALID %d" % (len(objs), n["VALID_FULL"], n["VALID_SIGNATURE_ONLY"], n["INVALID"])) |
| return 1 if n["INVALID"] else 3 if n["VALID_SIGNATURE_ONLY"] else 0 |
| sys.exit(main()) |
|
|