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.

258 lines
6.0 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1997 - 1999
  6. //
  7. // File: certie3.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <windows.h>
  13. #include <stdio.h>
  14. #include "wincrypt.h"
  15. int ln = 0;
  16. // This has to be big enough to hold a registry value's data.
  17. char szStr[5000];
  18. #define DISPLAY(sz) printf("%hs\n", sz)
  19. void __cdecl
  20. main(
  21. int argc,
  22. char **argv)
  23. {
  24. char szRegPath[MAX_PATH] = "SOFTWARE\\Microsoft\\Cryptography\\";
  25. char sourceloc[MAX_PATH];
  26. char *pszFileOut;
  27. char *pszRegKey;
  28. HKEY hKeyBase;
  29. BOOL fAuth = FALSE;
  30. fAuth = argc > 1 && argv[1][0] == '-';
  31. if (fAuth)
  32. {
  33. pszFileOut = "ClientAuth.dat";
  34. strcpy(sourceloc, "HKEY_CURRENT_USER");
  35. pszRegKey = "PersonalCertificates\\ClientAuth\\Certificates";
  36. hKeyBase = HKEY_CURRENT_USER;
  37. }
  38. else
  39. {
  40. pszFileOut = "CertStore.dat";
  41. strcpy(sourceloc, "HKEY_LOCAL_MACHINE");
  42. pszRegKey = "CertificateStore\\Certificates";
  43. hKeyBase = HKEY_LOCAL_MACHINE;
  44. }
  45. ln = 0;
  46. strcat(szRegPath, pszRegKey);
  47. strcat(sourceloc, "\\");
  48. strcat(sourceloc, szRegPath);
  49. strcpy(szStr, "Collect information from Registry");
  50. DISPLAY(szStr);
  51. ln++;
  52. strcpy(szStr, "Registry location: ");
  53. strcat(szStr, sourceloc);
  54. DISPLAY(szStr);
  55. ln++;
  56. strcpy(szStr, "Target destination for registry dump: ");
  57. strcat(szStr, pszFileOut);
  58. DISPLAY(szStr);
  59. // Declarations for the output file related stuff
  60. HCRYPTPROV hProv = NULL;
  61. HCERTSTORE hCertStore = NULL;
  62. CERT_INFO certinfo;
  63. CERT_CONTEXT const *pPrevCertContext = NULL;
  64. CERT_CONTEXT const *pCertContext = NULL;
  65. DWORD dwErr;
  66. if (!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, 0))
  67. {
  68. dwErr = GetLastError();
  69. if (dwErr == NTE_BAD_KEYSET)
  70. {
  71. strcpy(szStr, "NTE_BAD_KEYSET error on call CryptAcquireContext");
  72. DISPLAY(szStr);
  73. hProv = NULL;
  74. if (!CryptAcquireContext(
  75. &hProv,
  76. NULL,
  77. NULL,
  78. PROV_RSA_FULL,
  79. CRYPT_NEWKEYSET))
  80. {
  81. strcpy(szStr, "CryptAcquireContext - call failed");
  82. DISPLAY(szStr);
  83. exit(6);
  84. }
  85. }
  86. }
  87. HANDLE hFile = NULL;
  88. hFile = CreateFile(
  89. pszFileOut,
  90. GENERIC_WRITE,
  91. 0,
  92. NULL,
  93. CREATE_ALWAYS,
  94. FILE_ATTRIBUTE_NORMAL,
  95. NULL);
  96. if (hFile == INVALID_HANDLE_VALUE)
  97. {
  98. printf("Couldn't open output file\n");
  99. exit(5);
  100. }
  101. hCertStore = CertOpenStore(
  102. CERT_STORE_PROV_MEMORY,
  103. X509_ASN_ENCODING,
  104. NULL, // hProv
  105. CERT_STORE_NO_CRYPT_RELEASE_FLAG,
  106. NULL);
  107. if (NULL == hCertStore)
  108. {
  109. exit(8);
  110. }
  111. // Declarations for the registry stuff
  112. HKEY hkMain;
  113. HRESULT hr;
  114. hr = RegOpenKeyEx(
  115. hKeyBase,
  116. szRegPath,
  117. 0,
  118. KEY_QUERY_VALUE,
  119. &hkMain);
  120. if (hr != S_OK)
  121. {
  122. exit(3);
  123. }
  124. // Use the RegQueryInfoKey function to determine the maximum size of the
  125. // name and data buffers,
  126. CHAR ClassName[MAX_PATH] = ""; // Buffer for class name.
  127. DWORD dwcClassLen = MAX_PATH; // Length of class string.
  128. DWORD dwcSubKeys; // Number of sub keys.
  129. DWORD dwcMaxSubKey; // Longest sub key size.
  130. DWORD dwcMaxClass; // Longest class string.
  131. DWORD dwcValues; // Number of values for this key.
  132. DWORD dwcMaxValueName; // Longest Value name.
  133. DWORD dwcMaxValueData; // Longest Value data.
  134. DWORD dwcSecDesc; // Security descriptor.
  135. FILETIME ftLastWriteTime; // Last write time.
  136. RegQueryInfoKey(
  137. hkMain, // Key handle.
  138. ClassName, // Buffer for class name.
  139. &dwcClassLen, // Length of class string.
  140. NULL, // Reserved.
  141. &dwcSubKeys, // Number of sub keys.
  142. &dwcMaxSubKey, // Longest sub key size.
  143. &dwcMaxClass, // Longest class string.
  144. &dwcValues, // Number of values for this key.
  145. &dwcMaxValueName, // Longest Value name.
  146. &dwcMaxValueData, // Longest Value data.
  147. &dwcSecDesc, // Security descriptor.
  148. &ftLastWriteTime); // Last write time
  149. DWORD i;
  150. CHAR ValueName[MAX_PATH];
  151. DWORD dwcValueName;
  152. // address of buffer for type code (this is returned by RegEnumValue)
  153. DWORD pType;
  154. // address of buffer for value data
  155. unsigned char *pData = new unsigned char[dwcMaxValueData + 1];
  156. DWORD pcbData; // address for size of data buffer
  157. for (i = 0; i < dwcValues; i++)
  158. {
  159. ValueName[0] = '\0';
  160. dwcValueName = sizeof(ValueName)/sizeof(ValueName[0]);
  161. pcbData = dwcMaxValueData + 1;
  162. hr = RegEnumValue(
  163. hkMain,
  164. i, // index of value to query
  165. ValueName, // address of buffer for value string
  166. &dwcValueName, // address for size of value string buf
  167. NULL, // reserved
  168. &pType, // &pType
  169. pData, // pData
  170. &pcbData); // &pcbData
  171. hr = myHError(hr);
  172. if (HRESULT_FROM_WIN32(ERROR_NO_MORE_ITEMS) == hr)
  173. {
  174. break;
  175. }
  176. if (S_OK != hr)
  177. {
  178. exit(2);
  179. }
  180. // Display the value name
  181. ln++;
  182. strcpy(szStr, ValueName);
  183. DISPLAY(szStr);
  184. if (pType == REG_BINARY)
  185. {
  186. // Write the data which is pointed to by pData,
  187. // count of bytes is gotten from pcbData
  188. CertAddEncodedCertificateToStore(
  189. hCertStore,
  190. X509_ASN_ENCODING,
  191. pData,
  192. pcbData,
  193. CERT_STORE_ADD_USE_EXISTING,
  194. NULL);
  195. }
  196. }
  197. // Save
  198. CertSaveStore(
  199. hCertStore,
  200. 0, // dwEncodingType,
  201. CERT_STORE_SAVE_AS_STORE,
  202. CERT_STORE_SAVE_TO_FILE,
  203. (void *) hFile,
  204. 0 // dwFlags
  205. );
  206. // Close memory store
  207. CertCloseStore(hCertStore, CERT_CLOSE_STORE_FORCE_FLAG);
  208. if (!CryptReleaseContext(hProv, 0))
  209. {
  210. exit(7);
  211. }
  212. RegCloseKey(hkMain);
  213. ln++;
  214. strcpy(szStr, "CertIE3.exe completed successfully");
  215. DISPLAY(szStr);
  216. }