Source code of Windows XP (NT5)
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.

2946 lines
91 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"rundll32.exe cryptext.dll,CryptExtOpenCER %1", 0,
  26. L"CERFile\\shell\\add\\command", L"rundll32.exe cryptext.dll,CryptExtAddCER %1", 0,
  27. L"CERFile\\shell\\add", NULL, IDS_INSTALL_CERT,
  28. L"CERFile", NULL, IDS_CER_NAME,
  29. L".pfx", L"PFXFile", 0,
  30. L"PFXFile\\shell\\add\\command", L"rundll32.exe cryptext.dll,CryptExtAddPFX %1", 0,
  31. L"PFXFile\\shell\\add", NULL, 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"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"rundll32.exe cryptext.dll,CryptExtOpenCTL %1", 0,
  41. L"STLFile\\shell\\add\\command", L"rundll32.exe cryptext.dll,CryptExtAddCTL %1", 0,
  42. L"STLFile\\shell\\add", NULL, IDS_INSTALL_STL,
  43. L"STLFile", NULL, IDS_STL_NAME,
  44. L".crl", L"CRLFile", 0,
  45. L"CRLFile\\shell\\open\\command", L"rundll32.exe cryptext.dll,CryptExtOpenCRL %1", 0,
  46. L"CRLFile\\shell\\add\\command", L"rundll32.exe cryptext.dll,CryptExtAddCRL %1", 0,
  47. L"CRLFile\\shell\\add", NULL, IDS_INSTALL_CRL,
  48. L"CRLFile", NULL, IDS_CRL_NAME,
  49. L".spc", L"SPCFile", 0,
  50. L"SPCFile\\shell\\add\\command", L"rundll32.exe cryptext.dll,CryptExtAddSPC %1", 0,
  51. L"SPCFile\\shell\\add", NULL, 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"rundll32.exe cryptext.dll,CryptExtOpenP7R %1", 0,
  60. L"P7RFile\\shell\\add\\command", L"rundll32.exe cryptext.dll,CryptExtAddP7R %1", 0,
  61. L"P7RFile\\shell\\add", NULL, 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"rundll32.exe cryptext.dll,CryptExtOpenPKCS7 %1", 0,
  77. L"P7SFile\\shell\\open\\command", L"rundll32.exe cryptext.dll,CryptExtOpenPKCS7 %1", 0,
  78. L"CertificateStoreFile\\shell\\open\\command", L"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[200];
  605. CHAR szValue[MAX_PATH + 1];
  606. CHAR szSystem[MAX_PATH + 1];
  607. WCHAR wszContentType[MAX_STRING_SIZE];
  608. LPWSTR pwszFriendlyTypeName = NULL;
  609. //before anything else, we need to remove the .ctl, .str, and .p7c
  610. //entries. No need to check the return values
  611. RemoveOldExtensions();
  612. //1st, do the registry based context menu
  613. //get the count of the reg entries
  614. dwRegEntry=sizeof(rgRegEntry)/sizeof(rgRegEntry[0]);
  615. for(dwRegIndex=0; dwRegIndex<dwRegEntry; dwRegIndex++)
  616. {
  617. //open a registry entry under HKEY_CLASSES_ROOT
  618. if (ERROR_SUCCESS != RegCreateKeyExU(
  619. HKEY_CLASSES_ROOT,
  620. rgRegEntry[dwRegIndex].wszKey,
  621. 0,
  622. NULL,
  623. REG_OPTION_NON_VOLATILE,
  624. KEY_WRITE,
  625. NULL,
  626. &hKey,
  627. &dwDisposition))
  628. goto RegCreateKeyErr;
  629. //set the value
  630. if(NULL==rgRegEntry[dwRegIndex].wszName)
  631. {
  632. //load the string
  633. if(0==LoadStringU(g_hmodThisDll,rgRegEntry[dwRegIndex].idsName,
  634. wszName,MAX_STRING_SIZE))
  635. goto LoadStringErr;
  636. if(ERROR_SUCCESS != RegSetValueExU(
  637. hKey,
  638. NULL,
  639. 0,
  640. REG_SZ,
  641. (BYTE *)wszName,
  642. (wcslen(wszName) + 1) * sizeof(WCHAR)))
  643. goto RegSetValueErr;
  644. }
  645. else
  646. {
  647. if(ERROR_SUCCESS != RegSetValueExU(
  648. hKey,
  649. NULL,
  650. 0,
  651. REG_SZ,
  652. (BYTE *)(rgRegEntry[dwRegIndex].wszName),
  653. (wcslen(rgRegEntry[dwRegIndex].wszName) + 1) * sizeof(WCHAR)))
  654. goto RegSetValueErr;
  655. }
  656. //close the registry key
  657. if(ERROR_SUCCESS != RegCloseKey(hKey))
  658. goto RegCloseKeyErr;
  659. hKey=NULL;
  660. }
  661. //some of the registry based context menu is specific for NT5
  662. if(FIsWinNT5())
  663. {
  664. //get the count of the reg entries
  665. dwRegEntry=sizeof(rgWINNT5RegEntry)/sizeof(rgWINNT5RegEntry[0]);
  666. for(dwRegIndex=0; dwRegIndex<dwRegEntry; dwRegIndex++)
  667. {
  668. //open a registry entry under HKEY_CLASSES_ROOT
  669. if (ERROR_SUCCESS != RegCreateKeyExU(
  670. HKEY_CLASSES_ROOT,
  671. rgWINNT5RegEntry[dwRegIndex].wszKey,
  672. 0,
  673. NULL,
  674. REG_OPTION_NON_VOLATILE,
  675. KEY_WRITE,
  676. NULL,
  677. &hKey,
  678. &dwDisposition))
  679. goto RegCreateKeyErr;
  680. //set the value
  681. if(NULL==rgWINNT5RegEntry[dwRegIndex].wszName)
  682. {
  683. //load the string
  684. if(0==LoadStringU(g_hmodThisDll,rgWINNT5RegEntry[dwRegIndex].idsName,
  685. wszName,MAX_STRING_SIZE))
  686. goto LoadStringErr;
  687. if(ERROR_SUCCESS != RegSetValueExU(
  688. hKey,
  689. NULL,
  690. 0,
  691. REG_SZ,
  692. (BYTE *)wszName,
  693. (wcslen(wszName) + 1) * sizeof(WCHAR)))
  694. goto RegSetValueErr;
  695. }
  696. else
  697. {
  698. if(ERROR_SUCCESS != RegSetValueExU(
  699. hKey,
  700. NULL,
  701. 0,
  702. REG_SZ,
  703. (BYTE *)(rgWINNT5RegEntry[dwRegIndex].wszName),
  704. (wcslen(rgWINNT5RegEntry[dwRegIndex].wszName) + 1) * sizeof(WCHAR)))
  705. goto RegSetValueErr;
  706. }
  707. //close the registry key
  708. if(ERROR_SUCCESS != RegCloseKey(hKey))
  709. goto RegCloseKeyErr;
  710. hKey=NULL;
  711. }
  712. }
  713. //now, we need to register for the content type
  714. //1. Under the HEKY_CLASSES_ROOT, under ".foo" key, add an entry of name
  715. // "Content Type" and value "application/xxxxxxxx".
  716. //2. Under HKEY_CLASSES_ROOT\MIME\Database\Content Type, add a key of "application/xxxxxxxx",
  717. // under which add an entry of name "Extension" and value ".foo".
  718. dwRegEntry=sizeof(rgContentTypeEntry)/sizeof(rgContentTypeEntry[0]);
  719. for(dwRegIndex=0; dwRegIndex<dwRegEntry; dwRegIndex++)
  720. {
  721. //open a registry entry under HKEY_CLASSES_ROOT
  722. if (ERROR_SUCCESS != RegCreateKeyExU(
  723. HKEY_CLASSES_ROOT,
  724. rgContentTypeEntry[dwRegIndex].wszKey,
  725. 0,
  726. NULL,
  727. REG_OPTION_NON_VOLATILE,
  728. KEY_WRITE,
  729. NULL,
  730. &hKey,
  731. &dwDisposition))
  732. goto RegCreateKeyErr;
  733. //set the value
  734. if(ERROR_SUCCESS != RegSetValueExU(
  735. hKey,
  736. L"Content Type",
  737. 0,
  738. REG_SZ,
  739. (BYTE *)(rgContentTypeEntry[dwRegIndex].wszName),
  740. (wcslen(rgContentTypeEntry[dwRegIndex].wszName) + 1) * sizeof(WCHAR)))
  741. goto RegSetValueErr;
  742. //close the registry key
  743. if(ERROR_SUCCESS != RegCloseKey(hKey))
  744. goto RegCloseKeyErr;
  745. hKey=NULL;
  746. }
  747. //2. Under HKEY_CLASSES_ROOT\MIME\Database\Content Type, add a key of "application/xxxxxxxx",
  748. // under which add an entry of name "Extension" and value ".foo".
  749. dwRegEntry=sizeof(rgContentTypeEntry)/sizeof(rgContentTypeEntry[0]);
  750. for(dwRegIndex=0; dwRegIndex<dwRegEntry; dwRegIndex++)
  751. {
  752. //concatenate the key L"MIME\\Database\\Content Type\\application/XXXXXXXXXX
  753. wszContentType[0]=L'\0';
  754. wcscpy(wszContentType, L"MIME\\Database\\Content Type\\");
  755. wcscat(wszContentType, rgContentTypeEntry[dwRegIndex].wszName);
  756. //open a registry entry under HKEY_CLASSES_ROOT
  757. if (ERROR_SUCCESS != RegCreateKeyExU(
  758. HKEY_CLASSES_ROOT,
  759. wszContentType,
  760. 0,
  761. NULL,
  762. REG_OPTION_NON_VOLATILE,
  763. KEY_WRITE,
  764. NULL,
  765. &hKey,
  766. &dwDisposition))
  767. goto RegCreateKeyErr;
  768. //set the value
  769. if(ERROR_SUCCESS != RegSetValueExU(
  770. hKey,
  771. L"Extension",
  772. 0,
  773. REG_SZ,
  774. (BYTE *)(rgContentTypeEntry[dwRegIndex].wszKey),
  775. (wcslen(rgContentTypeEntry[dwRegIndex].wszKey) + 1) * sizeof(WCHAR)))
  776. goto RegSetValueErr;
  777. //close the registry key
  778. if(ERROR_SUCCESS != RegCloseKey(hKey))
  779. goto RegCloseKeyErr;
  780. hKey=NULL;
  781. }
  782. //2nd, do the registry based DefaultIcon
  783. //we do things differently based on Win95 or WinNT
  784. if(FIsWinNT())
  785. {
  786. dwRegEntry=sizeof(rgIconEntry)/sizeof(rgIconEntry[0]);
  787. for(dwRegIndex=0; dwRegIndex<dwRegEntry; dwRegIndex++)
  788. {
  789. //open a registry entry under HKEY_CLASSES_ROOT
  790. if (ERROR_SUCCESS != RegCreateKeyExU(
  791. HKEY_CLASSES_ROOT,
  792. rgIconEntry[dwRegIndex].wszKey,
  793. 0,
  794. NULL,
  795. REG_OPTION_NON_VOLATILE,
  796. KEY_WRITE,
  797. NULL,
  798. &hKey,
  799. &dwDisposition))
  800. goto RegCreateKeyErr;
  801. if(ERROR_SUCCESS != RegSetValueExU(
  802. hKey,
  803. NULL,
  804. 0,
  805. REG_EXPAND_SZ,
  806. (BYTE *)(rgIconEntry[dwRegIndex].wszName),
  807. (wcslen(rgIconEntry[dwRegIndex].wszName) + 1) * sizeof(WCHAR)))
  808. goto RegSetValueErr;
  809. //close the registry key
  810. if(ERROR_SUCCESS != RegCloseKey(hKey))
  811. goto RegCloseKeyErr;
  812. hKey=NULL;
  813. }
  814. }
  815. else
  816. {
  817. //get the system directory
  818. if(!GetSystemDirectory(szSystem, MAX_PATH))
  819. goto GetSystemErr;
  820. dwRegEntry=sizeof(rgWin95IconEntry)/sizeof(rgWin95IconEntry[0]);
  821. for(dwRegIndex=0; dwRegIndex<dwRegEntry; dwRegIndex++)
  822. {
  823. //open a registry entry under HKEY_CLASSES_ROOT
  824. if (ERROR_SUCCESS != RegCreateKeyEx(
  825. HKEY_CLASSES_ROOT,
  826. rgWin95IconEntry[dwRegIndex].szKey,
  827. 0,
  828. NULL,
  829. REG_OPTION_NON_VOLATILE,
  830. KEY_WRITE,
  831. NULL,
  832. &hKey,
  833. &dwDisposition))
  834. goto RegCreateKeyErr;
  835. //concantenate the values
  836. strcpy(szValue, szSystem);
  837. strcat(szValue, (rgWin95IconEntry[dwRegIndex].szName));
  838. if(ERROR_SUCCESS != RegSetValueEx(
  839. hKey,
  840. NULL,
  841. 0,
  842. REG_SZ,
  843. (BYTE *)szValue,
  844. (strlen(szValue) + 1) * sizeof(CHAR)))
  845. goto RegSetValueErr;
  846. //close the registry key
  847. if(ERROR_SUCCESS != RegCloseKey(hKey))
  848. goto RegCloseKeyErr;
  849. hKey=NULL;
  850. }
  851. }
  852. //3rd, set the .PKO context menu handler and property sheet handler
  853. //set the values related to the GUIDs
  854. dwRegEntry=sizeof(rgGuidEntry)/sizeof(rgGuidEntry[0]);
  855. for(dwRegIndex=0; dwRegIndex<dwRegEntry; dwRegIndex++)
  856. {
  857. //load the string
  858. if(0==LoadStringU(g_hmodThisDll,rgGuidEntry[dwRegIndex].idsName,
  859. wszName,MAX_STRING_SIZE))
  860. goto LoadStringErr;
  861. //get the string presentation of the CLSID
  862. if(0==StringFromGUID2(*(rgGuidEntry[dwRegIndex].pGuid), wszGUID, MAX_STRING_SIZE))
  863. goto StringFromGUIDErr;
  864. //open a registry entry under HKEY_CLASSES_ROOT for the context menu handler
  865. if(NULL!=rgGuidEntry[dwRegIndex].wszKey1)
  866. {
  867. if (ERROR_SUCCESS != RegCreateKeyExU(
  868. HKEY_CLASSES_ROOT,
  869. rgGuidEntry[dwRegIndex].wszKey1,
  870. 0,
  871. NULL,
  872. REG_OPTION_NON_VOLATILE,
  873. KEY_WRITE,
  874. NULL,
  875. &hKey,
  876. &dwDisposition))
  877. goto RegCreateKeyErr;
  878. if(ERROR_SUCCESS != RegSetValueExU(
  879. hKey,
  880. NULL,
  881. 0,
  882. REG_SZ,
  883. (BYTE *)wszGUID,
  884. (wcslen(wszGUID) + 1) * sizeof(WCHAR)))
  885. goto RegSetValueErr;
  886. //close the registry key
  887. if(ERROR_SUCCESS != RegCloseKey(hKey))
  888. goto RegCloseKeyErr;
  889. hKey=NULL;
  890. }
  891. //open a registry entry under HKEY_CLASSES_ROOT for the property sheet hander
  892. if (ERROR_SUCCESS != RegCreateKeyExU(
  893. HKEY_CLASSES_ROOT,
  894. rgGuidEntry[dwRegIndex].wszKey2,
  895. 0,
  896. NULL,
  897. REG_OPTION_NON_VOLATILE,
  898. KEY_WRITE,
  899. NULL,
  900. &hKey,
  901. &dwDisposition))
  902. goto RegCreateKeyErr;
  903. if(ERROR_SUCCESS != RegSetValueExU(
  904. hKey,
  905. NULL,
  906. 0,
  907. REG_SZ,
  908. (BYTE *)wszGUID,
  909. (wcslen(wszGUID) + 1) * sizeof(WCHAR)))
  910. goto RegSetValueErr;
  911. //close the registry key
  912. if(ERROR_SUCCESS != RegCloseKey(hKey))
  913. goto RegCloseKeyErr;
  914. hKey=NULL;
  915. //open a registry entry under HKEY_LOCAL_MACHINE
  916. if (ERROR_SUCCESS != RegCreateKeyExU(
  917. HKEY_LOCAL_MACHINE,
  918. rgGuidEntry[dwRegIndex].wszKey3,
  919. 0,
  920. NULL,
  921. REG_OPTION_NON_VOLATILE,
  922. KEY_WRITE,
  923. NULL,
  924. &hKey,
  925. &dwDisposition))
  926. goto RegCreateKeyErr;
  927. if(ERROR_SUCCESS != RegSetValueExU(
  928. hKey,
  929. wszGUID,
  930. 0,
  931. REG_SZ,
  932. (BYTE *)wszName,
  933. (wcslen(wszName) + 1) * sizeof(WCHAR)))
  934. goto RegSetValueErr;
  935. //close the registry key
  936. if(ERROR_SUCCESS != RegCloseKey(hKey))
  937. goto RegCloseKeyErr;
  938. hKey=NULL;
  939. }
  940. //now, under the classID of &CLSID_CryptPKO, we need to add
  941. //the registry shellex\MayChangeDefaultMenu
  942. dwRegEntry=sizeof(g_CLSIDDefault)/sizeof(g_CLSIDDefault[0]);
  943. //get the string presentation of the CLSID
  944. if(0==StringFromGUID2(CLSID_CryptPKO, wszGUID, MAX_STRING_SIZE))
  945. goto StringFromGUIDErr;
  946. for(dwRegIndex=0; dwRegIndex<dwRegEntry; dwRegIndex++)
  947. {
  948. wcscpy(wszDefault, L"CLSID\\");
  949. wcscat(wszDefault, wszGUID);
  950. wcscat(wszDefault, g_CLSIDDefault[dwRegIndex]);
  951. if (ERROR_SUCCESS != RegCreateKeyExU(
  952. HKEY_CLASSES_ROOT,
  953. wszDefault,
  954. 0,
  955. NULL,
  956. REG_OPTION_NON_VOLATILE,
  957. KEY_WRITE,
  958. NULL,
  959. &hKey,
  960. &dwDisposition))
  961. goto RegCreateKeyErr;
  962. if(ERROR_SUCCESS != RegCloseKey(hKey))
  963. goto RegCloseKeyErr;
  964. hKey=NULL;
  965. }
  966. // Set the FriendlyTypeName value for the new MUI requirement of Whistler.
  967. dwRegEntry=sizeof(rgFriendlyTypeNameEntry)/sizeof(rgFriendlyTypeNameEntry[0]);
  968. for(dwRegIndex=0; dwRegIndex<dwRegEntry; dwRegIndex++)
  969. {
  970. //format the data by inserting the IDS value.
  971. if (0 == FormatMessageU(
  972. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ARGUMENT_ARRAY,
  973. rgFriendlyTypeNameEntry[dwRegIndex].wszName,
  974. 0, // dwMessageId
  975. 0, // dwLanguageId
  976. (LPWSTR) (&pwszFriendlyTypeName),
  977. 0, // minimum size to allocate
  978. (va_list *) &rgFriendlyTypeNameEntry[dwRegIndex].idsName))
  979. goto FormatMsgError;
  980. //open a registry entry under HKEY_CLASSES_ROOT
  981. if (ERROR_SUCCESS != RegCreateKeyExU(
  982. HKEY_CLASSES_ROOT,
  983. rgFriendlyTypeNameEntry[dwRegIndex].wszKey,
  984. 0,
  985. NULL,
  986. REG_OPTION_NON_VOLATILE,
  987. KEY_WRITE,
  988. NULL,
  989. &hKey,
  990. &dwDisposition))
  991. goto RegCreateKeyErr;
  992. //set the value
  993. if(ERROR_SUCCESS != RegSetValueExU(
  994. hKey,
  995. L"FriendlyTypeName",
  996. 0,
  997. REG_EXPAND_SZ,
  998. (BYTE *) pwszFriendlyTypeName,
  999. (wcslen(pwszFriendlyTypeName) + 1) * sizeof(WCHAR)))
  1000. goto RegSetValueErr;
  1001. //close the registry key
  1002. if(ERROR_SUCCESS != RegCloseKey(hKey))
  1003. goto RegCloseKeyErr;
  1004. hKey=NULL;
  1005. // free friendly name type string.
  1006. LocalFree((HLOCAL) pwszFriendlyTypeName);
  1007. pwszFriendlyTypeName = NULL;
  1008. }
  1009. hr=S_OK;
  1010. CommonReturn:
  1011. if(hKey)
  1012. RegCloseKey(hKey);
  1013. if(pwszFriendlyTypeName)
  1014. LocalFree((HLOCAL) pwszFriendlyTypeName);
  1015. return hr;
  1016. ErrorReturn:
  1017. hr=GetLastError();
  1018. goto CommonReturn;
  1019. TRACE_ERROR(RegCloseKeyErr);
  1020. TRACE_ERROR(RegCreateKeyErr);
  1021. TRACE_ERROR(LoadStringErr);
  1022. TRACE_ERROR(RegSetValueErr);
  1023. TRACE_ERROR(StringFromGUIDErr);
  1024. TRACE_ERROR(GetSystemErr);
  1025. TRACE_ERROR(FormatMsgError);
  1026. }
  1027. //--------------------------------------------------------------------------
  1028. //
  1029. // UnregisterMimeHandler
  1030. //
  1031. //--------------------------------------------------------------------------
  1032. HRESULT UnregisterMimeHandler()
  1033. {
  1034. HKEY hKey=NULL;
  1035. DWORD dwRegEntry=0;
  1036. DWORD dwRegIndex=0;
  1037. WCHAR wszGUID[MAX_STRING_SIZE];
  1038. DWORD dwDisposition=0;
  1039. WCHAR wszDefault[200];
  1040. WCHAR wszContentType[MAX_STRING_SIZE];
  1041. //1st, delete the entries related to the GUID
  1042. //that is, the .PKO context menu handler and property sheet handler
  1043. dwRegEntry=sizeof(rgGuidEntry)/sizeof(rgGuidEntry[0]);
  1044. for(dwRegIndex=0; dwRegIndex<dwRegEntry; dwRegIndex++)
  1045. {
  1046. //get the string presentation of the CLSID
  1047. if(0==StringFromGUID2(*(rgGuidEntry[dwRegIndex].pGuid), wszGUID, MAX_STRING_SIZE))
  1048. continue;
  1049. //open a registry entry under HKEY_LOCAL_MACHINE
  1050. if (ERROR_SUCCESS == RegCreateKeyExU(
  1051. HKEY_LOCAL_MACHINE,
  1052. rgGuidEntry[dwRegIndex].wszKey3,
  1053. 0,
  1054. NULL,
  1055. REG_OPTION_NON_VOLATILE,
  1056. KEY_WRITE,
  1057. NULL,
  1058. &hKey,
  1059. &dwDisposition))
  1060. {
  1061. RegDeleteValueU(hKey,wszGUID);
  1062. //close the registry key
  1063. RegCloseKey(hKey);
  1064. hKey=NULL;
  1065. }
  1066. //delete a registry entry under HKEY_CLASSES_ROOT for property sheet
  1067. RegDeleteKeyU(HKEY_CLASSES_ROOT,
  1068. rgGuidEntry[dwRegIndex].wszKey2);
  1069. //delete a registry entry under HKEY_CLASSES_ROOT for context menu
  1070. if(NULL !=rgGuidEntry[dwRegIndex].wszKey1)
  1071. {
  1072. RegDeleteKeyU(HKEY_CLASSES_ROOT,
  1073. rgGuidEntry[dwRegIndex].wszKey1);
  1074. }
  1075. }
  1076. //2nd, detelet all the registry based context menu and Icon
  1077. //get the count of the reg entries
  1078. dwRegEntry=sizeof(rgwszDelete)/sizeof(rgwszDelete[0]);
  1079. for(dwRegIndex=0; dwRegIndex<dwRegEntry; dwRegIndex++)
  1080. //delete the registry entries
  1081. RegDeleteKeyU(HKEY_CLASSES_ROOT,rgwszDelete[dwRegIndex]);
  1082. //now, delete anything related to the content type
  1083. dwRegEntry=sizeof(rgContentTypeEntry)/sizeof(rgContentTypeEntry[0]);
  1084. for(dwRegIndex=0; dwRegIndex<dwRegEntry; dwRegIndex++)
  1085. {
  1086. wszContentType[0]=L'\0';
  1087. wcscpy(wszContentType, L"MIME\\Database\\Content Type\\");
  1088. wcscat(wszContentType, rgContentTypeEntry[dwRegIndex].wszName);
  1089. RegDeleteKeyU(HKEY_CLASSES_ROOT,wszContentType);
  1090. }
  1091. //3nd, under the classID of &CLSID_CryptPKO, we need to delete
  1092. //the registry shellex\MayChangeDefaultMenu
  1093. dwRegEntry=sizeof(g_CLSIDDefault)/sizeof(g_CLSIDDefault[0]);
  1094. //get the string presentation of the CLSID
  1095. if(0==StringFromGUID2(CLSID_CryptPKO, wszGUID, MAX_STRING_SIZE))
  1096. return S_OK;
  1097. for(dwRegIndex=0; dwRegIndex<dwRegEntry; dwRegIndex++)
  1098. {
  1099. wcscpy(wszDefault, L"CLSID\\");
  1100. wcscat(wszDefault, wszGUID);
  1101. wcscat(wszDefault, g_CLSIDDefault[dwRegIndex]);
  1102. RegDeleteKeyU(HKEY_CLASSES_ROOT,wszDefault);
  1103. }
  1104. return S_OK;
  1105. }
  1106. //--------------------------------------------------------------------------
  1107. //
  1108. // View a CTL context
  1109. //
  1110. //--------------------------------------------------------------------------
  1111. void I_ViewCTL(PCCTL_CONTEXT pCTLContext)
  1112. {
  1113. CRYPTUI_VIEWCTL_STRUCT ViewCTLStruct;
  1114. if(NULL==pCTLContext)
  1115. return;
  1116. //memset
  1117. memset(&ViewCTLStruct, 0, sizeof(ViewCTLStruct));
  1118. ViewCTLStruct.dwSize=sizeof(ViewCTLStruct);
  1119. ViewCTLStruct.pCTLContext=pCTLContext;
  1120. CryptUIDlgViewCTL(&ViewCTLStruct);
  1121. }
  1122. //--------------------------------------------------------------------------
  1123. //
  1124. // View a signer Info
  1125. //
  1126. //--------------------------------------------------------------------------
  1127. /*void I_ViewSignerInfo(HCRYPTMSG hMsg)
  1128. {
  1129. CERT_VIEWSIGNERINFO_STRUCT_W ViewSignerInfoStruct;
  1130. PCMSG_SIGNER_INFO pSignerInfo=NULL;
  1131. HCERTSTORE hCertStore=NULL;
  1132. DWORD cbData=0;
  1133. //get the cert store from the hMsg
  1134. hCertStore=CertOpenStore(CERT_STORE_PROV_MSG,
  1135. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
  1136. NULL,
  1137. 0,
  1138. hMsg);
  1139. if(NULL==hCertStore)
  1140. goto CLEANUP;
  1141. //get the signer info struct
  1142. if(!CryptMsgGetParam(hMsg,
  1143. CMSG_SIGNER_INFO_PARAM,
  1144. 0,
  1145. NULL,
  1146. &cbData))
  1147. goto CLEANUP;
  1148. pSignerInfo=(PCMSG_SIGNER_INFO)malloc(cbData);
  1149. if(NULL==pSignerInfo)
  1150. goto CLEANUP;
  1151. if(!CryptMsgGetParam(hMsg,
  1152. CMSG_SIGNER_INFO_PARAM,
  1153. 0,
  1154. pSignerInfo,
  1155. &cbData))
  1156. goto CLEANUP;
  1157. //Init
  1158. memset(&ViewSignerInfoStruct, 0, sizeof(ViewSignerInfoStruct));
  1159. ViewSignerInfoStruct.dwSize=sizeof(ViewSignerInfoStruct);
  1160. ViewSignerInfoStruct.pSignerInfo=pSignerInfo;
  1161. ViewSignerInfoStruct.cStores=1;
  1162. ViewSignerInfoStruct.rghStores=&hCertStore;
  1163. CertViewSignerInfo_W(&ViewSignerInfoStruct);
  1164. CLEANUP:
  1165. if(hCertStore)
  1166. CertCloseStore(hCertStore, 0);
  1167. if(pSignerInfo)
  1168. free(pSignerInfo);
  1169. } */
  1170. //---------------------------------------------------------------------------------
  1171. //
  1172. // Check is the PKCS signed MSG has a signerInfo attached
  1173. //
  1174. //---------------------------------------------------------------------------------
  1175. BOOL PKCS7WithSignature(HCRYPTMSG hMsg)
  1176. {
  1177. DWORD dwSignerCount=0;
  1178. DWORD cbSignerCount=0;
  1179. if(NULL==hMsg)
  1180. return FALSE;
  1181. cbSignerCount=sizeof(dwSignerCount);
  1182. //get the Param CMSG_SIGNER_COUNT_PARAM on the message handle
  1183. //if 0==CMSG_SIGNER_COUNT_PARAM, there is no signerInfo
  1184. //on the message handle
  1185. if(!CryptMsgGetParam(hMsg,
  1186. CMSG_SIGNER_COUNT_PARAM,
  1187. 0,
  1188. &dwSignerCount,
  1189. &cbSignerCount))
  1190. return FALSE;
  1191. if(0==dwSignerCount)
  1192. return FALSE;
  1193. return TRUE;
  1194. }
  1195. //---------------------------------------------------------------------------------
  1196. //
  1197. // Mime Handler the command line "Open" for CER and CRT file
  1198. //---------------------------------------------------------------------------------
  1199. STDAPI CryptExtOpenCERW(HINSTANCE hinst, HINSTANCE hPrevInstance, LPWSTR lpszCmdLine, int nCmdShow)
  1200. {
  1201. LPWSTR pwszFileName=NULL;
  1202. PCCERT_CONTEXT pCertContext=NULL;
  1203. CRYPTUI_VIEWCERTIFICATE_STRUCT CertViewStruct;
  1204. if (!lpszCmdLine)
  1205. return E_FAIL;
  1206. //get the WCHAR file name
  1207. pwszFileName=lpszCmdLine;
  1208. if(CryptQueryObject(CERT_QUERY_OBJECT_FILE,
  1209. pwszFileName,
  1210. CERT_QUERY_CONTENT_FLAG_CERT,
  1211. CERT_QUERY_FORMAT_FLAG_ALL,
  1212. 0,
  1213. NULL,
  1214. NULL,
  1215. NULL,
  1216. NULL,
  1217. NULL,
  1218. (const void **)&pCertContext))
  1219. {
  1220. //call the Certificate Common Dialogue
  1221. memset(&CertViewStruct, 0, sizeof(CRYPTUI_VIEWCERTIFICATE_STRUCT));
  1222. CertViewStruct.dwSize=sizeof(CRYPTUI_VIEWCERTIFICATE_STRUCT);
  1223. CertViewStruct.pCertContext=pCertContext;
  1224. CryptUIDlgViewCertificate(&CertViewStruct,NULL);
  1225. }
  1226. else
  1227. {
  1228. I_NoticeBox(
  1229. GetLastError(),
  1230. 0,
  1231. NULL,
  1232. IDS_MSG_TITLE,
  1233. IDS_CER_NAME,
  1234. IDS_MSG_INVALID_FILE,
  1235. MB_OK|MB_ICONINFORMATION);
  1236. }
  1237. if(pCertContext)
  1238. CertFreeCertificateContext(pCertContext);
  1239. return S_OK;
  1240. }
  1241. //---------------------------------------------------------------------------------
  1242. //
  1243. // Mime Handler the command line "Open" for CER and CRT file
  1244. //---------------------------------------------------------------------------------
  1245. STDAPI CryptExtOpenCER(HINSTANCE hinst, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
  1246. {
  1247. LPWSTR pwszFileName=NULL;
  1248. HRESULT hr=S_OK;
  1249. if (!lpszCmdLine)
  1250. return E_FAIL;
  1251. //get the WCHAR file name
  1252. pwszFileName=MkWStr(lpszCmdLine);
  1253. if(NULL==pwszFileName)
  1254. return HRESULT_FROM_WIN32(GetLastError());
  1255. hr=CryptExtOpenCERW(hinst, hPrevInstance, pwszFileName, nCmdShow);
  1256. if(pwszFileName)
  1257. FreeWStr(pwszFileName);
  1258. return hr;
  1259. }
  1260. BOOL IsCatalog(PCCTL_CONTEXT pCTLContext)
  1261. {
  1262. BOOL fRet=FALSE;
  1263. if (pCTLContext)
  1264. {
  1265. if (pCTLContext->pCtlInfo)
  1266. {
  1267. if(pCTLContext->pCtlInfo->SubjectUsage.cUsageIdentifier)
  1268. {
  1269. if (pCTLContext->pCtlInfo->SubjectUsage.rgpszUsageIdentifier)
  1270. {
  1271. if (strcmp(pCTLContext->pCtlInfo->SubjectUsage.rgpszUsageIdentifier[0],
  1272. szOID_CATALOG_LIST) == 0)
  1273. {
  1274. fRet = TRUE;
  1275. }
  1276. }
  1277. }
  1278. }
  1279. }
  1280. return fRet;
  1281. }
  1282. //---------------------------------------------------------------------------------
  1283. //
  1284. // Mime Handler the command line "Open" for .CTL file
  1285. //---------------------------------------------------------------------------------
  1286. STDAPI CryptExtOpenCATW(HINSTANCE hinst, HINSTANCE hPrevInstance, LPWSTR lpszCmdLine, int nCmdShow)
  1287. {
  1288. LPWSTR pwszFileName=NULL;
  1289. PCCTL_CONTEXT pCTLContext=NULL;
  1290. if (!lpszCmdLine)
  1291. return E_FAIL;
  1292. //get the WCHAR file name
  1293. pwszFileName=lpszCmdLine;
  1294. if(CryptQueryObject(CERT_QUERY_OBJECT_FILE,
  1295. pwszFileName,
  1296. CERT_QUERY_CONTENT_FLAG_CTL,
  1297. CERT_QUERY_FORMAT_FLAG_ALL,
  1298. 0,
  1299. NULL,
  1300. NULL,
  1301. NULL,
  1302. NULL,
  1303. NULL,
  1304. (const void **)&pCTLContext) &&
  1305. IsCatalog(pCTLContext))
  1306. {
  1307. I_ViewCTL(pCTLContext);
  1308. }
  1309. else
  1310. {
  1311. I_NoticeBox(
  1312. GetLastError(),
  1313. 0,
  1314. NULL,
  1315. IDS_MSG_TITLE,
  1316. IDS_CAT_NAME,
  1317. IDS_MSG_INVALID_FILE,
  1318. MB_OK|MB_ICONINFORMATION);
  1319. }
  1320. if(pCTLContext)
  1321. CertFreeCTLContext(pCTLContext);
  1322. return S_OK;
  1323. }
  1324. //---------------------------------------------------------------------------------
  1325. //
  1326. // Mime Handler the command line "Open" for .CTL file
  1327. //---------------------------------------------------------------------------------
  1328. STDAPI CryptExtOpenCAT(HINSTANCE hinst, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
  1329. {
  1330. LPWSTR pwszFileName=NULL;
  1331. if (!lpszCmdLine)
  1332. return E_FAIL;
  1333. //get the WCHAR file name
  1334. pwszFileName=MkWStr(lpszCmdLine);
  1335. if(NULL==pwszFileName)
  1336. return HRESULT_FROM_WIN32(GetLastError());
  1337. CryptExtOpenCATW(hinst, hPrevInstance, pwszFileName, nCmdShow);
  1338. if(pwszFileName)
  1339. FreeWStr(pwszFileName);
  1340. return S_OK;
  1341. }
  1342. //---------------------------------------------------------------------------------
  1343. //
  1344. // Handler the command line "Add" for .PFX File
  1345. //---------------------------------------------------------------------------------
  1346. STDAPI CryptExtAddPFXW(HINSTANCE hinst, HINSTANCE hPrevInstance, LPWSTR lpszCmdLine, int nCmdShow)
  1347. {
  1348. LPWSTR pwszFileName=NULL;
  1349. CRYPTUI_WIZ_IMPORT_SRC_INFO importSubject;
  1350. if (!lpszCmdLine)
  1351. return E_FAIL;
  1352. //get the WCHAR file name
  1353. pwszFileName=lpszCmdLine;
  1354. if(CryptQueryObject(CERT_QUERY_OBJECT_FILE,
  1355. pwszFileName,
  1356. CERT_QUERY_CONTENT_FLAG_PFX,
  1357. CERT_QUERY_FORMAT_FLAG_ALL,
  1358. 0,
  1359. NULL,
  1360. NULL,
  1361. NULL,
  1362. NULL,
  1363. NULL,
  1364. NULL))
  1365. {
  1366. memset(&importSubject, 0, sizeof(CRYPTUI_WIZ_IMPORT_SRC_INFO));
  1367. importSubject.dwSize=sizeof(CRYPTUI_WIZ_IMPORT_SRC_INFO);
  1368. importSubject.dwSubjectChoice=CRYPTUI_WIZ_IMPORT_SUBJECT_FILE;
  1369. importSubject.pwszFileName=pwszFileName;
  1370. CryptUIWizImport(0,
  1371. NULL,
  1372. NULL,
  1373. &importSubject,
  1374. NULL);
  1375. }
  1376. else
  1377. {
  1378. I_NoticeBox(
  1379. GetLastError(),
  1380. 0,
  1381. NULL,
  1382. IDS_MSG_TITLE,
  1383. IDS_PFX_NAME,
  1384. IDS_MSG_INVALID_FILE,
  1385. MB_OK|MB_ICONINFORMATION);
  1386. }
  1387. return S_OK;
  1388. }
  1389. //---------------------------------------------------------------------------------
  1390. //
  1391. // Handler the command line "Add" for .PFX File
  1392. //---------------------------------------------------------------------------------
  1393. STDAPI CryptExtAddPFX(HINSTANCE hinst, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
  1394. {
  1395. LPWSTR pwszFileName=NULL;
  1396. if (!lpszCmdLine)
  1397. return E_FAIL;
  1398. //get the WCHAR file name
  1399. pwszFileName=MkWStr(lpszCmdLine);
  1400. if(NULL==pwszFileName)
  1401. return HRESULT_FROM_WIN32(GetLastError());
  1402. CryptExtAddPFXW(hinst, hPrevInstance, pwszFileName, nCmdShow);
  1403. if(pwszFileName)
  1404. FreeWStr(pwszFileName);
  1405. return S_OK;
  1406. }
  1407. //---------------------------------------------------------------------------------
  1408. //
  1409. // Handler the command line "Add" for CER and CRT file
  1410. //---------------------------------------------------------------------------------
  1411. STDAPI CryptExtAddCERW(HINSTANCE hinst, HINSTANCE hPrevInstance, LPWSTR lpszCmdLine, int nCmdShow)
  1412. {
  1413. LPWSTR pwszFileName=NULL;
  1414. PCCERT_CONTEXT pCertContext=NULL;
  1415. CRYPTUI_WIZ_IMPORT_SRC_INFO importSubject;
  1416. if (!lpszCmdLine)
  1417. return E_FAIL;
  1418. //get the WCHAR file name
  1419. pwszFileName=lpszCmdLine;
  1420. if(CryptQueryObject(CERT_QUERY_OBJECT_FILE,
  1421. pwszFileName,
  1422. CERT_QUERY_CONTENT_FLAG_CERT,
  1423. CERT_QUERY_FORMAT_FLAG_ALL,
  1424. 0,
  1425. NULL,
  1426. NULL,
  1427. NULL,
  1428. NULL,
  1429. NULL,
  1430. (const void **)&pCertContext))
  1431. {
  1432. memset(&importSubject, 0, sizeof(CRYPTUI_WIZ_IMPORT_SRC_INFO));
  1433. importSubject.dwSize=sizeof(CRYPTUI_WIZ_IMPORT_SRC_INFO);
  1434. importSubject.dwSubjectChoice=CRYPTUI_WIZ_IMPORT_SUBJECT_CERT_CONTEXT;
  1435. importSubject.pCertContext=pCertContext;
  1436. CryptUIWizImport(0,
  1437. NULL,
  1438. NULL,
  1439. &importSubject,
  1440. NULL);
  1441. }
  1442. else
  1443. {
  1444. I_NoticeBox(
  1445. GetLastError(),
  1446. 0,
  1447. NULL,
  1448. IDS_MSG_TITLE,
  1449. IDS_CER_NAME,
  1450. IDS_MSG_INVALID_FILE,
  1451. MB_OK|MB_ICONINFORMATION);
  1452. }
  1453. if(pCertContext)
  1454. CertFreeCertificateContext(pCertContext);
  1455. return S_OK;
  1456. }
  1457. //---------------------------------------------------------------------------------
  1458. //
  1459. // Handler the command line "Add" for CER and CRT file
  1460. //---------------------------------------------------------------------------------
  1461. STDAPI CryptExtAddCER(HINSTANCE hinst, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
  1462. {
  1463. LPWSTR pwszFileName=NULL;
  1464. if (!lpszCmdLine)
  1465. return E_FAIL;
  1466. //get the WCHAR file name
  1467. pwszFileName=MkWStr(lpszCmdLine);
  1468. if(NULL==pwszFileName)
  1469. return HRESULT_FROM_WIN32(GetLastError());
  1470. CryptExtAddCERW(hinst, hPrevInstance,pwszFileName, nCmdShow);
  1471. if(pwszFileName)
  1472. FreeWStr(pwszFileName);
  1473. return S_OK;
  1474. }
  1475. //---------------------------------------------------------------------------------
  1476. //
  1477. // Mime Handler the command line "Open" for .CTL file
  1478. //---------------------------------------------------------------------------------
  1479. STDAPI CryptExtOpenCTLW(HINSTANCE hinst, HINSTANCE hPrevInstance, LPWSTR lpszCmdLine, int nCmdShow)
  1480. {
  1481. LPWSTR pwszFileName=NULL;
  1482. PCCTL_CONTEXT pCTLContext=NULL;
  1483. if (!lpszCmdLine)
  1484. return E_FAIL;
  1485. //get the WCHAR file name
  1486. pwszFileName=lpszCmdLine;
  1487. if(CryptQueryObject(CERT_QUERY_OBJECT_FILE,
  1488. pwszFileName,
  1489. CERT_QUERY_CONTENT_FLAG_CTL,
  1490. CERT_QUERY_FORMAT_FLAG_ALL,
  1491. 0,
  1492. NULL,
  1493. NULL,
  1494. NULL,
  1495. NULL,
  1496. NULL,
  1497. (const void **)&pCTLContext))
  1498. {
  1499. I_ViewCTL(pCTLContext);
  1500. }
  1501. else
  1502. {
  1503. I_NoticeBox(
  1504. GetLastError(),
  1505. 0,
  1506. NULL,
  1507. IDS_MSG_TITLE,
  1508. IDS_STL_NAME,
  1509. IDS_MSG_INVALID_FILE,
  1510. MB_OK|MB_ICONINFORMATION);
  1511. }
  1512. if(pCTLContext)
  1513. CertFreeCTLContext(pCTLContext);
  1514. return S_OK;
  1515. }
  1516. //---------------------------------------------------------------------------------
  1517. //
  1518. // Mime Handler the command line "Open" for .CTL file
  1519. //---------------------------------------------------------------------------------
  1520. STDAPI CryptExtOpenCTL(HINSTANCE hinst, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
  1521. {
  1522. LPWSTR pwszFileName=NULL;
  1523. if (!lpszCmdLine)
  1524. return E_FAIL;
  1525. //get the WCHAR file name
  1526. pwszFileName=MkWStr(lpszCmdLine);
  1527. if(NULL==pwszFileName)
  1528. return HRESULT_FROM_WIN32(GetLastError());
  1529. CryptExtOpenCTLW(hinst, hPrevInstance, pwszFileName, nCmdShow);
  1530. if(pwszFileName)
  1531. FreeWStr(pwszFileName);
  1532. return S_OK;
  1533. }
  1534. //---------------------------------------------------------------------------------
  1535. //
  1536. // Handler the command line "Add" for .CTL file
  1537. //---------------------------------------------------------------------------------
  1538. STDAPI CryptExtAddCTLW(HINSTANCE hinst, HINSTANCE hPrevInstance, LPWSTR lpszCmdLine, int nCmdShow)
  1539. {
  1540. LPWSTR pwszFileName=NULL;
  1541. PCCTL_CONTEXT pCTLContext=NULL;
  1542. CRYPTUI_WIZ_IMPORT_SRC_INFO importSubject;
  1543. if (!lpszCmdLine)
  1544. return E_FAIL;
  1545. //get the WCHAR file name
  1546. pwszFileName=lpszCmdLine;
  1547. if(CryptQueryObject(CERT_QUERY_OBJECT_FILE,
  1548. pwszFileName,
  1549. CERT_QUERY_CONTENT_FLAG_CTL,
  1550. CERT_QUERY_FORMAT_FLAG_ALL,
  1551. 0,
  1552. NULL,
  1553. NULL,
  1554. NULL,
  1555. NULL,
  1556. NULL,
  1557. (const void **)&pCTLContext))
  1558. {
  1559. memset(&importSubject, 0, sizeof(CRYPTUI_WIZ_IMPORT_SRC_INFO));
  1560. importSubject.dwSize=sizeof(CRYPTUI_WIZ_IMPORT_SRC_INFO);
  1561. importSubject.dwSubjectChoice=CRYPTUI_WIZ_IMPORT_SUBJECT_CTL_CONTEXT;
  1562. importSubject.pCTLContext=pCTLContext;
  1563. CryptUIWizImport(0,
  1564. NULL,
  1565. NULL,
  1566. &importSubject,
  1567. NULL);
  1568. }
  1569. else
  1570. {
  1571. I_NoticeBox(
  1572. GetLastError(),
  1573. 0,
  1574. NULL,
  1575. IDS_MSG_TITLE,
  1576. IDS_STL_NAME,
  1577. IDS_MSG_INVALID_FILE,
  1578. MB_OK|MB_ICONINFORMATION);
  1579. }
  1580. if(pCTLContext)
  1581. CertFreeCTLContext(pCTLContext);
  1582. return S_OK;
  1583. }
  1584. //---------------------------------------------------------------------------------
  1585. //
  1586. // Handler the command line "Add" for .CTL file
  1587. //---------------------------------------------------------------------------------
  1588. STDAPI CryptExtAddCTL(HINSTANCE hinst, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
  1589. {
  1590. LPWSTR pwszFileName=NULL;
  1591. if (!lpszCmdLine)
  1592. return E_FAIL;
  1593. //get the WCHAR file name
  1594. pwszFileName=MkWStr(lpszCmdLine);
  1595. if(NULL==pwszFileName)
  1596. return HRESULT_FROM_WIN32(GetLastError());
  1597. CryptExtAddCTLW(hinst, hPrevInstance, pwszFileName, nCmdShow);
  1598. if(pwszFileName)
  1599. FreeWStr(pwszFileName);
  1600. return S_OK;
  1601. }
  1602. //---------------------------------------------------------------------------------
  1603. //
  1604. // Mime Handler the command line "Open" for .CRL file
  1605. //---------------------------------------------------------------------------------
  1606. STDAPI CryptExtOpenCRLW(HINSTANCE hinst, HINSTANCE hPrevInstance, LPWSTR lpszCmdLine, int nCmdShow)
  1607. {
  1608. LPWSTR pwszFileName=NULL;
  1609. PCCRL_CONTEXT pCRLContext=NULL;
  1610. CRYPTUI_VIEWCRL_STRUCT CRLViewStruct;
  1611. if (!lpszCmdLine)
  1612. return E_FAIL;
  1613. //get the WCHAR file name
  1614. pwszFileName=lpszCmdLine;
  1615. if(CryptQueryObject(CERT_QUERY_OBJECT_FILE,
  1616. pwszFileName,
  1617. CERT_QUERY_CONTENT_FLAG_CRL,
  1618. CERT_QUERY_FORMAT_FLAG_ALL,
  1619. 0,
  1620. NULL,
  1621. NULL,
  1622. NULL,
  1623. NULL,
  1624. NULL,
  1625. (const void **)&pCRLContext))
  1626. {
  1627. //call the CRL view dialogue
  1628. memset(&CRLViewStruct, 0, sizeof(CRYPTUI_VIEWCRL_STRUCT));
  1629. CRLViewStruct.dwSize=sizeof(CRYPTUI_VIEWCRL_STRUCT);
  1630. CRLViewStruct.pCRLContext=pCRLContext;
  1631. CryptUIDlgViewCRL(&CRLViewStruct);
  1632. }
  1633. else
  1634. {
  1635. I_NoticeBox(
  1636. GetLastError(),
  1637. 0,
  1638. NULL,
  1639. IDS_MSG_TITLE,
  1640. IDS_CRL_NAME,
  1641. IDS_MSG_INVALID_FILE,
  1642. MB_OK|MB_ICONINFORMATION);
  1643. }
  1644. if(pCRLContext)
  1645. CertFreeCRLContext(pCRLContext);
  1646. return S_OK;
  1647. }
  1648. //---------------------------------------------------------------------------------
  1649. //
  1650. // Mime Handler the command line "Open" for .CRL file
  1651. //---------------------------------------------------------------------------------
  1652. STDAPI CryptExtOpenCRL(HINSTANCE hinst, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
  1653. {
  1654. LPWSTR pwszFileName=NULL;
  1655. if (!lpszCmdLine)
  1656. return E_FAIL;
  1657. //get the WCHAR file name
  1658. pwszFileName=MkWStr(lpszCmdLine);
  1659. if(NULL==pwszFileName)
  1660. return HRESULT_FROM_WIN32(GetLastError());
  1661. CryptExtOpenCRLW(hinst, hPrevInstance, pwszFileName, nCmdShow);
  1662. if(pwszFileName)
  1663. FreeWStr(pwszFileName);
  1664. return S_OK;
  1665. }
  1666. //---------------------------------------------------------------------------------
  1667. //
  1668. // Handler the command line "Add" for .CRL file
  1669. //---------------------------------------------------------------------------------
  1670. STDAPI CryptExtAddCRLW(HINSTANCE hinst, HINSTANCE hPrevInstance, LPWSTR lpszCmdLine, int nCmdShow)
  1671. {
  1672. LPWSTR pwszFileName=NULL;
  1673. PCCRL_CONTEXT pCRLContext=NULL;
  1674. CRYPTUI_WIZ_IMPORT_SRC_INFO importSubject;
  1675. if (!lpszCmdLine)
  1676. return E_FAIL;
  1677. //get the WCHAR file name
  1678. pwszFileName=lpszCmdLine;
  1679. if(CryptQueryObject(CERT_QUERY_OBJECT_FILE,
  1680. pwszFileName,
  1681. CERT_QUERY_CONTENT_FLAG_CRL,
  1682. CERT_QUERY_FORMAT_FLAG_ALL,
  1683. 0,
  1684. NULL,
  1685. NULL,
  1686. NULL,
  1687. NULL,
  1688. NULL,
  1689. (const void **)&pCRLContext))
  1690. {
  1691. memset(&importSubject, 0, sizeof(CRYPTUI_WIZ_IMPORT_SRC_INFO));
  1692. importSubject.dwSize=sizeof(CRYPTUI_WIZ_IMPORT_SRC_INFO);
  1693. importSubject.dwSubjectChoice=CRYPTUI_WIZ_IMPORT_SUBJECT_CRL_CONTEXT;
  1694. importSubject.pCRLContext=pCRLContext;
  1695. CryptUIWizImport(0,
  1696. NULL,
  1697. NULL,
  1698. &importSubject,
  1699. NULL);
  1700. }
  1701. else
  1702. {
  1703. I_NoticeBox(
  1704. GetLastError(),
  1705. 0,
  1706. NULL,
  1707. IDS_MSG_TITLE,
  1708. IDS_CRL_NAME,
  1709. IDS_MSG_INVALID_FILE,
  1710. MB_OK|MB_ICONINFORMATION);
  1711. }
  1712. if(pCRLContext)
  1713. CertFreeCRLContext(pCRLContext);
  1714. return S_OK;
  1715. }
  1716. //---------------------------------------------------------------------------------
  1717. //
  1718. // Handler the command line "Add" for .CRL file
  1719. //---------------------------------------------------------------------------------
  1720. STDAPI CryptExtAddCRL(HINSTANCE hinst, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
  1721. {
  1722. LPWSTR pwszFileName=NULL;
  1723. if (!lpszCmdLine)
  1724. return E_FAIL;
  1725. //get the WCHAR file name
  1726. pwszFileName=MkWStr(lpszCmdLine);
  1727. if(NULL==pwszFileName)
  1728. return HRESULT_FROM_WIN32(GetLastError());
  1729. CryptExtAddCRLW(hinst, hPrevInstance, pwszFileName, nCmdShow);
  1730. if(pwszFileName)
  1731. FreeWStr(pwszFileName);
  1732. return S_OK;
  1733. }
  1734. //---------------------------------------------------------------------------------
  1735. //
  1736. // Mime Handler the command line "Open" for .SPC, .P7S, .P7B, .P7M file
  1737. //---------------------------------------------------------------------------------
  1738. STDAPI CryptExtOpenPKCS7W(HINSTANCE hinst, HINSTANCE hPrevInstance, LPWSTR lpszCmdLine, int nCmdShow)
  1739. {
  1740. LPWSTR pwszFileName=NULL;
  1741. HCERTSTORE hCertStore=NULL;
  1742. HCRYPTMSG hMsg=NULL;
  1743. DWORD dwContentType=0;
  1744. if (!lpszCmdLine)
  1745. return E_FAIL;
  1746. //get the WCHAR file name
  1747. pwszFileName=lpszCmdLine;
  1748. //check the object type. Make sure the PKCS7 is not embedded
  1749. if(CryptQueryObject(CERT_QUERY_OBJECT_FILE,
  1750. pwszFileName,
  1751. CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED,
  1752. CERT_QUERY_FORMAT_FLAG_ALL,
  1753. 0,
  1754. NULL,
  1755. &dwContentType,
  1756. NULL,
  1757. &hCertStore,
  1758. &hMsg,
  1759. NULL) )
  1760. {
  1761. LauchCertMgr(pwszFileName);
  1762. }
  1763. else
  1764. {
  1765. I_NoticeBox(
  1766. GetLastError(),
  1767. 0,
  1768. NULL,
  1769. IDS_MSG_TITLE,
  1770. IDS_PKCS7_NAME,
  1771. IDS_MSG_INVALID_FILE,
  1772. MB_OK|MB_ICONINFORMATION);
  1773. }
  1774. if(hCertStore)
  1775. CertCloseStore(hCertStore, 0);
  1776. if(hMsg)
  1777. CryptMsgClose(hMsg);
  1778. return S_OK;
  1779. }
  1780. //---------------------------------------------------------------------------------
  1781. //
  1782. // Mime Handler the command line "Open" for .SPC, .P7S, .P7B, .P7M file
  1783. //---------------------------------------------------------------------------------
  1784. STDAPI CryptExtOpenPKCS7(HINSTANCE hinst, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
  1785. {
  1786. LPWSTR pwszFileName=NULL;
  1787. if (!lpszCmdLine)
  1788. return E_FAIL;
  1789. //get the WCHAR file name
  1790. pwszFileName=MkWStr(lpszCmdLine);
  1791. if(NULL==pwszFileName)
  1792. return HRESULT_FROM_WIN32(GetLastError());
  1793. CryptExtOpenPKCS7W(hinst, hPrevInstance, pwszFileName, nCmdShow);
  1794. if(pwszFileName)
  1795. FreeWStr(pwszFileName);
  1796. return S_OK;
  1797. }
  1798. //---------------------------------------------------------------------------------
  1799. //
  1800. // Handler the command line "Add" for .SPC file
  1801. //---------------------------------------------------------------------------------
  1802. STDAPI CryptExtAddSPCW(HINSTANCE hinst, HINSTANCE hPrevInstance, LPWSTR lpszCmdLine, int nCmdShow)
  1803. {
  1804. LPWSTR pwszFileName=NULL;
  1805. HCERTSTORE hCertStore=NULL;
  1806. CRYPTUI_WIZ_IMPORT_SRC_INFO importSubject;
  1807. if (!lpszCmdLine)
  1808. return E_FAIL;
  1809. //get the WCHAR file name
  1810. pwszFileName=lpszCmdLine;
  1811. //check the object type. Make sure the PKCS7 is not embedded
  1812. if(CryptQueryObject(CERT_QUERY_OBJECT_FILE,
  1813. pwszFileName,
  1814. CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED,
  1815. CERT_QUERY_FORMAT_FLAG_ALL,
  1816. 0,
  1817. NULL,
  1818. NULL,
  1819. NULL,
  1820. &hCertStore,
  1821. NULL,
  1822. NULL) )
  1823. {
  1824. memset(&importSubject, 0, sizeof(CRYPTUI_WIZ_IMPORT_SRC_INFO));
  1825. importSubject.dwSize=sizeof(CRYPTUI_WIZ_IMPORT_SRC_INFO);
  1826. importSubject.dwSubjectChoice=CRYPTUI_WIZ_IMPORT_SUBJECT_FILE;
  1827. importSubject.pwszFileName=pwszFileName;
  1828. CryptUIWizImport(0,
  1829. NULL,
  1830. NULL,
  1831. &importSubject,
  1832. NULL);
  1833. }
  1834. else
  1835. {
  1836. I_NoticeBox(
  1837. GetLastError(),
  1838. 0,
  1839. NULL,
  1840. IDS_MSG_TITLE,
  1841. IDS_SPC_NAME,
  1842. IDS_MSG_INVALID_FILE,
  1843. MB_OK|MB_ICONINFORMATION);
  1844. }
  1845. return S_OK;
  1846. }
  1847. //---------------------------------------------------------------------------------
  1848. //
  1849. // Handler the command line "Add" for .SPC file
  1850. //---------------------------------------------------------------------------------
  1851. STDAPI CryptExtAddSPC(HINSTANCE hinst, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
  1852. {
  1853. LPWSTR pwszFileName=NULL;
  1854. if (!lpszCmdLine)
  1855. return E_FAIL;
  1856. //get the WCHAR file name
  1857. pwszFileName=MkWStr(lpszCmdLine);
  1858. if(NULL==pwszFileName)
  1859. return HRESULT_FROM_WIN32(GetLastError());
  1860. CryptExtAddSPCW(hinst, hPrevInstance, pwszFileName, nCmdShow);
  1861. if(pwszFileName)
  1862. FreeWStr(pwszFileName);
  1863. return S_OK;
  1864. }
  1865. //---------------------------------------------------------------------------------
  1866. //
  1867. // Mime Handler the command line "Open" for .p7r file. This file is returned by
  1868. // certificate authority
  1869. //---------------------------------------------------------------------------------
  1870. STDAPI CryptExtOpenP7RW(HINSTANCE hinst, HINSTANCE hPrevInstance, LPWSTR lpszCmdLine, int nCmdShow)
  1871. {
  1872. CRYPT_DATA_BLOB PKCS7Blob;
  1873. IEnroll *pIEnroll=NULL;
  1874. LPWSTR pwszFileName=NULL;
  1875. PCCERT_CONTEXT pCertContext=NULL;
  1876. CRYPTUI_VIEWCERTIFICATE_STRUCT ViewCertStruct;
  1877. UINT ids=IDS_INSTALL_CERT_SUCCEEDED;
  1878. //init
  1879. memset(&PKCS7Blob, 0, sizeof(CRYPT_DATA_BLOB));
  1880. //get the WCHAR file name
  1881. pwszFileName=lpszCmdLine;
  1882. if(FAILED(CoInitialize(NULL)))
  1883. {
  1884. ids=IDS_NO_XENROLL;
  1885. goto CLEANUP;
  1886. }
  1887. //initialize information for xEnroll
  1888. if(FAILED(CoCreateInstance(CLSID_CEnroll,
  1889. NULL,CLSCTX_INPROC_SERVER,IID_IEnroll,
  1890. (LPVOID *)&pIEnroll)))
  1891. {
  1892. ids=IDS_NO_XENROLL;
  1893. goto CLEANUP;
  1894. }
  1895. //get the BLOB from the file
  1896. if(S_OK != RetrieveBLOBFromFile(pwszFileName,&(PKCS7Blob.cbData),
  1897. &(PKCS7Blob.pbData)))
  1898. {
  1899. ids=IDS_INVALID_P7R_FILE;
  1900. goto CLEANUP;
  1901. }
  1902. //get the certifcate context
  1903. if(NULL==(pCertContext=pIEnroll->getCertContextFromPKCS7(&PKCS7Blob)))
  1904. {
  1905. ids=IDS_INVALID_P7R_FILE;
  1906. goto CLEANUP;
  1907. }
  1908. memset(&ViewCertStruct, 0, sizeof(CRYPTUI_VIEWCERTIFICATE_STRUCT));
  1909. ViewCertStruct.dwSize=sizeof(CRYPTUI_VIEWCERTIFICATE_STRUCT);
  1910. ViewCertStruct.hwndParent=NULL;
  1911. ViewCertStruct.dwFlags=CRYPTUI_ACCEPT_DECLINE_STYLE;
  1912. ViewCertStruct.pCertContext=pCertContext;
  1913. ViewCertStruct.cStores=0;
  1914. ViewCertStruct.rghStores=NULL;
  1915. if(!CryptUIDlgViewCertificate(&ViewCertStruct,NULL))
  1916. goto CLEANUP;
  1917. if(S_OK !=(pIEnroll->acceptPKCS7Blob(&PKCS7Blob)))
  1918. {
  1919. ids=IDS_FAIL_TO_INSTALL;
  1920. goto CLEANUP;
  1921. }
  1922. CLEANUP:
  1923. I_MessageBox(
  1924. NULL,
  1925. ids,
  1926. IDS_P7R_NAME,
  1927. NULL,
  1928. MB_OK|MB_APPLMODAL);
  1929. if(PKCS7Blob.pbData)
  1930. UnmapViewOfFile(PKCS7Blob.pbData);
  1931. if(pCertContext)
  1932. CertFreeCertificateContext(pCertContext);
  1933. if(pIEnroll)
  1934. pIEnroll->Release();
  1935. CoUninitialize( );
  1936. return S_OK;
  1937. }
  1938. //---------------------------------------------------------------------------------
  1939. //
  1940. // Mime Handler the command line "Open" for .p7r file. This file is returned by
  1941. // certificate authority
  1942. //---------------------------------------------------------------------------------
  1943. STDAPI CryptExtOpenP7R(HINSTANCE hinst, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
  1944. {
  1945. LPWSTR pwszFileName=NULL;
  1946. //get the WCHAR file name
  1947. pwszFileName=MkWStr(lpszCmdLine);
  1948. if(NULL==pwszFileName)
  1949. return HRESULT_FROM_WIN32(GetLastError());
  1950. CryptExtOpenP7RW(hinst, hPrevInstance, pwszFileName, nCmdShow);
  1951. if(pwszFileName)
  1952. FreeWStr(pwszFileName);
  1953. return S_OK;
  1954. }
  1955. //---------------------------------------------------------------------------------
  1956. //
  1957. // Mime Handler the command line "Open" for .p7r file. This file is returned by
  1958. // certificate authority
  1959. //---------------------------------------------------------------------------------
  1960. STDAPI CryptExtAddP7RW(HINSTANCE hinst, HINSTANCE hPrevInstance, LPWSTR lpszCmdLine, int nCmdShow)
  1961. {
  1962. CRYPT_DATA_BLOB PKCS7Blob;
  1963. IEnroll *pIEnroll=NULL;
  1964. LPWSTR pwszFileName=NULL;
  1965. UINT ids=IDS_INSTALL_CERT_SUCCEEDED;
  1966. //init
  1967. memset(&PKCS7Blob, 0, sizeof(CRYPT_DATA_BLOB));
  1968. //get the WCHAR file name
  1969. pwszFileName=lpszCmdLine;
  1970. if(FAILED(CoInitialize(NULL)))
  1971. {
  1972. ids=IDS_NO_XENROLL;
  1973. goto CLEANUP;
  1974. }
  1975. //initialize information for xEnroll
  1976. if(FAILED(CoCreateInstance(CLSID_CEnroll,
  1977. NULL,CLSCTX_INPROC_SERVER,IID_IEnroll,
  1978. (LPVOID *)&pIEnroll)))
  1979. {
  1980. ids=IDS_NO_XENROLL;
  1981. goto CLEANUP;
  1982. }
  1983. //get the BLOB from the file
  1984. if(S_OK != RetrieveBLOBFromFile(pwszFileName,&(PKCS7Blob.cbData),
  1985. &(PKCS7Blob.pbData)))
  1986. {
  1987. ids=IDS_INVALID_P7R_FILE;
  1988. goto CLEANUP;
  1989. }
  1990. if(S_OK !=(pIEnroll->acceptPKCS7Blob(&PKCS7Blob)))
  1991. {
  1992. ids=IDS_FAIL_TO_INSTALL;
  1993. goto CLEANUP;
  1994. }
  1995. CLEANUP:
  1996. I_MessageBox(
  1997. NULL,
  1998. ids,
  1999. IDS_P7R_NAME,
  2000. NULL,
  2001. MB_OK|MB_APPLMODAL);
  2002. if(PKCS7Blob.pbData)
  2003. UnmapViewOfFile(PKCS7Blob.pbData);
  2004. if(pIEnroll)
  2005. pIEnroll->Release();
  2006. CoUninitialize( );
  2007. return S_OK;
  2008. }
  2009. //---------------------------------------------------------------------------------
  2010. //
  2011. // Mime Handler the command line "Open" for .p7r file. This file is returned by
  2012. // certificate authority
  2013. //---------------------------------------------------------------------------------
  2014. STDAPI CryptExtAddP7R(HINSTANCE hinst, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
  2015. {
  2016. LPWSTR pwszFileName=NULL;
  2017. //get the WCHAR file name
  2018. pwszFileName=MkWStr(lpszCmdLine);
  2019. if(NULL==pwszFileName)
  2020. return HRESULT_FROM_WIN32(GetLastError());
  2021. CryptExtAddP7RW(hinst, hPrevInstance, pwszFileName, nCmdShow);
  2022. if(pwszFileName)
  2023. FreeWStr(pwszFileName);
  2024. return S_OK;
  2025. }
  2026. //---------------------------------------------------------------------------------
  2027. //
  2028. // Mime Handler the command line "Open" for .sst file
  2029. //---------------------------------------------------------------------------------
  2030. STDAPI CryptExtOpenSTRW(HINSTANCE hinst, HINSTANCE hPrevInstance, LPWSTR lpszCmdLine, int nCmdShow)
  2031. {
  2032. LPWSTR pwszFileName=NULL;
  2033. HCERTSTORE hCertStore=NULL;
  2034. if (!lpszCmdLine)
  2035. return E_FAIL;
  2036. //get the WCHAR file name
  2037. pwszFileName=lpszCmdLine;
  2038. //check the object type.
  2039. if(CryptQueryObject(CERT_QUERY_OBJECT_FILE,
  2040. pwszFileName,
  2041. CERT_QUERY_CONTENT_FLAG_SERIALIZED_STORE |
  2042. CERT_QUERY_CONTENT_FLAG_SERIALIZED_CERT |
  2043. CERT_QUERY_CONTENT_FLAG_SERIALIZED_CTL |
  2044. CERT_QUERY_CONTENT_FLAG_SERIALIZED_CRL,
  2045. CERT_QUERY_FORMAT_FLAG_ALL,
  2046. 0,
  2047. NULL,
  2048. NULL,
  2049. NULL,
  2050. &hCertStore,
  2051. NULL,
  2052. NULL))
  2053. {
  2054. LauchCertMgr(pwszFileName);
  2055. }
  2056. else
  2057. {
  2058. I_NoticeBox(
  2059. GetLastError(),
  2060. 0,
  2061. NULL,
  2062. IDS_MSG_TITLE,
  2063. IDS_SST_NAME,
  2064. IDS_MSG_INVALID_FILE,
  2065. MB_OK|MB_ICONINFORMATION);
  2066. }
  2067. if(hCertStore)
  2068. CertCloseStore(hCertStore, 0);
  2069. return S_OK;
  2070. }
  2071. //---------------------------------------------------------------------------------
  2072. //
  2073. // Mime Handler the command line "Open" for .sst file
  2074. //---------------------------------------------------------------------------------
  2075. STDAPI CryptExtOpenSTR(HINSTANCE hinst, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
  2076. {
  2077. LPWSTR pwszFileName=NULL;
  2078. if (!lpszCmdLine)
  2079. return E_FAIL;
  2080. //get the WCHAR file name
  2081. pwszFileName=MkWStr(lpszCmdLine);
  2082. if(NULL==pwszFileName)
  2083. return HRESULT_FROM_WIN32(GetLastError());
  2084. CryptExtOpenSTRW(hinst, hPrevInstance, pwszFileName, nCmdShow);
  2085. if(pwszFileName)
  2086. FreeWStr(pwszFileName);
  2087. return S_OK;
  2088. }
  2089. //---------------------------------------------------------------------------------
  2090. //
  2091. // Mime Handler the command line "Open" for .p10 file
  2092. //---------------------------------------------------------------------------------
  2093. STDAPI CryptExtOpenP10W(HINSTANCE hinst, HINSTANCE hPrevInstance, LPWSTR lpszCmdLine, int nCmdShow)
  2094. {
  2095. LPWSTR pwszFileName=NULL;
  2096. if (!lpszCmdLine)
  2097. return E_FAIL;
  2098. //get the WCHAR file name
  2099. pwszFileName=lpszCmdLine;
  2100. //check the object type.
  2101. if(CryptQueryObject(CERT_QUERY_OBJECT_FILE,
  2102. pwszFileName,
  2103. CERT_QUERY_CONTENT_FLAG_PKCS10,
  2104. CERT_QUERY_FORMAT_FLAG_ALL,
  2105. 0,
  2106. NULL,
  2107. NULL,
  2108. NULL,
  2109. NULL,
  2110. NULL,
  2111. NULL))
  2112. {
  2113. I_NoticeBox(
  2114. 0,
  2115. 0,
  2116. NULL,
  2117. IDS_MSG_TITLE,
  2118. IDS_P10_NAME,
  2119. IDS_MSG_VALID_FILE,
  2120. MB_OK|MB_ICONINFORMATION);
  2121. }
  2122. else
  2123. {
  2124. I_NoticeBox(
  2125. GetLastError(),
  2126. 0,
  2127. NULL,
  2128. IDS_MSG_TITLE,
  2129. IDS_P10_NAME,
  2130. IDS_MSG_INVALID_FILE,
  2131. MB_OK|MB_ICONINFORMATION);
  2132. }
  2133. return S_OK;
  2134. }
  2135. //---------------------------------------------------------------------------------
  2136. //
  2137. // Mime Handler the command line "Open" for .p10 file
  2138. //---------------------------------------------------------------------------------
  2139. STDAPI CryptExtOpenP10(HINSTANCE hinst, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
  2140. {
  2141. LPWSTR pwszFileName=NULL;
  2142. if (!lpszCmdLine)
  2143. return E_FAIL;
  2144. //get the WCHAR file name
  2145. pwszFileName=MkWStr(lpszCmdLine);
  2146. if(NULL==pwszFileName)
  2147. return HRESULT_FROM_WIN32(GetLastError());
  2148. CryptExtOpenP10W(hinst, hPrevInstance, pwszFileName, nCmdShow);
  2149. if(pwszFileName)
  2150. FreeWStr(pwszFileName);
  2151. return S_OK;
  2152. }
  2153. //--------------------------------------------------------------------------------
  2154. //
  2155. //get the bytes from the file name
  2156. //
  2157. //---------------------------------------------------------------------------------
  2158. HRESULT RetrieveBLOBFromFile(LPWSTR pwszFileName,DWORD *pcb,BYTE **ppb)
  2159. {
  2160. HRESULT hr=E_FAIL;
  2161. HANDLE hFile=NULL;
  2162. HANDLE hFileMapping=NULL;
  2163. DWORD cbData=0;
  2164. BYTE *pbData=0;
  2165. DWORD cbHighSize=0;
  2166. if(!pcb || !ppb || !pwszFileName)
  2167. return E_INVALIDARG;
  2168. *ppb=NULL;
  2169. *pcb=0;
  2170. if ((hFile = CreateFileU(pwszFileName,
  2171. GENERIC_READ,
  2172. FILE_SHARE_READ,
  2173. NULL, // lpsa
  2174. OPEN_EXISTING,
  2175. FILE_ATTRIBUTE_NORMAL,
  2176. NULL)) == INVALID_HANDLE_VALUE)
  2177. {
  2178. hr=HRESULT_FROM_WIN32(GetLastError());
  2179. goto CLEANUP;
  2180. }
  2181. if((cbData = GetFileSize(hFile, &cbHighSize)) == 0xffffffff)
  2182. {
  2183. hr=HRESULT_FROM_WIN32(GetLastError());
  2184. goto CLEANUP;
  2185. }
  2186. //we do not handle file more than 4G bytes
  2187. if(cbHighSize != 0)
  2188. {
  2189. hr=E_FAIL;
  2190. goto CLEANUP;
  2191. }
  2192. //create a file mapping object
  2193. if(NULL == (hFileMapping=CreateFileMapping(
  2194. hFile,
  2195. NULL,
  2196. PAGE_READONLY,
  2197. 0,
  2198. 0,
  2199. NULL)))
  2200. {
  2201. hr=HRESULT_FROM_WIN32(GetLastError());
  2202. goto CLEANUP;
  2203. }
  2204. //create a view of the file
  2205. if(NULL == (pbData=(BYTE *)MapViewOfFile(
  2206. hFileMapping,
  2207. FILE_MAP_READ,
  2208. 0,
  2209. 0,
  2210. cbData)))
  2211. {
  2212. hr=HRESULT_FROM_WIN32(GetLastError());
  2213. goto CLEANUP;
  2214. }
  2215. hr=S_OK;
  2216. *pcb=cbData;
  2217. *ppb=pbData;
  2218. CLEANUP:
  2219. if(hFile)
  2220. CloseHandle(hFile);
  2221. if(hFileMapping)
  2222. CloseHandle(hFileMapping);
  2223. return hr;
  2224. }
  2225. //-----------------------------------------------------------------------
  2226. // Private implementation of the message box
  2227. //------------------------------------------------------------------------
  2228. int I_NoticeBox(
  2229. DWORD dwError,
  2230. DWORD dwFlags,
  2231. HWND hWnd,
  2232. UINT idsTitle,
  2233. UINT idsFileName,
  2234. UINT idsMsgFormat,
  2235. UINT uType)
  2236. {
  2237. WCHAR wszTitle[MAX_STRING_SIZE];
  2238. WCHAR wszFileName[MAX_STRING_SIZE];
  2239. WCHAR wszMsg[MAX_STRING_SIZE];
  2240. WCHAR wszMsgFormat[MAX_STRING_SIZE];
  2241. #if (0) //DSIE: Bug 160612
  2242. if(!LoadStringU(g_hmodThisDll, idsTitle, wszTitle, sizeof(wszTitle)))
  2243. #else
  2244. if(!LoadStringU(g_hmodThisDll, idsTitle, wszTitle, sizeof(wszTitle) / sizeof(wszTitle[0])))
  2245. #endif
  2246. return 0;
  2247. #if (0) //DSIE: Bug 160613
  2248. if(!LoadStringU(g_hmodThisDll, idsFileName, wszFileName, sizeof(wszFileName)))
  2249. #else
  2250. if(!LoadStringU(g_hmodThisDll, idsFileName, wszFileName, sizeof(wszFileName) / sizeof(wszFileName[0])))
  2251. #endif
  2252. return 0;
  2253. if(E_ACCESSDENIED == dwError)
  2254. {
  2255. #if (0) //DSIE: Simular bug to 160612 & 160613 not caught by Prefix tools.
  2256. if(!LoadStringU(g_hmodThisDll, IDS_ACCESS_DENIED, wszMsgFormat, sizeof(wszMsgFormat)))
  2257. #else
  2258. if(!LoadStringU(g_hmodThisDll, IDS_ACCESS_DENIED, wszMsgFormat, sizeof(wszMsgFormat) / sizeof(wszMsgFormat[0])))
  2259. #endif
  2260. return 0;
  2261. }
  2262. else
  2263. {
  2264. #if (0) //DSIE: Simular bug to 160612 & 160613 not caught by Prefix tools.
  2265. if(!LoadStringU(g_hmodThisDll, idsMsgFormat, wszMsgFormat, sizeof(wszMsgFormat)))
  2266. #else
  2267. if(!LoadStringU(g_hmodThisDll, idsMsgFormat, wszMsgFormat, sizeof(wszMsgFormat) / sizeof(wszMsgFormat[0])))
  2268. #endif
  2269. return 0;
  2270. }
  2271. //make the string
  2272. if(0 == swprintf(wszMsg, wszMsgFormat, wszFileName))
  2273. return 0;
  2274. return MessageBoxU(hWnd, wszMsg, wszTitle, uType);
  2275. }
  2276. //-----------------------------------------------------------------------
  2277. // Private implementation of the message box
  2278. //------------------------------------------------------------------------
  2279. int I_MessageBox(
  2280. HWND hWnd,
  2281. UINT idsText,
  2282. UINT idsCaption,
  2283. LPCWSTR pwszCaption,
  2284. UINT uType
  2285. )
  2286. {
  2287. WCHAR wszText[MAX_STRING_SIZE];
  2288. WCHAR wszCaption[MAX_STRING_SIZE];
  2289. UINT intReturn=0;
  2290. //get the caption string
  2291. if(NULL == pwszCaption)
  2292. {
  2293. #if (0) //DSIE: Bug 160611
  2294. if(!LoadStringU(g_hmodThisDll, idsCaption, wszCaption, sizeof(wszCaption)))
  2295. #else
  2296. if(!LoadStringU(g_hmodThisDll, idsCaption, wszCaption, sizeof(wszCaption) / sizeof(wszCaption[0])))
  2297. #endif
  2298. return 0;
  2299. }
  2300. //get the text string
  2301. #if (0) //DSIE: Bug 160610
  2302. if(!LoadStringU(g_hmodThisDll, idsText, wszText, sizeof(wszText)))
  2303. #else
  2304. if(!LoadStringU(g_hmodThisDll, idsText, wszText, sizeof(wszText) / sizeof(wszText[0])))
  2305. #endif
  2306. {
  2307. return 0;
  2308. }
  2309. //message box
  2310. if( pwszCaption)
  2311. {
  2312. intReturn=MessageBoxU(hWnd, wszText, pwszCaption, uType);
  2313. }
  2314. else
  2315. intReturn=MessageBoxU(hWnd, wszText, wszCaption, uType);
  2316. return intReturn;
  2317. }
  2318. //--------------------------------------------------------------------------
  2319. //
  2320. // FormatMessageUnicode
  2321. //
  2322. //--------------------------------------------------------------------------
  2323. BOOL FormatMessageUnicode(LPWSTR *ppwszFormat,UINT ids,...)
  2324. {
  2325. // get format string from resources
  2326. WCHAR wszFormat[1000];
  2327. va_list argList;
  2328. DWORD cbMsg=0;
  2329. BOOL fResult=FALSE;
  2330. HRESULT hr=S_OK;
  2331. if(NULL == ppwszFormat)
  2332. goto InvalidArgErr;
  2333. #if (0) //DSIE: Bug 160609
  2334. if(!LoadStringU(g_hmodThisDll, ids, wszFormat, sizeof(wszFormat)))
  2335. #else
  2336. if(!LoadStringU(g_hmodThisDll, ids, wszFormat, sizeof(wszFormat) / sizeof(wszFormat[0])))
  2337. #endif
  2338. goto LoadStringError;
  2339. // format message into requested buffer
  2340. va_start(argList, ids);
  2341. cbMsg = FormatMessageU(
  2342. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_STRING,
  2343. wszFormat,
  2344. 0, // dwMessageId
  2345. 0, // dwLanguageId
  2346. (LPWSTR) (ppwszFormat),
  2347. 0, // minimum size to allocate
  2348. &argList);
  2349. va_end(argList);
  2350. if(!cbMsg)
  2351. goto FormatMessageError;
  2352. fResult=TRUE;
  2353. CommonReturn:
  2354. return fResult;
  2355. ErrorReturn:
  2356. fResult=FALSE;
  2357. goto CommonReturn;
  2358. TRACE_ERROR(LoadStringError);
  2359. TRACE_ERROR(FormatMessageError);
  2360. SET_ERROR(InvalidArgErr, E_INVALIDARG);
  2361. }
  2362. //--------------------------------------------------------------------------
  2363. //
  2364. // LauchCertMgr()
  2365. //
  2366. // We use W version of APIs since this call is only made on WinNT5
  2367. //
  2368. //--------------------------------------------------------------------------
  2369. void LauchCertMgr(LPWSTR pwszFileName)
  2370. {
  2371. LPWSTR pwszCommandParam=NULL;
  2372. LPWSTR pwszRealFileName=NULL;
  2373. WCHAR wszMSCFileName[_MAX_PATH];
  2374. WCHAR wszSystemDirectory[_MAX_PATH];
  2375. if(NULL == pwszFileName)
  2376. return;
  2377. pwszRealFileName=(LPWSTR)LocalAlloc(LPTR, sizeof(WCHAR)*(wcslen(pwszFileName)+10));
  2378. if(NULL == pwszRealFileName)
  2379. return;
  2380. //add the " around the file name
  2381. wcscpy(pwszRealFileName, L"\"");
  2382. wcscat(pwszRealFileName, pwszFileName);
  2383. wcscat(pwszRealFileName, L"\"");
  2384. //Open the MMC via "MMC.exe CertMgr.msc /certmgr:FileName=MyFoo.Exe"
  2385. //get the system path
  2386. if(GetSystemDirectoryW(wszSystemDirectory, sizeof(wszSystemDirectory)/sizeof(wszSystemDirectory[0])))
  2387. {
  2388. //copy the system directory
  2389. wcscpy(wszMSCFileName, wszSystemDirectory);
  2390. //cancatecate the string \certmgr.msc
  2391. wcscat(wszMSCFileName, CERTMGR_MSC);
  2392. //make the string "MMC.exe c:\winnt\system32\CertMgr.msc /certmgr:FileName=MyFoo.Exe"
  2393. if(FormatMessageUnicode(&pwszCommandParam, IDS_MMC_PARAM,
  2394. wszMSCFileName, pwszRealFileName))
  2395. {
  2396. ShellExecuteW(NULL,
  2397. L"Open",
  2398. MMC_NAME,
  2399. pwszCommandParam,
  2400. wszSystemDirectory,
  2401. SW_SHOWNORMAL);
  2402. }
  2403. }
  2404. if(pwszCommandParam)
  2405. LocalFree((HLOCAL)pwszCommandParam);
  2406. if(pwszRealFileName)
  2407. LocalFree((HLOCAL)pwszRealFileName);
  2408. }