Merge "Gradually increase the level of authentication on failed GATT operations."

This commit is contained in:
Treehugger Robot
2016-10-20 22:50:07 +00:00
committed by Gerrit Code Review

View File

@@ -45,14 +45,18 @@ public final class BluetoothGatt implements BluetoothProfile {
private IBluetoothGatt mService; private IBluetoothGatt mService;
private BluetoothGattCallback mCallback; private BluetoothGattCallback mCallback;
private int mClientIf; private int mClientIf;
private boolean mAuthRetry = false;
private BluetoothDevice mDevice; private BluetoothDevice mDevice;
private boolean mAutoConnect; private boolean mAutoConnect;
private int mAuthRetryState;
private int mConnState; private int mConnState;
private final Object mStateLock = new Object(); private final Object mStateLock = new Object();
private Boolean mDeviceBusy = false; private Boolean mDeviceBusy = false;
private int mTransport; private int mTransport;
private static final int AUTH_RETRY_STATE_IDLE = 0;
private static final int AUTH_RETRY_STATE_NO_MITM = 1;
private static final int AUTH_RETRY_STATE_MITM = 2;
private static final int CONN_STATE_IDLE = 0; private static final int CONN_STATE_IDLE = 0;
private static final int CONN_STATE_CONNECTING = 1; private static final int CONN_STATE_CONNECTING = 1;
private static final int CONN_STATE_CONNECTED = 2; private static final int CONN_STATE_CONNECTED = 2;
@@ -260,17 +264,19 @@ public final class BluetoothGatt implements BluetoothProfile {
if ((status == GATT_INSUFFICIENT_AUTHENTICATION if ((status == GATT_INSUFFICIENT_AUTHENTICATION
|| status == GATT_INSUFFICIENT_ENCRYPTION) || status == GATT_INSUFFICIENT_ENCRYPTION)
&& mAuthRetry == false) { && (mAuthRetryState != AUTH_RETRY_STATE_MITM)) {
try { try {
mAuthRetry = true; final int authReq = (mAuthRetryState == AUTH_RETRY_STATE_IDLE) ?
mService.readCharacteristic(mClientIf, address, handle, AUTHENTICATION_MITM); AUTHENTICATION_NO_MITM : AUTHENTICATION_MITM;
mService.readCharacteristic(mClientIf, address, handle, authReq);
mAuthRetryState++;
return; return;
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG,"",e); Log.e(TAG,"",e);
} }
} }
mAuthRetry = false; mAuthRetryState = AUTH_RETRY_STATE_IDLE;
BluetoothGattCharacteristic characteristic = getCharacteristicById(mDevice, handle); BluetoothGattCharacteristic characteristic = getCharacteristicById(mDevice, handle);
if (characteristic == null) { if (characteristic == null) {
@@ -309,19 +315,20 @@ public final class BluetoothGatt implements BluetoothProfile {
if ((status == GATT_INSUFFICIENT_AUTHENTICATION if ((status == GATT_INSUFFICIENT_AUTHENTICATION
|| status == GATT_INSUFFICIENT_ENCRYPTION) || status == GATT_INSUFFICIENT_ENCRYPTION)
&& mAuthRetry == false) { && (mAuthRetryState != AUTH_RETRY_STATE_MITM)) {
try { try {
mAuthRetry = true; final int authReq = (mAuthRetryState == AUTH_RETRY_STATE_IDLE) ?
AUTHENTICATION_NO_MITM : AUTHENTICATION_MITM;
mService.writeCharacteristic(mClientIf, address, handle, mService.writeCharacteristic(mClientIf, address, handle,
characteristic.getWriteType(), AUTHENTICATION_MITM, characteristic.getWriteType(), authReq, characteristic.getValue());
characteristic.getValue()); mAuthRetryState++;
return; return;
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG,"",e); Log.e(TAG,"",e);
} }
} }
mAuthRetry = false; mAuthRetryState = AUTH_RETRY_STATE_IDLE;
try { try {
mCallback.onCharacteristicWrite(BluetoothGatt.this, characteristic, status); mCallback.onCharacteristicWrite(BluetoothGatt.this, characteristic, status);
@@ -376,17 +383,19 @@ public final class BluetoothGatt implements BluetoothProfile {
if ((status == GATT_INSUFFICIENT_AUTHENTICATION if ((status == GATT_INSUFFICIENT_AUTHENTICATION
|| status == GATT_INSUFFICIENT_ENCRYPTION) || status == GATT_INSUFFICIENT_ENCRYPTION)
&& mAuthRetry == false) { && (mAuthRetryState != AUTH_RETRY_STATE_MITM)) {
try { try {
mAuthRetry = true; final int authReq = (mAuthRetryState == AUTH_RETRY_STATE_IDLE) ?
mService.readDescriptor(mClientIf, address, handle, AUTHENTICATION_MITM); AUTHENTICATION_NO_MITM : AUTHENTICATION_MITM;
mService.readDescriptor(mClientIf, address, handle, authReq);
mAuthRetryState++;
return; return;
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG,"",e); Log.e(TAG,"",e);
} }
} }
mAuthRetry = true; mAuthRetryState = AUTH_RETRY_STATE_IDLE;
try { try {
mCallback.onDescriptorRead(BluetoothGatt.this, descriptor, status); mCallback.onDescriptorRead(BluetoothGatt.this, descriptor, status);
@@ -415,18 +424,20 @@ public final class BluetoothGatt implements BluetoothProfile {
if ((status == GATT_INSUFFICIENT_AUTHENTICATION if ((status == GATT_INSUFFICIENT_AUTHENTICATION
|| status == GATT_INSUFFICIENT_ENCRYPTION) || status == GATT_INSUFFICIENT_ENCRYPTION)
&& mAuthRetry == false) { && (mAuthRetryState != AUTH_RETRY_STATE_MITM)) {
try { try {
mAuthRetry = true; final int authReq = (mAuthRetryState == AUTH_RETRY_STATE_IDLE) ?
AUTHENTICATION_NO_MITM : AUTHENTICATION_MITM;
mService.writeDescriptor(mClientIf, address, handle, mService.writeDescriptor(mClientIf, address, handle,
AUTHENTICATION_MITM, descriptor.getValue()); authReq, descriptor.getValue());
mAuthRetryState++;
return; return;
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG,"",e); Log.e(TAG,"",e);
} }
} }
mAuthRetry = false; mAuthRetryState = AUTH_RETRY_STATE_IDLE;
try { try {
mCallback.onDescriptorWrite(BluetoothGatt.this, descriptor, status); mCallback.onDescriptorWrite(BluetoothGatt.this, descriptor, status);
@@ -501,6 +512,7 @@ public final class BluetoothGatt implements BluetoothProfile {
mServices = new ArrayList<BluetoothGattService>(); mServices = new ArrayList<BluetoothGattService>();
mConnState = CONN_STATE_IDLE; mConnState = CONN_STATE_IDLE;
mAuthRetryState = AUTH_RETRY_STATE_IDLE;
} }
/** /**
@@ -514,6 +526,7 @@ public final class BluetoothGatt implements BluetoothProfile {
unregisterApp(); unregisterApp();
mConnState = CONN_STATE_CLOSED; mConnState = CONN_STATE_CLOSED;
mAuthRetryState = AUTH_RETRY_STATE_IDLE;
} }
/** /**