1 /* atr.c - ISO 7816 ATR fucntions
2 * Copyright (C) 2003, 2011 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <https://www.gnu.org/licenses/>.
27 #include <gpg-error.h>
28 #include "../common/logging.h"
31 static int const fi_table[16] = { 0, 372, 558, 744, 1116,1488, 1860, -1,
32 -1, 512, 768, 1024, 1536, 2048, -1, -1 };
33 static int const di_table[16] = { -1, 1, 2, 4, 8, 16, -1, -1,
34 0, -1, -2, -4, -8, -16, -32, -64};
37 /* Dump the ATR in (BUFFER,BUFLEN) to a human readable format and
38 return that as a malloced buffer. The caller must release this
39 buffer using es_free! On error this function returns NULL and sets
42 atr_dump (const void *buffer, size_t buflen)
44 const unsigned char *atr = buffer;
45 size_t atrlen = buflen;
47 int have_ta, have_tb, have_tc, have_td;
53 fp = es_fopenmem (0, "rwb,samethread");
59 es_fprintf (fp, "error: empty ATR\n");
63 for (idx=0; idx < atrlen ; idx++)
64 es_fprintf (fp, "%s%02X", idx?" ":"", atr[idx]);
68 es_fputs ("Direct convention\n", fp);
69 else if (*atr == 0x3f)
70 es_fputs ("Inverse convention\n", fp);
72 es_fprintf (fp,"error: invalid TS character 0x%02x\n", *atr);
78 for (idx=1; idx < atrlen-1; idx++)
81 have_ta = !!(*atr & 0x10);
82 have_tb = !!(*atr & 0x20);
83 have_tc = !!(*atr & 0x40);
84 have_td = !!(*atr & 0x80);
85 n_historical = (*atr & 0x0f);
86 es_fprintf (fp, "%d historical characters indicated\n", n_historical);
88 if (have_ta + have_tb + have_tc + have_td + n_historical > atrlen)
89 es_fputs ("error: ATR shorter than indicated by format character\n", fp);
96 es_fputs ("TA1: F=", fp);
97 val = fi_table[(*atr >> 4) & 0x0f];
99 es_fputs ("internal clock", fp);
101 es_fputs ("RFU", fp);
103 es_fprintf (fp, "%d", val);
104 es_fputs (" D=", fp);
105 val = di_table[*atr & 0x0f];
107 es_fputs ("[impossible value]\n", fp);
109 es_fputs ("RFU\n", fp);
111 es_fprintf (fp, "1/%d\n", val);
113 es_fprintf (fp, "%d\n", val);
122 es_fprintf (fp, "TB1: II=%d PI1=%d%s\n",
123 ((*atr >> 5) & 3), (*atr & 0x1f),
124 (*atr & 0x80)? " [high bit not cleared]":"");
133 es_fputs ("TC1: guard time shortened to 1 etu\n", fp);
135 es_fprintf (fp, "TC1: (extra guard time) N=%d\n", *atr);
144 have_ta = !!(*atr & 0x10);
145 have_tb = !!(*atr & 0x20);
146 have_tc = !!(*atr & 0x40);
147 have_td = !!(*atr & 0x80);
148 es_fprintf (fp, "TD1: protocol T%d supported\n", (*atr & 0x0f));
150 if (have_ta + have_tb + have_tc + have_td + n_historical > atrlen)
151 es_fputs ("error: ATR shorter than indicated by format character\n",
159 have_ta = have_tb = have_tc = have_td = 0;
163 es_fprintf (fp, "TA2: (PTS) %stoggle, %splicit, T=%02X\n",
164 (*atr & 0x80)? "no-":"",
165 (*atr & 0x10)? "im": "ex",
168 es_fprintf (fp, "note: reserved bits are set (TA2=0x%02X)\n", *atr);
176 es_fprintf (fp, "TB2: PI2=%d\n", *atr);
184 es_fprintf (fp, "TC2: PWI=%d\n", *atr);
192 have_ta = !!(*atr & 0x10);
193 have_tb = !!(*atr & 0x20);
194 have_tc = !!(*atr & 0x40);
195 have_td = !!(*atr & 0x80);
196 es_fprintf (fp, "TD2: protocol T%d supported\n", *atr & 0x0f);
198 if (have_ta + have_tb + have_tc + have_td + n_historical > atrlen)
199 es_fputs ("error: ATR shorter than indicated by format character\n",
207 have_ta = have_tb = have_tc = have_td = 0;
209 for (idx = 3; have_ta || have_tb || have_tc || have_td; idx++)
213 es_fprintf (fp, "TA%d: IFSC=%d\n", idx, *atr);
221 es_fprintf (fp, "TB%d: BWI=%d CWI=%d\n",
222 idx, (*atr >> 4) & 0x0f, *atr & 0x0f);
230 es_fprintf (fp, "TC%d: 0x%02X\n", idx, *atr);
238 have_ta = !!(*atr & 0x10);
239 have_tb = !!(*atr & 0x20);
240 have_tc = !!(*atr & 0x40);
241 have_td = !!(*atr & 0x80);
242 es_fprintf (fp, "TD%d: protocol T%d supported\n", idx, *atr & 0x0f);
244 if (have_ta + have_tb + have_tc + have_td + n_historical > atrlen)
246 "ATR shorter than indicated by format character\n",
254 have_ta = have_tb = have_tc = have_td = 0;
257 if (n_historical + 1 > atrlen)
258 es_fputs ("error: ATR shorter than required for historical bytes "
259 "and checksum\n", fp);
263 es_fputs ("HCH:", fp);
264 for (; n_historical && atrlen ; n_historical--, atrlen--, atr++)
265 es_fprintf (fp, " %02X", *atr);
270 es_fputs ("error: checksum missing\n", fp);
271 else if (*atr == chksum)
272 es_fprintf (fp, "TCK: %02X (good)\n", *atr);
274 es_fprintf (fp, "TCK: %02X (bad; computed %02X)\n", *atr, chksum);
278 es_fprintf (fp, "error: %u bytes garbage at end of ATR\n",
279 (unsigned int)atrlen );
282 es_putc ('\0', fp); /* We want a string. */
283 if (es_fclose_snatch (fp, (void**)&result, NULL))
285 log_error ("oops: es_fclose_snatch failed: %s\n", strerror (errno));