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.6 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1996 - 1999
  6. //
  7. // File: usagedef.cpp
  8. //
  9. // Contents: Microsoft Internet Security Trust Provider Model
  10. //
  11. // Functions: WintrustAddDefaultForUsage
  12. // WintrustGetDefaultForUsage
  13. //
  14. // History: 07-Sep-1997 pberkman created
  15. //
  16. //--------------------------------------------------------------------------
  17. #include "global.hxx"
  18. #include "cryptreg.h"
  19. BOOL WINAPI WintrustAddDefaultForUsage(const char *pszUsageOID,
  20. CRYPT_PROVIDER_REGDEFUSAGE *psDefUsage)
  21. {
  22. HKEY hKey;
  23. WCHAR wsz[REG_MAX_KEY_NAME];
  24. WCHAR wszUsage[REG_MAX_FUNC_NAME];
  25. WCHAR wszGuid[REG_MAX_GUID_TEXT];
  26. DWORD dwDisposition;
  27. HRESULT hr;
  28. int cchUsage;
  29. if (!(pszUsageOID) ||
  30. !(psDefUsage) ||
  31. !(_ISINSTRUCT(CRYPT_PROVIDER_REGDEFUSAGE,
  32. psDefUsage->cbStruct,
  33. pwszFreeCallbackDataFunctionName)) ||
  34. !(psDefUsage->pgActionID))
  35. {
  36. SetLastError(ERROR_INVALID_PARAMETER);
  37. return(FALSE);
  38. }
  39. if (!(guid2wstr(psDefUsage->pgActionID, &wszGuid[0])))
  40. {
  41. SetLastError(ERROR_INVALID_PARAMETER);
  42. return(FALSE);
  43. }
  44. wszUsage[0] = L'\0';
  45. cchUsage = MultiByteToWideChar(0, 0, pszUsageOID, -1, &wszUsage[0],
  46. REG_MAX_FUNC_NAME);
  47. if (0 >= cchUsage)
  48. {
  49. SetLastError(ERROR_INVALID_PARAMETER);
  50. return(FALSE);
  51. }
  52. if (REG_MAX_KEY_NAME < wcslen(REG_TRUST_USAGE_KEY) + 1 + cchUsage + 1)
  53. {
  54. SetLastError(ERROR_INVALID_PARAMETER);
  55. return(FALSE);
  56. }
  57. wcscpy(&wsz[0], REG_TRUST_USAGE_KEY);
  58. wcscat(&wsz[0], L"\\");
  59. wcscat(&wsz[0], &wszUsage[0]);
  60. if (RegCreateKeyExU(HKEY_LOCAL_MACHINE,
  61. &wsz[0],
  62. 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
  63. &hKey, &dwDisposition) != ERROR_SUCCESS)
  64. {
  65. return(FALSE);
  66. }
  67. hr = RegSetValueExU(hKey, REG_DEF_FOR_USAGE,
  68. 0, REG_SZ,
  69. (BYTE *)&wszGuid[0],
  70. (wcslen(&wszGuid[0]) + 1) * sizeof(WCHAR));
  71. if (psDefUsage->pwszDllName)
  72. {
  73. hr |= RegSetValueExU(hKey, REG_DLL_NAME,
  74. 0, REG_SZ,
  75. (BYTE *)psDefUsage->pwszDllName,
  76. (wcslen(psDefUsage->pwszDllName) + 1) * sizeof(WCHAR));
  77. if (psDefUsage->pwszLoadCallbackDataFunctionName)
  78. {
  79. hr |= RegSetValueExA(hKey, REG_DEF_CALLBACK_ALLOC,
  80. 0, REG_SZ,
  81. (BYTE *)psDefUsage->pwszLoadCallbackDataFunctionName,
  82. strlen(psDefUsage->pwszLoadCallbackDataFunctionName) + 1);
  83. if (psDefUsage->pwszFreeCallbackDataFunctionName)
  84. {
  85. hr |= RegSetValueExA(hKey, REG_DEF_CALLBACK_FREE,
  86. 0, REG_SZ,
  87. (BYTE *)psDefUsage->pwszFreeCallbackDataFunctionName,
  88. strlen(psDefUsage->pwszFreeCallbackDataFunctionName) + 1);
  89. }
  90. }
  91. }
  92. RegCloseKey(hKey);
  93. return((hr == ERROR_SUCCESS) ? TRUE : FALSE);
  94. }
  95. BOOL WINAPI WintrustGetDefaultForUsage(DWORD dwAction, const char *pszUsageOID,
  96. CRYPT_PROVIDER_DEFUSAGE *psUsage)
  97. {
  98. BOOL fRet;
  99. HKEY hKey;
  100. WCHAR wsz[REG_MAX_KEY_NAME];
  101. char szFunc[REG_MAX_FUNC_NAME];
  102. WCHAR wszUsage[REG_MAX_FUNC_NAME];
  103. WCHAR wszGuid[REG_MAX_GUID_TEXT];
  104. DWORD dwType;
  105. DWORD dwSize;
  106. HINSTANCE hDll;
  107. PFN_ALLOCANDFILLDEFUSAGE pfnAlloc;
  108. PFN_FREEDEFUSAGE pfnFree;
  109. int cchUsage;
  110. fRet = TRUE;
  111. hKey = NULL;
  112. hDll = NULL;
  113. pfnAlloc = NULL;
  114. pfnFree = NULL;
  115. if (!(pszUsageOID) ||
  116. !(psUsage) ||
  117. !(_ISINSTRUCT(CRYPT_PROVIDER_DEFUSAGE,
  118. psUsage->cbStruct,
  119. pDefSIPClientData)))
  120. {
  121. goto InvalidParamError;
  122. }
  123. memset(&psUsage->gActionID, 0x00, sizeof(GUID));
  124. wszUsage[0] = L'\0';
  125. cchUsage = MultiByteToWideChar(0, 0, pszUsageOID, -1, &wszUsage[0],
  126. REG_MAX_FUNC_NAME);
  127. if (0 >= cchUsage)
  128. {
  129. goto InvalidParamError;
  130. }
  131. if (REG_MAX_KEY_NAME < wcslen(REG_TRUST_USAGE_KEY) + 1 + cchUsage + 1)
  132. {
  133. goto InvalidParamError;
  134. }
  135. wcscpy(&wsz[0], REG_TRUST_USAGE_KEY);
  136. wcscat(&wsz[0], L"\\");
  137. wcscat(&wsz[0], &wszUsage[0]);
  138. if (RegOpenKeyExU( HKEY_LOCAL_MACHINE,
  139. &wsz[0],
  140. 0,
  141. KEY_READ,
  142. &hKey) != ERROR_SUCCESS)
  143. {
  144. goto RegOpenError;
  145. }
  146. //
  147. // get the dll name and function entry points
  148. //
  149. dwType = 0;
  150. dwSize = REG_MAX_KEY_NAME * sizeof(WCHAR);
  151. if (RegQueryValueExU(hKey, REG_DLL_NAME, NULL, &dwType, (BYTE *)&wsz[0], &dwSize) == ERROR_SUCCESS)
  152. {
  153. if (hDll = LoadLibraryU(&wsz[0]))
  154. {
  155. dwType = 0;
  156. dwSize = REG_MAX_FUNC_NAME;
  157. if (RegQueryValueExA(hKey,
  158. (dwAction == DWACTION_FREE) ? REG_DEF_CALLBACK_FREE : REG_DEF_CALLBACK_ALLOC,
  159. NULL, &dwType, (BYTE *)&szFunc[0], &dwSize) == ERROR_SUCCESS)
  160. {
  161. if (dwAction == DWACTION_FREE)
  162. {
  163. pfnFree = (PFN_FREEDEFUSAGE)GetProcAddress(hDll, &szFunc[0]);
  164. if (pfnFree)
  165. {
  166. (*pfnFree)(pszUsageOID, psUsage);
  167. }
  168. fRet = TRUE;
  169. goto CommonReturn;
  170. }
  171. pfnAlloc = (PFN_ALLOCANDFILLDEFUSAGE)GetProcAddress(hDll, &szFunc[0]);
  172. }
  173. }
  174. }
  175. if (dwAction != DWACTION_ALLOCANDFILL)
  176. {
  177. goto CommonReturn;
  178. }
  179. dwType = 0;
  180. dwSize = REG_MAX_GUID_TEXT * sizeof(WCHAR);
  181. wszGuid[0] = NULL;
  182. if (RegQueryValueExU( hKey,
  183. REG_DEF_FOR_USAGE,
  184. NULL,
  185. &dwType,
  186. (BYTE *)&wszGuid[0],
  187. &dwSize) != ERROR_SUCCESS)
  188. {
  189. goto RegQueryError;
  190. }
  191. if (!(wstr2guid(&wszGuid[0], &psUsage->gActionID)))
  192. {
  193. goto GuidError;
  194. }
  195. if (pfnAlloc)
  196. {
  197. if (!(*pfnAlloc)(pszUsageOID, psUsage))
  198. {
  199. goto UsageAllocError;
  200. }
  201. }
  202. CommonReturn:
  203. if (hKey)
  204. {
  205. RegCloseKey(hKey);
  206. }
  207. if (hDll)
  208. {
  209. FreeLibrary(hDll);
  210. }
  211. return(fRet);
  212. ErrorReturn:
  213. fRet = FALSE;
  214. goto CommonReturn;
  215. TRACE_ERROR_EX(DBG_SS, RegOpenError);
  216. TRACE_ERROR_EX(DBG_SS, RegQueryError);
  217. TRACE_ERROR_EX(DBG_SS, GuidError);
  218. TRACE_ERROR_EX(DBG_SS, UsageAllocError);
  219. SET_ERROR_VAR_EX(DBG_SS, InvalidParamError, ERROR_INVALID_PARAMETER);
  220. }