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.

171 lines
4.6 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1997 - 1999
  6. //
  7. // File: fileguid.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. #include "global.hxx"
  11. #include "sgnerror.h"
  12. //+-----------------------------------------------------------------------
  13. // SignGetFileType
  14. //
  15. // Parameters:
  16. // Return Values:
  17. // Error Codes:
  18. // E_INVALIDARG
  19. // Invalid arguement passed in (Requires a file name
  20. // and pointer to a guid ptr)
  21. // See also:
  22. // GetFileInformationByHandle()
  23. // CreateFile()
  24. //
  25. //------------------------------------------------------------------------
  26. HRESULT SignGetFileType(HANDLE hFile,
  27. const WCHAR *pwszFile,
  28. GUID* pGuid)
  29. // Answer as to the type as which we should sign this file
  30. {
  31. if (!(pGuid) || !(hFile) || (hFile == INVALID_HANDLE_VALUE))
  32. {
  33. return(E_INVALIDARG);
  34. }
  35. if (!(CryptSIPRetrieveSubjectGuid(pwszFile, hFile, pGuid)))
  36. {
  37. return(GetLastError());
  38. }
  39. return(S_OK);
  40. # ifdef PCB_OLD
  41. // Java class files have a magic number at their start. They always begin
  42. // 0xCA 0xFE 0xBA 0xBE
  43. // CAB files begin 'M' 'S' 'C' 'F'
  44. //
  45. if(!pGuid || hFile == NULL || hFile == INVALID_HANDLE_VALUE)
  46. return E_INVALIDARG;
  47. ZeroMemory(pGuid, sizeof(GUID));
  48. PKITRY {
  49. static BYTE rgbMagicJava[] = { 0xCA, 0xFE, 0xBA, 0xBE };
  50. static BYTE rgbMagicCab [] = { 'M', 'S', 'C', 'F' };
  51. BYTE rgbRead[4];
  52. DWORD dwRead;
  53. if (ReadFile(hFile, rgbRead, 4, &dwRead, NULL) &&
  54. dwRead == 4) {
  55. if (memcmp(rgbRead, rgbMagicJava, 4)==0)
  56. *pGuid = JavaImage;
  57. else if (memcmp(rgbRead, rgbMagicCab, 4)==0)
  58. *pGuid = CabImage;
  59. else
  60. *pGuid = PeImage;
  61. }
  62. // Rewind the file
  63. if(SetFilePointer(hFile, 0, 0, FILE_BEGIN) == 0xffffffff)
  64. PKITHROW(SignError());
  65. }
  66. PKICATCH(err) {
  67. hr = err.pkiError;
  68. } PKIEND;
  69. return hr;
  70. # endif // PCB_OLD
  71. }
  72. //Xiaohs: the following function is no longer necessary after auth2upd
  73. /*HRESULT SignLoadSipFlags(GUID* pSubjectGuid,
  74. DWORD *dwFlags)
  75. {
  76. HRESULT hr = S_OK;
  77. GUID sSip;
  78. if(!dwFlags)
  79. return E_INVALIDARG;
  80. if (dwFlags)
  81. {
  82. *dwFlags = 0;
  83. sSip = PeImage;
  84. if(memcmp(&sSip, pSubjectGuid, sizeof(GUID)) == 0)
  85. {
  86. *dwFlags = SPC_INC_PE_RESOURCES_FLAG | SPC_INC_PE_IMPORT_ADDR_TABLE_FLAG;
  87. return hr;
  88. }
  89. }
  90. return hr;
  91. } */
  92. //+-----------------------------------------------------------------------
  93. // FileToSubjectType
  94. //
  95. // Parameters:
  96. // Return Values:
  97. // Error Codes:
  98. // E_INVALIDARG
  99. // Invalid arguement passed in (Requires a file name
  100. // and pointer to a guid ptr)
  101. // TRUST_E_SUBJECT_FORM_UNKNOWN
  102. // Unknow file type
  103. // See also:
  104. // GetFileInformationByHandle()
  105. // CreateFile()
  106. //
  107. //------------------------------------------------------------------------
  108. HRESULT SignOpenFile(LPCWSTR pwszFilename,
  109. HANDLE* pFileHandle)
  110. {
  111. HRESULT hr = S_OK;
  112. HANDLE hFile = NULL;
  113. BY_HANDLE_FILE_INFORMATION hFileInfo;
  114. if(!pwszFilename || !pFileHandle)
  115. return E_INVALIDARG;
  116. PKITRY {
  117. hFile = CreateFileU(pwszFilename,
  118. GENERIC_READ | GENERIC_WRITE,
  119. FILE_SHARE_READ,
  120. NULL, // lpsa
  121. OPEN_EXISTING,
  122. FILE_ATTRIBUTE_NORMAL,
  123. NULL); // hTemplateFile
  124. if(INVALID_HANDLE_VALUE == hFile)
  125. PKITHROW(SignError());
  126. if(!GetFileInformationByHandle(hFile,
  127. &hFileInfo))
  128. PKITHROW(SignError());
  129. // Test to see if we have a directory or offline
  130. if( (hFileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
  131. (hFileInfo.dwFileAttributes & FILE_ATTRIBUTE_OFFLINE) )
  132. {
  133. PKITHROW(TRUST_E_SUBJECT_FORM_UNKNOWN);
  134. }
  135. }
  136. PKICATCH(err) {
  137. hr = err.pkiError;
  138. CloseHandle(hFile);
  139. hFile = NULL;
  140. } PKIEND;
  141. *pFileHandle = hFile;
  142. return hr;
  143. }