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.

242 lines
6.4 KiB

  1. //+-------------------------------------------------------------------------
  2. // Microsoft Windows
  3. //
  4. // Copyright (C) Microsoft Corporation, 1995 - 1996
  5. //
  6. // File: pkcs8im.cpp
  7. //
  8. // Contents: Private Key Load Test
  9. //
  10. // See Usage() for list of load options.
  11. //
  12. // Functions: main
  13. //
  14. // History: 6-26-96
  15. //
  16. //--------------------------------------------------------------------------
  17. #include <windows.h>
  18. #include <assert.h>
  19. #include "wincrypt.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: pkcs8im [options] <Filename> <KeyType>\n");
  40. printf("Options are:\n");
  41. printf(" -p<name> - Crypto provider name (if not default)\n");
  42. printf(" -c<name> - Crypto key container name\n");
  43. printf(" -E - Exportable private keys\n");
  44. printf(" -h - This message\n");
  45. printf("\n");
  46. printf("KeyType (case insensitive):\n");
  47. for (i = 0; i < NKEYTYPES; i++)
  48. printf(" %s\n", KeyTypes[i].pszName);
  49. printf("\n");
  50. }
  51. static BOOL CALLBACK ResolvehCryptFunc(
  52. CRYPT_PRIVATE_KEY_INFO *pPrivateKeyInfo,
  53. HCRYPTPROV *phCryptProv,
  54. LPVOID pVoidResolveFunc)
  55. {
  56. CRYPT_KEY_PROV_INFO *pCryptKeyProvInfo = (CRYPT_KEY_PROV_INFO *) pVoidResolveFunc;
  57. return (CryptAcquireContext(
  58. phCryptProv,
  59. (LPSTR) pCryptKeyProvInfo->pwszContainerName,
  60. (LPSTR) pCryptKeyProvInfo->pwszProvName,
  61. PROV_RSA_FULL,
  62. CRYPT_NEWKEYSET));
  63. }
  64. int _cdecl main(int argc, char * argv[])
  65. {
  66. int ReturnStatus;
  67. HCRYPTPROV hProv = 0;
  68. HANDLE hFile = INVALID_HANDLE_VALUE;
  69. BOOL fForce = FALSE;
  70. BYTE *pbKey = NULL;
  71. DWORD cbKey;
  72. DWORD cbRead;
  73. LPSTR pszProvider = NULL;
  74. DWORD numWideChars;
  75. LPSTR pszContainer = NULL;
  76. LPSTR pszFilename = NULL;
  77. LPSTR pszKeyType = NULL;
  78. int KeyIdx = 0;
  79. DWORD dwFlags = 0;
  80. DWORD dwKeySpec = 0;
  81. CRYPT_KEY_PROV_INFO CryptKeyProvInfo;
  82. CRYPT_PRIVATE_KEY_BLOB_AND_PARAMS KeyBlobAndParams;
  83. BYTE *pPrivateKeyBuffer = NULL;
  84. while (--argc>0)
  85. {
  86. if (**++argv == '-')
  87. {
  88. switch(argv[0][1])
  89. {
  90. case 'F':
  91. fForce = TRUE;
  92. break;
  93. case 'E':
  94. dwFlags = CRYPT_EXPORTABLE;
  95. break;
  96. case 'p':
  97. pszProvider = (LPSTR) argv[0]+2;
  98. if (*pszContainer == L'\0') {
  99. printf("Need to specify crypto key container name\n");
  100. goto BadUsage;
  101. }
  102. break;
  103. case 'c':
  104. pszContainer = (LPSTR) argv[0]+2;
  105. if (*pszContainer == L'\0') {
  106. printf("Need to specify crypto key container name\n");
  107. goto BadUsage;
  108. }
  109. break;
  110. case 'h':
  111. default:
  112. goto BadUsage;
  113. }
  114. } else {
  115. if (pszFilename == NULL)
  116. pszFilename = argv[0];
  117. else if(pszKeyType == NULL)
  118. pszKeyType = argv[0];
  119. else {
  120. printf("Too many arguments\n");
  121. goto BadUsage;
  122. }
  123. }
  124. }
  125. if (pszFilename == NULL) {
  126. printf("missing Filename\n");
  127. goto BadUsage;
  128. }
  129. printf("command line: %s\n", GetCommandLine());
  130. if (pszKeyType) {
  131. for (KeyIdx = 0; KeyIdx < NKEYTYPES; KeyIdx++) {
  132. if (_stricmp(pszKeyType, KeyTypes[KeyIdx].pszName) == 0)
  133. break;
  134. }
  135. if (KeyIdx >= NKEYTYPES) {
  136. printf("Bad KeyType: %s\n", pszKeyType);
  137. goto BadUsage;
  138. }
  139. } else {
  140. printf("No KeyType specified... using type specified in key\n");
  141. }
  142. hFile = CreateFileA(
  143. pszFilename,
  144. GENERIC_READ,
  145. FILE_SHARE_READ,
  146. NULL, // lpsa
  147. OPEN_EXISTING,
  148. FILE_ATTRIBUTE_NORMAL,
  149. NULL // hTemplateFile
  150. );
  151. if (hFile == INVALID_HANDLE_VALUE) {
  152. printf( "can't open %s\n", pszFilename);
  153. goto ErrorReturn;
  154. }
  155. memset(&CryptKeyProvInfo, 0, sizeof(CRYPT_KEY_PROV_INFO));
  156. CryptKeyProvInfo.pwszContainerName = (LPWSTR) pszContainer;
  157. CryptKeyProvInfo.pwszProvName = (LPWSTR) pszProvider;
  158. if (pszKeyType)
  159. CryptKeyProvInfo.dwKeySpec = KeyTypes[KeyIdx].dwKeySpec;
  160. else
  161. CryptKeyProvInfo.dwKeySpec = 0;
  162. cbKey = GetFileSize(hFile, NULL);
  163. if (cbKey == 0) {
  164. printf( "empty file %s\n", pszFilename);
  165. goto ErrorReturn;
  166. }
  167. if (NULL == (pbKey = (PBYTE)TestAlloc(cbKey)))
  168. goto ErrorReturn;
  169. if (!ReadFile(hFile, pbKey, cbKey, &cbRead, NULL) ||
  170. (cbRead != cbKey)) {
  171. printf( "can't read %s\n", pszFilename);
  172. goto ErrorReturn;
  173. }
  174. KeyBlobAndParams.PrivateKey.cbData = cbKey;
  175. KeyBlobAndParams.PrivateKey.pbData = pbKey;
  176. KeyBlobAndParams.pResolvehCryptProvFunc = ResolvehCryptFunc;
  177. KeyBlobAndParams.pVoidResolveFunc = &CryptKeyProvInfo;
  178. KeyBlobAndParams.pDecryptPrivateKeyFunc = NULL;
  179. KeyBlobAndParams.pVoidDecryptFunc = NULL;
  180. if (!CryptImportPKCS8(
  181. KeyBlobAndParams,
  182. dwFlags,
  183. NULL,
  184. NULL
  185. )) {
  186. PrintLastError("CryptImportPKCS8()");
  187. goto ErrorReturn;
  188. }
  189. if (dwKeySpec == AT_SIGNATURE)
  190. printf("Key imported as type 'Sign'\n");
  191. else if (dwKeySpec == AT_KEYEXCHANGE)
  192. printf("Key imported as type 'Xchg'\n");
  193. ReturnStatus = 0;
  194. goto CommonReturn;
  195. BadUsage:
  196. Usage();
  197. ErrorReturn:
  198. ReturnStatus = -1;
  199. CommonReturn:
  200. if (hFile != INVALID_HANDLE_VALUE)
  201. CloseHandle(hFile);
  202. if (hProv)
  203. CryptReleaseContext(hProv, 0);
  204. if (pbKey)
  205. TestFree(pbKey);
  206. if (!ReturnStatus)
  207. printf("Passed\n");
  208. else
  209. printf("Failed\n");
  210. return ReturnStatus;
  211. }