| | | 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.Collections.Generic; |
| | | 6 | | using System.Xml; |
| | | 7 | | using CoreWCF.IdentityModel.Selectors; |
| | | 8 | | using CoreWCF.Runtime; |
| | | 9 | | |
| | | 10 | | namespace CoreWCF.IdentityModel.Tokens |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Abstract class for SecurityKeyIdentifierClause Serializer. |
| | | 14 | | /// </summary> |
| | | 15 | | internal class KeyInfoSerializer : SecurityTokenSerializer |
| | | 16 | | { |
| | | 17 | | private readonly List<KeyIdentifierEntry> _keyIdentifierEntries; |
| | | 18 | | private readonly List<KeyIdentifierClauseEntry> _keyIdentifierClauseEntries; |
| | | 19 | | private readonly List<SerializerEntries> _serializerEntries; |
| | | 20 | | private readonly List<TokenEntry> _tokenEntries; |
| | | 21 | | private SecurityTokenSerializer _innerSecurityTokenSerializer; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Creates an instance of <see cref="SecurityKeyIdentifierClauseSerializer"/> |
| | | 25 | | /// </summary> |
| | | 26 | | public KeyInfoSerializer(bool emitBspRequiredAttributes) |
| | 2 | 27 | | : this(emitBspRequiredAttributes, new DictionaryManager(), DXD.TrustDec2005Dictionary, null) |
| | | 28 | | { |
| | 2 | 29 | | } |
| | | 30 | | |
| | | 31 | | public KeyInfoSerializer( |
| | | 32 | | bool emitBspRequiredAttributes, |
| | | 33 | | DictionaryManager dictionaryManager, |
| | | 34 | | TrustDictionary trustDictionary, |
| | | 35 | | SecurityTokenSerializer innerSecurityTokenSerializer) : |
| | 2 | 36 | | this(emitBspRequiredAttributes, dictionaryManager, trustDictionary, innerSecurityTokenSerializer, null) |
| | | 37 | | { |
| | 2 | 38 | | } |
| | | 39 | | |
| | 134 | 40 | | public KeyInfoSerializer( |
| | 134 | 41 | | bool emitBspRequiredAttributes, |
| | 134 | 42 | | DictionaryManager dictionaryManager, |
| | 134 | 43 | | TrustDictionary trustDictionary, |
| | 134 | 44 | | SecurityTokenSerializer innerSecurityTokenSerializer, |
| | 134 | 45 | | Func<KeyInfoSerializer, IEnumerable<SerializerEntries>> additionalEntries) |
| | | 46 | | { |
| | 134 | 47 | | DictionaryManager = dictionaryManager; |
| | 134 | 48 | | EmitBspRequiredAttributes = emitBspRequiredAttributes; |
| | 134 | 49 | | _innerSecurityTokenSerializer = innerSecurityTokenSerializer; |
| | | 50 | | |
| | 134 | 51 | | _serializerEntries = new List<SerializerEntries> |
| | 134 | 52 | | { |
| | 134 | 53 | | new XmlDsigSep2000(this), |
| | 134 | 54 | | new Security.WSTrust(this, trustDictionary) |
| | 134 | 55 | | }; |
| | 134 | 56 | | if (additionalEntries != null) |
| | | 57 | | { |
| | 792 | 58 | | foreach (SerializerEntries entries in additionalEntries(this)) |
| | | 59 | | { |
| | 264 | 60 | | _serializerEntries.Add(entries); |
| | | 61 | | } |
| | | 62 | | } |
| | | 63 | | |
| | 134 | 64 | | bool wsSecuritySerializerFound = false; |
| | 936 | 65 | | foreach (SerializerEntries entry in _serializerEntries) |
| | | 66 | | { |
| | 400 | 67 | | if ((entry is WSSecurityXXX2005) || (entry is WSSecurityJan2004)) |
| | | 68 | | { |
| | 132 | 69 | | wsSecuritySerializerFound = true; |
| | 132 | 70 | | break; |
| | | 71 | | } |
| | | 72 | | } |
| | | 73 | | |
| | 134 | 74 | | if (!wsSecuritySerializerFound) |
| | | 75 | | { |
| | 2 | 76 | | _serializerEntries.Add(new WSSecurityXXX2005(this)); |
| | | 77 | | } |
| | | 78 | | |
| | 134 | 79 | | _tokenEntries = new List<TokenEntry>(); |
| | 134 | 80 | | _keyIdentifierEntries = new List<KeyIdentifierEntry>(); |
| | 134 | 81 | | _keyIdentifierClauseEntries = new List<KeyIdentifierClauseEntry>(); |
| | | 82 | | |
| | 1336 | 83 | | for (int i = 0; i < _serializerEntries.Count; ++i) |
| | | 84 | | { |
| | 534 | 85 | | SerializerEntries serializerEntry = _serializerEntries[i]; |
| | 534 | 86 | | serializerEntry.PopulateTokenEntries(_tokenEntries); |
| | 534 | 87 | | serializerEntry.PopulateKeyIdentifierEntries(_keyIdentifierEntries); |
| | 534 | 88 | | serializerEntry.PopulateKeyIdentifierClauseEntries(_keyIdentifierClauseEntries); |
| | | 89 | | } |
| | 134 | 90 | | } |
| | | 91 | | |
| | 262 | 92 | | public DictionaryManager DictionaryManager { get; } |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Gets or sets a value indicating if BSP required attributes should be written out. |
| | | 96 | | /// </summary> |
| | 658 | 97 | | public bool EmitBspRequiredAttributes { get; } |
| | | 98 | | |
| | | 99 | | public SecurityTokenSerializer InnerSecurityTokenSerializer |
| | | 100 | | { |
| | | 101 | | get |
| | | 102 | | { |
| | 23 | 103 | | return _innerSecurityTokenSerializer ?? this; |
| | | 104 | | } |
| | | 105 | | set |
| | | 106 | | { |
| | 0 | 107 | | _innerSecurityTokenSerializer = value; |
| | 0 | 108 | | } |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | protected override bool CanReadTokenCore(XmlReader reader) |
| | | 112 | | { |
| | 0 | 113 | | return false; |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | protected override SecurityToken ReadTokenCore(XmlReader reader, SecurityTokenResolver tokenResolver) |
| | | 117 | | { |
| | 0 | 118 | | XmlDictionaryReader localReader = XmlDictionaryReader.CreateDictionaryReader(reader); |
| | 0 | 119 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.CannotReadToken, rea |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | protected override bool CanWriteTokenCore(SecurityToken token) |
| | | 123 | | { |
| | 0 | 124 | | return false; |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | protected override void WriteTokenCore(XmlWriter writer, SecurityToken token) |
| | | 128 | | { |
| | 0 | 129 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Standar |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | protected override bool CanReadKeyIdentifierCore(XmlReader reader) |
| | | 133 | | { |
| | 0 | 134 | | XmlDictionaryReader localReader = XmlDictionaryReader.CreateDictionaryReader(reader); |
| | 0 | 135 | | for (int i = 0; i < _keyIdentifierEntries.Count; i++) |
| | | 136 | | { |
| | 0 | 137 | | KeyIdentifierEntry keyIdentifierEntry = _keyIdentifierEntries[i]; |
| | 0 | 138 | | if (keyIdentifierEntry.CanReadKeyIdentifierCore(localReader)) |
| | | 139 | | { |
| | 0 | 140 | | return true; |
| | | 141 | | } |
| | | 142 | | } |
| | 0 | 143 | | return false; |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | protected override SecurityKeyIdentifier ReadKeyIdentifierCore(XmlReader reader) |
| | | 147 | | { |
| | 23 | 148 | | XmlDictionaryReader localReader = XmlDictionaryReader.CreateDictionaryReader(reader); |
| | 23 | 149 | | localReader.ReadStartElement(CoreWCF.XD.XmlSignatureDictionary.KeyInfo, CoreWCF.XD.XmlSignatureDictionary.Na |
| | 23 | 150 | | SecurityKeyIdentifier keyIdentifier = new SecurityKeyIdentifier(); |
| | 46 | 151 | | while (localReader.IsStartElement()) |
| | | 152 | | { |
| | 23 | 153 | | SecurityKeyIdentifierClause clause = InnerSecurityTokenSerializer.ReadKeyIdentifierClause(localReader); |
| | 23 | 154 | | if (clause == null) |
| | | 155 | | { |
| | 0 | 156 | | localReader.Skip(); |
| | | 157 | | } |
| | | 158 | | else |
| | | 159 | | { |
| | 23 | 160 | | keyIdentifier.Add(clause); |
| | | 161 | | } |
| | | 162 | | } |
| | 23 | 163 | | if (keyIdentifier.Count == 0) |
| | | 164 | | { |
| | 0 | 165 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format("ErrorDeserializing |
| | | 166 | | } |
| | 23 | 167 | | localReader.ReadEndElement(); |
| | | 168 | | |
| | 23 | 169 | | return keyIdentifier; |
| | | 170 | | } |
| | | 171 | | |
| | | 172 | | protected override bool CanWriteKeyIdentifierCore(SecurityKeyIdentifier keyIdentifier) |
| | | 173 | | { |
| | 0 | 174 | | for (int i = 0; i < _keyIdentifierEntries.Count; ++i) |
| | | 175 | | { |
| | 0 | 176 | | KeyIdentifierEntry keyIdentifierEntry = _keyIdentifierEntries[i]; |
| | 0 | 177 | | if (keyIdentifierEntry.SupportsCore(keyIdentifier)) |
| | | 178 | | { |
| | 0 | 179 | | return true; |
| | | 180 | | } |
| | | 181 | | } |
| | 0 | 182 | | return false; |
| | | 183 | | } |
| | | 184 | | |
| | | 185 | | protected override void WriteKeyIdentifierCore(XmlWriter writer, SecurityKeyIdentifier keyIdentifier) |
| | | 186 | | { |
| | 0 | 187 | | bool wroteKeyIdentifier = false; |
| | 0 | 188 | | XmlDictionaryWriter localWriter = XmlDictionaryWriter.CreateDictionaryWriter(writer); |
| | 0 | 189 | | for (int i = 0; i < _keyIdentifierEntries.Count; ++i) |
| | | 190 | | { |
| | 0 | 191 | | KeyIdentifierEntry keyIdentifierEntry = _keyIdentifierEntries[i]; |
| | 0 | 192 | | if (keyIdentifierEntry.SupportsCore(keyIdentifier)) |
| | | 193 | | { |
| | | 194 | | try |
| | | 195 | | { |
| | 0 | 196 | | keyIdentifierEntry.WriteKeyIdentifierCore(localWriter, keyIdentifier); |
| | 0 | 197 | | } |
| | 0 | 198 | | catch (Exception e) |
| | | 199 | | { |
| | 0 | 200 | | if (Fx.IsFatal(e)) |
| | | 201 | | { |
| | 0 | 202 | | throw; |
| | | 203 | | } |
| | | 204 | | |
| | 0 | 205 | | if (!ShouldWrapException(e)) |
| | | 206 | | { |
| | 0 | 207 | | throw; |
| | | 208 | | } |
| | | 209 | | |
| | 0 | 210 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format("ErrorSeria |
| | | 211 | | } |
| | 0 | 212 | | wroteKeyIdentifier = true; |
| | 0 | 213 | | break; |
| | | 214 | | } |
| | | 215 | | } |
| | | 216 | | |
| | 0 | 217 | | if (!wroteKeyIdentifier) |
| | | 218 | | { |
| | 0 | 219 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Sta |
| | | 220 | | } |
| | | 221 | | |
| | 0 | 222 | | localWriter.Flush(); |
| | 0 | 223 | | } |
| | | 224 | | |
| | | 225 | | protected override bool CanReadKeyIdentifierClauseCore(XmlReader reader) |
| | | 226 | | { |
| | 6 | 227 | | XmlDictionaryReader localReader = XmlDictionaryReader.CreateDictionaryReader(reader); |
| | 48 | 228 | | for (int i = 0; i < _keyIdentifierClauseEntries.Count; i++) |
| | | 229 | | { |
| | 24 | 230 | | KeyIdentifierClauseEntry keyIdentifierClauseEntry = _keyIdentifierClauseEntries[i]; |
| | 24 | 231 | | if (keyIdentifierClauseEntry.CanReadKeyIdentifierClauseCore(localReader)) |
| | | 232 | | { |
| | 6 | 233 | | return true; |
| | | 234 | | } |
| | | 235 | | } |
| | 0 | 236 | | return false; |
| | | 237 | | } |
| | | 238 | | |
| | | 239 | | protected override SecurityKeyIdentifierClause ReadKeyIdentifierClauseCore(XmlReader reader) |
| | | 240 | | { |
| | 39 | 241 | | XmlDictionaryReader localReader = XmlDictionaryReader.CreateDictionaryReader(reader); |
| | 312 | 242 | | for (int i = 0; i < _keyIdentifierClauseEntries.Count; i++) |
| | | 243 | | { |
| | 156 | 244 | | KeyIdentifierClauseEntry keyIdentifierClauseEntry = _keyIdentifierClauseEntries[i]; |
| | 156 | 245 | | if (keyIdentifierClauseEntry.CanReadKeyIdentifierClauseCore(localReader)) |
| | | 246 | | { |
| | | 247 | | try |
| | | 248 | | { |
| | 39 | 249 | | return keyIdentifierClauseEntry.ReadKeyIdentifierClauseCore(localReader); |
| | | 250 | | } |
| | 0 | 251 | | catch (Exception e) |
| | | 252 | | { |
| | 0 | 253 | | if (Fx.IsFatal(e)) |
| | | 254 | | { |
| | 0 | 255 | | throw; |
| | | 256 | | } |
| | | 257 | | |
| | 0 | 258 | | if (!ShouldWrapException(e)) |
| | | 259 | | { |
| | 0 | 260 | | throw; |
| | | 261 | | } |
| | 0 | 262 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format("ErrorDeser |
| | | 263 | | } |
| | | 264 | | } |
| | | 265 | | } |
| | 0 | 266 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format("CannotReadKeyIdentifie |
| | 39 | 267 | | } |
| | | 268 | | |
| | | 269 | | protected override bool CanWriteKeyIdentifierClauseCore(SecurityKeyIdentifierClause keyIdentifierClause) |
| | | 270 | | { |
| | 0 | 271 | | for (int i = 0; i < _keyIdentifierClauseEntries.Count; ++i) |
| | | 272 | | { |
| | 0 | 273 | | KeyIdentifierClauseEntry keyIdentifierClauseEntry = _keyIdentifierClauseEntries[i]; |
| | 0 | 274 | | if (keyIdentifierClauseEntry.SupportsCore(keyIdentifierClause)) |
| | | 275 | | { |
| | 0 | 276 | | return true; |
| | | 277 | | } |
| | | 278 | | } |
| | 0 | 279 | | return false; |
| | | 280 | | } |
| | | 281 | | |
| | | 282 | | protected override void WriteKeyIdentifierClauseCore(XmlWriter writer, SecurityKeyIdentifierClause keyIdentifier |
| | | 283 | | { |
| | 20 | 284 | | bool wroteKeyIdentifierClause = false; |
| | 20 | 285 | | XmlDictionaryWriter localWriter = XmlDictionaryWriter.CreateDictionaryWriter(writer); |
| | 160 | 286 | | for (int i = 0; i < _keyIdentifierClauseEntries.Count; ++i) |
| | | 287 | | { |
| | 80 | 288 | | KeyIdentifierClauseEntry keyIdentifierClauseEntry = _keyIdentifierClauseEntries[i]; |
| | 80 | 289 | | if (keyIdentifierClauseEntry.SupportsCore(keyIdentifierClause)) |
| | | 290 | | { |
| | | 291 | | try |
| | | 292 | | { |
| | 20 | 293 | | keyIdentifierClauseEntry.WriteKeyIdentifierClauseCore(localWriter, keyIdentifierClause); |
| | 20 | 294 | | } |
| | 0 | 295 | | catch (Exception e) |
| | | 296 | | { |
| | 0 | 297 | | if (Fx.IsFatal(e)) |
| | | 298 | | { |
| | 0 | 299 | | throw; |
| | | 300 | | } |
| | | 301 | | |
| | 0 | 302 | | if (!ShouldWrapException(e)) |
| | | 303 | | { |
| | 0 | 304 | | throw; |
| | | 305 | | } |
| | | 306 | | |
| | 0 | 307 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException("ErrorSerializingKeyI |
| | | 308 | | } |
| | 20 | 309 | | wroteKeyIdentifierClause = true; |
| | 20 | 310 | | break; |
| | | 311 | | } |
| | | 312 | | } |
| | | 313 | | |
| | 20 | 314 | | if (!wroteKeyIdentifierClause) |
| | | 315 | | { |
| | 0 | 316 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Sta |
| | | 317 | | } |
| | | 318 | | |
| | 20 | 319 | | localWriter.Flush(); |
| | 20 | 320 | | } |
| | | 321 | | |
| | | 322 | | internal void PopulateStrEntries(IList<StrEntry> strEntries) |
| | | 323 | | { |
| | 1336 | 324 | | foreach (SerializerEntries serializerEntry in _serializerEntries) |
| | | 325 | | { |
| | 534 | 326 | | serializerEntry.PopulateStrEntries(strEntries); |
| | | 327 | | } |
| | 134 | 328 | | } |
| | | 329 | | |
| | | 330 | | private bool ShouldWrapException(Exception e) |
| | | 331 | | { |
| | 0 | 332 | | return ((e is ArgumentException) || (e is FormatException) || (e is InvalidOperationException)); |
| | | 333 | | } |
| | | 334 | | |
| | | 335 | | internal Type[] GetTokenTypes(string tokenTypeUri) |
| | | 336 | | { |
| | 19 | 337 | | if (tokenTypeUri != null) |
| | | 338 | | { |
| | 248 | 339 | | for (int i = 0; i < _tokenEntries.Count; i++) |
| | | 340 | | { |
| | 124 | 341 | | TokenEntry tokenEntry = _tokenEntries[i]; |
| | | 342 | | |
| | 124 | 343 | | if (tokenEntry.SupportsTokenTypeUri(tokenTypeUri)) |
| | | 344 | | { |
| | 19 | 345 | | return tokenEntry.GetTokenTypes(); |
| | | 346 | | } |
| | | 347 | | } |
| | | 348 | | } |
| | 0 | 349 | | return null; |
| | | 350 | | } |
| | | 351 | | |
| | | 352 | | protected internal virtual string GetTokenTypeUri(Type tokenType) |
| | | 353 | | { |
| | 16 | 354 | | if (tokenType != null) |
| | | 355 | | { |
| | 224 | 356 | | for (int i = 0; i < _tokenEntries.Count; i++) |
| | | 357 | | { |
| | 112 | 358 | | TokenEntry tokenEntry = _tokenEntries[i]; |
| | | 359 | | |
| | 112 | 360 | | if (tokenEntry.SupportsCore(tokenType)) |
| | | 361 | | { |
| | 16 | 362 | | return tokenEntry.TokenTypeUri; |
| | | 363 | | } |
| | | 364 | | } |
| | | 365 | | } |
| | 0 | 366 | | return null; |
| | | 367 | | } |
| | | 368 | | } |
| | | 369 | | } |