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: Casper Bonde <c.bonde@samsung.com>
diff --git a/core/java/android/bluetooth/BluetoothAdapter.java b/core/java/android/bluetooth/BluetoothAdapter.java
index ab3f7bc..97afafa 100644
--- a/core/java/android/bluetooth/BluetoothAdapter.java
+++ b/core/java/android/bluetooth/BluetoothAdapter.java
@@ -1467,7 +1467,7 @@
      * @hide
      */
     public BluetoothServerSocket listenUsingRfcommOn(int channel) throws IOException {
-        return listenUsingRfcommOn(channel, false);
+        return listenUsingRfcommOn(channel, false, false);
     }
 
     /**
@@ -1482,14 +1482,17 @@
      * {@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 @@
      * {@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 @@
      * @hide
      */
     public BluetoothServerSocket listenUsingL2capOn(int port) throws IOException {
-        return listenUsingL2capOn(port, false);
+        return listenUsingL2capOn(port, false, false);
     }
 
     /**
diff --git a/core/java/android/bluetooth/BluetoothDevice.java b/core/java/android/bluetooth/BluetoothDevice.java
index dcf06d8..c96fe71e 100644
--- a/core/java/android/bluetooth/BluetoothDevice.java
+++ b/core/java/android/bluetooth/BluetoothDevice.java
@@ -531,6 +531,13 @@
     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
      * is a parcelable version of {@link UUID}.
@@ -1315,8 +1322,8 @@
             Log.e(TAG, "", e);
         }
         return false;
-    }    
-    
+    }
+
     /**
      * Create an RFCOMM {@link BluetoothSocket} ready to start a secure
      * outgoing connection to this remote device on given channel.
diff --git a/core/java/android/bluetooth/BluetoothServerSocket.java b/core/java/android/bluetooth/BluetoothServerSocket.java
index a80f55c..c15852d 100644
--- a/core/java/android/bluetooth/BluetoothServerSocket.java
+++ b/core/java/android/bluetooth/BluetoothServerSocket.java
@@ -98,14 +98,16 @@
      * @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);
         }
diff --git a/core/java/android/bluetooth/BluetoothSocket.java b/core/java/android/bluetooth/BluetoothSocket.java
index 6ca6976..6302521 100644
--- a/core/java/android/bluetooth/BluetoothSocket.java
+++ b/core/java/android/bluetooth/BluetoothSocket.java
@@ -107,6 +107,7 @@
     /*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 @@
     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 @@
      */
     /*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 @@
      * @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 @@
         mType = type;
         mAuth = auth;
         mAuthMitm = mitm;
+        mMin16DigitPin = min16DigitPin;
         mEncrypt = encrypt;
         mDevice = device;
         mPort = port;
@@ -223,6 +228,7 @@
         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 @@
      */
     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 @@
             flags |= BTSOCK_FLAG_NO_SDP;
         if(mAuthMitm)
             flags |= SEC_FLAG_AUTH_MITM;
+        if(mMin16DigitPin)
+            flags |= SEC_FLAG_AUTH_16_DIGIT;
         return flags;
     }
 
diff --git a/core/tests/bluetoothtests/src/android/bluetooth/BluetoothTestUtils.java b/core/tests/bluetoothtests/src/android/bluetooth/BluetoothTestUtils.java
index 8fbd214..0d9980a 100644
--- a/core/tests/bluetoothtests/src/android/bluetooth/BluetoothTestUtils.java
+++ b/core/tests/bluetoothtests/src/android/bluetooth/BluetoothTestUtils.java
@@ -170,6 +170,7 @@
                 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: