From bfc62fe1331da4eca6dec70133c5d4cd1d6bc958 Mon Sep 17 00:00:00 2001 From: Jouni Malinen Date: Thu, 28 Jun 2012 20:54:53 +0300 Subject: [PATCH] WPS: Share a single function for generating NFS password tokens There is no need for both hostapd and wpa_supplicant to have their own functions for this. Signed-hostap: Jouni Malinen --- src/ap/wps_hostapd.c | 48 +++--------------------------- src/wps/wps.h | 3 ++ src/wps/wps_common.c | 52 ++++++++++++++++++++++++++++++++- wpa_supplicant/wps_supplicant.c | 48 +++--------------------------- 4 files changed, 62 insertions(+), 89 deletions(-) diff --git a/src/ap/wps_hostapd.c b/src/ap/wps_hostapd.c index fbff90fa0..07ce06c10 100644 --- a/src/ap/wps_hostapd.c +++ b/src/ap/wps_hostapd.c @@ -1626,50 +1626,10 @@ struct wpabuf * hostapd_wps_nfc_config_token(struct hostapd_data *hapd, struct wpabuf * hostapd_wps_nfc_token_gen(struct hostapd_data *hapd, int ndef) { - struct wpabuf *priv = NULL, *pub = NULL, *pw; - void *dh_ctx; - struct wpabuf *ret; - u16 val; - - pw = wpabuf_alloc(WPS_OOB_DEVICE_PASSWORD_LEN); - if (pw == NULL) - return NULL; - - if (random_get_bytes(wpabuf_put(pw, WPS_OOB_DEVICE_PASSWORD_LEN), - WPS_OOB_DEVICE_PASSWORD_LEN) || - random_get_bytes((u8 *) &val, sizeof(val))) { - wpabuf_free(pw); - return NULL; - } - - dh_ctx = dh5_init(&priv, &pub); - if (dh_ctx == NULL) { - wpabuf_free(pw); - return NULL; - } - dh5_free(dh_ctx); - - hapd->conf->wps_nfc_dev_pw_id = 0x10 + val % 0xfff0; - wpabuf_free(hapd->conf->wps_nfc_dh_pubkey); - hapd->conf->wps_nfc_dh_pubkey = pub; - wpabuf_free(hapd->conf->wps_nfc_dh_privkey); - hapd->conf->wps_nfc_dh_privkey = priv; - wpabuf_free(hapd->conf->wps_nfc_dev_pw); - hapd->conf->wps_nfc_dev_pw = pw; - - ret = wps_build_nfc_pw_token(hapd->conf->wps_nfc_dev_pw_id, - hapd->conf->wps_nfc_dh_pubkey, - hapd->conf->wps_nfc_dev_pw); - if (ndef && ret) { - struct wpabuf *tmp; - tmp = ndef_build_wifi(ret); - wpabuf_free(ret); - if (tmp == NULL) - return NULL; - ret = tmp; - } - - return ret; + return wps_nfc_token_gen(ndef, &hapd->conf->wps_nfc_dev_pw_id, + &hapd->conf->wps_nfc_dh_pubkey, + &hapd->conf->wps_nfc_dh_privkey, + &hapd->conf->wps_nfc_dev_pw); } diff --git a/src/wps/wps.h b/src/wps/wps.h index a94a90a00..177c8226a 100644 --- a/src/wps/wps.h +++ b/src/wps/wps.h @@ -850,6 +850,9 @@ u16 wps_config_methods_str2bin(const char *str); struct wpabuf * wps_build_nfc_pw_token(u16 dev_pw_id, const struct wpabuf *pubkey, const struct wpabuf *dev_pw); +struct wpabuf * wps_nfc_token_gen(int ndef, int *id, struct wpabuf **pubkey, + struct wpabuf **privkey, + struct wpabuf **dev_pw); /* ndef.c */ struct wpabuf * ndef_parse_wifi(const struct wpabuf *buf); diff --git a/src/wps/wps_common.c b/src/wps/wps_common.c index 16412ca33..9e06087b4 100644 --- a/src/wps/wps_common.c +++ b/src/wps/wps_common.c @@ -1,6 +1,6 @@ /* * Wi-Fi Protected Setup - common functionality - * Copyright (c) 2008-2009, Jouni Malinen + * Copyright (c) 2008-2012, Jouni Malinen * * This software may be distributed under the terms of the BSD license. * See README for more details. @@ -727,3 +727,53 @@ struct wpabuf * wps_build_wsc_nack(struct wps_data *wps) return msg; } + + +#ifdef CONFIG_WPS_NFC +struct wpabuf * wps_nfc_token_gen(int ndef, int *id, struct wpabuf **pubkey, + struct wpabuf **privkey, + struct wpabuf **dev_pw) +{ + struct wpabuf *priv = NULL, *pub = NULL, *pw, *ret; + void *dh_ctx; + u16 val; + + pw = wpabuf_alloc(WPS_OOB_DEVICE_PASSWORD_LEN); + if (pw == NULL) + return NULL; + + if (random_get_bytes(wpabuf_put(pw, WPS_OOB_DEVICE_PASSWORD_LEN), + WPS_OOB_DEVICE_PASSWORD_LEN) || + random_get_bytes((u8 *) &val, sizeof(val))) { + wpabuf_free(pw); + return NULL; + } + + dh_ctx = dh5_init(&priv, &pub); + if (dh_ctx == NULL) { + wpabuf_free(pw); + return NULL; + } + dh5_free(dh_ctx); + + *id = 0x10 + val % 0xfff0; + wpabuf_free(*pubkey); + *pubkey = pub; + wpabuf_free(*privkey); + *privkey = priv; + wpabuf_free(*dev_pw); + *dev_pw = pw; + + ret = wps_build_nfc_pw_token(*id, *pubkey, *dev_pw); + if (ndef && ret) { + struct wpabuf *tmp; + tmp = ndef_build_wifi(ret); + wpabuf_free(ret); + if (tmp == NULL) + return NULL; + ret = tmp; + } + + return ret; +} +#endif /* CONFIG_WPS_NFC */ diff --git a/wpa_supplicant/wps_supplicant.c b/wpa_supplicant/wps_supplicant.c index 4314bb077..7356d1a7a 100644 --- a/wpa_supplicant/wps_supplicant.c +++ b/wpa_supplicant/wps_supplicant.c @@ -1796,50 +1796,10 @@ void wpas_wps_update_config(struct wpa_supplicant *wpa_s) struct wpabuf * wpas_wps_nfc_token(struct wpa_supplicant *wpa_s, int ndef) { - struct wpabuf *priv = NULL, *pub = NULL, *pw; - void *dh_ctx; - struct wpabuf *ret; - u16 val; - - pw = wpabuf_alloc(WPS_OOB_DEVICE_PASSWORD_LEN); - if (pw == NULL) - return NULL; - - if (random_get_bytes(wpabuf_put(pw, WPS_OOB_DEVICE_PASSWORD_LEN), - WPS_OOB_DEVICE_PASSWORD_LEN) || - random_get_bytes((u8 *) &val, sizeof(val))) { - wpabuf_free(pw); - return NULL; - } - - dh_ctx = dh5_init(&priv, &pub); - if (dh_ctx == NULL) { - wpabuf_free(pw); - return NULL; - } - dh5_free(dh_ctx); - - wpa_s->conf->wps_nfc_dev_pw_id = 0x10 + val % 0xfff0; - wpabuf_free(wpa_s->conf->wps_nfc_dh_pubkey); - wpa_s->conf->wps_nfc_dh_pubkey = pub; - wpabuf_free(wpa_s->conf->wps_nfc_dh_privkey); - wpa_s->conf->wps_nfc_dh_privkey = priv; - wpabuf_free(wpa_s->conf->wps_nfc_dev_pw); - wpa_s->conf->wps_nfc_dev_pw = pw; - - ret = wps_build_nfc_pw_token(wpa_s->conf->wps_nfc_dev_pw_id, - wpa_s->conf->wps_nfc_dh_pubkey, - wpa_s->conf->wps_nfc_dev_pw); - if (ndef && ret) { - struct wpabuf *tmp; - tmp = ndef_build_wifi(ret); - wpabuf_free(ret); - if (tmp == NULL) - return NULL; - ret = tmp; - } - - return ret; + return wps_nfc_token_gen(ndef, &wpa_s->conf->wps_nfc_dev_pw_id, + &wpa_s->conf->wps_nfc_dh_pubkey, + &wpa_s->conf->wps_nfc_dh_privkey, + &wpa_s->conf->wps_nfc_dev_pw); }