Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [#630] Use the correct algorithm for at_hash and c_hash #659

Merged
merged 4 commits into from
Apr 16, 2022

Conversation

vivshankar
Copy link
Contributor

Related Issue or Design Document

The defect is described in #630 along with the discussion.

  1. at_hash: https://openid.net/specs/openid-connect-core-1_0.html#CodeIDToken
  2. c_hash: https://openid.net/specs/openid-connect-core-1_0.html#HybridIDToken

Checklist

  • I have read the contributing guidelines
    and signed the CLA.
  • I have referenced an issue containing the design document if my change
    introduces a new feature.
  • I have read the security policy.
  • I confirm that this pull request does not address a security
    vulnerability. If this pull request addresses a security vulnerability, I
    confirm that I got green light (please contact
    [email protected]) from the maintainers to push
    the changes.
  • I have added tests that prove my fix is effective or that my feature
    works.
  • I have added necessary documentation within the code base (if
    appropriate).

Further comments

There are two key changes:

  1. Standardize computation of the hash across the different flows
  2. Introduce ComputeHash that extracts the algorithm, if available, from the openid.Session header and computes the hash. It falls back to 256 key length to retain backward compatibility.

@vivshankar vivshankar changed the title bug: [#630] Use the correct algorithm for at_hash and c_hash fix: [#630] Use the correct algorithm for at_hash and c_hash Mar 26, 2022
Copy link
Member

@aeneasr aeneasr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thank you for your contribution! This looks pretty good and I have some ideas how to improve it further :)

}

var hash hash.Hash
if hashSize == 384 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would make sense to set hash directly, without the intermediate hashSize variable :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, could you elaborate on this please? The alg may or may not be present in the header, so the first step here is to compute the hash size.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess something along this pseudo-code:

hash := sha256.New()

if alg, ok := sess.IDTokenHeaders().Get("alg").(string); ok {
 		hashSize, _ := strconv.Atoi(alg[2:]) // 0 per default
 		switch hashSize {
 			case 384
				hash = sha512.New()
 			// ...
 		}
 	}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aeneasr Made the change.

handler/openid/helper.go Show resolved Hide resolved
@vivshankar
Copy link
Contributor Author

@aeneasr Pushed in the change based on your review comments. Hope this works.

Copy link
Member

@aeneasr aeneasr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thank you for your contribution!

@aeneasr aeneasr merged commit 8cb4b4b into ory:master Apr 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants