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