Skip to content

Commit 423836c

Browse files
author
Robert Henigan
authored
Merge pull request #1758 from smartdevicelink/bugfix/issue_1753
Update SecurityQueryPayload to include json data
2 parents b1bf822 + f094e56 commit 423836c

3 files changed

Lines changed: 110 additions & 54 deletions

File tree

android/sdl_android/src/androidTest/java/com/smartdevicelink/test/protocol/SecurityQueryPayloadTests.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static SecurityQueryPayload createDummyBqh() {
2525
bqh.setQueryID(SecurityQueryID.SEND_HANDSHAKE_DATA);
2626
bqh.setQueryType(SecurityQueryType.REQUEST);
2727
bqh.setBulkData(null);
28-
bqh.setJsonSize(0);
28+
bqh.setJsonData(null);
2929
return bqh;
3030
}
3131

@@ -66,9 +66,9 @@ public void testCorrectHeaderAssembly() {
6666
dummyBqh.setQueryType(SecurityQueryType.REQUEST);
6767
dummyBqh.setQueryID(SecurityQueryID.SEND_HANDSHAKE_DATA);
6868
dummyBqh.setCorrelationID(3);
69-
dummyBqh.setJsonSize(0);
69+
dummyBqh.setJsonData(new byte[0]);
7070

71-
byte[] assembledHeader = dummyBqh.assembleHeaderBytes();
71+
byte[] assembledHeader = dummyBqh.assembleBinaryData();
7272
assertEquals(dummyBqh.getQueryType(), SecurityQueryType.valueOf(assembledHeader[0]));
7373
byte[] queryIDFromHeader = new byte[3];
7474
System.arraycopy(assembledHeader, 1, queryIDFromHeader, 0, 3);
@@ -81,7 +81,7 @@ public void testCorrectHeaderAssembly() {
8181
public void testAssemblyAndParse() {
8282
SecurityQueryPayload bqh = createDummyBqh();
8383

84-
byte[] bqhBytes = bqh.assembleHeaderBytes();
84+
byte[] bqhBytes = bqh.assembleBinaryData();
8585
assertNotNull(bqhBytes);
8686

8787
SecurityQueryPayload parsedBqh = SecurityQueryPayload.parseBinaryQueryHeader(bqhBytes);
@@ -99,7 +99,7 @@ public void testAssemblyAndParse() {
9999
public void testCorruptHeader() {
100100
SecurityQueryPayload bqh = createDummyBqh();
101101

102-
byte[] bqhBytes = bqh.assembleHeaderBytes();
102+
byte[] bqhBytes = bqh.assembleBinaryData();
103103

104104
assertNotNull(safeParse(bqhBytes));
105105

@@ -114,13 +114,18 @@ public void testCorruptHeader() {
114114
}
115115

116116
@Test
117-
public void testJsonSetException() {
118-
try {
119-
SecurityQueryPayload bqh = createDummyBqh();
120-
bqh.setJsonData(null);
121-
fail("Setting JSON data to null should have thrown an exception");
122-
} catch (Exception e) {
123-
//Pass
124-
}
117+
public void testNullJsonData() {
118+
SecurityQueryPayload bqh = createDummyBqh();
119+
bqh.setJsonData(null);
120+
assertEquals(0, bqh.getJsonSize());
121+
assertEquals(null, bqh.getJsonData());
122+
}
123+
124+
@Test
125+
public void testNullBulkData() {
126+
SecurityQueryPayload bqh = createDummyBqh();
127+
bqh.setBulkData(null);
128+
assertEquals(0, bqh.getBulkDataSize());
129+
assertEquals(null, bqh.getBulkData());
125130
}
126131
}

base/src/main/java/com/smartdevicelink/protocol/SecurityQueryPayload.java

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class SecurityQueryPayload {
1616
private SecurityQueryID _securityQueryID;
1717
private int _correlationID;
1818
private int _jsonSize;
19-
private SecurityQueryErrorCode _errorCode;
19+
private int _bulkDataSize;
2020

2121
private byte[] _jsonData = null;
2222
private byte[] _bulkData = null;
@@ -51,11 +51,6 @@ public static SecurityQueryPayload parseBinaryQueryHeader(byte[] binHeader) {
5151
int _jsonSize = BitConverter.intFromByteArray(binHeader, 8);
5252
msg.setJsonSize(_jsonSize);
5353

54-
//If we get an error message we want the error code from the last 8 bits
55-
if (msg.getQueryType() == SecurityQueryType.NOTIFICATION && msg.getQueryID() == SecurityQueryID.SEND_INTERNAL_ERROR) {
56-
msg.setErrorCode(SecurityQueryErrorCode.valueOf(binHeader[binHeader.length - 1]));
57-
}
58-
5954
try {
6055
//Get the JsonData after the header (after 96 bits) based on the jsonData size
6156
if (_jsonSize > 0 && _jsonSize <= (binHeader.length - SECURITY_QUERY_HEADER_SIZE)) {
@@ -84,18 +79,28 @@ public static SecurityQueryPayload parseBinaryQueryHeader(byte[] binHeader) {
8479
return msg;
8580
}
8681

87-
public byte[] assembleHeaderBytes() {
82+
public byte[] assembleBinaryData() {
8883
// From the properties, create a data buffer
8984
// Query Type - first 8 bits
9085
// Query ID - next 24 bits
9186
// Sequence Number - next 32 bits
9287
// JSON size - next 32 bits
93-
byte[] ret = new byte[SECURITY_QUERY_HEADER_SIZE];
94-
ret[0] = _securityQueryType.getValue();
95-
System.arraycopy(_securityQueryID.getValue(), 0, ret, 1, 3);
96-
System.arraycopy(BitConverter.intToByteArray(_correlationID), 0, ret, 4, 4);
97-
System.arraycopy(BitConverter.intToByteArray(_jsonSize), 0, ret, 8, 4);
98-
return ret;
88+
byte[] header = new byte[SECURITY_QUERY_HEADER_SIZE];
89+
header[0] = _securityQueryType.getValue();
90+
System.arraycopy(_securityQueryID.getValue(), 0, header, 1, 3);
91+
System.arraycopy(BitConverter.intToByteArray(_correlationID), 0, header, 4, 4);
92+
System.arraycopy(BitConverter.intToByteArray(_jsonSize), 0, header, 8, 4);
93+
94+
int size = _jsonSize + _bulkDataSize + SECURITY_QUERY_HEADER_SIZE;
95+
byte[] dataOut = new byte[size];
96+
System.arraycopy(header, 0, dataOut, 0, SECURITY_QUERY_HEADER_SIZE);
97+
if (_jsonData != null) {
98+
System.arraycopy(_jsonData, 0, dataOut, SECURITY_QUERY_HEADER_SIZE, _jsonSize);
99+
}
100+
if (_bulkData != null) {
101+
System.arraycopy(_bulkData, 0, dataOut, SECURITY_QUERY_HEADER_SIZE + _jsonSize, _bulkDataSize);
102+
}
103+
return dataOut;
99104
}
100105

101106
public SecurityQueryType getQueryType() {
@@ -126,23 +131,29 @@ public int getJsonSize() {
126131
return _jsonSize;
127132
}
128133

129-
public void setJsonSize(int _jsonSize) {
134+
private void setJsonSize(int _jsonSize) {
130135
this._jsonSize = _jsonSize;
131136
}
132137

133-
public SecurityQueryErrorCode getErrorCode() {
134-
return _errorCode;
138+
public int getBulkDataSize() {
139+
return _bulkDataSize;
135140
}
136141

137-
public void setErrorCode(SecurityQueryErrorCode _errorCode) {
138-
this._errorCode = _errorCode;
142+
private void setBulkDataSize(int _bulkDataSize) {
143+
this._bulkDataSize = _bulkDataSize;
139144
}
140145

141146
public byte[] getJsonData() {
142147
return _jsonData;
143148
}
144149

145150
public void setJsonData(byte[] _jsonData) {
151+
if (_jsonData == null) {
152+
this._jsonSize = 0;
153+
this._jsonData = null;
154+
return;
155+
}
156+
this._jsonSize = _jsonData.length;
146157
this._jsonData = new byte[this._jsonSize];
147158
System.arraycopy(_jsonData, 0, this._jsonData, 0, _jsonSize);
148159
}
@@ -152,6 +163,13 @@ public byte[] getBulkData() {
152163
}
153164

154165
public void setBulkData(byte[] _bulkData) {
155-
this._bulkData = _bulkData;
166+
if(_bulkData == null) {
167+
this._bulkDataSize = 0;
168+
this._bulkData = null;
169+
return;
170+
}
171+
this._bulkDataSize = _bulkData.length;
172+
this._bulkData = new byte[this._bulkDataSize];
173+
System.arraycopy(_bulkData, 0, this._bulkData, 0, _bulkDataSize);
156174
}
157175
}

base/src/main/java/com/smartdevicelink/session/BaseSdlSession.java

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import com.smartdevicelink.protocol.SdlPacket;
4545
import com.smartdevicelink.protocol.SdlProtocolBase;
4646
import com.smartdevicelink.protocol.enums.ControlFrameTags;
47+
import com.smartdevicelink.protocol.enums.SecurityQueryErrorCode;
4748
import com.smartdevicelink.protocol.enums.SecurityQueryID;
4849
import com.smartdevicelink.protocol.enums.SecurityQueryType;
4950
import com.smartdevicelink.protocol.enums.SessionType;
@@ -61,7 +62,9 @@
6162
import com.smartdevicelink.util.SystemInfo;
6263
import com.smartdevicelink.util.Version;
6364

64-
import java.util.ArrayList;
65+
import org.json.JSONException;
66+
import org.json.JSONObject;
67+
6568
import java.util.HashMap;
6669
import java.util.List;
6770
import java.util.ListIterator;
@@ -217,8 +220,8 @@ protected void processControlService(ProtocolMessage msg) {
217220
// 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.
218221
if (receivedHeader.getQueryID() == SecurityQueryID.SEND_INTERNAL_ERROR
219222
&& receivedHeader.getQueryType() == SecurityQueryType.NOTIFICATION) {
220-
if (receivedHeader.getErrorCode() != null) {
221-
DebugTool.logError(TAG, "Security Query module internal error: " + receivedHeader.getErrorCode().getName());
223+
if (receivedHeader.getBulkData() != null && receivedHeader.getBulkDataSize() == 1) {
224+
DebugTool.logError(TAG, "Security Query module internal error: " + SecurityQueryErrorCode.valueOf(receivedHeader.getBulkData()[0]).getName());
222225
} else {
223226
DebugTool.logError(TAG, "Security Query module error: No information provided");
224227
}
@@ -237,29 +240,28 @@ protected void processControlService(ProtocolMessage msg) {
237240

238241
iNumBytes = sdlSecurity.runHandshake(data, dataToRead);
239242

240-
// Assemble a security query payload header for our response
241-
SecurityQueryPayload responseHeader = new SecurityQueryPayload();
242-
243-
byte[] returnBytes;
243+
ProtocolMessage protocolMessage;
244244
if (iNumBytes == null || iNumBytes <= 0) {
245245
DebugTool.logError(TAG, "Internal Error processing control service");
246-
247-
responseHeader.setQueryID(SecurityQueryID.SEND_INTERNAL_ERROR);
248-
responseHeader.setQueryType(SecurityQueryType.NOTIFICATION);
249-
responseHeader.setCorrelationID(msg.getCorrID());
250-
responseHeader.setJsonSize(0);
251-
returnBytes = new byte[12];
246+
protocolMessage = serverSecurityFailedMessageWithClientMessageHeader(msg.getCorrID());
252247
} else {
253-
responseHeader.setQueryID(SecurityQueryID.SEND_HANDSHAKE_DATA);
254-
responseHeader.setQueryType(SecurityQueryType.RESPONSE);
255-
responseHeader.setCorrelationID(msg.getCorrID());
256-
responseHeader.setJsonSize(0);
257-
returnBytes = new byte[iNumBytes + 12];
258-
System.arraycopy(dataToRead, 0, returnBytes, 12, iNumBytes);
248+
protocolMessage = serverSecurityHandshakeMessageWithData(msg.getCorrID(), dataToRead);
259249
}
260250

251+
//sdlSecurity.hs();
252+
253+
sendMessage(protocolMessage);
254+
}
255+
256+
private ProtocolMessage serverSecurityHandshakeMessageWithData(int correlationId, byte[] bulkData) {
257+
SecurityQueryPayload responseHeader = new SecurityQueryPayload();
258+
responseHeader.setQueryID(SecurityQueryID.SEND_HANDSHAKE_DATA);
259+
responseHeader.setQueryType(SecurityQueryType.RESPONSE);
260+
responseHeader.setCorrelationID(correlationId);
261+
responseHeader.setBulkData(bulkData);
262+
responseHeader.setJsonData(null);
261263

262-
System.arraycopy(responseHeader.assembleHeaderBytes(), 0, returnBytes, 0, 12);
264+
byte[] returnBytes = responseHeader.assembleBinaryData();
263265

264266
ProtocolMessage protocolMessage = new ProtocolMessage();
265267
protocolMessage.setSessionType(SessionType.CONTROL);
@@ -268,9 +270,40 @@ protected void processControlService(ProtocolMessage msg) {
268270
protocolMessage.setVersion((byte) sdlProtocol.getProtocolVersion().getMajor());
269271
protocolMessage.setSessionID((byte) this.sessionId);
270272

271-
//sdlSecurity.hs();
273+
return protocolMessage;
274+
}
272275

273-
sendMessage(protocolMessage);
276+
private ProtocolMessage serverSecurityFailedMessageWithClientMessageHeader(int correlationId) {
277+
SecurityQueryPayload responseHeader = new SecurityQueryPayload();
278+
responseHeader.setQueryID(SecurityQueryID.SEND_INTERNAL_ERROR);
279+
responseHeader.setQueryType(SecurityQueryType.NOTIFICATION);
280+
responseHeader.setCorrelationID(correlationId);
281+
byte[] jsonData;
282+
JSONObject jsonObject = new JSONObject();
283+
try {
284+
jsonObject.put("id", SecurityQueryErrorCode.ERROR_UNKNOWN_INTERNAL_ERROR.getValue());
285+
jsonObject.put("text", SecurityQueryErrorCode.ERROR_UNKNOWN_INTERNAL_ERROR.getName());
286+
jsonData = jsonObject.toString().getBytes();
287+
} catch (JSONException e) {
288+
DebugTool.logError(TAG, "JSON exception when constructing handshake error Notification");
289+
e.printStackTrace();
290+
jsonData = new byte[0];
291+
}
292+
responseHeader.setJsonData(jsonData);
293+
byte[] errorCode = new byte[1];
294+
errorCode[0] = SecurityQueryErrorCode.ERROR_UNKNOWN_INTERNAL_ERROR.getValue();
295+
responseHeader.setBulkData(errorCode);
296+
297+
byte[] returnBytes = responseHeader.assembleBinaryData();
298+
299+
ProtocolMessage protocolMessage = new ProtocolMessage();
300+
protocolMessage.setSessionType(SessionType.CONTROL);
301+
protocolMessage.setData(returnBytes);
302+
protocolMessage.setFunctionID(0x01);
303+
protocolMessage.setVersion((byte) sdlProtocol.getProtocolVersion().getMajor());
304+
protocolMessage.setSessionID((byte) this.sessionId);
305+
306+
return protocolMessage;
274307
}
275308

276309
/**

0 commit comments

Comments
 (0)