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
|
/* app-geldkarte.c - The German Geldkarte application
* Copyright (C) 2004 g10 Code GmbH
* Copyright (C) 2009 Free Software Foundation, Inc.
*
* This file is part of GnuPG.
*
* GnuPG is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* GnuPG 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses/>.
*/
/* This is a read-only application to quickly dump information of a
German Geldkarte (debit card for small amounts). We only support
newer Geldkarte (with the AID DF_BOERSE_NEU) issued since 2000 or
even earlier.
*/
#include <config.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <time.h>
#include <ctype.h>
#include "scdaemon.h"
#include "../common/i18n.h"
#include "iso7816.h"
#include "../common/tlv.h"
/* Object with application (i.e. Geldkarte) specific data. */
struct app_local_s
{
char kblz[2+1+4+1];
const char *banktype;
char *cardno;
char expires[7+1];
char validfrom[10+1];
char *country;
char currency[3+1];
unsigned int currency_mult100;
unsigned char chipid;
unsigned char osvers;
int balance;
int maxamount;
int maxamount1;
};
/* Deconstructor. */
static void
do_deinit (app_t app)
{
if (app && app->app_local)
{
xfree (app->app_local->cardno);
xfree (app->app_local->country);
xfree (app->app_local);
app->app_local = NULL;
}
}
static gpg_error_t
send_one_string (ctrl_t ctrl, const char *name, const char *string)
{
if (!name || !string)
return 0;
send_status_info (ctrl, name, string, strlen (string), NULL, 0);
return 0;
}
/* Implement the GETATTR command. This is similar to the LEARN
command but returns just one value via the status interface. */
static gpg_error_t
do_getattr (app_t app, ctrl_t ctrl, const char *name)
{
gpg_error_t err;
struct app_local_s *ld = app->app_local;
char numbuf[100];
if (!strcmp (name, "X-KBLZ"))
err = send_one_string (ctrl, name, ld->kblz);
else if (!strcmp (name, "X-BANKINFO"))
err = send_one_string (ctrl, name, ld->banktype);
else if (!strcmp (name, "X-CARDNO"))
err = send_one_string (ctrl, name, ld->cardno);
else if (!strcmp (name, "X-EXPIRES"))
err = send_one_string (ctrl, name, ld->expires);
else if (!strcmp (name, "X-VALIDFROM"))
err = send_one_string (ctrl, name, ld->validfrom);
else if (!strcmp (name, "X-COUNTRY"))
err = send_one_string (ctrl, name, ld->country);
else if (!strcmp (name, "X-CURRENCY"))
err = send_one_string (ctrl, name, ld->currency);
else if (!strcmp (name, "X-ZKACHIPID"))
{
snprintf (numbuf, sizeof numbuf, "0x%02X", ld->chipid);
err = send_one_string (ctrl, name, numbuf);
}
else if (!strcmp (name, "X-OSVERSION"))
{
snprintf (numbuf, sizeof numbuf, "0x%02X", ld->osvers);
err = send_one_string (ctrl, name, numbuf);
}
else if (!strcmp (name, "X-BALANCE"))
{
snprintf (numbuf, sizeof numbuf, "%.2f",
(double)ld->balance / 100 * ld->currency_mult100);
err = send_one_string (ctrl, name, numbuf);
}
else if (!strcmp (name, "X-MAXAMOUNT"))
{
snprintf (numbuf, sizeof numbuf, "%.2f",
(double)ld->maxamount / 100 * ld->currency_mult100);
err = send_one_string (ctrl, name, numbuf);
}
else if (!strcmp (name, "X-MAXAMOUNT1"))
{
snprintf (numbuf, sizeof numbuf, "%.2f",
(double)ld->maxamount1 / 100 * ld->currency_mult100);
err = send_one_string (ctrl, name, numbuf);
}
else
err = gpg_error (GPG_ERR_INV_NAME);
return err;
}
static gpg_error_t
do_learn_status (app_t app, ctrl_t ctrl, unsigned int flags)
{
static const char *names[] = {
"X-KBLZ",
"X-BANKINFO",
"X-CARDNO",
"X-EXPIRES",
"X-VALIDFROM",
"X-COUNTRY",
"X-CURRENCY",
"X-ZKACHIPID",
"X-OSVERSION",
"X-BALANCE",
"X-MAXAMOUNT",
"X-MAXAMOUNT1",
NULL
};
gpg_error_t err = 0;
int idx;
(void)flags;
for (idx=0; names[idx] && !err; idx++)
err = do_getattr (app, ctrl, names[idx]);
return err;
}
static char *
copy_bcd (const unsigned char *string, size_t length)
{
const unsigned char *s;
size_t n;
size_t needed;
char *buffer, *dst;
if (!length)
return xtrystrdup ("");
/* Skip leading zeroes. */
for (; length && !*string; length--, string++)
;
s = string;
n = length;
needed = 0;
for (; n ; n--, s++)
{
if (!needed && !(*s & 0xf0))
; /* Skip the leading zero in the first nibble. */
else
{
if ( ((*s >> 4) & 0x0f) > 9 )
{
errno = EINVAL;
return NULL;
}
needed++;
}
if ( n == 1 && (*s & 0x0f) > 9 )
; /* Ignore the last digit if it has the sign. */
else
{
needed++;
if ( (*s & 0x0f) > 9 )
{
errno = EINVAL;
return NULL;
}
}
}
if (!needed) /* If it is all zero, print a "0". */
needed++;
buffer = dst = xtrymalloc (needed+1);
if (!buffer)
return NULL;
s = string;
n = length;
needed = 0;
for (; n ; n--, s++)
{
if (!needed && !(*s & 0xf0))
; /* Skip the leading zero in the first nibble. */
else
{
*dst++ = '0' + ((*s >> 4) & 0x0f);
needed++;
}
if ( n == 1 && (*s & 0x0f) > 9 )
; /* Ignore the last digit if it has the sign. */
else
{
*dst++ = '0' + (*s & 0x0f);
needed++;
}
}
if (!needed)
*dst++ = '0';
*dst = 0;
return buffer;
}
/* Convert the BCD number at STING of LENGTH into an integer and store
that at RESULT. Return 0 on success. */
static gpg_error_t
bcd_to_int (const unsigned char *string, size_t length, int *result)
{
char *tmp;
tmp = copy_bcd (string, length);
if (!tmp)
return gpg_error (GPG_ERR_BAD_DATA);
*result = strtol (tmp, NULL, 10);
xfree (tmp);
return 0;
}
/* Select the Geldkarte application. */
gpg_error_t
app_select_geldkarte (app_t app)
{
static char const aid[] =
{ 0xD2, 0x76, 0x00, 0x00, 0x25, 0x45, 0x50, 0x02, 0x00 };
gpg_error_t err;
int slot = app->slot;
unsigned char *result = NULL;
size_t resultlen;
struct app_local_s *ld;
const char *banktype;
err = iso7816_select_application (slot, aid, sizeof aid, 0);
if (err)
goto leave;
/* Read the first record of EF_ID (SFI=0x17). We require this
record to be at least 24 bytes with the first byte 0x67 and a
correct filler byte. */
err = iso7816_read_record (slot, 1, 1, ((0x17 << 3)|4), &result, &resultlen);
if (err)
goto leave; /* Oops - not a Geldkarte. */
if (resultlen < 24 || *result != 0x67 || result[22])
{
err = gpg_error (GPG_ERR_NOT_FOUND);
goto leave;
}
/* The short Bankleitzahl consists of 3 bytes at offset 1. */
switch (result[1])
{
case 0x21: banktype = "Oeffentlich-rechtliche oder private Bank"; break;
case 0x22: banktype = "Privat- oder Geschaeftsbank"; break;
case 0x25: banktype = "Sparkasse"; break;
case 0x26:
case 0x29: banktype = "Genossenschaftsbank"; break;
default:
err = gpg_error (GPG_ERR_NOT_FOUND);
goto leave; /* Probably not a Geldkarte. */
}
app->apptype = APPTYPE_GELDKARTE;
app->fnc.deinit = do_deinit;
/* If we don't have a serialno yet construct it from the EF_ID. */
if (!app->serialno)
{
app->serialno = xtrymalloc (10);
if (!app->serialno)
{
err = gpg_error_from_syserror ();
goto leave;
}
memcpy (app->serialno, result, 10);
app->serialnolen = 10;
err = app_munge_serialno (app);
if (err)
goto leave;
}
app->app_local = ld = xtrycalloc (1, sizeof *app->app_local);
if (!app->app_local)
{
err = gpg_err_code_from_syserror ();
goto leave;
}
snprintf (ld->kblz, sizeof ld->kblz, "%02X-%02X%02X",
result[1], result[2], result[3]);
ld->banktype = banktype;
ld->cardno = copy_bcd (result+4, 5);
if (!ld->cardno)
{
err = gpg_err_code_from_syserror ();
goto leave;
}
snprintf (ld->expires, sizeof ld->expires, "20%02X-%02X",
result[10], result[11]);
snprintf (ld->validfrom, sizeof ld->validfrom, "20%02X-%02X-%02X",
result[12], result[13], result[14]);
ld->country = copy_bcd (result+15, 2);
if (!ld->country)
{
err = gpg_err_code_from_syserror ();
goto leave;
}
snprintf (ld->currency, sizeof ld->currency, "%c%c%c",
isascii (result[17])? result[17]:' ',
isascii (result[18])? result[18]:' ',
isascii (result[19])? result[19]:' ');
ld->currency_mult100 = (result[20] == 0x01? 1:
result[20] == 0x02? 10:
result[20] == 0x04? 100:
result[20] == 0x08? 1000:
result[20] == 0x10? 10000:
result[20] == 0x20? 100000:0);
ld->chipid = result[21];
ld->osvers = result[23];
/* Read the first record of EF_BETRAG (SFI=0x18). */
xfree (result);
err = iso7816_read_record (slot, 1, 1, ((0x18 << 3)|4), &result, &resultlen);
if (err)
goto leave; /* It does not make sense to continue. */
if (resultlen < 12)
{
err = gpg_error (GPG_ERR_NOT_FOUND);
goto leave;
}
err = bcd_to_int (result+0, 3, &ld->balance);
if (!err)
err = bcd_to_int (result+3, 3, &ld->maxamount);
if (!err)
err = bcd_to_int (result+6, 3, &ld->maxamount1);
/* The next 3 bytes are the maximum amount chargable without using a
MAC. This is usually 0. */
if (err)
goto leave;
/* Setup the rest of the methods. */
app->fnc.learn_status = do_learn_status;
app->fnc.getattr = do_getattr;
leave:
xfree (result);
if (err)
do_deinit (app);
return err;
}
|