-
Notifications
You must be signed in to change notification settings - Fork 711
/
dtrust-tool.c
428 lines (364 loc) · 10.9 KB
/
dtrust-tool.c
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
* dtrust-tool.c: tool for D-Trust cards
*
* Copyright (C) 2024 [email protected]
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "config.h"
#include <stdio.h>
#include <string.h>
#include "libopensc/opensc.h"
#include "libopensc/cards.h"
#include "libopensc/errors.h"
#include "sm/sm-eac.h"
#include "util.h"
static const char *app_name = "dtrust-tool";
// clang-format off
static const struct option options[] = {
{"reader", 1, NULL, 'r'},
{"wait", 0, NULL, 'w'},
{"pin-status", 0, NULL, 's'},
{"check-transport-protection", 0, NULL, 'c'},
{"unlock-transport-protection", 0, NULL, 'u'},
{"help", 0, NULL, 'h'},
{"verbose", 0, NULL, 'v'},
{NULL, 0, NULL, 0}
};
static const char *option_help[] = {
"Uses reader number <arg> [0]",
"Wait for card insertion",
"Show PIN status",
"Check transport protection",
"Unlock transport protection",
"This message",
"Verbose operation, may be used several times",
};
// clang-format on
static const char *opt_reader = NULL;
static int opt_wait = 0, verbose = 0;
static int opt_status = 0;
static int opt_check = 0;
static int opt_unlock = 0;
int
verify_pace(sc_card_t *card, int ref, const char *pin_label)
{
int r;
char *pin = NULL;
size_t pin_len = 0;
struct establish_pace_channel_input pace_input;
struct establish_pace_channel_output pace_output;
printf("Enter %s:", pin_label);
r = util_getpass(&pin, &pin_len, stdin);
if (r < 0 || pin == NULL) {
printf("Unable to get PIN.\n");
return -1;
}
memset(&pace_input, 0, sizeof pace_input);
memset(&pace_output, 0, sizeof pace_output);
pace_input.pin_id = ref;
pace_input.pin = (unsigned char *)pin;
pace_input.pin_length = strlen(pin);
r = perform_pace(card, pace_input, &pace_output, EAC_TR_VERSION_2_02);
sc_mem_clear(pin, pin_len);
free(pin);
free(pace_output.ef_cardaccess);
free(pace_output.recent_car);
free(pace_output.previous_car);
free(pace_output.id_icc);
free(pace_output.id_pcd);
if (r) {
printf("Error verifying CAN.\n");
return -1;
}
return 0;
}
void
pin_status(sc_card_t *card, int ref, const char *pin_label, unsigned char transport)
{
int r;
struct sc_pin_cmd_data data;
int tries_left = 0;
memset(&data, 0, sizeof(data));
data.cmd = SC_PIN_CMD_GET_INFO;
data.pin_type = SC_AC_CHV;
data.pin_reference = ref;
r = sc_pin_cmd(card, &data, &tries_left);
if (r == SC_SUCCESS) {
if (tries_left < 0)
printf("%s: usable\n", pin_label);
else
printf("%s: usable (%d tries left)\n", pin_label, tries_left);
} else if (r == SC_ERROR_SECURITY_STATUS_NOT_SATISFIED)
printf("%s: not usable (transport protection still in force)\n", pin_label);
else if (r == SC_ERROR_AUTH_METHOD_BLOCKED)
printf("%s: blocked (use PUK to unblock PIN)\n", pin_label);
else if (r == SC_ERROR_REF_DATA_NOT_USABLE) {
if (transport)
printf("%s: not usable (transport protection already broken)\n", pin_label);
else
printf("%s: not usable\n", pin_label);
} else
fprintf(stderr, "%s: status query failed (%s).\n", pin_label, sc_strerror(r));
}
int
check_transport_protection(sc_card_t *card, u8 ref, const char *pin_label)
{
struct sc_apdu apdu;
int r;
u8 buf[6];
u8 prot_intact[6] = {0xE3, 0x04, 0x90, 0x02, 0x00, 0x01};
u8 prot_broken[6] = {0xE3, 0x04, 0x90, 0x02, 0x00, 0x00};
sc_format_apdu_ex(&apdu, 0x80, 0xCA, 0x00, ref, NULL, 0, buf, sizeof(buf));
r = sc_transmit_apdu(card, &apdu);
if (r != SC_SUCCESS) {
fprintf(stderr, "Check transport protection of %s: APDU transmit failed (%s)\n", pin_label, sc_strerror(r));
return -1;
}
r = sc_check_sw(card, apdu.sw1, apdu.sw2);
if (r != SC_SUCCESS) {
fprintf(stderr, "Check transport protection of %s: GET_DATA failed (%s)\n", pin_label, sc_strerror(r));
return -1;
}
if (apdu.resplen == sizeof(prot_intact) && !memcmp(apdu.resp, prot_intact, 6)) {
printf("Transport protection of %s is still intact.\n", pin_label);
return 0;
} else if (apdu.resplen == sizeof(prot_broken) && !memcmp(apdu.resp, prot_broken, 6)) {
printf("Transport protection of %s is broken.\n", pin_label);
return 1;
}
fprintf(stderr, "Check transport protection of %s: illegal response: ", pin_label);
util_hex_dump(stderr, apdu.resp, apdu.resplen, " ");
fprintf(stderr, "\n");
return -1;
}
void
unlock_transport_protection(sc_card_t *card)
{
struct sc_pin_cmd_data data;
int r;
char *tpin = NULL;
char *qespin1 = NULL;
char *qespin2 = NULL;
size_t tpin_len = 0;
size_t qespin1_len = 0;
size_t qespin2_len = 0;
int tries_left;
memset(&data, 0, sizeof(data));
data.cmd = SC_PIN_CMD_CHANGE;
data.pin_type = SC_AC_CHV;
data.pin_reference = 0x87;
data.pin1.min_length = 5;
data.pin1.max_length = 5;
data.pin2.min_length = 6;
data.pin2.max_length = 12;
if (card->reader->capabilities & SC_READER_CAP_PIN_PAD) {
printf("Please enter PINs on the reader's pin pad.\n");
data.pin1.prompt = "Enter Transport PIN";
data.pin2.prompt = "Enter Signature PIN";
data.flags |= SC_PIN_CMD_USE_PINPAD;
} else {
printf("Enter Transport PIN:");
r = util_getpass(&tpin, &tpin_len, stdin);
if (r < 0 || tpin == NULL) {
fprintf(stderr, "Unable to get PIN");
return;
}
printf("Enter new Signature PIN:");
r = util_getpass(&qespin1, &qespin1_len, stdin);
if (r < 0 || qespin1 == NULL) {
fprintf(stderr, "Unable to get PIN");
goto fail_qespin1;
}
printf("Enter new Signature PIN again:");
r = util_getpass(&qespin2, &qespin1_len, stdin);
if (r < 0 || qespin2 == NULL) {
fprintf(stderr, "Unable to get PIN");
goto fail_qespin2;
}
if (strcmp(qespin1, qespin2)) {
fprintf(stderr, "New signature PINs doesn't match.\n");
goto fail_qespin2;
}
data.pin1.data = (u8 *)tpin;
data.pin1.len = strlen(tpin);
data.pin2.data = (u8 *)qespin1;
data.pin2.len = strlen(qespin1);
}
r = sc_pin_cmd(card, &data, &tries_left);
if (r == SC_SUCCESS)
printf("Transport protection removed. You can now use your Signature PIN.\n");
else if (r == SC_ERROR_PIN_CODE_INCORRECT)
printf("Wrong pin. %d attempts left.\n", tries_left);
else
printf("Can't change pin: %s\n", sc_strerror(r));
if (qespin2 != NULL) {
sc_mem_clear(qespin2, qespin2_len);
free(qespin2);
}
fail_qespin2:
if (qespin1 != NULL) {
sc_mem_clear(qespin1, qespin1_len);
free(qespin1);
}
fail_qespin1:
if (tpin != NULL) {
sc_mem_clear(tpin, tpin_len);
free(tpin);
}
}
int
main(int argc, char *argv[])
{
int r, c, long_optind = 0;
sc_context_param_t ctx_param;
sc_card_t *card = NULL;
sc_context_t *ctx = NULL;
sc_path_t path;
while (1) {
c = getopt_long(argc, argv, "r:wscuhv", options, &long_optind);
if (c == -1)
break;
if (c == '?' || c == 'h')
util_print_usage_and_die(app_name, options, option_help, NULL);
switch (c) {
case 'r':
opt_reader = optarg;
break;
case 'w':
opt_wait = 1;
break;
case 's':
opt_status = 1;
break;
case 'c':
opt_check = 1;
break;
case 'u':
opt_unlock = 1;
break;
case 'v':
verbose++;
break;
}
}
memset(&ctx_param, 0, sizeof(ctx_param));
ctx_param.ver = 0;
ctx_param.app_name = argv[0];
ctx_param.debug = verbose;
if (verbose)
ctx_param.debug_file = stderr;
r = sc_context_create(&ctx, &ctx_param);
if (r) {
printf("Failed to establish context: %s\n", sc_strerror(r));
return 1;
}
r = sc_set_card_driver(ctx, "dtrust");
if (r) {
printf("Driver 'dtrust' not found!\n");
goto out;
}
r = util_connect_card(ctx, &card, opt_reader, opt_wait);
if (r)
goto out;
if (card->type == SC_CARD_TYPE_DTRUST_V5_1_STD ||
card->type == SC_CARD_TYPE_DTRUST_V5_1_MULTI ||
card->type == SC_CARD_TYPE_DTRUST_V5_1_M100 ||
card->type == SC_CARD_TYPE_DTRUST_V5_4_STD ||
card->type == SC_CARD_TYPE_DTRUST_V5_4_MULTI) {
/* D-Trust Card 5 requires PACE authentication with CAN */
if (opt_status || opt_check || opt_unlock) {
r = verify_pace(card, PACE_CAN, "CAN");
if (r)
goto out;
}
}
if (opt_status) {
switch (card->type) {
case SC_CARD_TYPE_DTRUST_V4_1_STD:
case SC_CARD_TYPE_DTRUST_V4_1_MULTI:
case SC_CARD_TYPE_DTRUST_V4_1_M100:
pin_status(card, 0x03, "Card Holder PIN", 0);
/* FALLTHRU */
case SC_CARD_TYPE_DTRUST_V4_4_STD:
case SC_CARD_TYPE_DTRUST_V4_4_MULTI:
/* We have to select the QES app to verify and change the Signature PIN. */
sc_format_path("3F000101", &path);
r = sc_select_file(card, &path, NULL);
if (r)
goto out;
pin_status(card, 0x04, "Card Holder PUK", 0);
pin_status(card, 0x87, "Signature PIN", 0);
pin_status(card, 0x8B, "Transport PIN", 1);
break;
case SC_CARD_TYPE_DTRUST_V5_1_STD:
case SC_CARD_TYPE_DTRUST_V5_1_MULTI:
case SC_CARD_TYPE_DTRUST_V5_1_M100:
/* We have to select the eSign app to verify and change the Authentication PIN. */
sc_format_path("3F000102", &path);
r = sc_select_file(card, &path, NULL);
if (r)
goto out;
pin_status(card, 0x91, "Authentication PIN", 0);
pin_status(card, 0x0C, "Transport PIN (Authentication)", 1);
/* FALLTHRU */
case SC_CARD_TYPE_DTRUST_V5_4_STD:
case SC_CARD_TYPE_DTRUST_V5_4_MULTI:
/* We have to select the QES app to verify and change the Signature PIN. */
sc_format_path("3F000101", &path);
r = sc_select_file(card, &path, NULL);
if (r)
goto out;
pin_status(card, 0x04, "Card Holder PUK", 0);
pin_status(card, 0x87, "Signature PIN", 0);
pin_status(card, 0x0B, "Transport PIN (Signature)", 1);
break;
}
}
if (opt_check) {
switch (card->type) {
case SC_CARD_TYPE_DTRUST_V4_1_STD:
case SC_CARD_TYPE_DTRUST_V4_1_MULTI:
case SC_CARD_TYPE_DTRUST_V4_1_M100:
case SC_CARD_TYPE_DTRUST_V4_4_STD:
case SC_CARD_TYPE_DTRUST_V4_4_MULTI:
check_transport_protection(card, 0x0B, "Signature PIN");
break;
case SC_CARD_TYPE_DTRUST_V5_1_STD:
case SC_CARD_TYPE_DTRUST_V5_1_MULTI:
case SC_CARD_TYPE_DTRUST_V5_1_M100:
check_transport_protection(card, 0x0B, "Signature PIN");
/* FALLTHRU */
case SC_CARD_TYPE_DTRUST_V5_4_STD:
case SC_CARD_TYPE_DTRUST_V5_4_MULTI:
check_transport_protection(card, 0x0C, "Authentication PIN");
break;
}
}
if (opt_unlock) {
r = check_transport_protection(card, 0x0B, "Signature PIN");
if (r)
printf("Cannot remove transport protection of Signature PIN.\n");
else
unlock_transport_protection(card);
}
out:
if (card) {
sc_unlock(card);
sc_disconnect_card(card);
}
sc_release_context(ctx);
return EXIT_SUCCESS;
}