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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
|
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace System.ServiceModel.ComIntegration
{
using System;
using System.ServiceModel.Description;
using System.Reflection;
using System.Net;
using System.Security;
using System.Security.AccessControl;
using System.Security.Principal;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Security.Cryptography.X509Certificates;
using System.ServiceModel.Security;
using System.ServiceModel.Security.Tokens;
internal class ChannelCredentials : IChannelCredentials, IDisposable
{
protected IProvideChannelBuilderSettings channelBuilderSettings;
internal ChannelCredentials(IProvideChannelBuilderSettings channelBuilderSettings)
{
this.channelBuilderSettings = channelBuilderSettings;
}
internal static ComProxy Create(IntPtr outer, IProvideChannelBuilderSettings channelBuilderSettings)
{
if (channelBuilderSettings == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.CannotCreateChannelOption)));
ChannelCredentials ChannelCredentials = null;
ComProxy proxy = null;
try
{
ChannelCredentials = new ChannelCredentials(channelBuilderSettings);
proxy = ComProxy.Create(outer, ChannelCredentials, ChannelCredentials);
return proxy;
}
finally
{
if (proxy == null)
{
if (ChannelCredentials != null)
((IDisposable)ChannelCredentials).Dispose();
}
}
}
void IDisposable.Dispose()
{
}
void IChannelCredentials.SetWindowsCredential(string domain, string userName, string password, int impersonationLevel, bool allowNtlm)
{
lock (channelBuilderSettings)
{
KeyedByTypeCollection<IEndpointBehavior> behaviors = channelBuilderSettings.Behaviors;
NetworkCredential newCredentials = null;
if ((!String.IsNullOrEmpty(domain)) || (!String.IsNullOrEmpty(userName)) || (!String.IsNullOrEmpty(password)))
{
if (String.IsNullOrEmpty(userName))
{
userName = "";
}
System.ServiceModel.Security.SecurityUtils.PrepareNetworkCredential();
newCredentials = new NetworkCredential(userName, password, domain);
}
ClientCredentials channelCredentials = behaviors.Find<ClientCredentials>();
if (channelCredentials == null)
{
channelCredentials = new ClientCredentials();
behaviors.Add(channelCredentials);
}
channelCredentials.Windows.AllowedImpersonationLevel = (TokenImpersonationLevel)impersonationLevel;
// To disable AllowNtlm warning.
#pragma warning disable 618
channelCredentials.Windows.AllowNtlm = allowNtlm;
#pragma warning restore 618
channelCredentials.Windows.ClientCredential = newCredentials;
}
}
void IChannelCredentials.SetUserNameCredential(string userName, string password)
{
lock (channelBuilderSettings)
{
KeyedByTypeCollection<IEndpointBehavior> behaviors = channelBuilderSettings.Behaviors;
ClientCredentials channelCredentials = behaviors.Find<ClientCredentials>();
if (channelCredentials == null)
{
channelCredentials = new ClientCredentials();
behaviors.Add(channelCredentials);
}
channelCredentials.UserName.UserName = userName;
channelCredentials.UserName.Password = password;
}
}
void IChannelCredentials.SetServiceCertificateAuthentication(string storeLocation, string revocationMode, string certificationValidationMode)
{
lock (channelBuilderSettings)
{
StoreLocation location = (StoreLocation)Enum.Parse(typeof(StoreLocation), storeLocation);
X509RevocationMode mode = (X509RevocationMode)Enum.Parse(typeof(X509RevocationMode), revocationMode);
X509CertificateValidationMode validationMode = X509ServiceCertificateAuthentication.DefaultCertificateValidationMode;
if (!String.IsNullOrEmpty(certificationValidationMode))
validationMode = (X509CertificateValidationMode)Enum.Parse(typeof(X509CertificateValidationMode), certificationValidationMode);
KeyedByTypeCollection<IEndpointBehavior> behaviors = channelBuilderSettings.Behaviors;
ClientCredentials channelCredentials = behaviors.Find<ClientCredentials>();
if (channelCredentials == null)
{
channelCredentials = new ClientCredentials();
behaviors.Add(channelCredentials);
}
channelCredentials.ServiceCertificate.Authentication.TrustedStoreLocation = location;
channelCredentials.ServiceCertificate.Authentication.RevocationMode = mode;
channelCredentials.ServiceCertificate.Authentication.CertificateValidationMode = validationMode;
}
}
void IChannelCredentials.SetClientCertificateFromStore(string storeLocation, string storeName, string findType, object findValue)
{
lock (channelBuilderSettings)
{
StoreLocation location = (StoreLocation)Enum.Parse(typeof(StoreLocation), storeLocation);
StoreName name = (StoreName)Enum.Parse(typeof(StoreName), storeName);
X509FindType type = (X509FindType)Enum.Parse(typeof(X509FindType), findType);
KeyedByTypeCollection<IEndpointBehavior> behaviors = channelBuilderSettings.Behaviors;
ClientCredentials channelCredentials = behaviors.Find<ClientCredentials>();
if (channelCredentials == null)
{
channelCredentials = new ClientCredentials();
behaviors.Add(channelCredentials);
}
channelCredentials.ClientCertificate.SetCertificate(location, name, type, findValue);
}
}
void IChannelCredentials.SetClientCertificateFromStoreByName(string subjectName, string storeLocation, string storeName)
{
((IChannelCredentials)this).SetClientCertificateFromStore(storeLocation, storeName, X509CertificateInitiatorClientCredential.DefaultFindType.ToString("G"), subjectName);
}
void IChannelCredentials.SetClientCertificateFromFile(string fileName, string password, string keyStorageFlags)
{
lock (channelBuilderSettings)
{
KeyedByTypeCollection<IEndpointBehavior> behaviors = channelBuilderSettings.Behaviors;
X509Certificate2 cert;
if (!String.IsNullOrEmpty(keyStorageFlags))
{
X509KeyStorageFlags flags = (X509KeyStorageFlags)Enum.Parse(typeof(X509KeyStorageFlags), keyStorageFlags);
cert = new X509Certificate2(fileName, password, flags);
}
else
{
cert = new X509Certificate2(fileName, password);
}
ClientCredentials channelCredentials = behaviors.Find<ClientCredentials>();
if (channelCredentials == null)
{
channelCredentials = new ClientCredentials();
behaviors.Add(channelCredentials);
}
channelCredentials.ClientCertificate.Certificate = cert;
}
}
void IChannelCredentials.SetDefaultServiceCertificateFromStore(string storeLocation, string storeName, string findType, object findValue)
{
lock (channelBuilderSettings)
{
StoreLocation location = (StoreLocation)Enum.Parse(typeof(StoreLocation), storeLocation);
StoreName name = (StoreName)Enum.Parse(typeof(StoreName), storeName);
X509FindType type = (X509FindType)Enum.Parse(typeof(X509FindType), findType);
KeyedByTypeCollection<IEndpointBehavior> behaviors = channelBuilderSettings.Behaviors;
ClientCredentials channelCredentials = behaviors.Find<ClientCredentials>();
if (channelCredentials == null)
{
channelCredentials = new ClientCredentials();
behaviors.Add(channelCredentials);
}
channelCredentials.ServiceCertificate.SetDefaultCertificate(location, name, type, findValue);
}
}
void IChannelCredentials.SetDefaultServiceCertificateFromStoreByName(string subjectName, string storeLocation, string storeName)
{
((IChannelCredentials)this).SetDefaultServiceCertificateFromStore(storeLocation, storeName, X509CertificateInitiatorClientCredential.DefaultFindType.ToString("G"), subjectName);
}
void IChannelCredentials.SetDefaultServiceCertificateFromFile(string fileName, string password, string keyStorageFlags)
{
lock (channelBuilderSettings)
{
KeyedByTypeCollection<IEndpointBehavior> behaviors = channelBuilderSettings.Behaviors;
X509Certificate2 cert;
if (!String.IsNullOrEmpty(keyStorageFlags))
{
X509KeyStorageFlags flags = (X509KeyStorageFlags)Enum.Parse(typeof(X509KeyStorageFlags), keyStorageFlags);
cert = new X509Certificate2(fileName, password, flags);
}
else
{
cert = new X509Certificate2(fileName, password);
}
ClientCredentials channelCredentials = behaviors.Find<ClientCredentials>();
if (channelCredentials == null)
{
channelCredentials = new ClientCredentials();
behaviors.Add(channelCredentials);
}
channelCredentials.ServiceCertificate.DefaultCertificate = cert;
}
}
void IChannelCredentials.SetIssuedToken(string localIssuerAddres, string localIssuerBindingType, string localIssuerBinding)
{
lock (channelBuilderSettings)
{
Binding binding = null;
binding = ConfigLoader.LookupBinding(localIssuerBindingType, localIssuerBinding);
KeyedByTypeCollection<IEndpointBehavior> behaviors = channelBuilderSettings.Behaviors;
ClientCredentials channelCredentials = behaviors.Find<ClientCredentials>();
if (channelCredentials == null)
{
channelCredentials = new ClientCredentials();
behaviors.Add(channelCredentials);
}
channelCredentials.IssuedToken.LocalIssuerAddress = new EndpointAddress(localIssuerAddres);
channelCredentials.IssuedToken.LocalIssuerBinding = binding;
}
}
}
}
|