Fix NullReferenceException when signing with an RSA JWK containing both private key and x5c#3533
Open
chris-peterson wants to merge 1 commit into
Open
Fix NullReferenceException when signing with an RSA JWK containing both private key and x5c#3533chris-peterson wants to merge 1 commit into
chris-peterson wants to merge 1 commit into
Conversation
JsonWebKeyConverter.TryConvertToSecurityKey tried TryConvertToX509SecurityKey before TryCreateToRsaSecurityKey for kty=RSA. A JWK carrying both private RSA parameters and x5c was converted to an X509SecurityKey built from x5c[0] (a public-only certificate), discarding the private key. Signing then dereferenced the null private key and threw a NullReferenceException in AsymmetricAdapter.SignUsingSpanRsa. Reorder the two attempts so RSA key material is used when present. An x5c-only JWK still converts to an X509SecurityKey, because TryCreateToRsaSecurityKey returns false unless both n and e are present. Add a converter unit test (RSA JWK with private parameters + x5c converts to an RsaSecurityKey) and an end-to-end signing test that reproduced the NRE. Refs: AzureAD#3260
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3260
Problem
JsonWebKeyConverter.TryConvertToSecurityKeytriesTryConvertToX509SecurityKeybeforeTryCreateToRsaSecurityKeyforkty=RSA. A JWK that carries both private RSA parameters (d/p/q/…) andx5cis therefore converted to anX509SecurityKeybuilt fromx5c[0]— a public-only certificate — and the private RSA parameters are discarded.When that key is used to sign:
AsymmetricAdapter.InitializeUsingX509SecurityKeycallsInitializeUsingRsa(x509SecurityKey.PrivateKey as RSA, …)withrequirePrivateKey: true.X509SecurityKey.PrivateKeyreturnsnull(Certificate.GetRSAPrivateKey()on a public-only cert), so the adapter stores a nullRSA.NullReferenceExceptioninAsymmetricAdapter.SignUsingSpanRsa.Removing
x5cfrom the same key (forcing theRsaSecurityKeyconversion) signs successfully, which confirms the precedence is the cause. This is the signing-correctness angle of #3260, in addition to the memory consideration the issue was originally filed for.Fix
Reorder the two attempts in the
kty=RSAbranch so RSA key material is used when present:An
x5c-only JWK still converts to anX509SecurityKey, becauseTryCreateToRsaSecurityKeyreturnsfalseunless bothnandeare present.Behavior change
A JWK that carries both
n/eandx5cnow converts to anRsaSecurityKeyinstead of anX509SecurityKey. Cryptographic operations are unaffected; the difference is the concreteSecurityKeytype a caller observes (and the avoidedX509Certificate2load). This is the prioritization requested in #3260.Tests
JsonWebKeyConverterTest.TryConvertToSecurityKey_RsaJsonWebKeyWithPrivateKeyAndX5c_ConvertsToRsaSecurityKey— asserts a JWK with private RSA parameters andx5cconverts to anRsaSecurityKey.JsonWebTokenHandlerTests.CreateToken_RsaJsonWebKeyWithPrivateKeyAndX5c_Signs— end-to-end signing test that reproduced theNullReferenceExceptionbefore this change.Both tests fail on the prior ordering (the converter yields
X509SecurityKey, and signing throws the NRE atAsymmetricAdapter.SignUsingSpanRsa) and pass with the reorder.