| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.IO; |
| | | 6 | | using System.Security.Cryptography; |
| | | 7 | | using System.Security.Cryptography.Xml; |
| | | 8 | | using System.Text; |
| | | 9 | | using System.Threading.Tasks; |
| | | 10 | | using System.Xml; |
| | | 11 | | using CoreWCF.Channels; |
| | | 12 | | using CoreWCF.Description; |
| | | 13 | | using CoreWCF.Diagnostics; |
| | | 14 | | using CoreWCF.IdentityModel; |
| | | 15 | | using CoreWCF.IdentityModel.Tokens; |
| | | 16 | | using CoreWCF.Runtime; |
| | | 17 | | using CoreWCF.Security.Tokens; |
| | | 18 | | using IPrefixGenerator = CoreWCF.IdentityModel.IPrefixGenerator; |
| | | 19 | | using ISecurityElement = CoreWCF.IdentityModel.ISecurityElement; |
| | | 20 | | using ISignatureValueSecurityElement = CoreWCF.IdentityModel.ISignatureValueSecurityElement; |
| | | 21 | | using KeyInfo = System.Security.Cryptography.Xml.KeyInfo; |
| | | 22 | | |
| | | 23 | | namespace CoreWCF.Security |
| | | 24 | | { |
| | | 25 | | internal class WSSecurityOneDotZeroSendSecurityHeader : SendSecurityHeader |
| | | 26 | | { |
| | | 27 | | private HashStream _hashStream; |
| | | 28 | | private SignedXml _signedXml; |
| | | 29 | | private KeyedHashAlgorithm _signingKey; |
| | | 30 | | private MessagePartSpecification _effectiveSignatureParts; |
| | | 31 | | |
| | | 32 | | // For Transport Security we have to sign the 'To' header with the |
| | | 33 | | // supporting tokens. |
| | | 34 | | private Stream _toHeaderStream = null; |
| | | 35 | | private string _toHeaderId = null; |
| | | 36 | | |
| | | 37 | | public WSSecurityOneDotZeroSendSecurityHeader(Message message, string actor, bool mustUnderstand, bool relay, |
| | | 38 | | SecurityStandardsManager standardsManager, |
| | | 39 | | SecurityAlgorithmSuite algorithmSuite, |
| | | 40 | | MessageDirection direction) |
| | 63 | 41 | | : base(message, actor, mustUnderstand, relay, standardsManager, algorithmSuite, direction) |
| | | 42 | | { |
| | 63 | 43 | | } |
| | | 44 | | |
| | | 45 | | protected string EncryptionAlgorithm |
| | | 46 | | { |
| | 0 | 47 | | get { return AlgorithmSuite.DefaultEncryptionAlgorithm; } |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | protected XmlDictionaryString EncryptionAlgorithmDictionaryString |
| | | 51 | | { |
| | 0 | 52 | | get { return AlgorithmSuite.DefaultEncryptionAlgorithmDictionaryString; } |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | private void AddEncryptionReference(MessageHeader header, string headerId, IPrefixGenerator prefixGenerator, boo |
| | | 56 | | out MemoryStream plainTextStream, out string encryptedDataId) |
| | | 57 | | { |
| | 0 | 58 | | throw new PlatformNotSupportedException(); |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | private void AddSignatureReference(SecurityToken token, int position, SecurityTokenAttachmentMode mode) |
| | | 62 | | { |
| | 0 | 63 | | bool strTransformEnabled = ShouldUseStrTransformForToken(token, position, mode, out SecurityKeyIdentifierCla |
| | 0 | 64 | | AddTokenSignatureReference(token, keyIdentifierClause, strTransformEnabled); |
| | 0 | 65 | | } |
| | | 66 | | |
| | | 67 | | private void AddPrimaryTokenSignatureReference(SecurityToken token, SecurityTokenParameters securityTokenParamet |
| | | 68 | | { |
| | 0 | 69 | | return; |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | // Given a token and useStarTransform value this method adds apporopriate reference accordingly. |
| | | 73 | | // 1. If strTransform is disabled, it adds a reference to the token's id. |
| | | 74 | | // 2. Else if strtransform is enabled it adds a reference the security token's keyIdentifier's id. |
| | | 75 | | private void AddTokenSignatureReference(SecurityToken token, SecurityKeyIdentifierClause keyIdentifierClause, bo |
| | | 76 | | { |
| | 0 | 77 | | if (strTransformEnabled) |
| | | 78 | | { |
| | 0 | 79 | | throw new PlatformNotSupportedException(); |
| | | 80 | | } |
| | 0 | 81 | | } |
| | | 82 | | |
| | | 83 | | private void AddSignatureReference(SendSecurityHeaderElement[] elements) |
| | | 84 | | { |
| | 0 | 85 | | if (elements != null) |
| | | 86 | | { |
| | 0 | 87 | | for (int i = 0; i < elements.Length; ++i) |
| | | 88 | | { |
| | | 89 | | // signedEncryptedTokenElement can either be a TokenElement ( in SignThenEncrypt case) or EncryptedD |
| | | 90 | | // STR-Transform does not make sense in !SignThenEncrypt case . |
| | | 91 | | // note: signedEncryptedTokenElement can also be SignatureConfirmation but we do not care about it h |
| | 0 | 92 | | bool useStrTransform = elements[i].Item is TokenElement signedEncryptedTokenElement |
| | 0 | 93 | | && SignThenEncrypt |
| | 0 | 94 | | && ShouldUseStrTransformForToken(signedEncryptedTokenElement.Token, |
| | 0 | 95 | | i, |
| | 0 | 96 | | SecurityTokenAttachmentMode.SignedEncry |
| | 0 | 97 | | out SecurityKeyIdentifierClause keyIden |
| | | 98 | | |
| | 0 | 99 | | if (!useStrTransform && elements[i].Id == null) |
| | | 100 | | { |
| | 0 | 101 | | throw TraceUtility.ThrowHelperError(new MessageSecurityException(SR.ElementToSignMustHaveId), Me |
| | | 102 | | } |
| | | 103 | | |
| | 0 | 104 | | MemoryStream stream = new MemoryStream(); |
| | 0 | 105 | | XmlDictionaryWriter utf8Writer = TakeUtf8Writer(); |
| | 0 | 106 | | utf8Writer.StartCanonicalization(stream, false, null); |
| | 0 | 107 | | elements[i].Item.WriteTo(utf8Writer, ServiceModelDictionaryManager.Instance); |
| | 0 | 108 | | utf8Writer.EndCanonicalization(); |
| | 0 | 109 | | stream.Position = 0; |
| | 0 | 110 | | if (useStrTransform) |
| | | 111 | | { |
| | 0 | 112 | | throw new PlatformNotSupportedException("StrTransform not supported yet"); |
| | | 113 | | } |
| | | 114 | | else |
| | | 115 | | { |
| | 0 | 116 | | AddReference("#" + elements[i].Id, stream); |
| | | 117 | | } |
| | | 118 | | } |
| | | 119 | | } |
| | 0 | 120 | | } |
| | | 121 | | |
| | | 122 | | private string GetSignatureHash(MessageHeader header, string headerId, IPrefixGenerator prefixGenerator, XmlDict |
| | | 123 | | { |
| | 0 | 124 | | HashStream hashStream = TakeHashStream(); |
| | | 125 | | XmlDictionaryWriter effectiveWriter; |
| | 0 | 126 | | XmlBuffer canonicalBuffer = null; |
| | | 127 | | |
| | 0 | 128 | | if (writer.CanCanonicalize) |
| | | 129 | | { |
| | 0 | 130 | | effectiveWriter = writer; |
| | | 131 | | } |
| | | 132 | | else |
| | | 133 | | { |
| | 0 | 134 | | canonicalBuffer = new XmlBuffer(int.MaxValue); |
| | 0 | 135 | | effectiveWriter = canonicalBuffer.OpenSection(XmlDictionaryReaderQuotas.Max); |
| | | 136 | | } |
| | | 137 | | |
| | 0 | 138 | | effectiveWriter.StartCanonicalization(hashStream, false, null); |
| | | 139 | | |
| | 0 | 140 | | header.WriteStartHeader(effectiveWriter, Version); |
| | 0 | 141 | | if (headerId == null) |
| | | 142 | | { |
| | 0 | 143 | | headerId = GenerateId(); |
| | 0 | 144 | | StandardsManager.IdManager.WriteIdAttribute(effectiveWriter, headerId); |
| | | 145 | | } |
| | 0 | 146 | | header.WriteHeaderContents(effectiveWriter, Version); |
| | 0 | 147 | | effectiveWriter.WriteEndElement(); |
| | 0 | 148 | | effectiveWriter.EndCanonicalization(); |
| | 0 | 149 | | effectiveWriter.Flush(); |
| | | 150 | | |
| | 0 | 151 | | if (!ReferenceEquals(effectiveWriter, writer)) |
| | | 152 | | { |
| | | 153 | | Fx.Assert(canonicalBuffer != null, "Canonical buffer cannot be null."); |
| | 0 | 154 | | canonicalBuffer.CloseSection(); |
| | 0 | 155 | | canonicalBuffer.Close(); |
| | 0 | 156 | | XmlDictionaryReader dicReader = canonicalBuffer.GetReader(0); |
| | 0 | 157 | | writer.WriteNode(dicReader, false); |
| | 0 | 158 | | dicReader.Close(); |
| | | 159 | | } |
| | | 160 | | |
| | 0 | 161 | | hash = hashStream.FlushHashAndGetValue(); |
| | | 162 | | |
| | 0 | 163 | | return headerId; |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | private async ValueTask<(string, byte[])> GetSignatureHashAsync(MessageHeader header, string headerId, IPrefixGe |
| | | 167 | | { |
| | 0 | 168 | | HashStream hashStream = TakeHashStream(); |
| | | 169 | | XmlDictionaryWriter effectiveWriter; |
| | 0 | 170 | | XmlBuffer canonicalBuffer = null; |
| | | 171 | | |
| | 0 | 172 | | if (writer.CanCanonicalize) |
| | | 173 | | { |
| | 0 | 174 | | effectiveWriter = writer; |
| | | 175 | | } |
| | | 176 | | else |
| | | 177 | | { |
| | 0 | 178 | | canonicalBuffer = new XmlBuffer(int.MaxValue); |
| | 0 | 179 | | effectiveWriter = canonicalBuffer.OpenSection(XmlDictionaryReaderQuotas.Max); |
| | | 180 | | } |
| | | 181 | | |
| | 0 | 182 | | effectiveWriter.StartCanonicalization(hashStream, false, null); |
| | | 183 | | |
| | 0 | 184 | | header.WriteStartHeader(effectiveWriter, Version); |
| | 0 | 185 | | if (headerId == null) |
| | | 186 | | { |
| | 0 | 187 | | headerId = GenerateId(); |
| | 0 | 188 | | StandardsManager.IdManager.WriteIdAttribute(effectiveWriter, headerId); |
| | | 189 | | } |
| | 0 | 190 | | header.WriteHeaderContents(effectiveWriter, Version); |
| | 0 | 191 | | await effectiveWriter.WriteEndElementAsync(); |
| | 0 | 192 | | effectiveWriter.EndCanonicalization(); |
| | 0 | 193 | | await effectiveWriter.FlushAsync(); |
| | | 194 | | |
| | 0 | 195 | | if (!ReferenceEquals(effectiveWriter, writer)) |
| | | 196 | | { |
| | | 197 | | Fx.Assert(canonicalBuffer != null, "Canonical buffer cannot be null."); |
| | 0 | 198 | | canonicalBuffer.CloseSection(); |
| | 0 | 199 | | canonicalBuffer.Close(); |
| | 0 | 200 | | XmlDictionaryReader dicReader = canonicalBuffer.GetReader(0); |
| | 0 | 201 | | await writer.WriteNodeAsync(dicReader, false); |
| | 0 | 202 | | dicReader.Close(); |
| | 0 | 203 | | } |
| | | 204 | | |
| | 0 | 205 | | var hash = hashStream.FlushHashAndGetValue(); |
| | | 206 | | |
| | 0 | 207 | | return (headerId, hash); |
| | 0 | 208 | | } |
| | | 209 | | |
| | | 210 | | private string GetSignatureStream(MessageHeader header, string headerId, IPrefixGenerator prefixGenerator, XmlDi |
| | | 211 | | { |
| | 0 | 212 | | stream = new MemoryStream(); |
| | | 213 | | XmlDictionaryWriter effectiveWriter; |
| | 0 | 214 | | XmlBuffer canonicalBuffer = null; |
| | | 215 | | |
| | 0 | 216 | | if (writer.CanCanonicalize) |
| | | 217 | | { |
| | 0 | 218 | | effectiveWriter = writer; |
| | | 219 | | } |
| | | 220 | | else |
| | | 221 | | { |
| | 0 | 222 | | canonicalBuffer = new XmlBuffer(int.MaxValue); |
| | 0 | 223 | | effectiveWriter = canonicalBuffer.OpenSection(XmlDictionaryReaderQuotas.Max); |
| | | 224 | | } |
| | | 225 | | |
| | 0 | 226 | | effectiveWriter.StartCanonicalization(stream, false, null); |
| | | 227 | | |
| | 0 | 228 | | header.WriteStartHeader(effectiveWriter, Version); |
| | 0 | 229 | | if (headerId == null) |
| | | 230 | | { |
| | 0 | 231 | | headerId = GenerateId(); |
| | 0 | 232 | | StandardsManager.IdManager.WriteIdAttribute(effectiveWriter, headerId); |
| | | 233 | | } |
| | 0 | 234 | | header.WriteHeaderContents(effectiveWriter, Version); |
| | 0 | 235 | | effectiveWriter.WriteEndElement(); |
| | 0 | 236 | | effectiveWriter.EndCanonicalization(); |
| | 0 | 237 | | effectiveWriter.Flush(); |
| | | 238 | | |
| | 0 | 239 | | if (!ReferenceEquals(effectiveWriter, writer)) |
| | | 240 | | { |
| | | 241 | | Fx.Assert(canonicalBuffer != null, "Canonical buffer cannot be null."); |
| | 0 | 242 | | canonicalBuffer.CloseSection(); |
| | 0 | 243 | | canonicalBuffer.Close(); |
| | 0 | 244 | | XmlDictionaryReader dicReader = canonicalBuffer.GetReader(0); |
| | 0 | 245 | | writer.WriteNode(dicReader, false); |
| | 0 | 246 | | dicReader.Close(); |
| | | 247 | | } |
| | | 248 | | |
| | 0 | 249 | | stream.Position = 0; |
| | | 250 | | |
| | 0 | 251 | | return headerId; |
| | | 252 | | } |
| | | 253 | | |
| | | 254 | | private async ValueTask<(string, Stream)> GetSignatureStreamAsync(MessageHeader header, string headerId, IPrefix |
| | | 255 | | { |
| | 0 | 256 | | Stream stream = new MemoryStream(); |
| | | 257 | | XmlDictionaryWriter effectiveWriter; |
| | 0 | 258 | | XmlBuffer canonicalBuffer = null; |
| | | 259 | | |
| | 0 | 260 | | if (writer.CanCanonicalize) |
| | | 261 | | { |
| | 0 | 262 | | effectiveWriter = writer; |
| | | 263 | | } |
| | | 264 | | else |
| | | 265 | | { |
| | 0 | 266 | | canonicalBuffer = new XmlBuffer(int.MaxValue); |
| | 0 | 267 | | effectiveWriter = canonicalBuffer.OpenSection(XmlDictionaryReaderQuotas.Max); |
| | | 268 | | } |
| | | 269 | | |
| | 0 | 270 | | effectiveWriter.StartCanonicalization(stream, false, null); |
| | | 271 | | |
| | 0 | 272 | | header.WriteStartHeader(effectiveWriter, Version); |
| | 0 | 273 | | if (headerId == null) |
| | | 274 | | { |
| | 0 | 275 | | headerId = GenerateId(); |
| | 0 | 276 | | StandardsManager.IdManager.WriteIdAttribute(effectiveWriter, headerId); |
| | | 277 | | } |
| | 0 | 278 | | header.WriteHeaderContents(effectiveWriter, Version); |
| | 0 | 279 | | await effectiveWriter.WriteEndElementAsync(); |
| | 0 | 280 | | effectiveWriter.EndCanonicalization(); |
| | 0 | 281 | | await effectiveWriter.FlushAsync(); |
| | | 282 | | |
| | 0 | 283 | | if (!ReferenceEquals(effectiveWriter, writer)) |
| | | 284 | | { |
| | | 285 | | Fx.Assert(canonicalBuffer != null, "Canonical buffer cannot be null."); |
| | 0 | 286 | | canonicalBuffer.CloseSection(); |
| | 0 | 287 | | canonicalBuffer.Close(); |
| | 0 | 288 | | XmlDictionaryReader dicReader = canonicalBuffer.GetReader(0); |
| | 0 | 289 | | await writer.WriteNodeAsync(dicReader, false); |
| | 0 | 290 | | dicReader.Close(); |
| | 0 | 291 | | } |
| | | 292 | | |
| | 0 | 293 | | stream.Position = 0; |
| | | 294 | | |
| | 0 | 295 | | return (headerId, stream); |
| | 0 | 296 | | } |
| | | 297 | | |
| | | 298 | | private void AddReference(string id, Stream contents) |
| | | 299 | | { |
| | 0 | 300 | | var reference = new System.Security.Cryptography.Xml.Reference(contents) |
| | 0 | 301 | | { |
| | 0 | 302 | | Uri = id, |
| | 0 | 303 | | DigestMethod = AlgorithmSuite.DefaultDigestAlgorithm |
| | 0 | 304 | | }; |
| | 0 | 305 | | reference.AddTransform(new XmlDsigExcC14NTransform()); |
| | 0 | 306 | | _signedXml.AddReference(reference); |
| | 0 | 307 | | } |
| | | 308 | | |
| | | 309 | | private void AddSignatureReference(MessageHeader header, string headerId, IPrefixGenerator prefixGenerator, XmlD |
| | | 310 | | { |
| | | 311 | | // No transforms added to Reference as the digest value has already been calculated |
| | 0 | 312 | | headerId = GetSignatureHash(header, headerId, prefixGenerator, writer, out byte[] hashValue); |
| | 0 | 313 | | var reference = new Reference |
| | 0 | 314 | | { |
| | 0 | 315 | | DigestMethod = AlgorithmSuite.DefaultDigestAlgorithm, |
| | 0 | 316 | | DigestValue = hashValue, |
| | 0 | 317 | | Id = headerId |
| | 0 | 318 | | }; |
| | 0 | 319 | | _signedXml.AddReference(reference); |
| | 0 | 320 | | } |
| | | 321 | | |
| | | 322 | | private async ValueTask AddSignatureReferenceAsync(MessageHeader header, string headerId, IPrefixGenerator prefi |
| | | 323 | | { |
| | | 324 | | // No transforms added to Reference as the digest value has already been calculated |
| | 0 | 325 | | var (generatedHeaderId, hashValue) = await GetSignatureHashAsync(header, headerId, prefixGenerator, writer); |
| | 0 | 326 | | headerId = generatedHeaderId; |
| | | 327 | | |
| | 0 | 328 | | var reference = new Reference |
| | 0 | 329 | | { |
| | 0 | 330 | | DigestMethod = AlgorithmSuite.DefaultDigestAlgorithm, |
| | 0 | 331 | | DigestValue = hashValue, |
| | 0 | 332 | | Id = headerId |
| | 0 | 333 | | }; |
| | 0 | 334 | | _signedXml.AddReference(reference); |
| | 0 | 335 | | } |
| | | 336 | | |
| | | 337 | | private void ApplySecurityAndWriteHeader(MessageHeader header, string headerId, XmlDictionaryWriter writer, IPre |
| | | 338 | | { |
| | 122 | 339 | | if (!RequireMessageProtection && ShouldSignToHeader) |
| | | 340 | | { |
| | 0 | 341 | | if ((header.Name == XD.AddressingDictionary.To.Value) && |
| | 0 | 342 | | (header.Namespace == Message.Version.Addressing.Namespace)) |
| | | 343 | | { |
| | 0 | 344 | | if (_toHeaderStream == null) |
| | | 345 | | { |
| | 0 | 346 | | headerId = GetSignatureStream(header, headerId, prefixGenerator, writer, out Stream headerStream |
| | 0 | 347 | | _toHeaderStream = headerStream; |
| | 0 | 348 | | _toHeaderId = headerId; |
| | | 349 | | } |
| | | 350 | | else |
| | | 351 | | { |
| | | 352 | | // More than one 'To' header is specified in the message. |
| | 0 | 353 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(SR.Transp |
| | | 354 | | } |
| | | 355 | | |
| | | 356 | | return; |
| | | 357 | | } |
| | | 358 | | } |
| | | 359 | | |
| | 122 | 360 | | MessagePartProtectionMode protectionMode = GetProtectionMode(header); |
| | | 361 | | switch (protectionMode) |
| | | 362 | | { |
| | | 363 | | case MessagePartProtectionMode.None: |
| | 122 | 364 | | header.WriteHeader(writer, Version); |
| | 122 | 365 | | return; |
| | | 366 | | case MessagePartProtectionMode.Sign: |
| | 0 | 367 | | AddSignatureReference(header, headerId, prefixGenerator, writer); |
| | 0 | 368 | | return; |
| | | 369 | | case MessagePartProtectionMode.SignThenEncrypt: |
| | | 370 | | case MessagePartProtectionMode.Encrypt: |
| | | 371 | | case MessagePartProtectionMode.EncryptThenSign: |
| | 0 | 372 | | throw new PlatformNotSupportedException(); |
| | | 373 | | default: |
| | | 374 | | Fx.Assert("Invalid MessagePartProtectionMode"); |
| | | 375 | | return; |
| | | 376 | | } |
| | | 377 | | } |
| | | 378 | | |
| | | 379 | | private async ValueTask ApplySecurityAndWriteHeaderAsync(MessageHeader header, string headerId, XmlDictionaryWri |
| | | 380 | | { |
| | 0 | 381 | | if (!RequireMessageProtection && ShouldSignToHeader) |
| | | 382 | | { |
| | 0 | 383 | | if ((header.Name == XD.AddressingDictionary.To.Value) && |
| | 0 | 384 | | (header.Namespace == Message.Version.Addressing.Namespace)) |
| | | 385 | | { |
| | 0 | 386 | | if (_toHeaderStream == null) |
| | | 387 | | { |
| | 0 | 388 | | var (generatedHeaderId, stream) = await GetSignatureStreamAsync(header, headerId, prefixGenerato |
| | 0 | 389 | | headerId = generatedHeaderId; |
| | | 390 | | |
| | 0 | 391 | | _toHeaderStream = stream; |
| | 0 | 392 | | _toHeaderId = headerId; |
| | | 393 | | } |
| | | 394 | | else |
| | | 395 | | { |
| | | 396 | | // More than one 'To' header is specified in the message. |
| | 0 | 397 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(SR.Transp |
| | | 398 | | } |
| | | 399 | | |
| | 0 | 400 | | return; |
| | | 401 | | } |
| | | 402 | | } |
| | | 403 | | |
| | 0 | 404 | | MessagePartProtectionMode protectionMode = GetProtectionMode(header); |
| | | 405 | | switch (protectionMode) |
| | | 406 | | { |
| | | 407 | | case MessagePartProtectionMode.None: |
| | 0 | 408 | | header.WriteHeader(writer, Version); |
| | 0 | 409 | | return; |
| | | 410 | | case MessagePartProtectionMode.Sign: |
| | 0 | 411 | | await AddSignatureReferenceAsync(header, headerId, prefixGenerator, writer); |
| | 0 | 412 | | return; |
| | | 413 | | case MessagePartProtectionMode.SignThenEncrypt: |
| | | 414 | | case MessagePartProtectionMode.Encrypt: |
| | | 415 | | case MessagePartProtectionMode.EncryptThenSign: |
| | 0 | 416 | | throw new PlatformNotSupportedException(); |
| | | 417 | | default: |
| | | 418 | | Fx.Assert("Invalid MessagePartProtectionMode"); |
| | 0 | 419 | | return; |
| | | 420 | | } |
| | 0 | 421 | | } |
| | | 422 | | |
| | | 423 | | public override void ApplySecurityAndWriteHeaders(MessageHeaders headers, XmlDictionaryWriter writer, IPrefixGen |
| | | 424 | | { |
| | | 425 | | string[] headerIds; |
| | 63 | 426 | | if (RequireMessageProtection || ShouldSignToHeader) |
| | | 427 | | { |
| | 0 | 428 | | headerIds = headers.GetHeaderAttributes(UtilityStrings.IdAttribute, |
| | 0 | 429 | | StandardsManager.IdManager.DefaultIdNamespaceUri); |
| | | 430 | | } |
| | | 431 | | else |
| | | 432 | | { |
| | 63 | 433 | | headerIds = null; |
| | | 434 | | } |
| | 370 | 435 | | for (int i = 0; i < headers.Count; i++) |
| | | 436 | | { |
| | 122 | 437 | | MessageHeader header = headers.GetMessageHeader(i); |
| | 122 | 438 | | if (Version.Addressing == AddressingVersion.None && header.Namespace == AddressingVersion.None.Namespace |
| | | 439 | | { |
| | | 440 | | continue; |
| | | 441 | | } |
| | | 442 | | |
| | 122 | 443 | | if (header != this) |
| | | 444 | | { |
| | 122 | 445 | | ApplySecurityAndWriteHeader(header, headerIds?[i], writer, prefixGenerator); |
| | | 446 | | } |
| | | 447 | | } |
| | 63 | 448 | | } |
| | | 449 | | |
| | | 450 | | public override async ValueTask ApplySecurityAndWriteHeadersAsync(MessageHeaders headers, XmlDictionaryWriter wr |
| | | 451 | | { |
| | | 452 | | string[] headerIds; |
| | 0 | 453 | | if (RequireMessageProtection || ShouldSignToHeader) |
| | | 454 | | { |
| | 0 | 455 | | headerIds = headers.GetHeaderAttributes(UtilityStrings.IdAttribute, |
| | 0 | 456 | | StandardsManager.IdManager.DefaultIdNamespaceUri); |
| | | 457 | | } |
| | | 458 | | else |
| | | 459 | | { |
| | 0 | 460 | | headerIds = null; |
| | | 461 | | } |
| | 0 | 462 | | for (int i = 0; i < headers.Count; i++) |
| | | 463 | | { |
| | 0 | 464 | | MessageHeader header = headers.GetMessageHeader(i); |
| | 0 | 465 | | if (Version.Addressing == AddressingVersion.None && header.Namespace == AddressingVersion.None.Namespace |
| | | 466 | | { |
| | | 467 | | continue; |
| | | 468 | | } |
| | | 469 | | |
| | 0 | 470 | | if (header != this) |
| | | 471 | | { |
| | 0 | 472 | | await ApplySecurityAndWriteHeaderAsync(header, headerIds?[i], writer, prefixGenerator); |
| | | 473 | | } |
| | | 474 | | } |
| | 0 | 475 | | } |
| | | 476 | | |
| | | 477 | | private static bool CanCanonicalizeAndFragment(XmlDictionaryWriter writer) |
| | | 478 | | { |
| | 0 | 479 | | if (!writer.CanCanonicalize) |
| | | 480 | | { |
| | 0 | 481 | | return false; |
| | | 482 | | } |
| | 0 | 483 | | return writer is IFragmentCapableXmlDictionaryWriter fragmentingWriter && fragmentingWriter.CanFragment; |
| | | 484 | | } |
| | | 485 | | |
| | | 486 | | public override void ApplyBodySecurity(XmlDictionaryWriter writer, IPrefixGenerator prefixGenerator) |
| | | 487 | | { |
| | 63 | 488 | | SecurityAppliedMessage message = SecurityAppliedMessage; |
| | 63 | 489 | | switch (message.BodyProtectionMode) |
| | | 490 | | { |
| | | 491 | | case MessagePartProtectionMode.None: |
| | 63 | 492 | | return; |
| | | 493 | | case MessagePartProtectionMode.Sign: |
| | 0 | 494 | | var ms = new MemoryStream(); |
| | 0 | 495 | | if (CanCanonicalizeAndFragment(writer)) |
| | | 496 | | { |
| | 0 | 497 | | message.WriteBodyToSignWithFragments(ms, false, null, writer); |
| | | 498 | | } |
| | | 499 | | else |
| | | 500 | | { |
| | 0 | 501 | | message.WriteBodyToSign(ms); |
| | | 502 | | } |
| | | 503 | | |
| | 0 | 504 | | ms.Position = 0; |
| | 0 | 505 | | AddReference("#" + message.BodyId, ms); |
| | 0 | 506 | | return; |
| | | 507 | | case MessagePartProtectionMode.SignThenEncrypt: |
| | 0 | 508 | | throw new PlatformNotSupportedException(); |
| | | 509 | | case MessagePartProtectionMode.Encrypt: |
| | 0 | 510 | | throw new PlatformNotSupportedException(); |
| | | 511 | | case MessagePartProtectionMode.EncryptThenSign: |
| | 0 | 512 | | throw new PlatformNotSupportedException(); |
| | | 513 | | default: |
| | | 514 | | Fx.Assert("Invalid MessagePartProtectionMode"); |
| | | 515 | | return; |
| | | 516 | | } |
| | | 517 | | } |
| | | 518 | | |
| | | 519 | | public override async ValueTask ApplyBodySecurityAsync(XmlDictionaryWriter writer, IPrefixGenerator prefixGenera |
| | | 520 | | { |
| | 0 | 521 | | SecurityAppliedMessage message = SecurityAppliedMessage; |
| | 0 | 522 | | switch (message.BodyProtectionMode) |
| | | 523 | | { |
| | | 524 | | case MessagePartProtectionMode.None: |
| | 0 | 525 | | return; |
| | | 526 | | case MessagePartProtectionMode.Sign: |
| | 0 | 527 | | var ms = new MemoryStream(); |
| | 0 | 528 | | if (CanCanonicalizeAndFragment(writer)) |
| | | 529 | | { |
| | 0 | 530 | | await message.WriteBodyToSignWithFragmentsAsync(ms, false, null, writer); |
| | | 531 | | } |
| | | 532 | | else |
| | | 533 | | { |
| | 0 | 534 | | await message.WriteBodyToSignAsync(ms); |
| | | 535 | | } |
| | | 536 | | |
| | 0 | 537 | | ms.Position = 0; |
| | 0 | 538 | | AddReference("#" + message.BodyId, ms); |
| | 0 | 539 | | return; |
| | | 540 | | case MessagePartProtectionMode.SignThenEncrypt: |
| | 0 | 541 | | throw new PlatformNotSupportedException(); |
| | | 542 | | case MessagePartProtectionMode.Encrypt: |
| | 0 | 543 | | throw new PlatformNotSupportedException(); |
| | | 544 | | case MessagePartProtectionMode.EncryptThenSign: |
| | 0 | 545 | | throw new PlatformNotSupportedException(); |
| | | 546 | | default: |
| | | 547 | | Fx.Assert("Invalid MessagePartProtectionMode"); |
| | 0 | 548 | | return; |
| | | 549 | | } |
| | 0 | 550 | | } |
| | | 551 | | |
| | | 552 | | protected override ISignatureValueSecurityElement CompletePrimarySignatureCore( |
| | | 553 | | SendSecurityHeaderElement[] signatureConfirmations, |
| | | 554 | | SecurityToken[] signedEndorsingTokens, |
| | | 555 | | SecurityToken[] signedTokens, |
| | | 556 | | SendSecurityHeaderElement[] basicTokens, bool isPrimarySignature) |
| | | 557 | | { |
| | 63 | 558 | | if (_signedXml == null) |
| | | 559 | | { |
| | 63 | 560 | | return null; |
| | | 561 | | } |
| | | 562 | | |
| | 0 | 563 | | SecurityTimestamp timestamp = Timestamp; |
| | 0 | 564 | | if (timestamp != null) |
| | | 565 | | { |
| | 0 | 566 | | if (timestamp.Id == null) |
| | | 567 | | { |
| | 0 | 568 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Timestamp |
| | | 569 | | } |
| | | 570 | | |
| | 0 | 571 | | byte[] buffer = new byte[64]; |
| | 0 | 572 | | var ms = new MemoryStream(); |
| | 0 | 573 | | StandardsManager.WSUtilitySpecificationVersion.WriteTimestampCanonicalForm( |
| | 0 | 574 | | ms, timestamp, buffer); |
| | 0 | 575 | | ms.Position = 0; |
| | 0 | 576 | | AddReference("#" + timestamp.Id, ms); |
| | 0 | 577 | | var reference = new System.Security.Cryptography.Xml.Reference(ms); |
| | | 578 | | } |
| | | 579 | | |
| | 0 | 580 | | if ((ShouldSignToHeader) && (_signingKey != null || _signedXml.SigningKey != null) && (Version.Addressing != |
| | | 581 | | { |
| | 0 | 582 | | if (_toHeaderStream != null) |
| | | 583 | | { |
| | 0 | 584 | | AddReference("#" + _toHeaderId, _toHeaderStream); |
| | | 585 | | } |
| | | 586 | | else |
| | | 587 | | { |
| | 0 | 588 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Transport |
| | | 589 | | } |
| | | 590 | | } |
| | | 591 | | |
| | 0 | 592 | | AddSignatureReference(signatureConfirmations); |
| | 0 | 593 | | if (isPrimarySignature && ShouldProtectTokens) |
| | | 594 | | { |
| | 0 | 595 | | AddPrimaryTokenSignatureReference(ElementContainer.SourceSigningToken, SigningTokenParameters); |
| | | 596 | | } |
| | | 597 | | |
| | 0 | 598 | | if (RequireMessageProtection) |
| | | 599 | | { |
| | 0 | 600 | | throw new PlatformNotSupportedException(nameof(RequireMessageProtection)); |
| | | 601 | | } |
| | | 602 | | |
| | 0 | 603 | | if (_signedXml.SignedInfo.References.Count == 0) |
| | | 604 | | { |
| | 0 | 605 | | throw TraceUtility.ThrowHelperError(new MessageSecurityException(SR.NoPartsOfMessageMatchedPartsToSign), |
| | | 606 | | } |
| | | 607 | | try |
| | | 608 | | { |
| | 0 | 609 | | if (_signingKey != null) |
| | | 610 | | { |
| | 0 | 611 | | _signedXml.ComputeSignature(_signingKey); |
| | | 612 | | } |
| | | 613 | | else |
| | | 614 | | { |
| | 0 | 615 | | _signedXml.ComputeSignature(); |
| | | 616 | | } |
| | | 617 | | |
| | 0 | 618 | | return new SignatureValue(_signedXml.Signature); |
| | | 619 | | } |
| | | 620 | | finally |
| | | 621 | | { |
| | 0 | 622 | | _hashStream = null; |
| | 0 | 623 | | _signingKey = null; |
| | 0 | 624 | | _signedXml = null; |
| | 0 | 625 | | _effectiveSignatureParts = null; |
| | 0 | 626 | | } |
| | 0 | 627 | | } |
| | | 628 | | |
| | | 629 | | internal class SignatureValue : ISignatureValueSecurityElement |
| | | 630 | | { |
| | | 631 | | private readonly Signature _signature; |
| | | 632 | | |
| | 0 | 633 | | public SignatureValue(Signature signature) |
| | | 634 | | { |
| | 0 | 635 | | _signature = signature; |
| | 0 | 636 | | } |
| | | 637 | | |
| | | 638 | | public void WriteTo(XmlDictionaryWriter writer, DictionaryManager dictionaryManager) |
| | | 639 | | { |
| | 0 | 640 | | _signature.GetXml().WriteTo(writer); |
| | 0 | 641 | | } |
| | | 642 | | |
| | | 643 | | public bool HasId |
| | | 644 | | { |
| | 0 | 645 | | get { return true; } |
| | | 646 | | } |
| | | 647 | | |
| | | 648 | | public string Id |
| | | 649 | | { |
| | 0 | 650 | | get { return _signature.Id; } |
| | | 651 | | } |
| | | 652 | | |
| | | 653 | | public byte[] GetSignatureValue() |
| | | 654 | | { |
| | 0 | 655 | | return _signature.SignatureValue; |
| | | 656 | | } |
| | | 657 | | } |
| | | 658 | | |
| | | 659 | | private HashStream TakeHashStream() |
| | | 660 | | { |
| | | 661 | | HashStream hashStream; |
| | 0 | 662 | | if (_hashStream == null) |
| | | 663 | | { |
| | 0 | 664 | | _hashStream = hashStream = new HashStream(CryptoHelper.CreateHashAlgorithm(AlgorithmSuite.DefaultDigestA |
| | | 665 | | } |
| | | 666 | | else |
| | | 667 | | { |
| | 0 | 668 | | hashStream = _hashStream; |
| | | 669 | | ; |
| | 0 | 670 | | hashStream.Reset(); |
| | | 671 | | } |
| | 0 | 672 | | return hashStream; |
| | | 673 | | } |
| | | 674 | | |
| | | 675 | | private XmlDictionaryWriter TakeUtf8Writer() |
| | | 676 | | { |
| | 0 | 677 | | throw new PlatformNotSupportedException(); |
| | | 678 | | } |
| | | 679 | | |
| | | 680 | | private MessagePartProtectionMode GetProtectionMode(MessageHeader header) |
| | | 681 | | { |
| | 122 | 682 | | if (!RequireMessageProtection) |
| | | 683 | | { |
| | 122 | 684 | | return MessagePartProtectionMode.None; |
| | | 685 | | } |
| | 0 | 686 | | bool sign = _signedXml != null && _effectiveSignatureParts.IsHeaderIncluded(header); |
| | 0 | 687 | | bool encrypt = false; |
| | 0 | 688 | | return MessagePartProtectionModeHelper.GetProtectionMode(sign, encrypt, SignThenEncrypt); |
| | | 689 | | } |
| | | 690 | | |
| | | 691 | | protected override void StartPrimarySignatureCore(SecurityToken token, |
| | | 692 | | SecurityKeyIdentifier keyIdentifier, |
| | | 693 | | MessagePartSpecification signatureParts, |
| | | 694 | | bool generateTargettableSignature) |
| | | 695 | | { |
| | 0 | 696 | | SecurityAlgorithmSuite suite = AlgorithmSuite; |
| | 0 | 697 | | string canonicalizationAlgorithm = suite.DefaultCanonicalizationAlgorithm; |
| | 0 | 698 | | if (canonicalizationAlgorithm != SecurityAlgorithms.ExclusiveC14n) |
| | | 699 | | { |
| | 0 | 700 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( |
| | 0 | 701 | | new MessageSecurityException(SR.Format(SR.UnsupportedCanonicalizationAlgorithm, suite.DefaultCanonic |
| | | 702 | | } |
| | 0 | 703 | | suite.GetSignatureAlgorithmAndKey(token, out string signatureAlgorithm, out SecurityKey signatureKey, out Xm |
| | 0 | 704 | | GetSigningAlgorithm(signatureKey, signatureAlgorithm, out _signingKey, out AsymmetricAlgorithm asymmetricAlg |
| | | 705 | | |
| | 0 | 706 | | _signedXml = new SignedXml(); |
| | 0 | 707 | | _signedXml.SignedInfo.CanonicalizationMethod = canonicalizationAlgorithm; |
| | 0 | 708 | | _signedXml.SignedInfo.SignatureMethod = signatureAlgorithm; |
| | 0 | 709 | | _signedXml.SigningKey = asymmetricAlgorithm; |
| | 0 | 710 | | if (keyIdentifier != null) |
| | | 711 | | { |
| | 0 | 712 | | var stream = new MemoryStream(); |
| | 0 | 713 | | using (var xmlWriter = XmlDictionaryWriter.CreateTextWriter(stream, Encoding.UTF8, false)) |
| | | 714 | | { |
| | 0 | 715 | | StandardsManager.SecurityTokenSerializer.WriteKeyIdentifier(xmlWriter, keyIdentifier); |
| | 0 | 716 | | } |
| | | 717 | | |
| | 0 | 718 | | stream.Position = 0; |
| | 0 | 719 | | XmlDocument doc = new XmlDocument(); |
| | 0 | 720 | | doc.Load(stream); |
| | 0 | 721 | | var keyInfo = new KeyInfo(); |
| | 0 | 722 | | keyInfo.LoadXml(doc.DocumentElement); |
| | 0 | 723 | | _signedXml.KeyInfo = keyInfo; |
| | | 724 | | } |
| | | 725 | | |
| | 0 | 726 | | if (generateTargettableSignature) |
| | | 727 | | { |
| | 0 | 728 | | _signedXml.Signature.Id = GenerateId(); |
| | | 729 | | } |
| | 0 | 730 | | _effectiveSignatureParts = signatureParts; |
| | 0 | 731 | | } |
| | | 732 | | |
| | | 733 | | private void GetSigningAlgorithm(SecurityKey signatureKey, string algorithmName, out KeyedHashAlgorithm symmetri |
| | | 734 | | { |
| | 0 | 735 | | symmetricAlgorithm = null; |
| | 0 | 736 | | asymmetricAlgorithm = null; |
| | 0 | 737 | | if (signatureKey is SymmetricSecurityKey symmetricKey) |
| | | 738 | | { |
| | 0 | 739 | | _signingKey = symmetricKey.GetKeyedHashAlgorithm(algorithmName); |
| | 0 | 740 | | if (_signingKey == null) |
| | | 741 | | { |
| | 0 | 742 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 743 | | SR.Format("UnableToCreateKeyedHashAlgorithm", symmetricKey, algorithmName))); |
| | | 744 | | } |
| | | 745 | | } |
| | | 746 | | else |
| | | 747 | | { |
| | 0 | 748 | | if (!(signatureKey is AsymmetricSecurityKey asymmetricKey)) |
| | | 749 | | { |
| | 0 | 750 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 751 | | SR.Format(SR.UnknownICryptoType, _signingKey))); |
| | | 752 | | } |
| | | 753 | | |
| | 0 | 754 | | asymmetricAlgorithm = asymmetricKey.GetAsymmetricAlgorithm(algorithmName, privateKey: true); |
| | 0 | 755 | | if (asymmetricAlgorithm == null) |
| | | 756 | | { |
| | | 757 | | //TODO MUST before checkin search and replace SR.Format(" |
| | 0 | 758 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 759 | | SR.Format("UnableToCreateHashAlgorithmFromAsymmetricCrypto", algorithmName, |
| | 0 | 760 | | asymmetricKey))); |
| | | 761 | | } |
| | | 762 | | } |
| | 0 | 763 | | } |
| | | 764 | | |
| | | 765 | | protected override ISignatureValueSecurityElement CreateSupportingSignature(SecurityToken token, SecurityKeyIden |
| | | 766 | | { |
| | 0 | 767 | | StartPrimarySignatureCore(token, identifier, MessagePartSpecification.NoParts, false); |
| | 0 | 768 | | return CompletePrimarySignatureCore(null, null, null, null, false); |
| | | 769 | | } |
| | | 770 | | |
| | | 771 | | protected override ISignatureValueSecurityElement CreateSupportingSignature(SecurityToken token, SecurityKeyIden |
| | | 772 | | { |
| | 0 | 773 | | AlgorithmSuite.GetSignatureAlgorithmAndKey(token, out string signatureAlgorithm, out SecurityKey signatureKe |
| | | 774 | | |
| | 0 | 775 | | SignedXml signedXml = new SignedXml(); |
| | 0 | 776 | | SignedInfo signedInfo = signedXml.SignedInfo; |
| | 0 | 777 | | signedInfo.CanonicalizationMethod = AlgorithmSuite.DefaultCanonicalizationAlgorithm; |
| | 0 | 778 | | signedInfo.SignatureMethod = signatureAlgorithm; |
| | | 779 | | |
| | 0 | 780 | | if (elementToSign.Id == null) |
| | | 781 | | { |
| | 0 | 782 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.ElementToSign |
| | | 783 | | } |
| | | 784 | | |
| | 0 | 785 | | MemoryStream stream = new MemoryStream(); |
| | 0 | 786 | | XmlDictionaryWriter utf8Writer = TakeUtf8Writer(); |
| | 0 | 787 | | utf8Writer.StartCanonicalization(stream, false, null); |
| | 0 | 788 | | elementToSign.WriteTo(utf8Writer, ServiceModelDictionaryManager.Instance); |
| | 0 | 789 | | utf8Writer.EndCanonicalization(); |
| | 0 | 790 | | stream.Position = 0; |
| | 0 | 791 | | AddReference("#" + elementToSign.Id, stream); |
| | | 792 | | |
| | 0 | 793 | | GetSigningAlgorithm(signatureKey, signatureAlgorithm, out KeyedHashAlgorithm keyedHashAlgorithm, out Asymmet |
| | 0 | 794 | | if (keyedHashAlgorithm != null) |
| | | 795 | | { |
| | 0 | 796 | | signedXml.ComputeSignature(keyedHashAlgorithm); |
| | | 797 | | } |
| | | 798 | | else |
| | | 799 | | { |
| | 0 | 800 | | signedXml.SigningKey = asymmetricAlgorithm; |
| | 0 | 801 | | signedXml.ComputeSignature(); |
| | | 802 | | } |
| | | 803 | | |
| | 0 | 804 | | SetKeyInfo(signedXml, identifier); |
| | 0 | 805 | | return new SignatureValue(signedXml.Signature); |
| | | 806 | | } |
| | | 807 | | |
| | | 808 | | private void SetKeyInfo(SignedXml signedXml, SecurityKeyIdentifier identifier) |
| | | 809 | | { |
| | 0 | 810 | | if (identifier != null) |
| | | 811 | | { |
| | 0 | 812 | | var stream = new MemoryStream(); |
| | 0 | 813 | | using (var xmlWriter = XmlDictionaryWriter.CreateTextWriter(stream, Encoding.UTF8, false)) |
| | | 814 | | { |
| | 0 | 815 | | StandardsManager.SecurityTokenSerializer.WriteKeyIdentifier(xmlWriter, identifier); |
| | 0 | 816 | | } |
| | | 817 | | |
| | 0 | 818 | | stream.Position = 0; |
| | 0 | 819 | | XmlDocument doc = new XmlDocument(); |
| | 0 | 820 | | doc.Load(stream); |
| | 0 | 821 | | var keyInfo = new KeyInfo(); |
| | 0 | 822 | | keyInfo.LoadXml(doc.DocumentElement); |
| | 0 | 823 | | signedXml.KeyInfo = keyInfo; |
| | | 824 | | } |
| | 0 | 825 | | } |
| | | 826 | | |
| | | 827 | | protected override void WriteSecurityTokenReferencyEntry(XmlDictionaryWriter writer, SecurityToken securityToken |
| | | 828 | | { |
| | 0 | 829 | | return; |
| | | 830 | | } |
| | | 831 | | |
| | | 832 | | protected override void StartEncryptionCore(SecurityToken token, SecurityKeyIdentifier keyIdentifier) |
| | | 833 | | { |
| | 0 | 834 | | throw new NotImplementedException(); |
| | | 835 | | } |
| | | 836 | | |
| | | 837 | | protected override ISecurityElement CompleteEncryptionCore(SendSecurityHeaderElement primarySignature, SendSecur |
| | | 838 | | { |
| | 63 | 839 | | return null; |
| | | 840 | | } |
| | | 841 | | } |
| | | 842 | | |
| | | 843 | | internal class WrappedXmlDictionaryWriter : XmlDictionaryWriter |
| | | 844 | | { |
| | | 845 | | private readonly XmlDictionaryWriter _innerWriter; |
| | | 846 | | private int _index; |
| | | 847 | | private bool _insertId; |
| | | 848 | | private bool _isStrReferenceElement; |
| | | 849 | | private readonly string _id; |
| | | 850 | | |
| | | 851 | | public WrappedXmlDictionaryWriter(XmlDictionaryWriter writer, string id) |
| | | 852 | | { |
| | | 853 | | _innerWriter = writer; |
| | | 854 | | _index = 0; |
| | | 855 | | _insertId = false; |
| | | 856 | | _isStrReferenceElement = false; |
| | | 857 | | _id = id; |
| | | 858 | | } |
| | | 859 | | |
| | | 860 | | public override void WriteStartAttribute(string prefix, string localName, string namespaceUri) |
| | | 861 | | { |
| | | 862 | | if (_isStrReferenceElement && _insertId && localName == XD.UtilityDictionary.IdAttribute.Value) |
| | | 863 | | { |
| | | 864 | | // This means the serializer is already writing the Id out, so we don't write it again. |
| | | 865 | | _insertId = false; |
| | | 866 | | } |
| | | 867 | | _innerWriter.WriteStartAttribute(prefix, localName, namespaceUri); |
| | | 868 | | } |
| | | 869 | | |
| | | 870 | | public override void WriteStartElement(string prefix, string localName, string namespaceUri) |
| | | 871 | | { |
| | | 872 | | if (_isStrReferenceElement && _insertId) |
| | | 873 | | { |
| | | 874 | | if (_id != null) |
| | | 875 | | { |
| | | 876 | | _innerWriter.WriteAttributeString(XD.UtilityDictionary.Prefix.Value, XD.UtilityDictionary.IdAttribut |
| | | 877 | | } |
| | | 878 | | |
| | | 879 | | _isStrReferenceElement = false; |
| | | 880 | | _insertId = false; |
| | | 881 | | } |
| | | 882 | | |
| | | 883 | | _index++; |
| | | 884 | | |
| | | 885 | | if (_index == 1 && localName == XD.SecurityJan2004Dictionary.SecurityTokenReference.Value) |
| | | 886 | | { |
| | | 887 | | _insertId = true; |
| | | 888 | | _isStrReferenceElement = true; |
| | | 889 | | } |
| | | 890 | | |
| | | 891 | | _innerWriter.WriteStartElement(prefix, localName, namespaceUri); |
| | | 892 | | } |
| | | 893 | | |
| | | 894 | | // Below methods simply call into innerWritter |
| | | 895 | | public override void Close() |
| | | 896 | | { |
| | | 897 | | _innerWriter.Close(); |
| | | 898 | | } |
| | | 899 | | |
| | | 900 | | public override void Flush() |
| | | 901 | | { |
| | | 902 | | _innerWriter.Flush(); |
| | | 903 | | } |
| | | 904 | | |
| | | 905 | | public override string LookupPrefix(string ns) |
| | | 906 | | { |
| | | 907 | | return _innerWriter.LookupPrefix(ns); |
| | | 908 | | } |
| | | 909 | | |
| | | 910 | | public override void WriteBase64(byte[] buffer, int index, int count) |
| | | 911 | | { |
| | | 912 | | _innerWriter.WriteBase64(buffer, index, count); |
| | | 913 | | } |
| | | 914 | | |
| | | 915 | | public override void WriteCData(string text) |
| | | 916 | | { |
| | | 917 | | _innerWriter.WriteCData(text); |
| | | 918 | | } |
| | | 919 | | |
| | | 920 | | public override void WriteCharEntity(char ch) |
| | | 921 | | { |
| | | 922 | | _innerWriter.WriteCharEntity(ch); |
| | | 923 | | } |
| | | 924 | | |
| | | 925 | | public override void WriteChars(char[] buffer, int index, int count) |
| | | 926 | | { |
| | | 927 | | _innerWriter.WriteChars(buffer, index, count); |
| | | 928 | | } |
| | | 929 | | |
| | | 930 | | public override void WriteComment(string text) |
| | | 931 | | { |
| | | 932 | | _innerWriter.WriteComment(text); |
| | | 933 | | } |
| | | 934 | | |
| | | 935 | | public override void WriteDocType(string name, string pubid, string sysid, string subset) |
| | | 936 | | { |
| | | 937 | | _innerWriter.WriteDocType(name, pubid, sysid, subset); |
| | | 938 | | } |
| | | 939 | | |
| | | 940 | | public override void WriteEndAttribute() |
| | | 941 | | { |
| | | 942 | | _innerWriter.WriteEndAttribute(); |
| | | 943 | | } |
| | | 944 | | |
| | | 945 | | public override void WriteEndDocument() |
| | | 946 | | { |
| | | 947 | | _innerWriter.WriteEndDocument(); |
| | | 948 | | } |
| | | 949 | | |
| | | 950 | | public override void WriteEndElement() |
| | | 951 | | { |
| | | 952 | | _innerWriter.WriteEndElement(); |
| | | 953 | | } |
| | | 954 | | |
| | | 955 | | public override void WriteEntityRef(string name) |
| | | 956 | | { |
| | | 957 | | _innerWriter.WriteEntityRef(name); |
| | | 958 | | } |
| | | 959 | | |
| | | 960 | | public override void WriteFullEndElement() |
| | | 961 | | { |
| | | 962 | | _innerWriter.WriteFullEndElement(); |
| | | 963 | | } |
| | | 964 | | |
| | | 965 | | public override void WriteProcessingInstruction(string name, string text) |
| | | 966 | | { |
| | | 967 | | _innerWriter.WriteProcessingInstruction(name, text); |
| | | 968 | | } |
| | | 969 | | |
| | | 970 | | public override void WriteRaw(string data) |
| | | 971 | | { |
| | | 972 | | _innerWriter.WriteRaw(data); |
| | | 973 | | } |
| | | 974 | | |
| | | 975 | | public override void WriteRaw(char[] buffer, int index, int count) |
| | | 976 | | { |
| | | 977 | | _innerWriter.WriteRaw(buffer, index, count); |
| | | 978 | | } |
| | | 979 | | |
| | | 980 | | public override void WriteStartDocument(bool standalone) |
| | | 981 | | { |
| | | 982 | | _innerWriter.WriteStartDocument(standalone); |
| | | 983 | | } |
| | | 984 | | |
| | | 985 | | public override void WriteStartDocument() |
| | | 986 | | { |
| | | 987 | | _innerWriter.WriteStartDocument(); |
| | | 988 | | } |
| | | 989 | | |
| | | 990 | | public override WriteState WriteState |
| | | 991 | | { |
| | | 992 | | get { return _innerWriter.WriteState; } |
| | | 993 | | } |
| | | 994 | | |
| | | 995 | | public override void WriteString(string text) |
| | | 996 | | { |
| | | 997 | | _innerWriter.WriteString(text); |
| | | 998 | | } |
| | | 999 | | |
| | | 1000 | | public override void WriteSurrogateCharEntity(char lowChar, char highChar) |
| | | 1001 | | { |
| | | 1002 | | _innerWriter.WriteSurrogateCharEntity(lowChar, highChar); |
| | | 1003 | | } |
| | | 1004 | | |
| | | 1005 | | public override void WriteWhitespace(string ws) |
| | | 1006 | | { |
| | | 1007 | | _innerWriter.WriteWhitespace(ws); |
| | | 1008 | | } |
| | | 1009 | | } |
| | | 1010 | | } |
| | | 1011 | | |