Handle NULL return from os_zalloc() in sta_track_add()
[mech_eap.git] / src / eap_server / eap_server_gpsk.c
index 66f4271..94e74ec 100644 (file)
@@ -24,6 +24,8 @@ struct eap_gpsk_data {
        size_t sk_len;
        u8 pk[EAP_GPSK_MAX_PK_LEN];
        size_t pk_len;
+       u8 session_id[128];
+       size_t id_len;
        u8 *id_peer;
        size_t id_peer_len;
 #define MAX_NUM_CSUITES 2
@@ -95,7 +97,7 @@ static void eap_gpsk_reset(struct eap_sm *sm, void *priv)
 {
        struct eap_gpsk_data *data = priv;
        os_free(data->id_peer);
-       os_free(data);
+       bin_clear_free(data, sizeof(*data));
 }
 
 
@@ -417,6 +419,21 @@ static void eap_gpsk_process_gpsk_2(struct eap_sm *sm,
                return;
        }
 
+       if (eap_gpsk_derive_session_id(sm->user->password,
+                                      sm->user->password_len,
+                                      data->vendor, data->specifier,
+                                      data->rand_peer, data->rand_server,
+                                      data->id_peer, data->id_peer_len,
+                                      sm->server_id, sm->server_id_len,
+                                      EAP_TYPE_GPSK,
+                                      data->session_id, &data->id_len) < 0) {
+               wpa_printf(MSG_DEBUG, "EAP-GPSK: Failed to derive Session-Id");
+               eap_gpsk_state(data, FAILURE);
+               return;
+       }
+       wpa_hexdump(MSG_DEBUG, "EAP-GPSK: Derived Session-Id",
+                   data->session_id, data->id_len);
+
        miclen = eap_gpsk_mic_len(data->vendor, data->specifier);
        if (end - pos < (int) miclen) {
                wpa_printf(MSG_DEBUG, "EAP-GPSK: Message too short for MIC "
@@ -433,7 +450,7 @@ static void eap_gpsk_process_gpsk_2(struct eap_sm *sm,
                eap_gpsk_state(data, FAILURE);
                return;
        }
-       if (os_memcmp(mic, pos, miclen) != 0) {
+       if (os_memcmp_const(mic, pos, miclen) != 0) {
                wpa_printf(MSG_INFO, "EAP-GPSK: Incorrect MIC in GPSK-2");
                wpa_hexdump(MSG_DEBUG, "EAP-GPSK: Received MIC", pos, miclen);
                wpa_hexdump(MSG_DEBUG, "EAP-GPSK: Computed MIC", mic, miclen);
@@ -502,7 +519,7 @@ static void eap_gpsk_process_gpsk_4(struct eap_sm *sm,
                eap_gpsk_state(data, FAILURE);
                return;
        }
-       if (os_memcmp(mic, pos, miclen) != 0) {
+       if (os_memcmp_const(mic, pos, miclen) != 0) {
                wpa_printf(MSG_INFO, "EAP-GPSK: Incorrect MIC in GPSK-4");
                wpa_hexdump(MSG_DEBUG, "EAP-GPSK: Received MIC", pos, miclen);
                wpa_hexdump(MSG_DEBUG, "EAP-GPSK: Computed MIC", mic, miclen);
@@ -593,10 +610,27 @@ static Boolean eap_gpsk_isSuccess(struct eap_sm *sm, void *priv)
 }
 
 
+static u8 * eap_gpsk_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
+{
+       struct eap_gpsk_data *data = priv;
+       u8 *sid;
+
+       if (data->state != SUCCESS)
+               return NULL;
+
+       sid = os_malloc(data->id_len);
+       if (sid == NULL)
+               return NULL;
+       os_memcpy(sid, data->session_id, data->id_len);
+       *len = data->id_len;
+
+       return sid;
+}
+
+
 int eap_server_gpsk_register(void)
 {
        struct eap_method *eap;
-       int ret;
 
        eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
                                      EAP_VENDOR_IETF, EAP_TYPE_GPSK, "GPSK");
@@ -612,9 +646,7 @@ int eap_server_gpsk_register(void)
        eap->getKey = eap_gpsk_getKey;
        eap->isSuccess = eap_gpsk_isSuccess;
        eap->get_emsk = eap_gpsk_get_emsk;
+       eap->getSessionId = eap_gpsk_get_session_id;
 
-       ret = eap_server_method_register(eap);
-       if (ret)
-               eap_server_method_free(eap);
-       return ret;
+       return eap_server_method_register(eap);
 }