Implement Secure .NET Web Services with WS-Security

Implement Secure .NET Web Services with WS-Security

Web Services Enhancements 1.0 for Microsoft .NET (WSE) provides the functionality for Microsoft .NET Framework developers to support the latest Web services capabilities. WSE is the cornerstone of the GXA (Global XML Web-Services Architecture), an architecture of proposed Web services standards that Microsoft, IBM, and other companies have written. (Table 1 describes the core specifications of GXA.)

This article examines the GXA’s WS-Security spec and demonstrates how you can use it to implement secure .NET Web services by digitally signing, encrypting, and adding security credentials to SOAP messages.

How to Implement WSE
WSE is implemented as a SOAP extension and therefore must be registered within the web.config file of your Web service. To accommodate this task, the web.config file contains the element. Within this element you can configure all SOAP extensions, which should be available to your Web service at runtime.

I have added the following lines within the section of the web.config file:

                           

Note that the new element must be written on one line. I’ve divided it into several lines only for better reading. Adding this new text to web.config configures the SOAP extension on the server side. It is then ready to use.

To expose the functionality of the WSE to the client, you must derive the Web service proxy class from the class WebServicesClientProtocol, which lives in the namespace Microsoft.Web.Services. So when you add a Web reference to your project and want to use the WSE, you must modify the Reference.cs file manually and change the base class of the proxy to WebServicesClientProtocol. Currently, Visual Studio.NET does not provide an option to mark a Web service as WSE-enabled.

Once you’ve registered WSE and derived the Web service proxy class, both the client and server sides are ready to use the new features of the WSE.

RequestSoapContext
On the client side, all WSE features can be accessed through the proxy class and a property called RequestSoapContext. With this context object you can now encrypt SOAP messages, sign them, and assign user credentials to them. The following code shows how to get a reference to this object:

SoapContext myContext = myProxyClass.RequestSoapContext;

Traceable Client/Server Traffic
A very useful feature of the WSE is all the network traffic between the client and server can be traced. These tracing capabilities can be also configured in the web.config file. But before you can use this feature, you must create a new section called Microsoft.Web.Services. This task can be accomplish with the following code:

         

With this entry in web.config you configure a new section called Microsoft.Web.Services and the appropriate section handler Microsoft.Web.Services.Configuration.WebServicesConfiguration. After that, the tracing feature can be enabled with the following lines:

                           

All further requests from the client are written in the inputTrace.config file and the response from the Web service is written in the outputTrace.config file. How you can examine and interpret the content of both these files is shown later in this article.

If you have programmed Web services with the .NET Framework, you realize it has no platform-independent support for securing Web services across the Internet. With the introduction of WS-Security, that support now exists.

