Skip to content

Commit 9406251

Browse files
Jaikumar GaneshAndroid Code Review
authored andcommitted
Merge "Require bonding and encryption for PBAP server"
2 parents 038e8f9 + e3b9dc1 commit 9406251

File tree

1 file changed

+63
-3
lines changed

1 file changed

+63
-3
lines changed

core/java/android/bluetooth/BluetoothAdapter.java

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -799,10 +799,10 @@ public BluetoothServerSocket listenUsingRfcommWithServiceRecord(String name, UUI
799799

800800
/**
801801
* Create a listening, insecure RFCOMM Bluetooth socket with Service Record.
802-
* <p>The link key will be unauthenticated i.e the communication is
802+
* <p>The link key is not required to be authenticated, i.e the communication may be
803803
* vulnerable to Man In the Middle attacks. For Bluetooth 2.1 devices,
804-
* the link key will be encrypted, as encryption is mandartory.
805-
* For legacy devices (pre Bluetooth 2.1 devices) the link key will not
804+
* the link will be encrypted, as encryption is mandartory.
805+
* For legacy devices (pre Bluetooth 2.1 devices) the link will not
806806
* be encrypted. Use {@link #listenUsingRfcommWithServiceRecord}, if an
807807
* encrypted and authenticated communication channel is desired.
808808
* <p>Use {@link BluetoothServerSocket#accept} to retrieve incoming
@@ -828,6 +828,44 @@ public BluetoothServerSocket listenUsingInsecureRfcommWithServiceRecord(String n
828828
return createNewRfcommSocketAndRecord(name, uuid, false, false);
829829
}
830830

831+
/**
832+
* Create a listening, encrypted,
833+
* RFCOMM Bluetooth socket with Service Record.
834+
* <p>The link will be encrypted, but the link key is not required to be authenticated
835+
* i.e the communication is vulnerable to Man In the Middle attacks. Use
836+
* {@link #listenUsingRfcommWithServiceRecord}, to ensure an authenticated link key.
837+
* <p> Use this socket if authentication of link key is not possible.
838+
* For example, for Bluetooth 2.1 devices, if any of the devices does not have
839+
* an input and output capability or just has the ability to display a numeric key,
840+
* a secure socket connection is not possible and this socket can be used.
841+
* Use {@link #listenUsingInsecureRfcommWithServiceRecord}, if encryption is not required.
842+
* For Bluetooth 2.1 devices, the link will be encrypted, as encryption is mandartory.
843+
* For more details, refer to the Security Model section 5.2 (vol 3) of
844+
* Bluetooth Core Specification version 2.1 + EDR.
845+
* <p>Use {@link BluetoothServerSocket#accept} to retrieve incoming
846+
* connections from a listening {@link BluetoothServerSocket}.
847+
* <p>The system will assign an unused RFCOMM channel to listen on.
848+
* <p>The system will also register a Service Discovery
849+
* Protocol (SDP) record with the local SDP server containing the specified
850+
* UUID, service name, and auto-assigned channel. Remote Bluetooth devices
851+
* can use the same UUID to query our SDP server and discover which channel
852+
* to connect to. This SDP record will be removed when this socket is
853+
* closed, or if this application closes unexpectedly.
854+
* <p>Use {@link BluetoothDevice#createRfcommSocketToServiceRecord} to
855+
* connect to this socket from another device using the same {@link UUID}.
856+
* <p>Requires {@link android.Manifest.permission#BLUETOOTH}
857+
* @param name service name for SDP record
858+
* @param uuid uuid for SDP record
859+
* @return a listening RFCOMM BluetoothServerSocket
860+
* @throws IOException on error, for example Bluetooth not available, or
861+
* insufficient permissions, or channel in use.
862+
* @hide
863+
*/
864+
public BluetoothServerSocket listenUsingEncryptedRfcommWithServiceRecord(
865+
String name, UUID uuid) throws IOException {
866+
return createNewRfcommSocketAndRecord(name, uuid, false, true);
867+
}
868+
831869
private BluetoothServerSocket createNewRfcommSocketAndRecord(String name, UUID uuid,
832870
boolean auth, boolean encrypt) throws IOException {
833871
RfcommChannelPicker picker = new RfcommChannelPicker(uuid);
@@ -898,6 +936,28 @@ public BluetoothServerSocket listenUsingInsecureRfcommOn(int port) throws IOExce
898936
return socket;
899937
}
900938

939+
/**
940+
* Construct an encrypted, RFCOMM server socket.
941+
* Call #accept to retrieve connections to this socket.
942+
* @return An RFCOMM BluetoothServerSocket
943+
* @throws IOException On error, for example Bluetooth not available, or
944+
* insufficient permissions.
945+
* @hide
946+
*/
947+
public BluetoothServerSocket listenUsingEncryptedRfcommOn(int port)
948+
throws IOException {
949+
BluetoothServerSocket socket = new BluetoothServerSocket(
950+
BluetoothSocket.TYPE_RFCOMM, false, true, port);
951+
int errno = socket.mSocket.bindListen();
952+
if (errno != 0) {
953+
try {
954+
socket.close();
955+
} catch (IOException e) {}
956+
socket.mSocket.throwErrnoNative(errno);
957+
}
958+
return socket;
959+
}
960+
901961
/**
902962
* Construct a SCO server socket.
903963
* Call #accept to retrieve connections to this socket.

0 commit comments

Comments
 (0)