| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Security.Cryptography.X509Certificates; |
| | | 6 | | |
| | | 7 | | namespace CoreWCF.IdentityModel.Tokens.Saml |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Represents the SecurityTokenReference property of X509Data as per: https://www.w3.org/TR/2001/PR-xmldsig-core-2 |
| | | 11 | | /// </summary> |
| | | 12 | | public class SecurityTokenReference |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Creates an IssuerSerial using the specified IssuerName and SerialNumber. |
| | | 16 | | /// </summary> |
| | 3 | 17 | | public SecurityTokenReference(SecurityKeyIdentifier securityKeyIdentifier) |
| | | 18 | | { |
| | 3 | 19 | | SecurityKeyIdentifier = securityKeyIdentifier; |
| | 3 | 20 | | } |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Gets the SecurityKeyIdentifier |
| | | 24 | | /// </summary> |
| | 10 | 25 | | public SecurityKeyIdentifier SecurityKeyIdentifier { get; } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Compares two SecurityTokenReference instances. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <param name="securityKey"></param> |
| | | 31 | | /// <returns></returns> |
| | | 32 | | public bool MatchesKey(Microsoft.IdentityModel.Tokens.SecurityKey securityKey) |
| | | 33 | | { |
| | 0 | 34 | | if (securityKey == null) |
| | 0 | 35 | | return false; |
| | | 36 | | |
| | 0 | 37 | | if (SecurityKeyIdentifier == null) |
| | 0 | 38 | | return false; |
| | | 39 | | |
| | 0 | 40 | | if (securityKey is Microsoft.IdentityModel.Tokens.X509SecurityKey x509SecurityKey) |
| | | 41 | | { |
| | 0 | 42 | | X509SubjectKeyIdentifierExtension skiExtension = x509SecurityKey.Certificate.Extensions["2.5.29.14"] as |
| | 0 | 43 | | if (skiExtension == null) |
| | 0 | 44 | | return false; |
| | | 45 | | |
| | | 46 | | // TODO: skiExtension.SecurityKeyIdentifier.Value is base64 encoded, skiExtension.RawData is not |
| | | 47 | | |
| | 0 | 48 | | return SecurityKeyIdentifier?.Value == Convert.ToBase64String(skiExtension.RawData, 2, skiExtension.RawD |
| | | 49 | | } |
| | | 50 | | |
| | 0 | 51 | | return false; |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Determines whether the specified object is equal to the current object. |
| | | 56 | | /// </summary> |
| | | 57 | | /// <param name="obj">The object to compare with the current object.</param> |
| | | 58 | | /// <returns>true if the specified object is equal to the current object; otherwise, false.</returns> |
| | | 59 | | public override bool Equals(object obj) |
| | | 60 | | { |
| | 0 | 61 | | if (obj == null) |
| | 0 | 62 | | return false; |
| | | 63 | | |
| | 0 | 64 | | if (ReferenceEquals(this, obj)) |
| | 0 | 65 | | return true; |
| | | 66 | | |
| | 0 | 67 | | if (obj is SecurityTokenReference other) |
| | 0 | 68 | | return SecurityKeyIdentifier?.Value == other.SecurityKeyIdentifier?.Value; |
| | | 69 | | |
| | 0 | 70 | | return false; |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Serves as the default hash function. |
| | | 75 | | /// </summary> |
| | | 76 | | /// <returns>A hash code for the current object.</returns> |
| | | 77 | | public override int GetHashCode() |
| | | 78 | | { |
| | 0 | 79 | | return SecurityKeyIdentifier?.GetHashCode() ?? 0; |
| | | 80 | | } |
| | | 81 | | } |
| | | 82 | | } |
| | | 83 | | |