Token.IssuerPublicKeyHash

Security Briefs

Syndication

Updated on 20 May 2007 to fix a bug in the code.

In my last post, I complained about the Token class not exposing the public key claim directly. Here's some code that will help you if you simply want to track the public key for a personal card in a user profile. Add this to the Token class and party on.

Here's the code:

// If this code works, Keith wrote it!
string issuerPublicKeyHash;
public string IssuerPublicKeyHash {
    get {
        if (null == issuerPublicKeyHash) {
            issuerPublicKeyHash = computeIssuerPublicKeyHash();
        }
        return issuerPublicKeyHash;
    }
}
string computeIssuerPublicKeyHash() {
    RSA issuerPublicKey = null;
    foreach (ClaimSet cs in m_authorizationContext.ClaimSets) {
        // find the ClaimSet whose issuer is identified by an Rsa key.
        foreach (Claim rsaClaim in cs.Issuer.FindClaims(ClaimTypes.Rsa, Rights.Identity)) {
            issuerPublicKey = (RSA)rsaClaim.Resource;
            break;
        }
        if (null != issuerPublicKey) break;
    }
    if (null == issuerPublicKey) throw new Exception("Couldn't find issuer's RSA claim");
    // hash exponent and modulus, and return base64 encoded string
    RSAParameters keyParams = issuerPublicKey.ExportParameters(false);
    SHA256Managed hashAlg = new SHA256Managed();
    hashAlg.TransformBlock(keyParams.Exponent, 0,
        keyParams.Exponent.Length, null, 0);
    byte[] hash = hashAlg.TransformFinalBlock(keyParams.Modulus, 0,
        keyParams.Modulus.Length);
    return Convert.ToBase64String(hash);
}

Posted May 10 2007, 11:44 AM by keith-brown
Filed under: , ,

Comments

Security Briefs wrote Primary keys in the identity metasystem
on 05-10-2007 1:45 PM
Marc Brooks wrote re: Token.IssuerPublicKeyHash
on 05-20-2007 12:05 AM
Should that inner check be != (as a double-break facilitator)? Otherwise, you're hosed if the identity Claim isn't in the first ClaimSet.

if (null != issuerPublicKey) break;
Keith Brown wrote re: Token.IssuerPublicKeyHash
on 05-20-2007 6:44 AM
Good catch, Marc. Fixed.
Security Briefs wrote Marc finds a bug
on 05-20-2007 8:48 AM