| | | 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.Diagnostics; |
| | | 6 | | using System.IO; |
| | | 7 | | using System.Xml; |
| | | 8 | | using CoreWCF.Channels; |
| | | 9 | | using CoreWCF.Diagnostics; |
| | | 10 | | using CoreWCF.Runtime; |
| | | 11 | | |
| | | 12 | | namespace CoreWCF.Security |
| | | 13 | | { |
| | | 14 | | internal sealed class SecurityVerifiedMessage : DelegatingMessage |
| | | 15 | | { |
| | | 16 | | private byte[] _decryptedBuffer; |
| | | 17 | | private XmlDictionaryReader _cachedDecryptedBodyContentReader; |
| | | 18 | | private XmlAttributeHolder[] _envelopeAttributes; |
| | | 19 | | private XmlAttributeHolder[] _headerAttributes; |
| | | 20 | | private XmlAttributeHolder[] _bodyAttributes; |
| | | 21 | | private string _envelopePrefix; |
| | | 22 | | private bool _bodyDecrypted; |
| | | 23 | | private BodyState _state = BodyState.Created; |
| | | 24 | | private string _bodyPrefix; |
| | | 25 | | private bool _isDecryptedBodyStatusDetermined; |
| | | 26 | | private bool _isDecryptedBodyFault; |
| | | 27 | | private bool _isDecryptedBodyEmpty; |
| | | 28 | | private XmlDictionaryReader _cachedReaderAtSecurityHeader; |
| | | 29 | | private XmlBuffer _messageBuffer; |
| | | 30 | | private bool _canDelegateCreateBufferedCopyToInnerMessage; |
| | | 31 | | |
| | | 32 | | public SecurityVerifiedMessage(Message messageToProcess, ReceiveSecurityHeader securityHeader) |
| | 94 | 33 | | : base(messageToProcess) |
| | | 34 | | { |
| | 94 | 35 | | ReceivedSecurityHeader = securityHeader; |
| | 94 | 36 | | if (securityHeader.RequireMessageProtection) |
| | | 37 | | { |
| | | 38 | | XmlDictionaryReader messageReader; |
| | 0 | 39 | | if (InnerMessage is BufferedMessage bufferedMessage && Headers.ContainsOnlyBufferedMessageHeaders) |
| | | 40 | | { |
| | 0 | 41 | | messageReader = bufferedMessage.GetMessageReader(); |
| | | 42 | | } |
| | | 43 | | else |
| | | 44 | | { |
| | 0 | 45 | | _messageBuffer = new XmlBuffer(int.MaxValue); |
| | 0 | 46 | | XmlDictionaryWriter writer = _messageBuffer.OpenSection(ReceivedSecurityHeader.ReaderQuotas); |
| | 0 | 47 | | InnerMessage.WriteMessage(writer); |
| | 0 | 48 | | _messageBuffer.CloseSection(); |
| | 0 | 49 | | _messageBuffer.Close(); |
| | 0 | 50 | | messageReader = _messageBuffer.GetReader(0); |
| | | 51 | | } |
| | 0 | 52 | | MoveToSecurityHeader(messageReader, securityHeader.HeaderIndex, true); |
| | 0 | 53 | | _cachedReaderAtSecurityHeader = messageReader; |
| | 0 | 54 | | _state = BodyState.Buffered; |
| | | 55 | | } |
| | | 56 | | else |
| | | 57 | | { |
| | 94 | 58 | | _envelopeAttributes = XmlAttributeHolder.emptyArray; |
| | 94 | 59 | | _headerAttributes = XmlAttributeHolder.emptyArray; |
| | 94 | 60 | | _bodyAttributes = XmlAttributeHolder.emptyArray; |
| | 94 | 61 | | _canDelegateCreateBufferedCopyToInnerMessage = true; |
| | | 62 | | } |
| | 94 | 63 | | } |
| | | 64 | | |
| | | 65 | | public override bool IsEmpty |
| | | 66 | | { |
| | | 67 | | get |
| | | 68 | | { |
| | 106 | 69 | | if (IsDisposed) |
| | | 70 | | { |
| | 0 | 71 | | throw TraceUtility.ThrowHelperError(CreateMessageDisposedException(), this); |
| | | 72 | | } |
| | 106 | 73 | | if (!_bodyDecrypted) |
| | | 74 | | { |
| | 106 | 75 | | return InnerMessage.IsEmpty; |
| | | 76 | | } |
| | | 77 | | |
| | 0 | 78 | | EnsureDecryptedBodyStatusDetermined(); |
| | | 79 | | |
| | 0 | 80 | | return _isDecryptedBodyEmpty; |
| | | 81 | | } |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | public override bool IsFault |
| | | 85 | | { |
| | | 86 | | get |
| | | 87 | | { |
| | 0 | 88 | | if (IsDisposed) |
| | | 89 | | { |
| | 0 | 90 | | throw TraceUtility.ThrowHelperError(CreateMessageDisposedException(), this); |
| | | 91 | | } |
| | 0 | 92 | | if (!_bodyDecrypted) |
| | | 93 | | { |
| | 0 | 94 | | return InnerMessage.IsFault; |
| | | 95 | | } |
| | | 96 | | |
| | 0 | 97 | | EnsureDecryptedBodyStatusDetermined(); |
| | | 98 | | |
| | 0 | 99 | | return _isDecryptedBodyFault; |
| | | 100 | | } |
| | | 101 | | } |
| | | 102 | | |
| | 0 | 103 | | internal byte[] PrimarySignatureValue => ReceivedSecurityHeader.PrimarySignatureValue; |
| | | 104 | | |
| | 94 | 105 | | internal ReceiveSecurityHeader ReceivedSecurityHeader { get; } |
| | | 106 | | |
| | | 107 | | private Exception CreateBadStateException(string operation) |
| | | 108 | | { |
| | 0 | 109 | | return new InvalidOperationException(SR.Format(SR.MessageBodyOperationNotValidInBodyState, |
| | 0 | 110 | | operation, _state)); |
| | | 111 | | } |
| | | 112 | | |
| | | 113 | | public XmlDictionaryReader CreateFullBodyReader() |
| | | 114 | | { |
| | 0 | 115 | | switch (_state) |
| | | 116 | | { |
| | | 117 | | case BodyState.Buffered: |
| | 0 | 118 | | return CreateFullBodyReaderFromBufferedState(); |
| | | 119 | | case BodyState.Decrypted: |
| | 0 | 120 | | return CreateFullBodyReaderFromDecryptedState(); |
| | | 121 | | default: |
| | 0 | 122 | | throw TraceUtility.ThrowHelperError(CreateBadStateException("CreateFullBodyReader"), this); |
| | | 123 | | } |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | private XmlDictionaryReader CreateFullBodyReaderFromBufferedState() |
| | | 127 | | { |
| | 0 | 128 | | if (_messageBuffer != null) |
| | | 129 | | { |
| | 0 | 130 | | XmlDictionaryReader reader = _messageBuffer.GetReader(0); |
| | 0 | 131 | | MoveToBody(reader); |
| | 0 | 132 | | return reader; |
| | | 133 | | } |
| | | 134 | | else |
| | | 135 | | { |
| | 0 | 136 | | return ((BufferedMessage)InnerMessage).GetBufferedReaderAtBody(); |
| | | 137 | | } |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | private XmlDictionaryReader CreateFullBodyReaderFromDecryptedState() |
| | | 141 | | { |
| | 0 | 142 | | XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(_decryptedBuffer, 0, _decryptedBuffer.Leng |
| | 0 | 143 | | MoveToBody(reader); |
| | 0 | 144 | | return reader; |
| | | 145 | | } |
| | | 146 | | |
| | | 147 | | private void EnsureDecryptedBodyStatusDetermined() |
| | | 148 | | { |
| | 0 | 149 | | if (!_isDecryptedBodyStatusDetermined) |
| | | 150 | | { |
| | 0 | 151 | | XmlDictionaryReader reader = CreateFullBodyReader(); |
| | 0 | 152 | | if (ReadStartBody(reader, InnerMessage.Version.Envelope, out _isDecryptedBodyFault, out _isDecryptedBody |
| | | 153 | | { |
| | 0 | 154 | | _cachedDecryptedBodyContentReader = reader; |
| | | 155 | | } |
| | | 156 | | else |
| | | 157 | | { |
| | 0 | 158 | | reader.Close(); |
| | | 159 | | } |
| | 0 | 160 | | _isDecryptedBodyStatusDetermined = true; |
| | | 161 | | } |
| | 0 | 162 | | } |
| | | 163 | | |
| | | 164 | | public XmlAttributeHolder[] GetEnvelopeAttributes() |
| | | 165 | | { |
| | 0 | 166 | | return _envelopeAttributes; |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | public XmlAttributeHolder[] GetHeaderAttributes() |
| | | 170 | | { |
| | 0 | 171 | | return _headerAttributes; |
| | | 172 | | } |
| | | 173 | | |
| | | 174 | | private XmlDictionaryReader GetReaderAtEnvelope() |
| | | 175 | | { |
| | 0 | 176 | | if (_messageBuffer != null) |
| | | 177 | | { |
| | 0 | 178 | | return _messageBuffer.GetReader(0); |
| | | 179 | | } |
| | | 180 | | else |
| | | 181 | | { |
| | 0 | 182 | | return ((BufferedMessage)InnerMessage).GetMessageReader(); |
| | | 183 | | } |
| | | 184 | | } |
| | | 185 | | |
| | | 186 | | public XmlDictionaryReader GetReaderAtFirstHeader() |
| | | 187 | | { |
| | 0 | 188 | | XmlDictionaryReader reader = GetReaderAtEnvelope(); |
| | 0 | 189 | | MoveToHeaderBlock(reader, false); |
| | 0 | 190 | | reader.ReadStartElement(); |
| | 0 | 191 | | return reader; |
| | | 192 | | } |
| | | 193 | | |
| | | 194 | | public XmlDictionaryReader GetReaderAtSecurityHeader() |
| | | 195 | | { |
| | 94 | 196 | | if (_cachedReaderAtSecurityHeader != null) |
| | | 197 | | { |
| | 0 | 198 | | XmlDictionaryReader result = _cachedReaderAtSecurityHeader; |
| | 0 | 199 | | _cachedReaderAtSecurityHeader = null; |
| | 0 | 200 | | return result; |
| | | 201 | | } |
| | 94 | 202 | | return Headers.GetReaderAtHeader(ReceivedSecurityHeader.HeaderIndex); |
| | | 203 | | } |
| | | 204 | | |
| | | 205 | | private void MoveToBody(XmlDictionaryReader reader) |
| | | 206 | | { |
| | 0 | 207 | | if (reader.NodeType != XmlNodeType.Element) |
| | | 208 | | { |
| | 0 | 209 | | reader.MoveToContent(); |
| | | 210 | | } |
| | 0 | 211 | | reader.ReadStartElement(); |
| | 0 | 212 | | if (reader.IsStartElement(XD.MessageDictionary.Header, Version.Envelope.DictionaryNamespace)) |
| | | 213 | | { |
| | 0 | 214 | | reader.Skip(); |
| | | 215 | | } |
| | 0 | 216 | | if (reader.NodeType != XmlNodeType.Element) |
| | | 217 | | { |
| | 0 | 218 | | reader.MoveToContent(); |
| | | 219 | | } |
| | 0 | 220 | | } |
| | | 221 | | |
| | | 222 | | private void MoveToHeaderBlock(XmlDictionaryReader reader, bool captureAttributes) |
| | | 223 | | { |
| | 0 | 224 | | if (reader.NodeType != XmlNodeType.Element) |
| | | 225 | | { |
| | 0 | 226 | | reader.MoveToContent(); |
| | | 227 | | } |
| | 0 | 228 | | if (captureAttributes) |
| | | 229 | | { |
| | 0 | 230 | | _envelopePrefix = reader.Prefix; |
| | 0 | 231 | | _envelopeAttributes = XmlAttributeHolder.ReadAttributes(reader); |
| | | 232 | | } |
| | 0 | 233 | | reader.ReadStartElement(); |
| | 0 | 234 | | reader.MoveToStartElement(XD.MessageDictionary.Header, Version.Envelope.DictionaryNamespace); |
| | 0 | 235 | | if (captureAttributes) |
| | | 236 | | { |
| | 0 | 237 | | _headerAttributes = XmlAttributeHolder.ReadAttributes(reader); |
| | | 238 | | } |
| | 0 | 239 | | } |
| | | 240 | | |
| | | 241 | | private void MoveToSecurityHeader(XmlDictionaryReader reader, int headerIndex, bool captureAttributes) |
| | | 242 | | { |
| | 0 | 243 | | MoveToHeaderBlock(reader, captureAttributes); |
| | 0 | 244 | | reader.ReadStartElement(); |
| | 0 | 245 | | while (true) |
| | | 246 | | { |
| | 0 | 247 | | if (reader.NodeType != XmlNodeType.Element) |
| | | 248 | | { |
| | 0 | 249 | | reader.MoveToContent(); |
| | | 250 | | } |
| | 0 | 251 | | if (headerIndex == 0) |
| | | 252 | | { |
| | | 253 | | break; |
| | | 254 | | } |
| | 0 | 255 | | reader.Skip(); |
| | 0 | 256 | | headerIndex--; |
| | | 257 | | } |
| | 0 | 258 | | } |
| | | 259 | | |
| | | 260 | | protected override void OnBodyToString(XmlDictionaryWriter writer) |
| | | 261 | | { |
| | 0 | 262 | | if (_state == BodyState.Created) |
| | | 263 | | { |
| | 0 | 264 | | base.OnBodyToString(writer); |
| | | 265 | | } |
| | | 266 | | else |
| | | 267 | | { |
| | 0 | 268 | | OnWriteBodyContents(writer); |
| | | 269 | | } |
| | 0 | 270 | | } |
| | | 271 | | |
| | | 272 | | protected override void OnClose() |
| | | 273 | | { |
| | 63 | 274 | | if (_cachedDecryptedBodyContentReader != null) |
| | | 275 | | { |
| | | 276 | | try |
| | | 277 | | { |
| | 0 | 278 | | _cachedDecryptedBodyContentReader.Close(); |
| | 0 | 279 | | } |
| | | 280 | | catch (System.IO.IOException exception) |
| | | 281 | | { |
| | | 282 | | // |
| | | 283 | | // We only want to catch and log the I/O exception here |
| | | 284 | | // assuming reader only throw those exceptions |
| | | 285 | | // |
| | 0 | 286 | | DiagnosticUtility.TraceHandledException(exception, TraceEventType.Warning); |
| | 0 | 287 | | } |
| | | 288 | | finally |
| | | 289 | | { |
| | 0 | 290 | | _cachedDecryptedBodyContentReader = null; |
| | 0 | 291 | | } |
| | | 292 | | } |
| | | 293 | | |
| | 63 | 294 | | if (_cachedReaderAtSecurityHeader != null) |
| | | 295 | | { |
| | | 296 | | try |
| | | 297 | | { |
| | 0 | 298 | | _cachedReaderAtSecurityHeader.Close(); |
| | 0 | 299 | | } |
| | | 300 | | catch (System.IO.IOException exception) |
| | | 301 | | { |
| | | 302 | | // |
| | | 303 | | // We only want to catch and log the I/O exception here |
| | | 304 | | // assuming reader only throw those exceptions |
| | | 305 | | // |
| | 0 | 306 | | DiagnosticUtility.TraceHandledException(exception, TraceEventType.Warning); |
| | 0 | 307 | | } |
| | | 308 | | finally |
| | | 309 | | { |
| | 0 | 310 | | _cachedReaderAtSecurityHeader = null; |
| | 0 | 311 | | } |
| | | 312 | | } |
| | | 313 | | |
| | 63 | 314 | | _messageBuffer = null; |
| | 63 | 315 | | _decryptedBuffer = null; |
| | 63 | 316 | | _state = BodyState.Disposed; |
| | 63 | 317 | | InnerMessage.Close(); |
| | 63 | 318 | | } |
| | | 319 | | |
| | | 320 | | protected override XmlDictionaryReader OnGetReaderAtBodyContents() |
| | | 321 | | { |
| | 63 | 322 | | if (_state == BodyState.Created) |
| | | 323 | | { |
| | 63 | 324 | | return InnerMessage.GetReaderAtBodyContents(); |
| | | 325 | | } |
| | 0 | 326 | | if (_bodyDecrypted) |
| | | 327 | | { |
| | 0 | 328 | | EnsureDecryptedBodyStatusDetermined(); |
| | | 329 | | } |
| | 0 | 330 | | if (_cachedDecryptedBodyContentReader != null) |
| | | 331 | | { |
| | 0 | 332 | | XmlDictionaryReader result = _cachedDecryptedBodyContentReader; |
| | 0 | 333 | | _cachedDecryptedBodyContentReader = null; |
| | 0 | 334 | | return result; |
| | | 335 | | } |
| | | 336 | | else |
| | | 337 | | { |
| | 0 | 338 | | XmlDictionaryReader reader = CreateFullBodyReader(); |
| | 0 | 339 | | reader.ReadStartElement(); |
| | 0 | 340 | | reader.MoveToContent(); |
| | 0 | 341 | | return reader; |
| | | 342 | | } |
| | | 343 | | } |
| | | 344 | | |
| | | 345 | | protected override MessageBuffer OnCreateBufferedCopy(int maxBufferSize) |
| | | 346 | | { |
| | 0 | 347 | | if (_canDelegateCreateBufferedCopyToInnerMessage && InnerMessage is BufferedMessage) |
| | | 348 | | { |
| | 0 | 349 | | return InnerMessage.CreateBufferedCopy(maxBufferSize); |
| | | 350 | | } |
| | | 351 | | else |
| | | 352 | | { |
| | 0 | 353 | | return base.OnCreateBufferedCopy(maxBufferSize); |
| | | 354 | | } |
| | | 355 | | } |
| | | 356 | | |
| | | 357 | | internal void OnMessageProtectionPassComplete(bool atLeastOneHeaderOrBodyEncrypted) |
| | | 358 | | { |
| | 0 | 359 | | _canDelegateCreateBufferedCopyToInnerMessage = !atLeastOneHeaderOrBodyEncrypted; |
| | 0 | 360 | | } |
| | | 361 | | |
| | | 362 | | internal void OnUnencryptedPart(string name, string ns) |
| | | 363 | | { |
| | 0 | 364 | | if (ns == null) |
| | | 365 | | { |
| | 0 | 366 | | throw TraceUtility.ThrowHelperError(new MessageSecurityException(SR.Format(SR.RequiredMessagePartNotEncr |
| | | 367 | | } |
| | | 368 | | else |
| | | 369 | | { |
| | 0 | 370 | | throw TraceUtility.ThrowHelperError(new MessageSecurityException(SR.Format(SR.RequiredMessagePartNotEncr |
| | | 371 | | } |
| | | 372 | | } |
| | | 373 | | |
| | | 374 | | internal void OnUnsignedPart(string name, string ns) |
| | | 375 | | { |
| | 0 | 376 | | if (ns == null) |
| | | 377 | | { |
| | 0 | 378 | | throw TraceUtility.ThrowHelperError(new MessageSecurityException(SR.Format(SR.RequiredMessagePartNotSign |
| | | 379 | | } |
| | | 380 | | else |
| | | 381 | | { |
| | 0 | 382 | | throw TraceUtility.ThrowHelperError(new MessageSecurityException(SR.Format(SR.RequiredMessagePartNotSign |
| | | 383 | | } |
| | | 384 | | } |
| | | 385 | | |
| | | 386 | | protected override void OnWriteStartBody(XmlDictionaryWriter writer) |
| | | 387 | | { |
| | 0 | 388 | | if (_state == BodyState.Created) |
| | | 389 | | { |
| | 0 | 390 | | InnerMessage.WriteStartBody(writer); |
| | 0 | 391 | | return; |
| | | 392 | | } |
| | | 393 | | |
| | 0 | 394 | | XmlDictionaryReader reader = CreateFullBodyReader(); |
| | 0 | 395 | | reader.MoveToContent(); |
| | 0 | 396 | | writer.WriteStartElement(reader.Prefix, reader.LocalName, reader.NamespaceURI); |
| | 0 | 397 | | writer.WriteAttributes(reader, false); |
| | 0 | 398 | | reader.Close(); |
| | 0 | 399 | | } |
| | | 400 | | |
| | | 401 | | protected override void OnWriteBodyContents(XmlDictionaryWriter writer) |
| | | 402 | | { |
| | 0 | 403 | | if (_state == BodyState.Created) |
| | | 404 | | { |
| | 0 | 405 | | InnerMessage.WriteBodyContents(writer); |
| | 0 | 406 | | return; |
| | | 407 | | } |
| | | 408 | | |
| | 0 | 409 | | XmlDictionaryReader reader = CreateFullBodyReader(); |
| | 0 | 410 | | reader.ReadStartElement(); |
| | 0 | 411 | | while (reader.NodeType != XmlNodeType.EndElement) |
| | | 412 | | { |
| | 0 | 413 | | writer.WriteNode(reader, false); |
| | | 414 | | } |
| | | 415 | | |
| | 0 | 416 | | reader.ReadEndElement(); |
| | 0 | 417 | | reader.Close(); |
| | 0 | 418 | | } |
| | | 419 | | |
| | | 420 | | public void SetBodyPrefixAndAttributes(XmlDictionaryReader bodyReader) |
| | | 421 | | { |
| | 0 | 422 | | _bodyPrefix = bodyReader.Prefix; |
| | 0 | 423 | | _bodyAttributes = XmlAttributeHolder.ReadAttributes(bodyReader); |
| | 0 | 424 | | } |
| | | 425 | | |
| | | 426 | | public void SetDecryptedBody(byte[] decryptedBodyContent) |
| | | 427 | | { |
| | 0 | 428 | | if (_state != BodyState.Buffered) |
| | | 429 | | { |
| | 0 | 430 | | throw TraceUtility.ThrowHelperError(CreateBadStateException("SetDecryptedBody"), this); |
| | | 431 | | } |
| | | 432 | | |
| | 0 | 433 | | MemoryStream stream = new MemoryStream(); |
| | 0 | 434 | | XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(stream); |
| | | 435 | | |
| | 0 | 436 | | writer.WriteStartElement(_envelopePrefix, XD.MessageDictionary.Envelope, Version.Envelope.DictionaryNamespac |
| | 0 | 437 | | XmlAttributeHolder.WriteAttributes(_envelopeAttributes, writer); |
| | | 438 | | |
| | 0 | 439 | | writer.WriteStartElement(_bodyPrefix, XD.MessageDictionary.Body, Version.Envelope.DictionaryNamespace); |
| | 0 | 440 | | XmlAttributeHolder.WriteAttributes(_bodyAttributes, writer); |
| | 0 | 441 | | writer.WriteString(" "); // ensure non-empty element |
| | 0 | 442 | | writer.WriteEndElement(); |
| | 0 | 443 | | writer.WriteEndElement(); |
| | 0 | 444 | | writer.Flush(); |
| | | 445 | | |
| | 0 | 446 | | _decryptedBuffer = ContextImportHelper.SpliceBuffers(decryptedBodyContent, stream.GetBuffer(), (int)stream.L |
| | | 447 | | |
| | 0 | 448 | | _bodyDecrypted = true; |
| | 0 | 449 | | _state = BodyState.Decrypted; |
| | 0 | 450 | | } |
| | | 451 | | |
| | | 452 | | private enum BodyState |
| | | 453 | | { |
| | | 454 | | Created, |
| | | 455 | | Buffered, |
| | | 456 | | Decrypted, |
| | | 457 | | Disposed, |
| | | 458 | | } |
| | | 459 | | } |
| | | 460 | | |
| | | 461 | | //TODO investigate |
| | | 462 | | // Adding wrapping tags using a writer is a temporary feature to |
| | | 463 | | // support interop with a partner. Eventually, the serialization |
| | | 464 | | // team will add a feature to XmlUTF8TextReader to directly |
| | | 465 | | // support the addition of outer namespaces before creating a |
| | | 466 | | // Reader. This roundabout way of supporting context-sensitive |
| | | 467 | | // decryption can then be removed. |
| | | 468 | | internal static class ContextImportHelper |
| | | 469 | | { |
| | | 470 | | internal static XmlDictionaryReader CreateSplicedReader(byte[] decryptedBuffer, |
| | | 471 | | XmlAttributeHolder[] outerContext1, XmlAttributeHolder[] outerContext2, XmlAttributeHolder[] outerContext3, |
| | | 472 | | { |
| | | 473 | | const string wrapper1 = "x"; |
| | | 474 | | const string wrapper2 = "y"; |
| | | 475 | | const string wrapper3 = "z"; |
| | | 476 | | const int wrappingDepth = 3; |
| | | 477 | | |
| | | 478 | | MemoryStream stream = new MemoryStream(); |
| | | 479 | | XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(stream); |
| | | 480 | | writer.WriteStartElement(wrapper1); |
| | | 481 | | WriteNamespaceDeclarations(outerContext1, writer); |
| | | 482 | | writer.WriteStartElement(wrapper2); |
| | | 483 | | WriteNamespaceDeclarations(outerContext2, writer); |
| | | 484 | | writer.WriteStartElement(wrapper3); |
| | | 485 | | WriteNamespaceDeclarations(outerContext3, writer); |
| | | 486 | | writer.WriteString(" "); // ensure non-empty element |
| | | 487 | | writer.WriteEndElement(); |
| | | 488 | | writer.WriteEndElement(); |
| | | 489 | | writer.WriteEndElement(); |
| | | 490 | | writer.Flush(); |
| | | 491 | | |
| | | 492 | | byte[] splicedBuffer = SpliceBuffers(decryptedBuffer, stream.GetBuffer(), (int)stream.Length, wrappingDepth) |
| | | 493 | | XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(splicedBuffer, quotas); |
| | | 494 | | reader.ReadStartElement(wrapper1); |
| | | 495 | | reader.ReadStartElement(wrapper2); |
| | | 496 | | reader.ReadStartElement(wrapper3); |
| | | 497 | | if (reader.NodeType != XmlNodeType.Element) |
| | | 498 | | { |
| | | 499 | | reader.MoveToContent(); |
| | | 500 | | } |
| | | 501 | | return reader; |
| | | 502 | | } |
| | | 503 | | |
| | | 504 | | internal static string GetPrefixIfNamespaceDeclaration(string prefix, string localName) |
| | | 505 | | { |
| | | 506 | | if (prefix == "xmlns") |
| | | 507 | | { |
| | | 508 | | return localName; |
| | | 509 | | } |
| | | 510 | | if (prefix.Length == 0 && localName == "xmlns") |
| | | 511 | | { |
| | | 512 | | return string.Empty; |
| | | 513 | | } |
| | | 514 | | return null; |
| | | 515 | | } |
| | | 516 | | |
| | | 517 | | private static bool IsNamespaceDeclaration(string prefix, string localName) |
| | | 518 | | { |
| | | 519 | | return GetPrefixIfNamespaceDeclaration(prefix, localName) != null; |
| | | 520 | | } |
| | | 521 | | |
| | | 522 | | internal static byte[] SpliceBuffers(byte[] middle, byte[] wrapper, int wrapperLength, int wrappingDepth) |
| | | 523 | | { |
| | | 524 | | const byte openChar = (byte)'<'; |
| | | 525 | | int openCharsFound = 0; |
| | | 526 | | int openCharIndex; |
| | | 527 | | for (openCharIndex = wrapperLength - 1; openCharIndex >= 0; openCharIndex--) |
| | | 528 | | { |
| | | 529 | | if (wrapper[openCharIndex] == openChar) |
| | | 530 | | { |
| | | 531 | | openCharsFound++; |
| | | 532 | | if (openCharsFound == wrappingDepth) |
| | | 533 | | { |
| | | 534 | | break; |
| | | 535 | | } |
| | | 536 | | } |
| | | 537 | | } |
| | | 538 | | |
| | | 539 | | Fx.Assert(openCharIndex > 0, ""); |
| | | 540 | | |
| | | 541 | | byte[] splicedBuffer = Fx.AllocateByteArray(checked(middle.Length + wrapperLength - 1)); |
| | | 542 | | int offset = 0; |
| | | 543 | | int count = openCharIndex - 1; |
| | | 544 | | Buffer.BlockCopy(wrapper, 0, splicedBuffer, offset, count); |
| | | 545 | | offset += count; |
| | | 546 | | count = middle.Length; |
| | | 547 | | Buffer.BlockCopy(middle, 0, splicedBuffer, offset, count); |
| | | 548 | | offset += count; |
| | | 549 | | count = wrapperLength - openCharIndex; |
| | | 550 | | Buffer.BlockCopy(wrapper, openCharIndex, splicedBuffer, offset, count); |
| | | 551 | | |
| | | 552 | | return splicedBuffer; |
| | | 553 | | } |
| | | 554 | | |
| | | 555 | | private static void WriteNamespaceDeclarations(XmlAttributeHolder[] attributes, XmlWriter writer) |
| | | 556 | | { |
| | | 557 | | if (attributes != null) |
| | | 558 | | { |
| | | 559 | | for (int i = 0; i < attributes.Length; i++) |
| | | 560 | | { |
| | | 561 | | XmlAttributeHolder a = attributes[i]; |
| | | 562 | | if (IsNamespaceDeclaration(a.Prefix, a.LocalName)) |
| | | 563 | | { |
| | | 564 | | a.WriteTo(writer); |
| | | 565 | | } |
| | | 566 | | } |
| | | 567 | | } |
| | | 568 | | } |
| | | 569 | | } |
| | | 570 | | } |