-
Notifications
You must be signed in to change notification settings - Fork 458
Expand file tree
/
Copy pathJsonWebTokenHandler.ValidateSignature.cs
More file actions
368 lines (330 loc) · 17.2 KB
/
JsonWebTokenHandler.ValidateSignature.cs
File metadata and controls
368 lines (330 loc) · 17.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Microsoft.IdentityModel.Abstractions;
using Microsoft.IdentityModel.JsonWebTokens.Results;
using Microsoft.IdentityModel.Logging;
using Microsoft.IdentityModel.Tokens;
using TokenLogMessages = Microsoft.IdentityModel.Tokens.LogMessages;
namespace Microsoft.IdentityModel.JsonWebTokens
{
#nullable enable
/// <remarks>This partial class contains methods and logic related to the validation of tokens' signatures.</remarks>
public partial class JsonWebTokenHandler : TokenHandler
{
/// <summary>
/// Validates the JWT signature.
/// </summary>
/// <param name="jwtToken">The JWT token to validate.</param>
/// <param name="validationParameters">The parameters used for validation.</param>
/// <param name="configuration">The optional configuration used for validation.</param>
/// <param name="callContext">The context in which the method is called.</param>
/// <exception cref="ArgumentNullException">Returned if <paramref name="jwtToken"/> or <paramref name="validationParameters"/> is null.</exception>"
/// <exception cref="SecurityTokenInvalidSignatureException">Returned by the default implementation if the token is not signed, or if the validation fails.</exception>
/// <exception cref="SecurityTokenInvalidAlgorithmException">Returned if the algorithm is not supported by the key.</exception>
/// <exception cref="SecurityTokenSignatureKeyNotFoundException">Returned if the key cannot be resolved.</exception>
internal static SignatureValidationResult ValidateSignature(
JsonWebToken jwtToken,
ValidationParameters validationParameters,
BaseConfiguration? configuration,
CallContext callContext)
{
if (jwtToken is null)
return SignatureValidationResult.NullParameterFailure(nameof(jwtToken));
if (validationParameters is null)
return SignatureValidationResult.NullParameterFailure(nameof(validationParameters));
// Delegate is set by the user, we call it and return the result.
if (validationParameters.SignatureValidator is not null)
return validationParameters.SignatureValidator(jwtToken, validationParameters, configuration, callContext);
// If the user wants to accept unsigned tokens, they must implement the delegate.
if (!jwtToken.IsSigned)
return new SignatureValidationResult(
ValidationFailureType.SignatureValidationFailed,
new ExceptionDetail(
new MessageDetail(
TokenLogMessages.IDX10504,
LogHelper.MarkAsSecurityArtifact(
jwtToken.EncodedToken,
JwtTokenUtilities.SafeLogJwtToken)
),
typeof(SecurityTokenInvalidSignatureException),
new StackFrame()));
SecurityKey? key = null;
if (validationParameters.IssuerSigningKeyResolver is not null)
{
key = validationParameters.IssuerSigningKeyResolver(
jwtToken.EncodedToken,
jwtToken,
jwtToken.Kid,
validationParameters,
configuration,
callContext);
}
else
{
// Resolve the key using the token's 'kid' and 'x5t' headers.
// Fall back to the validation parameters' keys if configuration keys are not set.
key = JwtTokenUtilities.ResolveTokenSigningKey(jwtToken.Kid, jwtToken.X5t, configuration?.SigningKeys)
?? JwtTokenUtilities.ResolveTokenSigningKey(jwtToken.Kid, jwtToken.X5t, validationParameters.IssuerSigningKeys);
}
if (key is not null)
{
jwtToken.SigningKey = key;
// If the key is found, validate the signature.
return ValidateSignatureWithKey(jwtToken, key, validationParameters, callContext);
}
// Key could not be resolved. Depending on the configuration, try all keys or return an error.
if (validationParameters.TryAllIssuerSigningKeys)
return ValidateSignatureUsingAllKeys(jwtToken, validationParameters, configuration, callContext);
else
return new SignatureValidationResult(
ValidationFailureType.SignatureValidationFailed,
new ExceptionDetail(
new MessageDetail(TokenLogMessages.IDX10500),
typeof(SecurityTokenSignatureKeyNotFoundException),
new StackFrame()));
}
private static SignatureValidationResult ValidateSignatureUsingAllKeys(
JsonWebToken jwtToken,
ValidationParameters
validationParameters, BaseConfiguration? configuration,
CallContext callContext)
{
// control gets here if:
// 1. User specified delegate: IssuerSigningKeyResolver returned null
// 2. ResolveIssuerSigningKey returned null
// Try all the keys. This is the degenerate case, not concerned about perf.
(SignatureValidationResult? configResult, bool configKidMatched, KeyMatchFailedResult? configFailedResult) = ValidateUsingKeys(
jwtToken,
validationParameters,
configuration?.SigningKeys,
callContext);
if (configResult is not null)
return configResult;
(SignatureValidationResult? vpResult, bool vpKidMatched, KeyMatchFailedResult? vpFailedResult) = ValidateUsingKeys(
jwtToken,
validationParameters,
validationParameters.IssuerSigningKeys,
callContext);
if (vpResult is not null)
return vpResult;
if (vpFailedResult is null && configFailedResult is null) // No keys were attempted
return new SignatureValidationResult(
ValidationFailureType.SignatureValidationFailed,
new ExceptionDetail(
new MessageDetail(TokenLogMessages.IDX10500),
typeof(SecurityTokenSignatureKeyNotFoundException),
new StackFrame()));
StringBuilder exceptionStrings = new();
StringBuilder keysAttempted = new ();
PopulateFailedResults(configFailedResult, exceptionStrings, keysAttempted);
PopulateFailedResults(vpFailedResult, exceptionStrings, keysAttempted);
bool kidExists = !string.IsNullOrEmpty(jwtToken.Kid);
bool kidMatched = configKidMatched || vpKidMatched;
// No valid signature found. Return the exception details.
return new SignatureValidationResult(
ValidationFailureType.SignatureValidationFailed,
GetSignatureValidationFailureExceptionDetails(
jwtToken,
validationParameters,
configuration,
exceptionStrings,
keysAttempted,
kidExists,
kidMatched));
}
private static (SignatureValidationResult? validResult, bool KidMatched, KeyMatchFailedResult? failedResult) ValidateUsingKeys(
JsonWebToken jwtToken,
ValidationParameters validationParameters,
ICollection<SecurityKey>? keys,
CallContext callContext)
{
if (keys is null || keys.Count == 0)
return (null, false, null);
if (keys is not IList<SecurityKey> keysList)
keysList = keys.ToList();
bool kidExists = !string.IsNullOrEmpty(jwtToken.Kid);
bool kidMatched = false;
IList<SecurityKey>? keysAttempted = null;
IList<SignatureValidationResult>? results = null;
for (int i = 0; i < keysList.Count; i++)
{
SecurityKey key = keysList[i];
SignatureValidationResult result = ValidateSignatureWithKey(jwtToken, key, validationParameters, callContext);
if (result.IsValid)
{
jwtToken.SigningKey = key;
return (result, true, null);
}
keysAttempted ??= [];
results ??= [];
results.Add(result);
keysAttempted.Add(key);
if (kidExists && !kidMatched && key.KeyId is not null)
kidMatched = jwtToken.Kid.Equals(key.KeyId, key is X509SecurityKey ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal);
}
if (results is not null && results.Count > 0 && keysAttempted is not null && keysAttempted.Count > 0)
return (null, kidMatched, new KeyMatchFailedResult(results, keysAttempted));
// No keys were attempted.
return (null, kidMatched, null);
}
private static SignatureValidationResult ValidateSignatureWithKey(
JsonWebToken jsonWebToken,
SecurityKey key,
ValidationParameters validationParameters,
CallContext callContext)
{
CryptoProviderFactory cryptoProviderFactory = validationParameters.CryptoProviderFactory ?? key.CryptoProviderFactory;
if (!cryptoProviderFactory.IsSupportedAlgorithm(jsonWebToken.Alg, key))
{
return new SignatureValidationResult(
ValidationFailureType.SignatureValidationFailed,
new ExceptionDetail(
new MessageDetail(
LogMessages.IDX14000,
LogHelper.MarkAsNonPII(jsonWebToken.Alg),
key),
typeof(SecurityTokenInvalidAlgorithmException),
new StackFrame()));
}
AlgorithmValidationResult result = validationParameters.AlgorithmValidator(
jsonWebToken.Alg,
key,
jsonWebToken,
validationParameters,
callContext);
if (!result.IsValid)
return new SignatureValidationResult(
ValidationFailureType.SignatureValidationFailed,
result.ExceptionDetail);
SignatureProvider signatureProvider = cryptoProviderFactory.CreateForVerifying(key, jsonWebToken.Alg);
try
{
if (signatureProvider == null)
return new SignatureValidationResult(
ValidationFailureType.SignatureValidationFailed,
new ExceptionDetail(
new MessageDetail(TokenLogMessages.IDX10636,
key?.ToString() ?? "Null",
LogHelper.MarkAsNonPII(jsonWebToken.Alg)),
typeof(InvalidOperationException),
new StackFrame()));
bool valid = EncodingUtils.PerformEncodingDependentOperation<bool, string, int, SignatureProvider>(
jsonWebToken.EncodedToken,
0,
jsonWebToken.Dot2,
Encoding.UTF8,
jsonWebToken.EncodedToken,
jsonWebToken.Dot2,
signatureProvider,
ValidateSignature);
if (valid)
return SignatureValidationResult.Success();
else
return new SignatureValidationResult(
ValidationFailureType.SignatureValidationFailed,
new ExceptionDetail(
new MessageDetail(TokenLogMessages.IDX10504),
typeof(SecurityTokenInvalidSignatureException),
new StackFrame()));
}
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
{
return new SignatureValidationResult(
ValidationFailureType.SignatureValidationFailed,
new ExceptionDetail(
new MessageDetail(TokenLogMessages.IDX10504, ex.ToString()),
ex.GetType(),
new StackFrame(),
ex));
}
finally
{
cryptoProviderFactory.ReleaseSignatureProvider(signatureProvider);
}
}
private static ExceptionDetail GetSignatureValidationFailureExceptionDetails(
JsonWebToken jwtToken,
ValidationParameters validationParameters,
BaseConfiguration? configuration,
StringBuilder exceptionStrings,
StringBuilder keysAttempted,
bool kidExists,
bool kidMatched)
{
// Get information on where keys used during token validation came from for debugging purposes.
IList<SecurityKey> keysInTokenValidationParameters = validationParameters.IssuerSigningKeys;
ICollection<SecurityKey>? keysInConfiguration = configuration?.SigningKeys;
int numKeysInTokenValidationParameters = keysInTokenValidationParameters.Count;
int numKeysInConfiguration = keysInConfiguration?.Count ?? 0;
if (kidExists && kidMatched)
{
JsonWebToken localJwtToken = jwtToken; // avoid closure on non-exceptional path
bool isKidInTVP = keysInTokenValidationParameters.Any(x => x.KeyId.Equals(localJwtToken.Kid));
string keyLocation = isKidInTVP ? "TokenValidationParameters" : "Configuration";
return new ExceptionDetail(
new MessageDetail(
TokenLogMessages.IDX10511,
LogHelper.MarkAsNonPII(keysAttempted.ToString()),
LogHelper.MarkAsNonPII(numKeysInTokenValidationParameters),
LogHelper.MarkAsNonPII(numKeysInConfiguration),
LogHelper.MarkAsNonPII(keyLocation),
LogHelper.MarkAsNonPII(jwtToken.Kid),
exceptionStrings.ToString(),
LogHelper.MarkAsSecurityArtifact(jwtToken.EncodedToken, JwtTokenUtilities.SafeLogJwtToken)),
typeof(SecurityTokenSignatureKeyNotFoundException),
new StackFrame());
}
if (kidExists)
return new ExceptionDetail(
new MessageDetail(
TokenLogMessages.IDX10503, // No match for kid found among the keys provided.
LogHelper.MarkAsNonPII(jwtToken.Kid),
LogHelper.MarkAsNonPII(keysAttempted.ToString()),
LogHelper.MarkAsNonPII(numKeysInTokenValidationParameters),
LogHelper.MarkAsNonPII(numKeysInConfiguration),
exceptionStrings.ToString(),
LogHelper.MarkAsSecurityArtifact(jwtToken.EncodedToken, JwtTokenUtilities.SafeLogJwtToken)),
typeof(SecurityTokenSignatureKeyNotFoundException),
new StackFrame());
return new ExceptionDetail(
new MessageDetail(
TokenLogMessages.IDX10517, // Kid is missing and no keys match.
LogHelper.MarkAsNonPII(keysAttempted.ToString()),
LogHelper.MarkAsNonPII(numKeysInTokenValidationParameters),
LogHelper.MarkAsNonPII(numKeysInConfiguration),
exceptionStrings.ToString(),
LogHelper.MarkAsSecurityArtifact(jwtToken.EncodedToken, JwtTokenUtilities.SafeLogJwtToken)),
typeof(SecurityTokenSignatureKeyNotFoundException),
new StackFrame());
}
private static void PopulateFailedResults(
KeyMatchFailedResult? failedResult,
StringBuilder exceptionStrings,
StringBuilder keysAttempted)
{
if (failedResult is KeyMatchFailedResult result)
{
for (int i = 0; i < result.KeysAttempted.Count; i++)
{
exceptionStrings.AppendLine(result.FailedResults[i].ExceptionDetail?.MessageDetail.Message ?? "Null");
keysAttempted.AppendLine(result.KeysAttempted[i].ToString());
}
}
}
private struct KeyMatchFailedResult(
IList<SignatureValidationResult> failedResults,
IList<SecurityKey> keysAttempted)
{
public IList<SignatureValidationResult> FailedResults = failedResults;
public IList<SecurityKey> KeysAttempted = keysAttempted;
}
}
#nullable restore
}