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 <epan/crypt/airpdcap_ws.h>
47 #include "capture_ui_utils.h"
49 #include "simple_dialog.h"
52 #include "airpcap_loader.h"
55 * We load dinamically the dag library in order link it only when
56 * it's present on the system
58 static HMODULE AirpcapLib = NULL;
61 * Set to TRUE if the DLL was successfully loaded AND all functions
64 static gboolean AirpcapLoaded = FALSE;
66 static AirpcapGetLastErrorHandler g_PAirpcapGetLastError;
67 static AirpcapGetDeviceListHandler g_PAirpcapGetDeviceList;
68 static AirpcapFreeDeviceListHandler g_PAirpcapFreeDeviceList;
69 static AirpcapOpenHandler g_PAirpcapOpen;
70 static AirpcapCloseHandler g_PAirpcapClose;
71 static AirpcapGetLinkTypeHandler g_PAirpcapGetLinkType;
72 static AirpcapSetLinkTypeHandler g_PAirpcapSetLinkType;
73 static AirpcapSetKernelBufferHandler g_PAirpcapSetKernelBuffer;
74 static AirpcapSetFilterHandler g_PAirpcapSetFilter;
75 static AirpcapGetMacAddressHandler g_PAirpcapGetMacAddress;
76 static AirpcapSetMinToCopyHandler g_PAirpcapSetMinToCopy;
77 static AirpcapGetReadEventHandler g_PAirpcapGetReadEvent;
78 static AirpcapReadHandler g_PAirpcapRead;
79 static AirpcapGetStatsHandler g_PAirpcapGetStats;
80 static AirpcapTurnLedOnHandler g_PAirpcapTurnLedOn;
81 static AirpcapTurnLedOffHandler g_PAirpcapTurnLedOff;
82 static AirpcapGetDeviceChannelHandler g_PAirpcapGetDeviceChannel;
83 static AirpcapSetDeviceChannelHandler g_PAirpcapSetDeviceChannel;
84 static AirpcapGetFcsPresenceHandler g_PAirpcapGetFcsPresence;
85 static AirpcapSetFcsPresenceHandler g_PAirpcapSetFcsPresence;
86 static AirpcapGetFcsValidationHandler g_PAirpcapGetFcsValidation;
87 static AirpcapSetFcsValidationHandler g_PAirpcapSetFcsValidation;
88 static AirpcapGetDeviceKeysHandler g_PAirpcapGetDeviceKeys;
89 static AirpcapSetDeviceKeysHandler g_PAirpcapSetDeviceKeys;
90 static AirpcapGetDriverKeysHandler g_PAirpcapGetDriverKeys;
91 static AirpcapSetDriverKeysHandler g_PAirpcapSetDriverKeys;
92 static AirpcapGetDecryptionStateHandler g_PAirpcapGetDecryptionState;
93 static AirpcapSetDecryptionStateHandler g_PAirpcapSetDecryptionState;
94 static AirpcapGetDriverDecryptionStateHandler g_PAirpcapGetDriverDecryptionState;
95 static AirpcapSetDriverDecryptionStateHandler g_PAirpcapSetDriverDecryptionState;
96 static AirpcapStoreCurConfigAsAdapterDefaultHandler g_PAirpcapStoreCurConfigAsAdapterDefault;
97 static AirpcapGetVersionHandler g_PAirpcapGetVersion;
99 /* Airpcap interface list */
100 GList *airpcap_if_list = NULL;
102 /* Airpcap current selected interface */
103 airpcap_if_info_t *airpcap_if_selected = NULL;
105 /* Airpcap current active interface */
106 airpcap_if_info_t *airpcap_if_active = NULL;
108 /* WLAN preferences pointer */
109 module_t *wlan_prefs = NULL;
112 * Callback used by the load_wlan_keys() routine in order to read a WEP decryption key
115 get_wep_key(pref_t *pref, gpointer ud _U_)
117 gchar *my_string = NULL;
118 keys_cb_data_t* user_data;
120 decryption_key_t* new_key;
122 /* Retrieve user data info */
123 user_data = (keys_cb_data_t*)ud;
125 if (g_strncasecmp(pref->name, "wep_key", 7) == 0 && pref->type == PREF_STRING)
127 my_string = g_strdup(*pref->varp.string);
129 /* Here we have the string describing the key... */
130 new_key = parse_key_string(my_string);
134 /* Key is added only if not null ... */
135 user_data->list = g_list_append(user_data->list,new_key);
136 user_data->number_of_keys++;
137 user_data->current_index++;
143 /* Returs TRUE if the WEP key is valid, false otherwise */
145 wep_key_is_valid(char* key)
147 GString *new_key_string;
153 new_key_string = g_string_new(key);
155 if( ((new_key_string->len) > WEP_KEY_MAX_CHAR_SIZE) || ((new_key_string->len) < 2))
157 g_string_free(new_key_string,FALSE);
160 if((new_key_string->len % 2) != 0)
162 g_string_free(new_key_string,FALSE);
165 for(i = 0; i < new_key_string->len; i++)
167 if(!g_ascii_isxdigit(new_key_string->str[i]))
169 g_string_free(new_key_string,FALSE);
174 g_string_free(new_key_string,FALSE);
178 /* Callback used by the save_wlan_keys() routine in order to write a decryption key */
180 set_wep_key(pref_t *pref, gpointer ud _U_)
182 gchar *my_string = NULL;
183 keys_cb_data_t* user_data;
184 gint wep_key_number = 0;
186 decryption_key_t* new_key;
188 /* Retrieve user data info */
189 user_data = (keys_cb_data_t*)ud;
191 if (g_strncasecmp(pref->name, "wep_key", 7) == 0 && pref->type == PREF_STRING)
193 /* Ok, the pref we're gonna set is a wep_key ... but what number? */
194 sscanf(pref->name,"wep_key%d",&wep_key_number);
196 if(user_data->current_index < user_data->number_of_keys)
198 if(wep_key_number == (user_data->current_index+1))
200 /* Retrieve the nth decryption_key_t structure pointer */
201 new_key = (decryption_key_t*)g_list_nth_data(user_data->list,user_data->current_index);
203 /* Free the old key string */
204 g_free((void *)*pref->varp.string);
206 /* Create the new string describing the decryption key */
207 my_string = get_key_string(new_key);
209 /* Duplicate the string, and assign it to the variable pointer */
210 *pref->varp.string = (void *)g_strdup(my_string);
212 /* Free the previously allocated string */
216 else /* If the number of keys has been reduced somehow, we need to delete all the other keys
217 * (remember that the new ones have been probably overwritten)
220 g_free((void *)*pref->varp.string);
221 *pref->varp.string = (void *)g_strdup(""); /* Do not just free memory!!! Put an 'empty' string! */
223 user_data->current_index++;
230 * Function used to read the Decryption Keys from the preferences and store them
231 * properly into the airpcap adapter.
234 load_wlan_driver_wep_keys()
236 keys_cb_data_t* user_data;
240 /* Retrieve the wlan preferences */
241 wlan_prefs = prefs_find_module("wlan");
243 /* Allocate a structure used to keep infos between the callbacks */
244 user_data = (keys_cb_data_t*)g_malloc(sizeof(keys_cb_data_t));
246 /* Fill the structure */
247 user_data->list = NULL;
248 user_data->current_index = 0;
249 user_data->number_of_keys= 0; /* Still unknown */
251 /* Run the callback on each 802.11 preference */
252 prefs_pref_foreach(wlan_prefs, get_wep_key, (gpointer)user_data);
254 /* Now the key list should be filled */
257 * Signal that we've changed things, and run the 802.11 dissector's
260 wlan_prefs->prefs_changed = TRUE;
262 prefs_apply(wlan_prefs);
264 write_wlan_driver_wep_keys_to_regitry(user_data->list);
267 /* free the WEP key string */
268 for(i=0;i<g_list_length(user_data->list);i++)
270 g_free(g_list_nth(user_data->list,i)->data);
273 /* free the (empty) list */
274 g_list_free(user_data->list);
276 /* free the user_data structure */
279 /* airpcap_if_info_free(fake_info_if); */
285 * This function will tell the airpcap driver the key list to use
286 * This will be stored into the registry...
289 write_wlan_wep_keys_to_regitry(airpcap_if_info_t* info_if, GList* key_list)
294 PAirpcapKeysCollection KeysCollection;
295 ULONG KeysCollectionSize;
297 UINT keys_in_list = 0;
298 decryption_key_t* key_item = NULL;
300 keys_in_list = g_list_length(key_list);
303 * Save the encryption keys, if we have any of them
305 KeysCollectionSize = 0;
308 * Calculate the size of the keys collection
310 KeysCollectionSize = sizeof(AirpcapKeysCollection) + keys_in_list * sizeof(AirpcapKey);
313 * Allocate the collection
315 KeysCollection = (PAirpcapKeysCollection)g_malloc(KeysCollectionSize);
322 * Populate the key collection
324 KeysCollection->nKeys = keys_in_list;
326 for(i = 0; i < keys_in_list; i++)
328 KeysCollection->Keys[i].KeyType = AIRPDCAP_KEY_TYPE_WEP;
330 /* Retrieve the Item corresponding to the i-th key */
331 key_item = (decryption_key_t*)g_list_nth_data(key_list,i);
332 new_key = g_string_new(key_item->key->str);
334 KeysCollection->Keys[i].KeyLen = new_key->len / 2;
335 memset(&KeysCollection->Keys[i].KeyData, 0, sizeof(KeysCollection->Keys[i].KeyData));
337 for(j = 0 ; j < new_key->len; j += 2)
339 s[0] = new_key->str[j];
340 s[1] = new_key->str[j+1];
342 KeyByte = (UCHAR)strtol(s, NULL, 16);
343 KeysCollection->Keys[i].KeyData[j / 2] = KeyByte;
346 g_string_free(new_key,TRUE);
350 * Free the old adapter key collection!
352 if(info_if->keysCollection != NULL)
353 g_free(info_if->keysCollection);
356 * Set this collection ad the new one
358 info_if->keysCollection = KeysCollection;
359 info_if->keysCollectionSize = KeysCollectionSize;
362 * Configuration must be saved
364 info_if->saved = FALSE;
367 * Write down the changes to the registry
369 airpcap_save_selected_if_configuration(info_if);
375 * This function will tell the airpcap driver the key list to use
376 * This will be stored into the registry...
379 write_wlan_driver_wep_keys_to_regitry(GList* key_list)
384 PAirpcapKeysCollection KeysCollection;
385 ULONG KeysCollectionSize;
387 UINT keys_in_list = 0;
388 decryption_key_t* key_item = NULL;
389 airpcap_if_info_t* fake_info_if = NULL;
391 /* Create the fake_info_if from the first adapter of the list */
392 fake_info_if = airpcap_driver_fake_if_info_new();
394 if(fake_info_if == NULL)
398 * XXX - When WPA will be supported, change this to: keys_in_list = g_list_length(key_list);
399 * but right now we will have to count only the WEP keys (or we will have a malloc-mess :-) )
401 n = g_list_length(key_list);
402 for(k = 0; k < n; k++ )
403 if(((decryption_key_t*)g_list_nth_data(key_list,k))->type == AIRPDCAP_KEY_TYPE_WEP)
407 * Save the encryption keys, if we have any of them
409 KeysCollectionSize = 0;
412 * Calculate the size of the keys collection
414 KeysCollectionSize = sizeof(AirpcapKeysCollection) + keys_in_list * sizeof(AirpcapKey);
417 * Allocate the collection
419 KeysCollection = (PAirpcapKeysCollection)g_malloc(KeysCollectionSize);
426 * Populate the key collection
428 KeysCollection->nKeys = keys_in_list;
431 * XXX - If we have, let's say, six keys, the first three are WEP, then two are WPA, and the
432 * last is WEP, we have to scroll the whole list (n) but increment the array counter only
433 * when a WEP key is found (y) .. When WPA will be supported by the driver, I'll have to change
436 y = 0; /* Current position in the key list */
438 for(i = 0; i < n; i++)
440 /* Retrieve the Item corresponding to the i-th key */
441 key_item = (decryption_key_t*)g_list_nth_data(key_list,i);
444 * XXX - The AIRPDCAP_KEY_TYPE_WEP is the only supportd right now!
445 * We will have to modify the AirpcapKey structure in order to
446 * support the other two types! What happens now, is that simply the
447 * not supported keys will just be discarded (they will be saved in wireshark though)
449 if(key_item->type == AIRPDCAP_KEY_TYPE_WEP)
451 KeysCollection->Keys[y].KeyType = AIRPDCAP_KEY_TYPE_WEP;
453 new_key = g_string_new(key_item->key->str);
455 KeysCollection->Keys[y].KeyLen = new_key->len / 2;
456 memset(&KeysCollection->Keys[y].KeyData, 0, sizeof(KeysCollection->Keys[y].KeyData));
458 for(j = 0 ; j < new_key->len; j += 2)
460 s[0] = new_key->str[j];
461 s[1] = new_key->str[j+1];
463 KeyByte = (UCHAR)strtol(s, NULL, 16);
464 KeysCollection->Keys[y].KeyData[j / 2] = KeyByte;
466 /* XXX - Change when WPA will be supported!!! */
468 g_string_free(new_key,TRUE);
470 else if(key_item->type == AIRPDCAP_KEY_TYPE_WPA_PWD)
472 /* XXX - The driver cannot deal with this kind of key yet... */
474 else if(key_item->type == AIRPDCAP_KEY_TYPE_WPA_PMK)
476 /* XXX - The driver cannot deal with this kind of key yet... */
481 * Free the old adapter key collection!
483 if(fake_info_if->keysCollection != NULL)
484 g_free(fake_info_if->keysCollection);
487 * Set this collection ad the new one
489 fake_info_if->keysCollection = KeysCollection;
490 fake_info_if->keysCollectionSize = KeysCollectionSize;
493 * Configuration must be saved
495 fake_info_if->saved = FALSE;
498 * Write down the changes to the registry
500 airpcap_save_driver_if_configuration(fake_info_if);
502 airpcap_if_info_free(fake_info_if);
508 * Function used to save to the preference file the Decryption Keys.
511 save_wlan_driver_wep_keys()
513 GList* key_list = NULL;
514 char* tmp_key = NULL;
515 guint keys_in_list,i;
516 keys_cb_data_t* user_data;
517 airpcap_if_info_t* fake_info_if = NULL;
519 /* Create the fake_info_if from the first adapter of the list */
520 fake_info_if = airpcap_driver_fake_if_info_new();
522 if(fake_info_if == NULL)
525 /* Retrieve the wlan preferences */
526 wlan_prefs = prefs_find_module("wlan");
528 /* Allocate a structure used to keep infos between the callbacks */
529 user_data = (keys_cb_data_t*)g_malloc(sizeof(keys_cb_data_t));
531 /* Number of keys in key list */
532 /* Number of keys in key list */
533 if(fake_info_if->keysCollectionSize != 0)
534 keys_in_list = (guint)(fake_info_if->keysCollectionSize - sizeof(AirpcapKeysCollection))/sizeof(AirpcapKey);
538 for(i=0; i<keys_in_list; i++)
540 /* Only if it is a WEP key... */
541 if(fake_info_if->keysCollection->Keys[i].KeyType == AIRPDCAP_KEY_TYPE_WEP)
543 tmp_key = airpcap_get_key_string(fake_info_if->keysCollection->Keys[i]);
544 key_list = g_list_append(key_list,g_strdup(tmp_key));
549 /* Now we know the exact number of WEP keys in the list, so store it ... */
550 keys_in_list = g_list_length(key_list);
552 /* Fill the structure */
553 user_data->list = key_list;
554 user_data->current_index = 0;
555 user_data->number_of_keys= keys_in_list;
557 /* Retrieve the wlan preferences */
558 wlan_prefs = prefs_find_module("wlan");
560 /* Run the callback on each 802.11 preference */
561 prefs_pref_foreach(wlan_prefs, set_wep_key, (gpointer)user_data);
563 /* Signal that we've changed things, and run the 802.11 dissector's
565 wlan_prefs->prefs_changed = TRUE;
567 /* Apply changes for the specified preference */
568 prefs_apply(wlan_prefs);
571 /* free the WEP key string */
572 for(i=0;i<g_list_length(user_data->list);i++)
574 g_free(g_list_nth(user_data->list,i)->data);
577 /* free the (empty) list */
578 g_list_free(user_data->list);
580 /* free the user_data structure */
583 airpcap_if_info_free(fake_info_if);
589 * Function used to save to the preference file the Decryption Keys.
592 save_wlan_wireshark_wep_keys(GList* key_ls)
594 GList* key_list = NULL;
595 char* tmp_key = NULL;
596 guint keys_in_list,i;
597 keys_cb_data_t* user_data;
598 airpcap_if_info_t* fake_info_if = NULL;
599 decryption_key_t* tmp_dk;
601 /* Retrieve the wlan preferences */
602 wlan_prefs = prefs_find_module("wlan");
604 /* Allocate a structure used to keep infos between the callbacks */
605 user_data = (keys_cb_data_t*)g_malloc(sizeof(keys_cb_data_t));
607 keys_in_list = g_list_length(key_ls);
611 /* Fill the structure */
612 user_data->list = key_list;
613 user_data->current_index = 0;
614 user_data->number_of_keys= keys_in_list;
616 /* Retrieve the wlan preferences */
617 wlan_prefs = prefs_find_module("wlan");
619 /* Run the callback on each 802.11 preference */
620 prefs_pref_foreach(wlan_prefs, set_wep_key, (gpointer)user_data);
622 /* Signal that we've changed things, and run the 802.11 dissector's
624 wlan_prefs->prefs_changed = TRUE;
626 /* Apply changes for the specified preference */
627 prefs_apply(wlan_prefs);
630 /* free the WEP key string */
631 for(i=0;i<g_list_length(user_data->list);i++)
633 tmp_dk = (decryption_key_t*)g_list_nth(user_data->list,i)->data;
634 g_string_free(tmp_dk->key,TRUE);
635 if(tmp_dk->ssid != NULL) g_string_free(tmp_dk->ssid,TRUE);
638 /* free the (empty) list */
639 g_list_free(user_data->list);
641 /* free the user_data structure */
648 * Get an error message string for a CANT_GET_INTERFACE_LIST error from
649 * "get_airpcap_interface_list()".
652 cant_get_airpcap_if_list_error_message(const char *err_str)
654 return g_strdup_printf("Can't get list of Wireless interfaces: %s", err_str);
658 * Airpcap wrapper, used to store the current settings for the selected adapter
661 airpcap_if_store_cur_config_as_adapter_default(PAirpcapHandle ah)
663 if (!AirpcapLoaded) return FALSE;
664 return g_PAirpcapStoreCurConfigAsAdapterDefault(ah);
668 * Airpcap wrapper, used to open an airpcap adapter
671 airpcap_if_open(PCHAR name, PCHAR err)
673 if (!AirpcapLoaded) return NULL;
674 if (name == NULL) return NULL;
675 return g_PAirpcapOpen(name,err);
679 * Airpcap wrapper, used to close an airpcap adapter
682 airpcap_if_close(PAirpcapHandle handle)
684 if (!AirpcapLoaded) return;
685 g_PAirpcapClose(handle);
689 * Airpcap wrapper, used to turn on the led of an airpcap adapter
692 airpcap_if_turn_led_on(PAirpcapHandle AdapterHandle, UINT LedNumber)
694 if (!AirpcapLoaded) return FALSE;
695 return g_PAirpcapTurnLedOn(AdapterHandle,LedNumber);
699 * Airpcap wrapper, used to turn off the led of an airpcap adapter
702 airpcap_if_turn_led_off(PAirpcapHandle AdapterHandle, UINT LedNumber)
704 if (!AirpcapLoaded) return FALSE;
705 return g_PAirpcapTurnLedOff(AdapterHandle,LedNumber);
709 * Airpcap wrapper, used to get the channel of an airpcap adapter
712 airpcap_if_get_device_channel(PAirpcapHandle ah, PUINT ch)
714 if (!AirpcapLoaded) return FALSE;
715 return g_PAirpcapGetDeviceChannel(ah,ch);
719 * Airpcap wrapper, used to set the channel of an airpcap adapter
722 airpcap_if_set_device_channel(PAirpcapHandle ah, UINT ch)
724 if (!AirpcapLoaded) return FALSE;
725 return g_PAirpcapSetDeviceChannel(ah,ch);
729 * Airpcap wrapper, used to get the link type of an airpcap adapter
732 airpcap_if_get_link_type(PAirpcapHandle ah, PAirpcapLinkType lt)
734 if (!AirpcapLoaded) return FALSE;
735 return g_PAirpcapGetLinkType(ah,lt);
739 * Airpcap wrapper, used to set the link type of an airpcap adapter
742 airpcap_if_set_link_type(PAirpcapHandle ah, AirpcapLinkType lt)
744 if (!AirpcapLoaded) return FALSE;
745 return g_PAirpcapSetLinkType(ah,lt);
749 * Airpcap wrapper, used to get the fcs presence of an airpcap adapter
752 airpcap_if_get_fcs_presence(PAirpcapHandle ah, PBOOL fcs)
754 if (!AirpcapLoaded) return FALSE;
755 return g_PAirpcapGetFcsPresence(ah,fcs);
759 * Airpcap wrapper, used to set the fcs presence of an airpcap adapter
762 airpcap_if_set_fcs_presence(PAirpcapHandle ah, BOOL fcs)
764 if (!AirpcapLoaded) return FALSE;
765 return g_PAirpcapSetFcsPresence(ah,fcs);
769 * Airpcap wrapper, used to get the decryption enabling of an airpcap adapter
772 airpcap_if_get_decryption_state(PAirpcapHandle ah, PAirpcapDecryptionState PEnable)
774 if (!AirpcapLoaded) return FALSE;
775 return g_PAirpcapGetDecryptionState(ah,PEnable);
779 * Airpcap wrapper, used to set the decryption enabling of an airpcap adapter
782 airpcap_if_set_decryption_state(PAirpcapHandle ah, AirpcapDecryptionState Enable)
784 if (!AirpcapLoaded) return FALSE;
785 return g_PAirpcapSetDecryptionState(ah,Enable);
789 * Airpcap wrapper, used to get the decryption enabling of an airpcap driver
792 airpcap_if_get_driver_decryption_state(PAirpcapHandle ah, PAirpcapDecryptionState PEnable)
794 if (!AirpcapLoaded || (g_PAirpcapGetDriverDecryptionState==NULL)) return FALSE;
795 return g_PAirpcapGetDriverDecryptionState(ah,PEnable);
799 * Airpcap wrapper, used to set the decryption enabling of an airpcap driver
802 airpcap_if_set_driver_decryption_state(PAirpcapHandle ah, AirpcapDecryptionState Enable)
804 if (!AirpcapLoaded || (g_PAirpcapSetDriverDecryptionState==NULL)) return FALSE;
805 return g_PAirpcapSetDriverDecryptionState(ah,Enable);
809 * Airpcap wrapper, used to get the fcs validation of an airpcap adapter
812 airpcap_if_get_fcs_validation(PAirpcapHandle ah, PAirpcapValidationType val)
814 if (!AirpcapLoaded) return FALSE;
815 return g_PAirpcapGetFcsValidation(ah,val);
819 * Airpcap wrapper, used to set the fcs validation of an airpcap adapter
822 airpcap_if_set_fcs_validation(PAirpcapHandle ah, AirpcapValidationType val)
824 if (!AirpcapLoaded) return FALSE;
825 return g_PAirpcapSetFcsValidation(ah,val);
829 * Airpcap wrapper, used to save the settings for the selected_if
832 airpcap_if_set_device_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection)
834 if (!AirpcapLoaded) return FALSE;
835 return g_PAirpcapSetDeviceKeys(AdapterHandle,KeysCollection);
839 * Airpcap wrapper, used to save the settings for the selected_if
842 airpcap_if_get_device_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection, PUINT PKeysCollectionSize)
844 if (!AirpcapLoaded) return FALSE;
845 return g_PAirpcapGetDeviceKeys(AdapterHandle,KeysCollection,PKeysCollectionSize);
849 * Airpcap wrapper, used to save the driver's set of keys
852 airpcap_if_set_driver_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection)
854 if (!AirpcapLoaded || (g_PAirpcapSetDriverKeys==NULL)) return FALSE;
855 return g_PAirpcapSetDriverKeys(AdapterHandle,KeysCollection);
859 * Airpcap wrapper, used to load the driver's set of keys
862 airpcap_if_get_driver_keys(PAirpcapHandle AdapterHandle, PAirpcapKeysCollection KeysCollection, PUINT PKeysCollectionSize)
864 if (!AirpcapLoaded || (g_PAirpcapGetDriverKeys==NULL)) return FALSE;
865 return g_PAirpcapGetDriverKeys(AdapterHandle,KeysCollection,PKeysCollectionSize);
869 * This function will create a new airpcap_if_info_t using a name and a description
872 airpcap_if_info_new(char *name, char *description)
875 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
877 airpcap_if_info_t *if_info = NULL;
879 /* Probably I have to switch on the leds!!! */
880 ad = airpcap_if_open(name, ebuf);
883 if_info = g_malloc(sizeof (airpcap_if_info_t));
884 if_info->name = g_strdup(name);
885 if (description == NULL)
886 if_info->description = NULL;
888 if_info->description = g_strdup(description);
889 if_info->ip_addr = NULL;
890 if_info->loopback = FALSE;
891 airpcap_if_get_fcs_validation(ad,&(if_info->CrcValidationOn));
892 airpcap_if_get_fcs_presence(ad,&(if_info->IsFcsPresent));
893 airpcap_if_get_link_type(ad,&(if_info->linkType));
894 airpcap_if_get_device_channel(ad,&(if_info->channel));
895 airpcap_if_turn_led_on(ad, 0);
896 airpcap_if_get_decryption_state(ad, &(if_info->DecryptionOn));
898 if_info->blinking = FALSE;
899 if_info->saved = TRUE; /* NO NEED TO BE SAVED */
901 /* get the keys, if everything is ok, close the adapter */
902 if(airpcap_if_load_keys(ad,if_info))
903 airpcap_if_close(ad);
909 * This function will create a new fake drivers' interface, to load global keys...
912 airpcap_driver_fake_if_info_new()
915 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
917 airpcap_if_info_t *if_info = NULL;
918 airpcap_if_info_t *fake_if_info = NULL;
920 /* Maybe for some reason no airpcap adapter is found */
921 if(airpcap_if_list == NULL)
925 * Retrieve the first AirPcap adapter available. If no interface is found,
926 * it is not possible to retrieve the driver's settings, so return NULL.
928 if_info = g_list_nth_data(airpcap_if_list,0);
932 /* Open the 'fake' adapter */
933 ad = airpcap_if_open(if_info->name, ebuf);
936 fake_if_info = g_malloc(sizeof (airpcap_if_info_t));
937 fake_if_info->name = g_strdup(if_info->name);
938 fake_if_info->description = g_strdup(if_info->description);
939 fake_if_info->loopback = FALSE;
940 fake_if_info->ip_addr = NULL;
941 airpcap_if_get_driver_decryption_state(ad, &(fake_if_info->DecryptionOn));
942 airpcap_if_get_fcs_validation(ad,&(fake_if_info->CrcValidationOn));
943 airpcap_if_get_fcs_presence(ad,&(fake_if_info->IsFcsPresent));
944 airpcap_if_get_link_type(ad,&(fake_if_info->linkType));
945 airpcap_if_get_device_channel(ad,&(fake_if_info->channel));
946 airpcap_if_turn_led_on(ad, 0);
947 fake_if_info->led = TRUE;
948 fake_if_info->blinking = FALSE;
949 fake_if_info->saved = TRUE; /* NO NEED TO BE SAVED */
951 /* get the keys, if everything is ok, close the adapter */
952 if(airpcap_if_load_driver_keys(ad,fake_if_info))
953 airpcap_if_close(ad);
960 * USED FOR DEBUG ONLY... PRINTS AN AirPcap ADAPTER STRUCTURE in a fancy way.
963 airpcap_if_info_print(airpcap_if_info_t* if_info)
967 g_print("\nWARNING : AirPcap Interface pointer is NULL!\n");
971 g_print("\n----------------- AirPcap Interface \n");
972 g_print(" NAME: %s\n",if_info->name);
973 g_print(" DESCRIPTION: %s\n",if_info->description);
974 g_print(" BLINKING: %s\n",if_info->blinking ? "TRUE" : "FALSE");
975 g_print(" CHANNEL: %2u\n",if_info->channel);
976 g_print(" CRCVALIDATION: %s\n",if_info->CrcValidationOn ? "ON" : "OFF");
977 g_print(" DECRYPTION: %s\n",if_info->DecryptionOn ? "ON" : "OFF");
978 g_print(" IP ADDR: %s\n",if_info->ip_addr!=NULL ? "NOT NULL" : "NULL");
979 g_print(" FCSPRESENT: %s\n",if_info->IsFcsPresent ? "TRUE" : "FALSE");
980 g_print(" KEYSCOLLECTION: %s\n",if_info->keysCollection!=NULL ? "NOT NULL" : "NULL");
981 g_print("KEYSCOLLECTIONSIZE: %u\n",if_info->keysCollectionSize);
982 g_print(" LED: %s\n",if_info->led ? "ON" : "OFF");
983 g_print(" LINKTYPE: %d\n",if_info->linkType);
984 g_print(" LOOPBACK: %s\n",if_info->loopback ? "YES" : "NO");
985 g_print(" (GTK) TAG: %d\n",if_info->tag);
990 * Function used to load the WEP keys for a selected interface
993 airpcap_if_load_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
995 if(!if_info) return FALSE;
997 if_info->keysCollectionSize = 0;
998 if_info->keysCollection = NULL;
1000 if(!airpcap_if_get_device_keys(ad, NULL, &(if_info->keysCollectionSize)))
1002 if(if_info->keysCollectionSize == 0)
1004 if_info->keysCollection = NULL;
1005 airpcap_if_close(ad);
1009 if_info->keysCollection = (PAirpcapKeysCollection)g_malloc(if_info->keysCollectionSize);
1010 if(!if_info->keysCollection)
1012 if_info->keysCollectionSize = 0;
1013 if_info->keysCollection = NULL;
1014 airpcap_if_close(ad);
1018 airpcap_if_get_device_keys(ad, if_info->keysCollection, &(if_info->keysCollectionSize));
1022 airpcap_if_close(ad);
1027 * Function used to load the WEP keys for a selected interface
1030 airpcap_if_load_driver_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
1032 if_info->keysCollectionSize = 0;
1033 if_info->keysCollection = NULL;
1035 if(!airpcap_if_get_driver_keys(ad, NULL, &(if_info->keysCollectionSize)))
1037 if(if_info->keysCollectionSize == 0)
1039 if_info->keysCollection = NULL;
1040 airpcap_if_close(ad);
1044 if_info->keysCollection = (PAirpcapKeysCollection)g_malloc(if_info->keysCollectionSize);
1045 if(!if_info->keysCollection)
1047 if_info->keysCollectionSize = 0;
1048 if_info->keysCollection = NULL;
1049 airpcap_if_close(ad);
1053 airpcap_if_get_driver_keys(ad, if_info->keysCollection, &(if_info->keysCollectionSize));
1057 airpcap_if_close(ad);
1062 * Function used to save the WEP keys for a selected interface
1065 airpcap_if_save_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
1067 if(!if_info || !AirpcapLoaded) return;
1069 if(if_info->keysCollection != NULL)
1070 g_PAirpcapSetDeviceKeys(ad,if_info->keysCollection);
1074 * Function used to save the WEP keys for a selected interface
1077 airpcap_if_save_driver_keys(PAirpcapHandle ad, airpcap_if_info_t *if_info)
1079 if(if_info->keysCollection != NULL)
1080 airpcap_if_set_driver_keys(ad,if_info->keysCollection);
1084 * Callback used to free an instance of airpcap_if_info_t
1087 free_airpcap_if_cb(gpointer data, gpointer user_data _U_)
1089 airpcap_if_info_t *if_info = data;
1091 if (if_info->name != NULL)
1092 g_free(if_info->name);
1094 if (if_info->description != NULL)
1095 g_free(if_info->description);
1097 /* XXX - FREE THE WEP KEY LIST HERE!!!*/
1098 if(if_info->keysCollection != NULL)
1100 g_free(if_info->keysCollection);
1101 if_info->keysCollection = NULL;
1104 if(if_info->ip_addr != NULL)
1105 g_slist_free(if_info->ip_addr);
1112 * Function used to free the airpcap interface list
1115 free_airpcap_interface_list(GList *if_list)
1117 g_list_foreach(if_list, free_airpcap_if_cb, NULL);
1118 g_list_free(if_list);
1123 * This function will use the airpcap.dll to find all the airpcap devices.
1124 * Will return null if no device is found.
1127 get_airpcap_interface_list(int *err, char *err_str)
1130 airpcap_if_info_t *if_info;
1132 AirpcapDeviceDescription *devsList, *adListEntry;
1135 *err = NO_AIRPCAP_INTERFACES_FOUND;
1137 if(!AirpcapLoaded || !g_PAirpcapGetDeviceList(&devsList, err_str))
1139 /* No interfaces, return il = NULL; */
1144 * Count the adapters
1146 adListEntry = devsList;
1151 adListEntry = adListEntry->next;
1156 /* No interfaces, return il= NULL */
1157 g_PAirpcapFreeDeviceList(devsList);
1162 * Insert the adapters in our list
1164 adListEntry = devsList;
1165 for(i = 0; i < n_adapts; i++)
1167 if_info = airpcap_if_info_new(adListEntry->Name, adListEntry->Description);
1168 il = g_list_append(il, if_info);
1170 adListEntry = adListEntry->next;
1173 g_PAirpcapFreeDeviceList(devsList);
1179 * Used to retrieve the name of the interface given the description
1180 * (the name is used in AirpcapOpen, the description is put in the combo box)
1182 gchar* get_airpcap_name_from_description(GList* if_list, gchar* description)
1186 airpcap_if_info_t* if_info;
1191 while( ifn < g_list_length(if_list) )
1193 curr = g_list_nth(if_list, ifn);
1197 if_info = curr->data;
1199 if ( g_ascii_strcasecmp(if_info->description,description) == 0)
1201 return if_info->name;
1210 * Used to retrieve the interface given the name
1211 * (the name is used in AirpcapOpen)
1213 airpcap_if_info_t* get_airpcap_if_by_name(GList* if_list, const gchar* name)
1217 airpcap_if_info_t* if_info;
1222 while( ifn < g_list_length(if_list) )
1224 curr = g_list_nth(if_list, ifn);
1228 if_info = curr->data;
1230 if ( g_ascii_strcasecmp(if_info->name,name) == 0)
1241 * Returns the ASCII string of a key given the key bytes
1244 airpcap_get_key_string(AirpcapKey key)
1253 if(key.KeyType == AIRPDCAP_KEY_TYPE_WEP)
1257 /* Allocate the string used to store the ASCII representation of the WEP key */
1258 dst = (gchar*)g_malloc(sizeof(gchar)*WEP_KEY_MAX_CHAR_SIZE + 1);
1259 /* Make sure that the first char is '\0' in order to make g_strlcat() work */
1262 for(j = 0; j < key.KeyLen; j++)
1264 src = g_strdup_printf("%.2x\0", key.KeyData[j]);
1266 * XXX - use g_strconcat() or GStrings instead ???
1268 l = g_strlcat(dst,src,WEP_KEY_MAX_CHAR_SIZE+1);
1273 else if(key.KeyType == AIRPDCAP_KEY_TYPE_WPA_PWD)
1275 /* XXX - Add code here */
1277 else if(key.KeyType == AIRPDCAP_KEY_TYPE_WPA_PMK)
1279 /* XXX - Add code here */
1283 /* XXX - Add code here */
1290 * Clear keys and decryption status for the specified interface
1293 airpcap_if_clear_decryption_settings(airpcap_if_info_t* info_if)
1297 if(info_if->keysCollection != NULL)
1299 g_free(info_if->keysCollection);
1300 info_if->keysCollection = NULL;
1303 info_if->keysCollectionSize = 0;
1305 info_if->DecryptionOn = FALSE;
1306 info_if->saved = FALSE;
1311 * Used to retrieve the airpcap_if_info_t of the selected interface given the
1312 * description (that is the entry of the combo box).
1314 gpointer get_airpcap_if_from_description(GList* if_list, const gchar* description)
1318 airpcap_if_info_t* if_info;
1323 while( ifn < g_list_length(if_list) )
1325 curr = g_list_nth(if_list, ifn);
1329 if_info = curr->data;
1331 if ( g_ascii_strcasecmp(if_info->description,description) == 0)
1342 * Used to retrieve the two chars string from interface
1345 airpcap_get_if_string_number(airpcap_if_info_t* if_info)
1351 a = sscanf(if_info->name,AIRPCAP_DEVICE_NUMBER_EXTRACT_STRING,&n);
1353 /* If sscanf() returned 1, it means that has read a number, so interface is not "Any"
1354 * Otherwise, check if it is the "Any" adapter...
1358 if(g_strcasecmp(if_info->name,AIRPCAP_DEVICE_ANY_EXTRACT_STRING)!=0)
1359 number = g_strdup_printf("??");
1361 number = g_strdup_printf(AIRPCAP_CHANNEL_ANY_NAME);
1365 number = g_strdup_printf("%.2u\0",n);
1372 * Used to retrieve the two chars string from interface
1375 airpcap_get_if_string_number_from_description(gchar* description)
1380 number = (gchar*)g_malloc(sizeof(gchar)*3);
1382 pointer = g_strrstr(description,"#\0");
1384 number[0] = *(pointer+1);
1385 number[1] = *(pointer+2);
1392 * Returns the default airpcap interface of a list, NULL if list is empty
1395 airpcap_get_default_if(GList* airpcap_if_list)
1398 GList* popdown_if_list = NULL;
1402 airpcap_if_info_t* if_info = NULL;
1404 if(prefs.capture_device != NULL)
1406 s = g_strdup(get_if_name(prefs.capture_device));
1407 if_info = get_airpcap_if_by_name(airpcap_if_list,g_strdup(get_if_name(prefs.capture_device)));
1414 * Load the configuration for the specified interface
1417 airpcap_load_selected_if_configuration(airpcap_if_info_t* if_info)
1419 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
1424 ad = airpcap_if_open(get_airpcap_name_from_description(airpcap_if_list, if_info->description), ebuf);
1428 /* Stop blinking (if it was blinkig!)*/
1429 if(if_info->blinking)
1431 /* Turn on the light (if it was off) */
1432 if(!(if_info->led)) airpcap_if_turn_led_on(ad, 0);
1435 /* Apply settings... */
1436 airpcap_if_get_device_channel(ad,&(if_info->channel));
1437 airpcap_if_get_fcs_validation(ad,&(if_info->CrcValidationOn));
1438 airpcap_if_get_fcs_presence(ad,&(if_info->IsFcsPresent));
1439 airpcap_if_get_link_type(ad,&(if_info->linkType));
1440 airpcap_if_get_decryption_state(ad, &(if_info->DecryptionOn));
1441 /* get the keys, if everything is ok, close the adapter */
1442 if(airpcap_if_load_keys(ad,if_info))
1443 airpcap_if_close(ad);
1445 if_info->saved = TRUE;
1449 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, " Error in opening adapter for %s",if_info->description);
1455 * Save the configuration for the specified interface
1458 airpcap_save_selected_if_configuration(airpcap_if_info_t* if_info)
1460 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
1465 ad = airpcap_if_open(get_airpcap_name_from_description(airpcap_if_list, if_info->description), ebuf);
1469 /* Stop blinking (if it was blinkig!)*/
1470 if(if_info->blinking)
1472 /* Turn on the light (if it was off) */
1473 if(!(if_info->led)) airpcap_if_turn_led_on(ad, 0);
1476 /* Apply settings... */
1477 airpcap_if_set_device_channel(ad,if_info->channel);
1478 airpcap_if_set_fcs_validation(ad,if_info->CrcValidationOn);
1479 airpcap_if_set_fcs_presence(ad,if_info->IsFcsPresent);
1480 airpcap_if_set_link_type(ad,if_info->linkType);
1481 airpcap_if_set_decryption_state(ad, if_info->DecryptionOn);
1482 airpcap_if_save_keys(ad,if_info);
1484 /* ... and save them */
1485 if(!airpcap_if_store_cur_config_as_adapter_default(ad))
1487 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.");
1488 if_info->saved = FALSE;
1489 airpcap_if_close(ad);
1493 if_info->saved = TRUE;
1494 airpcap_if_close(ad);
1498 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, " Error in opening adapter for %s",if_info->description);
1504 * Save the configuration for the specified interface
1507 airpcap_save_driver_if_configuration(airpcap_if_info_t* fake_if_info)
1509 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
1512 if(fake_if_info != NULL)
1514 ad = airpcap_if_open(fake_if_info->name, ebuf);
1518 /* Apply decryption settings... */
1519 airpcap_if_set_driver_decryption_state(ad, fake_if_info->DecryptionOn);
1520 airpcap_if_save_driver_keys(ad,fake_if_info);
1521 airpcap_if_close(ad);
1525 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, " Error in opening adapter for %s",fake_if_info->description);
1533 * DECRYPTION KEYS FUNCTIONS
1536 * This function is used for DEBUG POURPOSES ONLY!!!
1539 print_key_list(GList* key_list)
1542 decryption_key_t* tmp;
1544 if(key_list == NULL)
1546 g_print("\n\n******* KEY LIST NULL *******\n\n");
1550 n = g_list_length(key_list);
1552 g_print("\n\n********* KEY LIST **********\n\n");
1554 g_print("NUMBER OF KEYS IN LIST : %d\n\n",n);
1556 for(i =0; i < n; i++)
1558 g_print("[%d] :\n",i+1);
1559 tmp = (decryption_key_t*)(g_list_nth_data(key_list,i));
1560 g_print("KEY : %s\n",tmp->key->str);
1562 g_print("BITS: %d\n",tmp->bits);
1564 if(tmp->type == AIRPDCAP_KEY_TYPE_WEP)
1565 g_print("TYPE: %s\n",AIRPCAP_WEP_KEY_STRING);
1566 else if(tmp->type == AIRPDCAP_KEY_TYPE_WPA_PWD)
1567 g_print("TYPE: %s\n",AIRPCAP_WPA_PWD_KEY_STRING);
1568 else if(tmp->type == AIRPDCAP_KEY_TYPE_WPA_PMK)
1569 g_print("TYPE: %s\n",AIRPCAP_WPA_BIN_KEY_STRING);
1571 g_print("TYPE: %s\n","???");
1573 g_print("SSID: %s\n",(tmp->ssid != NULL) ? tmp->ssid->str : "---");
1577 g_print("\n*****************************\n\n");
1581 * Retrieves a GList of decryption_key_t structures containing infos about the
1582 * keys for the given adapter... returns NULL if no keys are found.
1585 get_airpcap_device_keys(airpcap_if_info_t* info_if)
1588 char* tmp_key = NULL;
1589 guint i,keys_in_list = 0;
1592 decryption_key_t *new_key = NULL;
1593 GList *key_list = NULL;
1595 /* Number of keys in key list */
1596 if(info_if->keysCollectionSize != 0)
1597 keys_in_list = (guint)(info_if->keysCollectionSize - sizeof(AirpcapKeysCollection))/sizeof(AirpcapKey);
1601 for(i=0; i<keys_in_list; i++)
1603 /* Different things to do depending on the key type */
1604 if(info_if->keysCollection->Keys[i].KeyType == AIRPDCAP_KEY_TYPE_WEP)
1606 /* allocate memory for the new key item */
1607 new_key = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
1609 /* fill the fields */
1611 tmp_key = airpcap_get_key_string(info_if->keysCollection->Keys[i]);
1612 new_key->key = g_string_new(tmp_key);
1616 new_key->bits = new_key->key->len *4; /* every char is 4 bits in WEP keys (it is an exadecimal number) */
1618 /* SSID not used in WEP keys */
1619 new_key->ssid = NULL;
1621 /* TYPE (WEP in this case) */
1622 new_key->type = info_if->keysCollection->Keys[i].KeyType;
1624 /* Append the new element in the list */
1625 key_list = g_list_append(key_list,(gpointer)new_key);
1627 else if(info_if->keysCollection->Keys[i].KeyType == AIRPDCAP_KEY_TYPE_WPA_PWD)
1629 /* XXX - Not supported yet */
1631 else if(info_if->keysCollection->Keys[i].KeyType == AIRPDCAP_KEY_TYPE_WPA_PMK)
1633 /* XXX - Not supported yet */
1641 * Retrieves a GList of decryption_key_t structures containing infos about the
1642 * keys for the global AirPcap driver... returns NULL if no keys are found.
1645 get_airpcap_driver_keys()
1648 char* tmp_key = NULL;
1649 guint i,keys_in_list = 0;
1652 decryption_key_t *new_key = NULL;
1653 GList *key_list = NULL;
1656 * To read the drivers general settings we need to create and use one airpcap adapter...
1657 * The only way to do that is to instantiate a fake adapter, and then close it and delete it.
1659 airpcap_if_info_t* fake_info_if = NULL;
1661 /* Create the fake_info_if from the first adapter of the list */
1662 fake_info_if = airpcap_driver_fake_if_info_new();
1664 if(fake_info_if == NULL)
1667 /* Number of keys in key list */
1668 if(fake_info_if->keysCollectionSize != 0)
1669 keys_in_list = (guint)(fake_info_if->keysCollectionSize - sizeof(AirpcapKeysCollection))/sizeof(AirpcapKey);
1673 for(i=0; i<keys_in_list; i++)
1675 /* Different things to do depending on the key type */
1676 if(fake_info_if->keysCollection->Keys[i].KeyType == AIRPDCAP_KEY_TYPE_WEP)
1678 /* allocate memory for the new key item */
1679 new_key = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
1681 /* fill the fields */
1683 tmp_key = airpcap_get_key_string(fake_info_if->keysCollection->Keys[i]);
1684 new_key->key = g_string_new(tmp_key);
1685 if(tmp_key != NULL) g_free(tmp_key);
1688 new_key->bits = new_key->key->len *4; /* every char is 4 bits in WEP keys (it is an exadecimal number) */
1690 /* SSID not used in WEP keys */
1691 new_key->ssid = NULL;
1693 /* TYPE (WEP in this case) */
1694 new_key->type = fake_info_if->keysCollection->Keys[i].KeyType;
1696 /* Append the new element in the list */
1697 key_list = g_list_append(key_list,(gpointer)new_key);
1699 else if(fake_info_if->keysCollection->Keys[i].KeyType == AIRPDCAP_KEY_TYPE_WPA_PWD)
1701 /* XXX - Not supported yet */
1703 else if(fake_info_if->keysCollection->Keys[i].KeyType == AIRPDCAP_KEY_TYPE_WPA_PMK)
1705 /* XXX - Not supported yet */
1709 airpcap_if_info_free(fake_info_if);
1715 * Returns the list of the decryption keys specified for wireshark, NULL if
1719 get_wireshark_keys()
1721 keys_cb_data_t* wep_user_data = NULL;
1725 GList* final_list = NULL;
1726 GList* wep_final_list = NULL;
1728 /* Retrieve the wlan preferences */
1729 wlan_prefs = prefs_find_module("wlan");
1731 /* Allocate a structure used to keep infos between the callbacks */
1732 wep_user_data = (keys_cb_data_t*)g_malloc(sizeof(keys_cb_data_t));
1734 /* Fill the structure */
1735 wep_user_data->list = NULL;
1736 wep_user_data->current_index = 0;
1737 wep_user_data->number_of_keys= 0; /* Still unknown */
1739 /* Run the callback on each 802.11 preference */
1740 /* XXX - Right now, only WEP keys will be loaded */
1741 prefs_pref_foreach(wlan_prefs, get_wep_key, (gpointer)wep_user_data);
1743 /* Copy the list field in the user data structure pointer into the final_list */
1744 if(wep_user_data != NULL) wep_final_list = wep_user_data->list;
1746 /* XXX - Merge the three lists!!!!! */
1747 final_list = wep_final_list;
1749 /* free the wep_user_data structure */
1750 g_free(wep_user_data);
1756 * Merges two lists of keys and return a newly created GList. If a key is
1757 * found multiple times, it will just appear once!
1758 * list1 and list 2 pointer will have to be freed manually if needed!!!
1759 * If the total number of keys exceeeds the maximum number allowed,
1760 * exceeding keys will be discarded...
1763 merge_key_list(GList* list1, GList* list2)
1767 decryption_key_t *dk1=NULL,
1771 GList* merged_list = NULL;
1773 if( (list1 == NULL) && (list2 == NULL) )
1779 n2 = g_list_length(list2);
1783 new_dk = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
1784 dk2 = (decryption_key_t *)g_list_nth_data(list2,i);
1786 new_dk->bits = dk2->bits;
1787 new_dk->type = dk2->type;
1788 new_dk->key = g_string_new(dk2->key->str);
1789 if(dk2->ssid != NULL)
1790 new_dk->ssid = g_string_new(dk2->ssid->str);
1792 new_dk->ssid = NULL;
1794 /* Check the total length of the merged list */
1795 if(g_list_length(merged_list) < MAX_ENCRYPTION_KEYS)
1796 merged_list = g_list_append(merged_list,(gpointer)new_dk);
1799 else if(list2 == NULL)
1801 n1 = g_list_length(list1);
1806 new_dk = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
1807 dk1 = (decryption_key_t*)g_list_nth_data(list1,i);
1809 new_dk->bits = dk1->bits;
1810 new_dk->type = dk1->type;
1811 new_dk->key = g_string_new(dk1->key->str);
1812 if(dk1->ssid != NULL)
1813 new_dk->ssid = g_string_new(dk1->ssid->str);
1815 new_dk->ssid = NULL;
1817 /* Check the total length of the merged list */
1818 if(g_list_length(merged_list) < MAX_ENCRYPTION_KEYS)
1819 merged_list = g_list_append(merged_list,(gpointer)new_dk);
1824 n1 = g_list_length(list1);
1825 n2 = g_list_length(list2);
1827 /* Copy the whole list1 into merged_list */
1830 new_dk = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
1831 dk1 = (decryption_key_t *)g_list_nth_data(list1,i);
1833 new_dk->bits = dk1->bits;
1834 new_dk->type = dk1->type;
1835 new_dk->key = g_string_new(dk1->key->str);
1837 if(dk1->ssid != NULL)
1838 new_dk->ssid = g_string_new(dk1->ssid->str);
1840 new_dk->ssid = NULL;
1842 /* Check the total length of the merged list */
1843 if(g_list_length(merged_list) < MAX_ENCRYPTION_KEYS)
1844 merged_list = g_list_append(merged_list,(gpointer)new_dk);
1847 /* Look for keys that are present in list2 but aren't in list1 yet...
1848 * Add them to merged_list
1852 dk2 = (decryption_key_t *)g_list_nth_data(list2,i);
1854 if(!key_is_in_list(dk2,merged_list))
1856 new_dk = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
1858 new_dk->bits = dk2->bits;
1859 new_dk->type = dk2->type;
1860 new_dk->key = g_string_new(dk2->key->str);
1861 if(dk2->ssid != NULL)
1862 new_dk->ssid = g_string_new(dk2->ssid->str);
1864 new_dk->ssid = NULL;
1866 /* Check the total length of the merged list */
1867 if(g_list_length(merged_list) < MAX_ENCRYPTION_KEYS)
1868 merged_list = g_list_append(merged_list,(gpointer)new_dk);
1877 * Use this function to free a key list.
1880 free_key_list(GList *list)
1883 decryption_key_t *curr_key;
1888 n = g_list_length(list);
1890 for(i = 0; i < n; i++)
1892 curr_key = (decryption_key_t*)g_list_nth_data(list,i);
1894 /* Free all the strings */
1895 if(curr_key->key != NULL)
1896 g_string_free(curr_key->key,TRUE);
1898 if(curr_key->ssid != NULL)
1899 g_string_free(curr_key->ssid,TRUE);
1901 /* free the decryption_key_t structure*/
1914 * If the given key is contained in the list, returns TRUE.
1915 * Returns FALSE otherwise.
1918 key_is_in_list(decryption_key_t *dk,GList *list)
1921 decryption_key_t* curr_key = NULL;
1922 gboolean found = FALSE;
1924 if( (list == NULL) || (dk == NULL) )
1927 n = g_list_length(list);
1932 for(i = 0; i < n; i++)
1934 curr_key = (decryption_key_t*)g_list_nth_data(list,i);
1935 if(keys_are_equals(dk,curr_key))
1943 * Returns TRUE if keys are equals, FALSE otherwise
1946 keys_are_equals(decryption_key_t *k1,decryption_key_t *k2)
1949 if((k1==NULL) || (k2==NULL))
1952 /* XXX - Remove this check when we will have the WPA/WPA2 decryption in the Driver! */
1953 //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) )
1956 if( g_string_equal(k1->key,k2->key) &&
1957 (k1->bits == k2->bits) && /* If the previous is TRUE, this must be TRUE as well */
1958 k1->type == k2->type)
1960 /* Check the ssid... if the key type is WEP, the two fields should be NULL */
1961 if((k1->ssid == NULL) && (k2->ssid == NULL))
1964 /* Check if one of them is null and one is not... */
1965 if((k1->ssid == NULL) || (k2->ssid == NULL))
1968 /* If they are not null, they must share the same ssid */
1969 return g_string_equal(k1->ssid,k2->ssid);
1972 /* Some field is not equal ... */
1977 * Tests if two collection of keys are equal or not, to be considered equals, they have to
1978 * contain the same keys in the SAME ORDER! (If both lists are NULL, which means empty will
1982 key_lists_are_equal(GList* list1, GList* list2)
1986 guint wep_n1=0,wep_n2=0;
1987 GList *wep_list1=NULL;
1988 GList *wep_list2=NULL;
1991 decryption_key_t *dk1=NULL,*dk2=NULL;
1993 n1 = g_list_length(list1);
1994 n2 = g_list_length(list2);
1997 * XXX - START : Retrieve the aublists of WEP keys!!! This is needed only 'till Driver WPA decryption
1998 * is not implemented.
2002 dk1=(decryption_key_t*)g_list_nth_data(list1,i);
2003 if(dk1->type == AIRPDCAP_KEY_TYPE_WEP)
2005 wep_list1 = g_list_append(wep_list1,(gpointer)dk1);
2011 dk2=(decryption_key_t*)g_list_nth_data(list2,i);
2012 if(dk2->type == AIRPDCAP_KEY_TYPE_WEP)
2014 wep_list2 = g_list_append(wep_list2,(gpointer)dk2);
2020 * XXX - END : Remove from START to END when the WPA/WPA2 decryption will be implemented in
2025 * Commented, because in the new AirPcap version all the keys will be saved
2026 * into the driver, and all the keys for every specific adapter will be
2027 * removed. This means that this check will always fail... and the user will
2028 * always be asked what to do... and it doesn't make much sense.
2030 /* if(n1 != n2) return FALSE; */
2031 if(wep_n1 != wep_n2) return FALSE;
2038 dk1=(decryption_key_t*)g_list_nth_data(list1,i);
2039 dk2=(decryption_key_t*)g_list_nth_data(list2,i);
2041 if(!g_string_equal(dk1->key,dk2->key)) return FALSE;
2045 dk2=(decryption_key_t*)g_list_nth_data(wep_list2,i);
2046 if(!key_is_in_list(dk2,wep_list1)) return FALSE;
2053 test_if_on(pref_t *pref, gpointer ud _U_)
2058 /* Retrieve user data info */
2059 is_on = (gboolean*)ud;
2062 if (g_strncasecmp(pref->name, "enable_decryption", 17) == 0 && pref->type == PREF_BOOL)
2064 number = *pref->varp.boolp;
2066 if(number) *is_on = TRUE;
2067 else *is_on = FALSE;
2075 * Returns TRUE if the Wireshark decryption is active, false otherwise
2078 wireshark_decryption_on()
2082 /* Retrieve the wlan preferences */
2083 wlan_prefs = prefs_find_module("wlan");
2085 /* Run the callback on each 802.11 preference */
2086 prefs_pref_foreach(wlan_prefs, test_if_on, (gpointer)&is_on);
2092 * Returns TRUE if the AirPcap decryption for the current adapter is active, false otherwise
2095 airpcap_decryption_on()
2097 gboolean is_on = FALSE;
2099 airpcap_if_info_t* fake_if_info = NULL;
2101 fake_if_info = airpcap_driver_fake_if_info_new();
2103 if(fake_if_info != NULL)
2105 if(fake_if_info->DecryptionOn == AIRPCAP_DECRYPTION_ON)
2107 else if(fake_if_info->DecryptionOn == AIRPCAP_DECRYPTION_OFF)
2111 airpcap_if_info_free(fake_if_info);
2117 * Free an instance of airpcap_if_info_t
2120 airpcap_if_info_free(airpcap_if_info_t *if_info)
2124 if (if_info->name != NULL)
2125 g_free(if_info->name);
2127 if (if_info->description != NULL)
2128 g_free(if_info->description);
2130 if(if_info->keysCollection != NULL)
2132 g_free(if_info->keysCollection);
2133 if_info->keysCollection = NULL;
2136 if(if_info->ip_addr != NULL)
2138 g_slist_free(if_info->ip_addr);
2139 if_info->ip_addr = NULL;
2151 set_on_off(pref_t *pref, gpointer ud _U_)
2156 /* Retrieve user data info */
2157 is_on = (gboolean*)ud;
2159 if (g_strncasecmp(pref->name, "enable_decryption", 17) == 0 && pref->type == PREF_BOOL)
2161 number = *pref->varp.boolp;
2163 g_free((void *)*pref->varp.boolp);
2165 *pref->varp.boolp = TRUE;
2167 *pref->varp.boolp = FALSE;
2175 * Enables decryption for Wireshark if on_off is TRUE, disables it otherwise.
2178 set_wireshark_decryption(gboolean on_off)
2184 /* Retrieve the wlan preferences */
2185 wlan_prefs = prefs_find_module("wlan");
2187 /* Run the callback on each 802.11 preference */
2188 prefs_pref_foreach(wlan_prefs, set_on_off, (gpointer)&is_on);
2191 * Signal that we've changed things, and run the 802.11 dissector's
2194 wlan_prefs->prefs_changed = TRUE;
2196 prefs_apply(wlan_prefs);
2200 * Enables decryption for all the adapters if on_off is TRUE, disables it otherwise.
2203 set_airpcap_decryption(gboolean on_off)
2205 /* We need to directly access the .dll functions here... */
2206 gchar ebuf[AIRPCAP_ERRBUF_SIZE];
2207 PAirpcapHandle ad,ad_driver;
2209 gboolean success = TRUE;
2213 airpcap_if_info_t* curr_if = NULL;
2214 airpcap_if_info_t* fake_if_info = NULL;
2216 fake_if_info = airpcap_driver_fake_if_info_new();
2218 if(fake_if_info == NULL)
2219 /* We apparently don't have any adapters installed.
2220 * This isn't a failure, so return TRUE
2224 /* Set the driver decryption */
2225 ad_driver = airpcap_if_open(fake_if_info->name, ebuf);
2229 airpcap_if_set_driver_decryption_state(ad_driver,AIRPCAP_DECRYPTION_ON);
2231 airpcap_if_set_driver_decryption_state(ad_driver,AIRPCAP_DECRYPTION_OFF);
2233 airpcap_if_close(ad_driver);
2236 airpcap_if_info_free(fake_if_info);
2238 n = g_list_length(airpcap_if_list);
2240 /* Set to FALSE the decryption for all the adapters */
2241 /* Apply this change to all the adapters !!! */
2242 for(i = 0; i < n; i++)
2244 curr_if = (airpcap_if_info_t*)g_list_nth_data(airpcap_if_list,i);
2246 if( curr_if != NULL )
2248 ad = airpcap_if_open(get_airpcap_name_from_description(airpcap_if_list,curr_if->description), ebuf);
2251 curr_if->DecryptionOn = (gboolean)AIRPCAP_DECRYPTION_OFF;
2252 airpcap_if_set_decryption_state(ad,curr_if->DecryptionOn);
2253 /* Save configuration for the curr_if */
2254 if(!airpcap_if_store_cur_config_as_adapter_default(ad))
2258 airpcap_if_close(ad);
2267 /* DYNAMIC LIBRARY LOADER */
2269 * Used to dynamically load the airpcap library in order link it only when
2270 * it's present on the system
2272 int load_airpcap(void)
2274 BOOL base_functions = TRUE;
2275 BOOL new_functions = TRUE;
2277 if((AirpcapLib = LoadLibrary(TEXT("airpcap.dll"))) == NULL)
2279 /* Report the error but go on */
2280 return AIRPCAP_DLL_NOT_FOUND;
2284 if((g_PAirpcapGetLastError = (AirpcapGetLastErrorHandler) GetProcAddress(AirpcapLib, "AirpcapGetLastError")) == NULL) base_functions = FALSE;
2285 if((g_PAirpcapGetDeviceList = (AirpcapGetDeviceListHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceList")) == NULL) base_functions = FALSE;
2286 if((g_PAirpcapFreeDeviceList = (AirpcapFreeDeviceListHandler) GetProcAddress(AirpcapLib, "AirpcapFreeDeviceList")) == NULL) base_functions = FALSE;
2287 if((g_PAirpcapOpen = (AirpcapOpenHandler) GetProcAddress(AirpcapLib, "AirpcapOpen")) == NULL) base_functions = FALSE;
2288 if((g_PAirpcapClose = (AirpcapCloseHandler) GetProcAddress(AirpcapLib, "AirpcapClose")) == NULL) base_functions = FALSE;
2289 if((g_PAirpcapGetLinkType = (AirpcapGetLinkTypeHandler) GetProcAddress(AirpcapLib, "AirpcapGetLinkType")) == NULL) base_functions = FALSE;
2290 if((g_PAirpcapSetLinkType = (AirpcapSetLinkTypeHandler) GetProcAddress(AirpcapLib, "AirpcapSetLinkType")) == NULL) base_functions = FALSE;
2291 if((g_PAirpcapSetKernelBuffer = (AirpcapSetKernelBufferHandler) GetProcAddress(AirpcapLib, "AirpcapSetKernelBuffer")) == NULL) base_functions = FALSE;
2292 if((g_PAirpcapSetFilter = (AirpcapSetFilterHandler) GetProcAddress(AirpcapLib, "AirpcapSetFilter")) == NULL) base_functions = FALSE;
2293 if((g_PAirpcapGetMacAddress = (AirpcapGetMacAddressHandler) GetProcAddress(AirpcapLib, "AirpcapGetMacAddress")) == NULL) base_functions = FALSE;
2294 if((g_PAirpcapSetMinToCopy = (AirpcapSetMinToCopyHandler) GetProcAddress(AirpcapLib, "AirpcapSetMinToCopy")) == NULL) base_functions = FALSE;
2295 if((g_PAirpcapGetReadEvent = (AirpcapGetReadEventHandler) GetProcAddress(AirpcapLib, "AirpcapGetReadEvent")) == NULL) base_functions = FALSE;
2296 if((g_PAirpcapRead = (AirpcapReadHandler) GetProcAddress(AirpcapLib, "AirpcapRead")) == NULL) base_functions = FALSE;
2297 if((g_PAirpcapGetStats = (AirpcapGetStatsHandler) GetProcAddress(AirpcapLib, "AirpcapGetStats")) == NULL) base_functions = FALSE;
2298 if((g_PAirpcapTurnLedOn = (AirpcapTurnLedOnHandler) GetProcAddress(AirpcapLib, "AirpcapTurnLedOn")) == NULL) base_functions = FALSE;
2299 if((g_PAirpcapTurnLedOff = (AirpcapTurnLedOffHandler) GetProcAddress(AirpcapLib, "AirpcapTurnLedOff")) == NULL) base_functions = FALSE;
2300 if((g_PAirpcapGetDeviceChannel = (AirpcapGetDeviceChannelHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceChannel")) == NULL) base_functions = FALSE;
2301 if((g_PAirpcapSetDeviceChannel = (AirpcapSetDeviceChannelHandler) GetProcAddress(AirpcapLib, "AirpcapSetDeviceChannel")) == NULL) base_functions = FALSE;
2302 if((g_PAirpcapGetFcsPresence = (AirpcapGetFcsPresenceHandler) GetProcAddress(AirpcapLib, "AirpcapGetFcsPresence")) == NULL) base_functions = FALSE;
2303 if((g_PAirpcapSetFcsPresence = (AirpcapSetFcsPresenceHandler) GetProcAddress(AirpcapLib, "AirpcapSetFcsPresence")) == NULL) base_functions = FALSE;
2304 if((g_PAirpcapGetFcsValidation = (AirpcapGetFcsValidationHandler) GetProcAddress(AirpcapLib, "AirpcapGetFcsValidation")) == NULL) base_functions = FALSE;
2305 if((g_PAirpcapSetFcsValidation = (AirpcapSetFcsValidationHandler) GetProcAddress(AirpcapLib, "AirpcapSetFcsValidation")) == NULL) base_functions = FALSE;
2306 if((g_PAirpcapGetDeviceKeys = (AirpcapGetDeviceKeysHandler) GetProcAddress(AirpcapLib, "AirpcapGetDeviceKeys")) == NULL) base_functions = FALSE;
2307 if((g_PAirpcapSetDeviceKeys = (AirpcapSetDeviceKeysHandler) GetProcAddress(AirpcapLib, "AirpcapSetDeviceKeys")) == NULL) base_functions = FALSE;
2308 if((g_PAirpcapGetDecryptionState = (AirpcapGetDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapGetDecryptionState")) == NULL) base_functions = FALSE;
2309 if((g_PAirpcapSetDecryptionState = (AirpcapSetDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapSetDecryptionState")) == NULL) base_functions = FALSE;
2310 if((g_PAirpcapStoreCurConfigAsAdapterDefault = (AirpcapStoreCurConfigAsAdapterDefaultHandler) GetProcAddress(AirpcapLib, "AirpcapStoreCurConfigAsAdapterDefault")) == NULL) base_functions = FALSE;
2311 if((g_PAirpcapGetVersion = (AirpcapGetVersionHandler) GetProcAddress(AirpcapLib, "AirpcapGetVersion")) == NULL) base_functions = FALSE;
2313 /* TEST IF WE CAN FIND AIRPCAP NEW DRIVER FEATURES */
2314 if((g_PAirpcapGetDriverDecryptionState = (AirpcapGetDriverDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapGetDriverDecryptionState")) == NULL) new_functions = FALSE;
2315 if((g_PAirpcapSetDriverDecryptionState = (AirpcapSetDriverDecryptionStateHandler) GetProcAddress(AirpcapLib, "AirpcapSetDriverDecryptionState")) == NULL) new_functions = FALSE;
2316 if((g_PAirpcapGetDriverKeys = (AirpcapGetDriverKeysHandler) GetProcAddress(AirpcapLib, "AirpcapGetDriverKeys")) == NULL) new_functions = FALSE;
2317 if((g_PAirpcapSetDriverKeys = (AirpcapSetDriverKeysHandler) GetProcAddress(AirpcapLib, "AirpcapSetDriverKeys")) == NULL) new_functions = FALSE;
2323 AirpcapLoaded = TRUE;
2324 return AIRPCAP_DLL_OK;
2328 AirpcapLoaded = TRUE;
2329 return AIRPCAP_DLL_OLD;
2334 AirpcapLoaded = FALSE;
2335 return AIRPCAP_DLL_ERROR;
2341 * Append the version of AirPcap with which we were compiled to a GString.
2344 get_compiled_airpcap_version(GString *str)
2346 g_string_append(str, "with AirPcap");
2350 * Append the version of AirPcap with which we we're running to a GString.
2353 get_runtime_airpcap_version(GString *str)
2355 guint vmaj, vmin, vrev, build;
2357 /* See if the DLL has been loaded successfully. Bail if it hasn't */
2358 if (AirpcapLoaded == FALSE) {
2359 g_string_append(str, "without AirPcap");
2363 g_PAirpcapGetVersion(&vmaj, &vmin, &vrev, &build);
2364 g_string_sprintfa(str, "with AirPcap %d.%d.%d build %d", vmaj, vmin,
2369 * Returns the decryption_key_t struct given a string describing the key.
2370 * Returns NULL if the key_string cannot be parsed.
2373 parse_key_string(gchar* input_string)
2379 GString *key_string,
2386 decryption_key_t *dk;
2388 if(input_string == NULL)
2392 * Parse the input_string. It should be in the form <key type>:<key data>[:<ssid>]
2393 * XXX - For backward compatibility, the a WEP key can be just a string of hexadecimal
2394 * characters (if WEP key is wrong, null will be returned...).
2396 tokens = g_strsplit(input_string,":",0);
2398 /* Tokens is a null termiated array of strings ... */
2399 while(tokens[n] != NULL)
2404 /* Free the array of strings */
2410 * 'n' contains the number of tokens. If the key string is correct, we should have
2411 * 2 or 3 tokens... If we have 1 token, it can be an 'old style' WEP key... check for it...
2415 /* Maybe it is an 'old style' WEP key */
2416 key = g_strdup(tokens[0]);
2418 /* Create a new string */
2419 key_string = g_string_new(key);
2421 /* Check if it is a correct WEP key */
2422 if( ((key_string->len) > WEP_KEY_MAX_CHAR_SIZE) || ((key_string->len) < WEP_KEY_MIN_CHAR_SIZE))
2424 g_string_free(key_string, TRUE);
2426 /* Free the array of strings */
2431 if((key_string->len % 2) != 0)
2433 g_string_free(key_string, TRUE);
2435 /* Free the array of strings */
2440 for(i = 0; i < key_string->len; i++)
2442 if(!g_ascii_isxdigit(key_string->str[i]))
2444 g_string_free(key_string, TRUE);
2446 /* Free the array of strings */
2452 /* Key is correct! It was probably an 'old style' WEP key */
2453 /* Create the decryption_key_t structure, fill it and return it*/
2454 dk = g_malloc(sizeof(decryption_key_t));
2456 dk->type = AIRPDCAP_KEY_TYPE_WEP;
2457 dk->key = g_string_new(key);
2458 dk->bits = dk->key->len * 4;
2461 g_string_free(key_string, TRUE);
2464 /* Free the array of strings */
2470 /* There were at least 2 tokens... copy the type value */
2471 type = g_strdup(tokens[0]);
2474 * The second token is the key (right now it doesn't matter
2475 * if it is a passphrase or an hexadecimal one)
2477 key = g_strdup(tokens[1]);
2483 /* Maybe there is a third token (an ssid, if everything else is ok) */
2486 ssid = g_strdup(tokens[2]);
2495 * Now the initial key string has been divided in two/three tokens... let's see
2496 * which kind of key it is, and if it is the correct form
2498 if(g_strcasecmp(type,STRING_KEY_TYPE_WEP) == 0) /* WEP key */
2500 /* Create a new string */
2501 key_string = g_string_new(key);
2503 /* Check if it is a correct WEP key */
2504 if( ((key_string->len) > WEP_KEY_MAX_CHAR_SIZE) || ((key_string->len) < WEP_KEY_MIN_CHAR_SIZE))
2506 g_string_free(key_string, TRUE);
2508 /* Free the array of strings */
2513 if((key_string->len % 2) != 0)
2515 g_string_free(key_string, TRUE);
2517 /* Free the array of strings */
2522 for(i = 0; i < key_string->len; i++)
2524 if(!g_ascii_isxdigit(key_string->str[i]))
2526 g_string_free(key_string, TRUE);
2528 /* Free the array of strings */
2534 dk = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
2536 dk->type = AIRPDCAP_KEY_TYPE_WEP;
2537 dk->key = g_string_new(key);
2538 dk->bits = dk->key->len * 4;
2541 g_string_free(key_string, TRUE);
2544 /* Free the array of strings */
2548 else if(g_strcasecmp(type,STRING_KEY_TYPE_WPA_PSK) == 0) /* WPA key */
2550 /* Create a new string */
2551 key_string = g_string_new(key);
2553 /* Two tokens means that the user should have entered a WPA-BIN key ... */
2554 if( ((key_string->len) != WPA_PSK_KEY_CHAR_SIZE))
2556 g_string_free(key_string, TRUE);
2560 /* No ssid has been created ... */
2561 /* Free the array of strings */
2566 for(i = 0; i < key_string->len; i++)
2568 if(!g_ascii_isxdigit(key_string->str[i]))
2570 g_string_free(key_string, TRUE);
2571 /* No ssid_string has been created ... */
2575 /* No ssid has been created ... */
2576 /* Free the array of strings */
2582 /* Key was correct!!! Create the new decryption_key_t ... */
2583 dk = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
2585 dk->type = AIRPDCAP_KEY_TYPE_WPA_PMK;
2586 dk->key = g_string_new(key);
2587 dk->bits = dk->key->len * 4;
2590 g_string_free(key_string, TRUE);
2594 /* Free the array of strings */
2598 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... */
2600 /* Create a new string */
2601 key_string = g_string_new(key);
2605 /* Three (or more) tokens mean that the user entered a WPA-PWD key ... */
2606 if( ((key_string->len) > WPA_KEY_MAX_CHAR_SIZE) || ((key_string->len) < WPA_KEY_MIN_CHAR_SIZE))
2608 g_string_free(key_string, TRUE);
2614 /* Free the array of strings */
2619 if(ssid != NULL) /* more than three tokens found, means that the user specified the ssid */
2621 ssid_string = g_string_new(ssid);
2624 * XXX - Maybe we need some check on the characters? I'm not sure if only standard ASCII are ok...
2626 if( ((ssid_string->len) > WPA_SSID_MAX_CHAR_SIZE) || ((ssid_string->len) < WPA_SSID_MIN_CHAR_SIZE))
2628 g_string_free(key_string, TRUE);
2629 g_string_free(ssid_string, TRUE);
2635 /* Free the array of strings */
2641 /* Key was correct!!! Create the new decryption_key_t ... */
2642 dk = (decryption_key_t*)g_malloc(sizeof(decryption_key_t));
2644 dk->type = AIRPDCAP_KEY_TYPE_WPA_PWD;
2645 dk->key = g_string_new(key);
2646 dk->bits = 256; /* This is the lenght of the array pf bytes that will be generated using key+ssid ...*/
2648 dk->ssid = g_string_new(ssid);
2652 g_string_free(key_string, TRUE);
2653 if(ssid_string != NULL) g_string_free(ssid_string, TRUE);
2657 if(ssid != NULL) g_free(ssid);
2659 /* Free the array of strings */
2664 /* Something was wrong ... free everything */
2668 if(ssid != NULL) g_free(ssid); /* It is not always present */
2669 /* Free the array of strings */
2676 * Returns a newly allocated string representing the given decryption_key_t struct, or NULL if
2677 * something is wrong...
2680 get_key_string(decryption_key_t* dk)
2682 gchar* output_string = NULL;
2687 #ifdef HAVE_AIRPDCAP
2688 if(dk->type == AIRPDCAP_KEY_TYPE_WEP)
2690 if(dk->key == NULL) /* Should NOT happen at all... */
2693 output_string = g_strdup_printf("%s:%s",STRING_KEY_TYPE_WEP,dk->key->str);
2695 else if(dk->type == AIRPDCAP_KEY_TYPE_WPA_PWD)
2697 if(dk->key == NULL) /* Should NOT happen at all... */
2700 if(dk->ssid == NULL)
2701 output_string = g_strdup_printf("%s:%s",STRING_KEY_TYPE_WPA_PWD,dk->key->str);
2703 output_string = g_strdup_printf("%s:%s:%s",STRING_KEY_TYPE_WPA_PWD,dk->key->str,dk->ssid->str);
2705 else if(dk->type == AIRPDCAP_KEY_TYPE_WPA_PMK)
2707 if(dk->key == NULL) /* Should NOT happen at all... */
2710 output_string = g_strdup_printf("%s:%s",STRING_KEY_TYPE_WPA_PSK,dk->key->str);
2716 #else /* not HAVE_AIRPDCAP*/
2717 output_string = g_strdup(dk->key->str);
2720 return output_string;