Skip to content

Commit a9a37e8

Browse files
iNinjaCopilot
andcommitted
Fix PrivateKeyStatus ordering and make unsupported flag volatile
Access MlDsaPrivateKey before checking _mlDsaPrivateKeyUnsupported so the flag is set during initialization. Previously, first access would return DoesNotExist instead of Unknown on unsupported platforms. Make _mlDsaPrivateKeyUnsupported volatile for correct cross-thread visibility, consistent with the other initialization flags. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 26d94db commit a9a37e8

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

src/Microsoft.IdentityModel.Tokens/X509SecurityKey.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class X509SecurityKey : AsymmetricSecurityKey
3737
bool _isMlDsa;
3838
volatile bool _mlDsaPrivateKeyInitialized;
3939
volatile bool _mlDsaPublicKeyInitialized;
40-
bool _mlDsaPrivateKeyUnsupported;
40+
volatile bool _mlDsaPrivateKeyUnsupported;
4141
#if NET9_0_OR_GREATER
4242
Lock _thisLock = new();
4343
#else
@@ -282,10 +282,12 @@ public override PrivateKeyStatus PrivateKeyStatus
282282
// DoesNotExist, so Unknown will pass the constructor guard. However, the adapter
283283
// will fail with a clear InvalidOperationException (IDX10723) when it attempts
284284
// to access the null MlDsaPrivateKey during signing initialization.
285+
MLDsa mlDsaPrivateKey = MlDsaPrivateKey;
286+
285287
if (_mlDsaPrivateKeyUnsupported)
286288
return PrivateKeyStatus.Unknown;
287289

288-
return MlDsaPrivateKey != null ? PrivateKeyStatus.Exists : PrivateKeyStatus.DoesNotExist;
290+
return mlDsaPrivateKey != null ? PrivateKeyStatus.Exists : PrivateKeyStatus.DoesNotExist;
289291
}
290292

291293
return PrivateKey == null ? PrivateKeyStatus.DoesNotExist : PrivateKeyStatus.Exists;

0 commit comments

Comments
 (0)