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.
26 wpas_ctrl = '/var/run/wpa_supplicant'
32 if os.path.isdir(wpas_ctrl):
34 ifaces = [os.path.join(wpas_ctrl, i) for i in os.listdir(wpas_ctrl)]
35 except OSError, error:
36 print "Could not find wpa_supplicant: ", error
40 print "No wpa_supplicant control interface found"
45 wpas = wpaspy.Ctrl(ctrl)
52 def wpas_tag_read(message):
56 if "FAIL" in wpas.request("WPS_NFC_TAG_READ " + str(message).encode("hex")):
60 def wpas_get_config_token(id=None):
65 ret = wpas.request("WPS_NFC_CONFIG_TOKEN NDEF " + id)
67 ret = wpas.request("WPS_NFC_CONFIG_TOKEN NDEF")
70 return ret.rstrip().decode("hex")
73 def wpas_get_er_config_token(uuid):
77 return wpas.request("WPS_ER_NFC_CONFIG_TOKEN NDEF " + uuid).rstrip().decode("hex")
80 def wpas_get_password_token():
84 return wpas.request("WPS_NFC_TOKEN NDEF").rstrip().decode("hex")
87 def wpas_get_handover_req():
91 return wpas.request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip().decode("hex")
94 def wpas_get_handover_sel(uuid):
99 return wpas.request("NFC_GET_HANDOVER_SEL NDEF WPS-CR").rstrip().decode("hex")
100 return wpas.request("NFC_GET_HANDOVER_SEL NDEF WPS-CR " + uuid).rstrip().decode("hex")
103 def wpas_report_handover(req, sel, type):
104 wpas = wpas_connect()
107 return wpas.request("NFC_REPORT_HANDOVER " + type + " WPS " +
108 str(req).encode("hex") + " " +
109 str(sel).encode("hex"))
112 class HandoverServer(nfc.handover.HandoverServer):
113 def __init__(self, llc):
114 super(HandoverServer, self).__init__(llc)
115 self.sent_carrier = None
116 self.ho_server_processing = False
119 def process_request(self, request):
120 self.ho_server_processing = True
121 print "HandoverServer - request received"
122 print "Parsed handover request: " + request.pretty()
124 sel = nfc.ndef.HandoverSelectMessage(version="1.2")
126 for carrier in request.carriers:
127 print "Remote carrier type: " + carrier.type
128 if carrier.type == "application/vnd.wfa.wsc":
129 print "WPS carrier type match - add WPS carrier record"
130 data = wpas_get_handover_sel(self.uuid)
132 print "Could not get handover select carrier record from wpa_supplicant"
134 print "Handover select carrier record from wpa_supplicant:"
135 print data.encode("hex")
136 self.sent_carrier = data
137 wpas_report_handover(carrier.record, self.sent_carrier, "RESP")
139 message = nfc.ndef.Message(data);
140 sel.add_carrier(message[0], "active", message[1:])
142 print "Handover select:"
144 print str(sel).encode("hex")
146 print "Sending handover select"
151 def wps_handover_init(llc):
152 print "Trying to initiate WPS handover"
154 data = wpas_get_handover_req()
156 print "Could not get handover request carrier record from wpa_supplicant"
158 print "Handover request carrier record from wpa_supplicant: " + data.encode("hex")
159 record = nfc.ndef.Record()
160 f = StringIO.StringIO(data)
162 record = nfc.ndef.HandoverCarrierRecord(record)
163 print "Parsed handover request carrier record:"
164 print record.pretty()
166 message = nfc.ndef.HandoverRequestMessage(version="1.2")
167 message.nonce = random.randint(0, 0xffff)
168 message.add_carrier(record, "active")
170 print "Handover request:"
171 print message.pretty()
173 client = nfc.handover.HandoverClient(llc)
175 print "Trying handover";
177 print "Connected for handover"
178 except nfc.llcp.ConnectRefused:
179 print "Handover connection refused"
183 print "Sending handover request"
185 if not client.send(message):
186 print "Failed to send handover request"
188 print "Receiving handover response"
189 message = client._recv()
191 print "No response received"
194 if message.type != "urn:nfc:wkt:Hs":
195 print "Response was not Hs - received: " + message.type
199 print "Received message"
200 print message.pretty()
201 message = nfc.ndef.HandoverSelectMessage(message)
202 print "Handover select received"
203 print message.pretty()
205 for carrier in message.carriers:
206 print "Remote carrier type: " + carrier.type
207 if carrier.type == "application/vnd.wfa.wsc":
208 print "WPS carrier type match - send to wpa_supplicant"
209 wpas_report_handover(data, carrier.record, "INIT")
210 wifi = nfc.ndef.WifiConfigRecord(carrier.record)
215 print "Done with handover"
219 continue_loop = False
222 def wps_tag_read(tag, wait_remove=True):
224 if len(tag.ndef.message):
225 for record in tag.ndef.message:
226 print "record type " + record.type
227 if record.type == "application/vnd.wfa.wsc":
228 print "WPS tag - send to wpa_supplicant"
229 success = wpas_tag_read(tag.ndef.message)
236 while tag.is_present:
242 def rdwr_connected_write(tag):
243 print "Tag found - writing"
245 tag.ndef.message = str(write_data)
246 print "Done - remove tag"
250 continue_loop = False
251 global write_wait_remove
252 while write_wait_remove and tag.is_present:
255 def wps_write_config_tag(clf, id=None, wait_remove=True):
256 print "Write WPS config token"
257 global write_data, write_wait_remove
258 write_wait_remove = wait_remove
259 write_data = wpas_get_config_token(id)
260 if write_data == None:
261 print "Could not get WPS config token from wpa_supplicant"
264 print "Touch an NFC tag"
265 clf.connect(rdwr={'on-connect': rdwr_connected_write})
268 def wps_write_er_config_tag(clf, uuid):
269 print "Write WPS ER config token"
270 global write_data, write_wait_remove
271 write_wait_remove = True
272 write_data = wpas_get_er_config_token(uuid)
273 if write_data == None:
274 print "Could not get WPS config token from wpa_supplicant"
277 print "Touch an NFC tag"
278 clf.connect(rdwr={'on-connect': rdwr_connected_write})
281 def wps_write_password_tag(clf, wait_remove=True):
282 print "Write WPS password token"
283 global write_data, write_wait_remove
284 write_wait_remove = wait_remove
285 write_data = wpas_get_password_token()
286 if write_data == None:
287 print "Could not get WPS password token from wpa_supplicant"
290 print "Touch an NFC tag"
291 clf.connect(rdwr={'on-connect': rdwr_connected_write})
294 def rdwr_connected(tag):
296 print "Tag connected: " + str(tag)
299 print "NDEF tag: " + tag.type
301 print tag.ndef.message.pretty()
304 success = wps_tag_read(tag, not only_one)
305 if only_one and success:
307 continue_loop = False
309 print "Not an NDEF tag - remove tag"
310 while tag.is_present:
315 def llcp_worker(llc):
318 wps_handover_init(llc)
322 global wait_connection
323 while not wait_connection and srv.sent_carrier is None:
324 if srv.ho_server_processing:
327 def llcp_startup(clf, llc):
330 print "Start LLCP server"
332 srv = HandoverServer(llc)
334 print "Trying to handle WPS handover"
337 print "Trying to handle WPS handover with AP " + arg_uuid
341 def llcp_connected(llc):
342 print "P2P LLCP connected"
343 global wait_connection
344 wait_connection = False
350 threading.Thread(target=llcp_worker, args=(llc,)).start()
355 clf = nfc.ContactlessFrontend()
360 if len(sys.argv) > 1 and sys.argv[1] != '-1':
361 arg_uuid = sys.argv[1]
364 if len(sys.argv) > 1 and sys.argv[1] == '-1':
369 if not clf.open("usb"):
370 print "Could not open connection with an NFC device"
373 if len(sys.argv) > 1 and sys.argv[1] == "write-config":
374 wps_write_config_tag(clf)
377 if len(sys.argv) > 1 and sys.argv[1] == "write-config-no-wait":
378 wps_write_config_tag(clf, wait_remove=False)
381 if len(sys.argv) > 2 and sys.argv[1] == "write-config-id":
382 wps_write_config_tag(clf, sys.argv[2])
385 if len(sys.argv) > 2 and sys.argv[1] == "write-er-config":
386 wps_write_er_config_tag(clf, sys.argv[2])
389 if len(sys.argv) > 1 and sys.argv[1] == "write-password":
390 wps_write_password_tag(clf)
393 if len(sys.argv) > 1 and sys.argv[1] == "write-password-no-wait":
394 wps_write_password_tag(clf, wait_remove=False)
399 print "Waiting for a tag or peer to be touched"
400 wait_connection = True
402 if not clf.connect(rdwr={'on-connect': rdwr_connected},
403 llcp={'on-startup': llcp_startup,
404 'on-connect': llcp_connected}):
407 print "clf.connect failed"
410 if only_one and srv and srv.success:
413 except KeyboardInterrupt:
420 if __name__ == '__main__':