PageRenderTime 86ms CodeModel.GetById 26ms RepoModel.GetById 7ms app.codeStats 0ms

/frameworks/base/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/AccessPointParserHelper.java

https://gitlab.com/brian0218/rk3066_r-box_android4.2.2_sdk
Java | 366 lines | 302 code | 17 blank | 47 comment | 71 complexity | afcc147c1bc762fbdadca9b10d862a19 MD5 | raw file
  1. /*
  2. * Copyright (C) 2010, 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. package com.android.connectivitymanagertest;
  17. import javax.xml.parsers.SAXParser;
  18. import javax.xml.parsers.SAXParserFactory;
  19. import org.xml.sax.Attributes;
  20. import org.xml.sax.SAXException;
  21. import org.xml.sax.helpers.DefaultHandler;
  22. import android.net.wifi.WifiConfiguration;
  23. import android.net.wifi.WifiConfiguration.AuthAlgorithm;
  24. import android.net.wifi.WifiConfiguration.IpAssignment;
  25. import android.net.wifi.WifiConfiguration.KeyMgmt;
  26. import android.net.wifi.WifiConfiguration.ProxySettings;
  27. import android.net.LinkAddress;
  28. import android.net.LinkProperties;
  29. import android.net.RouteInfo;
  30. import android.util.Log;
  31. import java.io.InputStream;
  32. import java.net.InetAddress;
  33. import java.net.UnknownHostException;
  34. import java.util.ArrayList;
  35. import java.util.HashMap;
  36. import java.util.List;
  37. /**
  38. * Help class to process configurations of access points saved in an XML file.
  39. * The configurations of an access point is included in tag
  40. * <accesspoint></accesspoint>. The supported configuration includes: ssid,
  41. * security, eap, phase2, identity, password, anonymousidentity, cacert, usercert,
  42. * in which each is included in the corresponding tags. Static IP setting is also supported.
  43. * Tags that can be used include: ip, gateway, networkprefixlength, dns1, dns2. All access points
  44. * have to be enclosed in tags of <resources></resources>.
  45. *
  46. * The following is a sample configuration file for an access point using EAP-PEAP with MSCHAP2.
  47. * <resources>
  48. * <accesspoint>
  49. * <ssid>testnet</ssid>
  50. * <security>EAP</security>
  51. * <eap>PEAP</eap>
  52. * <phase2>MSCHAP2</phase2>
  53. * <identity>donut</identity</identity>
  54. * <password>abcdefgh</password>
  55. * </accesspoint>
  56. * </resources>
  57. *
  58. * Note:ssid and security have to be the first two tags
  59. * for static ip setting, tag "ip" should be listed before other fields: dns, gateway,
  60. * networkprefixlength.
  61. */
  62. public class AccessPointParserHelper {
  63. private static final String KEYSTORE_SPACE = "keystore://";
  64. private static final String TAG = "AccessPointParserHelper";
  65. static final int NONE = 0;
  66. static final int WEP = 1;
  67. static final int PSK = 2;
  68. static final int EAP = 3;
  69. List<WifiConfiguration> networks = new ArrayList<WifiConfiguration>();
  70. private int getSecurityType (String security) {
  71. if (security.equalsIgnoreCase("NONE")) {
  72. return NONE;
  73. } else if (security.equalsIgnoreCase("WEP")) {
  74. return WEP;
  75. } else if (security.equalsIgnoreCase("PSK")) {
  76. return PSK;
  77. } else if (security.equalsIgnoreCase("EAP")) {
  78. return EAP;
  79. } else {
  80. return -1;
  81. }
  82. }
  83. private boolean validateEapValue(String value) {
  84. if (value.equalsIgnoreCase("PEAP") ||
  85. value.equalsIgnoreCase("TLS") ||
  86. value.equalsIgnoreCase("TTLS")) {
  87. return true;
  88. } else {
  89. return false;
  90. }
  91. }
  92. DefaultHandler mHandler = new DefaultHandler() {
  93. boolean ssid = false;
  94. boolean security = false;
  95. boolean password = false;
  96. boolean ip = false;
  97. boolean gateway = false;
  98. boolean networkprefix = false;
  99. boolean dns1 = false;
  100. boolean dns2 = false;
  101. boolean eap = false;
  102. boolean phase2 = false;
  103. boolean identity = false;
  104. boolean anonymousidentity = false;
  105. boolean cacert = false;
  106. boolean usercert = false;
  107. WifiConfiguration config = null;
  108. int securityType = NONE;
  109. LinkProperties mLinkProperties = null;
  110. InetAddress mInetAddr = null;
  111. @Override
  112. public void startElement(String uri, String localName, String tagName,
  113. Attributes attributes) throws SAXException {
  114. if (tagName.equalsIgnoreCase("accesspoint")) {
  115. config = new WifiConfiguration();
  116. }
  117. if (tagName.equalsIgnoreCase("ssid")) {
  118. ssid = true;
  119. }
  120. if (tagName.equalsIgnoreCase("security")) {
  121. security = true;
  122. }
  123. if (tagName.equalsIgnoreCase("password")) {
  124. password = true;
  125. }
  126. if (tagName.equalsIgnoreCase("eap")) {
  127. eap = true;
  128. }
  129. if (tagName.equalsIgnoreCase("phase2")) {
  130. phase2 = true;
  131. }
  132. if (tagName.equalsIgnoreCase("identity")) {
  133. identity = true;
  134. }
  135. if (tagName.equalsIgnoreCase("anonymousidentity")) {
  136. anonymousidentity = true;
  137. }
  138. if (tagName.equalsIgnoreCase("cacert")) {
  139. cacert = true;
  140. }
  141. if (tagName.equalsIgnoreCase("usercert")) {
  142. usercert = true;
  143. }
  144. if (tagName.equalsIgnoreCase("ip")) {
  145. mLinkProperties = new LinkProperties();
  146. ip = true;
  147. }
  148. if (tagName.equalsIgnoreCase("gateway")) {
  149. gateway = true;
  150. }
  151. if (tagName.equalsIgnoreCase("networkprefixlength")) {
  152. networkprefix = true;
  153. }
  154. if (tagName.equalsIgnoreCase("dns1")) {
  155. dns1 = true;
  156. }
  157. if (tagName.equalsIgnoreCase("dns2")) {
  158. dns2 = true;
  159. }
  160. }
  161. @Override
  162. public void endElement(String uri, String localName, String tagName) throws SAXException {
  163. if (tagName.equalsIgnoreCase("accesspoint")) {
  164. if (mLinkProperties != null) {
  165. config.ipAssignment = IpAssignment.STATIC;
  166. config.linkProperties = mLinkProperties;
  167. } else {
  168. config.ipAssignment = IpAssignment.DHCP;
  169. }
  170. config.proxySettings = ProxySettings.NONE;
  171. networks.add(config);
  172. mLinkProperties = null;
  173. }
  174. }
  175. @Override
  176. public void characters(char ch[], int start, int length) throws SAXException {
  177. if (ssid) {
  178. config.SSID = new String(ch, start, length);
  179. ssid = false;
  180. }
  181. if (security) {
  182. String securityStr = (new String(ch, start, length)).toUpperCase();
  183. securityType = getSecurityType(securityStr);
  184. switch (securityType) {
  185. case NONE:
  186. config.allowedKeyManagement.set(KeyMgmt.NONE);
  187. break;
  188. case WEP:
  189. config.allowedKeyManagement.set(KeyMgmt.NONE);
  190. config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
  191. config.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED);
  192. break;
  193. case PSK:
  194. config.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
  195. break;
  196. case EAP:
  197. config.allowedKeyManagement.set(KeyMgmt.WPA_EAP);
  198. config.allowedKeyManagement.set(KeyMgmt.IEEE8021X);
  199. // Initialize other fields.
  200. config.phase2.setValue("");
  201. config.ca_cert.setValue("");
  202. config.client_cert.setValue("");
  203. config.engine.setValue("");
  204. config.engine_id.setValue("");
  205. config.key_id.setValue("");
  206. config.identity.setValue("");
  207. config.anonymous_identity.setValue("");
  208. break;
  209. default:
  210. throw new SAXException();
  211. }
  212. security = false;
  213. }
  214. if (password) {
  215. String passwordStr = new String(ch, start, length);
  216. int len = passwordStr.length();
  217. if (len == 0) {
  218. throw new SAXException();
  219. }
  220. if (securityType == WEP) {
  221. if ((len == 10 || len == 26 || len == 58) &&
  222. passwordStr.matches("[0-9A-Fa-f]*")) {
  223. config.wepKeys[0] = passwordStr;
  224. } else {
  225. config.wepKeys[0] = '"' + passwordStr + '"';
  226. }
  227. } else if (securityType == PSK) {
  228. if (passwordStr.matches("[0-9A-Fa-f]{64}")) {
  229. config.preSharedKey = passwordStr;
  230. } else {
  231. config.preSharedKey = '"' + passwordStr + '"';
  232. }
  233. } else if (securityType == EAP) {
  234. config.password.setValue(passwordStr);
  235. } else {
  236. throw new SAXException();
  237. }
  238. password = false;
  239. }
  240. if (eap) {
  241. String eapValue = new String(ch, start, length);
  242. if (!validateEapValue(eapValue)) {
  243. throw new SAXException();
  244. }
  245. config.eap.setValue(eapValue);
  246. eap = false;
  247. }
  248. if (phase2) {
  249. String phase2Value = new String(ch, start, length);
  250. config.phase2.setValue("auth=" + phase2Value);
  251. phase2 = false;
  252. }
  253. if (identity) {
  254. String identityValue = new String(ch, start, length);
  255. config.identity.setValue(identityValue);
  256. identity = false;
  257. }
  258. if (anonymousidentity) {
  259. String anonyId = new String(ch, start, length);
  260. config.anonymous_identity.setValue(anonyId);
  261. anonymousidentity = false;
  262. }
  263. if (cacert) {
  264. String cacertValue = new String(ch, start, length);
  265. // need to install the credentail to "keystore://"
  266. config.ca_cert.setValue(KEYSTORE_SPACE);
  267. cacert = false;
  268. }
  269. if (usercert) {
  270. String usercertValue = new String(ch, start, length);
  271. config.client_cert.setValue(KEYSTORE_SPACE);
  272. usercert = false;
  273. }
  274. if (ip) {
  275. try {
  276. String ipAddr = new String(ch, start, length);
  277. if (!InetAddress.isNumeric(ipAddr)) {
  278. throw new SAXException();
  279. }
  280. mInetAddr = InetAddress.getByName(ipAddr);
  281. } catch (UnknownHostException e) {
  282. throw new SAXException();
  283. }
  284. ip = false;
  285. }
  286. if (gateway) {
  287. try {
  288. String gwAddr = new String(ch, start, length);
  289. if (!InetAddress.isNumeric(gwAddr)) {
  290. throw new SAXException();
  291. }
  292. mLinkProperties.addRoute(new RouteInfo(InetAddress.getByName(gwAddr)));
  293. } catch (UnknownHostException e) {
  294. throw new SAXException();
  295. }
  296. gateway = false;
  297. }
  298. if (networkprefix) {
  299. try {
  300. int nwPrefixLength = Integer.parseInt(new String(ch, start, length));
  301. if ((nwPrefixLength < 0) || (nwPrefixLength > 32)) {
  302. throw new SAXException();
  303. }
  304. mLinkProperties.addLinkAddress(new LinkAddress(mInetAddr, nwPrefixLength));
  305. } catch (NumberFormatException e) {
  306. throw new SAXException();
  307. }
  308. networkprefix = false;
  309. }
  310. if (dns1) {
  311. try {
  312. String dnsAddr = new String(ch, start, length);
  313. if (!InetAddress.isNumeric(dnsAddr)) {
  314. throw new SAXException();
  315. }
  316. mLinkProperties.addDns(InetAddress.getByName(dnsAddr));
  317. } catch (UnknownHostException e) {
  318. throw new SAXException();
  319. }
  320. dns1 = false;
  321. }
  322. if (dns2) {
  323. try {
  324. String dnsAddr = new String(ch, start, length);
  325. if (!InetAddress.isNumeric(dnsAddr)) {
  326. throw new SAXException();
  327. }
  328. mLinkProperties.addDns(InetAddress.getByName(dnsAddr));
  329. } catch (UnknownHostException e) {
  330. throw new SAXException();
  331. }
  332. dns2 = false;
  333. }
  334. }
  335. };
  336. /**
  337. * Process the InputStream in
  338. * @param in is the InputStream that can be used for XML parsing
  339. * @throws Exception
  340. */
  341. public AccessPointParserHelper(InputStream in) throws Exception {
  342. SAXParserFactory factory = SAXParserFactory.newInstance();
  343. SAXParser saxParser = factory.newSAXParser();
  344. saxParser.parse(in, mHandler);
  345. }
  346. public List<WifiConfiguration> getNetworkConfigurations() throws Exception {
  347. return networks;
  348. }
  349. }