How to: Verify Digital Signatures of SOAP Messages Signed Using a Security Context Token

WSE validates that a digital signature is cryptographically correct. However, user code or policy should be used to verify that a signature exists and that the signature applies to the expected set of XML elements. When WSE is configured to run with the recipient, signature validation is done by WSE before recipient code executes.

To configure WSE to validate digital signatures for incoming SOAP messages

  1. In the Web.config file for the Web service, include an <add> Element for <soapExtensionTypes> (WSE for Microsoft .NET) element in the <soapExtensionTypes> section.

    When the SOAP message recipient is a Web service client, this configuration entry is not required. Instead, the base class that the proxy class derives from must be changed to derive from the WebServicesClientProtocol.

    The following code example shows the configuration entry that must be placed in the Web.config file for WSE to run with a Web service. The type attribute of the <add> Element for <soapExtensionTypes> (WSE for Microsoft .NET) element in the <soapExtensionTypes> section must be on one line, even though the following example shows it split across multiple lines for readability.

    <configuration>
      <system.web>
        <webServices>
           <soapExtensionTypes>
             <add type="Microsoft.Web.Services2.WebServicesExtension, 
               Microsoft.Web.Services2,Version=2.0.0.0, Culture=neutral, 
               PublicKeyToken=31bf3856ad364e35" 
               priority="1" group="0"/>
          </soapExtensionTypes>
        </webServices>
      </system.web>
    </configuration>
    

