3 # Example nfcpy to wpa_supplicant wrapper for WPS NFC operations
4 # Copyright (c) 2012-2013, Jouni Malinen <j@w1.fi>
6 # This software may be distributed under the terms of the BSD license.
7 # See README for more details.
25 wpas_ctrl = '/var/run/wpa_supplicant'
29 if os.path.isdir(wpas_ctrl):
31 ifaces = [os.path.join(wpas_ctrl, i) for i in os.listdir(wpas_ctrl)]
32 except OSError, error:
33 print "Could not find wpa_supplicant: ", error
37 print "No wpa_supplicant control interface found"
42 wpas = wpaspy.Ctrl(ctrl)
49 def wpas_tag_read(message):
53 if "FAIL" in wpas.request("WPS_NFC_TAG_READ " + message.encode("hex")):
57 def wpas_get_config_token(id=None):
62 ret = wpas.request("WPS_NFC_CONFIG_TOKEN NDEF " + id)
64 ret = wpas.request("WPS_NFC_CONFIG_TOKEN NDEF")
67 return ret.rstrip().decode("hex")
70 def wpas_get_er_config_token(uuid):
74 return wpas.request("WPS_ER_NFC_CONFIG_TOKEN NDEF " + uuid).rstrip().decode("hex")
77 def wpas_get_password_token():
81 return wpas.request("WPS_NFC_TOKEN NDEF").rstrip().decode("hex")
84 def wpas_get_handover_req():
88 return wpas.request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip().decode("hex")
91 def wpas_get_handover_sel(uuid):
96 return wpas.request("NFC_GET_HANDOVER_SEL NDEF WPS-CR").rstrip().decode("hex")
97 return wpas.request("NFC_GET_HANDOVER_SEL NDEF WPS-CR " + uuid).rstrip().decode("hex")
100 def wpas_report_handover(req, sel, type):
101 wpas = wpas_connect()
104 return wpas.request("NFC_REPORT_HANDOVER " + type + " WPS " +
105 str(req).encode("hex") + " " +
106 str(sel).encode("hex"))
109 class HandoverServer(nfc.handover.HandoverServer):
111 super(HandoverServer, self).__init__()
113 def process_request(self, request):
114 print "HandoverServer - request received"
115 print "Parsed handover request: " + request.pretty()
117 sel = nfc.ndef.HandoverSelectMessage(version="1.2")
119 for carrier in request.carriers:
120 print "Remote carrier type: " + carrier.type
121 if carrier.type == "application/vnd.wfa.wsc":
122 print "WPS carrier type match - add WPS carrier record"
123 self.received_carrier = carrier.record
124 data = wpas_get_handover_sel(self.uuid)
126 print "Could not get handover select carrier record from wpa_supplicant"
128 print "Handover select carrier record from wpa_supplicant:"
129 print data.encode("hex")
130 self.sent_carrier = data
132 message = nfc.ndef.Message(data);
133 sel.add_carrier(message[0], "active", message[1:])
135 print "Handover select:"
137 print str(sel).encode("hex")
139 print "Sending handover select"
143 def wps_handover_resp(peer, uuid):
145 print "Trying to handle WPS handover"
147 print "Trying to handle WPS handover with AP " + uuid
149 srv = HandoverServer()
150 srv.sent_carrier = None
153 nfc.llcp.activate(peer);
156 print "Trying handover";
158 print "Wait for disconnect"
159 while nfc.llcp.connected():
161 print "Disconnected after handover"
162 except nfc.llcp.ConnectRefused:
163 print "Handover connection refused"
168 wpas_report_handover(srv.received_carrier, srv.sent_carrier, "RESP")
172 print "Done with handover"
176 def wps_handover_init(peer):
177 print "Trying to initiate WPS handover"
179 data = wpas_get_handover_req()
181 print "Could not get handover request carrier record from wpa_supplicant"
183 print "Handover request carrier record from wpa_supplicant: " + data.encode("hex")
184 record = nfc.ndef.Record()
185 f = StringIO.StringIO(data)
187 record = nfc.ndef.HandoverCarrierRecord(record)
188 print "Parsed handover request carrier record:"
189 print record.pretty()
191 message = nfc.ndef.HandoverRequestMessage(version="1.2")
192 message.nonce = random.randint(0, 0xffff)
193 message.add_carrier(record, "active")
195 print "Handover request:"
196 print message.pretty()
198 nfc.llcp.activate(peer);
200 client = nfc.handover.HandoverClient()
202 print "Trying handover";
204 print "Connected for handover"
205 except nfc.llcp.ConnectRefused:
206 print "Handover connection refused"
211 print "Sending handover request"
213 if not client.send(message):
214 print "Failed to send handover request"
216 print "Receiving handover response"
217 message = client._recv()
219 print "No response received"
223 if message.type != "urn:nfc:wkt:Hs":
224 print "Response was not Hs - received: " + message.type
229 print "Received message"
230 print message.pretty()
231 message = nfc.ndef.HandoverSelectMessage(message)
232 print "Handover select received"
233 print message.pretty()
235 for carrier in message.carriers:
236 print "Remote carrier type: " + carrier.type
237 if carrier.type == "application/vnd.wfa.wsc":
238 print "WPS carrier type match - send to wpa_supplicant"
239 wpas_report_handover(data, carrier.record, "INIT")
240 wifi = nfc.ndef.WifiConfigRecord(carrier.record)
246 print "Done with handover"
249 def wps_tag_read(tag, wait_remove=True):
251 if len(tag.ndef.message):
252 message = nfc.ndef.Message(tag.ndef.message)
253 print "message type " + message.type
255 for record in message:
256 print "record type " + record.type
257 if record.type == "application/vnd.wfa.wsc":
258 print "WPS tag - send to wpa_supplicant"
259 success = wpas_tag_read(tag.ndef.message)
266 while tag.is_present:
272 def wps_write_config_tag(clf, id=None, wait_remove=True):
273 print "Write WPS config token"
274 data = wpas_get_config_token(id)
276 print "Could not get WPS config token from wpa_supplicant"
280 print "Touch an NFC tag"
288 print "Tag found - writing"
289 tag.ndef.message = data
290 print "Done - remove tag"
291 while wait_remove and tag.is_present:
295 def wps_write_er_config_tag(clf, uuid):
296 print "Write WPS ER config token"
297 data = wpas_get_er_config_token(uuid)
299 print "Could not get WPS config token from wpa_supplicant"
302 print "Touch an NFC tag"
310 print "Tag found - writing"
311 tag.ndef.message = data
312 print "Done - remove tag"
313 while tag.is_present:
317 def wps_write_password_tag(clf, wait_remove=True):
318 print "Write WPS password token"
319 data = wpas_get_password_token()
321 print "Could not get WPS password token from wpa_supplicant"
324 print "Touch an NFC tag"
332 print "Tag found - writing"
333 tag.ndef.message = data
334 print "Done - remove tag"
335 while wait_remove and tag.is_present:
341 if nfc.llcp.connected():
342 print "LLCP connected"
343 general_bytes = nfc.llcp.startup({})
344 peer = clf.listen(ord(os.urandom(1)) + 250, general_bytes)
345 if isinstance(peer, nfc.DEP):
346 print "listen -> DEP";
347 if peer.general_bytes.startswith("Ffm"):
350 print "mismatch in general_bytes"
351 print peer.general_bytes
353 peer = clf.poll(general_bytes)
354 if isinstance(peer, nfc.DEP):
356 if peer.general_bytes.startswith("Ffm"):
359 print "mismatch in general_bytes"
360 print peer.general_bytes
368 clf = nfc.ContactlessFrontend()
372 if len(sys.argv) > 1 and sys.argv[1] != '-1':
373 arg_uuid = sys.argv[1]
375 if len(sys.argv) > 1 and sys.argv[1] == '-1':
380 if len(sys.argv) > 1 and sys.argv[1] == "write-config":
381 wps_write_config_tag(clf)
384 if len(sys.argv) > 1 and sys.argv[1] == "write-config-no-wait":
385 wps_write_config_tag(clf, wait_remove=False)
388 if len(sys.argv) > 2 and sys.argv[1] == "write-config-id":
389 wps_write_config_tag(clf, sys.argv[2])
392 if len(sys.argv) > 2 and sys.argv[1] == "write-er-config":
393 wps_write_er_config_tag(clf, sys.argv[2])
396 if len(sys.argv) > 1 and sys.argv[1] == "write-password":
397 wps_write_password_tag(clf)
400 if len(sys.argv) > 1 and sys.argv[1] == "write-password-no-wait":
401 wps_write_password_tag(clf, wait_remove=False)
405 print "Waiting for a tag or peer to be touched"
408 if isinstance(tag, nfc.DEP):
410 wps_handover_init(tag)
411 elif arg_uuid is "ap":
412 wps_handover_resp(tag, None)
414 wps_handover_resp(tag, arg_uuid)
420 success = wps_tag_read(tag, not only_one)
427 print "Not an NDEF tag - remove tag"
430 while tag.is_present:
433 except KeyboardInterrupt:
440 if __name__ == '__main__':