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 namespace android {
29 namespace hardware {
30 namespace secure_element {
31 namespace V1_0 {
32 namespace implementation {
33
34 #ifndef MAX_CHANNELS
35 #define MAX_CHANNELS 0x04
36 #endif
37
38 #ifndef BASIC_CHANNEL
39 #define BASIC_CHANNEL 0x00
40 #endif
41
42 #ifndef LOG_HAL_LEVEL
43 #define LOG_HAL_LEVEL 4
44 #endif
45
46 #ifndef MAX_AID_LEN
47 #define MAX_AID_LEN 16
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 se_gto_set_log_level(ctx, 3);
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 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 if (aid.size() > MAX_AID_LEN) {
219 ALOGE("SecureElement:%s: Bad AID size", __func__);
220 _hidl_cb(resApduBuff, SecureElementStatus::FAILED);
221 return Void();
222 }
223
224 uint8_t *apdu; //65536
225 int apdu_len = 0;
226 uint8_t *resp;
227 int resp_len = 0;
228 uint8_t index = 0;
229 int getResponseOffset = 0;
230
231 apdu_len = 5;
232 apdu = (uint8_t*)malloc(apdu_len * sizeof(uint8_t));
233 resp = (uint8_t*)malloc(65536 * sizeof(uint8_t));
234
235 if (apdu != NULL && resp!=NULL) {
236 index = 0;
237 apdu[index++] = 0x00;
238 apdu[index++] = 0x70;
239 apdu[index++] = 0x00;
240 apdu[index++] = 0x00;
241 apdu[index++] = 0x01;
242
243 dump_bytes("CMD: ", ':', apdu, apdu_len, stdout);
244
245 resp_len = se_gto_apdu_transmit(ctx, apdu, apdu_len, resp, 65536);
246 ALOGD("SecureElement:%s Manage channel resp_len = %d", __func__,resp_len);
247 }
248
249 if (resp_len >= 0)
250 dump_bytes("RESP: ", ':', resp, resp_len, stdout);
251
252
253 if (resp_len < 0) {
254 if (deinitializeSE() != SecureElementStatus::SUCCESS) {
255 ALOGE("SecureElement:%s deinitializeSE Failed", __func__);
256 }
257 mSecureElementStatus = SecureElementStatus::IOERROR;
258 ALOGD("SecureElement:%s Free memory after manage channel after ERROR", __func__);
259 if(apdu) free(apdu);
260 if(resp) free(resp);
261 _hidl_cb(resApduBuff, mSecureElementStatus);
262 return Void();
263 } else if (resp[resp_len - 2] == 0x90 && resp[resp_len - 1] == 0x00) {
264 resApduBuff.channelNumber = resp[0];
265 nbrOpenChannel++;
266 mSecureElementStatus = SecureElementStatus::SUCCESS;
267 } else {
268 if (resp[resp_len - 2] == 0x6A && resp[resp_len - 1] == 0x81) {
269 mSecureElementStatus = SecureElementStatus::CHANNEL_NOT_AVAILABLE;
270 }else if (resp[resp_len - 2] == 0x68 && resp[resp_len - 1] == 0x81) {
271 mSecureElementStatus = SecureElementStatus::CHANNEL_NOT_AVAILABLE;
272 } else {
273 mSecureElementStatus = SecureElementStatus::IOERROR;
274 }
275 ALOGD("SecureElement:%s Free memory after manage channel after ERROR", __func__);
276 if(apdu) free(apdu);
277 if(resp) free(resp);
278 _hidl_cb(resApduBuff, mSecureElementStatus);
279 return Void();
280 }
281
282 if(apdu) free(apdu);
283 if(resp) free(resp);
284 ALOGD("SecureElement:%s Free memory after manage channel", __func__);
285 ALOGD("SecureElement:%s mSecureElementStatus = %d", __func__, (int)mSecureElementStatus);
286
287 /*Start Sending select command after Manage Channel is successful.*/
288 ALOGD("SecureElement:%s Sending selectApdu", __func__);
289
290 mSecureElementStatus = SecureElementStatus::IOERROR;
291
292 apdu_len = (int32_t)(6 + aid.size());
293 resp_len = 0;
294 apdu = (uint8_t*)malloc(apdu_len * sizeof(uint8_t));
295 resp = (uint8_t*)malloc(65536 * sizeof(uint8_t));
296
297 if (apdu != NULL && resp!=NULL) {
298 index = 0;
299 apdu[index++] = resApduBuff.channelNumber;
300 apdu[index++] = 0xA4;
301 apdu[index++] = 0x04;
302 apdu[index++] = p2;
303 apdu[index++] = aid.size();
304 memcpy(&apdu[index], aid.data(), aid.size());
305 index += aid.size();
306 apdu[index] = 0x00;
307
308 send_logical:
309 dump_bytes("CMD: ", ':', apdu, apdu_len, stdout);
310 resp_len = se_gto_apdu_transmit(ctx, apdu, apdu_len, resp, 65536);
311 ALOGD("SecureElement:%s selectApdu resp_len = %d", __func__,resp_len);
312 }
313
314 if (resp_len < 0) {
315 ALOGE("SecureElement:%s selectApdu resp_len = %d", __func__,resp_len);
316 if (deinitializeSE() != SecureElementStatus::SUCCESS) {
317 ALOGE("SecureElement:%s deinitializeSE Failed", __func__);
318 }
319 mSecureElementStatus = SecureElementStatus::IOERROR;
320 } else {
321 dump_bytes("RESP: ", ':', resp, resp_len, stdout);
322
323 if (resp[resp_len - 2] == 0x90 || resp[resp_len - 2] == 0x62 || resp[resp_len - 2] == 0x63) {
324 resApduBuff.selectResponse.resize(getResponseOffset + resp_len);
325 memcpy(&resApduBuff.selectResponse[getResponseOffset], resp, resp_len);
326 mSecureElementStatus = SecureElementStatus::SUCCESS;
327 }
328 else if (resp[resp_len - 2] == 0x61) {
329 resApduBuff.selectResponse.resize(getResponseOffset + resp_len - 2);
330 memcpy(&resApduBuff.selectResponse[getResponseOffset], resp, resp_len - 2);
331 getResponseOffset += (resp_len - 2);
332 getResponse[4] = resp[resp_len - 1];
333 getResponse[0] = apdu[0];
334 dump_bytes("getResponse CMD: ", ':', getResponse, 5, stdout);
335 free(apdu);
336 apdu_len = 5;
337 apdu = (uint8_t*)malloc(apdu_len * sizeof(uint8_t));
338 memset(resp, 0, resp_len);
339 memcpy(apdu, getResponse, apdu_len);
340 apdu[0] = resApduBuff.channelNumber;
341 goto send_logical;
342 }
343 else if (resp[resp_len - 2] == 0x6C) {
344 resApduBuff.selectResponse.resize(getResponseOffset + resp_len - 2);
345 memcpy(&resApduBuff.selectResponse[getResponseOffset], resp, resp_len - 2);
346 getResponseOffset += (resp_len - 2);
347 apdu[4] = resp[resp_len - 1];
348 dump_bytes("case2 getResponse CMD: ", ':', apdu, 5, stdout);
349 memset(resp, 0, resp_len);
350 goto send_logical;
351 }
352 else if (resp[resp_len - 2] == 0x6A && resp[resp_len - 1] == 0x80) {
353 mSecureElementStatus = SecureElementStatus::IOERROR;
354 }
355 else if (resp[resp_len - 2] == 0x6A && resp[resp_len - 1] == 0x81) {
356 mSecureElementStatus = SecureElementStatus::IOERROR;
357 }
358 else if (resp[resp_len - 2] == 0x6A && resp[resp_len - 1] == 0x82) {
359 mSecureElementStatus = SecureElementStatus::NO_SUCH_ELEMENT_ERROR;
360 }
361 else if (resp[resp_len - 2] == 0x6A && resp[resp_len - 1] == 0x86) {
362 mSecureElementStatus = SecureElementStatus::UNSUPPORTED_OPERATION;
363 }
364 else if (resp[resp_len - 2] == 0x6A && resp[resp_len - 1] == 0x87) {
365 mSecureElementStatus = SecureElementStatus::UNSUPPORTED_OPERATION;
366 }
367 }
368
369 /*Check if SELECT command failed, close oppened channel*/
370 if (mSecureElementStatus != SecureElementStatus::SUCCESS) {
371 closeChannel(resApduBuff.channelNumber);
372 }
373
374 ALOGD("SecureElement:%s mSecureElementStatus = %d", __func__, (int)mSecureElementStatus);
375 _hidl_cb(resApduBuff, mSecureElementStatus);
376
377 ALOGD("SecureElement:%s Free memory after selectApdu", __func__);
378 if(apdu) free(apdu);
379 if(resp) free(resp);
380 return Void();
381 }
382
openBasicChannel(const hidl_vec<uint8_t> & aid,uint8_t p2,openBasicChannel_cb _hidl_cb)383 Return<void> SecureElement::openBasicChannel(const hidl_vec<uint8_t>& aid, uint8_t p2, openBasicChannel_cb _hidl_cb) {
384 hidl_vec<uint8_t> result;
385
386 SecureElementStatus mSecureElementStatus = SecureElementStatus::IOERROR;
387
388 uint8_t *apdu; //65536
389 int apdu_len = 0;
390 uint8_t *resp;
391 int resp_len = 0;
392 int getResponseOffset = 0;
393 uint8_t index = 0;
394
395 if (isBasicChannelOpen) {
396 ALOGE("SecureElement:%s: Basic Channel already open", __func__);
397 _hidl_cb(result, SecureElementStatus::CHANNEL_NOT_AVAILABLE);
398 return Void();
399 }
400
401 if (!checkSeUp) {
402 if (initializeSE() != EXIT_SUCCESS) {
403 ALOGE("SecureElement:%s: Failed to re-initialise the eSE HAL", __func__);
404 internalClientCallback->onStateChange(false);
405 _hidl_cb(result, SecureElementStatus::IOERROR);
406 return Void();
407 }
408 }
409
410 if (aid.size() > MAX_AID_LEN) {
411 ALOGE("SecureElement:%s: Bad AID size", __func__);
412 _hidl_cb(result, SecureElementStatus::FAILED);
413 return Void();
414 }
415
416 apdu_len = (int32_t)(6 + aid.size());
417 resp_len = 0;
418 apdu = (uint8_t*)malloc(apdu_len * sizeof(uint8_t));
419 resp = (uint8_t*)malloc(65536 * sizeof(uint8_t));
420
421
422 if (apdu != NULL) {
423 index = 0;
424 apdu[index++] = 0x00;
425 apdu[index++] = 0xA4;
426 apdu[index++] = 0x04;
427 apdu[index++] = p2;
428 apdu[index++] = aid.size();
429 memcpy(&apdu[index], aid.data(), aid.size());
430 index += aid.size();
431 apdu[index] = 0x00;
432
433 send_basic:
434 dump_bytes("CMD: ", ':', apdu, apdu_len, stdout);
435 resp_len = se_gto_apdu_transmit(ctx, apdu, apdu_len, resp, 65536);
436 ALOGD("SecureElement:%s selectApdu resp_len = %d", __func__,resp_len);
437 }
438
439 if (resp_len < 0) {
440 if (deinitializeSE() != SecureElementStatus::SUCCESS) {
441 ALOGE("SecureElement:%s deinitializeSE Failed", __func__);
442 }
443 mSecureElementStatus = SecureElementStatus::IOERROR;
444 } else {
445 dump_bytes("RESP: ", ':', resp, resp_len, stdout);
446
447 if (resp[resp_len - 2] == 0x90 || resp[resp_len - 2] == 0x62 || resp[resp_len - 2] == 0x63) {
448 result.resize(getResponseOffset + resp_len);
449 memcpy(&result[getResponseOffset], resp, resp_len);
450
451 isBasicChannelOpen = true;
452 nbrOpenChannel++;
453 mSecureElementStatus = SecureElementStatus::SUCCESS;
454 }
455 else if (resp[resp_len - 2] == 0x61) {
456 result.resize(getResponseOffset + resp_len - 2);
457 memcpy(&result[getResponseOffset], resp, resp_len - 2);
458 getResponseOffset += (resp_len - 2);
459 getResponse[4] = resp[resp_len - 1];
460 getResponse[0] = apdu[0];
461 dump_bytes("getResponse CMD: ", ':', getResponse, 5, stdout);
462 free(apdu);
463 apdu_len = 5;
464 apdu = (uint8_t*)malloc(apdu_len * sizeof(uint8_t));
465 memset(resp, 0, resp_len);
466 memcpy(apdu, getResponse, apdu_len);
467 goto send_basic;
468 }
469 else if (resp[resp_len - 2] == 0x6C) {
470 result.resize(getResponseOffset + resp_len - 2);
471 memcpy(&result[getResponseOffset], resp, resp_len - 2);
472 getResponseOffset += (resp_len - 2);
473 apdu[4] = resp[resp_len - 1];
474 dump_bytes("case2 getResponse CMD: ", ':', apdu, 5, stdout);
475 apdu_len = 5;
476 memset(resp, 0, resp_len);
477 goto send_basic;
478 }
479 else if (resp[resp_len - 2] == 0x68 && resp[resp_len - 1] == 0x81) {
480 mSecureElementStatus = SecureElementStatus::CHANNEL_NOT_AVAILABLE;
481 }
482 else if (resp[resp_len - 2] == 0x6A && resp[resp_len - 1] == 0x81) {
483 mSecureElementStatus = SecureElementStatus::CHANNEL_NOT_AVAILABLE;
484 }
485 }
486
487 if ((mSecureElementStatus != SecureElementStatus::SUCCESS) && isBasicChannelOpen) {
488 closeChannel(BASIC_CHANNEL);
489 }
490
491 ALOGD("SecureElement:%s mSecureElementStatus = %d", __func__, (int)mSecureElementStatus);
492 _hidl_cb(result, mSecureElementStatus);
493
494 ALOGD("SecureElement:%s Free memory after openBasicChannel", __func__);
495 if(apdu) free(apdu);
496 if(resp) free(resp);
497 return Void();
498 }
499
closeChannel(uint8_t channelNumber)500 Return<::android::hardware::secure_element::V1_0::SecureElementStatus> SecureElement::closeChannel(uint8_t channelNumber) {
501 ALOGD("SecureElement:%s start", __func__);
502 SecureElementStatus mSecureElementStatus = SecureElementStatus::FAILED;
503
504 uint8_t *apdu; //65536
505 int apdu_len = 10;
506 uint8_t *resp;
507 int resp_len = 0;
508
509 if (!checkSeUp) {
510 ALOGE("SecureElement:%s cannot closeChannel, HAL is deinitialized", __func__);
511 mSecureElementStatus = SecureElementStatus::FAILED;
512 ALOGD("SecureElement:%s end", __func__);
513 return mSecureElementStatus;
514 }
515
516 if ((channelNumber < 0) || (channelNumber >= MAX_CHANNELS)) {
517 ALOGE("SecureElement:%s Channel not supported", __func__);
518 mSecureElementStatus = SecureElementStatus::FAILED;
519 } else if (channelNumber == 0) {
520 isBasicChannelOpen = false;
521 mSecureElementStatus = SecureElementStatus::SUCCESS;
522 nbrOpenChannel--;
523 } else {
524 apdu = (uint8_t*)malloc(apdu_len * sizeof(uint8_t));
525 resp = (uint8_t*)malloc(65536 * sizeof(uint8_t));
526
527 if (apdu != NULL) {
528 uint8_t index = 0;
529
530 apdu[index++] = channelNumber;
531 apdu[index++] = 0x70;
532 apdu[index++] = 0x80;
533 apdu[index++] = channelNumber;
534 apdu[index++] = 0x00;
535 apdu_len = index;
536
537 resp_len = se_gto_apdu_transmit(ctx, apdu, apdu_len, resp, 65536);
538 }
539 if (resp_len < 0) {
540 mSecureElementStatus = SecureElementStatus::FAILED;
541 if (deinitializeSE() != SecureElementStatus::SUCCESS) {
542 ALOGE("SecureElement:%s deinitializeSE Failed", __func__);
543 }
544 } else if ((resp[resp_len - 2] == 0x90) && (resp[resp_len - 1] == 0x00)) {
545 mSecureElementStatus = SecureElementStatus::SUCCESS;
546 nbrOpenChannel--;
547 } else {
548 mSecureElementStatus = SecureElementStatus::FAILED;
549 }
550 if(apdu) free(apdu);
551 if(resp) free(resp);
552 }
553
554 if (nbrOpenChannel == 0 && isBasicChannelOpen == false) {
555 ALOGD("SecureElement:%s All Channels are closed", __func__);
556 if (deinitializeSE() != SecureElementStatus::SUCCESS) {
557 ALOGE("SecureElement:%s deinitializeSE Failed", __func__);
558 }
559 }
560 ALOGD("SecureElement:%s end", __func__);
561 return mSecureElementStatus;
562 }
563
564 void
dump_bytes(const char * pf,char sep,const uint8_t * p,int n,FILE * out)565 SecureElement::dump_bytes(const char *pf, char sep, const uint8_t *p, int n, FILE *out)
566 {
567 const uint8_t *s = p;
568 char *msg;
569 int len = 0;
570 int input_len = n;
571
572 if (!debug_log_enabled) return;
573
574 msg = (char*) malloc ( (pf ? strlen(pf) : 0) + input_len * 3 + 1);
575 if(!msg) {
576 errno = ENOMEM;
577 return;
578 }
579
580 if (pf) {
581 len += sprintf(msg , "%s" , pf);
582 }
583 while (input_len--) {
584 len += sprintf(msg + len, "%02X" , *s++);
585 if (input_len && sep) {
586 len += sprintf(msg + len, ":");
587 }
588 }
589 sprintf(msg + len, "\n");
590 ALOGD("SecureElement:%s ==> size = %d data = %s", __func__, n, msg);
591
592 if(msg) free(msg);
593 }
594
595 int
toint(char c)596 SecureElement::toint(char c)
597 {
598 if ((c >= '0') && (c <= '9'))
599 return c - '0';
600
601 if ((c >= 'A') && (c <= 'F'))
602 return c - 'A' + 10;
603
604 if ((c >= 'a') && (c <= 'f'))
605 return c - 'a' + 10;
606
607 return 0;
608 }
609
610 int
run_apdu(struct se_gto_ctx * ctx,const uint8_t * apdu,uint8_t * resp,int n,int verbose)611 SecureElement::run_apdu(struct se_gto_ctx *ctx, const uint8_t *apdu, uint8_t *resp, int n, int verbose)
612 {
613 int sw;
614
615 if (verbose)
616 dump_bytes("APDU: ", ':', apdu, n, stdout);
617
618
619 n = se_gto_apdu_transmit(ctx, apdu, n, resp, sizeof(resp));
620 if (n < 0) {
621 ALOGE("SecureElement:%s FAILED: APDU transmit (%s).\n\n", __func__, strerror(errno));
622 return -2;
623 } else if (n < 2) {
624 dump_bytes("RESP: ", ':', resp, n, stdout);
625 ALOGE("SecureElement:%s FAILED: not enough data to have a status word.\n", __func__);
626 return -2;
627 }
628
629 if (verbose) {
630 sw = (resp[n - 2] << 8) | resp[n - 1];
631 printf("%d bytes, SW=0x%04x\n", n - 2, sw);
632 if (n > 2)
633 dump_bytes("RESP: ", ':', resp, n - 2, stdout);
634 }
635 return 0;
636 }
637
638 int
parseConfigFile(FILE * f,int verbose)639 SecureElement::parseConfigFile(FILE *f, int verbose)
640 {
641 static char buf[65536 * 2 + 2];
642
643 int line;
644 char * pch;
645
646 line = 0;
647 while (feof(f) == 0) {
648 char *s;
649
650 s = fgets(buf, sizeof buf, f);
651 if (s == NULL)
652 break;
653 if (s[0] == '#') {
654 continue;
655 }
656
657 pch = strtok(s," =;");
658 if (strcmp("GTO_DEV", pch) == 0) {
659 pch = strtok(NULL, " =;");
660 ALOGD("SecureElement:%s Defined node : %s", __func__, pch);
661 if (strlen(pch) > 0 && strcmp("\n", pch) != 0 && strcmp("\0", pch) != 0 ) {
662 se_gto_set_gtodev(ctx, pch);
663 }
664 } else if (strcmp("GTO_DEBUG", pch) == 0) {
665 pch = strtok(NULL, " =;");
666 ALOGD("SecureElement:%s Log state : %s", __func__, pch);
667 if (strlen(pch) > 0 && strcmp("\n", pch) != 0 && strcmp("\0", pch) != 0 ) {
668 if (strcmp(pch, "enable") == 0) {
669 debug_log_enabled = true;
670 se_gto_set_log_level(ctx, 4);
671 } else {
672 debug_log_enabled = false;
673 se_gto_set_log_level(ctx, 3);
674 }
675 }
676 }
677 }
678 return 0;
679 }
680
681 int
openConfigFile(int verbose)682 SecureElement::openConfigFile(int verbose)
683 {
684 int r;
685 FILE *f;
686
687
688 /* filename is not NULL */
689 ALOGD("SecureElement:%s Open Config file : %s", __func__, config_filename);
690 f = fopen(config_filename, "r");
691 if (f) {
692 r = parseConfigFile(f, verbose);
693 if (r == -1) {
694 perror(config_filename);
695 ALOGE("SecureElement:%s Error parse %s Failed", __func__, config_filename);
696 }
697 if (fclose(f) != 0) {
698 r = -1;
699 ALOGE("SecureElement:%s Error close %s Failed", __func__, config_filename);
700 }
701 } else {
702 r = -1;
703 ALOGE("SecureElement:%s Error open %s Failed", __func__, config_filename);
704 }
705 return r;
706 }
707
serviceDied(uint64_t,const wp<IBase> &)708 void SecureElement::serviceDied(uint64_t, const wp<IBase>&) {
709 ALOGD("SecureElement:%s SecureElement serviceDied", __func__);
710 SecureElementStatus mSecureElementStatus = deinitializeSE();
711 if (mSecureElementStatus != SecureElementStatus::SUCCESS) {
712 ALOGE("SecureElement:%s deinitializeSE Failed", __func__);
713 }
714 if (internalClientCallback != nullptr) {
715 internalClientCallback->unlinkToDeath(this);
716 internalClientCallback = nullptr;
717 }
718 }
719
720 Return<::android::hardware::secure_element::V1_0::SecureElementStatus>
deinitializeSE()721 SecureElement::deinitializeSE() {
722 SecureElementStatus mSecureElementStatus = SecureElementStatus::FAILED;
723
724 ALOGD("SecureElement:%s start", __func__);
725
726 if(checkSeUp){
727 if (se_gto_close(ctx) < 0) {
728 mSecureElementStatus = SecureElementStatus::FAILED;
729 internalClientCallback->onStateChange(false);
730 } else {
731 ctx = NULL;
732 mSecureElementStatus = SecureElementStatus::SUCCESS;
733 isBasicChannelOpen = false;
734 nbrOpenChannel = 0;
735 }
736 checkSeUp = false;
737 }else{
738 ALOGD("SecureElement:%s No need to deinitialize SE", __func__);
739 mSecureElementStatus = SecureElementStatus::SUCCESS;
740 }
741
742 ALOGD("SecureElement:%s end", __func__);
743 return mSecureElementStatus;
744 }
745 } // namespace implementation
746 } // namespace V1_0
747 } // namespace secure_element
748 } // namespace hardware
749 } // namespace android
750