5 * Giorgio Tino <giorgio.tino@cacetech.com>
6 * Copyright (c) CACE Technologies, LLC 2006
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
10 * Copyright 2000 Gerald Combs
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
42 #include <epan/packet.h>
43 #include <epan/prefs.h>
44 #include <epan/prefs-int.h>
45 #include <epan/crypt/wep-wpadefs.h>
46 #include "capture_ui_utils.h"
48 #include "simple_dialog.h"
51 #include "airpdcap/airpdcap_ws.h"
54 #include "airpcap_loader.h"
57 * We load dinamically the dag library in order link it only when
58 * it's present on the system
60 static HMODULE AirpcapLib = NULL;
63 * Set to TRUE if the DLL was successfully loaded AND all functions
66 static gboolean AirpcapLoaded = FALSE;
68 static AirpcapGetLastErrorHandler g_PAirpcapGetLastError;
69 static AirpcapGetDeviceListHandler g_PAirpcapGetDeviceList;
70 static AirpcapFreeDeviceListHandler g_PAirpcapFreeDeviceList;
71 static AirpcapOpenHandler g_PAirpcapOpen;
72 static AirpcapCloseHandler g_PAirpcapClose;
73 static AirpcapGetLinkTypeHandler g_PAirpcapGetLinkType;
74 static AirpcapSetLinkTypeHandler g_PAirpcapSetLinkType;
75 static AirpcapSetKernelBufferHandler g_PAirpcapSetKernelBuffer;
76 static AirpcapSetFilterHandler g_PAirpcapSetFilter;
77 static AirpcapGetMacAddressHandler g_PAirpcapGetMacAddress;
78 static AirpcapSetMinToCopyHandler g_PAirpcapSetMinToCopy;
79 static AirpcapGetReadEventHandler g_PAirpcapGetReadEvent;
80 static AirpcapReadHandler g_PAirpcapRead;
81 static AirpcapGetStatsHandler g_PAirpcapGetStats;
82 static AirpcapTurnLedOnHandler g_PAirpcapTurnLedOn;
83 static AirpcapTurnLedOffHandler g_PAirpcapTurnLedOff;
84 static AirpcapGetDeviceChannelHandler g_PAirpcapGetDeviceChannel;
85 static AirpcapSetDeviceChannelHandler g_PAirpcapSetDeviceChannel;
86 static AirpcapGetFcsPresenceHandler g_PAirpcapGetFcsPresence;
87 static AirpcapSetFcsPresenceHandler g_PAirpcapSetFcsPresence;
88 static AirpcapGetFcsValidationHandler g_PAirpcapGetFcsValidation;
89 static AirpcapSetFcsValidationHandler g_PAirpcapSetFcsValidation;
90 static AirpcapGetDeviceKeysHandler g_PAirpcapGetDeviceKeys;
91 static AirpcapSetDeviceKeysHandler g_PAirpcapSetDeviceKeys;
92 static AirpcapGetDriverKeysHandler g_PAirpcapGetDriverKeys;
93 static AirpcapSetDriverKeysHandler g_PAirpcapSetDriverKeys;
94 static AirpcapGetDecryptionStateHandler g_PAirpcapGetDecryptionState;
95 static AirpcapSetDecryptionStateHandler g_PAirpcapSetDecryptionState;
96 static AirpcapGetDriverDecryptionStateHandler g_PAirpcapGetDriverDecryptionState;
97 static AirpcapSetDriverDecryptionStateHandler g_PAirpcapSetDriverDecryptionState;
98 static AirpcapStoreCurConfigAsAdapterDefaultHandler g_PAirpcapStoreCurConfigAsAdapterDefault;
99 static AirpcapGetVersionHandler g_PAirpcapGetVersion;
101 /* Airpcap interface list */
102 GList *airpcap_if_list = NULL;
104 /* Airpcap current selected interface */
105 airpcap_if_info_t *airpcap_if_selected = NULL;
107 /* Airpcap current active interface */
108 airpcap_if_info_t *airpcap_if_active = NULL;
110 /* WLAN preferences pointer */
111 module_t *wlan_prefs = NULL;
114 * Callback used by the load_wlan_keys() routine in order to read a WEP decryption key
117 get_wep_key(pref_t *pref, gpointer ud _U_)
119 gchar *my_string = NULL;
120 keys_cb_data_t* user_data;
122 decryption_key_t* new_key;
124 /* Retrieve user data info */
125 user_data = (keys_cb_data_t*)ud;
127 if (g_strncasecmp(pref->name, "wep_key", 7) == 0 && pref->type == PREF_STRING)
129 my_string = g_strdup(*pref->varp.string);
131 /* Here we have the string describing the key... */
132 new_key = parse_key_string(my_string);
136 /* Key is added only if not null ... */
137 user_data->list = g_list_append(user_data->list,new_key);
138 user_data->number_of_keys++;
139 user_data->current_index++;
145 /* Returs TRUE if the WEP key is valid, false otherwise */
147 wep_key_is_valid(char* key)
149 GString *new_key_string;
155 new_key_string = g_string_new(key);
157 if( ((new_key_string->len) > WEP_KEY_MAX_CHAR_SIZE) || ((new_key_string->len) < 2))
159 g_string_free(new_key_string,FALSE);
162 if((new_key_string->len % 2) != 0)
164 g_string_free(new_key_string,FALSE);
167 for(i = 0; i < new_key_string->len; i++)
169 if(!g_ascii_isxdigit(new_key_string->str[i]))
171 g_string_free(new_key_string,FALSE);
176 g_string_free(new_key_string,FALSE);
180 /* Callback used by the save_wlan_keys() routine in order to write a decryption key */
182 set_wep_key(pref_t *pref, gpointer ud _U_)
184 gchar *my_string = NULL;
185 keys_cb_data_t* user_data;
186 gint wep_key_number = 0;
188 decryption_key_t* new_key;
190 /* Retrieve user data info */
191 user_data = (keys_cb_data_t*)ud;
193 if (g_strncasecmp(pref->name, "wep_key", 7) == 0 && pref->type == PREF_STRING)
195 /* Ok, the pref we're gonna set is a wep_key ... but what number? */
196 sscanf(pref->name,"wep_key%d",&wep_key_number);
198 if(user_data->current_index < user_data->number_of_keys)
200 if(wep_key_number == (user_data->current_index+1))
202 /* Retrieve the nth decryption_key_t structure pointer */
203 new_key = (decryption_key_t*)g_list_nth_data(user_data->list,user_data->current_index);
205 /* Free the old key string */
206 g_free((void *)*pref->varp.string);
208 /* Create the new string describing the decryption key */
209 my_string = get_key_string(new_key);
211 /* Duplicate the string, and assign it to the variable pointer */
212 *pref->varp.string = (void *)g_strdup(my_string);
214 /* Free the previously allocated string */
218 else /* If the number of keys has been reduced somehow, we need to delete all the other keys
219 * (remember that the new ones have been probably overwritten)
222 g_free((void *)*pref->varp.string);
223 *pref->varp.string = (void *)g_strdup(""); /* Do not just free memory!!! Put an 'empty' string! */
225 user_data->current_index++;
232 * Function used to read the Decryption Keys from the preferences and store them
233 * properly into the airpcap adapter.
236 load_wlan_driver_wep_keys()
238 keys_cb_data_t* user_data;
242 /* Retrieve the wlan preferences */
243 wlan_prefs = prefs_find_module("wlan");
245 /* Allocate a structure used to keep infos between the callbacks */
246 user_data = (keys_cb_data_t*)g_malloc(sizeof(keys_cb_data_t));
248 /* Fill the structure */
249 user_data->list = NULL;
250 user_data->current_index = 0;
251 user_data->number_of_keys= 0; /* Still unknown */
253 /* Run the callback on each 802.11 preference */
254 prefs_pref_foreach(wlan_prefs, get_wep_key, (gpointer)user_data);
256 /* Now the key list should be filled */
259 * Signal that we've changed things, and run the 802.11 dissector's
262 wlan_prefs->prefs_changed = TRUE;
264 prefs_apply(wlan_prefs);
266 write_wlan_driver_wep_keys_to_regitry(user_data->list);
269 /* free the WEP key string */
270 for(i=0;i<g_list_length(user_data->list);i++)
272 g_free(g_list_nth(user_data->list,i)->data);
275 /* free the (empty) list */
276 g_list_free(user_data->list);
278 /* free the user_data structure */
281 /* airpcap_if_info_free(fake_info_if); */
287 * This function will tell the airpcap driver the key list to use
288 * This will be stored into the registry...
291 write_wlan_wep_keys_to_regitry(airpcap_if_info_t* info_if, GList* key_list)
296 PAirpcapKeysCollection KeysCollection;
297 ULONG KeysCollectionSize;
299 UINT keys_in_list = 0;
300 decryption_key_t* key_item = NULL;
302 keys_in_list = g_list_length(key_list);
305 * Save the encryption keys, if we have any of them
307 KeysCollectionSize = 0;
310 * Calculate the size of the keys collection
312 KeysCollectionSize = sizeof(AirpcapKeysCollection) + keys_in_list * sizeof(AirpcapKey);
315 * Allocate the collection
317 KeysCollection = (PAirpcapKeysCollection)g_malloc(KeysCollectionSize);
324 * Populate the key collection
326 KeysCollection->nKeys = keys_in_list;
328 for(i = 0; i < keys_in_list; i++)
330 KeysCollection->Keys[i].KeyType = AIRPDCAP_KEY_TYPE_WEP;
332 /* Retrieve the Item corresponding to the i-th key */
333 key_item = (decryption_key_t*)g_list_nth_data(key_list,i);
334 new_key = g_string_new(key_item->key->str);
336 KeysCollection->Keys[i].KeyLen = new_key->len / 2;
337 memset(&KeysCollection->Keys[i].KeyData, 0, sizeof(KeysCollection->Keys[i].KeyData));
339 for(j = 0 ; j < new_key->len; j += 2)
341 s[0] = new_key->str[j];
342 s[1] = new_key->str[j+1];
344 KeyByte = (UCHAR)strtol(s, NULL, 16);
345 KeysCollection->Keys[i].KeyData[j / 2] = KeyByte;
348 g_string_free(new_key,TRUE);
352 * Free the old adapter key collection!
354 if(info_if->keysCollection != NULL)
355 g_free(info_if->keysCollection);
358 * Set this collection ad the new one
360 info_if->keysCollection = KeysCollection;
361 info_if->keysCollectionSize = KeysCollectionSize;
364 * Configuration must be saved
366 info_if->saved = FALSE;
369 * Write down the changes to the registry
371 airpcap_save_selected_if_configuration(info_if);
377 * This function will tell the airpcap driver the key list to use
378 * This will be stored into the registry...
381 write_wlan_driver_wep_keys_to_regitry(GList* key_list)
386 PAirpcapKeysCollection KeysCollection;
387 ULONG KeysCollectionSize;
389 UINT keys_in_list = 0;
390 decryption_key_t* key_item = NULL;
391 airpcap_if_info_t* fake_info_if = NULL;
393 /* Create the fake_info_if from the first adapter of the list */
394 fake_info_if = airpcap_driver_fake_if_info_new();
396 if(fake_info_if == NULL)
400 * XXX - When WPA will be supported, change this to: keys_in_list = g_list_length(key_list);
401 * but right now we will have to count only the WEP keys (or we will have a malloc-mess :-) )
403 n = g_list_length(key_list);
404 for(k = 0; k < n; k++ )
405 if(((decryption_key_t*)g_list_nth_data(key_list,k))->type == AIRPDCAP_KEY_TYPE_WEP)
409 * Save the encryption keys, if we have any of them
411 KeysCollectionSize = 0;
414 * Calculate the size of the keys collection
416 KeysCollectionSize = sizeof(AirpcapKeysCollection) + keys_in_list * sizeof(AirpcapKey);
419 * Allocate the collection
421 KeysCollection = (PAirpcapKeysCollection)g_malloc(KeysCollectionSize);
428 * Populate the key collection
430 KeysCollection->nKeys = keys_in_list;
433 * XXX - If we have, let's say, six keys, the first three are WEP, then two are WPA, and the
434 * last is WEP, we have to scroll the whole list (n) but increment the array counter only
435 * when a WEP key is found (y) .. When WPA will be supported by the driver, I'll have to change
438 y = 0; /* Current position in the key list */
440 for(i = 0; i < n; i++)
442 /* Retrieve the Item corresponding to the i-th key */
443 key_item = (decryption_key_t*)g_list_nth_data(key_list,i);
446 * XXX - The AIRPDCAP_KEY_TYPE_WEP is the only supportd right now!
447 * We will have to modify the AirpcapKey structure in order to
448 * support the other two types! What happens now, is that simply the
449 * not supported keys will just be discarded (they will be saved in wireshark though)
451 if(key_item->type == AIRPDCAP_KEY_TYPE_WEP)
453 KeysCollection->Keys[y].KeyType = AIRPDCAP_KEY_TYPE_WEP;
455 new_key = g_string_new(key_item->key->str);
457 KeysCollection->Keys[y].KeyLen = new_key->len / 2;
458 memset(&KeysCollection->Keys[y].KeyData, 0, sizeof(KeysCollection->Keys[y].KeyData));
460 for(j = 0 ; j < new_key->len; j += 2)
462 s[0] = new_key->str[j];
463 s[1] = new_key->str[j+1];
465 KeyByte = (UCHAR)strtol(s, NULL, 16);
466 KeysCollection->Keys[y].KeyData[j / 2] = KeyByte;
468 /* XXX - Change when WPA will be supported!!! */
470 g_string_free(new_key,TRUE);
472 else if(key_item->type == AIRPDCAP_KEY_TYPE_WPA_PWD)
474 /* XXX - The driver cannot deal with this kind of key yet... */
476 else if(key_item->type == AIRPDCAP_KEY_TYPE_WPA_PMK)
478 /* XXX - The driver cannot deal with this kind of key yet... */
483 * Free the old adapter key collection!
485 if(fake_info_if->keysCollection != NULL)
486 g_free(fake_info_if->keysCollection);
489 * Set this collection ad the new one
491 fake_info_if->keysCollection = KeysCollection;
492 fake_info_if->keysCollectionSize = KeysCollectionSize;
495 * Configuration must be saved
497 fake_info_if->saved = FALSE;
500 * Write down the changes to the registry
502 airpcap_save_driver_if_configuration(fake_info_if);
504 airpcap_if_info_free(fake_info_if);
510 * Function used to save to the preference file the Decryption Keys.
513 save_wlan_driver_wep_keys()
515 GList* key_list = NULL;
516 char* tmp_key = NULL;
517 guint keys_in_list,i;
518 keys_cb_data_t* user_data;
519 airpcap_if_info_t* fake_info_if = NULL;
521 /* Create the fake_info_if from the first adapter of the list */
522 fake_info_if = airpcap_driver_fake_if_info_new();
524 if(fake_info_if == NULL)
527 /* Retrieve the wlan preferences */
528 wlan_prefs = prefs_find_module("wlan");
530 /* Allocate a structure used to keep infos between the callbacks */
531 user_data = (keys_cb_data_t*)g_malloc(sizeof(keys_cb_data_t));
533 /* Number of keys in key list */
534 /* Number of keys in key list */
535 if(fake_info_if->keysCollectionSize != 0)
536 keys_in_list = (guint)(fake_info_if->keysCollectionSize - sizeof(AirpcapKeysCollection))/sizeof(AirpcapKey);
540 for(i=0; i<keys_in_list; i++)
542 /* Only if it is a WEP key... */
543 if(fake_info_if->keysCollection->Keys[i].KeyType == AIRPDCAP_KEY_TYPE_WEP)
545 tmp_key = airpcap_get_key_string(fake_info_if->keysCollection->Keys[i]);
546 key_list = g_list_append(key_list,g_strdup(tmp_key));
551 /* Now we know the exact number of WEP keys in the list, so store it ... */
552 keys_in_list = g_list_length(key_list);
554 /* Fill the structure */
555 user_data->list = key_list;
556 user_data->current_index = 0;
557 user_data->number_of_keys= keys_in_list;
559 /* Retrieve the wlan preferences */
560 wlan_prefs = prefs_find_module("wlan");
562 /* Run the callback on each 802.11 preference */
563 prefs_pref_foreach(wlan_prefs, set_wep_key, (gpointer)user_data);
565 /* Signal that we've changed things, and run the 802.11 dissector's
567 wlan_prefs->prefs_changed = TRUE;
569 /* Apply changes for the specified preference */
570 prefs_apply(wlan_prefs);
573 /* free the WEP key string */
574 for(i=0;i<g_list_length(user_data->list);i++)
576 g_free(g_list_nth(user_data->list,i)->data);
579 /* free the (empty) list */
580 g_list_free(user_data->list);
582 /* free the user_data structure */
585 airpcap_if_info_free(fake_info_if);
591 * Function used to save to the preference file the Decryption Keys.
594 save_wlan_wireshark_wep_keys(GList* key_ls)
596 GList* key_list = NULL;
597 char* tmp_key = NULL;
598 guint keys_in_list,i;
599 keys_cb_data_t* user_data;
600 airpcap_if_info_t* fake_info_if = NULL;
601 decryption_key_t* tmp_dk;
603 /* Retrieve the wlan preferences */
604 wlan_prefs = prefs_find_module("wlan");
606 /* Allocate a structure used to keep infos between the callbacks */
607 user_data = (keys_cb_data_t*)g_malloc(sizeof(keys_cb_data_t));
609 keys_in_list = g_list_length(key_ls);
613 /* Fill the structure */
614 user_data->list = key_list;
615 user_data->current_index = 0;
616 user_data->number_of_keys= keys_in_list;
618 /* Retrieve the wlan preferences */
619 wlan_prefs = prefs_find_module("wlan");
621 /* Run the callback on each 802.11 preference */
622 prefs_pref_foreach(wlan_prefs, set_wep_key, (gpointer)user_data);
624 /* Signal that we've changed things, and run the 802.11 dissector's
626 wlan_prefs->prefs_changed = TRUE;
628 /* Apply changes for the specified preference */
629 prefs_apply(wlan_prefs);
632 /* free the WEP key string */
633 for(i=0;i<g_list_length(user_data->list);i++)
635 tmp_dk = (decryption_key_t*)g_list_nth(user_data->list,i)->data;
636 g_string_free(tmp_dk->key,TRUE);
637 if(tmp_dk->ssid != NULL) g_string_free(tmp_dk->ssid,TRUE);
640 /* free the (empty) list */
641 g_list_free(user_data->list);
643 /* free the user_data structure */
650 * Get an error message string for a CANT_GET_INTERFACE_LIST error from
651 * "get_airpcap_interface_list()".
654 cant_get_airpcap_if_list_error_message(const char *err_str)
656 return g_strdup_printf("Can't get list of Wireless interfaces: %s", err_str);
660 * Airpcap wrapper, used to store the current settings for the selected adapter
663 airpcap_if_store_cur_config_as_adapter_default(PAirpcapHandle ah)
665 if (!AirpcapLoaded) return FALSE;
666 return g_PAirpcapStoreCurConfigAsAdapterDefault(ah);
670 * Airpcap wrapper, used to open an airpcap adapter
673 airpcap_if_open(PCHAR name, PCHAR err)
675 if (!AirpcapLoaded) return NULL;
676 if (name == NULL) return NULL;
677 return g_PAirpcapOpen(name,err);
681 * Airpcap wrapper, used to close an airpcap adapter
684 airpcap_if_close(PAirpcapHandle handle)
686 if (!AirpcapLoaded) return;
687 g_PAirpcapClose(handle);
691 * Airpcap wrapper, used to turn on the led of an airpcap adapter
694 airpcap_if_turn_led_on(PAirpcapHandle AdapterHandle, UINT LedNumber)
696 if (!AirpcapLoaded) return FALSE;
697 return g_PAirpcapTurnLedOn(AdapterHandle,LedNumber);
701 * Airpcap wrapper, used to turn off the led of an airpcap adapter
704 airpcap_if_turn_led_off(PAirpcapHandle AdapterHandle, UINT LedNumber)
706 if (!AirpcapLoaded) return FALSE;
707 return g_PAirpcapTurnLedOff(AdapterHandle,LedNumber);
711 * Airpcap wrapper, used to get the channel of an airpcap adapter
714 airpcap_if_get_device_channel(PAirpcapHandle ah, PUINT ch)
716 if (!AirpcapLoaded) return FALSE;
717 return g_PAirpcapGetDeviceChannel(ah,ch);
721 * Airpcap wrapper, used to set the channel of an airpcap adapter
724 airpcap_if_set_device_channel(PAirpcapHandle ah, UINT ch)
726 if (!AirpcapLoaded) return FALSE;
727 return g_PAirpcapSetDeviceChannel(ah,ch);
731 * Airpcap wrapper, used to get the link type of an airpcap adapter
734 airpcap_if_get_link_type(PAirpcapHandle ah, PAirpcapLinkType lt)
736 if (!AirpcapLoaded) return FALSE;
737 return g_PAirpcapGetLinkType(ah,lt);
741 * Airpcap wrapper, used to set the link type of an airpcap adapter
744 airpcap_if_set_link_type(PAirpcapHandle ah, AirpcapLinkType lt)
746 if (!AirpcapLoaded) return FALSE;
747 return g_PAirpcapSetLinkType(ah,lt);
751 * Airpcap wrapper, used to get the fcs presence of an airpcap adapter
754 airpcap_if_get_fcs_presence(PAirpcapHandle ah, PBOOL fcs)
756 if (!AirpcapLoaded) return FALSE;
757 return g_PAirpcapGetFcsPresence(ah,fcs);
761 * Airpcap wrapper, used to set the fcs presence of an airpcap adapter
764 airpcap_if_set_fcs_presence(PAirpcapHandle ah, BOOL fcs)
766 if (!AirpcapLoaded) return FALSE;
767 return g_PAirpcapSetFcsPresence(ah,fcs);
771 * Airpcap wrapper, used to get the decryption enabling of an airpcap adapter
774 airpcap_if_get_decryption_state(PAirpcapHandle ah, PAirpcapDecryptionState PEnable)
776 if (!AirpcapLoaded) return FALSE;
777 return g_PAirpcapGetDecryptionState(ah,PEnable);
781 * Airpcap wrapper, used to set the decryption enabling of an airpcap adapter
784 airpcap_if_set_decryption_state(PAirpcapHandle ah, AirpcapDecryptionState Enable)
786 if (!AirpcapLoaded) return FALSE;
787 return g_PAirpcapSetDecryptionState(ah,Enable);
791 * Airpcap wrapper, used to get the decryption enabling of an airpcap driver
794 airpcap_if_get_driver_decryption_state(PAirpcapHandle ah, PAirpcapDecryptionState PEnable)
796 if (!AirpcapLoaded || (g_PAirpcapGetDriverDecryptionState==NULL)) return FALSE;
797 return g_PAirpcapGetDriverDecryptionState(ah,PEnable);
801 * Airpcap wrapper, used to set the decryption enabling of an airpcap driver
804 airpcap_if_set_driver_decryption_state(PAirpcapHandle ah, AirpcapDecryptionState Enable)
806 if (!AirpcapLoaded || (g_PAirpcapSetDriverDecryptionState==NULL)) return FALSE;
807 return g_PAirpcapSetDriverDecryptionState(ah,Enable);
811 * Airpcap wrapper, used to get the fcs validation of an airpcap adapter
814 airpcap_if_get_fcs_validation(PAirpcapHandle ah, PAirpcapValidationType val)
816 if (!AirpcapLoaded) return FALSE;
817 return g_PAirpcapGetFcsValidation(ah,val);
821 * Airpcap wrapper, used to set the fcs validation of an airpcap adapter
824 airpcap_if_set_fcs_validation(PAirpcapHandle ah, AirpcapValidationType val)
826 if (!AirpcapLoaded) return FALSE;
827 return g_PAirpcapSetFcsValidation(ah,val);
831 * Airpcap wrapper, used to save the settings for the selected_if
834 airpcap_if_set_device_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection)
836 if (!AirpcapLoaded) return FALSE;
837 return g_PAirpcapSetDeviceKeys(AdapterHandle,KeysCollection);
841 * Airpcap wrapper, used to save the settings for the selected_if
844 airpcap_if_get_device_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection, PUINT PKeysCollectionSize)
846 if (!AirpcapLoaded) return FALSE;
847 return g_PAirpcapGetDeviceKeys(AdapterHandle,KeysCollection,PKeysCollectionSize);
851 * Airpcap wrapper, used to save the driver's set of keys
854 airpcap_if_set_driver_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection)
856 if (!AirpcapLoaded || (g_PAirpcapSetDriverKeys==NULL)) return FALSE;
857 return g_PAirpcapSetDriverKeys(AdapterHandle,KeysCollection);
861 * Airpcap wrapper, used to load the driver's set of keys
864 airpcap_if_get_driver_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection, PUINT PKeysCollectionSize)
866 if (!AirpcapLoaded || (g_PAirpcapGetDriverKeys==NULL)) return FALSE;
867 return g_PAirpcapGetDriverKeys(AdapterHandle,KeysCollection,PKeysCollectionSize);
871 * This function will create a new airpcap_if_info_t using a name and a description
874 airpcap_if_info_new(char *name, char *description)
877 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
879 airpcap_if_info_t *if_info = NULL;
881 /* Probably I have to switch on the leds!!! */
882 ad = airpcap_if_open(name, ebuf);
885 if_info = g_malloc(sizeof (airpcap_if_info_t));
886 if_info->name = g_strdup(name);
887 if (description == NULL)
888 if_info->description = NULL;
890 if_info->description = g_strdup(description);
891 if_info->ip_addr = NULL;
892 if_info->loopback = FALSE;
893 airpcap_if_get_fcs_validation(ad,&(if_info->CrcValidationOn));
894 airpcap_if_get_fcs_presence(ad,&(if_info->IsFcsPresent));
895 airpcap_if_get_link_type(ad,&(if_info->linkType));
896 airpcap_if_get_device_channel(ad,&(if_info->channel));
897 airpcap_if_turn_led_on(ad, 0);
898 airpcap_if_get_decryption_state(ad, &(if_info->DecryptionOn));
900 if_info->blinking = FALSE;
901 if_info->saved = TRUE; /* NO NEED TO BE SAVED */
903 /* get the keys, if everything is ok, close the adapter */
904 if(airpcap_if_load_keys(ad,if_info))
905 airpcap_if_close(ad);
911 * This function will create a new fake drivers' interface, to load global keys...
914 airpcap_driver_fake_if_info_new()
917 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
919 airpcap_if_info_t *if_info = NULL;
920 airpcap_if_info_t *fake_if_info = NULL;
922 /* Maybe for some reason no airpcap adapter is found */
923 if(airpcap_if_list == NULL)
927 * Retrieve the first AirPcap adapter available. If no interface is found,
928 * it is not possible to retrieve the driver's settings, so return NULL.
930 if_info = g_list_nth_data(airpcap_if_list,0);
934 /* Open the 'fake' adapter */
935 ad = airpcap_if_open(if_info->name, ebuf);
938 fake_if_info = g_malloc(sizeof (airpcap_if_info_t));
939 fake_if_info->name = g_strdup(if_info->name);
940 fake_if_info->description = g_strdup(if_info->description);
941 fake_if_info->loopback = FALSE;
942 fake_if_info->ip_addr = NULL;
943 airpcap_if_get_driver_decryption_state(ad, &(fake_if_info->DecryptionOn));
944 airpcap_if_get_fcs_validation(ad,&(fake_if_info->CrcValidationOn));
945 airpcap_if_get_fcs_presence(ad,&(fake_if_info->IsFcsPresent));
946 airpcap_if_get_link_type(ad,&(fake_if_info->linkType));
947 airpcap_if_get_device_channel(ad,&(fake_if_info->channel));
948 airpcap_if_turn_led_on(ad, 0);
949 fake_if_info->led = TRUE;
950 fake_if_info->blinking = FALSE;
951 fake_if_info->saved = TRUE; /* NO NEED TO BE SAVED */
953 /* get the keys, if everything is ok, close the adapter */
954 if(airpcap_if_load_driver_keys(ad,fake_if_info))
955 airpcap_if_close(ad);
962 * USED FOR DEBUG ONLY... PRINTS AN AirPcap ADAPTER STRUCTURE in a fancy way.
965 airpcap_if_info_print(airpcap_if_info_t* if_info)
969 g_print("\nWARNING : AirPcap Interface pointer is NULL!\n");
973 g_print("\n----------------- AirPcap Interface \n");
974 g_print(" NAME: %s\n",if_info->name);
975 g_print(" DESCRIPTION: %s\n",if_info->description);
976 g_print(" BLINKING: %s\n",if_info->blinking ? "TRUE" : "FALSE");
977 g_print(" CHANNEL: %2u\n",if_info->channel);
978 g_print(" CRCVALIDATION: %s\n",if_info->CrcValidationOn ? "ON" : "OFF");
979 g_print(" DECRYPTION: %s\n",if_info->DecryptionOn ? "ON" : "OFF");
980 g_print(" IP ADDR: %s\n",if_info->ip_addr!=NULL ? "NOT NULL" : "NULL");
981 g_print(" FCSPRESENT: %s\n",if_info->IsFcsPresent ? "TRUE" : "FALSE");
982 g_print(" KEYSCOLLECTION: %s\n",if_info->keysCollection!=NULL ? "NOT NULL" : "NULL");
983 g_print("KEYSCOLLECTIONSIZE: %u\n",if_info->keysCollectionSize);
984 g_print(" LED: %s\n",if_info->led ? "ON" : "OFF");
985 g_print(" LINKTYPE: %d\n",if_info->linkType);
986 g_print(" LOOPBACK: %s\n",if_info->loopback ? "YES" : "NO");
987 g_print(" (GTK) TAG: %d\n",if_info->tag);
992 * Function used to load the WEP keys for a selected interface
995 airpcap_if_load_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
997 if(!if_info) return FALSE;
999 if_info->keysCollectionSize = 0;
1000 if_info->keysCollection = NULL;
1002 if(!airpcap_if_get_device_keys(ad, NULL, &(if_info->keysCollectionSize)))
1004 if(if_info->keysCollectionSize == 0)
1006 if_info->keysCollection = NULL;
1007 airpcap_if_close(ad);
1011 if_info->keysCollection = (PAirpcapKeysCollection)g_malloc(if_info->keysCollectionSize);
1012 if(!if_info->keysCollection)
1014 if_info->keysCollectionSize = 0;
1015 if_info->keysCollection = NULL;
1016 airpcap_if_close(ad);
1020 airpcap_if_get_device_keys(ad, if_info->keysCollection, &(if_info->keysCollectionSize));
1024 airpcap_if_close(ad);
1029 * Function used to load the WEP keys for a selected interface
1032 airpcap_if_load_driver_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
1034 if_info->keysCollectionSize = 0;
1035 if_info->keysCollection = NULL;
1037 if(!airpcap_if_get_driver_keys(ad, NULL, &(if_info->keysCollectionSize)))
1039 if(if_info->keysCollectionSize == 0)
1041 if_info->keysCollection = NULL;
1042 airpcap_if_close(ad);
1046 if_info->keysCollection = (PAirpcapKeysCollection)g_malloc(if_info->keysCollectionSize);
1047 if(!if_info->keysCollection)
1049 if_info->keysCollectionSize = 0;
1050 if_info->keysCollection = NULL;
1051 airpcap_if_close(ad);
1055 airpcap_if_get_driver_keys(ad, if_info->keysCollection, &(if_info->keysCollectionSize));
1059 airpcap_if_close(ad);
1064 * Function used to save the WEP keys for a selected interface
1067 airpcap_if_save_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
1069 if(!if_info || !AirpcapLoaded) return;
1071 if(if_info->keysCollection != NULL)
1072 g_PAirpcapSetDeviceKeys(ad,if_info->keysCollection);
1076 * Function used to save the WEP keys for a selected interface
1079 airpcap_if_save_driver_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
1081 if(if_info->keysCollection != NULL)
1082 airpcap_if_set_driver_keys(ad,if_info->keysCollection);
1086 * Callback used to free an instance of airpcap_if_info_t
1089 free_airpcap_if_cb(gpointer data, gpointer user_data _U_)
1091 airpcap_if_info_t *if_info = data;
1093 if (if_info->name != NULL)
1094 g_free(if_info->name);
1096 if (if_info->description != NULL)
1097 g_free(if_info->description);
1099 /* XXX - FREE THE WEP KEY LIST HERE!!!*/
1100 if(if_info->keysCollection != NULL)
1102 g_free(if_info->keysCollection);
1103 if_info->keysCollection = NULL;
1106 if(if_info->ip_addr != NULL)
1107 g_slist_free(if_info->ip_addr);
1114 * Function used to free the airpcap interface list
1117 free_airpcap_interface_list(GList *if_list)
1119 g_list_foreach(if_list, free_airpcap_if_cb, NULL);
1120 g_list_free(if_list);
1125 * This function will use the airpcap.dll to find all the airpcap devices.
1126 * Will return null if no device is found.
1129 get_airpcap_interface_list(int *err, char *err_str)
1132 airpcap_if_info_t *if_info;
1134 AirpcapDeviceDescription *devsList, *adListEntry;
1137 *err = NO_AIRPCAP_INTERFACES_FOUND;
1139 if(!AirpcapLoaded || !g_PAirpcapGetDeviceList(&devsList, err_str))
1141 /* No interfaces, return il = NULL; */
1146 * Count the adapters
1148 adListEntry = devsList;
1153 adListEntry = adListEntry->next;
1158 /* No interfaces, return il= NULL */
1159 g_PAirpcapFreeDeviceList(devsList);
1164 * Insert the adapters in our list
1166 adListEntry = devsList;
1167 for(i = 0; i < n_adapts; i++)
1169 if_info = airpcap_if_info_new(adListEntry->Name, adListEntry->Description);
1170 il = g_list_append(il, if_info);
1172 adListEntry = adListEntry->next;
1175 g_PAirpcapFreeDeviceList(devsList);
1181 * Used to retrieve the name of the interface given the description
1182 * (the name is used in AirpcapOpen, the description is put in the combo box)
1184 gchar* get_airpcap_name_from_description(GList* if_list, gchar* description)
1188 airpcap_if_info_t* if_info;
1193 while( ifn < g_list_length(if_list) )
1195 curr = g_list_nth(if_list, ifn);
1199 if_info = curr->data;
1201 if ( g_ascii_strcasecmp(if_info->description,description) == 0)
1203 return if_info->name;
1212 * Used to retrieve the interface given the name
1213 * (the name is used in AirpcapOpen)
1215 airpcap_if_info_t* get_airpcap_if_by_name(GList* if_list, const gchar* name)
1219 airpcap_if_info_t* if_info;
1224 while( ifn < g_list_length(if_list) )
1226 curr = g_list_nth(if_list, ifn);
1230 if_info = curr->data;
1232 if ( g_ascii_strcasecmp(if_info->name,name) == 0)
1243 * Returns the ASCII string of a key given the key bytes
1246 airpcap_get_key_string(AirpcapKey key)
1255 if(key.KeyType == AIRPDCAP_KEY_TYPE_WEP)
1259 /* Allocate the string used to store the ASCII representation of the WEP key */
1260 dst = (gchar*)g_malloc(sizeof(gchar)*WEP_KEY_MAX_CHAR_SIZE + 1);
1261 /* Make sure that the first char is '\0' in order to make g_strlcat() work */
1264 for(j = 0; j < key.KeyLen; j++)
1266 src = g_strdup_printf("%.2x\0", key.KeyData[j]);
1268 * XXX - use g_strconcat() or GStrings instead ???
1270 l = g_strlcat(dst,src,WEP_KEY_MAX_CHAR_SIZE+1);
1275 else if(key.KeyType == AIRPDCAP_KEY_TYPE_WPA_PWD)
1277 /* XXX - Add code here */
1279 else if(key.KeyType == AIRPDCAP_KEY_TYPE_WPA_PMK)
1281 /* XXX - Add code here */
1285 /* XXX - Add code here */
1292 * Clear keys and decryption status for the specified interface
1295 airpcap_if_clear_decryption_settings(airpcap_if_info_t* info_if)
1299 if(info_if->keysCollection != NULL)
1301 g_free(info_if->keysCollection);
1302 info_if->keysCollection = NULL;
1305 info_if->keysCollectionSize = 0;
1307 info_if->DecryptionOn = FALSE;
1308 info_if->saved = FALSE;
1313 * Used to retrieve the airpcap_if_info_t of the selected interface given the
1314 * description (that is the entry of the combo box).
1316 gpointer get_airpcap_if_from_description(GList* if_list, const gchar* description)
1320 airpcap_if_info_t* if_info;
1325 while( ifn < g_list_length(if_list) )
1327 curr = g_list_nth(if_list, ifn);
1331 if_info = curr->data;
1333 if ( g_ascii_strcasecmp(if_info->description,description) == 0)
1344 * Used to retrieve the two chars string from interface
1347 airpcap_get_if_string_number(airpcap_if_info_t* if_info)
1353 a = sscanf(if_info->name,AIRPCAP_DEVICE_NUMBER_EXTRACT_STRING,&n);
1355 /* If sscanf() returned 1, it means that has read a number, so interface is not "Any"
1356 * Otherwise, check if it is the "Any" adapter...
1360 if(g_strcasecmp(if_info->name,AIRPCAP_DEVICE_ANY_EXTRACT_STRING)!=0)
1361 number = g_strdup_printf("??");
1363 number = g_strdup_printf(AIRPCAP_CHANNEL_ANY_NAME);
1367 number = g_strdup_printf("%.2u\0",n);
1374 * Used to retrieve the two chars string from interface
1377 airpcap_get_if_string_number_from_description(gchar* description)
1382 number = (gchar*)g_malloc(sizeof(gchar)*3);
1384 pointer = g_strrstr(description,"#\0");
1386 number[0] = *(pointer+1);
1387 number[1] = *(pointer+2);
1394 * Returns the default airpcap interface of a list, NULL if list is empty
1397 airpcap_get_default_if(GList* airpcap_if_list)
1400 GList* popdown_if_list = NULL;
1404 airpcap_if_info_t* if_info = NULL;
1406 if(prefs.capture_device != NULL)
1408 s = g_strdup(get_if_name(prefs.capture_device));
1409 if_info = get_airpcap_if_by_name(airpcap_if_list,g_strdup(get_if_name(prefs.capture_device)));
1416 * Load the configuration for the specified interface
1419 airpcap_load_selected_if_configuration(airpcap_if_info_t* if_info)
1421 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
1426 ad = airpcap_if_open(get_airpcap_name_from_description(airpcap_if_list, if_info->description), ebuf);
1430 /* Stop blinking (if it was blinkig!)*/
1431 if(if_info->blinking)
1433 /* Turn on the light (if it was off) */
1434 if(!(if_info->led)) airpcap_if_turn_led_on(ad, 0);
1437 /* Apply settings... */
1438 airpcap_if_get_device_channel(ad,&(if_info->channel));
1439 airpcap_if_get_fcs_validation(ad,&(if_info->CrcValidationOn));
1440 airpcap_if_get_fcs_presence(ad,&(if_info->IsFcsPresent));
1441 airpcap_if_get_link_type(ad,&(if_info->linkType));
1442 airpcap_if_get_decryption_state(ad, &(if_info->DecryptionOn));
1443 /* get the keys, if everything is ok, close the adapter */
1444 if(airpcap_if_load_keys(ad,if_info))
1445 airpcap_if_close(ad);
1447 if_info->saved = TRUE;
1451 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, " Error in opening adapter for %s",if_info->description);
1457 * Save the configuration for the specified interface
1460 airpcap_save_selected_if_configuration(airpcap_if_info_t* if_info)
1462 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
1467 ad = airpcap_if_open(get_airpcap_name_from_description(airpcap_if_list, if_info->description), ebuf);
1471 /* Stop blinking (if it was blinkig!)*/
1472 if(if_info->blinking)
1474 /* Turn on the light (if it was off) */
1475 if(!(if_info->led)) airpcap_if_turn_led_on(ad, 0);
1478 /* Apply settings... */
1479 airpcap_if_set_device_channel(ad,if_info->channel);
1480 airpcap_if_set_fcs_validation(ad,if_info->CrcValidationOn);
1481 airpcap_if_set_fcs_presence(ad,if_info->IsFcsPresent);
1482 airpcap_if_set_link_type(ad,if_info->linkType);
1483 airpcap_if_set_decryption_state(ad, if_info->DecryptionOn);
1484 airpcap_if_save_keys(ad,if_info);
1486 /* ... and save them */
1487 if(!airpcap_if_store_cur_config_as_adapter_default(ad))
1489 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Cannot save Wireless configuration!!!\nRemember that in order to store the configuration in the registry you have to:\n\n- Close all the airpcap-based applications.\n- Be sure to have administrative privileges.");
1490 if_info->saved = FALSE;
1491 airpcap_if_close(ad);
1495 if_info->saved = TRUE;
1496 airpcap_if_close(ad);
1500 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, " Error in opening adapter for %s",if_info->description);
1506 * Save the configuration for the specified interface
1509 airpcap_save_driver_if_configuration(airpcap_if_info_t* fake_if_info)
1511 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
1514 if(fake_if_info != NULL)
1516 ad = airpcap_if_open(fake_if_info->name, ebuf);
1520 /* Apply decryption settings... */
1521 airpcap_if_set_driver_decryption_state(ad, fake_if_info->DecryptionOn);
1522 airpcap_if_save_driver_keys(ad,fake_if_info);
1523 airpcap_if_close(ad);
1527 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, " Error in opening adapter for %s",fake_if_info->description);
1535 * DECRYPTION KEYS FUNCTIONS
1538 * This function is used for DEBUG POURPOSES ONLY!!!
1541 print_key_list(GList* key_list)
1544 decryption_key_t* tmp;
1546 if(key_list == NULL)
1548 g_print("\n\n******* KEY LIST NULL *******\n\n");
1552 n = g_list_length(key_list);
1554 g_print("\n\n********* KEY LIST **********\n\n");
1556 g_print("NUMBER OF KEYS IN LIST : %d\n\n",n);
1558 for(i =0; i < n; i++)
1560 g_print("[%d] :\n",i+1);
1561 tmp = (decryption_key_t*)(g_list_nth_data(key_list,i));
1562 g_print("KEY : %s\n",tmp->key->str);
1564 g_print("BITS: %d\n",tmp->bits);
1566 if(tmp->type == AIRPDCAP_KEY_TYPE_WEP)
1567 g_print("TYPE: %s\n",AIRPCAP_WEP_KEY_STRING);
1568 else if(tmp->type == AIRPDCAP_KEY_TYPE_WPA_PWD)
1569 g_print("TYPE: %s\n",AIRPCAP_WPA_PWD_KEY_STRING);
1570 else if(tmp->type == AIRPDCAP_KEY_TYPE_WPA_PMK)
1571 g_print("TYPE: %s\n",AIRPCAP_WPA_BIN_KEY_STRING);
1573 g_print("TYPE: %s\n","???");
1575 g_print("SSID: %s\n",(tmp->ssid != NULL) ? tmp->ssid->str : "---");
1579 g_print("\n*****************************\n\n");
1583 * Retrieves a GList of decryption_key_t structures containing infos about the
1584 * keys for the given adapter... returns NULL if no keys are found.
1587 get_airpcap_device_keys(airpcap_if_info_t* info_if)
1590 char* tmp_key = NULL;
1591 guint i,keys_in_list = 0;
1594 decryption_key_t *new_key = NULL;
1595 GList *key_list = NULL;
1597 /* Number of keys in key list */
1598 if(info_if->keysCollectionSize != 0)
1599 keys_in_list = (guint)(info_if->keysCollectionSize - sizeof(AirpcapKeysCollection))/sizeof(AirpcapKey);
1603 for(i=0; i<keys_in_list; i++)
1605 /* Different things to do depending on the key type */
1606 if(info_if->keysCollection->Keys[i].KeyType == AIRPDCAP_KEY_TYPE_WEP)
1608 /* allocate memory for the new key item */
1609 new_key = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
1611 /* fill the fields */
1613 tmp_key = airpcap_get_key_string(info_if->keysCollection->Keys[i]);
1614 new_key->key = g_string_new(tmp_key);
1618 new_key->bits = new_key->key->len *4; /* every char is 4 bits in WEP keys (it is an exadecimal number) */
1620 /* SSID not used in WEP keys */
1621 new_key->ssid = NULL;
1623 /* TYPE (WEP in this case) */
1624 new_key->type = info_if->keysCollection->Keys[i].KeyType;
1626 /* Append the new element in the list */
1627 key_list = g_list_append(key_list,(gpointer)new_key);
1629 else if(info_if->keysCollection->Keys[i].KeyType == AIRPDCAP_KEY_TYPE_WPA_PWD)
1631 /* XXX - Not supported yet */
1633 else if(info_if->keysCollection->Keys[i].KeyType == AIRPDCAP_KEY_TYPE_WPA_PMK)
1635 /* XXX - Not supported yet */
1643 * Retrieves a GList of decryption_key_t structures containing infos about the
1644 * keys for the global AirPcap driver... returns NULL if no keys are found.
1647 get_airpcap_driver_keys()
1650 char* tmp_key = NULL;
1651 guint i,keys_in_list = 0;
1654 decryption_key_t *new_key = NULL;
1655 GList *key_list = NULL;
1658 * To read the drivers general settings we need to create and use one airpcap adapter...
1659 * The only way to do that is to instantiate a fake adapter, and then close it and delete it.
1661 airpcap_if_info_t* fake_info_if = NULL;
1663 /* Create the fake_info_if from the first adapter of the list */
1664 fake_info_if = airpcap_driver_fake_if_info_new();
1666 if(fake_info_if == NULL)
1669 /* Number of keys in key list */
1670 if(fake_info_if->keysCollectionSize != 0)
1671 keys_in_list = (guint)(fake_info_if->keysCollectionSize - sizeof(AirpcapKeysCollection))/sizeof(AirpcapKey);
1675 for(i=0; i<keys_in_list; i++)
1677 /* Different things to do depending on the key type */
1678 if(fake_info_if->keysCollection->Keys[i].KeyType == AIRPDCAP_KEY_TYPE_WEP)
1680 /* allocate memory for the new key item */
1681 new_key = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
1683 /* fill the fields */
1685 tmp_key = airpcap_get_key_string(fake_info_if->keysCollection->Keys[i]);
1686 new_key->key = g_string_new(tmp_key);
1687 if(tmp_key != NULL) g_free(tmp_key);
1690 new_key->bits = new_key->key->len *4; /* every char is 4 bits in WEP keys (it is an exadecimal number) */
1692 /* SSID not used in WEP keys */
1693 new_key->ssid = NULL;
1695 /* TYPE (WEP in this case) */
1696 new_key->type = fake_info_if->keysCollection->Keys[i].KeyType;
1698 /* Append the new element in the list */
1699 key_list = g_list_append(key_list,(gpointer)new_key);
1701 else if(fake_info_if->keysCollection->Keys[i].KeyType == AIRPDCAP_KEY_TYPE_WPA_PWD)
1703 /* XXX - Not supported yet */
1705 else if(fake_info_if->keysCollection->Keys[i].KeyType == AIRPDCAP_KEY_TYPE_WPA_PMK)
1707 /* XXX - Not supported yet */
1711 airpcap_if_info_free(fake_info_if);
1717 * Returns the list of the decryption keys specified for wireshark, NULL if
1721 get_wireshark_keys()
1723 keys_cb_data_t* wep_user_data = NULL;
1727 GList* final_list = NULL;
1728 GList* wep_final_list = NULL;
1730 /* Retrieve the wlan preferences */
1731 wlan_prefs = prefs_find_module("wlan");
1733 /* Allocate a structure used to keep infos between the callbacks */
1734 wep_user_data = (keys_cb_data_t*)g_malloc(sizeof(keys_cb_data_t));
1736 /* Fill the structure */
1737 wep_user_data->list = NULL;
1738 wep_user_data->current_index = 0;
1739 wep_user_data->number_of_keys= 0; /* Still unknown */
1741 /* Run the callback on each 802.11 preference */
1742 /* XXX - Right now, only WEP keys will be loaded */
1743 prefs_pref_foreach(wlan_prefs, get_wep_key, (gpointer)wep_user_data);
1745 /* Copy the list field in the user data structure pointer into the final_list */
1746 if(wep_user_data != NULL) wep_final_list = wep_user_data->list;
1748 /* XXX - Merge the three lists!!!!! */
1749 final_list = wep_final_list;
1751 /* free the wep_user_data structure */
1752 g_free(wep_user_data);
1758 * Merges two lists of keys and return a newly created GList. If a key is
1759 * found multiple times, it will just appear once!
1760 * list1 and list 2 pointer will have to be freed manually if needed!!!
1761 * If the total number of keys exceeeds the maximum number allowed,
1762 * exceeding keys will be discarded...
1765 merge_key_list(GList* list1, GList* list2)
1769 decryption_key_t *dk1=NULL,
1773 GList* merged_list = NULL;
1775 if( (list1 == NULL) && (list2 == NULL) )
1781 n2 = g_list_length(list2);
1785 new_dk = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
1786 dk2 = (decryption_key_t *)g_list_nth_data(list2,i);
1788 new_dk->bits = dk2->bits;
1789 new_dk->type = dk2->type;
1790 new_dk->key = g_string_new(dk2->key->str);
1791 if(dk2->ssid != NULL)
1792 new_dk->ssid = g_string_new(dk2->ssid->str);
1794 new_dk->ssid = NULL;
1796 /* Check the total length of the merged list */
1797 if(g_list_length(merged_list) < MAX_ENCRYPTION_KEYS)
1798 merged_list = g_list_append(merged_list,(gpointer)new_dk);
1801 else if(list2 == NULL)
1803 n1 = g_list_length(list1);
1808 new_dk = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
1809 dk1 = (decryption_key_t*)g_list_nth_data(list1,i);
1811 new_dk->bits = dk1->bits;
1812 new_dk->type = dk1->type;
1813 new_dk->key = g_string_new(dk1->key->str);
1814 if(dk1->ssid != NULL)
1815 new_dk->ssid = g_string_new(dk1->ssid->str);
1817 new_dk->ssid = NULL;
1819 /* Check the total length of the merged list */
1820 if(g_list_length(merged_list) < MAX_ENCRYPTION_KEYS)
1821 merged_list = g_list_append(merged_list,(gpointer)new_dk);
1826 n1 = g_list_length(list1);
1827 n2 = g_list_length(list2);
1829 /* Copy the whole list1 into merged_list */
1832 new_dk = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
1833 dk1 = (decryption_key_t *)g_list_nth_data(list1,i);
1835 new_dk->bits = dk1->bits;
1836 new_dk->type = dk1->type;
1837 new_dk->key = g_string_new(dk1->key->str);
1839 if(dk1->ssid != NULL)
1840 new_dk->ssid = g_string_new(dk1->ssid->str);
1842 new_dk->ssid = NULL;
1844 /* Check the total length of the merged list */
1845 if(g_list_length(merged_list) < MAX_ENCRYPTION_KEYS)
1846 merged_list = g_list_append(merged_list,(gpointer)new_dk);
1849 /* Look for keys that are present in list2 but aren't in list1 yet...
1850 * Add them to merged_list
1854 dk2 = (decryption_key_t *)g_list_nth_data(list2,i);
1856 if(!key_is_in_list(dk2,merged_list))
1858 new_dk = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
1860 new_dk->bits = dk2->bits;
1861 new_dk->type = dk2->type;
1862 new_dk->key = g_string_new(dk2->key->str);
1863 if(dk2->ssid != NULL)
1864 new_dk->ssid = g_string_new(dk2->ssid->str);
1866 new_dk->ssid = NULL;
1868 /* Check the total length of the merged list */
1869 if(g_list_length(merged_list) < MAX_ENCRYPTION_KEYS)
1870 merged_list = g_list_append(merged_list,(gpointer)new_dk);
1879 * Use this function to free a key list.
1882 free_key_list(GList *list)
1885 decryption_key_t *curr_key;
1890 n = g_list_length(list);
1892 for(i = 0; i < n; i++)
1894 curr_key = (decryption_key_t*)g_list_nth_data(list,i);
1896 /* Free all the strings */
1897 if(curr_key->key != NULL)
1898 g_string_free(curr_key->key,TRUE);
1900 if(curr_key->ssid != NULL)
1901 g_string_free(curr_key->ssid,TRUE);
1903 /* free the decryption_key_t structure*/
1916 * If the given key is contained in the list, returns TRUE.
1917 * Returns FALSE otherwise.
1920 key_is_in_list(decryption_key_t *dk,GList *list)
1923 decryption_key_t* curr_key = NULL;
1924 gboolean found = FALSE;
1926 if( (list == NULL) || (dk == NULL) )
1929 n = g_list_length(list);
1934 for(i = 0; i < n; i++)
1936 curr_key = (decryption_key_t*)g_list_nth_data(list,i);
1937 if(keys_are_equals(dk,curr_key))
1945 * Returns TRUE if keys are equals, FALSE otherwise
1948 keys_are_equals(decryption_key_t *k1,decryption_key_t *k2)
1951 if((k1==NULL) || (k2==NULL))
1954 /* XXX - Remove this check when we will have the WPA/WPA2 decryption in the Driver! */
1955 //if( (k1->type == AIRPDCAP_KEY_TYPE_WPA_PWD) || (k2->type == AIRPDCAP_KEY_TYPE_WPA_PWD) || (k1->type == AIRPDCAP_KEY_TYPE_WPA_PMK) || (k2->type == AIRPDCAP_KEY_TYPE_WPA_PMK) )
1958 if( g_string_equal(k1->key,k2->key) &&
1959 (k1->bits == k2->bits) && /* If the previous is TRUE, this must be TRUE as well */
1960 k1->type == k2->type)
1962 /* Check the ssid... if the key type is WEP, the two fields should be NULL */
1963 if((k1->ssid == NULL) && (k2->ssid == NULL))
1966 /* Check if one of them is null and one is not... */
1967 if((k1->ssid == NULL) || (k2->ssid == NULL))
1970 /* If they are not null, they must share the same ssid */
1971 return g_string_equal(k1->ssid,k2->ssid);
1974 /* Some field is not equal ... */
1979 * Tests if two collection of keys are equal or not, to be considered equals, they have to
1980 * contain the same keys in the SAME ORDER! (If both lists are NULL, which means empty will
1984 key_lists_are_equal(GList* list1, GList* list2)
1988 guint wep_n1=0,wep_n2=0;
1989 GList *wep_list1=NULL;
1990 GList *wep_list2=NULL;
1993 decryption_key_t *dk1=NULL,*dk2=NULL;
1995 n1 = g_list_length(list1);
1996 n2 = g_list_length(list2);
1999 * XXX - START : Retrieve the aublists of WEP keys!!! This is needed only 'till Driver WPA decryption
2000 * is not implemented.
2004 dk1=(decryption_key_t*)g_list_nth_data(list1,i);
2005 if(dk1->type == AIRPDCAP_KEY_TYPE_WEP)
2007 wep_list1 = g_list_append(wep_list1,(gpointer)dk1);
2013 dk2=(decryption_key_t*)g_list_nth_data(list2,i);
2014 if(dk2->type == AIRPDCAP_KEY_TYPE_WEP)
2016 wep_list2 = g_list_append(wep_list2,(gpointer)dk2);
2022 * XXX - END : Remove from START to END when the WPA/WPA2 decryption will be implemented in
2027 * Commented, because in the new AirPcap version all the keys will be saved
2028 * into the driver, and all the keys for every specific adapter will be
2029 * removed. This means that this check will always fail... and the user will
2030 * always be asked what to do... and it doesn't make much sense.
2032 /* if(n1 != n2) return FALSE; */
2033 if(wep_n1 != wep_n2) return FALSE;
2040 dk1=(decryption_key_t*)g_list_nth_data(list1,i);
2041 dk2=(decryption_key_t*)g_list_nth_data(list2,i);
2043 if(!g_string_equal(dk1->key,dk2->key)) return FALSE;
2047 dk2=(decryption_key_t*)g_list_nth_data(wep_list2,i);
2048 if(!key_is_in_list(dk2,wep_list1)) return FALSE;
2055 test_if_on(pref_t *pref, gpointer ud _U_)
2060 /* Retrieve user data info */
2061 is_on = (gboolean*)ud;
2064 if (g_strncasecmp(pref->name, "enable_decryption", 17) == 0 && pref->type == PREF_BOOL)
2066 number = *pref->varp.boolp;
2068 if(number) *is_on = TRUE;
2069 else *is_on = FALSE;
2077 * Returns TRUE if the Wireshark decryption is active, false otherwise
2080 wireshark_decryption_on()
2084 /* Retrieve the wlan preferences */
2085 wlan_prefs = prefs_find_module("wlan");
2087 /* Run the callback on each 802.11 preference */
2088 prefs_pref_foreach(wlan_prefs, test_if_on, (gpointer)&is_on);
2094 * Returns TRUE if the AirPcap decryption for the current adapter is active, false otherwise
2097 airpcap_decryption_on()
2099 gboolean is_on = FALSE;
2101 airpcap_if_info_t* fake_if_info = NULL;
2103 fake_if_info = airpcap_driver_fake_if_info_new();
2105 if(fake_if_info != NULL)
2107 if(fake_if_info->DecryptionOn == AIRPCAP_DECRYPTION_ON)
2109 else if(fake_if_info->DecryptionOn == AIRPCAP_DECRYPTION_OFF)
2113 airpcap_if_info_free(fake_if_info);
2119 * Free an instance of airpcap_if_info_t
2122 airpcap_if_info_free(airpcap_if_info_t *if_info)
2126 if (if_info->name != NULL)
2127 g_free(if_info->name);
2129 if (if_info->description != NULL)
2130 g_free(if_info->description);
2132 if(if_info->keysCollection != NULL)
2134 g_free(if_info->keysCollection);
2135 if_info->keysCollection = NULL;
2138 if(if_info->ip_addr != NULL)
2140 g_slist_free(if_info->ip_addr);
2141 if_info->ip_addr = NULL;
2153 set_on_off(pref_t *pref, gpointer ud _U_)
2158 /* Retrieve user data info */
2159 is_on = (gboolean*)ud;
2161 if (g_strncasecmp(pref->name, "enable_decryption", 17) == 0 && pref->type == PREF_BOOL)
2163 number = *pref->varp.boolp;
2165 g_free((void *)*pref->varp.boolp);
2167 *pref->varp.boolp = TRUE;
2169 *pref->varp.boolp = FALSE;
2177 * Enables decryption for Wireshark if on_off is TRUE, disables it otherwise.
2180 set_wireshark_decryption(gboolean on_off)
2186 /* Retrieve the wlan preferences */
2187 wlan_prefs = prefs_find_module("wlan");
2189 /* Run the callback on each 802.11 preference */
2190 prefs_pref_foreach(wlan_prefs, set_on_off, (gpointer)&is_on);
2193 * Signal that we've changed things, and run the 802.11 dissector's
2196 wlan_prefs->prefs_changed = TRUE;
2198 prefs_apply(wlan_prefs);
2202 * Enables decryption for all the adapters if on_off is TRUE, disables it otherwise.
2205 set_airpcap_decryption(gboolean on_off)
2207 /* We need to directly access the .dll functions here... */
2208 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
2209 PAirpcapHandle ad,ad_driver;
2211 gboolean success = TRUE;
2215 airpcap_if_info_t* curr_if = NULL;
2216 airpcap_if_info_t* fake_if_info = NULL;
2218 fake_if_info = airpcap_driver_fake_if_info_new();
2220 if(fake_if_info == NULL)
2221 /* We apparently don't have any adapters installed.
2222 * This isn't a failure, so return TRUE
2226 /* Set the driver decryption */
2227 ad_driver = airpcap_if_open(fake_if_info->name, ebuf);
2231 airpcap_if_set_driver_decryption_state(ad_driver,AIRPCAP_DECRYPTION_ON);
2233 airpcap_if_set_driver_decryption_state(ad_driver,AIRPCAP_DECRYPTION_OFF);
2235 airpcap_if_close(ad_driver);
2238 airpcap_if_info_free(fake_if_info);
2240 n = g_list_length(airpcap_if_list);
2242 /* Set to FALSE the decryption for all the adapters */
2243 /* Apply this change to all the adapters !!! */
2244 for(i = 0; i < n; i++)
2246 curr_if = (airpcap_if_info_t*)g_list_nth_data(airpcap_if_list,i);
2248 if( curr_if != NULL )
2250 ad = airpcap_if_open(get_airpcap_name_from_description(airpcap_if_list,curr_if->description), ebuf);
2253 curr_if->DecryptionOn = (gboolean)AIRPCAP_DECRYPTION_OFF;
2254 airpcap_if_set_decryption_state(ad,curr_if->DecryptionOn);
2255 /* Save configuration for the curr_if */
2256 if(!airpcap_if_store_cur_config_as_adapter_default(ad))
2260 airpcap_if_close(ad);
2269 /* DYNAMIC LIBRARY LOADER */
2271 * Used to dynamically load the airpcap library in order link it only when
2272 * it's present on the system
2274 int load_airpcap(void)
2276 BOOL base_functions = TRUE;
2277 BOOL new_functions = TRUE;
2279 if((AirpcapLib = LoadLibrary(TEXT("airpcap.dll"))) == NULL)
2281 /* Report the error but go on */
2282 return AIRPCAP_DLL_NOT_FOUND;
2286 if((g_PAirpcapGetLastError = (AirpcapGetLastErrorHandler) GetProcAddress(AirpcapLib, "AirpcapGetLastError")) == NULL) base_functions = FALSE;
2287 if((g_PAirpcapGetDeviceList = (AirpcapGetDeviceListHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceList")) == NULL) base_functions = FALSE;
2288 if((g_PAirpcapFreeDeviceList = (AirpcapFreeDeviceListHandler) GetProcAddress(AirpcapLib, "AirpcapFreeDeviceList")) == NULL) base_functions = FALSE;
2289 if((g_PAirpcapOpen = (AirpcapOpenHandler) GetProcAddress(AirpcapLib, "AirpcapOpen")) == NULL) base_functions = FALSE;
2290 if((g_PAirpcapClose = (AirpcapCloseHandler) GetProcAddress(AirpcapLib, "AirpcapClose")) == NULL) base_functions = FALSE;
2291 if((g_PAirpcapGetLinkType = (AirpcapGetLinkTypeHandler) GetProcAddress(AirpcapLib, "AirpcapGetLinkType")) == NULL) base_functions = FALSE;
2292 if((g_PAirpcapSetLinkType = (AirpcapSetLinkTypeHandler) GetProcAddress(AirpcapLib, "AirpcapSetLinkType")) == NULL) base_functions = FALSE;
2293 if((g_PAirpcapSetKernelBuffer = (AirpcapSetKernelBufferHandler) GetProcAddress(AirpcapLib, "AirpcapSetKernelBuffer")) == NULL) base_functions = FALSE;
2294 if((g_PAirpcapSetFilter = (AirpcapSetFilterHandler) GetProcAddress(AirpcapLib, "AirpcapSetFilter")) == NULL) base_functions = FALSE;
2295 if((g_PAirpcapGetMacAddress = (AirpcapGetMacAddressHandler) GetProcAddress(AirpcapLib, "AirpcapGetMacAddress")) == NULL) base_functions = FALSE;
2296 if((g_PAirpcapSetMinToCopy = (AirpcapSetMinToCopyHandler) GetProcAddress(AirpcapLib, "AirpcapSetMinToCopy")) == NULL) base_functions = FALSE;
2297 if((g_PAirpcapGetReadEvent = (AirpcapGetReadEventHandler) GetProcAddress(AirpcapLib, "AirpcapGetReadEvent")) == NULL) base_functions = FALSE;
2298 if((g_PAirpcapRead = (AirpcapReadHandler) GetProcAddress(AirpcapLib, "AirpcapRead")) == NULL) base_functions = FALSE;
2299 if((g_PAirpcapGetStats = (AirpcapGetStatsHandler) GetProcAddress(AirpcapLib, "AirpcapGetStats")) == NULL) base_functions = FALSE;
2300 if((g_PAirpcapTurnLedOn = (AirpcapTurnLedOnHandler) GetProcAddress(AirpcapLib, "AirpcapTurnLedOn")) == NULL) base_functions = FALSE;
2301 if((g_PAirpcapTurnLedOff = (AirpcapTurnLedOffHandler) GetProcAddress(AirpcapLib, "AirpcapTurnLedOff")) == NULL) base_functions = FALSE;
2302 if((g_PAirpcapGetDeviceChannel = (AirpcapGetDeviceChannelHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceChannel")) == NULL) base_functions = FALSE;
2303 if((g_PAirpcapSetDeviceChannel = (AirpcapSetDeviceChannelHandler) GetProcAddress(AirpcapLib, "AirpcapSetDeviceChannel")) == NULL) base_functions = FALSE;
2304 if((g_PAirpcapGetFcsPresence = (AirpcapGetFcsPresenceHandler) GetProcAddress(AirpcapLib, "AirpcapGetFcsPresence")) == NULL) base_functions = FALSE;
2305 if((g_PAirpcapSetFcsPresence = (AirpcapSetFcsPresenceHandler) GetProcAddress(AirpcapLib, "AirpcapSetFcsPresence")) == NULL) base_functions = FALSE;
2306 if((g_PAirpcapGetFcsValidation = (AirpcapGetFcsValidationHandler) GetProcAddress(AirpcapLib, "AirpcapGetFcsValidation")) == NULL) base_functions = FALSE;
2307 if((g_PAirpcapSetFcsValidation = (AirpcapSetFcsValidationHandler) GetProcAddress(AirpcapLib, "AirpcapSetFcsValidation")) == NULL) base_functions = FALSE;
2308 if((g_PAirpcapGetDeviceKeys = (AirpcapGetDeviceKeysHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceKeys")) == NULL) base_functions = FALSE;
2309 if((g_PAirpcapSetDeviceKeys = (AirpcapSetDeviceKeysHandler) GetProcAddress(AirpcapLib, "AirpcapSetDeviceKeys")) == NULL) base_functions = FALSE;
2310 if((g_PAirpcapGetDecryptionState = (AirpcapGetDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapGetDecryptionState")) == NULL) base_functions = FALSE;
2311 if((g_PAirpcapSetDecryptionState = (AirpcapSetDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapSetDecryptionState")) == NULL) base_functions = FALSE;
2312 if((g_PAirpcapStoreCurConfigAsAdapterDefault = (AirpcapStoreCurConfigAsAdapterDefaultHandler) GetProcAddress(AirpcapLib, "AirpcapStoreCurConfigAsAdapterDefault")) == NULL) base_functions = FALSE;
2313 if((g_PAirpcapGetVersion = (AirpcapGetVersionHandler) GetProcAddress(AirpcapLib, "AirpcapGetVersion")) == NULL) base_functions = FALSE;
2315 /* TEST IF WE CAN FIND AIRPCAP NEW DRIVER FEATURES */
2316 if((g_PAirpcapGetDriverDecryptionState = (AirpcapGetDriverDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapGetDriverDecryptionState")) == NULL) new_functions = FALSE;
2317 if((g_PAirpcapSetDriverDecryptionState = (AirpcapSetDriverDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapSetDriverDecryptionState")) == NULL) new_functions = FALSE;
2318 if((g_PAirpcapGetDriverKeys = (AirpcapGetDriverKeysHandler) GetProcAddress(AirpcapLib, "AirpcapGetDriverKeys")) == NULL) new_functions = FALSE;
2319 if((g_PAirpcapSetDriverKeys = (AirpcapSetDriverKeysHandler) GetProcAddress(AirpcapLib, "AirpcapSetDriverKeys")) == NULL) new_functions = FALSE;
2325 AirpcapLoaded = TRUE;
2326 return AIRPCAP_DLL_OK;
2330 AirpcapLoaded = TRUE;
2331 return AIRPCAP_DLL_OLD;
2336 AirpcapLoaded = FALSE;
2337 return AIRPCAP_DLL_ERROR;
2343 * Append the version of AirPcap with which we were compiled to a GString.
2346 get_compiled_airpcap_version(GString *str)
2348 g_string_append(str, "with AirPcap");
2352 * Append the version of AirPcap with which we we're running to a GString.
2355 get_runtime_airpcap_version(GString *str)
2357 guint vmaj, vmin, vrev, build;
2359 /* See if the DLL has been loaded successfully. Bail if it hasn't */
2360 if (AirpcapLoaded == FALSE) {
2361 g_string_append(str, "without AirPcap");
2365 g_PAirpcapGetVersion(&vmaj, &vmin, &vrev, &build);
2366 g_string_sprintfa(str, "with AirPcap %d.%d.%d build %d", vmaj, vmin,
2371 * Returns the decryption_key_t struct given a string describing the key.
2372 * Returns NULL if the key_string cannot be parsed.
2375 parse_key_string(gchar* input_string)
2381 GString *key_string,
2388 decryption_key_t *dk;
2390 if(input_string == NULL)
2394 * Parse the input_string. It should be in the form <key type>:<key data>[:<ssid>]
2395 * XXX - For backward compatibility, the a WEP key can be just a string of hexadecimal
2396 * characters (if WEP key is wrong, null will be returned...).
2398 tokens = g_strsplit(input_string,":",0);
2400 /* Tokens is a null termiated array of strings ... */
2401 while(tokens[n] != NULL)
2406 /* Free the array of strings */
2412 * 'n' contains the number of tokens. If the key string is correct, we should have
2413 * 2 or 3 tokens... If we have 1 token, it can be an 'old style' WEP key... check for it...
2417 /* Maybe it is an 'old style' WEP key */
2418 key = g_strdup(tokens[0]);
2420 /* Create a new string */
2421 key_string = g_string_new(key);
2423 /* Check if it is a correct WEP key */
2424 if( ((key_string->len) > WEP_KEY_MAX_CHAR_SIZE) || ((key_string->len) < WEP_KEY_MIN_CHAR_SIZE))
2426 g_string_free(key_string, TRUE);
2428 /* Free the array of strings */
2433 if((key_string->len % 2) != 0)
2435 g_string_free(key_string, TRUE);
2437 /* Free the array of strings */
2442 for(i = 0; i < key_string->len; i++)
2444 if(!g_ascii_isxdigit(key_string->str[i]))
2446 g_string_free(key_string, TRUE);
2448 /* Free the array of strings */
2454 /* Key is correct! It was probably an 'old style' WEP key */
2455 /* Create the decryption_key_t structure, fill it and return it*/
2456 dk = g_malloc(sizeof(decryption_key_t));
2458 dk->type = AIRPDCAP_KEY_TYPE_WEP;
2459 dk->key = g_string_new(key);
2460 dk->bits = dk->key->len * 4;
2463 g_string_free(key_string, TRUE);
2466 /* Free the array of strings */
2472 /* There were at least 2 tokens... copy the type value */
2473 type = g_strdup(tokens[0]);
2476 * The second token is the key (right now it doesn't matter
2477 * if it is a passphrase or an hexadecimal one)
2479 key = g_strdup(tokens[1]);
2485 /* Maybe there is a third token (an ssid, if everything else is ok) */
2488 ssid = g_strdup(tokens[2]);
2497 * Now the initial key string has been divided in two/three tokens... let's see
2498 * which kind of key it is, and if it is the correct form
2500 if(g_strcasecmp(type,STRING_KEY_TYPE_WEP) == 0) /* WEP key */
2502 /* Create a new string */
2503 key_string = g_string_new(key);
2505 /* Check if it is a correct WEP key */
2506 if( ((key_string->len) > WEP_KEY_MAX_CHAR_SIZE) || ((key_string->len) < WEP_KEY_MIN_CHAR_SIZE))
2508 g_string_free(key_string, TRUE);
2510 /* Free the array of strings */
2515 if((key_string->len % 2) != 0)
2517 g_string_free(key_string, TRUE);
2519 /* Free the array of strings */
2524 for(i = 0; i < key_string->len; i++)
2526 if(!g_ascii_isxdigit(key_string->str[i]))
2528 g_string_free(key_string, TRUE);
2530 /* Free the array of strings */
2536 dk = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
2538 dk->type = AIRPDCAP_KEY_TYPE_WEP;
2539 dk->key = g_string_new(key);
2540 dk->bits = dk->key->len * 4;
2543 g_string_free(key_string, TRUE);
2546 /* Free the array of strings */
2550 else if(g_strcasecmp(type,STRING_KEY_TYPE_WPA_PSK) == 0) /* WPA key */
2552 /* Create a new string */
2553 key_string = g_string_new(key);
2555 /* Two tokens means that the user should have entered a WPA-BIN key ... */
2556 if( ((key_string->len) != WPA_PSK_KEY_CHAR_SIZE))
2558 g_string_free(key_string, TRUE);
2562 /* No ssid has been created ... */
2563 /* Free the array of strings */
2568 for(i = 0; i < key_string->len; i++)
2570 if(!g_ascii_isxdigit(key_string->str[i]))
2572 g_string_free(key_string, TRUE);
2573 /* No ssid_string has been created ... */
2577 /* No ssid has been created ... */
2578 /* Free the array of strings */
2584 /* Key was correct!!! Create the new decryption_key_t ... */
2585 dk = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
2587 dk->type = AIRPDCAP_KEY_TYPE_WPA_PMK;
2588 dk->key = g_string_new(key);
2589 dk->bits = dk->key->len * 4;
2592 g_string_free(key_string, TRUE);
2596 /* Free the array of strings */
2600 else if(g_strcasecmp(type,STRING_KEY_TYPE_WPA_PWD) == 0) /* WPA key *//* If the number of tokens is more than three, we accept the string... if the first three tokens are correct... */
2602 /* Create a new string */
2603 key_string = g_string_new(key);
2607 /* Three (or more) tokens mean that the user entered a WPA-PWD key ... */
2608 if( ((key_string->len) > WPA_KEY_MAX_CHAR_SIZE) || ((key_string->len) < WPA_KEY_MIN_CHAR_SIZE))
2610 g_string_free(key_string, TRUE);
2616 /* Free the array of strings */
2621 if(ssid != NULL) /* more than three tokens found, means that the user specified the ssid */
2623 ssid_string = g_string_new(ssid);
2626 * XXX - Maybe we need some check on the characters? I'm not sure if only standard ASCII are ok...
2628 if( ((ssid_string->len) > WPA_SSID_MAX_CHAR_SIZE) || ((ssid_string->len) < WPA_SSID_MIN_CHAR_SIZE))
2630 g_string_free(key_string, TRUE);
2631 g_string_free(ssid_string, TRUE);
2637 /* Free the array of strings */
2643 /* Key was correct!!! Create the new decryption_key_t ... */
2644 dk = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
2646 dk->type = AIRPDCAP_KEY_TYPE_WPA_PWD;
2647 dk->key = g_string_new(key);
2648 dk->bits = 256; /* This is the lenght of the array pf bytes that will be generated using key+ssid ...*/
2650 dk->ssid = g_string_new(ssid);
2654 g_string_free(key_string, TRUE);
2655 if(ssid_string != NULL) g_string_free(ssid_string, TRUE);
2659 if(ssid != NULL) g_free(ssid);
2661 /* Free the array of strings */
2666 /* Something was wrong ... free everything */
2670 if(ssid != NULL) g_free(ssid); /* It is not always present */
2671 /* Free the array of strings */
2678 * Returns a newly allocated string representing the given decryption_key_t struct, or NULL if
2679 * something is wrong...
2682 get_key_string(decryption_key_t* dk)
2684 gchar* output_string = NULL;
2689 #ifdef HAVE_AIRPDCAP
2690 if(dk->type == AIRPDCAP_KEY_TYPE_WEP)
2692 if(dk->key == NULL) /* Should NOT happen at all... */
2695 output_string = g_strdup_printf("%s:%s",STRING_KEY_TYPE_WEP,dk->key->str);
2697 else if(dk->type == AIRPDCAP_KEY_TYPE_WPA_PWD)
2699 if(dk->key == NULL) /* Should NOT happen at all... */
2702 if(dk->ssid == NULL)
2703 output_string = g_strdup_printf("%s:%s",STRING_KEY_TYPE_WPA_PWD,dk->key->str);
2705 output_string = g_strdup_printf("%s:%s:%s",STRING_KEY_TYPE_WPA_PWD,dk->key->str,dk->ssid->str);
2707 else if(dk->type == AIRPDCAP_KEY_TYPE_WPA_PMK)
2709 if(dk->key == NULL) /* Should NOT happen at all... */
2712 output_string = g_strdup_printf("%s:%s",STRING_KEY_TYPE_WPA_PSK,dk->key->str);
2718 #else /* not HAVE_AIRPDCAP*/
2719 output_string = g_strdup(dk->key->str);
2722 return output_string;