Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / net / 80211 / wpa_psk.c
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
57         len = fetch_string_setting ( netdev_settings ( dev->netdev ),
58                                      &net80211_key_setting, passphrase,
59                                      64 + 1 );
60
61         if ( len <= 0 ) {
62                 DBGC ( ctx, "WPA-PSK %p: no passphrase provided!\n", ctx );
63                 net80211_deauthenticate ( dev, -EACCES );
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  */
103 static int wpa_psk_no_change_key ( struct net80211_device *dev __unused )
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 = {
120         .protocol = NET80211_SECPROT_PSK,
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 };