Ejemplo n.º 1
0
void main()
{
  int rc,i;
  ULONG ulNumDevices;
  USBHANDLE hUSBDevice;
  UCHAR ucData[20];
  char brc;
  rc = UsbQueryNumberDevices(&ulNumDevices);
  printf("Num devices = %d (rc=%d)\r\n",ulNumDevices,rc);

  printf("Try to open Card Reader ... ");
  brc = CT_init(0,0);

  if(!brc)
  {
    UCHAR ucReset[] ={0x20,0x11,0x01,0x01,0x00};
    UCHAR ucResponse[64];
    USHORT usRetLen = sizeof(ucResponse);
    UCHAR sad = 2;
    UCHAR dad = 1;
    printf("SUCCESS\r\n try to reset card");
    brc = CT_data(0, &dad, &sad, 5, ucReset, &usRetLen, ucResponse);
    if(brc)
    {
      printf("FAILED!\r\n");
    }
    else
    {
      printf("ATR : ");
      for(USHORT i =0;i< usRetLen;i++)
        printf("%02X ",ucResponse[i]);
      printf("\r\n");
    }
    CT_close(0);
  }
  else
    printf("FAILED!\r\n");
}
Ejemplo n.º 2
0
int test1( int argc, char **argv){
  unsigned char dad, sad, rsp[512];
  unsigned short ctn=1, lr;
  unsigned char apdu[256];
  unsigned int alen;
  char ret;
  int i;
  int pn;
  int x;
  int checkSum=-1;
  time_t t0;

  if (argc<2)
    pn=1;
  else
    pn=atoi(argv[1]);

  /* Initialise CT-API library */
  ret=CT_init(ctn, pn);
  printf( "CT_init: %d\n", ret);
  if (ret!=CT_API_RV_OK )
    return 1;

  /* REQUEST ICC (timeout 20s) */
  dad=CT;
  sad=HOST;
  alen=0;
  apdu[alen++]=0x20;
  apdu[alen++]=0x12;
  apdu[alen++]=0x01; /* Unit */
  apdu[alen++]=0x01; /* request ATR */

  apdu[alen++]=0x00;
  lr=sizeof(rsp);
  ret=CT_data( ctn, &dad, &sad,
	      alen, apdu,
	      &lr, rsp );
  printf( "CT_data: %d\n", ret );
  if(ret!=CT_API_RV_OK)
    return(1);
  printf("    sad: %d, dad: %d, rsp:", sad, dad );
  for( i=0; i<lr; i++ ) printf( " %.2X", rsp[i] );
  printf( "\n" );

  if (rsp[lr-2]!=0x90) {
      return 1;
  }

  t0=time(NULL);
  for (x=0; ; x++) {
    uint8_t c;
    time_t t1;
    int dt;

    memset(rsp, 0, sizeof(rsp));

    /* read KVK */
    dad=CT;
    sad=HOST;
    alen=0;
    apdu[alen++]=0x00;
    apdu[alen++]=0xb0;
    apdu[alen++]=0x00;
    apdu[alen++]=0x00;
    apdu[alen++]=0x00;
    lr=sizeof(rsp);

    ret=CT_data( ctn, &dad, &sad,
		alen, apdu,
		&lr, rsp );
    if(ret!=CT_API_RV_OK) {
      printf( "CT_data: %d\n", ret );
      return(1);
    }
    c=0;
    for( i=0; i<lr; i++ ) {
      c^=rsp[i];
    }
    if (checkSum==-1) {
      checkSum=(int)c;
    }
    else {
      if (checkSum!=(int)c) {
	fprintf(stderr, "CheckSum-Error!\n");
	for (i=0; i<lr; i++ ) printf( " %.2X", rsp[i] );
	printf( "\n" );
        return 2;
      }
    }

    t1=time(NULL);
    dt=difftime(t1, t0);
    fprintf(stderr, "%02d:%02d: checksum=%02x (%d bytes)\n",
	    dt/60, dt%60, c, lr);
    if (dt>(30*60))
      break;
  }

  CT_close(ctn);
  fprintf(stderr, "Success.\n");
  return 0;
}
Ejemplo n.º 3
0
int main( int argc, char **argv){
#if 1
  return test1(argc, argv);
#else
  unsigned char dad, sad, rsp[258];
  unsigned short ctn=1, lr;
  unsigned char apdu[256];
  unsigned int alen;
  char ret;
  int i;
  int x;
  int pn;

  if (argc<2)
    pn=1;
  else
    pn=atoi(argv[1]);

  for (x=0; x<2; x++) {
    /* Initialise CT-API library */
    ret=CT_init(ctn, pn);
    printf( "CT_init: %d\n", ret);
    if (ret!=CT_API_RV_OK )
      return 1;

#if 1
    /* RESET CT */
    dad=CT;
    sad=HOST;
    alen=0;
    apdu[alen++]=0x20;
    apdu[alen++]=0x11;
    apdu[alen++]=0x00;
    apdu[alen++]=0x00;
    apdu[alen++]=0x00;
    lr=sizeof(rsp);
    ret=CT_data(ctn, &dad, &sad, alen, apdu,
		&lr, rsp);
    printf("CT_data: %d\n", ret);
    if( ret!=CT_API_RV_OK ) return( 1 );
    printf( "    sad: %d, dad: %d, rsp:", sad, dad );
    for( i=0; i<lr; i++ ) printf( " %.2X", rsp[i] );
    printf( "\n" );
#endif

    /* REQUEST ICC (timeout 20s) */
    dad=CT;
    sad=HOST;
    alen=0;
    apdu[alen++]=0x20;
    apdu[alen++]=0x12;
    apdu[alen++]=0x01; /* Unit */
    apdu[alen++]=0x01; /* request ATR */
#if 0
    apdu[alen++]=0x03;
    apdu[alen++]=0x80;
    apdu[alen++]=0x01;
    apdu[alen++]=0x14;
#endif
    apdu[alen++]=0x00;
    lr=sizeof(rsp);
    ret=CT_data( ctn, &dad, &sad,
		alen, apdu, 
		&lr, rsp );
    printf( "CT_data: %d\n", ret );
    if(ret!=CT_API_RV_OK)
      return(1);
    printf("    sad: %d, dad: %d, rsp:", sad, dad );
    for( i=0; i<lr; i++ ) printf( " %.2X", rsp[i] );
    printf( "\n" );
  
    if (rsp[lr-2]!=0x90) {
      return 1;
    }

    /* modify pin */
#if 0
    dad=CT;
    sad=HOST;
    alen=0;
    apdu[alen++]=0x20;
    apdu[alen++]=0x19;
    apdu[alen++]=0x01;
    apdu[alen++]=0x00;
    apdu[alen++]=0x1a;
    apdu[alen++]=0x52;
    apdu[alen++]=0x18;
    apdu[alen++]=0x01;
    apdu[alen++]=0x06;
    apdu[alen++]=0x0e;
    apdu[alen++]=0x00;
    apdu[alen++]=0x24;
    apdu[alen++]=0x00;
    apdu[alen++]=0x90;
    apdu[alen++]=0x10;
    apdu[alen++]=0x20;
    apdu[alen++]=0x20;
    apdu[alen++]=0x20;
    apdu[alen++]=0x20;
    apdu[alen++]=0x20;
    apdu[alen++]=0x20;
    apdu[alen++]=0x20;
    apdu[alen++]=0x20;
    apdu[alen++]=0x20;
    apdu[alen++]=0x20;
    apdu[alen++]=0x20;
    apdu[alen++]=0x20;
    apdu[alen++]=0x20;
    apdu[alen++]=0x20;
    apdu[alen++]=0x20;
    apdu[alen++]=0x20;
    lr=sizeof(rsp);
#else
  
    /* verify pin */
    dad=CT;
    sad=HOST;
    alen=0;
    apdu[alen++]=0x20;
    apdu[alen++]=0x18;
    apdu[alen++]=0x01;
    apdu[alen++]=0x00;
    apdu[alen++]=0x11;
    apdu[alen++]=0x52;
    apdu[alen++]=0x0f;
    apdu[alen++]=0x01;
    apdu[alen++]=0x06;
    apdu[alen++]=0x00;
    apdu[alen++]=0x20;
    apdu[alen++]=0x00;
    apdu[alen++]=0x90;
    apdu[alen++]=0x08;
    apdu[alen++]=0x20;
    apdu[alen++]=0x20;
    apdu[alen++]=0x20;
    apdu[alen++]=0x20;
    apdu[alen++]=0x20;
    apdu[alen++]=0x20;
    apdu[alen++]=0x20;
    apdu[alen++]=0x20;
    lr=sizeof(rsp);
#endif
  
    ret=CT_data( ctn, &dad, &sad,
		alen, apdu,
		&lr, rsp );
    printf( "CT_data: %d\n", ret );
    if(ret!=CT_API_RV_OK)
      return(1);
    printf("    sad: %d, dad: %d, rsp:", sad, dad );
    for( i=0; i<lr; i++ ) printf( " %.2X", rsp[i] );
    printf( "\n" );


    CT_close(ctn);

    fprintf(stderr, "Hit the ENTER key:\n");
    getchar();
  }

  return 0;
#endif
}
Ejemplo n.º 4
0
static int readerTest(int ctn) {
  int8_t res;
  uint8_t rsp[256+2];
  uint8_t sad, dad;
  uint16_t lenc, lenr;

  uint8_t apdu_reset_icc[]={0x20, 0x11, 0x01, 0x01, 0x00};

#ifdef VERBOUS
  printCurrentTime(stdout);
  fprintf(stdout, "Thread %d: Started\n", ctn);
#endif
  res=CT_init(ctn, ctn);
  if (res!=CT_API_RV_OK) {
    fprintf(stderr, "===== CTAPI(%d): Error on CT_init (%d)\n", ctn, res);
    return -1;
  }

  lenc=sizeof(apdu_reset_icc);
  lenr=sizeof(rsp);
  sad=2;
  dad=CT_API_AD_CT;
  res=CT_data(ctn, &dad, &sad, lenc, apdu_reset_icc, &lenr, rsp);
  if (res!=CT_API_RV_OK) {
    fprintf(stderr, "===== CTAPI::RESET_ICC(%d): Error on CT_data (%d)\n", ctn, res);
    CT_close(ctn);
    return -1;
  }
  if (lenr<2) {
    fprintf(stderr, "===== CTAPI::RESET_ICC(%d): Too few bytes returned (%d)\n", ctn, lenr);
    CT_close(ctn);
    return -1;
  }

  if (rsp[lenr-2]==0x90) {
    int loop;

#ifdef VERBOUS
    printCurrentTime(stdout);
    fprintf(stdout, "Thread %d: Got a card\n", ctn);
#endif

    for (loop=0; loop<NUM_LOOPS; loop++) {
      long int r;
      uint8_t apdu_select_mf[]={0x00, 0xa4, 0x00, 0x00, 0x02, 0x3f, 0x00, 0x00};

      lenc=sizeof(apdu_select_mf);
      lenr=sizeof(rsp);
      sad=2;
      dad=CT_API_AD_CT;
#ifdef VERBOUS
      printCurrentTime(stdout);
      fprintf(stdout, "Thread %d: -> Selecting MF\n", ctn);
#endif
      res=CT_data(ctn, &dad, &sad, lenc, apdu_select_mf, &lenr, rsp);
#ifdef VERBOUS
      printCurrentTime(stdout);
      fprintf(stdout, "Thread %d: <- Returned from selecting MF\n", ctn);
#endif
      if (res!=CT_API_RV_OK) {
	fprintf(stderr, "===== CTAPI::SELECT_MF(%d): Error on CT_data (%d)\n", ctn, res);
	CT_close(ctn);
	return -1;
      }
      if (lenr<2) {
	fprintf(stderr, "===== CTAPI::SELECT_MF(%d): Too few bytes returned (%d)\n", ctn, lenr);
	CT_close(ctn);
	return -1;
      }

      if (rsp[lenr-2]==0x90) {
#ifdef VERBOUS
	printCurrentTime(stdout);
	fprintf(stdout, "Thread %d: MF selected.\n", ctn);
#endif
      }
      else {
	fprintf(stderr, "===== CTAPI::SELECT_MF(%d): Command error (%02x %02x)\n", ctn,
		rsp[lenr-2], rsp[lenr-1]);
	CT_close(ctn);
	return -1;
      }

      r=(random()% 200);
#ifdef VERBOUS
      usleep(r);
#endif
    }
  }
  else {
    fprintf(stderr, "===== CTAPI::RESET_ICC(%d): No card (%02x %02x)\n", ctn,
	    rsp[lenr-2], rsp[lenr-1]);
    CT_close(ctn);
    return -1;
  }

  sleep(2);

  res=CT_close(ctn);
  if (res!=CT_API_RV_OK) {
    fprintf(stderr, "===== CTAPI(ctn): Error on CT_close (%d)\n", res);
    return -1;
  }

#ifdef VERBOUS
  fprintf(stdout, "Thread %d: Stopped\n", ctn);
#endif
  return 0;
}