1 /* packet-wifi-dpp.c
2  *
3  * Wi-Fi Device Provisioning Protocol (DPP)
4  *
5  * Copyright 2017-2020 Richard Sharpe <realrichardsharpe@gmail.com>
6  * Copyright 2017-2020 The WiFi Alliance
7  *
8  * SPDX-License-Identifier: GPL-2.0-or-later
9  */
10 
11 /*
12  * Code and constants based on Device_Provisioning_Protocol_Specification_v1.2.9
13  */
14 
15 #include "config.h"
16 
17 #include <epan/packet.h>
18 #include "packet-tcp.h"
19 #include <epan/to_str.h>
20 #include <epan/expert.h>
21 
22 #include "packet-wifi-dpp.h"
23 #include "packet-ieee80211.h"
24 
25 extern const value_string wfa_subtype_vals[];
26 
27 void proto_register_wifi_dpp(void);
28 void proto_reg_handoff_wifi_dpp(void);
29 
30 #define WIFI_DPP_TCP_PORT (7871)
31 static guint wifi_dpp_tcp_port = WIFI_DPP_TCP_PORT;
32 
33 enum {
34   DPP_STATUS_OK =                0,
35   DPP_STATUS_NOT_COMPATIBLE =    1,
36   DPP_STATUS_AUTH_FAILURE =      2,
37   DPP_STATUS_UNWRAP_FAILURE =    3,
38   DPP_STATUS_BAD_GROUP =         4,
39   DPP_STATUS_CONFIGURE_FAILURE = 5,
40   DPP_STATUS_RESPONSE_PENDING =  6,
41   DPP_STATUS_INVALID_CONNECTOR = 7,
42   DPP_STATUS_NO_MATCH =          8,
43   DPP_STATUS_CONFIG_REJECTED   = 9,
44   DPP_STATUS_NO_AP =             10,
45   DPP_STATUS_CONFIGURE_PENDING = 11,
46   DPP_STATUS_CSR_NEEDED =        12,
47   DPP_STATUS_CSR_BAD =           13
48 };
49 
50 static const value_string dpp_status_codes[] = {
51   { DPP_STATUS_OK,                 "OK" },
52   { DPP_STATUS_NOT_COMPATIBLE,     "Not Compatible" },
53   { DPP_STATUS_AUTH_FAILURE,       "Auth Failure" },
54   { DPP_STATUS_UNWRAP_FAILURE,     "Unwrap Failure" },
55   { DPP_STATUS_BAD_GROUP,          "Bad Group" },
56   { DPP_STATUS_CONFIGURE_FAILURE,  "Configure Failure" },
57   { DPP_STATUS_RESPONSE_PENDING,   "Response Pending" },
58   { DPP_STATUS_INVALID_CONNECTOR,  "Invalid Connector" },
59   { DPP_STATUS_NO_MATCH,           "No Match" },
60   { DPP_STATUS_CONFIG_REJECTED,    "Enrollee rejected the config" },
61   { DPP_STATUS_NO_AP,              "Enrollee failed to discover an AP" },
62   { DPP_STATUS_CONFIGURE_PENDING,  "Configuration response is not ready yet. The enrollee needs to request again." },
63   { DPP_STATUS_CSR_NEEDED,         "Configuration requires a Certificate Signing Request. Enrollee needs to request again." },
64   { DPP_STATUS_CSR_BAD,            "The Certificate Signing Request was invalid." },
65   { DPP_STATUS_OK,                "OK" },
66   { DPP_STATUS_NOT_COMPATIBLE,    "Not Compatible" },
67   { DPP_STATUS_AUTH_FAILURE,      "Auth Failure" },
68   { DPP_STATUS_UNWRAP_FAILURE,    "Unwrap Failure" },
69   { DPP_STATUS_BAD_GROUP,         "Bad Group" },
70   { DPP_STATUS_CONFIGURE_FAILURE, "Configure Failure" },
71   { DPP_STATUS_RESPONSE_PENDING,  "Response Pending" },
72   { 0, NULL }
73 };
74 
75 enum {
76   DPP_STATUS                           = 0x1000,
77   DPP_INITIATOR_BOOTSTRAPPING_KEY_HASH = 0x1001,
78   DPP_RESPONDER_BOOTSTRAPPING_KEY_HASH = 0x1002,
79   DPP_INITIATOR_PROTOCOL_KEY           = 0x1003,
80   DPP_WRAPPED_DATA                     = 0x1004,
81   DPP_INITIATOR_NONCE                  = 0x1005,
82   DPP_INITIATOR_CAPABILITIES           = 0x1006,
83   DPP_RESPONDER_NONCE                  = 0x1007,
84   DPP_RESPONDER_CAPABILITIES           = 0x1008,
85   DPP_RESPONDER_PROTOCOL_KEY           = 0x1009,
86   DPP_INITIATOR_AUTHENTICATING_TAG     = 0x100A,
87   DPP_RESPONDER_AUTHENTICATING_TAG     = 0x100B,
88   DPP_CONFIGURATION_OBJECT             = 0x100C,
89   DPP_CONNECTOR                        = 0x100D,
90   DPP_CONFIGURATION_ATTRIBUTES_OBJECT  = 0x100E,
G_DEFINE_TYPE_WITH_PRIVATE(GSocketListener,g_socket_listener,G_TYPE_OBJECT)91   DPP_BOOTSTRAPPING_KEY                = 0x100F,
92   DPP_FINITE_CYCLIC_GROUP              = 0x1012,
93   DPP_ENCRYPTED_KEY                    = 0x1013,
94   DPP_ENROLLEE_NONCE                   = 0x1014,
95   DPP_CODE_IDENTIFIER                  = 0x1015,
96   DPP_TRANSACTION_ID                   = 0x1016,
97   DPP_BOOTSTRAPPING_INFO               = 0x1017,
98   DPP_CHANNEL                          = 0x1018,
99   DPP_PROTOCOL_VERSION                 = 0x1019,
100   DPP_ENVELOPEDATA                     = 0x101A,
101   DPP_SENDCONNSTATUS                   = 0x101B,
102   DPP_CONNSTATUS                       = 0x101C,
103   DPP_RECONFIG_FLAGS                   = 0x101D,
104   DPP_C_SIGN_KEY_HASH                  = 0x101E,
105   DPP_CSR_ATTRIBUTES_REQUEST           = 0x101F,
106   DPP_A_NONCE                          = 0x1020,
107   DPP_E_PRIME_ID                       = 0x1021
108 };
109 
110 static const value_string dpp_ie_attr_ids[] = {
111   { DPP_STATUS,                           "DPP Status" },
112   { DPP_INITIATOR_BOOTSTRAPPING_KEY_HASH, "DPP Initiator Bootstrapping Key Hash" },
g_socket_listener_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)113   { DPP_RESPONDER_BOOTSTRAPPING_KEY_HASH, "DPP Responder Bootstrapping Key Hash" },
114   { DPP_INITIATOR_PROTOCOL_KEY,           "DPP Initiator Protocol Key" },
115   { DPP_WRAPPED_DATA,                     "DPP Primary Wrapped Data" },
116   { DPP_INITIATOR_NONCE,                  "DPP Initiator Nonce" },
117   { DPP_INITIATOR_CAPABILITIES,           "DPP Initiator Capabilities" },
118   { DPP_RESPONDER_NONCE,                  "DPP Responder Nonce" },
119   { DPP_RESPONDER_CAPABILITIES,           "DPP Responder Capabilities" },
120   { DPP_RESPONDER_PROTOCOL_KEY,           "DPP Responder Protocol Key" },
121   { DPP_INITIATOR_AUTHENTICATING_TAG,     "DPP Initiator Authenticating Tag" },
122   { DPP_RESPONDER_AUTHENTICATING_TAG,     "DPP Responder Authenticating Tag" },
123   { DPP_CONFIGURATION_OBJECT,             "DPP Configuration Object" },
124   { DPP_CONNECTOR,                        "DPP Connector" },
125   { DPP_CONFIGURATION_ATTRIBUTES_OBJECT,  "DPP Configuration Attributes Object" },
126   { DPP_BOOTSTRAPPING_KEY,                "DPP Bootstrapping Key" },
127   { DPP_FINITE_CYCLIC_GROUP,              "DPP Finite Cyclic Group" },
128   { DPP_ENCRYPTED_KEY,                    "DPP Encrypted Key" },
129   { DPP_CODE_IDENTIFIER,                  "DPP Code Identifier" },
130   { DPP_TRANSACTION_ID,                   "DPP Transaction ID" },
131   { DPP_BOOTSTRAPPING_INFO,               "DPP Bootstrapping Info" },
g_socket_listener_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)132   { DPP_CHANNEL,                          "DPP Channel" },
133   { DPP_PROTOCOL_VERSION,                 "DPP Protocol Version" },
134   { DPP_ENVELOPEDATA,                     "DPP Enveloped Data" },
135   { DPP_SENDCONNSTATUS,                   "DPP Send Conn Status" },
136   { DPP_CONNSTATUS,                       "DPP Conn Status" },
137   { DPP_RECONFIG_FLAGS,                   "DPP Reconfig Flags" },
138   { DPP_C_SIGN_KEY_HASH,                  "DPP C-sign key Hash" },
139   { DPP_CSR_ATTRIBUTES_REQUEST,           "DPP CSR Attributes Request" },
140   { DPP_A_NONCE,                          "DPP A-NONCE" },
141   { DPP_E_PRIME_ID,                       "DPP E'-id" },
142   { 0, NULL }
143 };
144 
145 enum {
146   DPP_AUTHENTICATION_REQUEST          = 0,
147   DPP_AUTHENTICATION_RESPONSE         = 1,
148   DPP_AUTHENTICATION_CONFIRM          = 2,
149   DPP_PEER_DISCOVERY_REQUEST          = 5,
150   DPP_PEER_DISCOVERY_RESPONSE         = 6,
g_socket_listener_class_init(GSocketListenerClass * klass)151   DPP_PKEX_EXCHANGE_REQUEST           = 7,
152   DPP_PKEX_EXCHANGE_RESPONSE          = 8,
153   DPP_PKEX_COMMIT_REVEAL_REQUEST      = 9,
154   DPP_PKEX_COMMIT_REVEAL_RESPONSE     = 10,
155   DPP_CONFIGURATION_RESULT            = 11,
156   DPP_CONNECTION_STATUS_RESULT        = 12,
157   DPP_PRESENCE_ANNOUNCEMENT           = 13,
158   DPP_RECONFIG_ANNOUNCEMENT           = 14,
159   DPP_RECONFIG_AUTH_REQUEST           = 15,
160   DPP_RECONFIG_AUTH_RESPONSE          = 16,
161   DPP_RECONFIG_AUTH_CONFIRM           = 17
162 };
163 
164 static const value_string dpp_public_action_subtypes[] = {
165   { DPP_AUTHENTICATION_REQUEST,           "Authentication Request" },
166   { DPP_AUTHENTICATION_RESPONSE,          "Authentication Response" },
167   { DPP_AUTHENTICATION_CONFIRM,           "Authentication Confirm" },
168   { DPP_PEER_DISCOVERY_REQUEST,           "Peer Discovery Request" },
169   { DPP_PEER_DISCOVERY_RESPONSE,          "Peer Discovery Response" },
170   { DPP_PKEX_EXCHANGE_REQUEST,            "PKEX Exchange Request" },
171   { DPP_PKEX_EXCHANGE_RESPONSE,           "PKEX Exchange Response" },
172   { DPP_PKEX_COMMIT_REVEAL_REQUEST,       "PKEX Commit-Reveal Request" },
173   { DPP_PKEX_COMMIT_REVEAL_RESPONSE,      "PKEX Commit-Reveal Response" },
174   { DPP_CONFIGURATION_RESULT,             "Configuration Result" },
175   { DPP_CONNECTION_STATUS_RESULT,         "Connection Status Result" },
176   { DPP_PRESENCE_ANNOUNCEMENT,            "Presence Announcement" },
177   { DPP_RECONFIG_ANNOUNCEMENT,            "Reconfig Announcement" },
178   { DPP_RECONFIG_AUTH_REQUEST,            "Reconfig Authentication Request" },
179   { DPP_RECONFIG_AUTH_RESPONSE,           "Reconfig Authentication Response" },
180   { DPP_RECONFIG_AUTH_CONFIRM,            "Reconfig Authentication Confirm" },
181   { 0, NULL }
182 };
183 
184 /*
185  * This table and the one above share values ... but this one is truncated.
186  */
187 static const value_string dpp_action_subtypes[] = {
188   { DPP_AUTHENTICATION_REQUEST, "Authentication Request" },
189   { DPP_AUTHENTICATION_RESPONSE, "Authentication Response" },
190   { DPP_AUTHENTICATION_CONFIRM, "Authentication Confirm" },
191   { DPP_PEER_DISCOVERY_REQUEST, "Peer Discovery Request" },
192   { DPP_PEER_DISCOVERY_RESPONSE, "Peer Discovery Response" },
193   { 0, NULL }
194 };
195 
196 static const range_string dpp_protocol_version_rvals[] = {
197   { 0, 0,   "Reserved" },
g_socket_listener_init(GSocketListener * listener)198   { 1, 1,   "1.0" },
199   { 2, 255, "Reserved" },
200   { 0, 0, NULL }
201 };
202 
203 static int proto_wifi_dpp = -1;
204 
205 static gint ett_wifi_dpp_ie_generic = -1;
206 static gint ett_wifi_dpp_attributes = -1;
207 static gint ett_wifi_dpp_pa = -1;
208 static gint ett_wifi_dpp_attribute = -1;
209 static gint ett_wifi_dpp_attr_header = -1;
210 static gint ett_wifi_dpp_attr_value = -1;
211 
212 static int hf_wifi_dpp_ie_attr_id = -1;
213 static int hf_wifi_dpp_ie_attr_len = -1;
214 static int hf_wifi_dpp_ie_generic = -1;  /* Remove eventually */
215 static int hf_wifi_dpp_action_dialog_token = -1;
216 static int hf_wifi_dpp_action_subtype = -1;
217 static int hf_wifi_dpp_crypto_suite = -1;
g_socket_listener_new(void)218 static int hf_wifi_dpp_public_action_subtype = -1;
219 static int hf_wifi_dpp_init_hash = -1;
220 static int hf_wifi_dpp_resp_hash = -1;
221 static int hf_wifi_dpp_status = -1;
222 static int hf_wifi_dpp_key_x = -1;
223 static int hf_wifi_dpp_key_y = -1;
check_listener(GSocketListener * listener,GError ** error)224 static int hf_wifi_dpp_trans_id = -1;
225 static int hf_wifi_dpp_finite_cyclic_group = -1;
226 static int hf_wifi_dpp_capabilities = -1;
227 static int hf_wifi_dpp_code_identifier = -1;
228 static int hf_wifi_dpp_enc_key_attribute = -1;
229 static int hf_wifi_dpp_primary_wrapped_data = -1;
230 static int hf_wifi_dpp_connector_attr = -1;
231 static int hf_wifi_dpp_initiator_nonce = -1;
232 static int hf_wifi_dpp_operating_class = -1;
233 static int hf_wifi_dpp_channel = -1;
234 static int hf_wifi_dpp_protocol_version = -1;
235 static int hf_wifi_dpp_a_nonce = -1;
236 static int hf_wifi_dpp_e_prime_id = -1;
237 static int hf_wifi_dpp_unknown_anqp_item = -1;
238 
239 static int hf_wifi_dpp_tcp_pdu_length = -1;
240 static int hf_wifi_dpp_tcp_pdu_action_field = -1;
241 static int hf_wifi_dpp_tcp_oui = -1;
242 static int hf_wifi_dpp_tcp_oui_type = -1;
243 static int hf_wifi_dpp_tcp_dialog_token = -1;
244 static int hf_wifi_dpp_tcp_adv_proto_elt = -1;
245 static int hf_wifi_dpp_tcp_vendor_specific = -1;
246 static int hf_wifi_dpp_tcp_vendor_spec_len = -1;
247 static int hf_wifi_dpp_tcp_config = -1;
248 static int hf_wifi_dpp_tcp_query_req_len = -1;
249 static int hf_wifi_dpp_tcp_status_code = -1;
250 static int hf_wifi_dpp_gas_query_resp_frag_id = -1;
251 static int hf_wifi_dpp_tcp_comeback_delay = -1;
252 static int hf_wifi_dpp_tcp_query_resp_len = -1;
253 
254 static int
255 dissect_wifi_dpp(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree, void *data _U_)
256 {
257   int offset = 0;
258 
259   proto_tree_add_item(tree, hf_wifi_dpp_unknown_anqp_item, tvb, offset, tvb_reported_length_remaining(tvb, offset), ENC_NA);
260   return tvb_captured_length(tvb);
261 }
262 
g_socket_listener_add_socket(GSocketListener * listener,GSocket * socket,GObject * source_object,GError ** error)263 static int
264 dissect_wifi_dpp_ie(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree, void *data _U_)
265 {
266   proto_item *ie;
267   guint remaining_len = tvb_reported_length(tvb);
268 
269   ie = proto_tree_add_subtree(tree, tvb, 0, remaining_len, ett_wifi_dpp_ie_generic, NULL, "Generic DPP IE");
270   proto_tree_add_item(ie, hf_wifi_dpp_ie_generic, tvb, 0, remaining_len,
271                       ENC_NA);
272   return tvb_captured_length(tvb);
273 }
274 
275 static int
276 dissect_wifi_dpp_attributes(packet_info *pinfo _U_, proto_tree *tree,
277                             tvbuff_t *tvb, int offset _U_)
278 {
279   proto_item *si = NULL;
280   guint8 status;
281   proto_tree *attr, *specific_attr, *attr_hdr;
282   guint16 attribute_id;
283   guint16 attribute_len;
284   guint attributes_len = 0;
285   guint remaining_len = tvb_reported_length_remaining(tvb, offset);
286 
287   while (remaining_len) {
288     attribute_id = tvb_get_guint16(tvb, offset, ENC_LITTLE_ENDIAN);
289     attribute_len = tvb_get_guint16(tvb, offset + 2, ENC_LITTLE_ENDIAN);
290     attr = proto_tree_add_subtree_format(tree, tvb, offset,
291                                 attribute_len + 4, ett_wifi_dpp_attribute,
292                                 &si, "%s Attribute",
293                                 val_to_str(attribute_id,
294                                         dpp_ie_attr_ids,
295                                         "Unknown (%u)"));
296     attr_hdr = proto_tree_add_subtree(attr, tvb, offset, 4,
297                                         ett_wifi_dpp_attr_header, NULL,
298                                         "Attribute Header");
299 
300     proto_tree_add_item(attr_hdr, hf_wifi_dpp_ie_attr_id, tvb, offset, 2,
301                         ENC_LITTLE_ENDIAN);
302     offset += 2;
303     proto_tree_add_item(attr_hdr, hf_wifi_dpp_ie_attr_len, tvb, offset, 2,
304                         ENC_LITTLE_ENDIAN);
305     offset += 2;
306 
307     specific_attr = proto_tree_add_subtree(attr, tvb, offset, attribute_len,
308                                         ett_wifi_dpp_attr_value,
309                                         NULL, "Attribute Value");
310 
311     switch (attribute_id) {
312     case DPP_STATUS:
313       status = tvb_get_guint8(tvb, offset);
314       proto_item_append_text(si, ": %s", val_to_str(status,
315                                          dpp_status_codes,
316                                          "Unknown (%u)"));
317       proto_tree_add_item(specific_attr, hf_wifi_dpp_status, tvb, offset, attribute_len, ENC_LITTLE_ENDIAN);
318       break;
319 
320     case DPP_INITIATOR_BOOTSTRAPPING_KEY_HASH:
321       proto_tree_add_item(specific_attr, hf_wifi_dpp_init_hash, tvb, offset, attribute_len, ENC_NA);
322       break;
323 
324     case DPP_RESPONDER_BOOTSTRAPPING_KEY_HASH:
325       proto_tree_add_item(specific_attr, hf_wifi_dpp_resp_hash, tvb, offset, attribute_len, ENC_NA);
326       break;
327 
328     case DPP_RESPONDER_PROTOCOL_KEY:
329     case DPP_INITIATOR_PROTOCOL_KEY:
330       // This is two protocol keys of equal length, X then Y.
331       proto_tree_add_item(specific_attr, hf_wifi_dpp_key_x, tvb, offset, attribute_len/2, ENC_NA);
332       proto_tree_add_item(specific_attr, hf_wifi_dpp_key_y, tvb, offset + attribute_len/2, attribute_len/2, ENC_NA);
g_socket_listener_add_address(GSocketListener * listener,GSocketAddress * address,GSocketType type,GSocketProtocol protocol,GObject * source_object,GSocketAddress ** effective_address,GError ** error)333       break;
334 
335     case DPP_TRANSACTION_ID:
336       proto_tree_add_item(specific_attr, hf_wifi_dpp_trans_id, tvb, offset, 1, ENC_LITTLE_ENDIAN);
337       break;
338 
339     case DPP_FINITE_CYCLIC_GROUP:
340     case DPP_RESPONDER_CAPABILITIES:
341       proto_tree_add_item(specific_attr, hf_wifi_dpp_finite_cyclic_group, tvb, offset, 2, ENC_LITTLE_ENDIAN);
342       break;
343 
344     case DPP_INITIATOR_CAPABILITIES:
345       proto_tree_add_item(specific_attr, hf_wifi_dpp_capabilities, tvb, offset, 1, ENC_LITTLE_ENDIAN);
346       break;
347 
348     case DPP_CODE_IDENTIFIER:
349       proto_tree_add_item(specific_attr, hf_wifi_dpp_code_identifier, tvb, offset, attribute_len, ENC_UTF_8|ENC_NA);
350       break;
351 
352     case DPP_ENCRYPTED_KEY:
353       proto_tree_add_item(specific_attr, hf_wifi_dpp_enc_key_attribute, tvb, offset, attribute_len, ENC_NA);
354       break;
355 
356     case DPP_WRAPPED_DATA:
357       proto_tree_add_item(specific_attr, hf_wifi_dpp_primary_wrapped_data, tvb, offset, attribute_len, ENC_NA);
358       break;
359 
360     case DPP_CONNECTOR:
361       proto_tree_add_item(specific_attr, hf_wifi_dpp_connector_attr, tvb,
362                           offset, attribute_len, ENC_NA);
363       break;
364 
365     case DPP_INITIATOR_NONCE:
366       proto_tree_add_item(specific_attr, hf_wifi_dpp_initiator_nonce, tvb,
367                           offset, attribute_len, ENC_NA);
368       break;
369 
370     case DPP_CHANNEL:
371       proto_tree_add_item(specific_attr, hf_wifi_dpp_operating_class, tvb,
372                           offset, 1, ENC_NA);
373       proto_tree_add_item(specific_attr, hf_wifi_dpp_channel, tvb, offset + 1,
374                           1, ENC_NA);
375       break;
376 
377     case DPP_PROTOCOL_VERSION:
378       proto_tree_add_item(specific_attr, hf_wifi_dpp_protocol_version, tvb,
379                           offset, 1, ENC_NA);
380       break;
381 
382     case DPP_A_NONCE:
383       proto_tree_add_item(specific_attr, hf_wifi_dpp_a_nonce, tvb, offset,
384                           attribute_len, ENC_NA);
385       break;
386 
387     case DPP_E_PRIME_ID:
388       proto_tree_add_item(specific_attr, hf_wifi_dpp_e_prime_id, tvb, offset,
389                           attribute_len, ENC_NA);
390        break;
391 
392     case DPP_INITIATOR_AUTHENTICATING_TAG:
393 
394     case DPP_RESPONDER_AUTHENTICATING_TAG:
395 
396     case DPP_CONFIGURATION_OBJECT:
397 
398     case DPP_CONFIGURATION_ATTRIBUTES_OBJECT:
399 
400     case DPP_BOOTSTRAPPING_KEY:
401 
402     case DPP_ENROLLEE_NONCE:
403 
404     default:
405       proto_tree_add_item(specific_attr, hf_wifi_dpp_ie_generic, tvb, offset, attribute_len, ENC_NA);
406       break;
407     }
408 
409     offset += attribute_len;
410     attributes_len += attribute_len + 4;
411     remaining_len -= (attribute_len + 4);
412 
413   }
414 
415   return attributes_len; // We return the attribute length plus hdr!
416 }
417 
418 int
419 dissect_wifi_dpp_config_proto(packet_info *pinfo _U_, proto_tree *tree,
420                              tvbuff_t *tvb, int offset _U_)
421 {
422   proto_item *dpp_item;
423   proto_tree *dpp_tree, *attr_tree;
424   guint remaining_len = tvb_reported_length_remaining(tvb, offset);
425 
426   dpp_item = proto_tree_add_item(tree, proto_wifi_dpp, tvb, offset, -1, ENC_NA);
427   dpp_tree = proto_item_add_subtree(dpp_item, ett_wifi_dpp_pa);
428   proto_item_append_text(dpp_item, " Configuration");
429 
430   attr_tree = proto_tree_add_subtree_format(dpp_tree, tvb, offset,
431                                             remaining_len,
g_socket_listener_add_inet_port(GSocketListener * listener,guint16 port,GObject * source_object,GError ** error)432                                             ett_wifi_dpp_attributes, NULL,
433                                             "DPP Attributes");
434 
435   offset = dissect_wifi_dpp_attributes(pinfo, attr_tree, tvb, offset);
436 
437   return offset;
438 }
439 
440 int
441 dissect_wifi_dpp_public_action(tvbuff_t *tvb, packet_info *pinfo,
442                                proto_tree *tree, void *data _U_)
443 {
444   guint8 subtype;
445   guint remaining_len;
446   proto_item *dpp_item;
447   proto_tree *dpp_tree, *attr_tree;
448   guint16 attributes_len;
449   int offset = 0;
450 
451   col_set_str(pinfo->cinfo, COL_PROTOCOL, "wifi_dpp");
452 
453   /* The Crypto suite comes before the DPP frame type */
454   subtype = tvb_get_guint8(tvb, offset + 1);
455   col_append_fstr(pinfo->cinfo, COL_INFO, ", DPP - %s",
456                   val_to_str(subtype, dpp_public_action_subtypes,
457                              "Unknown (%u)"));
458 
459   remaining_len = tvb_reported_length_remaining(tvb, offset);
460 
461   dpp_item = proto_tree_add_item(tree, proto_wifi_dpp, tvb, offset, -1, ENC_NA);
462   dpp_tree = proto_item_add_subtree(dpp_item, ett_wifi_dpp_pa);
463   proto_item_append_text(dpp_item, ": %s", val_to_str(subtype,
464                                                  dpp_public_action_subtypes,
465                                                  "Unknown (%u)"));
466   proto_tree_add_item(dpp_tree, hf_wifi_dpp_crypto_suite, tvb, offset, 1,
467                       ENC_LITTLE_ENDIAN);
468   offset++;
469   remaining_len--;
470 
471   proto_tree_add_item(dpp_tree, hf_wifi_dpp_public_action_subtype, tvb, offset,
472                       1, ENC_LITTLE_ENDIAN);
473   offset++;  /* Skip the OUI Subtype/DPP Request type */
474   remaining_len--;
475   if (remaining_len) {
476     attr_tree = proto_tree_add_subtree_format(dpp_tree, tvb, offset,
477                        remaining_len,
478                        ett_wifi_dpp_attributes, NULL, "DPP Attributes");
479 
480     attributes_len = dissect_wifi_dpp_attributes(pinfo, attr_tree, tvb, offset);
481     offset += attributes_len;
482   }
483 
484   return offset;
485 }
486 
487 static int
488 dissect_wifi_dpp_tcp_pdu(tvbuff_t *tvb, packet_info *pinfo _U_,
489   proto_tree *tree, void *data _U_)
490 {
491   int offset = 0;
492   guint8 action;
493   tvbuff_t *newtvb;
494 
495   col_set_str(pinfo->cinfo, COL_PROTOCOL, "dpp");
496 
497   /*
498    * We get a length, followed by Action field, OUI, OUI type and then a
499    * DPP public action
500    */
501   proto_tree_add_item(tree, hf_wifi_dpp_tcp_pdu_length, tvb, offset, 4,
502                       ENC_BIG_ENDIAN);
503   offset += 4;
504 
505   action = tvb_get_guint8(tvb, offset);
506   proto_tree_add_item(tree, hf_wifi_dpp_tcp_pdu_action_field, tvb, offset, 1,
507                       ENC_NA);
508   offset += 1;
509 
510   if (action == 0x09) {
511     proto_tree_add_item(tree, hf_wifi_dpp_tcp_oui, tvb, offset, 3, ENC_NA);
512     offset += 3;
513 
514     proto_tree_add_item(tree, hf_wifi_dpp_tcp_oui_type, tvb, offset, 1, ENC_NA);
515     offset += 1;
516 
517     newtvb = tvb_new_subset_remaining(tvb, offset);
518 
519     offset += dissect_wifi_dpp_public_action(newtvb, pinfo, tree, NULL);
520   } else if (action == 0x0a) {
521     col_append_str(pinfo->cinfo, COL_INFO, ", DPP - Configuration Request");
522 
523     proto_tree_add_item(tree, hf_wifi_dpp_tcp_dialog_token, tvb, offset, 1,
524                         ENC_NA);
525     offset += 1;
526 
527     proto_tree_add_item(tree, hf_wifi_dpp_tcp_adv_proto_elt, tvb, offset, 3,
528                         ENC_NA);
529     offset += 3;
530 
531     proto_tree_add_item(tree, hf_wifi_dpp_tcp_vendor_specific, tvb, offset, 1,
532                         ENC_NA);
533     offset += 1;
534     proto_tree_add_item(tree, hf_wifi_dpp_tcp_vendor_spec_len, tvb, offset, 1,
535                         ENC_NA);
536     offset += 1;
537 
538     proto_tree_add_item(tree, hf_wifi_dpp_tcp_oui, tvb, offset, 3, ENC_NA);
539     offset += 3;
540 
541     proto_tree_add_item(tree, hf_wifi_dpp_tcp_oui_type, tvb, offset, 1, ENC_NA);
542     offset += 1;
543 
544     proto_tree_add_item(tree, hf_wifi_dpp_tcp_config, tvb, offset, 1, ENC_NA);
545     offset += 1;
546 
547     proto_tree_add_item(tree, hf_wifi_dpp_tcp_query_req_len, tvb, offset, 2,
548                         ENC_LITTLE_ENDIAN);
549     offset += 2;
550 
551     offset += dissect_wifi_dpp_config_proto(pinfo, tree, tvb, offset);
552   } else if (action == 0x0b || action == 0x0d) {
553     guint16 qr_len;
554 
555     col_append_str(pinfo->cinfo, COL_INFO, ", DPP - Configuration Response");
556 
557     proto_tree_add_item(tree, hf_wifi_dpp_tcp_dialog_token, tvb, offset, 1,
558                         ENC_NA);
559     offset += 1;
560 
561     proto_tree_add_item(tree, hf_wifi_dpp_tcp_status_code, tvb, offset, 2,
562                         ENC_LITTLE_ENDIAN);
563     offset += 2;
564 
565     if (action == 0x0d) {
566       proto_tree_add_item(tree, hf_wifi_dpp_gas_query_resp_frag_id, tvb, offset,
567                           1, ENC_NA);
568       offset += 1;
569     }
570 
571     proto_tree_add_item(tree, hf_wifi_dpp_tcp_comeback_delay, tvb, offset, 2,
572                         ENC_LITTLE_ENDIAN);
573     offset += 2;
574 
575     proto_tree_add_item(tree, hf_wifi_dpp_tcp_adv_proto_elt, tvb, offset, 3,
576                         ENC_NA);
577     offset += 3;
578 
579     proto_tree_add_item(tree, hf_wifi_dpp_tcp_vendor_specific, tvb, offset, 1,
580                         ENC_NA);
581     offset += 1;
582     proto_tree_add_item(tree, hf_wifi_dpp_tcp_vendor_spec_len, tvb, offset, 1,
583                         ENC_NA);
584     offset += 1;
585 
586     proto_tree_add_item(tree, hf_wifi_dpp_tcp_oui, tvb, offset, 3, ENC_NA);
587     offset += 3;
588 
589     proto_tree_add_item(tree, hf_wifi_dpp_tcp_oui_type, tvb, offset, 1, ENC_NA);
590     offset += 1;
591 
592     proto_tree_add_item(tree, hf_wifi_dpp_tcp_config, tvb, offset, 1, ENC_NA);
593     offset += 1;
594 
595     qr_len = tvb_get_letohs(tvb, offset);
596     proto_tree_add_item(tree, hf_wifi_dpp_tcp_query_resp_len, tvb, offset, 2,
597                         ENC_LITTLE_ENDIAN);
598     offset += 2;
599 
add_sources(GSocketListener * listener,GSocketSourceFunc callback,gpointer callback_data,GCancellable * cancellable,GMainContext * context)600     if (qr_len) {
601       offset += dissect_wifi_dpp_config_proto(pinfo, tree, tvb, offset);
602     }
603   }
604 
605   return offset;
606 }
607 
608 static guint
609 get_wifi_dpp_tcp_len(packet_info *pinfo _U_, tvbuff_t *tvb, int offset,
610   void *data _U_)
611 {
612   guint pkt_len;
613 
614   pkt_len = tvb_get_ntohl(tvb, offset);
615 
616   return pkt_len + 4;
617 }
618 
619 /*
620  * We need 4 bytes for the length ...
621  */
622 #define DPP_TCP_HEADER_LEN 4
623 static int
624 dissect_wifi_dpp_tcp_pdus(tvbuff_t *tvb, packet_info *pinfo _U_,
625   proto_tree *tree, void *data _U_)
626 {
627   if (!tvb_bytes_exist(tvb, 0, DPP_TCP_HEADER_LEN))
628     return 0;
free_sources(GList * sources)629 
630   tcp_dissect_pdus(tvb, pinfo, tree, TRUE, DPP_TCP_HEADER_LEN,
631                    get_wifi_dpp_tcp_len, dissect_wifi_dpp_tcp_pdu, data);
632   return tvb_reported_length(tvb);
633 }
634 
635 void
636 proto_register_wifi_dpp(void)
637 {
638   static module_t *wifi_dpp_module;
639   static hf_register_info hf[] = {
640     { &hf_wifi_dpp_status,
641       { "Wi-Fi DPP Status", "dpp.status",
642         FT_UINT8, BASE_HEX, VALS(dpp_status_codes), 0x0, NULL, HFILL }},
643     { &hf_wifi_dpp_init_hash,
644       { "Wi-Fi DPP Initiator Hash", "dpp.init.hash",
645         FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
646     { &hf_wifi_dpp_resp_hash,
accept_callback(GSocket * socket,GIOCondition condition,gpointer user_data)647       { "Wi-Fi DPP Responder Hash", "dpp.resp.hash",
648         FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
649     { &hf_wifi_dpp_key_x,
650       { "Wi-Fi DPP Key X value", "dpp.key.x",
651         FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
652     { &hf_wifi_dpp_key_y,
653       { "Wi-Fi DPP Key Y value", "dpp.key.y",
654         FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
655     { &hf_wifi_dpp_trans_id,
656       { "Wi-Fi DPP Transaction ID", "dpp.trans_id",
657         FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
658     { &hf_wifi_dpp_finite_cyclic_group,
659       { "Wi-Fi DPP Finite Cyclic Group", "dpp.finite_cyclic_group",
660         FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }},
661     { &hf_wifi_dpp_capabilities,
662       { "Wi-Fi DPP Capabilities", "dpp.capabilities",
663         FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL }},
664     { &hf_wifi_dpp_code_identifier,
665       { "Wi-Fi DPP Code Identifier", "dpp.code_identifier",
666         FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL }},
667     { &hf_wifi_dpp_enc_key_attribute,
668       { "Wi-Fi DPP Encrypted Key Attribute", "dpp.pkex.enckey",
669         FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
670     { &hf_wifi_dpp_primary_wrapped_data,
671       { "Wi-Fi DPP Primary Wrapped Data", "dpp.primary.wrapped_data",
672         FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
673     { &hf_wifi_dpp_connector_attr,
674       { "Wi-Fi DPP Connector Attribute", "dpp.connector_data",
675         FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
676     { &hf_wifi_dpp_initiator_nonce,
677       { "Wi-Fi DPP Initiator Nonce", "dpp.initiator_nonce",
678         FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
679     { &hf_wifi_dpp_operating_class,
680       { "Operating Class", "dpp.operating_class",
681        FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
682     { &hf_wifi_dpp_channel,
683       { "Channel", "dpp.channel",
684         FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
685     { &hf_wifi_dpp_protocol_version,
g_socket_listener_accept_socket(GSocketListener * listener,GObject ** source_object,GCancellable * cancellable,GError ** error)686       { "Protocol Version", "dpp.protocol_version",
687         FT_UINT8, BASE_DEC|BASE_RANGE_STRING, RVALS(dpp_protocol_version_rvals),
688         0x0, NULL, HFILL }},
689     { &hf_wifi_dpp_a_nonce,
690       { "A-NONCE", "dpp.a_nonce",
691         FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
692     { &hf_wifi_dpp_e_prime_id,
693       { "E'-id", "dpp.e_prime_id",
694         FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
695     { &hf_wifi_dpp_ie_attr_id,
696       { "Wi-Fi DPP IE Attribute ID", "dpp.ie.attr_id",
697         FT_UINT16, BASE_HEX, VALS(dpp_ie_attr_ids), 0x0, NULL, HFILL }},
698     { &hf_wifi_dpp_ie_attr_len,
699       { "Wi-Fi DPP IE Attribute Len", "dpp.ie.attr_len",
700        FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
701     { &hf_wifi_dpp_ie_generic,
702       { "Wi-Fi DPP IE generic", "dpp.ie.generic",
703         FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
704     { &hf_wifi_dpp_action_subtype,
705       { "Wi-Fi DPP Action Subtype", "dpp.action.subtype",
706         FT_UINT8, BASE_DEC, VALS(dpp_action_subtypes), 0x0, NULL, HFILL }},
707     { &hf_wifi_dpp_action_dialog_token,
708       { "Wi-Fi DPP Action Dialog Token", "dpp.action.dialog_token",
709         FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
710     { &hf_wifi_dpp_crypto_suite,
711       { "Wi-Fi DPP Cryptographic Suite", "dpp.public_action.crypto_suite",
712         FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
713     { &hf_wifi_dpp_public_action_subtype,
714       { "Wi-Fi DPP Public Action Subtype", "dpp.public_action.subtype",
715         FT_UINT8, BASE_DEC, VALS(dpp_public_action_subtypes), 0x0, NULL, HFILL }},
716     { &hf_wifi_dpp_unknown_anqp_item,
717       { "Wi-fi DPP Unknown ANQP Item", "dpp.unknown_anqp_item",
718         FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
719 
720     { &hf_wifi_dpp_tcp_pdu_length,
721       { "DPP TCP PDU length", "dpp.tcp.length",
722         FT_UINT32, BASE_DEC, NULL, 0x0, NULL, HFILL }},
723 
724     { &hf_wifi_dpp_tcp_pdu_action_field,
725       { "DPP TCP PDU Action type", "dpp.tcp.action_type",
726         FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL }},
727 
728     { &hf_wifi_dpp_tcp_oui,
729       { "DPP TCP PDU OUI", "dpp.tcp.oui",
730         FT_UINT24, BASE_OUI, NULL, 0x0, NULL, HFILL }},
731 
732     { &hf_wifi_dpp_tcp_oui_type,
733       { "DPP TCP PDU OUI type", "dpp.tcp.oui_type",
734         FT_UINT8, BASE_DEC, VALS(wfa_subtype_vals), 0, NULL, HFILL }},
735 
736     { &hf_wifi_dpp_tcp_dialog_token,
737       { "DPP TCP PDU Dialog Token", "dpp.tcp.dialog_token",
738         FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL }},
739 
740     { &hf_wifi_dpp_tcp_adv_proto_elt,
741       { "DPP TCP PDU Advertisement Protocol Element",
742         "dpp.tcp.adv_proto_elt",
743         FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
744 
745     { &hf_wifi_dpp_tcp_vendor_specific,
746       { "DPP TCP PDU Vendor Specific tag", "dpp.tcp.vendor_spec_tag",
747         FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL }},
748 
749     { &hf_wifi_dpp_tcp_vendor_spec_len,
750       { "DPP TCP PDU Vendor Specific len", "dpp.tcp.vendor_spec_len",
751         FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
752 
753     { &hf_wifi_dpp_tcp_config,
754       { "DPP TCP PDU Configuration", "dpp.tcp.config",
755         FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
756 
757     { &hf_wifi_dpp_tcp_query_req_len,
758       { "DPP TCP PDU Query Req len", "dpp.tcp.query_req_len",
759         FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
g_socket_listener_accept(GSocketListener * listener,GObject ** source_object,GCancellable * cancellable,GError ** error)760 
761     { &hf_wifi_dpp_gas_query_resp_frag_id,
762       { "DPP TCP PDU GAS Query Response Fragment ID",
763         "dpp.tp.query_resp_frag_id",
764         FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
765 
766     { &hf_wifi_dpp_tcp_status_code,
767       { "DPP TCP PDU Status Code", "dpp.tcp.status_code",
768         FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
769 
770     { &hf_wifi_dpp_tcp_comeback_delay,
771       { "DPP TCP PDU Comeback Delay", "dpp.tcp.comeback_delay",
772         FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
773 
774     { &hf_wifi_dpp_tcp_query_resp_len,
775       { "DPP TCP PDU Query Resp Len", "dpp.tcp.query_resp_len",
776         FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
777   };
778   static gint *ett[] = {
779     &ett_wifi_dpp_ie_generic,
780     &ett_wifi_dpp_attributes,
781     &ett_wifi_dpp_pa,
782     &ett_wifi_dpp_attribute,
783     &ett_wifi_dpp_attr_header,
784     &ett_wifi_dpp_attr_value,
785   };
786 
787   proto_wifi_dpp = proto_register_protocol("Wi-Fi Device Provisioning Protocol", "Wi-Fi DPP", "dpp");
accept_socket_async_data_free(AcceptSocketAsyncData * data)788   proto_register_field_array(proto_wifi_dpp, hf, array_length(hf));
789   proto_register_subtree_array(ett, array_length(ett));
790 
791   /* Register the preferred TCP port? Is there one? */
792   wifi_dpp_module = prefs_register_protocol(proto_wifi_dpp, NULL);
793   prefs_register_uint_preference(wifi_dpp_module, "tcp.port", "DPP TCP Port",
794                                  "The TCP port DPP over TCP uses",
accept_ready(GSocket * accept_socket,GIOCondition condition,gpointer user_data)795                                  10, &wifi_dpp_tcp_port);
796 }
797 
798 void
799 proto_reg_handoff_wifi_dpp(void)
800 {
801   static gboolean initialized = FALSE;
802   static dissector_handle_t wifi_dpp_tcp_handle;
803   static int current_port;
804 
805   dissector_add_uint("wlan.anqp.wifi_alliance.subtype", WFA_SUBTYPE_DPP, create_dissector_handle(dissect_wifi_dpp, proto_wifi_dpp));
806   dissector_add_uint("wlan.ie.wifi_alliance.subtype", WFA_SUBTYPE_DPP, create_dissector_handle(dissect_wifi_dpp_ie, proto_wifi_dpp));
807   dissector_add_uint("wlan.pa.wifi_alliance.subtype", WFA_SUBTYPE_DPP, create_dissector_handle(dissect_wifi_dpp_public_action, proto_wifi_dpp));
808 
809   /*
810    * Register the TCP port
811    */
812   if (!initialized) {
813     wifi_dpp_tcp_handle = create_dissector_handle(dissect_wifi_dpp_tcp_pdus,
814                                                   proto_wifi_dpp);
815     initialized = TRUE;
816   } else {
817     dissector_delete_uint("tcp.port", current_port, wifi_dpp_tcp_handle);
818   }
819 
820   current_port = wifi_dpp_tcp_port;
821   dissector_add_uint("tcp.port", current_port, wifi_dpp_tcp_handle);
822 }
823 
824 /*
825  * Editor modelines
826  *
827  * Local Variables:
828  * c-basic-offset: 2
829  * tab-width: 8
830  * indent-tabs-mode: nil
831  * End:
832  *
833  * ex: set shiftwidth=2 tabstop=8 expandtab:
834  * :indentSize=2:tabSize=8:noTabs=true:
835  */
836