|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Security.Cryptography.X509Certificates; |
| 6 | +using Microsoft.IdentityModel.Abstractions; |
| 7 | +using Microsoft.IdentityModel.Logging; |
| 8 | + |
| 9 | +namespace Microsoft.IdentityModel.Tokens |
| 10 | +{ |
| 11 | + public static partial class Validators |
| 12 | + { |
| 13 | + /// <summary> |
| 14 | + /// Validates the <see cref="SecurityKey"/> that signed a <see cref="SecurityToken"/>. |
| 15 | + /// </summary> |
| 16 | + /// <param name="securityKey">The <see cref="SecurityKey"/> that signed the <see cref="SecurityToken"/>.</param> |
| 17 | + /// <param name="securityToken">The <see cref="SecurityToken"/> being validated.</param> |
| 18 | + /// <param name="validationParameters"><see cref="TokenValidationParameters"/> required for validation.</param> |
| 19 | + /// <exception cref="ArgumentNullException"> if 'securityKey' is null and ValidateIssuerSigningKey is true.</exception> |
| 20 | + /// <exception cref="ArgumentNullException"> if 'securityToken' is null and ValidateIssuerSigningKey is true.</exception> |
| 21 | + /// <exception cref="ArgumentNullException"> if 'validationParameters' is null.</exception> |
| 22 | + public static void ValidateIssuerSecurityKey(SecurityKey securityKey, SecurityToken securityToken, TokenValidationParameters validationParameters) |
| 23 | + { |
| 24 | + ValidateIssuerSecurityKey(securityKey, securityToken, validationParameters, null); |
| 25 | + } |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Validates the <see cref="SecurityKey"/> that signed a <see cref="SecurityToken"/>. |
| 29 | + /// </summary> |
| 30 | + /// <param name="securityKey">The <see cref="SecurityKey"/> that signed the <see cref="SecurityToken"/>.</param> |
| 31 | + /// <param name="securityToken">The <see cref="SecurityToken"/> being validated.</param> |
| 32 | + /// <param name="validationParameters"><see cref="TokenValidationParameters"/> required for validation.</param> |
| 33 | + /// <param name="configuration">The <see cref="BaseConfiguration"/> required for issuer and signing key validation.</param> |
| 34 | + /// <exception cref="ArgumentNullException"> if 'securityKey' is null and ValidateIssuerSigningKey is true.</exception> |
| 35 | + /// <exception cref="ArgumentNullException"> if 'securityToken' is null and ValidateIssuerSigningKey is true.</exception> |
| 36 | + /// <exception cref="ArgumentNullException"> if 'validationParameters' is null.</exception> |
| 37 | + internal static void ValidateIssuerSecurityKey(SecurityKey securityKey, SecurityToken securityToken, TokenValidationParameters validationParameters, BaseConfiguration configuration) |
| 38 | + { |
| 39 | + if (validationParameters == null) |
| 40 | + throw LogHelper.LogArgumentNullException(nameof(validationParameters)); |
| 41 | + |
| 42 | + if (validationParameters.IssuerSigningKeyValidatorUsingConfiguration != null) |
| 43 | + { |
| 44 | + if (!validationParameters.IssuerSigningKeyValidatorUsingConfiguration(securityKey, securityToken, validationParameters, configuration)) |
| 45 | + throw LogHelper.LogExceptionMessage(new SecurityTokenInvalidSigningKeyException(LogHelper.FormatInvariant(LogMessages.IDX10232, securityKey)) { SigningKey = securityKey }); |
| 46 | + |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + if (validationParameters.IssuerSigningKeyValidator != null) |
| 51 | + { |
| 52 | + if (!validationParameters.IssuerSigningKeyValidator(securityKey, securityToken, validationParameters)) |
| 53 | + throw LogHelper.LogExceptionMessage(new SecurityTokenInvalidSigningKeyException(LogHelper.FormatInvariant(LogMessages.IDX10232, securityKey)) { SigningKey = securityKey }); |
| 54 | + |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + if (!validationParameters.ValidateIssuerSigningKey) |
| 59 | + { |
| 60 | + LogHelper.LogVerbose(LogMessages.IDX10237); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + if (!validationParameters.RequireSignedTokens && securityKey == null) |
| 65 | + { |
| 66 | + LogHelper.LogInformation(LogMessages.IDX10252); |
| 67 | + return; |
| 68 | + } |
| 69 | + else if (securityKey == null) |
| 70 | + { |
| 71 | + throw LogHelper.LogExceptionMessage(new ArgumentNullException(nameof(securityKey), LogMessages.IDX10253)); |
| 72 | + } |
| 73 | + |
| 74 | + if (securityToken == null) |
| 75 | + throw LogHelper.LogArgumentNullException(nameof(securityToken)); |
| 76 | + |
| 77 | + ValidateIssuerSigningKeyLifeTime(securityKey, validationParameters); |
| 78 | + } |
| 79 | + |
| 80 | + /// <summary> |
| 81 | + /// Given a signing key, when it's derived from a certificate, validates that the certificate is already active and non-expired |
| 82 | + /// </summary> |
| 83 | + /// <param name="securityKey">The <see cref="SecurityKey"/> that signed the <see cref="SecurityToken"/>.</param> |
| 84 | + /// <param name="validationParameters">The <see cref="TokenValidationParameters"/> that are used to validate the token.</param> |
| 85 | + internal static void ValidateIssuerSigningKeyLifeTime(SecurityKey securityKey, TokenValidationParameters validationParameters) |
| 86 | + { |
| 87 | + X509SecurityKey x509SecurityKey = securityKey as X509SecurityKey; |
| 88 | + if (x509SecurityKey?.Certificate is X509Certificate2 cert) |
| 89 | + { |
| 90 | + DateTime utcNow = DateTime.UtcNow; |
| 91 | + var notBeforeUtc = cert.NotBefore.ToUniversalTime(); |
| 92 | + var notAfterUtc = cert.NotAfter.ToUniversalTime(); |
| 93 | + |
| 94 | + if (notBeforeUtc > DateTimeUtil.Add(utcNow, validationParameters.ClockSkew)) |
| 95 | + throw LogHelper.LogExceptionMessage(new SecurityTokenInvalidSigningKeyException(LogHelper.FormatInvariant(LogMessages.IDX10248, LogHelper.MarkAsNonPII(notBeforeUtc), LogHelper.MarkAsNonPII(utcNow)))); |
| 96 | + |
| 97 | + if (LogHelper.IsEnabled(EventLogLevel.Informational)) |
| 98 | + LogHelper.LogInformation(LogMessages.IDX10250, LogHelper.MarkAsNonPII(notBeforeUtc), LogHelper.MarkAsNonPII(utcNow)); |
| 99 | + |
| 100 | + if (notAfterUtc < DateTimeUtil.Add(utcNow, validationParameters.ClockSkew.Negate())) |
| 101 | + throw LogHelper.LogExceptionMessage(new SecurityTokenInvalidSigningKeyException(LogHelper.FormatInvariant(LogMessages.IDX10249, LogHelper.MarkAsNonPII(notAfterUtc), LogHelper.MarkAsNonPII(utcNow)))); |
| 102 | + |
| 103 | + if (LogHelper.IsEnabled(EventLogLevel.Informational)) |
| 104 | + LogHelper.LogInformation(LogMessages.IDX10251, LogHelper.MarkAsNonPII(notAfterUtc), LogHelper.MarkAsNonPII(utcNow)); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments