| | | 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.Globalization; |
| | | 6 | | using System.Net.Security; |
| | | 7 | | using System.Text; |
| | | 8 | | using CoreWCF.Configuration; |
| | | 9 | | using CoreWCF.Security; |
| | | 10 | | using CoreWCF.Security.Tokens; |
| | | 11 | | |
| | | 12 | | namespace CoreWCF.Channels |
| | | 13 | | { |
| | | 14 | | public sealed class SymmetricSecurityBindingElement : SecurityBindingElement //, IPolicyExportExtension |
| | | 15 | | { |
| | | 16 | | private MessageProtectionOrder _messageProtectionOrder; |
| | | 17 | | |
| | | 18 | | private SymmetricSecurityBindingElement(SymmetricSecurityBindingElement elementToBeCloned) |
| | 0 | 19 | | : base(elementToBeCloned) |
| | | 20 | | { |
| | 0 | 21 | | _messageProtectionOrder = elementToBeCloned._messageProtectionOrder; |
| | 0 | 22 | | if (elementToBeCloned.ProtectionTokenParameters != null) |
| | | 23 | | { |
| | 0 | 24 | | ProtectionTokenParameters = (SecurityTokenParameters)elementToBeCloned.ProtectionTokenParameters.Clone() |
| | | 25 | | } |
| | | 26 | | |
| | 0 | 27 | | RequireSignatureConfirmation = elementToBeCloned.RequireSignatureConfirmation; |
| | 0 | 28 | | } |
| | | 29 | | |
| | | 30 | | public SymmetricSecurityBindingElement() |
| | 0 | 31 | | : this((SecurityTokenParameters)null) |
| | | 32 | | { |
| | | 33 | | // empty |
| | 0 | 34 | | } |
| | | 35 | | |
| | | 36 | | public SymmetricSecurityBindingElement(SecurityTokenParameters protectionTokenParameters) |
| | 0 | 37 | | : base() |
| | | 38 | | { |
| | 0 | 39 | | _messageProtectionOrder = DefaultMessageProtectionOrder; |
| | 0 | 40 | | RequireSignatureConfirmation = DefaultRequireSignatureConfirmation; |
| | 0 | 41 | | ProtectionTokenParameters = protectionTokenParameters; |
| | 0 | 42 | | } |
| | | 43 | | |
| | 0 | 44 | | public bool RequireSignatureConfirmation { get; set; } |
| | | 45 | | |
| | | 46 | | public MessageProtectionOrder MessageProtectionOrder |
| | | 47 | | { |
| | | 48 | | get |
| | | 49 | | { |
| | 0 | 50 | | return _messageProtectionOrder; |
| | | 51 | | } |
| | | 52 | | set |
| | | 53 | | { |
| | 0 | 54 | | if (!MessageProtectionOrderHelper.IsDefined(value)) |
| | | 55 | | { |
| | 0 | 56 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | | 57 | | } |
| | | 58 | | |
| | 0 | 59 | | _messageProtectionOrder = value; |
| | 0 | 60 | | } |
| | | 61 | | } |
| | | 62 | | |
| | 0 | 63 | | public SecurityTokenParameters ProtectionTokenParameters { get; set; } |
| | | 64 | | |
| | | 65 | | internal override ISecurityCapabilities GetIndividualISecurityCapabilities() |
| | | 66 | | { |
| | 0 | 67 | | bool supportsServerAuthentication = false; |
| | 0 | 68 | | GetSupportingTokensCapabilities(out bool supportsClientAuthentication, out bool supportsClientWindowsIdentit |
| | 0 | 69 | | if (ProtectionTokenParameters != null) |
| | | 70 | | { |
| | 0 | 71 | | supportsClientAuthentication = supportsClientAuthentication || ProtectionTokenParameters.SupportsClientA |
| | 0 | 72 | | supportsClientWindowsIdentity = supportsClientWindowsIdentity || ProtectionTokenParameters.SupportsClien |
| | | 73 | | |
| | 0 | 74 | | if (ProtectionTokenParameters.HasAsymmetricKey) |
| | | 75 | | { |
| | 0 | 76 | | supportsServerAuthentication = ProtectionTokenParameters.SupportsClientAuthentication; |
| | | 77 | | } |
| | | 78 | | else |
| | | 79 | | { |
| | 0 | 80 | | supportsServerAuthentication = ProtectionTokenParameters.SupportsServerAuthentication; |
| | | 81 | | } |
| | | 82 | | } |
| | | 83 | | |
| | 0 | 84 | | return new SecurityCapabilities(supportsClientAuthentication, supportsServerAuthentication, supportsClientWi |
| | 0 | 85 | | ProtectionLevel.EncryptAndSign, ProtectionLevel.EncryptAndSign); |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | internal override bool SessionMode |
| | | 89 | | { |
| | | 90 | | get |
| | | 91 | | { |
| | 0 | 92 | | if (ProtectionTokenParameters is SecureConversationSecurityTokenParameters secureConversationTokenParame |
| | | 93 | | { |
| | 0 | 94 | | return secureConversationTokenParameters.RequireCancellation; |
| | | 95 | | } |
| | | 96 | | else |
| | | 97 | | { |
| | 0 | 98 | | return false; |
| | | 99 | | } |
| | | 100 | | } |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | internal override bool SupportsDuplex |
| | | 104 | | { |
| | 0 | 105 | | get { return SessionMode; } |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | internal override bool SupportsRequestReply |
| | | 109 | | { |
| | 0 | 110 | | get { return true; } |
| | | 111 | | } |
| | | 112 | | |
| | | 113 | | public override void SetKeyDerivation(bool requireDerivedKeys) |
| | | 114 | | { |
| | 0 | 115 | | base.SetKeyDerivation(requireDerivedKeys); |
| | 0 | 116 | | if (ProtectionTokenParameters != null) |
| | | 117 | | { |
| | 0 | 118 | | ProtectionTokenParameters.RequireDerivedKeys = requireDerivedKeys; |
| | | 119 | | } |
| | 0 | 120 | | } |
| | | 121 | | |
| | | 122 | | public override T GetProperty<T>(BindingContext context) |
| | | 123 | | { |
| | 0 | 124 | | if (context == null) |
| | | 125 | | { |
| | 0 | 126 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(context)); |
| | | 127 | | } |
| | | 128 | | |
| | 0 | 129 | | if (typeof(T) == typeof(ChannelProtectionRequirements)) |
| | | 130 | | { |
| | 0 | 131 | | AddressingVersion addressing = MessageVersion.Default.Addressing; |
| | 0 | 132 | | MessageEncodingBindingElement encoding = context.Binding.Elements.Find<MessageEncodingBindingElement>(); |
| | 0 | 133 | | if (encoding != null) |
| | | 134 | | { |
| | 0 | 135 | | addressing = encoding.MessageVersion.Addressing; |
| | | 136 | | } |
| | 0 | 137 | | ChannelProtectionRequirements myRequirements = GetProtectionRequirements(addressing, ProtectionLevel.Enc |
| | 0 | 138 | | myRequirements.Add(context.GetInnerProperty<ChannelProtectionRequirements>() ?? new ChannelProtectionReq |
| | 0 | 139 | | return (T)(object)myRequirements; |
| | | 140 | | } |
| | | 141 | | else |
| | | 142 | | { |
| | 0 | 143 | | return base.GetProperty<T>(context); |
| | | 144 | | } |
| | | 145 | | } |
| | | 146 | | |
| | | 147 | | public override string ToString() |
| | | 148 | | { |
| | 0 | 149 | | StringBuilder sb = new StringBuilder(); |
| | 0 | 150 | | sb.AppendLine(base.ToString()); |
| | | 151 | | |
| | 0 | 152 | | sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "MessageProtectionOrder: {0}", _messageProtectionO |
| | 0 | 153 | | sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "RequireSignatureConfirmation: {0}", RequireSignat |
| | 0 | 154 | | sb.Append("ProtectionTokenParameters: "); |
| | 0 | 155 | | if (ProtectionTokenParameters != null) |
| | | 156 | | { |
| | 0 | 157 | | sb.AppendLine(ProtectionTokenParameters.ToString().Trim().Replace("\n", "\n ")); |
| | | 158 | | } |
| | | 159 | | else |
| | | 160 | | { |
| | 0 | 161 | | sb.AppendLine("null"); |
| | | 162 | | } |
| | | 163 | | |
| | 0 | 164 | | return sb.ToString().Trim(); |
| | | 165 | | } |
| | | 166 | | |
| | | 167 | | public override BindingElement Clone() |
| | | 168 | | { |
| | 0 | 169 | | return new SymmetricSecurityBindingElement(this); |
| | | 170 | | } |
| | | 171 | | |
| | | 172 | | internal override SecurityProtocolFactory CreateSecurityProtocolFactory<TChannel>(BindingContext context, Securi |
| | | 173 | | { |
| | 0 | 174 | | throw new NotImplementedException(); |
| | | 175 | | } |
| | | 176 | | |
| | | 177 | | protected override IServiceDispatcher BuildServiceDispatcherCore<TChannel>(BindingContext context, IServiceDispa |
| | | 178 | | { |
| | 0 | 179 | | throw new NotImplementedException(); |
| | | 180 | | } |
| | | 181 | | } |
| | | 182 | | } |