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.

217 lines
6.5 KiB

  1. #undef UNICODE // ## Not Yet
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <ctype.h>
  5. #include <windows.h>
  6. #include <wincrypt.h>
  7. #define MS_DSS_TYPE "DSS Signature"
  8. #define MS_DH_TYPE "DSS Signature with Diffie-Hellman Key Exachange"
  9. #define PROVPATH "SOFTWARE\\Microsoft\\Cryptography\\Defaults\\Provider\\"
  10. #define PROVPATH_LEN sizeof(PROVPATH)
  11. #define TYPEPATH "SOFTWARE\\Microsoft\\Cryptography\\Defaults\\Provider Types\\Type "
  12. #define TYPEPATH_LEN sizeof(TYPEPATH)
  13. #define IMAGEPATH "dssbase.dll"
  14. #define IMAGEPATH_LEN sizeof(IMAGEPATH)
  15. BOOL SetCSPInfo(
  16. LPSTR pszProvider,
  17. LPSTR pszImagePath,
  18. BYTE *pbSig,
  19. DWORD cbSig,
  20. DWORD dwProvType,
  21. LPSTR pszTypeName
  22. )
  23. {
  24. DWORD dwIgn;
  25. HKEY hKey = 0;
  26. HKEY hTypeKey = 0;
  27. DWORD cbProv;
  28. BYTE *pszProv = NULL;
  29. CHAR pszTypeString[32];
  30. DWORD cbType;
  31. BYTE *pszType = NULL;
  32. DWORD err;
  33. BOOL fRet = FALSE;
  34. cbProv = PROVPATH_LEN + strlen(pszProvider);
  35. if (NULL == (pszProv = (LPSTR)LocalAlloc(LMEM_ZEROINIT, cbProv)))
  36. goto Ret;
  37. strcpy(pszProv, PROVPATH);
  38. strcat(pszProv, pszProvider);
  39. //
  40. // Create or open in local machine for provider:
  41. // Microsoft Base Cryptographic Provider v1.0
  42. //
  43. if (ERROR_SUCCESS != (err = RegCreateKeyEx(HKEY_LOCAL_MACHINE,
  44. (const char *)pszProv,
  45. 0L, "", REG_OPTION_NON_VOLATILE,
  46. KEY_ALL_ACCESS, NULL, &hKey,
  47. &dwIgn)))
  48. goto Ret;
  49. //
  50. // Set Image path to: scp.dll
  51. //
  52. if (ERROR_SUCCESS != (err = RegSetValueEx(hKey, "Image Path", 0L, REG_SZ,
  53. pszImagePath,
  54. strlen(pszImagePath) + 1)))
  55. goto Ret;
  56. //
  57. // Set Type to: Type 003
  58. //
  59. if (ERROR_SUCCESS != (err = RegSetValueEx(hKey, "Type", 0L, REG_DWORD,
  60. (LPTSTR)&dwProvType,
  61. sizeof(DWORD))))
  62. goto Ret;
  63. //
  64. // Place signature
  65. //
  66. if (ERROR_SUCCESS != (err = RegSetValueEx(hKey, "Signature", 0L,
  67. REG_BINARY, pbSig, cbSig)))
  68. goto Ret;
  69. //
  70. // Create or open in local machine for provider type:
  71. //
  72. memset(pszTypeString, 0, sizeof(pszTypeString));
  73. sprintf(pszTypeString, "%.3d", dwProvType);
  74. cbType = TYPEPATH_LEN + strlen(pszTypeString);
  75. if (NULL == (pszType = (LPSTR)LocalAlloc(LMEM_ZEROINIT, cbType)))
  76. goto Ret;
  77. strcpy(pszType, TYPEPATH);
  78. strcat(pszType, pszTypeString);
  79. if (ERROR_SUCCESS != (err = RegCreateKeyEx(HKEY_LOCAL_MACHINE,
  80. (const char *) pszType,
  81. 0L, "", REG_OPTION_NON_VOLATILE,
  82. KEY_ALL_ACCESS, NULL, &hTypeKey,
  83. &dwIgn)))
  84. goto Ret;
  85. if (ERROR_SUCCESS != (err = RegSetValueEx(hTypeKey, "Name", 0L,
  86. REG_SZ, pszProvider,
  87. strlen(pszProvider) + 1)))
  88. goto Ret;
  89. if (ERROR_SUCCESS != (err = RegSetValueEx(hTypeKey, "TypeName", 0L,
  90. REG_SZ, pszTypeName,
  91. strlen(pszTypeName) + 1)))
  92. goto Ret;
  93. fRet = TRUE;
  94. Ret:
  95. if (hKey)
  96. RegCloseKey(hKey);
  97. if (hTypeKey)
  98. RegCloseKey(hTypeKey);
  99. if (pszProv)
  100. LocalFree(pszProv);
  101. if (pszType)
  102. LocalFree(pszType);
  103. return fRet;
  104. }
  105. int __cdecl main(int cArg, char *rgszArg[])
  106. {
  107. DWORD err;
  108. DWORD dwValue;
  109. HANDLE hFileSig = INVALID_HANDLE_VALUE;
  110. DWORD NumBytesRead;
  111. DWORD lpdwFileSizeHigh;
  112. LPVOID lpvAddress = NULL;
  113. DWORD NumBytes;
  114. BOOL fRet = FALSE;
  115. //
  116. // Just to open scp.dll signature file. This file was created by
  117. // sign.exe.
  118. //
  119. if (INVALID_HANDLE_VALUE == (hFileSig = CreateFile("dssbase.sig",
  120. GENERIC_READ, 0, NULL,
  121. OPEN_EXISTING,
  122. FILE_ATTRIBUTE_NORMAL,
  123. 0)))
  124. {
  125. printf("Install failed: Unable to open dssbase.sig: %x\n", GetLastError());
  126. goto Ret;
  127. }
  128. if ((NumBytes = GetFileSize((HANDLE) hFileSig, &lpdwFileSizeHigh)) ==
  129. 0xffffffff)
  130. {
  131. printf("Install failed: Getting size of file dssbase.sig: %x\n", GetLastError());
  132. goto Ret;
  133. }
  134. if ((lpvAddress = VirtualAlloc(NULL, NumBytes, MEM_RESERVE |
  135. MEM_COMMIT,
  136. PAGE_READWRITE)) == NULL)
  137. {
  138. printf("Install failed: Alloc to read dssbase.sig: %x\n", GetLastError());
  139. goto Ret;
  140. }
  141. if (!ReadFile((HANDLE) hFileSig, lpvAddress, NumBytes,
  142. &NumBytesRead, 0))
  143. {
  144. printf("Install failed: Reading dssbase.sig: %x\n", GetLastError());
  145. goto Ret;
  146. }
  147. if (NumBytesRead != NumBytes)
  148. {
  149. printf("Install failed: Bytes read doesn't match file size\n");
  150. goto Ret;
  151. }
  152. //
  153. // install the default DSS provider
  154. // Type 003
  155. //
  156. if (!SetCSPInfo(MS_DEF_DSS_PROV,
  157. IMAGEPATH,
  158. lpvAddress,
  159. NumBytesRead,
  160. PROV_DSS,
  161. MS_DSS_TYPE))
  162. {
  163. printf("Unable to install dssbase.dll");
  164. goto Ret;
  165. }
  166. //
  167. // install the default DSS/DH provider
  168. // Type 013
  169. //
  170. if (!SetCSPInfo(MS_DEF_DSS_DH_PROV,
  171. IMAGEPATH,
  172. lpvAddress,
  173. NumBytesRead,
  174. PROV_DSS_DH,
  175. MS_DH_TYPE))
  176. {
  177. printf("Unable to install dssbase.dll");
  178. goto Ret;
  179. }
  180. printf("Installed: %s\n", IMAGEPATH);
  181. fRet = TRUE;
  182. Ret:
  183. if (lpvAddress)
  184. VirtualFree(lpvAddress, 0, MEM_RELEASE);
  185. if (INVALID_HANDLE_VALUE != hFileSig)
  186. CloseHandle(hFileSig);
  187. return fRet;
  188. }