TExcelTAD

Passkeys and WebAuthn Concepts

Every authentication method so far shares one weakness: the secret, or a code derived from it, is typed into a page. If the page is a convincing fake, the user hands the secret to the attacker, and an MFA code relayed within thirty seconds works just as well as the password. Passkeys remove that weakness. The user's device holds a private key per site, the site stores only the public key, and the browser signs a challenge together with the origin it is really talking to. A phishing site gets a signature for the phishing origin, which the real site rejects. There is nothing to type, nothing to reuse across sites and nothing worth stealing from the server. This lesson walks through the protocol with real ECDSA signatures in Python and shows exactly which server check stops the phish.

Definitions (W3C WebAuthn Level 3, FIDO2)

  • Relying party (RP): the website, identified by an RP ID, a registrable domain suffix of its origin (shop.lab.test). The browser refuses to use a credential for an RP ID that doesn't match the page's origin.
  • Authenticator: the thing holding private keys: a phone or laptop platform authenticator (Touch ID, Windows Hello, Android), or a roaming security key over USB/NFC/BLE.
  • Credential: a key pair created for one RP ID, with a credential id. Passkey is the common name for a discoverable credential, often synced across the user's devices by the platform (iCloud Keychain, Google Password Manager, password managers).
  • Ceremonies: registration (navigator.credentials.create(): the authenticator makes a key pair and returns the public key) and authentication (navigator.credentials.get(): the authenticator signs).
  • What is signed: authenticatorData || SHA-256(clientDataJSON). clientDataJSON contains the type, the server's random challenge and the origin the browser saw. authenticatorData contains SHA-256(RP ID), flags (UP user present, UV user verified by PIN or biometric) and a signature counter.

One authentication, verified by hand

The example plays all three parts: an authenticator with a P-256 key (the most common WebAuthn algorithm, COSE -7, ES256), a browser that builds clientDataJSON, and a server that verifies. ECDSA keys and signatures are random, so the output prints verification results, never key material. Real code uses a WebAuthn library (py_webauthn, SimpleWebAuthn, java-webauthn-server) rather than this sketch.

python · try it yourself
import hashlib, json, struct
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.exceptions import InvalidSignature

RP_ID, ORIGIN = "shop.lab.test", "https://shop.lab.test"

# Registration: the authenticator creates a key pair for this RP; the server stores only the public key.
private_key = ec.generate_private_key(ec.SECP256R1())
stored = {"public_key": private_key.public_key(), "sign_count": 0}

def authenticator_sign(rp_id, client_data_json, counter):
    flags = 0x01 | 0x04                                     # UP and UV
    auth_data = hashlib.sha256(rp_id.encode()).digest() + bytes([flags]) + struct.pack(">I", counter)
    sig = private_key.sign(auth_data + hashlib.sha256(client_data_json).digest(), ec.ECDSA(hashes.SHA256()))
    return auth_data, sig

def browser_get(page_origin, rp_id, challenge, counter):
    client_data = json.dumps({"type": "webauthn.get", "challenge": challenge, "origin": page_origin}).encode()
    return client_data, *authenticator_sign(rp_id, client_data, counter)

def server_verify(client_data_json, auth_data, sig, expected_challenge):
    cd = json.loads(client_data_json)
    checks = {
        "type": cd["type"] == "webauthn.get",
        "challenge": cd["challenge"] == expected_challenge,
        "origin": cd["origin"] == ORIGIN,
        "rpIdHash": auth_data[:32] == hashlib.sha256(RP_ID.encode()).digest(),
        "user present+verified": auth_data[32] & 0x05 == 0x05,
        "counter grew": struct.unpack(">I", auth_data[33:37])[0] > stored["sign_count"],
    }
    try:
        stored["public_key"].verify(sig, auth_data + hashlib.sha256(client_data_json).digest(), ec.ECDSA(hashes.SHA256()))
        checks["signature"] = True
    except InvalidSignature:
        checks["signature"] = False
    failed = [k for k, ok in checks.items() if not ok]
    if not failed:
        stored["sign_count"] = struct.unpack(">I", auth_data[33:37])[0]
    return "accepted" if not failed else "rejected: " + ", ".join(failed)

challenge = "c2VydmVyLWNoYWxsZW5nZS0x"                    # real servers: 16+ unpredictable bytes, single use
cd, ad, sig = browser_get(ORIGIN, RP_ID, challenge, counter=1)
print("1. real site:            ", server_verify(cd, ad, sig, challenge))
print("2. same response replayed:", server_verify(cd, ad, sig, challenge))

