prothandler.c

Go to the documentation of this file.
00001 /*
00002  * MUSCLE SmartCard Development ( http://www.linuxnet.com )
00003  *
00004  * Copyright (C) 1999
00005  *  David Corcoran <corcoran@linuxnet.com>
00006  * Copyright (C) 2004
00007  *  Ludovic Rousseau <ludovic.rousseau@free.fr>
00008  *
00009  * $Id: prothandler.c 2896 2008-04-22 09:20:00Z rousseau $
00010  */
00011 
00017 #include "config.h"
00018 #include <string.h>
00019 
00020 #include "misc.h"
00021 #include "pcscd.h"
00022 #include "ifdhandler.h"
00023 #include "debuglog.h"
00024 #include "readerfactory.h"
00025 #include "prothandler.h"
00026 #include "atrhandler.h"
00027 #include "ifdwrapper.h"
00028 #include "eventhandler.h"
00029 
00035 UCHAR PHGetDefaultProtocol(PUCHAR pucAtr, DWORD dwLength)
00036 {
00037     SMARTCARD_EXTENSION sSmartCard;
00038 
00039     /*
00040      * Zero out everything
00041      */
00042     memset(&sSmartCard, 0x00, sizeof(SMARTCARD_EXTENSION));
00043 
00044     if (ATRDecodeAtr(&sSmartCard, pucAtr, dwLength))
00045         return sSmartCard.CardCapabilities.CurrentProtocol;
00046     else
00047         return 0x00;
00048 }
00049 
00055 UCHAR PHGetAvailableProtocols(PUCHAR pucAtr, DWORD dwLength)
00056 {
00057     SMARTCARD_EXTENSION sSmartCard;
00058 
00059     /*
00060      * Zero out everything
00061      */
00062     memset(&sSmartCard, 0x00, sizeof(SMARTCARD_EXTENSION));
00063 
00064     if (ATRDecodeAtr(&sSmartCard, pucAtr, dwLength))
00065         return sSmartCard.CardCapabilities.AvailableProtocols;
00066     else
00067         return 0x00;
00068 }
00069 
00080 DWORD PHSetProtocol(struct ReaderContext * rContext,
00081     DWORD dwPreferred, UCHAR ucAvailable, UCHAR ucDefault)
00082 {
00083     DWORD protocol;
00084     LONG rv;
00085     UCHAR ucChosen;
00086 
00087     /* App has specified no protocol */
00088     if (dwPreferred == 0)
00089         return SET_PROTOCOL_WRONG_ARGUMENT;
00090 
00091     /* requested protocol is not available */
00092     if (! (dwPreferred & ucAvailable))
00093     {
00094         /* Note:
00095          * dwPreferred must be either SCARD_PROTOCOL_T0 or SCARD_PROTOCOL_T1
00096          * if dwPreferred == SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1 the test
00097          * (SCARD_PROTOCOL_T0 == dwPreferred) will not work as expected
00098          * and the debug message will not be correct.
00099          *
00100          * This case may only occur if
00101          * dwPreferred == SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1
00102          * and ucAvailable == 0 since we have (dwPreferred & ucAvailable) == 0
00103          * and the case ucAvailable == 0 should never occur (the card is at
00104          * least T=0 or T=1)
00105          */
00106         Log2(PCSC_LOG_ERROR, "Protocol T=%d requested but unsupported by the card",
00107             (SCARD_PROTOCOL_T0 == dwPreferred) ? 0 : 1);
00108         return SET_PROTOCOL_WRONG_ARGUMENT;
00109     }
00110 
00111     /* set default value */
00112     protocol = ucDefault;
00113 
00114     /* keep only the available protocols */
00115     dwPreferred &= ucAvailable;
00116 
00117     /* we try to use T=1 first */
00118     if (dwPreferred & SCARD_PROTOCOL_T1)
00119         ucChosen = SCARD_PROTOCOL_T1;
00120     else
00121         if (dwPreferred & SCARD_PROTOCOL_T0)
00122             ucChosen = SCARD_PROTOCOL_T0;
00123         else
00124             /* App wants unsupported protocol */
00125             return SET_PROTOCOL_WRONG_ARGUMENT;
00126 
00127     Log2(PCSC_LOG_INFO, "Attempting PTS to T=%d",
00128         (SCARD_PROTOCOL_T0 == ucChosen ? 0 : 1));
00129     rv = IFDSetPTS(rContext, ucChosen, 0x00, 0x00, 0x00, 0x00);
00130 
00131     if (IFD_SUCCESS == rv)
00132         protocol = ucChosen;
00133     else
00134         if (IFD_NOT_SUPPORTED == rv)
00135             Log2(PCSC_LOG_INFO, "PTS not supported by driver, using T=%d",
00136                 (SCARD_PROTOCOL_T0 == protocol) ? 0 : 1);
00137         else
00138             if (IFD_PROTOCOL_NOT_SUPPORTED == rv)
00139                 Log2(PCSC_LOG_INFO, "PTS protocol not supported, using T=%d",
00140                     (SCARD_PROTOCOL_T0 == protocol) ? 0 : 1);
00141             else
00142             {
00143                 Log3(PCSC_LOG_INFO, "PTS failed (%d), using T=%d", rv,
00144                     (SCARD_PROTOCOL_T0 == protocol) ? 0 : 1);
00145 
00146                 /* ISO 7816-3:1997 ch. 7.2 PPS protocol page 14
00147                  * - If the PPS exchange is unsuccessful, then the interface device
00148                  *   shall either reset or reject the card.
00149                  */
00150                 return SET_PROTOCOL_PPS_FAILED;
00151             }
00152 
00153     return protocol;
00154 }
00155 

Generated on Mon Aug 17 01:00:10 2009 for pcsc-lite by  doxygen 1.5.9