On Monday 05 August 2002 18:22, you wrote:
> I've managed to get your program (gsmcard) to get me lists of numbers
> from an Orange sim card (orange is a UK phone provider). However, when
> I try a t-mobile sim card I get errors such as:
>
> Err: cardcomm.c#192 => -2146435057=0x8010000F
> 402 select 3f 00: #define SCARD_E_PROTO_MISMATCH          0x8010000F
I get these for some other smart cards - I didn't get this error with GSM 
cards up until now. I've tested max. (which is now t_mobile) cards and they 
worked.

> Also I notice that on sim cards that work - I get a valid card ATR
> from pcscd, but the t-mobile cards give me a NULL value. So I'm
> guessing that this is the likely problem.
Well, that's likely.

> I thought that you would probably be the person most likely to have
> experienced problems with gsm sim cards - have you experienced
> anything like this before? Do you think its the card itself? Or maybe
> you need to "unlock" the card before getting a valid ATR?
No. The ATR is the "Answer To Reset" - there ain't no communication
before that.

> My best guess is that t-mobile sims just aren't complient with some
> protocol being used. When I insert a working card, I get a valid ATR and
> the light on my reader stays on. With a t-mobile sim, the light goes
> out and a NULL ATR is debugged to the screen (by pcscd)...
Well, everything is possible. But I can't believe that these cards are not 
compliant - maybe they are broken? How many did you test? Do they work 
in phones?

> So, if you've got any suggestions at all, they'd be greatly
> appreciated. I really don't know where to start debugging...
Hmm. I've attached the verbose (and butt-ugly :-) program used with my gsm 
viewer. Please try to compile and send me the output.
I expect you'll have to change the paths in Makefile.
There should be some lines with "prot=" which show the used protocol (T=0 or 
T=1).
Maybe that's the problem? I can't say now.

> Thanks,
>
> Matt
Regards,

Phil


CFLAGS=-I/usr/local/include -g -Wall
LDFLAGS=-L/usr/local/lib -lpcsclite -ldl


all:	mytest tags

