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.

2952 lines
95 KiB

  1. //--------------------------------------------------------------
  2. // Copyright (C) Microsoft Corporation, 1996 - 1999
  3. //
  4. // File: exthlpr.cpp
  5. //
  6. // Contents: Helper functions for cryptext.dll
  7. // 1. Registry Functions
  8. // 2. String Formatting Functions
  9. // 3. Exports for RunDll
  10. //
  11. // History: 16-09-1997 xiaohs created
  12. //
  13. //--------------------------------------------------------------
  14. #include "cryptext.h"
  15. #include "private.h"
  16. #include <ole2.h>
  17. #include "xenroll.h"
  18. #include "xenroll_i.c"
  19. #include "initguid.h"
  20. //*************************************************************************************
  21. //global data for registry entries
  22. //************************************************************************************
  23. MIME_REG_ENTRY rgRegEntry[]={
  24. L".cer", L"CERFile", 0,
  25. L"CERFile\\shell\\open\\command", L"\"%SystemRoot%\\system32\\rundll32.exe\" cryptext.dll,CryptExtOpenCER %1", 0,
  26. L"CERFile\\shell\\add\\command", L"\"%SystemRoot%\\system32\\rundll32.exe\" cryptext.dll,CryptExtAddCER %1", 0,
  27. L"CERFile\\shell\\add", (LPWSTR) 1, IDS_INSTALL_CERT,
  28. L"CERFile", NULL, IDS_CER_NAME,
  29. L".pfx", L"PFXFile", 0,
  30. L"PFXFile\\shell\\add\\command", L"\"%SystemRoot%\\system32\\rundll32.exe\" cryptext.dll,CryptExtAddPFX %1", 0,
  31. L"PFXFile\\shell\\add", (LPWSTR) 1, IDS_MENU_INSTALL_PFX,
  32. L"PFXFile", NULL, IDS_PFX_NAME,
  33. L".p12", L"PFXFile", 0,
  34. L".cat", L"CATFile", 0,
  35. L"CATFile\\shell\\open\\command", L"\"%SystemRoot%\\system32\\rundll32.exe\" cryptext.dll,CryptExtOpenCAT %1", 0,
  36. L"CATFile", NULL, IDS_CAT_NAME,
  37. L".crt", L"CERFile", 0,
  38. L".der", L"CERFile", 0,
  39. L".stl", L"STLFile", 0,
  40. L"STLFile\\shell\\open\\command", L"\"%SystemRoot%\\system32\\rundll32.exe\" cryptext.dll,CryptExtOpenCTL %1", 0,
  41. L"STLFile\\shell\\add\\command", L"\"%SystemRoot%\\system32\\rundll32.exe\" cryptext.dll,CryptExtAddCTL %1", 0,
  42. L"STLFile\\shell\\add", (LPWSTR) 1, IDS_INSTALL_STL,
  43. L"STLFile", NULL, IDS_STL_NAME,
  44. L".crl", L"CRLFile", 0,
  45. L"CRLFile\\shell\\open\\command", L"\"%SystemRoot%\\system32\\rundll32.exe\" cryptext.dll,CryptExtOpenCRL %1", 0,
  46. L"CRLFile\\shell\\add\\command", L"\"%SystemRoot%\\system32\\rundll32.exe\" cryptext.dll,CryptExtAddCRL %1", 0,
  47. L"CRLFile\\shell\\add", (LPWSTR) 1, IDS_INSTALL_CRL,
  48. L"CRLFile", NULL, IDS_CRL_NAME,
  49. L".spc", L"SPCFile", 0,
  50. L"SPCFile\\shell\\add\\command", L"\"%SystemRoot%\\system32\\rundll32.exe\" cryptext.dll,CryptExtAddSPC %1", 0,
  51. L"SPCFile\\shell\\add", (LPWSTR) 1, IDS_INSTALL_CERT,
  52. L"SPCFile", NULL, IDS_SPC_NAME,
  53. L".p7s", L"P7SFile", 0,
  54. L"P7SFile", NULL, IDS_P7S_NAME,
  55. L".p7b", L"SPCFile", 0,
  56. L".p7m", L"P7MFile", 0,
  57. L"P7MFile", NULL, IDS_P7M_NAME,
  58. L".p7r", L"SPCFile", 0,
  59. L"P7RFile\\shell\\open\\command", L"\"%SystemRoot%\\system32\\rundll32.exe\" cryptext.dll,CryptExtOpenP7R %1", 0,
  60. L"P7RFile\\shell\\add\\command", L"\"%SystemRoot%\\system32\\rundll32.exe\" cryptext.dll,CryptExtAddP7R %1", 0,
  61. L"P7RFile\\shell\\add", (LPWSTR) 1, IDS_INSTALL_CERT,
  62. L"P7RFile", NULL, IDS_P7R_NAME,
  63. L".sst", L"CertificateStoreFile", 0,
  64. L"CertificateStoreFile", NULL, IDS_SST_NAME,
  65. L".p10", L"P10File", 0,
  66. L"P10File", NULL, IDS_P10_NAME,
  67. L".pko", L"PKOFile", 0,
  68. L"PKOFile\\shellex\\ContextMenuHandlers", L"CryptoMenu", 0,
  69. L"PKOFile\\shellex\\PropertySheetHandlers", L"CryptoMenu", 0,
  70. L"PKOFile", NULL, IDS_PKO_NAME,
  71. };
  72. //the following registry entries.
  73. //it uses MMC.exe to display PKCS7 and Store files. MMC.exe
  74. //is only available on NT5 enviroment
  75. MIME_REG_ENTRY rgWINNT5RegEntry[]={
  76. L"SPCFile\\shell\\open\\command", L"\"%SystemRoot%\\system32\\rundll32.exe\" cryptext.dll,CryptExtOpenPKCS7 %1", 0,
  77. L"P7SFile\\shell\\open\\command", L"\"%SystemRoot%\\system32\\rundll32.exe\" cryptext.dll,CryptExtOpenPKCS7 %1", 0,
  78. L"CertificateStoreFile\\shell\\open\\command", L"\"%SystemRoot%\\system32\\rundll32.exe\" cryptext.dll,CryptExtOpenSTR %1", 0,
  79. };
  80. WIN95_REG_ENTRY rgWin95IconEntry[]={
  81. "CERFile\\DefaultIcon", "\\cryptui.dll,-3410",
  82. "PFXFile\\DefaultIcon", "\\cryptui.dll,-3425",
  83. "CATFile\\DefaultIcon", "\\cryptui.dll,-3418",
  84. "STLFile\\DefaultIcon", "\\cryptui.dll,-3413",
  85. "CRLFile\\DefaultIcon", "\\cryptui.dll,-3417",
  86. "P7RFile\\DefaultIcon", "\\cryptui.dll,-3410",
  87. "SPCFile\\DefaultIcon", "\\cryptui.dll,-3410",
  88. };
  89. MIME_REG_ENTRY rgIconEntry[]={
  90. L"CERFile\\DefaultIcon", L"%SystemRoot%\\System32\\cryptui.dll,-3410", 0,
  91. L"PFXFile\\DefaultIcon", L"%SystemRoot%\\System32\\cryptui.dll,-3425", 0,
  92. L"CATFile\\DefaultIcon", L"%SystemRoot%\\System32\\cryptui.dll,-3418", 0,
  93. L"STLFile\\DefaultIcon", L"%SystemRoot%\\System32\\cryptui.dll,-3413", 0,
  94. L"CRLFile\\DefaultIcon", L"%SystemRoot%\\System32\\cryptui.dll,-3417", 0,
  95. L"P7RFile\\DefaultIcon", L"%SystemRoot%\\System32\\cryptui.dll,-3410", 0,
  96. L"SPCFile\\DefaultIcon", L"%SystemRoot%\\System32\\cryptui.dll,-3410", 0,
  97. };
  98. MIME_GUID_ENTRY rgGuidEntry[]={
  99. &CLSID_CryptPKO,
  100. L"PKOFile\\shellex\\ContextMenuHandlers\\CryptoMenu",
  101. L"PKOFile\\shellex\\PropertySheetHandlers\\CrytoMenu",
  102. L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Shell Extensions\\Approved",
  103. IDS_PKO_EXT,
  104. &CLSID_CryptSig,
  105. NULL,
  106. L"*\\shellex\\PropertySheetHandlers\\CryptoSignMenu",
  107. L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Shell Extensions\\Approved",
  108. IDS_SIGN_EXT,
  109. };
  110. LPWSTR rgwszDelete[] = {
  111. L".cer",
  112. L"CERFile\\DefaultIcon",
  113. L"CERFile\\shell\\open\\command",
  114. L"CERFile\\shell\\add\\command",
  115. L"CERFile\\shell\\open",
  116. L"CERFile\\shell\\add",
  117. L"CERFile\\shell",
  118. L"CERFile",
  119. L".pfx",
  120. L".p12",
  121. L"PFXFile\\DefaultIcon",
  122. L"PFXFile\\shell\\add\\command",
  123. L"PFXFile\\shell\\add",
  124. L"PFXFile\\shell",
  125. L"PFXFile",
  126. L".cat",
  127. L"CATFile\\DefaultIcon",
  128. L"CATFile\\shell\\open\\command",
  129. L"CATFile\\shell\\open",
  130. L"CATFile\\shell",
  131. L"CATFile",
  132. L".crt",
  133. L".der",
  134. L".stl",
  135. L"STLFile\\DefaultIcon",
  136. L"STLFile\\shell\\open\\command",
  137. L"STLFile\\shell\\add\\command",
  138. L"STLFile\\shell\\open",
  139. L"STLFile\\shell\\add",
  140. L"STLFile\\shell",
  141. L"STLFile",
  142. L".crl",
  143. L"CRLFile\\DefaultIcon",
  144. L"CRLFile\\shell\\open\\command",
  145. L"CRLFile\\shell\\add\\command",
  146. L"CRLFile\\shell\\open",
  147. L"CRLFile\\shell\\add",
  148. L"CRLFile\\shell",
  149. L"CRLFile",
  150. L".spc",
  151. L"SPCFile\\DefaultIcon",
  152. L"SPCFile\\shell\\open\\command",
  153. L"SPCFile\\shell\\open",
  154. L"SPCFile\\shell\\add\\command",
  155. L"SPCFile\\shell\\add",
  156. L"SPCFile\\shell",
  157. L"SPCFile",
  158. L".p7s",
  159. L"P7SFile\\shell\\open\\command",
  160. L"P7SFile\\shell\\open",
  161. L"P7SFile\\shell",
  162. L"P7SFile",
  163. L".p7b",
  164. L".p7m",
  165. L"P7MFile",
  166. L".p7r",
  167. L"P7RFile\\DefaultIcon",
  168. L"P7RFile\\shell\\open\\command",
  169. L"P7RFile\\shell\\add\\command",
  170. L"P7RFile\\shell\\open",
  171. L"P7RFile\\shell\\add",
  172. L"P7RFile\\shell",
  173. L"P7RFile",
  174. L".sst",
  175. L"CertificateStoreFile\\shell\\open\\command",
  176. L"CertificateStoreFile\\shell\\open",
  177. L"CertificateStoreFile\\shell",
  178. L"CertificateStoreFile",
  179. L".p10",
  180. L"P10File",
  181. L".pko",
  182. L"PKOFile\\shellex\\ContextMenuHandlers",
  183. L"PKOFile\\shellex\\PropertySheetHandlers",
  184. L"PKOFile\\shellex",
  185. L"PKOFile",
  186. };
  187. LPWSTR g_CLSIDDefault[]={
  188. L"\\shellex\\MayChangeDefaultMenu",
  189. L"\\shellex",
  190. };
  191. //the following is the entries for the content type
  192. // For any extension, say ".foo", we need to do the following:
  193. //1. Under the HEKY_CLASSES_ROOT, under ".foo" key, add an entry of name "Content Type" and value "application/xxxxxxxx".
  194. //2. Under HKEY_CLASSES_ROOT\MIME\Database\Content Type, add a key of "application/xxxxxxxx", under which add an entry of name "Extension" and value ".foo".
  195. MIME_REG_ENTRY rgContentTypeEntry[]={
  196. L".der", L"application/pkix-cert", 0,
  197. L".der", L"application/x-x509-ca-cert", 0,
  198. L".crt", L"application/pkix-cert", 0,
  199. L".crt", L"application/x-x509-ca-cert", 0,
  200. L".cer", L"application/pkix-cert", 0,
  201. L".cer", L"application/x-x509-ca-cert", 0,
  202. L".spc", L"application/x-pkcs7-certificates", 0,
  203. L".p7b", L"application/x-pkcs7-certificates", 0,
  204. L".pfx", L"application/x-pkcs12", 0,
  205. L".p12", L"application/x-pkcs12", 0,
  206. L".stl", L"application/vnd.ms-pki.stl", 0,
  207. L".crl", L"application/pkix-crl", 0,
  208. L".p7r", L"application/x-pkcs7-certreqresp", 0,
  209. L".p10", L"application/pkcs10", 0,
  210. L".sst", L"application/vnd.ms-pki.certstore", 0,
  211. L".cat", L"application/vnd.ms-pki.seccat", 0,
  212. L".p7m", L"application/pkcs7-mime", 0,
  213. L".p7s", L"application/pkcs7-signature", 0,
  214. L".pko", L"application/vnd.ms-pki.pko", 0,
  215. };
  216. //The following entries need to be deleted at Regsvr32 time
  217. //due to the following changes from NT5 B2 to B3:
  218. //.ctl -> .stl
  219. //.str -> .sst
  220. //.p7b -> .p7c
  221. MIME_REG_ENTRY rgRemoveRelatedEntry[]={
  222. L"P7CFile\\shell\\open\\command", L"rundll32.exe cryptext.dll,CryptExtOpenPKCS7 %1", 0,
  223. L"P7CFile\\shell\\open", NULL, 0,
  224. L"CERTSTOREFile\\shell\\open\\command", L"rundll32.exe cryptext.dll,CryptExtOpenSTR %1", 0,
  225. L"CERTSTOREFile\\shell\\open", NULL, 0,
  226. L"CTLFile\\shell\\open\\command", L"rundll32.exe cryptext.dll,CryptExtOpenCTL %1", 0,
  227. L"CTLFile\\shell\\open", NULL, 0,
  228. L"CTLFile\\shell\\add\\command", L"rundll32.exe cryptext.dll,CryptExtAddCTL %1", 0,
  229. L"CTLFile\\shell\\add", NULL, 0,
  230. };
  231. MIME_REG_ENTRY rgRemoveEmptyEntry[]={
  232. L"P7CFile\\shell", NULL, 0,
  233. L"CTLFile\\shell", NULL, 0,
  234. L"P7CFile", NULL, 0,
  235. L"CTLFile", NULL, 0,
  236. L"CERTSTOREFile\\shell", NULL, 0,
  237. L"CERTSTOREFile", NULL, 0,
  238. };
  239. MIME_REG_ENTRY rgResetChangedEntry[]={
  240. L"P7CFile", NULL, IDS_OLD_P7C_NAME,
  241. L"CTLFile", NULL, IDS_OLD_CTL_NAME,
  242. L"CERTSTOREFile", NULL, IDS_OLD_STR_NAME,
  243. };
  244. MIME_REG_ENTRY rgRemoveChangedEntry[]={
  245. L".str", L"CERTSTOREFile", 0,
  246. L".p7c", L"P7CFile", 0,
  247. L".ctl", L"CTLFile", 0,
  248. L"CTLFile\\DefaultIcon", L"cryptui.dll,-3413", 0,
  249. L"P7CFile\\DefaultIcon", L"cryptui.dll,-3410", 0,
  250. };
  251. //
  252. // DSIE: Starting with Whistler, the MUI system requires a new registry value named
  253. // "FirendlyTypeName" where the data will be loaded from a specified DLL.
  254. //
  255. MIME_REG_ENTRY rgFriendlyTypeNameEntry[]={
  256. L"CERFile", L"@%%SystemRoot%%\\System32\\cryptext.dll,-%1!u!", IDS_CER_NAME,
  257. L"STLFile", L"@%%SystemRoot%%\\System32\\cryptext.dll,-%1!u!", IDS_STL_NAME,
  258. L"CRLFile", L"@%%SystemRoot%%\\System32\\cryptext.dll,-%1!u!", IDS_CRL_NAME,
  259. L"SPCFile", L"@%%SystemRoot%%\\System32\\cryptext.dll,-%1!u!", IDS_SPC_NAME,
  260. L"CertificateStoreFile", L"@%%SystemRoot%%\\System32\\cryptext.dll,-%1!u!", IDS_SST_NAME,
  261. L"P7SFile", L"@%%SystemRoot%%\\System32\\cryptext.dll,-%1!u!", IDS_P7S_NAME,
  262. L"P7MFile", L"@%%SystemRoot%%\\System32\\cryptext.dll,-%1!u!", IDS_P7M_NAME,
  263. L"P10File", L"@%%SystemRoot%%\\System32\\cryptext.dll,-%1!u!", IDS_P10_NAME,
  264. L"PKOFile", L"@%%SystemRoot%%\\System32\\cryptext.dll,-%1!u!", IDS_PKO_NAME,
  265. L"P7RFile", L"@%%SystemRoot%%\\System32\\cryptext.dll,-%1!u!", IDS_P7R_NAME,
  266. L"CATFile", L"@%%SystemRoot%%\\System32\\cryptext.dll,-%1!u!", IDS_CAT_NAME,
  267. L"PFXFile", L"@%%SystemRoot%%\\System32\\cryptext.dll,-%1!u!", IDS_PFX_NAME,
  268. };
  269. #include <dbgdef.h>
  270. //--------------------------------------------------------------------------
  271. //
  272. // before anything else, we need to remove the .ctl, .str, and .p7c
  273. // entries. No need to check the return values
  274. //
  275. //--------------------------------------------------------------------------
  276. void RemoveOldExtensions()
  277. {
  278. DWORD dwRegEntry=0;
  279. DWORD dwRegIndex=0;
  280. HKEY hKey=NULL;
  281. BOOL fCorrectValue=FALSE;
  282. BOOL fPreviousValue=FALSE;
  283. WCHAR wszValue[MAX_STRING_SIZE];
  284. DWORD dwLastStringSize=0;
  285. DWORD dwType=0;
  286. DWORD cbSize=0;
  287. BOOL fCTLOpen=FALSE;
  288. BOOL fP7COpen=FALSE;
  289. FILETIME fileTime;
  290. WCHAR wszLoadString[MAX_STRING_SIZE];
  291. //1st, delete the rgRemoveChangedEntry about Icons
  292. dwRegEntry=sizeof(rgRemoveChangedEntry)/sizeof(rgRemoveChangedEntry[0]);
  293. for(dwRegIndex=0; dwRegIndex<dwRegEntry; dwRegIndex++)
  294. {
  295. fCorrectValue=FALSE;
  296. if (ERROR_SUCCESS == RegOpenKeyExU(
  297. HKEY_CLASSES_ROOT,
  298. rgRemoveChangedEntry[dwRegIndex].wszKey,
  299. 0,
  300. KEY_READ,
  301. &hKey))
  302. {
  303. //get the value
  304. wszValue[0]=L'\0';
  305. cbSize=sizeof(wszValue)/sizeof(wszValue[0]);
  306. if(ERROR_SUCCESS == RegQueryValueExU(
  307. hKey,
  308. NULL,
  309. NULL,
  310. &dwType,
  311. (BYTE *)wszValue,
  312. &cbSize))
  313. {
  314. if(REG_SZ == dwType|| REG_EXPAND_SZ == dwType)
  315. {
  316. dwLastStringSize=wcslen(rgRemoveChangedEntry[dwRegIndex].wszName);
  317. if(((DWORD)wcslen(wszValue)) >= dwLastStringSize)
  318. {
  319. if(0 == _wcsicmp(
  320. (LPWSTR)(wszValue+wcslen(wszValue)-dwLastStringSize),
  321. rgRemoveChangedEntry[dwRegIndex].wszName))
  322. fCorrectValue=TRUE;
  323. }
  324. }
  325. }
  326. if(hKey)
  327. {
  328. RegCloseKey(hKey);
  329. hKey=NULL;
  330. }
  331. }
  332. if(fCorrectValue)
  333. {
  334. RegDeleteKeyU(HKEY_CLASSES_ROOT,rgRemoveChangedEntry[dwRegIndex].wszKey);
  335. }
  336. }
  337. //2nd, reset the values to NULL of rgResetChangedEntry
  338. dwRegEntry=sizeof(rgResetChangedEntry)/sizeof(rgResetChangedEntry[0]);
  339. hKey=NULL;
  340. for(dwRegIndex=0; dwRegIndex<dwRegEntry; dwRegIndex++)
  341. {
  342. fCorrectValue=FALSE;
  343. if(0!=LoadStringU(g_hmodThisDll,rgResetChangedEntry[dwRegIndex].idsName,
  344. wszLoadString,MAX_STRING_SIZE))
  345. {
  346. if (ERROR_SUCCESS == RegOpenKeyExU(
  347. HKEY_CLASSES_ROOT,
  348. rgResetChangedEntry[dwRegIndex].wszKey,
  349. 0,
  350. KEY_WRITE | KEY_READ,
  351. &hKey))
  352. {
  353. //get the value
  354. wszValue[0]=L'\0';
  355. cbSize=sizeof(wszValue)/sizeof(wszValue[0]);
  356. //use try{}except here since not sure what WIN95 will behave
  357. //when the value is NULL
  358. __try{
  359. if(ERROR_SUCCESS == RegQueryValueExU(
  360. hKey,
  361. NULL,
  362. NULL,
  363. &dwType,
  364. (BYTE *)wszValue,
  365. &cbSize))
  366. {
  367. if(REG_SZ == dwType || REG_EXPAND_SZ == dwType)
  368. {
  369. if(0 == _wcsicmp(
  370. wszValue,
  371. wszLoadString))
  372. fCorrectValue=TRUE;
  373. }
  374. }
  375. } __except(EXCEPTION_EXECUTE_HANDLER) {
  376. }
  377. }
  378. }
  379. if(fCorrectValue)
  380. {
  381. //set the value to NULL
  382. //use try{}except here since not sure what WIN95 will behave
  383. //when the value is NULL
  384. __try{
  385. RegSetValueExU(
  386. hKey,
  387. NULL,
  388. 0,
  389. REG_SZ,
  390. NULL,
  391. 0);
  392. } __except(EXCEPTION_EXECUTE_HANDLER) {
  393. }
  394. }
  395. if(hKey)
  396. {
  397. RegCloseKey(hKey);
  398. hKey=NULL;
  399. }
  400. }
  401. //3rd, delete the related keys in rgRemoveRelatedEntry
  402. dwRegEntry=sizeof(rgRemoveRelatedEntry)/sizeof(rgRemoveRelatedEntry[0]);
  403. hKey=NULL;
  404. for(dwRegIndex=0; dwRegIndex<dwRegEntry; dwRegIndex+=2)
  405. {
  406. fCorrectValue=FALSE;
  407. if (ERROR_SUCCESS == RegOpenKeyExU(
  408. HKEY_CLASSES_ROOT,
  409. rgRemoveRelatedEntry[dwRegIndex].wszKey,
  410. 0,
  411. KEY_READ,
  412. &hKey))
  413. {
  414. //get the value
  415. wszValue[0]=L'\0';
  416. cbSize=sizeof(wszValue)/sizeof(wszValue[0]);
  417. if(ERROR_SUCCESS == RegQueryValueExU(
  418. hKey,
  419. NULL,
  420. NULL,
  421. &dwType,
  422. (BYTE *)wszValue,
  423. &cbSize))
  424. {
  425. if(REG_SZ == dwType|| REG_EXPAND_SZ == dwType)
  426. {
  427. if(0 == _wcsicmp(
  428. wszValue,
  429. rgRemoveRelatedEntry[dwRegIndex].wszName))
  430. fCorrectValue=TRUE;
  431. }
  432. }
  433. if(hKey)
  434. {
  435. RegCloseKey(hKey);
  436. hKey=NULL;
  437. }
  438. }
  439. if(fCorrectValue)
  440. {
  441. //mark if the CTLAdd and CTLOpen were the correct values
  442. if(dwRegIndex == 0)
  443. fP7COpen=TRUE;
  444. if(dwRegIndex == 2)
  445. fCTLOpen=TRUE;
  446. RegDeleteKeyU(HKEY_CLASSES_ROOT,rgRemoveRelatedEntry[dwRegIndex].wszKey);
  447. RegDeleteKeyU(HKEY_CLASSES_ROOT,rgRemoveRelatedEntry[dwRegIndex+1].wszKey);
  448. }
  449. }
  450. //fourth, if the shell subkey is empty, we need to remove the subkeys
  451. dwRegEntry=sizeof(rgRemoveEmptyEntry)/sizeof(rgRemoveEmptyEntry[0]);
  452. hKey=NULL;
  453. for(dwRegIndex=0; dwRegIndex<dwRegEntry; dwRegIndex++)
  454. {
  455. fCorrectValue=FALSE;
  456. if (ERROR_SUCCESS == RegOpenKeyExU(
  457. HKEY_CLASSES_ROOT,
  458. rgRemoveEmptyEntry[dwRegIndex].wszKey,
  459. 0,
  460. KEY_READ,
  461. &hKey))
  462. {
  463. //enum the subkey
  464. cbSize=0;
  465. if(ERROR_SUCCESS != RegEnumKeyExU(
  466. hKey,
  467. 0,
  468. NULL,
  469. &cbSize,
  470. NULL,
  471. NULL,
  472. NULL,
  473. &fileTime))
  474. {
  475. fCorrectValue=TRUE;
  476. }
  477. if(hKey)
  478. {
  479. RegCloseKey(hKey);
  480. hKey=NULL;
  481. }
  482. }
  483. if(fCorrectValue)
  484. {
  485. //mark if the CTLAdd and CTLOpen were the correct values
  486. // if((0 == dwRegIndex && TRUE == fP7COpen) ||
  487. // (1 == dwRegIndex && TRUE == fCTLOpen)
  488. // )
  489. // {
  490. RegDeleteKeyU(HKEY_CLASSES_ROOT,rgRemoveEmptyEntry[dwRegIndex].wszKey);
  491. // }
  492. }
  493. }
  494. }
  495. //--------------------------------------------------------------------------
  496. //
  497. // RegisterMimeHandler
  498. //
  499. // This function adds the following registry entries:
  500. //
  501. //[HKEY_CLASSES_ROOT\.cer]
  502. // @="CERFile"
  503. //[HKEY_CLASSES_ROOT\CERFile]
  504. // @="Security Certificate"
  505. //[HKEY_CLASSES_ROOT\CERFile\shell\open\command]
  506. // @="rundll32.exe cryptext.dll,CryptExtOpenCER %1"
  507. //[HKEY_CLASSES_ROOT\CERFile\shell\add]
  508. // @="&Add"
  509. //[HKEY_CLASSES_ROOT\CERFile\shell\add\command]
  510. // @="rundll32.exe cryptext.dll,CryptExtAddCER %1"
  511. //
  512. //[HKEY_CLASSES_ROOT\.crt]
  513. // @="CERFile"
  514. //
  515. //[HKEY_CLASSES_ROOT\.stl]
  516. // @="sTLFile"
  517. //[HKEY_CLASSES_ROOT\sTLFile]
  518. // @="Trust List"
  519. //[HKEY_sLASSES_ROOT\sTLFile\shell\open\command]
  520. // @="rundll32.exe cryptext.dll,CryptExtOpenCTL %1"
  521. //[HKEY_CLASSES_ROOT\sTLFile\shell\add]
  522. // @="&Add"
  523. //[HKEY_CLASSES_ROOT\sTLFile\shell\add\command]
  524. // @="rundll32.exe cryptext.dll,CryptExtAddCTL %1"
  525. //
  526. //[HKEY_CLASSES_ROOT\.crl]
  527. // @="CRLFile"
  528. //[HKEY_CLASSES_ROOT\CRLFile]
  529. // @="Certificate Revocation List"
  530. //[HKEY_CLASSES_ROOT\CRLFile\shell\open\command]
  531. // @="rundll32.exe cryptext.dll,CryptExtOpenCRL %1"
  532. //[HKEY_CLASSES_ROOT\CRLFile\shell\add]
  533. // @="&Add"
  534. //[HKEY_CLASSES_ROOT\CRLFile\shell\add\command]
  535. // @="rundll32.exe cryptext.dll,CryptExtAddCRL %1"
  536. //
  537. //[HKEY_CLASSES_ROOT\.spc]
  538. // @="SPCFile"
  539. //[HKEY_CLASSES_ROOT\SPCFile]
  540. // @="Software Publishing Credentials"
  541. //[HKEY_CLASSES_ROOT\SPCFile\shell\open\command]
  542. // @="rundll32.exe cryptext.dll,CryptExtOpenPKCS7 %1"
  543. //
  544. //[HKEY_CLASSES_ROOT\.p7s]
  545. // @="P7SFile"
  546. //[HKEY_CLASSES_ROOT\P7SFile]
  547. // @="PKCS7 Signature"
  548. //[HKEY_CLASSES_ROOT\P7SFile\shell\open\command]
  549. // @="rundll32.exe cryptext.dll,CryptExtOpenPKCS7 %1"
  550. //
  551. //[HKEY_CLASSES_ROOT\.p7b]
  552. // @="P7BFile"
  553. //[HKEY_CLASSES_ROOT\P7BFile]
  554. // @="PKCS7 Certificates"
  555. //[HKEY_CLASSES_ROOT\P7BFile\shell\open\command]
  556. // @="rundll32.exe cryptext.dll,CryptExtOpenPKCS7 %1"
  557. //
  558. //[HKEY_CLASSES_ROOT\.p7m]
  559. // @="P7MFile"
  560. //[HKEY_CLASSES_ROOT\P7MFile]
  561. // @="PKCS7 MIME"
  562. //
  563. //[HKEY_CLASSES_ROOT\.sst]
  564. // @="CertificateStoreFile"
  565. //[HKEY_CLASSES_ROOT\CertificateStoreFile]
  566. // @="Certificate Store"
  567. //[HKEY_CLASSES_ROOT\CertificateStoreFile\shell\open\command]
  568. // @="rundll32.exe cryptext.dll,CryptExtOpenSTR %1"
  569. //
  570. //[HKEY_CLASSES_ROOT\.p10]
  571. // @="P10File"
  572. //[HKEY_CLASSES_ROOT\P10File]
  573. // @="Certificate Request"
  574. //
  575. //[HKEY_CLASSES_ROOT\.pko]
  576. // @="PKOFile"
  577. //[HKEY_CLASSES_ROOT\PKOFile]
  578. // @="Public Key Object"
  579. //[HKEY_CLASSES_ROOT\PKOFile\shellex\ContextMenuHandlers]
  580. // @="CryptoMenu"
  581. //[HKEY_CLASSES_ROOT\PKOFile\shellex\ContextMenuHandlers\CryptoMenu]
  582. // @="{7444C717-39BF-11D1-8CD9-00C04FC29D45}"
  583. //
  584. //[HKEY_CLASSES_ROOT\*\shellex\ContextMenuHandlers\CryptoSignMenu]
  585. // @="{7444C719-39BF-11D1-8CD9-00C04FC29D45}"
  586. //
  587. //
  588. //[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Shell Extensions\Approved]
  589. // "{7444C717-39BF-11D1-8CD9-00C04FC29D45}"="Crypto PKO Extension"
  590. //
  591. //[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Shell Extensions\Approved]
  592. // "{7444C719-39BF-11D1-8CD9-00C04FC29D45}"="Crypto Sign Extension"
  593. //
  594. //--------------------------------------------------------------------------
  595. HRESULT RegisterMimeHandler()
  596. {
  597. HRESULT hr=E_FAIL;
  598. DWORD dwRegEntry=0;
  599. DWORD dwRegIndex=0;
  600. WCHAR wszName[MAX_STRING_SIZE];
  601. HKEY hKey=NULL;
  602. DWORD dwDisposition=0;
  603. WCHAR wszGUID[MAX_STRING_SIZE];
  604. WCHAR wszDefault[MAX_STRING_SIZE*2];
  605. CHAR szValue[2 * MAX_PATH + 1];
  606. CHAR szSystem[MAX_PATH + 1];
  607. WCHAR wszContentType[MAX_STRING_SIZE];
  608. LPWSTR pwszCommandName = NULL;
  609. LPWSTR pwszFriendlyTypeName = NULL;
  610. //before anything else, we need to remove the .ctl, .str, and .p7c
  611. //entries. No need to check the return values
  612. RemoveOldExtensions();
  613. //1st, do the registry based context menu
  614. //get the count of the reg entries
  615. dwRegEntry=sizeof(rgRegEntry)/sizeof(rgRegEntry[0]);
  616. for(dwRegIndex=0; dwRegIndex<dwRegEntry; dwRegIndex++)
  617. {
  618. //open a registry entry under HKEY_CLASSES_ROOT
  619. if (ERROR_SUCCESS != RegCreateKeyExU(
  620. HKEY_CLASSES_ROOT,
  621. rgRegEntry[dwRegIndex].wszKey,
  622. 0,
  623. NULL,
  624. REG_OPTION_NON_VOLATILE,
  625. KEY_WRITE,
  626. NULL,
  627. &hKey,
  628. &dwDisposition))
  629. goto RegCreateKeyErr;
  630. //set the value
  631. if (NULL == rgRegEntry[dwRegIndex].wszName)
  632. {
  633. //load the string
  634. if(0==LoadStringU(g_hmodThisDll,rgRegEntry[dwRegIndex].idsName,
  635. wszName,MAX_STRING_SIZE))
  636. goto LoadStringErr;
  637. if(ERROR_SUCCESS != RegSetValueExU(
  638. hKey,
  639. NULL,
  640. 0,
  641. REG_SZ,
  642. (BYTE *)wszName,
  643. (wcslen(wszName) + 1) * sizeof(WCHAR)))
  644. goto RegSetValueErr;
  645. }
  646. else if ((LPWSTR) 1 == rgRegEntry[dwRegIndex].wszName)
  647. {
  648. //Redirect to load the string from the DLL.
  649. //format the data by inserting the IDS value.
  650. if (0 == FormatMessageU(
  651. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ARGUMENT_ARRAY,
  652. L"@%%SystemRoot%%\\System32\\cryptext.dll,-%1!u!",
  653. 0, // dwMessageId
  654. 0, // dwLanguageId
  655. (LPWSTR) (&pwszCommandName),
  656. 0, // minimum size to allocate
  657. (va_list *) &rgRegEntry[dwRegIndex].idsName))
  658. goto FormatMsgError;
  659. if(ERROR_SUCCESS != RegSetValueExU(
  660. hKey,
  661. NULL,
  662. 0,
  663. REG_SZ,
  664. (BYTE *) pwszCommandName,
  665. (wcslen(pwszCommandName) + 1) * sizeof(WCHAR)))
  666. goto RegSetValueErr;
  667. }
  668. else
  669. {
  670. if(ERROR_SUCCESS != RegSetValueExU(
  671. hKey,
  672. NULL,
  673. 0,
  674. REG_EXPAND_SZ,
  675. (BYTE *)(rgRegEntry[dwRegIndex].wszName),
  676. (wcslen(rgRegEntry[dwRegIndex].wszName) + 1) * sizeof(WCHAR)))
  677. goto RegSetValueErr;
  678. }
  679. //close the registry key
  680. if(ERROR_SUCCESS != RegCloseKey(hKey))
  681. goto RegCloseKeyErr;
  682. hKey=NULL;
  683. // free command name string.
  684. LocalFree((HLOCAL) pwszCommandName);
  685. pwszCommandName = NULL;
  686. }
  687. //some of the registry based context menu is specific for NT5
  688. if(FIsWinNT5())
  689. {
  690. //get the count of the reg entries
  691. dwRegEntry=sizeof(rgWINNT5RegEntry)/sizeof(rgWINNT5RegEntry[0]);
  692. for(dwRegIndex=0; dwRegIndex<dwRegEntry; dwRegIndex++)
  693. {
  694. //open a registry entry under HKEY_CLASSES_ROOT
  695. if (ERROR_SUCCESS != RegCreateKeyExU(
  696. HKEY_CLASSES_ROOT,
  697. rgWINNT5RegEntry[dwRegIndex].wszKey,
  698. 0,
  699. NULL,
  700. REG_OPTION_NON_VOLATILE,
  701. KEY_WRITE,
  702. NULL,
  703. &hKey,
  704. &dwDisposition))
  705. goto RegCreateKeyErr;
  706. //set the value
  707. if(NULL==rgWINNT5RegEntry[dwRegIndex].wszName)
  708. {
  709. //load the string
  710. if(0==LoadStringU(g_hmodThisDll,rgWINNT5RegEntry[dwRegIndex].idsName,
  711. wszName,MAX_STRING_SIZE))
  712. goto LoadStringErr;
  713. if(ERROR_SUCCESS != RegSetValueExU(
  714. hKey,
  715. NULL,
  716. 0,
  717. REG_SZ,
  718. (BYTE *)wszName,
  719. (wcslen(wszName) + 1) * sizeof(WCHAR)))
  720. goto RegSetValueErr;
  721. }
  722. else
  723. {
  724. if(ERROR_SUCCESS != RegSetValueExU(
  725. hKey,
  726. NULL,
  727. 0,
  728. REG_EXPAND_SZ,
  729. (BYTE *)(rgWINNT5RegEntry[dwRegIndex].wszName),
  730. (wcslen(rgWINNT5RegEntry[dwRegIndex].wszName) + 1) * sizeof(WCHAR)))
  731. goto RegSetValueErr;
  732. }
  733. //close the registry key
  734. if(ERROR_SUCCESS != RegCloseKey(hKey))
  735. goto RegCloseKeyErr;
  736. hKey=NULL;
  737. }
  738. }
  739. //now, we need to register for the content type
  740. //1. Under the HEKY_CLASSES_ROOT, under ".foo" key, add an entry of name
  741. // "Content Type" and value "application/xxxxxxxx".
  742. //2. Under HKEY_CLASSES_ROOT\MIME\Database\Content Type, add a key of "application/xxxxxxxx",
  743. // under which add an entry of name "Extension" and value ".foo".
  744. dwRegEntry=sizeof(rgContentTypeEntry)/sizeof(rgContentTypeEntry[0]);
  745. for(dwRegIndex=0; dwRegIndex<dwRegEntry; dwRegIndex++)
  746. {
  747. //open a registry entry under HKEY_CLASSES_ROOT
  748. if (ERROR_SUCCESS != RegCreateKeyExU(
  749. HKEY_CLASSES_ROOT,
  750. rgContentTypeEntry[dwRegIndex].wszKey,
  751. 0,
  752. NULL,
  753. REG_OPTION_NON_VOLATILE,
  754. KEY_WRITE,
  755. NULL,
  756. &hKey,
  757. &dwDisposition))
  758. goto RegCreateKeyErr;
  759. //set the value
  760. if(ERROR_SUCCESS != RegSetValueExU(
  761. hKey,
  762. L"Content Type",
  763. 0,
  764. REG_SZ,
  765. (BYTE *)(rgContentTypeEntry[dwRegIndex].wszName),
  766. (wcslen(rgContentTypeEntry[dwRegIndex].wszName) + 1) * sizeof(WCHAR)))
  767. goto RegSetValueErr;
  768. //close the registry key
  769. if(ERROR_SUCCESS != RegCloseKey(hKey))
  770. goto RegCloseKeyErr;
  771. hKey=NULL;
  772. }
  773. //2. Under HKEY_CLASSES_ROOT\MIME\Database\Content Type, add a key of "application/xxxxxxxx",
  774. // under which add an entry of name "Extension" and value ".foo".
  775. dwRegEntry=sizeof(rgContentTypeEntry)/sizeof(rgContentTypeEntry[0]);
  776. for(dwRegIndex=0; dwRegIndex<dwRegEntry; dwRegIndex++)
  777. {
  778. //concatenate the key L"MIME\\Database\\Content Type\\application/XXXXXXXXXX
  779. wszContentType[0]=L'\0';
  780. wcscpy(wszContentType, L"MIME\\Database\\Content Type\\");
  781. wcscat(wszContentType, rgContentTypeEntry[dwRegIndex].wszName);
  782. //open a registry entry under HKEY_CLASSES_ROOT
  783. if (ERROR_SUCCESS != RegCreateKeyExU(
  784. HKEY_CLASSES_ROOT,
  785. wszContentType,
  786. 0,
  787. NULL,
  788. REG_OPTION_NON_VOLATILE,
  789. KEY_WRITE,
  790. NULL,
  791. &hKey,
  792. &dwDisposition))
  793. goto RegCreateKeyErr;
  794. //set the value
  795. if(ERROR_SUCCESS != RegSetValueExU(
  796. hKey,
  797. L"Extension",
  798. 0,
  799. REG_SZ,
  800. (BYTE *)(rgContentTypeEntry[dwRegIndex].wszKey),
  801. (wcslen(rgContentTypeEntry[dwRegIndex].wszKey) + 1) * sizeof(WCHAR)))
  802. goto RegSetValueErr;
  803. //close the registry key
  804. if(ERROR_SUCCESS != RegCloseKey(hKey))
  805. goto RegCloseKeyErr;
  806. hKey=NULL;
  807. }
  808. //2nd, do the registry based DefaultIcon
  809. //we do things differently based on Win95 or WinNT
  810. if(FIsWinNT())
  811. {
  812. dwRegEntry=sizeof(rgIconEntry)/sizeof(rgIconEntry[0]);
  813. for(dwRegIndex=0; dwRegIndex<dwRegEntry; dwRegIndex++)
  814. {
  815. //open a registry entry under HKEY_CLASSES_ROOT
  816. if (ERROR_SUCCESS != RegCreateKeyExU(
  817. HKEY_CLASSES_ROOT,
  818. rgIconEntry[dwRegIndex].wszKey,
  819. 0,
  820. NULL,
  821. REG_OPTION_NON_VOLATILE,
  822. KEY_WRITE,
  823. NULL,
  824. &hKey,
  825. &dwDisposition))
  826. goto RegCreateKeyErr;
  827. if(ERROR_SUCCESS != RegSetValueExU(
  828. hKey,
  829. NULL,
  830. 0,
  831. REG_EXPAND_SZ,
  832. (BYTE *)(rgIconEntry[dwRegIndex].wszName),
  833. (wcslen(rgIconEntry[dwRegIndex].wszName) + 1) * sizeof(WCHAR)))
  834. goto RegSetValueErr;
  835. //close the registry key
  836. if(ERROR_SUCCESS != RegCloseKey(hKey))
  837. goto RegCloseKeyErr;
  838. hKey=NULL;
  839. }
  840. }
  841. else
  842. {
  843. //get the system directory
  844. if(!GetSystemDirectory(szSystem, MAX_PATH))
  845. goto GetSystemErr;
  846. dwRegEntry=sizeof(rgWin95IconEntry)/sizeof(rgWin95IconEntry[0]);
  847. for(dwRegIndex=0; dwRegIndex<dwRegEntry; dwRegIndex++)
  848. {
  849. //open a registry entry under HKEY_CLASSES_ROOT
  850. if (ERROR_SUCCESS != RegCreateKeyEx(
  851. HKEY_CLASSES_ROOT,
  852. rgWin95IconEntry[dwRegIndex].szKey,
  853. 0,
  854. NULL,
  855. REG_OPTION_NON_VOLATILE,
  856. KEY_WRITE,
  857. NULL,
  858. &hKey,
  859. &dwDisposition))
  860. goto RegCreateKeyErr;
  861. //concantenate the values
  862. strcpy(szValue, szSystem);
  863. strcat(szValue, (rgWin95IconEntry[dwRegIndex].szName));
  864. if(ERROR_SUCCESS != RegSetValueEx(
  865. hKey,
  866. NULL,
  867. 0,
  868. REG_SZ,
  869. (BYTE *)szValue,
  870. (strlen(szValue) + 1) * sizeof(CHAR)))
  871. goto RegSetValueErr;
  872. //close the registry key
  873. if(ERROR_SUCCESS != RegCloseKey(hKey))
  874. goto RegCloseKeyErr;
  875. hKey=NULL;
  876. }
  877. }
  878. //3rd, set the .PKO context menu handler and property sheet handler
  879. //set the values related to the GUIDs
  880. dwRegEntry=sizeof(rgGuidEntry)/sizeof(rgGuidEntry[0]);
  881. for(dwRegIndex=0; dwRegIndex<dwRegEntry; dwRegIndex++)
  882. {
  883. //load the string
  884. if(0==LoadStringU(g_hmodThisDll,rgGuidEntry[dwRegIndex].idsName,
  885. wszName,MAX_STRING_SIZE))
  886. goto LoadStringErr;
  887. //get the string presentation of the CLSID
  888. if(0==StringFromGUID2(*(rgGuidEntry[dwRegIndex].pGuid), wszGUID, MAX_STRING_SIZE))
  889. goto StringFromGUIDErr;
  890. //open a registry entry under HKEY_CLASSES_ROOT for the context menu handler
  891. if(NULL!=rgGuidEntry[dwRegIndex].wszKey1)
  892. {
  893. if (ERROR_SUCCESS != RegCreateKeyExU(
  894. HKEY_CLASSES_ROOT,
  895. rgGuidEntry[dwRegIndex].wszKey1,
  896. 0,
  897. NULL,
  898. REG_OPTION_NON_VOLATILE,
  899. KEY_WRITE,
  900. NULL,
  901. &hKey,
  902. &dwDisposition))
  903. goto RegCreateKeyErr;
  904. if(ERROR_SUCCESS != RegSetValueExU(
  905. hKey,
  906. NULL,
  907. 0,
  908. REG_SZ,
  909. (BYTE *)wszGUID,
  910. (wcslen(wszGUID) + 1) * sizeof(WCHAR)))
  911. goto RegSetValueErr;
  912. //close the registry key
  913. if(ERROR_SUCCESS != RegCloseKey(hKey))
  914. goto RegCloseKeyErr;
  915. hKey=NULL;
  916. }
  917. //open a registry entry under HKEY_CLASSES_ROOT for the property sheet hander
  918. if (ERROR_SUCCESS != RegCreateKeyExU(
  919. HKEY_CLASSES_ROOT,
  920. rgGuidEntry[dwRegIndex].wszKey2,
  921. 0,
  922. NULL,
  923. REG_OPTION_NON_VOLATILE,
  924. KEY_WRITE,
  925. NULL,
  926. &hKey,
  927. &dwDisposition))
  928. goto RegCreateKeyErr;
  929. if(ERROR_SUCCESS != RegSetValueExU(
  930. hKey,
  931. NULL,
  932. 0,
  933. REG_SZ,
  934. (BYTE *)wszGUID,
  935. (wcslen(wszGUID) + 1) * sizeof(WCHAR)))
  936. goto RegSetValueErr;
  937. //close the registry key
  938. if(ERROR_SUCCESS != RegCloseKey(hKey))
  939. goto RegCloseKeyErr;
  940. hKey=NULL;
  941. //open a registry entry under HKEY_LOCAL_MACHINE
  942. if (ERROR_SUCCESS != RegCreateKeyExU(
  943. HKEY_LOCAL_MACHINE,
  944. rgGuidEntry[dwRegIndex].wszKey3,
  945. 0,
  946. NULL,
  947. REG_OPTION_NON_VOLATILE,
  948. KEY_WRITE,
  949. NULL,
  950. &hKey,
  951. &dwDisposition))
  952. goto RegCreateKeyErr;
  953. if(ERROR_SUCCESS != RegSetValueExU(
  954. hKey,
  955. wszGUID,
  956. 0,
  957. REG_SZ,
  958. (BYTE *)wszName,
  959. (wcslen(wszName) + 1) * sizeof(WCHAR)))
  960. goto RegSetValueErr;
  961. //close the registry key
  962. if(ERROR_SUCCESS != RegCloseKey(hKey))
  963. goto RegCloseKeyErr;
  964. hKey=NULL;
  965. }
  966. //now, under the classID of &CLSID_CryptPKO, we need to add
  967. //the registry shellex\MayChangeDefaultMenu
  968. dwRegEntry=sizeof(g_CLSIDDefault)/sizeof(g_CLSIDDefault[0]);
  969. //get the string presentation of the CLSID
  970. if(0==StringFromGUID2(CLSID_CryptPKO, wszGUID, MAX_STRING_SIZE))
  971. goto StringFromGUIDErr;
  972. for(dwRegIndex=0; dwRegIndex<dwRegEntry; dwRegIndex++)
  973. {
  974. wcscpy(wszDefault, L"CLSID\\");
  975. wcscat(wszDefault, wszGUID);
  976. wcscat(wszDefault, g_CLSIDDefault[dwRegIndex]);
  977. if (ERROR_SUCCESS != RegCreateKeyExU(
  978. HKEY_CLASSES_ROOT,
  979. wszDefault,
  980. 0,
  981. NULL,
  982. REG_OPTION_NON_VOLATILE,
  983. KEY_WRITE,
  984. NULL,
  985. &hKey,
  986. &dwDisposition))
  987. goto RegCreateKeyErr;
  988. if(ERROR_SUCCESS != RegCloseKey(hKey))
  989. goto RegCloseKeyErr;
  990. hKey=NULL;
  991. }
  992. // Set the FriendlyTypeName value for the new MUI requirement of Whistler.
  993. dwRegEntry=sizeof(rgFriendlyTypeNameEntry)/sizeof(rgFriendlyTypeNameEntry[0]);
  994. for(dwRegIndex=0; dwRegIndex<dwRegEntry; dwRegIndex++)
  995. {
  996. //format the data by inserting the IDS value.
  997. if (0 == FormatMessageU(
  998. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ARGUMENT_ARRAY,
  999. rgFriendlyTypeNameEntry[dwRegIndex].wszName,
  1000. 0, // dwMessageId
  1001. 0, // dwLanguageId
  1002. (LPWSTR) (&pwszFriendlyTypeName),
  1003. 0, // minimum size to allocate
  1004. (va_list *) &rgFriendlyTypeNameEntry[dwRegIndex].idsName))
  1005. goto FormatMsgError;
  1006. //open a registry entry under HKEY_CLASSES_ROOT
  1007. if (ERROR_SUCCESS != RegCreateKeyExU(
  1008. HKEY_CLASSES_ROOT,
  1009. rgFriendlyTypeNameEntry[dwRegIndex].wszKey,
  1010. 0,
  1011. NULL,
  1012. REG_OPTION_NON_VOLATILE,
  1013. KEY_WRITE,
  1014. NULL,
  1015. &hKey,
  1016. &dwDisposition))
  1017. goto RegCreateKeyErr;
  1018. //set the value
  1019. if(ERROR_SUCCESS != RegSetValueExU(
  1020. hKey,
  1021. L"FriendlyTypeName",
  1022. 0,
  1023. REG_EXPAND_SZ,
  1024. (BYTE *) pwszFriendlyTypeName,
  1025. (wcslen(pwszFriendlyTypeName) + 1) * sizeof(WCHAR)))
  1026. goto RegSetValueErr;
  1027. //close the registry key
  1028. if(ERROR_SUCCESS != RegCloseKey(hKey))
  1029. goto RegCloseKeyErr;
  1030. hKey=NULL;
  1031. // free friendly name type string.
  1032. LocalFree((HLOCAL) pwszFriendlyTypeName);
  1033. pwszFriendlyTypeName = NULL;
  1034. }
  1035. hr=S_OK;
  1036. CommonReturn:
  1037. if(hKey)
  1038. RegCloseKey(hKey);
  1039. if(pwszCommandName)
  1040. LocalFree((HLOCAL) pwszCommandName);
  1041. if(pwszFriendlyTypeName)
  1042. LocalFree((HLOCAL) pwszFriendlyTypeName);
  1043. return hr;
  1044. ErrorReturn:
  1045. hr=GetLastError();
  1046. goto CommonReturn;
  1047. TRACE_ERROR(RegCloseKeyErr);
  1048. TRACE_ERROR(RegCreateKeyErr);
  1049. TRACE_ERROR(LoadStringErr);
  1050. TRACE_ERROR(RegSetValueErr);
  1051. TRACE_ERROR(StringFromGUIDErr);
  1052. TRACE_ERROR(GetSystemErr);
  1053. TRACE_ERROR(FormatMsgError);
  1054. }
  1055. //--------------------------------------------------------------------------
  1056. //
  1057. // UnregisterMimeHandler
  1058. //
  1059. //--------------------------------------------------------------------------
  1060. HRESULT UnregisterMimeHandler()
  1061. {
  1062. HKEY hKey=NULL;
  1063. DWORD dwRegEntry=0;
  1064. DWORD dwRegIndex=0;
  1065. WCHAR wszGUID[MAX_STRING_SIZE];
  1066. DWORD dwDisposition=0;
  1067. WCHAR wszDefault[MAX_STRING_SIZE*2];
  1068. WCHAR wszContentType[MAX_STRING_SIZE];
  1069. //1st, delete the entries related to the GUID
  1070. //that is, the .PKO context menu handler and property sheet handler
  1071. dwRegEntry=sizeof(rgGuidEntry)/sizeof(rgGuidEntry[0]);
  1072. for(dwRegIndex=0; dwRegIndex<dwRegEntry; dwRegIndex++)
  1073. {
  1074. //get the string presentation of the CLSID
  1075. if(0==StringFromGUID2(*(rgGuidEntry[dwRegIndex].pGuid), wszGUID, MAX_STRING_SIZE))
  1076. continue;
  1077. //open a registry entry under HKEY_LOCAL_MACHINE
  1078. if (ERROR_SUCCESS == RegCreateKeyExU(
  1079. HKEY_LOCAL_MACHINE,
  1080. rgGuidEntry[dwRegIndex].wszKey3,
  1081. 0,
  1082. NULL,
  1083. REG_OPTION_NON_VOLATILE,
  1084. KEY_WRITE,
  1085. NULL,
  1086. &hKey,
  1087. &dwDisposition))
  1088. {
  1089. RegDeleteValueU(hKey,wszGUID);
  1090. //close the registry key
  1091. RegCloseKey(hKey);
  1092. hKey=NULL;
  1093. }
  1094. //delete a registry entry under HKEY_CLASSES_ROOT for property sheet
  1095. RegDeleteKeyU(HKEY_CLASSES_ROOT,
  1096. rgGuidEntry[dwRegIndex].wszKey2);
  1097. //delete a registry entry under HKEY_CLASSES_ROOT for context menu
  1098. if(NULL !=rgGuidEntry[dwRegIndex].wszKey1)
  1099. {
  1100. RegDeleteKeyU(HKEY_CLASSES_ROOT,
  1101. rgGuidEntry[dwRegIndex].wszKey1);
  1102. }
  1103. }
  1104. //2nd, detelet all the registry based context menu and Icon
  1105. //get the count of the reg entries
  1106. dwRegEntry=sizeof(rgwszDelete)/sizeof(rgwszDelete[0]);
  1107. for(dwRegIndex=0; dwRegIndex<dwRegEntry; dwRegIndex++)
  1108. //delete the registry entries
  1109. RegDeleteKeyU(HKEY_CLASSES_ROOT,rgwszDelete[dwRegIndex]);
  1110. //now, delete anything related to the content type
  1111. dwRegEntry=sizeof(rgContentTypeEntry)/sizeof(rgContentTypeEntry[0]);
  1112. for(dwRegIndex=0; dwRegIndex<dwRegEntry; dwRegIndex++)
  1113. {
  1114. wszContentType[0]=L'\0';
  1115. wcscpy(wszContentType, L"MIME\\Database\\Content Type\\");
  1116. wcscat(wszContentType, rgContentTypeEntry[dwRegIndex].wszName);
  1117. RegDeleteKeyU(HKEY_CLASSES_ROOT,wszContentType);
  1118. }
  1119. //3nd, under the classID of &CLSID_CryptPKO, we need to delete
  1120. //the registry shellex\MayChangeDefaultMenu
  1121. dwRegEntry=sizeof(g_CLSIDDefault)/sizeof(g_CLSIDDefault[0]);
  1122. //get the string presentation of the CLSID
  1123. if(0==StringFromGUID2(CLSID_CryptPKO, wszGUID, MAX_STRING_SIZE))
  1124. return S_OK;
  1125. for(dwRegIndex=0; dwRegIndex<dwRegEntry; dwRegIndex++)
  1126. {
  1127. wcscpy(wszDefault, L"CLSID\\");
  1128. wcscat(wszDefault, wszGUID);
  1129. wcscat(wszDefault, g_CLSIDDefault[dwRegIndex]);
  1130. RegDeleteKeyU(HKEY_CLASSES_ROOT,wszDefault);
  1131. }
  1132. return S_OK;
  1133. }
  1134. //--------------------------------------------------------------------------
  1135. //
  1136. // View a CTL context
  1137. //
  1138. //--------------------------------------------------------------------------
  1139. void I_ViewCTL(PCCTL_CONTEXT pCTLContext)
  1140. {
  1141. CRYPTUI_VIEWCTL_STRUCT ViewCTLStruct;
  1142. if(NULL==pCTLContext)
  1143. return;
  1144. //memset
  1145. memset(&ViewCTLStruct, 0, sizeof(ViewCTLStruct));
  1146. ViewCTLStruct.dwSize=sizeof(ViewCTLStruct);
  1147. ViewCTLStruct.pCTLContext=pCTLContext;
  1148. CryptUIDlgViewCTL(&ViewCTLStruct);
  1149. }
  1150. //--------------------------------------------------------------------------
  1151. //
  1152. // View a signer Info
  1153. //
  1154. //--------------------------------------------------------------------------
  1155. /*void I_ViewSignerInfo(HCRYPTMSG hMsg)
  1156. {
  1157. CERT_VIEWSIGNERINFO_STRUCT_W ViewSignerInfoStruct;
  1158. PCMSG_SIGNER_INFO pSignerInfo=NULL;
  1159. HCERTSTORE hCertStore=NULL;
  1160. DWORD cbData=0;
  1161. //get the cert store from the hMsg
  1162. hCertStore=CertOpenStore(CERT_STORE_PROV_MSG,
  1163. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
  1164. NULL,
  1165. 0,
  1166. hMsg);
  1167. if(NULL==hCertStore)
  1168. goto CLEANUP;
  1169. //get the signer info struct
  1170. if(!CryptMsgGetParam(hMsg,
  1171. CMSG_SIGNER_INFO_PARAM,
  1172. 0,
  1173. NULL,
  1174. &cbData))
  1175. goto CLEANUP;
  1176. pSignerInfo=(PCMSG_SIGNER_INFO)malloc(cbData);
  1177. if(NULL==pSignerInfo)
  1178. goto CLEANUP;
  1179. if(!CryptMsgGetParam(hMsg,
  1180. CMSG_SIGNER_INFO_PARAM,
  1181. 0,
  1182. pSignerInfo,
  1183. &cbData))
  1184. goto CLEANUP;
  1185. //Init
  1186. memset(&ViewSignerInfoStruct, 0, sizeof(ViewSignerInfoStruct));
  1187. ViewSignerInfoStruct.dwSize=sizeof(ViewSignerInfoStruct);
  1188. ViewSignerInfoStruct.pSignerInfo=pSignerInfo;
  1189. ViewSignerInfoStruct.cStores=1;
  1190. ViewSignerInfoStruct.rghStores=&hCertStore;
  1191. CertViewSignerInfo_W(&ViewSignerInfoStruct);
  1192. CLEANUP:
  1193. if(hCertStore)
  1194. CertCloseStore(hCertStore, 0);
  1195. if(pSignerInfo)
  1196. free(pSignerInfo);
  1197. } */
  1198. //---------------------------------------------------------------------------------
  1199. //
  1200. // Check is the PKCS signed MSG has a signerInfo attached
  1201. //
  1202. //---------------------------------------------------------------------------------
  1203. BOOL PKCS7WithSignature(HCRYPTMSG hMsg)
  1204. {
  1205. DWORD dwSignerCount=0;
  1206. DWORD cbSignerCount=0;
  1207. if(NULL==hMsg)
  1208. return FALSE;
  1209. cbSignerCount=sizeof(dwSignerCount);
  1210. //get the Param CMSG_SIGNER_COUNT_PARAM on the message handle
  1211. //if 0==CMSG_SIGNER_COUNT_PARAM, there is no signerInfo
  1212. //on the message handle
  1213. if(!CryptMsgGetParam(hMsg,
  1214. CMSG_SIGNER_COUNT_PARAM,
  1215. 0,
  1216. &dwSignerCount,
  1217. &cbSignerCount))
  1218. return FALSE;
  1219. if(0==dwSignerCount)
  1220. return FALSE;
  1221. return TRUE;
  1222. }
  1223. //---------------------------------------------------------------------------------
  1224. //
  1225. // Mime Handler the command line "Open" for CER and CRT file
  1226. //---------------------------------------------------------------------------------
  1227. STDAPI CryptExtOpenCERW(HINSTANCE hinst, HINSTANCE hPrevInstance, LPWSTR lpszCmdLine, int nCmdShow)
  1228. {
  1229. LPWSTR pwszFileName=NULL;
  1230. PCCERT_CONTEXT pCertContext=NULL;
  1231. CRYPTUI_VIEWCERTIFICATE_STRUCT CertViewStruct;
  1232. if (!lpszCmdLine)
  1233. return E_FAIL;
  1234. //get the WCHAR file name
  1235. pwszFileName=lpszCmdLine;
  1236. if(CryptQueryObject(CERT_QUERY_OBJECT_FILE,
  1237. pwszFileName,
  1238. CERT_QUERY_CONTENT_FLAG_CERT,
  1239. CERT_QUERY_FORMAT_FLAG_ALL,
  1240. 0,
  1241. NULL,
  1242. NULL,
  1243. NULL,
  1244. NULL,
  1245. NULL,
  1246. (const void **)&pCertContext))
  1247. {
  1248. //call the Certificate Common Dialogue
  1249. memset(&CertViewStruct, 0, sizeof(CRYPTUI_VIEWCERTIFICATE_STRUCT));
  1250. CertViewStruct.dwSize=sizeof(CRYPTUI_VIEWCERTIFICATE_STRUCT);
  1251. CertViewStruct.pCertContext=pCertContext;
  1252. CryptUIDlgViewCertificate(&CertViewStruct,NULL);
  1253. }
  1254. else
  1255. {
  1256. I_NoticeBox(
  1257. GetLastError(),
  1258. 0,
  1259. NULL,
  1260. IDS_MSG_TITLE,
  1261. IDS_CER_NAME,
  1262. IDS_MSG_INVALID_FILE,
  1263. MB_OK|MB_ICONINFORMATION);
  1264. }
  1265. if(pCertContext)
  1266. CertFreeCertificateContext(pCertContext);
  1267. return S_OK;
  1268. }
  1269. //---------------------------------------------------------------------------------
  1270. //
  1271. // Mime Handler the command line "Open" for CER and CRT file
  1272. //---------------------------------------------------------------------------------
  1273. STDAPI CryptExtOpenCER(HINSTANCE hinst, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
  1274. {
  1275. LPWSTR pwszFileName=NULL;
  1276. HRESULT hr=S_OK;
  1277. if (!lpszCmdLine)
  1278. return E_FAIL;
  1279. //get the WCHAR file name
  1280. pwszFileName=MkWStr(lpszCmdLine);
  1281. if(NULL==pwszFileName)
  1282. return HRESULT_FROM_WIN32(GetLastError());
  1283. hr=CryptExtOpenCERW(hinst, hPrevInstance, pwszFileName, nCmdShow);
  1284. if(pwszFileName)
  1285. FreeWStr(pwszFileName);
  1286. return hr;
  1287. }
  1288. BOOL IsCatalog(PCCTL_CONTEXT pCTLContext)
  1289. {
  1290. BOOL fRet=FALSE;
  1291. if (pCTLContext)
  1292. {
  1293. if (pCTLContext->pCtlInfo)
  1294. {
  1295. if(pCTLContext->pCtlInfo->SubjectUsage.cUsageIdentifier)
  1296. {
  1297. if (pCTLContext->pCtlInfo->SubjectUsage.rgpszUsageIdentifier)
  1298. {
  1299. if (strcmp(pCTLContext->pCtlInfo->SubjectUsage.rgpszUsageIdentifier[0],
  1300. szOID_CATALOG_LIST) == 0)
  1301. {
  1302. fRet = TRUE;
  1303. }
  1304. }
  1305. }
  1306. }
  1307. }
  1308. return fRet;
  1309. }
  1310. //---------------------------------------------------------------------------------
  1311. //
  1312. // Mime Handler the command line "Open" for .CTL file
  1313. //---------------------------------------------------------------------------------
  1314. STDAPI CryptExtOpenCATW(HINSTANCE hinst, HINSTANCE hPrevInstance, LPWSTR lpszCmdLine, int nCmdShow)
  1315. {
  1316. LPWSTR pwszFileName=NULL;
  1317. PCCTL_CONTEXT pCTLContext=NULL;
  1318. if (!lpszCmdLine)
  1319. return E_FAIL;
  1320. //get the WCHAR file name
  1321. pwszFileName=lpszCmdLine;
  1322. if(CryptQueryObject(CERT_QUERY_OBJECT_FILE,
  1323. pwszFileName,
  1324. CERT_QUERY_CONTENT_FLAG_CTL,
  1325. CERT_QUERY_FORMAT_FLAG_ALL,
  1326. 0,
  1327. NULL,
  1328. NULL,
  1329. NULL,
  1330. NULL,
  1331. NULL,
  1332. (const void **)&pCTLContext) &&
  1333. IsCatalog(pCTLContext))
  1334. {
  1335. I_ViewCTL(pCTLContext);
  1336. }
  1337. else
  1338. {
  1339. I_NoticeBox(
  1340. GetLastError(),
  1341. 0,
  1342. NULL,
  1343. IDS_MSG_TITLE,
  1344. IDS_CAT_NAME,
  1345. IDS_MSG_INVALID_FILE,
  1346. MB_OK|MB_ICONINFORMATION);
  1347. }
  1348. if(pCTLContext)
  1349. CertFreeCTLContext(pCTLContext);
  1350. return S_OK;
  1351. }
  1352. //---------------------------------------------------------------------------------
  1353. //
  1354. // Mime Handler the command line "Open" for .CTL file
  1355. //---------------------------------------------------------------------------------
  1356. STDAPI CryptExtOpenCAT(HINSTANCE hinst, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
  1357. {
  1358. LPWSTR pwszFileName=NULL;
  1359. if (!lpszCmdLine)
  1360. return E_FAIL;
  1361. //get the WCHAR file name
  1362. pwszFileName=MkWStr(lpszCmdLine);
  1363. if(NULL==pwszFileName)
  1364. return HRESULT_FROM_WIN32(GetLastError());
  1365. CryptExtOpenCATW(hinst, hPrevInstance, pwszFileName, nCmdShow);
  1366. if(pwszFileName)
  1367. FreeWStr(pwszFileName);
  1368. return S_OK;
  1369. }
  1370. //---------------------------------------------------------------------------------
  1371. //
  1372. // Handler the command line "Add" for .PFX File
  1373. //---------------------------------------------------------------------------------
  1374. STDAPI CryptExtAddPFXW(HINSTANCE hinst, HINSTANCE hPrevInstance, LPWSTR lpszCmdLine, int nCmdShow)
  1375. {
  1376. LPWSTR pwszFileName=NULL;
  1377. CRYPTUI_WIZ_IMPORT_SRC_INFO importSubject;
  1378. if (!lpszCmdLine)
  1379. return E_FAIL;
  1380. //get the WCHAR file name
  1381. pwszFileName=lpszCmdLine;
  1382. if(CryptQueryObject(CERT_QUERY_OBJECT_FILE,
  1383. pwszFileName,
  1384. CERT_QUERY_CONTENT_FLAG_PFX,
  1385. CERT_QUERY_FORMAT_FLAG_ALL,
  1386. 0,
  1387. NULL,
  1388. NULL,
  1389. NULL,
  1390. NULL,
  1391. NULL,
  1392. NULL))
  1393. {
  1394. memset(&importSubject, 0, sizeof(CRYPTUI_WIZ_IMPORT_SRC_INFO));
  1395. importSubject.dwSize=sizeof(CRYPTUI_WIZ_IMPORT_SRC_INFO);
  1396. importSubject.dwSubjectChoice=CRYPTUI_WIZ_IMPORT_SUBJECT_FILE;
  1397. importSubject.pwszFileName=pwszFileName;
  1398. CryptUIWizImport(0,
  1399. NULL,
  1400. NULL,
  1401. &importSubject,
  1402. NULL);
  1403. }
  1404. else
  1405. {
  1406. I_NoticeBox(
  1407. GetLastError(),
  1408. 0,
  1409. NULL,
  1410. IDS_MSG_TITLE,
  1411. IDS_PFX_NAME,
  1412. IDS_MSG_INVALID_FILE,
  1413. MB_OK|MB_ICONINFORMATION);
  1414. }
  1415. return S_OK;
  1416. }
  1417. //---------------------------------------------------------------------------------
  1418. //
  1419. // Handler the command line "Add" for .PFX File
  1420. //---------------------------------------------------------------------------------
  1421. STDAPI CryptExtAddPFX(HINSTANCE hinst, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
  1422. {
  1423. LPWSTR pwszFileName=NULL;
  1424. if (!lpszCmdLine)
  1425. return E_FAIL;
  1426. //get the WCHAR file name
  1427. pwszFileName=MkWStr(lpszCmdLine);
  1428. if(NULL==pwszFileName)
  1429. return HRESULT_FROM_WIN32(GetLastError());
  1430. CryptExtAddPFXW(hinst, hPrevInstance, pwszFileName, nCmdShow);
  1431. if(pwszFileName)
  1432. FreeWStr(pwszFileName);
  1433. return S_OK;
  1434. }
  1435. //---------------------------------------------------------------------------------
  1436. //
  1437. // Handler the command line "Add" for CER and CRT file
  1438. //---------------------------------------------------------------------------------
  1439. STDAPI CryptExtAddCERW(HINSTANCE hinst, HINSTANCE hPrevInstance, LPWSTR lpszCmdLine, int nCmdShow)
  1440. {
  1441. LPWSTR pwszFileName=NULL;
  1442. PCCERT_CONTEXT pCertContext=NULL;
  1443. CRYPTUI_WIZ_IMPORT_SRC_INFO importSubject;
  1444. if (!lpszCmdLine)
  1445. return E_FAIL;
  1446. //get the WCHAR file name
  1447. pwszFileName=lpszCmdLine;
  1448. if(CryptQueryObject(CERT_QUERY_OBJECT_FILE,
  1449. pwszFileName,
  1450. CERT_QUERY_CONTENT_FLAG_CERT,
  1451. CERT_QUERY_FORMAT_FLAG_ALL,
  1452. 0,
  1453. NULL,
  1454. NULL,
  1455. NULL,
  1456. NULL,
  1457. NULL,
  1458. (const void **)&pCertContext))
  1459. {
  1460. memset(&importSubject, 0, sizeof(CRYPTUI_WIZ_IMPORT_SRC_INFO));
  1461. importSubject.dwSize=sizeof(CRYPTUI_WIZ_IMPORT_SRC_INFO);
  1462. importSubject.dwSubjectChoice=CRYPTUI_WIZ_IMPORT_SUBJECT_CERT_CONTEXT;
  1463. importSubject.pCertContext=pCertContext;
  1464. CryptUIWizImport(0,
  1465. NULL,
  1466. NULL,
  1467. &importSubject,
  1468. NULL);
  1469. }
  1470. else
  1471. {
  1472. I_NoticeBox(
  1473. GetLastError(),
  1474. 0,
  1475. NULL,
  1476. IDS_MSG_TITLE,
  1477. IDS_CER_NAME,
  1478. IDS_MSG_INVALID_FILE,
  1479. MB_OK|MB_ICONINFORMATION);
  1480. }
  1481. if(pCertContext)
  1482. CertFreeCertificateContext(pCertContext);
  1483. return S_OK;
  1484. }
  1485. //---------------------------------------------------------------------------------
  1486. //
  1487. // Handler the command line "Add" for CER and CRT file
  1488. //---------------------------------------------------------------------------------
  1489. STDAPI CryptExtAddCER(HINSTANCE hinst, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
  1490. {
  1491. LPWSTR pwszFileName=NULL;
  1492. if (!lpszCmdLine)
  1493. return E_FAIL;
  1494. //get the WCHAR file name
  1495. pwszFileName=MkWStr(lpszCmdLine);
  1496. if(NULL==pwszFileName)
  1497. return HRESULT_FROM_WIN32(GetLastError());
  1498. CryptExtAddCERW(hinst, hPrevInstance,pwszFileName, nCmdShow);
  1499. if(pwszFileName)
  1500. FreeWStr(pwszFileName);
  1501. return S_OK;
  1502. }
  1503. //---------------------------------------------------------------------------------
  1504. //
  1505. // Mime Handler the command line "Open" for .CTL file
  1506. //---------------------------------------------------------------------------------
  1507. STDAPI CryptExtOpenCTLW(HINSTANCE hinst, HINSTANCE hPrevInstance, LPWSTR lpszCmdLine, int nCmdShow)
  1508. {
  1509. LPWSTR pwszFileName=NULL;
  1510. PCCTL_CONTEXT pCTLContext=NULL;
  1511. if (!lpszCmdLine)
  1512. return E_FAIL;
  1513. //get the WCHAR file name
  1514. pwszFileName=lpszCmdLine;
  1515. if(CryptQueryObject(CERT_QUERY_OBJECT_FILE,
  1516. pwszFileName,
  1517. CERT_QUERY_CONTENT_FLAG_CTL,
  1518. CERT_QUERY_FORMAT_FLAG_ALL,
  1519. 0,
  1520. NULL,
  1521. NULL,
  1522. NULL,
  1523. NULL,
  1524. NULL,
  1525. (const void **)&pCTLContext))
  1526. {
  1527. I_ViewCTL(pCTLContext);
  1528. }
  1529. else
  1530. {
  1531. I_NoticeBox(
  1532. GetLastError(),
  1533. 0,
  1534. NULL,
  1535. IDS_MSG_TITLE,
  1536. IDS_STL_NAME,
  1537. IDS_MSG_INVALID_FILE,
  1538. MB_OK|MB_ICONINFORMATION);
  1539. }
  1540. if(pCTLContext)
  1541. CertFreeCTLContext(pCTLContext);
  1542. return S_OK;
  1543. }
  1544. //---------------------------------------------------------------------------------
  1545. //
  1546. // Mime Handler the command line "Open" for .CTL file
  1547. //---------------------------------------------------------------------------------
  1548. STDAPI CryptExtOpenCTL(HINSTANCE hinst, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
  1549. {
  1550. LPWSTR pwszFileName=NULL;
  1551. if (!lpszCmdLine)
  1552. return E_FAIL;
  1553. //get the WCHAR file name
  1554. pwszFileName=MkWStr(lpszCmdLine);
  1555. if(NULL==pwszFileName)
  1556. return HRESULT_FROM_WIN32(GetLastError());
  1557. CryptExtOpenCTLW(hinst, hPrevInstance, pwszFileName, nCmdShow);
  1558. if(pwszFileName)
  1559. FreeWStr(pwszFileName);
  1560. return S_OK;
  1561. }
  1562. //---------------------------------------------------------------------------------
  1563. //
  1564. // Handler the command line "Add" for .CTL file
  1565. //---------------------------------------------------------------------------------
  1566. STDAPI CryptExtAddCTLW(HINSTANCE hinst, HINSTANCE hPrevInstance, LPWSTR lpszCmdLine, int nCmdShow)
  1567. {
  1568. LPWSTR pwszFileName=NULL;
  1569. PCCTL_CONTEXT pCTLContext=NULL;
  1570. CRYPTUI_WIZ_IMPORT_SRC_INFO importSubject;
  1571. if (!lpszCmdLine)
  1572. return E_FAIL;
  1573. //get the WCHAR file name
  1574. pwszFileName=lpszCmdLine;
  1575. if(CryptQueryObject(CERT_QUERY_OBJECT_FILE,
  1576. pwszFileName,
  1577. CERT_QUERY_CONTENT_FLAG_CTL,
  1578. CERT_QUERY_FORMAT_FLAG_ALL,
  1579. 0,
  1580. NULL,
  1581. NULL,
  1582. NULL,
  1583. NULL,
  1584. NULL,
  1585. (const void **)&pCTLContext))
  1586. {
  1587. memset(&importSubject, 0, sizeof(CRYPTUI_WIZ_IMPORT_SRC_INFO));
  1588. importSubject.dwSize=sizeof(CRYPTUI_WIZ_IMPORT_SRC_INFO);
  1589. importSubject.dwSubjectChoice=CRYPTUI_WIZ_IMPORT_SUBJECT_CTL_CONTEXT;
  1590. importSubject.pCTLContext=pCTLContext;
  1591. CryptUIWizImport(0,
  1592. NULL,
  1593. NULL,
  1594. &importSubject,
  1595. NULL);
  1596. }
  1597. else
  1598. {
  1599. I_NoticeBox(
  1600. GetLastError(),
  1601. 0,
  1602. NULL,
  1603. IDS_MSG_TITLE,
  1604. IDS_STL_NAME,
  1605. IDS_MSG_INVALID_FILE,
  1606. MB_OK|MB_ICONINFORMATION);
  1607. }
  1608. if(pCTLContext)
  1609. CertFreeCTLContext(pCTLContext);
  1610. return S_OK;
  1611. }
  1612. //---------------------------------------------------------------------------------
  1613. //
  1614. // Handler the command line "Add" for .CTL file
  1615. //---------------------------------------------------------------------------------
  1616. STDAPI CryptExtAddCTL(HINSTANCE hinst, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
  1617. {
  1618. LPWSTR pwszFileName=NULL;
  1619. if (!lpszCmdLine)
  1620. return E_FAIL;
  1621. //get the WCHAR file name
  1622. pwszFileName=MkWStr(lpszCmdLine);
  1623. if(NULL==pwszFileName)
  1624. return HRESULT_FROM_WIN32(GetLastError());
  1625. CryptExtAddCTLW(hinst, hPrevInstance, pwszFileName, nCmdShow);
  1626. if(pwszFileName)
  1627. FreeWStr(pwszFileName);
  1628. return S_OK;
  1629. }
  1630. //---------------------------------------------------------------------------------
  1631. //
  1632. // Mime Handler the command line "Open" for .CRL file
  1633. //---------------------------------------------------------------------------------
  1634. STDAPI CryptExtOpenCRLW(HINSTANCE hinst, HINSTANCE hPrevInstance, LPWSTR lpszCmdLine, int nCmdShow)
  1635. {
  1636. LPWSTR pwszFileName=NULL;
  1637. PCCRL_CONTEXT pCRLContext=NULL;
  1638. CRYPTUI_VIEWCRL_STRUCT CRLViewStruct;
  1639. if (!lpszCmdLine)
  1640. return E_FAIL;
  1641. //get the WCHAR file name
  1642. pwszFileName=lpszCmdLine;
  1643. if(CryptQueryObject(CERT_QUERY_OBJECT_FILE,
  1644. pwszFileName,
  1645. CERT_QUERY_CONTENT_FLAG_CRL,
  1646. CERT_QUERY_FORMAT_FLAG_ALL,
  1647. 0,
  1648. NULL,
  1649. NULL,
  1650. NULL,
  1651. NULL,
  1652. NULL,
  1653. (const void **)&pCRLContext))
  1654. {
  1655. //call the CRL view dialogue
  1656. memset(&CRLViewStruct, 0, sizeof(CRYPTUI_VIEWCRL_STRUCT));
  1657. CRLViewStruct.dwSize=sizeof(CRYPTUI_VIEWCRL_STRUCT);
  1658. CRLViewStruct.pCRLContext=pCRLContext;
  1659. CryptUIDlgViewCRL(&CRLViewStruct);
  1660. }
  1661. else
  1662. {
  1663. I_NoticeBox(
  1664. GetLastError(),
  1665. 0,
  1666. NULL,
  1667. IDS_MSG_TITLE,
  1668. IDS_CRL_NAME,
  1669. IDS_MSG_INVALID_FILE,
  1670. MB_OK|MB_ICONINFORMATION);
  1671. }
  1672. if(pCRLContext)
  1673. CertFreeCRLContext(pCRLContext);
  1674. return S_OK;
  1675. }
  1676. //---------------------------------------------------------------------------------
  1677. //
  1678. // Mime Handler the command line "Open" for .CRL file
  1679. //---------------------------------------------------------------------------------
  1680. STDAPI CryptExtOpenCRL(HINSTANCE hinst, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
  1681. {
  1682. LPWSTR pwszFileName=NULL;
  1683. if (!lpszCmdLine)
  1684. return E_FAIL;
  1685. //get the WCHAR file name
  1686. pwszFileName=MkWStr(lpszCmdLine);
  1687. if(NULL==pwszFileName)
  1688. return HRESULT_FROM_WIN32(GetLastError());
  1689. CryptExtOpenCRLW(hinst, hPrevInstance, pwszFileName, nCmdShow);
  1690. if(pwszFileName)
  1691. FreeWStr(pwszFileName);
  1692. return S_OK;
  1693. }
  1694. //---------------------------------------------------------------------------------
  1695. //
  1696. // Handler the command line "Add" for .CRL file
  1697. //---------------------------------------------------------------------------------
  1698. STDAPI CryptExtAddCRLW(HINSTANCE hinst, HINSTANCE hPrevInstance, LPWSTR lpszCmdLine, int nCmdShow)
  1699. {
  1700. LPWSTR pwszFileName=NULL;
  1701. PCCRL_CONTEXT pCRLContext=NULL;
  1702. CRYPTUI_WIZ_IMPORT_SRC_INFO importSubject;
  1703. if (!lpszCmdLine)
  1704. return E_FAIL;
  1705. //get the WCHAR file name
  1706. pwszFileName=lpszCmdLine;
  1707. if(CryptQueryObject(CERT_QUERY_OBJECT_FILE,
  1708. pwszFileName,
  1709. CERT_QUERY_CONTENT_FLAG_CRL,
  1710. CERT_QUERY_FORMAT_FLAG_ALL,
  1711. 0,
  1712. NULL,
  1713. NULL,
  1714. NULL,
  1715. NULL,
  1716. NULL,
  1717. (const void **)&pCRLContext))
  1718. {
  1719. memset(&importSubject, 0, sizeof(CRYPTUI_WIZ_IMPORT_SRC_INFO));
  1720. importSubject.dwSize=sizeof(CRYPTUI_WIZ_IMPORT_SRC_INFO);
  1721. importSubject.dwSubjectChoice=CRYPTUI_WIZ_IMPORT_SUBJECT_CRL_CONTEXT;
  1722. importSubject.pCRLContext=pCRLContext;
  1723. CryptUIWizImport(0,
  1724. NULL,
  1725. NULL,
  1726. &importSubject,
  1727. NULL);
  1728. }
  1729. else
  1730. {
  1731. I_NoticeBox(
  1732. GetLastError(),
  1733. 0,
  1734. NULL,
  1735. IDS_MSG_TITLE,
  1736. IDS_CRL_NAME,
  1737. IDS_MSG_INVALID_FILE,
  1738. MB_OK|MB_ICONINFORMATION);
  1739. }
  1740. if(pCRLContext)
  1741. CertFreeCRLContext(pCRLContext);
  1742. return S_OK;
  1743. }
  1744. //---------------------------------------------------------------------------------
  1745. //
  1746. // Handler the command line "Add" for .CRL file
  1747. //---------------------------------------------------------------------------------
  1748. STDAPI CryptExtAddCRL(HINSTANCE hinst, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
  1749. {
  1750. LPWSTR pwszFileName=NULL;
  1751. if (!lpszCmdLine)
  1752. return E_FAIL;
  1753. //get the WCHAR file name
  1754. pwszFileName=MkWStr(lpszCmdLine);
  1755. if(NULL==pwszFileName)
  1756. return HRESULT_FROM_WIN32(GetLastError());
  1757. CryptExtAddCRLW(hinst, hPrevInstance, pwszFileName, nCmdShow);
  1758. if(pwszFileName)
  1759. FreeWStr(pwszFileName);
  1760. return S_OK;
  1761. }
  1762. //---------------------------------------------------------------------------------
  1763. //
  1764. // Mime Handler the command line "Open" for .SPC, .P7S, .P7B, .P7M file
  1765. //---------------------------------------------------------------------------------
  1766. STDAPI CryptExtOpenPKCS7W(HINSTANCE hinst, HINSTANCE hPrevInstance, LPWSTR lpszCmdLine, int nCmdShow)
  1767. {
  1768. LPWSTR pwszFileName=NULL;
  1769. HCERTSTORE hCertStore=NULL;
  1770. HCRYPTMSG hMsg=NULL;
  1771. DWORD dwContentType=0;
  1772. if (!lpszCmdLine)
  1773. return E_FAIL;
  1774. //get the WCHAR file name
  1775. pwszFileName=lpszCmdLine;
  1776. //check the object type. Make sure the PKCS7 is not embedded
  1777. if(CryptQueryObject(CERT_QUERY_OBJECT_FILE,
  1778. pwszFileName,
  1779. CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED,
  1780. CERT_QUERY_FORMAT_FLAG_ALL,
  1781. 0,
  1782. NULL,
  1783. &dwContentType,
  1784. NULL,
  1785. &hCertStore,
  1786. &hMsg,
  1787. NULL) )
  1788. {
  1789. LauchCertMgr(pwszFileName);
  1790. }
  1791. else
  1792. {
  1793. I_NoticeBox(
  1794. GetLastError(),
  1795. 0,
  1796. NULL,
  1797. IDS_MSG_TITLE,
  1798. IDS_PKCS7_NAME,
  1799. IDS_MSG_INVALID_FILE,
  1800. MB_OK|MB_ICONINFORMATION);
  1801. }
  1802. if(hCertStore)
  1803. CertCloseStore(hCertStore, 0);
  1804. if(hMsg)
  1805. CryptMsgClose(hMsg);
  1806. return S_OK;
  1807. }
  1808. //---------------------------------------------------------------------------------
  1809. //
  1810. // Mime Handler the command line "Open" for .SPC, .P7S, .P7B, .P7M file
  1811. //---------------------------------------------------------------------------------
  1812. STDAPI CryptExtOpenPKCS7(HINSTANCE hinst, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
  1813. {
  1814. LPWSTR pwszFileName=NULL;
  1815. if (!lpszCmdLine)
  1816. return E_FAIL;
  1817. //get the WCHAR file name
  1818. pwszFileName=MkWStr(lpszCmdLine);
  1819. if(NULL==pwszFileName)
  1820. return HRESULT_FROM_WIN32(GetLastError());
  1821. CryptExtOpenPKCS7W(hinst, hPrevInstance, pwszFileName, nCmdShow);
  1822. if(pwszFileName)
  1823. FreeWStr(pwszFileName);
  1824. return S_OK;
  1825. }
  1826. //---------------------------------------------------------------------------------
  1827. //
  1828. // Handler the command line "Add" for .SPC file
  1829. //---------------------------------------------------------------------------------
  1830. STDAPI CryptExtAddSPCW(HINSTANCE hinst, HINSTANCE hPrevInstance, LPWSTR lpszCmdLine, int nCmdShow)
  1831. {
  1832. LPWSTR pwszFileName=NULL;
  1833. HCERTSTORE hCertStore=NULL;
  1834. CRYPTUI_WIZ_IMPORT_SRC_INFO importSubject;
  1835. if (!lpszCmdLine)
  1836. return E_FAIL;
  1837. //get the WCHAR file name
  1838. pwszFileName=lpszCmdLine;
  1839. //check the object type. Make sure the PKCS7 is not embedded
  1840. if(CryptQueryObject(CERT_QUERY_OBJECT_FILE,
  1841. pwszFileName,
  1842. CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED,
  1843. CERT_QUERY_FORMAT_FLAG_ALL,
  1844. 0,
  1845. NULL,
  1846. NULL,
  1847. NULL,
  1848. &hCertStore,
  1849. NULL,
  1850. NULL) )
  1851. {
  1852. memset(&importSubject, 0, sizeof(CRYPTUI_WIZ_IMPORT_SRC_INFO));
  1853. importSubject.dwSize=sizeof(CRYPTUI_WIZ_IMPORT_SRC_INFO);
  1854. importSubject.dwSubjectChoice=CRYPTUI_WIZ_IMPORT_SUBJECT_FILE;
  1855. importSubject.pwszFileName=pwszFileName;
  1856. CryptUIWizImport(0,
  1857. NULL,
  1858. NULL,
  1859. &importSubject,
  1860. NULL);
  1861. }
  1862. else
  1863. {
  1864. I_NoticeBox(
  1865. GetLastError(),
  1866. 0,
  1867. NULL,
  1868. IDS_MSG_TITLE,
  1869. IDS_SPC_NAME,
  1870. IDS_MSG_INVALID_FILE,
  1871. MB_OK|MB_ICONINFORMATION);
  1872. }
  1873. return S_OK;
  1874. }
  1875. //---------------------------------------------------------------------------------
  1876. //
  1877. // Handler the command line "Add" for .SPC file
  1878. //---------------------------------------------------------------------------------
  1879. STDAPI CryptExtAddSPC(HINSTANCE hinst, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
  1880. {
  1881. LPWSTR pwszFileName=NULL;
  1882. if (!lpszCmdLine)
  1883. return E_FAIL;
  1884. //get the WCHAR file name
  1885. pwszFileName=MkWStr(lpszCmdLine);
  1886. if(NULL==pwszFileName)
  1887. return HRESULT_FROM_WIN32(GetLastError());
  1888. CryptExtAddSPCW(hinst, hPrevInstance, pwszFileName, nCmdShow);
  1889. if(pwszFileName)
  1890. FreeWStr(pwszFileName);
  1891. return S_OK;
  1892. }
  1893. //---------------------------------------------------------------------------------
  1894. //
  1895. // Mime Handler the command line "Open" for .p7r file. This file is returned by
  1896. // certificate authority
  1897. //---------------------------------------------------------------------------------
  1898. STDAPI CryptExtOpenP7RW(HINSTANCE hinst, HINSTANCE hPrevInstance, LPWSTR lpszCmdLine, int nCmdShow)
  1899. {
  1900. CRYPT_DATA_BLOB PKCS7Blob;
  1901. IEnroll *pIEnroll=NULL;
  1902. LPWSTR pwszFileName=NULL;
  1903. PCCERT_CONTEXT pCertContext=NULL;
  1904. CRYPTUI_VIEWCERTIFICATE_STRUCT ViewCertStruct;
  1905. UINT ids=IDS_INSTALL_CERT_SUCCEEDED;
  1906. //init
  1907. memset(&PKCS7Blob, 0, sizeof(CRYPT_DATA_BLOB));
  1908. //get the WCHAR file name
  1909. pwszFileName=lpszCmdLine;
  1910. if(FAILED(CoInitialize(NULL)))
  1911. {
  1912. ids=IDS_NO_XENROLL;
  1913. goto CLEANUP;
  1914. }
  1915. //initialize information for xEnroll
  1916. if(FAILED(CoCreateInstance(CLSID_CEnroll,
  1917. NULL,CLSCTX_INPROC_SERVER,IID_IEnroll,
  1918. (LPVOID *)&pIEnroll)))
  1919. {
  1920. ids=IDS_NO_XENROLL;
  1921. goto CLEANUP;
  1922. }
  1923. //get the BLOB from the file
  1924. if(S_OK != RetrieveBLOBFromFile(pwszFileName,&(PKCS7Blob.cbData),
  1925. &(PKCS7Blob.pbData)))
  1926. {
  1927. ids=IDS_INVALID_P7R_FILE;
  1928. goto CLEANUP;
  1929. }
  1930. //get the certifcate context
  1931. if(NULL==(pCertContext=pIEnroll->getCertContextFromPKCS7(&PKCS7Blob)))
  1932. {
  1933. ids=IDS_INVALID_P7R_FILE;
  1934. goto CLEANUP;
  1935. }
  1936. memset(&ViewCertStruct, 0, sizeof(CRYPTUI_VIEWCERTIFICATE_STRUCT));
  1937. ViewCertStruct.dwSize=sizeof(CRYPTUI_VIEWCERTIFICATE_STRUCT);
  1938. ViewCertStruct.hwndParent=NULL;
  1939. ViewCertStruct.dwFlags=CRYPTUI_ACCEPT_DECLINE_STYLE;
  1940. ViewCertStruct.pCertContext=pCertContext;
  1941. ViewCertStruct.cStores=0;
  1942. ViewCertStruct.rghStores=NULL;
  1943. if(!CryptUIDlgViewCertificate(&ViewCertStruct,NULL))
  1944. goto CLEANUP;
  1945. if(S_OK !=(pIEnroll->acceptPKCS7Blob(&PKCS7Blob)))
  1946. {
  1947. ids=IDS_FAIL_TO_INSTALL;
  1948. goto CLEANUP;
  1949. }
  1950. CLEANUP:
  1951. I_MessageBox(
  1952. NULL,
  1953. ids,
  1954. IDS_P7R_NAME,
  1955. NULL,
  1956. MB_OK|MB_APPLMODAL);
  1957. if(PKCS7Blob.pbData)
  1958. UnmapViewOfFile(PKCS7Blob.pbData);
  1959. if(pCertContext)
  1960. CertFreeCertificateContext(pCertContext);
  1961. if(pIEnroll)
  1962. pIEnroll->Release();
  1963. CoUninitialize( );
  1964. return S_OK;
  1965. }
  1966. //---------------------------------------------------------------------------------
  1967. //
  1968. // Mime Handler the command line "Open" for .p7r file. This file is returned by
  1969. // certificate authority
  1970. //---------------------------------------------------------------------------------
  1971. STDAPI CryptExtOpenP7R(HINSTANCE hinst, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
  1972. {
  1973. LPWSTR pwszFileName=NULL;
  1974. //get the WCHAR file name
  1975. pwszFileName=MkWStr(lpszCmdLine);
  1976. if(NULL==pwszFileName)
  1977. return HRESULT_FROM_WIN32(GetLastError());
  1978. CryptExtOpenP7RW(hinst, hPrevInstance, pwszFileName, nCmdShow);
  1979. if(pwszFileName)
  1980. FreeWStr(pwszFileName);
  1981. return S_OK;
  1982. }
  1983. //---------------------------------------------------------------------------------
  1984. //
  1985. // Mime Handler the command line "Open" for .p7r file. This file is returned by
  1986. // certificate authority
  1987. //---------------------------------------------------------------------------------
  1988. STDAPI CryptExtAddP7RW(HINSTANCE hinst, HINSTANCE hPrevInstance, LPWSTR lpszCmdLine, int nCmdShow)
  1989. {
  1990. CRYPT_DATA_BLOB PKCS7Blob;
  1991. IEnroll *pIEnroll=NULL;
  1992. LPWSTR pwszFileName=NULL;
  1993. UINT ids=IDS_INSTALL_CERT_SUCCEEDED;
  1994. //init
  1995. memset(&PKCS7Blob, 0, sizeof(CRYPT_DATA_BLOB));
  1996. //get the WCHAR file name
  1997. pwszFileName=lpszCmdLine;
  1998. if(FAILED(CoInitialize(NULL)))
  1999. {
  2000. ids=IDS_NO_XENROLL;
  2001. goto CLEANUP;
  2002. }
  2003. //initialize information for xEnroll
  2004. if(FAILED(CoCreateInstance(CLSID_CEnroll,
  2005. NULL,CLSCTX_INPROC_SERVER,IID_IEnroll,
  2006. (LPVOID *)&pIEnroll)))
  2007. {
  2008. ids=IDS_NO_XENROLL;
  2009. goto CLEANUP;
  2010. }
  2011. //get the BLOB from the file
  2012. if(S_OK != RetrieveBLOBFromFile(pwszFileName,&(PKCS7Blob.cbData),
  2013. &(PKCS7Blob.pbData)))
  2014. {
  2015. ids=IDS_INVALID_P7R_FILE;
  2016. goto CLEANUP;
  2017. }
  2018. if(S_OK !=(pIEnroll->acceptPKCS7Blob(&PKCS7Blob)))
  2019. {
  2020. ids=IDS_FAIL_TO_INSTALL;
  2021. goto CLEANUP;
  2022. }
  2023. CLEANUP:
  2024. I_MessageBox(
  2025. NULL,
  2026. ids,
  2027. IDS_P7R_NAME,
  2028. NULL,
  2029. MB_OK|MB_APPLMODAL);
  2030. if(PKCS7Blob.pbData)
  2031. UnmapViewOfFile(PKCS7Blob.pbData);
  2032. if(pIEnroll)
  2033. pIEnroll->Release();
  2034. CoUninitialize( );
  2035. return S_OK;
  2036. }
  2037. //---------------------------------------------------------------------------------
  2038. //
  2039. // Mime Handler the command line "Open" for .p7r file. This file is returned by
  2040. // certificate authority
  2041. //---------------------------------------------------------------------------------
  2042. STDAPI CryptExtAddP7R(HINSTANCE hinst, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
  2043. {
  2044. LPWSTR pwszFileName=NULL;
  2045. //get the WCHAR file name
  2046. pwszFileName=MkWStr(lpszCmdLine);
  2047. if(NULL==pwszFileName)
  2048. return HRESULT_FROM_WIN32(GetLastError());
  2049. CryptExtAddP7RW(hinst, hPrevInstance, pwszFileName, nCmdShow);
  2050. if(pwszFileName)
  2051. FreeWStr(pwszFileName);
  2052. return S_OK;
  2053. }
  2054. //---------------------------------------------------------------------------------
  2055. //
  2056. // Mime Handler the command line "Open" for .sst file
  2057. //---------------------------------------------------------------------------------
  2058. STDAPI CryptExtOpenSTRW(HINSTANCE hinst, HINSTANCE hPrevInstance, LPWSTR lpszCmdLine, int nCmdShow)
  2059. {
  2060. LPWSTR pwszFileName=NULL;
  2061. HCERTSTORE hCertStore=NULL;
  2062. if (!lpszCmdLine)
  2063. return E_FAIL;
  2064. //get the WCHAR file name
  2065. pwszFileName=lpszCmdLine;
  2066. //check the object type.
  2067. if(CryptQueryObject(CERT_QUERY_OBJECT_FILE,
  2068. pwszFileName,
  2069. CERT_QUERY_CONTENT_FLAG_SERIALIZED_STORE |
  2070. CERT_QUERY_CONTENT_FLAG_SERIALIZED_CERT |
  2071. CERT_QUERY_CONTENT_FLAG_SERIALIZED_CTL |
  2072. CERT_QUERY_CONTENT_FLAG_SERIALIZED_CRL,
  2073. CERT_QUERY_FORMAT_FLAG_ALL,
  2074. 0,
  2075. NULL,
  2076. NULL,
  2077. NULL,
  2078. &hCertStore,
  2079. NULL,
  2080. NULL))
  2081. {
  2082. LauchCertMgr(pwszFileName);
  2083. }
  2084. else
  2085. {
  2086. I_NoticeBox(
  2087. GetLastError(),
  2088. 0,
  2089. NULL,
  2090. IDS_MSG_TITLE,
  2091. IDS_SST_NAME,
  2092. IDS_MSG_INVALID_FILE,
  2093. MB_OK|MB_ICONINFORMATION);
  2094. }
  2095. if(hCertStore)
  2096. CertCloseStore(hCertStore, 0);
  2097. return S_OK;
  2098. }
  2099. //---------------------------------------------------------------------------------
  2100. //
  2101. // Mime Handler the command line "Open" for .sst file
  2102. //---------------------------------------------------------------------------------
  2103. STDAPI CryptExtOpenSTR(HINSTANCE hinst, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
  2104. {
  2105. LPWSTR pwszFileName=NULL;
  2106. if (!lpszCmdLine)
  2107. return E_FAIL;
  2108. //get the WCHAR file name
  2109. pwszFileName=MkWStr(lpszCmdLine);
  2110. if(NULL==pwszFileName)
  2111. return HRESULT_FROM_WIN32(GetLastError());
  2112. CryptExtOpenSTRW(hinst, hPrevInstance, pwszFileName, nCmdShow);
  2113. if(pwszFileName)
  2114. FreeWStr(pwszFileName);
  2115. return S_OK;
  2116. }
  2117. //---------------------------------------------------------------------------------
  2118. //
  2119. // Mime Handler the command line "Open" for .p10 file
  2120. //---------------------------------------------------------------------------------
  2121. STDAPI CryptExtOpenP10W(HINSTANCE hinst, HINSTANCE hPrevInstance, LPWSTR lpszCmdLine, int nCmdShow)
  2122. {
  2123. LPWSTR pwszFileName=NULL;
  2124. if (!lpszCmdLine)
  2125. return E_FAIL;
  2126. //get the WCHAR file name
  2127. pwszFileName=lpszCmdLine;
  2128. //check the object type.
  2129. if(CryptQueryObject(CERT_QUERY_OBJECT_FILE,
  2130. pwszFileName,
  2131. CERT_QUERY_CONTENT_FLAG_PKCS10,
  2132. CERT_QUERY_FORMAT_FLAG_ALL,
  2133. 0,
  2134. NULL,
  2135. NULL,
  2136. NULL,
  2137. NULL,
  2138. NULL,
  2139. NULL))
  2140. {
  2141. I_NoticeBox(
  2142. 0,
  2143. 0,
  2144. NULL,
  2145. IDS_MSG_TITLE,
  2146. IDS_P10_NAME,
  2147. IDS_MSG_VALID_FILE,
  2148. MB_OK|MB_ICONINFORMATION);
  2149. }
  2150. else
  2151. {
  2152. I_NoticeBox(
  2153. GetLastError(),
  2154. 0,
  2155. NULL,
  2156. IDS_MSG_TITLE,
  2157. IDS_P10_NAME,
  2158. IDS_MSG_INVALID_FILE,
  2159. MB_OK|MB_ICONINFORMATION);
  2160. }
  2161. return S_OK;
  2162. }
  2163. //---------------------------------------------------------------------------------
  2164. //
  2165. // Mime Handler the command line "Open" for .p10 file
  2166. //---------------------------------------------------------------------------------
  2167. STDAPI CryptExtOpenP10(HINSTANCE hinst, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
  2168. {
  2169. LPWSTR pwszFileName=NULL;
  2170. if (!lpszCmdLine)
  2171. return E_FAIL;
  2172. //get the WCHAR file name
  2173. pwszFileName=MkWStr(lpszCmdLine);
  2174. if(NULL==pwszFileName)
  2175. return HRESULT_FROM_WIN32(GetLastError());
  2176. CryptExtOpenP10W(hinst, hPrevInstance, pwszFileName, nCmdShow);
  2177. if(pwszFileName)
  2178. FreeWStr(pwszFileName);
  2179. return S_OK;
  2180. }
  2181. //--------------------------------------------------------------------------------
  2182. //
  2183. //get the bytes from the file name
  2184. //
  2185. //---------------------------------------------------------------------------------
  2186. HRESULT RetrieveBLOBFromFile(LPWSTR pwszFileName,DWORD *pcb,BYTE **ppb)
  2187. {
  2188. HRESULT hr=E_FAIL;
  2189. HANDLE hFile=NULL;
  2190. HANDLE hFileMapping=NULL;
  2191. DWORD cbData=0;
  2192. BYTE *pbData=0;
  2193. DWORD cbHighSize=0;
  2194. if(!pcb || !ppb || !pwszFileName)
  2195. return E_INVALIDARG;
  2196. *ppb=NULL;
  2197. *pcb=0;
  2198. if ((hFile = CreateFileU(pwszFileName,
  2199. GENERIC_READ,
  2200. FILE_SHARE_READ,
  2201. NULL, // lpsa
  2202. OPEN_EXISTING,
  2203. FILE_ATTRIBUTE_NORMAL,
  2204. NULL)) == INVALID_HANDLE_VALUE)
  2205. {
  2206. hr=HRESULT_FROM_WIN32(GetLastError());
  2207. goto CLEANUP;
  2208. }
  2209. if((cbData = GetFileSize(hFile, &cbHighSize)) == 0xffffffff)
  2210. {
  2211. hr=HRESULT_FROM_WIN32(GetLastError());
  2212. goto CLEANUP;
  2213. }
  2214. //we do not handle file more than 4G bytes
  2215. if(cbHighSize != 0)
  2216. {
  2217. hr=E_FAIL;
  2218. goto CLEANUP;
  2219. }
  2220. //create a file mapping object
  2221. if(NULL == (hFileMapping=CreateFileMapping(
  2222. hFile,
  2223. NULL,
  2224. PAGE_READONLY,
  2225. 0,
  2226. 0,
  2227. NULL)))
  2228. {
  2229. hr=HRESULT_FROM_WIN32(GetLastError());
  2230. goto CLEANUP;
  2231. }
  2232. //create a view of the file
  2233. if(NULL == (pbData=(BYTE *)MapViewOfFile(
  2234. hFileMapping,
  2235. FILE_MAP_READ,
  2236. 0,
  2237. 0,
  2238. cbData)))
  2239. {
  2240. hr=HRESULT_FROM_WIN32(GetLastError());
  2241. goto CLEANUP;
  2242. }
  2243. hr=S_OK;
  2244. *pcb=cbData;
  2245. *ppb=pbData;
  2246. CLEANUP:
  2247. if(hFile)
  2248. CloseHandle(hFile);
  2249. if(hFileMapping)
  2250. CloseHandle(hFileMapping);
  2251. return hr;
  2252. }
  2253. //-----------------------------------------------------------------------
  2254. // Private implementation of the message box
  2255. //------------------------------------------------------------------------
  2256. int I_NoticeBox(
  2257. DWORD dwError,
  2258. DWORD dwFlags,
  2259. HWND hWnd,
  2260. UINT idsTitle,
  2261. UINT idsFileName,
  2262. UINT idsMsgFormat,
  2263. UINT uType)
  2264. {
  2265. WCHAR wszTitle[MAX_STRING_SIZE];
  2266. WCHAR wszFileName[MAX_STRING_SIZE];
  2267. WCHAR wszMsg[MAX_STRING_SIZE];
  2268. WCHAR wszMsgFormat[MAX_STRING_SIZE];
  2269. if(!LoadStringU(g_hmodThisDll, idsTitle, wszTitle, sizeof(wszTitle) / sizeof(wszTitle[0])))
  2270. return 0;
  2271. if(!LoadStringU(g_hmodThisDll, idsFileName, wszFileName, sizeof(wszFileName) / sizeof(wszFileName[0])))
  2272. return 0;
  2273. if(E_ACCESSDENIED == dwError)
  2274. {
  2275. if(!LoadStringU(g_hmodThisDll, IDS_ACCESS_DENIED, wszMsgFormat, sizeof(wszMsgFormat) / sizeof(wszMsgFormat[0])))
  2276. return 0;
  2277. }
  2278. else
  2279. {
  2280. if(!LoadStringU(g_hmodThisDll, idsMsgFormat, wszMsgFormat, sizeof(wszMsgFormat) / sizeof(wszMsgFormat[0])))
  2281. return 0;
  2282. }
  2283. //make the string
  2284. #if (0) // DSIE: Security code reviewed 02/19/2002.
  2285. // This can overflow anytime.
  2286. if(0 == swprintf(wszMsg, wszMsgFormat, wszFileName))
  2287. #else
  2288. if(0 == _snwprintf(wszMsg, sizeof(wszMsg) / sizeof(wszMsg[0]), wszMsgFormat, wszFileName))
  2289. #endif
  2290. return 0;
  2291. return MessageBoxU(hWnd, wszMsg, wszTitle, uType);
  2292. }
  2293. //-----------------------------------------------------------------------
  2294. // Private implementation of the message box
  2295. //------------------------------------------------------------------------
  2296. int I_MessageBox(
  2297. HWND hWnd,
  2298. UINT idsText,
  2299. UINT idsCaption,
  2300. LPCWSTR pwszCaption,
  2301. UINT uType
  2302. )
  2303. {
  2304. WCHAR wszText[MAX_STRING_SIZE];
  2305. WCHAR wszCaption[MAX_STRING_SIZE];
  2306. UINT intReturn=0;
  2307. //get the caption string
  2308. if(NULL == pwszCaption)
  2309. {
  2310. if(!LoadStringU(g_hmodThisDll, idsCaption, wszCaption, sizeof(wszCaption) / sizeof(wszCaption[0])))
  2311. return 0;
  2312. }
  2313. //get the text string
  2314. if(!LoadStringU(g_hmodThisDll, idsText, wszText, sizeof(wszText) / sizeof(wszText[0])))
  2315. {
  2316. return 0;
  2317. }
  2318. //message box
  2319. if( pwszCaption)
  2320. {
  2321. intReturn=MessageBoxU(hWnd, wszText, pwszCaption, uType);
  2322. }
  2323. else
  2324. intReturn=MessageBoxU(hWnd, wszText, wszCaption, uType);
  2325. return intReturn;
  2326. }
  2327. //--------------------------------------------------------------------------
  2328. //
  2329. // FormatMessageUnicode
  2330. //
  2331. //--------------------------------------------------------------------------
  2332. BOOL FormatMessageUnicode(LPWSTR *ppwszFormat,UINT ids,...)
  2333. {
  2334. // get format string from resources
  2335. WCHAR wszFormat[1000];
  2336. va_list argList;
  2337. DWORD cbMsg=0;
  2338. BOOL fResult=FALSE;
  2339. HRESULT hr=S_OK;
  2340. if(NULL == ppwszFormat)
  2341. goto InvalidArgErr;
  2342. if(!LoadStringU(g_hmodThisDll, ids, wszFormat, sizeof(wszFormat) / sizeof(wszFormat[0])))
  2343. goto LoadStringError;
  2344. // format message into requested buffer
  2345. va_start(argList, ids);
  2346. cbMsg = FormatMessageU(
  2347. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_STRING,
  2348. wszFormat,
  2349. 0, // dwMessageId
  2350. 0, // dwLanguageId
  2351. (LPWSTR) (ppwszFormat),
  2352. 0, // minimum size to allocate
  2353. &argList);
  2354. va_end(argList);
  2355. if(!cbMsg)
  2356. goto FormatMessageError;
  2357. fResult=TRUE;
  2358. CommonReturn:
  2359. return fResult;
  2360. ErrorReturn:
  2361. fResult=FALSE;
  2362. goto CommonReturn;
  2363. TRACE_ERROR(LoadStringError);
  2364. TRACE_ERROR(FormatMessageError);
  2365. SET_ERROR(InvalidArgErr, E_INVALIDARG);
  2366. }
  2367. //--------------------------------------------------------------------------
  2368. //
  2369. // LauchCertMgr()
  2370. //
  2371. // We use W version of APIs since this call is only made on WinNT5
  2372. //
  2373. //--------------------------------------------------------------------------
  2374. void LauchCertMgr(LPWSTR pwszFileName)
  2375. {
  2376. LPWSTR pwszCommandParam=NULL;
  2377. LPWSTR pwszRealFileName=NULL;
  2378. WCHAR wszMSCFileName[_MAX_PATH * 2];
  2379. WCHAR wszSystemDirectory[_MAX_PATH];
  2380. if(NULL == pwszFileName)
  2381. return;
  2382. pwszRealFileName=(LPWSTR)LocalAlloc(LPTR, sizeof(WCHAR)*(wcslen(pwszFileName)+10));
  2383. if(NULL == pwszRealFileName)
  2384. return;
  2385. //add the " around the file name
  2386. wcscpy(pwszRealFileName, L"\"");
  2387. wcscat(pwszRealFileName, pwszFileName);
  2388. wcscat(pwszRealFileName, L"\"");
  2389. //Open the MMC via "MMC.exe CertMgr.msc /certmgr:FileName=MyFoo.Exe"
  2390. //get the system path
  2391. if(GetSystemDirectoryW(wszSystemDirectory, sizeof(wszSystemDirectory)/sizeof(wszSystemDirectory[0])))
  2392. {
  2393. //copy the system directory
  2394. wcscpy(wszMSCFileName, wszSystemDirectory);
  2395. //cancatecate the string \certmgr.msc
  2396. wcscat(wszMSCFileName, CERTMGR_MSC);
  2397. //make the string "MMC.exe c:\winnt\system32\CertMgr.msc /certmgr:FileName=MyFoo.Exe"
  2398. if(FormatMessageUnicode(&pwszCommandParam, IDS_MMC_PARAM,
  2399. wszMSCFileName, pwszRealFileName))
  2400. {
  2401. ShellExecuteW(NULL,
  2402. L"Open",
  2403. MMC_NAME,
  2404. pwszCommandParam,
  2405. wszSystemDirectory,
  2406. SW_SHOWNORMAL);
  2407. }
  2408. }
  2409. if(pwszCommandParam)
  2410. LocalFree((HLOCAL)pwszCommandParam);
  2411. if(pwszRealFileName)
  2412. LocalFree((HLOCAL)pwszRealFileName);
  2413. }