1 /*******************************************************************************
2  *
3  *  Copyright 2018 NXP
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *****************************************************************************/
18 
19 #ifndef LSC_H_
20 #define LSC_H_
21 
22 #include <stdio.h>
23 #include "LsClient.h"
24 #include "phNxpEse_Api.h"
25 
26 extern const size_t HASH_DATA_LENGTH;
27 
28 typedef struct Lsc_ChannelInfo {
29   uint8_t channel_id;
30   bool isOpend;
31 } Lsc_ChannelInfo_t;
32 
33 typedef struct Lsc_TranscieveInfo {
34   int32_t timeout;
35   uint8_t sRecvData[1024];
36   uint8_t sSendData[1024];
37   int32_t sSendlength;
38   int sRecvlength;
39   uint8_t sTemp_recvbuf[1024];
40 } Lsc_TranscieveInfo_t;
41 
42 typedef struct Lsc_ImageInfo {
43   FILE* fp;
44   int fls_size;
45   char fls_path[384];
46   int bytes_read;
47   FILE* fResp;
48   int fls_RespSize;
49   char fls_RespPath[384];
50   int bytes_wrote;
51   Lsc_ChannelInfo_t Channel_Info[10];
52   uint8_t channel_cnt;
53   uint8_t initChannelNum;
54 } Lsc_ImageInfo_t;
55 
56 typedef struct Lsc_HashInfo {
57   uint16_t readHashLen;
58   uint8_t* lsRawScriptBuf = nullptr;
59   uint8_t* lsScriptHash = nullptr;
60   uint8_t* readBuffHash = nullptr;
61 } Lsc_HashInfo_t;
62 
63 typedef enum {
64   LS_Default = 0x00,
65   LS_Cert = 0x7F21,
66   LS_Sign = 0x60,
67   LS_Comm = 0x40
68 } Ls_TagType;
69 
70 static uint8_t OpenChannel[] = {0x00, 0x70, 0x00, 0x00, 0x01};
71 static uint8_t GetData[] = {0x80, 0xCA, 0x00, 0x46, 0x00};
72 
73 static const uint8_t SelectLsc[] = {0xA4, 0x04, 0x00, 0x0E, 0xA0, 0x00,
74                                     0x00, 0x03, 0x96, 0x54, 0x43, 0x00,
75                                     0x00, 0x00, 0x01, 0x00, 0x0B, 0x00};
76 
77 static uint8_t SelectLscSlotHash[] = {0x00, 0xA4, 0x04, 0x00, 0x10, 0xA0, 0x00,
78                                       0x00, 0x03, 0x96, 0x54, 0x53, 0x00, 0x00,
79                                       0x00, 0x01, 0x00, 0x60, 0x00, 0x00, 0x00};
80 
81 #define TAG_CERTIFICATE 0x7F21
82 #define TAG_LSES_RESP 0x4E
83 #define TAG_LSES_RSPLEN 0x02
84 #define TAG_SERIAL_NO 0x93
85 #define TAG_LSRE_ID 0x42
86 #define TAG_LSRE_SIGNID 0x45
87 #define TAG_CERTFHOLD_ID 0x5F20
88 #define TAG_KEY_USAGE 0x95
89 #define TAG_EFF_DATE 0x5F25
90 #define TAG_EXP_DATE 0x5F24
91 #define TAG_CCM_PERMISSION 0x53
92 #define TAG_SIG_RNS_COMP 0x5F37
93 
94 #define TAG_LS_VER1 0x9F
95 #define TAG_LS_VER2 0x08
96 #define LS_DEFAULT_STATUS 0x6340
97 #define LS_SUCCESS_STATUS 0x9000
98 #define TAG_RE_KEYID 0x65
99 
100 #define LS_ABORT_SW1 0x69
101 #define LS_ABORT_SW2 0x87
102 #define LS_STATUS_PATH "/data/vendor/secure_element/LS_Status.txt"
103 #define LS_SRC_BACKUP "/data/vendor/secure_element/LS_Src_Backup.txt"
104 #define LS_DST_BACKUP "/data/vendor/secure_element/LS_Dst_Backup.txt"
105 #define MAX_CERT_LEN (255 + 137)
106 
107 /*LSC2*/
108 
109 #define MAX_SIZE 0xFF
110 #define PARAM_P1_OFFSET 0x02
111 #define FIRST_BLOCK 0x05
112 #define LAST_BLOCK 0x84
113 #define ONLY_BLOCK 0x85
114 #define CLA_BYTE 0x80
115 #define JSBL_HEADER_LEN 0x03
116 #define LSC_CMD_HDR_LEN 0x02
117 
118 /* Definations for TAG ID's present in the script file*/
119 #define TAG_SELECT_ID 0x6F
120 #define TAG_LSC_ID 0x84
121 #define TAG_PRO_DATA_ID 0xA5
122 #define TAG_JSBL_HDR_ID 0x60
123 #define TAG_JSBL_KEY_ID 0x61
124 #define TAG_SIGNATURE_ID 0x41
125 #define TAG_LSC_CMD_ID 0x40
126 #define TAG_JSBL_CER_ID 0x44
127 
128 /*Definitions for Install for load*/
129 #define INSTAL_LOAD_ID 0xE6
130 #define LOAD_CMD_ID 0xE8
131 #define LOAD_MORE_BLOCKS 0x00
132 #define LOAD_LAST_BLOCK 0x80
133 
134 #define STORE_DATA_CLA 0x80
135 #define STORE_DATA_INS 0xE2
136 #define STORE_DATA_LEN 32
137 #define STORE_DATA_TAG 0x4F
138 
139 /*******************************************************************************
140 **
141 ** Function:        Perform_LSC
142 **
143 ** Description:     Performs the LSC download sequence
144 **
145 ** Returns:         Success if ok.
146 **
147 *******************************************************************************/
148 LSCSTATUS Perform_LSC(const char* path, const char* dest, const uint8_t* pdata,
149                       uint16_t len, uint8_t* respSW);
150 
151 /*******************************************************************************
152 **
153 ** Function:        LSC_OpenChannel
154 **
155 ** Description:     Creates the logical channel with lsc
156 **
157 ** Returns:         Success if ok.
158 **
159 *******************************************************************************/
160 static LSCSTATUS LSC_OpenChannel(Lsc_ImageInfo_t* pContext, LSCSTATUS status,
161                                  Lsc_TranscieveInfo_t* pInfo)
162     __attribute__((unused));
163 
164 /*******************************************************************************
165 **
166 ** Function:        LSC_ResetChannel
167 **
168 ** Description:     Reset(Open & Close) next available logical channel
169 **
170 ** Returns:         Success if ok.
171 **
172 *******************************************************************************/
173 static LSCSTATUS LSC_ResetChannel(Lsc_ImageInfo_t* pContext, LSCSTATUS status,
174                                   Lsc_TranscieveInfo_t* pInfo)
175     __attribute__((unused));
176 
177 /*******************************************************************************
178 **
179 ** Function:        LSC_SelectLsc
180 **
181 ** Description:     Creates the logical channel with lsc
182 **                  Channel_id will be used for any communication with Lsc
183 **
184 ** Returns:         Success if ok.
185 **
186 *******************************************************************************/
187 static LSCSTATUS LSC_SelectLsc(Lsc_ImageInfo_t* pContext, LSCSTATUS status,
188                                Lsc_TranscieveInfo_t* pInfo)
189     __attribute__((unused));
190 
191 /*******************************************************************************
192 **
193 ** Function:        LSC_StoreData
194 **
195 ** Description:     It is used to provide the LSC with an Unique
196 **                  Identifier of the Application that has triggered the LSC
197 *script.
198 **
199 ** Returns:         Success if ok.
200 **
201 *******************************************************************************/
202 static LSCSTATUS LSC_StoreData(Lsc_ImageInfo_t* pContext, LSCSTATUS status,
203                                Lsc_TranscieveInfo_t* pInfo)
204     __attribute__((unused));
205 
206 /*******************************************************************************
207 **
208 ** Function:        LSC_loadapplet
209 **
210 ** Description:     Reads the script from the file and sent to Lsc
211 **
212 ** Returns:         Success if ok.
213 **
214 *******************************************************************************/
215 static LSCSTATUS LSC_loadapplet(Lsc_ImageInfo_t* Os_info, LSCSTATUS status,
216                                 Lsc_TranscieveInfo_t* pTranscv_Info)
217     __attribute__((unused));
218 
219 /*******************************************************************************
220 **
221 ** Function:        LSC_update_seq_handler
222 **
223 ** Description:     Performs the LSC update sequence handler sequence
224 **
225 ** Returns:         Success if ok.
226 **
227 *******************************************************************************/
228 static LSCSTATUS LSC_update_seq_handler(
229     LSCSTATUS (*seq_handler[])(Lsc_ImageInfo_t* pContext, LSCSTATUS status,
230                                Lsc_TranscieveInfo_t* pInfo),
231     const char* name, const char* dest) __attribute__((unused));
232 
233 /*******************************************************************************
234 **
235 ** Function:        Write_Response_To_OutFile
236 **
237 ** Description:     Write the response to Out file
238 **                  with length recvlen from buffer RecvData.
239 **
240 ** Returns:         Success if OK
241 **
242 *******************************************************************************/
243 LSCSTATUS Write_Response_To_OutFile(Lsc_ImageInfo_t* image_info,
244                                     uint8_t* RecvData, int32_t recvlen,
245                                     Ls_TagType tType);
246 
247 /*******************************************************************************
248 **
249 ** Function:        Check_Certificate_Tag
250 **
251 ** Description:     Check certificate Tag presence in script
252 **                  by 7F21 .
253 **
254 ** Returns:         Success if Tag found
255 **
256 *******************************************************************************/
257 LSCSTATUS Check_Certificate_Tag(uint8_t* read_buf, uint16_t* offset1);
258 
259 /*******************************************************************************
260 **
261 ** Function:        Check_SerialNo_Tag
262 **
263 ** Description:     Check Serial number Tag presence in script
264 **                  by 0x93 .
265 **
266 ** Returns:         Success if Tag found
267 **
268 *******************************************************************************/
269 LSCSTATUS Check_SerialNo_Tag(uint8_t* read_buf, uint16_t* offset1);
270 
271 /*******************************************************************************
272 **
273 ** Function:        Check_LSRootID_Tag
274 **
275 ** Description:     Check LS root ID tag presence in script and compare with
276 **                  select response root ID value.
277 **
278 ** Returns:         Success if Tag found
279 **
280 *******************************************************************************/
281 LSCSTATUS Check_LSRootID_Tag(uint8_t* read_buf, uint16_t* offset1);
282 
283 /*******************************************************************************
284 **
285 ** Function:        Check_CertHoldID_Tag
286 **
287 ** Description:     Check certificate holder ID tag presence in script.
288 **
289 ** Returns:         Success if Tag found
290 **
291 *******************************************************************************/
292 LSCSTATUS Check_CertHoldID_Tag(uint8_t* read_buf, uint16_t* offset1);
293 
294 /*******************************************************************************
295 **
296 ** Function:        Check_Date_Tag
297 **
298 ** Description:     Check date tags presence in script.
299 **
300 ** Returns:         Success if Tag found
301 **
302 *******************************************************************************/
303 LSCSTATUS Check_Date_Tag(uint8_t* read_buf, uint16_t* offset1);
304 
305 /*******************************************************************************
306 **
307 ** Function:        Check_45_Tag
308 **
309 ** Description:     Check 45 tags presence in script and compare the value
310 **                  with select response tag 45 value
311 **
312 ** Returns:         Success if Tag found
313 **
314 *******************************************************************************/
315 LSCSTATUS Check_45_Tag(uint8_t* read_buf, uint16_t* offset1, uint8_t* tag45Len);
316 
317 /*******************************************************************************
318 **
319 ** Function:        Certificate_Verification
320 **
321 ** Description:     Perform the certificate verification by forwarding it to
322 **                  LS applet.
323 **
324 ** Returns:         Success if certificate is verified
325 **
326 *******************************************************************************/
327 LSCSTATUS Certificate_Verification(Lsc_ImageInfo_t* Os_info,
328                                    Lsc_TranscieveInfo_t* pTranscv_Info,
329                                    uint8_t* read_buf, uint16_t* offset1,
330                                    uint8_t* tag45Len);
331 
332 /*******************************************************************************
333 **
334 ** Function:        Check_Complete_7F21_Tag
335 **
336 ** Description:     Traverses the 7F21 tag for verification of each sub tag with
337 **                  in the 7F21 tag.
338 **
339 ** Returns:         Success if all tags are verified
340 **
341 *******************************************************************************/
342 LSCSTATUS Check_Complete_7F21_Tag(Lsc_ImageInfo_t* Os_info,
343                                   Lsc_TranscieveInfo_t* pTranscv_Info,
344                                   uint8_t* read_buf, uint16_t* offset);
345 
346 /*******************************************************************************
347 **
348 ** Function:        LSC_UpdateExeStatus
349 **
350 ** Description:     Updates LSC status to a file
351 **
352 ** Returns:         true if success else false
353 **
354 *******************************************************************************/
355 bool LSC_UpdateExeStatus(uint16_t status);
356 
357 /*******************************************************************************
358 **
359 ** Function:        Get_LsStatus
360 **
361 ** Description:     Interface to fetch Loader service client status to JNI,
362 *Services
363 **
364 ** Returns:         SUCCESS/FAILURE
365 **
366 *******************************************************************************/
367 LSCSTATUS Get_LsStatus(uint8_t* pVersion);
368 
369 /*******************************************************************************
370 **
371 ** Function:        LSC_SendtoEse
372 **
373 ** Description:     It is used to send the packet to p61
374 **
375 ** Returns:         Success if ok.
376 **
377 *******************************************************************************/
378 LSCSTATUS LSC_SendtoEse(Lsc_ImageInfo_t* Os_info, LSCSTATUS status,
379                         Lsc_TranscieveInfo_t* pTranscv_Info);
380 
381 /*******************************************************************************
382 **
383 ** Function:        LSC_SendtoLsc
384 **
385 ** Description:     It is used to forward the packet to Lsc
386 **
387 ** Returns:         Success if ok.
388 **
389 *******************************************************************************/
390 LSCSTATUS LSC_SendtoLsc(Lsc_ImageInfo_t* Os_info, LSCSTATUS status,
391                         Lsc_TranscieveInfo_t* pTranscv_Info, Ls_TagType tType);
392 
393 /*******************************************************************************
394 **
395 ** Function:        LSC_CloseChannel
396 **
397 ** Description:     Closes the previously opened logical channel
398 **
399 ** Returns:         Success if ok.
400 **
401 *******************************************************************************/
402 LSCSTATUS LSC_CloseChannel(Lsc_ImageInfo_t* Os_info, LSCSTATUS status,
403                            Lsc_TranscieveInfo_t* pTranscv_Info);
404 
405 /*******************************************************************************
406 **
407 ** Function:        LSC_ProcessResp
408 **
409 ** Description:     Process the response packet received from Lsc
410 **
411 ** Returns:         Success if ok.
412 **
413 *******************************************************************************/
414 LSCSTATUS LSC_ProcessResp(Lsc_ImageInfo_t* image_info, int32_t recvlen,
415                           Lsc_TranscieveInfo_t* trans_info, Ls_TagType tType);
416 
417 /*******************************************************************************
418 **
419 ** Function:        LSC_Check_KeyIdentifier
420 **
421 ** Description:     Checks and validates certificate
422 **
423 ** Returns:         Success if ok.
424 **
425 *******************************************************************************/
426 LSCSTATUS LSC_Check_KeyIdentifier(Lsc_ImageInfo_t* Os_info, LSCSTATUS status,
427                                   Lsc_TranscieveInfo_t* pTranscv_Info,
428                                   uint8_t* temp_buf, LSCSTATUS flag,
429                                   int32_t wNewLen);
430 
431 /*******************************************************************************
432 **
433 ** Function:        LSC_ReadScript
434 **
435 ** Description:     Reads the current line if the script
436 **
437 ** Returns:         Success if ok.
438 **
439 *******************************************************************************/
440 LSCSTATUS LSC_ReadScript(Lsc_ImageInfo_t* Os_info, uint8_t* read_buf);
441 
442 /*******************************************************************************
443 **
444 ** Function:        Process_EseResponse
445 **
446 ** Description:     It is used to process the received response packet from ESE
447 **
448 ** Returns:         Success if ok.
449 **
450 *******************************************************************************/
451 LSCSTATUS Process_EseResponse(Lsc_TranscieveInfo_t* pTranscv_Info,
452                               int32_t recv_len, Lsc_ImageInfo_t* Os_info);
453 
454 /*******************************************************************************
455 **
456 ** Function:        Process_SelectRsp
457 **
458 ** Description:     It is used to process the received response for SELECT LSC
459 *cmd
460 **
461 ** Returns:         Success if ok.
462 **
463 *******************************************************************************/
464 LSCSTATUS Process_SelectRsp(uint8_t* Recv_data, int32_t Recv_len);
465 
466 /*******************************************************************************
467 **
468 ** Function:        LSC_CloseAllLogicalChannels
469 **
470 ** Description:     Close all opened logical channels
471 **
472 ** Returns:         SUCCESS/FAILURE
473 **
474 *******************************************************************************/
475 LSCSTATUS LSC_CloseAllLogicalChannels(Lsc_ImageInfo_t* Os_info);
476 
477 /*******************************************************************************
478 **
479 ** Function:        LSC_SelectLsHash
480 **
481 ** Description:     Selects LS Hash applet
482 **
483 ** Returns:         SUCCESS/FAILURE
484 **
485 *******************************************************************************/
486 
487 LSCSTATUS LSC_SelectLsHash();
488 
489 /*******************************************************************************
490 **
491 ** Function:        LSC_ReadLsHash
492 **
493 ** Description:     Read the LS SHA1 for the intended slot
494 **
495 ** Returns:         SUCCESS/FAILURE
496 **
497 *******************************************************************************/
498 LSCSTATUS LSC_ReadLsHash(uint8_t* hash, uint16_t* readHashLen, uint8_t slotId);
499 
500 /*******************************************************************************
501 **
502 ** Function:        LSC_UpdateLsHash
503 **
504 ** Description:     Updates SHA1 of LS script to the respective Slot ID
505 **
506 ** Returns:         Update status
507 **
508 *******************************************************************************/
509 LSCSTATUS LSC_UpdateLsHash(uint8_t* hash, long hashLen, uint8_t slotId);
510 
511 /*******************************************************************************
512 **
513 ** Function:        LSC_ReadLscInfo
514 **
515 ** Description:     Read the info of LS applet
516 **
517 ** Returns:         SUCCESS/FAILURE
518 **
519 *******************************************************************************/
520 LSCSTATUS LSC_ReadLscInfo(uint8_t* state, uint16_t* version);
521 
522 /*******************************************************************************
523 **
524 ** Function:        Numof_lengthbytes
525 **
526 ** Description:     Checks the number of length bytes and assigns
527 **                  length value to wLen.
528 **
529 ** Returns:         Number of Length bytes
530 **
531 *******************************************************************************/
532 uint8_t Numof_lengthbytes(uint8_t* read_buf, int32_t* wLen);
533 
534 LSCSTATUS Send_Backall_Loadcmds(Lsc_ImageInfo_t* Os_info, LSCSTATUS status,
535                                 Lsc_TranscieveInfo_t* pTranscv_Info);
536 
537 LSCSTATUS Bufferize_load_cmds(Lsc_ImageInfo_t* Os_info, LSCSTATUS status,
538                               Lsc_TranscieveInfo_t* pTranscv_Info);
539 
FSCANF_BYTE(FILE * stream,const char * format,void * pVal)540 inline int FSCANF_BYTE(FILE* stream, const char* format, void* pVal) {
541   int Result = 0;
542 
543   if ((NULL != stream) && (NULL != format) && (NULL != pVal)) {
544     unsigned int dwVal;
545     unsigned char* pTmp = (unsigned char*)pVal;
546     Result = fscanf(stream, format, &dwVal);
547 
548     (*pTmp) = (unsigned char)(dwVal & 0x000000FF);
549   }
550   return Result;
551 }
552 
553 #endif /*LSC_H*/
554