tags:	*.c 
	ctags *.c /usr/local/pcsc/include/*.h
mytest: mytest.c
#include <stdio.h>
#include <winscard.h>
#include <ctype.h>
#include <unistd.h>


#define PROTOCOL (SCARD_PROTOCOL_T0)
// #define PROTOCOL (SCARD_PROTOCOL_T1)
// #define PROTOCOL (SCARD_PROTOCOL_RAW)


void OnExit(void) __attribute__ ((destructor));
void OnStart(void) __attribute__ ((constructor));
int Interactive(void);
int DumpCmd(char *, void *, int );


#define E(x)  \
  if (x!=SCARD_S_SUCCESS)  \
  {  \
    int e=x; \
    OnExit(); \
    fprintf(stderr,"Err: %s#%d => %d=0x%X\n",__FILE__,__LINE__,e,e);  \
    sprintf(buffer,"0x%04x",e); \
    execlp("grep",  \
        "grep","-i",buffer,"/usr/local/include/pcsclite.h",NULL); \
    exit(1);  \
  }


SCARDCONTEXT scc=0;
SCARDHANDLE card=0;

#define MAXBUFF (256)
char buffer[MAXBUFF];
char buffer2[MAXBUFF];
LONG ret,len,state,prot,len2;
SCARD_IO_REQUEST scir;
char *cp;



char emptyverify[]={0xc0,0x20,0x00,0x00};
char selectroot[]={0xa0,0xa4,0x00,0x00,0x02,0x3f,0x00};
char readbin[]={0xc0,0xb0,0x00,0x00,0x00};


typedef struct { char msk[2], rsp[2], *text; } t_response;

t_response Cresponse[]=
    {
      { { 0xff, 0xff }, { 0x90, 0x00 } , "Ok" },

      { { 0xff, 0x00 }, { 0x61, 0x00 } , "%d response bytes available" },
      { { 0xff, 0x00 }, { 0x9f, 0x00 } , "%d response bytes available" },

      { { 0xff, 0xff }, { 0x62, 0x81 } , "returned data may be corrupt" },
      { { 0xff, 0xff }, { 0x62, 0x82 } , "EOF reached prematurely" },
      { { 0xff, 0xff }, { 0x62, 0x83 } , "selected file invalid" },
      { { 0xff, 0xff }, { 0x62, 0x84 } , "FCI not formated" },
      { { 0xff, 0x00 }, { 0x62, 0x00 } , "nvmem unchanged" },

      { { 0xff, 0x00 }, { 0x63, 0x00 } , "nvmem changed" },
      { { 0xff, 0x00 }, { 0x63, 0x81 } , "file filled up by last write" },
      { { 0xff, 0xf0 }, { 0x63, 0xc0 } , "Counter=%1.1X" },

      { { 0xff, 0xff }, { 0x64, 0x00 } , "nvmem unchanged" },
      { { 0xff, 0x00 }, { 0x64, 0x00 } , "nvmem unchanged - RFU" },

      { { 0xff, 0xff }, { 0x65, 0x00 } , "nvmem changed" },
      { { 0xff, 0xff }, { 0x65, 0x81 } , "nvmem changed - memory failure" },
      { { 0xff, 0x00 }, { 0x65, 0x00 } , "nvmem changed - unknown?" },

      { { 0xff, 0x00 }, { 0x66, 0x00 } , "security related %d" },

      { { 0xff, 0xff }, { 0x67, 0x00 } , "wrong length" },
      { { 0xff, 0x00 }, { 0x67, 0x00 } , "wrong length - %d expected" },

      { { 0xff, 0xff }, { 0x68, 0x81 } , "wrong cla - logical channel not supported" },
      { { 0xff, 0xff }, { 0x68, 0x82 } , "wrong cla - secure messaging not supported" },
      { { 0xff, 0x00 }, { 0x68, 0x00 } , "cla not supported" },

      { { 0xff, 0xff }, { 0x69, 0x81 } , "command incompatible with file structure" },
      { { 0xff, 0xff }, { 0x69, 0x82 } , "security status not satisfied" },
      { { 0xff, 0xff }, { 0x69, 0x83 } , "authentication method blocked" },
      { { 0xff, 0xff }, { 0x69, 0x84 } , "referenced data invalid" },
      { { 0xff, 0xff }, { 0x69, 0x85 } , "conditions of use not satisfied" },
      { { 0xff, 0xff }, { 0x69, 0x86 } , "command not allowed - no current EF" },
      { { 0xff, 0xff }, { 0x69, 0x87 } , "expected SM data objects missing" },
      { { 0xff, 0xff }, { 0x69, 0x88 } , "SM data objects incorrect" },
      { { 0xff, 0x00 }, { 0x69, 0x00 } , "command not allowed" },

      { { 0xff, 0xff }, { 0x6a, 0x80 } , "P1-P2: incorrect parameters in data field" },
      { { 0xff, 0xff }, { 0x6a, 0x81 } , "P1-P2: function not supported" },
      { { 0xff, 0xff }, { 0x6a, 0x82 } , "P1-P2: file not found" },
      { { 0xff, 0xff }, { 0x6a, 0x83 } , "P1-P2: record not found" },
      { { 0xff, 0xff }, { 0x6a, 0x84 } , "P1-P2: not enough memory space in file" },
      { { 0xff, 0xff }, { 0x6a, 0x85 } , "P1-P2: Lc inconsistent with TLV" },
      { { 0xff, 0xff }, { 0x6a, 0x86 } , "P1-P2 incorrect" },
      { { 0xff, 0xff }, { 0x6a, 0x87 } , "P1-P2 inconsistent with Lc" },
      { { 0xff, 0xff }, { 0x6a, 0x88 } , "Referenced data not found" },
      { { 0xff, 0x00 }, { 0x6a, 0x00 } , "P1-P2 invalid" },

      { { 0xff, 0x00 }, { 0x6b, 0x00 } , "P1-P2 invalid" },

      { { 0xff, 0x00 }, { 0x6c, 0x00 } , "wrong length -  %d expected" },

      { { 0xff, 0x00 }, { 0x6d, 0x00 } , "INS code not supported or invalid" },
      
      { { 0xff, 0x00 }, { 0x6e, 0x00 } , "CLA %02X not supported" },

      { { 0xff, 0x00 }, { 0x6f, 0x00 } , "no precise diagnosis" },

      { { 0x00, 0x00 }, { 0x00, 0x00 } , "Unknown response" }
    };


#define DUMPWIDTH (16)
void hexdump(char *cp, int len)
{
  int i,j;

  for(i=0; i<len; i+=DUMPWIDTH)
  {
    printf(" %4X: ",i);
    for(j=0; j<DUMPWIDTH; j++)
      printf(i+j< len ? " %02X" : "   ",cp[i+j] & 0xff);
    printf("  ");
    for(j=0; j<DUMPWIDTH; j++)
      printf("%c",i+j<len ? isprint(cp[i+j]) ? cp[i+j] : '.' : ' ');

    printf("\n");
  }

}



int main(int argc, char *args[])
{

/****************** WORK **************/  

  while (1)
  {
    len=MAXBUFF;
    len2=MAXBUFF;
    ret=SCardStatus(card,buffer,&len,&state,&prot,buffer2,&len2);
    E(ret);

    printf("Reader: %s\nState: %ld=%lX\nProt: %ld=%lX\nATR: (%ld)\n",
        buffer,state,state,prot,prot,len2);
    hexdump(buffer2,len2);

//    DumpCmd("Emptyverify",emptyverify,sizeof(emptyverify));
    DumpCmd("Select Root",selectroot,sizeof(selectroot));
  //  DumpCmd("Read binary",readbin,sizeof(readbin));

  ret=SCardBeginTransaction(card);
  E(ret);
    while (Interactive())
    {
      fflush(stdout);
    }
  ret=SCardEndTransaction(card,SCARD_LEAVE_CARD);
  E(ret);

    sleep(1);
    break;
  }


  
  OnExit();

  printf("End of file\n");
  return 0;
}

