public static void UpdateSpecialHashes(CodeDirectoryBlob codeDirectory, byte[] codeToHash, byte[] infoFileBytes, CodeRequirementsBlob codeRequirements, byte[] codeResourcesBytes, EntitlementsBlob entitlements) { codeDirectory.CodeHashes = HashAlgorithmHelper.ComputeHashes(codeDirectory.HashType, codeDirectory.PageSize, codeToHash); codeDirectory.SpecialHashes = new List <byte[]>(); codeDirectory.SpecialHashes.Insert(0, (HashAlgorithmHelper.ComputeHash(codeDirectory.HashType, infoFileBytes))); codeDirectory.SpecialHashes.Insert(0, (HashAlgorithmHelper.ComputeHash(codeDirectory.HashType, codeRequirements.GetBytes()))); codeDirectory.SpecialHashes.Insert(0, (HashAlgorithmHelper.ComputeHash(codeDirectory.HashType, codeResourcesBytes))); if (SpecialHashCount >= CodeDirectoryBlob.ApplicationSpecificHashOffset) { codeDirectory.SpecialHashes.Insert(0, new byte[HashAlgorithmHelper.GetHashLength(codeDirectory.HashType)]); if (SpecialHashCount >= CodeDirectoryBlob.EntitlementsHashOffset) { codeDirectory.SpecialHashes.Insert(0, (HashAlgorithmHelper.ComputeHash(codeDirectory.HashType, entitlements.GetBytes()))); } } }
public static bool ValidateExecutableHash(MachObjectFile file) { byte[] codeSignatureBytes = file.GetCodeSignatureBytes(); if (CodeSignatureSuperBlob.IsCodeSignatureSuperBlob(codeSignatureBytes, 0)) { CodeSignatureSuperBlob codeSignature = new CodeSignatureSuperBlob(codeSignatureBytes, 0); CodeDirectoryBlob codeDirectory = codeSignature.GetEntry(CodeSignatureEntryType.CodeDirectory) as CodeDirectoryBlob; byte[] signedFileData = ByteReader.ReadBytes(file.GetBytes(), 0, (int)codeDirectory.CodeLimit); List <byte[]> hashes = HashAlgorithmHelper.ComputeHashes(codeDirectory.HashType, codeDirectory.PageSize, signedFileData); if (hashes.Count != codeDirectory.CodeHashes.Count) { return(false); } for (int index = 0; index < hashes.Count; index++) { if (!ByteUtils.AreByteArraysEqual(hashes[index], codeDirectory.CodeHashes[index])) { return(false); } } return(true); } return(false); }