huangcm
2025-07-17 5e909b7bed41a27a688a9734cda9187a164260f5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Test program to select the CardManager and request the card production
 * lifcycle data (CPLC).
 */
 
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
 
#include <ese/ese.h>
ESE_INCLUDE_HW(ESE_HW_NXP_PN80T_NQ_NCI);
 
/* APDU: CLA INS P1-P2 Lc Data Le */
struct Apdu {
  uint32_t length;
  const uint8_t *bytes;
  const char *desc;
};
 
struct ApduSession {
  uint32_t count;
  const char *desc;
  const struct Apdu *apdus[];
};
 
const uint8_t kSelectCardManagerBytes[] = {0x00, 0xA4, 0x04, 0x00, 0x00};
/* Implicitly selects the ISD at A0 00 00 01 51 00 00 */
const struct Apdu kSelectCardManager = {
    .length = sizeof(kSelectCardManagerBytes),
    .bytes = &kSelectCardManagerBytes[0],
    .desc = "SELECT CARD MANAGER",
};
 
const uint8_t kGetCplcBytes[] = {0x80, 0xCA, 0x9F, 0x7F, 0x00};
const struct Apdu kGetCplc = {
    .length = sizeof(kGetCplcBytes),
    .bytes = &kGetCplcBytes[0],
    .desc = "GET CPLC",
};
 
const struct ApduSession kGetCplcSession = {
    .count = 2,
    .desc = "GET-CPLC",
    .apdus =
        {
            &kSelectCardManager, &kGetCplc,
        },
};
 
const struct ApduSession kEmptySession = {
    .count = 0, .desc = "Empty session (hw close only)", .apdus = {},
};
 
/* Define the loader service sessions here! */
const uint8_t kSelectJcopIdentifyBytes[] = {
    0x00, 0xA4, 0x04, 0x00, 0x09, 0xA0, 0x00,
    0x00, 0x01, 0x67, 0x41, 0x30, 0x00, 0xFF,
};
const struct Apdu kSelectJcopIdentify = {
    .length = sizeof(kSelectJcopIdentifyBytes),
    .bytes = &kSelectJcopIdentifyBytes[0],
    .desc = "SELECT JCOP IDENTIFY",
};
 
const struct ApduSession *kSessions[] = {
    &kGetCplcSession, &kEmptySession,
};
 
int main() {
  struct EseInterface ese = ESE_INITIALIZER(ESE_HW_NXP_PN80T_NQ_NCI);
  void *ese_hw_open_data = NULL;
  size_t s = 0;
  for (; s < sizeof(kSessions) / sizeof(kSessions[0]); ++s) {
    int recvd;
    uint32_t apdu_index = 0;
    uint8_t rx_buf[1024];
    if (ese_open(&ese, ese_hw_open_data) < 0) {
      printf("Cannot open hw\n");
      if (ese_error(&ese))
        printf("eSE error (%d): %s\n", ese_error_code(&ese),
               ese_error_message(&ese));
      return 1;
    }
    printf("Running session %s\n", kSessions[s]->desc);
    for (; apdu_index < kSessions[s]->count; ++apdu_index) {
      uint32_t i;
      const struct Apdu *apdu = kSessions[s]->apdus[apdu_index];
      printf("Sending APDU %u: %s\n", apdu_index, apdu->desc);
      printf("Sending %u bytes to card\n", apdu->length);
      printf("TX: ");
      for (i = 0; i < apdu->length; ++i)
        printf("%.2X ", apdu->bytes[i]);
      printf("\n");
 
      recvd = ese_transceive(&ese, (uint8_t *)apdu->bytes, apdu->length, rx_buf,
                             sizeof(rx_buf));
      if (ese_error(&ese)) {
        printf("An error (%d) occurred: %s", ese_error_code(&ese),
               ese_error_message(&ese));
        return -1;
      }
      if (recvd > 0) {
        int i;
        printf("Read %d bytes from card\n", recvd);
        printf("RX: ");
        for (i = 0; i < recvd; ++i)
          printf("%.2X ", rx_buf[i]);
        printf("\n");
      }
      usleep(1000);
    }
    printf("Session completed\n\n");
    ese_close(&ese);
  }
  return 0;
}