Updated through tag hostap_2_5 from git://w1.fi/hostap.git
[mech_eap.git] / libeap / src / eap_peer / eap_tls_common.c
index 8559c4d..b1f9300 100644 (file)
@@ -1,15 +1,9 @@
 /*
  * EAP peer: EAP-TLS/PEAP/TTLS/FAST common functions
- * Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi>
+ * Copyright (c) 2004-2013, Jouni Malinen <j@w1.fi>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation.
- *
- * Alternatively, this software may be distributed under the terms of BSD
- * license.
- *
- * See README and COPYING for more details.
+ * This software may be distributed under the terms of the BSD license.
+ * See README for more details.
  */
 
 #include "includes.h"
 #include "eap_config.h"
 
 
+static struct wpabuf * eap_tls_msg_alloc(EapType type, size_t payload_len,
+                                        u8 code, u8 identifier)
+{
+       if (type == EAP_UNAUTH_TLS_TYPE)
+               return eap_msg_alloc(EAP_VENDOR_UNAUTH_TLS,
+                                    EAP_VENDOR_TYPE_UNAUTH_TLS, payload_len,
+                                    code, identifier);
+       if (type == EAP_WFA_UNAUTH_TLS_TYPE)
+               return eap_msg_alloc(EAP_VENDOR_WFA_NEW,
+                                    EAP_VENDOR_WFA_UNAUTH_TLS, payload_len,
+                                    code, identifier);
+       return eap_msg_alloc(EAP_VENDOR_IETF, type, payload_len, code,
+                            identifier);
+}
+
+
 static int eap_tls_check_blob(struct eap_sm *sm, const char **name,
                              const u8 **data, size_t *data_len)
 {
@@ -54,6 +64,22 @@ static void eap_tls_params_flags(struct tls_connection_params *params,
                params->flags |= TLS_CONN_ALLOW_SIGN_RSA_MD5;
        if (os_strstr(txt, "tls_disable_time_checks=1"))
                params->flags |= TLS_CONN_DISABLE_TIME_CHECKS;
+       if (os_strstr(txt, "tls_disable_session_ticket=1"))
+               params->flags |= TLS_CONN_DISABLE_SESSION_TICKET;
+       if (os_strstr(txt, "tls_disable_session_ticket=0"))
+               params->flags &= ~TLS_CONN_DISABLE_SESSION_TICKET;
+       if (os_strstr(txt, "tls_disable_tlsv1_0=1"))
+               params->flags |= TLS_CONN_DISABLE_TLSv1_0;
+       if (os_strstr(txt, "tls_disable_tlsv1_0=0"))
+               params->flags &= ~TLS_CONN_DISABLE_TLSv1_0;
+       if (os_strstr(txt, "tls_disable_tlsv1_1=1"))
+               params->flags |= TLS_CONN_DISABLE_TLSv1_1;
+       if (os_strstr(txt, "tls_disable_tlsv1_1=0"))
+               params->flags &= ~TLS_CONN_DISABLE_TLSv1_1;
+       if (os_strstr(txt, "tls_disable_tlsv1_2=1"))
+               params->flags |= TLS_CONN_DISABLE_TLSv1_2;
+       if (os_strstr(txt, "tls_disable_tlsv1_2=0"))
+               params->flags &= ~TLS_CONN_DISABLE_TLSv1_2;
 }
 
 
@@ -68,6 +94,8 @@ static void eap_tls_params_from_conf1(struct tls_connection_params *params,
        params->dh_file = (char *) config->dh_file;
        params->subject_match = (char *) config->subject_match;
        params->altsubject_match = (char *) config->altsubject_match;
+       params->suffix_match = config->domain_suffix_match;
+       params->domain_match = config->domain_match;
        params->engine = config->engine;
        params->engine_id = config->engine_id;
        params->pin = config->pin;
@@ -89,6 +117,8 @@ static void eap_tls_params_from_conf2(struct tls_connection_params *params,
        params->dh_file = (char *) config->dh_file2;
        params->subject_match = (char *) config->subject_match2;
        params->altsubject_match = (char *) config->altsubject_match2;
+       params->suffix_match = config->domain_suffix_match2;
+       params->domain_match = config->domain_match2;
        params->engine = config->engine2;
        params->engine_id = config->engine2_id;
        params->pin = config->pin2;
@@ -105,14 +135,27 @@ static int eap_tls_params_from_conf(struct eap_sm *sm,
                                    struct eap_peer_config *config, int phase2)
 {
        os_memset(params, 0, sizeof(*params));
+       if (sm->workaround && data->eap_type != EAP_TYPE_FAST) {
+               /*
+                * Some deployed authentication servers seem to be unable to
+                * handle the TLS Session Ticket extension (they are supposed
+                * to ignore unrecognized TLS extensions, but end up rejecting
+                * the ClientHello instead). As a workaround, disable use of
+                * TLS Sesson Ticket extension for EAP-TLS, EAP-PEAP, and
+                * EAP-TTLS (EAP-FAST uses session ticket, so any server that
+                * supports EAP-FAST does not need this workaround).
+                */
+               params->flags |= TLS_CONN_DISABLE_SESSION_TICKET;
+       }
        if (phase2) {
                wpa_printf(MSG_DEBUG, "TLS: using phase2 config options");
                eap_tls_params_from_conf2(params, config);
        } else {
                wpa_printf(MSG_DEBUG, "TLS: using phase1 config options");
                eap_tls_params_from_conf1(params, config);
+               if (data->eap_type == EAP_TYPE_FAST)
+                       params->flags |= TLS_CONN_EAP_FAST;
        }
-       params->tls_ia = data->tls_ia;
 
        /*
         * Use blob data, if available. Otherwise, leave reference to external
@@ -132,6 +175,8 @@ static int eap_tls_params_from_conf(struct eap_sm *sm,
                return -1;
        }
 
+       params->openssl_ciphers = config->openssl_ciphers;
+
        return 0;
 }
 
@@ -143,39 +188,40 @@ static int eap_tls_init_connection(struct eap_sm *sm,
 {
        int res;
 
-       data->conn = tls_connection_init(sm->ssl_ctx);
+       if (config->ocsp)
+               params->flags |= TLS_CONN_REQUEST_OCSP;
+       if (config->ocsp == 2)
+               params->flags |= TLS_CONN_REQUIRE_OCSP;
+       data->conn = tls_connection_init(data->ssl_ctx);
        if (data->conn == NULL) {
                wpa_printf(MSG_INFO, "SSL: Failed to initialize new TLS "
                           "connection");
                return -1;
        }
 
-       res = tls_connection_set_params(sm->ssl_ctx, data->conn, params);
-       if (res == TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED) {
+       res = tls_connection_set_params(data->ssl_ctx, data->conn, params);
+       if (res == TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN) {
                /*
-                * At this point with the pkcs11 engine the PIN might be wrong.
-                * We reset the PIN in the configuration to be sure to not use
-                * it again and the calling function must request a new one.
+                * At this point with the pkcs11 engine the PIN is wrong. We
+                * reset the PIN in the configuration to be sure to not use it
+                * again and the calling function must request a new one.
                 */
+               wpa_printf(MSG_INFO,
+                          "TLS: Bad PIN provided, requesting a new one");
                os_free(config->pin);
                config->pin = NULL;
+               eap_sm_request_pin(sm);
+               sm->ignore = TRUE;
+       } else if (res == TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED) {
+               wpa_printf(MSG_INFO, "TLS: Failed to initialize engine");
        } else if (res == TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED) {
                wpa_printf(MSG_INFO, "TLS: Failed to load private key");
-               /*
-                * We do not know exactly but maybe the PIN was wrong,
-                * so ask for a new one.
-                */
-               os_free(config->pin);
-               config->pin = NULL;
-               eap_sm_request_pin(sm);
                sm->ignore = TRUE;
-               tls_connection_deinit(sm->ssl_ctx, data->conn);
-               data->conn = NULL;
-               return -1;
-       } else if (res) {
+       }
+       if (res) {
                wpa_printf(MSG_INFO, "TLS: Failed to set TLS connection "
                           "parameters");
-               tls_connection_deinit(sm->ssl_ctx, data->conn);
+               tls_connection_deinit(data->ssl_ctx, data->conn);
                data->conn = NULL;
                return -1;
        }
@@ -189,13 +235,14 @@ static int eap_tls_init_connection(struct eap_sm *sm,
  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  * @data: Data for TLS processing
  * @config: Pointer to the network configuration
+ * @eap_type: EAP method used in Phase 1 (EAP_TYPE_TLS/PEAP/TTLS/FAST)
  * Returns: 0 on success, -1 on failure
  *
  * This function is used to initialize shared TLS functionality for EAP-TLS,
  * EAP-PEAP, EAP-TTLS, and EAP-FAST.
  */
 int eap_peer_tls_ssl_init(struct eap_sm *sm, struct eap_ssl_data *data,
-                         struct eap_peer_config *config)
+                         struct eap_peer_config *config, u8 eap_type)
 {
        struct tls_connection_params params;
 
@@ -203,7 +250,10 @@ int eap_peer_tls_ssl_init(struct eap_sm *sm, struct eap_ssl_data *data,
                return -1;
 
        data->eap = sm;
+       data->eap_type = eap_type;
        data->phase2 = sm->init_phase2;
+       data->ssl_ctx = sm->init_phase2 && sm->ssl_ctx2 ? sm->ssl_ctx2 :
+               sm->ssl_ctx;
        if (eap_tls_params_from_conf(sm, data, &params, config, data->phase2) <
            0)
                return -1;
@@ -241,7 +291,7 @@ int eap_peer_tls_ssl_init(struct eap_sm *sm, struct eap_ssl_data *data,
  */
 void eap_peer_tls_ssl_deinit(struct eap_sm *sm, struct eap_ssl_data *data)
 {
-       tls_connection_deinit(sm->ssl_ctx, data->conn);
+       tls_connection_deinit(data->ssl_ctx, data->conn);
        eap_peer_tls_reset_input(data);
        eap_peer_tls_reset_output(data);
 }
@@ -264,49 +314,60 @@ void eap_peer_tls_ssl_deinit(struct eap_sm *sm, struct eap_ssl_data *data)
 u8 * eap_peer_tls_derive_key(struct eap_sm *sm, struct eap_ssl_data *data,
                             const char *label, size_t len)
 {
-       struct tls_keys keys;
-       u8 *rnd = NULL, *out;
+       u8 *out;
 
        out = os_malloc(len);
        if (out == NULL)
                return NULL;
 
-       /* First, try to use TLS library function for PRF, if available. */
-       if (tls_connection_prf(sm->ssl_ctx, data->conn, label, 0, out, len) ==
-           0)
-               return out;
+       if (tls_connection_prf(data->ssl_ctx, data->conn, label, 0, 0,
+                              out, len)) {
+               os_free(out);
+               return NULL;
+       }
 
-       /*
-        * TLS library did not support key generation, so get the needed TLS
-        * session parameters and use an internal implementation of TLS PRF to
-        * derive the key.
-        */
-       if (tls_connection_get_keys(sm->ssl_ctx, data->conn, &keys))
-               goto fail;
-
-       if (keys.client_random == NULL || keys.server_random == NULL ||
-           keys.master_key == NULL)
-               goto fail;
-
-       rnd = os_malloc(keys.client_random_len + keys.server_random_len);
-       if (rnd == NULL)
-               goto fail;
-       os_memcpy(rnd, keys.client_random, keys.client_random_len);
-       os_memcpy(rnd + keys.client_random_len, keys.server_random,
-                 keys.server_random_len);
+       return out;
+}
 
-       if (tls_prf(keys.master_key, keys.master_key_len,
-                   label, rnd, keys.client_random_len +
-                   keys.server_random_len, out, len))
-               goto fail;
 
-       os_free(rnd);
-       return out;
+/**
+ * eap_peer_tls_derive_session_id - Derive a Session-Id based on TLS data
+ * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
+ * @data: Data for TLS processing
+ * @eap_type: EAP method used in Phase 1 (EAP_TYPE_TLS/PEAP/TTLS/FAST)
+ * @len: Pointer to length of the session ID generated
+ * Returns: Pointer to allocated Session-Id on success or %NULL on failure
+ *
+ * This function derive the Session-Id based on the TLS session data
+ * (client/server random and method type).
+ *
+ * The caller is responsible for freeing the returned buffer.
+ */
+u8 * eap_peer_tls_derive_session_id(struct eap_sm *sm,
+                                   struct eap_ssl_data *data, u8 eap_type,
+                                   size_t *len)
+{
+       struct tls_random keys;
+       u8 *out;
+
+       if (tls_connection_get_random(sm->ssl_ctx, data->conn, &keys))
+               return NULL;
+
+       if (keys.client_random == NULL || keys.server_random == NULL)
+               return NULL;
+
+       *len = 1 + keys.client_random_len + keys.server_random_len;
+       out = os_malloc(*len);
+       if (out == NULL)
+               return NULL;
+
+       /* Session-Id = EAP type || client.random || server.random */
+       out[0] = eap_type;
+       os_memcpy(out + 1, keys.client_random, keys.client_random_len);
+       os_memcpy(out + 1 + keys.client_random_len, keys.server_random,
+                 keys.server_random_len);
 
-fail:
-       os_free(out);
-       os_free(rnd);
-       return NULL;
+       return out;
 }
 
 
@@ -361,7 +422,8 @@ static int eap_peer_tls_reassemble_fragment(struct eap_ssl_data *data,
                eap_peer_tls_reset_input(data);
                return -1;
        }
-       wpabuf_put_buf(data->tls_in, in_data);
+       if (in_data)
+               wpabuf_put_buf(data->tls_in, in_data);
        data->tls_in_left -= in_len;
 
        if (data->tls_in_left > 0) {
@@ -419,22 +481,19 @@ static const struct wpabuf * eap_peer_tls_data_reassemble(
  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  * @data: Data for TLS processing
  * @in_data: Message received from the server
- * @in_len: Length of in_data
  * @out_data: Buffer for returning a pointer to application data (if available)
  * Returns: 0 on success, 1 if more input data is needed, 2 if application data
  * is available, -1 on failure
  */
 static int eap_tls_process_input(struct eap_sm *sm, struct eap_ssl_data *data,
-                                const u8 *in_data, size_t in_len,
+                                const struct wpabuf *in_data,
                                 struct wpabuf **out_data)
 {
        const struct wpabuf *msg;
        int need_more_input;
        struct wpabuf *appl_data;
-       struct wpabuf buf;
 
-       wpabuf_set(&buf, in_data, in_len);
-       msg = eap_peer_tls_data_reassemble(data, &buf, &need_more_input);
+       msg = eap_peer_tls_data_reassemble(data, in_data, &need_more_input);
        if (msg == NULL)
                return need_more_input ? 1 : -1;
 
@@ -447,14 +506,14 @@ static int eap_tls_process_input(struct eap_sm *sm, struct eap_ssl_data *data,
                WPA_ASSERT(data->tls_out == NULL);
        }
        appl_data = NULL;
-       data->tls_out = tls_connection_handshake(sm->ssl_ctx, data->conn,
+       data->tls_out = tls_connection_handshake(data->ssl_ctx, data->conn,
                                                 msg, &appl_data);
 
        eap_peer_tls_reset_input(data);
 
        if (appl_data &&
-           tls_connection_established(sm->ssl_ctx, data->conn) &&
-           !tls_connection_get_failed(sm->ssl_ctx, data->conn)) {
+           tls_connection_established(data->ssl_ctx, data->conn) &&
+           !tls_connection_get_failed(data->ssl_ctx, data->conn)) {
                wpa_hexdump_buf_key(MSG_MSGDUMP, "SSL: Application data",
                                    appl_data);
                *out_data = appl_data;
@@ -520,9 +579,8 @@ static int eap_tls_process_output(struct eap_ssl_data *data, EapType eap_type,
                length_included = 1;
        }
 
-       *out_data = eap_msg_alloc(EAP_VENDOR_IETF, eap_type,
-                                 1 + length_included * 4 + len,
-                                 EAP_CODE_RESPONSE, id);
+       *out_data = eap_tls_msg_alloc(eap_type, 1 + length_included * 4 + len,
+                                     EAP_CODE_RESPONSE, id);
        if (*out_data == NULL)
                return -1;
 
@@ -555,7 +613,6 @@ static int eap_tls_process_output(struct eap_ssl_data *data, EapType eap_type,
  * @peap_version: Version number for EAP-PEAP/TTLS
  * @id: EAP identifier for the response
  * @in_data: Message received from the server
- * @in_len: Length of in_data
  * @out_data: Buffer for returning a pointer to the response message
  * Returns: 0 on success, 1 if more input data is needed, 2 if application data
  * is available, or -1 on failure
@@ -578,14 +635,15 @@ static int eap_tls_process_output(struct eap_ssl_data *data, EapType eap_type,
  */
 int eap_peer_tls_process_helper(struct eap_sm *sm, struct eap_ssl_data *data,
                                EapType eap_type, int peap_version,
-                               u8 id, const u8 *in_data, size_t in_len,
+                               u8 id, const struct wpabuf *in_data,
                                struct wpabuf **out_data)
 {
        int ret = 0;
 
        *out_data = NULL;
 
-       if (data->tls_out && wpabuf_len(data->tls_out) > 0 && in_len > 0) {
+       if (data->tls_out && wpabuf_len(data->tls_out) > 0 &&
+           wpabuf_len(in_data) > 0) {
                wpa_printf(MSG_DEBUG, "SSL: Received non-ACK when output "
                           "fragments are waiting to be sent out");
                return -1;
@@ -596,8 +654,7 @@ int eap_peer_tls_process_helper(struct eap_sm *sm, struct eap_ssl_data *data,
                 * No more data to send out - expect to receive more data from
                 * the AS.
                 */
-               int res = eap_tls_process_input(sm, data, in_data, in_len,
-                                               out_data);
+               int res = eap_tls_process_input(sm, data, in_data, out_data);
                if (res) {
                        /*
                         * Input processing failed (res = -1) or more data is
@@ -622,15 +679,21 @@ int eap_peer_tls_process_helper(struct eap_sm *sm, struct eap_ssl_data *data,
                return -1;
        }
 
-       if (tls_connection_get_failed(sm->ssl_ctx, data->conn)) {
+       if (tls_connection_get_failed(data->ssl_ctx, data->conn)) {
                /* TLS processing has failed - return error */
                wpa_printf(MSG_DEBUG, "SSL: Failed - tls_out available to "
-                          "report error");
+                          "report error (len=%u)",
+                          (unsigned int) wpabuf_len(data->tls_out));
                ret = -1;
                /* TODO: clean pin if engine used? */
+               if (wpabuf_len(data->tls_out) == 0) {
+                       wpabuf_free(data->tls_out);
+                       data->tls_out = NULL;
+                       return -1;
+               }
        }
 
-       if (data->tls_out == NULL || wpabuf_len(data->tls_out) == 0) {
+       if (wpabuf_len(data->tls_out) == 0) {
                /*
                 * TLS negotiation should now be complete since all other cases
                 * needing more data should have been caught above based on
@@ -660,8 +723,7 @@ struct wpabuf * eap_peer_tls_build_ack(u8 id, EapType eap_type,
 {
        struct wpabuf *resp;
 
-       resp = eap_msg_alloc(EAP_VENDOR_IETF, eap_type, 1, EAP_CODE_RESPONSE,
-                            id);
+       resp = eap_tls_msg_alloc(eap_type, 1, EAP_CODE_RESPONSE, id);
        if (resp == NULL)
                return NULL;
        wpa_printf(MSG_DEBUG, "SSL: Building ACK (type=%d id=%d ver=%d)",
@@ -681,7 +743,7 @@ int eap_peer_tls_reauth_init(struct eap_sm *sm, struct eap_ssl_data *data)
 {
        eap_peer_tls_reset_input(data);
        eap_peer_tls_reset_output(data);
-       return tls_connection_shutdown(sm->ssl_ctx, data->conn);
+       return tls_connection_shutdown(data->ssl_ctx, data->conn);
 }
 
 
@@ -697,16 +759,24 @@ int eap_peer_tls_reauth_init(struct eap_sm *sm, struct eap_ssl_data *data)
 int eap_peer_tls_status(struct eap_sm *sm, struct eap_ssl_data *data,
                        char *buf, size_t buflen, int verbose)
 {
-       char name[128];
+       char version[20], name[128];
        int len = 0, ret;
 
-       if (tls_get_cipher(sm->ssl_ctx, data->conn, name, sizeof(name)) == 0) {
-               ret = os_snprintf(buf + len, buflen - len,
-                                 "EAP TLS cipher=%s\n", name);
-               if (ret < 0 || (size_t) ret >= buflen - len)
-                       return len;
-               len += ret;
-       }
+       if (tls_get_version(data->ssl_ctx, data->conn, version,
+                           sizeof(version)) < 0)
+               version[0] = '\0';
+       if (tls_get_cipher(data->ssl_ctx, data->conn, name, sizeof(name)) < 0)
+               name[0] = '\0';
+
+       ret = os_snprintf(buf + len, buflen - len,
+                         "eap_tls_version=%s\n"
+                         "EAP TLS cipher=%s\n"
+                         "tls_session_reused=%d\n",
+                         version, name,
+                         tls_connection_resumed(data->ssl_ctx, data->conn));
+       if (os_snprintf_error(buflen - len, ret))
+               return len;
+       len += ret;
 
        return len;
 }
@@ -749,7 +819,24 @@ const u8 * eap_peer_tls_process_init(struct eap_sm *sm,
 
        /* Ignore errors before we do anything*/
        (void) tls_get_errors(sm->ssl_ctx);
-       pos = eap_hdr_validate(EAP_VENDOR_IETF, eap_type, reqData, &left);
+
+       //// if (tls_get_errors(data->ssl_ctx)) {
+       ////    wpa_printf(MSG_INFO, "SSL: TLS errors detected");
+       ////    ret->ignore = TRUE;
+       ////    return NULL;
+       //// }
+
+       if (eap_type == EAP_UNAUTH_TLS_TYPE)
+               pos = eap_hdr_validate(EAP_VENDOR_UNAUTH_TLS,
+                                      EAP_VENDOR_TYPE_UNAUTH_TLS, reqData,
+                                      &left);
+       else if (eap_type == EAP_WFA_UNAUTH_TLS_TYPE)
+               pos = eap_hdr_validate(EAP_VENDOR_WFA_NEW,
+                                      EAP_VENDOR_WFA_UNAUTH_TLS, reqData,
+                                      &left);
+       else
+               pos = eap_hdr_validate(EAP_VENDOR_IETF, eap_type, reqData,
+                                      &left);
        if (pos == NULL) {
                ret->ignore = TRUE;
                return NULL;
@@ -790,6 +877,14 @@ const u8 * eap_peer_tls_process_init(struct eap_sm *sm,
                }
                pos += 4;
                left -= 4;
+
+               if (left > tls_msg_len) {
+                       wpa_printf(MSG_INFO, "SSL: TLS Message Length (%d "
+                                  "bytes) smaller than this fragment (%d "
+                                  "bytes)", (int) tls_msg_len, (int) left);
+                       ret->ignore = TRUE;
+                       return NULL;
+               }
        }
 
        ret->ignore = FALSE;
@@ -851,7 +946,7 @@ int eap_peer_tls_decrypt(struct eap_sm *sm, struct eap_ssl_data *data,
        if (msg == NULL)
                return need_more_input ? 1 : -1;
 
-       *in_decrypted = tls_connection_decrypt(sm->ssl_ctx, data->conn, msg);
+       *in_decrypted = tls_connection_decrypt(data->ssl_ctx, data->conn, msg);
        eap_peer_tls_reset_input(data);
        if (*in_decrypted == NULL) {
                wpa_printf(MSG_INFO, "SSL: Failed to decrypt Phase 2 data");
@@ -879,8 +974,8 @@ int eap_peer_tls_encrypt(struct eap_sm *sm, struct eap_ssl_data *data,
 {
        if (in_data) {
                eap_peer_tls_reset_output(data);
-               data->tls_out = tls_connection_encrypt(sm->ssl_ctx, data->conn,
-                                                      in_data);
+               data->tls_out = tls_connection_encrypt(data->ssl_ctx,
+                                                      data->conn, in_data);
                if (data->tls_out == NULL) {
                        wpa_printf(MSG_INFO, "SSL: Failed to encrypt Phase 2 "
                                   "data (in_len=%lu)",
@@ -913,7 +1008,7 @@ int eap_peer_select_phase2_methods(struct eap_peer_config *config,
 {
        char *start, *pos, *buf;
        struct eap_method_type *methods = NULL, *_methods;
-       u8 method;
+       u32 method;
        size_t num_methods = 0, prefix_len;
 
        if (config == NULL || config->phase2 == NULL)
@@ -945,8 +1040,8 @@ int eap_peer_select_phase2_methods(struct eap_peer_config *config,
                                   "method '%s'", start);
                } else {
                        num_methods++;
-                       _methods = os_realloc(methods,
-                                             num_methods * sizeof(*methods));
+                       _methods = os_realloc_array(methods, num_methods,
+                                                   sizeof(*methods));
                        if (_methods == NULL) {
                                os_free(methods);
                                os_free(buf);