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.

423 lines
13 KiB

  1. //+-------------------------------------------------------------------------
  2. // Microsoft Windows
  3. //
  4. // Copyright (C) Microsoft Corporation, 1995 - 1996
  5. //
  6. // File: tprov.cpp
  7. //
  8. // Contents: Get provider information
  9. //
  10. //
  11. // Functions: main
  12. //
  13. // History: 10-Jun-97 philh created
  14. //--------------------------------------------------------------------------
  15. #include <windows.h>
  16. #include <assert.h>
  17. #include "wincrypt.h"
  18. #include "certtest.h"
  19. #include "unicode.h"
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <memory.h>
  24. #include <time.h>
  25. static void DisplayProvContainers(
  26. IN HCRYPTPROV hProv,
  27. IN LPWSTR pwszProvName,
  28. IN DWORD dwProvType,
  29. IN DWORD dwProvFlags
  30. )
  31. {
  32. LPSTR pszContainerName = NULL;
  33. DWORD cchContainerName;
  34. DWORD cchMaxContainerName;
  35. LPWSTR pwszContainerName = NULL;
  36. DWORD dwEnumFlags;
  37. // Get maximum container name length
  38. dwEnumFlags = CRYPT_FIRST;
  39. cchMaxContainerName = 0;
  40. if (!CryptGetProvParam(
  41. hProv,
  42. PP_ENUMCONTAINERS,
  43. NULL, // pbData
  44. &cchMaxContainerName,
  45. dwEnumFlags
  46. )) {
  47. DWORD dwErr = GetLastError();
  48. if (ERROR_MORE_DATA != dwErr) {
  49. if (ERROR_FILE_NOT_FOUND == dwErr || ERROR_NO_MORE_ITEMS == dwErr)
  50. printf(" <No Containers>\n");
  51. else if (ERROR_INVALID_PARAMETER == dwErr)
  52. printf(" <Invalid Parameter>\n");
  53. else
  54. PrintLastError("CryptGetProvParam(PP_ENUMCONTAINERS)");
  55. goto ErrorReturn;
  56. }
  57. }
  58. if (0 == cchMaxContainerName) {
  59. printf(" MaxContainerName length = 0\n");
  60. goto ErrorReturn;
  61. }
  62. if (NULL == (pszContainerName = (LPSTR) TestAlloc(
  63. cchMaxContainerName + 1)))
  64. goto ErrorReturn;
  65. while (TRUE) {
  66. HCRYPTPROV hContainerProv;
  67. cchContainerName = cchMaxContainerName;
  68. if (!CryptGetProvParam(
  69. hProv,
  70. PP_ENUMCONTAINERS,
  71. (BYTE *) pszContainerName,
  72. &cchContainerName,
  73. dwEnumFlags
  74. )) {
  75. DWORD dwEnumErr = GetLastError();
  76. if (!(ERROR_NO_MORE_ITEMS == dwEnumErr ||
  77. ERROR_FILE_NOT_FOUND == dwEnumErr)) {
  78. PrintLastError("CryptGetProvParam(PP_ENUMCONTAINERS)");
  79. }
  80. break;
  81. }
  82. dwEnumFlags = 0; // CRYPT_NEXT
  83. if (NULL == (pwszContainerName = MkWStr(pszContainerName))) {
  84. PrintLastError("MkWStr");
  85. goto ErrorReturn;
  86. }
  87. printf(" %S\n", pwszContainerName);
  88. if (!CryptAcquireContextU(
  89. &hContainerProv,
  90. pwszContainerName,
  91. pwszProvName,
  92. dwProvType,
  93. dwProvFlags
  94. )) {
  95. DWORD dwErr = GetLastError();
  96. if (PROV_RSA_FULL == dwProvType &&
  97. 0 == _wcsicmp(pwszProvName, MS_DEF_PROV_W))
  98. printf(
  99. " CryptAcquireContext(MS_DEF_PROV) returned 0x%x (%d)\n",
  100. dwErr, dwErr);
  101. else if (PROV_DSS == dwProvType &&
  102. 0 == _wcsicmp(pwszProvName, MS_DEF_DSS_PROV_W))
  103. printf(
  104. " CryptAcquireContext(MS_DEF_DSS_PROV) returned 0x%x (%d)\n",
  105. dwErr, dwErr);
  106. else if (PROV_DSS_DH == dwProvType &&
  107. 0 == _wcsicmp(pwszProvName, MS_DEF_DSS_DH_PROV_W))
  108. printf(
  109. " CryptAcquireContext(MS_DEF_DSS_DH_PROV) returned 0x%x (%d)\n",
  110. dwErr, dwErr);
  111. else
  112. PrintLastError("CryptAcquireContextU(Container)");
  113. } else
  114. CryptReleaseContext(hContainerProv, 0);
  115. FreeWStr(pwszContainerName);
  116. pwszContainerName = NULL;
  117. }
  118. ErrorReturn:
  119. TestFree(pszContainerName);
  120. FreeWStr(pwszContainerName);
  121. }
  122. static void DisplayProvAlgidEx(HCRYPTPROV hProv)
  123. {
  124. DWORD i;
  125. CHAR *pszAlgType = NULL;
  126. PROV_ENUMALGS_EX Data;
  127. DWORD cbData;
  128. DWORD dwFlags;
  129. dwFlags = CRYPT_FIRST;
  130. for (i = 0; TRUE; i++) {
  131. cbData = sizeof(Data);
  132. if (!CryptGetProvParam(
  133. hProv,
  134. PP_ENUMALGS_EX,
  135. (BYTE *) &Data,
  136. &cbData,
  137. dwFlags
  138. )) {
  139. DWORD dwErr = GetLastError();
  140. if (ERROR_INVALID_PARAMETER == dwErr)
  141. printf(" <Invalid Parameter>\n");
  142. else if (NTE_BAD_TYPE == dwErr)
  143. printf(" <Bad Parameter Type>\n");
  144. else if (ERROR_NO_MORE_ITEMS != dwErr)
  145. PrintLastError("CryptGetProvParam(PP_ENUMALGS_EX)");
  146. break;
  147. }
  148. dwFlags = 0; // CRYPT_NEXT
  149. // Determine the algorithm type.
  150. switch(GET_ALG_CLASS(Data.aiAlgid)) {
  151. case ALG_CLASS_DATA_ENCRYPT: pszAlgType = "Encrypt ";
  152. break;
  153. case ALG_CLASS_HASH: pszAlgType = "Hash ";
  154. break;
  155. case ALG_CLASS_KEY_EXCHANGE: pszAlgType = "Exchange ";
  156. break;
  157. case ALG_CLASS_SIGNATURE: pszAlgType = "Signature";
  158. break;
  159. default: pszAlgType = "Unknown ";
  160. }
  161. // Print information about the algorithm.
  162. printf("Algid:%8.8xh, Bits:%-4d, %-4d - %-4d, Type:%s\n",
  163. Data.aiAlgid, Data.dwDefaultLen, Data.dwMinLen, Data.dwMaxLen,
  164. pszAlgType
  165. );
  166. printf(" Name: %s LongName: %s Protocols: 0x%x\n",
  167. Data.szName, Data.szLongName, Data.dwProtocols
  168. );
  169. }
  170. printf("\n");
  171. }
  172. static void DisplayProvAlgid(HCRYPTPROV hProv)
  173. {
  174. DWORD i;
  175. CHAR *pszAlgType = NULL;
  176. PROV_ENUMALGS Data;
  177. DWORD cbData;
  178. DWORD dwFlags;
  179. dwFlags = CRYPT_FIRST;
  180. for (i = 0; TRUE; i++) {
  181. cbData = sizeof(Data);
  182. if (!CryptGetProvParam(
  183. hProv,
  184. PP_ENUMALGS,
  185. (BYTE *) &Data,
  186. &cbData,
  187. dwFlags
  188. )) {
  189. if (ERROR_NO_MORE_ITEMS != GetLastError())
  190. PrintLastError("CryptGetProvParam(PP_ENUMALGS)");
  191. break;
  192. }
  193. dwFlags = 0; // CRYPT_NEXT
  194. // Determine the algorithm type.
  195. switch(GET_ALG_CLASS(Data.aiAlgid)) {
  196. case ALG_CLASS_DATA_ENCRYPT: pszAlgType = "Encrypt ";
  197. break;
  198. case ALG_CLASS_HASH: pszAlgType = "Hash ";
  199. break;
  200. case ALG_CLASS_KEY_EXCHANGE: pszAlgType = "Exchange ";
  201. break;
  202. case ALG_CLASS_SIGNATURE: pszAlgType = "Signature";
  203. break;
  204. default: pszAlgType = "Unknown ";
  205. }
  206. // Print information about the algorithm.
  207. printf("Algid:%8.8xh, Bits:%-4d, Type:%s, NameLen:%-2d, Name:%s\n",
  208. Data.aiAlgid, Data.dwBitLen, pszAlgType, Data.dwNameLen,
  209. Data.szName
  210. );
  211. }
  212. printf("\n");
  213. }
  214. #if 0
  215. static void DisplayProvAlgid(HCRYPTPROV hProv)
  216. {
  217. BYTE *ptr = NULL;
  218. DWORD i;
  219. ALG_ID aiAlgid;
  220. DWORD dwBits;
  221. DWORD dwNameLen;
  222. CHAR szName[100]; // Often allocated dynamically
  223. CHAR *pszAlgType = NULL;
  224. BYTE *pbData = NULL;
  225. DWORD cbMaxData;
  226. DWORD cbData;
  227. DWORD dwFlags;
  228. // Get maximum length of provider algorithm parameter data
  229. cbMaxData = 0;
  230. if (!CryptGetProvParam(
  231. hProv,
  232. PP_ENUMALGS,
  233. NULL, // pbData
  234. &cbMaxData,
  235. CRYPT_FIRST // dwFlags
  236. ) || 0 == cbMaxData) {
  237. PrintLastError("CryptGetProvParam(PP_ENUMALGS)");
  238. goto ErrorReturn;
  239. }
  240. if (NULL == (pbData = (BYTE *) TestAlloc(cbMaxData)))
  241. goto ErrorReturn;
  242. dwFlags = CRYPT_FIRST;
  243. for (i = 0; TRUE; i++) {
  244. ALG_ID aiProv;
  245. cbData = cbMaxData;
  246. if (!CryptGetProvParam(
  247. hProv,
  248. PP_ENUMALGS,
  249. pbData,
  250. &cbData,
  251. dwFlags
  252. )) {
  253. if (ERROR_NO_MORE_ITEMS != GetLastError())
  254. PrintLastError("CryptGetProvParam(PP_ENUMALGS)");
  255. break;
  256. }
  257. dwFlags = 0; // CRYPT_NEXT
  258. // Extract algorithm information from the 'pbData' buffer.
  259. ptr = pbData;
  260. aiAlgid = *(ALG_ID *)ptr;
  261. ptr += sizeof(ALG_ID);
  262. dwBits = *(DWORD *)ptr;
  263. ptr += sizeof(DWORD);
  264. dwNameLen = *(DWORD *)ptr;
  265. ptr += sizeof(DWORD);
  266. strncpy(szName, (LPSTR) ptr, dwNameLen);
  267. // Determine the algorithm type.
  268. switch(GET_ALG_CLASS(aiAlgid)) {
  269. case ALG_CLASS_DATA_ENCRYPT: pszAlgType = "Encrypt ";
  270. break;
  271. case ALG_CLASS_HASH: pszAlgType = "Hash ";
  272. break;
  273. case ALG_CLASS_KEY_EXCHANGE: pszAlgType = "Exchange ";
  274. break;
  275. case ALG_CLASS_SIGNATURE: pszAlgType = "Signature";
  276. break;
  277. default: pszAlgType = "Unknown ";
  278. }
  279. // Print information about the algorithm.
  280. printf("Algid:%8.8xh, Bits:%-4d, Type:%s, NameLen:%-2d, Name:%s\n",
  281. aiAlgid, dwBits, pszAlgType, dwNameLen, szName
  282. );
  283. }
  284. ErrorReturn:
  285. TestFree(pbData);
  286. }
  287. #endif
  288. int _cdecl main(int argc, char * argv[])
  289. {
  290. DWORD dwProvIndex;
  291. printf("command line: %s\n", GetCommandLine());
  292. for (dwProvIndex = 0; TRUE; dwProvIndex++) {
  293. BOOL fResult;
  294. LPWSTR pwszProvName;
  295. DWORD cbProvName;
  296. HCRYPTPROV hProv;
  297. DWORD dwProvType;
  298. cbProvName = 0;
  299. dwProvType = 0;
  300. if (!CryptEnumProvidersU(
  301. dwProvIndex,
  302. NULL, // pdwReserved
  303. 0, // dwFlags
  304. &dwProvType,
  305. NULL, // pwszProvName,
  306. &cbProvName
  307. ) || 0 == cbProvName) {
  308. if (ERROR_NO_MORE_ITEMS != GetLastError())
  309. PrintLastError("CryptEnumProvidersU");
  310. break;
  311. }
  312. if (NULL == (pwszProvName = (LPWSTR) TestAlloc(
  313. (cbProvName + 1) * sizeof(WCHAR))))
  314. break;
  315. if (!CryptEnumProvidersU(
  316. dwProvIndex,
  317. NULL, // pdwReserved
  318. 0, // dwFlags
  319. &dwProvType,
  320. pwszProvName,
  321. &cbProvName
  322. )) {
  323. TestFree(pwszProvName);
  324. PrintLastError("CryptEnumProvidersU");
  325. break;
  326. }
  327. printf("\n============ [%d] ============\n", dwProvIndex);
  328. printf("Provider:: Type(%d) %S\n\n", dwProvType, pwszProvName);
  329. fResult = FALSE;
  330. if (!CryptAcquireContextU(
  331. &hProv,
  332. NULL, // pwszContainerName,
  333. pwszProvName,
  334. dwProvType,
  335. CRYPT_VERIFYCONTEXT // dwFlags
  336. )) {
  337. PrintLastError("CryptAcquireContextU");
  338. } else {
  339. DisplayProvAlgidEx(hProv);
  340. DisplayProvAlgid(hProv);
  341. printf("\nCurrentUser Containers::\n");
  342. DisplayProvContainers(hProv, pwszProvName, dwProvType, 0);
  343. CryptReleaseContext(hProv, 0);
  344. }
  345. if (!CryptAcquireContextU(
  346. &hProv,
  347. NULL, // pwszContainerName,
  348. pwszProvName,
  349. dwProvType,
  350. CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET // dwFlags
  351. )) {
  352. DWORD dwErr = GetLastError();
  353. if (NTE_BAD_FLAGS == dwErr) {
  354. printf("CryptAcquireContextU(CRYPT_MACHINE_KEYSET) returned NTE_BAD_FLAGS\n");
  355. }
  356. else {
  357. PrintLastError("CryptAcquireContextU(CRYPT_MACHINE_KEYSET)");
  358. }
  359. } else {
  360. printf("\nLocalMachine Containers::\n");
  361. DisplayProvContainers(hProv, pwszProvName, dwProvType,
  362. CRYPT_MACHINE_KEYSET);
  363. CryptReleaseContext(hProv, 0);
  364. }
  365. TestFree(pwszProvName);
  366. }
  367. printf("Done.\n");
  368. return 0;
  369. }