To use policy to require incoming SOAP messages to be signed using a security context token and to verify that the required XML elements are signed

  1. Define a policy assertion and digital signature requirements by adding <Policy> Element (WSE for Microsoft .NET) (1) and <Integrity> Element elements.

    1. Add a <Policy> Element (WSE for Microsoft .NET) (1) element to the policy file for the application. Add the <Policy> Element (WSE for Microsoft .NET) (1) element as a child element of the <policies> Element element.
      The <Policy> Element (WSE for Microsoft .NET) (1) element defines criteria that a SOAP message must meet. The criteria are specified as child elements of the <Policy> Element (WSE for Microsoft .NET) (1) element. The Id attribute value provides a name that is used by the <request> Element (WSE for Microsoft .NET), <response> Element (WSE for Microsoft .NET), and <fault> Element elements to refer to the policy assertion when applying the policy to an endpoint.
    2. Add an <Integrity> Element child element to the <Policy> Element (WSE for Microsoft .NET) (1) element.
      The <Integrity> Element element defines digital signature requirements. The Usage attribute value of "Required" specifies that a signature is required, and additional requirements are specified in child elements.

    The following code example defines a policy assertion named policy-c0a22319-6b89-49ff-9b82-bdbac5f04618 and specifies that there are digital signature requirements.

                  <wsp:Policy wsu:Id="policy-c0a22319-6b89-49ff-9b82-bdbac5f04618"
      xmlns:wsp="https://schemas.xmlsoap.org/ws/2002/12/policy"
      xmlns:wsa="https://schemas.xmlsoap.org/ws/2004/03/addressing">
      <wssp:Integrity wsp:Usage="wsp:Required"     xmlns:wssp="https://schemas.xmlsoap.org/ws/2002/12/secext">
    
  2. Specify the token type by adding <TokenInfo> Element, <SecurityToken> Element, and <TokenType> Element elements.

    1. Add a <TokenInfo> Element child element to the <Integrity> Element element.
    2. Add a <SecurityToken> Element child element to the <TokenInfo> Element element.
    3. Add a <TokenType> Element child element to the <SecurityToken> Element element and set its value to https://schemas.xmlsoap.org/ws/2004/04/security/sc/sct.
      The <TokenType> Element element specifies the type of security token that must be used to create the digital signature.

    The following code example specifies that a SecurityContextToken security token must be used to digitally sign the SOAP message.

    <wssp:Integrity wsp:Usage="wsp:Required"
      xmlns:wssp="https://schemas.xmlsoap.org/ws/2002/12/secext">
      <wssp:TokenInfo>    <wssp:SecurityToken>      <wssp:TokenType>https://schemas.xmlsoap.org/ws/2004/04/security/sc/sct</wssp:TokenType>
    
  3. Specify the security token service that issued the token by adding a <TokenIssuer> Element (WSE for Microsoft .NET) (1) element.

    The following code example specifies that the http://www.cohowinery.com/secureConversation.ashx security token service issued the SecurityContextToken security token.

    <wssp:SecurityToken>
      <wssp:TokenIssuer>    http://www.cohowinery.com/secureConversation.ashx  </wssp:TokenIssuer>
    
  4. Map the policy assertion to an endpoint adding an <endpoint> Element element. Optionally, designate the policy as the default for all SOAP messages by adding the <defaultOperation> Element element.

    1. Add an <endpoint> Element child element to the <mappings> Element element in the policy file for the application, and set the uri attribute value to the URI of the application.
      The <endpoint> Element element maps a policy assertion to an endpoint. The uri attribute value specifies the URI of the service to which the policy is mapped.
    2. Add a <defaultOperation> Element child element to the <endpoint> Element element.
      The <defaultOperation> Element element specifies the default policy for all operations at the URI specified in the uri attribute.
    3. Add <request> Element (WSE for Microsoft .NET), <response> Element (WSE for Microsoft .NET), and <fault> Element child elements to the <defaultOperation> Element element. The value of the policy attribute must match the value of the Id attribute of the <Policy> Element (WSE for Microsoft .NET) (1) element that defines the policy assertion.

    The following code example sets the application's default policy for all SOAP messages to the policy-c0a22319-6b89-49ff-9b82-bdbac5f04618 policy assertion.

    <mappings xmlns:wse="https://schemas.microsoft.com/wse/2003/06/Policy">
      <endpoint uri="https://localhost/SecureConvPolicyService/SecureConvService.asmx">
        <defaultOperation>
          <request policy="#policy-c0a22319-6b89-49ff-9b82-bdbac5f04618" />
          <response policy="#policy-c0a22319-6b89-49ff-9b82-bdbac5f04618" />
          <fault policy="#policy-c0a22319-6b89-49ff-9b82-bdbac5f04618" />
        </defaultOperation>
      </endpoint>
    </mappings>
    
  5. Specify the XML elements to be signed by adding a <MessageParts> Element for <Integrity> Element element to the <Integrity> Element element.

    1. Add a <MessageParts> Element for <Integrity> Element child element to the <Integrity> Element element in the policy file for the application, and set the Default attribute value to "https://schemas.xmlsoap.org/2002/12/wsse#part".
      The <MessageParts> Element for <Integrity> Element element specifies the XML elements that must be signed. The Dialect attribute specifies the expression dialect used to specify the parts of the SOAP message. The only dialect supported by WSE is "https://schemas.xmlsoap.org/2002/12/wsse#part".
    2. Specify the parts of the message to be signed by listing them, separated by spaces, as the value of the <MessageParts> Element for <Integrity> Element element.

    The following code example specifies that the <Body> element and the To, Action, MessageID, and From headers are signed.

    <wssp:MessageParts Dialect="https://schemas.xmlsoap.org/2002/12/wsse#part">
      wsp:Body() wsp:Header(wsa:To) wsp:Header(wsa:Action)
      wsp:Header(wsa:MessageID) wsp:Header(wsa:From)
    </wssp:MessageParts>
    
  6. Specify that the XML elements that must be signed, exist in incoming SOAP messages.

    1. Add a <MessagePredicate> Element child element to the <Policy> Element (WSE for Microsoft .NET) (1) element in the policy file for the application.
      When using policy to require digital signatures on incoming SOAP messages, always use the <MessagePredicate> element. The <MessagePredicate> element ensures that the XML elements that must be signed actually exist in the SOAP message. Just adding an <Integrity> Element element to a policy file specifies that a digital signature must exist, if the XML elements specified in the <Integrity> element exist in the SOAP messages. If the SOAP message does not contain the XML elements specified in the <Integrity> element, the SOAP message satisfies the requirements of the policy and is allowed to access the Web service. To remedy this potential problem, add a <MessagePredicate> element specifying the XML elements that must exist in the incoming SOAP message.
      Set the Dialect attribute value to "https://schemas.xmlsoap.org/2002/12/wsse#part".

    2. Specify the parts of the message that must exist by listing them, separated by spaces, as the value of the <MessagePredicate> Element element.
      The following code example specifies that the <Body> element and the To, Action, MessageID, and From headers exist in the SOAP message.

      <wsp:MessagePredicate 
        Dialect="https://schemas.xmlsoap.org/2002/12/wsse#part">
        wsp:Body() wsp:Header(wsa:To) wsp:Header(wsa:Action) wsp:Header(wsa:MessageID) wsp:Header(wsa:From)
      </wsp:MessagePredicate>
      

