eiwd

no dbus iwd with iwctl functionality
git clone https://git.daat.foo/repos/eiwd.git
Log | Files | Refs | Submodules | README | LICENSE

eap-gtc.c (4425B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2018-2019  Intel Corporation. All rights reserved.
      6  *
      7  *  This library is free software; you can redistribute it and/or
      8  *  modify it under the terms of the GNU Lesser General Public
      9  *  License as published by the Free Software Foundation; either
     10  *  version 2.1 of the License, or (at your option) any later version.
     11  *
     12  *  This library is distributed in the hope that it will be useful,
     13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15  *  Lesser General Public License for more details.
     16  *
     17  *  You should have received a copy of the GNU Lesser General Public
     18  *  License along with this library; if not, write to the Free Software
     19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     20  *
     21  */
     22 
     23 #ifdef HAVE_CONFIG_H
     24 #include <config.h>
     25 #endif
     26 
     27 #define _GNU_SOURCE
     28 #include <stdio.h>
     29 #include <errno.h>
     30 #include <string.h>
     31 #include <ell/ell.h>
     32 
     33 #include "src/missing.h"
     34 #include "src/eap.h"
     35 #include "src/eap-private.h"
     36 
     37 #define EAP_GTC_MAX_PASSWORD_LEN	2048
     38 
     39 struct eap_gtc_state {
     40 	char *password;
     41 };
     42 
     43 static void eap_gtc_free(struct eap_state *eap)
     44 {
     45 	struct eap_gtc_state *gtc = eap_get_data(eap);
     46 
     47 	eap_set_data(eap, NULL);
     48 
     49 	if (gtc->password)
     50 		explicit_bzero(gtc->password, strlen(gtc->password));
     51 
     52 	l_free(gtc->password);
     53 	l_free(gtc);
     54 }
     55 
     56 static void eap_gtc_handle_request(struct eap_state *eap,
     57 					const uint8_t *pkt, size_t len)
     58 {
     59 	struct eap_gtc_state *gtc = eap_get_data(eap);
     60 	size_t secret_len = strlen(gtc->password);
     61 	uint8_t response[5 + secret_len];
     62 
     63 	if (len < 8 || strncmp((const char *)pkt, "Password", 8))
     64 		l_warn("GTC request not understood, proceeding anyway: %.*s",
     65 				(int) len, (const char *) pkt);
     66 
     67 	memcpy(response + 5, gtc->password, secret_len);
     68 
     69 	eap_method_respond(eap, response, 5 + secret_len);
     70 
     71 	eap_method_success(eap);
     72 }
     73 
     74 static int eap_gtc_check_settings(struct l_settings *settings,
     75 					struct l_queue *secrets,
     76 					const char *prefix,
     77 					struct l_queue **out_missing)
     78 {
     79 	const struct eap_secret_info *secret;
     80 	char identity_key[72];
     81 	char password_key[72];
     82 
     83 	L_AUTO_FREE_VAR(char *, identity);
     84 	L_AUTO_FREE_VAR(char *, password) = NULL;
     85 
     86 	snprintf(identity_key, sizeof(identity_key), "%sIdentity", prefix);
     87 	snprintf(password_key, sizeof(password_key), "%sPassword", prefix);
     88 
     89 	identity = l_settings_get_string(settings, "Security", identity_key);
     90 
     91 	if (!identity) {
     92 		secret = l_queue_find(secrets, eap_secret_info_match,
     93 								identity_key);
     94 		if (secret)
     95 			return 0;
     96 
     97 		eap_append_secret(out_missing, EAP_SECRET_REMOTE_USER_PASSWORD,
     98 					identity_key, password_key, NULL,
     99 					EAP_CACHE_TEMPORARY);
    100 
    101 		return 0;
    102 	}
    103 
    104 	password = l_settings_get_string(settings, "Security", password_key);
    105 
    106 	if (!password) {
    107 		secret = l_queue_find(secrets, eap_secret_info_match,
    108 								password_key);
    109 		if (secret)
    110 			return 0;
    111 
    112 		eap_append_secret(out_missing, EAP_SECRET_REMOTE_PASSWORD,
    113 					password_key, NULL, identity,
    114 					EAP_CACHE_TEMPORARY);
    115 	} else
    116 		explicit_bzero(password, strlen(password));
    117 
    118 	return 0;
    119 }
    120 
    121 static bool eap_gtc_load_settings(struct eap_state *eap,
    122 					struct l_settings *settings,
    123 					const char *prefix)
    124 {
    125 	struct eap_gtc_state *gtc;
    126 	char password_key[72];
    127 	char *password;
    128 
    129 	snprintf(password_key, sizeof(password_key), "%sPassword", prefix);
    130 	password = l_settings_get_string(settings, "Security", password_key);
    131 
    132 	if (!password) {
    133 		snprintf(password_key, sizeof(password_key), "%sGTC-Secret",
    134 									prefix);
    135 		password = l_settings_get_string(settings, "Security",
    136 								password_key);
    137 		if (!password)
    138 			return false;
    139 	}
    140 
    141 	/*
    142 	 * Limit length to prevent a stack overflow
    143 	 */
    144 	if (strlen(password) > EAP_GTC_MAX_PASSWORD_LEN) {
    145 		l_free(password);
    146 		return false;
    147 	}
    148 
    149 	gtc = l_new(struct eap_gtc_state, 1);
    150 	gtc->password = password;
    151 
    152 	eap_set_data(eap, gtc);
    153 
    154 	return true;
    155 }
    156 
    157 static struct eap_method eap_gtc = {
    158 	.request_type = EAP_TYPE_GTC,
    159 	.exports_msk = false,
    160 	.name = "GTC",
    161 	.free = eap_gtc_free,
    162 	.handle_request = eap_gtc_handle_request,
    163 	.check_settings = eap_gtc_check_settings,
    164 	.load_settings = eap_gtc_load_settings,
    165 };
    166 
    167 static int eap_gtc_init(void)
    168 {
    169 	l_debug("");
    170 	return eap_register_method(&eap_gtc);
    171 }
    172 
    173 static void eap_gtc_exit(void)
    174 {
    175 	l_debug("");
    176 	eap_unregister_method(&eap_gtc);
    177 }
    178 
    179 EAP_METHOD_BUILTIN(eap_gtc, eap_gtc_init, eap_gtc_exit)