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.

760 lines
28 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1997 - 1999
  6. //
  7. // File: selstore.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. #include "global.hxx"
  11. #include <dbgdef.h>
  12. extern HINSTANCE HinstDll;
  13. extern HMODULE HmodRichEdit;
  14. static const HELPMAP helpmap[] = {
  15. {IDC_SELECTSTORE_TREE, IDH_SELECTSTORE_STORE_TREE},
  16. {IDC_SHOWPHYSICALSTORES_CHECK, IDH_SELECTSTORE_SHOWPHYSICAL_CHECK}
  17. };
  18. typedef struct _STORE_SELECT_HELPER
  19. {
  20. PCCRYPTUI_SELECTSTORE_STRUCTW pcss;
  21. HCERTSTORE hSelectedStore;
  22. HWND hwndTreeView;
  23. DWORD dwExtraOpenStoreFlag;
  24. HTREEITEM hParentItem;
  25. BOOL fCollapseMode;
  26. int CurrentSysEnumIndex;
  27. int CurrentPhysEnumIndex;
  28. } STORE_SELECT_HELPER, *PSTORE_SELECT_HELPER;
  29. typedef struct _OPEN_STORE_STRUCT
  30. {
  31. BOOL fStoreHandle;
  32. HCERTSTORE hCertStore;
  33. DWORD dwFlags;
  34. LPCSTR ProviderType;
  35. LPWSTR pwszStoreName;
  36. PCERT_PHYSICAL_STORE_INFO pStoreInfo;
  37. int EnumerationStructIndex;
  38. } OPEN_STORE_STRUCT, *POPEN_STORE_STRUCT;
  39. //////////////////////////////////////////////////////////////////////////////////////
  40. //
  41. //////////////////////////////////////////////////////////////////////////////////////
  42. static POPEN_STORE_STRUCT AllocAndReturnOpenStoreStruct(
  43. BOOL fStoreHandle,
  44. HCERTSTORE hCertStore,
  45. DWORD dwFlags,
  46. LPCSTR ProviderType,
  47. LPWSTR pwszStoreName,
  48. PCERT_PHYSICAL_STORE_INFO pStoreInfo,
  49. int EnumerationStructIndex)
  50. {
  51. POPEN_STORE_STRUCT pOpenStoreStruct;
  52. if (NULL == (pOpenStoreStruct = (POPEN_STORE_STRUCT) malloc(sizeof(OPEN_STORE_STRUCT))))
  53. {
  54. return FALSE;
  55. }
  56. pOpenStoreStruct->fStoreHandle = fStoreHandle;
  57. if (fStoreHandle)
  58. {
  59. pOpenStoreStruct->hCertStore = hCertStore;
  60. }
  61. else
  62. {
  63. if (NULL == (pOpenStoreStruct->pwszStoreName = AllocAndCopyWStr(pwszStoreName)))
  64. {
  65. free(pOpenStoreStruct);
  66. return NULL;
  67. }
  68. pOpenStoreStruct->dwFlags = dwFlags;
  69. pOpenStoreStruct->ProviderType = ProviderType;
  70. pOpenStoreStruct->pStoreInfo = NULL;
  71. }
  72. pOpenStoreStruct->EnumerationStructIndex = EnumerationStructIndex;
  73. return pOpenStoreStruct;
  74. }
  75. //////////////////////////////////////////////////////////////////////////////////////
  76. //
  77. //////////////////////////////////////////////////////////////////////////////////////
  78. static void FreeOpenStoreStruct(POPEN_STORE_STRUCT pOpenStoreStruct)
  79. {
  80. if (!(pOpenStoreStruct->fStoreHandle))
  81. {
  82. free(pOpenStoreStruct->pwszStoreName);
  83. }
  84. free(pOpenStoreStruct);
  85. }
  86. //////////////////////////////////////////////////////////////////////////////////////
  87. //
  88. //////////////////////////////////////////////////////////////////////////////////////
  89. static BOOL WINAPI EnumPhyCallback(
  90. IN const void *pvSystemStore,
  91. IN DWORD dwFlags,
  92. IN LPCWSTR pwszStoreName,
  93. IN PCERT_PHYSICAL_STORE_INFO pStoreInfo,
  94. IN OPTIONAL void *pvReserved,
  95. IN OPTIONAL void *pvArg
  96. )
  97. {
  98. PSTORE_SELECT_HELPER pviewhelp = (PSTORE_SELECT_HELPER) pvArg;
  99. TVINSERTSTRUCTW tvins;
  100. LPWSTR pwszFullStoreName;
  101. HCERTSTORE hTestStore = NULL;
  102. DWORD dwAccess;
  103. DWORD cbdwAccess = sizeof(DWORD);
  104. LPCWSTR pszLocalizedName;
  105. //
  106. // if the store that is passed back cannot be opened, OR,
  107. // if the caller specified to display writable stores only, and
  108. // the current store being enumerated is read only, then that store
  109. // will not be displayed
  110. //
  111. if ((pStoreInfo->dwFlags & (CERT_PHYSICAL_STORE_OPEN_DISABLE_FLAG | CERT_PHYSICAL_STORE_REMOTE_OPEN_DISABLE_FLAG)) ||
  112. ((pviewhelp->pcss->dwFlags & CRYPTUI_DISPLAY_WRITE_ONLY_STORES) &&
  113. (pStoreInfo->dwOpenFlags & CERT_STORE_READONLY_FLAG)))
  114. {
  115. return TRUE;
  116. }
  117. if (NULL == (pwszFullStoreName = (LPWSTR) malloc((wcslen((LPWSTR)pvSystemStore)+wcslen(L"\\")+wcslen(pwszStoreName)+1) * sizeof(WCHAR))))
  118. {
  119. return FALSE;
  120. }
  121. wcscpy(pwszFullStoreName, (LPWSTR)pvSystemStore);
  122. wcscat(pwszFullStoreName, L"\\");
  123. wcscat(pwszFullStoreName, pwszStoreName);
  124. //
  125. // now, if the caller passed in the CRYPTUI_VALIDATE_STORES_AS_WRITABLE flag,
  126. // we need to verify that the store can actually be opened with writable rights
  127. //
  128. if (pviewhelp->pcss->dwFlags & CRYPTUI_VALIDATE_STORES_AS_WRITABLE)
  129. {
  130. hTestStore = CertOpenStore(
  131. CERT_STORE_PROV_PHYSICAL,
  132. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
  133. NULL,
  134. (dwFlags & CERT_SYSTEM_STORE_MASK) |
  135. pviewhelp->dwExtraOpenStoreFlag |
  136. CERT_STORE_SET_LOCALIZED_NAME_FLAG |
  137. (pviewhelp->pcss->pStoresForSelection->rgEnumerationStructs[pviewhelp->CurrentPhysEnumIndex].dwFlags & CERT_STORE_MAXIMUM_ALLOWED_FLAG),
  138. pwszFullStoreName);
  139. if (hTestStore == NULL)
  140. {
  141. free(pwszFullStoreName);
  142. return TRUE;
  143. }
  144. //
  145. // make call to get the store property to see if it is writable
  146. //
  147. CertGetStoreProperty(hTestStore, CERT_ACCESS_STATE_PROP_ID, &dwAccess, &cbdwAccess);
  148. CertCloseStore(hTestStore, 0);
  149. //
  150. // if the store can't be written to, then simply return
  151. //
  152. if (!(dwAccess & CERT_ACCESS_STATE_WRITE_PERSIST_FLAG))
  153. {
  154. return TRUE;
  155. }
  156. }
  157. pszLocalizedName = CryptFindLocalizedName(pwszStoreName);
  158. tvins.item.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM;
  159. tvins.hParent = pviewhelp->hParentItem;
  160. tvins.hInsertAfter = TVI_LAST;
  161. tvins.item.pszText = (pszLocalizedName != NULL) ? (LPWSTR) pszLocalizedName : (LPWSTR) pwszStoreName;
  162. tvins.item.cchTextMax = wcslen(tvins.item.pszText);
  163. tvins.item.iImage = 0;
  164. tvins.item.iSelectedImage = tvins.item.iImage;
  165. tvins.item.lParam = (LPARAM) AllocAndReturnOpenStoreStruct(
  166. FALSE,
  167. 0,
  168. (dwFlags & CERT_SYSTEM_STORE_MASK) |
  169. (pviewhelp->pcss->pStoresForSelection->rgEnumerationStructs[pviewhelp->CurrentPhysEnumIndex].dwFlags & CERT_STORE_MAXIMUM_ALLOWED_FLAG),
  170. CERT_STORE_PROV_PHYSICAL,
  171. (LPWSTR) pwszFullStoreName,
  172. pStoreInfo,
  173. -1);
  174. SendMessage(pviewhelp->hwndTreeView, TVM_INSERTITEMW, 0, (LPARAM) &tvins);
  175. free(pwszFullStoreName);
  176. return TRUE;
  177. }
  178. //////////////////////////////////////////////////////////////////////////////////////
  179. //
  180. //////////////////////////////////////////////////////////////////////////////////////
  181. static BOOL WINAPI EnumSysCallback(
  182. IN const void* pwszSystemStore,
  183. IN DWORD dwFlags,
  184. IN PCERT_SYSTEM_STORE_INFO pStoreInfo,
  185. IN OPTIONAL void *pvReserved,
  186. IN OPTIONAL void *pvArg
  187. )
  188. {
  189. PSTORE_SELECT_HELPER pviewhelp = (PSTORE_SELECT_HELPER) pvArg;
  190. TVINSERTSTRUCTW tvins;
  191. HTREEITEM hItem;
  192. LPCWSTR pszLocalizedName;
  193. HCERTSTORE hTestStore = NULL;
  194. DWORD dwAccess;
  195. DWORD cbdwAccess = sizeof(DWORD);
  196. if ((_wcsicmp((LPWSTR)pwszSystemStore, L"acrs") == 0) ||
  197. (_wcsicmp((LPWSTR)pwszSystemStore, L"request") == 0))
  198. {
  199. return TRUE;
  200. }
  201. //
  202. // now, if the caller passed in the CRYPTUI_VALIDATE_STORES_AS_WRITABLE flag,
  203. // we need to verify that the store can actually be opened with writable rights
  204. //
  205. if (pviewhelp->pcss->dwFlags & CRYPTUI_VALIDATE_STORES_AS_WRITABLE)
  206. {
  207. hTestStore = CertOpenStore(
  208. CERT_STORE_PROV_SYSTEM,
  209. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
  210. NULL,
  211. (dwFlags & CERT_SYSTEM_STORE_MASK) |
  212. pviewhelp->dwExtraOpenStoreFlag |
  213. CERT_STORE_SET_LOCALIZED_NAME_FLAG |
  214. (pviewhelp->pcss->pStoresForSelection->rgEnumerationStructs[pviewhelp->CurrentSysEnumIndex].dwFlags & CERT_STORE_MAXIMUM_ALLOWED_FLAG),
  215. pwszSystemStore);
  216. if (hTestStore == NULL)
  217. {
  218. return TRUE;
  219. }
  220. //
  221. // make call to get the store property to see if it is writable
  222. //
  223. CertGetStoreProperty(hTestStore, CERT_ACCESS_STATE_PROP_ID, &dwAccess, &cbdwAccess);
  224. CertCloseStore(hTestStore, 0);
  225. //
  226. // if the store can't be written to, then simply return
  227. //
  228. if (!(dwAccess & CERT_ACCESS_STATE_WRITE_PERSIST_FLAG))
  229. {
  230. return TRUE;
  231. }
  232. }
  233. pszLocalizedName = CryptFindLocalizedName((LPWSTR)pwszSystemStore);
  234. tvins.item.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM;
  235. tvins.hParent = TVI_ROOT;
  236. tvins.hInsertAfter = TVI_LAST;
  237. tvins.item.pszText = (pszLocalizedName != NULL) ? (LPWSTR) pszLocalizedName : (LPWSTR) pwszSystemStore;
  238. tvins.item.cchTextMax = wcslen(tvins.item.pszText);
  239. tvins.item.iImage = 0;
  240. tvins.item.iSelectedImage = tvins.item.iImage;
  241. tvins.item.lParam = (LPARAM) AllocAndReturnOpenStoreStruct(
  242. FALSE,
  243. 0,
  244. dwFlags & CERT_SYSTEM_STORE_MASK |
  245. (pviewhelp->pcss->pStoresForSelection->rgEnumerationStructs[pviewhelp->CurrentSysEnumIndex].dwFlags & CERT_STORE_MAXIMUM_ALLOWED_FLAG),
  246. CERT_STORE_PROV_SYSTEM,
  247. (LPWSTR) pwszSystemStore,
  248. NULL,
  249. pviewhelp->CurrentSysEnumIndex);
  250. pviewhelp->hParentItem = (HTREEITEM) SendMessage(pviewhelp->hwndTreeView, TVM_INSERTITEMW, 0, (LPARAM) &tvins);
  251. return TRUE;
  252. }
  253. //////////////////////////////////////////////////////////////////////////////////////
  254. //
  255. //////////////////////////////////////////////////////////////////////////////////////
  256. INT_PTR APIENTRY SelectStoreDialogProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
  257. {
  258. PSTORE_SELECT_HELPER pviewhelp;
  259. HIMAGELIST hIml;
  260. PCCRYPTUI_SELECTSTORE_STRUCTW pcss;
  261. TV_ITEM tvi;
  262. TVINSERTSTRUCTW tvins;
  263. DWORD i;
  264. LPNMTREEVIEW pnmtv;
  265. HTREEITEM hParentItem, hChildItem;
  266. HWND hwndTreeView;
  267. POPEN_STORE_STRUCT pOpenStoreStruct;
  268. WCHAR szText[CRYPTUI_MAX_STRING_SIZE];
  269. HWND hwnd;
  270. WCHAR errorString[CRYPTUI_MAX_STRING_SIZE];
  271. WCHAR errorTitle[CRYPTUI_MAX_STRING_SIZE];
  272. switch ( msg ) {
  273. case WM_INITDIALOG:
  274. pviewhelp = (PSTORE_SELECT_HELPER) lParam;
  275. SetWindowLongPtr(hwndDlg, DWLP_USER, (DWORD_PTR) pviewhelp);
  276. pcss = pviewhelp->pcss;
  277. pviewhelp->hwndTreeView = GetDlgItem(hwndDlg, IDC_SELECTSTORE_TREE);
  278. pviewhelp->fCollapseMode = FALSE;
  279. //
  280. // set the dialog title and the display string
  281. //
  282. if (pcss->szTitle != NULL)
  283. {
  284. SetWindowTextU(hwndDlg, pcss->szTitle);
  285. }
  286. if (pcss->szDisplayString != NULL)
  287. {
  288. SetDlgItemTextU(hwndDlg, IDC_SELECTSTORE_DISPLAYSTRING, pcss->szDisplayString);
  289. }
  290. else
  291. {
  292. LoadStringU(HinstDll, IDS_SELECT_STORE_DEFAULT, szText, ARRAYSIZE(szText));
  293. SetDlgItemTextU(hwndDlg, IDC_SELECTSTORE_DISPLAYSTRING, szText);
  294. }
  295. //
  296. // enable/disable the show physical stores check box
  297. //
  298. if (pcss->dwFlags & CRYPTUI_ALLOW_PHYSICAL_STORE_VIEW)
  299. {
  300. ShowWindow(GetDlgItem(hwndDlg, IDC_SHOWPHYSICALSTORES_CHECK), SW_SHOW);
  301. }
  302. else
  303. {
  304. ShowWindow(GetDlgItem(hwndDlg, IDC_SHOWPHYSICALSTORES_CHECK), SW_HIDE);
  305. }
  306. //
  307. // Build up the image list for the control
  308. //
  309. hIml = ImageList_LoadImage(HinstDll, MAKEINTRESOURCE(IDB_FOLDER), 0, 1, RGB(255,0,255), IMAGE_BITMAP, 0);
  310. if (hIml != NULL)
  311. {
  312. TreeView_SetImageList(GetDlgItem(hwndDlg, IDC_SELECTSTORE_TREE), hIml, TVSIL_NORMAL);
  313. }
  314. //
  315. // add all of the stores from the enumeration to the tree view
  316. //
  317. i = 0;
  318. while (i < pcss->pStoresForSelection->cEnumerationStructs)
  319. {
  320. pviewhelp->CurrentSysEnumIndex = (int) i;
  321. if (!CertEnumSystemStore(
  322. pcss->pStoresForSelection->rgEnumerationStructs[i].dwFlags,
  323. pcss->pStoresForSelection->rgEnumerationStructs[i].pvSystemStoreLocationPara,
  324. pviewhelp,
  325. EnumSysCallback))
  326. {
  327. // ERROR
  328. }
  329. i++;
  330. }
  331. //
  332. // add all of the stores from the enumeration to the tree view
  333. //
  334. i = 0;
  335. while (i < pcss->pStoresForSelection->cStores)
  336. {
  337. memset(&tvins, 0, sizeof(tvins));
  338. tvins.item.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM;
  339. tvins.hParent = TVI_ROOT;
  340. tvins.hInsertAfter = TVI_LAST;
  341. tvins.item.pszText = (LPWSTR) GetStoreName(pcss->pStoresForSelection->rghStores[i], TRUE);
  342. //
  343. // if we didn't get a name then just continue on to the next store
  344. //
  345. if (tvins.item.pszText == NULL)
  346. {
  347. i++;
  348. continue;
  349. }
  350. tvins.item.cchTextMax = wcslen(tvins.item.pszText);
  351. tvins.item.iImage = 0;
  352. tvins.item.iSelectedImage = tvins.item.iImage;
  353. tvins.item.lParam =
  354. (LPARAM) AllocAndReturnOpenStoreStruct(TRUE, pcss->pStoresForSelection->rghStores[i], 0, NULL, NULL, NULL, -1);
  355. SendMessage(pviewhelp->hwndTreeView, TVM_INSERTITEMW, 0, (LPARAM) &tvins);
  356. free(tvins.item.pszText);
  357. i++;
  358. }
  359. break;
  360. case WM_NOTIFY:
  361. pviewhelp = (PSTORE_SELECT_HELPER) GetWindowLongPtr(hwndDlg, DWLP_USER);
  362. if (pviewhelp == NULL)
  363. {
  364. break;
  365. }
  366. pcss = pviewhelp->pcss;
  367. switch (((NMHDR FAR *) lParam)->code) {
  368. case TVN_ITEMEXPANDINGA:
  369. case TVN_ITEMEXPANDINGW:
  370. pnmtv = (LPNMTREEVIEW) lParam;
  371. //
  372. // if in collapse mode the just return
  373. //
  374. if (pviewhelp->fCollapseMode)
  375. {
  376. return TRUE;
  377. }
  378. //
  379. // don't allow expansion if physical stores are not allowed to be viewed
  380. //
  381. if (!((pcss->dwFlags & CRYPTUI_ALLOW_PHYSICAL_STORE_VIEW) &&
  382. ((SendMessage(GetDlgItem(hwndDlg, IDC_SHOWPHYSICALSTORES_CHECK), BM_GETSTATE, 0, 0) & BST_CHECKED) == BST_CHECKED)))
  383. {
  384. SetWindowLongPtr(hwndDlg, DWLP_MSGRESULT, (LRESULT)TRUE);
  385. }
  386. return TRUE;
  387. }
  388. break;
  389. case WM_COMMAND:
  390. pviewhelp = (PSTORE_SELECT_HELPER) GetWindowLongPtr(hwndDlg, DWLP_USER);
  391. if (pviewhelp == NULL)
  392. {
  393. break;
  394. }
  395. pcss = pviewhelp->pcss;
  396. switch (LOWORD(wParam))
  397. {
  398. case IDC_SHOWPHYSICALSTORES_CHECK:
  399. LRESULT checkState;
  400. if (HIWORD(wParam) != BN_CLICKED)
  401. {
  402. return TRUE;
  403. }
  404. hwndTreeView = GetDlgItem(hwndDlg, IDC_SELECTSTORE_TREE);
  405. memset(&tvi, 0, sizeof(tvi));
  406. tvi.mask = TVIF_PARAM | TVIF_HANDLE;
  407. checkState = SendMessage(GetDlgItem(hwndDlg, IDC_SHOWPHYSICALSTORES_CHECK), BM_GETSTATE, 0, 0);
  408. if ((checkState & BST_CHECKED) == BST_CHECKED)
  409. {
  410. ShowWindow(hwndTreeView, SW_HIDE);
  411. //
  412. // add all the physical stores under each system store
  413. //
  414. pviewhelp->hParentItem = TreeView_GetRoot(hwndTreeView);
  415. while (NULL != pviewhelp->hParentItem)
  416. {
  417. tvi.hItem = pviewhelp->hParentItem;
  418. TreeView_GetItem(hwndTreeView, &tvi);
  419. pOpenStoreStruct = (POPEN_STORE_STRUCT) tvi.lParam;
  420. if (!(pOpenStoreStruct->fStoreHandle))
  421. {
  422. pviewhelp->CurrentPhysEnumIndex = (int) pOpenStoreStruct->EnumerationStructIndex;
  423. CertEnumPhysicalStore(
  424. (LPWSTR) pOpenStoreStruct->pwszStoreName, //pwszSystemStore,
  425. pcss->pStoresForSelection->rgEnumerationStructs[pOpenStoreStruct->EnumerationStructIndex].dwFlags,
  426. pviewhelp,
  427. EnumPhyCallback);
  428. TreeView_Expand(hwndTreeView, pviewhelp->hParentItem, TVE_EXPAND);
  429. TreeView_Expand(hwndTreeView, pviewhelp->hParentItem, TVE_COLLAPSE);
  430. }
  431. pviewhelp->hParentItem = TreeView_GetNextItem(hwndTreeView, pviewhelp->hParentItem, TVGN_NEXT);
  432. }
  433. ShowWindow(hwndTreeView, SW_SHOW);
  434. }
  435. else
  436. {
  437. //
  438. // delete all of the physical stores under each system store
  439. //
  440. pviewhelp->fCollapseMode = TRUE;
  441. hParentItem = TreeView_GetRoot(hwndTreeView);
  442. while (NULL != hParentItem)
  443. {
  444. while (NULL != (hChildItem = TreeView_GetNextItem(hwndTreeView, hParentItem, TVGN_CHILD)))
  445. {
  446. tvi.hItem = hChildItem;
  447. TreeView_GetItem(hwndTreeView, &tvi);
  448. FreeOpenStoreStruct((POPEN_STORE_STRUCT) tvi.lParam);
  449. TreeView_DeleteItem(hwndTreeView, hChildItem);
  450. }
  451. hParentItem = TreeView_GetNextItem(hwndTreeView, hParentItem, TVGN_NEXT);
  452. }
  453. pviewhelp->fCollapseMode = FALSE;
  454. }
  455. break;
  456. case IDOK:
  457. hwndTreeView = GetDlgItem(hwndDlg, IDC_SELECTSTORE_TREE);
  458. hParentItem = TreeView_GetSelection(hwndTreeView);
  459. if (hParentItem != NULL)
  460. {
  461. memset(&tvi, 0, sizeof(tvi));
  462. tvi.mask = TVIF_PARAM | TVIF_HANDLE;
  463. tvi.hItem = hParentItem;
  464. TreeView_GetItem(hwndTreeView, &tvi);
  465. pOpenStoreStruct = (POPEN_STORE_STRUCT) tvi.lParam;
  466. if (pOpenStoreStruct->fStoreHandle)
  467. {
  468. pviewhelp->hSelectedStore = CertDuplicateStore(pOpenStoreStruct->hCertStore);
  469. }
  470. else
  471. {
  472. pviewhelp->hSelectedStore = CertOpenStore(
  473. pOpenStoreStruct->ProviderType,
  474. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
  475. NULL,
  476. pOpenStoreStruct->dwFlags |
  477. pviewhelp->dwExtraOpenStoreFlag |
  478. CERT_STORE_SET_LOCALIZED_NAME_FLAG,
  479. pOpenStoreStruct->pwszStoreName);
  480. //
  481. // check to make sure the store got opened correctly,
  482. // if not, then notify the user
  483. //
  484. if (pviewhelp->hSelectedStore == NULL)
  485. {
  486. LoadStringU(HinstDll, IDS_UNABLE_TO_OPEN_STORE, errorString, ARRAYSIZE(errorString));
  487. if (pcss->szTitle != NULL)
  488. {
  489. MessageBoxU(hwndDlg, errorString, pcss->szTitle, MB_OK | MB_ICONWARNING);
  490. }
  491. else
  492. {
  493. LoadStringU(HinstDll, IDS_SELECT_STORE_TITLE, errorTitle, ARRAYSIZE(errorTitle));
  494. MessageBoxU(hwndDlg, errorString, errorTitle, MB_OK | MB_ICONWARNING);
  495. }
  496. SetWindowLongPtr(hwndDlg, DWLP_MSGRESULT, (LRESULT)TRUE);
  497. return TRUE;
  498. }
  499. }
  500. //
  501. // if a validation callback was passed in then do the validation
  502. //
  503. if ((pcss->pValidateStoreCallback) &&
  504. ((*(pcss->pValidateStoreCallback))(pviewhelp->hSelectedStore, hwndDlg, pcss->pvCallbackData) != TRUE))
  505. {
  506. CertCloseStore(pviewhelp->hSelectedStore, 0);
  507. pviewhelp->hSelectedStore = NULL;
  508. return TRUE;
  509. }
  510. }
  511. else
  512. {
  513. LoadStringU(HinstDll, IDS_SELECT_STORE_ERROR, errorString, ARRAYSIZE(errorString));
  514. if (pcss->szTitle != NULL)
  515. {
  516. MessageBoxU(hwndDlg, errorString, pcss->szTitle, MB_OK | MB_ICONWARNING);
  517. }
  518. else
  519. {
  520. LoadStringU(HinstDll, IDS_SELECT_STORE_TITLE, errorTitle, ARRAYSIZE(errorTitle));
  521. MessageBoxU(hwndDlg, errorString, errorTitle, MB_OK | MB_ICONWARNING);
  522. }
  523. SetWindowLongPtr(hwndDlg, DWLP_MSGRESULT, (LRESULT)TRUE);
  524. return TRUE;
  525. }
  526. EndDialog(hwndDlg, NULL);
  527. break;
  528. case IDCANCEL:
  529. EndDialog(hwndDlg, NULL);
  530. break;
  531. }
  532. break;
  533. case WM_DESTROY:
  534. pviewhelp = (PSTORE_SELECT_HELPER) GetWindowLongPtr(hwndDlg, DWLP_USER);
  535. if (pviewhelp == NULL)
  536. {
  537. break;
  538. }
  539. hwndTreeView = GetDlgItem(hwndDlg, IDC_SELECTSTORE_TREE);
  540. //
  541. // free up all the store open helper structs if we are in enum mode
  542. //
  543. memset(&tvi, 0, sizeof(tvi));
  544. tvi.mask = TVIF_PARAM | TVIF_HANDLE;
  545. while (NULL != (hParentItem = TreeView_GetRoot(hwndTreeView)))
  546. {
  547. while (NULL != (hChildItem = TreeView_GetNextItem(hwndTreeView, hParentItem, TVGN_CHILD)))
  548. {
  549. tvi.hItem = hChildItem;
  550. TreeView_GetItem(hwndTreeView, &tvi);
  551. FreeOpenStoreStruct((POPEN_STORE_STRUCT) tvi.lParam);
  552. TreeView_DeleteItem(hwndTreeView, hChildItem);
  553. }
  554. tvi.hItem = hParentItem;
  555. TreeView_GetItem(hwndTreeView, &tvi);
  556. FreeOpenStoreStruct((POPEN_STORE_STRUCT) tvi.lParam);
  557. TreeView_DeleteItem(hwndTreeView, hParentItem);
  558. }
  559. ImageList_Destroy(TreeView_GetImageList(GetDlgItem(hwndDlg, IDC_SELECTSTORE_TREE), TVSIL_NORMAL));
  560. break;
  561. case WM_HELP:
  562. case WM_CONTEXTMENU:
  563. if (msg == WM_HELP)
  564. {
  565. hwnd = GetDlgItem(hwndDlg, ((LPHELPINFO)lParam)->iCtrlId);
  566. }
  567. else
  568. {
  569. hwnd = (HWND) wParam;
  570. }
  571. if ((hwnd != GetDlgItem(hwndDlg, IDOK)) &&
  572. (hwnd != GetDlgItem(hwndDlg, IDCANCEL)) &&
  573. (hwnd != GetDlgItem(hwndDlg, IDC_SELECTSTORE_TREE)) &&
  574. (hwnd != GetDlgItem(hwndDlg, IDC_SHOWPHYSICALSTORES_CHECK)))
  575. {
  576. SetWindowLongPtr(hwndDlg, DWLP_MSGRESULT, (LRESULT)TRUE);
  577. return TRUE;
  578. }
  579. else
  580. {
  581. return OnContextHelp(hwndDlg, msg, wParam, lParam, helpmap);
  582. }
  583. }
  584. return FALSE;
  585. }
  586. //////////////////////////////////////////////////////////////////////////////////////
  587. //
  588. //////////////////////////////////////////////////////////////////////////////////////
  589. HCERTSTORE
  590. WINAPI
  591. CryptUIDlgSelectStoreW(
  592. PCCRYPTUI_SELECTSTORE_STRUCTW pcss
  593. )
  594. {
  595. STORE_SELECT_HELPER viewhelper;
  596. if (CommonInit() == FALSE)
  597. {
  598. return NULL;
  599. }
  600. if (pcss->dwSize != sizeof(CRYPTUI_SELECTSTORE_STRUCTW)) {
  601. SetLastError(E_INVALIDARG);
  602. return FALSE;
  603. }
  604. viewhelper.pcss = pcss;
  605. viewhelper.hSelectedStore = NULL;
  606. viewhelper.dwExtraOpenStoreFlag = (pcss->dwFlags & CRYPTUI_RETURN_READ_ONLY_STORE) ? CERT_STORE_READONLY_FLAG : 0;
  607. DialogBoxParamU(
  608. HinstDll,
  609. (LPWSTR) MAKEINTRESOURCE(IDD_SELECT_STORE_DIALOG),
  610. (pcss->hwndParent != NULL) ? pcss->hwndParent : GetDesktopWindow(),
  611. SelectStoreDialogProc,
  612. (LPARAM) &viewhelper);
  613. return(viewhelper.hSelectedStore);
  614. }
  615. //////////////////////////////////////////////////////////////////////////////////////
  616. //
  617. //////////////////////////////////////////////////////////////////////////////////////
  618. HCERTSTORE
  619. WINAPI
  620. CryptUIDlgSelectStoreA(
  621. PCCRYPTUI_SELECTSTORE_STRUCTA pcss
  622. )
  623. {
  624. CRYPTUI_SELECTSTORE_STRUCTW cssW;
  625. HCERTSTORE hReturnStore = NULL;
  626. memcpy(&cssW, pcss, sizeof(cssW));
  627. if (pcss->szTitle)
  628. {
  629. cssW.szTitle = CertUIMkWStr(pcss->szTitle);
  630. }
  631. if (pcss->szDisplayString)
  632. {
  633. cssW.szDisplayString = CertUIMkWStr(pcss->szDisplayString);
  634. }
  635. hReturnStore = CryptUIDlgSelectStoreW(&cssW);
  636. if (cssW.szTitle)
  637. {
  638. free((void *) cssW.szTitle);
  639. }
  640. if (cssW.szDisplayString)
  641. {
  642. free((void *) cssW.szDisplayString);
  643. }
  644. return(hReturnStore);
  645. }