How a native app pairs with the agent and connects to it. For the message protocol itself see the Device API.
Devices do not need a certificate authority, and native apps should not install one. A CA in a trust store can sign for any name, not just this agent, and on iOS granting Full Trust is a system-wide decision.
TLS itself stays on. The agent serves wss:// with a self-signed
certificate, and your app verifies it by pinning the agent’s public key
rather than by chain of trust. The pin is handed to you at pairing and does not
change when the agent’s certificate is reissued.
So the setup cost moved off the user and onto the app: no profile to install, a few lines of trust-evaluation code instead.
You can run the agent without TLS (-auto-tls=false) and connect over ws://,
but see Running without TLS: both platforms block
cleartext by default, and the traffic carries card UIDs and tag contents across
whatever network the device is on.
Pairing exchanges the PIN shown on the kiosk for a credential belonging to this device. Do it once.
The agent prints a QR at startup, next to the PIN, encoding:
davi-pair://<agent-host>:9470/?spki=sha256%2F47DE…&code=123456&name=Davi%20NFC%20Agent
spki is the agent’s public key pin and code is the pairing PIN. Read this
off the screen. A QR fetched over the network carries no more authority than the
connection that served it, and the pin is the value that authenticates that
connection.
spkiPairing is served from the agent’s own port, which serves the certificate
spki covers. Pin the connection to spki before sending anything.
POST https://<agent-host>:9470/pair?pin=123456
Content-Type: application/json
{"deviceName": "Warehouse iPhone", "platform": "ios"}
Pairing over a cleartext connection is refused (426 Upgrade Required) from
anything but loopback: the response carries a durable token and the key pin, so
issuing it in the clear hands both to an observer and lets an active attacker
substitute a pin of their own.
{
"deviceID": "6f1c…",
"deviceToken": "kQ8x…",
"publicKeyPin": "sha256/47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=",
"agentPort": 9470
}
Store deviceToken and publicKeyPin in the keychain / keystore. The token
is shown once, since the agent keeps only a hash of it, so losing it means
pairing again.
publicKeyPin in the response repeats the spki the QR carried. They must
match; a mismatch means the QR did not come from this agent.
The PIN is on the kiosk: system tray, agent logs, and the pairing page at
http://<agent-host>:9472. Five wrong attempts lock pairing until the agent
restarts.
Port 9472 is the cleartext bootstrap listener. It serves the setup page and hands out the local certificate authority to a device that does not trust the agent’s certificate yet, which is why it is not TLS. Pairing is not served from it.
wss://<agent-host>:9470/ws?mode=device
Present the token as Authorization: Bearer <deviceToken>, or ?secret= if
your WebSocket client cannot set headers. Offer the davi-nfc-device.v1
subprotocol, then send hello. See
Device Registration.
This is the part that replaces the CA. On each connection, compare the SHA-256
of the server certificate’s SubjectPublicKeyInfo against the stored
publicKeyPin. Refuse the connection when it differs.
Do not use OkHttp’s CertificatePinner for this. It runs after the trust
manager has validated the chain, so a self-signed certificate is rejected during
the handshake before pinning is ever consulted, so the pin appears to be
ignored.
This is documented OkHttp behavior,
not a bug, and it is the most common way this goes wrong.
Supply a custom X509TrustManager that accepts the certificate when its SPKI
hash matches, and rejects it otherwise:
class PinnedTrustManager(private val expectedPin: String) : X509TrustManager {
override fun checkServerTrusted(chain: Array<X509Certificate>, authType: String) {
val spki = chain.first().publicKey.encoded // already SPKI DER
val digest = MessageDigest.getInstance("SHA-256").digest(spki)
val pin = "sha256/" + Base64.encodeToString(digest, Base64.NO_WRAP)
if (pin != expectedPin) throw CertificateException("agent key pin mismatch")
}
override fun checkClientTrusted(chain: Array<X509Certificate>, authType: String) = Unit
override fun getAcceptedIssuers(): Array<X509Certificate> = emptyArray()
}
PublicKey.getEncoded() returns SPKI DER directly, which the agent
hashes, with no reassembly needed.
Pair it with a hostname verifier appropriate to how you address the agent; a self-signed certificate carries the agent’s hostnames and LAN IPs as SANs, so standard verification works if you connect by one of those.
Handle the server-trust challenge in URLSessionDelegate and compare the pin
yourself:
func urlSession(_ session: URLSession,
didReceive challenge: URLAuthenticationChallenge,
completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
guard challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust,
let trust = challenge.protectionSpace.serverTrust,
pinMatches(trust, expected: storedPin)
else { return completionHandler(.cancelAuthenticationChallenge, nil) }
completionHandler(.useCredential, URLCredential(trust: trust))
}
Watch the key encoding. SecKeyCopyExternalRepresentation returns the raw
key, not SPKI DER, so hashing it directly produces a value that will never match.
Prepend the ASN.1 SubjectPublicKeyInfo header for the key’s algorithm before
hashing. The agent uses ECDSA P-256, whose header is a fixed 26-byte constant.
Getting this wrong is the iOS equivalent of the CertificatePinner trap: the
code looks right and every connection fails.
The agent can send a tag raw commands through a phone only if the phone’s app can speak ISO 7816 to it, which both platforms gate.
iOS. The app’s entitlements must list the applications it selects, in
com.apple.developer.nfc.readersession.iso7816.select-identifiers, and an
NFCTagReaderSession must be opened with .iso14443. Without the identifier
the OS refuses the select. For NDEF on an NTAG 424 DNA, and for any raw access
to it, the list must include D2760000850101, the NDEF application:
<key>com.apple.developer.nfc.readersession.iso7816.select-identifiers</key>
<array>
<string>D2760000850101</string>
</array>
Add the identifiers of any other application the agent will be asked to select
(a DESFire application’s AID, for instance). The same value belongs in the
app’s Info.plist under com.apple.developer.nfc.readersession.iso7816.select-identifiers.
Android. Use IsoDep for APDU-level exchange (IsoDep.transceive) and
NfcA for framing-level. Call IsoDep.connect() once per tag and keep the
connection for the whole session; raise IsoDep.setTimeout for slow commands.
Reply format. deviceTransceiveResponse.data must be the card’s whole
reply and must end with SW1SW2. Android’s IsoDep.transceive already returns
it that way. iOS sendCommand returns the status word beside the payload, so
append sw1 and sw2 to the data before answering. A reply of the status word
alone is valid. See Transceive Request.
Sequences and NTAG 424 operations. A client’s transceiveSequence and
ntag424Request work on a tag your app holds. The agent builds the commands and
sends them to the phone as ordinary deviceTransceiveRequest exchanges, so
nothing beyond canTransceive is needed; an EV2 authentication then costs two
round trips. To avoid a round trip per step, declare canTransceiveSequence and
answer deviceTransceiveSequenceRequest: run its steps in order on the open
IsoDep connection (or iOS tag session), stop after a step whose status word is
in stopOnSW or, when expectSW is present, not in it, and answer with every
reply that ran and stoppedAt (-1 when all ran). See
Transceive Sequence Request. The agent
checks your stoppedAt against the rules it sent, so apply them exactly.
Deadline. An NTAG 424 operation on a tag your app holds has 15 seconds in
all, counted from when the agent starts sending to the phone, on top of the
5 seconds each exchange may take. Before every step that changes the tag
(writing the NDEF file, ChangeFileSettings, ChangeKey, lock) the agent
estimates what the step needs from the slowest exchange it has seen in this
operation, at least 100 ms each, and if the time left would not cover it, it
fails with TIMEOUT before sending anything of that step. A slow phone
therefore fails early with the tag untouched, rather than part way through. If
the caller’s own timeout is shorter than 15 seconds, that one applies. On iOS,
keep the tag still against the phone until the operation answers: an iOS tag
session ends after about 20 seconds and drops the tag as soon as it moves, which
is why the deadline is below that. Do not hold exchanges back (queueing,
background throttling); answer each one as soon as the card has.
Recovering a half-configured tag. configureSDM writes the NDEF message,
then the file settings, then reads the file back to verify. Its error says what
the tag was left holding:
| Tag state in the error | What is on the tag |
|---|---|
| nothing written | The failure came before any write was sent. The tag is as it was |
| NDEF write interrupted, file contents indeterminate | A write of the NDEF file was sent and did not finish. The file may hold part of the new message |
| NDEF written, SDM not configured | The new message is written and the file settings are unchanged, so the URL shows without its mirrors |
| NDEF written, SDM configuration unconfirmed | The settings change was sent and its answer did not arrive. SDM may or may not be on |
| NDEF written, SDM configured, read-back not verified | Both writes succeeded and the check afterwards failed. Tap the tag to verify it |
In every case run configureSDM again with the same template and options. It is
idempotent: it rewrites the same message and settings whatever the tag holds,
and a run on a tag it has already configured leaves it as it was (the
simulated-phone tests check both). It needs the change key to still be the one
the agent holds, which a failed run does not alter.
A tag held by a phone cannot be leased as a raw session: the operating system
owns the tag session, so rawSessionBeginRequest answers NOT_SUPPORTED. A
sequence or an NTAG 424 operation is the unit that runs without interruption.
Any connection to a LAN address needs the local network permission, whether or
not you use TLS. Add NSLocalNetworkUsageDescription to Info.plist, and
expect the system prompt on first use. Without it the connection fails with no
useful error.
If you discover the agent via mDNS, also declare NSBonjourServices with
_nfc-device._tcp.
-auto-tls=false makes the agent serve ws://. Both platforms block cleartext
by default, so this needs a declaration either way:
NSAllowsLocalNetworking covers
.local names and private IP ranges, which is the narrow way to allow it.network_security_config.xml permitting it for the agent’s address only,
never cleartextTrafficPermitted="true" globally.Consider what is on the wire before choosing this: tag UIDs and NDEF contents, readable by anything on the same network. It is reasonable for a wired bench setup, and a poor default for a shop floor on shared WiFi.
Once your devices pair, the agent can be set to admit nothing else. See
Requiring pairing. Until then the shared API secret
still works, so an unpaired device is not locked out. A device connecting over
loopback presents a credential too, unless the agent was started with
-allow-loopback-bypass. See The loopback bypass.
| Symptom | Likely cause |
|---|---|
| Handshake fails immediately, no error detail | Pin mismatch, or CertificatePinner used instead of a trust manager (Android) |
| Every connection fails despite a correct-looking pin | Raw key hashed instead of SPKI (iOS) |
| Connection fails only on a real device, works in a simulator | Local network permission not declared or not granted |
401 Unauthorized on upgrade |
Token missing, or revoked from the tray |
| Worked yesterday, fails after the host moved network | The certificate was reissued, which is expected, and the pin should still match. If it does not, the agent’s key was regenerated, so pair again |