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.

440 lines
10 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1996 - 1999
  6. //
  7. // File: sipguid.cpp
  8. //
  9. // Contents: Microsoft Internet Security SIP Provider
  10. //
  11. // Functions: CryptSIPRetrieveSubjectGuid
  12. //
  13. // *** local functions ***
  14. // _DetermineWhichPE
  15. // _QueryLoadedIsMyFileType
  16. // _QueryRegisteredIsMyFileType
  17. //
  18. // History: 03-Jun-1997 pberkman created
  19. //
  20. //--------------------------------------------------------------------------
  21. #include "global.hxx"
  22. #include "cryptreg.h"
  23. #include "sipbase.h"
  24. #include "mssip.h"
  25. #include "mscat.h"
  26. #include "sipguids.h" // located in pki-mssip32
  27. BOOL _FindGuidFromMagicNumber(BYTE *pszMN, GUID *pgRet);
  28. BOOL _DetermineWhichPE(BYTE *pbFile, DWORD cbFile, GUID *pgRet);
  29. static inline DWORD FourBToDWORD(BYTE rgb[])
  30. {
  31. return ((DWORD)rgb[0]<<24) |
  32. ((DWORD)rgb[1]<<16) |
  33. ((DWORD)rgb[2]<<8) |
  34. ((DWORD)rgb[3]<<0);
  35. }
  36. static inline void DWORDToFourB(DWORD dwIn, BYTE *pszOut)
  37. {
  38. pszOut[0] = (BYTE)((dwIn >> 24) & 0x000000FF);
  39. pszOut[1] = (BYTE)((dwIn >> 16) & 0x000000FF);
  40. pszOut[2] = (BYTE)((dwIn >> 8) & 0x000000FF);
  41. pszOut[3] = (BYTE)( dwIn & 0x000000FF);
  42. }
  43. #define PE_EXE_HEADER_TAG "MZ"
  44. #define CAB_MAGIC_NUMBER "MSCF"
  45. BOOL WINAPI CryptSIPRetrieveSubjectGuid(IN LPCWSTR FileName, IN OPTIONAL HANDLE hFileIn, OUT GUID *pgSubject)
  46. {
  47. BYTE *pbFile;
  48. DWORD cbFile;
  49. DWORD dwCheck;
  50. HANDLE hMappedFile;
  51. BOOL bCloseFile;
  52. BOOL fRet;
  53. DWORD dwException=0;
  54. PCCTL_CONTEXT pCTLContext=NULL;
  55. bCloseFile = FALSE;
  56. pbFile = NULL;
  57. fRet = TRUE;
  58. if (!(pgSubject))
  59. {
  60. goto InvalidParameter;
  61. }
  62. memset(pgSubject, 0x00, sizeof(GUID));
  63. if ((hFileIn == NULL) || (hFileIn == INVALID_HANDLE_VALUE))
  64. {
  65. if (!(FileName))
  66. {
  67. goto InvalidParameter;
  68. }
  69. if ((hFileIn = CreateFileU(FileName, GENERIC_READ, FILE_SHARE_READ, NULL,
  70. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
  71. NULL)) == INVALID_HANDLE_VALUE)
  72. {
  73. goto FileOpenError;
  74. }
  75. bCloseFile = TRUE;
  76. }
  77. hMappedFile = CreateFileMapping(hFileIn, NULL, PAGE_READONLY, 0, 0, NULL);
  78. if (!(hMappedFile) || (hMappedFile == INVALID_HANDLE_VALUE))
  79. {
  80. goto FileMapError;
  81. }
  82. pbFile = (BYTE *)MapViewOfFile(hMappedFile, FILE_MAP_READ, 0, 0, 0);
  83. CloseHandle(hMappedFile);
  84. cbFile = GetFileSize(hFileIn, NULL);
  85. if (cbFile < SIP_MAX_MAGIC_NUMBER)
  86. {
  87. goto FileSizeError;
  88. }
  89. //we need to check for the pbFile
  90. if(NULL == pbFile)
  91. goto FileMapError;
  92. //we need to handle the exception when we access the mapped file
  93. __try {
  94. //
  95. // PE
  96. //
  97. if (memcmp(&pbFile[0], PE_EXE_HEADER_TAG, strlen(PE_EXE_HEADER_TAG)) == 0)
  98. {
  99. //
  100. // if it is an Exe, Dll, Ocx, etc. make sure it is a 32 bit PE and set the
  101. // "internal" magic number.
  102. //
  103. if (_DetermineWhichPE(pbFile, cbFile, pgSubject))
  104. {
  105. goto CommonReturn;
  106. }
  107. }
  108. //
  109. // CAB
  110. //
  111. if (memcmp(&pbFile[0], CAB_MAGIC_NUMBER, strlen(CAB_MAGIC_NUMBER)) == 0)
  112. {
  113. GUID gCAB = CRYPT_SUBJTYPE_CABINET_IMAGE;
  114. memcpy(pgSubject, &gCAB, sizeof(GUID));
  115. goto CommonReturn;
  116. }
  117. //
  118. // JAVA Class
  119. //
  120. dwCheck = FourBToDWORD(&pbFile[0]);
  121. if (dwCheck == 0xCAFEBABE)
  122. {
  123. GUID gJClass = CRYPT_SUBJTYPE_JAVACLASS_IMAGE;
  124. memcpy(pgSubject, &gJClass, sizeof(GUID));
  125. goto CommonReturn;
  126. }
  127. //
  128. // Catalog/CTL
  129. //
  130. if (pbFile[0] == 0x30) // could be a PKCS#7!
  131. {
  132. //
  133. // we could be a PKCS7.... check for CTL
  134. //
  135. pCTLContext = (PCCTL_CONTEXT) CertCreateContext(
  136. CERT_STORE_CTL_CONTEXT,
  137. PKCS_7_ASN_ENCODING | X509_ASN_ENCODING,
  138. pbFile,
  139. cbFile,
  140. CERT_CREATE_CONTEXT_NOCOPY_FLAG |
  141. CERT_CREATE_CONTEXT_NO_HCRYPTMSG_FLAG |
  142. CERT_CREATE_CONTEXT_NO_ENTRY_FLAG,
  143. NULL // pCreatePara
  144. );
  145. if (pCTLContext)
  146. {
  147. if (pCTLContext->pCtlInfo->SubjectUsage.cUsageIdentifier)
  148. {
  149. char *pszCatalogListUsageOID = szOID_CATALOG_LIST;
  150. if (strcmp(pCTLContext->pCtlInfo->SubjectUsage.rgpszUsageIdentifier[0],
  151. pszCatalogListUsageOID) == 0)
  152. {
  153. GUID gCat = CRYPT_SUBJTYPE_CATALOG_IMAGE;
  154. memcpy(pgSubject, &gCat, sizeof(GUID));
  155. CertFreeCTLContext(pCTLContext);
  156. pCTLContext=NULL;
  157. goto CommonReturn;
  158. }
  159. }
  160. //
  161. // otherwise, it is a CTL of some other type...
  162. //
  163. GUID gCTL = CRYPT_SUBJTYPE_CTL_IMAGE;
  164. memcpy(pgSubject, &gCTL, sizeof(GUID));
  165. CertFreeCTLContext(pCTLContext);
  166. pCTLContext=NULL;
  167. goto CommonReturn;
  168. }
  169. }
  170. //we need to unmap the file
  171. if(pbFile)
  172. {
  173. UnmapViewOfFile(pbFile);
  174. pbFile=NULL;
  175. }
  176. //
  177. // none that we know about... Check the providers...
  178. //
  179. if (_QueryRegisteredIsMyFileType(hFileIn, FileName, pgSubject))
  180. {
  181. goto CommonReturn;
  182. }
  183. } __except(EXCEPTION_EXECUTE_HANDLER) {
  184. dwException = GetExceptionCode();
  185. goto ExceptionError;
  186. }
  187. //
  188. // cant find any provider to support this file type...
  189. //
  190. goto NoSIPProviderFound;
  191. CommonReturn:
  192. //we need to handle the exception when we access the mapped file
  193. if (pbFile)
  194. {
  195. UnmapViewOfFile(pbFile);
  196. }
  197. if(pCTLContext)
  198. {
  199. CertFreeCTLContext(pCTLContext);
  200. }
  201. if ((hFileIn) && (hFileIn != INVALID_HANDLE_VALUE))
  202. {
  203. if (bCloseFile)
  204. {
  205. CloseHandle(hFileIn);
  206. }
  207. }
  208. return(fRet);
  209. ErrorReturn:
  210. fRet = FALSE;
  211. goto CommonReturn;
  212. SET_ERROR_VAR_EX(DBG_SS, InvalidParameter, ERROR_INVALID_PARAMETER);
  213. SET_ERROR_VAR_EX(DBG_SS, NoSIPProviderFound, TRUST_E_SUBJECT_FORM_UNKNOWN);
  214. SET_ERROR_VAR_EX(DBG_SS, FileOpenError, GetLastError());
  215. SET_ERROR_VAR_EX(DBG_SS, FileMapError, GetLastError());
  216. SET_ERROR_VAR_EX(DBG_SS, FileSizeError, ERROR_INVALID_PARAMETER);
  217. SET_ERROR_VAR_EX(DBG_SS, ExceptionError, dwException);
  218. }
  219. BOOL _DetermineWhichPE(BYTE *pbFile, DWORD cbFile, GUID *pgRet)
  220. {
  221. IMAGE_DOS_HEADER *pDosHead;
  222. pDosHead = (IMAGE_DOS_HEADER *)pbFile;
  223. if (pDosHead->e_magic == IMAGE_DOS_SIGNATURE)
  224. {
  225. if (cbFile >= sizeof(IMAGE_DOS_HEADER))
  226. {
  227. if (cbFile >= (sizeof(IMAGE_DOS_HEADER) + pDosHead->e_lfanew))
  228. {
  229. IMAGE_NT_HEADERS *pNTHead;
  230. pNTHead = (IMAGE_NT_HEADERS *)((ULONG_PTR)pDosHead + pDosHead->e_lfanew);
  231. if (pNTHead->Signature == IMAGE_NT_SIGNATURE)
  232. {
  233. GUID gPE = CRYPT_SUBJTYPE_PE_IMAGE;
  234. memcpy(pgRet, &gPE, sizeof(GUID));
  235. return(TRUE);
  236. }
  237. }
  238. }
  239. }
  240. return(FALSE);
  241. }
  242. BOOL WINAPI CryptSIPRetrieveSubjectGuidForCatalogFile(IN LPCWSTR FileName, IN OPTIONAL HANDLE hFileIn, OUT GUID *pgSubject)
  243. {
  244. BYTE *pbFile;
  245. DWORD cbFile;
  246. HANDLE hMappedFile;
  247. BOOL bCloseFile;
  248. BOOL fRet;
  249. DWORD dwException = 0;
  250. GUID gFlat = CRYPT_SUBJTYPE_FLAT_IMAGE;
  251. bCloseFile = FALSE;
  252. pbFile = NULL;
  253. fRet = TRUE;
  254. if (!(pgSubject))
  255. {
  256. goto InvalidParameter;
  257. }
  258. memset(pgSubject, 0x00, sizeof(GUID));
  259. if ((hFileIn == NULL) || (hFileIn == INVALID_HANDLE_VALUE))
  260. {
  261. if (!(FileName))
  262. {
  263. goto InvalidParameter;
  264. }
  265. if ((hFileIn = CreateFileU(FileName, GENERIC_READ, FILE_SHARE_READ, NULL,
  266. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
  267. NULL)) == INVALID_HANDLE_VALUE)
  268. {
  269. goto FileOpenError;
  270. }
  271. bCloseFile = TRUE;
  272. }
  273. hMappedFile = CreateFileMapping(hFileIn, NULL, PAGE_READONLY, 0, 0, NULL);
  274. if (!(hMappedFile) || (hMappedFile == INVALID_HANDLE_VALUE))
  275. {
  276. goto FileMapError;
  277. }
  278. pbFile = (BYTE *)MapViewOfFile(hMappedFile, FILE_MAP_READ, 0, 0, 0);
  279. CloseHandle(hMappedFile);
  280. cbFile = GetFileSize(hFileIn, NULL);
  281. if (cbFile < SIP_MAX_MAGIC_NUMBER)
  282. {
  283. goto FlatFile;
  284. }
  285. //we need to check for the pbFile
  286. if(NULL == pbFile)
  287. goto FileMapError;
  288. //we need to handle the exception when we access the mapped file
  289. __try {
  290. //
  291. // PE
  292. //
  293. if (memcmp(&pbFile[0], PE_EXE_HEADER_TAG, strlen(PE_EXE_HEADER_TAG)) == 0)
  294. {
  295. //
  296. // if it is an Exe, Dll, Ocx, etc. make sure it is a 32 bit PE and set the
  297. // "internal" magic number.
  298. //
  299. if (_DetermineWhichPE(pbFile, cbFile, pgSubject))
  300. {
  301. goto CommonReturn;
  302. }
  303. }
  304. //
  305. // CAB
  306. //
  307. if (memcmp(&pbFile[0], CAB_MAGIC_NUMBER, strlen(CAB_MAGIC_NUMBER)) == 0)
  308. {
  309. GUID gCAB = CRYPT_SUBJTYPE_CABINET_IMAGE;
  310. memcpy(pgSubject, &gCAB, sizeof(GUID));
  311. goto CommonReturn;
  312. }
  313. } __except(EXCEPTION_EXECUTE_HANDLER) {
  314. dwException = GetExceptionCode();
  315. goto ExceptionError;
  316. }
  317. //
  318. // Not PE, so go for flat
  319. //
  320. FlatFile:
  321. memcpy(pgSubject, &gFlat, sizeof(GUID));
  322. CommonReturn:
  323. __try {
  324. if (pbFile)
  325. {
  326. UnmapViewOfFile(pbFile);
  327. }
  328. } __except(EXCEPTION_EXECUTE_HANDLER) {
  329. // can't really do anything
  330. }
  331. if ((hFileIn) && (hFileIn != INVALID_HANDLE_VALUE))
  332. {
  333. if (bCloseFile)
  334. {
  335. CloseHandle(hFileIn);
  336. }
  337. }
  338. return(fRet);
  339. ErrorReturn:
  340. fRet = FALSE;
  341. goto CommonReturn;
  342. SET_ERROR_VAR_EX(DBG_SS, InvalidParameter, ERROR_INVALID_PARAMETER);
  343. SET_ERROR_VAR_EX(DBG_SS, FileOpenError, GetLastError());
  344. SET_ERROR_VAR_EX(DBG_SS, FileMapError, GetLastError());
  345. SET_ERROR_VAR_EX(DBG_SS, ExceptionError, dwException);
  346. }