[OpenSC-devel] sc_detect_card_presence() and usbtoken

Andreas Steffen andreas.steffen at strongsec.net
Tue Jul 8 18:27:38 UTC 2003


Hi Andreas,

below is my scx_establish_context() function which connects to
a smartcard in the FreeS/WAN code. As you can see I have
commented out the sc_detect_card_presence() call in order to
make it work with the etoken.

Regards

Andreas

Andreas Jellinghaus wrote:
> Hi Andreas,
> 
> Can you send me some test code, and what it does / should do?
> I will look at usbtoken and track down the issue.
> 
> I know there are some dark places in usbtoken code, you might
> have found one.
> 
> Regards, Andreas
>

static struct sc_context *ctx = NULL;
static struct sc_card *card = NULL;
static struct sc_pkcs15_card *p15card = NULL;

/*
  * Connect to the card in card reader card_reader, lock and bind it
  *
  * Status information and context is saved in the
  * global variables ctx, card and p15card
  */
bool
scx_establish_context(u_int card_reader)
{
     int r;

     /* establish a context */
     r = sc_establish_context(&ctx, "pluto");
     if (r)
     {
	log("failed to establish context: %s", sc_strerror(r));
	return FALSE;
     }

     /* test if reader card_reader is available */
     if (card_reader >= (unsigned int) ctx->reader_count)
     {
	log("illegal reader number - only %d reader(s) configured."
	    , ctx->reader_count);
	return FALSE;
     }

     /* test if card is inserted */
     /* r = sc_detect_card_presence(ctx->reader[card_reader], 0);
     if (r != 1)
     {
	log("card not present: %s", sc_strerror(r));
	return FALSE;
     }*/
     DBG(DBG_CONTROL | DBG_CRYPT,
	DBG_log("connecting to card in reader %s..."
	    , ctx->reader[card_reader]->name)
     )

     /* connect to the card */
     r = sc_connect_card(ctx->reader[card_reader], 0, &card);
     if (r)
     {
	log("failed to connect to card: %s", sc_strerror(r));
	return FALSE;
     }

     /* lock card, so it can't be used by another application */
     r = sc_lock(card);
     if (r)
     {
	log("unable to lock card: %s", sc_strerror(r));
	return FALSE;
     }

     /* establish context for pkcs15-functions */
     r = sc_pkcs15_bind(card, &p15card);
     if (r)
     {
	log("PKCS #15 initialization failed: %s", sc_strerror(r));
	return FALSE;
     }
     DBG(DBG_CONTROL | DBG_CRYPT,
	DBG_log("found %s!", p15card->label)
     )
     return TRUE;
}

=======================================================================
Andreas Steffen                   e-mail: andreas.steffen at strongsec.com
strongSec GmbH                    home:   http://www.strongsec.com
Alter Zürichweg 20                phone:  +41 1 730 80 64
CH-8952 Schlieren (Switzerland)   fax:    +41 1 730 80 65
==========================================[strong internet security]===




More information about the opensc-devel mailing list