2017-04-28 03:06:33 +02:00
|
|
|
/* atr.c - ISO 7816 ATR functions
|
2011-12-15 14:47:04 +01:00
|
|
|
* Copyright (C) 2003, 2011 Free Software Foundation, Inc.
|
2003-06-05 09:14:21 +02:00
|
|
|
*
|
|
|
|
* 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
|
2007-07-04 21:49:40 +02:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2003-06-05 09:14:21 +02:00
|
|
|
* (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
|
2016-11-05 12:02:19 +01:00
|
|
|
* along with this program; if not, see <https://www.gnu.org/licenses/>.
|
2003-06-05 09:14:21 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2014-08-26 17:47:22 +02:00
|
|
|
#include <gpg-error.h>
|
2011-12-15 14:47:04 +01:00
|
|
|
#include "../common/logging.h"
|
2003-06-05 09:14:21 +02:00
|
|
|
#include "atr.h"
|
|
|
|
|
|
|
|
static int const fi_table[16] = { 0, 372, 558, 744, 1116,1488, 1860, -1,
|
|
|
|
-1, 512, 768, 1024, 1536, 2048, -1, -1 };
|
|
|
|
static int const di_table[16] = { -1, 1, 2, 4, 8, 16, -1, -1,
|
|
|
|
0, -1, -2, -4, -8, -16, -32, -64};
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2003-06-05 09:14:21 +02:00
|
|
|
|
2011-12-15 14:47:04 +01:00
|
|
|
/* Dump the ATR in (BUFFER,BUFLEN) to a human readable format and
|
|
|
|
return that as a malloced buffer. The caller must release this
|
|
|
|
buffer using es_free! On error this function returns NULL and sets
|
|
|
|
ERRNO. */
|
|
|
|
char *
|
|
|
|
atr_dump (const void *buffer, size_t buflen)
|
2003-06-05 09:14:21 +02:00
|
|
|
{
|
2011-12-15 14:47:04 +01:00
|
|
|
const unsigned char *atr = buffer;
|
|
|
|
size_t atrlen = buflen;
|
|
|
|
estream_t fp;
|
2003-06-05 09:14:21 +02:00
|
|
|
int have_ta, have_tb, have_tc, have_td;
|
|
|
|
int n_historical;
|
|
|
|
int idx, val;
|
|
|
|
unsigned char chksum;
|
2011-12-15 14:47:04 +01:00
|
|
|
char *result;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
2014-05-06 09:49:26 +02:00
|
|
|
fp = es_fopenmem (0, "rwb,samethread");
|
2011-12-15 14:47:04 +01:00
|
|
|
if (!fp)
|
|
|
|
return NULL;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2003-06-05 09:14:21 +02:00
|
|
|
if (!atrlen)
|
|
|
|
{
|
2011-12-15 14:47:04 +01:00
|
|
|
es_fprintf (fp, "error: empty ATR\n");
|
2003-06-05 09:14:21 +02:00
|
|
|
goto bailout;
|
|
|
|
}
|
|
|
|
|
2011-12-15 14:47:04 +01:00
|
|
|
for (idx=0; idx < atrlen ; idx++)
|
|
|
|
es_fprintf (fp, "%s%02X", idx?" ":"", atr[idx]);
|
|
|
|
es_putc ('\n', fp);
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2003-06-05 09:14:21 +02:00
|
|
|
if (*atr == 0x3b)
|
2011-12-15 14:47:04 +01:00
|
|
|
es_fputs ("Direct convention\n", fp);
|
2003-06-05 09:14:21 +02:00
|
|
|
else if (*atr == 0x3f)
|
2011-12-15 14:47:04 +01:00
|
|
|
es_fputs ("Inverse convention\n", fp);
|
2003-06-05 09:14:21 +02:00
|
|
|
else
|
2011-12-15 14:47:04 +01:00
|
|
|
es_fprintf (fp,"error: invalid TS character 0x%02x\n", *atr);
|
2003-06-05 09:14:21 +02:00
|
|
|
if (!--atrlen)
|
|
|
|
goto bailout;
|
|
|
|
atr++;
|
|
|
|
|
|
|
|
chksum = *atr;
|
|
|
|
for (idx=1; idx < atrlen-1; idx++)
|
|
|
|
chksum ^= atr[idx];
|
|
|
|
|
|
|
|
have_ta = !!(*atr & 0x10);
|
|
|
|
have_tb = !!(*atr & 0x20);
|
|
|
|
have_tc = !!(*atr & 0x40);
|
|
|
|
have_td = !!(*atr & 0x80);
|
|
|
|
n_historical = (*atr & 0x0f);
|
2011-12-15 14:47:04 +01:00
|
|
|
es_fprintf (fp, "%d historical characters indicated\n", n_historical);
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
|
|
if (have_ta + have_tb + have_tc + have_td + n_historical > atrlen)
|
2011-12-15 14:47:04 +01:00
|
|
|
es_fputs ("error: ATR shorter than indicated by format character\n", fp);
|
2003-06-05 09:14:21 +02:00
|
|
|
if (!--atrlen)
|
|
|
|
goto bailout;
|
|
|
|
atr++;
|
|
|
|
|
|
|
|
if (have_ta)
|
|
|
|
{
|
2011-12-15 14:47:04 +01:00
|
|
|
es_fputs ("TA1: F=", fp);
|
2003-06-05 09:14:21 +02:00
|
|
|
val = fi_table[(*atr >> 4) & 0x0f];
|
|
|
|
if (!val)
|
2011-12-15 14:47:04 +01:00
|
|
|
es_fputs ("internal clock", fp);
|
2003-06-05 09:14:21 +02:00
|
|
|
else if (val == -1)
|
2011-12-15 14:47:04 +01:00
|
|
|
es_fputs ("RFU", fp);
|
2003-06-05 09:14:21 +02:00
|
|
|
else
|
2011-12-15 14:47:04 +01:00
|
|
|
es_fprintf (fp, "%d", val);
|
|
|
|
es_fputs (" D=", fp);
|
2011-02-04 12:57:53 +01:00
|
|
|
val = di_table[*atr & 0x0f];
|
2003-06-05 09:14:21 +02:00
|
|
|
if (!val)
|
2011-12-15 14:47:04 +01:00
|
|
|
es_fputs ("[impossible value]\n", fp);
|
2003-06-05 09:14:21 +02:00
|
|
|
else if (val == -1)
|
2011-12-15 14:47:04 +01:00
|
|
|
es_fputs ("RFU\n", fp);
|
2003-06-05 09:14:21 +02:00
|
|
|
else if (val < 0 )
|
2011-12-15 14:47:04 +01:00
|
|
|
es_fprintf (fp, "1/%d\n", val);
|
2011-02-04 12:57:53 +01:00
|
|
|
else
|
2011-12-15 14:47:04 +01:00
|
|
|
es_fprintf (fp, "%d\n", val);
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2003-06-05 09:14:21 +02:00
|
|
|
if (!--atrlen)
|
|
|
|
goto bailout;
|
|
|
|
atr++;
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2003-06-05 09:14:21 +02:00
|
|
|
if (have_tb)
|
|
|
|
{
|
2011-12-15 14:47:04 +01:00
|
|
|
es_fprintf (fp, "TB1: II=%d PI1=%d%s\n",
|
|
|
|
((*atr >> 5) & 3), (*atr & 0x1f),
|
|
|
|
(*atr & 0x80)? " [high bit not cleared]":"");
|
2003-06-05 09:14:21 +02:00
|
|
|
if (!--atrlen)
|
|
|
|
goto bailout;
|
|
|
|
atr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (have_tc)
|
|
|
|
{
|
|
|
|
if (*atr == 255)
|
2011-12-15 14:47:04 +01:00
|
|
|
es_fputs ("TC1: guard time shortened to 1 etu\n", fp);
|
2003-06-05 09:14:21 +02:00
|
|
|
else
|
2011-12-15 14:47:04 +01:00
|
|
|
es_fprintf (fp, "TC1: (extra guard time) N=%d\n", *atr);
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
|
|
if (!--atrlen)
|
|
|
|
goto bailout;
|
|
|
|
atr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (have_td)
|
|
|
|
{
|
|
|
|
have_ta = !!(*atr & 0x10);
|
|
|
|
have_tb = !!(*atr & 0x20);
|
|
|
|
have_tc = !!(*atr & 0x40);
|
|
|
|
have_td = !!(*atr & 0x80);
|
2011-12-15 14:47:04 +01:00
|
|
|
es_fprintf (fp, "TD1: protocol T%d supported\n", (*atr & 0x0f));
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
|
|
if (have_ta + have_tb + have_tc + have_td + n_historical > atrlen)
|
2011-12-15 14:47:04 +01:00
|
|
|
es_fputs ("error: ATR shorter than indicated by format character\n",
|
|
|
|
fp);
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
|
|
if (!--atrlen)
|
|
|
|
goto bailout;
|
|
|
|
atr++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
have_ta = have_tb = have_tc = have_td = 0;
|
|
|
|
|
|
|
|
if (have_ta)
|
|
|
|
{
|
2011-12-15 14:47:04 +01:00
|
|
|
es_fprintf (fp, "TA2: (PTS) %stoggle, %splicit, T=%02X\n",
|
|
|
|
(*atr & 0x80)? "no-":"",
|
|
|
|
(*atr & 0x10)? "im": "ex",
|
|
|
|
(*atr & 0x0f));
|
2003-06-05 09:14:21 +02:00
|
|
|
if ((*atr & 0x60))
|
2011-12-15 14:47:04 +01:00
|
|
|
es_fprintf (fp, "note: reserved bits are set (TA2=0x%02X)\n", *atr);
|
2003-06-05 09:14:21 +02:00
|
|
|
if (!--atrlen)
|
|
|
|
goto bailout;
|
|
|
|
atr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (have_tb)
|
|
|
|
{
|
2011-12-15 14:47:04 +01:00
|
|
|
es_fprintf (fp, "TB2: PI2=%d\n", *atr);
|
2003-06-05 09:14:21 +02:00
|
|
|
if (!--atrlen)
|
|
|
|
goto bailout;
|
|
|
|
atr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (have_tc)
|
|
|
|
{
|
2011-12-15 14:47:04 +01:00
|
|
|
es_fprintf (fp, "TC2: PWI=%d\n", *atr);
|
2003-06-05 09:14:21 +02:00
|
|
|
if (!--atrlen)
|
|
|
|
goto bailout;
|
|
|
|
atr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (have_td)
|
|
|
|
{
|
|
|
|
have_ta = !!(*atr & 0x10);
|
|
|
|
have_tb = !!(*atr & 0x20);
|
|
|
|
have_tc = !!(*atr & 0x40);
|
|
|
|
have_td = !!(*atr & 0x80);
|
2011-12-15 14:47:04 +01:00
|
|
|
es_fprintf (fp, "TD2: protocol T%d supported\n", *atr & 0x0f);
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
|
|
if (have_ta + have_tb + have_tc + have_td + n_historical > atrlen)
|
2011-12-15 14:47:04 +01:00
|
|
|
es_fputs ("error: ATR shorter than indicated by format character\n",
|
|
|
|
fp);
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
|
|
if (!--atrlen)
|
|
|
|
goto bailout;
|
|
|
|
atr++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
have_ta = have_tb = have_tc = have_td = 0;
|
|
|
|
|
|
|
|
for (idx = 3; have_ta || have_tb || have_tc || have_td; idx++)
|
|
|
|
{
|
|
|
|
if (have_ta)
|
|
|
|
{
|
2011-12-15 14:47:04 +01:00
|
|
|
es_fprintf (fp, "TA%d: IFSC=%d\n", idx, *atr);
|
2003-06-05 09:14:21 +02:00
|
|
|
if (!--atrlen)
|
|
|
|
goto bailout;
|
|
|
|
atr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (have_tb)
|
|
|
|
{
|
2011-12-15 14:47:04 +01:00
|
|
|
es_fprintf (fp, "TB%d: BWI=%d CWI=%d\n",
|
2003-06-05 09:14:21 +02:00
|
|
|
idx, (*atr >> 4) & 0x0f, *atr & 0x0f);
|
|
|
|
if (!--atrlen)
|
|
|
|
goto bailout;
|
|
|
|
atr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (have_tc)
|
|
|
|
{
|
2011-12-15 14:47:04 +01:00
|
|
|
es_fprintf (fp, "TC%d: 0x%02X\n", idx, *atr);
|
2003-06-05 09:14:21 +02:00
|
|
|
if (!--atrlen)
|
|
|
|
goto bailout;
|
|
|
|
atr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (have_td)
|
|
|
|
{
|
|
|
|
have_ta = !!(*atr & 0x10);
|
|
|
|
have_tb = !!(*atr & 0x20);
|
|
|
|
have_tc = !!(*atr & 0x40);
|
|
|
|
have_td = !!(*atr & 0x80);
|
2011-12-15 14:47:04 +01:00
|
|
|
es_fprintf (fp, "TD%d: protocol T%d supported\n", idx, *atr & 0x0f);
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
|
|
if (have_ta + have_tb + have_tc + have_td + n_historical > atrlen)
|
2011-12-15 14:47:04 +01:00
|
|
|
es_fputs ("error: "
|
|
|
|
"ATR shorter than indicated by format character\n",
|
|
|
|
fp);
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
|
|
if (!--atrlen)
|
|
|
|
goto bailout;
|
|
|
|
atr++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
have_ta = have_tb = have_tc = have_td = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n_historical + 1 > atrlen)
|
2011-12-15 14:47:04 +01:00
|
|
|
es_fputs ("error: ATR shorter than required for historical bytes "
|
|
|
|
"and checksum\n", fp);
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2003-06-05 09:14:21 +02:00
|
|
|
if (n_historical)
|
|
|
|
{
|
2011-12-15 14:47:04 +01:00
|
|
|
es_fputs ("HCH:", fp);
|
2003-06-05 09:14:21 +02:00
|
|
|
for (; n_historical && atrlen ; n_historical--, atrlen--, atr++)
|
2011-12-15 14:47:04 +01:00
|
|
|
es_fprintf (fp, " %02X", *atr);
|
|
|
|
es_putc ('\n', fp);
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!atrlen)
|
2011-12-15 14:47:04 +01:00
|
|
|
es_fputs ("error: checksum missing\n", fp);
|
2003-06-05 09:14:21 +02:00
|
|
|
else if (*atr == chksum)
|
2011-12-15 14:47:04 +01:00
|
|
|
es_fprintf (fp, "TCK: %02X (good)\n", *atr);
|
2003-06-05 09:14:21 +02:00
|
|
|
else
|
2011-12-15 14:47:04 +01:00
|
|
|
es_fprintf (fp, "TCK: %02X (bad; computed %02X)\n", *atr, chksum);
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
|
|
atrlen--;
|
|
|
|
if (atrlen)
|
2011-12-15 14:47:04 +01:00
|
|
|
es_fprintf (fp, "error: %u bytes garbage at end of ATR\n",
|
|
|
|
(unsigned int)atrlen );
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
|
|
bailout:
|
2011-12-15 14:47:04 +01:00
|
|
|
es_putc ('\0', fp); /* We want a string. */
|
|
|
|
if (es_fclose_snatch (fp, (void**)&result, NULL))
|
2008-09-23 11:57:45 +02:00
|
|
|
{
|
2011-12-15 14:47:04 +01:00
|
|
|
log_error ("oops: es_fclose_snatch failed: %s\n", strerror (errno));
|
|
|
|
return NULL;
|
2008-09-23 11:57:45 +02:00
|
|
|
}
|
2003-06-05 09:14:21 +02:00
|
|
|
|
2008-09-23 11:57:45 +02:00
|
|
|
return result;
|
|
|
|
}
|