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 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
//----------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace System.ServiceModel.Security
{
using System.Collections.Generic;
using System.ServiceModel;
using System.IO;
using System.IdentityModel.Claims;
using System.IdentityModel.Policy;
using System.IdentityModel.Selectors;
using System.Security.Cryptography;
using System.ServiceModel.Channels;
using System.ServiceModel.Security.Tokens;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Net.Security;
abstract class MessageSecurityProtocolFactory : SecurityProtocolFactory
{
internal const MessageProtectionOrder defaultMessageProtectionOrder = MessageProtectionOrder.SignBeforeEncrypt;
internal const bool defaultDoRequestSignatureConfirmation = false;
bool applyIntegrity = true;
bool applyConfidentiality = true;
bool doRequestSignatureConfirmation = defaultDoRequestSignatureConfirmation;
IdentityVerifier identityVerifier;
ChannelProtectionRequirements protectionRequirements = new ChannelProtectionRequirements();
MessageProtectionOrder messageProtectionOrder = defaultMessageProtectionOrder;
bool requireIntegrity = true;
bool requireConfidentiality = true;
List<SecurityTokenAuthenticator> wrappedKeyTokenAuthenticator;
protected MessageSecurityProtocolFactory()
{
}
internal MessageSecurityProtocolFactory(MessageSecurityProtocolFactory factory)
: base(factory)
{
if (factory == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("factory");
this.applyIntegrity = factory.applyIntegrity;
this.applyConfidentiality = factory.applyConfidentiality;
this.identityVerifier = factory.identityVerifier;
this.protectionRequirements = new ChannelProtectionRequirements(factory.protectionRequirements);
this.messageProtectionOrder = factory.messageProtectionOrder;
this.requireIntegrity = factory.requireIntegrity;
this.requireConfidentiality = factory.requireConfidentiality;
this.doRequestSignatureConfirmation = factory.doRequestSignatureConfirmation;
}
public bool ApplyConfidentiality
{
get
{
return this.applyConfidentiality;
}
set
{
ThrowIfImmutable();
this.applyConfidentiality = value;
}
}
public bool ApplyIntegrity
{
get
{
return this.applyIntegrity;
}
set
{
ThrowIfImmutable();
this.applyIntegrity = value;
}
}
public bool DoRequestSignatureConfirmation
{
get
{
return this.doRequestSignatureConfirmation;
}
set
{
ThrowIfImmutable();
this.doRequestSignatureConfirmation = value;
}
}
public IdentityVerifier IdentityVerifier
{
get
{
return this.identityVerifier;
}
set
{
ThrowIfImmutable();
this.identityVerifier = value;
}
}
public ChannelProtectionRequirements ProtectionRequirements
{
get
{
return this.protectionRequirements;
}
}
public MessageProtectionOrder MessageProtectionOrder
{
get
{
return this.messageProtectionOrder;
}
set
{
ThrowIfImmutable();
this.messageProtectionOrder = value;
}
}
public bool RequireIntegrity
{
get
{
return this.requireIntegrity;
}
set
{
ThrowIfImmutable();
this.requireIntegrity = value;
}
}
public bool RequireConfidentiality
{
get
{
return this.requireConfidentiality;
}
set
{
ThrowIfImmutable();
this.requireConfidentiality = value;
}
}
internal List<SecurityTokenAuthenticator> WrappedKeySecurityTokenAuthenticator
{
get
{
return this.wrappedKeyTokenAuthenticator;
}
}
protected virtual void ValidateCorrelationSecuritySettings()
{
if (this.ActAsInitiator && this.SupportsRequestReply)
{
bool savesCorrelationTokenOnRequest = this.ApplyIntegrity || this.ApplyConfidentiality;
bool needsCorrelationTokenOnReply = this.RequireIntegrity || this.RequireConfidentiality;
if (!savesCorrelationTokenOnRequest && needsCorrelationTokenOnReply)
{
OnPropertySettingsError("ApplyIntegrity", false);
}
}
}
public override void OnOpen(TimeSpan timeout)
{
base.OnOpen(timeout);
this.protectionRequirements.MakeReadOnly();
if (this.DetectReplays && !this.RequireIntegrity)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("RequireIntegrity", SR.GetString(SR.ForReplayDetectionToBeDoneRequireIntegrityMustBeSet));
}
if (this.DoRequestSignatureConfirmation)
{
if (!this.SupportsRequestReply)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.SignatureConfirmationRequiresRequestReply));
}
if (!this.StandardsManager.SecurityVersion.SupportsSignatureConfirmation)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.SecurityVersionDoesNotSupportSignatureConfirmation, this.StandardsManager.SecurityVersion));
}
}
this.wrappedKeyTokenAuthenticator = new List<SecurityTokenAuthenticator>(1);
SecurityTokenAuthenticator authenticator = new NonValidatingSecurityTokenAuthenticator<WrappedKeySecurityToken>();
this.wrappedKeyTokenAuthenticator.Add(authenticator);
ValidateCorrelationSecuritySettings();
}
static MessagePartSpecification ExtractMessageParts(string action,
ScopedMessagePartSpecification scopedParts, bool isForSignature)
{
MessagePartSpecification parts = null;
if (scopedParts.TryGetParts(action, out parts))
{
return parts;
}
else if (scopedParts.TryGetParts(MessageHeaders.WildcardAction, out parts))
{
return parts;
}
// send back a fault indication that the action is unknown
SecurityVersion wss = MessageSecurityVersion.Default.SecurityVersion;
FaultCode subCode = new FaultCode(wss.InvalidSecurityFaultCode.Value, wss.HeaderNamespace.Value);
FaultCode senderCode = FaultCode.CreateSenderFaultCode(subCode);
FaultReason reason = new FaultReason(SR.GetString(SR.InvalidOrUnrecognizedAction, action), System.Globalization.CultureInfo.CurrentCulture);
MessageFault fault = MessageFault.CreateFault(senderCode, reason);
if (isForSignature)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(SR.GetString(SR.NoSignaturePartsSpecified, action), null, fault));
}
else
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(SR.GetString(SR.NoEncryptionPartsSpecified, action), null, fault));
}
}
internal MessagePartSpecification GetIncomingEncryptionParts(string action)
{
if (this.RequireConfidentiality)
{
//return ExtractMessageParts(action, (this.SecurityTokenManager is ClientCredentialsSecurityTokenManager) ? this.ProtectionRequirements.OutgoingEncryptionParts : this.ProtectionRequirements.IncomingEncryptionParts, false);
if (this.IsDuplexReply)
return ExtractMessageParts(action, this.ProtectionRequirements.OutgoingEncryptionParts, false);
else
return ExtractMessageParts(action, (this.ActAsInitiator) ? this.ProtectionRequirements.OutgoingEncryptionParts : this.ProtectionRequirements.IncomingEncryptionParts, false);
}
else
{
return MessagePartSpecification.NoParts;
}
}
internal MessagePartSpecification GetIncomingSignatureParts(string action)
{
if (this.RequireIntegrity)
{
//return ExtractMessageParts(action, (this.SecurityTokenManager is ClientCredentialsSecurityTokenManager) ? this.ProtectionRequirements.OutgoingSignatureParts : this.ProtectionRequirements.IncomingSignatureParts, true);
if (this.IsDuplexReply)
return ExtractMessageParts(action, this.ProtectionRequirements.OutgoingSignatureParts, true);
else
return ExtractMessageParts(action, (this.ActAsInitiator) ? this.ProtectionRequirements.OutgoingSignatureParts : this.ProtectionRequirements.IncomingSignatureParts, true);
}
else
{
return MessagePartSpecification.NoParts;
}
}
internal MessagePartSpecification GetOutgoingEncryptionParts(string action)
{
if (this.ApplyConfidentiality)
{
//return ExtractMessageParts(action, (this.SecurityTokenManager is ClientCredentialsSecurityTokenManager) ? this.ProtectionRequirements.IncomingEncryptionParts : this.ProtectionRequirements.OutgoingEncryptionParts, false);
if (this.IsDuplexReply)
return ExtractMessageParts(action, this.ProtectionRequirements.OutgoingEncryptionParts, false);
else
return ExtractMessageParts(action, (this.ActAsInitiator) ? this.ProtectionRequirements.IncomingEncryptionParts : this.ProtectionRequirements.OutgoingEncryptionParts, false);
}
else
{
return MessagePartSpecification.NoParts;
}
}
internal MessagePartSpecification GetOutgoingSignatureParts(string action)
{
if (this.ApplyIntegrity)
{
//return ExtractMessageParts(action, (this.SecurityTokenManager is ClientCredentialsSecurityTokenManager) ? this.ProtectionRequirements.IncomingSignatureParts : this.ProtectionRequirements.OutgoingSignatureParts, true);
if (this.IsDuplexReply)
return ExtractMessageParts(action, this.ProtectionRequirements.OutgoingSignatureParts, true);
else
return ExtractMessageParts(action, (this.ActAsInitiator) ? this.ProtectionRequirements.IncomingSignatureParts : this.ProtectionRequirements.OutgoingSignatureParts, true);
}
else
{
return MessagePartSpecification.NoParts;
}
}
}
}
|