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_1 {
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;
83 sp<V1_1::ISecureElementHalCallback> SecureElement::internalClientCallback_v1_1 = nullptr;
initializeSE()84 int SecureElement::initializeSE() {
85 
86     int n;
87 
88     ALOGD("SecureElement:%s start", __func__);
89 
90     if (checkSeUp) {
91         ALOGD("SecureElement:%s Already initialized", __func__);
92         ALOGD("SecureElement:%s end", __func__);
93         return EXIT_SUCCESS;
94     }
95 
96     if (se_gto_new(&ctx) < 0) {
97         ALOGE("SecureElement:%s se_gto_new FATAL:%s", __func__,strerror(errno));
98 
99         return EXIT_FAILURE;
100     }
101     //settings = default_settings(ctx);
102     se_gto_set_log_level(ctx, 4);
103 
104     openConfigFile(1);
105 
106     if (se_gto_open(ctx) < 0) {
107         ALOGE("SecureElement:%s se_gto_open FATAL:%s", __func__,strerror(errno));
108         return EXIT_FAILURE;
109     }
110 
111     if (resetSE() < 0) {
112         se_gto_close(ctx);
113         ctx = NULL;
114         return EXIT_FAILURE;
115     }
116 
117     checkSeUp = true;
118 
119     ALOGD("SecureElement:%s end", __func__);
120     return EXIT_SUCCESS;
121 }
122 
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         internalClientCallback_v1_1 = nullptr;
132         if (!internalClientCallback->linkToDeath(this, 0)) {
133             ALOGE("SecureElement:%s: linkToDeath Failed", __func__);
134         }
135     }
136 
137     if (initializeSE() != EXIT_SUCCESS) {
138         ALOGE("SecureElement:%s initializeSE Failed", __func__);
139         clientCallback->onStateChange(false);
140     } else {
141         ALOGD("SecureElement:%s initializeSE Success", __func__);
142         clientCallback->onStateChange(true);
143     }
144 
145     ALOGD("SecureElement:%s end", __func__);
146 
147     return Void();
148 }
149 
init_1_1(const sp<::android::hardware::secure_element::V1_1::ISecureElementHalCallback> & clientCallback)150 Return<void> SecureElement::init_1_1(const sp<::android::hardware::secure_element::V1_1::ISecureElementHalCallback>& clientCallback) {
151 
152     ALOGD("SecureElement:%s start", __func__);
153     if (clientCallback == nullptr) {
154         ALOGE("SecureElement:%s clientCallback == nullptr", __func__);
155         return Void();
156     } else {
157         internalClientCallback = nullptr;
158         internalClientCallback_v1_1 = clientCallback;
159         if (!internalClientCallback_v1_1->linkToDeath(this, 0)) {
160             ALOGE("SecureElement:%s: linkToDeath Failed", __func__);
161         }
162     }
163 
164     if (initializeSE() != EXIT_SUCCESS) {
165         ALOGE("SecureElement:%s initializeSE Failed", __func__);
166         clientCallback->onStateChange_1_1(false, "SE Initialized failed");
167     } else {
168         ALOGD("SecureElement:%s initializeSE Success", __func__);
169         clientCallback->onStateChange_1_1(true, "SE Initialized");
170     }
171 
172     ALOGD("SecureElement:%s end", __func__);
173 
174     return Void();
175 }
176 
getAtr(getAtr_cb _hidl_cb)177 Return<void> SecureElement::getAtr(getAtr_cb _hidl_cb) {
178     hidl_vec<uint8_t> response;
179     response.resize(atr_size);
180     memcpy(&response[0], atr, atr_size);
181     _hidl_cb(response);
182     return Void();
183 }
184 
isCardPresent()185 Return<bool> SecureElement::isCardPresent() {
186     return true;
187 }
188 
transmit(const hidl_vec<uint8_t> & data,transmit_cb _hidl_cb)189 Return<void> SecureElement::transmit(const hidl_vec<uint8_t>& data, transmit_cb _hidl_cb) {
190 
191     uint8_t *apdu;
192     uint8_t *resp;
193     int status = 0 ;
194     int apdu_len = data.size();
195     int resp_len = 0;
196 
197     apdu = (uint8_t*)malloc(apdu_len * sizeof(uint8_t));
198     resp = (uint8_t*)malloc(65536 * sizeof(uint8_t));
199 
200     hidl_vec<uint8_t> result;
201 
202     if (checkSeUp && nbrOpenChannel != 0) {
203         if (apdu != NULL) {
204             memcpy(apdu, data.data(), data.size());
205             dump_bytes("CMD: ", ':', apdu, apdu_len, stdout);
206             resp_len = se_gto_apdu_transmit(ctx, apdu, apdu_len, resp, 65536);
207         }
208 
209         if (resp_len < 0) {
210             ALOGE("SecureElement:%s: transmit failed", __func__);
211             if (deinitializeSE() != SecureElementStatus::SUCCESS) {
212                 ALOGE("SecureElement:%s deinitializeSE Failed", __func__);
213             }
214         } else {
215             dump_bytes("RESP: ", ':', resp, resp_len, stdout);
216             result.resize(resp_len);
217             memcpy(&result[0], resp, resp_len);
218         }
219     } else {
220         ALOGE("SecureElement:%s: transmit failed! No channel is open", __func__);
221     }
222     _hidl_cb(result);
223     if(apdu) free(apdu);
224     if(resp) free(resp);
225     return Void();
226 }
227 
openLogicalChannel(const hidl_vec<uint8_t> & aid,uint8_t p2,openLogicalChannel_cb _hidl_cb)228 Return<void> SecureElement::openLogicalChannel(const hidl_vec<uint8_t>& aid, uint8_t p2, openLogicalChannel_cb _hidl_cb) {
229     ALOGD("SecureElement:%s start", __func__);
230 
231     LogicalChannelResponse resApduBuff;
232     resApduBuff.channelNumber = 0xff;
233     memset(&resApduBuff, 0x00, sizeof(resApduBuff));
234 
235     if (!checkSeUp) {
236         if (initializeSE() != EXIT_SUCCESS) {
237             ALOGE("SecureElement:%s: Failed to re-initialise the eSE HAL", __func__);
238             if(internalClientCallback_v1_1 != nullptr) {
239                 internalClientCallback_v1_1->onStateChange_1_1(false, "SE Initialized failed");
240             }
241             else {
242                 internalClientCallback->onStateChange(false);
243             }
244             _hidl_cb(resApduBuff, SecureElementStatus::IOERROR);
245             return Void();
246         }
247     }
248 
249     SecureElementStatus mSecureElementStatus = SecureElementStatus::IOERROR;
250 
251     uint8_t *apdu; //65536
252     int apdu_len = 0;
253     uint8_t *resp;
254     int resp_len = 0;
255     uint8_t index = 0;
256 
257     apdu_len = 5;
258     apdu = (uint8_t*)malloc(apdu_len * sizeof(uint8_t));
259     resp = (uint8_t*)malloc(65536 * sizeof(uint8_t));
260 
261     if (apdu != NULL && resp!=NULL) {
262         index = 0;
263         apdu[index++] = 0x00;
264         apdu[index++] = 0x70;
265         apdu[index++] = 0x00;
266         apdu[index++] = 0x00;
267         apdu[index++] = 0x01;
268 
269         dump_bytes("CMD: ", ':', apdu, apdu_len, stdout);
270 
271         resp_len = se_gto_apdu_transmit(ctx, apdu, apdu_len, resp, 65536);
272         ALOGD("SecureElement:%s Manage channel resp_len = %d", __func__,resp_len);
273     }
274 
275     if (resp_len >= 0)
276         dump_bytes("RESP: ", ':', resp, resp_len, stdout);
277 
278 
279     if (resp_len < 0) {
280         if (deinitializeSE() != SecureElementStatus::SUCCESS) {
281              ALOGE("SecureElement:%s deinitializeSE Failed", __func__);
282         }
283         mSecureElementStatus = SecureElementStatus::IOERROR;
284         ALOGD("SecureElement:%s Free memory after manage channel after ERROR", __func__);
285         if(apdu) free(apdu);
286         if(resp) free(resp);
287         _hidl_cb(resApduBuff, mSecureElementStatus);
288         return Void();
289     } else if (resp[resp_len - 2] == 0x90 && resp[resp_len - 1] == 0x00) {
290         resApduBuff.channelNumber = resp[0];
291         nbrOpenChannel++;
292         mSecureElementStatus = SecureElementStatus::SUCCESS;
293     } else {
294         if (resp[resp_len - 2] == 0x6A && resp[resp_len - 1] == 0x81) {
295             mSecureElementStatus = SecureElementStatus::CHANNEL_NOT_AVAILABLE;
296         }else if (resp[resp_len - 2] == 0x68 && resp[resp_len - 1] == 0x81) {
297             mSecureElementStatus = SecureElementStatus::CHANNEL_NOT_AVAILABLE;
298         } else {
299             mSecureElementStatus = SecureElementStatus::IOERROR;
300         }
301         ALOGD("SecureElement:%s Free memory after manage channel after ERROR", __func__);
302         if(apdu) free(apdu);
303         if(resp) free(resp);
304         _hidl_cb(resApduBuff, mSecureElementStatus);
305         return Void();
306     }
307 
308     if(apdu) free(apdu);
309     if(resp) free(resp);
310     ALOGD("SecureElement:%s Free memory after manage channel", __func__);
311     ALOGD("SecureElement:%s mSecureElementStatus = %d", __func__, (int)mSecureElementStatus);
312 
313     /*Start Sending select command after Manage Channel is successful.*/
314     ALOGD("SecureElement:%s Sending selectApdu", __func__);
315 
316     mSecureElementStatus = SecureElementStatus::IOERROR;
317 
318     apdu_len = (int32_t)(5 + aid.size());
319     resp_len = 0;
320     apdu = (uint8_t*)malloc(apdu_len * sizeof(uint8_t));
321     resp = (uint8_t*)malloc(65536 * sizeof(uint8_t));
322 
323     if (apdu != NULL && resp!=NULL) {
324         index = 0;
325         apdu[index++] = resApduBuff.channelNumber;
326         apdu[index++] = 0xA4;
327         apdu[index++] = 0x04;
328         apdu[index++] = p2;
329         apdu[index++] = aid.size();
330         memcpy(&apdu[index], aid.data(), aid.size());
331         dump_bytes("CMD: ", ':', apdu, apdu_len, stdout);
332 
333         resp_len = se_gto_apdu_transmit(ctx, apdu, apdu_len, resp, 65536);
334         ALOGD("SecureElement:%s selectApdu resp_len = %d", __func__,resp_len);
335     }
336 
337     if (resp_len < 0) {
338         ALOGE("SecureElement:%s selectApdu resp_len = %d", __func__,resp_len);
339         if (deinitializeSE() != SecureElementStatus::SUCCESS) {
340              ALOGE("SecureElement:%s deinitializeSE Failed", __func__);
341         }
342         mSecureElementStatus = SecureElementStatus::IOERROR;
343     } else {
344         dump_bytes("RESP: ", ':', resp, resp_len, stdout);
345 
346         if (resp[resp_len - 2] == 0x90 && resp[resp_len - 1] == 0x00) {
347             resApduBuff.selectResponse.resize(resp_len);
348             memcpy(&resApduBuff.selectResponse[0], resp, resp_len);
349             mSecureElementStatus = SecureElementStatus::SUCCESS;
350         }
351         else if (resp[resp_len - 2] == 0x6A && resp[resp_len - 1] == 0x80) {
352             mSecureElementStatus = SecureElementStatus::IOERROR;
353         }
354         else if (resp[resp_len - 2] == 0x6A && resp[resp_len - 1] == 0x81) {
355             mSecureElementStatus = SecureElementStatus::IOERROR;
356         }
357         else if (resp[resp_len - 2] == 0x6A && resp[resp_len - 1] == 0x82) {
358             mSecureElementStatus = SecureElementStatus::NO_SUCH_ELEMENT_ERROR;
359         }
360         else if (resp[resp_len - 2] == 0x6A && resp[resp_len - 1] == 0x86) {
361             mSecureElementStatus = SecureElementStatus::UNSUPPORTED_OPERATION;
362         }
363         else if (resp[resp_len - 2] == 0x6A && resp[resp_len - 1] == 0x87) {
364             mSecureElementStatus = SecureElementStatus::UNSUPPORTED_OPERATION;
365         }
366     }
367 
368     /*Check if SELECT command failed, close oppened channel*/
369     if (mSecureElementStatus != SecureElementStatus::SUCCESS) {
370         closeChannel(resApduBuff.channelNumber);
371     }
372 
373     ALOGD("SecureElement:%s mSecureElementStatus = %d", __func__, (int)mSecureElementStatus);
374     _hidl_cb(resApduBuff, mSecureElementStatus);
375 
376     ALOGD("SecureElement:%s Free memory after selectApdu", __func__);
377     if(apdu) free(apdu);
378     if(resp) free(resp);
379     return Void();
380 }
381 
openBasicChannel(const hidl_vec<uint8_t> & aid,uint8_t p2,openBasicChannel_cb _hidl_cb)382 Return<void> SecureElement::openBasicChannel(const hidl_vec<uint8_t>& aid, uint8_t p2, openBasicChannel_cb _hidl_cb) {
383     hidl_vec<uint8_t> result;
384 
385     SecureElementStatus mSecureElementStatus = SecureElementStatus::IOERROR;
386 
387     uint8_t *apdu; //65536
388     int apdu_len = 0;
389     uint8_t *resp;
390     int resp_len = 0;
391 
392     if (!checkSeUp) {
393         if (initializeSE() != EXIT_SUCCESS) {
394             ALOGE("SecureElement:%s: Failed to re-initialise the eSE HAL", __func__);
395             if(internalClientCallback_v1_1 != nullptr) {
396                 internalClientCallback_v1_1->onStateChange_1_1(false, "SE Initialized failed");
397             }
398             else {
399                 internalClientCallback->onStateChange(false);
400             }
401             _hidl_cb(result, SecureElementStatus::IOERROR);
402             return Void();
403         }
404     }
405 
406 
407     apdu_len = (int32_t)(5 + aid.size());
408     resp_len = 0;
409     apdu = (uint8_t*)malloc(apdu_len * sizeof(uint8_t));
410     resp = (uint8_t*)malloc(65536 * sizeof(uint8_t));
411 
412 
413     if (apdu != NULL) {
414         uint8_t index = 0;
415         apdu[index++] = 0x00;
416         apdu[index++] = 0xA4;
417         apdu[index++] = 0x04;
418         apdu[index++] = p2;
419         apdu[index++] = aid.size();
420         memcpy(&apdu[index], aid.data(), aid.size());
421         dump_bytes("CMD: ", ':', apdu, apdu_len, stdout);
422 
423         resp_len = se_gto_apdu_transmit(ctx, apdu, apdu_len, resp, 65536);
424         ALOGD("SecureElement:%s selectApdu resp_len = %d", __func__,resp_len);
425     }
426 
427     if (resp_len < 0) {
428         if (deinitializeSE() != SecureElementStatus::SUCCESS) {
429              ALOGE("SecureElement:%s deinitializeSE Failed", __func__);
430         }
431         mSecureElementStatus = SecureElementStatus::IOERROR;
432     } else {
433         dump_bytes("RESP: ", ':', resp, resp_len, stdout);
434 
435         if ((resp[resp_len - 2] == 0x90) && (resp[resp_len - 1] == 0x00)) {
436             result.resize(resp_len);
437             memcpy(&result[0], resp, resp_len);
438 
439             isBasicChannelOpen = true;
440             nbrOpenChannel++;
441             mSecureElementStatus = SecureElementStatus::SUCCESS;
442         }
443         else if (resp[resp_len - 2] == 0x68 && resp[resp_len - 1] == 0x81) {
444             mSecureElementStatus = SecureElementStatus::CHANNEL_NOT_AVAILABLE;
445         }
446         else if (resp[resp_len - 2] == 0x6A && resp[resp_len - 1] == 0x81) {
447             mSecureElementStatus = SecureElementStatus::CHANNEL_NOT_AVAILABLE;
448         }
449     }
450 
451     if ((mSecureElementStatus != SecureElementStatus::SUCCESS) && isBasicChannelOpen) {
452       closeChannel(BASIC_CHANNEL);
453     }
454 
455     ALOGD("SecureElement:%s mSecureElementStatus = %d", __func__, (int)mSecureElementStatus);
456     _hidl_cb(result, mSecureElementStatus);
457 
458     ALOGD("SecureElement:%s Free memory after openBasicChannel", __func__);
459     if(apdu) free(apdu);
460     if(resp) free(resp);
461     return Void();
462 }
463 
closeChannel(uint8_t channelNumber)464 Return<::android::hardware::secure_element::V1_0::SecureElementStatus> SecureElement::closeChannel(uint8_t channelNumber) {
465     ALOGD("SecureElement:%s start", __func__);
466     SecureElementStatus mSecureElementStatus = SecureElementStatus::FAILED;
467 
468     uint8_t *apdu; //65536
469     int apdu_len = 10;
470     uint8_t *resp;
471     int resp_len = 0;
472 
473     if (!checkSeUp) {
474         ALOGE("SecureElement:%s cannot closeChannel, HAL is deinitialized", __func__);
475         mSecureElementStatus = SecureElementStatus::FAILED;
476         ALOGD("SecureElement:%s end", __func__);
477         return mSecureElementStatus;
478     }
479 
480     if ((channelNumber < 0) || (channelNumber >= MAX_CHANNELS)) {
481         ALOGE("SecureElement:%s Channel not supported", __func__);
482         mSecureElementStatus = SecureElementStatus::FAILED;
483     } else if (channelNumber == 0) {
484         isBasicChannelOpen = false;
485         mSecureElementStatus = SecureElementStatus::SUCCESS;
486         nbrOpenChannel--;
487     } else {
488         apdu = (uint8_t*)malloc(apdu_len * sizeof(uint8_t));
489         resp = (uint8_t*)malloc(65536 * sizeof(uint8_t));
490 
491         if (apdu != NULL) {
492             uint8_t index = 0;
493 
494             apdu[index++] = channelNumber;
495             apdu[index++] = 0x70;
496             apdu[index++] = 0x80;
497             apdu[index++] = channelNumber;
498             apdu[index++] = 0x00;
499             apdu_len = index;
500 
501             resp_len = se_gto_apdu_transmit(ctx, apdu, apdu_len, resp, 65536);
502         }
503         if (resp_len < 0) {
504             mSecureElementStatus = SecureElementStatus::FAILED;
505             if (deinitializeSE() != SecureElementStatus::SUCCESS) {
506                 ALOGE("SecureElement:%s deinitializeSE Failed", __func__);
507             }
508         } else if ((resp[resp_len - 2] == 0x90) && (resp[resp_len - 1] == 0x00)) {
509             mSecureElementStatus = SecureElementStatus::SUCCESS;
510             nbrOpenChannel--;
511         } else {
512             mSecureElementStatus = SecureElementStatus::FAILED;
513         }
514         if(apdu) free(apdu);
515         if(resp) free(resp);
516     }
517 
518     if (nbrOpenChannel == 0 && isBasicChannelOpen == false) {
519         ALOGD("SecureElement:%s All Channels are closed", __func__);
520         if (deinitializeSE() != SecureElementStatus::SUCCESS) {
521             ALOGE("SecureElement:%s deinitializeSE Failed", __func__);
522         }
523     }
524     ALOGD("SecureElement:%s end", __func__);
525     return mSecureElementStatus;
526 }
527 
528 void
dump_bytes(const char * pf,char sep,const uint8_t * p,int n,FILE * out)529 SecureElement::dump_bytes(const char *pf, char sep, const uint8_t *p, int n, FILE *out)
530 {
531     const uint8_t *s = p;
532     char *msg;
533     int len = 0;
534     int input_len = n;
535 
536     msg = (char*) malloc ( (pf ? strlen(pf) : 0) + input_len * 3 + 1);
537     if(!msg) {
538         errno = ENOMEM;
539         return;
540     }
541 
542     if (pf) {
543         len += sprintf(msg , "%s" , pf);
544         //len = len + 8;
545     }
546     while (input_len--) {
547         len += sprintf(msg + len, "%02X" , *s++);
548         //len = len + 2;
549         if (input_len && sep) {
550             len += sprintf(msg + len, ":");
551             //len++;
552         }
553     }
554     sprintf(msg + len, "\n");
555     ALOGD("SecureElement:%s ==> size = %d data = %s", __func__, n, msg);
556 
557     if(msg) free(msg);
558 }
559 
560 int
toint(char c)561 SecureElement::toint(char c)
562 {
563     if ((c >= '0') && (c <= '9'))
564         return c - '0';
565 
566     if ((c >= 'A') && (c <= 'F'))
567         return c - 'A' + 10;
568 
569     if ((c >= 'a') && (c <= 'f'))
570         return c - 'a' + 10;
571 
572     return 0;
573 }
574 
575 int
run_apdu(struct se_gto_ctx * ctx,const uint8_t * apdu,uint8_t * resp,int n,int verbose)576 SecureElement::run_apdu(struct se_gto_ctx *ctx, const uint8_t *apdu, uint8_t *resp, int n, int verbose)
577 {
578     int sw;
579 
580     if (verbose)
581         dump_bytes("APDU: ", ':', apdu, n, stdout);
582 
583 
584     n = se_gto_apdu_transmit(ctx, apdu, n, resp, sizeof(resp));
585     if (n < 0) {
586         ALOGE("SecureElement:%s FAILED: APDU transmit (%s).\n\n", __func__, strerror(errno));
587         return -2;
588     } else if (n < 2) {
589         dump_bytes("RESP: ", ':', resp, n, stdout);
590         ALOGE("SecureElement:%s FAILED: not enough data to have a status word.\n", __func__);
591         return -2;
592     }
593 
594     if (verbose) {
595         sw = (resp[n - 2] << 8) | resp[n - 1];
596         printf("%d bytes, SW=0x%04x\n", n - 2, sw);
597         if (n > 2)
598             dump_bytes("RESP: ", ':', resp, n - 2, stdout);
599     }
600     return 0;
601 }
602 
603 int
parseConfigFile(FILE * f,int verbose)604 SecureElement::parseConfigFile(FILE *f, int verbose)
605 {
606     static char    buf[65536 * 2 + 2];
607 
608     int line;
609     char * pch;
610 
611     line = 0;
612     while (feof(f) == 0) {
613         char *s;
614 
615         s = fgets(buf, sizeof buf, f);
616         if (s == NULL)
617             break;
618         if (s[0] == '#') {
619             /*if (verbose)
620                 fputs(buf, stdout);*/
621             continue;
622         }
623 
624         pch = strtok (s," =;");
625         if (strcmp("GTO_DEV", pch) == 0){
626             pch = strtok (NULL, " =;");
627             ALOGD("SecureElement:%s Defined node : %s", __func__, pch);
628             if (strlen (pch) > 0 && strcmp("\n",pch) != 0 && strcmp("\0",pch) != 0 ) se_gto_set_gtodev(ctx, pch);
629         }
630 
631     }
632     return 0;
633 }
634 
635 int
openConfigFile(int verbose)636 SecureElement::openConfigFile(int verbose)
637 {
638     int   r;
639     FILE *f;
640 
641 
642     /* filename is not NULL */
643     ALOGD("SecureElement:%s Open Config file : %s", __func__, config_filename);
644     f = fopen(config_filename, "r");
645     if (f) {
646         r = parseConfigFile(f, verbose);
647         if (r == -1) {
648             perror(config_filename);
649             ALOGE("SecureElement:%s Error parse %s Failed", __func__, config_filename);
650         }
651         if (fclose(f) != 0) {
652             r = -1;
653             ALOGE("SecureElement:%s Error close %s Failed", __func__, config_filename);
654         }
655     } else {
656         r = -1;
657         ALOGE("SecureElement:%s Error open %s Failed", __func__, config_filename);
658     }
659     return r;
660 }
661 
serviceDied(uint64_t,const wp<IBase> &)662 void SecureElement::serviceDied(uint64_t, const wp<IBase>&) {
663   ALOGD("SecureElement:%s SecureElement serviceDied", __func__);
664   SecureElementStatus mSecureElementStatus = deinitializeSE();
665   if (mSecureElementStatus != SecureElementStatus::SUCCESS) {
666     ALOGE("SecureElement:%s deinitializeSE Failed", __func__);
667   }
668   if (internalClientCallback != nullptr) {
669     internalClientCallback->unlinkToDeath(this);
670     internalClientCallback = nullptr;
671   }
672   if (internalClientCallback_v1_1 != nullptr) {
673     internalClientCallback_v1_1->unlinkToDeath(this);
674     internalClientCallback_v1_1 = nullptr;
675   }
676 }
677 
678 Return<::android::hardware::secure_element::V1_0::SecureElementStatus>
deinitializeSE()679 SecureElement::deinitializeSE() {
680     SecureElementStatus mSecureElementStatus = SecureElementStatus::FAILED;
681 
682     ALOGD("SecureElement:%s start", __func__);
683 
684     if(checkSeUp){
685         if (se_gto_close(ctx) < 0) {
686             mSecureElementStatus = SecureElementStatus::FAILED;
687             if (internalClientCallback_v1_1 != nullptr) {
688                 internalClientCallback_v1_1->onStateChange_1_1(false, "SE Initialized failed");
689             } else {
690                 internalClientCallback->onStateChange(false);
691             }
692         } else {
693             ctx = NULL;
694             mSecureElementStatus = SecureElementStatus::SUCCESS;
695             isBasicChannelOpen = false;
696             nbrOpenChannel = 0;
697         }
698         checkSeUp = false;
699     }else{
700         ALOGD("SecureElement:%s No need to deinitialize SE", __func__);
701         mSecureElementStatus = SecureElementStatus::SUCCESS;
702     }
703 
704     ALOGD("SecureElement:%s end", __func__);
705     return mSecureElementStatus;
706 }
707 
708 // Methods from ::android::hidl::base::V1_0::IBase follow.
709 
710 //ISecureElement* HIDL_FETCH_ISecureElement(const char* /* name */) {
711     //return new SecureElement();
712 //}
713 //
714 }  // namespace implementation
715 }  // namespace V1_0
716 }  // namespace secure_element
717 }  // namespace hardware
718 }  // namespace android
719