/[openpgpmdrv]/trunk/OpenPGPminidriverTest/CryptoOperations.cpp
ViewVC logotype

Diff of /trunk/OpenPGPminidriverTest/CryptoOperations.cpp

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

revision 2 by vletoux, Tue Feb 23 19:18:59 2010 UTC revision 3 by vletoux, Thu Feb 25 22:09:17 2010 UTC
# Line 59  DWORD GenerateNewKey(DWORD dwIndex) Line 59  DWORD GenerateNewKey(DWORD dwIndex)
59          return dwReturn;          return dwReturn;
60  }  }
61    
62    #pragma pack(push,1)
63    typedef struct _RSAPUBLICKEYBLOB
64    {
65            BLOBHEADER blobheader;
66            RSAPUBKEY rsapubkey;
67            BYTE modulus[sizeof(DWORD)];
68    } RSAPUBLICKEYBLOB, *PRSAPUBLICKEYBLOB;
69    #pragma pack(pop)
70    
71  DWORD ImportKey(DWORD dwIndex)  DWORD ImportKey(DWORD dwIndex)
72  {  {
73          DWORD dwReturn, dwKeySpec;          DWORD dwReturn, dwKeySpec;
74          PIN_ID  PinId;          PIN_ID  PinId;
75          HCRYPTPROV hProv = NULL;          HCRYPTPROV hProv = NULL;
76          HCRYPTKEY hKey = NULL;          HCRYPTKEY hKey = NULL;
77          TCHAR szContainerName[] = TEXT("Test_OPENPGPG");          TCHAR szContainerName[] = OPENPGP_TEST_CONTAINER;
78          BYTE pbData[4096];          BYTE pbData[4096];
79            BYTE pbBlobRef[4096];
80          DWORD dwDataSize = ARRAYSIZE(pbData);          DWORD dwDataSize = ARRAYSIZE(pbData);
81            DWORD dwBlobRefSize = ARRAYSIZE(pbBlobRef);
82          BOOL bStatus;          BOOL bStatus;
83            CONTAINER_INFO  ContainerInfo;
84            PRSAPUBLICKEYBLOB pBlob, pBlobRef;
85            DWORD dwAglLen, dwSize;
86          __try          __try
87          {          {
88                   if (!pCardData)                   if (!pCardData)
# Line 94  DWORD ImportKey(DWORD dwIndex) Line 108  DWORD ImportKey(DWORD dwIndex)
108                          dwReturn = SCARD_E_UNEXPECTED;                          dwReturn = SCARD_E_UNEXPECTED;
109                          __leave;                          __leave;
110                  }                  }
111                  bStatus = CryptAcquireContext(&hProv, szContainerName, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET);                  bStatus = CryptAcquireContext(&hProv, szContainerName, MS_ENHANCED_PROV, PROV_RSA_FULL, 0);
112                  if (!bStatus)                  if (!bStatus)
113                  {                  {
114                          dwReturn = GetLastError();                          dwReturn = GetLastError();
115                          __leave;                          if (dwReturn == NTE_BAD_KEYSET)
116                            {
117                                    bStatus = CryptAcquireContext(&hProv, szContainerName, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET);
118                            }
119                            if (!bStatus)
120                            {
121                                    dwReturn = GetLastError();
122                                    __leave;
123                            }
124                  }                  }
125                  bStatus = CryptGenKey(hProv, AT_KEYEXCHANGE, CRYPT_EXPORTABLE, &hKey);                  bStatus = CryptGenKey(hProv, dwKeySpec, CRYPT_EXPORTABLE, &hKey);
126                  if (!bStatus)                  if (!bStatus)
127                  {                  {
128                          dwReturn = GetLastError();                          dwReturn = GetLastError();
# Line 112  DWORD ImportKey(DWORD dwIndex) Line 134  DWORD ImportKey(DWORD dwIndex)
134                          dwReturn = GetLastError();                          dwReturn = GetLastError();
135                          __leave;                          __leave;
136                  }                  }
137                    dwSize = sizeof(DWORD);
138                    bStatus = CryptGetKeyParam(hKey, KP_KEYLEN, (PBYTE) &dwAglLen,&dwSize , 0);
139                  dwReturn = pCardData->pfnCardCreateContainerEx(pCardData, (BYTE) dwIndex,                  dwReturn = pCardData->pfnCardCreateContainerEx(pCardData, (BYTE) dwIndex,
140                                                                                          CARD_CREATE_CONTAINER_KEY_IMPORT,                                                                                          CARD_CREATE_CONTAINER_KEY_IMPORT,
141                                                                                          dwKeySpec, 1024, pbData, PinId);                                                                                          dwKeySpec, dwAglLen, pbData, PinId);
142                    if (dwReturn)
143                    {
144                            __leave;
145                    }
146                    memset(&ContainerInfo,0,sizeof(CONTAINER_INFO));
147                    ContainerInfo.dwVersion = 0;
148                    dwReturn = pCardData->pfnCardGetContainerInfo(pCardData, (BYTE) dwIndex, 0, &ContainerInfo);
149                    if (dwReturn)
150                    {
151                            __leave;
152                    }
153                    bStatus = CryptExportKey(hKey,  NULL, PUBLICKEYBLOB, 0, pbBlobRef, &dwBlobRefSize);
154                    if (!bStatus)
155                    {
156                            dwReturn = GetLastError();
157                            __leave;
158                    }
159                    pBlobRef = (PRSAPUBLICKEYBLOB) pbBlobRef;
160                    pBlob = (PRSAPUBLICKEYBLOB) ContainerInfo.pbSigPublicKey;
161                    //if (memcmp(pBlobRef, pBlob, ContainerInfo.cbSigPublicKey) != 0)
162                    for (DWORD dwI = 0; dwI < pBlobRef->rsapubkey.bitlen / 8; dwI++)
163                    {
164                            if ( pBlobRef->modulus[dwI] != pBlob->modulus[dwI])
165                            {
166                                    dwReturn = SCARD_E_UNEXPECTED;
167                                    __leave;
168                            }
169                    }
170                    dwReturn = 0;
171    
172          }          }
173          __finally          __finally
174          {          {
175                  if (hKey)                  if (hKey)
176                          CryptDestroyKey(hKey);                          CryptDestroyKey(hKey);
177                  CryptAcquireContext(&hProv, szContainerName, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_DELETEKEYSET);                  //CryptAcquireContext(&hProv, szContainerName, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_DELETEKEYSET);
178                    if (hProv)
179                            CryptReleaseContext(hProv,0);
180          }          }
181          return dwReturn;          return dwReturn;
182  }  }

Legend:
Removed from v.2  
changed lines
  Added in v.3

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26