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.

552 lines
14 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1996 - 1999
  6. //
  7. // File: sipfuncs.cpp
  8. //
  9. // Contents: Microsoft Internet Security SIP Provider
  10. //
  11. // Functions: CryptSIPDllMain
  12. // CryptSIPPutSignedDataMsg
  13. // CryptSIPGetSignedDataMsg
  14. // CryptSIPRemoveSignedDataMsg
  15. // CryptSIPCreateIndirectData
  16. // CryptSIPVerifyIndirectData
  17. //
  18. // *** local functions ***
  19. // _Guid2Sz
  20. // _LoadIsFuncs
  21. // _EnumOIDCallback
  22. //
  23. // History: 01-Dec-1997 pberkman created
  24. //
  25. //--------------------------------------------------------------------------
  26. #include "global.hxx"
  27. HCRYPTOIDFUNCSET hPutFuncSet;
  28. HCRYPTOIDFUNCSET hGetFuncSet;
  29. HCRYPTOIDFUNCSET hRemoveFuncSet;
  30. HCRYPTOIDFUNCSET hCreateFuncSet;
  31. HCRYPTOIDFUNCSET hVerifyFuncSet;
  32. HCRYPTOIDFUNCSET hIsMyFileFuncSet;
  33. HCRYPTOIDFUNCSET hIsMyFileFuncSet2;
  34. CRITICAL_SECTION SIP_CriticalSection;
  35. BOOL fLoadedIsFuncs = FALSE;
  36. typedef struct ISFUNCREF_
  37. {
  38. DWORD cGuids;
  39. GUID *pGuids;
  40. } ISFUNCREF;
  41. ISFUNCREF sIsGuids;
  42. ISFUNCREF sIsGuids2;
  43. void _LoadIsFuncs(void);
  44. BOOL _CallIsMyFileType2(GUID *pgIn, WCHAR *pwszFile, GUID *pgOut);
  45. BOOL _CallIsMyFileType(GUID *pgIn, HANDLE hFile, GUID *pgOut);
  46. BOOL WINAPI _EnumOIDCallback(DWORD dwEncodingType, LPCSTR pszFuncName, LPCSTR pszOID,
  47. DWORD cValue, const DWORD rgdwValueType[], LPCWSTR const rgpwszValueName[],
  48. const BYTE * const rgpbValueData[], const DWORD rgcbValueData[],
  49. void *pvArg);
  50. BOOL WINAPI CryptSIPDllMain(HMODULE hModule, DWORD fdwReason, LPVOID lpReserved)
  51. {
  52. BOOL fRet;
  53. fRet = TRUE;
  54. switch (fdwReason)
  55. {
  56. case DLL_PROCESS_ATTACH:
  57. memset(&sIsGuids, 0x00, sizeof(ISFUNCREF));
  58. memset(&sIsGuids2, 0x00, sizeof(ISFUNCREF));
  59. if (!(hPutFuncSet = CryptInitOIDFunctionSet(SIPFUNC_PUTSIGNATURE, 0)))
  60. {
  61. goto PutFuncSetFailed;
  62. }
  63. if (!(hGetFuncSet = CryptInitOIDFunctionSet(SIPFUNC_GETSIGNATURE, 0)))
  64. {
  65. goto GetFuncSetFailed;
  66. }
  67. if (!(hRemoveFuncSet = CryptInitOIDFunctionSet(SIPFUNC_REMSIGNATURE, 0)))
  68. {
  69. goto RemoveFuncSetFailed;
  70. }
  71. if (!(hCreateFuncSet = CryptInitOIDFunctionSet(SIPFUNC_CREATEINDIRECT, 0)))
  72. {
  73. goto CreateFuncSetFailed;
  74. }
  75. if (!(hVerifyFuncSet = CryptInitOIDFunctionSet(SIPFUNC_VERIFYINDIRECT, 0)))
  76. {
  77. goto VerifyFuncSetFailed;
  78. }
  79. if (!(hIsMyFileFuncSet = CryptInitOIDFunctionSet(SIPFUNC_ISMYTYPE, 0)))
  80. {
  81. goto VerifyFuncSetFailed;
  82. }
  83. if (!(hIsMyFileFuncSet2 = CryptInitOIDFunctionSet(SIPFUNC_ISMYTYPE2, 0)))
  84. {
  85. goto VerifyFuncSetFailed;
  86. }
  87. if (!Pki_InitializeCriticalSection(&SIP_CriticalSection))
  88. {
  89. goto InitCritSectionFailed;
  90. }
  91. break;
  92. case DLL_PROCESS_DETACH:
  93. if (fLoadedIsFuncs)
  94. {
  95. EnterCriticalSection(&SIP_CriticalSection);
  96. DELETE_OBJECT(sIsGuids.pGuids);
  97. DELETE_OBJECT(sIsGuids2.pGuids);
  98. sIsGuids.cGuids = 0;
  99. sIsGuids2.cGuids = 0;
  100. fLoadedIsFuncs = FALSE;
  101. LeaveCriticalSection(&SIP_CriticalSection);
  102. }
  103. DeleteCriticalSection(&SIP_CriticalSection);
  104. break;
  105. }
  106. CommonReturn:
  107. return(fRet);
  108. ErrorReturn:
  109. fRet = FALSE;
  110. goto CommonReturn;
  111. TRACE_ERROR_EX(DBG_SS, InitCritSectionFailed);
  112. TRACE_ERROR_EX(DBG_SS, PutFuncSetFailed);
  113. TRACE_ERROR_EX(DBG_SS, GetFuncSetFailed);
  114. TRACE_ERROR_EX(DBG_SS, RemoveFuncSetFailed);
  115. TRACE_ERROR_EX(DBG_SS, CreateFuncSetFailed);
  116. TRACE_ERROR_EX(DBG_SS, VerifyFuncSetFailed);
  117. }
  118. BOOL WINAPI CryptSIPPutSignedDataMsg(SIP_SUBJECTINFO *pSubjectInfo,
  119. DWORD dwEncodingType,
  120. DWORD *pdwIndex,
  121. DWORD cbSignedDataMsg,
  122. BYTE *pbSignedDataMsg)
  123. {
  124. BOOL fRet;
  125. if (!(pSubjectInfo) ||
  126. !(pSubjectInfo->pgSubjectType))
  127. {
  128. goto InvalidParam;
  129. }
  130. HCRYPTOIDFUNCADDR hPfn;
  131. pCryptSIPPutSignedDataMsg pfn;
  132. char szGuid[REG_MAX_GUID_TEXT];
  133. if (!(_Guid2Sz(pSubjectInfo->pgSubjectType, &szGuid[0])))
  134. {
  135. goto GuidConvertFailed;
  136. }
  137. if (!(CryptGetOIDFunctionAddress(hPutFuncSet, 0, &szGuid[0], 0, (void **)&pfn, &hPfn)))
  138. {
  139. goto GetFuncAddrFailed;
  140. }
  141. fRet = pfn(pSubjectInfo, dwEncodingType, pdwIndex, cbSignedDataMsg, pbSignedDataMsg);
  142. CryptFreeOIDFunctionAddress(hPfn, 0);
  143. CommonReturn:
  144. return(fRet);
  145. ErrorReturn:
  146. fRet = FALSE;
  147. goto CommonReturn;
  148. SET_ERROR_VAR_EX(DBG_SS, GetFuncAddrFailed, TRUST_E_SUBJECT_FORM_UNKNOWN);
  149. SET_ERROR_VAR_EX(DBG_SS, GuidConvertFailed, TRUST_E_SUBJECT_FORM_UNKNOWN);
  150. SET_ERROR_VAR_EX(DBG_SS, InvalidParam, ERROR_INVALID_PARAMETER);
  151. }
  152. BOOL WINAPI CryptSIPGetSignedDataMsg(SIP_SUBJECTINFO *pSubjectInfo,
  153. DWORD *pdwEncodingType,
  154. DWORD dwIndex,
  155. DWORD *pcbSignedDataMsg,
  156. BYTE *pbSignedDataMsg)
  157. {
  158. BOOL fRet;
  159. if (!(pSubjectInfo) ||
  160. !(pSubjectInfo->pgSubjectType))
  161. {
  162. goto InvalidParam;
  163. }
  164. HCRYPTOIDFUNCADDR hPfn;
  165. pCryptSIPGetSignedDataMsg pfn;
  166. char szGuid[REG_MAX_GUID_TEXT];
  167. if (!(_Guid2Sz(pSubjectInfo->pgSubjectType, &szGuid[0])))
  168. {
  169. goto GuidConvertFailed;
  170. }
  171. if (!(CryptGetOIDFunctionAddress(hGetFuncSet, 0, &szGuid[0], 0, (void **)&pfn, &hPfn)))
  172. {
  173. goto GetFuncAddrFailed;
  174. }
  175. fRet = pfn(pSubjectInfo, pdwEncodingType, dwIndex, pcbSignedDataMsg, pbSignedDataMsg);
  176. CryptFreeOIDFunctionAddress(hPfn, 0);
  177. CommonReturn:
  178. return(fRet);
  179. ErrorReturn:
  180. fRet = FALSE;
  181. goto CommonReturn;
  182. SET_ERROR_VAR_EX(DBG_SS, GetFuncAddrFailed, TRUST_E_SUBJECT_FORM_UNKNOWN);
  183. SET_ERROR_VAR_EX(DBG_SS, GuidConvertFailed, TRUST_E_SUBJECT_FORM_UNKNOWN);
  184. SET_ERROR_VAR_EX(DBG_SS, InvalidParam, ERROR_INVALID_PARAMETER);
  185. }
  186. BOOL WINAPI CryptSIPRemoveSignedDataMsg(SIP_SUBJECTINFO *pSubjectInfo,
  187. DWORD dwIndex)
  188. {
  189. BOOL fRet;
  190. if (!(pSubjectInfo) ||
  191. !(pSubjectInfo->pgSubjectType))
  192. {
  193. goto InvalidParam;
  194. }
  195. HCRYPTOIDFUNCADDR hPfn;
  196. pCryptSIPRemoveSignedDataMsg pfn;
  197. char szGuid[REG_MAX_GUID_TEXT];
  198. if (!(_Guid2Sz(pSubjectInfo->pgSubjectType, &szGuid[0])))
  199. {
  200. goto GuidConvertFailed;
  201. }
  202. if (!(CryptGetOIDFunctionAddress(hRemoveFuncSet, 0, &szGuid[0], 0, (void **)&pfn, &hPfn)))
  203. {
  204. goto GetFuncAddrFailed;
  205. }
  206. fRet = pfn(pSubjectInfo, dwIndex);
  207. CryptFreeOIDFunctionAddress(hPfn, 0);
  208. CommonReturn:
  209. return(fRet);
  210. ErrorReturn:
  211. fRet = FALSE;
  212. goto CommonReturn;
  213. SET_ERROR_VAR_EX(DBG_SS, GetFuncAddrFailed, TRUST_E_SUBJECT_FORM_UNKNOWN);
  214. SET_ERROR_VAR_EX(DBG_SS, GuidConvertFailed, TRUST_E_SUBJECT_FORM_UNKNOWN);
  215. SET_ERROR_VAR_EX(DBG_SS, InvalidParam, ERROR_INVALID_PARAMETER);
  216. }
  217. BOOL WINAPI CryptSIPCreateIndirectData(SIP_SUBJECTINFO *pSubjectInfo,
  218. DWORD *pcbIndirectData,
  219. SIP_INDIRECT_DATA *pIndirectData)
  220. {
  221. BOOL fRet;
  222. if (!(pSubjectInfo) ||
  223. !(pSubjectInfo->pgSubjectType))
  224. {
  225. goto InvalidParam;
  226. }
  227. HCRYPTOIDFUNCADDR hPfn;
  228. pCryptSIPCreateIndirectData pfn;
  229. char szGuid[REG_MAX_GUID_TEXT];
  230. if (!(_Guid2Sz(pSubjectInfo->pgSubjectType, &szGuid[0])))
  231. {
  232. goto GuidConvertFailed;
  233. }
  234. if (!(CryptGetOIDFunctionAddress(hCreateFuncSet, 0, &szGuid[0], 0, (void **)&pfn, &hPfn)))
  235. {
  236. goto GetFuncAddrFailed;
  237. }
  238. fRet = pfn(pSubjectInfo, pcbIndirectData, pIndirectData);
  239. CryptFreeOIDFunctionAddress(hPfn, 0);
  240. CommonReturn:
  241. return(fRet);
  242. ErrorReturn:
  243. fRet = FALSE;
  244. goto CommonReturn;
  245. SET_ERROR_VAR_EX(DBG_SS, GetFuncAddrFailed, TRUST_E_SUBJECT_FORM_UNKNOWN);
  246. SET_ERROR_VAR_EX(DBG_SS, GuidConvertFailed, TRUST_E_SUBJECT_FORM_UNKNOWN);
  247. SET_ERROR_VAR_EX(DBG_SS, InvalidParam, ERROR_INVALID_PARAMETER);
  248. }
  249. BOOL WINAPI CryptSIPVerifyIndirectData(SIP_SUBJECTINFO *pSubjectInfo,
  250. SIP_INDIRECT_DATA *pIndirectData)
  251. {
  252. BOOL fRet;
  253. if (!(pSubjectInfo) ||
  254. !(pSubjectInfo->pgSubjectType))
  255. {
  256. goto InvalidParam;
  257. }
  258. HCRYPTOIDFUNCADDR hPfn;
  259. pCryptSIPVerifyIndirectData pfn;
  260. char szGuid[REG_MAX_GUID_TEXT];
  261. if (!(_Guid2Sz(pSubjectInfo->pgSubjectType, &szGuid[0])))
  262. {
  263. goto GuidConvertFailed;
  264. }
  265. if (!(CryptGetOIDFunctionAddress(hVerifyFuncSet, 0, &szGuid[0], 0, (void **)&pfn, &hPfn)))
  266. {
  267. goto GetFuncAddrFailed;
  268. }
  269. fRet = pfn(pSubjectInfo, pIndirectData);
  270. CryptFreeOIDFunctionAddress(hPfn, 0);
  271. CommonReturn:
  272. return(fRet);
  273. ErrorReturn:
  274. fRet = FALSE;
  275. goto CommonReturn;
  276. SET_ERROR_VAR_EX(DBG_SS, GetFuncAddrFailed, TRUST_E_SUBJECT_FORM_UNKNOWN);
  277. SET_ERROR_VAR_EX(DBG_SS, GuidConvertFailed, TRUST_E_SUBJECT_FORM_UNKNOWN);
  278. SET_ERROR_VAR_EX(DBG_SS, InvalidParam, ERROR_INVALID_PARAMETER);
  279. }
  280. BOOL _Guid2Sz(GUID *pgGuid, char *pszGuid)
  281. {
  282. WCHAR wszGuid[REG_MAX_GUID_TEXT];
  283. if (!(guid2wstr(pgGuid, &wszGuid[0])))
  284. {
  285. return(FALSE);
  286. }
  287. if (WideCharToMultiByte(0, 0, &wszGuid[0], -1, pszGuid, REG_MAX_GUID_TEXT, NULL, NULL) == 0)
  288. {
  289. return(FALSE);
  290. }
  291. return(TRUE);
  292. }
  293. BOOL _QueryRegisteredIsMyFileType(HANDLE hFile, LPCWSTR pwszFile, GUID *pgSubject)
  294. {
  295. if (!(fLoadedIsFuncs))
  296. {
  297. EnterCriticalSection(&SIP_CriticalSection);
  298. if (!(fLoadedIsFuncs))
  299. {
  300. CryptEnumOIDFunction(0, SIPFUNC_ISMYTYPE, NULL, 0, (void *)&sIsGuids, _EnumOIDCallback);
  301. CryptEnumOIDFunction(0, SIPFUNC_ISMYTYPE2, NULL, 0, (void *)&sIsGuids2, _EnumOIDCallback);
  302. fLoadedIsFuncs = TRUE;
  303. }
  304. LeaveCriticalSection(&SIP_CriticalSection);
  305. }
  306. DWORD i;
  307. i = 0;
  308. while (i < sIsGuids.cGuids)
  309. {
  310. if (_CallIsMyFileType(&sIsGuids.pGuids[i], hFile, pgSubject))
  311. {
  312. return(TRUE);
  313. }
  314. i++;
  315. }
  316. i = 0;
  317. while (i < sIsGuids2.cGuids)
  318. {
  319. if (_CallIsMyFileType2(&sIsGuids2.pGuids[i], (WCHAR *)pwszFile, pgSubject))
  320. {
  321. return(TRUE);
  322. }
  323. i++;
  324. }
  325. return(FALSE);
  326. }
  327. BOOL _CallIsMyFileType(GUID *pgIn, HANDLE hFile, GUID *pgOut)
  328. {
  329. BOOL fRet;
  330. HCRYPTOIDFUNCADDR hPfn;
  331. pfnIsFileSupported pfn;
  332. char szGuid[REG_MAX_GUID_TEXT];
  333. if (!(_Guid2Sz(pgIn, &szGuid[0])))
  334. {
  335. goto GuidConvertFailed;
  336. }
  337. if (!(CryptGetOIDFunctionAddress(hIsMyFileFuncSet, 0, &szGuid[0], 0, (void **)&pfn, &hPfn)))
  338. {
  339. goto GetFuncAddrFailed;
  340. }
  341. fRet = pfn(hFile, pgOut);
  342. CryptFreeOIDFunctionAddress(hPfn, 0);
  343. CommonReturn:
  344. return(fRet);
  345. ErrorReturn:
  346. fRet = FALSE;
  347. goto CommonReturn;
  348. TRACE_ERROR_EX(DBG_SS, GetFuncAddrFailed);
  349. TRACE_ERROR_EX(DBG_SS, GuidConvertFailed);
  350. }
  351. BOOL _CallIsMyFileType2(GUID *pgIn, WCHAR *pwszFile, GUID *pgOut)
  352. {
  353. BOOL fRet;
  354. HCRYPTOIDFUNCADDR hPfn;
  355. pfnIsFileSupported pfn;
  356. char szGuid[REG_MAX_GUID_TEXT];
  357. if (!(_Guid2Sz(pgIn, &szGuid[0])))
  358. {
  359. goto GuidConvertFailed;
  360. }
  361. if (!(CryptGetOIDFunctionAddress(hIsMyFileFuncSet2, 0, &szGuid[0], 0, (void **)&pfn, &hPfn)))
  362. {
  363. goto GetFuncAddrFailed;
  364. }
  365. fRet = pfn(pwszFile, pgOut);
  366. CryptFreeOIDFunctionAddress(hPfn, 0);
  367. CommonReturn:
  368. return(fRet);
  369. ErrorReturn:
  370. fRet = FALSE;
  371. goto CommonReturn;
  372. TRACE_ERROR_EX(DBG_SS, GetFuncAddrFailed);
  373. TRACE_ERROR_EX(DBG_SS, GuidConvertFailed);
  374. }
  375. void *SIPRealloc(void *pvOrg, size_t cb)
  376. {
  377. void *pv;
  378. pv = (pvOrg) ? realloc(pvOrg, cb) : malloc(cb);
  379. if (!(pv))
  380. {
  381. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  382. }
  383. return(pv);
  384. }
  385. BOOL WINAPI _EnumOIDCallback(IN DWORD dwEncodingType,
  386. IN LPCSTR pszFuncName,
  387. IN LPCSTR pszOID,
  388. IN DWORD cValue,
  389. IN const DWORD rgdwValueType[],
  390. IN LPCWSTR const rgpwszValueName[],
  391. IN const BYTE * const rgpbValueData[],
  392. IN const DWORD rgcbValueData[],
  393. IN void *pvArg)
  394. {
  395. WCHAR wszGuid[REG_MAX_GUID_TEXT];
  396. GUID g;
  397. wszGuid[0] = NULL;
  398. MultiByteToWideChar(0, 0, pszOID, -1, &wszGuid[0], REG_MAX_GUID_TEXT);
  399. if (wszGuid[0])
  400. {
  401. if (wstr2guid(&wszGuid[0], &g))
  402. {
  403. ISFUNCREF *pIsGuids;
  404. pIsGuids = (ISFUNCREF *)pvArg;
  405. if (!(pvArg))
  406. {
  407. return(TRUE);
  408. }
  409. pIsGuids->cGuids++;
  410. pIsGuids->pGuids = (GUID *)SIPRealloc(pIsGuids->pGuids, pIsGuids->cGuids * sizeof(GUID));
  411. if (!(pIsGuids->pGuids))
  412. {
  413. pIsGuids->cGuids = 0;
  414. return(FALSE);
  415. }
  416. memcpy(&pIsGuids->pGuids[pIsGuids->cGuids - 1], &g, sizeof(GUID));
  417. }
  418. }
  419. return(TRUE); // keep going!
  420. }