| | | 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 | | using CoreWCF.IdentityModel.Tokens; |
| | | 7 | | using CoreWCF.Security; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.IdentityModel.Selectors |
| | | 10 | | { |
| | | 11 | | internal class X509SecurityTokenProvider : SecurityTokenProvider, IDisposable |
| | | 12 | | { |
| | 5 | 13 | | public X509SecurityTokenProvider(X509Certificate2 certificate) |
| | | 14 | | { |
| | 5 | 15 | | if (certificate == null) |
| | | 16 | | { |
| | 0 | 17 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(certificate)); |
| | | 18 | | } |
| | | 19 | | |
| | 5 | 20 | | Certificate = new X509Certificate2(certificate); |
| | 5 | 21 | | } |
| | | 22 | | |
| | 0 | 23 | | public X509SecurityTokenProvider(StoreLocation storeLocation, StoreName storeName, X509FindType findType, object |
| | | 24 | | { |
| | 0 | 25 | | if (findValue == null) |
| | | 26 | | { |
| | 0 | 27 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(findValue)); |
| | | 28 | | } |
| | | 29 | | |
| | 0 | 30 | | X509Store store = new X509Store(storeName, storeLocation); |
| | 0 | 31 | | X509Certificate2Collection certificates = null; |
| | | 32 | | try |
| | | 33 | | { |
| | 0 | 34 | | store.Open(OpenFlags.ReadOnly); |
| | 0 | 35 | | certificates = store.Certificates.Find(findType, findValue, false); |
| | 0 | 36 | | if (certificates.Count < 1) |
| | | 37 | | { |
| | 0 | 38 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.Format(SR.Ca |
| | | 39 | | } |
| | 0 | 40 | | if (certificates.Count > 1) |
| | | 41 | | { |
| | 0 | 42 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.Format(SR.Fo |
| | | 43 | | } |
| | | 44 | | |
| | 0 | 45 | | Certificate = new X509Certificate2(certificates[0]); |
| | 0 | 46 | | } |
| | | 47 | | finally |
| | | 48 | | { |
| | 0 | 49 | | SecurityUtils.ResetAllCertificates(certificates); |
| | 0 | 50 | | store.Close(); |
| | 0 | 51 | | } |
| | 0 | 52 | | } |
| | | 53 | | |
| | 10 | 54 | | public X509Certificate2 Certificate { get; } |
| | | 55 | | |
| | | 56 | | protected override SecurityToken GetTokenCore(TimeSpan timeout) |
| | | 57 | | { |
| | 0 | 58 | | return new X509SecurityToken(Certificate); |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | public void Dispose() |
| | | 62 | | { |
| | 5 | 63 | | SecurityUtils.ResetCertificate(Certificate); |
| | 5 | 64 | | } |
| | | 65 | | } |
| | | 66 | | } |