| | | 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.Threading.Tasks; |
| | | 8 | | using System.Xml; |
| | | 9 | | using CoreWCF.Channels; |
| | | 10 | | using CoreWCF.IdentityModel.Tokens; |
| | | 11 | | using CoreWCF.Runtime; |
| | | 12 | | using CoreWCF.Security.Tokens; |
| | | 13 | | using IPrefixGenerator = CoreWCF.IdentityModel.IPrefixGenerator; |
| | | 14 | | using ISecurityElement = CoreWCF.IdentityModel.ISecurityElement; |
| | | 15 | | using XmlAttributeHolder = CoreWCF.IdentityModel.XmlAttributeHolder; |
| | | 16 | | |
| | | 17 | | namespace CoreWCF.Security |
| | | 18 | | { |
| | | 19 | | internal sealed class SecurityAppliedMessage : DelegatingMessage |
| | | 20 | | { |
| | | 21 | | private bool _bodyIdInserted; |
| | 63 | 22 | | private string _bodyPrefix = MessageStrings.Prefix; |
| | | 23 | | private XmlBuffer _fullBodyBuffer; |
| | | 24 | | private ISecurityElement _encryptedBodyContent; |
| | | 25 | | private XmlAttributeHolder[] _bodyAttributes; |
| | | 26 | | private bool _delayedApplicationHandled; |
| | | 27 | | private BodyState _state = BodyState.Created; |
| | | 28 | | private readonly SendSecurityHeader _securityHeader; |
| | | 29 | | private MemoryStream _startBodyFragment; |
| | | 30 | | private MemoryStream _endBodyFragment; |
| | | 31 | | private byte[] _fullBodyFragment; |
| | | 32 | | private int _fullBodyFragmentLength; |
| | | 33 | | |
| | | 34 | | public SecurityAppliedMessage(Message messageToProcess, SendSecurityHeader securityHeader, bool signBody, bool e |
| | 63 | 35 | | : base(messageToProcess) |
| | | 36 | | { |
| | | 37 | | Fx.Assert(!(messageToProcess is SecurityAppliedMessage), "SecurityAppliedMessage should not be wrapped"); |
| | 63 | 38 | | _securityHeader = securityHeader; |
| | 63 | 39 | | BodyProtectionMode = MessagePartProtectionModeHelper.GetProtectionMode(signBody, encryptBody, securityHeader |
| | 63 | 40 | | } |
| | | 41 | | |
| | 0 | 42 | | public string BodyId { get; private set; } |
| | | 43 | | |
| | 63 | 44 | | public MessagePartProtectionMode BodyProtectionMode { get; } |
| | | 45 | | |
| | 0 | 46 | | internal byte[] PrimarySignatureValue => _securityHeader.PrimarySignatureValue; |
| | | 47 | | |
| | | 48 | | private Exception CreateBadStateException(string operation) |
| | | 49 | | { |
| | 0 | 50 | | return new InvalidOperationException(SR.Format(SR.MessageBodyOperationNotValidInBodyState, |
| | 0 | 51 | | operation, _state)); |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | private void EnsureUniqueSecurityApplication() |
| | | 55 | | { |
| | 63 | 56 | | if (_delayedApplicationHandled) |
| | | 57 | | { |
| | 0 | 58 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Del |
| | | 59 | | } |
| | 63 | 60 | | _delayedApplicationHandled = true; |
| | 63 | 61 | | } |
| | | 62 | | |
| | | 63 | | protected override void OnBodyToString(XmlDictionaryWriter writer) |
| | | 64 | | { |
| | 0 | 65 | | if (_state == BodyState.Created || _fullBodyFragment != null) |
| | | 66 | | { |
| | 0 | 67 | | base.OnBodyToString(writer); |
| | | 68 | | } |
| | | 69 | | else |
| | | 70 | | { |
| | 0 | 71 | | OnWriteBodyContents(writer); |
| | | 72 | | } |
| | 0 | 73 | | } |
| | | 74 | | |
| | | 75 | | protected override void OnClose() |
| | | 76 | | { |
| | | 77 | | try |
| | | 78 | | { |
| | 10 | 79 | | InnerMessage.Close(); |
| | 10 | 80 | | } |
| | | 81 | | finally |
| | | 82 | | { |
| | 10 | 83 | | _fullBodyBuffer = null; |
| | 10 | 84 | | _bodyAttributes = null; |
| | 10 | 85 | | _encryptedBodyContent = null; |
| | 10 | 86 | | _state = BodyState.Disposed; |
| | 10 | 87 | | } |
| | 10 | 88 | | } |
| | | 89 | | |
| | | 90 | | protected override void OnWriteStartBody(XmlDictionaryWriter writer) |
| | | 91 | | { |
| | 63 | 92 | | if (_startBodyFragment != null || _fullBodyFragment != null) |
| | | 93 | | { |
| | 0 | 94 | | WriteStartInnerMessageWithId(writer); |
| | 0 | 95 | | return; |
| | | 96 | | } |
| | | 97 | | |
| | 63 | 98 | | switch (_state) |
| | | 99 | | { |
| | | 100 | | case BodyState.Created: |
| | | 101 | | case BodyState.Encrypted: |
| | 63 | 102 | | InnerMessage.WriteStartBody(writer); |
| | 63 | 103 | | return; |
| | | 104 | | case BodyState.Signed: |
| | | 105 | | case BodyState.EncryptedThenSigned: |
| | 0 | 106 | | XmlDictionaryReader reader = _fullBodyBuffer.GetReader(0); |
| | 0 | 107 | | writer.WriteStartElement(reader.Prefix, reader.LocalName, reader.NamespaceURI); |
| | 0 | 108 | | writer.WriteAttributes(reader, false); |
| | 0 | 109 | | reader.Close(); |
| | 0 | 110 | | return; |
| | | 111 | | case BodyState.SignedThenEncrypted: |
| | 0 | 112 | | writer.WriteStartElement(_bodyPrefix, XD.MessageDictionary.Body, Version.Envelope.DictionaryNamespac |
| | 0 | 113 | | if (_bodyAttributes != null) |
| | | 114 | | { |
| | 0 | 115 | | XmlAttributeHolder.WriteAttributes(_bodyAttributes, writer); |
| | | 116 | | } |
| | 0 | 117 | | return; |
| | | 118 | | default: |
| | 0 | 119 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateBadStateException(nameof(OnWriteStar |
| | | 120 | | } |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | protected override void OnWriteBodyContents(XmlDictionaryWriter writer) |
| | | 124 | | { |
| | 63 | 125 | | switch (_state) |
| | | 126 | | { |
| | | 127 | | case BodyState.Created: |
| | 63 | 128 | | InnerMessage.WriteBodyContents(writer); |
| | 63 | 129 | | return; |
| | | 130 | | case BodyState.Signed: |
| | | 131 | | case BodyState.EncryptedThenSigned: |
| | 0 | 132 | | XmlDictionaryReader reader = _fullBodyBuffer.GetReader(0); |
| | 0 | 133 | | reader.ReadStartElement(); |
| | 0 | 134 | | while (reader.NodeType != XmlNodeType.EndElement) |
| | | 135 | | { |
| | 0 | 136 | | writer.WriteNode(reader, false); |
| | | 137 | | } |
| | | 138 | | |
| | 0 | 139 | | reader.ReadEndElement(); |
| | 0 | 140 | | reader.Close(); |
| | 0 | 141 | | return; |
| | | 142 | | case BodyState.Encrypted: |
| | | 143 | | case BodyState.SignedThenEncrypted: |
| | 0 | 144 | | _encryptedBodyContent.WriteTo(writer, ServiceModelDictionaryManager.Instance); |
| | 0 | 145 | | break; |
| | | 146 | | default: |
| | 0 | 147 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateBadStateException(nameof(OnWriteBody |
| | | 148 | | } |
| | | 149 | | } |
| | | 150 | | |
| | | 151 | | public override async Task OnWriteMessageAsync(XmlDictionaryWriter writer) |
| | | 152 | | { |
| | | 153 | | // With NetFx the writer passed to SecurityAppliedMessage.OnWriteMessageAsync is of type XmlUtf8TextWriter w |
| | | 154 | | // On .NET, the type is XmlDictionaryAsyncCheckWriter which wraps XmlUtf8TextWriter, both of which do implem |
| | 2 | 155 | | if (Environment.Version.MajorRevision < 8) |
| | | 156 | | { |
| | 2 | 157 | | OnWriteMessage(writer); |
| | 2 | 158 | | return; |
| | | 159 | | } |
| | | 160 | | |
| | | 161 | | // For Kerb one shot, the channel binding will be need to be fished out of the message, cached and added to |
| | | 162 | | // token before calling ISC. |
| | | 163 | | |
| | 0 | 164 | | AttachChannelBindingTokenIfFound(); |
| | | 165 | | |
| | 0 | 166 | | EnsureUniqueSecurityApplication(); |
| | | 167 | | |
| | 0 | 168 | | MessagePrefixGenerator prefixGenerator = new MessagePrefixGenerator(writer); |
| | 0 | 169 | | _securityHeader.StartSecurityApplication(); |
| | | 170 | | |
| | 0 | 171 | | Headers.Add(_securityHeader); |
| | | 172 | | |
| | 0 | 173 | | InnerMessage.WriteStartEnvelope(writer); |
| | | 174 | | |
| | 0 | 175 | | Headers.RemoveAt(Headers.Count - 1); |
| | | 176 | | |
| | 0 | 177 | | await _securityHeader.ApplyBodySecurityAsync(writer, prefixGenerator); |
| | | 178 | | |
| | 0 | 179 | | InnerMessage.WriteStartHeaders(writer); |
| | 0 | 180 | | await _securityHeader.ApplySecurityAndWriteHeadersAsync(Headers, writer, prefixGenerator); |
| | | 181 | | |
| | 0 | 182 | | _securityHeader.RemoveSignatureEncryptionIfAppropriate(); |
| | | 183 | | |
| | 0 | 184 | | _securityHeader.CompleteSecurityApplication(); |
| | 0 | 185 | | _securityHeader.WriteHeader(writer, Version); |
| | 0 | 186 | | await writer.WriteEndElementAsync(); |
| | | 187 | | |
| | 0 | 188 | | if (_fullBodyFragment != null) |
| | | 189 | | { |
| | 0 | 190 | | ((IFragmentCapableXmlDictionaryWriter)writer).WriteFragment(_fullBodyFragment, 0, _fullBodyFragmentLengt |
| | | 191 | | } |
| | | 192 | | else |
| | | 193 | | { |
| | 0 | 194 | | if (_startBodyFragment != null) |
| | | 195 | | { |
| | 0 | 196 | | ((IFragmentCapableXmlDictionaryWriter)writer).WriteFragment(_startBodyFragment.GetBuffer(), 0, (int) |
| | | 197 | | } |
| | | 198 | | else |
| | | 199 | | { |
| | 0 | 200 | | OnWriteStartBody(writer); |
| | | 201 | | } |
| | | 202 | | |
| | 0 | 203 | | OnWriteBodyContents(writer); |
| | | 204 | | |
| | 0 | 205 | | if (_endBodyFragment != null) |
| | | 206 | | { |
| | 0 | 207 | | ((IFragmentCapableXmlDictionaryWriter)writer).WriteFragment(_endBodyFragment.GetBuffer(), 0, (int)_e |
| | | 208 | | } |
| | | 209 | | else |
| | | 210 | | { |
| | 0 | 211 | | await writer.WriteEndElementAsync(); |
| | | 212 | | } |
| | | 213 | | } |
| | | 214 | | |
| | 0 | 215 | | await writer.WriteEndElementAsync(); |
| | 2 | 216 | | } |
| | | 217 | | |
| | | 218 | | protected override void OnWriteMessage(XmlDictionaryWriter writer) |
| | | 219 | | { |
| | | 220 | | // For Kerb one shot, the channel binding will be need to be fished out of the message, cached and added to |
| | | 221 | | // token before calling ISC. |
| | | 222 | | |
| | 63 | 223 | | AttachChannelBindingTokenIfFound(); |
| | | 224 | | |
| | 63 | 225 | | EnsureUniqueSecurityApplication(); |
| | | 226 | | |
| | 63 | 227 | | MessagePrefixGenerator prefixGenerator = new MessagePrefixGenerator(writer); |
| | 63 | 228 | | _securityHeader.StartSecurityApplication(); |
| | | 229 | | |
| | 63 | 230 | | Headers.Add(_securityHeader); |
| | | 231 | | |
| | 63 | 232 | | InnerMessage.WriteStartEnvelope(writer); |
| | | 233 | | |
| | 63 | 234 | | Headers.RemoveAt(Headers.Count - 1); |
| | | 235 | | |
| | 63 | 236 | | _securityHeader.ApplyBodySecurity(writer, prefixGenerator); |
| | | 237 | | |
| | 63 | 238 | | InnerMessage.WriteStartHeaders(writer); |
| | 63 | 239 | | _securityHeader.ApplySecurityAndWriteHeaders(Headers, writer, prefixGenerator); |
| | | 240 | | |
| | 63 | 241 | | _securityHeader.RemoveSignatureEncryptionIfAppropriate(); |
| | | 242 | | |
| | 63 | 243 | | _securityHeader.CompleteSecurityApplication(); |
| | 63 | 244 | | _securityHeader.WriteHeader(writer, Version); |
| | 63 | 245 | | writer.WriteEndElement(); |
| | | 246 | | |
| | 63 | 247 | | if (_fullBodyFragment != null) |
| | | 248 | | { |
| | 0 | 249 | | ((IFragmentCapableXmlDictionaryWriter)writer).WriteFragment(_fullBodyFragment, 0, _fullBodyFragmentLengt |
| | | 250 | | } |
| | | 251 | | else |
| | | 252 | | { |
| | 63 | 253 | | if (_startBodyFragment != null) |
| | | 254 | | { |
| | 0 | 255 | | ((IFragmentCapableXmlDictionaryWriter)writer).WriteFragment(_startBodyFragment.GetBuffer(), 0, (int) |
| | | 256 | | } |
| | | 257 | | else |
| | | 258 | | { |
| | 63 | 259 | | OnWriteStartBody(writer); |
| | | 260 | | } |
| | | 261 | | |
| | 63 | 262 | | OnWriteBodyContents(writer); |
| | | 263 | | |
| | 63 | 264 | | if (_endBodyFragment != null) |
| | | 265 | | { |
| | 0 | 266 | | ((IFragmentCapableXmlDictionaryWriter)writer).WriteFragment(_endBodyFragment.GetBuffer(), 0, (int)_e |
| | | 267 | | } |
| | | 268 | | else |
| | | 269 | | { |
| | 63 | 270 | | writer.WriteEndElement(); |
| | | 271 | | } |
| | | 272 | | } |
| | | 273 | | |
| | 63 | 274 | | writer.WriteEndElement(); |
| | 63 | 275 | | } |
| | | 276 | | |
| | | 277 | | private void AttachChannelBindingTokenIfFound() |
| | | 278 | | { |
| | 63 | 279 | | ChannelBindingMessageProperty.TryGet(InnerMessage, out ChannelBindingMessageProperty cbmp); |
| | | 280 | | |
| | 63 | 281 | | if (cbmp != null) |
| | | 282 | | { |
| | 0 | 283 | | if (_securityHeader.ElementContainer != null && _securityHeader.ElementContainer.EndorsingSupportingToke |
| | | 284 | | { |
| | 0 | 285 | | foreach (SecurityToken token in _securityHeader.ElementContainer.EndorsingSupportingTokens) |
| | | 286 | | { |
| | 0 | 287 | | if (token is ProviderBackedSecurityToken pbst) |
| | | 288 | | { |
| | 0 | 289 | | pbst.ChannelBinding = cbmp.ChannelBinding; |
| | | 290 | | } |
| | | 291 | | } |
| | | 292 | | } |
| | | 293 | | } |
| | 63 | 294 | | } |
| | | 295 | | |
| | | 296 | | private void SetBodyId() |
| | | 297 | | { |
| | 0 | 298 | | BodyId = InnerMessage.GetBodyAttribute( |
| | 0 | 299 | | UtilityStrings.IdAttribute, |
| | 0 | 300 | | _securityHeader.StandardsManager.IdManager.DefaultIdNamespaceUri); |
| | 0 | 301 | | if (BodyId == null) |
| | | 302 | | { |
| | 0 | 303 | | BodyId = _securityHeader.GenerateId(); |
| | 0 | 304 | | _bodyIdInserted = true; |
| | | 305 | | } |
| | 0 | 306 | | } |
| | | 307 | | |
| | | 308 | | public void WriteBodyToEncrypt(EncryptedData encryptedData, SymmetricAlgorithm algorithm) |
| | | 309 | | { |
| | 0 | 310 | | encryptedData.Id = _securityHeader.GenerateId(); |
| | | 311 | | |
| | 0 | 312 | | BodyContentHelper helper = new BodyContentHelper(); |
| | 0 | 313 | | XmlDictionaryWriter encryptingWriter = helper.CreateWriter(); |
| | 0 | 314 | | InnerMessage.WriteBodyContents(encryptingWriter); |
| | 0 | 315 | | encryptedData.SetUpEncryption(algorithm, helper.ExtractResult()); |
| | 0 | 316 | | _encryptedBodyContent = encryptedData; |
| | | 317 | | |
| | 0 | 318 | | _state = BodyState.Encrypted; |
| | 0 | 319 | | } |
| | | 320 | | |
| | | 321 | | public void WriteBodyToEncryptThenSign(Stream canonicalStream, EncryptedData encryptedData, SymmetricAlgorithm a |
| | | 322 | | { |
| | 0 | 323 | | encryptedData.Id = _securityHeader.GenerateId(); |
| | 0 | 324 | | SetBodyId(); |
| | | 325 | | |
| | 0 | 326 | | XmlDictionaryWriter encryptingWriter = XmlDictionaryWriter.CreateTextWriter(Stream.Null); |
| | | 327 | | // The XmlSerializer body formatter would add a |
| | | 328 | | // document declaration to the body fragment when a fresh writer |
| | | 329 | | // is provided. Hence, insert a dummy element here and capture |
| | | 330 | | // the body contents as a fragment. |
| | 0 | 331 | | encryptingWriter.WriteStartElement("a"); |
| | 0 | 332 | | MemoryStream ms = new MemoryStream(); |
| | 0 | 333 | | ((IFragmentCapableXmlDictionaryWriter)encryptingWriter).StartFragment(ms, true); |
| | | 334 | | |
| | 0 | 335 | | InnerMessage.WriteBodyContents(encryptingWriter); |
| | 0 | 336 | | ((IFragmentCapableXmlDictionaryWriter)encryptingWriter).EndFragment(); |
| | 0 | 337 | | encryptingWriter.WriteEndElement(); |
| | 0 | 338 | | ms.Flush(); |
| | 0 | 339 | | encryptedData.SetUpEncryption(algorithm, new ArraySegment<byte>(ms.GetBuffer(), 0, (int)ms.Length)); |
| | | 340 | | |
| | 0 | 341 | | _fullBodyBuffer = new XmlBuffer(int.MaxValue); |
| | 0 | 342 | | XmlDictionaryWriter canonicalWriter = _fullBodyBuffer.OpenSection(XmlDictionaryReaderQuotas.Max); |
| | | 343 | | |
| | 0 | 344 | | canonicalWriter.StartCanonicalization(canonicalStream, false, null); |
| | 0 | 345 | | WriteStartInnerMessageWithId(canonicalWriter); |
| | 0 | 346 | | encryptedData.WriteTo(canonicalWriter, ServiceModelDictionaryManager.Instance); |
| | 0 | 347 | | canonicalWriter.WriteEndElement(); |
| | 0 | 348 | | canonicalWriter.EndCanonicalization(); |
| | 0 | 349 | | canonicalWriter.Flush(); |
| | | 350 | | |
| | 0 | 351 | | _fullBodyBuffer.CloseSection(); |
| | 0 | 352 | | _fullBodyBuffer.Close(); |
| | | 353 | | |
| | 0 | 354 | | _state = BodyState.EncryptedThenSigned; |
| | 0 | 355 | | } |
| | | 356 | | |
| | | 357 | | public void WriteBodyToSign(Stream canonicalStream) |
| | | 358 | | { |
| | 0 | 359 | | SetBodyId(); |
| | | 360 | | |
| | 0 | 361 | | _fullBodyBuffer = new XmlBuffer(int.MaxValue); |
| | 0 | 362 | | XmlDictionaryWriter canonicalWriter = _fullBodyBuffer.OpenSection(XmlDictionaryReaderQuotas.Max); |
| | 0 | 363 | | canonicalWriter.StartCanonicalization(canonicalStream, false, null); |
| | 0 | 364 | | WriteInnerMessageWithId(canonicalWriter); |
| | 0 | 365 | | canonicalWriter.EndCanonicalization(); |
| | 0 | 366 | | canonicalWriter.Flush(); |
| | 0 | 367 | | _fullBodyBuffer.CloseSection(); |
| | 0 | 368 | | _fullBodyBuffer.Close(); |
| | | 369 | | |
| | 0 | 370 | | _state = BodyState.Signed; |
| | 0 | 371 | | } |
| | | 372 | | |
| | | 373 | | public async ValueTask WriteBodyToSignAsync(Stream canonicalStream) |
| | | 374 | | { |
| | 0 | 375 | | SetBodyId(); |
| | | 376 | | |
| | 0 | 377 | | _fullBodyBuffer = new XmlBuffer(int.MaxValue); |
| | 0 | 378 | | XmlDictionaryWriter canonicalWriter = _fullBodyBuffer.OpenSection(XmlDictionaryReaderQuotas.Max); |
| | 0 | 379 | | canonicalWriter.StartCanonicalization(canonicalStream, false, null); |
| | 0 | 380 | | WriteInnerMessageWithId(canonicalWriter); |
| | 0 | 381 | | canonicalWriter.EndCanonicalization(); |
| | 0 | 382 | | await canonicalWriter.FlushAsync(); |
| | 0 | 383 | | _fullBodyBuffer.CloseSection(); |
| | 0 | 384 | | _fullBodyBuffer.Close(); |
| | | 385 | | |
| | 0 | 386 | | _state = BodyState.Signed; |
| | 0 | 387 | | } |
| | | 388 | | |
| | | 389 | | public void WriteBodyToSignThenEncrypt(Stream canonicalStream, EncryptedData encryptedData, SymmetricAlgorithm a |
| | | 390 | | { |
| | 0 | 391 | | XmlBuffer buffer = new XmlBuffer(int.MaxValue); |
| | 0 | 392 | | XmlDictionaryWriter fragmentingWriter = buffer.OpenSection(XmlDictionaryReaderQuotas.Max); |
| | 0 | 393 | | WriteBodyToSignThenEncryptWithFragments(canonicalStream, false, null, encryptedData, algorithm, fragmentingW |
| | 0 | 394 | | ((IFragmentCapableXmlDictionaryWriter)fragmentingWriter).WriteFragment(_startBodyFragment.GetBuffer(), 0, (i |
| | 0 | 395 | | ((IFragmentCapableXmlDictionaryWriter)fragmentingWriter).WriteFragment(_endBodyFragment.GetBuffer(), 0, (int |
| | 0 | 396 | | buffer.CloseSection(); |
| | 0 | 397 | | buffer.Close(); |
| | | 398 | | |
| | 0 | 399 | | _startBodyFragment = null; |
| | 0 | 400 | | _endBodyFragment = null; |
| | | 401 | | |
| | 0 | 402 | | XmlDictionaryReader reader = buffer.GetReader(0); |
| | 0 | 403 | | reader.MoveToContent(); |
| | 0 | 404 | | _bodyPrefix = reader.Prefix; |
| | 0 | 405 | | if (reader.HasAttributes) |
| | | 406 | | { |
| | 0 | 407 | | _bodyAttributes = XmlAttributeHolder.ReadAttributes(reader); |
| | | 408 | | } |
| | 0 | 409 | | reader.Close(); |
| | 0 | 410 | | } |
| | | 411 | | |
| | | 412 | | public void WriteBodyToSignThenEncryptWithFragments( |
| | | 413 | | Stream stream, bool includeComments, string[] inclusivePrefixes, |
| | | 414 | | EncryptedData encryptedData, SymmetricAlgorithm algorithm, XmlDictionaryWriter writer) |
| | | 415 | | { |
| | 0 | 416 | | IFragmentCapableXmlDictionaryWriter fragmentingWriter = (IFragmentCapableXmlDictionaryWriter)writer; |
| | | 417 | | |
| | 0 | 418 | | SetBodyId(); |
| | 0 | 419 | | encryptedData.Id = _securityHeader.GenerateId(); |
| | | 420 | | |
| | 0 | 421 | | _startBodyFragment = new MemoryStream(); |
| | 0 | 422 | | BufferedOutputStream bodyContentFragment = new BufferManagerOutputStream(SRCommon.XmlBufferQuotaExceeded, 10 |
| | 0 | 423 | | _endBodyFragment = new MemoryStream(); |
| | | 424 | | |
| | 0 | 425 | | writer.StartCanonicalization(stream, includeComments, inclusivePrefixes); |
| | | 426 | | |
| | 0 | 427 | | fragmentingWriter.StartFragment(_startBodyFragment, false); |
| | 0 | 428 | | WriteStartInnerMessageWithId(writer); |
| | 0 | 429 | | fragmentingWriter.EndFragment(); |
| | | 430 | | |
| | 0 | 431 | | fragmentingWriter.StartFragment(bodyContentFragment, true); |
| | 0 | 432 | | InnerMessage.WriteBodyContents(writer); |
| | 0 | 433 | | fragmentingWriter.EndFragment(); |
| | | 434 | | |
| | 0 | 435 | | fragmentingWriter.StartFragment(_endBodyFragment, false); |
| | 0 | 436 | | writer.WriteEndElement(); |
| | 0 | 437 | | fragmentingWriter.EndFragment(); |
| | | 438 | | |
| | 0 | 439 | | writer.EndCanonicalization(); |
| | | 440 | | |
| | 0 | 441 | | byte[] bodyBuffer = bodyContentFragment.ToArray(out int bodyLength); |
| | | 442 | | |
| | 0 | 443 | | encryptedData.SetUpEncryption(algorithm, new ArraySegment<byte>(bodyBuffer, 0, bodyLength)); |
| | 0 | 444 | | _encryptedBodyContent = encryptedData; |
| | | 445 | | |
| | 0 | 446 | | _state = BodyState.SignedThenEncrypted; |
| | 0 | 447 | | } |
| | | 448 | | |
| | | 449 | | public void WriteBodyToSignWithFragments(Stream stream, bool includeComments, string[] inclusivePrefixes, XmlDic |
| | | 450 | | { |
| | 0 | 451 | | IFragmentCapableXmlDictionaryWriter fragmentingWriter = (IFragmentCapableXmlDictionaryWriter)writer; |
| | | 452 | | |
| | 0 | 453 | | SetBodyId(); |
| | 0 | 454 | | BufferedOutputStream fullBodyFragment = new BufferManagerOutputStream(SRCommon.XmlBufferQuotaExceeded, 1024, |
| | 0 | 455 | | writer.StartCanonicalization(stream, includeComments, inclusivePrefixes); |
| | 0 | 456 | | fragmentingWriter.StartFragment(fullBodyFragment, false); |
| | 0 | 457 | | WriteStartInnerMessageWithId(writer); |
| | 0 | 458 | | InnerMessage.WriteBodyContents(writer); |
| | 0 | 459 | | writer.WriteEndElement(); |
| | 0 | 460 | | fragmentingWriter.EndFragment(); |
| | 0 | 461 | | writer.EndCanonicalization(); |
| | | 462 | | |
| | 0 | 463 | | _fullBodyFragment = fullBodyFragment.ToArray(out _fullBodyFragmentLength); |
| | | 464 | | |
| | 0 | 465 | | _state = BodyState.Signed; |
| | 0 | 466 | | } |
| | | 467 | | |
| | | 468 | | public async ValueTask WriteBodyToSignWithFragmentsAsync(Stream stream, bool includeComments, string[] inclusive |
| | | 469 | | { |
| | 0 | 470 | | IFragmentCapableXmlDictionaryWriter fragmentingWriter = (IFragmentCapableXmlDictionaryWriter)writer; |
| | | 471 | | |
| | 0 | 472 | | SetBodyId(); |
| | 0 | 473 | | BufferedOutputStream fullBodyFragment = new BufferManagerOutputStream(SRCommon.XmlBufferQuotaExceeded, 1024, |
| | 0 | 474 | | writer.StartCanonicalization(stream, includeComments, inclusivePrefixes); |
| | 0 | 475 | | fragmentingWriter.StartFragment(fullBodyFragment, false); |
| | 0 | 476 | | WriteStartInnerMessageWithId(writer); |
| | 0 | 477 | | await InnerMessage.WriteBodyContentsAsync(writer); |
| | 0 | 478 | | await writer.WriteEndElementAsync(); |
| | 0 | 479 | | fragmentingWriter.EndFragment(); |
| | 0 | 480 | | writer.EndCanonicalization(); |
| | | 481 | | |
| | 0 | 482 | | _fullBodyFragment = fullBodyFragment.ToArray(out _fullBodyFragmentLength); |
| | | 483 | | |
| | 0 | 484 | | _state = BodyState.Signed; |
| | 0 | 485 | | } |
| | | 486 | | |
| | | 487 | | private void WriteInnerMessageWithId(XmlDictionaryWriter writer) |
| | | 488 | | { |
| | 0 | 489 | | WriteStartInnerMessageWithId(writer); |
| | 0 | 490 | | InnerMessage.WriteBodyContents(writer); |
| | 0 | 491 | | writer.WriteEndElement(); |
| | 0 | 492 | | } |
| | | 493 | | |
| | | 494 | | private void WriteStartInnerMessageWithId(XmlDictionaryWriter writer) |
| | | 495 | | { |
| | 0 | 496 | | InnerMessage.WriteStartBody(writer); |
| | 0 | 497 | | if (_bodyIdInserted) |
| | | 498 | | { |
| | 0 | 499 | | _securityHeader.StandardsManager.IdManager.WriteIdAttribute(writer, BodyId); |
| | | 500 | | } |
| | 0 | 501 | | } |
| | | 502 | | |
| | | 503 | | private enum BodyState |
| | | 504 | | { |
| | | 505 | | Created, |
| | | 506 | | Signed, |
| | | 507 | | SignedThenEncrypted, |
| | | 508 | | EncryptedThenSigned, |
| | | 509 | | Encrypted, |
| | | 510 | | Disposed, |
| | | 511 | | } |
| | | 512 | | |
| | | 513 | | private struct BodyContentHelper |
| | | 514 | | { |
| | | 515 | | private MemoryStream _stream; |
| | | 516 | | private XmlDictionaryWriter _writer; |
| | | 517 | | |
| | | 518 | | public XmlDictionaryWriter CreateWriter() |
| | | 519 | | { |
| | 0 | 520 | | _stream = new MemoryStream(); |
| | 0 | 521 | | _writer = XmlDictionaryWriter.CreateTextWriter(_stream); |
| | 0 | 522 | | return _writer; |
| | | 523 | | } |
| | | 524 | | |
| | | 525 | | public ArraySegment<byte> ExtractResult() |
| | | 526 | | { |
| | 0 | 527 | | _writer.Flush(); |
| | 0 | 528 | | return new ArraySegment<byte>(_stream.GetBuffer(), 0, (int)_stream.Length); |
| | | 529 | | } |
| | | 530 | | } |
| | | 531 | | |
| | | 532 | | private sealed class MessagePrefixGenerator : IPrefixGenerator |
| | | 533 | | { |
| | | 534 | | private readonly XmlWriter _writer; |
| | | 535 | | |
| | 63 | 536 | | public MessagePrefixGenerator(XmlWriter writer) |
| | | 537 | | { |
| | 63 | 538 | | _writer = writer; |
| | 63 | 539 | | } |
| | | 540 | | |
| | | 541 | | public string GetPrefix(string namespaceUri, int depth, bool isForAttribute) |
| | | 542 | | { |
| | 0 | 543 | | return _writer.LookupPrefix(namespaceUri); |
| | | 544 | | } |
| | | 545 | | } |
| | | 546 | | } |
| | | 547 | | } |