/[openpgpmdrv]/trunk/OpenPGPminidriver/ContextManagement.c
ViewVC logotype

Diff of /trunk/OpenPGPminidriver/ContextManagement.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 7 by vletoux, Thu Mar 4 21:17:51 2010 UTC revision 8 by vletoux, Thu Mar 11 20:32:26 2010 UTC
# Line 73  BOOL find_compacttlv(__in PBYTE pbData, Line 73  BOOL find_compacttlv(__in PBYTE pbData,
73  }  }
74    
75    
76  DWORD CreateContext(__in PCARD_DATA pCardData)  
77    DWORD CheckContextEx(__in PCARD_DATA pCardData, __in BOOL fOpenPGPContextShouldBeNotNull)
78    {
79            DWORD dwReturn;
80            DWORD dwI, dwJ;
81            BOOL fFound = FALSE;
82            BOOL fRightATRLenFound = FALSE;
83            __try
84            {
85                    if ( pCardData == NULL )
86                    {
87                            Trace(WINEVENT_LEVEL_ERROR, L"pCardData == NULL");
88                            dwReturn  = SCARD_E_INVALID_PARAMETER;
89                            __leave;
90                    }
91                    if (pCardData->pbAtr == NULL)
92                    {
93                            Trace(WINEVENT_LEVEL_ERROR, L"pCardData->pbAtr == NULL");
94                            dwReturn  = SCARD_E_INVALID_PARAMETER;
95                            __leave;
96                    }
97    
98                    if (pCardData->dwVersion < CARD_DATA_VERSION_SIX)
99                    {
100                            Trace(WINEVENT_LEVEL_ERROR, L"pCardData->dwVersion(%d) < CARD_DATA_VERSION_SIX", pCardData->dwVersion);
101                            dwReturn  = ERROR_REVISION_MISMATCH;
102                            __leave;
103                    }
104                    pCardData->dwVersion = min(pCardData->dwVersion, CARD_DATA_VERSION_SIX);
105                    for (dwI = 0; dwI < dwSupportedATRCount; dwI++)
106                    {
107                            if (SupportedATR[dwI].cbAtr == pCardData->cbAtr)
108                            {
109                                    BYTE pbAtr[36];
110                                    fRightATRLenFound = TRUE;
111                                    memcpy(pbAtr, pCardData->pbAtr, SupportedATR[dwI].cbAtr);
112                                    for( dwJ = 0; dwJ < SupportedATR[dwI].cbAtr; dwJ++)
113                                    {
114                                            pbAtr[dwJ] &= SupportedATR[dwI].rgbMask[dwJ];
115                                    }
116                                    if (memcmp(pbAtr, SupportedATR[dwI].rgbAtr, SupportedATR[dwI].cbAtr) == 0)
117                                    {
118                                            fFound = TRUE;
119                                            //Trace(WINEVENT_LEVEL_VERBOSE, L"card match ATR %d", dwI);
120                                            break;
121                                    }
122    
123                            }
124                    }
125                    if (!fRightATRLenFound)
126                    {
127                            Trace(WINEVENT_LEVEL_ERROR, L"card doesn't match ATR len %d",pCardData->cbAtr);
128                            dwReturn  = SCARD_E_INVALID_PARAMETER;
129                            __leave;
130                    }
131                    if (!fFound)
132                    {
133                            Trace(WINEVENT_LEVEL_ERROR, L"card doesn't match ATR");
134                            dwReturn  = SCARD_E_UNKNOWN_CARD;
135                            __leave;
136                    }
137    
138                    if ( pCardData->pwszCardName == NULL )
139                    {
140                            Trace(WINEVENT_LEVEL_ERROR, L"pCardData->pwszCardName");
141                            dwReturn  = SCARD_E_INVALID_PARAMETER;
142                            __leave;
143                    }
144                    /* Memory management functions */
145                    if ( ( pCardData->pfnCspAlloc   == NULL ) ||
146                            ( pCardData->pfnCspReAlloc == NULL ) ||
147                            ( pCardData->pfnCspFree    == NULL ) )
148                    {
149                            Trace(WINEVENT_LEVEL_ERROR, L"Memory functions null");
150                            dwReturn  = SCARD_E_INVALID_PARAMETER;
151                            __leave;
152                    }
153                    if (fOpenPGPContextShouldBeNotNull)
154                    {
155                            if (!pCardData->pvVendorSpecific)
156                            {
157                                    // not found =>
158                                    Trace(WINEVENT_LEVEL_ERROR, L"pCardData->pvVendorSpecific == NULL");
159                                    dwReturn = SCARD_E_UNEXPECTED;
160                                    __leave;
161                            }
162                            if (pCardData->hSCardCtx == 0)
163                            {
164                                    Trace(WINEVENT_LEVEL_ERROR, L"pCardData->hSCardCtx == NULL");
165                                    dwReturn  = SCARD_E_INVALID_PARAMETER;
166                                    __leave;
167                            }
168                            if (pCardData->hScard == 0)
169                            {
170                                    Trace(WINEVENT_LEVEL_ERROR, L"pCardData->hScard == NULL");
171                                    dwReturn  = SCARD_E_INVALID_PARAMETER;
172                                    __leave;
173                            }
174                    }
175                    else
176                    {
177                            if (pCardData->pvVendorSpecific)
178                            {
179                                    Trace(WINEVENT_LEVEL_ERROR, L"pContext != NULL");
180                                    dwReturn = SCARD_E_UNEXPECTED;
181                                    __leave;
182                            }
183                    }
184                    dwReturn = 0;
185            }
186            __finally
187            {
188            }
189            return dwReturn;
190    }
191    
192    DWORD CheckContext(__in PCARD_DATA pCardData)
193    {
194            return CheckContextEx(pCardData, TRUE);
195    }
196    
197    DWORD CleanContext(__in PCARD_DATA pCardData)
198    {
199            DWORD dwReturn = 0;
200            __try
201            {
202                    if (pCardData)
203                    {
204                            if ( pCardData->pvVendorSpecific)
205                            {
206                                    pCardData->pfnCspFree( pCardData->pvVendorSpecific);
207                                    pCardData->pvVendorSpecific = NULL;
208                            }
209                            else
210                            {
211                                    Trace(WINEVENT_LEVEL_ERROR, L"pCardData->pvVendorSpecific == NULL");
212                            }
213                    }
214                    else
215                    {
216                            Trace(WINEVENT_LEVEL_ERROR, L"pCardData == NULL");
217                            dwReturn = SCARD_E_INVALID_PARAMETER;
218                            __leave;
219                    }
220            }
221            __finally
222            {
223            }
224            return dwReturn;
225    }
226    
227    
228    DWORD CreateContext(__in PCARD_DATA pCardData, __in DWORD dwFlags)
229  {  {
230          DWORD dwReturn;          DWORD dwReturn;
231          PBYTE                                   pbCapabilities = NULL, pbCardCapabilities;          PBYTE                                   pbCapabilities = NULL, pbCardCapabilities;
232          PBYTE                                   pbExtendedCapabilities = NULL;          PBYTE                                   pbExtendedCapabilities = NULL;
233          PBYTE                                   pbApplicationIdentifier = NULL;          PBYTE                                   pbApplicationIdentifier = NULL;
234            PBYTE                                   pbFingerPrint = NULL;
235          DWORD                                   dwCapabilitiesSize,          DWORD                                   dwCapabilitiesSize,
236                                                          dwCardCapabilitiesSize,                                                          dwCardCapabilitiesSize,
237                                                          dwApplicationIdentifierSize,                                                          dwApplicationIdentifierSize,
238                                                          dwExtendedCapabilitiesSize;                                                          dwExtendedCapabilitiesSize,
239                                                            dwFingerPrintSize;
240            DWORD dwI;
241          BYTE bCategoryIndicator, bStatusIndicator;          BYTE bCategoryIndicator, bStatusIndicator;
242            POPENPGP_CONTEXT pContext;
243          __try          __try
244          {          {
245                  POPENPGP_CONTEXT pContext = (POPENPGP_CONTEXT) pCardData->pvVendorSpecific;                  dwReturn = CheckContextEx(pCardData, FALSE);
246                  if (pContext)                  if (dwReturn)
247                  {                  {
248                          Trace(WINEVENT_LEVEL_ERROR, L"pContext != NULL");                          Trace(WINEVENT_LEVEL_ERROR, L"CheckContext");
                         dwReturn = SCARD_E_UNEXPECTED;  
249                          __leave;                          __leave;
250                  }                  }
251                    if (!(dwFlags & CARD_SECURE_KEY_INJECTION_NO_CARD_MODE))
252                    {
253                            if (pCardData->hSCardCtx == 0)
254                            {
255                                    Trace(WINEVENT_LEVEL_ERROR, L"pCardData->hSCardCtx == NULL");
256                                    dwReturn  = SCARD_E_INVALID_HANDLE;
257                                    __leave;
258                            }
259                            if (pCardData->hScard == 0)
260                            {
261                                    Trace(WINEVENT_LEVEL_ERROR, L"pCardData->hScard == NULL");
262                                    dwReturn  = SCARD_E_INVALID_HANDLE;
263                                    __leave;
264                            }
265                    }
266                    
267                  // not found => initialize context                  // not found => initialize context
268                  pContext = pCardData->pfnCspAlloc(sizeof(OPENPGP_CONTEXT));                  pContext = pCardData->pfnCspAlloc(sizeof(OPENPGP_CONTEXT));
269                  if (!pContext)                  if (!pContext)
# Line 110  DWORD CreateContext(__in PCARD_DATA pCar Line 281  DWORD CreateContext(__in PCARD_DATA pCar
281                          Trace(WINEVENT_LEVEL_ERROR, L"No SelectOpenPGPApplication");                          Trace(WINEVENT_LEVEL_ERROR, L"No SelectOpenPGPApplication");
282                          __leave;                          __leave;
283                  }                  }
284                  dwReturn = SCardReadFile(pCardData, szOpenPGPDir, szOpenPGPApplicationIdentifier, &pbApplicationIdentifier, &dwApplicationIdentifierSize);                  dwReturn = OCardReadFile(pCardData, szOpenPGPDir, szOpenPGPApplicationIdentifier, &pbApplicationIdentifier, &dwApplicationIdentifierSize);
285                  if (dwReturn)                  if (dwReturn)
286                  {                  {
287                          __leave;                          __leave;
# Line 122  DWORD CreateContext(__in PCARD_DATA pCar Line 293  DWORD CreateContext(__in PCARD_DATA pCar
293                          __leave;                          __leave;
294                  }                  }
295                  memcpy(&(pContext->Aid),pbApplicationIdentifier,sizeof(OPENPGP_AID));                  memcpy(&(pContext->Aid),pbApplicationIdentifier,sizeof(OPENPGP_AID));
296                  dwReturn = SCardReadFile(pCardData, szOpenPGPDir, szOpenPGPHistoricalBytes, &pbCapabilities, &dwCapabilitiesSize);                  dwReturn = OCardReadFile(pCardData, szOpenPGPDir, szOpenPGPHistoricalBytes, &pbCapabilities, &dwCapabilitiesSize);
297                  if (dwReturn)                  if (dwReturn)
298                  {                  {
299                          __leave;                          __leave;
# Line 153  DWORD CreateContext(__in PCARD_DATA pCar Line 324  DWORD CreateContext(__in PCARD_DATA pCar
324                          dwReturn = SCARD_E_UNEXPECTED;                          dwReturn = SCARD_E_UNEXPECTED;
325                          __leave;                          __leave;
326                  }                  }
327                  dwReturn = SCardReadFile(pCardData, szOpenPGPDir, szOpenPGPExtendedCap, &pbExtendedCapabilities, &dwExtendedCapabilitiesSize);                  dwReturn = OCardReadFile(pCardData, szOpenPGPDir, szOpenPGPExtendedCap, &pbExtendedCapabilities, &dwExtendedCapabilitiesSize);
328                  if (dwReturn)                  if (dwReturn)
329                  {                  {
330                          __leave;                          __leave;
# Line 165  DWORD CreateContext(__in PCARD_DATA pCar Line 336  DWORD CreateContext(__in PCARD_DATA pCar
336                  pContext->dwMaxCertificateLength = pbExtendedCapabilities[4] * 0x100 + pbExtendedCapabilities[5];                  pContext->dwMaxCertificateLength = pbExtendedCapabilities[4] * 0x100 + pbExtendedCapabilities[5];
337                  pContext->dwMaxCommandDataLength = pbExtendedCapabilities[6] * 0x100 + pbExtendedCapabilities[7];                  pContext->dwMaxCommandDataLength = pbExtendedCapabilities[6] * 0x100 + pbExtendedCapabilities[7];
338                  pContext->dwMaxResponseLength = pbExtendedCapabilities[8] * 0x100 + pbExtendedCapabilities[9];                  pContext->dwMaxResponseLength = pbExtendedCapabilities[8] * 0x100 + pbExtendedCapabilities[9];
339                  dwReturn = CCIDgetFeatures(pCardData);                  pContext->fIsReadOnly = TRUE;
340                    dwReturn = OCardReadFile(pCardData, szOpenPGPDir, szOpenPGPFingerprint, &pbFingerPrint, &dwFingerPrintSize);
341                  if (dwReturn)                  if (dwReturn)
342                  {                  {
343                          __leave;                          __leave;
344                  }                  }
345                  dwReturn = 0;                  if (dwFingerPrintSize != 60)
         }  
         __finally  
         {  
                 if (pbApplicationIdentifier)  
                         pCardData->pfnCspFree(pbApplicationIdentifier);  
                 if (pbCapabilities)  
                         pCardData->pfnCspFree(pbCapabilities);  
                 if (pbExtendedCapabilities)  
                         pCardData->pfnCspFree(pbExtendedCapabilities);  
         }  
         return dwReturn;  
 }  
   
   
 DWORD CheckContext(__in PCARD_DATA pCardData)  
 {  
         DWORD dwReturn;  
         DWORD dwI, dwJ;  
         BOOL fFound = FALSE;  
         BOOL fRightATRLenFound = FALSE;  
         __try  
         {  
                 if ( pCardData == NULL )  
                 {  
                         Trace(WINEVENT_LEVEL_ERROR, L"pCardData == NULL");  
                         dwReturn  = SCARD_E_INVALID_PARAMETER;  
                         __leave;  
                 }  
                 if (pCardData->pbAtr == NULL)  
346                  {                  {
347                          Trace(WINEVENT_LEVEL_ERROR, L"pCardData->pbAtr == NULL");                          Trace(WINEVENT_LEVEL_ERROR, L"dwFingerPrintSize = %02X", dwFingerPrintSize);
348                          dwReturn  = SCARD_E_INVALID_PARAMETER;                          dwReturn = SCARD_E_UNEXPECTED;
                         __leave;  
                 }  
   
                 if (pCardData->dwVersion < CARD_DATA_VERSION_SIX)  
                 {  
                         Trace(WINEVENT_LEVEL_ERROR, L"pCardData->dwVersion(%d) < CARD_DATA_VERSION_SIX", pCardData->dwVersion);  
                         dwReturn  = ERROR_REVISION_MISMATCH;  
349                          __leave;                          __leave;
350                  }                  }
351                  pCardData->dwVersion = min(pCardData->dwVersion, CARD_DATA_VERSION_SIX);                  pContext->fHasSignature = FALSE;
352                  for (dwI = 0; dwI < dwSupportedATRCount; dwI++)                  for( dwI = 0; dwI < 20; dwI++)
353                  {                  {
354                          if (SupportedATR[dwI].cbAtr == pCardData->cbAtr)                          if (pbFingerPrint[dwI] != 0)
355                          {                          {
356                                  BYTE pbAtr[36];                                  pContext->fHasSignature = TRUE;
357                                  fRightATRLenFound = TRUE;                                  break;
                                 memcpy(pbAtr, pCardData->pbAtr, SupportedATR[dwI].cbAtr);  
                                 for( dwJ = 0; dwJ < SupportedATR[dwI].cbAtr; dwJ++)  
                                 {  
                                         pbAtr[dwJ] &= SupportedATR[dwI].rgbMask[dwJ];  
                                 }  
                                 if (memcmp(pbAtr, pCardData->pbAtr, SupportedATR[dwI].cbAtr) == 0)  
                                 {  
                                         fFound = TRUE;  
                                         //Trace(WINEVENT_LEVEL_VERBOSE, L"card match ATR %d", dwI);  
                                         break;  
                                 }  
   
358                          }                          }
359                  }                  }
360                  if (!fRightATRLenFound)                  pContext->fHasDecryption = FALSE;
361                  {                  for( dwI = 20; dwI < 40; dwI++)
                         Trace(WINEVENT_LEVEL_ERROR, L"card doesn't match ATR len %d",pCardData->cbAtr);  
                         dwReturn  = SCARD_E_INVALID_PARAMETER;  
                         __leave;  
                 }  
                 if (!fFound)  
362                  {                  {
363                          Trace(WINEVENT_LEVEL_ERROR, L"card doesn't match ATR");                          if (pbFingerPrint[dwI] != 0)
364                          dwReturn  = SCARD_E_UNKNOWN_CARD;                          {
365                          __leave;                                  pContext->fHasDecryption = TRUE;
366                  }                                  break;
367                            }
                 if ( pCardData->pwszCardName == NULL )  
                 {  
                         Trace(WINEVENT_LEVEL_ERROR, L"pCardData->pwszCardName");  
                         dwReturn  = SCARD_E_INVALID_PARAMETER;  
                         __leave;  
368                  }                  }
369                  /* Memory management functions */                  pContext->fHasAuthentication = FALSE;
370                  if ( ( pCardData->pfnCspAlloc   == NULL ) ||                  for( dwI = 40; dwI < 60; dwI++)
                         ( pCardData->pfnCspReAlloc == NULL ) ||  
                         ( pCardData->pfnCspFree    == NULL ) )  
371                  {                  {
372                          Trace(WINEVENT_LEVEL_ERROR, L"Memory functions null");                          if (pbFingerPrint[dwI] != 0)
373                          dwReturn  = SCARD_E_INVALID_PARAMETER;                          {
374                          __leave;                                  pContext->fHasAuthentication = TRUE;
375                                    break;
376                            }
377                  }                  }
378                  if (!pCardData->pvVendorSpecific)                  dwReturn = CCIDgetFeatures(pCardData);
379                    if (dwReturn)
380                  {                  {
                         // not found =>  
                         dwReturn = SCARD_E_UNEXPECTED;  
381                          __leave;                          __leave;
382                  }                  }
383                  dwReturn = 0;                  dwReturn = 0;
384          }          }
385          __finally          __finally
386          {          {
387                    if (pbFingerPrint)
388                            pCardData->pfnCspFree(pbFingerPrint);
389                    if (pbApplicationIdentifier)
390                            pCardData->pfnCspFree(pbApplicationIdentifier);
391                    if (pbCapabilities)
392                            pCardData->pfnCspFree(pbCapabilities);
393                    if (pbExtendedCapabilities)
394                            pCardData->pfnCspFree(pbExtendedCapabilities);
395          }          }
396          return dwReturn;          return dwReturn;
397  }  }
   
 DWORD CleanContext(__in PCARD_DATA pCardData)  
 {  
         DWORD dwReturn = 0;  
         __try  
         {  
                 if (pCardData)  
                 {  
                         if ( pCardData->pvVendorSpecific)  
                         {  
                                 pCardData->pfnCspFree( pCardData->pvVendorSpecific);  
                                 pCardData->pvVendorSpecific = NULL;  
                         }  
                 }  
         }  
         __finally  
         {  
         }  
         return dwReturn;  
 }  

Legend:
Removed from v.7  
changed lines
  Added in v.8

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26