| | | 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.Globalization; |
| | | 5 | | using System.Security.Cryptography.X509Certificates; |
| | | 6 | | |
| | | 7 | | namespace CoreWCF.IdentityModel.Tokens |
| | | 8 | | { |
| | | 9 | | public class X509ThumbprintKeyIdentifierClause : BinaryKeyIdentifierClause |
| | | 10 | | { |
| | | 11 | | public X509ThumbprintKeyIdentifierClause(X509Certificate2 certificate) |
| | 0 | 12 | | : this(GetHash(certificate), false) |
| | | 13 | | { |
| | 0 | 14 | | } |
| | | 15 | | |
| | | 16 | | public X509ThumbprintKeyIdentifierClause(byte[] thumbprint) |
| | 0 | 17 | | : this(thumbprint, true) |
| | | 18 | | { |
| | 0 | 19 | | } |
| | | 20 | | |
| | | 21 | | internal X509ThumbprintKeyIdentifierClause(byte[] thumbprint, bool cloneBuffer) |
| | 0 | 22 | | : base(null, thumbprint, cloneBuffer) |
| | | 23 | | { |
| | 0 | 24 | | } |
| | | 25 | | |
| | | 26 | | private static byte[] GetHash(X509Certificate2 certificate) |
| | | 27 | | { |
| | 0 | 28 | | if (certificate == null) |
| | | 29 | | { |
| | 0 | 30 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(certificate)); |
| | | 31 | | } |
| | | 32 | | //TODO switch to stronger hash algorithm once we're no longer on netstandard2.0 |
| | 0 | 33 | | return certificate.GetCertHash(); |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | public byte[] GetX509Thumbprint() |
| | | 37 | | { |
| | 0 | 38 | | return GetBuffer(); |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | public bool Matches(X509Certificate2 certificate) |
| | | 42 | | { |
| | 0 | 43 | | if (certificate == null) |
| | | 44 | | { |
| | 0 | 45 | | return false; |
| | | 46 | | } |
| | | 47 | | |
| | 0 | 48 | | return Matches(GetHash(certificate)); |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | public override string ToString() |
| | | 52 | | { |
| | 0 | 53 | | return string.Format(CultureInfo.InvariantCulture, "X509ThumbprintKeyIdentifierClause(Hash = 0x{0})", ToHexS |
| | | 54 | | } |
| | | 55 | | } |
| | | 56 | | } |