1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
|
//------------------------------------------------------------
// 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
{
bool includeComments;
string algorithm;
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)
{
}
public ExclusiveCanonicalizationTransform(bool isCanonicalizationMethod, bool includeComments)
{
this.isCanonicalizationMethod = isCanonicalizationMethod;
this.includeComments = includeComments;
this.algorithm = includeComments ? XD.SecurityAlgorithmDictionary.ExclusiveC14nWithComments.Value : XD.SecurityAlgorithmDictionary.ExclusiveC14n.Value;
}
public override string Algorithm
{
get
{
return algorithm;
}
}
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, bool preserveComments)
{
XmlDictionaryString elementName = this.isCanonicalizationMethod ?
dictionaryManager.XmlSignatureDictionary.CanonicalizationMethod : dictionaryManager.XmlSignatureDictionary.Transform;
reader.MoveToStartElement(elementName, dictionaryManager.XmlSignatureDictionary.Namespace);
this.prefix = reader.Prefix;
bool isEmptyElement = reader.IsEmptyElement;
algorithm = reader.GetAttribute(dictionaryManager.XmlSignatureDictionary.Algorithm, null);
if (string.IsNullOrEmpty(algorithm))
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(SR.GetString(SR.ID0001, dictionaryManager.XmlSignatureDictionary.Algorithm, reader.LocalName)));
}
if (algorithm == dictionaryManager.SecurityAlgorithmDictionary.ExclusiveC14nWithComments.Value)
{
// to include comments in canonicalization, two conditions need to be met
// 1. the Reference must be an xpointer.
// 2. the transform must be #withComments
includeComments = preserveComments && true;
}
else if (algorithm == dictionaryManager.SecurityAlgorithmDictionary.ExclusiveC14n.Value)
{
includeComments = false;
}
else
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(SR.GetString(SR.ID6005, algorithm)));
}
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;
writer.WriteStartElement(this.prefix, elementName, dictionaryManager.XmlSignatureDictionary.Namespace);
writer.WriteAttributeString(dictionaryManager.XmlSignatureDictionary.Algorithm, null, algorithm);
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;
}
}
}
}
|