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.

297 lines
8.7 KiB

  1. //+-------------------------------------------------------------------------
  2. // Microsoft Windows
  3. //
  4. // Copyright (C) Microsoft Corporation, 1995 - 1996
  5. //
  6. // File: tpvkload.cpp
  7. //
  8. // Contents: Private Key Load Test
  9. //
  10. // See Usage() for list of load options.
  11. //
  12. // Functions: main
  13. //
  14. // History: 11-May-96 philh created
  15. // 31-May-96 helles Removed check for a particular error code,
  16. // NTE_BAD_KEYSET, since this can get
  17. // overwritten due to known problem with
  18. // the msvcr40d.dll on Win95.
  19. // 07-Jun-96 HelleS Added printing the command line
  20. // and Failed or Passed at the end.
  21. //
  22. //--------------------------------------------------------------------------
  23. #include <windows.h>
  24. #include <assert.h>
  25. #include "wincrypt.h"
  26. #include "pvkhlpr.h"
  27. #include "certtest.h"
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <memory.h>
  32. #include <time.h>
  33. static struct
  34. {
  35. LPCSTR pszName;
  36. LPCWSTR pwszKeyTitle;
  37. DWORD dwKeySpec;
  38. } KeyTypes[] = {
  39. "Sign", L"Signature", AT_SIGNATURE,
  40. "Xchg", L"Exchange", AT_KEYEXCHANGE
  41. };
  42. #define NKEYTYPES (sizeof(KeyTypes)/sizeof(KeyTypes[0]))
  43. static void Usage(void)
  44. {
  45. int i;
  46. printf("Usage: tpvkload [options] <Filename> <KeyType>\n");
  47. printf("Options are:\n");
  48. printf(" -p<number> - Crypto provider type number\n");
  49. printf(" -c<name> - Crypto key container name\n");
  50. printf(" -F - Force load if keys already exist\n");
  51. printf(" -E - Exportable private keys\n");
  52. printf(" -m - test memory version of API\n");
  53. printf(" -h - This message\n");
  54. printf("\n");
  55. printf("KeyType (case insensitive):\n");
  56. for (i = 0; i < NKEYTYPES; i++)
  57. printf(" %s\n", KeyTypes[i].pszName);
  58. printf("\n");
  59. }
  60. int _cdecl main(int argc, char * argv[])
  61. {
  62. int ReturnStatus;
  63. HCRYPTPROV hProv = 0;
  64. HANDLE hFile = INVALID_HANDLE_VALUE;
  65. DWORD dwProvType = PROV_RSA_FULL;
  66. BOOL fMem = FALSE;
  67. BOOL fForce = FALSE;
  68. BOOL fExportable = FALSE;
  69. BYTE *pbKey = NULL;
  70. LPSTR pszContainer = NULL;
  71. LPSTR pszFilename = NULL;
  72. LPSTR pszKeyType = NULL;
  73. int KeyIdx = 0;
  74. DWORD dwKeySpec;
  75. while (--argc>0)
  76. {
  77. if (**++argv == '-')
  78. {
  79. switch(argv[0][1])
  80. {
  81. case 'F':
  82. fForce = TRUE;
  83. break;
  84. case 'E':
  85. fExportable = TRUE;
  86. break;
  87. case 'm':
  88. fMem = TRUE;
  89. break;
  90. case 'p':
  91. dwProvType = strtoul( argv[0]+2, NULL, 10);
  92. break;
  93. case 'c':
  94. pszContainer = argv[0]+2;
  95. if (*pszContainer == '\0') {
  96. printf("Need to specify crypto key container name\n");
  97. goto BadUsage;
  98. }
  99. break;
  100. case 'h':
  101. default:
  102. goto BadUsage;
  103. }
  104. } else {
  105. if (pszFilename == NULL)
  106. pszFilename = argv[0];
  107. else if(pszKeyType == NULL)
  108. pszKeyType = argv[0];
  109. else {
  110. printf("Too many arguments\n");
  111. goto BadUsage;
  112. }
  113. }
  114. }
  115. if (pszFilename == NULL) {
  116. printf("missing Filename\n");
  117. goto BadUsage;
  118. }
  119. printf("command line: %s\n", GetCommandLine());
  120. if (pszKeyType) {
  121. for (KeyIdx = 0; KeyIdx < NKEYTYPES; KeyIdx++) {
  122. if (_stricmp(pszKeyType, KeyTypes[KeyIdx].pszName) == 0)
  123. break;
  124. }
  125. if (KeyIdx >= NKEYTYPES) {
  126. printf("Bad KeyType: %s\n", pszKeyType);
  127. goto BadUsage;
  128. }
  129. } else {
  130. printf("missing KeyType\n");
  131. goto BadUsage;
  132. }
  133. hFile = CreateFileA(
  134. pszFilename,
  135. GENERIC_READ,
  136. FILE_SHARE_READ,
  137. NULL, // lpsa
  138. OPEN_EXISTING,
  139. FILE_ATTRIBUTE_NORMAL,
  140. NULL // hTemplateFile
  141. );
  142. if (hFile == INVALID_HANDLE_VALUE) {
  143. printf( "can't open %s\n", pszFilename);
  144. goto ErrorReturn;
  145. }
  146. if (!CryptAcquireContext(
  147. &hProv,
  148. pszContainer,
  149. NULL, // pszProvider
  150. dwProvType,
  151. 0 // dwFlags
  152. )) {
  153. // Removed check for a particular error code,
  154. // NTE_BAD_KEYSET, since this can get overwritten due to known problem
  155. // with the msvcr40d.dll on Win95.
  156. // if (GetLastError() != NTE_BAD_KEYSET) {
  157. // PrintLastError("CryptAcquireContext");
  158. // goto ErrorReturn;
  159. // }
  160. hProv = 0;
  161. if (!CryptAcquireContext(
  162. &hProv,
  163. pszContainer,
  164. NULL, // pszProvider
  165. dwProvType,
  166. CRYPT_NEWKEYSET
  167. ) || hProv == 0) {
  168. PrintLastError("CryptAcquireContext(CRYPT_NEWKEYSET)");
  169. goto ErrorReturn;
  170. }
  171. } else {
  172. HCRYPTKEY hKey = 0;
  173. if (!CryptGetUserKey(hProv, KeyTypes[KeyIdx].dwKeySpec, &hKey)) {
  174. if (GetLastError() != NTE_NO_KEY) {
  175. PrintLastError("CryptGetUserKey");
  176. goto ErrorReturn;
  177. }
  178. } else {
  179. CryptDestroyKey(hKey);
  180. if (!fForce) {
  181. printf("Private key already exists, use -F to delete private keys\n");
  182. goto ErrorReturn;
  183. }
  184. // Delete the existing keys
  185. CryptReleaseContext(hProv, 0);
  186. printf("Deleting existing private keys\n");
  187. // Note: for CRYPT_DELETEKEYSET, the returned hProv is undefined
  188. // and must not be released.
  189. if (!CryptAcquireContext(
  190. &hProv,
  191. pszContainer,
  192. NULL, // pszProvider
  193. dwProvType,
  194. CRYPT_DELETEKEYSET
  195. ))
  196. PrintLastError("CryptAcquireContext(CRYPT_DELETEKEYSET)");
  197. // Create new keyset
  198. hProv = 0;
  199. if (!CryptAcquireContext(
  200. &hProv,
  201. pszContainer,
  202. NULL, // pszProvider
  203. dwProvType,
  204. CRYPT_NEWKEYSET
  205. ) || hProv == 0) {
  206. PrintLastError("CryptAcquireContext(CRYPT_NEWKEYSET)");
  207. goto ErrorReturn;
  208. }
  209. }
  210. }
  211. dwKeySpec = KeyTypes[KeyIdx].dwKeySpec;
  212. if (fMem) {
  213. DWORD cbKey;
  214. DWORD cbRead;
  215. cbKey = GetFileSize(hFile, NULL);
  216. if (cbKey == 0) {
  217. printf( "empty file %s\n", pszFilename);
  218. goto ErrorReturn;
  219. }
  220. if (NULL == (pbKey = (PBYTE)TestAlloc(cbKey)))
  221. goto ErrorReturn;
  222. if (!ReadFile(hFile, pbKey, cbKey, &cbRead, NULL) ||
  223. (cbRead != cbKey)) {
  224. printf( "can't read %s\n", pszFilename);
  225. goto ErrorReturn;
  226. }
  227. if (!PvkPrivateKeyLoadFromMemory(
  228. hProv,
  229. pbKey,
  230. cbKey,
  231. NULL, // hwndOwner
  232. KeyTypes[KeyIdx].pwszKeyTitle,
  233. fExportable ? CRYPT_EXPORTABLE : 0, // dwFlags
  234. &dwKeySpec
  235. )) {
  236. PrintLastError("PrivateKeyLoadFromMemory");
  237. goto ErrorReturn;
  238. }
  239. } else {
  240. if (!PvkPrivateKeyLoad(
  241. hProv,
  242. hFile,
  243. NULL, // hwndOwner
  244. KeyTypes[KeyIdx].pwszKeyTitle,
  245. fExportable ? CRYPT_EXPORTABLE : 0, // dwFlags
  246. &dwKeySpec
  247. )) {
  248. PrintLastError("PrivateKeyLoad");
  249. goto ErrorReturn;
  250. }
  251. }
  252. ReturnStatus = 0;
  253. goto CommonReturn;
  254. BadUsage:
  255. Usage();
  256. ErrorReturn:
  257. ReturnStatus = -1;
  258. CommonReturn:
  259. if (hFile != INVALID_HANDLE_VALUE)
  260. CloseHandle(hFile);
  261. if (hProv)
  262. CryptReleaseContext(hProv, 0);
  263. if (pbKey)
  264. TestFree(pbKey);
  265. if (!ReturnStatus)
  266. printf("Passed\n");
  267. else
  268. printf("Failed\n");
  269. return ReturnStatus;
  270. }