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.

260 lines
7.0 KiB

  1. //+-------------------------------------------------------------------------
  2. // Microsoft Windows
  3. //
  4. // Copyright (C) Microsoft Corporation, 1995 - 1996
  5. //
  6. // File: tpvksave.cpp
  7. //
  8. // Contents: Private Key Save Test
  9. //
  10. // See Usage() for list of save options.
  11. //
  12. // Functions: main
  13. //
  14. // History: 11-May-96 philh created
  15. //--------------------------------------------------------------------------
  16. #include <windows.h>
  17. #include <assert.h>
  18. #include "wincrypt.h"
  19. #include "pvkhlpr.h"
  20. #include "certtest.h"
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <memory.h>
  25. #include <time.h>
  26. static struct
  27. {
  28. LPCSTR pszName;
  29. LPCWSTR pwszKeyTitle;
  30. DWORD dwKeySpec;
  31. } KeyTypes[] = {
  32. "Sign", L"Signature", AT_SIGNATURE,
  33. "Xchg", L"Exchange", AT_KEYEXCHANGE
  34. };
  35. #define NKEYTYPES (sizeof(KeyTypes)/sizeof(KeyTypes[0]))
  36. static void Usage(void)
  37. {
  38. int i;
  39. printf("Usage: tpvksave [options] <Filename> <KeyType>\n");
  40. printf("Options are:\n");
  41. printf(" -p<number> - Crypto provider type number\n");
  42. printf(" -c<name> - Crypto key container name\n");
  43. printf(" -d - Delete from provider after saving\n");
  44. printf(" -m - Test memory version of API\n");
  45. printf(" -n - Use machine key\n");
  46. printf(" -3 - Export as VER3 blob\n");
  47. printf(" -h - This message\n");
  48. printf("\n");
  49. printf("KeyType (case insensitive):\n");
  50. for (i = 0; i < NKEYTYPES; i++)
  51. printf(" %s\n", KeyTypes[i].pszName);
  52. printf("\n");
  53. }
  54. int _cdecl main(int argc, char * argv[])
  55. {
  56. int ReturnStatus;
  57. HCRYPTPROV hProv = 0;
  58. HANDLE hFile = INVALID_HANDLE_VALUE;
  59. DWORD dwProvType = PROV_RSA_FULL;
  60. BOOL fDelete = FALSE;
  61. BOOL fMem = FALSE;
  62. LPSTR pszContainer = NULL;
  63. LPSTR pszFilename = NULL;
  64. BYTE *pbKey = NULL;
  65. LPSTR pszKeyType = NULL;
  66. int KeyIdx = 0;
  67. DWORD dwFlags = 0;
  68. DWORD dwSaveFlags = 0;
  69. while (--argc>0)
  70. {
  71. if (**++argv == '-')
  72. {
  73. switch(argv[0][1])
  74. {
  75. case 'd':
  76. fDelete = TRUE;
  77. break;
  78. case 'm':
  79. fMem = TRUE;
  80. break;
  81. case 'p':
  82. dwProvType = strtoul( argv[0]+2, NULL, 10);
  83. break;
  84. case 'c':
  85. pszContainer = argv[0]+2;
  86. if (*pszContainer == '\0') {
  87. printf("Need to specify crypto key container name\n");
  88. goto BadUsage;
  89. }
  90. break;
  91. case 'n':
  92. dwFlags = CRYPT_MACHINE_KEYSET;
  93. break;
  94. case '3':
  95. dwSaveFlags |= CRYPT_BLOB_VER3;
  96. break;
  97. case 'h':
  98. default:
  99. goto BadUsage;
  100. }
  101. } else {
  102. if (pszFilename == NULL)
  103. pszFilename = argv[0];
  104. else if(pszKeyType == NULL)
  105. pszKeyType = argv[0];
  106. else {
  107. printf("Too many arguments\n");
  108. goto BadUsage;
  109. }
  110. }
  111. }
  112. if (pszFilename == NULL) {
  113. printf("missing Filename\n");
  114. goto BadUsage;
  115. }
  116. if (pszKeyType) {
  117. for (KeyIdx = 0; KeyIdx < NKEYTYPES; KeyIdx++) {
  118. if (_stricmp(pszKeyType, KeyTypes[KeyIdx].pszName) == 0)
  119. break;
  120. }
  121. if (KeyIdx >= NKEYTYPES) {
  122. printf("Bad KeyType: %s\n", pszKeyType);
  123. goto BadUsage;
  124. }
  125. } else {
  126. printf("missing KeyType\n");
  127. goto BadUsage;
  128. }
  129. if (!CryptAcquireContext(
  130. &hProv,
  131. pszContainer,
  132. NULL, // pszProvider
  133. dwProvType,
  134. dwFlags // dwFlags
  135. )) {
  136. PrintLastError("CryptAcquireContext");
  137. goto ErrorReturn;
  138. }
  139. hFile = CreateFileA(
  140. pszFilename,
  141. GENERIC_READ | GENERIC_WRITE,
  142. FILE_SHARE_READ,
  143. NULL, // lpsa
  144. CREATE_ALWAYS,
  145. FILE_ATTRIBUTE_NORMAL,
  146. NULL // hTemplateFile
  147. );
  148. if (hFile == INVALID_HANDLE_VALUE) {
  149. printf( "can't open %s\n", pszFilename);
  150. goto ErrorReturn;
  151. }
  152. if (fMem) {
  153. DWORD cbKey;
  154. DWORD cbWritten;
  155. cbKey = 0;
  156. PvkPrivateKeySaveToMemory(
  157. hProv,
  158. KeyTypes[KeyIdx].dwKeySpec,
  159. NULL, // hwndOwner
  160. KeyTypes[KeyIdx].pwszKeyTitle,
  161. dwSaveFlags,
  162. NULL, // pbKey
  163. &cbKey
  164. );
  165. if (cbKey == 0) {
  166. PrintLastError("PrivateKeySaveToMemory(cbKey == 0)");
  167. goto ErrorReturn;
  168. }
  169. if (NULL == (pbKey = (PBYTE)TestAlloc(cbKey)))
  170. goto ErrorReturn;
  171. if (!PvkPrivateKeySaveToMemory(
  172. hProv,
  173. KeyTypes[KeyIdx].dwKeySpec,
  174. NULL, // hwndOwner
  175. KeyTypes[KeyIdx].pwszKeyTitle,
  176. dwSaveFlags,
  177. pbKey,
  178. &cbKey
  179. )) {
  180. PrintLastError("PrivateKeySaveToMemory");
  181. goto ErrorReturn;
  182. }
  183. if (!WriteFile(hFile, pbKey, cbKey, &cbWritten, NULL)) {
  184. PrintLastError("WriteFile");
  185. goto ErrorReturn;
  186. }
  187. } else {
  188. if (!PvkPrivateKeySave(
  189. hProv,
  190. hFile,
  191. KeyTypes[KeyIdx].dwKeySpec,
  192. NULL, // hwndOwner
  193. KeyTypes[KeyIdx].pwszKeyTitle,
  194. dwSaveFlags
  195. )) {
  196. PrintLastError("PrivateKeySave");
  197. goto ErrorReturn;
  198. }
  199. }
  200. if (fDelete) {
  201. // Delete the existing keys
  202. CryptReleaseContext(hProv, 0);
  203. printf("Deleting existing private keys\n");
  204. // Note: for CRYPT_DELETEKEYSET, the returned hProv is undefined
  205. // and must not be released.
  206. if (!CryptAcquireContext(
  207. &hProv,
  208. pszContainer,
  209. NULL, // pszProvider
  210. dwProvType,
  211. CRYPT_DELETEKEYSET
  212. ))
  213. PrintLastError("CryptAcquireContext(CRYPT_DELETEKEYSET)");
  214. hProv = 0;
  215. }
  216. ReturnStatus = 0;
  217. goto CommonReturn;
  218. BadUsage:
  219. Usage();
  220. ErrorReturn:
  221. ReturnStatus = -1;
  222. if (hFile != INVALID_HANDLE_VALUE) {
  223. CloseHandle(hFile);
  224. hFile = INVALID_HANDLE_VALUE;
  225. DeleteFile(pszFilename);
  226. }
  227. CommonReturn:
  228. if (hFile != INVALID_HANDLE_VALUE)
  229. CloseHandle(hFile);
  230. if (hProv)
  231. CryptReleaseContext(hProv, 0);
  232. if (pbKey)
  233. TestFree(pbKey);
  234. return ReturnStatus;
  235. }