4444import com .smartdevicelink .protocol .SdlPacket ;
4545import com .smartdevicelink .protocol .SdlProtocolBase ;
4646import com .smartdevicelink .protocol .enums .ControlFrameTags ;
47+ import com .smartdevicelink .protocol .enums .SecurityQueryErrorCode ;
4748import com .smartdevicelink .protocol .enums .SecurityQueryID ;
4849import com .smartdevicelink .protocol .enums .SecurityQueryType ;
4950import com .smartdevicelink .protocol .enums .SessionType ;
6162import com .smartdevicelink .util .SystemInfo ;
6263import com .smartdevicelink .util .Version ;
6364
64- import java .util .ArrayList ;
65+ import org .json .JSONException ;
66+ import org .json .JSONObject ;
67+
6568import java .util .HashMap ;
6669import java .util .List ;
6770import 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