Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1095 lines
30 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1998 - 1999
  3. Module Name:
  4. autoreg
  5. Abstract:
  6. This module provides autoregistration capabilities to a CSP. It allows
  7. regsvr32 to call the DLL directly to add and remove Registry settings.
  8. Author:
  9. Doug Barlow (dbarlow) 3/11/1998
  10. Update for gemplus PY Roy 22/03/00
  11. Environment:
  12. Win32
  13. Notes:
  14. Look for "?vendor?" tags and edit appropriately.
  15. --*/
  16. #ifdef _UNICODE
  17. #define UNICODE
  18. #endif
  19. #ifndef WIN32_LEAN_AND_MEAN
  20. #define WIN32_LEAN_AND_MEAN
  21. #endif
  22. #ifdef _AFXDLL
  23. #include "stdafx.h"
  24. #else
  25. #include <windows.h>
  26. #endif
  27. #include <wincrypt.h>
  28. #include <winscard.h>
  29. #include <tchar.h>
  30. #include <malloc.h>
  31. #include <cspdk.h>
  32. struct CardInfo
  33. {
  34. PTCHAR szCardName;
  35. int lenATR;
  36. const BYTE* ATR;
  37. const BYTE* ATRMask;
  38. };
  39. ///////////////////////////////////////////////////////////////////////////////////////////
  40. //
  41. // Constants
  42. //
  43. ///////////////////////////////////////////////////////////////////////////////////////////
  44. static const TCHAR l_szProviderName[]
  45. #ifdef MS_BUILD
  46. = TEXT("Gemplus GemSAFE Card CSP v1.0");
  47. #else
  48. = TEXT("Gemplus GemSAFE Card CSP");
  49. #endif
  50. // = TEXT("?vendor? <Add your Provider Name Here>");
  51. const BYTE c_GPK4000ATR[] = { 0x3B, 0x27, 0x00, 0x80, 0x65, 0xA2, 0x04, 0x01, 0x01, 0x37 };
  52. const BYTE c_GPK4000ATRMask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xE5, 0xFF, 0xFF, 0xFF };
  53. const BYTE c_GPK8000ATR[] = { 0x3B, 0xA7, 0x00, 0x40, 0x00, 0x80, 0x65, 0xA2, 0x08, 0x00, 0x00, 0x00 };
  54. const BYTE c_GPK8000ATRMask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00 };
  55. // l_rgbATR[] = { ?vendor? <Add your ATR here> },
  56. // l_rgbATRMask[] = { ?vendor? <Add your ATR Mask here> };
  57. CardInfo c_cards[] =
  58. {
  59. { TEXT("GemSAFE Smart Card (4K)"), sizeof(c_GPK4000ATR), c_GPK4000ATR, c_GPK4000ATRMask },
  60. { TEXT("GemSAFE Smart Card (8K)"), sizeof(c_GPK8000ATR), c_GPK8000ATR, c_GPK8000ATRMask }
  61. };
  62. // = ( TEXT("?vendor? <Add your Smart Card Friendly Name Here>");
  63. static HMODULE
  64. GetInstanceHandle(
  65. void);
  66. static const DWORD
  67. l_dwCspType
  68. // ?vendor? Change this to match your CSP capabilities
  69. = PROV_RSA_FULL;
  70. typedef DWORD
  71. (__stdcall *LPSETCARDTYPEPROVIDERNAME)(
  72. IN SCARDCONTEXT hContext,
  73. IN LPCTSTR szCardName,
  74. IN DWORD dwProviderId,
  75. IN LPCTSTR szProvider);
  76. ///////////////////////////////////////////////////////////////////////////////////////////
  77. //
  78. // IntroduceCard
  79. //
  80. // Introduce the vendor card. Try various techniques until one works.
  81. //
  82. //
  83. ///////////////////////////////////////////////////////////////////////////////////////////
  84. namespace
  85. {
  86. HRESULT ForgetCard(const PTCHAR szCardName)
  87. {
  88. bool bCardForgeted = false;
  89. HRESULT hReturnStatus = NO_ERROR;
  90. // Try different methods until one works
  91. for ( int method = 0; !bCardForgeted; ++method )
  92. {
  93. switch (method)
  94. {
  95. case 0:
  96. {
  97. SCARDCONTEXT hContext = 0;
  98. DWORD dwStatus;
  99. dwStatus = SCardEstablishContext(SCARD_SCOPE_SYSTEM, 0, 0, &hContext);
  100. if (ERROR_SUCCESS != dwStatus)
  101. continue;
  102. dwStatus = SCardForgetCardType(hContext, szCardName);
  103. if (dwStatus != ERROR_SUCCESS && dwStatus != ERROR_FILE_NOT_FOUND)
  104. {
  105. if (0 == (dwStatus & 0xffff0000))
  106. hReturnStatus = HRESULT_FROM_WIN32(dwStatus);
  107. else
  108. hReturnStatus = (HRESULT)dwStatus;
  109. return hReturnStatus;
  110. }
  111. dwStatus = SCardReleaseContext(hContext);
  112. hContext = 0;
  113. if (dwStatus != ERROR_SUCCESS)
  114. {
  115. if (0 == (dwStatus & 0xffff0000))
  116. hReturnStatus = HRESULT_FROM_WIN32(dwStatus);
  117. else
  118. hReturnStatus = (HRESULT)dwStatus;
  119. return hReturnStatus;
  120. }
  121. bCardForgeted = true;
  122. }
  123. break;
  124. case 1:
  125. {
  126. HKEY hCalais = NULL;
  127. DWORD dwDisp;
  128. LONG nStatus;
  129. nStatus = RegCreateKeyEx(HKEY_LOCAL_MACHINE,
  130. TEXT("SOFTWARE\\Microsoft\\Cryptography\\Calais\\SmartCards"),
  131. 0,
  132. TEXT(""),
  133. REG_OPTION_NON_VOLATILE,
  134. KEY_ALL_ACCESS,
  135. NULL,
  136. &hCalais,
  137. &dwDisp);
  138. if (ERROR_SUCCESS != nStatus)
  139. continue;
  140. nStatus = RegDeleteKey(hCalais,
  141. szCardName);
  142. if (nStatus != ERROR_SUCCESS && nStatus != ERROR_FILE_NOT_FOUND)
  143. {
  144. hReturnStatus = HRESULT_FROM_WIN32(nStatus);
  145. if (NULL != hCalais)
  146. RegCloseKey(hCalais);
  147. return hReturnStatus;
  148. }
  149. nStatus = RegCloseKey(hCalais);
  150. hCalais = NULL;
  151. if (ERROR_SUCCESS != nStatus)
  152. {
  153. hReturnStatus = HRESULT_FROM_WIN32(nStatus);
  154. return hReturnStatus;
  155. }
  156. bCardForgeted = true;
  157. }
  158. break;
  159. default:
  160. hReturnStatus = ERROR_ACCESS_DENIED;
  161. return hReturnStatus;
  162. }
  163. }
  164. return hReturnStatus;
  165. }
  166. HRESULT IntroduceCard( const PTCHAR szCardName, int lenATR, const BYTE* ATR, const BYTE* ATRMask )
  167. {
  168. bool bCardIntroduced = false;
  169. HRESULT hReturnStatus = NO_ERROR;
  170. HKEY hCalais = NULL;
  171. HKEY hVendor = NULL;
  172. DWORD dwDisp;
  173. LONG nStatus;
  174. // Try different methods until one works
  175. for ( int method = 0; !bCardIntroduced; ++method )
  176. {
  177. switch (method)
  178. {
  179. case 0:
  180. {
  181. HINSTANCE hWinSCard = 0;
  182. LPSETCARDTYPEPROVIDERNAME pfSetCardTypeProviderName = 0;
  183. DWORD dwStatus;
  184. hWinSCard = GetModuleHandle(TEXT("WinSCard.DLL"));
  185. if (hWinSCard==0)
  186. continue;
  187. #if defined(UNICODE)
  188. pfSetCardTypeProviderName = (LPSETCARDTYPEPROVIDERNAME)GetProcAddress( hWinSCard, "SCardSetCardTypeProviderNameW"); //TEXT("SCardSetCardTypeProviderNameW") );
  189. #else
  190. pfSetCardTypeProviderName = (LPSETCARDTYPEPROVIDERNAME)GetProcAddress( hWinSCard, "SCardSetCardTypeProviderNameA");
  191. #endif
  192. if (pfSetCardTypeProviderName==0)
  193. continue;
  194. dwStatus = SCardIntroduceCardType( 0, szCardName, 0, 0, 0, ATR, ATRMask, lenATR );
  195. if (dwStatus != ERROR_SUCCESS && dwStatus != ERROR_ALREADY_EXISTS)
  196. continue;
  197. dwStatus = (*pfSetCardTypeProviderName)( 0, szCardName, SCARD_PROVIDER_CSP, l_szProviderName );
  198. if (dwStatus != ERROR_SUCCESS)
  199. {
  200. if (0 == (dwStatus & 0xffff0000))
  201. hReturnStatus = HRESULT_FROM_WIN32(dwStatus);
  202. else
  203. hReturnStatus = (HRESULT)dwStatus;
  204. return hReturnStatus;
  205. }
  206. bCardIntroduced = true;
  207. }
  208. break;
  209. case 1:
  210. {
  211. SCARDCONTEXT hContext = 0;
  212. DWORD dwStatus;
  213. dwStatus = SCardEstablishContext( SCARD_SCOPE_SYSTEM, 0, 0, &hContext );
  214. if (ERROR_SUCCESS != dwStatus)
  215. continue;
  216. dwStatus = SCardIntroduceCardType( hContext, szCardName, 0, 0, 0, ATR, ATRMask, lenATR );
  217. if (dwStatus != ERROR_SUCCESS && dwStatus != ERROR_ALREADY_EXISTS)
  218. {
  219. if (0 == (dwStatus & 0xffff0000))
  220. hReturnStatus = HRESULT_FROM_WIN32(dwStatus);
  221. else
  222. hReturnStatus = (HRESULT)dwStatus;
  223. return hReturnStatus;
  224. }
  225. dwStatus = SCardReleaseContext(hContext);
  226. hContext = 0;
  227. if (dwStatus != ERROR_SUCCESS)
  228. {
  229. if (0 == (dwStatus & 0xffff0000))
  230. hReturnStatus = HRESULT_FROM_WIN32(dwStatus);
  231. else
  232. hReturnStatus = (HRESULT)dwStatus;
  233. return hReturnStatus;
  234. }
  235. nStatus = RegCreateKeyEx(
  236. HKEY_LOCAL_MACHINE,
  237. TEXT("SOFTWARE\\Microsoft\\Cryptography\\Calais\\SmartCards"),
  238. 0,
  239. TEXT(""),
  240. REG_OPTION_NON_VOLATILE,
  241. KEY_ALL_ACCESS,
  242. NULL,
  243. &hCalais,
  244. &dwDisp);
  245. if (ERROR_SUCCESS != nStatus)
  246. {
  247. hReturnStatus = HRESULT_FROM_WIN32(nStatus);
  248. if (NULL != hCalais)
  249. RegCloseKey(hCalais);
  250. return hReturnStatus;
  251. }
  252. nStatus = RegCreateKeyEx(
  253. hCalais,
  254. szCardName,
  255. 0,
  256. TEXT(""),
  257. REG_OPTION_NON_VOLATILE,
  258. KEY_ALL_ACCESS,
  259. NULL,
  260. &hVendor,
  261. &dwDisp);
  262. if (ERROR_SUCCESS != nStatus)
  263. {
  264. hReturnStatus = HRESULT_FROM_WIN32(nStatus);
  265. if (NULL != hCalais)
  266. RegCloseKey(hCalais);
  267. if (NULL != hVendor)
  268. RegCloseKey(hVendor);
  269. return hReturnStatus;
  270. }
  271. nStatus = RegCloseKey(hCalais);
  272. hCalais = NULL;
  273. if (ERROR_SUCCESS != nStatus)
  274. {
  275. hReturnStatus = HRESULT_FROM_WIN32(nStatus);
  276. if (NULL != hVendor)
  277. RegCloseKey(hVendor);
  278. return hReturnStatus;
  279. }
  280. nStatus = RegSetValueEx(
  281. hVendor,
  282. TEXT("Crypto Provider"),
  283. 0,
  284. REG_SZ,
  285. (LPBYTE)l_szProviderName,
  286. (_tcslen(l_szProviderName) + 1) * sizeof(TCHAR));
  287. if (ERROR_SUCCESS != nStatus)
  288. {
  289. hReturnStatus = HRESULT_FROM_WIN32(nStatus);
  290. if (NULL != hVendor)
  291. RegCloseKey(hVendor);
  292. return hReturnStatus;
  293. }
  294. nStatus = RegCloseKey(hVendor);
  295. hVendor = NULL;
  296. if (ERROR_SUCCESS != nStatus)
  297. {
  298. hReturnStatus = HRESULT_FROM_WIN32(nStatus);
  299. return hReturnStatus;
  300. }
  301. bCardIntroduced = true;
  302. }
  303. break;
  304. case 2:
  305. nStatus = RegCreateKeyEx(
  306. HKEY_LOCAL_MACHINE,
  307. TEXT("SOFTWARE\\Microsoft\\Cryptography\\Calais\\SmartCards"),
  308. 0,
  309. TEXT(""),
  310. REG_OPTION_NON_VOLATILE,
  311. KEY_ALL_ACCESS,
  312. NULL,
  313. &hCalais,
  314. &dwDisp);
  315. if (ERROR_SUCCESS != nStatus)
  316. continue;
  317. nStatus = RegCreateKeyEx(
  318. hCalais,
  319. szCardName,
  320. 0,
  321. TEXT(""),
  322. REG_OPTION_NON_VOLATILE,
  323. KEY_ALL_ACCESS,
  324. NULL,
  325. &hVendor,
  326. &dwDisp);
  327. if (ERROR_SUCCESS != nStatus)
  328. {
  329. hReturnStatus = HRESULT_FROM_WIN32(nStatus);
  330. if (NULL != hCalais)
  331. RegCloseKey(hCalais);
  332. if (NULL != hVendor)
  333. RegCloseKey(hVendor);
  334. return hReturnStatus;
  335. }
  336. nStatus = RegCloseKey(hCalais);
  337. hCalais = NULL;
  338. if (ERROR_SUCCESS != nStatus)
  339. {
  340. hReturnStatus = HRESULT_FROM_WIN32(nStatus);
  341. if (NULL != hVendor)
  342. RegCloseKey(hVendor);
  343. return hReturnStatus;
  344. }
  345. /*
  346. nStatus = RegSetValueEx(
  347. hVendor,
  348. TEXT("Primary Provider"),
  349. 0,
  350. REG_BINARY,
  351. (LPCBYTE)&l_guidPrimaryProv,
  352. sizeof(l_guidPrimaryProv));
  353. if (ERROR_SUCCESS != nStatus)
  354. {
  355. hReturnStatus = HRESULT_FROM_WIN32(nStatus);
  356. if (NULL != hVendor)
  357. RegCloseKey(hVendor);
  358. return hReturnStatus;
  359. }
  360. */
  361. nStatus = RegSetValueEx(
  362. hVendor,
  363. TEXT("ATR"),
  364. 0,
  365. REG_BINARY,
  366. ATR,
  367. lenATR);
  368. if (ERROR_SUCCESS != nStatus)
  369. {
  370. hReturnStatus = HRESULT_FROM_WIN32(nStatus);
  371. if (NULL != hVendor)
  372. RegCloseKey(hVendor);
  373. return hReturnStatus;
  374. }
  375. nStatus = RegSetValueEx(
  376. hVendor,
  377. TEXT("ATRMask"),
  378. 0,
  379. REG_BINARY,
  380. ATRMask,
  381. lenATR);
  382. if (ERROR_SUCCESS != nStatus)
  383. {
  384. hReturnStatus = HRESULT_FROM_WIN32(nStatus);
  385. if (NULL != hVendor)
  386. RegCloseKey(hVendor);
  387. return hReturnStatus;
  388. }
  389. nStatus = RegSetValueEx(
  390. hVendor,
  391. TEXT("Crypto Provider"),
  392. 0,
  393. REG_SZ,
  394. (LPBYTE)l_szProviderName,
  395. (_tcslen(l_szProviderName) + 1) * sizeof(TCHAR));
  396. if (ERROR_SUCCESS != nStatus)
  397. {
  398. hReturnStatus = HRESULT_FROM_WIN32(nStatus);
  399. if (NULL != hVendor)
  400. RegCloseKey(hVendor);
  401. return hReturnStatus;
  402. }
  403. nStatus = RegCloseKey(hVendor);
  404. hVendor = NULL;
  405. if (ERROR_SUCCESS != nStatus)
  406. {
  407. hReturnStatus = HRESULT_FROM_WIN32(nStatus);
  408. return hReturnStatus;
  409. }
  410. bCardIntroduced = TRUE;
  411. break;
  412. default:
  413. hReturnStatus = ERROR_ACCESS_DENIED;
  414. return hReturnStatus;
  415. }
  416. }
  417. return hReturnStatus;
  418. }
  419. }
  420. /*++
  421. DllUnregisterServer:
  422. This service removes the registry entries associated with this CSP.
  423. Arguments:
  424. None
  425. Return Value:
  426. Status code as an HRESULT.
  427. Author:
  428. Doug Barlow (dbarlow) 3/11/1998
  429. --*/
  430. STDAPI
  431. DllUnregisterServer(
  432. void)
  433. {
  434. LONG nStatus;
  435. DWORD dwDisp;
  436. HRESULT hReturnStatus = NO_ERROR;
  437. HKEY hProviders = NULL;
  438. SCARDCONTEXT hCtx = NULL;
  439. #ifdef _AFXDLL
  440. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  441. #endif
  442. //
  443. // Delete the Registry key for this CSP.
  444. //
  445. nStatus = RegCreateKeyEx(
  446. HKEY_LOCAL_MACHINE,
  447. TEXT("SOFTWARE\\Microsoft\\Cryptography\\Defaults\\Provider"),
  448. 0,
  449. TEXT(""),
  450. REG_OPTION_NON_VOLATILE,
  451. KEY_ALL_ACCESS,
  452. NULL,
  453. &hProviders,
  454. &dwDisp);
  455. if (ERROR_SUCCESS == nStatus)
  456. {
  457. RegDeleteKey(hProviders, l_szProviderName);
  458. RegCloseKey(hProviders);
  459. hProviders = NULL;
  460. }
  461. //
  462. // Forget the card type.
  463. //
  464. //hCtx = NULL;
  465. //SCardEstablishContext(SCARD_SCOPE_SYSTEM, 0, 0, &hCtx);
  466. //if (NULL != hCtx)
  467. //{
  468. // int nbCard = sizeof(c_cards) / sizeof(c_cards[0]);
  469. // for (int i = 0; i < nbCard; ++i)
  470. // {
  471. // SCardForgetCardType( hCtx, c_cards[i].szCardName );
  472. // }
  473. //}
  474. //if (NULL != hCtx)
  475. //{
  476. // SCardReleaseContext(hCtx);
  477. // hCtx = NULL;
  478. //}
  479. int nbCard = sizeof(c_cards) / sizeof(c_cards[0]);
  480. int i = 0;
  481. while ( (i < nbCard) && (hReturnStatus == NO_ERROR) )
  482. {
  483. hReturnStatus &= ForgetCard(c_cards[i].szCardName);
  484. i++;
  485. }
  486. //
  487. // ?vendor?
  488. // Delete vendor specific registry entries.
  489. //
  490. RegDeleteKey(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Gemplus"));
  491. //
  492. // All done!
  493. //
  494. return hReturnStatus;
  495. }
  496. /*++
  497. DllRegisterServer:
  498. This function installs the proper registry entries to enable this CSP.
  499. Arguments:
  500. None
  501. Return Value:
  502. Status code as an HRESULT.
  503. Author:
  504. Doug Barlow (dbarlow) 3/11/1998
  505. --*/
  506. STDAPI
  507. DllRegisterServer(
  508. void)
  509. {
  510. TCHAR szModulePath[MAX_PATH+1];
  511. BYTE pbSignature[136]; // Room for a 1024 bit signature, with padding.
  512. OSVERSIONINFO osVer;
  513. LPTSTR szFileName, szFileExt;
  514. HINSTANCE hThisDll;
  515. HRSRC hSigResource;
  516. DWORD dwStatus;
  517. LONG nStatus;
  518. BOOL fStatus;
  519. DWORD dwDisp;
  520. DWORD dwIndex;
  521. DWORD dwSigLength;
  522. HRESULT hReturnStatus = NO_ERROR;
  523. HKEY hProviders = NULL;
  524. HKEY hMyCsp = NULL;
  525. BOOL fSignatureFound = FALSE;
  526. HANDLE hSigFile = INVALID_HANDLE_VALUE;
  527. SCARDCONTEXT hCtx = NULL;
  528. HKEY hGpk = NULL;
  529. #ifdef _AFXDLL
  530. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  531. #endif
  532. //
  533. // Figure out the file name and path.
  534. //
  535. hThisDll = GetInstanceHandle();
  536. if (NULL == hThisDll)
  537. {
  538. hReturnStatus = HRESULT_FROM_WIN32(ERROR_INVALID_HANDLE);
  539. goto ErrorExit;
  540. }
  541. dwStatus = GetModuleFileName(
  542. hThisDll,
  543. szModulePath,
  544. sizeof(szModulePath) / sizeof(TCHAR)-1);
  545. szModulePath[dwStatus]=0;
  546. if (0 == dwStatus)
  547. {
  548. hReturnStatus = HRESULT_FROM_WIN32(GetLastError());
  549. goto ErrorExit;
  550. }
  551. szFileName = _tcsrchr(szModulePath, TEXT('\\'));
  552. if (NULL == szFileName)
  553. szFileName = szModulePath;
  554. else
  555. szFileName += 1;
  556. szFileExt = _tcsrchr(szFileName, TEXT('.'));
  557. if ((NULL == szFileExt) || (_tcslen(szFileExt)<4))
  558. {
  559. hReturnStatus = HRESULT_FROM_WIN32(ERROR_INVALID_NAME);
  560. goto ErrorExit;
  561. }
  562. else
  563. szFileExt += 1;
  564. //
  565. // Create the Registry key for this CSP.
  566. //
  567. nStatus = RegCreateKeyEx(
  568. HKEY_LOCAL_MACHINE,
  569. TEXT("SOFTWARE\\Microsoft\\Cryptography\\Defaults\\Provider"),
  570. 0,
  571. TEXT(""),
  572. REG_OPTION_NON_VOLATILE,
  573. KEY_ALL_ACCESS,
  574. NULL,
  575. &hProviders,
  576. &dwDisp);
  577. if (ERROR_SUCCESS != nStatus)
  578. {
  579. hReturnStatus = HRESULT_FROM_WIN32(nStatus);
  580. goto ErrorExit;
  581. }
  582. nStatus = RegCreateKeyEx(
  583. hProviders,
  584. l_szProviderName,
  585. 0,
  586. TEXT(""),
  587. REG_OPTION_NON_VOLATILE,
  588. KEY_ALL_ACCESS,
  589. NULL,
  590. &hMyCsp,
  591. &dwDisp);
  592. if (ERROR_SUCCESS != nStatus)
  593. {
  594. hReturnStatus = HRESULT_FROM_WIN32(nStatus);
  595. goto ErrorExit;
  596. }
  597. nStatus = RegCloseKey(hProviders);
  598. hProviders = NULL;
  599. if (ERROR_SUCCESS != nStatus)
  600. {
  601. hReturnStatus = HRESULT_FROM_WIN32(nStatus);
  602. goto ErrorExit;
  603. }
  604. //
  605. // Install the trivial registry values.
  606. //
  607. nStatus = RegSetValueEx(
  608. hMyCsp,
  609. TEXT("Image Path"),
  610. 0,
  611. REG_SZ,
  612. (LPBYTE)szFileName,
  613. (_tcslen(szFileName) + 1) * sizeof(TCHAR));
  614. if (ERROR_SUCCESS != nStatus)
  615. {
  616. hReturnStatus = HRESULT_FROM_WIN32(nStatus);
  617. goto ErrorExit;
  618. }
  619. nStatus = RegSetValueEx(
  620. hMyCsp,
  621. TEXT("Type"),
  622. 0,
  623. REG_DWORD,
  624. (LPBYTE)&l_dwCspType,
  625. sizeof(DWORD));
  626. if (ERROR_SUCCESS != nStatus)
  627. {
  628. hReturnStatus = HRESULT_FROM_WIN32(nStatus);
  629. goto ErrorExit;
  630. }
  631. //
  632. // See if we're self-signed. On NT5, CSP images can carry their own
  633. // signatures.
  634. //
  635. hSigResource = FindResource(
  636. hThisDll,
  637. MAKEINTRESOURCE(CRYPT_SIG_RESOURCE_NUMBER),
  638. RT_RCDATA);
  639. //
  640. // Install the file signature.
  641. //
  642. ZeroMemory(&osVer, sizeof(OSVERSIONINFO));
  643. osVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  644. fStatus = GetVersionEx(&osVer);
  645. if (fStatus
  646. && (VER_PLATFORM_WIN32_NT == osVer.dwPlatformId)
  647. && (5 <= osVer.dwMajorVersion)
  648. && (NULL != hSigResource))
  649. {
  650. //
  651. // Signature in file flag is sufficient.
  652. //
  653. dwStatus = 0;
  654. nStatus = RegSetValueEx(
  655. hMyCsp,
  656. TEXT("SigInFile"),
  657. 0,
  658. REG_DWORD,
  659. (LPBYTE)&dwStatus,
  660. sizeof(DWORD));
  661. if (ERROR_SUCCESS != nStatus)
  662. {
  663. hReturnStatus = HRESULT_FROM_WIN32(nStatus);
  664. goto ErrorExit;
  665. }
  666. }
  667. else
  668. {
  669. //
  670. // We have to install a signature entry.
  671. // Try various techniques until one works.
  672. //
  673. for (dwIndex = 0; !fSignatureFound; dwIndex += 1)
  674. {
  675. switch (dwIndex)
  676. {
  677. //
  678. // Look for an external *.sig file and load that into the registry.
  679. //
  680. case 0:
  681. _tcscpy(szFileExt, TEXT("sig"));
  682. hSigFile = CreateFile(
  683. szModulePath,
  684. GENERIC_READ,
  685. FILE_SHARE_READ,
  686. NULL,
  687. OPEN_EXISTING,
  688. FILE_ATTRIBUTE_NORMAL,
  689. NULL);
  690. if (INVALID_HANDLE_VALUE == hSigFile)
  691. continue;
  692. dwSigLength = GetFileSize(hSigFile, NULL);
  693. if ((dwSigLength > sizeof(pbSignature))
  694. || (dwSigLength < 72)) // Accept a 512-bit signature
  695. {
  696. hReturnStatus = NTE_BAD_SIGNATURE;
  697. goto ErrorExit;
  698. }
  699. fStatus = ReadFile(
  700. hSigFile,
  701. pbSignature,
  702. sizeof(pbSignature),
  703. &dwSigLength,
  704. NULL);
  705. if (!fStatus)
  706. {
  707. hReturnStatus = HRESULT_FROM_WIN32(GetLastError());
  708. goto ErrorExit;
  709. }
  710. fStatus = CloseHandle(hSigFile);
  711. hSigFile = NULL;
  712. if (!fStatus)
  713. {
  714. hReturnStatus = HRESULT_FROM_WIN32(GetLastError());
  715. goto ErrorExit;
  716. }
  717. fSignatureFound = TRUE;
  718. break;
  719. //
  720. // Other cases may be added in the future.
  721. //
  722. default:
  723. hReturnStatus = NTE_BAD_SIGNATURE;
  724. goto ErrorExit;
  725. }
  726. if (fSignatureFound)
  727. {
  728. for (dwIndex = 0; dwIndex < dwSigLength; dwIndex += 1)
  729. {
  730. if (0 != pbSignature[dwIndex])
  731. break;
  732. }
  733. if (dwIndex >= dwSigLength)
  734. fSignatureFound = FALSE;
  735. }
  736. }
  737. //
  738. // We've found a signature somewhere! Install it.
  739. //
  740. nStatus = RegSetValueEx(
  741. hMyCsp,
  742. TEXT("Signature"),
  743. 0,
  744. REG_BINARY,
  745. pbSignature,
  746. dwSigLength);
  747. if (ERROR_SUCCESS != nStatus)
  748. {
  749. hReturnStatus = HRESULT_FROM_WIN32(nStatus);
  750. goto ErrorExit;
  751. }
  752. }
  753. nStatus = RegCloseKey(hMyCsp);
  754. hMyCsp = NULL;
  755. if (ERROR_SUCCESS != nStatus)
  756. {
  757. hReturnStatus = HRESULT_FROM_WIN32(nStatus);
  758. goto ErrorExit;
  759. }
  760. //
  761. // Introduce the vendor card. Try various techniques until one works.
  762. //
  763. {
  764. int nbCard = sizeof(c_cards) / sizeof(c_cards[0]);
  765. int i = 0;
  766. while ( (i < nbCard) && (hReturnStatus == NO_ERROR) )
  767. {
  768. hReturnStatus &= IntroduceCard( c_cards[i].szCardName, c_cards[i].lenATR, c_cards[i].ATR, c_cards[i].ATRMask );
  769. i++;
  770. }
  771. }
  772. if (hReturnStatus != NO_ERROR)
  773. goto ErrorExit;
  774. //
  775. // ?vendor?
  776. // Add any additional initialization required here.
  777. //
  778. // Add the GemSAFE card list and Dictionary name
  779. nStatus = RegCreateKeyEx(
  780. HKEY_LOCAL_MACHINE,
  781. TEXT("SOFTWARE\\Gemplus\\Cryptography\\SmartCards\\GemSAFE"),
  782. 0,
  783. TEXT(""),
  784. REG_OPTION_NON_VOLATILE,
  785. KEY_ALL_ACCESS,
  786. NULL,
  787. &hGpk,
  788. &dwDisp);
  789. if (ERROR_SUCCESS != nStatus)
  790. {
  791. hReturnStatus = HRESULT_FROM_WIN32(nStatus);
  792. goto ErrorExit;
  793. }
  794. // prepare the card list
  795. {
  796. int nbCard = sizeof(c_cards) / sizeof(c_cards[0]);
  797. int i;
  798. int sizeofCardList = 0;
  799. int CardListLen = 0;
  800. BYTE *pmszCardList ;
  801. BYTE *ptmpCardList ;
  802. for ( i = 0; i < nbCard; ++i)
  803. {
  804. sizeofCardList += (_tcslen(c_cards[i].szCardName) + 1);
  805. }
  806. sizeofCardList++;
  807. pmszCardList = (BYTE *) malloc(sizeofCardList);
  808. if(!pmszCardList)
  809. {
  810. hReturnStatus=HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY);
  811. goto ErrorExit;
  812. }
  813. ptmpCardList= pmszCardList;
  814. for ( i = 0; i < nbCard; ++i)
  815. {
  816. #ifndef UNICODE
  817. strcpy((char*)ptmpCardList, c_cards[i].szCardName);
  818. #else
  819. WideCharToMultiByte(CP_ACP, 0, c_cards[i].szCardName, -1, (char*)ptmpCardList, sizeofCardList - CardListLen, 0, 0);
  820. #endif
  821. CardListLen += _tcslen(c_cards[i].szCardName) + 1;
  822. ptmpCardList += _tcslen(c_cards[i].szCardName);
  823. *ptmpCardList = 0;
  824. ptmpCardList ++;
  825. }
  826. *ptmpCardList = 0;
  827. nStatus = RegSetValueEx(hGpk,
  828. TEXT("Card List"),
  829. 0,
  830. REG_BINARY,
  831. pmszCardList,
  832. sizeofCardList);
  833. free(pmszCardList);
  834. }
  835. if (ERROR_SUCCESS != nStatus)
  836. {
  837. hReturnStatus = HRESULT_FROM_WIN32(nStatus);
  838. RegCloseKey(hGpk);
  839. hGpk = NULL;
  840. goto ErrorExit;
  841. }
  842. nStatus = RegCloseKey(hGpk);
  843. hGpk = NULL;
  844. if (ERROR_SUCCESS != nStatus)
  845. {
  846. hReturnStatus = HRESULT_FROM_WIN32(nStatus);
  847. goto ErrorExit;
  848. }
  849. // Forget "GemSAFE card"
  850. //hCtx = NULL;
  851. //SCardEstablishContext(SCARD_SCOPE_SYSTEM, 0, 0, &hCtx);
  852. //if (NULL != hCtx)
  853. //{
  854. // SCardForgetCardType( hCtx, "GemSAFE" );
  855. //}
  856. //if (NULL != hCtx)
  857. //{
  858. // SCardReleaseContext(hCtx);
  859. // hCtx = NULL;
  860. //}
  861. hReturnStatus = ForgetCard(TEXT("GemSAFE"));
  862. if (hReturnStatus != NO_ERROR)
  863. goto ErrorExit;
  864. //
  865. // All done!
  866. //
  867. return hReturnStatus;
  868. //
  869. // An error was detected. Clean up any outstanding resources and
  870. // return the error.
  871. //
  872. ErrorExit:
  873. if (NULL != hGpk)
  874. RegCloseKey(hGpk);
  875. if (NULL != hCtx)
  876. SCardReleaseContext(hCtx);
  877. if (INVALID_HANDLE_VALUE != hSigFile)
  878. CloseHandle(hSigFile);
  879. if (NULL != hMyCsp)
  880. RegCloseKey(hMyCsp);
  881. if (NULL != hProviders)
  882. RegCloseKey(hProviders);
  883. DllUnregisterServer();
  884. return hReturnStatus;
  885. }
  886. /*++
  887. GetInstanceHandle:
  888. This routine is CSP dependant. It returns the DLL instance handle. This
  889. is typically provided by the DllMain routine and stored in a global
  890. location.
  891. Arguments:
  892. None
  893. Return Value:
  894. The DLL Instance handle provided to the DLL when DllMain was called.
  895. Author:
  896. Doug Barlow (dbarlow) 3/11/1998
  897. --*/
  898. extern "C" HINSTANCE g_hInstMod;
  899. static HINSTANCE
  900. GetInstanceHandle(
  901. void)
  902. {
  903. #ifdef _AFXDLL
  904. return AfxGetInstanceHandle();
  905. #else
  906. return g_hInstMod;
  907. #endif
  908. }