WifiConfiguration.java revision 5069cc78497209c035a7019b2f407bd1ed57f64a
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.net.wifi;
18
19import android.os.Parcelable;
20import android.os.Parcel;
21
22import java.util.BitSet;
23
24/**
25 * A class representing a configured Wi-Fi network, including the
26 * security configuration. Android will not necessarily support
27 * all of these security schemes initially.
28 */
29public class WifiConfiguration implements Parcelable {
30
31    /** {@hide} */
32    public static final String ssidVarName = "ssid";
33    /** {@hide} */
34    public static final String bssidVarName = "bssid";
35    /** {@hide} */
36    public static final String pskVarName = "psk";
37    /** {@hide} */
38    public static final String[] wepKeyVarNames = { "wep_key0", "wep_key1", "wep_key2", "wep_key3" };
39    /** {@hide} */
40    public static final String wepTxKeyIdxVarName = "wep_tx_keyidx";
41    /** {@hide} */
42    public static final String priorityVarName = "priority";
43    /** {@hide} */
44    public static final String hiddenSSIDVarName = "scan_ssid";
45    /** {@hide} */
46    public static final String eapVarName = "eap";
47    /** {@hide} */
48    public static final String identityVarName = "identity";
49    /** {@hide} */
50    public static final String anonymousIdentityVarName = "anonymous_identity";
51    /** {@hide} */
52    public static final String clientCertVarName = "client_cert";
53    /** {@hide} */
54    public static final String caCertVarName = "ca_cert";
55    /** {@hide} */
56    public static final String privateKeyVarName = "private_key";
57    /** {@hide} */
58    public static final String privateKeyPasswdVarName = "private_key_passwd";
59
60    /**
61     * Recognized key management schemes.
62     */
63    public static class KeyMgmt {
64        private KeyMgmt() { }
65
66        /** WPA is not used; plaintext or static WEP could be used. */
67        public static final int NONE = 0;
68        /** WPA pre-shared key (requires {@code preSharedKey} to be specified). */
69        public static final int WPA_PSK = 1;
70        /** WPA using EAP authentication. Generally used with an external authentication server. */
71        public static final int WPA_EAP = 2;
72        /** IEEE 802.1X using EAP authentication and (optionally) dynamically
73         * generated WEP keys. */
74        public static final int IEEE8021X = 3;
75
76        public static final String varName = "key_mgmt";
77
78        public static final String[] strings = { "NONE", "WPA_PSK", "WPA_EAP", "IEEE8021X" };
79    }
80
81    /**
82     * Recognized security protocols.
83     */
84    public static class Protocol {
85        private Protocol() { }
86
87        /** WPA/IEEE 802.11i/D3.0 */
88        public static final int WPA = 0;
89        /** WPA2/IEEE 802.11i */
90        public static final int RSN = 1;
91
92        public static final String varName = "proto";
93
94        public static final String[] strings = { "WPA", "RSN" };
95    }
96
97    /**
98     * Recognized IEEE 802.11 authentication algorithms.
99     */
100    public static class AuthAlgorithm {
101        private AuthAlgorithm() { }
102
103        /** Open System authentication (required for WPA/WPA2) */
104        public static final int OPEN = 0;
105        /** Shared Key authentication (requires static WEP keys) */
106        public static final int SHARED = 1;
107        /** LEAP/Network EAP (only used with LEAP) */
108        public static final int LEAP = 2;
109
110        public static final String varName = "auth_alg";
111
112        public static final String[] strings = { "OPEN", "SHARED", "LEAP" };
113    }
114
115    /**
116     * Recognized pairwise ciphers for WPA.
117     */
118    public static class PairwiseCipher {
119        private PairwiseCipher() { }
120
121        /** Use only Group keys (deprecated) */
122        public static final int NONE = 0;
123        /** Temporal Key Integrity Protocol [IEEE 802.11i/D7.0] */
124        public static final int TKIP = 1;
125        /** AES in Counter mode with CBC-MAC [RFC 3610, IEEE 802.11i/D7.0] */
126        public static final int CCMP = 2;
127
128        public static final String varName = "pairwise";
129
130        public static final String[] strings = { "NONE", "TKIP", "CCMP" };
131    }
132
133    /**
134     * Recognized group ciphers.
135     * <pre>
136     * CCMP = AES in Counter mode with CBC-MAC [RFC 3610, IEEE 802.11i/D7.0]
137     * TKIP = Temporal Key Integrity Protocol [IEEE 802.11i/D7.0]
138     * WEP104 = WEP (Wired Equivalent Privacy) with 104-bit key
139     * WEP40 = WEP (Wired Equivalent Privacy) with 40-bit key (original 802.11)
140     * </pre>
141     */
142    public static class GroupCipher {
143        private GroupCipher() { }
144
145        /** WEP40 = WEP (Wired Equivalent Privacy) with 40-bit key (original 802.11) */
146        public static final int WEP40 = 0;
147        /** WEP104 = WEP (Wired Equivalent Privacy) with 104-bit key */
148        public static final int WEP104 = 1;
149        /** Temporal Key Integrity Protocol [IEEE 802.11i/D7.0] */
150        public static final int TKIP = 2;
151        /** AES in Counter mode with CBC-MAC [RFC 3610, IEEE 802.11i/D7.0] */
152        public static final int CCMP = 3;
153
154        public static final String varName = "group";
155
156        public static final String[] strings = { "WEP40", "WEP104", "TKIP", "CCMP" };
157    }
158
159    /** Possible status of a network configuration. */
160    public static class Status {
161        private Status() { }
162
163        /** this is the network we are currently connected to */
164        public static final int CURRENT = 0;
165        /** supplicant will not attempt to use this network */
166        public static final int DISABLED = 1;
167        /** supplicant will consider this network available for association */
168        public static final int ENABLED = 2;
169
170        public static final String[] strings = { "current", "disabled", "enabled" };
171    }
172
173    /**
174     * The ID number that the supplicant uses to identify this
175     * network configuration entry. This must be passed as an argument
176     * to most calls into the supplicant.
177     */
178    public int networkId;
179
180    /**
181     * The current status of this network configuration entry.
182     * @see Status
183     */
184    public int status;
185    /**
186     * The network's SSID. Can either be an ASCII string,
187     * which must be enclosed in double quotation marks
188     * (e.g., {@code &quot;MyNetwork&quot;}, or a string of
189     * hex digits,which are not enclosed in quotes
190     * (e.g., {@code 01a243f405}).
191     */
192    public String SSID;
193    /**
194     * When set, this network configuration entry should only be used when
195     * associating with the AP having the specified BSSID. The value is
196     * a string in the format of an Ethernet MAC address, e.g.,
197     * <code>XX:XX:XX:XX:XX:XX</code> where each <code>X</code> is a hex digit.
198     */
199    public String BSSID;
200
201    /**
202     * Pre-shared key for use with WPA-PSK.
203     * <p/>
204     * When the value of this key is read, the actual key is
205     * not returned, just a "*" if the key has a value, or the null
206     * string otherwise.
207     */
208    public String preSharedKey;
209    /**
210     * Up to four WEP keys. Either an ASCII string enclosed in double
211     * quotation marks (e.g., {@code &quot;abcdef&quot;} or a string
212     * of hex digits (e.g., {@code 0102030405}).
213     * <p/>
214     * When the value of one of these keys is read, the actual key is
215     * not returned, just a "*" if the key has a value, or the null
216     * string otherwise.
217     */
218    public String[] wepKeys;
219
220    /** Default WEP key index, ranging from 0 to 3. */
221    public int wepTxKeyIndex;
222
223    /**
224     * Priority determines the preference given to a network by {@code wpa_supplicant}
225     * when choosing an access point with which to associate.
226     */
227    public int priority;
228
229    /**
230     * This is a network that does not broadcast its SSID, so an
231     * SSID-specific probe request must be used for scans.
232     */
233    public boolean hiddenSSID;
234
235    /**
236     * The set of key management protocols supported by this configuration.
237     * See {@link KeyMgmt} for descriptions of the values.
238     * Defaults to WPA-PSK WPA-EAP.
239     */
240    public BitSet allowedKeyManagement;
241    /**
242     * The set of security protocols supported by this configuration.
243     * See {@link Protocol} for descriptions of the values.
244     * Defaults to WPA RSN.
245     */
246    public BitSet allowedProtocols;
247    /**
248     * The set of authentication protocols supported by this configuration.
249     * See {@link AuthAlgorithm} for descriptions of the values.
250     * Defaults to automatic selection.
251     */
252    public BitSet allowedAuthAlgorithms;
253    /**
254     * The set of pairwise ciphers for WPA supported by this configuration.
255     * See {@link PairwiseCipher} for descriptions of the values.
256     * Defaults to CCMP TKIP.
257     */
258    public BitSet allowedPairwiseCiphers;
259    /**
260     * The set of group ciphers supported by this configuration.
261     * See {@link GroupCipher} for descriptions of the values.
262     * Defaults to CCMP TKIP WEP104 WEP40.
263     */
264    public BitSet allowedGroupCiphers;
265
266    /* The following fields are used for EAP/IEEE8021X authentication */
267
268    /**
269     * The eap mode should be PEAP, TLS or TTLS.
270     * {@hide}
271     */
272    public String eap;
273    /**
274     * The identity of the user in string,
275     * which is used for the authentication.
276     * {@hide}
277     */
278    public String identity;
279    /** {@hide} */
280    public String anonymousIdentity;
281    /** The path of the client certificate file.
282     * {@hide}
283     */
284    public String clientCert;
285    /** The path of the CA certificate file.
286     * {@hide}
287     */
288    public String caCert;
289    /** The path of the private key file.
290     * {@hide}
291     */
292    public String privateKey;
293    /** The password of the private key file if encrypted.
294     * {@hide}
295     */
296    public String privateKeyPasswd;
297
298    public WifiConfiguration() {
299        networkId = -1;
300        SSID = null;
301        BSSID = null;
302        priority = 0;
303        hiddenSSID = false;
304        allowedKeyManagement = new BitSet();
305        allowedProtocols = new BitSet();
306        allowedAuthAlgorithms = new BitSet();
307        allowedPairwiseCiphers = new BitSet();
308        allowedGroupCiphers = new BitSet();
309        wepKeys = new String[4];
310        for (int i = 0; i < wepKeys.length; i++)
311            wepKeys[i] = null;
312        eap = null;
313        identity = null;
314        anonymousIdentity = null;
315        clientCert = null;
316        caCert = null;
317        privateKey = null;
318        privateKeyPasswd = null;
319    }
320
321    public String toString() {
322        StringBuffer sbuf = new StringBuffer();
323        if (this.status == WifiConfiguration.Status.CURRENT) {
324            sbuf.append("* ");
325        } else if (this.status == WifiConfiguration.Status.DISABLED) {
326            sbuf.append("- ");
327        }
328        sbuf.append("ID: ").append(this.networkId).append(" SSID: ").append(this.SSID).
329                append(" BSSID: ").append(this.BSSID).append(" PRIO: ").append(this.priority).
330                append('\n');
331        sbuf.append(" KeyMgmt:");
332        for (int k = 0; k < this.allowedKeyManagement.size(); k++) {
333            if (this.allowedKeyManagement.get(k)) {
334                sbuf.append(" ");
335                if (k < KeyMgmt.strings.length) {
336                    sbuf.append(KeyMgmt.strings[k]);
337                } else {
338                    sbuf.append("??");
339                }
340            }
341        }
342        sbuf.append(" Protocols:");
343        for (int p = 0; p < this.allowedProtocols.size(); p++) {
344            if (this.allowedProtocols.get(p)) {
345                sbuf.append(" ");
346                if (p < Protocol.strings.length) {
347                    sbuf.append(Protocol.strings[p]);
348                } else {
349                    sbuf.append("??");
350                }
351            }
352        }
353        sbuf.append('\n');
354        sbuf.append(" AuthAlgorithms:");
355        for (int a = 0; a < this.allowedAuthAlgorithms.size(); a++) {
356            if (this.allowedAuthAlgorithms.get(a)) {
357                sbuf.append(" ");
358                if (a < AuthAlgorithm.strings.length) {
359                    sbuf.append(AuthAlgorithm.strings[a]);
360                } else {
361                    sbuf.append("??");
362                }
363            }
364        }
365        sbuf.append('\n');
366        sbuf.append(" PairwiseCiphers:");
367        for (int pc = 0; pc < this.allowedPairwiseCiphers.size(); pc++) {
368            if (this.allowedPairwiseCiphers.get(pc)) {
369                sbuf.append(" ");
370                if (pc < PairwiseCipher.strings.length) {
371                    sbuf.append(PairwiseCipher.strings[pc]);
372                } else {
373                    sbuf.append("??");
374                }
375            }
376        }
377        sbuf.append('\n');
378        sbuf.append(" GroupCiphers:");
379        for (int gc = 0; gc < this.allowedGroupCiphers.size(); gc++) {
380            if (this.allowedGroupCiphers.get(gc)) {
381                sbuf.append(" ");
382                if (gc < GroupCipher.strings.length) {
383                    sbuf.append(GroupCipher.strings[gc]);
384                } else {
385                    sbuf.append("??");
386                }
387            }
388        }
389        sbuf.append('\n').append(" PSK: ");
390        if (this.preSharedKey != null) {
391            sbuf.append('*');
392        }
393        sbuf.append('\n').append(" eap: ");
394        if (this.eap != null) {
395            sbuf.append(eap);
396        }
397        sbuf.append('\n').append(" Identity: ");
398        if (this.identity != null) {
399            sbuf.append(identity);
400        }
401        sbuf.append('\n').append(" AnonymousIdentity: ");
402        if (this.anonymousIdentity != null) {
403            sbuf.append(anonymousIdentity);
404        }
405        sbuf.append('\n').append(" ClientCert: ");
406        if (this.clientCert != null) {
407            sbuf.append(clientCert);
408        }
409        sbuf.append('\n').append(" CaCert: ");
410        if (this.caCert != null) {
411            sbuf.append(caCert);
412        }
413        sbuf.append('\n').append(" PrivateKey: ");
414        if (this.privateKey != null) {
415            sbuf.append(privateKey);
416        }
417        sbuf.append('\n').append(" PrivateKeyPasswd: ");
418        if (this.privateKeyPasswd != null) {
419            sbuf.append(privateKeyPasswd);
420        }
421        sbuf.append('\n');
422        return sbuf.toString();
423    }
424
425    /**
426     * Construct a WifiConfiguration from a scanned network
427     * @param scannedAP the scan result used to construct the config entry
428     * TODO: figure out whether this is a useful way to construct a new entry.
429     *
430    public WifiConfiguration(ScanResult scannedAP) {
431        networkId = -1;
432        SSID = scannedAP.SSID;
433        BSSID = scannedAP.BSSID;
434    }
435    */
436
437    private static BitSet readBitSet(Parcel src) {
438        int cardinality = src.readInt();
439
440        BitSet set = new BitSet();
441        for (int i = 0; i < cardinality; i++)
442            set.set(src.readInt());
443
444        return set;
445    }
446
447    private static void writeBitSet(Parcel dest, BitSet set) {
448        int nextSetBit = -1;
449
450        dest.writeInt(set.cardinality());
451
452        while ((nextSetBit = set.nextSetBit(nextSetBit + 1)) != -1)
453            dest.writeInt(nextSetBit);
454    }
455
456    /** Implement the Parcelable interface {@hide} */
457    public int describeContents() {
458        return 0;
459    }
460
461    /** Implement the Parcelable interface {@hide} */
462    public void writeToParcel(Parcel dest, int flags) {
463        dest.writeInt(networkId);
464        dest.writeInt(status);
465        dest.writeString(SSID);
466        dest.writeString(BSSID);
467        dest.writeString(preSharedKey);
468        for (String wepKey : wepKeys)
469            dest.writeString(wepKey);
470        dest.writeInt(wepTxKeyIndex);
471        dest.writeInt(priority);
472        dest.writeInt(hiddenSSID ? 1 : 0);
473
474        writeBitSet(dest, allowedKeyManagement);
475        writeBitSet(dest, allowedProtocols);
476        writeBitSet(dest, allowedAuthAlgorithms);
477        writeBitSet(dest, allowedPairwiseCiphers);
478        writeBitSet(dest, allowedGroupCiphers);
479        dest.writeString(eap);
480        dest.writeString(identity);
481        dest.writeString(anonymousIdentity);
482        dest.writeString(clientCert);
483        dest.writeString(caCert);
484        dest.writeString(privateKey);
485        dest.writeString(privateKeyPasswd);
486    }
487
488    /** Implement the Parcelable interface {@hide} */
489    public static final Creator<WifiConfiguration> CREATOR =
490        new Creator<WifiConfiguration>() {
491            public WifiConfiguration createFromParcel(Parcel in) {
492                WifiConfiguration config = new WifiConfiguration();
493                config.networkId = in.readInt();
494                config.status = in.readInt();
495                config.SSID = in.readString();
496                config.BSSID = in.readString();
497                config.preSharedKey = in.readString();
498                for (int i = 0; i < config.wepKeys.length; i++)
499                    config.wepKeys[i] = in.readString();
500                config.wepTxKeyIndex = in.readInt();
501                config.priority = in.readInt();
502                config.hiddenSSID = in.readInt() != 0;
503                config.allowedKeyManagement   = readBitSet(in);
504                config.allowedProtocols       = readBitSet(in);
505                config.allowedAuthAlgorithms  = readBitSet(in);
506                config.allowedPairwiseCiphers = readBitSet(in);
507                config.allowedGroupCiphers    = readBitSet(in);
508                config.eap = in.readString();
509                config.identity = in.readString();
510                config.anonymousIdentity = in.readString();
511                config.clientCert = in.readString();
512                config.caCert = in.readString();
513                config.privateKey = in.readString();
514                config.privateKeyPasswd = in.readString();
515                return config;
516            }
517
518            public WifiConfiguration[] newArray(int size) {
519                return new WifiConfiguration[size];
520            }
521        };
522}
523