| | | 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 X509RawDataKeyIdentifierClause : BinaryKeyIdentifierClause |
| | | 10 | | { |
| | | 11 | | private X509Certificate2 _certificate; |
| | | 12 | | private X509AsymmetricSecurityKey _key; |
| | | 13 | | |
| | | 14 | | public X509RawDataKeyIdentifierClause(X509Certificate2 certificate) |
| | 0 | 15 | | : this(GetRawData(certificate), false) |
| | | 16 | | { |
| | 0 | 17 | | _certificate = certificate; |
| | 0 | 18 | | } |
| | | 19 | | |
| | | 20 | | public X509RawDataKeyIdentifierClause(byte[] certificateRawData) |
| | 0 | 21 | | : this(certificateRawData, true) |
| | | 22 | | { |
| | 0 | 23 | | } |
| | | 24 | | |
| | | 25 | | internal X509RawDataKeyIdentifierClause(byte[] certificateRawData, bool cloneBuffer) |
| | 50 | 26 | | : base(null, certificateRawData, cloneBuffer) |
| | | 27 | | { |
| | 50 | 28 | | } |
| | | 29 | | |
| | | 30 | | public override bool CanCreateKey |
| | | 31 | | { |
| | 0 | 32 | | get { return true; } |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | public override SecurityKey CreateKey() |
| | | 36 | | { |
| | 0 | 37 | | if (_key == null) |
| | | 38 | | { |
| | 0 | 39 | | if (_certificate == null) |
| | | 40 | | { |
| | 0 | 41 | | _certificate = new X509Certificate2(GetBuffer()); |
| | | 42 | | } |
| | | 43 | | |
| | 0 | 44 | | _key = new X509AsymmetricSecurityKey(_certificate); |
| | | 45 | | } |
| | 0 | 46 | | return _key; |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | private static byte[] GetRawData(X509Certificate certificate) |
| | | 50 | | { |
| | 50 | 51 | | if (certificate == null) |
| | | 52 | | { |
| | 0 | 53 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(certificate)); |
| | | 54 | | } |
| | | 55 | | |
| | 50 | 56 | | return certificate.GetRawCertData(); |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | public byte[] GetX509RawData() |
| | | 60 | | { |
| | 0 | 61 | | return GetBuffer(); |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | public bool Matches(X509Certificate2 certificate) |
| | | 65 | | { |
| | 50 | 66 | | if (certificate == null) |
| | | 67 | | { |
| | 0 | 68 | | return false; |
| | | 69 | | } |
| | | 70 | | |
| | 50 | 71 | | return Matches(GetRawData(certificate)); |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | public override string ToString() |
| | | 75 | | { |
| | 0 | 76 | | return string.Format(CultureInfo.InvariantCulture, "X509RawDataKeyIdentifierClause(RawData = {0})", ToBase64 |
| | | 77 | | } |
| | | 78 | | } |
| | | 79 | | } |