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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# sovereign_external_verifier_v11.py
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
import argparse
|
| 6 |
+
import json
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
from typing import Any, Dict, List, Optional
|
| 9 |
+
|
| 10 |
+
from sovereign_crypto_seal import SovereignCryptoSeal
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def _load_json(path: str) -> Dict[str, Any]:
|
| 14 |
+
p = Path(path)
|
| 15 |
+
if not p.exists():
|
| 16 |
+
raise FileNotFoundError(f"File not found: {path}")
|
| 17 |
+
data = json.loads(p.read_text(encoding="utf-8"))
|
| 18 |
+
if not isinstance(data, dict):
|
| 19 |
+
raise ValueError(f"JSON root must be an object: {path}")
|
| 20 |
+
return data
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
class SovereignExternalVerifierV11:
|
| 24 |
+
"""
|
| 25 |
+
Extended independent verifier for Sovereign artifacts (bank-grade).
|
| 26 |
+
|
| 27 |
+
Added in v11:
|
| 28 |
+
- Hardening profile verification
|
| 29 |
+
- Hardening report verification
|
| 30 |
+
"""
|
| 31 |
+
|
| 32 |
+
def __init__(self, sealer: Optional[SovereignCryptoSeal] = None) -> None:
|
| 33 |
+
self.sealer = sealer or SovereignCryptoSeal()
|
| 34 |
+
|
| 35 |
+
# ------------------------------------------------------------------
|
| 36 |
+
# Core
|
| 37 |
+
# ------------------------------------------------------------------
|
| 38 |
+
def _verify_object_with_seal(
|
| 39 |
+
self,
|
| 40 |
+
*,
|
| 41 |
+
artifact_type: str,
|
| 42 |
+
obj: Dict[str, Any],
|
| 43 |
+
seal: Dict[str, Any],
|
| 44 |
+
) -> Dict[str, Any]:
|
| 45 |
+
verified = self.sealer.verify_object(obj, seal)
|
| 46 |
+
return {
|
| 47 |
+
"ok": bool(verified.get("ok")),
|
| 48 |
+
"artifact_type": artifact_type,
|
| 49 |
+
"verified": bool(verified.get("verified")),
|
| 50 |
+
"verification": verified,
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
# ------------------------------------------------------------------
|
| 54 |
+
# Hardening
|
| 55 |
+
# ------------------------------------------------------------------
|
| 56 |
+
def verify_hardening_profile_bundle(self, bundle: Dict[str, Any]) -> Dict[str, Any]:
|
| 57 |
+
seal = bundle.get("crypto_seal")
|
| 58 |
+
profile = bundle.get("hardening_profile")
|
| 59 |
+
|
| 60 |
+
if not isinstance(seal, dict):
|
| 61 |
+
return {
|
| 62 |
+
"ok": False,
|
| 63 |
+
"artifact_type": "hardening_profile_bundle",
|
| 64 |
+
"verified": False,
|
| 65 |
+
"error": "missing_hardening_crypto_seal",
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
if not isinstance(profile, dict):
|
| 69 |
+
return {
|
| 70 |
+
"ok": False,
|
| 71 |
+
"artifact_type": "hardening_profile_bundle",
|
| 72 |
+
"verified": False,
|
| 73 |
+
"error": "missing_hardening_profile",
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
return self._verify_object_with_seal(
|
| 77 |
+
artifact_type="hardening_profile_bundle",
|
| 78 |
+
obj=profile,
|
| 79 |
+
seal=seal,
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
def verify_hardening_report_bundle(self, bundle: Dict[str, Any]) -> Dict[str, Any]:
|
| 83 |
+
inner = bundle.get("bundle")
|
| 84 |
+
if not isinstance(inner, dict):
|
| 85 |
+
return {
|
| 86 |
+
"ok": False,
|
| 87 |
+
"artifact_type": "hardening_report_bundle",
|
| 88 |
+
"verified": False,
|
| 89 |
+
"error": "missing_inner_hardening_bundle",
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
verified = self.verify_hardening_profile_bundle(inner)
|
| 93 |
+
return {
|
| 94 |
+
"ok": bool(verified.get("ok")),
|
| 95 |
+
"artifact_type": "hardening_report_bundle",
|
| 96 |
+
"verified": bool(verified.get("verified")),
|
| 97 |
+
"inner_verification": verified,
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
# ------------------------------------------------------------------
|
| 101 |
+
# Runtime (minimal reuse)
|
| 102 |
+
# ------------------------------------------------------------------
|
| 103 |
+
def verify_runtime_result_bundle(self, bundle: Dict[str, Any]) -> Dict[str, Any]:
|
| 104 |
+
seal = bundle.get("crypto_seal")
|
| 105 |
+
|
| 106 |
+
if not isinstance(seal, dict):
|
| 107 |
+
return {
|
| 108 |
+
"ok": False,
|
| 109 |
+
"artifact_type": "runtime_result",
|
| 110 |
+
"verified": False,
|
| 111 |
+
"error": "missing_runtime_crypto_seal",
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
obj = dict(bundle)
|
| 115 |
+
obj.pop("crypto_seal", None)
|
| 116 |
+
obj.pop("sealed", None)
|
| 117 |
+
|
| 118 |
+
return self._verify_object_with_seal(
|
| 119 |
+
artifact_type="runtime_result",
|
| 120 |
+
obj=obj,
|
| 121 |
+
seal=seal,
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
# ------------------------------------------------------------------
|
| 125 |
+
# Technical Identity (extended)
|
| 126 |
+
# ------------------------------------------------------------------
|
| 127 |
+
def verify_technical_identity_bundle(self, bundle: Dict[str, Any]) -> Dict[str, Any]:
|
| 128 |
+
identity = bundle.get("technical_identity")
|
| 129 |
+
if not isinstance(identity, dict):
|
| 130 |
+
return {
|
| 131 |
+
"ok": False,
|
| 132 |
+
"artifact_type": "technical_identity_bundle",
|
| 133 |
+
"verified": False,
|
| 134 |
+
"error": "missing_technical_identity",
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
seal = identity.get("crypto_seal")
|
| 138 |
+
if not isinstance(seal, dict):
|
| 139 |
+
return {
|
| 140 |
+
"ok": False,
|
| 141 |
+
"artifact_type": "technical_identity_bundle",
|
| 142 |
+
"verified": False,
|
| 143 |
+
"error": "missing_technical_identity_seal",
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
identity_obj = dict(identity)
|
| 147 |
+
identity_obj.pop("crypto_seal", None)
|
| 148 |
+
identity_obj.pop("sealed", None)
|
| 149 |
+
|
| 150 |
+
identity_verification = self._verify_object_with_seal(
|
| 151 |
+
artifact_type="technical_identity",
|
| 152 |
+
obj=identity_obj,
|
| 153 |
+
seal=seal,
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
nested_checks: List[Dict[str, Any]] = []
|
| 157 |
+
|
| 158 |
+
hardening_bundle = bundle.get("hardening_bundle")
|
| 159 |
+
if isinstance(hardening_bundle, dict):
|
| 160 |
+
if "hardening_profile" in hardening_bundle and "crypto_seal" in hardening_bundle:
|
| 161 |
+
nested_checks.append(self.verify_hardening_profile_bundle(hardening_bundle))
|
| 162 |
+
elif "bundle" in hardening_bundle:
|
| 163 |
+
nested_checks.append(self.verify_hardening_report_bundle(hardening_bundle))
|
| 164 |
+
|
| 165 |
+
runtime_result = bundle.get("runtime_result")
|
| 166 |
+
if isinstance(runtime_result, dict):
|
| 167 |
+
nested_checks.append(self.verify_runtime_result_bundle(runtime_result))
|
| 168 |
+
|
| 169 |
+
all_nested_ok = all(bool(x.get("verified")) for x in nested_checks) if nested_checks else True
|
| 170 |
+
|
| 171 |
+
return {
|
| 172 |
+
"ok": bool(identity_verification.get("ok")) and all_nested_ok,
|
| 173 |
+
"artifact_type": "technical_identity_bundle",
|
| 174 |
+
"verified": bool(identity_verification.get("verified")) and all_nested_ok,
|
| 175 |
+
"technical_identity_verification": identity_verification,
|
| 176 |
+
"nested_verifications": nested_checks,
|
| 177 |
+
}
|
| 178 |
+
|
| 179 |
+
# ------------------------------------------------------------------
|
| 180 |
+
# Auto detect
|
| 181 |
+
# ------------------------------------------------------------------
|
| 182 |
+
def verify_auto(self, obj: Dict[str, Any]) -> Dict[str, Any]:
|
| 183 |
+
if "technical_identity" in obj:
|
| 184 |
+
return self.verify_technical_identity_bundle(obj)
|
| 185 |
+
|
| 186 |
+
if "bundle" in obj and isinstance(obj.get("bundle"), dict):
|
| 187 |
+
inner = obj["bundle"]
|
| 188 |
+
|
| 189 |
+
if "hardening_profile" in inner and "crypto_seal" in inner:
|
| 190 |
+
return self.verify_hardening_report_bundle(obj)
|
| 191 |
+
|
| 192 |
+
if "hardening_profile" in obj and "crypto_seal" in obj:
|
| 193 |
+
return self.verify_hardening_profile_bundle(obj)
|
| 194 |
+
|
| 195 |
+
if "crypto_seal" in obj:
|
| 196 |
+
return self.verify_runtime_result_bundle(obj)
|
| 197 |
+
|
| 198 |
+
return {
|
| 199 |
+
"ok": False,
|
| 200 |
+
"verified": False,
|
| 201 |
+
"artifact_type": "unknown",
|
| 202 |
+
"error": "could_not_detect_artifact_type",
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
def verify_file(self, path: str) -> Dict[str, Any]:
|
| 206 |
+
obj = _load_json(path)
|
| 207 |
+
result = self.verify_auto(obj)
|
| 208 |
+
result["source_file"] = path
|
| 209 |
+
return result
|
| 210 |
+
|
| 211 |
+
|
| 212 |
+
VERIFIER = SovereignExternalVerifierV11()
|
| 213 |
+
|
| 214 |
+
|
| 215 |
+
def verify_artifact_file_v11(path: str) -> Dict[str, Any]:
|
| 216 |
+
return VERIFIER.verify_file(path)
|
| 217 |
+
|
| 218 |
+
|
| 219 |
+
def _build_arg_parser() -> argparse.ArgumentParser:
|
| 220 |
+
parser = argparse.ArgumentParser(
|
| 221 |
+
description="Verifier v11 (with hardening support)."
|
| 222 |
+
)
|
| 223 |
+
parser.add_argument("--file", required=True)
|
| 224 |
+
return parser
|
| 225 |
+
|
| 226 |
+
|
| 227 |
+
if __name__ == "__main__":
|
| 228 |
+
parser = _build_arg_parser()
|
| 229 |
+
args = parser.parse_args()
|
| 230 |
+
|
| 231 |
+
result = verify_artifact_file_v11(args.file)
|
| 232 |
+
print(json.dumps(result, indent=2, ensure_ascii=False))
|
| 233 |
+
|