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