Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / clr / src / BCL / System / Security / Policy / HashMembershipCondition.cs / 1305376 / HashMembershipCondition.cs
// ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== //[....] // // // HashMembershipCondition.cs // // Implementation of membership condition for hashes of assemblies. // namespace System.Security.Policy { using System.Collections; using System.Runtime.Serialization; using System.Security; using System.Security.Cryptography; using System.Security.Util; using System.Security.Permissions; using System.Threading; using System.Globalization; using System.Diagnostics.Contracts; [Serializable] [System.Runtime.InteropServices.ComVisible(true)] public sealed class HashMembershipCondition : ISerializable, IDeserializationCallback, IMembershipCondition, IReportMatchMembershipCondition { private byte[] m_value = null; private HashAlgorithm m_hashAlg = null; private SecurityElement m_element = null; private object s_InternalSyncObject = null; private object InternalSyncObject { get { if (s_InternalSyncObject == null) { Object o = new Object(); Interlocked.CompareExchange(ref s_InternalSyncObject, o, null); } return s_InternalSyncObject; } } internal HashMembershipCondition() {} private HashMembershipCondition (SerializationInfo info, StreamingContext context) { m_value = (byte[]) info.GetValue("HashValue", typeof(byte[])); string hashAlgorithm = (string) info.GetValue("HashAlgorithm", typeof(string)); if (hashAlgorithm != null) m_hashAlg = HashAlgorithm.Create(hashAlgorithm); else m_hashAlg = new SHA1Managed(); } public HashMembershipCondition(HashAlgorithm hashAlg, byte[] value) { if (value == null) throw new ArgumentNullException("value"); if (hashAlg == null) throw new ArgumentNullException("hashAlg"); Contract.EndContractBlock(); m_value = new byte[value.Length]; Array.Copy(value, m_value, value.Length); m_hashAlg = hashAlg; } ///[System.Security.SecurityCritical] void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) { info.AddValue("HashValue", this.HashValue); info.AddValue("HashAlgorithm", this.HashAlgorithm.ToString()); } /// void IDeserializationCallback.OnDeserialization (Object sender) {} public HashAlgorithm HashAlgorithm { set { if (value == null) throw new ArgumentNullException("HashAlgorithm"); Contract.EndContractBlock(); m_hashAlg = value; } [System.Security.SecuritySafeCritical] // auto-generated get { if (m_hashAlg == null && m_element != null) ParseHashAlgorithm(); return m_hashAlg; } } public byte[] HashValue { set { if (value == null) throw new ArgumentNullException("value"); Contract.EndContractBlock(); m_value = new byte[value.Length]; Array.Copy(value, m_value, value.Length); } get { if (m_value == null && m_element != null) ParseHashValue(); if (m_value == null) return null; byte[] value = new byte[m_value.Length]; Array.Copy(m_value, value, m_value.Length); return value; } } public bool Check(Evidence evidence) { object usedEvidence = null; return (this as IReportMatchMembershipCondition).Check(evidence, out usedEvidence); } bool IReportMatchMembershipCondition.Check(Evidence evidence, out object usedEvidence) { usedEvidence = null; if (evidence == null) return false; Hash hash = evidence.GetHostEvidence (); if (hash != null) { if (m_value == null && m_element != null) ParseHashValue(); if (m_hashAlg == null && m_element != null) ParseHashAlgorithm(); byte[] asmHash = null; lock (InternalSyncObject) { asmHash = hash.GenerateHash(m_hashAlg); } if (asmHash != null && CompareArrays(asmHash, m_value)) { usedEvidence = hash; return true; } } return false; } [System.Security.SecuritySafeCritical] // auto-generated public IMembershipCondition Copy() { if (m_value == null && m_element != null) ParseHashValue(); if (m_hashAlg == null && m_element != null) ParseHashAlgorithm(); return new HashMembershipCondition(m_hashAlg, m_value); } [System.Security.SecuritySafeCritical] // auto-generated public SecurityElement ToXml() { return ToXml(null); } public void FromXml(SecurityElement e) { FromXml(e, null); } [System.Security.SecuritySafeCritical] // auto-generated public SecurityElement ToXml(PolicyLevel level) { if (m_value == null && m_element != null) ParseHashValue(); if (m_hashAlg == null && m_element != null) ParseHashAlgorithm(); SecurityElement root = new SecurityElement("IMembershipCondition"); XMLUtil.AddClassAttribute(root, this.GetType(), "System.Security.Policy.HashMembershipCondition"); // If you hit this assert then most likely you are trying to change the name of this class. // This is ok as long as you change the hard coded string above and change the assert below. Contract.Assert(this.GetType().FullName.Equals("System.Security.Policy.HashMembershipCondition"), "Class name changed!"); root.AddAttribute("version", "1"); if (m_value != null) root.AddAttribute(s_tagHashValue, Hex.EncodeHexString(HashValue)); if (m_hashAlg != null) root.AddAttribute(s_tagHashAlgorithm, HashAlgorithm.GetType().FullName); return root; } public void FromXml(SecurityElement e, PolicyLevel level) { if (e == null) throw new ArgumentNullException("e"); if (!e.Tag.Equals("IMembershipCondition")) throw new ArgumentException(Environment.GetResourceString("Argument_MembershipConditionElement")); Contract.EndContractBlock(); lock (InternalSyncObject) { m_element = e; m_value = null; m_hashAlg = null; } } [System.Security.SecuritySafeCritical] // auto-generated public override bool Equals(Object o) { HashMembershipCondition that = (o as HashMembershipCondition); if (that != null) { if (this.m_hashAlg == null && this.m_element != null) this.ParseHashAlgorithm(); if (that.m_hashAlg == null && that.m_element != null) that.ParseHashAlgorithm(); if (this.m_hashAlg != null && that.m_hashAlg != null && this.m_hashAlg.GetType() == that.m_hashAlg.GetType()) { if (this.m_value == null && this.m_element != null) this.ParseHashValue(); if (that.m_value == null && that.m_element != null) that.ParseHashValue(); if (this.m_value.Length != that.m_value.Length) return false; for (int i = 0; i < m_value.Length; i++) { if (this.m_value[i] != that.m_value[i]) return false; } return true; } } return false; } [System.Security.SecuritySafeCritical] // auto-generated public override int GetHashCode() { if (this.m_hashAlg == null && this.m_element != null) this.ParseHashAlgorithm(); int accumulator = this.m_hashAlg != null ? this.m_hashAlg.GetType().GetHashCode() : 0; if (this.m_value == null && this.m_element != null) this.ParseHashValue(); accumulator = accumulator ^ GetByteArrayHashCode(this.m_value); return accumulator; } [System.Security.SecuritySafeCritical] // auto-generated public override string ToString() { if (m_hashAlg == null) ParseHashAlgorithm(); return Environment.GetResourceString("Hash_ToString", m_hashAlg.GetType().AssemblyQualifiedName, Hex.EncodeHexString(HashValue)); } private const string s_tagHashValue = "HashValue"; private const string s_tagHashAlgorithm = "HashAlgorithm"; private void ParseHashValue() { lock (InternalSyncObject) { if (m_element == null) return; string elHash = m_element.Attribute(s_tagHashValue); if (elHash != null) m_value = Hex.DecodeHexString(elHash); else throw new ArgumentException(Environment.GetResourceString("Argument_InvalidXMLElement", s_tagHashValue, this.GetType().FullName)); if (m_value != null && m_hashAlg != null) { m_element = null; } } } private void ParseHashAlgorithm() { lock (InternalSyncObject) { if (m_element == null) return; string elHashAlg = m_element.Attribute(s_tagHashAlgorithm); if (elHashAlg != null) m_hashAlg = HashAlgorithm.Create(elHashAlg); else m_hashAlg = new SHA1Managed(); if (m_value != null && m_hashAlg != null) m_element = null; } } private static bool CompareArrays(byte[] first, byte[] second) { if (first.Length != second.Length) return false; int count = first.Length; for (int i = 0; i < count; ++i) { if (first[i] != second[i]) return false; } return true; } private static int GetByteArrayHashCode(byte[] baData) { if (baData == null) return 0; int accumulator = 0; for (int i = 0; i < baData.Length; ++i) { accumulator = (accumulator << 8) ^ (int)baData[i] ^ (accumulator >> 24); } return accumulator; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== // [....] // // // HashMembershipCondition.cs // // Implementation of membership condition for hashes of assemblies. // namespace System.Security.Policy { using System.Collections; using System.Runtime.Serialization; using System.Security; using System.Security.Cryptography; using System.Security.Util; using System.Security.Permissions; using System.Threading; using System.Globalization; using System.Diagnostics.Contracts; [Serializable] [System.Runtime.InteropServices.ComVisible(true)] public sealed class HashMembershipCondition : ISerializable, IDeserializationCallback, IMembershipCondition, IReportMatchMembershipCondition { private byte[] m_value = null; private HashAlgorithm m_hashAlg = null; private SecurityElement m_element = null; private object s_InternalSyncObject = null; private object InternalSyncObject { get { if (s_InternalSyncObject == null) { Object o = new Object(); Interlocked.CompareExchange(ref s_InternalSyncObject, o, null); } return s_InternalSyncObject; } } internal HashMembershipCondition() {} private HashMembershipCondition (SerializationInfo info, StreamingContext context) { m_value = (byte[]) info.GetValue("HashValue", typeof(byte[])); string hashAlgorithm = (string) info.GetValue("HashAlgorithm", typeof(string)); if (hashAlgorithm != null) m_hashAlg = HashAlgorithm.Create(hashAlgorithm); else m_hashAlg = new SHA1Managed(); } public HashMembershipCondition(HashAlgorithm hashAlg, byte[] value) { if (value == null) throw new ArgumentNullException("value"); if (hashAlg == null) throw new ArgumentNullException("hashAlg"); Contract.EndContractBlock(); m_value = new byte[value.Length]; Array.Copy(value, m_value, value.Length); m_hashAlg = hashAlg; } ///[System.Security.SecurityCritical] void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) { info.AddValue("HashValue", this.HashValue); info.AddValue("HashAlgorithm", this.HashAlgorithm.ToString()); } /// void IDeserializationCallback.OnDeserialization (Object sender) {} public HashAlgorithm HashAlgorithm { set { if (value == null) throw new ArgumentNullException("HashAlgorithm"); Contract.EndContractBlock(); m_hashAlg = value; } [System.Security.SecuritySafeCritical] // auto-generated get { if (m_hashAlg == null && m_element != null) ParseHashAlgorithm(); return m_hashAlg; } } public byte[] HashValue { set { if (value == null) throw new ArgumentNullException("value"); Contract.EndContractBlock(); m_value = new byte[value.Length]; Array.Copy(value, m_value, value.Length); } get { if (m_value == null && m_element != null) ParseHashValue(); if (m_value == null) return null; byte[] value = new byte[m_value.Length]; Array.Copy(m_value, value, m_value.Length); return value; } } public bool Check(Evidence evidence) { object usedEvidence = null; return (this as IReportMatchMembershipCondition).Check(evidence, out usedEvidence); } bool IReportMatchMembershipCondition.Check(Evidence evidence, out object usedEvidence) { usedEvidence = null; if (evidence == null) return false; Hash hash = evidence.GetHostEvidence (); if (hash != null) { if (m_value == null && m_element != null) ParseHashValue(); if (m_hashAlg == null && m_element != null) ParseHashAlgorithm(); byte[] asmHash = null; lock (InternalSyncObject) { asmHash = hash.GenerateHash(m_hashAlg); } if (asmHash != null && CompareArrays(asmHash, m_value)) { usedEvidence = hash; return true; } } return false; } [System.Security.SecuritySafeCritical] // auto-generated public IMembershipCondition Copy() { if (m_value == null && m_element != null) ParseHashValue(); if (m_hashAlg == null && m_element != null) ParseHashAlgorithm(); return new HashMembershipCondition(m_hashAlg, m_value); } [System.Security.SecuritySafeCritical] // auto-generated public SecurityElement ToXml() { return ToXml(null); } public void FromXml(SecurityElement e) { FromXml(e, null); } [System.Security.SecuritySafeCritical] // auto-generated public SecurityElement ToXml(PolicyLevel level) { if (m_value == null && m_element != null) ParseHashValue(); if (m_hashAlg == null && m_element != null) ParseHashAlgorithm(); SecurityElement root = new SecurityElement("IMembershipCondition"); XMLUtil.AddClassAttribute(root, this.GetType(), "System.Security.Policy.HashMembershipCondition"); // If you hit this assert then most likely you are trying to change the name of this class. // This is ok as long as you change the hard coded string above and change the assert below. Contract.Assert(this.GetType().FullName.Equals("System.Security.Policy.HashMembershipCondition"), "Class name changed!"); root.AddAttribute("version", "1"); if (m_value != null) root.AddAttribute(s_tagHashValue, Hex.EncodeHexString(HashValue)); if (m_hashAlg != null) root.AddAttribute(s_tagHashAlgorithm, HashAlgorithm.GetType().FullName); return root; } public void FromXml(SecurityElement e, PolicyLevel level) { if (e == null) throw new ArgumentNullException("e"); if (!e.Tag.Equals("IMembershipCondition")) throw new ArgumentException(Environment.GetResourceString("Argument_MembershipConditionElement")); Contract.EndContractBlock(); lock (InternalSyncObject) { m_element = e; m_value = null; m_hashAlg = null; } } [System.Security.SecuritySafeCritical] // auto-generated public override bool Equals(Object o) { HashMembershipCondition that = (o as HashMembershipCondition); if (that != null) { if (this.m_hashAlg == null && this.m_element != null) this.ParseHashAlgorithm(); if (that.m_hashAlg == null && that.m_element != null) that.ParseHashAlgorithm(); if (this.m_hashAlg != null && that.m_hashAlg != null && this.m_hashAlg.GetType() == that.m_hashAlg.GetType()) { if (this.m_value == null && this.m_element != null) this.ParseHashValue(); if (that.m_value == null && that.m_element != null) that.ParseHashValue(); if (this.m_value.Length != that.m_value.Length) return false; for (int i = 0; i < m_value.Length; i++) { if (this.m_value[i] != that.m_value[i]) return false; } return true; } } return false; } [System.Security.SecuritySafeCritical] // auto-generated public override int GetHashCode() { if (this.m_hashAlg == null && this.m_element != null) this.ParseHashAlgorithm(); int accumulator = this.m_hashAlg != null ? this.m_hashAlg.GetType().GetHashCode() : 0; if (this.m_value == null && this.m_element != null) this.ParseHashValue(); accumulator = accumulator ^ GetByteArrayHashCode(this.m_value); return accumulator; } [System.Security.SecuritySafeCritical] // auto-generated public override string ToString() { if (m_hashAlg == null) ParseHashAlgorithm(); return Environment.GetResourceString("Hash_ToString", m_hashAlg.GetType().AssemblyQualifiedName, Hex.EncodeHexString(HashValue)); } private const string s_tagHashValue = "HashValue"; private const string s_tagHashAlgorithm = "HashAlgorithm"; private void ParseHashValue() { lock (InternalSyncObject) { if (m_element == null) return; string elHash = m_element.Attribute(s_tagHashValue); if (elHash != null) m_value = Hex.DecodeHexString(elHash); else throw new ArgumentException(Environment.GetResourceString("Argument_InvalidXMLElement", s_tagHashValue, this.GetType().FullName)); if (m_value != null && m_hashAlg != null) { m_element = null; } } } private void ParseHashAlgorithm() { lock (InternalSyncObject) { if (m_element == null) return; string elHashAlg = m_element.Attribute(s_tagHashAlgorithm); if (elHashAlg != null) m_hashAlg = HashAlgorithm.Create(elHashAlg); else m_hashAlg = new SHA1Managed(); if (m_value != null && m_hashAlg != null) m_element = null; } } private static bool CompareArrays(byte[] first, byte[] second) { if (first.Length != second.Length) return false; int count = first.Length; for (int i = 0; i < count; ++i) { if (first[i] != second[i]) return false; } return true; } private static int GetByteArrayHashCode(byte[] baData) { if (baData == null) return 0; int accumulator = 0; for (int i = 0; i < baData.Length; ++i) { accumulator = (accumulator << 8) ^ (int)baData[i] ^ (accumulator >> 24); } return accumulator; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- ProxyFragment.cs
- SafeCryptoHandles.cs
- AuthenticationConfig.cs
- PathSegment.cs
- DataSourceUtil.cs
- IndexOutOfRangeException.cs
- RenderCapability.cs
- TrustLevelCollection.cs
- PriorityRange.cs
- LicFileLicenseProvider.cs
- HatchBrush.cs
- XmlSchemaAnnotated.cs
- BatchWriter.cs
- DataRow.cs
- DrawingAttributeSerializer.cs
- CodeAttributeArgument.cs
- SrgsElementList.cs
- ToolStripButton.cs
- WithParamAction.cs
- AsyncInvokeContext.cs
- ByteAnimationUsingKeyFrames.cs
- TabletDevice.cs
- FileDialogCustomPlacesCollection.cs
- SafeNativeMethods.cs
- SmtpAuthenticationManager.cs
- TreeNodeCollection.cs
- Decoder.cs
- EncoderNLS.cs
- DataObjectSettingDataEventArgs.cs
- ExtensionSurface.cs
- DataGridAutoFormat.cs
- ResourcesChangeInfo.cs
- BitmapPalettes.cs
- FlowLayoutPanelDesigner.cs
- CheckBoxAutomationPeer.cs
- ServicePoint.cs
- MetadataHelper.cs
- TokenizerHelper.cs
- HandlerFactoryCache.cs
- SqlPersonalizationProvider.cs
- DependencyObjectProvider.cs
- GatewayDefinition.cs
- SafeLibraryHandle.cs
- ObjectRef.cs
- CacheSection.cs
- RotateTransform.cs
- CurrentChangedEventManager.cs
- Vector.cs
- BitmapEffectGroup.cs
- TableLayoutCellPaintEventArgs.cs
- LayoutEvent.cs
- Peer.cs
- CompModSwitches.cs
- TypeDependencyAttribute.cs
- DataListAutoFormat.cs
- SelectionItemPattern.cs
- DataServiceClientException.cs
- TagNameToTypeMapper.cs
- CommandID.cs
- KeyConverter.cs
- ModuleBuilder.cs
- WindowsMenu.cs
- WinEventQueueItem.cs
- ComponentEditorPage.cs
- XmlSignatureProperties.cs
- comcontractssection.cs
- ByteAnimationBase.cs
- ElementHostPropertyMap.cs
- FtpWebRequest.cs
- HttpStreamMessage.cs
- Baml2006SchemaContext.cs
- DebugView.cs
- AppModelKnownContentFactory.cs
- Vector3DCollectionValueSerializer.cs
- SchemaElementDecl.cs
- RowVisual.cs
- DataColumnChangeEvent.cs
- UnlockInstanceCommand.cs
- ReadOnlyMetadataCollection.cs
- SafeNativeMethodsCLR.cs
- PassportAuthentication.cs
- InputProviderSite.cs
- JournalEntry.cs
- InvalidEnumArgumentException.cs
- UTF8Encoding.cs
- PackWebResponse.cs
- SettingsProperty.cs
- InputScopeNameConverter.cs
- X509ThumbprintKeyIdentifierClause.cs
- TranslateTransform.cs
- BuildResult.cs
- Deserializer.cs
- Helper.cs
- EditingCoordinator.cs
- CompareInfo.cs
- NativeActivityMetadata.cs
- IOException.cs
- Exceptions.cs
- ReadOnlyDictionary.cs
- ResetableIterator.cs