| | | 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.Security.Cryptography.X509Certificates; |
| | | 5 | | using CoreWCF.Security; |
| | | 6 | | |
| | | 7 | | namespace CoreWCF.IdentityModel.Tokens |
| | | 8 | | { |
| | | 9 | | public class X509SubjectKeyIdentifierClause : BinaryKeyIdentifierClause |
| | | 10 | | { |
| | | 11 | | private const string SubjectKeyIdentifierOid = "2.5.29.14"; |
| | | 12 | | private const int SkiDataOffset = 2; |
| | | 13 | | |
| | | 14 | | public X509SubjectKeyIdentifierClause(byte[] ski) |
| | 2 | 15 | | : this(ski, true) |
| | | 16 | | { |
| | 2 | 17 | | } |
| | | 18 | | |
| | | 19 | | internal X509SubjectKeyIdentifierClause(byte[] ski, bool cloneBuffer) |
| | 2 | 20 | | : base(null, ski, cloneBuffer) |
| | | 21 | | { |
| | 2 | 22 | | } |
| | | 23 | | |
| | | 24 | | private static byte[] GetSkiRawData(X509Certificate2 certificate) |
| | | 25 | | { |
| | 3 | 26 | | if (certificate == null) |
| | | 27 | | { |
| | 0 | 28 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(certificate)); |
| | | 29 | | } |
| | | 30 | | |
| | 3 | 31 | | if (certificate.Extensions[SubjectKeyIdentifierOid] is X509SubjectKeyIdentifierExtension skiExtension) |
| | | 32 | | { |
| | 3 | 33 | | return skiExtension.RawData; |
| | | 34 | | } |
| | | 35 | | else |
| | | 36 | | { |
| | 0 | 37 | | return null; |
| | | 38 | | } |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | public byte[] GetX509SubjectKeyIdentifier() |
| | | 42 | | { |
| | 0 | 43 | | return GetBuffer(); |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | public bool Matches(X509Certificate2 certificate) |
| | | 47 | | { |
| | 3 | 48 | | if (certificate == null) |
| | | 49 | | { |
| | 0 | 50 | | return false; |
| | | 51 | | } |
| | | 52 | | |
| | 3 | 53 | | byte[] data = GetSkiRawData(certificate); |
| | 3 | 54 | | return data != null && Matches(data, SkiDataOffset); |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | public static bool TryCreateFrom(X509Certificate2 certificate, out X509SubjectKeyIdentifierClause keyIdentifierC |
| | | 58 | | { |
| | 0 | 59 | | byte[] data = GetSkiRawData(certificate); |
| | 0 | 60 | | keyIdentifierClause = null; |
| | 0 | 61 | | if (data != null) |
| | | 62 | | { |
| | 0 | 63 | | byte[] ski = SecurityUtils.CloneBuffer(data, SkiDataOffset, data.Length - SkiDataOffset); |
| | 0 | 64 | | keyIdentifierClause = new X509SubjectKeyIdentifierClause(ski, false); |
| | | 65 | | } |
| | 0 | 66 | | return keyIdentifierClause != null; |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | public static bool CanCreateFrom(X509Certificate2 certificate) |
| | | 70 | | { |
| | 0 | 71 | | return null != GetSkiRawData(certificate); |
| | | 72 | | } |
| | | 73 | | } |
| | | 74 | | } |