# A phishing page on shop-lab-login.example relays the real site's challenge to the victim's browser.
# The browser reports the true origin, and it won't use the shop's credential for another RP ID,
# so the best the attacker can obtain is a signature over the phishing origin and RP ID:
cd2, ad2, sig2 = browser_get("https://shop-lab-login.example", "shop-lab-login.example", challenge, counter=2)
print("3. relayed from a phish: ", server_verify(cd2, ad2, sig2, challenge))

# Tampering: the attacker edits clientDataJSON to claim the real origin; the signature no longer matches.
forged = cd2.replace(b"https://shop-lab-login.example", ORIGIN.encode())
print("4. origin edited after:  ", server_verify(forged, ad2, sig2, challenge))
1. real site:             accepted
2. same response replayed: rejected: counter grew
3. relayed from a phish:  rejected: origin, rpIdHash
4. origin edited after:   rejected: rpIdHash, signature

Case 1 passes every check. Case 2 is a replay: the counter didn't grow. (This sketch doesn't track challenges; a real server also refuses because the challenge was single-use and is already consumed.) Case 3 is the phishing relay that defeats OTP codes: the browser wrote the phishing origin into clientDataJSON and the authenticator hashed the phishing RP ID, so two checks fail, and no effort by the user can change that. Case 4 shows that the attacker can't rewrite the origin afterwards: clientDataJSON is covered by the signature, so the signature check fails, and the RP ID hash inside authenticatorData still names the phishing domain.

How passkey deployments go wrong (detection)

Passkeys move the risk from the protocol to the integration. Review and test:

  • Server checks skipped: libraries make it easy to verify only the signature. Test with a mismatched origin, a reused challenge, a challenge from another session, and UV missing when the policy requires user verification.
  • Challenges that are predictable, reused, not bound to the session, or that never expire.
  • Account recovery and fallback: if "Sign in another way" offers an SMS code or a password with no rate limit, phishers simply choose that. Look at which factors can still log in.
  • Credential management: can a stolen session add a new passkey without re-authentication? Are users notified when one is added?
  • Logs: assertion failures by reason (origin, challenge, signature, counter); a counter going backwards on a non-synced key may indicate a cloned authenticator (synced passkeys often report 0 and can't use this signal).

How to deploy them well

  1. Use a maintained WebAuthn library, set the expected origin(s) and RP ID explicitly, and generate a fresh random challenge per ceremony, stored server-side with a short expiry.
  2. Decide the user-verification policy (required for passkey-only login) and enforce the UV flag.
  3. Allow several passkeys per account, show them with names and last-used dates, require re-authentication to add or remove one, and notify on changes.
  4. Make recovery at least as strong as the weakest remaining factor; phase out SMS and password fallbacks for high-value accounts.

Pitfalls

  • Treating the credential id as a secret or as proof of identity: it is public; only the signature proves possession.
  • Accepting several origins loosely (all subdomains, http://), which reopens the phishing door the protocol closed.
  • Assuming "passkey" means hardware-bound: synced passkeys are as strong as the platform account that syncs them; attestation can demand specific authenticators where that matters.

Practice

1. Which two server checks failed for the phishing relay, and which one would still fail if the attacker somehow controlled the browser's origin string?

origin and rpIdHash. The RP ID hash is computed by the authenticator from the RP ID the browser allowed for that origin; the shop's credential is scoped to shop.lab.test, so a phishing origin can't obtain a signature with the shop's RP ID hash.

2. Why does the server store only a public key, and what does a database breach give an attacker?

Signing needs the private key, which never leaves the authenticator. A stolen database holds public keys and credential ids, useless for logging in and worthless on other sites.

Test yourself

What makes passkeys phishing-resistant?

  1. They are long
  2. The browser includes the real origin in the signed data and only uses a credential for its own RP ID
  3. They are sent by SMS
  4. They expire every 30 seconds

In the demo, why did the replayed response fail?

  1. The signature was wrong
  2. The counter didn't grow (and a real server also consumes the single-use challenge)
  3. The origin changed
  4. UV was missing

What does the WebAuthn server store for each credential?

  1. The private key
  2. The public key, credential id and signature counter
  3. A password hash
  4. The TOTP secret

An account supports passkeys but still offers unlimited password + SMS login. What is the effective security?

  1. That of passkeys
  2. That of the weakest offered method
  3. None
  4. That of TLS