Source code of Windows XP (NT5)
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.

362 lines
7.2 KiB

  1. #include "precomp.h"
  2. LPWSTR gpszIpsecMMAuthMethodsKey =
  3. L"SOFTWARE\\Microsoft\\IPSec\\MM Auth Methods";
  4. DWORD
  5. PersistMMAuthMethods(
  6. PMM_AUTH_METHODS pMMAuthMethods
  7. )
  8. {
  9. DWORD dwError = 0;
  10. HKEY hRegistryKey = NULL;
  11. DWORD dwDisposition = 0;
  12. dwError = RegCreateKeyExW(
  13. HKEY_LOCAL_MACHINE,
  14. gpszIpsecMMAuthMethodsKey,
  15. 0,
  16. NULL,
  17. 0,
  18. KEY_ALL_ACCESS,
  19. NULL,
  20. &hRegistryKey,
  21. &dwDisposition
  22. );
  23. BAIL_ON_WIN32_ERROR(dwError);
  24. dwError = SPDWriteMMAuthMethods(
  25. hRegistryKey,
  26. pMMAuthMethods
  27. );
  28. BAIL_ON_WIN32_ERROR(dwError);
  29. cleanup:
  30. if (hRegistryKey) {
  31. RegCloseKey(hRegistryKey);
  32. }
  33. return (dwError);
  34. error:
  35. if (hRegistryKey) {
  36. (VOID) SPDPurgeMMAuthMethods(
  37. pMMAuthMethods->gMMAuthID
  38. );
  39. }
  40. goto cleanup;
  41. }
  42. DWORD
  43. SPDWriteMMAuthMethods(
  44. HKEY hParentRegKey,
  45. PMM_AUTH_METHODS pMMAuthMethods
  46. )
  47. {
  48. DWORD dwError = 0;
  49. WCHAR szAuthID[MAX_PATH];
  50. LPWSTR pszStringUuid = NULL;
  51. HKEY hRegKey = NULL;
  52. DWORD dwDisposition = 0;
  53. LPBYTE pBuffer = NULL;
  54. DWORD dwBufferSize = 0;
  55. szAuthID[0] = L'\0';
  56. dwError = UuidToString(
  57. &pMMAuthMethods->gMMAuthID,
  58. &pszStringUuid
  59. );
  60. BAIL_ON_WIN32_ERROR(dwError);
  61. wcscpy(szAuthID, L"{");
  62. wcscat(szAuthID, pszStringUuid);
  63. wcscat(szAuthID, L"}");
  64. dwError = RegCreateKeyExW(
  65. hParentRegKey,
  66. szAuthID,
  67. 0,
  68. NULL,
  69. 0,
  70. KEY_ALL_ACCESS,
  71. NULL,
  72. &hRegKey,
  73. &dwDisposition
  74. );
  75. BAIL_ON_WIN32_ERROR(dwError);
  76. dwError = RegSetValueExW(
  77. hRegKey,
  78. L"AuthID",
  79. 0,
  80. REG_SZ,
  81. (LPBYTE) szAuthID,
  82. (wcslen(szAuthID) + 1)*sizeof(WCHAR)
  83. );
  84. BAIL_ON_WIN32_ERROR(dwError);
  85. dwError = RegSetValueExW(
  86. hRegKey,
  87. L"Flags",
  88. 0,
  89. REG_DWORD,
  90. (LPBYTE)&pMMAuthMethods->dwFlags,
  91. sizeof(DWORD)
  92. );
  93. BAIL_ON_WIN32_ERROR(dwError);
  94. dwError = MarshallMMAuthInfoBundle(
  95. pMMAuthMethods->pAuthenticationInfo,
  96. pMMAuthMethods->dwNumAuthInfos,
  97. &pBuffer,
  98. &dwBufferSize
  99. );
  100. BAIL_ON_WIN32_ERROR(dwError);
  101. dwError = RegSetValueExW(
  102. hRegKey,
  103. L"AuthInfoBundle",
  104. 0,
  105. REG_BINARY,
  106. (LPBYTE) pBuffer,
  107. dwBufferSize
  108. );
  109. BAIL_ON_WIN32_ERROR(dwError);
  110. cleanup:
  111. if (pszStringUuid) {
  112. RpcStringFree(&pszStringUuid);
  113. }
  114. if (hRegKey) {
  115. RegCloseKey(hRegKey);
  116. }
  117. if (pBuffer) {
  118. FreeSPDMem(pBuffer);
  119. }
  120. return (dwError);
  121. error:
  122. goto cleanup;
  123. }
  124. DWORD
  125. MarshallMMAuthInfoBundle(
  126. PIPSEC_MM_AUTH_INFO pAuthInfoBundle,
  127. DWORD dwNumAuthInfos,
  128. LPBYTE * ppBuffer,
  129. PDWORD pdwBufferSize
  130. )
  131. {
  132. DWORD dwError = 0;
  133. LPBYTE pBuffer = NULL;
  134. DWORD dwBufferSize = 0;
  135. PIPSEC_MM_AUTH_INFO pTemp = NULL;
  136. DWORD i = 0;
  137. LPBYTE pMem = NULL;
  138. static const GUID GUID_IPSEC_MM_AUTH_INFO_VER1 =
  139. { 0xabcd0003, 0x0001, 0x0001, { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 } };
  140. DWORD dwNumBytesAdvanced = 0;
  141. dwBufferSize = sizeof(GUID) +
  142. sizeof(DWORD) +
  143. sizeof(DWORD);
  144. pTemp = pAuthInfoBundle;
  145. for (i = 0; i < dwNumAuthInfos; i++) {
  146. dwBufferSize += sizeof(DWORD);
  147. dwBufferSize += sizeof(DWORD);
  148. dwBufferSize += pTemp->dwAuthInfoSize;
  149. pTemp++;
  150. }
  151. pBuffer = (LPBYTE) AllocSPDMem(
  152. dwBufferSize
  153. );
  154. if (!pBuffer) {
  155. dwError = ERROR_OUTOFMEMORY;
  156. BAIL_ON_WIN32_ERROR(dwError);
  157. }
  158. pMem = pBuffer;
  159. memcpy(
  160. pMem,
  161. (LPBYTE) &GUID_IPSEC_MM_AUTH_INFO_VER1,
  162. sizeof(GUID)
  163. );
  164. pMem += sizeof(GUID);
  165. memcpy(
  166. pMem,
  167. (LPBYTE) &dwBufferSize,
  168. sizeof(DWORD)
  169. );
  170. pMem += sizeof(DWORD);
  171. memcpy(
  172. pMem,
  173. (LPBYTE) &dwNumAuthInfos,
  174. sizeof(DWORD)
  175. );
  176. pMem += sizeof(DWORD);
  177. pTemp = pAuthInfoBundle;
  178. for (i = 0; i < dwNumAuthInfos; i++) {
  179. CopyMMAuthInfoIntoBuffer(
  180. pTemp,
  181. pMem,
  182. &dwNumBytesAdvanced
  183. );
  184. pMem += dwNumBytesAdvanced;
  185. pTemp++;
  186. }
  187. *ppBuffer = pBuffer;
  188. *pdwBufferSize = dwBufferSize;
  189. return (dwError);
  190. error:
  191. *ppBuffer = NULL;
  192. *pdwBufferSize = 0;
  193. return (dwError);
  194. }
  195. VOID
  196. CopyMMAuthInfoIntoBuffer(
  197. PIPSEC_MM_AUTH_INFO pMMAuthInfo,
  198. LPBYTE pBuffer,
  199. PDWORD pdwNumBytesAdvanced
  200. )
  201. {
  202. DWORD dwError = 0;
  203. DWORD dwNumBytesAdvanced = 0;
  204. DWORD dwAuthMethod = 0;
  205. DWORD dwAuthInfoSize = 0;
  206. LPBYTE pAuthInfo = NULL;
  207. LPBYTE pMem = NULL;
  208. dwAuthMethod = (DWORD) pMMAuthInfo->AuthMethod;
  209. dwAuthInfoSize = pMMAuthInfo->dwAuthInfoSize;
  210. pAuthInfo = pMMAuthInfo->pAuthInfo;
  211. pMem = pBuffer;
  212. memcpy(
  213. pMem,
  214. (LPBYTE) &dwAuthMethod,
  215. sizeof(DWORD)
  216. );
  217. pMem += sizeof(DWORD);
  218. dwNumBytesAdvanced += sizeof(DWORD);
  219. memcpy(
  220. pMem,
  221. (LPBYTE) &dwAuthInfoSize,
  222. sizeof(DWORD)
  223. );
  224. pMem += sizeof(DWORD);
  225. dwNumBytesAdvanced += sizeof(DWORD);
  226. if (dwAuthInfoSize) {
  227. memcpy(
  228. pMem,
  229. pAuthInfo,
  230. dwAuthInfoSize
  231. );
  232. }
  233. pMem += dwAuthInfoSize;
  234. dwNumBytesAdvanced += dwAuthInfoSize;
  235. *pdwNumBytesAdvanced = dwNumBytesAdvanced;
  236. return;
  237. }
  238. DWORD
  239. SPDPurgeMMAuthMethods(
  240. GUID gMMAuthID
  241. )
  242. {
  243. DWORD dwError = 0;
  244. HKEY hParentRegKey = NULL;
  245. DWORD dwDisposition = 0;
  246. WCHAR szAuthID[MAX_PATH];
  247. LPWSTR pszStringUuid = NULL;
  248. dwError = RegCreateKeyExW(
  249. HKEY_LOCAL_MACHINE,
  250. gpszIpsecMMAuthMethodsKey,
  251. 0,
  252. NULL,
  253. 0,
  254. KEY_ALL_ACCESS,
  255. NULL,
  256. &hParentRegKey,
  257. &dwDisposition
  258. );
  259. BAIL_ON_WIN32_ERROR(dwError);
  260. szAuthID[0] = L'\0';
  261. dwError = UuidToString(
  262. &gMMAuthID,
  263. &pszStringUuid
  264. );
  265. BAIL_ON_WIN32_ERROR(dwError);
  266. wcscpy(szAuthID, L"{");
  267. wcscat(szAuthID, pszStringUuid);
  268. wcscat(szAuthID, L"}");
  269. dwError = RegDeleteKeyW(
  270. hParentRegKey,
  271. szAuthID
  272. );
  273. BAIL_ON_WIN32_ERROR(dwError);
  274. error:
  275. if (hParentRegKey) {
  276. RegCloseKey(hParentRegKey);
  277. }
  278. if (pszStringUuid) {
  279. RpcStringFree(&pszStringUuid);
  280. }
  281. return(dwError);
  282. }