WS-Security offers the following new security functions:

  • Digitally signing SOAP messages:
    • using an X.509 certificate
    • using a user name and a password
    • using a custom binary token
  • Encrypting SOAP messages:
    • using an X.509 certificate
    • using a shared secret
    • using a custom binary token
  • Adding security credentials to the SOAP message
  • Signing a SOAP Request with a User Name and Password
    Let’s look more closely at digitally signing SOAP messages with a user name and a password, since this option is used very often in Web services. To work correctly, the Web service itself must provide a class that implements the interface IPasswordProvider:

    public interface IPasswordProvider{   public String GetPassword(UsernameToken token);}

    As you can see, there is only one method, GetPassword, which is made available by the implementing class. The WSE framework calls this method when it must authenticate a Web service request. The parameter of type UsernameToken provides more information about the incoming user request. As a result the method must return the user’s password. The property Username of the class UsernameToken contains the credentials of the user making the SOAP request. With it, returning the correct user password is very easy (e.g., querying from a database or XML file).

    After that the runtime creates a hash of the password and compares it to the password hash provided by the Web service request. If the hashes match, the request is processed. If not, the request is rejected.

    A value from the enumeration PasswordOption defines how the password is sent across the wire:

  • SendNone: No password is sent in the SOAP message.
  • SendHashed: A SHA-1 hash of the password is sent in the SOAP message.
  • SendPlainText: The password is sent in clear text. When using this option, a secure transport channel such as SSL should be used.

    The following listing shows how a SOAP request can be signed with a username and a password:

    // Create a UsernameTokenUsernameToken userToken = new UsernameToken(   Environment.UserName, "MyPassword",    PasswordOption.SendHashed);// Create proxy classMyProxyClass proxy = new MyProxyClass();SoapContext context = proxy.RequestSoapContext();// Add a security tokencontext.Security.Tokens.Add(userToken);// Sign the SOAP requestcontext.Security.Elements.Add(new Signature(userToken))// Call a methodproxy.HelloWorld();

    As you can see, you can sign a SOAP request by just adding a new instance of the class Signature to the collection Elements. Easy, isn’t it? But which bits were sent across the wire?

    Within the element you will find the section . This section is responsible for the additional information that WSE uses. The sub-section contains the user credentials sent with the SOAP request:

       MyUserName         GXLG8cYAao4CGppz60e/cHz0M0o=   ...

    Only the hash of the password is sent across the network. This hash is then compared to the hash from the password that the method GetPassword of the interface IPasswordProvider returned. If both hashes match, the SOAP request is successfully authenticated.

    The section follows . It contains additional information about the signature used to sign the SOAP request. Here you will find some sections that define various algorithms for signing the request. Every algorithm has a unique URI identifying it. The SOAP body contains one of these URIs, which identifies the algorithm used to sign the body:

       ......   

    The View from the Server Side
    Now let’s look on the server side of this Web service. As mentioned previously, you must implement the interface IPasswordProvider and register the implementing class in the WSE runtime. You do this in the section of the web.config file of the Web service:

                

    The attribute type of the element takes the class in the form of Namespace.ClassName, ClassName. I have implemented the interface as follows:

    public class PasswordProvider : IPasswordProvider{   public String GetPassword(UsernameToken token)   {      return "password";   }}

    In a real Web service you can query the password of the current user (token.Username) from a storage entity like a database or a XML file. The Web method I use is straightforward:

     [WebMethod]public String HelloWorld(){   SoapContext requestContext =       HttpSoapContext.RequestContext;   String strResult = ";   if (requestContext != null)   {      UsernameToken token = GetFirstUsernameToken(         requestContext.Security);      if (token != null)      {         strResult = "Hello World, " + token.Username;      }   }   return strResult;}private UsernameToken GetFirstUsernameToken(Security sec){   UsernameToken retval = null;   if (sec.Tokens.Count > 0)   {      foreach (SecurityToken tok in sec.Tokens)      {         retval = tok as UsernameToken;         if (retval != null) return retval;      }   }}

    With the private method GetFirstUsernameToken I find the first UsernameToken and return the string Hello World, including the name of the user. When you set breakpoints at the methods GetPassword and HelloWorld, you can see that the former is called before the latter. When the client sends an incorrect password with the SOAP request and you debug the Web service again, you find out that the method GetPassword is called but then the request is rejected. The WSE runtime prevents the code in the function HelloWorld from being executed.

    Encrypting SOAP Messages
    When you use the Web service infrastructure provided by the .NET Framework, Web service calls are not encrypted. That means anyone who understands Internet protocols can read your messages and change them! When such a message is signed with a signature, as shown in the previous section, the attacker has no chance to compromise it?but he can still read the content of the SOAP call!

    To avoid this security risk you can encrypt the SOAP bodies of your messages, so that a network sniffer cannot read them. An attacker will see only unimportant chars and will not be able to reconstruct the original SOAP call.

    For the encryption of SOAP messages the WSE offers you the following three possibilities:

  • Use an X.509 certificate
  • Use a shared secret
  • Use a custom binary token
  • The Web service itself must provide a class that implements the interface IDecryptionKeyProvider:

    public interface IDecryptionKeyProvider{   public DecryptionKey GetDecryptionKey(     String algorithmUri,      KeyInfo keyInfo);}

    When the WSE runtime decrypts a SOAP request, it calls the method GetDecryptionKey. This method must return a key that can decrypt the request. If this key cannot, because you perhaps supplied a wrong key, the Web service rejects the request and throws an exception of type SoapException.

    The following listing shows the required code to implement the IDecryptionKeyProvider interface. The function GetDecryptionKey returns a key based on the Triple-DES-Algorithm:

    public DecryptionKey GetDecryptionKey(String algorithmUri,    KeyInfo keyInfo){   byte [] keyBytes = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,       12, 13, 14, 15, 16 };   byte [] ivBytes = { 1, 2, 3, 4, 5, 6, 7 };   foreach (KeyInfoClause clause in keyInfo)   {      if (((KeyInfoName)clause).Value == Solvion Symmetric          Key)      {         SymmetricAlgorithm algo = new             TripleDESCryptoServiceProvider();         algo.Key = keyBytes;         algo.IV = ivBytes;         SymmetricDecryptionKey key = new             SymmetricDecryptionKey(algo);         return key;      }   }   return null;}

    The class implementing the interface also must be registered in the file web.config, as shown here:

                               

    The Web method itself is programmed as simply as possible:

     [WebMethod]public String EncryptedSoapMessage(){   SoapContext requestContext =       HttpSoapContext.RequestContext;   String strResult = ;   if (requestContext != null)   {      strResult = Hello World from my encrypted Web          service.;   }   return strResult;}

    In the client code, I have written the method GetEncryptionKey, which returns the key for the encryption of the SOAP message. This key is then added to the proxy of the Web service, so that the WSE can encrypt the SOAP call:

    private void CallEncryptedSoapMessage(){   MyProxyClass proxy = new MyProxyClass();   SoapContext requestContext = proxy.RequestSoapContext;   EncryptionKey key = GetEncryptionKey();   try   {      requestContext.Security.Elements.Add(new          EncrytedData(key));      requestContext.Timestamp.Ttl = 60000;      Console.WriteLine(proxy.EncryptedSoapMessage());   }   catch (SoapException ex)   {      Console.WriteLine(ex.Message);   }}

    When you are debugging the Web service call, you can see that the method GetDecryptionKey is called before the Web method is executed. When you provide an incorrect key in the client code, you see that the WSE automatically rejects the call to the Web method. When you take a closer look at the trace files, you see that the whole body of the SOAP request is encrypted and it cannot be decrypted without the right key.

    The Cornerstone of the GXA
    This article has given you a brief introduction to the WSE and has shown how you can program secure Web services using WS-Security. This current WSE release offers you the possibilities to sign and encrypt Web service requests. Its actual implementation is the cornerstone of the GXA architecture, which provides many more proposal standards for Web services infrastructure issues.

    devx-admin

    devx-admin

    Share the Post:
    Clean Energy Adoption

    Inside Michigan’s Clean Energy Revolution

    Democratic state legislators in Michigan continue to discuss and debate clean energy legislation in the hopes of establishing a comprehensive clean energy strategy for the

    Chips Act Revolution

    European Chips Act: What is it?

    In response to the intensifying worldwide technology competition, Europe has unveiled the long-awaited European Chips Act. This daring legislative proposal aims to fortify Europe’s semiconductor

    Revolutionized Low-Code

    You Should Use Low-Code Platforms for Apps

    As the demand for rapid software development increases, low-code platforms have emerged as a popular choice among developers for their ability to build applications with

    Global Layoffs

    Tech Layoffs Are Getting Worse Globally

    Since the start of 2023, the global technology sector has experienced a significant rise in layoffs, with over 236,000 workers being let go by 1,019

    Clean Energy Adoption

    Inside Michigan’s Clean Energy Revolution

    Democratic state legislators in Michigan continue to discuss and debate clean energy legislation in the hopes of establishing a comprehensive clean energy strategy for the state. A Senate committee meeting

    Chips Act Revolution

    European Chips Act: What is it?

    In response to the intensifying worldwide technology competition, Europe has unveiled the long-awaited European Chips Act. This daring legislative proposal aims to fortify Europe’s semiconductor supply chain and enhance its

    Revolutionized Low-Code

    You Should Use Low-Code Platforms for Apps

    As the demand for rapid software development increases, low-code platforms have emerged as a popular choice among developers for their ability to build applications with minimal coding. These platforms not

    Cybersecurity Strategy

    Five Powerful Strategies to Bolster Your Cybersecurity

    In today’s increasingly digital landscape, businesses of all sizes must prioritize cyber security measures to defend against potential dangers. Cyber security professionals suggest five simple technological strategies to help companies

    Global Layoffs

    Tech Layoffs Are Getting Worse Globally

    Since the start of 2023, the global technology sector has experienced a significant rise in layoffs, with over 236,000 workers being let go by 1,019 tech firms, as per data

    Huawei Electric Dazzle

    Huawei Dazzles with Electric Vehicles and Wireless Earbuds

    During a prominent unveiling event, Huawei, the Chinese telecommunications powerhouse, kept quiet about its enigmatic new 5G phone and alleged cutting-edge chip development. Instead, Huawei astounded the audience by presenting

    Cybersecurity Banking Revolution

    Digital Banking Needs Cybersecurity

    The banking, financial, and insurance (BFSI) sectors are pioneers in digital transformation, using web applications and application programming interfaces (APIs) to provide seamless services to customers around the world. Rising

    FinTech Leadership

    Terry Clune’s Fintech Empire

    Over the past 30 years, Terry Clune has built a remarkable business empire, with CluneTech at the helm. The CEO and Founder has successfully created eight fintech firms, attracting renowned

    The Role Of AI Within A Web Design Agency?

    In the digital age, the role of Artificial Intelligence (AI) in web design is rapidly evolving, transitioning from a futuristic concept to practical tools used in design, coding, content writing

    Generative AI Revolution

    Is Generative AI the Next Internet?

    The increasing demand for Generative AI models has led to a surge in its adoption across diverse sectors, with healthcare, automotive, and financial services being among the top beneficiaries. These

    Microsoft Laptop

    The New Surface Laptop Studio 2 Is Nuts

    The Surface Laptop Studio 2 is a dynamic and robust all-in-one laptop designed for creators and professionals alike. It features a 14.4″ touchscreen and a cutting-edge design that is over

    5G Innovations

    GPU-Accelerated 5G in Japan

    NTT DOCOMO, a global telecommunications giant, is set to break new ground in the industry as it prepares to launch a GPU-accelerated 5G network in Japan. This innovative approach will

    AI Ethics

    AI Journalism: Balancing Integrity and Innovation

    An op-ed, produced using Microsoft’s Bing Chat AI software, recently appeared in the St. Louis Post-Dispatch, discussing the potential concerns surrounding the employment of artificial intelligence (AI) in journalism. These

    Savings Extravaganza

    Big Deal Days Extravaganza

    The highly awaited Big Deal Days event for October 2023 is nearly here, scheduled for the 10th and 11th. Similar to the previous year, this autumn sale has already created

    Cisco Splunk Deal

    Cisco Splunk Deal Sparks Tech Acquisition Frenzy

    Cisco’s recent massive purchase of Splunk, an AI-powered cybersecurity firm, for $28 billion signals a potential boost in tech deals after a year of subdued mergers and acquisitions in the

    Iran Drone Expansion

    Iran’s Jet-Propelled Drone Reshapes Power Balance

    Iran has recently unveiled a jet-propelled variant of its Shahed series drone, marking a significant advancement in the nation’s drone technology. The new drone is poised to reshape the regional

    Solar Geoengineering

    Did the Overshoot Commission Shoot Down Geoengineering?

    The Overshoot Commission has recently released a comprehensive report that discusses the controversial topic of Solar Geoengineering, also known as Solar Radiation Modification (SRM). The Commission’s primary objective is to

    Remote Learning

    Revolutionizing Remote Learning for Success

    School districts are preparing to reveal a substantial technological upgrade designed to significantly improve remote learning experiences for both educators and students amid the ongoing pandemic. This major investment, which

    Revolutionary SABERS Transforming

    SABERS Batteries Transforming Industries

    Scientists John Connell and Yi Lin from NASA’s Solid-state Architecture Batteries for Enhanced Rechargeability and Safety (SABERS) project are working on experimental solid-state battery packs that could dramatically change the

    Build a Website

    How Much Does It Cost to Build a Website?

    Are you wondering how much it costs to build a website? The approximated cost is based on several factors, including which add-ons and platforms you choose. For example, a self-hosted

    Battery Investments

    Battery Startups Attract Billion-Dollar Investments

    In recent times, battery startups have experienced a significant boost in investments, with three businesses obtaining over $1 billion in funding within the last month. French company Verkor amassed $2.1

    Copilot Revolution

    Microsoft Copilot: A Suit of AI Features

    Microsoft’s latest offering, Microsoft Copilot, aims to revolutionize the way we interact with technology. By integrating various AI capabilities, this all-in-one tool provides users with an improved experience that not

    AI Girlfriend Craze

    AI Girlfriend Craze Threatens Relationships

    The surge in virtual AI girlfriends’ popularity is playing a role in the escalating issue of loneliness among young males, and this could have serious repercussions for America’s future. A

    AIOps Innovations

    Senser is Changing AIOps

    Senser, an AIOps platform based in Tel Aviv, has introduced its groundbreaking AI-powered observability solution to support developers and operations teams in promptly pinpointing the root causes of service disruptions