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.

832 lines
30 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1995 - 1996
  6. //
  7. // File: tsstore.cpp
  8. //
  9. // Contents: System Store Tests: Register, Unregister or Enum
  10. //
  11. // See Usage() for a list of test options.
  12. //
  13. //
  14. // Functions: main
  15. //
  16. // History: 26-Aug-97 philh created
  17. //--------------------------------------------------------------------------
  18. #include <windows.h>
  19. #include <assert.h>
  20. #include "wincrypt.h"
  21. #include "certtest.h"
  22. #include "unicode.h"
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <memory.h>
  27. #include <time.h>
  28. static void Usage(void)
  29. {
  30. printf(
  31. "Usage: tsstore [options] <TestName> [<SystemName> [<PhysicalName>]]\n");
  32. printf("Options are:\n");
  33. printf(" -h - This message\n");
  34. printf(" -e<Expected Error> - For example, -e0x0\n");
  35. printf(" -f<number> - Flags (excludes store location)\n");
  36. printf(" -P<string> - Store Location Parameter\n");
  37. printf(" -R<string> - Current User Relocate\n");
  38. printf(" -RHKCU\\<string> - Current User Relocate\n");
  39. printf(" -RHKLM\\<string> - Local Machine Relocate\n");
  40. printf(" -RNULL - Test NULL HKEY Relocate\n");
  41. printf(" -v - Verbose\n");
  42. printf("\n");
  43. printf("Store Location:\n");
  44. printf(" -lCurrentUser - Store Location: CurrentUser\n");
  45. printf(" -lLocalMachine - Store Location: LocalMachine\n");
  46. printf(" -lCurrentService - Store Location: CurrentService\n");
  47. printf(" -lServices - Store Location: Services\n");
  48. printf(" -lUsers - Store Location: Users\n");
  49. printf(" -lCUGP - Store Location: CU GroupPolicy\n");
  50. printf(" -lLMGP - Store Location: LM GroupPolicy\n");
  51. printf(" -lEnterprise - Store Location: LM Enterprise\n");
  52. printf(" -L<number> - Store Location: ID Number\n");
  53. printf("\n");
  54. printf("RegPhy parameters:\n");
  55. printf(" -pOpenStoreProvider <string> - For example, System\n");
  56. printf(" -pOpenStoreProvider #<number> - For example, #10\n");
  57. printf(" -pOpenParameters <string> - For example, My\n");
  58. printf(" -pOpenEncodingType <number> - For example, 0x00010001\n");
  59. printf(" -pOpenFlags <number> - For example, 0x00010000\n");
  60. printf(" -pFlags <number> - For example, 0x1\n");
  61. printf(" -pPriority <number> - For example, 0\n");
  62. printf("\n");
  63. printf("TestNames (case insensitive):\n");
  64. printf(" Enum - Enum ALL recursively, default\n");
  65. printf(" EnumLoc - Enum store locations\n");
  66. printf(" EnumSys - Enum -l or -L store location\n");
  67. printf(" EnumPhy <SysName> - Enum physical stores\n");
  68. printf(" RegSys <SysName> - Register system store\n");
  69. printf(" UnregSys <SysName> - Unregister, delete system store\n");
  70. printf(" RegPhy <SysName> <PhyName> - Register physical store\n");
  71. printf(" UnregPhy <SysName> <PhyName> - Unregister physical store\n");
  72. printf("\n");
  73. printf("Defaults:\n");
  74. printf(" Enum\n");
  75. printf(" -e0x0\n");
  76. printf(" -f0x0\n");
  77. printf(" -lCurrentUser\n");
  78. printf(" -pOpenStoreProvider System\n");
  79. printf("\n");
  80. }
  81. #define SYSTEM_STORE_PROVIDER_FLAG 0x1
  82. #define ASCII_STORE_PROVIDER_FLAG 0x2
  83. #define UNICODE_STORE_PROVIDER_FLAG 0x4
  84. #define CONST_OID_STR_PREFIX_CHAR '#'
  85. static DWORD GetStoreProviderTypeFlags(
  86. IN LPCSTR pszStoreProvider
  87. )
  88. {
  89. DWORD dwFlags = 0;
  90. if (0xFFFF < (DWORD_PTR) pszStoreProvider &&
  91. CONST_OID_STR_PREFIX_CHAR == pszStoreProvider[0])
  92. // Convert "#<number>" string to its corresponding constant OID value
  93. pszStoreProvider = (LPCSTR)(DWORD_PTR) atol(pszStoreProvider + 1);
  94. dwFlags = UNICODE_STORE_PROVIDER_FLAG;
  95. if (CERT_STORE_PROV_FILENAME_A == pszStoreProvider)
  96. dwFlags = ASCII_STORE_PROVIDER_FLAG;
  97. else if (CERT_STORE_PROV_FILENAME_W == pszStoreProvider)
  98. dwFlags = UNICODE_STORE_PROVIDER_FLAG;
  99. else if (CERT_STORE_PROV_SYSTEM_A == pszStoreProvider)
  100. dwFlags = SYSTEM_STORE_PROVIDER_FLAG | ASCII_STORE_PROVIDER_FLAG;
  101. else if (CERT_STORE_PROV_SYSTEM_W == pszStoreProvider)
  102. dwFlags = SYSTEM_STORE_PROVIDER_FLAG | UNICODE_STORE_PROVIDER_FLAG;
  103. else if (CERT_STORE_PROV_SYSTEM_REGISTRY_A == pszStoreProvider)
  104. dwFlags = SYSTEM_STORE_PROVIDER_FLAG | ASCII_STORE_PROVIDER_FLAG;
  105. else if (CERT_STORE_PROV_SYSTEM_REGISTRY_W == pszStoreProvider)
  106. dwFlags = SYSTEM_STORE_PROVIDER_FLAG | UNICODE_STORE_PROVIDER_FLAG;
  107. else if (CERT_STORE_PROV_PHYSICAL_W == pszStoreProvider)
  108. dwFlags = SYSTEM_STORE_PROVIDER_FLAG | UNICODE_STORE_PROVIDER_FLAG;
  109. else if (0xFFFF < (DWORD_PTR) pszStoreProvider) {
  110. if (0 == _stricmp(sz_CERT_STORE_PROV_FILENAME_W, pszStoreProvider))
  111. dwFlags = UNICODE_STORE_PROVIDER_FLAG;
  112. else if (0 == _stricmp(sz_CERT_STORE_PROV_SYSTEM_W, pszStoreProvider))
  113. dwFlags = SYSTEM_STORE_PROVIDER_FLAG | UNICODE_STORE_PROVIDER_FLAG;
  114. else if (0 == _stricmp(sz_CERT_STORE_PROV_SYSTEM_REGISTRY_W,
  115. pszStoreProvider))
  116. dwFlags = SYSTEM_STORE_PROVIDER_FLAG | UNICODE_STORE_PROVIDER_FLAG;
  117. else if (0 == _stricmp(sz_CERT_STORE_PROV_PHYSICAL_W,
  118. pszStoreProvider))
  119. dwFlags = SYSTEM_STORE_PROVIDER_FLAG | UNICODE_STORE_PROVIDER_FLAG;
  120. }
  121. return dwFlags;
  122. }
  123. static BOOL IsSystemStoreProvider(
  124. IN LPCSTR pszStoreProvider
  125. )
  126. {
  127. return GetStoreProviderTypeFlags(pszStoreProvider) &
  128. SYSTEM_STORE_PROVIDER_FLAG;
  129. }
  130. static BOOL IsAsciiOpenParameters(
  131. IN LPCSTR pszStoreProvider
  132. )
  133. {
  134. return GetStoreProviderTypeFlags(pszStoreProvider) &
  135. ASCII_STORE_PROVIDER_FLAG;
  136. }
  137. static BOOL IsUnicodeOpenParameters(
  138. IN LPCSTR pszStoreProvider
  139. )
  140. {
  141. return GetStoreProviderTypeFlags(pszStoreProvider) &
  142. UNICODE_STORE_PROVIDER_FLAG;
  143. }
  144. static void DisplayOpenFlags(
  145. IN LPCSTR pszHdr,
  146. IN LPCSTR pszStoreProvider,
  147. IN DWORD dwFlags
  148. )
  149. {
  150. printf("%s = 0x%x :: ", pszHdr, dwFlags);
  151. if (IsSystemStoreProvider(pszStoreProvider)) {
  152. DWORD dwLocationID = (dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK) >>
  153. CERT_SYSTEM_STORE_LOCATION_SHIFT;
  154. if (CERT_SYSTEM_STORE_CURRENT_USER_ID == dwLocationID)
  155. printf("CurrentUser");
  156. else if (CERT_SYSTEM_STORE_LOCAL_MACHINE_ID == dwLocationID)
  157. printf("LocalMachine");
  158. else if (CERT_SYSTEM_STORE_CURRENT_SERVICE_ID == dwLocationID)
  159. printf("CurrentService");
  160. else if (CERT_SYSTEM_STORE_SERVICES_ID == dwLocationID)
  161. printf("Services");
  162. else if (CERT_SYSTEM_STORE_USERS_ID == dwLocationID)
  163. printf("Users");
  164. else if (CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY_ID ==
  165. dwLocationID)
  166. printf("CurrentUserGroupPolicy");
  167. else if (CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY_ID ==
  168. dwLocationID)
  169. printf("LocalMachineGroupPolicy");
  170. else if (CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE_ID ==
  171. dwLocationID)
  172. printf("LocalMachineEnterprise");
  173. else
  174. printf("StoreLocation = %d", dwLocationID);
  175. }
  176. if (dwFlags & CERT_STORE_DELETE_FLAG)
  177. printf(", DELETE");
  178. if (dwFlags & CERT_STORE_READONLY_FLAG)
  179. printf(", READONLY");
  180. if (dwFlags & CERT_STORE_BACKUP_RESTORE_FLAG)
  181. printf(", BACKUP_RESTORE");
  182. if (dwFlags & CERT_STORE_OPEN_EXISTING_FLAG)
  183. printf(", OPEN_EXISTING");
  184. if (dwFlags & CERT_STORE_CREATE_NEW_FLAG)
  185. printf(", CREATE_NEW");
  186. if (dwFlags & CERT_STORE_MAXIMUM_ALLOWED_FLAG)
  187. printf(", MAXIMUM_ALLOWED");
  188. if (dwFlags & CERT_STORE_MANIFOLD_FLAG)
  189. printf(", MANIFOLD");
  190. if (dwFlags & CERT_STORE_ENUM_ARCHIVED_FLAG)
  191. printf(", ENUM_ARCHIVED");
  192. if (dwFlags & CERT_STORE_UPDATE_KEYID_FLAG)
  193. printf(", UPDATE_KEYID");
  194. printf("\n");
  195. }
  196. static void DisplayPhysicalStoreInfo(
  197. IN PCERT_PHYSICAL_STORE_INFO pStoreInfo
  198. )
  199. {
  200. DWORD dwFlags;
  201. LPCSTR pszStoreProvider = pStoreInfo->pszOpenStoreProvider;
  202. if (0xFFFF >= (DWORD_PTR) pszStoreProvider)
  203. printf(" OpenStoreProvider: %d", (DWORD)(DWORD_PTR) pszStoreProvider);
  204. else
  205. printf(" OpenStoreProvider: %s", pszStoreProvider);
  206. if (0xFFFF < (DWORD_PTR) pszStoreProvider &&
  207. CONST_OID_STR_PREFIX_CHAR == pszStoreProvider[0])
  208. // Convert "#<number>" string to its corresponding constant OID value
  209. pszStoreProvider = (LPCSTR)(DWORD_PTR) atol(pszStoreProvider + 1);
  210. if (0xFFFF >= (DWORD_PTR) pszStoreProvider) {
  211. if (CERT_STORE_PROV_FILENAME_A == pszStoreProvider)
  212. printf(" (FILENAME_A)");
  213. else if (CERT_STORE_PROV_FILENAME_W == pszStoreProvider)
  214. printf(" (FILENAME_W)");
  215. else if (CERT_STORE_PROV_SYSTEM_A == pszStoreProvider)
  216. printf(" (SYSTEM_A)");
  217. else if (CERT_STORE_PROV_SYSTEM_W == pszStoreProvider)
  218. printf(" (SYSTEM_W)");
  219. else if (CERT_STORE_PROV_SYSTEM_REGISTRY_A == pszStoreProvider)
  220. printf(" (SYSTEM_REGISTRY_A)");
  221. else if (CERT_STORE_PROV_SYSTEM_REGISTRY_W == pszStoreProvider)
  222. printf(" (SYSTEM_REGISTRY_W)");
  223. else if (CERT_STORE_PROV_PHYSICAL_W == pszStoreProvider)
  224. printf(" (PHYSICAL_W)");
  225. }
  226. printf("\n");
  227. printf(" OpenEncodingType: 0x%x\n", pStoreInfo->dwOpenEncodingType);
  228. DisplayOpenFlags(" OpenFlags", pStoreInfo->pszOpenStoreProvider,
  229. pStoreInfo->dwOpenFlags);
  230. if (0 == pStoreInfo->OpenParameters.cbData)
  231. printf(" OpenParameters:: NONE\n");
  232. else if (IsSystemStoreProvider(pStoreInfo->pszOpenStoreProvider)) {
  233. if (IsUnicodeOpenParameters(pStoreInfo->pszOpenStoreProvider))
  234. printf(" OpenParameters: %S\n",
  235. pStoreInfo->OpenParameters.pbData);
  236. else
  237. printf(" OpenParameters: %s\n",
  238. pStoreInfo->OpenParameters.pbData);
  239. } else {
  240. printf(" OpenParameters::\n");
  241. PrintBytes(" ",
  242. pStoreInfo->OpenParameters.pbData,
  243. pStoreInfo->OpenParameters.cbData
  244. );
  245. }
  246. dwFlags = pStoreInfo->dwFlags;
  247. printf(" Flags: 0x%x", dwFlags);
  248. if (dwFlags)
  249. printf(" ::");
  250. if (dwFlags & CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG)
  251. printf(" ADD_ENABLE");
  252. if (dwFlags & CERT_PHYSICAL_STORE_OPEN_DISABLE_FLAG)
  253. printf(" OPEN_DISABLE");
  254. if (dwFlags & CERT_PHYSICAL_STORE_REMOTE_OPEN_DISABLE_FLAG)
  255. printf(" REMOTE_OPEN_DISABLE_FLAG");
  256. if (dwFlags & CERT_PHYSICAL_STORE_INSERT_COMPUTER_NAME_ENABLE_FLAG)
  257. printf(" INSERT_COMPUTER_NAME_ENABLE_FLAG");
  258. printf("\n");
  259. printf(" Priority: %d\n", pStoreInfo->dwPriority);
  260. }
  261. typedef struct _ENUM_ARG {
  262. BOOL fAll;
  263. BOOL fVerbose;
  264. DWORD dwFlags;
  265. const void *pvStoreLocationPara;
  266. HKEY hKeyBase;
  267. } ENUM_ARG, *PENUM_ARG;
  268. static BOOL GetSystemName(
  269. IN const void *pvSystemStore,
  270. IN DWORD dwFlags,
  271. IN PENUM_ARG pEnumArg,
  272. OUT LPCWSTR *ppwszSystemName
  273. )
  274. {
  275. *ppwszSystemName = NULL;
  276. if (pEnumArg->hKeyBase &&
  277. 0 == (dwFlags & CERT_SYSTEM_STORE_RELOCATE_FLAG)) {
  278. printf("failed => RELOCATE_FLAG not set in callback\n");
  279. return FALSE;
  280. } else if (dwFlags & CERT_SYSTEM_STORE_RELOCATE_FLAG) {
  281. PCERT_SYSTEM_STORE_RELOCATE_PARA pRelocatePara;
  282. if (NULL == pEnumArg->hKeyBase) {
  283. printf("failed => RELOCATE_FLAG is set in callback\n");
  284. return FALSE;
  285. }
  286. pRelocatePara = (PCERT_SYSTEM_STORE_RELOCATE_PARA) pvSystemStore;
  287. if (pRelocatePara->hKeyBase != pEnumArg->hKeyBase) {
  288. printf("failed => wrong hKeyBase passed to callback\n");
  289. return FALSE;
  290. }
  291. *ppwszSystemName = pRelocatePara->pwszSystemStore;
  292. } else
  293. *ppwszSystemName = (LPCWSTR) pvSystemStore;
  294. return TRUE;
  295. }
  296. static BOOL WINAPI EnumPhyCallback(
  297. IN const void *pvSystemStore,
  298. IN DWORD dwFlags,
  299. IN LPCWSTR pwszStoreName,
  300. IN PCERT_PHYSICAL_STORE_INFO pStoreInfo,
  301. IN OPTIONAL void *pvReserved,
  302. IN OPTIONAL void *pvArg
  303. )
  304. {
  305. PENUM_ARG pEnumArg = (PENUM_ARG) pvArg;
  306. LPCWSTR pwszSystemStore;
  307. if (!GetSystemName(pvSystemStore, dwFlags, pEnumArg, &pwszSystemStore))
  308. return FALSE;
  309. printf(" %S", pwszStoreName);
  310. if (pEnumArg->fVerbose &&
  311. (dwFlags & CERT_PHYSICAL_STORE_PREDEFINED_ENUM_FLAG))
  312. printf(" (implicitly created)");
  313. printf("\n");
  314. if (pEnumArg->fVerbose)
  315. DisplayPhysicalStoreInfo(pStoreInfo);
  316. return TRUE;
  317. }
  318. static BOOL WINAPI EnumSysCallback(
  319. IN const void *pvSystemStore,
  320. IN DWORD dwFlags,
  321. IN PCERT_SYSTEM_STORE_INFO pStoreInfo,
  322. IN OPTIONAL void *pvReserved,
  323. IN OPTIONAL void *pvArg
  324. )
  325. {
  326. PENUM_ARG pEnumArg = (PENUM_ARG) pvArg;
  327. LPCWSTR pwszSystemStore;
  328. if (!GetSystemName(pvSystemStore, dwFlags, pEnumArg, &pwszSystemStore))
  329. return FALSE;
  330. printf(" %S\n", pwszSystemStore);
  331. if (pEnumArg->fAll || pEnumArg->fVerbose) {
  332. dwFlags &= CERT_SYSTEM_STORE_MASK;
  333. dwFlags |= pEnumArg->dwFlags & ~CERT_SYSTEM_STORE_MASK;
  334. if (!CertEnumPhysicalStore(
  335. pvSystemStore,
  336. dwFlags,
  337. pEnumArg,
  338. EnumPhyCallback
  339. )) {
  340. DWORD dwErr = GetLastError();
  341. if (!(ERROR_FILE_NOT_FOUND == dwErr ||
  342. ERROR_NOT_SUPPORTED == dwErr))
  343. PrintLastError(" CertEnumPhysicalStore");
  344. }
  345. }
  346. return TRUE;
  347. }
  348. static BOOL WINAPI EnumLocCallback(
  349. IN LPCWSTR pwszStoreLocation,
  350. IN DWORD dwFlags,
  351. IN OPTIONAL void *pvReserved,
  352. IN OPTIONAL void *pvArg
  353. )
  354. {
  355. PENUM_ARG pEnumArg = (PENUM_ARG) pvArg;
  356. DWORD dwLocationID = (dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK) >>
  357. CERT_SYSTEM_STORE_LOCATION_SHIFT;
  358. printf("====== %S ======\n", pwszStoreLocation);
  359. if (pEnumArg->fAll) {
  360. dwFlags &= CERT_SYSTEM_STORE_MASK;
  361. dwFlags |= pEnumArg->dwFlags & ~CERT_SYSTEM_STORE_LOCATION_MASK;
  362. if (!CertEnumSystemStore(
  363. dwFlags,
  364. (void *) pEnumArg->pvStoreLocationPara,
  365. pEnumArg,
  366. EnumSysCallback
  367. )) {
  368. DWORD dwErr = GetLastError();
  369. if (E_INVALIDARG == dwErr && pEnumArg->pvStoreLocationPara)
  370. // \\ComputerName, ServiceName, or \\ComputerName\Service
  371. // not supported for all store locations
  372. ;
  373. else if (!(ERROR_FILE_NOT_FOUND == dwErr ||
  374. ERROR_PROC_NOT_FOUND == dwErr))
  375. PrintLastError(" CertEnumSystemStore");
  376. }
  377. }
  378. return TRUE;
  379. }
  380. int _cdecl main(int argc, char * argv[])
  381. {
  382. BOOL fResult;
  383. int status = 0;
  384. DWORD dwError;
  385. BOOL fVerbose = FALSE;
  386. DWORD dwExpectedError = 0;
  387. DWORD dwLocationID = CERT_SYSTEM_STORE_CURRENT_USER_ID;
  388. DWORD dwFlags = 0;
  389. CERT_PHYSICAL_STORE_INFO PhyStoreInfo;
  390. memset(&PhyStoreInfo, 0, sizeof(PhyStoreInfo));
  391. PhyStoreInfo.cbSize = sizeof(PhyStoreInfo);
  392. PhyStoreInfo.pszOpenStoreProvider = sz_CERT_STORE_PROV_SYSTEM_W;
  393. ENUM_ARG EnumArg;
  394. LPSTR pszStoreParameters = NULL; // not allocated
  395. LPSTR pszStoreLocationPara = NULL; // not allocated
  396. LPWSTR pwszStoreParameters = NULL;
  397. LPWSTR pwszSystemName = NULL;
  398. LPWSTR pwszPhysicalName = NULL;
  399. LPWSTR pwszStoreLocationPara = NULL;
  400. void *pvSystemName; // not allocated
  401. void *pvStoreLocationPara; // not allocated
  402. #define TEST_NAME_INDEX 0
  403. #define SYS_NAME_INDEX 1
  404. #define PHY_NAME_INDEX 2
  405. #define MAX_NAME_CNT 3
  406. DWORD dwNameCnt = 0;
  407. LPCSTR rgpszName[MAX_NAME_CNT];
  408. LPCSTR pszTestName;
  409. BOOL fRelocate = FALSE;
  410. HKEY hKeyRelocate = HKEY_CURRENT_USER;
  411. LPSTR pszRelocate = NULL; // not allocated
  412. CERT_SYSTEM_STORE_RELOCATE_PARA SystemNameRelocatePara;
  413. CERT_SYSTEM_STORE_RELOCATE_PARA StoreLocationRelocatePara;
  414. HKEY hKeyBase = NULL;
  415. while (--argc>0) {
  416. if (**++argv == '-')
  417. {
  418. switch(argv[0][1])
  419. {
  420. case 'l':
  421. if (argv[0][2]) {
  422. if (0 == _stricmp(argv[0]+2, "CurrentUser"))
  423. dwLocationID = CERT_SYSTEM_STORE_CURRENT_USER_ID;
  424. else if (0 == _stricmp(argv[0]+2, "LocalMachine"))
  425. dwLocationID = CERT_SYSTEM_STORE_LOCAL_MACHINE_ID;
  426. else if (0 == _stricmp(argv[0]+2, "CurrentService"))
  427. dwLocationID = CERT_SYSTEM_STORE_CURRENT_SERVICE_ID;
  428. else if (0 == _stricmp(argv[0]+2, "Services"))
  429. dwLocationID = CERT_SYSTEM_STORE_SERVICES_ID;
  430. else if (0 == _stricmp(argv[0]+2, "Users"))
  431. dwLocationID = CERT_SYSTEM_STORE_USERS_ID;
  432. else if (0 == _stricmp(argv[0]+2, "CUGP"))
  433. dwLocationID =
  434. CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY_ID;
  435. else if (0 == _stricmp(argv[0]+2, "LMGP"))
  436. dwLocationID =
  437. CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY_ID;
  438. else if (0 == _stricmp(argv[0]+2, "Enterprise"))
  439. dwLocationID =
  440. CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE_ID;
  441. else {
  442. printf("Need to specify -l<StoreLocation>\n");
  443. goto BadUsage;
  444. }
  445. } else {
  446. printf("Need to specify -l<StoreLocation>\n");
  447. goto BadUsage;
  448. }
  449. break;
  450. case 'L':
  451. dwLocationID = strtoul(argv[0]+2, NULL, 0);
  452. break;
  453. case 'P':
  454. pszStoreLocationPara = argv[0]+2;
  455. break;
  456. case 'R':
  457. if (argv[0][2]) {
  458. if (0 == _stricmp(argv[0]+2, "NULL")) {
  459. hKeyRelocate = NULL;
  460. pszRelocate = NULL;
  461. } else if (0 == _strnicmp(argv[0]+2, "HKCU", 4)) {
  462. hKeyRelocate = HKEY_CURRENT_USER;
  463. pszRelocate = argv[0]+2+4;
  464. if ('\\' == *pszRelocate)
  465. pszRelocate++;
  466. } else if (0 == _strnicmp(argv[0]+2, "HKLM", 4)) {
  467. hKeyRelocate = HKEY_LOCAL_MACHINE;
  468. pszRelocate = argv[0]+2+4;
  469. if ('\\' == *pszRelocate)
  470. pszRelocate++;
  471. } else {
  472. hKeyRelocate = HKEY_CURRENT_USER;
  473. pszRelocate = argv[0]+2;
  474. }
  475. } else {
  476. hKeyRelocate = HKEY_CURRENT_USER;
  477. pszRelocate = NULL;
  478. }
  479. fRelocate = TRUE;
  480. break;
  481. case 'f':
  482. dwFlags = strtoul(argv[0]+2, NULL, 0);
  483. break;
  484. case 'p':
  485. if (argc < 2 || argv[1][0] == '-') {
  486. printf("Option (%s) : missing parameter argument\n",
  487. argv[0]);
  488. goto BadUsage;
  489. }
  490. if (argv[0][2]) {
  491. if (0 == _stricmp(argv[0]+2, "OpenStoreProvider")) {
  492. if (CONST_OID_STR_PREFIX_CHAR == argv[1][0])
  493. // Convert "#<number>" string to its
  494. // corresponding constant OID value
  495. PhyStoreInfo.pszOpenStoreProvider =
  496. (LPSTR)(DWORD_PTR) atol(argv[1] + 1);
  497. else
  498. PhyStoreInfo.pszOpenStoreProvider = argv[1];
  499. } else if (0 == _stricmp(argv[0]+2, "OpenEncodingType"))
  500. PhyStoreInfo.dwOpenEncodingType =
  501. strtoul(argv[1], NULL, 0);
  502. else if (0 == _stricmp(argv[0]+2, "OpenFlags"))
  503. PhyStoreInfo.dwOpenFlags = strtoul(argv[1], NULL, 0);
  504. else if (0 == _stricmp(argv[0]+2, "OpenParameters"))
  505. pszStoreParameters = argv[1];
  506. else if (0 == _stricmp(argv[0]+2, "Flags"))
  507. PhyStoreInfo.dwFlags = strtoul(argv[1], NULL, 0);
  508. else if (0 == _stricmp(argv[0]+2, "Priority"))
  509. PhyStoreInfo.dwPriority = strtoul(argv[1], NULL, 0);
  510. else {
  511. printf("Invalid -p<ParameterName>\n");
  512. goto BadUsage;
  513. }
  514. } else {
  515. printf("Need to specify -p<ParameterName>\n");
  516. goto BadUsage;
  517. }
  518. argc -= 1;
  519. argv += 1;
  520. break;
  521. case 'v':
  522. fVerbose = TRUE;
  523. break;
  524. case 'e':
  525. dwExpectedError = strtoul(argv[0]+2, NULL, 0);
  526. break;
  527. case 'h':
  528. default:
  529. goto BadUsage;
  530. }
  531. } else {
  532. if (MAX_NAME_CNT <= dwNameCnt) {
  533. printf("Too many names starting with:: %s\n", argv[0]);
  534. goto BadUsage;
  535. }
  536. rgpszName[dwNameCnt++] = argv[0];
  537. }
  538. }
  539. printf("command line: %s\n", GetCommandLine());
  540. if (pszStoreLocationPara)
  541. pwszStoreLocationPara = AllocAndSzToWsz(pszStoreLocationPara);
  542. if (0 == dwNameCnt)
  543. rgpszName[dwNameCnt++] = "Enum";
  544. pszTestName = rgpszName[TEST_NAME_INDEX];
  545. if (SYS_NAME_INDEX < dwNameCnt)
  546. pwszSystemName = AllocAndSzToWsz(rgpszName[SYS_NAME_INDEX]);
  547. if (PHY_NAME_INDEX < dwNameCnt)
  548. pwszPhysicalName = AllocAndSzToWsz(rgpszName[PHY_NAME_INDEX]);
  549. dwFlags &= ~CERT_SYSTEM_STORE_LOCATION_MASK;
  550. dwFlags |= (dwLocationID << CERT_SYSTEM_STORE_LOCATION_SHIFT) &
  551. CERT_SYSTEM_STORE_LOCATION_MASK;
  552. DisplayOpenFlags("Flags", sz_CERT_STORE_PROV_SYSTEM_W, dwFlags);
  553. if (fRelocate) {
  554. printf("Relocation Enabled: ");
  555. if (hKeyRelocate) {
  556. LONG err;
  557. if (HKEY_CURRENT_USER == hKeyRelocate)
  558. printf("HKEY_CURRENT_USER\\%s\n", pszRelocate);
  559. else if (HKEY_LOCAL_MACHINE == hKeyRelocate)
  560. printf("HKEY_LOCAL_MACHINE\\%s\n", pszRelocate);
  561. else
  562. printf("???\\%s\n", pszRelocate);
  563. if (ERROR_SUCCESS != (err = RegOpenKeyExA(
  564. hKeyRelocate,
  565. pszRelocate,
  566. 0, // dwReserved
  567. KEY_ALL_ACCESS,
  568. &hKeyBase))) {
  569. printf("RegOpenKeyExA(%s) failed => %d 0x%x\n",
  570. pszRelocate, err, err);
  571. goto ErrorReturn;
  572. }
  573. } else
  574. printf("NULL hKeyBase\n");
  575. dwFlags |= CERT_SYSTEM_STORE_RELOCATE_FLAG;
  576. SystemNameRelocatePara.hKeyBase = hKeyBase;
  577. SystemNameRelocatePara.pwszSystemStore = pwszSystemName;
  578. pvSystemName = &SystemNameRelocatePara;
  579. StoreLocationRelocatePara.hKeyBase = hKeyBase;
  580. StoreLocationRelocatePara.pwszSystemStore = pwszStoreLocationPara;
  581. pvStoreLocationPara = &StoreLocationRelocatePara;
  582. } else {
  583. pvSystemName = pwszSystemName;
  584. pvStoreLocationPara = pwszStoreLocationPara;
  585. }
  586. memset(&EnumArg, 0, sizeof(EnumArg));
  587. EnumArg.fVerbose = fVerbose;
  588. EnumArg.dwFlags = dwFlags;
  589. EnumArg.hKeyBase = hKeyBase;
  590. if (pwszStoreLocationPara) {
  591. printf("System Store Location Parameter :: %S\n",
  592. pwszStoreLocationPara);
  593. EnumArg.pvStoreLocationPara = pvStoreLocationPara;
  594. } else if (fRelocate)
  595. EnumArg.pvStoreLocationPara = pvStoreLocationPara;
  596. {
  597. WCHAR wszCurrentComputer[_MAX_PATH + 1];
  598. DWORD cch = _MAX_PATH;
  599. if (!GetComputerNameU(wszCurrentComputer, &cch))
  600. PrintLastError("GetComputeName");
  601. else
  602. printf("CurrentComputer :: %S\n", wszCurrentComputer);
  603. }
  604. printf("\n");
  605. if (0 == _stricmp("Enum", pszTestName)) {
  606. printf("Enumeration of ALL System Stores\n\n");
  607. EnumArg.fAll = TRUE;
  608. fResult = CertEnumSystemStoreLocation(
  609. dwFlags,
  610. &EnumArg,
  611. EnumLocCallback
  612. );
  613. } else if (0 == _stricmp("EnumLoc", pszTestName)) {
  614. printf("Enumeration of System Store Locations\n\n");
  615. fResult = CertEnumSystemStoreLocation(
  616. dwFlags,
  617. &EnumArg,
  618. EnumLocCallback
  619. );
  620. } else if (0 == _stricmp("EnumSys", pszTestName)) {
  621. printf("Enumeration of System Stores\n\n");
  622. fResult = CertEnumSystemStore(
  623. dwFlags,
  624. pvStoreLocationPara,
  625. &EnumArg,
  626. EnumSysCallback
  627. );
  628. } else if (0 == _stricmp("EnumPhy", pszTestName)) {
  629. if (NULL == pwszSystemName) {
  630. printf("Missing <SystemName>\n");
  631. goto BadUsage;
  632. }
  633. printf("Enumeration of Physical Stores for System Store %S\n",
  634. pwszSystemName);
  635. fResult = CertEnumPhysicalStore(
  636. pvSystemName,
  637. dwFlags,
  638. &EnumArg,
  639. EnumPhyCallback
  640. );
  641. } else if (0 == _stricmp("RegSys", pszTestName)) {
  642. if (NULL == pwszSystemName) {
  643. printf("Missing <SystemName>\n");
  644. goto BadUsage;
  645. }
  646. printf("Registering System Store %S\n", pwszSystemName);
  647. fResult = CertRegisterSystemStore(
  648. pvSystemName,
  649. dwFlags,
  650. NULL, // pSystemStoreInfo
  651. NULL // pvReserved
  652. );
  653. } else if (0 == _stricmp("UnregSys", pszTestName)) {
  654. if (NULL == pwszSystemName) {
  655. printf("Missing <SystemName>\n");
  656. goto BadUsage;
  657. }
  658. printf("Unregistering System Store %S\n", pwszSystemName);
  659. fResult = CertUnregisterSystemStore(
  660. pvSystemName,
  661. dwFlags
  662. );
  663. if (!fResult && 0 == dwExpectedError) {
  664. if (ERROR_FILE_NOT_FOUND == GetLastError()) {
  665. if (fVerbose)
  666. printf("System store doesn't exist\n");
  667. fResult = TRUE;
  668. }
  669. }
  670. } else if (0 == _stricmp("RegPhy", pszTestName)) {
  671. if (NULL == pwszSystemName) {
  672. printf("Missing <SystemName>\n");
  673. goto BadUsage;
  674. }
  675. if (NULL == pwszPhysicalName) {
  676. printf("Missing <PhysicalName>\n");
  677. goto BadUsage;
  678. }
  679. printf("Registering Physical Store (%S) in System Store (%S)\n",
  680. pwszPhysicalName, pwszSystemName);
  681. if (pszStoreParameters) {
  682. if (IsUnicodeOpenParameters(PhyStoreInfo.pszOpenStoreProvider)) {
  683. if (pwszStoreParameters = AllocAndSzToWsz(
  684. pszStoreParameters)) {
  685. PhyStoreInfo.OpenParameters.pbData =
  686. (BYTE *) pwszStoreParameters;
  687. PhyStoreInfo.OpenParameters.cbData =
  688. (wcslen(pwszStoreParameters) + 1) * sizeof(WCHAR);
  689. }
  690. } else {
  691. PhyStoreInfo.OpenParameters.pbData =
  692. (BYTE *) pszStoreParameters;
  693. PhyStoreInfo.OpenParameters.cbData =
  694. strlen(pszStoreParameters) + 1;
  695. }
  696. }
  697. printf("Physical Store Info::\n");
  698. DisplayPhysicalStoreInfo(&PhyStoreInfo);
  699. fResult = CertRegisterPhysicalStore(
  700. pvSystemName,
  701. dwFlags,
  702. pwszPhysicalName,
  703. &PhyStoreInfo,
  704. NULL // pvReserved
  705. );
  706. } else if (0 == _stricmp("UnregPhy", pszTestName)) {
  707. if (NULL == pwszSystemName) {
  708. printf("Missing <SystemName>\n");
  709. goto BadUsage;
  710. }
  711. if (NULL == pwszPhysicalName) {
  712. printf("Missing <PhysicalName>\n");
  713. goto BadUsage;
  714. }
  715. printf("Unregistering Physical Store (%S) in System Store (%S)\n",
  716. pwszPhysicalName, pwszSystemName);
  717. fResult = CertUnregisterPhysicalStore(
  718. pvSystemName,
  719. dwFlags,
  720. pwszPhysicalName
  721. );
  722. } else {
  723. printf("Invalid TestName: %s\n", pszTestName);
  724. goto BadUsage;
  725. }
  726. printf("\n");
  727. if (fResult) {
  728. dwError = 0;
  729. printf("Successful %s\n", pszTestName);
  730. } else
  731. dwError = GetLastError();
  732. if (dwError != dwExpectedError) {
  733. if (!fResult)
  734. PrintLastError(pszTestName);
  735. status = -1;
  736. printf("Failed. Expected error => 0x%x (%d) \n",
  737. dwExpectedError, dwExpectedError);
  738. } else
  739. status = 0;
  740. CommonReturn:
  741. if (hKeyBase)
  742. RegCloseKey(hKeyBase);
  743. TestFree(pwszStoreParameters);
  744. TestFree(pwszSystemName);
  745. TestFree(pwszPhysicalName);
  746. TestFree(pwszStoreLocationPara);
  747. return status;
  748. ErrorReturn:
  749. status = -1;
  750. goto CommonReturn;
  751. BadUsage:
  752. Usage();
  753. status = -1;
  754. goto CommonReturn;
  755. }