eap_ikev2.c

Go to the documentation of this file.
00001 
00016 #include "includes.h"
00017 
00018 #include "common.h"
00019 #include "eap_i.h"
00020 #include "eap_common/eap_ikev2_common.h"
00021 #include "ikev2.h"
00022 
00023 
00024 struct eap_ikev2_data {
00025         struct ikev2_initiator_data ikev2;
00026         enum { MSG, FRAG_ACK, WAIT_FRAG_ACK, DONE, FAIL } state;
00027         struct wpabuf *in_buf;
00028         struct wpabuf *out_buf;
00029         size_t out_used;
00030         size_t fragment_size;
00031         int keys_ready;
00032         u8 keymat[EAP_MSK_LEN + EAP_EMSK_LEN];
00033         int keymat_ok;
00034 };
00035 
00036 
00037 static const u8 * eap_ikev2_get_shared_secret(void *ctx, const u8 *IDr,
00038                                               size_t IDr_len,
00039                                               size_t *secret_len)
00040 {
00041         struct eap_sm *sm = ctx;
00042 
00043         if (IDr == NULL) {
00044                 wpa_printf(MSG_DEBUG, "EAP-IKEV2: No IDr received - default "
00045                            "to user identity from EAP-Identity");
00046                 IDr = sm->identity;
00047                 IDr_len = sm->identity_len;
00048         }
00049 
00050         if (eap_user_get(sm, IDr, IDr_len, 0) < 0 || sm->user == NULL ||
00051             sm->user->password == NULL) {
00052                 wpa_printf(MSG_DEBUG, "EAP-IKEV2: No user entry found");
00053                 return NULL;
00054         }
00055 
00056         *secret_len = sm->user->password_len;
00057         return sm->user->password;
00058 }
00059 
00060 
00061 static const char * eap_ikev2_state_txt(int state)
00062 {
00063         switch (state) {
00064         case MSG:
00065                 return "MSG";
00066         case FRAG_ACK:
00067                 return "FRAG_ACK";
00068         case WAIT_FRAG_ACK:
00069                 return "WAIT_FRAG_ACK";
00070         case DONE:
00071                 return "DONE";
00072         case FAIL:
00073                 return "FAIL";
00074         default:
00075                 return "?";
00076         }
00077 }
00078 
00079 
00080 static void eap_ikev2_state(struct eap_ikev2_data *data, int state)
00081 {
00082         wpa_printf(MSG_DEBUG, "EAP-IKEV2: %s -> %s",
00083                    eap_ikev2_state_txt(data->state),
00084                    eap_ikev2_state_txt(state));
00085         data->state = state;
00086 }
00087 
00088 
00089 static void * eap_ikev2_init(struct eap_sm *sm)
00090 {
00091         struct eap_ikev2_data *data;
00092 
00093         data = os_zalloc(sizeof(*data));
00094         if (data == NULL)
00095                 return NULL;
00096         data->state = MSG;
00097         data->fragment_size = IKEV2_FRAGMENT_SIZE;
00098         data->ikev2.state = SA_INIT;
00099         data->ikev2.peer_auth = PEER_AUTH_SECRET;
00100         data->ikev2.key_pad = (u8 *) os_strdup("Key Pad for EAP-IKEv2");
00101         if (data->ikev2.key_pad == NULL)
00102                 goto failed;
00103         data->ikev2.key_pad_len = 21;
00104 
00105         /* TODO: make proposals configurable */
00106         data->ikev2.proposal.proposal_num = 1;
00107         data->ikev2.proposal.integ = AUTH_HMAC_SHA1_96;
00108         data->ikev2.proposal.prf = PRF_HMAC_SHA1;
00109         data->ikev2.proposal.encr = ENCR_AES_CBC;
00110         data->ikev2.proposal.dh = DH_GROUP2_1024BIT_MODP;
00111 
00112         data->ikev2.IDi = (u8 *) os_strdup("hostapd");
00113         data->ikev2.IDi_len = 7;
00114 
00115         data->ikev2.get_shared_secret = eap_ikev2_get_shared_secret;
00116         data->ikev2.cb_ctx = sm;
00117 
00118         return data;
00119 
00120 failed:
00121         ikev2_initiator_deinit(&data->ikev2);
00122         os_free(data);
00123         return NULL;
00124 }
00125 
00126 
00127 static void eap_ikev2_reset(struct eap_sm *sm, void *priv)
00128 {
00129         struct eap_ikev2_data *data = priv;
00130         wpabuf_free(data->in_buf);
00131         wpabuf_free(data->out_buf);
00132         ikev2_initiator_deinit(&data->ikev2);
00133         os_free(data);
00134 }
00135 
00136 
00137 static struct wpabuf * eap_ikev2_build_msg(struct eap_ikev2_data *data, u8 id)
00138 {
00139         struct wpabuf *req;
00140         u8 flags;
00141         size_t send_len, plen, icv_len = 0;
00142 
00143         wpa_printf(MSG_DEBUG, "EAP-IKEV2: Generating Request");
00144 
00145         flags = 0;
00146         send_len = wpabuf_len(data->out_buf) - data->out_used;
00147         if (1 + send_len > data->fragment_size) {
00148                 send_len = data->fragment_size - 1;
00149                 flags |= IKEV2_FLAGS_MORE_FRAGMENTS;
00150                 if (data->out_used == 0) {
00151                         flags |= IKEV2_FLAGS_LENGTH_INCLUDED;
00152                         send_len -= 4;
00153                 }
00154         }
00155 
00156         plen = 1 + send_len;
00157         if (flags & IKEV2_FLAGS_LENGTH_INCLUDED)
00158                 plen += 4;
00159         if (data->keys_ready) {
00160                 const struct ikev2_integ_alg *integ;
00161                 wpa_printf(MSG_DEBUG, "EAP-IKEV2: Add Integrity Checksum "
00162                            "Data");
00163                 flags |= IKEV2_FLAGS_ICV_INCLUDED;
00164                 integ = ikev2_get_integ(data->ikev2.proposal.integ);
00165                 if (integ == NULL) {
00166                         wpa_printf(MSG_DEBUG, "EAP-IKEV2: Unknown INTEG "
00167                                    "transform / cannot generate ICV");
00168                         return NULL;
00169                 }
00170                 icv_len = integ->hash_len;
00171 
00172                 plen += icv_len;
00173         }
00174         req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_IKEV2, plen,
00175                             EAP_CODE_REQUEST, id);
00176         if (req == NULL)
00177                 return NULL;
00178 
00179         wpabuf_put_u8(req, flags); /* Flags */
00180         if (flags & IKEV2_FLAGS_LENGTH_INCLUDED)
00181                 wpabuf_put_be32(req, wpabuf_len(data->out_buf));
00182 
00183         wpabuf_put_data(req, wpabuf_head_u8(data->out_buf) + data->out_used,
00184                         send_len);
00185         data->out_used += send_len;
00186 
00187         if (flags & IKEV2_FLAGS_ICV_INCLUDED) {
00188                 const u8 *msg = wpabuf_head(req);
00189                 size_t len = wpabuf_len(req);
00190                 ikev2_integ_hash(data->ikev2.proposal.integ,
00191                                  data->ikev2.keys.SK_ai,
00192                                  data->ikev2.keys.SK_integ_len,
00193                                  msg, len, wpabuf_put(req, icv_len));
00194         }
00195 
00196         if (data->out_used == wpabuf_len(data->out_buf)) {
00197                 wpa_printf(MSG_DEBUG, "EAP-IKEV2: Sending out %lu bytes "
00198                            "(message sent completely)",
00199                            (unsigned long) send_len);
00200                 wpabuf_free(data->out_buf);
00201                 data->out_buf = NULL;
00202                 data->out_used = 0;
00203         } else {
00204                 wpa_printf(MSG_DEBUG, "EAP-IKEV2: Sending out %lu bytes "
00205                            "(%lu more to send)", (unsigned long) send_len,
00206                            (unsigned long) wpabuf_len(data->out_buf) -
00207                            data->out_used);
00208                 eap_ikev2_state(data, WAIT_FRAG_ACK);
00209         }
00210 
00211         return req;
00212 }
00213 
00214 
00215 static struct wpabuf * eap_ikev2_buildReq(struct eap_sm *sm, void *priv, u8 id)
00216 {
00217         struct eap_ikev2_data *data = priv;
00218 
00219         switch (data->state) {
00220         case MSG:
00221                 if (data->out_buf == NULL) {
00222                         data->out_buf = ikev2_initiator_build(&data->ikev2);
00223                         if (data->out_buf == NULL) {
00224                                 wpa_printf(MSG_DEBUG, "EAP-IKEV2: Failed to "
00225                                            "generate IKEv2 message");
00226                                 return NULL;
00227                         }
00228                         data->out_used = 0;
00229                 }
00230                 /* pass through */
00231         case WAIT_FRAG_ACK:
00232                 return eap_ikev2_build_msg(data, id);
00233         case FRAG_ACK:
00234                 return eap_ikev2_build_frag_ack(id, EAP_CODE_REQUEST);
00235         default:
00236                 wpa_printf(MSG_DEBUG, "EAP-IKEV2: Unexpected state %d in "
00237                            "buildReq", data->state);
00238                 return NULL;
00239         }
00240 }
00241 
00242 
00243 static Boolean eap_ikev2_check(struct eap_sm *sm, void *priv,
00244                                struct wpabuf *respData)
00245 {
00246         const u8 *pos;
00247         size_t len;
00248 
00249         pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_IKEV2, respData,
00250                                &len);
00251         if (pos == NULL) {
00252                 wpa_printf(MSG_INFO, "EAP-IKEV2: Invalid frame");
00253                 return TRUE;
00254         }
00255 
00256         return FALSE;
00257 }
00258 
00259 
00260 static int eap_ikev2_process_icv(struct eap_ikev2_data *data,
00261                                  const struct wpabuf *respData,
00262                                  u8 flags, const u8 *pos, const u8 **end)
00263 {
00264         if (flags & IKEV2_FLAGS_ICV_INCLUDED) {
00265                 int icv_len = eap_ikev2_validate_icv(
00266                         data->ikev2.proposal.integ, &data->ikev2.keys, 0,
00267                         respData, pos, *end);
00268                 if (icv_len < 0)
00269                         return -1;
00270                 /* Hide Integrity Checksum Data from further processing */
00271                 *end -= icv_len;
00272         } else if (data->keys_ready) {
00273                 wpa_printf(MSG_INFO, "EAP-IKEV2: The message should have "
00274                            "included integrity checksum");
00275                 return -1;
00276         }
00277 
00278         return 0;
00279 }
00280 
00281 
00282 static int eap_ikev2_process_cont(struct eap_ikev2_data *data,
00283                                   const u8 *buf, size_t len)
00284 {
00285         /* Process continuation of a pending message */
00286         if (len > wpabuf_tailroom(data->in_buf)) {
00287                 wpa_printf(MSG_DEBUG, "EAP-IKEV2: Fragment overflow");
00288                 eap_ikev2_state(data, FAIL);
00289                 return -1;
00290         }
00291 
00292         wpabuf_put_data(data->in_buf, buf, len);
00293         wpa_printf(MSG_DEBUG, "EAP-IKEV2: Received %lu bytes, waiting for %lu "
00294                    "bytes more", (unsigned long) len,
00295                    (unsigned long) wpabuf_tailroom(data->in_buf));
00296 
00297         return 0;
00298 }
00299 
00300 
00301 static int eap_ikev2_process_fragment(struct eap_ikev2_data *data,
00302                                       u8 flags, u32 message_length,
00303                                       const u8 *buf, size_t len)
00304 {
00305         /* Process a fragment that is not the last one of the message */
00306         if (data->in_buf == NULL && !(flags & IKEV2_FLAGS_LENGTH_INCLUDED)) {
00307                 wpa_printf(MSG_DEBUG, "EAP-IKEV2: No Message Length field in "
00308                            "a fragmented packet");
00309                 return -1;
00310         }
00311 
00312         if (data->in_buf == NULL) {
00313                 /* First fragment of the message */
00314                 data->in_buf = wpabuf_alloc(message_length);
00315                 if (data->in_buf == NULL) {
00316                         wpa_printf(MSG_DEBUG, "EAP-IKEV2: No memory for "
00317                                    "message");
00318                         return -1;
00319                 }
00320                 wpabuf_put_data(data->in_buf, buf, len);
00321                 wpa_printf(MSG_DEBUG, "EAP-IKEV2: Received %lu bytes in first "
00322                            "fragment, waiting for %lu bytes more",
00323                            (unsigned long) len,
00324                            (unsigned long) wpabuf_tailroom(data->in_buf));
00325         }
00326 
00327         return 0;
00328 }
00329 
00330 
00331 static int eap_ikev2_server_keymat(struct eap_ikev2_data *data)
00332 {
00333         if (eap_ikev2_derive_keymat(
00334                     data->ikev2.proposal.prf, &data->ikev2.keys,
00335                     data->ikev2.i_nonce, data->ikev2.i_nonce_len,
00336                     data->ikev2.r_nonce, data->ikev2.r_nonce_len,
00337                     data->keymat) < 0) {
00338                 wpa_printf(MSG_DEBUG, "EAP-IKEV2: Failed to derive "
00339                            "key material");
00340                 return -1;
00341         }
00342         data->keymat_ok = 1;
00343         return 0;
00344 }
00345 
00346 
00347 static void eap_ikev2_process(struct eap_sm *sm, void *priv,
00348                               struct wpabuf *respData)
00349 {
00350         struct eap_ikev2_data *data = priv;
00351         const u8 *start, *pos, *end;
00352         size_t len;
00353         u8 flags;
00354         u32 message_length = 0;
00355         struct wpabuf tmpbuf;
00356 
00357         pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_IKEV2, respData,
00358                                &len);
00359         if (pos == NULL)
00360                 return; /* Should not happen; message already verified */
00361 
00362         start = pos;
00363         end = start + len;
00364 
00365         if (len == 0) {
00366                 /* fragment ack */
00367                 flags = 0;
00368         } else
00369                 flags = *pos++;
00370 
00371         if (eap_ikev2_process_icv(data, respData, flags, pos, &end) < 0) {
00372                 eap_ikev2_state(data, FAIL);
00373                 return;
00374         }
00375 
00376         if (flags & IKEV2_FLAGS_LENGTH_INCLUDED) {
00377                 if (end - pos < 4) {
00378                         wpa_printf(MSG_DEBUG, "EAP-IKEV2: Message underflow");
00379                         eap_ikev2_state(data, FAIL);
00380                         return;
00381                 }
00382                 message_length = WPA_GET_BE32(pos);
00383                 pos += 4;
00384 
00385                 if (message_length < (u32) (end - pos)) {
00386                         wpa_printf(MSG_DEBUG, "EAP-IKEV2: Invalid Message "
00387                                    "Length (%d; %ld remaining in this msg)",
00388                                    message_length, (long) (end - pos));
00389                         eap_ikev2_state(data, FAIL);
00390                         return;
00391                 }
00392         }
00393         wpa_printf(MSG_DEBUG, "EAP-IKEV2: Received packet: Flags 0x%x "
00394                    "Message Length %u", flags, message_length);
00395 
00396         if (data->state == WAIT_FRAG_ACK) {
00397                 if (len != 0) {
00398                         wpa_printf(MSG_DEBUG, "EAP-IKEV2: Unexpected payload "
00399                                    "in WAIT_FRAG_ACK state");
00400                         eap_ikev2_state(data, FAIL);
00401                         return;
00402                 }
00403                 wpa_printf(MSG_DEBUG, "EAP-IKEV2: Fragment acknowledged");
00404                 eap_ikev2_state(data, MSG);
00405                 return;
00406         }
00407 
00408         if (data->in_buf && eap_ikev2_process_cont(data, pos, end - pos) < 0) {
00409                 eap_ikev2_state(data, FAIL);
00410                 return;
00411         }
00412                 
00413         if (flags & IKEV2_FLAGS_MORE_FRAGMENTS) {
00414                 if (eap_ikev2_process_fragment(data, flags, message_length,
00415                                                pos, end - pos) < 0)
00416                         eap_ikev2_state(data, FAIL);
00417                 else
00418                         eap_ikev2_state(data, FRAG_ACK);
00419                 return;
00420         } else if (data->state == FRAG_ACK) {
00421                 wpa_printf(MSG_DEBUG, "EAP-TNC: All fragments received");
00422                 data->state = MSG;
00423         }
00424 
00425         if (data->in_buf == NULL) {
00426                 /* Wrap unfragmented messages as wpabuf without extra copy */
00427                 wpabuf_set(&tmpbuf, pos, end - pos);
00428                 data->in_buf = &tmpbuf;
00429         }
00430 
00431         if (ikev2_initiator_process(&data->ikev2, data->in_buf) < 0) {
00432                 if (data->in_buf == &tmpbuf)
00433                         data->in_buf = NULL;
00434                 eap_ikev2_state(data, FAIL);
00435                 return;
00436         }
00437 
00438         switch (data->ikev2.state) {
00439         case SA_AUTH:
00440                 /* SA_INIT was sent out, so message have to be
00441                  * integrity protected from now on. */
00442                 data->keys_ready = 1;
00443                 break;
00444         case IKEV2_DONE:
00445                 if (data->state == FAIL)
00446                         break;
00447                 wpa_printf(MSG_DEBUG, "EAP-IKEV2: Authentication completed "
00448                            "successfully");
00449                 if (eap_ikev2_server_keymat(data))
00450                         break;
00451                 eap_ikev2_state(data, DONE);
00452                 break;
00453         default:
00454                 break;
00455         }
00456 
00457         if (data->in_buf != &tmpbuf)
00458                 wpabuf_free(data->in_buf);
00459         data->in_buf = NULL;
00460 }
00461 
00462 
00463 static Boolean eap_ikev2_isDone(struct eap_sm *sm, void *priv)
00464 {
00465         struct eap_ikev2_data *data = priv;
00466         return data->state == DONE || data->state == FAIL;
00467 }
00468 
00469 
00470 static Boolean eap_ikev2_isSuccess(struct eap_sm *sm, void *priv)
00471 {
00472         struct eap_ikev2_data *data = priv;
00473         return data->state == DONE && data->ikev2.state == IKEV2_DONE &&
00474                 data->keymat_ok;
00475 }
00476 
00477 
00478 static u8 * eap_ikev2_getKey(struct eap_sm *sm, void *priv, size_t *len)
00479 {
00480         struct eap_ikev2_data *data = priv;
00481         u8 *key;
00482 
00483         if (data->state != DONE || !data->keymat_ok)
00484                 return NULL;
00485 
00486         key = os_malloc(EAP_MSK_LEN);
00487         if (key) {
00488                 os_memcpy(key, data->keymat, EAP_MSK_LEN);
00489                 *len = EAP_MSK_LEN;
00490         }
00491 
00492         return key;
00493 }
00494 
00495 
00496 static u8 * eap_ikev2_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
00497 {
00498         struct eap_ikev2_data *data = priv;
00499         u8 *key;
00500 
00501         if (data->state != DONE || !data->keymat_ok)
00502                 return NULL;
00503 
00504         key = os_malloc(EAP_EMSK_LEN);
00505         if (key) {
00506                 os_memcpy(key, data->keymat + EAP_MSK_LEN, EAP_EMSK_LEN);
00507                 *len = EAP_EMSK_LEN;
00508         }
00509 
00510         return key;
00511 }
00512 
00513 
00514 int eap_server_ikev2_register(void)
00515 {
00516         struct eap_method *eap;
00517         int ret;
00518 
00519         eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
00520                                       EAP_VENDOR_IETF, EAP_TYPE_IKEV2,
00521                                       "IKEV2");
00522         if (eap == NULL)
00523                 return -1;
00524 
00525         eap->init = eap_ikev2_init;
00526         eap->reset = eap_ikev2_reset;
00527         eap->buildReq = eap_ikev2_buildReq;
00528         eap->check = eap_ikev2_check;
00529         eap->process = eap_ikev2_process;
00530         eap->isDone = eap_ikev2_isDone;
00531         eap->getKey = eap_ikev2_getKey;
00532         eap->isSuccess = eap_ikev2_isSuccess;
00533         eap->get_emsk = eap_ikev2_get_emsk;
00534 
00535         ret = eap_server_method_register(eap);
00536         if (ret)
00537                 eap_server_method_free(eap);
00538         return ret;
00539 }
00540 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines

Generated on Sat Nov 21 23:16:51 2009 for hostapd by  doxygen 1.6.1