コード例 #1
0
static gboolean
add_wep_key (NMSupplicantConfig *self,
             const char *key,
             const char *name,
             NMWepKeyType wep_type)
{
    GBytes *bytes;
    gboolean success = FALSE;
    size_t key_len = key ? strlen (key) : 0;

    if (!key || !key_len)
        return TRUE;

    if (wep_type == NM_WEP_KEY_TYPE_UNKNOWN) {
        if (nm_utils_wep_key_valid (key, NM_WEP_KEY_TYPE_KEY))
            wep_type = NM_WEP_KEY_TYPE_KEY;
        else if (nm_utils_wep_key_valid (key, NM_WEP_KEY_TYPE_PASSPHRASE))
            wep_type = NM_WEP_KEY_TYPE_PASSPHRASE;
    }

    if (   (wep_type == NM_WEP_KEY_TYPE_UNKNOWN)
            || (wep_type == NM_WEP_KEY_TYPE_KEY)) {
        if ((key_len == 10) || (key_len == 26)) {
            bytes = nm_utils_hexstr2bin (key);
            if (bytes) {
                success = nm_supplicant_config_add_option (self,
                          name,
                          g_bytes_get_data (bytes, NULL),
                          g_bytes_get_size (bytes),
                          TRUE);
                g_bytes_unref (bytes);
            }
            if (!success) {
                nm_log_warn (LOGD_SUPPLICANT, "Error adding %s to supplicant config.", name);
                return FALSE;
            }
        } else if ((key_len == 5) || (key_len == 13)) {
            if (!nm_supplicant_config_add_option (self, name, key, key_len, TRUE)) {
                nm_log_warn (LOGD_SUPPLICANT, "Error adding %s to supplicant config.", name);
                return FALSE;
            }
        } else {
            nm_log_warn (LOGD_SUPPLICANT, "Invalid WEP key '%s'", name);
            return FALSE;
        }
    } else if (wep_type == NM_WEP_KEY_TYPE_PASSPHRASE) {
        guint8 digest[16];
        size_t digest_len = sizeof (digest);

        success = wep128_passphrase_hash (key, key_len, digest, &digest_len);
        if (success)
            success = nm_supplicant_config_add_option (self, name, (const char *) digest, digest_len, TRUE);
        if (!success) {
            nm_log_warn (LOGD_SUPPLICANT, "Error adding %s to supplicant config.", name);
            return FALSE;
        }
    }

    return TRUE;
}
コード例 #2
0
static gboolean
add_wep_key (NMSupplicantConfig *self,
             const char *key,
             const char *name,
             NMWepKeyType wep_type,
             GError **error)
{
	size_t key_len = key ? strlen (key) : 0;

	if (!key || !key_len)
		return TRUE;

	if (wep_type == NM_WEP_KEY_TYPE_UNKNOWN) {
		if (nm_utils_wep_key_valid (key, NM_WEP_KEY_TYPE_KEY))
			wep_type = NM_WEP_KEY_TYPE_KEY;
		else if (nm_utils_wep_key_valid (key, NM_WEP_KEY_TYPE_PASSPHRASE))
			wep_type = NM_WEP_KEY_TYPE_PASSPHRASE;
	}

	if (   (wep_type == NM_WEP_KEY_TYPE_UNKNOWN)
	    || (wep_type == NM_WEP_KEY_TYPE_KEY)) {
		if ((key_len == 10) || (key_len == 26)) {
			gs_unref_bytes GBytes *bytes = NULL;

			bytes = nm_utils_hexstr2bin (key);
			if (!bytes) {
				g_set_error (error, NM_SUPPLICANT_ERROR, NM_SUPPLICANT_ERROR_CONFIG,
				             "cannot add wep-key %s to suplicant config because key is not hex",
				             name);
				return FALSE;
			}
			if (!nm_supplicant_config_add_option (self,
			                                      name,
			                                      g_bytes_get_data (bytes, NULL),
			                                      g_bytes_get_size (bytes),
			                                      TRUE,
			                                      error))
				return FALSE;
		} else if ((key_len == 5) || (key_len == 13)) {
			if (!nm_supplicant_config_add_option (self, name, key, key_len, TRUE, error))
				return FALSE;
		} else {
			g_set_error (error, NM_SUPPLICANT_ERROR, NM_SUPPLICANT_ERROR_CONFIG,
			             "Cannot add wep-key %s to suplicant config because key-length %u is invalid",
			             name, (guint) key_len);
			return FALSE;
		}
	} else if (wep_type == NM_WEP_KEY_TYPE_PASSPHRASE) {
		guint8 digest[16];
		size_t digest_len = sizeof (digest);

		wep128_passphrase_hash (key, key_len, digest, &digest_len);
		if (!nm_supplicant_config_add_option (self, name, (const char *) digest, digest_len, TRUE, error))
			return FALSE;
	}

	return TRUE;
}