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 typedef struct Lsc_ChannelInfo {
27   uint8_t channel_id;
28   bool isOpend;
29 } Lsc_ChannelInfo_t;
30 
31 typedef struct Lsc_TranscieveInfo {
32   int32_t timeout;
33   uint8_t sRecvData[1024];
34   uint8_t sSendData[1024];
35   int32_t sSendlength;
36   int sRecvlength;
37   uint8_t sTemp_recvbuf[1024];
38 } Lsc_TranscieveInfo_t;
39 
40 typedef struct Lsc_ImageInfo {
41   FILE* fp;
42   int fls_size;
43   char fls_path[384];
44   int bytes_read;
45   FILE* fResp;
46   int fls_RespSize;
47   char fls_RespPath[384];
48   int bytes_wrote;
49   Lsc_ChannelInfo_t Channel_Info[10];
50   uint8_t channel_cnt;
51 } Lsc_ImageInfo_t;
52 
53 typedef enum {
54   LS_Default = 0x00,
55   LS_Cert = 0x7F21,
56   LS_Sign = 0x60,
57   LS_Comm = 0x40
58 } Ls_TagType;
59 
60 static uint8_t OpenChannel[] = {0x00, 0x70, 0x00, 0x00, 0x01};
61 static uint8_t GetData[] = {0x80, 0xCA, 0x00, 0x46, 0x00};
62 
63 static uint8_t SelectLsc[] = {0x00, 0xA4, 0x04, 0x00, 0x0F, 0xA0, 0x00,
64                               0x00, 0x03, 0x96, 0x54, 0x43, 0x00, 0x00,
65                               0x00, 0x01, 0x00, 0x0B, 0x00, 0x01};
66 
67 /*LSC2*/
68 #define NOOFAIDS 0x03
69 #define LENOFAIDS 0x16
70 
71 static uint8_t ArrayOfAIDs[NOOFAIDS][LENOFAIDS] = {
72     {0x14, 0x00, 0xA4, 0x04, 0x00, 0x0F, 0xA0, 0x00, 0x00, 0x03, 0x96,
73      0x54, 0x43, 0x00, 0x00, 0x00, 0x01, 0x00, 0x0B, 0x00, 0x02, 0x00},
74     {0x14, 0x00, 0xA4, 0x04, 0x00, 0x0F, 0xA0, 0x00, 0x00, 0x03, 0x96,
75      0x54, 0x43, 0x00, 0x00, 0x00, 0x01, 0x00, 0x0B, 0x00, 0x01, 0x00},
76     {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
77      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
78 
79 #define TAG_CERTIFICATE 0x7F21
80 #define TAG_LSES_RESP 0x4E
81 #define TAG_LSES_RSPLEN 0x02
82 #define TAG_SERIAL_NO 0x93
83 #define TAG_LSRE_ID 0x42
84 #define TAG_LSRE_SIGNID 0x45
85 #define TAG_CERTFHOLD_ID 0x5F20
86 #define TAG_KEY_USAGE 0x95
87 #define TAG_EFF_DATE 0x5F25
88 #define TAG_EXP_DATE 0x5F24
89 #define TAG_CCM_PERMISSION 0x53
90 #define TAG_SIG_RNS_COMP 0x5F37
91 
92 #define TAG_LS_VER1 0x9F
93 #define TAG_LS_VER2 0x08
94 #define LS_DEFAULT_STATUS 0x6340
95 #define LS_SUCCESS_STATUS 0x9000
96 #define TAG_RE_KEYID 0x65
97 
98 #define LS_ABORT_SW1 0x69
99 #define LS_ABORT_SW2 0x87
100 #define AID_MEM_PATH "/data/vendor/secure_element/AID_MEM.txt"
101 #define LS_STATUS_PATH "/data/vendor/secure_element/LS_Status.txt"
102 #define LS_SRC_BACKUP "/data/vendor/secure_element/LS_Src_Backup.txt"
103 #define LS_DST_BACKUP "/data/vendor/secure_element/LS_Dst_Backup.txt"
104 #define MAX_CERT_LEN (255 + 137)
105 
106 /*LSC2*/
107 
108 #define MAX_SIZE 0xFF
109 #define PARAM_P1_OFFSET 0x02
110 #define FIRST_BLOCK 0x05
111 #define LAST_BLOCK 0x84
112 #define ONLY_BLOCK 0x85
113 #define CLA_BYTE 0x80
114 #define JSBL_HEADER_LEN 0x03
115 #define LSC_CMD_HDR_LEN 0x02
116 
117 /* Definations for TAG ID's present in the script file*/
118 #define TAG_SELECT_ID 0x6F
119 #define TAG_LSC_ID 0x84
120 #define TAG_PRO_DATA_ID 0xA5
121 #define TAG_JSBL_HDR_ID 0x60
122 #define TAG_JSBL_KEY_ID 0x61
123 #define TAG_SIGNATURE_ID 0x41
124 #define TAG_LSC_CMD_ID 0x40
125 #define TAG_JSBL_CER_ID 0x44
126 
127 /*Definitions for Install for load*/
128 #define INSTAL_LOAD_ID 0xE6
129 #define LOAD_CMD_ID 0xE8
130 #define LOAD_MORE_BLOCKS 0x00
131 #define LOAD_LAST_BLOCK 0x80
132 
133 #define STORE_DATA_CLA 0x80
134 #define STORE_DATA_INS 0xE2
135 #define STORE_DATA_LEN 32
136 #define STORE_DATA_TAG 0x4F
137 
138 /*******************************************************************************
139 **
140 ** Function:        Perform_LSC
141 **
142 ** Description:     Performs the LSC download sequence
143 **
144 ** Returns:         Success if ok.
145 **
146 *******************************************************************************/
147 LSCSTATUS Perform_LSC(const char* path, const char* dest, const uint8_t* pdata,
148                       uint16_t len, uint8_t* respSW);
149 
150 /*******************************************************************************
151 **
152 ** Function:        LSC_OpenChannel
153 **
154 ** Description:     Creates the logical channel with lsc
155 **
156 ** Returns:         Success if ok.
157 **
158 *******************************************************************************/
159 static LSCSTATUS LSC_OpenChannel(Lsc_ImageInfo_t* pContext, LSCSTATUS status,
160                                  Lsc_TranscieveInfo_t* pInfo)
161     __attribute__((unused));
162 
163 /*******************************************************************************
164 **
165 ** Function:        LSC_SelectLsc
166 **
167 ** Description:     Creates the logical channel with lsc
168 **                  Channel_id will be used for any communication with Lsc
169 **
170 ** Returns:         Success if ok.
171 **
172 *******************************************************************************/
173 static LSCSTATUS LSC_SelectLsc(Lsc_ImageInfo_t* pContext, LSCSTATUS status,
174                                Lsc_TranscieveInfo_t* pInfo)
175     __attribute__((unused));
176 
177 /*******************************************************************************
178 **
179 ** Function:        LSC_StoreData
180 **
181 ** Description:     It is used to provide the LSC with an Unique
182 **                  Identifier of the Application that has triggered the LSC
183 *script.
184 **
185 ** Returns:         Success if ok.
186 **
187 *******************************************************************************/
188 static LSCSTATUS LSC_StoreData(Lsc_ImageInfo_t* pContext, LSCSTATUS status,
189                                Lsc_TranscieveInfo_t* pInfo)
190     __attribute__((unused));
191 
192 /*******************************************************************************
193 **
194 ** Function:        LSC_loadapplet
195 **
196 ** Description:     Reads the script from the file and sent to Lsc
197 **
198 ** Returns:         Success if ok.
199 **
200 *******************************************************************************/
201 static LSCSTATUS LSC_loadapplet(Lsc_ImageInfo_t* Os_info, LSCSTATUS status,
202                                 Lsc_TranscieveInfo_t* pTranscv_Info)
203     __attribute__((unused));
204 
205 /*******************************************************************************
206 **
207 ** Function:        LSC_update_seq_handler
208 **
209 ** Description:     Performs the LSC update sequence handler sequence
210 **
211 ** Returns:         Success if ok.
212 **
213 *******************************************************************************/
214 static LSCSTATUS LSC_update_seq_handler(
215     LSCSTATUS (*seq_handler[])(Lsc_ImageInfo_t* pContext, LSCSTATUS status,
216                                Lsc_TranscieveInfo_t* pInfo),
217     const char* name, const char* dest) __attribute__((unused));
218 
219 /*******************************************************************************
220 **
221 ** Function:        Write_Response_To_OutFile
222 **
223 ** Description:     Write the response to Out file
224 **                  with length recvlen from buffer RecvData.
225 **
226 ** Returns:         Success if OK
227 **
228 *******************************************************************************/
229 LSCSTATUS Write_Response_To_OutFile(Lsc_ImageInfo_t* image_info,
230                                     uint8_t* RecvData, int32_t recvlen,
231                                     Ls_TagType tType);
232 
233 /*******************************************************************************
234 **
235 ** Function:        Check_Certificate_Tag
236 **
237 ** Description:     Check certificate Tag presence in script
238 **                  by 7F21 .
239 **
240 ** Returns:         Success if Tag found
241 **
242 *******************************************************************************/
243 LSCSTATUS Check_Certificate_Tag(uint8_t* read_buf, uint16_t* offset1);
244 
245 /*******************************************************************************
246 **
247 ** Function:        Check_SerialNo_Tag
248 **
249 ** Description:     Check Serial number Tag presence in script
250 **                  by 0x93 .
251 **
252 ** Returns:         Success if Tag found
253 **
254 *******************************************************************************/
255 LSCSTATUS Check_SerialNo_Tag(uint8_t* read_buf, uint16_t* offset1);
256 
257 /*******************************************************************************
258 **
259 ** Function:        Check_LSRootID_Tag
260 **
261 ** Description:     Check LS root ID tag presence in script and compare with
262 **                  select response root ID value.
263 **
264 ** Returns:         Success if Tag found
265 **
266 *******************************************************************************/
267 LSCSTATUS Check_LSRootID_Tag(uint8_t* read_buf, uint16_t* offset1);
268 
269 /*******************************************************************************
270 **
271 ** Function:        Check_CertHoldID_Tag
272 **
273 ** Description:     Check certificate holder ID tag presence in script.
274 **
275 ** Returns:         Success if Tag found
276 **
277 *******************************************************************************/
278 LSCSTATUS Check_CertHoldID_Tag(uint8_t* read_buf, uint16_t* offset1);
279 
280 /*******************************************************************************
281 **
282 ** Function:        Check_Date_Tag
283 **
284 ** Description:     Check date tags presence in script.
285 **
286 ** Returns:         Success if Tag found
287 **
288 *******************************************************************************/
289 LSCSTATUS Check_Date_Tag(uint8_t* read_buf, uint16_t* offset1);
290 
291 /*******************************************************************************
292 **
293 ** Function:        Check_45_Tag
294 **
295 ** Description:     Check 45 tags presence in script and compare the value
296 **                  with select response tag 45 value
297 **
298 ** Returns:         Success if Tag found
299 **
300 *******************************************************************************/
301 LSCSTATUS Check_45_Tag(uint8_t* read_buf, uint16_t* offset1, uint8_t* tag45Len);
302 
303 /*******************************************************************************
304 **
305 ** Function:        Certificate_Verification
306 **
307 ** Description:     Perform the certificate verification by forwarding it to
308 **                  LS applet.
309 **
310 ** Returns:         Success if certificate is verified
311 **
312 *******************************************************************************/
313 LSCSTATUS Certificate_Verification(Lsc_ImageInfo_t* Os_info,
314                                    Lsc_TranscieveInfo_t* pTranscv_Info,
315                                    uint8_t* read_buf, uint16_t* offset1,
316                                    uint8_t* tag45Len);
317 
318 /*******************************************************************************
319 **
320 ** Function:        Check_Complete_7F21_Tag
321 **
322 ** Description:     Traverses the 7F21 tag for verification of each sub tag with
323 **                  in the 7F21 tag.
324 **
325 ** Returns:         Success if all tags are verified
326 **
327 *******************************************************************************/
328 LSCSTATUS Check_Complete_7F21_Tag(Lsc_ImageInfo_t* Os_info,
329                                   Lsc_TranscieveInfo_t* pTranscv_Info,
330                                   uint8_t* read_buf, uint16_t* offset);
331 
332 /*******************************************************************************
333 **
334 ** Function:        LSC_UpdateExeStatus
335 **
336 ** Description:     Updates LSC status to a file
337 **
338 ** Returns:         true if success else false
339 **
340 *******************************************************************************/
341 bool LSC_UpdateExeStatus(uint16_t status);
342 
343 /*******************************************************************************
344 **
345 ** Function:        Get_LsStatus
346 **
347 ** Description:     Interface to fetch Loader service client status to JNI,
348 *Services
349 **
350 ** Returns:         SUCCESS/FAILURE
351 **
352 *******************************************************************************/
353 LSCSTATUS Get_LsStatus(uint8_t* pVersion);
354 
355 /*******************************************************************************
356 **
357 ** Function:        LSC_SendtoEse
358 **
359 ** Description:     It is used to send the packet to p61
360 **
361 ** Returns:         Success if ok.
362 **
363 *******************************************************************************/
364 LSCSTATUS LSC_SendtoEse(Lsc_ImageInfo_t* Os_info, LSCSTATUS status,
365                         Lsc_TranscieveInfo_t* pTranscv_Info);
366 
367 /*******************************************************************************
368 **
369 ** Function:        LSC_SendtoLsc
370 **
371 ** Description:     It is used to forward the packet to Lsc
372 **
373 ** Returns:         Success if ok.
374 **
375 *******************************************************************************/
376 LSCSTATUS LSC_SendtoLsc(Lsc_ImageInfo_t* Os_info, LSCSTATUS status,
377                         Lsc_TranscieveInfo_t* pTranscv_Info, Ls_TagType tType);
378 
379 /*******************************************************************************
380 **
381 ** Function:        LSC_CloseChannel
382 **
383 ** Description:     Closes the previously opened logical channel
384 **
385 ** Returns:         Success if ok.
386 **
387 *******************************************************************************/
388 LSCSTATUS LSC_CloseChannel(Lsc_ImageInfo_t* Os_info, LSCSTATUS status,
389                            Lsc_TranscieveInfo_t* pTranscv_Info);
390 
391 /*******************************************************************************
392 **
393 ** Function:        LSC_ProcessResp
394 **
395 ** Description:     Process the response packet received from Lsc
396 **
397 ** Returns:         Success if ok.
398 **
399 *******************************************************************************/
400 LSCSTATUS LSC_ProcessResp(Lsc_ImageInfo_t* image_info, int32_t recvlen,
401                           Lsc_TranscieveInfo_t* trans_info, Ls_TagType tType);
402 
403 /*******************************************************************************
404 **
405 ** Function:        LSC_Check_KeyIdentifier
406 **
407 ** Description:     Checks and validates certificate
408 **
409 ** Returns:         Success if ok.
410 **
411 *******************************************************************************/
412 LSCSTATUS LSC_Check_KeyIdentifier(Lsc_ImageInfo_t* Os_info, LSCSTATUS status,
413                                   Lsc_TranscieveInfo_t* pTranscv_Info,
414                                   uint8_t* temp_buf, LSCSTATUS flag,
415                                   int32_t wNewLen);
416 
417 /*******************************************************************************
418 **
419 ** Function:        LSC_ReadScript
420 **
421 ** Description:     Reads the current line if the script
422 **
423 ** Returns:         Success if ok.
424 **
425 *******************************************************************************/
426 LSCSTATUS LSC_ReadScript(Lsc_ImageInfo_t* Os_info, uint8_t* read_buf);
427 
428 /*******************************************************************************
429 **
430 ** Function:        Process_EseResponse
431 **
432 ** Description:     It is used to process the received response packet from ESE
433 **
434 ** Returns:         Success if ok.
435 **
436 *******************************************************************************/
437 LSCSTATUS Process_EseResponse(Lsc_TranscieveInfo_t* pTranscv_Info,
438                               int32_t recv_len, Lsc_ImageInfo_t* Os_info);
439 
440 /*******************************************************************************
441 **
442 ** Function:        Process_SelectRsp
443 **
444 ** Description:     It is used to process the received response for SELECT LSC
445 *cmd
446 **
447 ** Returns:         Success if ok.
448 **
449 *******************************************************************************/
450 LSCSTATUS Process_SelectRsp(uint8_t* Recv_data, int32_t Recv_len);
451 
452 /*******************************************************************************
453 **
454 ** Function:        Numof_lengthbytes
455 **
456 ** Description:     Checks the number of length bytes and assigns
457 **                  length value to wLen.
458 **
459 ** Returns:         Number of Length bytes
460 **
461 *******************************************************************************/
462 uint8_t Numof_lengthbytes(uint8_t* read_buf, int32_t* wLen);
463 
464 LSCSTATUS Send_Backall_Loadcmds(Lsc_ImageInfo_t* Os_info, LSCSTATUS status,
465                                 Lsc_TranscieveInfo_t* pTranscv_Info);
466 
467 LSCSTATUS Bufferize_load_cmds(Lsc_ImageInfo_t* Os_info, LSCSTATUS status,
468                               Lsc_TranscieveInfo_t* pTranscv_Info);
469 
FSCANF_BYTE(FILE * stream,const char * format,void * pVal)470 inline int FSCANF_BYTE(FILE* stream, const char* format, void* pVal) {
471   int Result = 0;
472 
473   if ((NULL != stream) && (NULL != format) && (NULL != pVal)) {
474     unsigned int dwVal;
475     unsigned char* pTmp = (unsigned char*)pVal;
476     Result = fscanf(stream, format, &dwVal);
477 
478     (*pTmp) = (unsigned char)(dwVal & 0x000000FF);
479   }
480   return Result;
481 }
482 
483 #endif /*LSC_H*/
484