iPXE
wpa_psk.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009 Joshua Oreman <oremanj@rwcr.net>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19 
20 FILE_LICENCE ( GPL2_OR_LATER );
21 
22 #include <string.h>
23 #include <ipxe/net80211.h>
24 #include <ipxe/sha1.h>
25 #include <ipxe/wpa.h>
26 #include <errno.h>
27 
28 /** @file
29  *
30  * Frontend for WPA using a pre-shared key.
31  */
32 
33 /**
34  * Initialise WPA-PSK state
35  *
36  * @v dev 802.11 device
37  * @ret rc Return status code
38  */
39 static int wpa_psk_init ( struct net80211_device *dev )
40 {
41  return wpa_make_rsn_ie ( dev, &dev->rsn_ie );
42 }
43 
44 /**
45  * Start WPA-PSK authentication
46  *
47  * @v dev 802.11 device
48  * @ret rc Return status code
49  */
50 static int wpa_psk_start ( struct net80211_device *dev )
51 {
52  char passphrase[64+1];
53  u8 pmk[WPA_PMK_LEN];
54  int len;
55  struct wpa_common_ctx *ctx = dev->handshaker->priv;
56 
58  &net80211_key_setting, passphrase,
59  64 + 1 );
60 
61  if ( len <= 0 ) {
62  DBGC ( ctx, "WPA-PSK %p: no passphrase provided!\n", ctx );
64  return -EACCES;
65  }
66 
67  pbkdf2_sha1 ( passphrase, len, dev->essid, strlen ( dev->essid ),
68  4096, pmk, WPA_PMK_LEN );
69 
70  DBGC ( ctx, "WPA-PSK %p: derived PMK from passphrase `%s':\n", ctx,
71  passphrase );
72  DBGC_HD ( ctx, pmk, WPA_PMK_LEN );
73 
74  return wpa_start ( dev, ctx, pmk, WPA_PMK_LEN );
75 }
76 
77 /**
78  * Step WPA-PSK authentication
79  *
80  * @v dev 802.11 device
81  * @ret rc Return status code
82  */
83 static int wpa_psk_step ( struct net80211_device *dev )
84 {
85  struct wpa_common_ctx *ctx = dev->handshaker->priv;
86 
87  switch ( ctx->state ) {
88  case WPA_SUCCESS:
89  return 1;
90  case WPA_FAILURE:
91  return -EACCES;
92  default:
93  return 0;
94  }
95 }
96 
97 /**
98  * Do-nothing function; you can't change a WPA key post-authentication
99  *
100  * @v dev 802.11 device
101  * @ret rc Return status code
102  */
104 {
105  return 0;
106 }
107 
108 /**
109  * Disable handling of received WPA authentication frames
110  *
111  * @v dev 802.11 device
112  */
113 static void wpa_psk_stop ( struct net80211_device *dev )
114 {
115  wpa_stop ( dev );
116 }
117 
118 /** WPA-PSK security handshaker */
119 struct net80211_handshaker wpa_psk_handshaker __net80211_handshaker = {
121  .init = wpa_psk_init,
122  .start = wpa_psk_start,
123  .step = wpa_psk_step,
124  .change_key = wpa_psk_no_change_key,
125  .stop = wpa_psk_stop,
126  .priv_len = sizeof ( struct wpa_common_ctx ),
127 };
struct net80211_device * dev
802.11 device we are authenticating for
Definition: wpa.h:294
static int wpa_psk_step(struct net80211_device *dev)
Step WPA-PSK authentication.
Definition: wpa_psk.c:83
Error codes.
static int wpa_psk_start(struct net80211_device *dev)
Start WPA-PSK authentication.
Definition: wpa_psk.c:50
#define DBGC(...)
Definition: compiler.h:505
union ieee80211_ie * rsn_ie
RSN or WPA information element to include with association.
Definition: net80211.h:932
#define EACCES
Permission denied.
Definition: errno.h:298
FILE_LICENCE(GPL2_OR_LATER)
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
void net80211_deauthenticate(struct net80211_device *dev, int rc)
Deauthenticate from current network and try again.
Definition: net80211.c:2390
static struct settings * netdev_settings(struct net_device *netdev)
Get per-netdevice configuration settings block.
Definition: netdevice.h:583
u8 pmk[WPA_PMK_LEN]
The Pairwise Master Key to use in handshaking.
Definition: wpa.h:303
char essid[IEEE80211_MAX_SSID_LEN+1]
SSID of the access point we are or will be associated with.
Definition: net80211.h:962
#define WPA_PMK_LEN
Usual length of a Pairwise Master Key.
Definition: wpa.h:222
void * priv
Pointer to private data.
Definition: net80211.h:672
Common definitions for all types of WPA-protected networks.
Pre-shared key handshaking.
Definition: net80211.h:112
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
4-Way Handshake succeeded
Definition: wpa.h:187
enum net80211_security_proto protocol
The security handshaking protocol implemented.
Definition: net80211.h:567
static void wpa_psk_stop(struct net80211_device *dev)
Disable handling of received WPA authentication frames.
Definition: wpa_psk.c:113
static int wpa_psk_init(struct net80211_device *dev)
Initialise WPA-PSK state.
Definition: wpa_psk.c:39
Common context for WPA security handshaking.
Definition: wpa.h:291
int fetch_string_setting(struct settings *settings, const struct setting *setting, char *data, size_t len)
Fetch value of string setting.
Definition: settings.c:841
void pbkdf2_sha1(const void *passphrase, size_t pass_len, const void *salt, size_t salt_len, int iterations, void *key, size_t key_len)
PBKDF2 key derivation function using SHA1.
Definition: sha1extra.c:148
int wpa_make_rsn_ie(struct net80211_device *dev, union ieee80211_ie **ie_ret)
Construct RSN or WPA information element.
Definition: wpa.c:124
The iPXE 802.11 MAC layer.
Structure encapsulating the complete state of an 802.11 device.
Definition: net80211.h:786
#define DBGC_HD(...)
Definition: compiler.h:507
void wpa_stop(struct net80211_device *dev)
Disable handling of received WPA handshake frames.
Definition: wpa.c:260
size_t strlen(const char *src)
Get length of string.
Definition: string.c:243
struct net80211_handshaker * handshaker
Security handshaker being used.
Definition: net80211.h:879
int wpa_start(struct net80211_device *dev, struct wpa_common_ctx *ctx, const void *pmk, size_t pmk_len)
Set up generic WPA support to handle 4-Way Handshake.
Definition: wpa.c:216
Interface to an 802.11 security handshaking protocol.
Definition: net80211.h:564
struct net_device * netdev
The net_device that wraps us.
Definition: net80211.h:789
static int wpa_psk_no_change_key(struct net80211_device *dev __unused)
Do-nothing function; you can't change a WPA key post-authentication.
Definition: wpa_psk.c:103
SHA-1 algorithm.
struct net80211_handshaker wpa_psk_handshaker __net80211_handshaker
WPA-PSK security handshaker.
Definition: wpa_psk.c:119
uint32_t len
Length.
Definition: ena.h:14
String functions.
uint8_t u8
Definition: stdint.h:19
4-Way Handshake failed
Definition: wpa.h:190