To use code to require that incoming SOAP messages are signed using a security context token and that the required XML elements are signed

  1. Add a Web service method that processes the security context token.

    1. In Solution Explorer, right-click Service1.asmx, and then click View Code.

    2. Add the Imports or using directives to the Service1.asmx code-behind file as shown in the following code example.

      Imports Microsoft.Web.Services2.Security
      Imports Microsoft.Web.Services2
      
      using Microsoft.Web.Services2.Security;
      using Microsoft.Web.Services2;
      
    3. In the Service1 class, add code to verify that the <Body> element of the SOAP request was signed using a security context token.
      The following code example adds a Web service method named sayHello that verifies the request was made using SOAP and calls the method named IsMessageSigned defined in the next step.

      <WebMethod()>  _
      Public Function sayHello() As String
          Dim requestContext As SoapContext = _
            RequestSoapContext.Current
      
          ' Verify that a SOAP request was received.  
          If requestContext Is Nothing Then
              Throw New ApplicationException( "Either a non-SOAP " + _
                  "request was received or WSE is not properly " + _
                  "installed for the Web application hosting the " + _
                  "Web service.")
          End If
          If Not IsMessageSigned(requestContext) Then
              Throw New ApplicationException("The request is not signed.")
          End If
          Return "Hello"
      End Function
      
      [WebMethod]
      public string sayHello() 
      {
          SoapContext requestContext = RequestSoapContext.Current;
      
          // Verify that a SOAP request was received.  
          if (requestContext == null)
          {
              throw new ApplicationException( "Either a non-SOAP " +
                  "request was received or WSE is not properly " +
                  "installed for the Web application hosting the " +
                  "Web service.");
          }
          // Verify that the SOAP message is signed.
          if (!IsMessageSigned(requestContext))
          {
              throw new ApplicationException(
                  "The request is not signed.");
          }
          return "Hello";
      }
      
  2. Define a method to get the security context token that is used to sign the SOAP message.

    1. In Solution Explorer, right-click Service1.asmx, and then click View Code.

    2. In the Service1 class, add the code shown in the following code example.
      The following code example verifies that a digital signature exists for a SoapContext and that it signed the <Body> element.

      Private Function IsMessageSigned(ByVal context As SoapContext) As Boolean
          Dim element As ISecurityElement
          For Each element In context.Security.Elements
              If (TypeOf (element) Is MessageSignature) Then
                  ' The SoapContext contains a Signature element. 
                  Dim sign As MessageSignature = element
                  If ((sign.SignatureOptions And _
                      SignatureOptions.IncludeSoapBody) <> 0) Then
                      ' The SOAP body is signed.
                      Return True
                  End If
              End If
          Next
          Return False
      End Function 
      
      private bool IsMessageSigned(SoapContext context)
      {
          foreach (ISecurityElement element in context.Security.Elements)
          {
              if (element is MessageSignature)
              {
                  // The given context contains a Signature element.
                  MessageSignature sig = element as MessageSignature;
                  if ((sig.SignatureOptions & 
                      SignatureOptions.IncludeSoapBody) != 0)
                  {
                      // The SOAP Body is signed.
                      return true;
                  }
              }
          }
          return false;
      }
      

Example

The following code example is a policy file for a target Web service that issues SecurityContextToken security tokens. The policy requires that security token requests sent to the https://www.contoso.com/SecureConvService.asmx endpoint include a base token that is a UsernameToken security token. SOAP requests sent to the Web service must be signed using a DerivedKeyToken that is derived from the issued SecurityContextToken security token.

Note

This code example is designed to demonstrate WSE features and is not intended for production use.

