Example #1
0
		private static EndpointAddress ReadFrom (System.ServiceModel.Channels.AddressingVersion addressingVersion, XmlReader xreader)
		{
			string xml = xreader.ReadOuterXml ();
			StringReader sreader = new StringReader (xml);
			XmlReader reader = XmlReader.Create (sreader);
			Uri uri = null;
			EndpointIdentity identity = null;
			reader.MoveToContent ();
			List<AddressHeader> header = new List<AddressHeader> ();
			while (reader.NodeType == XmlNodeType.Element && !reader.IsEmptyElement) {
				if (reader.LocalName == "EndpointReference") {
					reader.Read ();
				} else if (reader.LocalName == "Address" && 
					reader.NodeType == XmlNodeType.Element &&
					!reader.IsEmptyElement)
					uri = new Uri (reader.ReadElementContentAsString ());
				else if (reader.LocalName == "Identity" && 
					reader.NodeType == XmlNodeType.Element &&
					!reader.IsEmptyElement) {
					//How can key re-empact Identity
					identity = new X509CertificateEndpointIdentity (new System.Security.Cryptography.X509Certificates.X509Certificate2 ("powershell.pfx", "mono"));
					break;
				} else {
					var headerName = reader.LocalName;
					var headerNamespace = reader.NamespaceURI;
					reader.MoveToContent ();
					reader.ReadStartElement ();
					var obj = reader.ReadElementContentAs (GetTypeFromLocalName (reader.LocalName), new XmlNamespaceManager (reader.NameTable));
					header.Add (AddressHeader.CreateAddressHeader (headerName, headerNamespace, obj));
					reader.MoveToContent ();
					reader.ReadEndElement ();
				}
			}
			return identity == null ? new EndpointAddress(uri, header.ToArray ()) : new EndpointAddress(uri, identity, header.ToArray ());
	  }
        X509Certificate2 GetServiceCertificate(SecurityTokenRequirement requirement)
        {
            // try X509CertificateEndpointIdentity,
            // ServiceCertificate.ScopedCertificate and
            // ServiceCertificate.DefaultCertificate.

            X509Certificate2 cert    = null;
            EndpointAddress  address = null;

            requirement.TryGetProperty(ReqType.TargetAddressProperty, out address);

            if (address != null)
            {
                X509CertificateEndpointIdentity ident = address.Identity as X509CertificateEndpointIdentity;
                if (ident != null && ident.Certificates.Count > 0)
                {
                    cert = ident.Certificates [0];
                }
                if (cert == null)
                {
                    credentials.ServiceCertificate.ScopedCertificates.TryGetValue(address.Uri, out cert);
                }
            }
            if (cert == null)
            {
                cert = credentials.ServiceCertificate.DefaultCertificate;
            }
            return(cert);
        }
Example #3
0
        public void WriteContentsTo(
            AddressingVersion addressingVersion,
            XmlDictionaryWriter writer)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
#if NET_2_1
            writer.WriteString(Uri.AbsoluteUri);
#else
            if (addressingVersion == AddressingVersion.None)
            {
                writer.WriteString(Uri.AbsoluteUri);
            }
            else
            {
                writer.WriteStartElement("Address", addressingVersion.Namespace);
                writer.WriteString(Uri.AbsoluteUri);
                writer.WriteEndElement();

                if (Identity == null)
                {
                    return;
                }

                if (Headers != null)
                {
                    foreach (AddressHeader ah in Headers)
                    {
                        ah.WriteAddressHeader(writer);
                    }
                }

                writer.WriteStartElement("Identity", Constants.WsaIdentityUri);

                X509CertificateEndpointIdentity x509 =
                    Identity as X509CertificateEndpointIdentity;
                if (x509 != null)
                {
                    KeyInfo         ki = new KeyInfo();
                    KeyInfoX509Data x  = new KeyInfoX509Data();
                    foreach (X509Certificate2 cert in x509.Certificates)
                    {
                        x.AddCertificate(cert);
                    }
                    ki.AddClause(x);
                    ki.GetXml().WriteTo(writer);
                }
                else
                {
                    DataContractSerializer ds = new DataContractSerializer(Identity.IdentityClaim.GetType());
                    ds.WriteObject(writer, Identity.IdentityClaim);
                }
                writer.WriteEndElement();
            }