/****************** subs **************/  


int Interactive(void)
{
  int c, l;

  if (feof(stdin)) return 0;

  printf("\nEnter APDU, '#' to quit: ");
  if (!fgets(buffer,MAXBUFF,stdin))
    return 1;

  if (buffer[0] == '#') return 0;

  if ((len2=strlen(buffer)) && isspace(buffer[len2]))
    buffer[len2]=0;

  cp=buffer;
  len2=0;
  while (*cp && sscanf(cp,"%02X %n",&c,&l)>0)
  {
    buffer2[len2]=c;
    len2++;
    cp+=l;
  }

  if (len2==0) return 1;

  DumpCmd("User",buffer2,len2);

  return 1;
}


int DumpCmd(char *desc, void *cmd, int clen)
{
  static char getresponse[5]= {0xa0,0xc0,0x00,0x00 };
  int i,j,p;

  printf("\nAPDU %s: prot %ld=%lX (%d)\n",
      desc,prot,prot,clen);
  hexdump(cmd,clen);

  len=MAXBUFF;
  ret=SCardTransmit(card,
     // prot ? SCARD_PCI_T1 : SCARD_PCI_T0,
      prot==SCARD_PROTOCOL_T1 ? SCARD_PCI_T1 : SCARD_PCI_T0,
      cmd,clen,&scir,
      (BYTE *)buffer,&len);
  E(ret);

  printf("Answer: (%ld)  ",len);

  if (len>=2)
  {
    p=len-2;
    for(j=0; j< sizeof(Cresponse)/sizeof(Cresponse[0]); j++)
    {
      for(i=sizeof(Cresponse[j].msk)-1; i>=0; i--)
        if (Cresponse[j].rsp[i] != (Cresponse[j].msk[i] & buffer[p+i]))
          break;

      if (i==-1)
      {
        printf(Cresponse[j].text,(int)(unsigned char)buffer[p+1]);
        break;
      }
    }
  }

  printf("\n");
  hexdump(buffer,len);
    
  if (len==2)
  {
    switch ((unsigned char)buffer[0])
    {
      case 0x61:
      case 0x9f:
        if (buffer[1]==0) 
        {
          printf("  -> Zero response bytes ???\n");
          break;
        }
        getresponse[4]=buffer[1];

        len=MAXBUFF;
        ret=SCardTransmit(card,
           // prot ? SCARD_PCI_T1 : SCARD_PCI_T0,
            prot==SCARD_PROTOCOL_T1 ? SCARD_PCI_T1 : SCARD_PCI_T0,
            getresponse,sizeof(getresponse),&scir,
            (BYTE *)buffer,&len);
        E(ret);

        printf("Answer: (%ld)\n",len);
        hexdump(buffer,len);
        break;
    }
  }

  return 0;
}

/****************** OPEN **************/  

void OnStart(void)
{
  ret=SCardEstablishContext(SCARD_SCOPE_GLOBAL,NULL,NULL,&scc); 
  E(ret);

  printf("Establish: %d=%X\n",(int)scc,(int)scc);

//  len=17;
  ret=SCardListReaders(scc,NULL,NULL,&len);
  E(ret);

  printf("%ld bytes req\n",len);
  if (len>MAXBUFF) exit(5);

  ret=SCardListReaders(scc,NULL,buffer,&len);
  E(ret);

  cp=buffer;
  while (*cp)
  {
    printf("Reader found: %s\n",cp);
    cp+=strlen(cp);
  }
 
  while (1)
  {
    ret=SCardConnect(scc,buffer,
        SCARD_SHARE_SHARED,
//        SCARD_SHARE_EXCLUSIVE,
        PROTOCOL,
      &card,&prot);

    if (ret == SCARD_S_SUCCESS) break;

    if (ret == SCARD_E_NO_SMARTCARD)
    {
      printf("No SmardCard inserted.\n");
      sleep(2);
      continue;
    }

    E(ret);
  }
 
  printf("Card: %ld=%lX  Prot: %d => %ld\n",card,card,PROTOCOL,prot);
}

/****************** CLOSE **************/  

void OnExit(void)
{
  LONG ret;

  if (card)
  {
//    ret=SCardDisconnect(card,SCARD_RESET_CARD);
    ret=SCardDisconnect(card,SCARD_UNPOWER_CARD);
    card=0;
    E(ret);
  }

  if (scc)
  {
    ret=SCardReleaseContext(scc); 
    scc=0;
    E(ret);
  }
}

Reply via email to