| | | 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; |
| | | 6 | | using System.Security.Cryptography.X509Certificates; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF.IdentityModel.Tokens |
| | | 9 | | { |
| | | 10 | | public class X509IssuerSerialKeyIdentifierClause : SecurityKeyIdentifierClause |
| | | 11 | | { |
| | | 12 | | public X509IssuerSerialKeyIdentifierClause(string issuerName, string issuerSerialNumber) |
| | 0 | 13 | | : base(null) |
| | | 14 | | { |
| | 0 | 15 | | if (string.IsNullOrEmpty(issuerName)) |
| | | 16 | | { |
| | 0 | 17 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(issuerName)); |
| | | 18 | | } |
| | | 19 | | |
| | 0 | 20 | | if (string.IsNullOrEmpty(issuerSerialNumber)) |
| | | 21 | | { |
| | 0 | 22 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(issuerSerialNumber)); |
| | | 23 | | } |
| | | 24 | | |
| | 0 | 25 | | IssuerName = issuerName; |
| | 0 | 26 | | IssuerSerialNumber = issuerSerialNumber; |
| | 0 | 27 | | } |
| | | 28 | | |
| | | 29 | | public X509IssuerSerialKeyIdentifierClause(X509Certificate2 certificate) |
| | 0 | 30 | | : base(null) |
| | | 31 | | { |
| | 0 | 32 | | if (certificate == null) |
| | | 33 | | { |
| | 0 | 34 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(certificate)); |
| | | 35 | | } |
| | | 36 | | |
| | 0 | 37 | | IssuerName = certificate.Issuer; |
| | 0 | 38 | | IssuerSerialNumber = certificate.GetSerialNumberString(); |
| | 0 | 39 | | } |
| | | 40 | | |
| | 0 | 41 | | public string IssuerName { get; } |
| | | 42 | | |
| | 0 | 43 | | public string IssuerSerialNumber { get; } |
| | | 44 | | |
| | | 45 | | public override bool Matches(SecurityKeyIdentifierClause keyIdentifierClause) |
| | | 46 | | { |
| | 0 | 47 | | X509IssuerSerialKeyIdentifierClause that = keyIdentifierClause as X509IssuerSerialKeyIdentifierClause; |
| | 0 | 48 | | return ReferenceEquals(this, that) || (that != null && that.Matches(IssuerName, IssuerSerialNumber)); |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | public bool Matches(X509Certificate2 certificate) |
| | | 52 | | { |
| | 0 | 53 | | if (certificate == null) |
| | | 54 | | { |
| | 0 | 55 | | return false; |
| | | 56 | | } |
| | | 57 | | |
| | 0 | 58 | | return Matches(certificate.Issuer, certificate.GetSerialNumberString()); |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | public bool Matches(string issuerName, string issuerSerialNumber) |
| | | 62 | | { |
| | 0 | 63 | | if (issuerName == null) |
| | | 64 | | { |
| | 0 | 65 | | return false; |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | // If serial numbers dont match, we can avoid the potentially expensive issuer name comparison |
| | 0 | 69 | | if (IssuerSerialNumber != issuerSerialNumber) |
| | | 70 | | { |
| | 0 | 71 | | return false; |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | // Serial numbers match. Do a string comparison of issuer names |
| | 0 | 75 | | if (IssuerName == issuerName) |
| | | 76 | | { |
| | 0 | 77 | | return true; |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | // String equality comparison for issuer names failed |
| | | 81 | | // Do a byte-level comparison of the X500 distinguished names corresponding to the issuer names. |
| | | 82 | | // X500DistinguishedName constructor can throw for malformed inputs |
| | 0 | 83 | | bool x500IssuerNameMatch = false; |
| | | 84 | | try |
| | | 85 | | { |
| | 0 | 86 | | if (CoreWCF.Security.SecurityUtils.IsEqual(new X500DistinguishedName(IssuerName).RawData, |
| | 0 | 87 | | new X500DistinguishedName(issuerName).RawData)) |
| | | 88 | | { |
| | 0 | 89 | | x500IssuerNameMatch = true; |
| | | 90 | | } |
| | 0 | 91 | | } |
| | 0 | 92 | | catch (CryptographicException) |
| | | 93 | | { |
| | 0 | 94 | | } |
| | | 95 | | |
| | 0 | 96 | | return x500IssuerNameMatch; |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | public override string ToString() |
| | | 100 | | { |
| | 0 | 101 | | return string.Format(CultureInfo.InvariantCulture, "X509IssuerSerialKeyIdentifierClause(Issuer = '{0}', Seri |
| | 0 | 102 | | IssuerName, IssuerSerialNumber); |
| | | 103 | | } |
| | | 104 | | } |
| | | 105 | | } |