OWNER_HOSTS (homelab: hub.gabvdl.xyz) answers as the owner without Authelia, for the one device that can't be on the personal tailnet (the Orus Mac). On it the gate takes only the owner's __Host-aia_owner session (7 days, host-only, Strict) — no Remote-User, host token or guest cookie is anyone; without one only the sign-in answers. - remoteaccess/webauthn.py: WebAuthn checked by the hub on `cryptography` (challenge, exact origin, RP ID, user present + verified, signature, counter); a small CBOR reader for new passkeys; fuzzed (80k inputs). - passkeys added only through a one-time link made on the admin host (15 min, one use, hashed, fragment-borne); no mail fallback — Pocket ID was rejected because a mailed code there can enrol a passkey. - Settings → Security: Internet access off (drops every session, back on from the admin host only); pushes on every sign-in and passkey added. - machine paths (worker proxy tokens, tool proxy) refused there. - tests: test_remote_access.py (31), test_webauthn.py (7). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
179 lines
7.6 KiB
Python
179 lines
7.6 KiB
Python
"""remoteaccess/webauthn.py against a software authenticator (real P-256 and
|
|
Ed25519 keys, CBOR built here): a passkey added and used, and every check
|
|
that must refuse — origin, challenge, RP ID, user verification, ceremony
|
|
type, signature, a counter going backwards, a mismatched credential id."""
|
|
|
|
import hashlib
|
|
import json
|
|
import os
|
|
import struct
|
|
import unittest
|
|
|
|
from cryptography.hazmat.primitives import hashes
|
|
from cryptography.hazmat.primitives.asymmetric import ec, ed25519
|
|
|
|
from remoteaccess import webauthn as wa
|
|
|
|
RP, ORIGIN = "hub.example.test", "https://hub.example.test"
|
|
|
|
|
|
def cbor(v) -> bytes:
|
|
def head(major, n):
|
|
if n < 24:
|
|
return bytes([major << 5 | n])
|
|
for info, size in ((24, 1), (25, 2), (26, 4), (27, 8)):
|
|
if n < 1 << (8 * size):
|
|
return bytes([major << 5 | info]) + n.to_bytes(size, "big")
|
|
raise ValueError(n)
|
|
if isinstance(v, bool):
|
|
return bytes([0xF5 if v else 0xF4])
|
|
if isinstance(v, int):
|
|
return head(0, v) if v >= 0 else head(1, -1 - v)
|
|
if isinstance(v, bytes):
|
|
return head(2, len(v)) + v
|
|
if isinstance(v, str):
|
|
b = v.encode()
|
|
return head(3, len(b)) + b
|
|
if isinstance(v, list):
|
|
return head(4, len(v)) + b"".join(cbor(x) for x in v)
|
|
if isinstance(v, dict):
|
|
return head(5, len(v)) + b"".join(cbor(k) + cbor(x) for k, x in v.items())
|
|
raise TypeError(v)
|
|
|
|
|
|
class Authenticator:
|
|
def __init__(self, kind="es256", synced=True):
|
|
self.kind = kind
|
|
self.key = ec.generate_private_key(ec.SECP256R1()) if kind == "es256" \
|
|
else ed25519.Ed25519PrivateKey.generate()
|
|
self.cid = os.urandom(16)
|
|
self.count = 0 if synced else 1
|
|
self.synced = synced
|
|
|
|
def cose(self) -> dict:
|
|
if self.kind == "es256":
|
|
n = self.key.public_key().public_numbers()
|
|
return {1: 2, 3: -7, -1: 1, -2: n.x.to_bytes(32, "big"), -3: n.y.to_bytes(32, "big")}
|
|
from cryptography.hazmat.primitives import serialization
|
|
raw = self.key.public_key().public_bytes(serialization.Encoding.Raw,
|
|
serialization.PublicFormat.Raw)
|
|
return {1: 1, 3: -8, -1: 6, -2: raw}
|
|
|
|
def _ad(self, rp, flags, extra=b""):
|
|
return hashlib.sha256(rp.encode()).digest() + bytes([flags]) + \
|
|
struct.pack(">I", self.count) + extra
|
|
|
|
def create(self, challenge, origin=ORIGIN, rp=RP, flags=None, cid=None):
|
|
flags = wa.UP | wa.UV | wa.AT | (wa.BE | wa.BS if self.synced else 0) \
|
|
if flags is None else flags
|
|
cid = self.cid if cid is None else cid
|
|
cred = b"\x00" * 16 + struct.pack(">H", len(cid)) + cid + cbor(self.cose())
|
|
att = cbor({"fmt": "none", "attStmt": {}, "authData": self._ad(rp, flags, cred)})
|
|
cd = json.dumps({"type": "webauthn.create", "challenge": wa.b64u(challenge),
|
|
"origin": origin, "crossOrigin": False}).encode()
|
|
return {"attestation_object": att, "client_data_json": cd}
|
|
|
|
def get(self, challenge, origin=ORIGIN, rp=RP, flags=wa.UP | wa.UV, kind="webauthn.get"):
|
|
if not self.synced:
|
|
self.count += 1
|
|
ad = self._ad(rp, flags)
|
|
cd = json.dumps({"type": kind, "challenge": wa.b64u(challenge), "origin": origin}).encode()
|
|
msg = ad + hashlib.sha256(cd).digest()
|
|
sig = self.key.sign(msg, ec.ECDSA(hashes.SHA256())) if self.kind == "es256" \
|
|
else self.key.sign(msg)
|
|
return {"authenticator_data": ad, "client_data_json": cd, "signature": sig}
|
|
|
|
|
|
def register(a: Authenticator, ch=b"c" * 32, **kw) -> dict:
|
|
return wa.verify_registration(challenge=ch, origin=ORIGIN, rp_id=RP,
|
|
credential_id=a.cid, **a.create(ch, **kw))
|
|
|
|
|
|
def assertion(a: Authenticator, cred: dict, ch=b"g" * 32, **kw) -> int:
|
|
return wa.verify_assertion(cred=cred, challenge=ch, origin=ORIGIN, rp_id=RP,
|
|
**a.get(ch, **kw))
|
|
|
|
|
|
class Registration(unittest.TestCase):
|
|
def test_es256_and_ed25519_passkeys_are_added_and_used(self):
|
|
for kind in ("es256", "ed25519"):
|
|
a = Authenticator(kind)
|
|
cred = register(a)
|
|
self.assertEqual(cred["id"], wa.b64u(a.cid))
|
|
self.assertEqual(cred["alg"], -7 if kind == "es256" else -8)
|
|
self.assertTrue(cred["synced"])
|
|
self.assertEqual(assertion(a, cred), 0)
|
|
|
|
def test_refusals(self):
|
|
a = Authenticator()
|
|
cases = {
|
|
"wrong origin": dict(origin="https://evil.example.test"),
|
|
"sibling origin": dict(origin="https://guest.example.test"),
|
|
"wrong rp": dict(rp="example.test"),
|
|
"no user verification": dict(flags=wa.UP | wa.AT),
|
|
"no credential": dict(flags=wa.UP | wa.UV),
|
|
}
|
|
for name, kw in cases.items():
|
|
with self.subTest(name), self.assertRaises(wa.WebAuthnError):
|
|
register(a, **kw)
|
|
with self.assertRaises(wa.WebAuthnError): # another challenge
|
|
wa.verify_registration(challenge=b"x" * 32, origin=ORIGIN, rp_id=RP,
|
|
credential_id=a.cid, **a.create(b"c" * 32))
|
|
with self.assertRaises(wa.WebAuthnError): # the id the browser claims ≠ the key's
|
|
wa.verify_registration(challenge=b"c" * 32, origin=ORIGIN, rp_id=RP,
|
|
credential_id=b"other", **a.create(b"c" * 32))
|
|
|
|
def test_garbage_never_crashes(self):
|
|
for blob in (b"", b"\xff", b"\x9f", b"\xbf" * 50, os.urandom(64), cbor([1, 2])):
|
|
with self.assertRaises(wa.WebAuthnError):
|
|
wa.verify_registration(attestation_object=blob, client_data_json=Authenticator()
|
|
.create(b"c" * 32)["client_data_json"],
|
|
challenge=b"c" * 32, origin=ORIGIN, rp_id=RP,
|
|
credential_id=b"x")
|
|
|
|
|
|
class Assertion(unittest.TestCase):
|
|
def setUp(self):
|
|
self.a = Authenticator()
|
|
self.cred = register(self.a)
|
|
|
|
def test_refusals(self):
|
|
cases = {
|
|
"wrong origin": dict(origin="https://evil.example.test"),
|
|
"wrong rp": dict(rp="evil.example.test"),
|
|
"no user verification": dict(flags=wa.UP),
|
|
"not present": dict(flags=wa.UV),
|
|
"a create, replayed as a get": dict(kind="webauthn.create"),
|
|
}
|
|
for name, kw in cases.items():
|
|
with self.subTest(name), self.assertRaises(wa.WebAuthnError):
|
|
assertion(self.a, self.cred, **kw)
|
|
with self.assertRaises(wa.WebAuthnError): # another challenge
|
|
wa.verify_assertion(cred=self.cred, challenge=b"x" * 32, origin=ORIGIN, rp_id=RP,
|
|
**self.a.get(b"g" * 32))
|
|
|
|
def test_another_key_cannot_sign_for_it(self):
|
|
other = Authenticator()
|
|
with self.assertRaises(wa.WebAuthnError):
|
|
assertion(other, self.cred)
|
|
|
|
def test_tampered_data_fails_the_signature(self):
|
|
got = self.a.get(b"g" * 32)
|
|
cd = json.loads(got["client_data_json"])
|
|
cd["extra"] = 1
|
|
got["client_data_json"] = json.dumps(cd).encode()
|
|
with self.assertRaises(wa.WebAuthnError):
|
|
wa.verify_assertion(cred=self.cred, challenge=b"g" * 32, origin=ORIGIN, rp_id=RP, **got)
|
|
|
|
def test_a_counter_must_move_forward(self):
|
|
hw = Authenticator(synced=False)
|
|
cred = register(hw)
|
|
cred["signCount"] = assertion(hw, cred)
|
|
hw.count -= 1 # a clone, one step behind
|
|
with self.assertRaises(wa.WebAuthnError):
|
|
assertion(hw, cred)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|