1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace System.ServiceModel.Security
{
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using System.ServiceModel;
public sealed class X509CertificateRecipientClientCredential
{
X509ServiceCertificateAuthentication authentication;
X509ServiceCertificateAuthentication sslCertificateAuthentication;
internal const StoreLocation DefaultStoreLocation = StoreLocation.CurrentUser;
internal const StoreName DefaultStoreName = StoreName.My;
internal const X509FindType DefaultFindType = X509FindType.FindBySubjectDistinguishedName;
X509Certificate2 defaultCertificate;
Dictionary<Uri, X509Certificate2> scopedCertificates;
bool isReadOnly;
internal X509CertificateRecipientClientCredential()
{
this.authentication = new X509ServiceCertificateAuthentication();
this.scopedCertificates = new Dictionary<Uri, X509Certificate2>();
}
internal X509CertificateRecipientClientCredential(X509CertificateRecipientClientCredential other)
{
this.authentication = new X509ServiceCertificateAuthentication(other.authentication);
if (other.sslCertificateAuthentication != null)
{
this.sslCertificateAuthentication = new X509ServiceCertificateAuthentication(other.sslCertificateAuthentication);
}
this.defaultCertificate = other.defaultCertificate;
this.scopedCertificates = new Dictionary<Uri, X509Certificate2>();
foreach (Uri uri in other.ScopedCertificates.Keys)
{
this.scopedCertificates.Add(uri, other.ScopedCertificates[uri]);
}
this.isReadOnly = other.isReadOnly;
}
public X509Certificate2 DefaultCertificate
{
get
{
return this.defaultCertificate;
}
set
{
ThrowIfImmutable();
this.defaultCertificate = value;
}
}
public Dictionary<Uri, X509Certificate2> ScopedCertificates
{
get
{
return this.scopedCertificates;
}
}
public X509ServiceCertificateAuthentication Authentication
{
get
{
return this.authentication;
}
}
public X509ServiceCertificateAuthentication SslCertificateAuthentication
{
get
{
return this.sslCertificateAuthentication;
}
set
{
ThrowIfImmutable();
this.sslCertificateAuthentication = value;
}
}
public void SetDefaultCertificate(string subjectName, StoreLocation storeLocation, StoreName storeName)
{
if (subjectName == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("subjectName");
}
SetDefaultCertificate(storeLocation, storeName, DefaultFindType, subjectName);
}
public void SetDefaultCertificate(StoreLocation storeLocation, StoreName storeName, X509FindType findType, object findValue)
{
if (findValue == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("findValue");
}
ThrowIfImmutable();
this.defaultCertificate = SecurityUtils.GetCertificateFromStore(storeName, storeLocation, findType, findValue, null);
}
public void SetScopedCertificate(string subjectName, StoreLocation storeLocation, StoreName storeName, Uri targetService)
{
if (subjectName == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("subjectName");
}
SetScopedCertificate(DefaultStoreLocation, DefaultStoreName, DefaultFindType, subjectName, targetService);
}
public void SetScopedCertificate(StoreLocation storeLocation, StoreName storeName, X509FindType findType, object findValue, Uri targetService)
{
if (findValue == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("findValue");
}
if (targetService == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("targetService");
}
ThrowIfImmutable();
X509Certificate2 certificate = SecurityUtils.GetCertificateFromStore(storeName, storeLocation, findType, findValue, null);
ScopedCertificates[targetService] = certificate;
}
internal void MakeReadOnly()
{
this.isReadOnly = true;
this.Authentication.MakeReadOnly();
if (this.sslCertificateAuthentication != null)
{
this.sslCertificateAuthentication.MakeReadOnly();
}
}
void ThrowIfImmutable()
{
if (this.isReadOnly)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
}
}
}
}
|