| | | 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.Xml; |
| | | 6 | | using CoreWCF.Channels; |
| | | 7 | | using CoreWCF.Description; |
| | | 8 | | using ISignatureValueSecurityElement = CoreWCF.IdentityModel.ISignatureValueSecurityElement; |
| | | 9 | | |
| | | 10 | | namespace CoreWCF.Security |
| | | 11 | | { |
| | | 12 | | public abstract class SecurityVersion |
| | | 13 | | { |
| | 9 | 14 | | internal SecurityVersion(XmlDictionaryString headerName, XmlDictionaryString headerNamespace, XmlDictionaryStrin |
| | | 15 | | { |
| | 9 | 16 | | HeaderName = headerName; |
| | 9 | 17 | | HeaderNamespace = headerNamespace; |
| | 9 | 18 | | HeaderPrefix = headerPrefix; |
| | 9 | 19 | | } |
| | | 20 | | |
| | 200 | 21 | | internal XmlDictionaryString HeaderName { get; } |
| | | 22 | | |
| | 337 | 23 | | internal XmlDictionaryString HeaderNamespace { get; } |
| | | 24 | | |
| | 63 | 25 | | internal XmlDictionaryString HeaderPrefix { get; } |
| | | 26 | | |
| | | 27 | | internal abstract XmlDictionaryString FailedAuthenticationFaultCode |
| | | 28 | | { |
| | | 29 | | get; |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | internal abstract XmlDictionaryString InvalidSecurityFaultCode |
| | | 33 | | { |
| | | 34 | | get; |
| | | 35 | | } |
| | 453 | 36 | | public static SecurityVersion WSSecurity10 => SecurityVersion10.Instance; |
| | | 37 | | |
| | | 38 | | |
| | 450 | 39 | | public static SecurityVersion WSSecurity11 => SecurityVersion11.Instance; |
| | | 40 | | |
| | 38 | 41 | | internal static SecurityVersion Default => WSSecurity11; |
| | | 42 | | |
| | | 43 | | internal abstract ReceiveSecurityHeader CreateReceiveSecurityHeader(Message message, |
| | | 44 | | string actor, bool mustUnderstand, bool relay, |
| | | 45 | | SecurityStandardsManager standardsManager, |
| | | 46 | | SecurityAlgorithmSuite algorithmSuite, |
| | | 47 | | MessageDirection direction, |
| | | 48 | | int headerIndex); |
| | | 49 | | |
| | | 50 | | internal abstract SendSecurityHeader CreateSendSecurityHeader(Message message, |
| | | 51 | | string actor, bool mustUnderstand, bool relay, |
| | | 52 | | SecurityStandardsManager standardsManager, |
| | | 53 | | SecurityAlgorithmSuite algorithmSuite, |
| | | 54 | | MessageDirection direction); |
| | | 55 | | |
| | | 56 | | internal bool DoesMessageContainSecurityHeader(Message message) |
| | | 57 | | { |
| | 23 | 58 | | return message.Headers.FindHeader(HeaderName.Value, HeaderNamespace.Value) >= 0; |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | internal int FindIndexOfSecurityHeader(Message message, string[] actors) |
| | | 62 | | { |
| | 20 | 63 | | return message.Headers.FindHeader(HeaderName.Value, HeaderNamespace.Value, actors); |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | internal virtual bool IsReaderAtSignatureConfirmation(XmlDictionaryReader reader) |
| | | 67 | | { |
| | 6 | 68 | | return false; |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | // The security always look for Empty soap role. If not found, we will also look for Ultimate actors (next incl |
| | | 72 | | // In the future, till we support intermediary scenario, we should refactor this api to do not take actor parame |
| | | 73 | | internal ReceiveSecurityHeader TryCreateReceiveSecurityHeader(Message message, |
| | | 74 | | string actor, |
| | | 75 | | SecurityStandardsManager standardsManager, |
| | | 76 | | SecurityAlgorithmSuite algorithmSuite, MessageDirection direction) |
| | | 77 | | { |
| | 94 | 78 | | int headerIndex = message.Headers.FindHeader(HeaderName.Value, HeaderNamespace.Value, actor); |
| | 94 | 79 | | if (headerIndex < 0 && string.IsNullOrEmpty(actor)) |
| | | 80 | | { |
| | 0 | 81 | | headerIndex = message.Headers.FindHeader(HeaderName.Value, HeaderNamespace.Value, message.Version.Envelo |
| | | 82 | | } |
| | | 83 | | |
| | 94 | 84 | | if (headerIndex < 0) |
| | | 85 | | { |
| | 0 | 86 | | return null; |
| | | 87 | | } |
| | 94 | 88 | | MessageHeaderInfo headerInfo = message.Headers[headerIndex]; |
| | 94 | 89 | | return CreateReceiveSecurityHeader(message, |
| | 94 | 90 | | headerInfo.Actor, headerInfo.MustUnderstand, headerInfo.Relay, |
| | 94 | 91 | | standardsManager, algorithmSuite, |
| | 94 | 92 | | direction, headerIndex); |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | internal virtual void WriteSignatureConfirmation(XmlDictionaryWriter writer, string id, byte[] signatureConfirma |
| | | 96 | | { |
| | 0 | 97 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 98 | | SR.Format(SR.SignatureConfirmationNotSupported))); |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | internal void WriteStartHeader(XmlDictionaryWriter writer) |
| | | 102 | | { |
| | 63 | 103 | | writer.WriteStartElement(HeaderPrefix.Value, HeaderName, HeaderNamespace); |
| | 63 | 104 | | } |
| | | 105 | | |
| | | 106 | | internal virtual ISignatureValueSecurityElement ReadSignatureConfirmation(XmlDictionaryReader reader) |
| | | 107 | | { |
| | 0 | 108 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.SignatureConfirma |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | private class SecurityVersion10 : SecurityVersion |
| | | 112 | | { |
| | 9 | 113 | | protected SecurityVersion10() : base(XD.SecurityJan2004Dictionary.Security, XD.SecurityJan2004Dictionary.Nam |
| | | 114 | | { |
| | 9 | 115 | | } |
| | | 116 | | |
| | 457 | 117 | | public static SecurityVersion10 Instance { get; } = new SecurityVersion10(); |
| | | 118 | | |
| | | 119 | | internal override SendSecurityHeader CreateSendSecurityHeader(Message message, |
| | | 120 | | string actor, bool mustUnderstand, bool relay, |
| | | 121 | | SecurityStandardsManager standardsManager, |
| | | 122 | | SecurityAlgorithmSuite algorithmSuite, |
| | | 123 | | MessageDirection direction) |
| | | 124 | | { |
| | 5 | 125 | | return new WSSecurityOneDotZeroSendSecurityHeader(message, actor, mustUnderstand, relay, standardsManage |
| | | 126 | | } |
| | | 127 | | |
| | | 128 | | internal override ReceiveSecurityHeader CreateReceiveSecurityHeader(Message message, |
| | | 129 | | string actor, bool mustUnderstand, bool relay, |
| | | 130 | | SecurityStandardsManager standardsManager, |
| | | 131 | | SecurityAlgorithmSuite algorithmSuite, |
| | | 132 | | MessageDirection direction, |
| | | 133 | | int headerIndex) |
| | | 134 | | { |
| | 6 | 135 | | return new WSSecurityOneDotZeroReceiveSecurityHeader( |
| | 6 | 136 | | message, |
| | 6 | 137 | | actor, mustUnderstand, relay, |
| | 6 | 138 | | standardsManager, |
| | 6 | 139 | | algorithmSuite, headerIndex, direction); |
| | | 140 | | } |
| | | 141 | | |
| | | 142 | | public override string ToString() |
| | | 143 | | { |
| | 0 | 144 | | return "WSSecurity10"; |
| | | 145 | | } |
| | | 146 | | |
| | 47 | 147 | | internal override XmlDictionaryString FailedAuthenticationFaultCode => XD.SecurityJan2004Dictionary.FailedAu |
| | | 148 | | |
| | 22 | 149 | | internal override XmlDictionaryString InvalidSecurityFaultCode => XD.SecurityJan2004Dictionary.InvalidSecuri |
| | | 150 | | } |
| | | 151 | | |
| | | 152 | | private sealed class SecurityVersion11 : SecurityVersion10 |
| | | 153 | | { |
| | | 154 | | private SecurityVersion11() |
| | 5 | 155 | | : base() |
| | | 156 | | { |
| | 5 | 157 | | } |
| | | 158 | | |
| | 455 | 159 | | public static new SecurityVersion11 Instance { get; } = new SecurityVersion11(); |
| | | 160 | | |
| | 0 | 161 | | internal bool SupportsSignatureConfirmation => true; |
| | | 162 | | |
| | | 163 | | internal override ReceiveSecurityHeader CreateReceiveSecurityHeader(Message message, |
| | | 164 | | string actor, bool mustUnderstand, bool relay, |
| | | 165 | | SecurityStandardsManager standardsManager, |
| | | 166 | | SecurityAlgorithmSuite algorithmSuite, |
| | | 167 | | MessageDirection direction, |
| | | 168 | | int headerIndex) |
| | | 169 | | { |
| | 88 | 170 | | return new WSSecurityOneDotOneReceiveSecurityHeader( |
| | 88 | 171 | | message, |
| | 88 | 172 | | actor, mustUnderstand, relay, |
| | 88 | 173 | | standardsManager, |
| | 88 | 174 | | algorithmSuite, headerIndex, direction); |
| | | 175 | | } |
| | | 176 | | |
| | | 177 | | internal override SendSecurityHeader CreateSendSecurityHeader(Message message, |
| | | 178 | | string actor, bool mustUnderstand, bool relay, |
| | | 179 | | SecurityStandardsManager standardsManager, |
| | | 180 | | SecurityAlgorithmSuite algorithmSuite, MessageDirection direction) |
| | | 181 | | { |
| | 58 | 182 | | return new WSSecurityOneDotOneSendSecurityHeader(message, actor, mustUnderstand, relay, standardsManager |
| | | 183 | | } |
| | | 184 | | |
| | | 185 | | internal override bool IsReaderAtSignatureConfirmation(XmlDictionaryReader reader) |
| | | 186 | | { |
| | 88 | 187 | | return reader.IsStartElement(XD.SecurityXXX2005Dictionary.SignatureConfirmation, XD.SecurityXXX2005Dicti |
| | | 188 | | } |
| | | 189 | | |
| | | 190 | | internal override ISignatureValueSecurityElement ReadSignatureConfirmation(XmlDictionaryReader reader) |
| | | 191 | | { |
| | 0 | 192 | | reader.MoveToStartElement(XD.SecurityXXX2005Dictionary.SignatureConfirmation, XD.SecurityXXX2005Dictiona |
| | 0 | 193 | | bool isEmptyElement = reader.IsEmptyElement; |
| | 0 | 194 | | string id = XmlHelper.GetRequiredNonEmptyAttribute(reader, XD.UtilityDictionary.IdAttribute, XD.UtilityD |
| | 0 | 195 | | byte[] signatureValue = XmlHelper.GetRequiredBase64Attribute(reader, XD.SecurityXXX2005Dictionary.ValueA |
| | 0 | 196 | | reader.ReadStartElement(); |
| | 0 | 197 | | if (!isEmptyElement) |
| | | 198 | | { |
| | 0 | 199 | | reader.ReadEndElement(); |
| | | 200 | | } |
| | 0 | 201 | | return new SignatureConfirmationElement(id, signatureValue, this); |
| | | 202 | | } |
| | | 203 | | |
| | | 204 | | internal override void WriteSignatureConfirmation(XmlDictionaryWriter writer, string id, byte[] signature) |
| | | 205 | | { |
| | 0 | 206 | | if (id == null) |
| | | 207 | | { |
| | 0 | 208 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(id)); |
| | | 209 | | } |
| | 0 | 210 | | if (signature == null) |
| | | 211 | | { |
| | 0 | 212 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(signature)); |
| | | 213 | | } |
| | 0 | 214 | | writer.WriteStartElement(XD.SecurityXXX2005Dictionary.Prefix.Value, XD.SecurityXXX2005Dictionary.Signatu |
| | 0 | 215 | | writer.WriteAttributeString(XD.UtilityDictionary.Prefix.Value, XD.UtilityDictionary.IdAttribute, XD.Util |
| | 0 | 216 | | writer.WriteStartAttribute(XD.SecurityXXX2005Dictionary.ValueAttribute, null); |
| | 0 | 217 | | writer.WriteBase64(signature, 0, signature.Length); |
| | 0 | 218 | | writer.WriteEndAttribute(); |
| | 0 | 219 | | writer.WriteEndElement(); |
| | 0 | 220 | | } |
| | | 221 | | |
| | | 222 | | public override string ToString() |
| | | 223 | | { |
| | 0 | 224 | | return "WSSecurity11"; |
| | | 225 | | } |
| | | 226 | | } |
| | | 227 | | } |
| | | 228 | | } |