#endif
        }
        internal static EndpointIdentity ReadIdentity(XmlDictionaryReader reader)
        {
            if (reader == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("reader");
            }
            EndpointIdentity identity = null;

            reader.MoveToContent();
            if (reader.IsEmptyElement)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(System.ServiceModel.SR.GetString("UnexpectedEmptyElementExpectingClaim", new object[] { XD.AddressingDictionary.Identity.Value, XD.AddressingDictionary.IdentityExtensionNamespace.Value })));
            }
            reader.ReadStartElement(XD.AddressingDictionary.Identity, XD.AddressingDictionary.IdentityExtensionNamespace);
            if (reader.IsStartElement(XD.AddressingDictionary.Spn, XD.AddressingDictionary.IdentityExtensionNamespace))
            {
                identity = new SpnEndpointIdentity(reader.ReadElementString());
            }
            else if (reader.IsStartElement(XD.AddressingDictionary.Upn, XD.AddressingDictionary.IdentityExtensionNamespace))
            {
                identity = new UpnEndpointIdentity(reader.ReadElementString());
            }
            else if (reader.IsStartElement(XD.AddressingDictionary.Dns, XD.AddressingDictionary.IdentityExtensionNamespace))
            {
                identity = new DnsEndpointIdentity(reader.ReadElementString());
            }
            else if (reader.IsStartElement(XD.XmlSignatureDictionary.KeyInfo, XD.XmlSignatureDictionary.Namespace))
            {
                reader.ReadStartElement();
                if (reader.IsStartElement(XD.XmlSignatureDictionary.X509Data, XD.XmlSignatureDictionary.Namespace))
                {
                    identity = new X509CertificateEndpointIdentity(reader);
                }
                else
                {
                    if (!reader.IsStartElement(XD.XmlSignatureDictionary.RsaKeyValue, XD.XmlSignatureDictionary.Namespace))
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(System.ServiceModel.SR.GetString("UnrecognizedIdentityType", new object[] { reader.Name, reader.NamespaceURI })));
                    }
                    identity = new RsaEndpointIdentity(reader);
                }
                reader.ReadEndElement();
            }
            else
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(System.ServiceModel.SR.GetString("UnrecognizedIdentityType", new object[] { reader.Name, reader.NamespaceURI })));
                }
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(System.ServiceModel.SR.GetString("InvalidIdentityElement")));
            }
            reader.ReadEndElement();
            return(identity);
        }
Example #5
0
        void IXmlSerializable.WriteXml(XmlWriter writer)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            writer.WriteStartElement("Address", Constants.WsaNamespace);
            writer.WriteString(address.Uri.AbsoluteUri);
            writer.WriteEndElement();

            if (address.Identity == null)
            {
                return;
            }

            if (address.Headers != null)
            {
                foreach (AddressHeader ah in address.Headers)
                {
                    ah.WriteAddressHeader(writer);
                }
            }

            writer.WriteStartElement("Identity", Constants.WsaIdentityUri);
#if !NET_2_1
            X509CertificateEndpointIdentity x509 =
                address.Identity as X509CertificateEndpointIdentity;
            if (x509 != null)
            {
                KeyInfo         ki = new KeyInfo();
                KeyInfoX509Data x  = new KeyInfoX509Data();
                foreach (X509Certificate2 cert in x509.Certificates)
                {
                    x.AddCertificate(cert);
                }
                ki.AddClause(x);
                ki.GetXml().WriteTo(writer);
            }
            else
            {
                DataContractSerializer ds = new DataContractSerializer(address.Identity.IdentityClaim.GetType());
                ds.WriteObject(writer, address.Identity.IdentityClaim);
            }
