From 08def323c33fdc8035a63cb1e9b8e79f3a98743c Mon Sep 17 00:00:00 2001 From: RHeniz Date: Fri, 22 Oct 2021 16:12:48 -0400 Subject: [PATCH 1/7] Update SecurityQueryPayload to include json data --- .../protocol/SecurityQueryPayloadTests.java | 23 +++++++-------- .../protocol/SecurityQueryPayload.java | 23 +++++++++++++-- .../session/BaseSdlSession.java | 29 +++++++++++++++---- 3 files changed, 54 insertions(+), 21 deletions(-) diff --git a/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/SecurityQueryPayloadTests.java b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/SecurityQueryPayloadTests.java index 8c0c360417..68c64fedfd 100644 --- a/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/SecurityQueryPayloadTests.java +++ b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/SecurityQueryPayloadTests.java @@ -25,7 +25,7 @@ public static SecurityQueryPayload createDummyBqh() { bqh.setQueryID(SecurityQueryID.SEND_HANDSHAKE_DATA); bqh.setQueryType(SecurityQueryType.REQUEST); bqh.setBulkData(null); - bqh.setJsonSize(0); + bqh.setJsonData(null); return bqh; } @@ -66,9 +66,9 @@ public void testCorrectHeaderAssembly() { dummyBqh.setQueryType(SecurityQueryType.REQUEST); dummyBqh.setQueryID(SecurityQueryID.SEND_HANDSHAKE_DATA); dummyBqh.setCorrelationID(3); - dummyBqh.setJsonSize(0); + dummyBqh.setJsonData(new byte[0]); - byte[] assembledHeader = dummyBqh.assembleHeaderBytes(); + byte[] assembledHeader = dummyBqh.assembleSecurityQueryPayload(0); assertEquals(dummyBqh.getQueryType(), SecurityQueryType.valueOf(assembledHeader[0])); byte[] queryIDFromHeader = new byte[3]; System.arraycopy(assembledHeader, 1, queryIDFromHeader, 0, 3); @@ -81,7 +81,7 @@ public void testCorrectHeaderAssembly() { public void testAssemblyAndParse() { SecurityQueryPayload bqh = createDummyBqh(); - byte[] bqhBytes = bqh.assembleHeaderBytes(); + byte[] bqhBytes = bqh.assembleSecurityQueryPayload(0); assertNotNull(bqhBytes); SecurityQueryPayload parsedBqh = SecurityQueryPayload.parseBinaryQueryHeader(bqhBytes); @@ -99,7 +99,7 @@ public void testAssemblyAndParse() { public void testCorruptHeader() { SecurityQueryPayload bqh = createDummyBqh(); - byte[] bqhBytes = bqh.assembleHeaderBytes(); + byte[] bqhBytes = bqh.assembleSecurityQueryPayload(0); assertNotNull(safeParse(bqhBytes)); @@ -114,13 +114,10 @@ public void testCorruptHeader() { } @Test - public void testJsonSetException() { - try { - SecurityQueryPayload bqh = createDummyBqh(); - bqh.setJsonData(null); - fail("Setting JSON data to null should have thrown an exception"); - } catch (Exception e) { - //Pass - } + public void testNullJsonData() { + SecurityQueryPayload bqh = createDummyBqh(); + bqh.setJsonData(null); + assertEquals(0, bqh.getJsonSize()); + assertEquals(null, bqh.getJsonData()); } } diff --git a/base/src/main/java/com/smartdevicelink/protocol/SecurityQueryPayload.java b/base/src/main/java/com/smartdevicelink/protocol/SecurityQueryPayload.java index c7bc1326d9..2a102b8aab 100644 --- a/base/src/main/java/com/smartdevicelink/protocol/SecurityQueryPayload.java +++ b/base/src/main/java/com/smartdevicelink/protocol/SecurityQueryPayload.java @@ -84,7 +84,20 @@ public static SecurityQueryPayload parseBinaryQueryHeader(byte[] binHeader) { return msg; } - public byte[] assembleHeaderBytes() { + public byte[] assembleSecurityQueryPayload(int payloadSize) { + byte[] payLoad = new byte[SECURITY_QUERY_HEADER_SIZE + payloadSize]; + System.arraycopy(assembleHeaderBytes(), 0, payLoad, 0, SECURITY_QUERY_HEADER_SIZE); + + if (_securityQueryID == SecurityQueryID.SEND_INTERNAL_ERROR && _securityQueryType == SecurityQueryType.NOTIFICATION) { + System.arraycopy(_jsonData, 0, payLoad, SECURITY_QUERY_HEADER_SIZE, _jsonSize); + } else if (_securityQueryID == SecurityQueryID.SEND_HANDSHAKE_DATA && _securityQueryType == SecurityQueryType.RESPONSE) { + System.arraycopy(_bulkData, 0, payLoad, SECURITY_QUERY_HEADER_SIZE, payloadSize); + } + + return payLoad; + } + + private byte[] assembleHeaderBytes() { // From the properties, create a data buffer // Query Type - first 8 bits // Query ID - next 24 bits @@ -126,7 +139,7 @@ public int getJsonSize() { return _jsonSize; } - public void setJsonSize(int _jsonSize) { + private void setJsonSize(int _jsonSize) { this._jsonSize = _jsonSize; } @@ -143,6 +156,12 @@ public byte[] getJsonData() { } public void setJsonData(byte[] _jsonData) { + if (_jsonData == null) { + this._jsonSize = 0; + this._jsonData = null; + return; + } + this._jsonSize = _jsonData.length; this._jsonData = new byte[this._jsonSize]; System.arraycopy(_jsonData, 0, this._jsonData, 0, _jsonSize); } diff --git a/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java b/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java index d68e9cd206..8f2b3740d0 100644 --- a/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java +++ b/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java @@ -44,6 +44,7 @@ import com.smartdevicelink.protocol.SdlPacket; import com.smartdevicelink.protocol.SdlProtocolBase; import com.smartdevicelink.protocol.enums.ControlFrameTags; +import com.smartdevicelink.protocol.enums.SecurityQueryErrorCode; import com.smartdevicelink.protocol.enums.SecurityQueryID; import com.smartdevicelink.protocol.enums.SecurityQueryType; import com.smartdevicelink.protocol.enums.SessionType; @@ -61,6 +62,9 @@ import com.smartdevicelink.util.SystemInfo; import com.smartdevicelink.util.Version; +import org.json.JSONException; +import org.json.JSONObject; + import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -240,24 +244,37 @@ protected void processControlService(ProtocolMessage msg) { // Assemble a security query payload header for our response SecurityQueryPayload responseHeader = new SecurityQueryPayload(); + byte[] returnBytes; if (iNumBytes == null || iNumBytes <= 0) { DebugTool.logError(TAG, "Internal Error processing control service"); responseHeader.setQueryID(SecurityQueryID.SEND_INTERNAL_ERROR); responseHeader.setQueryType(SecurityQueryType.NOTIFICATION); responseHeader.setCorrelationID(msg.getCorrID()); - responseHeader.setJsonSize(0); + byte[] jsonData; + JSONObject jsonObject = new JSONObject(); + try { + jsonObject.put("id", 254); + jsonObject.put("text", "Error value for testing"); + jsonData = jsonObject.toString().getBytes(); + } catch (JSONException e) { + DebugTool.logError(TAG, "JSON exception when constructing handshake error Notification"); + e.printStackTrace(); + jsonData = new byte[0]; + } + responseHeader.setJsonData(jsonData); + responseHeader.setBulkData(null); + responseHeader.setErrorCode(SecurityQueryErrorCode.ERROR_UNKNOWN_INTERNAL_ERROR); + returnBytes = responseHeader.assembleSecurityQueryPayload(responseHeader.getJsonSize()); } else { responseHeader.setQueryID(SecurityQueryID.SEND_HANDSHAKE_DATA); responseHeader.setQueryType(SecurityQueryType.RESPONSE); responseHeader.setCorrelationID(msg.getCorrID()); - responseHeader.setJsonSize(0); + responseHeader.setBulkData(dataToRead); + responseHeader.setJsonData(null); + returnBytes = responseHeader.assembleSecurityQueryPayload(iNumBytes); } - byte[] returnBytes = new byte[iNumBytes + 12]; - System.arraycopy(responseHeader.assembleHeaderBytes(), 0, returnBytes, 0, 12); - System.arraycopy(dataToRead, 0, returnBytes, 12, iNumBytes); - ProtocolMessage protocolMessage = new ProtocolMessage(); protocolMessage.setSessionType(SessionType.CONTROL); protocolMessage.setData(returnBytes); From 74e2a649d457cc1a7f9aef9e2112bdc75447eee0 Mon Sep 17 00:00:00 2001 From: RHeniz Date: Mon, 25 Oct 2021 09:14:54 -0400 Subject: [PATCH 2/7] Use ErrorCode enum instead of hard coded values --- .../main/java/com/smartdevicelink/session/BaseSdlSession.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java b/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java index 8f2b3740d0..26627a8791 100644 --- a/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java +++ b/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java @@ -254,8 +254,8 @@ protected void processControlService(ProtocolMessage msg) { byte[] jsonData; JSONObject jsonObject = new JSONObject(); try { - jsonObject.put("id", 254); - jsonObject.put("text", "Error value for testing"); + jsonObject.put("id", SecurityQueryErrorCode.ERROR_UNKNOWN_INTERNAL_ERROR.getValue()); + jsonObject.put("text", SecurityQueryErrorCode.ERROR_UNKNOWN_INTERNAL_ERROR.getName()); jsonData = jsonObject.toString().getBytes(); } catch (JSONException e) { DebugTool.logError(TAG, "JSON exception when constructing handshake error Notification"); From 35275fc0a6933aa9f3d824f1819beda5570b43aa Mon Sep 17 00:00:00 2001 From: RHeniz Date: Mon, 25 Oct 2021 11:51:28 -0400 Subject: [PATCH 3/7] Add errorCode byte to response payload --- .../protocol/SecurityQueryPayload.java | 15 ++++++++++++--- .../smartdevicelink/session/BaseSdlSession.java | 2 +- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/base/src/main/java/com/smartdevicelink/protocol/SecurityQueryPayload.java b/base/src/main/java/com/smartdevicelink/protocol/SecurityQueryPayload.java index 2a102b8aab..af82b004ae 100644 --- a/base/src/main/java/com/smartdevicelink/protocol/SecurityQueryPayload.java +++ b/base/src/main/java/com/smartdevicelink/protocol/SecurityQueryPayload.java @@ -85,15 +85,24 @@ public static SecurityQueryPayload parseBinaryQueryHeader(byte[] binHeader) { } public byte[] assembleSecurityQueryPayload(int payloadSize) { - byte[] payLoad = new byte[SECURITY_QUERY_HEADER_SIZE + payloadSize]; - System.arraycopy(assembleHeaderBytes(), 0, payLoad, 0, SECURITY_QUERY_HEADER_SIZE); - + byte[] payLoad = new byte[SECURITY_QUERY_HEADER_SIZE]; if (_securityQueryID == SecurityQueryID.SEND_INTERNAL_ERROR && _securityQueryType == SecurityQueryType.NOTIFICATION) { + payLoad = new byte[SECURITY_QUERY_HEADER_SIZE + payloadSize + 1]; System.arraycopy(_jsonData, 0, payLoad, SECURITY_QUERY_HEADER_SIZE, _jsonSize); + byte[] errorCode = new byte[1]; + if (this._errorCode != null) { + errorCode[0] = _errorCode.getValue(); + } else { + errorCode[0] = SecurityQueryErrorCode.ERROR_UNKNOWN_INTERNAL_ERROR.getValue(); + } + System.arraycopy(errorCode, 0, payLoad, payLoad.length - 1, 1); } else if (_securityQueryID == SecurityQueryID.SEND_HANDSHAKE_DATA && _securityQueryType == SecurityQueryType.RESPONSE) { + payLoad = new byte[SECURITY_QUERY_HEADER_SIZE + payloadSize]; System.arraycopy(_bulkData, 0, payLoad, SECURITY_QUERY_HEADER_SIZE, payloadSize); } + System.arraycopy(assembleHeaderBytes(), 0, payLoad, 0, SECURITY_QUERY_HEADER_SIZE); + return payLoad; } diff --git a/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java b/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java index 26627a8791..3507e38193 100644 --- a/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java +++ b/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java @@ -239,7 +239,7 @@ protected void processControlService(ProtocolMessage msg) { return; } - iNumBytes = sdlSecurity.runHandshake(data, dataToRead); + iNumBytes = null; // Assemble a security query payload header for our response SecurityQueryPayload responseHeader = new SecurityQueryPayload(); From a35281dd6b74b000734dce44103dbd913637d7fc Mon Sep 17 00:00:00 2001 From: RHeniz Date: Mon, 25 Oct 2021 12:04:39 -0400 Subject: [PATCH 4/7] Fix change from testing --- .../main/java/com/smartdevicelink/session/BaseSdlSession.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java b/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java index 3507e38193..26627a8791 100644 --- a/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java +++ b/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java @@ -239,7 +239,7 @@ protected void processControlService(ProtocolMessage msg) { return; } - iNumBytes = null; + iNumBytes = sdlSecurity.runHandshake(data, dataToRead); // Assemble a security query payload header for our response SecurityQueryPayload responseHeader = new SecurityQueryPayload(); From 7024b2afbebb2ff939a325b8dd2ad6bedb6832da Mon Sep 17 00:00:00 2001 From: RHeniz Date: Mon, 15 Nov 2021 11:38:57 -0500 Subject: [PATCH 5/7] Align to iOS --- .../protocol/SecurityQueryPayloadTests.java | 6 +- .../protocol/SecurityQueryPayload.java | 70 ++++++++----------- .../session/BaseSdlSession.java | 12 ++-- 3 files changed, 39 insertions(+), 49 deletions(-) diff --git a/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/SecurityQueryPayloadTests.java b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/SecurityQueryPayloadTests.java index 68c64fedfd..3a1cd50a15 100644 --- a/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/SecurityQueryPayloadTests.java +++ b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/SecurityQueryPayloadTests.java @@ -68,7 +68,7 @@ public void testCorrectHeaderAssembly() { dummyBqh.setCorrelationID(3); dummyBqh.setJsonData(new byte[0]); - byte[] assembledHeader = dummyBqh.assembleSecurityQueryPayload(0); + byte[] assembledHeader = dummyBqh.assembleBinaryData(); assertEquals(dummyBqh.getQueryType(), SecurityQueryType.valueOf(assembledHeader[0])); byte[] queryIDFromHeader = new byte[3]; System.arraycopy(assembledHeader, 1, queryIDFromHeader, 0, 3); @@ -81,7 +81,7 @@ public void testCorrectHeaderAssembly() { public void testAssemblyAndParse() { SecurityQueryPayload bqh = createDummyBqh(); - byte[] bqhBytes = bqh.assembleSecurityQueryPayload(0); + byte[] bqhBytes = bqh.assembleBinaryData(); assertNotNull(bqhBytes); SecurityQueryPayload parsedBqh = SecurityQueryPayload.parseBinaryQueryHeader(bqhBytes); @@ -99,7 +99,7 @@ public void testAssemblyAndParse() { public void testCorruptHeader() { SecurityQueryPayload bqh = createDummyBqh(); - byte[] bqhBytes = bqh.assembleSecurityQueryPayload(0); + byte[] bqhBytes = bqh.assembleBinaryData(); assertNotNull(safeParse(bqhBytes)); diff --git a/base/src/main/java/com/smartdevicelink/protocol/SecurityQueryPayload.java b/base/src/main/java/com/smartdevicelink/protocol/SecurityQueryPayload.java index af82b004ae..a41052c0da 100644 --- a/base/src/main/java/com/smartdevicelink/protocol/SecurityQueryPayload.java +++ b/base/src/main/java/com/smartdevicelink/protocol/SecurityQueryPayload.java @@ -16,7 +16,7 @@ public class SecurityQueryPayload { private SecurityQueryID _securityQueryID; private int _correlationID; private int _jsonSize; - private SecurityQueryErrorCode _errorCode; + private int _bulkDataSize; private byte[] _jsonData = null; private byte[] _bulkData = null; @@ -51,11 +51,6 @@ public static SecurityQueryPayload parseBinaryQueryHeader(byte[] binHeader) { int _jsonSize = BitConverter.intFromByteArray(binHeader, 8); msg.setJsonSize(_jsonSize); - //If we get an error message we want the error code from the last 8 bits - if (msg.getQueryType() == SecurityQueryType.NOTIFICATION && msg.getQueryID() == SecurityQueryID.SEND_INTERNAL_ERROR) { - msg.setErrorCode(SecurityQueryErrorCode.valueOf(binHeader[binHeader.length - 1])); - } - try { //Get the JsonData after the header (after 96 bits) based on the jsonData size if (_jsonSize > 0 && _jsonSize <= (binHeader.length - SECURITY_QUERY_HEADER_SIZE)) { @@ -84,40 +79,28 @@ public static SecurityQueryPayload parseBinaryQueryHeader(byte[] binHeader) { return msg; } - public byte[] assembleSecurityQueryPayload(int payloadSize) { - byte[] payLoad = new byte[SECURITY_QUERY_HEADER_SIZE]; - if (_securityQueryID == SecurityQueryID.SEND_INTERNAL_ERROR && _securityQueryType == SecurityQueryType.NOTIFICATION) { - payLoad = new byte[SECURITY_QUERY_HEADER_SIZE + payloadSize + 1]; - System.arraycopy(_jsonData, 0, payLoad, SECURITY_QUERY_HEADER_SIZE, _jsonSize); - byte[] errorCode = new byte[1]; - if (this._errorCode != null) { - errorCode[0] = _errorCode.getValue(); - } else { - errorCode[0] = SecurityQueryErrorCode.ERROR_UNKNOWN_INTERNAL_ERROR.getValue(); - } - System.arraycopy(errorCode, 0, payLoad, payLoad.length - 1, 1); - } else if (_securityQueryID == SecurityQueryID.SEND_HANDSHAKE_DATA && _securityQueryType == SecurityQueryType.RESPONSE) { - payLoad = new byte[SECURITY_QUERY_HEADER_SIZE + payloadSize]; - System.arraycopy(_bulkData, 0, payLoad, SECURITY_QUERY_HEADER_SIZE, payloadSize); - } - - System.arraycopy(assembleHeaderBytes(), 0, payLoad, 0, SECURITY_QUERY_HEADER_SIZE); - - return payLoad; - } - - private byte[] assembleHeaderBytes() { + public byte[] assembleBinaryData() { // From the properties, create a data buffer // Query Type - first 8 bits // Query ID - next 24 bits // Sequence Number - next 32 bits // JSON size - next 32 bits - byte[] ret = new byte[SECURITY_QUERY_HEADER_SIZE]; - ret[0] = _securityQueryType.getValue(); - System.arraycopy(_securityQueryID.getValue(), 0, ret, 1, 3); - System.arraycopy(BitConverter.intToByteArray(_correlationID), 0, ret, 4, 4); - System.arraycopy(BitConverter.intToByteArray(_jsonSize), 0, ret, 8, 4); - return ret; + byte[] header = new byte[SECURITY_QUERY_HEADER_SIZE]; + header[0] = _securityQueryType.getValue(); + System.arraycopy(_securityQueryID.getValue(), 0, header, 1, 3); + System.arraycopy(BitConverter.intToByteArray(_correlationID), 0, header, 4, 4); + System.arraycopy(BitConverter.intToByteArray(_jsonSize), 0, header, 8, 4); + + int size = _jsonSize + _bulkDataSize + SECURITY_QUERY_HEADER_SIZE; + byte[] dataOut = new byte[size]; + System.arraycopy(header, 0, dataOut, 0, SECURITY_QUERY_HEADER_SIZE); + if (_jsonData != null) { + System.arraycopy(_jsonData, 0, dataOut, SECURITY_QUERY_HEADER_SIZE, _jsonSize); + } + if (_bulkData != null) { + System.arraycopy(_bulkData, 0, dataOut, SECURITY_QUERY_HEADER_SIZE + _jsonSize, _bulkDataSize); + } + return dataOut; } public SecurityQueryType getQueryType() { @@ -152,12 +135,12 @@ private void setJsonSize(int _jsonSize) { this._jsonSize = _jsonSize; } - public SecurityQueryErrorCode getErrorCode() { - return _errorCode; + public int getBulkDataSize() { + return _bulkDataSize; } - public void setErrorCode(SecurityQueryErrorCode _errorCode) { - this._errorCode = _errorCode; + private void setBulkDataSize(int _bulkDataSize) { + this._bulkDataSize = _bulkDataSize; } public byte[] getJsonData() { @@ -180,6 +163,13 @@ public byte[] getBulkData() { } public void setBulkData(byte[] _bulkData) { - this._bulkData = _bulkData; + if(_bulkData == null) { + this._bulkDataSize = 0; + this._bulkData = null; + return; + } + this._bulkDataSize = _bulkData.length; + this._bulkData = new byte[this._bulkDataSize]; + System.arraycopy(_bulkData, 0, this._bulkData, 0, _bulkDataSize); } } diff --git a/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java b/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java index 26627a8791..2620f60fcf 100644 --- a/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java +++ b/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java @@ -221,8 +221,8 @@ protected void processControlService(ProtocolMessage msg) { // If the query is of type `Notification` and the id represents a client internal error, we abort the response message and the encryptionManager will not be in state ready. if (receivedHeader.getQueryID() == SecurityQueryID.SEND_INTERNAL_ERROR && receivedHeader.getQueryType() == SecurityQueryType.NOTIFICATION) { - if (receivedHeader.getErrorCode() != null) { - DebugTool.logError(TAG, "Security Query module internal error: " + receivedHeader.getErrorCode().getName()); + if (receivedHeader.getBulkData() != null && receivedHeader.getBulkDataSize() == 1) { + DebugTool.logError(TAG, "Security Query module internal error: " + SecurityQueryErrorCode.valueOf(receivedHeader.getBulkData()[0]).getName()); } else { DebugTool.logError(TAG, "Security Query module error: No information provided"); } @@ -263,17 +263,17 @@ protected void processControlService(ProtocolMessage msg) { jsonData = new byte[0]; } responseHeader.setJsonData(jsonData); - responseHeader.setBulkData(null); - responseHeader.setErrorCode(SecurityQueryErrorCode.ERROR_UNKNOWN_INTERNAL_ERROR); - returnBytes = responseHeader.assembleSecurityQueryPayload(responseHeader.getJsonSize()); + byte[] errorCode = new byte[1]; + errorCode[0] = SecurityQueryErrorCode.ERROR_UNKNOWN_INTERNAL_ERROR.getValue(); + responseHeader.setBulkData(errorCode); } else { responseHeader.setQueryID(SecurityQueryID.SEND_HANDSHAKE_DATA); responseHeader.setQueryType(SecurityQueryType.RESPONSE); responseHeader.setCorrelationID(msg.getCorrID()); responseHeader.setBulkData(dataToRead); responseHeader.setJsonData(null); - returnBytes = responseHeader.assembleSecurityQueryPayload(iNumBytes); } + returnBytes = responseHeader.assembleBinaryData(); ProtocolMessage protocolMessage = new ProtocolMessage(); protocolMessage.setSessionType(SessionType.CONTROL); From a26db32723a082e4f3268487ebbca0add9ee84d0 Mon Sep 17 00:00:00 2001 From: RHeniz Date: Mon, 15 Nov 2021 11:58:41 -0500 Subject: [PATCH 6/7] Add test --- .../test/protocol/SecurityQueryPayloadTests.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/SecurityQueryPayloadTests.java b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/SecurityQueryPayloadTests.java index 3a1cd50a15..c39e9bb42f 100644 --- a/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/SecurityQueryPayloadTests.java +++ b/android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/SecurityQueryPayloadTests.java @@ -120,4 +120,12 @@ public void testNullJsonData() { assertEquals(0, bqh.getJsonSize()); assertEquals(null, bqh.getJsonData()); } + + @Test + public void testNullBulkData() { + SecurityQueryPayload bqh = createDummyBqh(); + bqh.setBulkData(null); + assertEquals(0, bqh.getBulkDataSize()); + assertEquals(null, bqh.getBulkData()); + } } From f094e5620e03f8140a66253d2a4e57054c68fe4b Mon Sep 17 00:00:00 2001 From: RHeniz Date: Tue, 16 Nov 2021 15:06:16 -0500 Subject: [PATCH 7/7] Alignment to iOS --- .../session/BaseSdlSession.java | 83 ++++++++++++------- 1 file changed, 51 insertions(+), 32 deletions(-) diff --git a/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java b/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java index 2620f60fcf..a33d3eb048 100644 --- a/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java +++ b/base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java @@ -65,7 +65,6 @@ import org.json.JSONException; import org.json.JSONObject; -import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.ListIterator; @@ -241,39 +240,28 @@ protected void processControlService(ProtocolMessage msg) { iNumBytes = sdlSecurity.runHandshake(data, dataToRead); - // Assemble a security query payload header for our response - SecurityQueryPayload responseHeader = new SecurityQueryPayload(); - - byte[] returnBytes; + ProtocolMessage protocolMessage; if (iNumBytes == null || iNumBytes <= 0) { DebugTool.logError(TAG, "Internal Error processing control service"); - - responseHeader.setQueryID(SecurityQueryID.SEND_INTERNAL_ERROR); - responseHeader.setQueryType(SecurityQueryType.NOTIFICATION); - responseHeader.setCorrelationID(msg.getCorrID()); - byte[] jsonData; - JSONObject jsonObject = new JSONObject(); - try { - jsonObject.put("id", SecurityQueryErrorCode.ERROR_UNKNOWN_INTERNAL_ERROR.getValue()); - jsonObject.put("text", SecurityQueryErrorCode.ERROR_UNKNOWN_INTERNAL_ERROR.getName()); - jsonData = jsonObject.toString().getBytes(); - } catch (JSONException e) { - DebugTool.logError(TAG, "JSON exception when constructing handshake error Notification"); - e.printStackTrace(); - jsonData = new byte[0]; - } - responseHeader.setJsonData(jsonData); - byte[] errorCode = new byte[1]; - errorCode[0] = SecurityQueryErrorCode.ERROR_UNKNOWN_INTERNAL_ERROR.getValue(); - responseHeader.setBulkData(errorCode); + protocolMessage = serverSecurityFailedMessageWithClientMessageHeader(msg.getCorrID()); } else { - responseHeader.setQueryID(SecurityQueryID.SEND_HANDSHAKE_DATA); - responseHeader.setQueryType(SecurityQueryType.RESPONSE); - responseHeader.setCorrelationID(msg.getCorrID()); - responseHeader.setBulkData(dataToRead); - responseHeader.setJsonData(null); + protocolMessage = serverSecurityHandshakeMessageWithData(msg.getCorrID(), dataToRead); } - returnBytes = responseHeader.assembleBinaryData(); + + //sdlSecurity.hs(); + + sendMessage(protocolMessage); + } + + private ProtocolMessage serverSecurityHandshakeMessageWithData(int correlationId, byte[] bulkData) { + SecurityQueryPayload responseHeader = new SecurityQueryPayload(); + responseHeader.setQueryID(SecurityQueryID.SEND_HANDSHAKE_DATA); + responseHeader.setQueryType(SecurityQueryType.RESPONSE); + responseHeader.setCorrelationID(correlationId); + responseHeader.setBulkData(bulkData); + responseHeader.setJsonData(null); + + byte[] returnBytes = responseHeader.assembleBinaryData(); ProtocolMessage protocolMessage = new ProtocolMessage(); protocolMessage.setSessionType(SessionType.CONTROL); @@ -282,9 +270,40 @@ protected void processControlService(ProtocolMessage msg) { protocolMessage.setVersion((byte) sdlProtocol.getProtocolVersion().getMajor()); protocolMessage.setSessionID((byte) this.sessionId); - //sdlSecurity.hs(); + return protocolMessage; + } - sendMessage(protocolMessage); + private ProtocolMessage serverSecurityFailedMessageWithClientMessageHeader(int correlationId) { + SecurityQueryPayload responseHeader = new SecurityQueryPayload(); + responseHeader.setQueryID(SecurityQueryID.SEND_INTERNAL_ERROR); + responseHeader.setQueryType(SecurityQueryType.NOTIFICATION); + responseHeader.setCorrelationID(correlationId); + byte[] jsonData; + JSONObject jsonObject = new JSONObject(); + try { + jsonObject.put("id", SecurityQueryErrorCode.ERROR_UNKNOWN_INTERNAL_ERROR.getValue()); + jsonObject.put("text", SecurityQueryErrorCode.ERROR_UNKNOWN_INTERNAL_ERROR.getName()); + jsonData = jsonObject.toString().getBytes(); + } catch (JSONException e) { + DebugTool.logError(TAG, "JSON exception when constructing handshake error Notification"); + e.printStackTrace(); + jsonData = new byte[0]; + } + responseHeader.setJsonData(jsonData); + byte[] errorCode = new byte[1]; + errorCode[0] = SecurityQueryErrorCode.ERROR_UNKNOWN_INTERNAL_ERROR.getValue(); + responseHeader.setBulkData(errorCode); + + byte[] returnBytes = responseHeader.assembleBinaryData(); + + ProtocolMessage protocolMessage = new ProtocolMessage(); + protocolMessage.setSessionType(SessionType.CONTROL); + protocolMessage.setData(returnBytes); + protocolMessage.setFunctionID(0x01); + protocolMessage.setVersion((byte) sdlProtocol.getProtocolVersion().getMajor()); + protocolMessage.setSessionID((byte) this.sessionId); + + return protocolMessage; } /**