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

Diff of /trunk/OpenPGPminidriver/CryptoOperations.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 16  Line 16 
16  */  */
17    
18  #include <windows.h>  #include <windows.h>
19    #include <stdio.h>
20  #include <cardmod.h>  #include <cardmod.h>
21  #include "Tracing.h"  #include "Tracing.h"
22  #include "Context.h"  #include "Context.h"
# Line 68  OPENPGP_SUPPORTED_SIGNATURE_ALGORITHM Si Line 69  OPENPGP_SUPPORTED_SIGNATURE_ALGORITHM Si
69    
70  DWORD dwSignatureAlgorithmCount = ARRAYSIZE(SignatureAlgorithm);  DWORD dwSignatureAlgorithmCount = ARRAYSIZE(SignatureAlgorithm);
71    
72  #pragma pack(push,1)  
 typedef struct _OPENPGP_ALGORITHM_ATTRIBUTE  
 {  
         BYTE bAlgoId;  
         unsigned short wModulusLength;  
         unsigned short wExponentLength;  
         BYTE bFormat;  
 } OPENPGP_ALGORITHM_ATTRIBUTE, *POPENPGP_ALGORITHM_ATTRIBUTE;  
 #pragma pack(pop)  
73    
74  typedef struct _RSAPUBLICKEYBLOB  typedef struct _RSAPUBLICKEYBLOB
75  {  {
# Line 86  typedef struct _RSAPUBLICKEYBLOB Line 79  typedef struct _RSAPUBLICKEYBLOB
79  } RSAPUBLICKEYBLOB, *PRSAPUBLICKEYBLOB;  } RSAPUBLICKEYBLOB, *PRSAPUBLICKEYBLOB;
80    
81    
82  DWORD GetKeyAlgorithmAttributes(__in PCARD_DATA pCardData,  DWORD OCardGetKeyAlgorithmAttributes(__in PCARD_DATA pCardData,
83                                                                  __in OPENPGP_CONTAINER dwContainer,                                                                  __in OPENPGP_CONTAINER dwContainer,
84                                                                  __out POPENPGP_ALGORITHM_ATTRIBUTE pAttributes)                                                                  __out POPENPGP_ALGORITHM_ATTRIBUTE pAttributes)
85  {  {
# Line 114  DWORD GetKeyAlgorithmAttributes(__in PCA Line 107  DWORD GetKeyAlgorithmAttributes(__in PCA
107                          Trace(WINEVENT_LEVEL_ERROR, L"SCARD_E_NO_KEY_CONTAINER %d", dwContainer);                          Trace(WINEVENT_LEVEL_ERROR, L"SCARD_E_NO_KEY_CONTAINER %d", dwContainer);
108                          __leave;                          __leave;
109                  }                  }
110                  dwReturn = SCardReadFile(pCardData, szOpenPGPDir, szAlgorithmAttributes, &pbData, &dwResponseSize);                  dwReturn = OCardReadFile(pCardData, szOpenPGPDir, szAlgorithmAttributes, &pbData, &dwResponseSize);
111                  if (dwReturn)                  if (dwReturn)
112                  {                  {
113                          __leave;                          __leave;
# Line 127  DWORD GetKeyAlgorithmAttributes(__in PCA Line 120  DWORD GetKeyAlgorithmAttributes(__in PCA
120                  }                  }
121                  memcpy(pAttributes, pbData, dwResponseSize);                  memcpy(pAttributes, pbData, dwResponseSize);
122                  // big endian, little endian ...                  // big endian, little endian ...
123                  wTemp = pAttributes->wExponentLength;                  wTemp = pAttributes->wExponentLengthInBit;
124                  pAttributes->wExponentLength = (wTemp % 0x100) * 0x100 + (wTemp / 0x100);                  pAttributes->wExponentLengthInBit = (wTemp % 0x100) * 0x100 + (wTemp / 0x100);
125                  wTemp = pAttributes->wModulusLength;                  wTemp = pAttributes->wModulusLengthInBit;
126                  pAttributes->wModulusLength = (wTemp % 0x100) * 0x100 + (wTemp / 0x100);                  pAttributes->wModulusLengthInBit = (wTemp % 0x100) * 0x100 + (wTemp / 0x100);
127                                    
128                  dwReturn = 0;                  dwReturn = 0;
129          }          }
# Line 143  DWORD GetKeyAlgorithmAttributes(__in PCA Line 136  DWORD GetKeyAlgorithmAttributes(__in PCA
136          return dwReturn;          return dwReturn;
137  }  }
138    
139  DWORD SetKeyAlgorithmAttributes(__in PCARD_DATA pCardData,  DWORD OCardSetKeyAlgorithmAttributes(__in PCARD_DATA pCardData,
140                                                                  __in OPENPGP_CONTAINER dwContainer,                                                                  __in OPENPGP_CONTAINER dwContainer,
141                                                                  __out POPENPGP_ALGORITHM_ATTRIBUTE pAttributes)                                                                  __out POPENPGP_ALGORITHM_ATTRIBUTE pAttributes)
142  {  {
# Line 171  DWORD SetKeyAlgorithmAttributes(__in PCA Line 164  DWORD SetKeyAlgorithmAttributes(__in PCA
164                          __leave;                          __leave;
165                  }                  }
166                  memcpy(&TempAttributes, pAttributes, sizeof(OPENPGP_ALGORITHM_ATTRIBUTE));                  memcpy(&TempAttributes, pAttributes, sizeof(OPENPGP_ALGORITHM_ATTRIBUTE));
167                  wTemp = TempAttributes.wExponentLength;                  wTemp = TempAttributes.wExponentLengthInBit;
168                  TempAttributes.wExponentLength = (wTemp % 0x100) * 0x100 + (wTemp / 0x100);                  TempAttributes.wExponentLengthInBit = (wTemp % 0x100) * 0x100 + (wTemp / 0x100);
169                  wTemp = TempAttributes.wModulusLength;                  wTemp = TempAttributes.wModulusLengthInBit;
170                  TempAttributes.wModulusLength = (wTemp % 0x100) * 0x100 + (wTemp / 0x100);                  TempAttributes.wModulusLengthInBit = (wTemp % 0x100) * 0x100 + (wTemp / 0x100);
171    
172                  dwReturn = SCardWriteFile(pCardData, szOpenPGPDir, szAlgorithmAttributes, (PBYTE) &TempAttributes, sizeof(OPENPGP_ALGORITHM_ATTRIBUTE));                  dwReturn = OCardWriteFile(pCardData, szOpenPGPDir, szAlgorithmAttributes, (PBYTE) &TempAttributes, sizeof(OPENPGP_ALGORITHM_ATTRIBUTE));
173                  if (dwReturn)                  if (dwReturn)
174                  {                  {
175                          __leave;                          __leave;
# Line 407  DWORD UpdateGenerationDateTime(__in PCAR Line 400  DWORD UpdateGenerationDateTime(__in PCAR
400                  pbCommand[6] = (BYTE) ((*pdwSecondsSince1970 % 0x1000000) / 0x10000);                  pbCommand[6] = (BYTE) ((*pdwSecondsSince1970 % 0x1000000) / 0x10000);
401                  pbCommand[7] = (BYTE) ((*pdwSecondsSince1970 % 0x10000) / 0x100);                  pbCommand[7] = (BYTE) ((*pdwSecondsSince1970 % 0x10000) / 0x100);
402                  pbCommand[8] = (BYTE) ((*pdwSecondsSince1970 % 0x100) / 0x1);                  pbCommand[8] = (BYTE) ((*pdwSecondsSince1970 % 0x100) / 0x1);
403                  dwReturn = SCardSendCommand(pCardData, pbCommand, dwCommandSize);                  dwReturn = OCardSendCommand(pCardData, pbCommand, dwCommandSize);
404          }          }
405          __finally          __finally
406          {          {
# Line 506  DWORD UpdateFingerPrint(__in PCARD_DATA Line 499  DWORD UpdateFingerPrint(__in PCARD_DATA
499                          __leave;                          __leave;
500                  }                  }
501                  pbCommand[3] = Containers[dwContainer].bSignatureTag;                  pbCommand[3] = Containers[dwContainer].bSignatureTag;
502                  dwReturn = SCardSendCommand(pCardData, pbCommand, dwCommandSize);                  dwReturn = OCardSendCommand(pCardData, pbCommand, dwCommandSize);
503    
504          }          }
505          __finally          __finally
# Line 524  DWORD UpdateFingerPrint(__in PCARD_DATA Line 517  DWORD UpdateFingerPrint(__in PCARD_DATA
517    
518  }  }
519    
520  DWORD SCardReadPublicKey(PCARD_DATA pCardData, OPENPGP_CONTAINER dwContainer, PBYTE *pbPublicKey, PDWORD pdwPublicKeySize)  DWORD OCardReadPublicKey(PCARD_DATA pCardData, OPENPGP_CONTAINER dwContainer, PBYTE *pbPublicKey, PDWORD pdwPublicKeySize)
521  {  {
522          DWORD dwReturn;          DWORD dwReturn;
523          PBYTE pbData = NULL;          PBYTE pbData = NULL;
# Line 570  DWORD SCardReadPublicKey(PCARD_DATA pCar Line 563  DWORD SCardReadPublicKey(PCARD_DATA pCar
563                          pbCmd[dwCmdSize++] = 0xFF;                          pbCmd[dwCmdSize++] = 0xFF;
564                  }                  }
565    
566                  dwReturn = SCardGetData(pCardData, pbCmd, dwCmdSize, &pbData, &dwResponseSize);                  dwReturn = OCardGetData(pCardData, pbCmd, dwCmdSize, &pbData, &dwResponseSize);
567                  if (dwReturn)                  if (dwReturn)
568                  {                  {
569                          __leave;                          __leave;
# Line 623  DWORD SCardReadPublicKey(PCARD_DATA pCar Line 616  DWORD SCardReadPublicKey(PCARD_DATA pCar
616          return dwReturn;          return dwReturn;
617  }  }
618    
619  DWORD SCardCreateKey(PCARD_DATA pCardData, OPENPGP_CONTAINER dwContainer, DWORD dwBitLen)  DWORD OCardCreateKey(PCARD_DATA pCardData, OPENPGP_CONTAINER dwContainer, DWORD dwBitLen)
620  {  {
621          DWORD dwReturn;          DWORD dwReturn;
622          PBYTE pbData = NULL;          PBYTE pbData = NULL;
# Line 656  DWORD SCardCreateKey(PCARD_DATA pCardDat Line 649  DWORD SCardCreateKey(PCARD_DATA pCardDat
649                          __leave;                          __leave;
650                  }                  }
651                  // key len                  // key len
652                  dwReturn = GetKeyAlgorithmAttributes(pCardData, dwContainer, &Attributes);                  dwReturn = OCardGetKeyAlgorithmAttributes(pCardData, dwContainer, &Attributes);
653                  if (dwReturn)                  if (dwReturn)
654                  {                  {
655                          __leave;                          __leave;
656                  }                  }
657                  Attributes.wModulusLength = (WORD) dwBitLen;                  Attributes.wModulusLengthInBit = (WORD) dwBitLen;
658                  dwReturn = SetKeyAlgorithmAttributes(pCardData, dwContainer, &Attributes);                  dwReturn = OCardSetKeyAlgorithmAttributes(pCardData, dwContainer, &Attributes);
659                  if (dwReturn)                  if (dwReturn)
660                  {                  {
661                          __leave;                          __leave;
# Line 681  DWORD SCardCreateKey(PCARD_DATA pCardDat Line 674  DWORD SCardCreateKey(PCARD_DATA pCardDat
674                          pbCmd[dwCmdSize++] = 0xFF;                          pbCmd[dwCmdSize++] = 0xFF;
675                  }                  }
676                                    
677                  dwReturn = SCardGetData(pCardData, pbCmd, dwCmdSize, &pbData, &dwResponseSize);                  dwReturn = OCardGetData(pCardData, pbCmd, dwCmdSize, &pbData, &dwResponseSize);
678                  if (dwReturn)                  if (dwReturn)
679                  {                  {
680                          __leave;                          __leave;
# Line 712  DWORD SCardCreateKey(PCARD_DATA pCardDat Line 705  DWORD SCardCreateKey(PCARD_DATA pCardDat
705                                                                                  TRUE,                                                                                  TRUE,
706                                                                                  dwExponent                                                                                  dwExponent
707                                                                                  );                                                                                  );
708                    if (dwReturn)
709                    {
710                            __leave;
711                    }
712                    switch(dwContainer)
713                    {
714                    case Signature:
715                            pContext->fHasSignature = TRUE;
716                            break;
717                    case Authentication:
718                            pContext->fHasAuthentication = TRUE;
719                            break;
720                    case Confidentiality:
721                            pContext->fHasDecryption = TRUE;
722                            break;
723                    }
724          }          }
725          __finally          __finally
726          {          {
# Line 722  DWORD SCardCreateKey(PCARD_DATA pCardDat Line 731  DWORD SCardCreateKey(PCARD_DATA pCardDat
731          return dwReturn;          return dwReturn;
732  }  }
733    
734  DWORD SCardImportKey(PCARD_DATA pCardData,  DWORD OCardImportKey(PCARD_DATA pCardData,
735                                           OPENPGP_CONTAINER dwContainer,                                           OPENPGP_CONTAINER dwContainer,
736                                           PBYTE pBlob,                                           PBYTE pBlob,
737                                           DWORD dwKeySize)                                           DWORD dwKeySize)
# Line 761  DWORD SCardImportKey(PCARD_DATA pCardDat Line 770  DWORD SCardImportKey(PCARD_DATA pCardDat
770                          __leave;                          __leave;
771                  }                  }
772                                    
773                  dwReturn = GetKeyAlgorithmAttributes(pCardData, dwContainer, &Attributes);                  dwReturn = OCardGetKeyAlgorithmAttributes(pCardData, dwContainer, &Attributes);
774                  if (dwReturn)                  if (dwReturn)
775                  {                  {
776                          __leave;                          __leave;
777                  }                  }
778                  Attributes.wModulusLength = (WORD) pbPublicKeyBlob->rsapubkey.bitlen;                  Attributes.wModulusLengthInBit = (WORD) pbPublicKeyBlob->rsapubkey.bitlen;
779                  dwReturn = SetKeyAlgorithmAttributes(pCardData, dwContainer, &Attributes);                  dwReturn = OCardSetKeyAlgorithmAttributes(pCardData, dwContainer, &Attributes);
780                  if (dwReturn)                  if (dwReturn)
781                  {                  {
782                          __leave;                          __leave;
# Line 806  DWORD SCardImportKey(PCARD_DATA pCardDat Line 815  DWORD SCardImportKey(PCARD_DATA pCardDat
815                          pbCommand[4] = (BYTE) dwTlvSize;                          pbCommand[4] = (BYTE) dwTlvSize;
816                          memcpy(pbCommand + 5, pbTlv, dwTlvSize);                          memcpy(pbCommand + 5, pbTlv, dwTlvSize);
817                  }                  }
818                  dwReturn = SCardSendCommand(pCardData, pbCommand, dwCommandSize);                  dwReturn = OCardSendCommand(pCardData, pbCommand, dwCommandSize);
819                  if (dwReturn)                  if (dwReturn)
820                  {                  {
821                          __leave;                          __leave;
# Line 822  DWORD SCardImportKey(PCARD_DATA pCardDat Line 831  DWORD SCardImportKey(PCARD_DATA pCardDat
831                                                                                  FALSE,                                                                                  FALSE,
832                                                                                  pbPublicKeyBlob->rsapubkey.pubexp                                                                                  pbPublicKeyBlob->rsapubkey.pubexp
833                                                                                  );                                                                                  );
834                    if (dwReturn)
835                    {
836                            __leave;
837                    }
838                    switch(dwContainer)
839                    {
840                    case Signature:
841                            pContext->fHasSignature = TRUE;
842                            break;
843                    case Authentication:
844                            pContext->fHasAuthentication = TRUE;
845                            break;
846                    case Confidentiality:
847                            pContext->fHasDecryption = TRUE;
848                            break;
849                    }
850          }          }
851          __finally          __finally
852          {          {
# Line 840  DWORD SCardImportKey(PCARD_DATA pCardDat Line 865  DWORD SCardImportKey(PCARD_DATA pCardDat
865          return dwReturn;          return dwReturn;
866  }  }
867    
868  DWORD SCardSign(PCARD_DATA pCardData,  DWORD OCardSign(PCARD_DATA pCardData,
869                                  PCARD_SIGNING_INFO  pInfo)                                  PCARD_SIGNING_INFO  pInfo)
870  {  {
871          DWORD dwReturn;          DWORD dwReturn;
872          PBYTE pbData = NULL;          PBYTE pbData = NULL;
873          DWORD dwCmdSize = 0, dwI;          DWORD dwCmdSize = 0, dwI;
874          POPENPGP_CONTEXT pContext;          POPENPGP_CONTEXT pContext;
875            OPENPGP_ALGORITHM_ATTRIBUTE Attributes;
876          BYTE pbCmd[6 + 256 + 256] = {0x00,          BYTE pbCmd[6 + 256 + 256] = {0x00,
877                                      0x2A,                                      0x2A,
878                                          0x9E,                                          0x9E,
# Line 857  DWORD SCardSign(PCARD_DATA pCardData, Line 883  DWORD SCardSign(PCARD_DATA pCardData,
883          __try          __try
884          {          {
885                  Trace(WINEVENT_LEVEL_VERBOSE, L"Enter dwContainer=%d",pInfo->bContainerIndex);                  Trace(WINEVENT_LEVEL_VERBOSE, L"Enter dwContainer=%d",pInfo->bContainerIndex);
886                  if (pInfo->bContainerIndex != Signature)                  if (pInfo->dwSigningFlags & ~(CARD_PADDING_INFO_PRESENT | CARD_BUFFER_SIZE_ONLY | CRYPT_NOHASHOID | CRYPT_TYPE2_FORMAT))
887                  {                  {
888                          dwReturn = SCARD_E_NO_KEY_CONTAINER;                          Trace(WINEVENT_LEVEL_ERROR, L"wrong flag %d", pInfo->dwSigningFlags);
889                          Trace(WINEVENT_LEVEL_ERROR, L"SCARD_E_NO_KEY_CONTAINER %d", pInfo->bContainerIndex);                          dwReturn = SCARD_E_INVALID_PARAMETER;
890                          __leave;                          __leave;
891                  }                  }
892                  if (CARD_PADDING_PKCS1 & pInfo->dwPaddingType)                  if (pInfo->dwSigningFlags & CARD_PADDING_INFO_PRESENT)
893                  {                  {
894                          dwReturn = SCARD_E_UNSUPPORTED_FEATURE;                          if ( pInfo->dwPaddingType == CARD_PADDING_PKCS1)
895                          Trace(WINEVENT_LEVEL_ERROR, L"CARD_PADDING_PKCS1");                          {
896                          __leave;                                  dwReturn = SCARD_E_UNSUPPORTED_FEATURE;
897                                    Trace(WINEVENT_LEVEL_ERROR, L"CARD_PADDING_PKCS1");
898                                    __leave;
899                            }
900                            else if (pInfo->dwPaddingType == CARD_PADDING_PSS)
901                            {
902                                    dwReturn = SCARD_E_UNSUPPORTED_FEATURE;
903                                    Trace(WINEVENT_LEVEL_ERROR, L"CARD_PADDING_PSS");
904                                    __leave;
905                            }
906                            else
907                            {
908                                    dwReturn = SCARD_E_INVALID_PARAMETER;
909                                    Trace(WINEVENT_LEVEL_ERROR, L"pInfo->dwPaddingType = %d", pInfo->dwPaddingType);
910                                    __leave;
911                            }
912                  }                  }
913                  else if (CARD_PADDING_PSS & pInfo->dwPaddingType)                  if (!(pInfo->aiHashAlg & ALG_CLASS_HASH))
914                  {                  {
915                          dwReturn = SCARD_E_UNSUPPORTED_FEATURE;                          dwReturn = SCARD_E_INVALID_PARAMETER;
916                          Trace(WINEVENT_LEVEL_ERROR, L"CARD_PADDING_PSS");                          Trace(WINEVENT_LEVEL_ERROR, L"pInfo->aiHashAlg == %d", pInfo->aiHashAlg);
917                          __leave;                          __leave;
918                  }                  }
919                  for(dwI = 0 ; dwI < dwSignatureAlgorithmCount ; dwI++)                  for(dwI = 0 ; dwI < dwSignatureAlgorithmCount ; dwI++)
# Line 886  DWORD SCardSign(PCARD_DATA pCardData, Line 927  DWORD SCardSign(PCARD_DATA pCardData,
927                  if (dwI >= dwSignatureAlgorithmCount)                  if (dwI >= dwSignatureAlgorithmCount)
928                  {                  {
929                          Trace(WINEVENT_LEVEL_ERROR, L"alg not found %d", pInfo->aiHashAlg);                          Trace(WINEVENT_LEVEL_ERROR, L"alg not found %d", pInfo->aiHashAlg);
930                            dwReturn = SCARD_E_UNSUPPORTED_FEATURE;
931                          __leave;                          __leave;
932                  }                  }
933                  if (SignatureAlgorithm[dwI].dwHashSize != pInfo->cbData)                  if (SignatureAlgorithm[dwI].dwHashSize != pInfo->cbData)
934                  {                  {
935                          Trace(WINEVENT_LEVEL_ERROR, L"wrong hash size %d", pInfo->cbData);                          Trace(WINEVENT_LEVEL_ERROR, L"wrong hash size %d", pInfo->cbData);
936                            dwReturn = SCARD_E_INVALID_PARAMETER;
937                            __leave;
938                    }
939                    if (pInfo->bContainerIndex != Signature)
940                    {
941                            dwReturn = SCARD_E_NO_KEY_CONTAINER;
942                            Trace(WINEVENT_LEVEL_ERROR, L"SCARD_E_NO_KEY_CONTAINER %d", pInfo->bContainerIndex);
943                            __leave;
944                    }
945                    if (pInfo->dwSigningFlags & CARD_BUFFER_SIZE_ONLY)
946                    {
947                            // optimisation :
948                            // return the buffer size only
949                            dwReturn = OCardGetKeyAlgorithmAttributes(pCardData, Signature, &Attributes);
950                            if (dwReturn)
951                            {
952                                    __leave;
953                            }
954                            pInfo->cbSignedData = Attributes.wModulusLengthInBit/8;
955                            dwReturn = 0;
956                          __leave;                          __leave;
957                  }                  }
958    
959                  pContext = (POPENPGP_CONTEXT) pCardData->pvVendorSpecific;                  pContext = (POPENPGP_CONTEXT) pCardData->pvVendorSpecific;
960    
961                  dwCmdSize = 5;                  dwCmdSize = 5;
# Line 920  DWORD SCardSign(PCARD_DATA pCardData, Line 983  DWORD SCardSign(PCARD_DATA pCardData,
983                  {                  {
984                          pbCmd[dwCmdSize++] = 0;                          pbCmd[dwCmdSize++] = 0;
985                  }                  }
986                  dwReturn = SCardGetData(pCardData, pbCmd, dwCmdSize, &(pInfo->pbSignedData), &(pInfo->cbSignedData));                  dwReturn = OCardGetData(pCardData, pbCmd, dwCmdSize, &(pInfo->pbSignedData), &(pInfo->cbSignedData));
987                  if (dwReturn == SCARD_W_WRONG_CHV)                  if (dwReturn == SCARD_W_WRONG_CHV)
988                  {                  {
989                          dwReturn = SCARD_W_SECURITY_VIOLATION;                          dwReturn = SCARD_W_SECURITY_VIOLATION;
# Line 950  DWORD SCardSign(PCARD_DATA pCardData, Line 1013  DWORD SCardSign(PCARD_DATA pCardData,
1013          return dwReturn;          return dwReturn;
1014  }  }
1015    
1016  DWORD SCardAuthenticate(PCARD_DATA pCardData,  DWORD OCardAuthenticate(PCARD_DATA pCardData,
1017                                  PCARD_SIGNING_INFO  pInfo)                                  PCARD_SIGNING_INFO  pInfo)
1018  {  {
1019          DWORD dwReturn;          DWORD dwReturn;
1020          PBYTE pbData = NULL;          PBYTE pbData = NULL;
1021          DWORD dwCmdSize = 0, dwI;          DWORD dwCmdSize = 0, dwI;
1022          POPENPGP_CONTEXT pContext;          POPENPGP_CONTEXT pContext;
1023            OPENPGP_ALGORITHM_ATTRIBUTE Attributes;
1024          BYTE pbCmd[6 + 256 + 256] = {0x00,          BYTE pbCmd[6 + 256 + 256] = {0x00,
1025                                      0x88,                                      0x88,
1026                                          0x00,                                          0x00,
# Line 967  DWORD SCardAuthenticate(PCARD_DATA pCard Line 1031  DWORD SCardAuthenticate(PCARD_DATA pCard
1031          __try          __try
1032          {          {
1033                  Trace(WINEVENT_LEVEL_VERBOSE, L"Enter dwContainer=%d",pInfo->bContainerIndex);                  Trace(WINEVENT_LEVEL_VERBOSE, L"Enter dwContainer=%d",pInfo->bContainerIndex);
1034                  if (pInfo->bContainerIndex != Authentication)                  if (pInfo->dwSigningFlags & ~(CARD_PADDING_INFO_PRESENT | CARD_BUFFER_SIZE_ONLY | CRYPT_NOHASHOID | CRYPT_TYPE2_FORMAT))
                 {  
                         dwReturn = SCARD_E_NO_KEY_CONTAINER;  
                         Trace(WINEVENT_LEVEL_ERROR, L"SCARD_E_NO_KEY_CONTAINER %d", pInfo->bContainerIndex);  
                         __leave;  
                 }  
                 if (CARD_PADDING_PKCS1 & pInfo->dwPaddingType)  
1035                  {                  {
1036                          dwReturn = SCARD_E_UNSUPPORTED_FEATURE;                          Trace(WINEVENT_LEVEL_ERROR, L"wrong flag %d", pInfo->dwSigningFlags);
1037                          Trace(WINEVENT_LEVEL_ERROR, L"CARD_PADDING_PKCS1");                          dwReturn = SCARD_E_INVALID_PARAMETER;
1038                          __leave;                          __leave;
1039                  }                  }
1040                  else if (CARD_PADDING_PSS & pInfo->dwPaddingType)                  if (pInfo->dwSigningFlags & CARD_PADDING_INFO_PRESENT)
1041                  {                  {
1042                          dwReturn = SCARD_E_UNSUPPORTED_FEATURE;                          if ( pInfo->dwPaddingType == CARD_PADDING_PKCS1)
1043                          Trace(WINEVENT_LEVEL_ERROR, L"CARD_PADDING_PSS");                          {
1044                          __leave;                                  dwReturn = SCARD_E_UNSUPPORTED_FEATURE;
1045                                    Trace(WINEVENT_LEVEL_ERROR, L"CARD_PADDING_PKCS1");
1046                                    __leave;
1047                            }
1048                            else if (pInfo->dwPaddingType == CARD_PADDING_PSS)
1049                            {
1050                                    dwReturn = SCARD_E_UNSUPPORTED_FEATURE;
1051                                    Trace(WINEVENT_LEVEL_ERROR, L"CARD_PADDING_PSS");
1052                                    __leave;
1053                            }
1054                            else
1055                            {
1056                                    dwReturn = SCARD_E_INVALID_PARAMETER;
1057                                    Trace(WINEVENT_LEVEL_ERROR, L"pInfo->dwPaddingType = %d", pInfo->dwPaddingType);
1058                                    __leave;
1059                            }
1060                  }                  }
1061                  for(dwI = 0 ; dwI < dwSignatureAlgorithmCount ; dwI++)                  for(dwI = 0 ; dwI < dwSignatureAlgorithmCount ; dwI++)
1062                  {                  {
# Line 1003  DWORD SCardAuthenticate(PCARD_DATA pCard Line 1076  DWORD SCardAuthenticate(PCARD_DATA pCard
1076                          Trace(WINEVENT_LEVEL_ERROR, L"wrong hash size %d", pInfo->cbData);                          Trace(WINEVENT_LEVEL_ERROR, L"wrong hash size %d", pInfo->cbData);
1077                          __leave;                          __leave;
1078                  }                  }
1079                    if (pInfo->bContainerIndex != Authentication)
1080                    {
1081                            dwReturn = SCARD_E_NO_KEY_CONTAINER;
1082                            Trace(WINEVENT_LEVEL_ERROR, L"SCARD_E_NO_KEY_CONTAINER %d", pInfo->bContainerIndex);
1083                            __leave;
1084                    }
1085                    if (pInfo->dwSigningFlags & CARD_BUFFER_SIZE_ONLY)
1086                    {
1087                            // optimisation :
1088                            // return the buffer size only
1089                            dwReturn = OCardGetKeyAlgorithmAttributes(pCardData, Signature, &Attributes);
1090                            if (dwReturn)
1091                            {
1092                                    __leave;
1093                            }
1094                            pInfo->cbSignedData = Attributes.wModulusLengthInBit/8;
1095                            dwReturn = 0;
1096                            __leave;
1097                    }
1098    
1099                  pContext = (POPENPGP_CONTEXT) pCardData->pvVendorSpecific;                  pContext = (POPENPGP_CONTEXT) pCardData->pvVendorSpecific;
1100    
1101                  dwCmdSize = 5;                  dwCmdSize = 5;
# Line 1030  DWORD SCardAuthenticate(PCARD_DATA pCard Line 1123  DWORD SCardAuthenticate(PCARD_DATA pCard
1123                  {                  {
1124                          pbCmd[dwCmdSize++] = 0;                          pbCmd[dwCmdSize++] = 0;
1125                  }                  }
1126                  dwReturn = SCardGetData(pCardData, pbCmd, dwCmdSize, &(pInfo->pbSignedData), &(pInfo->cbSignedData));                  dwReturn = OCardGetData(pCardData, pbCmd, dwCmdSize, &(pInfo->pbSignedData), &(pInfo->cbSignedData));
1127                  if (dwReturn == SCARD_W_WRONG_CHV)                  if (dwReturn == SCARD_W_WRONG_CHV)
1128                  {                  {
1129                          dwReturn = SCARD_W_SECURITY_VIOLATION;                          dwReturn = SCARD_W_SECURITY_VIOLATION;
# Line 1060  DWORD SCardAuthenticate(PCARD_DATA pCard Line 1153  DWORD SCardAuthenticate(PCARD_DATA pCard
1153          return dwReturn;          return dwReturn;
1154  }  }
1155    
1156  DWORD SCardDecrypt(PCARD_DATA pCardData,  DWORD OCardDecrypt(PCARD_DATA pCardData,
1157                                  PCARD_RSA_DECRYPT_INFO  pInfo)                                  PCARD_RSA_DECRYPT_INFO  pInfo)
1158  {  {
1159          DWORD dwReturn;          DWORD dwReturn;
# Line 1074  DWORD SCardDecrypt(PCARD_DATA pCardData, Line 1167  DWORD SCardDecrypt(PCARD_DATA pCardData,
1167                                          };                                          };
1168          POPENPGP_CONTEXT pContext;          POPENPGP_CONTEXT pContext;
1169          DWORD dwI;          DWORD dwI;
1170            OPENPGP_ALGORITHM_ATTRIBUTE Attributes;
1171          __try          __try
1172          {          {
1173                  Trace(WINEVENT_LEVEL_VERBOSE, L"Enter dwContainer=%d",pInfo->bContainerIndex);                  Trace(WINEVENT_LEVEL_VERBOSE, L"Enter dwContainer=%d",pInfo->bContainerIndex);
# Line 1089  DWORD SCardDecrypt(PCARD_DATA pCardData, Line 1183  DWORD SCardDecrypt(PCARD_DATA pCardData,
1183                          Trace(WINEVENT_LEVEL_ERROR, L"SCARD_E_NO_KEY_CONTAINER %d", pInfo->bContainerIndex);                          Trace(WINEVENT_LEVEL_ERROR, L"SCARD_E_NO_KEY_CONTAINER %d", pInfo->bContainerIndex);
1184                          __leave;                          __leave;
1185                  }                  }
1186                    // check the buffer size
1187                    dwReturn = OCardGetKeyAlgorithmAttributes(pCardData, Confidentiality, &Attributes);
1188                    if (dwReturn)
1189                    {
1190                            __leave;
1191                    }
1192                    if (pInfo->cbData < (DWORD)(Attributes.wModulusLengthInBit/8))
1193                    {
1194                            dwReturn = SCARD_E_INSUFFICIENT_BUFFER;
1195                            Trace(WINEVENT_LEVEL_ERROR, L"SCARD_E_INSUFFICIENT_BUFFER %d", pInfo->cbData);
1196                            __leave;
1197                    }
1198                  pContext = (POPENPGP_CONTEXT) pCardData->pvVendorSpecific;                  pContext = (POPENPGP_CONTEXT) pCardData->pvVendorSpecific;
1199                  dwCmdSize = 5;                  dwCmdSize = 5;
1200                  if (pContext->fExtentedLeLcFields)                  if (pContext->fExtentedLeLcFields)
# Line 1116  DWORD SCardDecrypt(PCARD_DATA pCardData, Line 1222  DWORD SCardDecrypt(PCARD_DATA pCardData,
1222                  {                  {
1223                          pbCmd[dwCmdSize++] = 0;                          pbCmd[dwCmdSize++] = 0;
1224                  }                  }
1225                  dwReturn = SCardGetData(pCardData, pbCmd, dwCmdSize, &pbData, &dwResponseSize);                  dwReturn = OCardGetData(pCardData, pbCmd, dwCmdSize, &pbData, &dwResponseSize);
1226                  if (dwReturn)                  if (dwReturn)
1227                  {                  {
1228                          __leave;                          __leave;
# Line 1158  DWORD SCardDecrypt(PCARD_DATA pCardData, Line 1264  DWORD SCardDecrypt(PCARD_DATA pCardData,
1264  }  }
1265    
1266    
1267  DWORD GetPinInfo(DWORD __in dwPinIndex, __inout PPIN_INFO pPinInfo)  DWORD OCardReadContainerMapFile(__in PCARD_DATA  pCardData,
1268                                            __in PBYTE* ppbResponse, __in PDWORD pdwResponseSize)
1269  {  {
1270          DWORD dwReturn=0;          DWORD dwReturn = 0;
1271            POPENPGP_CONTEXT pContext = (POPENPGP_CONTEXT) pCardData->pvVendorSpecific;
1272            OPENPGP_ALGORITHM_ATTRIBUTE Attributes;
1273          __try          __try
1274          {          {
1275                  Trace(WINEVENT_LEVEL_VERBOSE, L"Enter dwPinIndex=%d",dwPinIndex);                  PCONTAINER_MAP_RECORD pContainer = NULL;
1276                  switch(dwPinIndex)                  BOOL fIsDefaultContainerSet = FALSE;
1277                    *pdwResponseSize = sizeof(CONTAINER_MAP_RECORD) * MaxContainer;
1278                    *ppbResponse = pCardData->pfnCspAlloc(*pdwResponseSize);
1279                    if (! *ppbResponse )
1280                    {
1281                            dwReturn = SCARD_E_NO_MEMORY;
1282                            Trace(WINEVENT_LEVEL_ERROR, L"SCARD_E_NO_MEMORY");
1283                            __leave;
1284                    }
1285                    pContainer = (PCONTAINER_MAP_RECORD) *ppbResponse;
1286                    memset(pContainer,0,sizeof(CONTAINER_MAP_RECORD) * 3);
1287                    
1288                    dwReturn = OCardGetKeyAlgorithmAttributes(pCardData, Authentication, &Attributes);
1289                    if (dwReturn)
1290                    {
1291                            __leave;
1292                    }
1293                    pContainer[Authentication].wSigKeySizeBits = Attributes.wModulusLengthInBit;
1294                    swprintf_s(pContainer[Authentication].wszGuid,MAX_CONTAINER_NAME_LEN + 1,
1295                            L"OPENPGP_%02X%02X_%02X%02X_%02X%02X%02X%02X_Authenticate",
1296                            pContext->Aid.AidVersion[0],pContext->Aid.AidVersion[1],
1297                            pContext->Aid.AidManufacturer[0],pContext->Aid.AidManufacturer[1],
1298                            pContext->Aid.AidSerialNumber[0],pContext->Aid.AidSerialNumber[1],
1299                            pContext->Aid.AidSerialNumber[2],pContext->Aid.AidSerialNumber[3]);
1300                    if (pContext->fHasAuthentication)
1301                    {
1302                            pContainer[Authentication].bFlags = CONTAINER_MAP_VALID_CONTAINER | CONTAINER_MAP_DEFAULT_CONTAINER;
1303                            fIsDefaultContainerSet = TRUE;
1304                    }
1305                    dwReturn = OCardGetKeyAlgorithmAttributes(pCardData, Signature, &Attributes);
1306                    if (dwReturn)
1307                    {
1308                            __leave;
1309                    }
1310                    pContainer[Signature].wSigKeySizeBits = Attributes.wModulusLengthInBit;
1311                    swprintf_s(pContainer[Signature].wszGuid,MAX_CONTAINER_NAME_LEN + 1,
1312                            L"OPENPGP_%02X%02X_%02X%02X_%02X%02X%02X%02X_Signature",
1313                            pContext->Aid.AidVersion[0],pContext->Aid.AidVersion[1],
1314                            pContext->Aid.AidManufacturer[0],pContext->Aid.AidManufacturer[1],
1315                            pContext->Aid.AidSerialNumber[0],pContext->Aid.AidSerialNumber[1],
1316                            pContext->Aid.AidSerialNumber[2],pContext->Aid.AidSerialNumber[3]);
1317                    if (pContext->fHasSignature)
1318                    {
1319                            pContainer[Signature].bFlags = CONTAINER_MAP_VALID_CONTAINER;
1320                            if (!fIsDefaultContainerSet)
1321                            {
1322                                    pContainer[Signature].bFlags |= CONTAINER_MAP_DEFAULT_CONTAINER;
1323                                    fIsDefaultContainerSet = TRUE;
1324                            }
1325                    }
1326                    
1327                    dwReturn = OCardGetKeyAlgorithmAttributes(pCardData, Confidentiality, &Attributes);
1328                    if (dwReturn)
1329                  {                  {
                 case ROLE_SIGNATURE:  
                         pPinInfo->PinType = AlphaNumericPinType;  
                         pPinInfo->PinPurpose = DigitalSignaturePin;  
                         pPinInfo->dwChangePermission = PIN_CHANGE_FLAG_CHANGEPIN;  
                         pPinInfo->PinCachePolicy.dwVersion = PIN_CACHE_POLICY_CURRENT_VERSION;  
                         pPinInfo->PinCachePolicy.PinCachePolicyType = PinCacheAlwaysPrompt;  
                         break;  
                 case ROLE_AUTHENTICATION:  
                         pPinInfo->PinType = AlphaNumericPinType;  
                         pPinInfo->PinPurpose = AuthenticationPin;  
                         pPinInfo->dwChangePermission = PIN_CHANGE_FLAG_CHANGEPIN;  
                         pPinInfo->PinCachePolicy.dwVersion = PIN_CACHE_POLICY_CURRENT_VERSION;  
                         pPinInfo->PinCachePolicy.PinCachePolicyType = PinCacheAlwaysPrompt;  
                         break;  
                 case ROLE_CONFIDENTIALITY:  
                         pPinInfo->PinType = AlphaNumericPinType;  
                         pPinInfo->PinPurpose = EncryptionPin;  
                         pPinInfo->dwChangePermission = PIN_CHANGE_FLAG_CHANGEPIN;  
                         pPinInfo->PinCachePolicy.dwVersion = PIN_CACHE_POLICY_CURRENT_VERSION;  
                         pPinInfo->PinCachePolicy.PinCachePolicyType = PinCacheAlwaysPrompt;  
                         break;  
                 default:  
                         Trace(WINEVENT_LEVEL_ERROR, L"dwPinIndex == %d", dwPinIndex);  
                         dwReturn  = SCARD_E_INVALID_PARAMETER ;  
1330                          __leave;                          __leave;
1331                  }                  }
1332                    pContainer[Confidentiality].wKeyExchangeKeySizeBits = Attributes.wModulusLengthInBit;
1333                    
1334                    swprintf_s(pContainer[Confidentiality].wszGuid,MAX_CONTAINER_NAME_LEN + 1,
1335                            L"OPENPGP_%02X%02X_%02X%02X_%02X%02X%02X%02X_Confidential",
1336                            pContext->Aid.AidVersion[0],pContext->Aid.AidVersion[1],
1337                            pContext->Aid.AidManufacturer[0],pContext->Aid.AidManufacturer[1],
1338                            pContext->Aid.AidSerialNumber[0],pContext->Aid.AidSerialNumber[1],
1339                            pContext->Aid.AidSerialNumber[2],pContext->Aid.AidSerialNumber[3]);
1340                    if (pContext->fHasDecryption)
1341                    {
1342                            pContainer[Confidentiality].bFlags = CONTAINER_MAP_VALID_CONTAINER;
1343                            if (!fIsDefaultContainerSet)
1344                            {
1345                                    pContainer[Confidentiality].bFlags |= CONTAINER_MAP_DEFAULT_CONTAINER;
1346                                    fIsDefaultContainerSet = TRUE;
1347                            }
1348                    }
1349          }          }
1350          __finally          __finally
1351          {          {
1352          }          }
         Trace(WINEVENT_LEVEL_VERBOSE, L"dwReturn = 0x%08X",dwReturn);  
1353          return dwReturn;          return dwReturn;
1354  }  }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26