#endif
            writer.WriteEndElement();
        }
		public void FullRequest ()
		{
			EndpointIdentity identity =
				new X509CertificateEndpointIdentity (new X509Certificate2 ("Test/Resources/test.pfx", "mono"));
			EndpointAddress address =
				new EndpointAddress (new Uri ("stream:dummy"), identity);

			Message mreq = Message.CreateMessage (MessageVersion.Default, "myAction");
			Message mreply = null;

XmlWriterSettings settings = new XmlWriterSettings ();
settings.Indent = true;

			// listener setup
			ReplyHandler replyHandler = delegate (Message rinput) {
				mreply = rinput;
			};
			RequestReceiver receiver = delegate () {
				return mreq;
			};
			IChannelListener<IReplyChannel> listener = CreateListener (replyHandler, receiver);
			listener.Open ();
			IReplyChannel reply = listener.AcceptChannel ();
			reply.Open ();

			RequestSender reqHandler = delegate (Message input) {
				try {
					// sync version somehow causes an infinite loop (!?)
					RequestContext ctx = reply.EndReceiveRequest (reply.BeginReceiveRequest (TimeSpan.FromSeconds (5), null, null));
//					RequestContext ctx = reply.ReceiveRequest (TimeSpan.FromSeconds (5));
					Console.Error.WriteLine ("Acquired RequestContext.");
					ctx.Reply (input);
				} catch (Exception ex) {
					Console.Error.WriteLine ("ERROR during processing a request in FullRequest()");
					Console.Error.WriteLine (ex);
					Console.Error.Flush ();
					throw;
				}
				return mreply;
			};
			CustomBinding b = CreateBinding (reqHandler);

			IRequestChannel ch = ChannelFactory<IRequestChannel>.CreateChannel (b, address);

			ch.Open ();
			Console.Error.WriteLine ("**** starting a request  ****");
			IAsyncResult async = ch.BeginRequest (mreq, null, null);
			Console.Error.WriteLine ("**** request started. ****");
			Message res = ch.EndRequest (async);
		}
		EndpointAddress CreateX509EndpointAddress (string uri)
		{
			EndpointIdentity identity =
				new X509CertificateEndpointIdentity (new X509Certificate2 ("Test/Resources/test.pfx", "mono"));
			return new EndpointAddress (new Uri (uri), identity);
		}
		public void ClientAcceptUpgrade ()
		{
			ServiceCredentials cred = new ServiceCredentials ();
			X509Certificate2 cert = 
				new X509Certificate2 ("Test/Resources/test.cer");
			cred.ServiceCertificate.Certificate = cert;
			X509CertificateEndpointIdentity ident =
				new X509CertificateEndpointIdentity (cert);
			StreamSecurityUpgradeProvider p = CreateClientProvider (cred, ident);
			p.Open ();
			try {
				StreamSecurityUpgradeAcceptor a =
					p.CreateUpgradeAcceptor ()
					as StreamSecurityUpgradeAcceptor;
				Assert.IsNotNull (a, "#1");
				SecurityMessageProperty prop =
					a.GetRemoteSecurity ();
				Assert.IsNull (prop, "#2"); // hmm
				Stream s = a.AcceptUpgrade (new MemoryStream (new byte [] {1, 2, 3, 4, 5}));
			} finally {
				p.Close ();
			}
		}
Example #9
0
        internal static EndpointIdentity ReadIdentity(XmlDictionaryReader reader)
        {
            if (reader == null)
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("reader");

            EndpointIdentity readIdentity = null;

            reader.MoveToContent();
            if (reader.IsEmptyElement)
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.UnexpectedEmptyElementExpectingClaim, XD.AddressingDictionary.Identity.Value, XD.AddressingDictionary.IdentityExtensionNamespace.Value)));

            reader.ReadStartElement(XD.AddressingDictionary.Identity, XD.AddressingDictionary.IdentityExtensionNamespace);

            if (reader.IsStartElement(XD.AddressingDictionary.Spn, XD.AddressingDictionary.IdentityExtensionNamespace))
            {
                readIdentity = new SpnEndpointIdentity(reader.ReadElementString());
            }
            else if (reader.IsStartElement(XD.AddressingDictionary.Upn, XD.AddressingDictionary.IdentityExtensionNamespace))
            {
                readIdentity = new UpnEndpointIdentity(reader.ReadElementString());
            }
            else if (reader.IsStartElement(XD.AddressingDictionary.Dns, XD.AddressingDictionary.IdentityExtensionNamespace))
            {
                readIdentity = new DnsEndpointIdentity(reader.ReadElementString());
            }
            else if (reader.IsStartElement(XD.XmlSignatureDictionary.KeyInfo, XD.XmlSignatureDictionary.Namespace))
            {
                reader.ReadStartElement();
                if (reader.IsStartElement(XD.XmlSignatureDictionary.X509Data, XD.XmlSignatureDictionary.Namespace))
                {
                    readIdentity = new X509CertificateEndpointIdentity(reader);
                }
                else if (reader.IsStartElement(XD.XmlSignatureDictionary.RsaKeyValue, XD.XmlSignatureDictionary.Namespace))
                {
                    throw ExceptionHelper.PlatformNotSupported("EndpointIdentity.ReadIdentity RsaEndpointIdentity is not supported.");
                }
                else
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.UnrecognizedIdentityType, reader.Name, reader.NamespaceURI)));
                }
                reader.ReadEndElement();
            }
            else if (reader.NodeType == XmlNodeType.Element)
            {
                //
                // Something unknown
                // 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.UnrecognizedIdentityType, reader.Name, reader.NamespaceURI)));
            }
            else
            {
                //
                // EndpointIdentity element is empty or some other invalid xml
                //
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.InvalidIdentityElement));
            }

            reader.ReadEndElement();

            return readIdentity;
        }
