Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3b3d1fea authored by Casper Bonde's avatar Casper Bonde Committed by Andre Eisenbach
Browse files

SAP: Make it possible to enforce a 16-digit pin code (4/5)



This change enable the posibility to enforce using a
16-digit pin or MITM for a RFCOMM or L2CAP connection.

This is needed for the SIM access profile.

Change-Id: I3205013f9e758c353381442a86845dab467780f8
Signed-off-by: default avatarCasper Bonde <c.bonde@samsung.com>
parent 6f024937
Loading
Loading
Loading
Loading
+11 −6
Original line number Diff line number Diff line
@@ -1467,7 +1467,7 @@ public final class BluetoothAdapter {
     * @hide
     */
    public BluetoothServerSocket listenUsingRfcommOn(int channel) throws IOException {
        return listenUsingRfcommOn(channel, false);
        return listenUsingRfcommOn(channel, false, false);
    }

    /**
@@ -1482,14 +1482,17 @@ public final class BluetoothAdapter {
     * {@link SOCKET_CHANNEL_AUTO_STATIC_NO_SDP} as channel number.
     * @param channel RFCOMM channel to listen on
     * @param mitm    enforce man-in-the-middle protection for authentication.
     * @param min16DigitPin enforce a pin key length og minimum 16 digit for sec mode 2 connections.
     * @return a listening RFCOMM BluetoothServerSocket
     * @throws IOException on error, for example Bluetooth not available, or
     *                     insufficient permissions, or channel in use.
     * @hide
     */
    public BluetoothServerSocket listenUsingRfcommOn(int channel, boolean mitm) throws IOException {
    public BluetoothServerSocket listenUsingRfcommOn(int channel, boolean mitm,
            boolean min16DigitPin)
            throws IOException {
        BluetoothServerSocket socket = new BluetoothServerSocket(
                BluetoothSocket.TYPE_RFCOMM, true, true, channel, mitm);
                BluetoothSocket.TYPE_RFCOMM, true, true, channel, mitm, min16DigitPin);
        int errno = socket.mSocket.bindListen();
        if (channel == SOCKET_CHANNEL_AUTO_STATIC_NO_SDP) {
            socket.setChannel(socket.mSocket.getPort());
@@ -1694,14 +1697,16 @@ public final class BluetoothAdapter {
     * {@link SOCKET_CHANNEL_AUTO_STATIC_NO_SDP} as port number.
     * @param port    the PSM to listen on
     * @param mitm    enforce man-in-the-middle protection for authentication.
     * @param min16DigitPin enforce a pin key length og minimum 16 digit for sec mode 2 connections.
     * @return An L2CAP BluetoothServerSocket
     * @throws IOException On error, for example Bluetooth not available, or
     *                     insufficient permissions.
     * @hide
     */
    public BluetoothServerSocket listenUsingL2capOn(int port, boolean mitm) throws IOException {
    public BluetoothServerSocket listenUsingL2capOn(int port, boolean mitm, boolean min16DigitPin)
            throws IOException {
        BluetoothServerSocket socket = new BluetoothServerSocket(
                BluetoothSocket.TYPE_L2CAP, true, true, port, mitm);
                BluetoothSocket.TYPE_L2CAP, true, true, port, mitm, min16DigitPin);
        int errno = socket.mSocket.bindListen();
        if(port == SOCKET_CHANNEL_AUTO_STATIC_NO_SDP) {
            socket.setChannel(socket.mSocket.getPort());
@@ -1727,7 +1732,7 @@ public final class BluetoothAdapter {
     * @hide
     */
    public BluetoothServerSocket listenUsingL2capOn(int port) throws IOException {
        return listenUsingL2capOn(port, false);
        return listenUsingL2capOn(port, false, false);
    }

    /**
+9 −2
Original line number Diff line number Diff line
@@ -530,6 +530,13 @@ public final class BluetoothDevice implements Parcelable {
     */
    public static final int PAIRING_VARIANT_OOB_CONSENT = 6;

    /**
     * The user will be prompted to enter a 16 digit pin or
     * an app will enter a 16 digit pin for user.
     * @hide
     */
    public static final int PAIRING_VARIANT_PIN_16_DIGITS = 7;

    /**
     * Used as an extra field in {@link #ACTION_UUID} intents,
     * Contains the {@link android.os.ParcelUuid}s of the remote device which
+4 −2
Original line number Diff line number Diff line
@@ -98,14 +98,16 @@ public final class BluetoothServerSocket implements Closeable {
     * @param encrypt require the connection to be encrypted
     * @param port    remote port
     * @param mitm    enforce man-in-the-middle protection for authentication.
     * @param min16DigitPin enforce a minimum length of 16 digits for a sec mode 2 connection
     * @throws IOException On error, for example Bluetooth not available, or
     *                     insufficient privileges
     */
    /*package*/ BluetoothServerSocket(int type, boolean auth, boolean encrypt, int port,
            boolean mitm)
            boolean mitm, boolean min16DigitPin)
            throws IOException {
        mChannel = port;
        mSocket = new BluetoothSocket(type, -1, auth, encrypt, null, port, null, mitm);
        mSocket = new BluetoothSocket(type, -1, auth, encrypt, null, port, null, mitm,
                min16DigitPin);
        if(port == BluetoothAdapter.SOCKET_CHANNEL_AUTO_STATIC_NO_SDP) {
            mSocket.setExcludeSdp(true);
        }
+11 −3
Original line number Diff line number Diff line
@@ -107,6 +107,7 @@ public final class BluetoothSocket implements Closeable {
    /*package*/ static final int SEC_FLAG_AUTH = 1 << 1;
    /*package*/ static final int BTSOCK_FLAG_NO_SDP  = 1 << 2;
    /*package*/ static final int SEC_FLAG_AUTH_MITM  = 1 << 3;
    /*package*/ static final int SEC_FLAG_AUTH_16_DIGIT  = 1 << 4;

    private final int mType;  /* one of TYPE_RFCOMM etc */
    private BluetoothDevice mDevice;    /* remote device */
@@ -118,6 +119,7 @@ public final class BluetoothSocket implements Closeable {
    private final ParcelUuid mUuid;
    private boolean mExcludeSdp = false; /* when true no SPP SDP record will be created */
    private boolean mAuthMitm = false;   /* when true Man-in-the-middle protection will be enabled*/
    private boolean mMin16DigitPin = false; /* Minimum 16 digit pin for sec mode 2 connections */
    private ParcelFileDescriptor mPfd;
    private LocalSocket mSocket;
    private InputStream mSocketIS;
@@ -160,7 +162,7 @@ public final class BluetoothSocket implements Closeable {
     */
    /*package*/ BluetoothSocket(int type, int fd, boolean auth, boolean encrypt,
            BluetoothDevice device, int port, ParcelUuid uuid) throws IOException {
        this(type, fd, auth, encrypt, device, port, uuid, false);
        this(type, fd, auth, encrypt, device, port, uuid, false, false);
    }

    /**
@@ -173,11 +175,13 @@ public final class BluetoothSocket implements Closeable {
     * @param port    remote port
     * @param uuid    SDP uuid
     * @param mitm    enforce man-in-the-middle protection.
     * @param min16DigitPin enforce a minimum length of 16 digits for a sec mode 2 connection
     * @throws IOException On error, for example Bluetooth not available, or
     *                     insufficient privileges
     */
    /*package*/ BluetoothSocket(int type, int fd, boolean auth, boolean encrypt,
            BluetoothDevice device, int port, ParcelUuid uuid, boolean mitm) throws IOException {
            BluetoothDevice device, int port, ParcelUuid uuid, boolean mitm, boolean min16DigitPin)
                    throws IOException {
        if (VDBG) Log.d(TAG, "Creating new BluetoothSocket of type: " + type);
        if (type == BluetoothSocket.TYPE_RFCOMM && uuid == null && fd == -1
                && port != BluetoothAdapter.SOCKET_CHANNEL_AUTO_STATIC_NO_SDP) {
@@ -191,6 +195,7 @@ public final class BluetoothSocket implements Closeable {
        mType = type;
        mAuth = auth;
        mAuthMitm = mitm;
        mMin16DigitPin = min16DigitPin;
        mEncrypt = encrypt;
        mDevice = device;
        mPort = port;
@@ -223,6 +228,7 @@ public final class BluetoothSocket implements Closeable {
        mServiceName = s.mServiceName;
        mExcludeSdp = s.mExcludeSdp;
        mAuthMitm = s.mAuthMitm;
        mMin16DigitPin = s.mMin16DigitPin;
    }
    private BluetoothSocket acceptSocket(String RemoteAddr) throws IOException {
        BluetoothSocket as = new BluetoothSocket(this);
@@ -254,7 +260,7 @@ public final class BluetoothSocket implements Closeable {
     */
    private BluetoothSocket(int type, int fd, boolean auth, boolean encrypt, String address,
            int port) throws IOException {
        this(type, fd, auth, encrypt, new BluetoothDevice(address), port, null, false);
        this(type, fd, auth, encrypt, new BluetoothDevice(address), port, null, false, false);
    }

    /** @hide */
@@ -276,6 +282,8 @@ public final class BluetoothSocket implements Closeable {
            flags |= BTSOCK_FLAG_NO_SDP;
        if(mAuthMitm)
            flags |= SEC_FLAG_AUTH_MITM;
        if(mMin16DigitPin)
            flags |= SEC_FLAG_AUTH_16_DIGIT;
        return flags;
    }

+1 −0
Original line number Diff line number Diff line
@@ -170,6 +170,7 @@ public class BluetoothTestUtils extends Assert {
                assertNotSame(-1, varient);
                switch (varient) {
                    case BluetoothDevice.PAIRING_VARIANT_PIN:
                    case BluetoothDevice.PAIRING_VARIANT_PIN_16_DIGITS:
                        mDevice.setPin(mPin);
                        break;
                    case BluetoothDevice.PAIRING_VARIANT_PASSKEY: