Skip to content

Commit

Permalink
Kobo/WifiDialog: use PBKDF2-SHA1 passwords
Browse files Browse the repository at this point in the history
On older Kobos like the Kobo Mini, wpa_supplicant accepts ASCII
passwords, newer models require a PBKDF2-SHA1 digest.  This is
implemented using libcrypto (OpenSSL).
  • Loading branch information
MaxKellermann committed Jan 27, 2022
1 parent 49f087c commit 5c46201
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 3 deletions.
1 change: 1 addition & 0 deletions NEWS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Version 7.23 - not yet released
- fix broken button text background
- add OTG support (serial, sound, net) for Clara HD
- fix "Network failure" in Wifi dialog
- support Wifi on Clara HD

Version 7.22 - 2022/01/14
* user interface
Expand Down
4 changes: 3 additions & 1 deletion build/kobo.mk
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
$(eval $(call pkg-config-library,LIBCRYPTO,libcrypto))

KOBO_MENU_SOURCES = \
$(SRC)/Version.cpp \
$(SRC)/Asset.cpp \
Expand Down Expand Up @@ -37,7 +39,7 @@ KOBO_MENU_SOURCES = \
$(SRC)/Kobo/WifiDialog.cpp \
$(SRC)/Kobo/FakeSymbols.cpp \
$(SRC)/Kobo/KoboMenu.cpp
KOBO_MENU_DEPENDS = WIDGET FORM SCREEN EVENT RESOURCE ASYNC LIBNET OS IO THREAD MATH UTIL
KOBO_MENU_DEPENDS = WIDGET FORM SCREEN EVENT RESOURCE ASYNC LIBNET OS IO THREAD MATH UTIL LIBCRYPTO
KOBO_MENU_STRIP = y

$(eval $(call link-program,KoboMenu,KOBO_MENU))
Expand Down
1 change: 1 addition & 0 deletions build/libutil.mk
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ UTIL_SOURCES = \
$(UTIL_SRC_DIR)/TruncateString.cpp \
$(UTIL_SRC_DIR)/EscapeBackslash.cpp \
$(UTIL_SRC_DIR)/ExtractParameters.cpp \
$(UTIL_SRC_DIR)/HexFormat.cxx \
$(UTIL_SRC_DIR)/ConvertString.cpp \
$(UTIL_SRC_DIR)/StaticString.cxx \
$(UTIL_SRC_DIR)/StringBuilder.cxx \
Expand Down
1 change: 1 addition & 0 deletions doc/build.rst
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ The following command installs these on Debian::
libtiff5-dev libgeotiff-dev \
libc-ares-dev \
libcurl4-openssl-dev \
libssl-dev \
libc-ares-dev \
liblua5.4-dev \
libxml-parser-perl \
Expand Down
1 change: 1 addition & 0 deletions ide/provisioning/install-debian-packages.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ apt-get install ${APTOPTS[*]} make g++ \
libfreetype6-dev \
libpng-dev libjpeg-dev \
libtiff5-dev libgeotiff-dev \
libssl-dev \
libcurl4-openssl-dev \
libc-ares-dev \
liblua5.4-dev \
Expand Down
2 changes: 1 addition & 1 deletion src/Kobo/WPASupplicant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class WPASupplicant {
}

void SetNetworkPSK(unsigned id, const char *psk) {
SetNetworkString(id, "psk", psk);
SetNetworkID(id, "psk", psk);
}

void SelectNetwork(unsigned id);
Expand Down
17 changes: 16 additions & 1 deletion src/Kobo/WifiDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ Copyright_License {
#include "WPASupplicant.hpp"
#include "net/IPv4Address.hxx"
#include "ui/event/PeriodicTimer.hpp"
#include "util/HexFormat.hxx"

/* workaround because OpenSSL has a typedef called "UI", which clashes
with our "UI" namespace */
#define UI OPENSSL_UI
#include <openssl/evp.h> // for PKCS5_PBKDF2_HMAC_SHA1()
#undef UI

class WifiListWidget final
: public ListWidget {
Expand Down Expand Up @@ -216,8 +223,16 @@ WifiConnect(enum WifiSecurity security, WPASupplicant &wpa_supplicant, const cha
wpa_supplicant.SetNetworkSSID(id, ssid);

if (security == WPA_SECURITY) {
std::array<unsigned char, 32> pmk;
PKCS5_PBKDF2_HMAC_SHA1(psk, strlen(psk),
(const unsigned char *)ssid, strlen(ssid),
4096,
pmk.size(), pmk.data());

std::array<char, sizeof(pmk) * 2 + 1> hex;
*HexFormat(hex.data(), {pmk.data(), pmk.size()}) = 0;

wpa_supplicant.SetNetworkPSK(id, psk);
wpa_supplicant.SetNetworkPSK(id, hex.data());
} else if (security == WEP_SECURITY) {
wpa_supplicant.SetNetworkID(id, "key_mgmt", "NONE");

Expand Down
43 changes: 43 additions & 0 deletions src/util/HexFormat.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (C) 2018 Max Kellermann <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "HexFormat.hxx"

#include <stdio.h>

char *
HexFormat(char *dest, ConstBuffer<uint8_t> src) noexcept
{
for (auto i : src) {
sprintf(dest, "%02x", i);
dest += 2;
}

return dest;
}
64 changes: 64 additions & 0 deletions src/util/HexFormat.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (C) 2018 Max Kellermann <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef HEX_FORMAT_HXX
#define HEX_FORMAT_HXX

#include "ConstBuffer.hxx"
#include "StringBuffer.hxx"

#include <cstddef>
#include <cstdint>

/**
* Format the given byte sequence into a null-terminated hexadecimal
* string.
*
* @param dest the destination buffer; must be large enough to hold
* all hex digits plus the null terminator
* @return a pointer to the generated null terminator
*/
char *
HexFormat(char *dest, ConstBuffer<uint8_t> src) noexcept;

/**
* Like HexFormat(), but return a #StringBuffer with exactly the
* required size.
*/
template<size_t size>
[[gnu::pure]]
auto
HexFormatBuffer(const uint8_t *src) noexcept
{
StringBuffer<size * 2 + 1> dest;
HexFormat(dest.data(), {src, size});
return dest;
}

#endif

0 comments on commit 5c46201

Please sign in to comment.