1 /* 2 * Copyright (C) 2014 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.nfc; 17 18 19 import android.app.ActivityManager; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.net.wifi.WifiConfiguration; 23 import android.nfc.NdefMessage; 24 import android.nfc.NdefRecord; 25 import android.nfc.tech.Ndef; 26 import android.os.UserHandle; 27 import android.os.UserManager; 28 29 import java.nio.BufferUnderflowException; 30 import java.nio.ByteBuffer; 31 import java.util.BitSet; 32 33 public final class NfcWifiProtectedSetup { 34 35 public static final String NFC_TOKEN_MIME_TYPE = "application/vnd.wfa.wsc"; 36 37 public static final String EXTRA_WIFI_CONFIG = "com.android.nfc.WIFI_CONFIG_EXTRA"; 38 39 /* 40 * ID into configuration record for SSID and Network Key in hex. 41 * Obtained from WFA Wifi Simple Configuration Technical Specification v2.0.2.1. 42 */ 43 private static final short CREDENTIAL_FIELD_ID = 0x100E; 44 private static final short SSID_FIELD_ID = 0x1045; 45 private static final short NETWORK_KEY_FIELD_ID = 0x1027; 46 private static final short AUTH_TYPE_FIELD_ID = 0x1003; 47 48 private static final short AUTH_TYPE_EXPECTED_SIZE = 2; 49 50 private static final short AUTH_TYPE_OPEN = 0x0001; 51 private static final short AUTH_TYPE_WPA_PSK = 0x0002; 52 private static final short AUTH_TYPE_WPA_EAP = 0x0008; 53 private static final short AUTH_TYPE_WPA2_EAP = 0x0010; 54 private static final short AUTH_TYPE_WPA2_PSK = 0x0020; 55 private static final short AUTH_TYPE_WPA_AND_WPA2_PSK = 0x0022; 56 57 private static final int MAX_NETWORK_KEY_SIZE_BYTES = 64; 58 NfcWifiProtectedSetup()59 private NfcWifiProtectedSetup() {} 60 tryNfcWifiSetup(Ndef ndef, Context context)61 public static boolean tryNfcWifiSetup(Ndef ndef, Context context) { 62 63 if (ndef == null || context == null) { 64 return false; 65 } 66 67 NdefMessage cachedNdefMessage = ndef.getCachedNdefMessage(); 68 if (cachedNdefMessage == null) { 69 return false; 70 } 71 72 final WifiConfiguration wifiConfiguration; 73 try { 74 wifiConfiguration = parse(cachedNdefMessage); 75 } catch (BufferUnderflowException e) { 76 // malformed payload 77 return false; 78 } 79 80 UserManager um = context.getSystemService(UserManager.class); 81 if (wifiConfiguration != null && !um.hasUserRestrictionForUser( 82 UserManager.DISALLOW_CONFIG_WIFI, 83 // hasUserRestriction does not support UserHandle.CURRENT. 84 UserHandle.of(ActivityManager.getCurrentUser()))) { 85 Intent configureNetworkIntent = new Intent() 86 .putExtra(EXTRA_WIFI_CONFIG, wifiConfiguration) 87 .setClass(context, ConfirmConnectToWifiNetworkActivity.class) 88 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 89 90 context.startActivityAsUser(configureNetworkIntent, UserHandle.CURRENT); 91 return true; 92 } 93 94 return false; 95 } 96 parse(NdefMessage message)97 private static WifiConfiguration parse(NdefMessage message) { 98 NdefRecord[] records = message.getRecords(); 99 100 for (NdefRecord record : records) { 101 if (new String(record.getType()).equals(NFC_TOKEN_MIME_TYPE)) { 102 ByteBuffer payload = ByteBuffer.wrap(record.getPayload()); 103 while (payload.hasRemaining()) { 104 short fieldId = payload.getShort(); 105 int fieldSize = payload.getShort() & 0xFFFF; 106 if (fieldId == CREDENTIAL_FIELD_ID) { 107 return parseCredential(payload, fieldSize); 108 } 109 payload.position(payload.position() + fieldSize); 110 } 111 } 112 } 113 return null; 114 } 115 parseCredential(ByteBuffer payload, int size)116 private static WifiConfiguration parseCredential(ByteBuffer payload, int size) { 117 int startPosition = payload.position(); 118 WifiConfiguration result = new WifiConfiguration(); 119 while (payload.position() < startPosition + size) { 120 short fieldId = payload.getShort(); 121 int fieldSize = payload.getShort() & 0xFFFF; 122 123 // Quick check 124 if (payload.position() + fieldSize > startPosition + size) { 125 return null; 126 } 127 128 switch (fieldId) { 129 case SSID_FIELD_ID: 130 byte[] ssid = new byte[fieldSize]; 131 payload.get(ssid); 132 result.SSID = "\"" + new String(ssid) + "\""; 133 break; 134 case NETWORK_KEY_FIELD_ID: 135 if (fieldSize > MAX_NETWORK_KEY_SIZE_BYTES) { 136 return null; 137 } 138 byte[] networkKey = new byte[fieldSize]; 139 payload.get(networkKey); 140 if (fieldSize > 0) { 141 result.preSharedKey = getPskValidFormat(new String(networkKey)); 142 } 143 break; 144 case AUTH_TYPE_FIELD_ID: 145 if (fieldSize != AUTH_TYPE_EXPECTED_SIZE) { 146 // corrupt data 147 return null; 148 } 149 150 short authType = payload.getShort(); 151 populateAllowedKeyManagement(result.allowedKeyManagement, authType); 152 break; 153 default: 154 // unknown / unparsed tag 155 payload.position(payload.position() + fieldSize); 156 break; 157 } 158 } 159 160 if (result.SSID != null) { 161 if (result.getAuthType() == WifiConfiguration.KeyMgmt.NONE) { 162 if (result.preSharedKey == null) { 163 return result; 164 } 165 } else { 166 if (result.preSharedKey != null) { 167 return result; 168 } 169 } 170 } 171 172 return null; 173 } 174 populateAllowedKeyManagement(BitSet allowedKeyManagement, short authType)175 private static void populateAllowedKeyManagement(BitSet allowedKeyManagement, short authType) { 176 if (authType == AUTH_TYPE_WPA_PSK || authType == AUTH_TYPE_WPA2_PSK 177 || authType == AUTH_TYPE_WPA_AND_WPA2_PSK) { 178 allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); 179 } else if (authType == AUTH_TYPE_WPA_EAP || authType == AUTH_TYPE_WPA2_EAP) { 180 allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP); 181 } else if (authType == AUTH_TYPE_OPEN) { 182 allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); 183 } 184 } 185 getPskValidFormat(String data)186 private static String getPskValidFormat(String data) { 187 if (!data.matches("[0-9A-Fa-f]{64}")) { // if not HEX string 188 data = convertToQuotedString(data); 189 } 190 return data; 191 } 192 convertToQuotedString(String str)193 private static String convertToQuotedString(String str) { 194 return '"' + str + '"'; 195 } 196 } 197