locked
How to get card name from SCARDHANDLE from SCardConnect RRS feed

  • Question

  • Hi.

    I'm looking for a way to get the card name in a reader from a SCARDHANDLE that I got with SCardConnect. If I use SCardUIDlgSelectCard I get back reader and card name. If I however use SCardListReaders and then SCardConnect, how do I get the card name that I need to pass to a minidrivers CardAcquireContext.

    Thanks for any help.

    Damjan

    Monday, November 1, 2010 7:47 PM

Answers

  • Hi,

    First, why do you need to call CardAcquireContext directly? You are not supposed to use minidrivers this way. Actually, minidrivers must be accessed through the "Microsoft Base Smart Card Crypto Provider" as a normal CSP. It is this provider who will find the right minidriver for the target card and then it will call the minidriver's exported functions.

    Can you explain what are you trying to implement?

    Concerning your question, you can get the card name by retrieving the ATR of the card using SCardGetAttrib and then calling SCardListCards with the ATR value in order to get the card name. Here is a sample code for doing this :

    BYTE pbAtr[36];
    DWORD dwLen = 36;
    LONG lRet = SCardGetAttrib(hCard, SCARD_ATTR_ATR_STRING, pbAtr, &dwLen);
    if (SCARD_S_SUCCESS == lRet)
    {
      LPTSTR szCardName = NULL;
      dwLen = SCARD_AUTOALLOCATE;
      lRet = SCardListCards(hContext, pbAtr, NULL, 0, (LPTSTR) &szCardName, &dwLen);
      if ((SCARD_S_SUCCESS == lRet) && (dwLen > 2))
      {
       _tprintf(_T("Card Name = %s\n"), szCardName);
      }
      else
       _tprintf(_T("Unknown card\n"));
    
      if (szCardName) SCardFreeMemory(hContext, szCardName);     
    }

     

    Cheers,
    --
    Mounir IDRASSI
    IDRIX
    http://www.idrix.fr


    Mounir IDRASSI IDRIX http://www.idrix.fr
    • Marked as answer by Damjan Cvetko Tuesday, November 2, 2010 6:13 PM
    Tuesday, November 2, 2010 3:50 PM

All replies

  • Hi,

    First, why do you need to call CardAcquireContext directly? You are not supposed to use minidrivers this way. Actually, minidrivers must be accessed through the "Microsoft Base Smart Card Crypto Provider" as a normal CSP. It is this provider who will find the right minidriver for the target card and then it will call the minidriver's exported functions.

    Can you explain what are you trying to implement?

    Concerning your question, you can get the card name by retrieving the ATR of the card using SCardGetAttrib and then calling SCardListCards with the ATR value in order to get the card name. Here is a sample code for doing this :

    BYTE pbAtr[36];
    DWORD dwLen = 36;
    LONG lRet = SCardGetAttrib(hCard, SCARD_ATTR_ATR_STRING, pbAtr, &dwLen);
    if (SCARD_S_SUCCESS == lRet)
    {
      LPTSTR szCardName = NULL;
      dwLen = SCARD_AUTOALLOCATE;
      lRet = SCardListCards(hContext, pbAtr, NULL, 0, (LPTSTR) &szCardName, &dwLen);
      if ((SCARD_S_SUCCESS == lRet) && (dwLen > 2))
      {
       _tprintf(_T("Card Name = %s\n"), szCardName);
      }
      else
       _tprintf(_T("Unknown card\n"));
    
      if (szCardName) SCardFreeMemory(hContext, szCardName);     
    }

     

    Cheers,
    --
    Mounir IDRASSI
    IDRIX
    http://www.idrix.fr


    Mounir IDRASSI IDRIX http://www.idrix.fr
    • Marked as answer by Damjan Cvetko Tuesday, November 2, 2010 6:13 PM
    Tuesday, November 2, 2010 3:50 PM
  • Hi.

    Thanks for your answer. The detail about getting the card name worked perfectly.

    Regarding why I'm doing this. In a nut shell, I want to call ChangeCardAuthenticatorEx for Administrator principal. I know that Base CSP should be the direct layer above Minidrivers, but I just can't seem to find an API to change the admin (3des) key.

     

    Best!

    Damjan

    Tuesday, November 2, 2010 6:13 PM
  • Hi,

    Indeed, Base CSP doesn't provide an API for changing smart card's authenticators. Usually, for this type of administrative tasks, smart card vendors ship proprietary tools in order to perform them. That's why Crypto API doesn't export functions for doing that. Moreover, many Minidrivers just have dummy implementations for the functions CardChangeAuthenticator and CardChangeAuthenticatorEx, so calling them directly does nothing. Of course, the minidriver you are using seems to have a more complete implementation.

     

    Cheers,
    --
    Mounir IDRASSI
    IDRIX
    http://www.idrix.fr


    Mounir IDRASSI IDRIX http://www.idrix.fr
    Tuesday, November 2, 2010 7:04 PM