| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590 |
- //------------------------------------------------------------
- // Copyright (c) Microsoft Corporation. All rights reserved.
- //------------------------------------------------------------
- namespace System.ServiceModel.Security
- {
- using System;
- using System.ServiceModel.Channels;
- using System.ServiceModel.Description;
- using System.ServiceModel;
- using System.Security.Cryptography.Xml;
- using System.IdentityModel.Claims;
- using System.IdentityModel.Policy;
- using System.IdentityModel.Tokens;
- using System.IdentityModel.Selectors;
- using System.ServiceModel.Security.Tokens;
- using System.Runtime.Serialization;
- using System.Xml.Serialization;
- using System.Xml.Schema;
- using System.Xml;
- using System.Collections;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.IO;
- using System.ServiceModel.Security;
- using System.Globalization;
- using System.ServiceModel.Dispatcher;
- using System.Security.Authentication.ExtendedProtection;
- class RequestSecurityToken : BodyWriter
- {
- string context;
- string tokenType;
- string requestType;
- SecurityToken entropyToken;
- BinaryNegotiation negotiationData;
- XmlElement rstXml;
- IList<XmlElement> requestProperties;
- byte[] cachedWriteBuffer;
- int cachedWriteBufferLength;
- int keySize;
- Message message;
- SecurityKeyIdentifierClause renewTarget;
- SecurityKeyIdentifierClause closeTarget;
- OnGetBinaryNegotiationCallback onGetBinaryNegotiation;
- SecurityStandardsManager standardsManager;
- bool isReceiver;
- bool isReadOnly;
- object appliesTo;
- DataContractSerializer appliesToSerializer;
- Type appliesToType;
- object thisLock = new Object();
- public RequestSecurityToken()
- : this(SecurityStandardsManager.DefaultInstance)
- {
- }
- public RequestSecurityToken(MessageSecurityVersion messageSecurityVersion, SecurityTokenSerializer securityTokenSerializer)
- : this(SecurityUtils.CreateSecurityStandardsManager(messageSecurityVersion, securityTokenSerializer))
- {
- }
- public RequestSecurityToken(MessageSecurityVersion messageSecurityVersion,
- SecurityTokenSerializer securityTokenSerializer,
- XmlElement requestSecurityTokenXml,
- string context,
- string tokenType,
- string requestType,
- int keySize,
- SecurityKeyIdentifierClause renewTarget,
- SecurityKeyIdentifierClause closeTarget)
- : this(SecurityUtils.CreateSecurityStandardsManager(messageSecurityVersion, securityTokenSerializer),
- requestSecurityTokenXml,
- context,
- tokenType,
- requestType,
- keySize,
- renewTarget,
- closeTarget)
- {
- }
- public RequestSecurityToken(XmlElement requestSecurityTokenXml,
- string context,
- string tokenType,
- string requestType,
- int keySize,
- SecurityKeyIdentifierClause renewTarget,
- SecurityKeyIdentifierClause closeTarget)
- : this(SecurityStandardsManager.DefaultInstance,
- requestSecurityTokenXml,
- context,
- tokenType,
- requestType,
- keySize,
- renewTarget,
- closeTarget)
- {
- }
- internal RequestSecurityToken(SecurityStandardsManager standardsManager,
- XmlElement rstXml,
- string context,
- string tokenType,
- string requestType,
- int keySize,
- SecurityKeyIdentifierClause renewTarget,
- SecurityKeyIdentifierClause closeTarget)
- : base(true)
- {
- if (standardsManager == null)
- {
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("standardsManager"));
- }
- this.standardsManager = standardsManager;
- if (rstXml == null)
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("rstXml");
- this.rstXml = rstXml;
- this.context = context;
- this.tokenType = tokenType;
- this.keySize = keySize;
- this.requestType = requestType;
- this.renewTarget = renewTarget;
- this.closeTarget = closeTarget;
- this.isReceiver = true;
- this.isReadOnly = true;
- }
- internal RequestSecurityToken(SecurityStandardsManager standardsManager)
- : this(standardsManager, true)
- {
- // no op
- }
- internal RequestSecurityToken(SecurityStandardsManager standardsManager, bool isBuffered)
- : base(isBuffered)
- {
- if (standardsManager == null)
- {
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("standardsManager"));
- }
- this.standardsManager = standardsManager;
- this.requestType = this.standardsManager.TrustDriver.RequestTypeIssue;
- this.requestProperties = null;
- this.isReceiver = false;
- this.isReadOnly = false;
- }
- public ChannelBinding GetChannelBinding()
- {
- if (this.message == null)
- {
- return null;
- }
- ChannelBindingMessageProperty channelBindingMessageProperty = null;
- ChannelBindingMessageProperty.TryGet( this.message, out channelBindingMessageProperty );
- ChannelBinding channelBinding = null;
- if ( channelBindingMessageProperty != null )
- {
- channelBinding = channelBindingMessageProperty.ChannelBinding;
- }
- return channelBinding;
- }
- /// <summary>
- /// Will hold a reference to the outbound message from which we will fish the ChannelBinding out of.
- /// </summary>
- public Message Message
- {
- get { return message; }
- set { message = value; }
- }
-
- public string Context
- {
- get
- {
- return this.context;
- }
- set
- {
- if (this.IsReadOnly)
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
- this.context = value;
- }
- }
- public string TokenType
- {
- get
- {
- return this.tokenType;
- }
- set
- {
- if (this.IsReadOnly)
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
- this.tokenType = value;
- }
- }
- public int KeySize
- {
- get
- {
- return this.keySize;
- }
- set
- {
- if (this.IsReadOnly)
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
- if (value < 0)
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value", SR.GetString(SR.ValueMustBeNonNegative)));
- this.keySize = value;
- }
- }
- public bool IsReadOnly
- {
- get
- {
- return this.isReadOnly;
- }
- }
- public delegate void OnGetBinaryNegotiationCallback( ChannelBinding channelBinding );
- public OnGetBinaryNegotiationCallback OnGetBinaryNegotiation
- {
- get
- {
- return this.onGetBinaryNegotiation;
- }
- set
- {
- if (this.IsReadOnly)
- {
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
- }
- this.onGetBinaryNegotiation = value;
- }
- }
- public IEnumerable<XmlElement> RequestProperties
- {
- get
- {
- if (this.isReceiver)
- {
- // PreSharp Bug: Property get methods should not throw exceptions.
- #pragma warning suppress 56503
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ItemNotAvailableInDeserializedRST, "RequestProperties")));
- }
- return this.requestProperties;
- }
- set
- {
- if (this.IsReadOnly)
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
- if (value != null)
- {
- int index = 0;
- Collection<XmlElement> coll = new Collection<XmlElement>();
- foreach (XmlElement property in value)
- {
- if (property == null)
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(String.Format(CultureInfo.InvariantCulture, "value[{0}]", index)));
- coll.Add(property);
- ++index;
- }
- this.requestProperties = coll;
- }
- else
- {
- this.requestProperties = null;
- }
- }
- }
- public string RequestType
- {
- get
- {
- return this.requestType;
- }
- set
- {
- if (this.IsReadOnly)
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
- if (value == null)
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
- this.requestType = value;
- }
- }
- public SecurityKeyIdentifierClause RenewTarget
- {
- get
- {
- return this.renewTarget;
- }
- set
- {
- if (this.IsReadOnly)
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
- this.renewTarget = value;
- }
- }
- public SecurityKeyIdentifierClause CloseTarget
- {
- get
- {
- return this.closeTarget;
- }
- set
- {
- if (this.IsReadOnly)
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
- this.closeTarget = value;
- }
- }
- public XmlElement RequestSecurityTokenXml
- {
- get
- {
- if (!this.isReceiver)
- {
- // PreSharp Bug: Property get methods should not throw exceptions.
- #pragma warning suppress 56503
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ItemAvailableInDeserializedRSTOnly, "RequestSecurityTokenXml")));
- }
- return this.rstXml;
- }
- }
- internal SecurityStandardsManager StandardsManager
- {
- get
- {
- return this.standardsManager;
- }
- set
- {
- if (this.IsReadOnly)
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
- if (value == null)
- {
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("value"));
- }
- this.standardsManager = value;
- }
- }
- internal bool IsReceiver
- {
- get
- {
- return this.isReceiver;
- }
- }
- internal object AppliesTo
- {
- get
- {
- if (this.isReceiver)
- {
- // PreSharp Bug: Property get methods should not throw exceptions.
- #pragma warning suppress 56503
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ItemNotAvailableInDeserializedRST, "AppliesTo")));
- }
- return this.appliesTo;
- }
- }
- internal DataContractSerializer AppliesToSerializer
- {
- get
- {
- if (this.isReceiver)
- {
- // PreSharp Bug: Property get methods should not throw exceptions.
- #pragma warning suppress 56503
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ItemNotAvailableInDeserializedRST, "AppliesToSerializer")));
- }
- return this.appliesToSerializer;
- }
- }
- internal Type AppliesToType
- {
- get
- {
- if (this.isReceiver)
- {
- // PreSharp Bug: Property get methods should not throw exceptions.
- #pragma warning suppress 56503
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ItemNotAvailableInDeserializedRST, "AppliesToType")));
- }
- return this.appliesToType;
- }
- }
- protected Object ThisLock
- {
- get
- {
- return this.thisLock;
- }
- }
- internal void SetBinaryNegotiation(BinaryNegotiation negotiation)
- {
- if (negotiation == null)
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("negotiation");
- if (this.IsReadOnly)
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
- this.negotiationData = negotiation;
- }
- internal BinaryNegotiation GetBinaryNegotiation()
- {
- if (this.isReceiver)
- {
- return this.standardsManager.TrustDriver.GetBinaryNegotiation(this);
- }
- else if (this.negotiationData == null && this.onGetBinaryNegotiation != null)
- {
- this.onGetBinaryNegotiation(this.GetChannelBinding());
- }
- return this.negotiationData;
- }
- public SecurityToken GetRequestorEntropy()
- {
- return this.GetRequestorEntropy(null);
- }
- internal SecurityToken GetRequestorEntropy(SecurityTokenResolver resolver)
- {
- if (this.isReceiver)
- {
- return this.standardsManager.TrustDriver.GetEntropy(this, resolver);
- }
- else
- return this.entropyToken;
- }
- public void SetRequestorEntropy(byte[] entropy)
- {
- if (this.IsReadOnly)
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
- this.entropyToken = (entropy != null) ? new NonceToken(entropy) : null;
- }
- internal void SetRequestorEntropy(WrappedKeySecurityToken entropyToken)
- {
- if (this.IsReadOnly)
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
- this.entropyToken = entropyToken;
- }
- public void SetAppliesTo<T>(T appliesTo, DataContractSerializer serializer)
- {
- if (this.IsReadOnly)
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
- if (appliesTo != null && serializer == null)
- {
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("serializer");
- }
- this.appliesTo = appliesTo;
- this.appliesToSerializer = serializer;
- this.appliesToType = typeof(T);
- }
- public void GetAppliesToQName(out string localName, out string namespaceUri)
- {
- if (!this.isReceiver)
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ItemAvailableInDeserializedRSTOnly, "MatchesAppliesTo")));
- this.standardsManager.TrustDriver.GetAppliesToQName(this, out localName, out namespaceUri);
- }
- public T GetAppliesTo<T>()
- {
- return this.GetAppliesTo<T>(DataContractSerializerDefaults.CreateSerializer(typeof(T), DataContractSerializerDefaults.MaxItemsInObjectGraph));
- }
- public T GetAppliesTo<T>(XmlObjectSerializer serializer)
- {
- if (this.isReceiver)
- {
- if (serializer == null)
- {
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("serializer");
- }
- return this.standardsManager.TrustDriver.GetAppliesTo<T>(this, serializer);
- }
- else
- {
- return (T)this.appliesTo;
- }
- }
- void OnWriteTo(XmlWriter writer)
- {
- if (this.isReceiver)
- {
- this.rstXml.WriteTo(writer);
- }
- else
- {
- this.standardsManager.TrustDriver.WriteRequestSecurityToken(this, writer);
- }
- }
- public void WriteTo(XmlWriter writer)
- {
- if (writer == null)
- throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("writer");
- if (this.IsReadOnly)
- {
- // cache the serialized bytes to ensure repeatability
- if (this.cachedWriteBuffer == null)
- {
- MemoryStream stream = new MemoryStream();
- using (XmlDictionaryWriter binaryWriter = XmlDictionaryWriter.CreateBinaryWriter(stream, XD.Dictionary))
- {
- this.OnWriteTo(binaryWriter);
- binaryWriter.Flush();
- stream.Flush();
- stream.Seek(0, SeekOrigin.Begin);
- this.cachedWriteBuffer = stream.GetBuffer();
- this.cachedWriteBufferLength = (int)stream.Length;
- }
- }
- writer.WriteNode(XmlDictionaryReader.CreateBinaryReader(this.cachedWriteBuffer, 0, this.cachedWriteBufferLength, XD.Dictionary, XmlDictionaryReaderQuotas.Max), false);
- }
- else
- this.OnWriteTo(writer);
- }
- public static RequestSecurityToken CreateFrom(XmlReader reader)
- {
- return CreateFrom(SecurityStandardsManager.DefaultInstance, reader);
- }
- public static RequestSecurityToken CreateFrom(XmlReader reader, MessageSecurityVersion messageSecurityVersion, SecurityTokenSerializer securityTokenSerializer)
- {
- return CreateFrom(SecurityUtils.CreateSecurityStandardsManager(messageSecurityVersion, securityTokenSerializer), reader);
- }
- internal static RequestSecurityToken CreateFrom(SecurityStandardsManager standardsManager, XmlReader reader)
- {
- return standardsManager.TrustDriver.CreateRequestSecurityToken(reader);
- }
- public void MakeReadOnly()
- {
- if (!this.isReadOnly)
- {
- this.isReadOnly = true;
- if (this.requestProperties != null)
- {
- this.requestProperties = new ReadOnlyCollection<XmlElement>(this.requestProperties);
- }
- this.OnMakeReadOnly();
- }
- }
- internal protected virtual void OnWriteCustomAttributes(XmlWriter writer) { }
- internal protected virtual void OnWriteCustomElements(XmlWriter writer) { }
- internal protected virtual void OnMakeReadOnly() { }
- protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
- {
- WriteTo(writer);
- }
- }
- }
|