Example #10
0
        internal static EndpointIdentity ReadIdentity(XmlDictionaryReader reader)
        {
            if (reader == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("reader");
            }

            EndpointIdentity readIdentity = null;

            reader.MoveToContent();
            if (reader.IsEmptyElement)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.UnexpectedEmptyElementExpectingClaim, XD.AddressingDictionary.Identity.Value, XD.AddressingDictionary.IdentityExtensionNamespace.Value)));
            }

            reader.ReadStartElement(XD.AddressingDictionary.Identity, XD.AddressingDictionary.IdentityExtensionNamespace);

            if (reader.IsStartElement(XD.AddressingDictionary.Spn, XD.AddressingDictionary.IdentityExtensionNamespace))
            {
                readIdentity = new SpnEndpointIdentity(reader.ReadElementString());
            }
            else if (reader.IsStartElement(XD.AddressingDictionary.Upn, XD.AddressingDictionary.IdentityExtensionNamespace))
            {
                readIdentity = new UpnEndpointIdentity(reader.ReadElementString());
            }
            else if (reader.IsStartElement(XD.AddressingDictionary.Dns, XD.AddressingDictionary.IdentityExtensionNamespace))
            {
                readIdentity = new DnsEndpointIdentity(reader.ReadElementString());
            }
            else if (reader.IsStartElement(XD.XmlSignatureDictionary.KeyInfo, XD.XmlSignatureDictionary.Namespace))
            {
                reader.ReadStartElement();
                if (reader.IsStartElement(XD.XmlSignatureDictionary.X509Data, XD.XmlSignatureDictionary.Namespace))
                {
                    readIdentity = new X509CertificateEndpointIdentity(reader);
                }
                else if (reader.IsStartElement(XD.XmlSignatureDictionary.RsaKeyValue, XD.XmlSignatureDictionary.Namespace))
                {
                    readIdentity = new RsaEndpointIdentity(reader);
                }
                else
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.UnrecognizedIdentityType, reader.Name, reader.NamespaceURI)));
                }
                reader.ReadEndElement();
            }
            else if (reader.NodeType == XmlNodeType.Element)
            {
                //
                // Something unknown
                //
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.UnrecognizedIdentityType, reader.Name, reader.NamespaceURI)));
            }
            else
            {
                //
                // EndpointIdentity element is empty or some other invalid xml
                //
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.InvalidIdentityElement)));
            }

            reader.ReadEndElement();

            return(readIdentity);
        }
 internal static EndpointIdentity ReadIdentity(XmlDictionaryReader reader)
 {
     if (reader == null)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("reader");
     }
     EndpointIdentity identity = null;
     reader.MoveToContent();
     if (reader.IsEmptyElement)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(System.ServiceModel.SR.GetString("UnexpectedEmptyElementExpectingClaim", new object[] { XD.AddressingDictionary.Identity.Value, XD.AddressingDictionary.IdentityExtensionNamespace.Value })));
     }
     reader.ReadStartElement(XD.AddressingDictionary.Identity, XD.AddressingDictionary.IdentityExtensionNamespace);
     if (reader.IsStartElement(XD.AddressingDictionary.Spn, XD.AddressingDictionary.IdentityExtensionNamespace))
     {
         identity = new SpnEndpointIdentity(reader.ReadElementString());
     }
     else if (reader.IsStartElement(XD.AddressingDictionary.Upn, XD.AddressingDictionary.IdentityExtensionNamespace))
     {
         identity = new UpnEndpointIdentity(reader.ReadElementString());
     }
     else if (reader.IsStartElement(XD.AddressingDictionary.Dns, XD.AddressingDictionary.IdentityExtensionNamespace))
     {
         identity = new DnsEndpointIdentity(reader.ReadElementString());
     }
     else if (reader.IsStartElement(XD.XmlSignatureDictionary.KeyInfo, XD.XmlSignatureDictionary.Namespace))
     {
         reader.ReadStartElement();
         if (reader.IsStartElement(XD.XmlSignatureDictionary.X509Data, XD.XmlSignatureDictionary.Namespace))
         {
             identity = new X509CertificateEndpointIdentity(reader);
         }
         else
         {
             if (!reader.IsStartElement(XD.XmlSignatureDictionary.RsaKeyValue, XD.XmlSignatureDictionary.Namespace))
             {
                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(System.ServiceModel.SR.GetString("UnrecognizedIdentityType", new object[] { reader.Name, reader.NamespaceURI })));
             }
             identity = new RsaEndpointIdentity(reader);
         }
         reader.ReadEndElement();
     }
     else
     {
         if (reader.NodeType == XmlNodeType.Element)
         {
             throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(System.ServiceModel.SR.GetString("UnrecognizedIdentityType", new object[] { reader.Name, reader.NamespaceURI })));
         }
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(System.ServiceModel.SR.GetString("InvalidIdentityElement")));
     }
     reader.ReadEndElement();
     return identity;
 }