1 /*****************************************************************************
2  * Copyright ©2017-2019 Gemalto – a Thales Company. All rights Reserved.
3  *
4  * This copy is licensed under the Apache License, Version 2.0 (the "License");
5  * You may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *     http://www.apache.org/licenses/LICENSE-2.0 or https://www.apache.org/licenses/LICENSE-2.0.html
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10  * See the License for the specific language governing permissions and limitations under the License.
11 
12  ****************************************************************************/
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <ctype.h>
17 #include <inttypes.h>
18 #include <errno.h>
19 #include <getopt.h>
20 #include <libgen.h>
21 #include <signal.h>
22 #include <limits.h>
23 #include <log/log.h>
24 
25 #include "se-gto/libse-gto.h"
26 #include "SecureElement.h"
27 
28 
29 //#include "profile.h"
30 //#include "settings.h"
31 
32 namespace android {
33 namespace hardware {
34 namespace secure_element {
35 namespace V1_0 {
36 namespace implementation {
37 
38 #ifndef MAX_CHANNELS
39 #define MAX_CHANNELS 0x04
40 #endif
41 
42 #ifndef BASIC_CHANNEL
43 #define BASIC_CHANNEL 0x00
44 #endif
45 
46 #ifndef LOG_HAL_LEVEL
47 #define LOG_HAL_LEVEL 4
48 #endif
49 
50 static struct se_gto_ctx *ctx;
51 
SecureElement(const char * ese_name)52 SecureElement::SecureElement(const char* ese_name){
53     nbrOpenChannel = 0;
54     ctx = NULL;
55 
56     if (strcmp(ese_name, "eSE2") == 0) {
57         strcpy( config_filename, "/vendor/etc/libse-gto-hal2.conf");
58     } else {
59         strcpy( config_filename, "/vendor/etc/libse-gto-hal.conf");
60     }
61 }
62 
resetSE()63 int SecureElement::resetSE(){
64     int n;
65 
66     isBasicChannelOpen = false;
67     nbrOpenChannel = 0;
68 
69     ALOGD("SecureElement:%s se_gto_reset start", __func__);
70     n = se_gto_reset(ctx, atr, sizeof(atr));
71     if (n >= 0) {
72         atr_size = n;
73         ALOGD("SecureElement:%s received ATR of %d bytes\n", __func__, n);
74         dump_bytes("ATR: ", ':',  atr, n, stdout);
75     } else {
76         ALOGE("SecureElement:%s Failed to reset and get ATR: %s\n", __func__, strerror(errno));
77     }
78 
79     return n;
80 }
81 
82 sp<V1_0::ISecureElementHalCallback> SecureElement::internalClientCallback = nullptr;
initializeSE()83 int SecureElement::initializeSE() {
84 
85     int n;
86 
87     ALOGD("SecureElement:%s start", __func__);
88 
89     if (checkSeUp) {
90         ALOGD("SecureElement:%s Already initialized", __func__);
91         ALOGD("SecureElement:%s end", __func__);
92         return EXIT_SUCCESS;
93     }
94 
95     if (se_gto_new(&ctx) < 0) {
96         ALOGE("SecureElement:%s se_gto_new FATAL:%s", __func__,strerror(errno));
97 
98         return EXIT_FAILURE;
99     }
100     //settings = default_settings(ctx);
101     se_gto_set_log_level(ctx, 4);
102 
103     openConfigFile(1);
104 
105     if (se_gto_open(ctx) < 0) {
106         ALOGE("SecureElement:%s se_gto_open FATAL:%s", __func__,strerror(errno));
107         return EXIT_FAILURE;
108     }
109 
110     if (resetSE() < 0) {
111         se_gto_close(ctx);
112         ctx = NULL;
113         return EXIT_FAILURE;
114     }
115 
116     checkSeUp = true;
117 
118     ALOGD("SecureElement:%s end", __func__);
119     return EXIT_SUCCESS;
120 }
121 
122 // Methods from ::android::hardware::secure_element::V1_0::ISecureElement follow.
init(const sp<::android::hardware::secure_element::V1_0::ISecureElementHalCallback> & clientCallback)123 Return<void> SecureElement::init(const sp<::android::hardware::secure_element::V1_0::ISecureElementHalCallback>& clientCallback) {
124 
125     ALOGD("SecureElement:%s start", __func__);
126     if (clientCallback == nullptr) {
127         ALOGE("SecureElement:%s clientCallback == nullptr", __func__);
128         return Void();
129     } else {
130         internalClientCallback = clientCallback;
131         if (!internalClientCallback->linkToDeath(this, 0)) {
132             ALOGE("SecureElement:%s: linkToDeath Failed", __func__);
133         }
134     }
135 
136     if (initializeSE() != EXIT_SUCCESS) {
137         ALOGE("SecureElement:%s initializeSE Failed", __func__);
138         clientCallback->onStateChange(false);
139     } else {
140         ALOGD("SecureElement:%s initializeSE Success", __func__);
141         clientCallback->onStateChange(true);
142     }
143 
144     ALOGD("SecureElement:%s end", __func__);
145 
146     return Void();
147 }
148 
getAtr(getAtr_cb _hidl_cb)149 Return<void> SecureElement::getAtr(getAtr_cb _hidl_cb) {
150     hidl_vec<uint8_t> response;
151     response.resize(atr_size);
152     memcpy(&response[0], atr, atr_size);
153     _hidl_cb(response);
154     return Void();
155 }
156 
isCardPresent()157 Return<bool> SecureElement::isCardPresent() {
158     return true;
159 }
160 
transmit(const hidl_vec<uint8_t> & data,transmit_cb _hidl_cb)161 Return<void> SecureElement::transmit(const hidl_vec<uint8_t>& data, transmit_cb _hidl_cb) {
162 
163     uint8_t *apdu;
164     uint8_t *resp;
165     int status = 0 ;
166     int apdu_len = data.size();
167     int resp_len = 0;
168 
169     apdu = (uint8_t*)malloc(apdu_len * sizeof(uint8_t));
170     resp = (uint8_t*)malloc(65536 * sizeof(uint8_t));
171 
172     hidl_vec<uint8_t> result;
173 
174     if (checkSeUp && nbrOpenChannel != 0) {
175         if (apdu != NULL) {
176             memcpy(apdu, data.data(), data.size());
177             dump_bytes("CMD: ", ':', apdu, apdu_len, stdout);
178             resp_len = se_gto_apdu_transmit(ctx, apdu, apdu_len, resp, 65536);
179         }
180 
181         if (resp_len < 0) {
182             ALOGE("SecureElement:%s: transmit failed", __func__);
183             if (deinitializeSE() != SecureElementStatus::SUCCESS) {
184                 ALOGE("SecureElement:%s deinitializeSE Failed", __func__);
185             }
186         } else {
187             dump_bytes("RESP: ", ':', resp, resp_len, stdout);
188             result.resize(resp_len);
189             memcpy(&result[0], resp, resp_len);
190         }
191     } else {
192         ALOGE("SecureElement:%s: transmit failed! No channel is open", __func__);
193     }
194     _hidl_cb(result);
195     if(apdu) free(apdu);
196     if(resp) free(resp);
197     return Void();
198 }
199 
openLogicalChannel(const hidl_vec<uint8_t> & aid,uint8_t p2,openLogicalChannel_cb _hidl_cb)200 Return<void> SecureElement::openLogicalChannel(const hidl_vec<uint8_t>& aid, uint8_t p2, openLogicalChannel_cb _hidl_cb) {
201     ALOGD("SecureElement:%s start", __func__);
202 
203     LogicalChannelResponse resApduBuff;
204     resApduBuff.channelNumber = 0xff;
205     memset(&resApduBuff, 0x00, sizeof(resApduBuff));
206 
207     if (!checkSeUp) {
208         if (initializeSE() != EXIT_SUCCESS) {
209             ALOGE("SecureElement:%s: Failed to re-initialise the eSE HAL", __func__);
210             internalClientCallback->onStateChange(false);
211             _hidl_cb(resApduBuff, SecureElementStatus::IOERROR);
212             return Void();
213         }
214     }
215 
216     SecureElementStatus mSecureElementStatus = SecureElementStatus::IOERROR;
217 
218     uint8_t *apdu; //65536
219     int apdu_len = 0;
220     uint8_t *resp;
221     int resp_len = 0;
222     uint8_t index = 0;
223 
224     apdu_len = 5;
225     apdu = (uint8_t*)malloc(apdu_len * sizeof(uint8_t));
226     resp = (uint8_t*)malloc(65536 * sizeof(uint8_t));
227 
228     if (apdu != NULL && resp!=NULL) {
229         index = 0;
230         apdu[index++] = 0x00;
231         apdu[index++] = 0x70;
232         apdu[index++] = 0x00;
233         apdu[index++] = 0x00;
234         apdu[index++] = 0x01;
235 
236         dump_bytes("CMD: ", ':', apdu, apdu_len, stdout);
237 
238         resp_len = se_gto_apdu_transmit(ctx, apdu, apdu_len, resp, 65536);
239         ALOGD("SecureElement:%s Manage channel resp_len = %d", __func__,resp_len);
240     }
241 
242     if (resp_len >= 0)
243         dump_bytes("RESP: ", ':', resp, resp_len, stdout);
244 
245 
246     if (resp_len < 0) {
247         if (deinitializeSE() != SecureElementStatus::SUCCESS) {
248              ALOGE("SecureElement:%s deinitializeSE Failed", __func__);
249         }
250         mSecureElementStatus = SecureElementStatus::IOERROR;
251         ALOGD("SecureElement:%s Free memory after manage channel after ERROR", __func__);
252         if(apdu) free(apdu);
253         if(resp) free(resp);
254         _hidl_cb(resApduBuff, mSecureElementStatus);
255         return Void();
256     } else if (resp[resp_len - 2] == 0x90 && resp[resp_len - 1] == 0x00) {
257         resApduBuff.channelNumber = resp[0];
258         nbrOpenChannel++;
259         mSecureElementStatus = SecureElementStatus::SUCCESS;
260     } else {
261         if (resp[resp_len - 2] == 0x6A && resp[resp_len - 1] == 0x81) {
262             mSecureElementStatus = SecureElementStatus::CHANNEL_NOT_AVAILABLE;
263         }else if (resp[resp_len - 2] == 0x68 && resp[resp_len - 1] == 0x81) {
264             mSecureElementStatus = SecureElementStatus::CHANNEL_NOT_AVAILABLE;
265         } else {
266             mSecureElementStatus = SecureElementStatus::IOERROR;
267         }
268         ALOGD("SecureElement:%s Free memory after manage channel after ERROR", __func__);
269         if(apdu) free(apdu);
270         if(resp) free(resp);
271         _hidl_cb(resApduBuff, mSecureElementStatus);
272         return Void();
273     }
274 
275     if(apdu) free(apdu);
276     if(resp) free(resp);
277     ALOGD("SecureElement:%s Free memory after manage channel", __func__);
278     ALOGD("SecureElement:%s mSecureElementStatus = %d", __func__, (int)mSecureElementStatus);
279 
280     /*Start Sending select command after Manage Channel is successful.*/
281     ALOGD("SecureElement:%s Sending selectApdu", __func__);
282 
283     mSecureElementStatus = SecureElementStatus::IOERROR;
284 
285     apdu_len = (int32_t)(5 + aid.size());
286     resp_len = 0;
287     apdu = (uint8_t*)malloc(apdu_len * sizeof(uint8_t));
288     resp = (uint8_t*)malloc(65536 * sizeof(uint8_t));
289 
290     if (apdu != NULL && resp!=NULL) {
291         index = 0;
292         apdu[index++] = resApduBuff.channelNumber;
293         apdu[index++] = 0xA4;
294         apdu[index++] = 0x04;
295         apdu[index++] = p2;
296         apdu[index++] = aid.size();
297         memcpy(&apdu[index], aid.data(), aid.size());
298         dump_bytes("CMD: ", ':', apdu, apdu_len, stdout);
299 
300         resp_len = se_gto_apdu_transmit(ctx, apdu, apdu_len, resp, 65536);
301         ALOGD("SecureElement:%s selectApdu resp_len = %d", __func__,resp_len);
302     }
303 
304     if (resp_len < 0) {
305         ALOGE("SecureElement:%s selectApdu resp_len = %d", __func__,resp_len);
306         if (deinitializeSE() != SecureElementStatus::SUCCESS) {
307              ALOGE("SecureElement:%s deinitializeSE Failed", __func__);
308         }
309         mSecureElementStatus = SecureElementStatus::IOERROR;
310     } else {
311         dump_bytes("RESP: ", ':', resp, resp_len, stdout);
312 
313         if (resp[resp_len - 2] == 0x90 && resp[resp_len - 1] == 0x00) {
314             resApduBuff.selectResponse.resize(resp_len);
315             memcpy(&resApduBuff.selectResponse[0], resp, resp_len);
316             mSecureElementStatus = SecureElementStatus::SUCCESS;
317         }
318         else if (resp[resp_len - 2] == 0x6A && resp[resp_len - 1] == 0x80) {
319             mSecureElementStatus = SecureElementStatus::IOERROR;
320         }
321         else if (resp[resp_len - 2] == 0x6A && resp[resp_len - 1] == 0x81) {
322             mSecureElementStatus = SecureElementStatus::IOERROR;
323         }
324         else if (resp[resp_len - 2] == 0x6A && resp[resp_len - 1] == 0x82) {
325             mSecureElementStatus = SecureElementStatus::NO_SUCH_ELEMENT_ERROR;
326         }
327         else if (resp[resp_len - 2] == 0x6A && resp[resp_len - 1] == 0x86) {
328             mSecureElementStatus = SecureElementStatus::UNSUPPORTED_OPERATION;
329         }
330         else if (resp[resp_len - 2] == 0x6A && resp[resp_len - 1] == 0x87) {
331             mSecureElementStatus = SecureElementStatus::UNSUPPORTED_OPERATION;
332         }
333     }
334 
335     /*Check if SELECT command failed, close oppened channel*/
336     if (mSecureElementStatus != SecureElementStatus::SUCCESS) {
337         closeChannel(resApduBuff.channelNumber);
338     }
339 
340     ALOGD("SecureElement:%s mSecureElementStatus = %d", __func__, (int)mSecureElementStatus);
341     _hidl_cb(resApduBuff, mSecureElementStatus);
342 
343     ALOGD("SecureElement:%s Free memory after selectApdu", __func__);
344     if(apdu) free(apdu);
345     if(resp) free(resp);
346     return Void();
347 }
348 
openBasicChannel(const hidl_vec<uint8_t> & aid,uint8_t p2,openBasicChannel_cb _hidl_cb)349 Return<void> SecureElement::openBasicChannel(const hidl_vec<uint8_t>& aid, uint8_t p2, openBasicChannel_cb _hidl_cb) {
350     hidl_vec<uint8_t> result;
351 
352     SecureElementStatus mSecureElementStatus = SecureElementStatus::IOERROR;
353 
354     uint8_t *apdu; //65536
355     int apdu_len = 0;
356     uint8_t *resp;
357     int resp_len = 0;
358 
359     if (!checkSeUp) {
360         if (initializeSE() != EXIT_SUCCESS) {
361             ALOGE("SecureElement:%s: Failed to re-initialise the eSE HAL", __func__);
362             internalClientCallback->onStateChange(false);
363             _hidl_cb(result, SecureElementStatus::IOERROR);
364             return Void();
365         }
366     }
367 
368 
369     apdu_len = (int32_t)(5 + aid.size());
370     resp_len = 0;
371     apdu = (uint8_t*)malloc(apdu_len * sizeof(uint8_t));
372     resp = (uint8_t*)malloc(65536 * sizeof(uint8_t));
373 
374 
375     if (apdu != NULL) {
376         uint8_t index = 0;
377         apdu[index++] = 0x00;
378         apdu[index++] = 0xA4;
379         apdu[index++] = 0x04;
380         apdu[index++] = p2;
381         apdu[index++] = aid.size();
382         memcpy(&apdu[index], aid.data(), aid.size());
383         dump_bytes("CMD: ", ':', apdu, apdu_len, stdout);
384 
385         resp_len = se_gto_apdu_transmit(ctx, apdu, apdu_len, resp, 65536);
386         ALOGD("SecureElement:%s selectApdu resp_len = %d", __func__,resp_len);
387     }
388 
389     if (resp_len < 0) {
390         if (deinitializeSE() != SecureElementStatus::SUCCESS) {
391              ALOGE("SecureElement:%s deinitializeSE Failed", __func__);
392         }
393         mSecureElementStatus = SecureElementStatus::IOERROR;
394     } else {
395         dump_bytes("RESP: ", ':', resp, resp_len, stdout);
396 
397         if ((resp[resp_len - 2] == 0x90) && (resp[resp_len - 1] == 0x00)) {
398             result.resize(resp_len);
399             memcpy(&result[0], resp, resp_len);
400 
401             isBasicChannelOpen = true;
402             nbrOpenChannel++;
403             mSecureElementStatus = SecureElementStatus::SUCCESS;
404         }
405         else if (resp[resp_len - 2] == 0x68 && resp[resp_len - 1] == 0x81) {
406             mSecureElementStatus = SecureElementStatus::CHANNEL_NOT_AVAILABLE;
407         }
408         else if (resp[resp_len - 2] == 0x6A && resp[resp_len - 1] == 0x81) {
409             mSecureElementStatus = SecureElementStatus::CHANNEL_NOT_AVAILABLE;
410         }
411     }
412 
413     if ((mSecureElementStatus != SecureElementStatus::SUCCESS) && isBasicChannelOpen) {
414       closeChannel(BASIC_CHANNEL);
415     }
416 
417     ALOGD("SecureElement:%s mSecureElementStatus = %d", __func__, (int)mSecureElementStatus);
418     _hidl_cb(result, mSecureElementStatus);
419 
420     ALOGD("SecureElement:%s Free memory after openBasicChannel", __func__);
421     if(apdu) free(apdu);
422     if(resp) free(resp);
423     return Void();
424 }
425 
closeChannel(uint8_t channelNumber)426 Return<::android::hardware::secure_element::V1_0::SecureElementStatus> SecureElement::closeChannel(uint8_t channelNumber) {
427     ALOGD("SecureElement:%s start", __func__);
428     SecureElementStatus mSecureElementStatus = SecureElementStatus::FAILED;
429 
430     uint8_t *apdu; //65536
431     int apdu_len = 10;
432     uint8_t *resp;
433     int resp_len = 0;
434 
435     if (!checkSeUp) {
436         ALOGE("SecureElement:%s cannot closeChannel, HAL is deinitialized", __func__);
437         mSecureElementStatus = SecureElementStatus::FAILED;
438         ALOGD("SecureElement:%s end", __func__);
439         return mSecureElementStatus;
440     }
441 
442     if ((channelNumber < 0) || (channelNumber >= MAX_CHANNELS)) {
443         ALOGE("SecureElement:%s Channel not supported", __func__);
444         mSecureElementStatus = SecureElementStatus::FAILED;
445     } else if (channelNumber == 0) {
446         isBasicChannelOpen = false;
447         mSecureElementStatus = SecureElementStatus::SUCCESS;
448         nbrOpenChannel--;
449     } else {
450         apdu = (uint8_t*)malloc(apdu_len * sizeof(uint8_t));
451         resp = (uint8_t*)malloc(65536 * sizeof(uint8_t));
452 
453         if (apdu != NULL) {
454             uint8_t index = 0;
455 
456             apdu[index++] = channelNumber;
457             apdu[index++] = 0x70;
458             apdu[index++] = 0x80;
459             apdu[index++] = channelNumber;
460             apdu[index++] = 0x00;
461             apdu_len = index;
462 
463             resp_len = se_gto_apdu_transmit(ctx, apdu, apdu_len, resp, 65536);
464         }
465         if (resp_len < 0) {
466             mSecureElementStatus = SecureElementStatus::FAILED;
467             if (deinitializeSE() != SecureElementStatus::SUCCESS) {
468                 ALOGE("SecureElement:%s deinitializeSE Failed", __func__);
469             }
470         } else if ((resp[resp_len - 2] == 0x90) && (resp[resp_len - 1] == 0x00)) {
471             mSecureElementStatus = SecureElementStatus::SUCCESS;
472             nbrOpenChannel--;
473         } else {
474             mSecureElementStatus = SecureElementStatus::FAILED;
475         }
476         if(apdu) free(apdu);
477         if(resp) free(resp);
478     }
479 
480     if (nbrOpenChannel == 0 && isBasicChannelOpen == false) {
481         ALOGD("SecureElement:%s All Channels are closed", __func__);
482         if (deinitializeSE() != SecureElementStatus::SUCCESS) {
483             ALOGE("SecureElement:%s deinitializeSE Failed", __func__);
484         }
485     }
486     ALOGD("SecureElement:%s end", __func__);
487     return mSecureElementStatus;
488 }
489 
490 void
dump_bytes(const char * pf,char sep,const uint8_t * p,int n,FILE * out)491 SecureElement::dump_bytes(const char *pf, char sep, const uint8_t *p, int n, FILE *out)
492 {
493     const uint8_t *s = p;
494     char *msg;
495     int len = 0;
496     int input_len = n;
497 
498     msg = (char*) malloc ( (pf ? strlen(pf) : 0) + input_len * 3 + 1);
499     if(!msg) {
500         errno = ENOMEM;
501         return;
502     }
503 
504     if (pf) {
505         len += sprintf(msg , "%s" , pf);
506         //len = len + 8;
507     }
508     while (input_len--) {
509         len += sprintf(msg + len, "%02X" , *s++);
510         //len = len + 2;
511         if (input_len && sep) {
512             len += sprintf(msg + len, ":");
513             //len++;
514         }
515     }
516     sprintf(msg + len, "\n");
517     ALOGD("SecureElement:%s ==> size = %d data = %s", __func__, n, msg);
518 
519     if(msg) free(msg);
520 }
521 
522 int
toint(char c)523 SecureElement::toint(char c)
524 {
525     if ((c >= '0') && (c <= '9'))
526         return c - '0';
527 
528     if ((c >= 'A') && (c <= 'F'))
529         return c - 'A' + 10;
530 
531     if ((c >= 'a') && (c <= 'f'))
532         return c - 'a' + 10;
533 
534     return 0;
535 }
536 
537 int
run_apdu(struct se_gto_ctx * ctx,const uint8_t * apdu,uint8_t * resp,int n,int verbose)538 SecureElement::run_apdu(struct se_gto_ctx *ctx, const uint8_t *apdu, uint8_t *resp, int n, int verbose)
539 {
540     int sw;
541 
542     if (verbose)
543         dump_bytes("APDU: ", ':', apdu, n, stdout);
544 
545 
546     n = se_gto_apdu_transmit(ctx, apdu, n, resp, sizeof(resp));
547     if (n < 0) {
548         ALOGE("SecureElement:%s FAILED: APDU transmit (%s).\n\n", __func__, strerror(errno));
549         return -2;
550     } else if (n < 2) {
551         dump_bytes("RESP: ", ':', resp, n, stdout);
552         ALOGE("SecureElement:%s FAILED: not enough data to have a status word.\n", __func__);
553         return -2;
554     }
555 
556     if (verbose) {
557         sw = (resp[n - 2] << 8) | resp[n - 1];
558         printf("%d bytes, SW=0x%04x\n", n - 2, sw);
559         if (n > 2)
560             dump_bytes("RESP: ", ':', resp, n - 2, stdout);
561     }
562     return 0;
563 }
564 
565 int
parseConfigFile(FILE * f,int verbose)566 SecureElement::parseConfigFile(FILE *f, int verbose)
567 {
568     static char    buf[65536 * 2 + 2];
569 
570     int line;
571     char * pch;
572 
573     line = 0;
574     while (feof(f) == 0) {
575         char *s;
576 
577         s = fgets(buf, sizeof buf, f);
578         if (s == NULL)
579             break;
580         if (s[0] == '#') {
581             /*if (verbose)
582                 fputs(buf, stdout);*/
583             continue;
584         }
585 
586         pch = strtok (s," =;");
587         if (strcmp("GTO_DEV", pch) == 0){
588             pch = strtok (NULL, " =;");
589             ALOGD("SecureElement:%s Defined node : %s", __func__, pch);
590             if (strlen (pch) > 0 && strcmp("\n",pch) != 0 && strcmp("\0",pch) != 0 ) se_gto_set_gtodev(ctx, pch);
591         }
592 
593     }
594     return 0;
595 }
596 
597 int
openConfigFile(int verbose)598 SecureElement::openConfigFile(int verbose)
599 {
600     int   r;
601     FILE *f;
602 
603 
604     /* filename is not NULL */
605     ALOGD("SecureElement:%s Open Config file : %s", __func__, config_filename);
606     f = fopen(config_filename, "r");
607     if (f) {
608         r = parseConfigFile(f, verbose);
609         if (r == -1) {
610             perror(config_filename);
611             ALOGE("SecureElement:%s Error parse %s Failed", __func__, config_filename);
612         }
613         if (fclose(f) != 0) {
614             r = -1;
615             ALOGE("SecureElement:%s Error close %s Failed", __func__, config_filename);
616         }
617     } else {
618         r = -1;
619         ALOGE("SecureElement:%s Error open %s Failed", __func__, config_filename);
620     }
621     return r;
622 }
623 
serviceDied(uint64_t,const wp<IBase> &)624 void SecureElement::serviceDied(uint64_t, const wp<IBase>&) {
625   ALOGD("SecureElement:%s SecureElement serviceDied", __func__);
626   SecureElementStatus mSecureElementStatus = deinitializeSE();
627   if (mSecureElementStatus != SecureElementStatus::SUCCESS) {
628     ALOGE("SecureElement:%s deinitializeSE Failed", __func__);
629   }
630   if (internalClientCallback != nullptr) {
631     internalClientCallback->unlinkToDeath(this);
632     internalClientCallback = nullptr;
633   }
634 }
635 
636 Return<::android::hardware::secure_element::V1_0::SecureElementStatus>
deinitializeSE()637 SecureElement::deinitializeSE() {
638     SecureElementStatus mSecureElementStatus = SecureElementStatus::FAILED;
639 
640     ALOGD("SecureElement:%s start", __func__);
641 
642     if(checkSeUp){
643         if (se_gto_close(ctx) < 0) {
644             mSecureElementStatus = SecureElementStatus::FAILED;
645             internalClientCallback->onStateChange(false);
646         } else {
647             ctx = NULL;
648             mSecureElementStatus = SecureElementStatus::SUCCESS;
649             isBasicChannelOpen = false;
650             nbrOpenChannel = 0;
651         }
652         checkSeUp = false;
653     }else{
654         ALOGD("SecureElement:%s No need to deinitialize SE", __func__);
655         mSecureElementStatus = SecureElementStatus::SUCCESS;
656     }
657 
658     ALOGD("SecureElement:%s end", __func__);
659     return mSecureElementStatus;
660 }
661 
662 // Methods from ::android::hidl::base::V1_0::IBase follow.
663 
664 //ISecureElement* HIDL_FETCH_ISecureElement(const char* /* name */) {
665     //return new SecureElement();
666 //}
667 //
668 }  // namespace implementation
669 }  // namespace V1_0
670 }  // namespace secure_element
671 }  // namespace hardware
672 }  // namespace android
673