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

Contents of /trunk/OpenPGPminidriverTest/CryptoOperations.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3 - (show annotations)
Thu Feb 25 22:09:17 2010 UTC (15 years, 2 months ago) by vletoux
File size: 4686 byte(s)
fixed public key export (big endian - little endian issue)
signature works (sign & verify)
decrypt really decrypt but output not recognized yet by cryptoapi
1 /* OpenPGP Smart Card Mini Driver
2 Copyright (C) 2009 Vincent Le Toux
3
4 This library is Free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License version 2.1 as published by the Free Software Foundation.
7
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Lesser General Public License for more details.
12
13 You should have received a copy of the GNU Lesser General Public
14 License along with this library; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18 #include <windows.h>
19 #include <tchar.h>
20 #include <cardmod.h>
21 #include "global.h"
22
23 DWORD GenerateNewKey(DWORD dwIndex)
24 {
25 DWORD dwReturn, dwKeySpec;
26 PIN_ID PinId;
27 __try
28 {
29 if (!pCardData)
30 {
31 dwReturn = SCARD_E_COMM_DATA_LOST;
32 __leave;
33 }
34 switch(dwIndex)
35 {
36 case 0: //Signature,
37 dwKeySpec = AT_SIGNATURE;
38 PinId = ROLE_USER;
39 break;
40 case 1: //Authentication,
41 dwKeySpec = AT_SIGNATURE;
42 PinId = 3;
43 break;
44 case 2: // Confidentiality,
45 dwKeySpec = AT_KEYEXCHANGE;
46 PinId = 4;
47 break;
48 default:
49 dwReturn = SCARD_E_UNEXPECTED;
50 __leave;
51 }
52 dwReturn = pCardData->pfnCardCreateContainerEx(pCardData, (BYTE) dwIndex,
53 CARD_CREATE_CONTAINER_KEY_GEN,
54 dwKeySpec, 1024, NULL, PinId);
55 }
56 __finally
57 {
58 }
59 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)
72 {
73 DWORD dwReturn, dwKeySpec;
74 PIN_ID PinId;
75 HCRYPTPROV hProv = NULL;
76 HCRYPTKEY hKey = NULL;
77 TCHAR szContainerName[] = OPENPGP_TEST_CONTAINER;
78 BYTE pbData[4096];
79 BYTE pbBlobRef[4096];
80 DWORD dwDataSize = ARRAYSIZE(pbData);
81 DWORD dwBlobRefSize = ARRAYSIZE(pbBlobRef);
82 BOOL bStatus;
83 CONTAINER_INFO ContainerInfo;
84 PRSAPUBLICKEYBLOB pBlob, pBlobRef;
85 DWORD dwAglLen, dwSize;
86 __try
87 {
88 if (!pCardData)
89 {
90 dwReturn = SCARD_E_COMM_DATA_LOST;
91 __leave;
92 }
93 switch(dwIndex)
94 {
95 case 0: //Signature,
96 dwKeySpec = AT_SIGNATURE;
97 PinId = ROLE_USER;
98 break;
99 case 1: //Authentication,
100 dwKeySpec = AT_SIGNATURE;
101 PinId = 3;
102 break;
103 case 2: // Confidentiality,
104 dwKeySpec = AT_KEYEXCHANGE;
105 PinId = 4;
106 break;
107 default:
108 dwReturn = SCARD_E_UNEXPECTED;
109 __leave;
110 }
111 bStatus = CryptAcquireContext(&hProv, szContainerName, MS_ENHANCED_PROV, PROV_RSA_FULL, 0);
112 if (!bStatus)
113 {
114 dwReturn = GetLastError();
115 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, dwKeySpec, CRYPT_EXPORTABLE, &hKey);
126 if (!bStatus)
127 {
128 dwReturn = GetLastError();
129 __leave;
130 }
131 bStatus = CryptExportKey(hKey, NULL, PRIVATEKEYBLOB, 0, pbData, &dwDataSize);
132 if (!bStatus)
133 {
134 dwReturn = GetLastError();
135 __leave;
136 }
137 dwSize = sizeof(DWORD);
138 bStatus = CryptGetKeyParam(hKey, KP_KEYLEN, (PBYTE) &dwAglLen,&dwSize , 0);
139 dwReturn = pCardData->pfnCardCreateContainerEx(pCardData, (BYTE) dwIndex,
140 CARD_CREATE_CONTAINER_KEY_IMPORT,
141 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
174 {
175 if (hKey)
176 CryptDestroyKey(hKey);
177 //CryptAcquireContext(&hProv, szContainerName, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_DELETEKEYSET);
178 if (hProv)
179 CryptReleaseContext(hProv,0);
180 }
181 return dwReturn;
182 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26