<?xml version="1.0" encoding="utf-8" ?>
<policyDocument xmlns="https://schemas.microsoft.com/wse/2003/06/Policy">
  <mappings>
    <endpoint uri="https://www.contoso.com/SecureConvService.asmx">
      <operation
        requestAction="http://stockservice.contoso.com/StockQuoteRequest">
        <!-- Requests must be signed with an SCT -->
        <request policy="#SignWithSCT" />
        <!-- Responses must be signed with an SCT -->
        <response policy="#SignWithSCT" />
        <!-- No policy for faults -->
        <fault policy="" />
      </operation>
            
      <!--
        The following section describes the policy requirements for the
        SCT issuing service. The built in SecurityContextTokenManager
        will send requests to an SCT's token issuer as described in policy
        and the following policy mapping demonstrates how to use policy
        make sure the requests get signed with an appropriate base token.
      -->
      <operation
        requestAction="https://schemas.xmlsoap.org/ws/2004/04/security/trust/RST/SCT">
        <request policy="#SignWithUsernameToken" />
        <response policy="" />
        <fault policy="" />
      </operation>
    </endpoint>
        
  </mappings>
  <policies 
    xmlns:wsa="https://schemas.xmlsoap.org/ws/2004/03/addressing"
    xmlns:wse="https://schemas.microsoft.com/wse/2003/06/Policy"
    xmlns:wsp="https://schemas.xmlsoap.org/ws/2002/12/policy"
    xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
    xmlns:wssp="https://schemas.xmlsoap.org/ws/2002/12/secext"
    xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
        
    <!--
      This policy is used for the target Web service. It requires 
      the body and addressing headers to be signed with the 
      SecureContextToken.
    -->
    <wsp:Policy wsu:Id="SignWithSCT">
      <!-- 
        MessagePredicate is used to require the headers.
        This assertion should be used along with the 
        Integrity assertion when the presence of the 
        signed element is required.
        
        NOTE: this assertion doesn't do anything for 
        enforcement (send-side) policy.
      -->
      <wsp:MessagePredicate 
        Dialect="https://schemas.xmlsoap.org/2002/12/wsse#part">
        wsp:Body() wsp:Header(wsa:To) wsp:Header(wsa:Action) wsp:Header(wsa:MessageID) wse:Timestamp()
       </wsp:MessagePredicate>

       <!-- 
         Integrity is used to require the headers to be signed 
         Note that a only an authenticated SCT token is 
         required. Many Web services will also use the token 
         for authorization, such as by using the <wse:Role>
         claim.
       -->            
       <wssp:Integrity wsp:Usage="wsp:Required">
         <wssp:TokenInfo>
           <wssp:SecurityToken wse:IdentityToken="true">
             <wssp:TokenType>https://schemas.xmlsoap.org/ws/2004/04/security/sc/dk</wssp:TokenType>
             <wssp:Claims>
               <wse:Parent>
                 <wssp:SecurityToken wse:IdentityToken="true">
                   <wssp:TokenType>https://schemas.xmlsoap.org/ws/2004/04/security/sc/sct</wssp:TokenType>
                   <wssp:Claims>
                     <wse:BaseToken>
                       <wssp:SecurityToken xmlns="https://schemas.xmlsoap.org/ws/2002/12/secext">
                         <wssp:TokenType>http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#UsernameToken</wssp:TokenType>
                       </wssp:SecurityToken>
                     </wse:BaseToken>
                   </wssp:Claims>                        
                 </wssp:SecurityToken>
               </wse:Parent>
             </wssp:Claims>                        
           </wssp:SecurityToken>
         </wssp:TokenInfo>
         <wssp:MessageParts
           Dialect="https://schemas.xmlsoap.org/2002/12/wsse#part">
           wse:Timestamp() wse:Addressing() wsp:Body()
         </wssp:MessageParts>
       </wssp:Integrity>
    </wsp:Policy>
        
    <!--
      This policy is used for the token issuer service. It requires 
      the body and addressing headers to be signed with a 
      UsernameToken.    
    -->
    <wsp:Policy wsu:Id="SignWithUsernameToken">

    <!-- 
      MessagePredicate is used to require the headers.
      This assertion should be used along with the 
      Integrity assertion when the presence of the 
      signed element is required.
       
      NOTE: this assertion doesn't do anything for 
      enforcement (send-side) policy.
    -->
      <wsp:MessagePredicate 
        Dialect="https://schemas.xmlsoap.org/2002/12/wsse#part">
        wsp:Body() wsp:Header(wsa:To) wsp:Header(wsa:Action) wsp:Header(wsa:MessageID) wse:Timestamp()
      </wsp:MessagePredicate>

      <!-- 
        Integrity is used to require the headers to be signed 
        Note that a only an authenticated username token is 
        required. Many Web services will also use the token 
        for authorization, such as by using the <wse:Role>
        claim.
      -->
      <wssp:Integrity wsp:Usage="wsp:Required">
        <wssp:TokenInfo>
          <SecurityToken xmlns="https://schemas.xmlsoap.org/ws/2002/12/secext">
            <wssp:TokenType>http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#UsernameToken</wssp:TokenType>
          </SecurityToken>
        </wssp:TokenInfo>
        <wssp:MessageParts
          Dialect="https://schemas.xmlsoap.org/2002/12/wsse#part">
          wse:Timestamp() wse:Addressing() wsp:Body()
        </wssp:MessageParts>
      </wssp:Integrity>
    </wsp:Policy>
  </policies>
</policyDocument>

See Also

Tasks

How to: Sign a SOAP Message By Using a Security Context Token
How to: Specify the Parts of a SOAP Message That Are Signed or Encrypted

Reference

SecurityContextToken

Other Resources

Issuing Security Context Tokens
Security Context Token