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.

201 lines
5.6 KiB

  1. #pragma once
  2. #include "FusionBuffer.h"
  3. #include "FusionArray.h"
  4. //#include "wincrypt.h"
  5. #include "fusionsha1.h"
  6. /*++
  7. Hashfile.h - Inclusions for file-hashing and verification testing functionality.
  8. --*/
  9. #define SHA1_HASH_SIZE_BYTES ( 160 / 8 )
  10. #define HASHFLAG_AUTODETECT ( 0x00000001 )
  11. #define HASHFLAG_STRAIGHT_HASH ( 0x00000002 )
  12. #define HASHFLAG_PROCESS_IMAGE ( 0x00000004 )
  13. #define HASHFLAG_VALID_PARAMS ( HASHFLAG_AUTODETECT | HASHFLAG_STRAIGHT_HASH | \
  14. HASHFLAG_PROCESS_IMAGE )
  15. //
  16. // If someone invents a hash with more than 512 bytes, I'll eat my socks.
  17. //
  18. #define MAX_HASH_BYTES ( 512 )
  19. BOOL
  20. SxspEnumKnownHashTypes(
  21. DWORD dwIndex,
  22. OUT CBaseStringBuffer &rbuffHashTypeName,
  23. OUT BOOL &rbNoMoreItems
  24. );
  25. BOOL
  26. SxspCreateFileHash(
  27. DWORD dwFlags,
  28. ALG_ID PreferredAlgorithm,
  29. const CBaseStringBuffer &pwsFileName,
  30. CFusionArray<BYTE> &bHashDestination
  31. );
  32. BOOL
  33. SxspIsFullHexString(
  34. PCWSTR wsString,
  35. SIZE_T Cch
  36. );
  37. typedef enum {
  38. HashValidate_Matches, // Hashes are identical
  39. HashValidate_InvalidPassedHash, // The hash passed in was somehow invalid
  40. HashValidate_InvalidAlgorithm, // The hash algorithm is invalid
  41. HashValidate_HashesCantBeMatched, // No match for another reason
  42. HashValidate_HashNotMatched, // Hashes are not identical (ie: not matched)
  43. HashValidate_OtherProblems // There was some other problem along the way
  44. } HashValidateResult;
  45. //
  46. // Do the normal validation process - single retry
  47. //
  48. #define SVFH_DEFAULT_ACTION (0x00000000)
  49. //
  50. // Retry this file N times until either (a) the file was unable to be
  51. // opened or (b) the file has other errors or (c) the file was checked
  52. // and it was ok / bad / etc.
  53. //
  54. #define SVFH_RETRY_LOGIC_SIMPLE (0x00000001)
  55. //
  56. // Wait until the file was able to be verified - spin in a backoff loop
  57. // until the file open didn't fail with ERROR_SHARING_VIOLATION
  58. //
  59. #define SVFH_RETRY_WAIT_UNTIL (0x00000002)
  60. BOOL
  61. SxspVerifyFileHash(
  62. const DWORD dwFlags,
  63. const CBaseStringBuffer &rhsFullFilePath,
  64. const CFusionArray<BYTE> &baTheorheticalHash,
  65. ALG_ID whichAlg,
  66. HashValidateResult &rHashResult
  67. );
  68. BOOL
  69. SxspHashAlgFromString(
  70. const CBaseStringBuffer &strAlgName,
  71. ALG_ID &algId
  72. );
  73. BOOL
  74. SxspHashStringFromAlg(
  75. ALG_ID algId,
  76. CBaseStringBuffer &rstrAlgName
  77. );
  78. typedef enum
  79. {
  80. ManifestValidate_Unknown = 0,
  81. ManifestValidate_IsIntact = 1,
  82. ManifestValidate_CatalogMissing = 2,
  83. ManifestValidate_ManifestMissing = 3,
  84. ManifestValidate_InvalidHash = 4,
  85. ManifestValidate_NotCertified = 5,
  86. ManifestValidate_StrongNameMismatch = 6,
  87. ManifestValidate_OtherProblems = 7
  88. } ManifestValidationResult;
  89. class CMetaDataFileElement;
  90. BOOL
  91. SxspValidateAllFileHashes(
  92. IN const CMetaDataFileElement &rmdfeElement,
  93. IN const CBaseStringBuffer &rbuffFileName,
  94. OUT HashValidateResult &rResult
  95. );
  96. #define ENUM_TO_STRING( x ) case x: return (L#x)
  97. #if DBG
  98. inline PCWSTR SxspManifestValidationResultToString( ManifestValidationResult r )
  99. {
  100. switch ( r )
  101. {
  102. ENUM_TO_STRING( ManifestValidate_Unknown );
  103. ENUM_TO_STRING( ManifestValidate_IsIntact );
  104. ENUM_TO_STRING( ManifestValidate_CatalogMissing );
  105. ENUM_TO_STRING( ManifestValidate_ManifestMissing );
  106. ENUM_TO_STRING( ManifestValidate_InvalidHash );
  107. ENUM_TO_STRING( ManifestValidate_NotCertified );
  108. ENUM_TO_STRING( ManifestValidate_OtherProblems );
  109. }
  110. return L"Bad manifest validation value";
  111. }
  112. inline PCWSTR SxspHashValidateResultToString( HashValidateResult r )
  113. {
  114. switch ( r )
  115. {
  116. ENUM_TO_STRING( HashValidate_Matches );
  117. ENUM_TO_STRING( HashValidate_InvalidPassedHash );
  118. ENUM_TO_STRING( HashValidate_InvalidAlgorithm );
  119. ENUM_TO_STRING( HashValidate_HashesCantBeMatched );
  120. ENUM_TO_STRING( HashValidate_HashNotMatched );
  121. ENUM_TO_STRING( HashValidate_OtherProblems );
  122. }
  123. return L"Bad hash validation value";
  124. }
  125. #endif
  126. // Default mode
  127. #define MANIFESTVALIDATE_OPTION_MASK ( 0x000000FF )
  128. #define MANIFESTVALIDATE_MODE_MASK ( 0x0000FF00 )
  129. #define MANIFESTVALIDATE_MODE_COMPLETE ( 0x00000100 )
  130. #define MANIFESTVALIDATE_MODE_NO_STRONGNAME ( 0x00000200 )
  131. // The manifest has to validate against a trusted root CA to be valid.
  132. #define MANIFESTVALIDATE_OPTION_NEEDS_ROOT_CA ( 0x00000001 )
  133. // The catalog gets validated first before the manifest is checked.
  134. #define MANIFESTVALIDATE_OPTION_VALIDATE_CATALOG ( 0x00000002 )
  135. // If the manifest or catalog are invalid, attempt to retrieve it
  136. #define MANIFESTVALIDATE_OPTION_ATTEMPT_RETRIEVAL ( 0x00000004 )
  137. #define MANIFESTVALIDATE_MOST_COMMON ( MANIFESTVALIDATE_MODE_COMPLETE + \
  138. ( MANIFESTVALIDATE_OPTION_NEEDS_ROOT_CA | \
  139. MANIFESTVALIDATE_OPTION_VALIDATE_CATALOG ) )
  140. BOOL
  141. SxspValidateManifestAgainstCatalog(
  142. const CBaseStringBuffer &rbuffManifestPath,
  143. ManifestValidationResult &rResult,
  144. DWORD dwOptionsFlags
  145. );
  146. BOOL
  147. SxspValidateManifestAgainstCatalog(
  148. const CBaseStringBuffer &rbuffManifestPath,
  149. const CBaseStringBuffer &rbuffCatalogPath,
  150. ManifestValidationResult &rResult,
  151. DWORD dwOptionsFlags
  152. );
  153. BOOL
  154. SxspCheckHashDuringInstall(
  155. BOOL bHasHashData,
  156. const CBaseStringBuffer &rbuffFile,
  157. const CBaseStringBuffer &rbuffHashDataString,
  158. ALG_ID HashAlgId,
  159. HashValidateResult &hvr
  160. );
  161. static inline BOOL IsSlash( WCHAR w ) { return ( ( w == L'\\' ) || ( w == L'/' ) ); }