From cc5494c9996f809e36539b24e8b6b67683383d29 Mon Sep 17 00:00:00 2001 From: Jaikumar Ganesh Date: Thu, 9 Sep 2010 15:37:57 -0700 Subject: Out Of Band API for Secure Simple Pairing. Change-Id: I54ded27ab85d46eef3d2cca84f2394b1ffe88ced --- core/java/android/bluetooth/BluetoothAdapter.java | 33 +++++++++ core/java/android/bluetooth/BluetoothDevice.java | 58 ++++++++++++++- core/java/android/bluetooth/IBluetooth.aidl | 4 + core/java/android/server/BluetoothEventLoop.java | 27 ++++++- core/java/android/server/BluetoothService.java | 90 ++++++++++++++++++++++- 5 files changed, 208 insertions(+), 4 deletions(-) (limited to 'core/java') diff --git a/core/java/android/bluetooth/BluetoothAdapter.java b/core/java/android/bluetooth/BluetoothAdapter.java index 03bcadc..16a8c57 100644 --- a/core/java/android/bluetooth/BluetoothAdapter.java +++ b/core/java/android/bluetooth/BluetoothAdapter.java @@ -26,8 +26,10 @@ import android.os.ParcelUuid; import android.os.RemoteException; import android.os.ServiceManager; import android.util.Log; +import android.util.Pair; import java.io.IOException; +import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.LinkedList; @@ -863,6 +865,37 @@ public final class BluetoothAdapter { return socket; } + /** + * Read the local Out of Band Pairing Data + *

Requires {@link android.Manifest.permission#BLUETOOTH} + * + * @return Pair of Hash and Randomizer + * + * @hide + */ + public Pair readOutOfBandData() { + if (getState() != STATE_ON) return null; + try { + byte[] hash = new byte[16]; + byte[] randomizer = new byte[16]; + + byte[] ret = mService.readOutOfBandData(); + + if (ret == null || ret.length != 32) return null; + + hash = Arrays.copyOfRange(ret, 0, 16); + randomizer = Arrays.copyOfRange(ret, 16, 32); + + if (DBG) { + Log.d(TAG, "readOutOfBandData:" + Arrays.toString(hash) + + ":" + Arrays.toString(randomizer)); + } + return new Pair(hash, randomizer); + + } catch (RemoteException e) {Log.e(TAG, "", e);} + return null; + } + private Set toDeviceSet(String[] addresses) { Set devices = new HashSet(addresses.length); for (int i = 0; i < addresses.length; i++) { diff --git a/core/java/android/bluetooth/BluetoothDevice.java b/core/java/android/bluetooth/BluetoothDevice.java index e77e76f..e577ec4 100644 --- a/core/java/android/bluetooth/BluetoothDevice.java +++ b/core/java/android/bluetooth/BluetoothDevice.java @@ -325,7 +325,9 @@ public final class BluetoothDevice implements Parcelable { /** The user will be prompted to enter the passkey displayed on remote device * @hide */ public static final int PAIRING_VARIANT_DISPLAY_PASSKEY = 4; - + /** The user will be prompted to accept or deny the OOB pairing request + * @hide */ + public static final int PAIRING_VARIANT_OOB_CONSENT = 5; /** * Used as an extra field in {@link #ACTION_UUID} intents, * Contains the {@link android.os.ParcelUuid}s of the remote device which @@ -464,6 +466,52 @@ public final class BluetoothDevice implements Parcelable { } /** + * Start the bonding (pairing) process with the remote device using the + * Out Of Band mechanism. + * + *

This is an asynchronous call, it will return immediately. Register + * for {@link #ACTION_BOND_STATE_CHANGED} intents to be notified when + * the bonding process completes, and its result. + * + *

Android system services will handle the necessary user interactions + * to confirm and complete the bonding process. + * + *

Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. + * + * @param hash - Simple Secure pairing hash + * @param randomizer - The random key obtained using OOB + * @return false on immediate error, true if bonding will begin + * + * @hide + */ + public boolean createBondOutOfBand(byte[] hash, byte[] randomizer) { + try { + return sService.createBondOutOfBand(mAddress, hash, randomizer); + } catch (RemoteException e) {Log.e(TAG, "", e);} + return false; + } + + /** + * Set the Out Of Band data for a remote device to be used later + * in the pairing mechanism. Users can obtain this data through other + * trusted channels + * + *

Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. + * + * @param hash Simple Secure pairing hash + * @param randomizer The random key obtained using OOB + * @return false on error; true otherwise + * + * @hide + */ + public boolean setDeviceOutOfBandData(byte[] hash, byte[] randomizer) { + try { + return sService.setDeviceOutOfBandData(mAddress, hash, randomizer); + } catch (RemoteException e) {Log.e(TAG, "", e);} + return false; + } + + /** * Cancel an in-progress bonding request started with {@link #createBond}. *

Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}. * @@ -617,6 +665,14 @@ public final class BluetoothDevice implements Parcelable { } /** @hide */ + public boolean setRemoteOutOfBandData() { + try { + return sService.setRemoteOutOfBandData(mAddress); + } catch (RemoteException e) {Log.e(TAG, "", e);} + return false; + } + + /** @hide */ public boolean cancelPairingUserInput() { try { return sService.cancelPairingUserInput(mAddress); diff --git a/core/java/android/bluetooth/IBluetooth.aidl b/core/java/android/bluetooth/IBluetooth.aidl index ea71034..6dd8dd6 100644 --- a/core/java/android/bluetooth/IBluetooth.aidl +++ b/core/java/android/bluetooth/IBluetooth.aidl @@ -44,12 +44,15 @@ interface IBluetooth boolean startDiscovery(); boolean cancelDiscovery(); boolean isDiscovering(); + byte[] readOutOfBandData(); boolean createBond(in String address); + boolean createBondOutOfBand(in String address, in byte[] hash, in byte[] randomizer); boolean cancelBondProcess(in String address); boolean removeBond(in String address); String[] listBonds(); int getBondState(in String address); + boolean setDeviceOutOfBandData(in String address, in byte[] hash, in byte[] randomizer); String getRemoteName(in String address); int getRemoteClass(in String address); @@ -60,6 +63,7 @@ interface IBluetooth boolean setPin(in String address, in byte[] pin); boolean setPasskey(in String address, int passkey); boolean setPairingConfirmation(in String address, boolean confirm); + boolean setRemoteOutOfBandData(in String addres); boolean cancelPairingUserInput(in String address); boolean setTrust(in String address, in boolean value); diff --git a/core/java/android/server/BluetoothEventLoop.java b/core/java/android/server/BluetoothEventLoop.java index 094258b..9b28a9a 100644 --- a/core/java/android/server/BluetoothEventLoop.java +++ b/core/java/android/server/BluetoothEventLoop.java @@ -551,6 +551,17 @@ class BluetoothEventLoop { mContext.sendBroadcast(intent, BLUETOOTH_ADMIN_PERM); } + private void onRequestOobData(String objectPath , int nativeData) { + String address = checkPairingRequestAndGetAddress(objectPath, nativeData); + if (address == null) return; + + Intent intent = new Intent(BluetoothDevice.ACTION_PAIRING_REQUEST); + intent.putExtra(BluetoothDevice.EXTRA_DEVICE, mAdapter.getRemoteDevice(address)); + intent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, + BluetoothDevice.PAIRING_VARIANT_OOB_CONSENT); + mContext.sendBroadcast(intent, BLUETOOTH_ADMIN_PERM); + } + private boolean onAgentAuthorize(String objectPath, String deviceUuid) { String address = mBluetoothService.getAddressFromObjectPath(objectPath); if (address == null) { @@ -583,7 +594,21 @@ class BluetoothEventLoop { return authorized; } - boolean isOtherSinkInNonDisconnectingState(String address) { + private boolean onAgentOutOfBandDataAvailable(String objectPath) { + if (!mBluetoothService.isEnabled()) return false; + + String address = mBluetoothService.getAddressFromObjectPath(objectPath); + if (address == null) return false; + + if (mBluetoothService.getDeviceOutOfBandData( + mAdapter.getRemoteDevice(address)) != null) { + return true; + } + return false; + + } + + private boolean isOtherSinkInNonDisconnectingState(String address) { BluetoothA2dp a2dp = new BluetoothA2dp(mContext); Set devices = a2dp.getNonDisconnectedSinks(); if (devices.size() == 0) return false; diff --git a/core/java/android/server/BluetoothService.java b/core/java/android/server/BluetoothService.java index b2aa04d..d53ac80 100644 --- a/core/java/android/server/BluetoothService.java +++ b/core/java/android/server/BluetoothService.java @@ -50,6 +50,7 @@ import android.os.ServiceManager; import android.os.SystemService; import android.provider.Settings; import android.util.Log; +import android.util.Pair; import com.android.internal.app.IBatteryStats; @@ -129,6 +130,8 @@ public class BluetoothService extends IBluetooth.Stub { private final BluetoothProfileState mHfpProfileState; private BluetoothA2dpService mA2dpService; + private final HashMap> mDeviceOobData; + private static String mDockAddress; private String mDockPin; @@ -183,6 +186,7 @@ public class BluetoothService extends IBluetooth.Stub { mDeviceProperties = new HashMap>(); mDeviceServiceChannelCache = new HashMap>(); + mDeviceOobData = new HashMap>(); mUuidIntentTracker = new ArrayList(); mUuidCallbackTracker = new HashMap(); mServiceRecordToPid = new HashMap(); @@ -1119,7 +1123,7 @@ public class BluetoothService extends IBluetooth.Stub { mIsDiscovering = isDiscovering; } - public synchronized boolean createBond(String address) { + private boolean isBondingFeasible(String address) { mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, "Need BLUETOOTH_ADMIN permission"); if (!isEnabledInternal()) return false; @@ -1149,8 +1153,13 @@ public class BluetoothService extends IBluetooth.Stub { return false; } } + return true; + } - if (!createPairedDeviceNative(address, 60000 /* 1 minute */)) { + public synchronized boolean createBond(String address) { + if (!isBondingFeasible(address)) return false; + + if (!createPairedDeviceNative(address, 60000 /*1 minute*/ )) { return false; } @@ -1160,6 +1169,51 @@ public class BluetoothService extends IBluetooth.Stub { return true; } + public synchronized boolean createBondOutOfBand(String address, byte[] hash, + byte[] randomizer) { + if (!isBondingFeasible(address)) return false; + + if (!createPairedDeviceOutOfBandNative(address, 60000 /* 1 minute */)) { + return false; + } + + setDeviceOutOfBandData(address, hash, randomizer); + mBondState.setPendingOutgoingBonding(address); + mBondState.setBondState(address, BluetoothDevice.BOND_BONDING); + + return true; + } + + public synchronized boolean setDeviceOutOfBandData(String address, byte[] hash, + byte[] randomizer) { + mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, + "Need BLUETOOTH_ADMIN permission"); + if (!isEnabledInternal()) return false; + + Pair value = new Pair(hash, randomizer); + + if (DBG) { + log("Setting out of band data for:" + address + ":" + + Arrays.toString(hash) + ":" + Arrays.toString(randomizer)); + } + + mDeviceOobData.put(address, value); + return true; + } + + Pair getDeviceOutOfBandData(BluetoothDevice device) { + return mDeviceOobData.get(device.getAddress()); + } + + + public synchronized byte[] readOutOfBandData() { + mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, + "Need BLUETOOTH permission"); + if (!isEnabledInternal()) return null; + + return readAdapterOutOfBandDataNative(); + } + public synchronized boolean cancelBondProcess(String address) { mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, "Need BLUETOOTH_ADMIN permission"); @@ -1551,6 +1605,32 @@ public class BluetoothService extends IBluetooth.Stub { return setPairingConfirmationNative(address, confirm, data.intValue()); } + public synchronized boolean setRemoteOutOfBandData(String address) { + mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, + "Need BLUETOOTH_ADMIN permission"); + if (!isEnabledInternal()) return false; + address = address.toUpperCase(); + Integer data = mEventLoop.getPasskeyAgentRequestData().remove(address); + if (data == null) { + Log.w(TAG, "setRemoteOobData(" + address + ") called but no native data available, " + + "ignoring. Maybe the PasskeyAgent Request was cancelled by the remote device" + + " or by bluez.\n"); + return false; + } + + Pair val = mDeviceOobData.get(address); + byte[] hash, randomizer; + if (val == null) { + // TODO: check what should be passed in this case. + hash = new byte[16]; + randomizer = new byte[16]; + } else { + hash = val.first; + randomizer = val.second; + } + return setRemoteOutOfBandDataNative(address, hash, randomizer, data.intValue()); + } + public synchronized boolean cancelPairingUserInput(String address) { mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, "Need BLUETOOTH_ADMIN permission"); @@ -2084,6 +2164,9 @@ public class BluetoothService extends IBluetooth.Stub { private native boolean stopDiscoveryNative(); private native boolean createPairedDeviceNative(String address, int timeout_ms); + private native boolean createPairedDeviceOutOfBandNative(String address, int timeout_ms); + private native byte[] readAdapterOutOfBandDataNative(); + private native boolean cancelDeviceCreationNative(String address); private native boolean removeDeviceNative(String objectPath); private native int getDeviceServiceChannelNative(String objectPath, String uuid, @@ -2094,6 +2177,9 @@ public class BluetoothService extends IBluetooth.Stub { private native boolean setPasskeyNative(String address, int passkey, int nativeData); private native boolean setPairingConfirmationNative(String address, boolean confirm, int nativeData); + private native boolean setRemoteOutOfBandDataNative(String address, byte[] hash, + byte[] randomizer, int nativeData); + private native boolean setDevicePropertyBooleanNative(String objectPath, String key, int value); private native boolean createDeviceNative(String address); -- cgit v1.1