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