Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / WCF / IdentityModel / System / IdentityModel / ExclusiveCanonicalizationTransform.cs / 1305376 / ExclusiveCanonicalizationTransform.cs
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------
namespace System.IdentityModel
{
using System.IO;
using System.Security.Cryptography;
using System.IdentityModel.Selectors;
using System.IdentityModel.Tokens;
using System.Text;
using System.Xml;
class ExclusiveCanonicalizationTransform : Transform
{
readonly bool includeComments;
string inclusiveNamespacesPrefixList;
string[] inclusivePrefixes;
string inclusiveListElementPrefix = ExclusiveC14NStrings.Prefix;
string prefix = XmlSignatureStrings.Prefix;
readonly bool isCanonicalizationMethod;
public ExclusiveCanonicalizationTransform()
: this(false)
{
}
public ExclusiveCanonicalizationTransform(bool isCanonicalizationMethod)
: this(isCanonicalizationMethod, false)
{
this.isCanonicalizationMethod = isCanonicalizationMethod;
}
protected ExclusiveCanonicalizationTransform(bool isCanonicalizationMethod, bool includeComments)
{
this.isCanonicalizationMethod = isCanonicalizationMethod;
this.includeComments = includeComments;
}
public override string Algorithm
{
get
{
return this.includeComments ? XD.SecurityAlgorithmDictionary.ExclusiveC14nWithComments.Value : XD.SecurityAlgorithmDictionary.ExclusiveC14n.Value;
}
}
public bool IncludeComments
{
get
{
return this.includeComments;
}
}
public string InclusiveNamespacesPrefixList
{
get
{
return this.inclusiveNamespacesPrefixList;
}
set
{
this.inclusiveNamespacesPrefixList = value;
this.inclusivePrefixes = TokenizeInclusivePrefixList(value);
}
}
public override bool NeedsInclusiveContext
{
get { return GetInclusivePrefixes() != null; }
}
public string[] GetInclusivePrefixes()
{
return this.inclusivePrefixes;
}
CanonicalizationDriver GetConfiguredDriver(SignatureResourcePool resourcePool)
{
CanonicalizationDriver driver = resourcePool.TakeCanonicalizationDriver();
driver.IncludeComments = this.IncludeComments;
driver.SetInclusivePrefixes(this.inclusivePrefixes);
return driver;
}
// multi-transform case, inefficient path
public override object Process(object input, SignatureResourcePool resourcePool, DictionaryManager dictionaryManager)
{
if (input is XmlReader)
{
CanonicalizationDriver driver = GetConfiguredDriver(resourcePool);
driver.SetInput(input as XmlReader);
return driver.GetMemoryStream();
}
else if (input is ISecurityElement)
{
MemoryStream stream = new MemoryStream();
XmlDictionaryWriter utf8Writer = resourcePool.TakeUtf8Writer();
utf8Writer.StartCanonicalization(stream, false, null);
(input as ISecurityElement).WriteTo(utf8Writer, dictionaryManager);
utf8Writer.EndCanonicalization();
stream.Seek(0, SeekOrigin.Begin);
return stream;
}
else
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.UnsupportedInputTypeForTransform, input.GetType())));
}
}
// common single-transform case; fold directly into a digest
public override byte[] ProcessAndDigest(object input, SignatureResourcePool resourcePool, string digestAlgorithm, DictionaryManager dictionaryManager)
{
HashAlgorithm hash = resourcePool.TakeHashAlgorithm(digestAlgorithm);
ProcessAndDigest(input, resourcePool, hash, dictionaryManager);
return hash.Hash;
}
public void ProcessAndDigest(object input, SignatureResourcePool resourcePool, HashAlgorithm hash, DictionaryManager dictionaryManger)
{
HashStream hashStream = resourcePool.TakeHashStream(hash);
XmlReader reader = input as XmlReader;
if (reader != null)
{
ProcessReaderInput(reader, resourcePool, hashStream);
}
else if (input is ISecurityElement)
{
XmlDictionaryWriter utf8Writer = resourcePool.TakeUtf8Writer();
utf8Writer.StartCanonicalization(hashStream, this.IncludeComments, GetInclusivePrefixes());
(input as ISecurityElement).WriteTo(utf8Writer, dictionaryManger);
utf8Writer.EndCanonicalization();
}
else
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.UnsupportedInputTypeForTransform, input.GetType())));
}
hashStream.FlushHash();
}
void ProcessReaderInput(XmlReader reader, SignatureResourcePool resourcePool, HashStream hashStream)
{
reader.MoveToContent();
XmlDictionaryReader dictionaryReader = reader as XmlDictionaryReader;
if (dictionaryReader != null && dictionaryReader.CanCanonicalize)
{
dictionaryReader.StartCanonicalization(hashStream, this.IncludeComments, GetInclusivePrefixes());
dictionaryReader.Skip();
dictionaryReader.EndCanonicalization();
}
else
{
CanonicalizationDriver driver = GetConfiguredDriver(resourcePool);
driver.SetInput(reader);
driver.WriteTo(hashStream);
}
}
public override void ReadFrom(XmlDictionaryReader reader, DictionaryManager dictionaryManager)
{
XmlDictionaryString elementName = this.isCanonicalizationMethod ?
dictionaryManager.XmlSignatureDictionary.CanonicalizationMethod : dictionaryManager.XmlSignatureDictionary.Transform;
reader.MoveToStartElement(elementName, dictionaryManager.XmlSignatureDictionary.Namespace);
this.prefix = reader.Prefix;
bool isEmptyElement = reader.IsEmptyElement;
string algorithm = reader.GetAttribute(dictionaryManager.XmlSignatureDictionary.Algorithm, null);
if (algorithm != this.Algorithm)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(SR.GetString(SR.AlgorithmMismatchForTransform)));
}
reader.Read();
reader.MoveToContent();
if (!isEmptyElement)
{
if (reader.IsStartElement(dictionaryManager.ExclusiveC14NDictionary.InclusiveNamespaces, dictionaryManager.ExclusiveC14NDictionary.Namespace))
{
reader.MoveToStartElement(dictionaryManager.ExclusiveC14NDictionary.InclusiveNamespaces, dictionaryManager.ExclusiveC14NDictionary.Namespace);
this.inclusiveListElementPrefix = reader.Prefix;
bool emptyElement = reader.IsEmptyElement;
// We treat PrefixList as optional Attribute.
this.InclusiveNamespacesPrefixList = reader.GetAttribute(dictionaryManager.ExclusiveC14NDictionary.PrefixList, null);
reader.Read();
if (!emptyElement)
reader.ReadEndElement();
}
reader.MoveToContent();
reader.ReadEndElement(); // Transform
}
}
public override void WriteTo(XmlDictionaryWriter writer, DictionaryManager dictionaryManager)
{
XmlDictionaryString elementName = this.isCanonicalizationMethod ?
dictionaryManager.XmlSignatureDictionary.CanonicalizationMethod : dictionaryManager.XmlSignatureDictionary.Transform;
XmlDictionaryString algorithm = this.includeComments ? dictionaryManager.SecurityAlgorithmDictionary.ExclusiveC14nWithComments : dictionaryManager.SecurityAlgorithmDictionary.ExclusiveC14n;
writer.WriteStartElement(this.prefix, elementName, dictionaryManager.XmlSignatureDictionary.Namespace);
writer.WriteStartAttribute(dictionaryManager.XmlSignatureDictionary.Algorithm, null);
if (algorithm != null)
{
writer.WriteString(algorithm);
}
else
{
writer.WriteString(algorithm.Value);
}
writer.WriteEndAttribute();
if (this.InclusiveNamespacesPrefixList != null)
{
writer.WriteStartElement(this.inclusiveListElementPrefix, dictionaryManager.ExclusiveC14NDictionary.InclusiveNamespaces, dictionaryManager.ExclusiveC14NDictionary.Namespace);
writer.WriteAttributeString(dictionaryManager.ExclusiveC14NDictionary.PrefixList, null, this.InclusiveNamespacesPrefixList);
writer.WriteEndElement(); // InclusiveNamespaces
}
writer.WriteEndElement(); // Transform
}
static string[] TokenizeInclusivePrefixList(string prefixList)
{
if (prefixList == null)
{
return null;
}
string[] prefixes = prefixList.Split(null);
int count = 0;
for (int i = 0; i < prefixes.Length; i++)
{
string prefix = prefixes[i];
if (prefix == "#default")
{
prefixes[count++] = string.Empty;
}
else if (prefix.Length > 0)
{
prefixes[count++] = prefix;
}
}
if (count == 0)
{
return null;
}
else if (count == prefixes.Length)
{
return prefixes;
}
else
{
string[] result = new string[count];
Array.Copy(prefixes, result, count);
return result;
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------
namespace System.IdentityModel
{
using System.IO;
using System.Security.Cryptography;
using System.IdentityModel.Selectors;
using System.IdentityModel.Tokens;
using System.Text;
using System.Xml;
class ExclusiveCanonicalizationTransform : Transform
{
readonly bool includeComments;
string inclusiveNamespacesPrefixList;
string[] inclusivePrefixes;
string inclusiveListElementPrefix = ExclusiveC14NStrings.Prefix;
string prefix = XmlSignatureStrings.Prefix;
readonly bool isCanonicalizationMethod;
public ExclusiveCanonicalizationTransform()
: this(false)
{
}
public ExclusiveCanonicalizationTransform(bool isCanonicalizationMethod)
: this(isCanonicalizationMethod, false)
{
this.isCanonicalizationMethod = isCanonicalizationMethod;
}
protected ExclusiveCanonicalizationTransform(bool isCanonicalizationMethod, bool includeComments)
{
this.isCanonicalizationMethod = isCanonicalizationMethod;
this.includeComments = includeComments;
}
public override string Algorithm
{
get
{
return this.includeComments ? XD.SecurityAlgorithmDictionary.ExclusiveC14nWithComments.Value : XD.SecurityAlgorithmDictionary.ExclusiveC14n.Value;
}
}
public bool IncludeComments
{
get
{
return this.includeComments;
}
}
public string InclusiveNamespacesPrefixList
{
get
{
return this.inclusiveNamespacesPrefixList;
}
set
{
this.inclusiveNamespacesPrefixList = value;
this.inclusivePrefixes = TokenizeInclusivePrefixList(value);
}
}
public override bool NeedsInclusiveContext
{
get { return GetInclusivePrefixes() != null; }
}
public string[] GetInclusivePrefixes()
{
return this.inclusivePrefixes;
}
CanonicalizationDriver GetConfiguredDriver(SignatureResourcePool resourcePool)
{
CanonicalizationDriver driver = resourcePool.TakeCanonicalizationDriver();
driver.IncludeComments = this.IncludeComments;
driver.SetInclusivePrefixes(this.inclusivePrefixes);
return driver;
}
// multi-transform case, inefficient path
public override object Process(object input, SignatureResourcePool resourcePool, DictionaryManager dictionaryManager)
{
if (input is XmlReader)
{
CanonicalizationDriver driver = GetConfiguredDriver(resourcePool);
driver.SetInput(input as XmlReader);
return driver.GetMemoryStream();
}
else if (input is ISecurityElement)
{
MemoryStream stream = new MemoryStream();
XmlDictionaryWriter utf8Writer = resourcePool.TakeUtf8Writer();
utf8Writer.StartCanonicalization(stream, false, null);
(input as ISecurityElement).WriteTo(utf8Writer, dictionaryManager);
utf8Writer.EndCanonicalization();
stream.Seek(0, SeekOrigin.Begin);
return stream;
}
else
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.UnsupportedInputTypeForTransform, input.GetType())));
}
}
// common single-transform case; fold directly into a digest
public override byte[] ProcessAndDigest(object input, SignatureResourcePool resourcePool, string digestAlgorithm, DictionaryManager dictionaryManager)
{
HashAlgorithm hash = resourcePool.TakeHashAlgorithm(digestAlgorithm);
ProcessAndDigest(input, resourcePool, hash, dictionaryManager);
return hash.Hash;
}
public void ProcessAndDigest(object input, SignatureResourcePool resourcePool, HashAlgorithm hash, DictionaryManager dictionaryManger)
{
HashStream hashStream = resourcePool.TakeHashStream(hash);
XmlReader reader = input as XmlReader;
if (reader != null)
{
ProcessReaderInput(reader, resourcePool, hashStream);
}
else if (input is ISecurityElement)
{
XmlDictionaryWriter utf8Writer = resourcePool.TakeUtf8Writer();
utf8Writer.StartCanonicalization(hashStream, this.IncludeComments, GetInclusivePrefixes());
(input as ISecurityElement).WriteTo(utf8Writer, dictionaryManger);
utf8Writer.EndCanonicalization();
}
else
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.UnsupportedInputTypeForTransform, input.GetType())));
}
hashStream.FlushHash();
}
void ProcessReaderInput(XmlReader reader, SignatureResourcePool resourcePool, HashStream hashStream)
{
reader.MoveToContent();
XmlDictionaryReader dictionaryReader = reader as XmlDictionaryReader;
if (dictionaryReader != null && dictionaryReader.CanCanonicalize)
{
dictionaryReader.StartCanonicalization(hashStream, this.IncludeComments, GetInclusivePrefixes());
dictionaryReader.Skip();
dictionaryReader.EndCanonicalization();
}
else
{
CanonicalizationDriver driver = GetConfiguredDriver(resourcePool);
driver.SetInput(reader);
driver.WriteTo(hashStream);
}
}
public override void ReadFrom(XmlDictionaryReader reader, DictionaryManager dictionaryManager)
{
XmlDictionaryString elementName = this.isCanonicalizationMethod ?
dictionaryManager.XmlSignatureDictionary.CanonicalizationMethod : dictionaryManager.XmlSignatureDictionary.Transform;
reader.MoveToStartElement(elementName, dictionaryManager.XmlSignatureDictionary.Namespace);
this.prefix = reader.Prefix;
bool isEmptyElement = reader.IsEmptyElement;
string algorithm = reader.GetAttribute(dictionaryManager.XmlSignatureDictionary.Algorithm, null);
if (algorithm != this.Algorithm)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(SR.GetString(SR.AlgorithmMismatchForTransform)));
}
reader.Read();
reader.MoveToContent();
if (!isEmptyElement)
{
if (reader.IsStartElement(dictionaryManager.ExclusiveC14NDictionary.InclusiveNamespaces, dictionaryManager.ExclusiveC14NDictionary.Namespace))
{
reader.MoveToStartElement(dictionaryManager.ExclusiveC14NDictionary.InclusiveNamespaces, dictionaryManager.ExclusiveC14NDictionary.Namespace);
this.inclusiveListElementPrefix = reader.Prefix;
bool emptyElement = reader.IsEmptyElement;
// We treat PrefixList as optional Attribute.
this.InclusiveNamespacesPrefixList = reader.GetAttribute(dictionaryManager.ExclusiveC14NDictionary.PrefixList, null);
reader.Read();
if (!emptyElement)
reader.ReadEndElement();
}
reader.MoveToContent();
reader.ReadEndElement(); // Transform
}
}
public override void WriteTo(XmlDictionaryWriter writer, DictionaryManager dictionaryManager)
{
XmlDictionaryString elementName = this.isCanonicalizationMethod ?
dictionaryManager.XmlSignatureDictionary.CanonicalizationMethod : dictionaryManager.XmlSignatureDictionary.Transform;
XmlDictionaryString algorithm = this.includeComments ? dictionaryManager.SecurityAlgorithmDictionary.ExclusiveC14nWithComments : dictionaryManager.SecurityAlgorithmDictionary.ExclusiveC14n;
writer.WriteStartElement(this.prefix, elementName, dictionaryManager.XmlSignatureDictionary.Namespace);
writer.WriteStartAttribute(dictionaryManager.XmlSignatureDictionary.Algorithm, null);
if (algorithm != null)
{
writer.WriteString(algorithm);
}
else
{
writer.WriteString(algorithm.Value);
}
writer.WriteEndAttribute();
if (this.InclusiveNamespacesPrefixList != null)
{
writer.WriteStartElement(this.inclusiveListElementPrefix, dictionaryManager.ExclusiveC14NDictionary.InclusiveNamespaces, dictionaryManager.ExclusiveC14NDictionary.Namespace);
writer.WriteAttributeString(dictionaryManager.ExclusiveC14NDictionary.PrefixList, null, this.InclusiveNamespacesPrefixList);
writer.WriteEndElement(); // InclusiveNamespaces
}
writer.WriteEndElement(); // Transform
}
static string[] TokenizeInclusivePrefixList(string prefixList)
{
if (prefixList == null)
{
return null;
}
string[] prefixes = prefixList.Split(null);
int count = 0;
for (int i = 0; i < prefixes.Length; i++)
{
string prefix = prefixes[i];
if (prefix == "#default")
{
prefixes[count++] = string.Empty;
}
else if (prefix.Length > 0)
{
prefixes[count++] = prefix;
}
}
if (count == 0)
{
return null;
}
else if (count == prefixes.Length)
{
return prefixes;
}
else
{
string[] result = new string[count];
Array.Copy(prefixes, result, count);
return result;
}
}
}
}
// 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
- ACE.cs
- BigInt.cs
- ClassGenerator.cs
- DataGridViewCellStateChangedEventArgs.cs
- SQLMembershipProvider.cs
- RawStylusSystemGestureInputReport.cs
- FreeFormDesigner.cs
- XPathNodeHelper.cs
- SvcMapFileLoader.cs
- DateTimeOffsetStorage.cs
- CorrelationRequestContext.cs
- AlternateView.cs
- MulticastDelegate.cs
- DefaultValueAttribute.cs
- PeerNameRecord.cs
- TcpChannelHelper.cs
- TableParagraph.cs
- HtmlTextArea.cs
- SafeNativeMethods.cs
- CultureInfo.cs
- ConnectionPointGlyph.cs
- HideDisabledControlAdapter.cs
- initElementDictionary.cs
- UIElementParaClient.cs
- ISAPIApplicationHost.cs
- LoginView.cs
- Int64AnimationUsingKeyFrames.cs
- ComboBoxRenderer.cs
- FamilyMap.cs
- TypeTypeConverter.cs
- DragDropHelper.cs
- PropertyGridEditorPart.cs
- BindingRestrictions.cs
- Vertex.cs
- HttpChannelHelper.cs
- EUCJPEncoding.cs
- categoryentry.cs
- CompiledQueryCacheEntry.cs
- MetadataItemSerializer.cs
- CryptoApi.cs
- ReferentialConstraint.cs
- Condition.cs
- JsonWriter.cs
- SingleAnimationUsingKeyFrames.cs
- PerfCounterSection.cs
- WebMessageEncodingBindingElement.cs
- Classification.cs
- ChangePasswordAutoFormat.cs
- SoapReflectionImporter.cs
- DesignTimeType.cs
- ReachDocumentReferenceCollectionSerializer.cs
- RenderContext.cs
- PenLineCapValidation.cs
- IsolatedStorageFileStream.cs
- AuthenticatedStream.cs
- TextEditorParagraphs.cs
- ClientSession.cs
- SupportingTokenSecurityTokenResolver.cs
- ListComponentEditorPage.cs
- StreamInfo.cs
- GrammarBuilderWildcard.cs
- IndexedWhereQueryOperator.cs
- DetailsViewUpdateEventArgs.cs
- XmlWrappingWriter.cs
- Decorator.cs
- ValidatorCompatibilityHelper.cs
- MarkupCompilePass1.cs
- BamlLocalizabilityResolver.cs
- Symbol.cs
- XmlConvert.cs
- DiscoveryProxy.cs
- Query.cs
- HttpCapabilitiesEvaluator.cs
- XmlUrlResolver.cs
- ListBox.cs
- WindowsFormsHostPropertyMap.cs
- ObjectAnimationUsingKeyFrames.cs
- ProcessRequestArgs.cs
- WorkflowDefinitionDispenser.cs
- UrlMappingsSection.cs
- CrossSiteScriptingValidation.cs
- ButtonBase.cs
- TemplateParser.cs
- LabelEditEvent.cs
- NavigationHelper.cs
- XmlEncoding.cs
- EmbeddedMailObjectCollectionEditor.cs
- RecognizerStateChangedEventArgs.cs
- CustomCategoryAttribute.cs
- UnauthorizedWebPart.cs
- PreviousTrackingServiceAttribute.cs
- HttpUnhandledOperationInvoker.cs
- securestring.cs
- BindingCompleteEventArgs.cs
- TraceHwndHost.cs
- ItemsControlAutomationPeer.cs
- Utility.cs
- TileBrush.cs
- PreservationFileReader.cs
- EventTask.cs