1 /* This file is part of the KDE project
2 Copyright (C) 2007 Will Stephenson <wstephenson@kde.org>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
20 #include "NetworkManager-dbushelper.h"
23 //#include <wireless.h>
24 // stuff copied from wireless.h so we don't have to include it.
25 // take aim at foot, prepare to fire
26 #define IW_AUTH_ALG_OPEN_SYSTEM 0x00000001
27 #define IW_AUTH_ALG_SHARED_KEY 0x00000002
28 #define IW_AUTH_WPA_VERSION_WPA 0x00000002
29 #define IW_AUTH_WPA_VERSION_WPA2 0x00000004
30 #define IW_AUTH_KEY_MGMT_802_1X 1
31 #define IW_AUTH_KEY_MGMT_PSK 2
32 #define IW_AUTH_CIPHER_NONE 0x00000001
34 #include <NetworkManager/NetworkManager.h>
35 #include <NetworkManager/cipher.h>
36 #include <NetworkManager/cipher-wep-ascii.h>
37 #include <NetworkManager/cipher-wep-hex.h>
38 #include <NetworkManager/cipher-wep-passphrase.h>
39 #include <NetworkManager/cipher-wpa-psk-hex.h>
40 #include <NetworkManager/cipher-wpa-psk-passphrase.h>
44 #include <solid/control/ifaces/authentication.h>
46 QList
<QVariant
> NMDBusHelper::serialize(Solid::Control::Authentication
* auth
, const QString
& essid
, QList
<QVariant
> & args
, bool * error
)
50 Solid::Control::AuthenticationNone
* none
= dynamic_cast<Solid::Control::AuthenticationNone
*>(auth
) ;
52 return doSerialize(none
, essid
, args
, error
);
53 Solid::Control::AuthenticationWep
* wep
= dynamic_cast<Solid::Control::AuthenticationWep
*>(auth
) ;
55 return doSerialize(wep
, essid
, args
, error
);
56 Solid::Control::AuthenticationWpaPersonal
* wpap
= dynamic_cast<Solid::Control::AuthenticationWpaPersonal
*>(auth
) ;
58 return doSerialize(wpap
, essid
, args
, error
);
59 Solid::Control::AuthenticationWpaEnterprise
* wpae
= dynamic_cast<Solid::Control::AuthenticationWpaEnterprise
*>(auth
);
61 return doSerialize(wpae
, essid
, args
, error
);
64 return QList
<QVariant
>();
67 QList
<QVariant
> NMDBusHelper::doSerialize(Solid::Control::AuthenticationNone
* none
, const QString
& essid
, QList
<QVariant
> & args
, bool * error
)
70 args
<< QVariant(IW_AUTH_CIPHER_NONE
);
74 QList
<QVariant
> NMDBusHelper::doSerialize(Solid::Control::AuthenticationWep
* auth
, const QString
& essid
, QList
<QVariant
> & args
, bool * error
)
77 // what's the algorithm for deciding the right cipher to use?
79 IEEE_802_11_Cipher
* cipher
= 0;
80 if (auth
->type() == Solid::Control::AuthenticationWep::WepAscii
)
82 if (auth
->keyLength() == 40 || auth
->keyLength() == 64)
83 cipher
= cipher_wep64_ascii_new();
84 else if (auth
->keyLength() == 104 || auth
->keyLength() == 128)
85 cipher
= cipher_wep128_ascii_new();
87 //NM only supports these 2 key lengths, flag an error!
90 else if (auth
->type() == Solid::Control::AuthenticationWep::WepHex
)
92 if (auth
->keyLength() == 40 || auth
->keyLength() == 64)
93 cipher
= cipher_wep64_hex_new();
94 else if (auth
->keyLength() == 104 || auth
->keyLength() == 128)
95 cipher
= cipher_wep128_hex_new();
97 //NM only supports these 2 key lengths, flag an error!
100 else if (auth
->type() == Solid::Control::AuthenticationWep::WepPassphrase
)
102 if (auth
->keyLength() == 40 || auth
->keyLength() == 64)
103 cipher
= cipher_wep64_passphrase_new();
104 else if (auth
->keyLength() == 104 || auth
->keyLength() == 128)
105 cipher
= cipher_wep128_passphrase_new();
107 //NM only supports these 2 key lengths, flag an error!
111 // unrecognised auth type, error!
116 int we_cipher
= ieee_802_11_cipher_get_we_cipher(cipher
);
117 args
<< QVariant(we_cipher
);
119 // cipher, essid, key
120 char * rawHashedKey
= 0;
121 rawHashedKey
= ieee_802_11_cipher_hash(cipher
, essid
.toUtf8(), auth
->secrets()["key"].toUtf8());
122 QString hashedKey
= QString::fromAscii(rawHashedKey
);
124 args
<< QVariant(hashedKey
);
126 if (auth
->method() == Solid::Control::AuthenticationWep::WepOpenSystem
)
127 args
<< QVariant(IW_AUTH_ALG_OPEN_SYSTEM
);
129 args
<< QVariant(IW_AUTH_ALG_SHARED_KEY
);
132 kDebug(1441) << "FIXME: delete cipher object";
137 QList
<QVariant
> NMDBusHelper::doSerialize(Solid::Control::AuthenticationWpaPersonal
* auth
, const QString
& essid
, QList
<QVariant
> & args
, bool * error
)
140 IEEE_802_11_Cipher
* cipher
= 0;
141 IEEE_802_11_Cipher
* hexCipher
= 0;
142 IEEE_802_11_Cipher
* ppCipher
= 0;
143 hexCipher
= cipher_wpa_psk_hex_new();
144 ppCipher
= cipher_wpa_psk_passphrase_new();
145 QString rawKey
= auth
->secrets()["key"];
147 // cipher identification algorithm
148 // can be either hex or passphrase
149 // we try both methods
151 // cipher needs the cipher setting on it
152 // which is the protocol
154 switch (auth
->protocol())
156 case Solid::Control::AuthenticationWpaPersonal::WpaTkip
:
157 cipher_wpa_psk_hex_set_we_cipher(hexCipher
, NM_AUTH_TYPE_WPA_PSK_TKIP
);
158 cipher_wpa_psk_passphrase_set_we_cipher(ppCipher
, NM_AUTH_TYPE_WPA_PSK_TKIP
);
160 case Solid::Control::AuthenticationWpaPersonal::WpaCcmpAes
:
161 cipher_wpa_psk_hex_set_we_cipher(hexCipher
, NM_AUTH_TYPE_WPA_PSK_CCMP
);
162 cipher_wpa_psk_passphrase_set_we_cipher(ppCipher
, NM_AUTH_TYPE_WPA_PSK_CCMP
);
164 case Solid::Control::AuthenticationWpaPersonal::WpaAuto
:
166 cipher_wpa_psk_hex_set_we_cipher(hexCipher
, NM_AUTH_TYPE_WPA_PSK_AUTO
);
167 cipher_wpa_psk_passphrase_set_we_cipher(ppCipher
, NM_AUTH_TYPE_WPA_PSK_AUTO
);
170 // now try both ciphers on the raw key
171 if (ieee_802_11_cipher_validate(hexCipher
, essid
.toUtf8(), rawKey
.toUtf8()) == 0)
176 else if (ieee_802_11_cipher_validate(ppCipher
, essid
.toUtf8(), rawKey
.toUtf8()) == 0)
187 int we_cipher
= ieee_802_11_cipher_get_we_cipher(cipher
);
188 args
<< QVariant(we_cipher
);
190 char * rawHashedKey
= 0;
191 rawHashedKey
= ieee_802_11_cipher_hash(cipher
, essid
.toUtf8(), rawKey
.toUtf8());
192 QString hashedKey
= QString::fromAscii(rawHashedKey
);
194 args
<< QVariant(hashedKey
);
196 if (auth
->version() == Solid::Control::AuthenticationWpaPersonal::Wpa1
)
197 args
<< QVariant(IW_AUTH_WPA_VERSION_WPA
);
199 args
<< QVariant(IW_AUTH_WPA_VERSION_WPA2
);
200 // int32 key management
201 if (auth
->keyManagement() == Solid::Control::AuthenticationWpaPersonal::WpaPsk
)
202 args
<< QVariant(IW_AUTH_KEY_MGMT_PSK
);
204 args
<< QVariant(IW_AUTH_KEY_MGMT_802_1X
);
205 kDebug(1411) << "Outbound args are: " << args
;
210 QList
<QVariant
> NMDBusHelper::doSerialize(Solid::Control::AuthenticationWpaEnterprise
* auth
, const QString
& essid
, QList
<QVariant
> & args
, bool * error
)
214 kDebug() << "Implement me!";
215 // int32 cipher, always NM_AUTH_TYPE_WPA_EAP
216 args
<< NM_AUTH_TYPE_WPA_EAP
;
217 switch (auth
->method())
219 case Solid::Control::AuthenticationWpaEnterprise::EapPeap
:
220 args
<< NM_EAP_METHOD_PEAP
;
222 case Solid::Control::AuthenticationWpaEnterprise::EapTls
:
223 args
<< NM_EAP_METHOD_TLS
;
225 case Solid::Control::AuthenticationWpaEnterprise::EapTtls
:
226 args
<< NM_EAP_METHOD_TTLS
;
228 case Solid::Control::AuthenticationWpaEnterprise::EapMd5
:
229 args
<< NM_EAP_METHOD_MD5
;
231 case Solid::Control::AuthenticationWpaEnterprise::EapMsChap
:
232 args
<< NM_EAP_METHOD_MSCHAP
;
234 case Solid::Control::AuthenticationWpaEnterprise::EapOtp
:
235 args
<< NM_EAP_METHOD_OTP
;
237 case Solid::Control::AuthenticationWpaEnterprise::EapGtc
:
238 args
<< NM_EAP_METHOD_GTC
;
242 args
<< NM_AUTH_TYPE_WPA_PSK_AUTO
;
244 args
<< auth
->identity();
246 args
<< auth
->idPasswordKey();
248 args
<< auth
->anonIdentity();
249 // s priv key password
250 args
<< auth
->certPrivatePasswordKey();
252 args
<< auth
->certPrivate();
253 // s client cert file
254 args
<< auth
->certClient();
256 args
<< auth
->certCA();
257 // int32 wpa version => {IW_AUTH_WPA_VERSION_WPA,IW_AUTH_WPA_VERSION_WPA2}
258 args
<< auth
->version();
259 return QList
<QVariant
>();