John Lam 1997 — 2026

WSE 2.0, UsernameToken and wsse:PasswordDigest

If you’re using WSE 2.0 and UsernameToken to handle your web service authentication requirements, you’ll immediately run into a few problems. First, if you use it “out of the box” (aka allowing the default UsernameTokenManager implementation to authenticate your users), you must pass your passwords to the server as plain text via the wsse:PasswordText option. UsernameTokenManager.AuthenticateToken() simply passes the plain text password to the Win32 LogonUser() API to authenticate the user.

This solution is sub-optimal for several reasons. First, you must send your credentials over a secure transport like SSL/TLS to avoid exposing your users’ credentials to eavesdroppers. Second, you pay the performance hit of using a secure transport even in cases where the payload of your SOAP message does not have to be encrypted. Finally, your users must either have local machine accounts or Active Directory domain accounts - which for many web applications is not what you really want.

If you want to use the wsse:PasswordDigest option to transport a hashed password to the server, you’ll have to implement a custom TokenManager class. You can do this by deriving a new type from UsernameTokenManager and overriding the AuthenticateToken virtual method. In this method, you must lookup and return the user’s plain text password in your authentication database.

The WSE 2.0 infrastructure does a number of things when it transports a hashed password to the server. On the client side, it transmits a base-64 encoded hash of a nonce (one-time value) + password + created time to the server. On the server, it calculates the base-64 encoded hash of the nonce + password supplied by your AuthenticateToken method + created time and performs a comparison. If the values match, you’re allowed in - if they don’t the client receives a security fault.

The main problem with this approach is that your authentication database must store the plain text user password. Since users typically use the same password across many servers, if your server is compromised you have effectively compromised your users’ security on multiple servers. One approach that Keith Brown has suggested is to use what he calls a scope URI. The scope URI is either known ahead of time, or is discovered at runtime by inspecting a WS-Policy document. It can be something as simple as the DNS name fof your web service endpoint (e.g. services.yourcompany.com).

Here’s a simple class that generates a hex encoded SHA1 hash of a password + a scope URI.

Update: Dominick Baier who I met last week through the DevelopMentor alumni network (he just joined my alma mater), posted a nice tip in my comments that trimmed a lot of excess fat (and added a whole lot of functionality) to my ScopeUriValidator class. It now handles Unicode correctly.

public sealed class ScopeUriValidator {
  private string _scopeUri;
  private SHA1 _hashFunction = new SHA1CryptoServiceProvider();

  public ScopeUriValidator(string uri) {
    _scopeUri = uri;
  }

  public string CalculateValidator(string password) {
    string validator = password + _scopeUri;
    byte[] bytes = System.Text.Encoding.Unicode.GetBytes(password);
    byte[] hash = _hashFunction.ComputeHash(bytes);
    return System.Text.Encoding.Unicode.GetString(hash);
  }
}
3 archived reader comments

Haacked (2004-11-01)

Interesting approach. I wrote up an approach whereby the client can request the Salt value for the particular user and then uses the salt when sending the hash value. Although the salt is somewhat publicly available, it solves the issue of using the hashed password value on other sites.

http://haacked.com/archive/2004/09/09/1177.aspx

dominick (2004-11-01)

hey john!

was nice meeting you at windev!

couldn’t you just replace your stringbyte conversion with

System.Text.Encoding.Unicode.GetBytes()/GetString()

dominick

John Lam (2004-11-01)

Hey Dominick -

This is why I left the exercise to the reader - I knew that method must exist, but I had no idea where to look :) Thanks for the pointer - I’ll update the code frag.

Hope to see you over here next time around on the conference circuit!