| | | 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 | | |
| | | 7 | | namespace CoreWCF.Security |
| | | 8 | | { |
| | | 9 | | public sealed class X509CertificateInitiatorServiceCredential |
| | | 10 | | { |
| | | 11 | | internal const StoreLocation DefaultStoreLocation = StoreLocation.LocalMachine; |
| | | 12 | | internal const StoreName DefaultStoreName = StoreName.My; |
| | | 13 | | internal const X509FindType DefaultFindType = X509FindType.FindBySubjectDistinguishedName; |
| | | 14 | | private X509Certificate2 _certificate; |
| | | 15 | | private bool _isReadOnly; |
| | | 16 | | |
| | 52 | 17 | | internal X509CertificateInitiatorServiceCredential() |
| | | 18 | | { |
| | 52 | 19 | | Authentication = new X509ClientCertificateAuthentication(); |
| | 52 | 20 | | } |
| | | 21 | | |
| | 66 | 22 | | internal X509CertificateInitiatorServiceCredential(X509CertificateInitiatorServiceCredential other) |
| | | 23 | | { |
| | 66 | 24 | | _certificate = other._certificate; |
| | 66 | 25 | | Authentication = new X509ClientCertificateAuthentication(other.Authentication); |
| | 66 | 26 | | _isReadOnly = other._isReadOnly; |
| | 66 | 27 | | } |
| | | 28 | | |
| | | 29 | | public X509Certificate2 Certificate |
| | | 30 | | { |
| | | 31 | | get |
| | | 32 | | { |
| | 0 | 33 | | return _certificate; |
| | | 34 | | } |
| | | 35 | | set |
| | | 36 | | { |
| | 0 | 37 | | ThrowIfImmutable(); |
| | 0 | 38 | | _certificate = value; |
| | 0 | 39 | | } |
| | | 40 | | } |
| | | 41 | | |
| | 93 | 42 | | public X509ClientCertificateAuthentication Authentication { get; } |
| | | 43 | | |
| | | 44 | | public void SetCertificate(string subjectName, StoreLocation storeLocation, StoreName storeName) |
| | | 45 | | { |
| | 0 | 46 | | if (subjectName == null) |
| | | 47 | | { |
| | 0 | 48 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(subjectName)); |
| | | 49 | | } |
| | 0 | 50 | | SetCertificate(storeLocation, storeName, DefaultFindType, subjectName); |
| | 0 | 51 | | } |
| | | 52 | | |
| | | 53 | | public void SetCertificate(StoreLocation storeLocation, StoreName storeName, X509FindType findType, object findV |
| | | 54 | | { |
| | 0 | 55 | | if (findValue == null) |
| | | 56 | | { |
| | 0 | 57 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(findValue)); |
| | | 58 | | } |
| | 0 | 59 | | ThrowIfImmutable(); |
| | 0 | 60 | | _certificate = SecurityUtils.GetCertificateFromStore(storeName, storeLocation, findType, findValue, null); |
| | 0 | 61 | | } |
| | | 62 | | |
| | | 63 | | internal void MakeReadOnly() |
| | | 64 | | { |
| | 0 | 65 | | _isReadOnly = true; |
| | 0 | 66 | | Authentication.MakeReadOnly(); |
| | 0 | 67 | | } |
| | | 68 | | |
| | | 69 | | private void ThrowIfImmutable() |
| | | 70 | | { |
| | 0 | 71 | | if (_isReadOnly) |
| | | 72 | | { |
| | 0 | 73 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.ObjectIsReadO |
| | | 74 | | } |
| | 0 | 75 | | } |
| | | 76 | | } |
| | | 77 | | } |