| | | 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.Globalization; |
| | | 7 | | using System.Text; |
| | | 8 | | using System.Xml; |
| | | 9 | | using CoreWCF.IdentityModel.Selectors; |
| | | 10 | | using CoreWCF.Security; |
| | | 11 | | using CoreWCF.Security.Tokens; |
| | | 12 | | using HexBinary = CoreWCF.Security.SoapHexBinary; |
| | | 13 | | using KeyIdentifierClauseEntry = CoreWCF.IdentityModel.Selectors.SecurityTokenSerializer.KeyIdentifierClauseEntry; |
| | | 14 | | using StrEntry = CoreWCF.IdentityModel.Selectors.SecurityTokenSerializer.StrEntry; |
| | | 15 | | using TokenEntry = CoreWCF.IdentityModel.Selectors.SecurityTokenSerializer.TokenEntry; |
| | | 16 | | |
| | | 17 | | namespace CoreWCF.IdentityModel.Tokens |
| | | 18 | | { |
| | | 19 | | internal class WSSecurityJan2004 : SecurityTokenSerializer.SerializerEntries |
| | | 20 | | { |
| | 134 | 21 | | public WSSecurityJan2004(KeyInfoSerializer securityTokenSerializer) |
| | | 22 | | { |
| | 134 | 23 | | SecurityTokenSerializer = securityTokenSerializer; |
| | 134 | 24 | | } |
| | | 25 | | |
| | 926 | 26 | | public KeyInfoSerializer SecurityTokenSerializer { get; } |
| | | 27 | | |
| | | 28 | | public override void PopulateKeyIdentifierClauseEntries(IList<KeyIdentifierClauseEntry> clauseEntries) |
| | | 29 | | { |
| | 6 | 30 | | List<StrEntry> strEntries = new List<StrEntry>(); |
| | 6 | 31 | | SecurityTokenSerializer.PopulateStrEntries(strEntries); |
| | 6 | 32 | | SecurityTokenReferenceJan2004ClauseEntry strClause = new SecurityTokenReferenceJan2004ClauseEntry(SecurityTo |
| | 6 | 33 | | clauseEntries.Add(strClause); |
| | 6 | 34 | | } |
| | | 35 | | |
| | | 36 | | protected void PopulateJan2004StrEntries(IList<StrEntry> strEntries) |
| | | 37 | | { |
| | 134 | 38 | | strEntries.Add(new LocalReferenceStrEntry(SecurityTokenSerializer.EmitBspRequiredAttributes, SecurityTokenSe |
| | 134 | 39 | | strEntries.Add(new X509SkiStrEntry(SecurityTokenSerializer.EmitBspRequiredAttributes)); |
| | 134 | 40 | | strEntries.Add(new X509IssuerSerialStrEntry()); |
| | 134 | 41 | | } |
| | | 42 | | |
| | | 43 | | public override void PopulateStrEntries(IList<StrEntry> strEntries) |
| | | 44 | | { |
| | 6 | 45 | | PopulateJan2004StrEntries(strEntries); |
| | 6 | 46 | | } |
| | | 47 | | |
| | | 48 | | protected void PopulateJan2004TokenEntries(IList<TokenEntry> tokenEntryList) |
| | | 49 | | { |
| | 134 | 50 | | tokenEntryList.Add(new GenericXmlTokenEntry()); |
| | 134 | 51 | | tokenEntryList.Add(new UserNamePasswordTokenEntry()); |
| | 134 | 52 | | tokenEntryList.Add(new X509TokenEntry()); |
| | 134 | 53 | | } |
| | | 54 | | |
| | | 55 | | public override void PopulateTokenEntries(IList<TokenEntry> tokenEntryList) |
| | | 56 | | { |
| | 6 | 57 | | PopulateJan2004TokenEntries(tokenEntryList); |
| | 6 | 58 | | } |
| | | 59 | | |
| | | 60 | | internal abstract class BinaryTokenEntry : TokenEntry |
| | | 61 | | { |
| | 0 | 62 | | internal static readonly XmlDictionaryString ElementName = CoreWCF.XD.SecurityJan2004Dictionary.BinarySecuri |
| | 0 | 63 | | internal static readonly XmlDictionaryString EncodingTypeAttribute = CoreWCF.XD.SecurityJan2004Dictionary.En |
| | | 64 | | internal const string EncodingTypeAttributeString = SecurityJan2004Strings.EncodingType; |
| | | 65 | | internal const string EncodingTypeValueBase64Binary = SecurityJan2004Strings.EncodingTypeValueBase64Binary; |
| | | 66 | | internal const string EncodingTypeValueHexBinary = SecurityJan2004Strings.EncodingTypeValueHexBinary; |
| | 0 | 67 | | internal static readonly XmlDictionaryString ValueTypeAttribute = CoreWCF.XD.SecurityJan2004Dictionary.Value |
| | | 68 | | |
| | | 69 | | private readonly string[] _valueTypeUris = null; |
| | | 70 | | |
| | 134 | 71 | | protected BinaryTokenEntry(string valueTypeUri) |
| | | 72 | | { |
| | 134 | 73 | | _valueTypeUris = new string[1]; |
| | 134 | 74 | | _valueTypeUris[0] = valueTypeUri; |
| | 134 | 75 | | } |
| | | 76 | | |
| | 0 | 77 | | protected BinaryTokenEntry(string[] valueTypeUris) |
| | | 78 | | { |
| | 0 | 79 | | if (valueTypeUris == null) |
| | | 80 | | { |
| | 0 | 81 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(valueTypeUris)); |
| | | 82 | | } |
| | | 83 | | |
| | 0 | 84 | | _valueTypeUris = new string[valueTypeUris.GetLength(0)]; |
| | 0 | 85 | | for (int i = 0; i < _valueTypeUris.GetLength(0); ++i) |
| | | 86 | | { |
| | 0 | 87 | | _valueTypeUris[i] = valueTypeUris[i]; |
| | | 88 | | } |
| | 0 | 89 | | } |
| | | 90 | | |
| | 0 | 91 | | protected override XmlDictionaryString LocalName { get { return ElementName; } } |
| | 0 | 92 | | protected override XmlDictionaryString NamespaceUri { get { return CoreWCF.XD.SecurityJan2004Dictionary.Name |
| | 0 | 93 | | public override string TokenTypeUri { get { return _valueTypeUris[0]; } } |
| | 0 | 94 | | protected override string ValueTypeUri { get { return _valueTypeUris[0]; } } |
| | | 95 | | public override bool SupportsTokenTypeUri(string tokenTypeUri) |
| | | 96 | | { |
| | 70 | 97 | | for (int i = 0; i < _valueTypeUris.GetLength(0); ++i) |
| | | 98 | | { |
| | 19 | 99 | | if (_valueTypeUris[i] == tokenTypeUri) |
| | | 100 | | { |
| | 3 | 101 | | return true; |
| | | 102 | | } |
| | | 103 | | } |
| | | 104 | | |
| | 16 | 105 | | return false; |
| | | 106 | | } |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | private class GenericXmlTokenEntry : TokenEntry |
| | | 110 | | { |
| | 0 | 111 | | protected override XmlDictionaryString LocalName { get { return null; } } |
| | 0 | 112 | | protected override XmlDictionaryString NamespaceUri { get { return null; } } |
| | 8 | 113 | | protected override Type[] GetTokenTypesCore() { return new Type[] { typeof(GenericXmlSecurityToken) }; } |
| | 19 | 114 | | public override string TokenTypeUri { get { return null; } } |
| | 0 | 115 | | protected override string ValueTypeUri { get { return null; } } |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | private class UserNamePasswordTokenEntry : TokenEntry |
| | | 119 | | { |
| | 0 | 120 | | protected override XmlDictionaryString LocalName { get { return CoreWCF.XD.SecurityJan2004Dictionary.UserNam |
| | 0 | 121 | | protected override XmlDictionaryString NamespaceUri { get { return CoreWCF.XD.SecurityJan2004Dictionary.Name |
| | 8 | 122 | | protected override Type[] GetTokenTypesCore() { return new Type[] { typeof(UserNameSecurityToken) }; } |
| | 19 | 123 | | public override string TokenTypeUri { get { return SecurityJan2004Strings.UPTokenType; } } |
| | 0 | 124 | | protected override string ValueTypeUri { get { return null; } } |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | //class KerberosTokenEntry : BinaryTokenEntry |
| | | 128 | | //{ |
| | | 129 | | // public KerberosTokenEntry() |
| | | 130 | | // : base(new string[] { SecurityJan2004Strings.KerberosTokenTypeGSS, SecurityJan2004Strings.KerberosToke |
| | | 131 | | // { |
| | | 132 | | // } |
| | | 133 | | |
| | | 134 | | // protected override Type[] GetTokenTypesCore() { return new Type[] { typeof(KerberosReceiverSecurityToken), |
| | | 135 | | //} |
| | | 136 | | |
| | | 137 | | //protected class SamlTokenEntry : TokenEntry |
| | | 138 | | //{ |
| | | 139 | | // protected override XmlDictionaryString LocalName { get { return XD.SecurityJan2004Dictionary.SamlAssertion |
| | | 140 | | // protected override XmlDictionaryString NamespaceUri { get { return XD.SecurityJan2004Dictionary.SamlUri; } |
| | | 141 | | // protected override Type[] GetTokenTypesCore() { return new Type[] { typeof(SamlSecurityToken) }; } |
| | | 142 | | // public override string TokenTypeUri { get { return null; } } |
| | | 143 | | // protected override string ValueTypeUri { get { return null; } } |
| | | 144 | | //} |
| | | 145 | | protected class WrappedKeyTokenEntry : TokenEntry |
| | | 146 | | { |
| | 0 | 147 | | protected override XmlDictionaryString LocalName { get { return EncryptedKey.ElementName; } } |
| | 0 | 148 | | protected override XmlDictionaryString NamespaceUri { get { return CoreWCF.XD.XmlEncryptionDictionary.Namesp |
| | 8 | 149 | | protected override Type[] GetTokenTypesCore() { return new Type[] { typeof(WrappedKeySecurityToken) }; } |
| | 0 | 150 | | public override string TokenTypeUri { get { return null; } } |
| | 0 | 151 | | protected override string ValueTypeUri { get { return null; } } |
| | | 152 | | } |
| | | 153 | | protected class X509TokenEntry : BinaryTokenEntry |
| | | 154 | | { |
| | | 155 | | internal const string ValueTypeAbsoluteUri = SecurityJan2004Strings.X509TokenType; |
| | | 156 | | |
| | | 157 | | public X509TokenEntry() |
| | 134 | 158 | | : base(ValueTypeAbsoluteUri) |
| | | 159 | | { |
| | 134 | 160 | | } |
| | | 161 | | |
| | 11 | 162 | | protected override Type[] GetTokenTypesCore() { return new Type[] { typeof(X509SecurityToken) }; } |
| | | 163 | | } |
| | | 164 | | |
| | | 165 | | protected class SecurityTokenReferenceJan2004ClauseEntry : KeyIdentifierClauseEntry |
| | | 166 | | { |
| | | 167 | | private const int DefaultDerivedKeyLength = 32; |
| | | 168 | | |
| | 134 | 169 | | public SecurityTokenReferenceJan2004ClauseEntry(bool emitBspRequiredAttributes, IList<StrEntry> strEntries) |
| | | 170 | | { |
| | 134 | 171 | | EmitBspRequiredAttributes = emitBspRequiredAttributes; |
| | 134 | 172 | | StrEntries = strEntries; |
| | 134 | 173 | | } |
| | 20 | 174 | | protected bool EmitBspRequiredAttributes { get; } |
| | 573 | 175 | | protected IList<StrEntry> StrEntries { get; } |
| | | 176 | | |
| | | 177 | | protected override XmlDictionaryString LocalName |
| | | 178 | | { |
| | | 179 | | get |
| | | 180 | | { |
| | 45 | 181 | | return CoreWCF.XD.SecurityJan2004Dictionary.SecurityTokenReference; |
| | | 182 | | } |
| | | 183 | | } |
| | | 184 | | |
| | | 185 | | protected override XmlDictionaryString NamespaceUri |
| | | 186 | | { |
| | | 187 | | get |
| | | 188 | | { |
| | 123 | 189 | | return CoreWCF.XD.SecurityJan2004Dictionary.Namespace; |
| | | 190 | | } |
| | | 191 | | } |
| | | 192 | | |
| | | 193 | | protected virtual string ReadTokenType(XmlDictionaryReader reader) |
| | | 194 | | { |
| | 0 | 195 | | return null; |
| | | 196 | | } |
| | | 197 | | |
| | | 198 | | public override SecurityKeyIdentifierClause ReadKeyIdentifierClauseCore(XmlDictionaryReader reader) |
| | | 199 | | { |
| | 39 | 200 | | byte[] nonce = null; |
| | 39 | 201 | | int length = 0; |
| | 39 | 202 | | if (reader.IsStartElement(CoreWCF.XD.SecurityJan2004Dictionary.SecurityTokenReference, NamespaceUri)) |
| | | 203 | | { |
| | 39 | 204 | | string nonceString = reader.GetAttribute(CoreWCF.XD.SecureConversationFeb2005Dictionary.Nonce, CoreW |
| | 39 | 205 | | if (nonceString != null) |
| | | 206 | | { |
| | 0 | 207 | | nonce = Convert.FromBase64String(nonceString); |
| | | 208 | | } |
| | | 209 | | |
| | 39 | 210 | | string lengthString = reader.GetAttribute(CoreWCF.XD.SecureConversationFeb2005Dictionary.Length, Cor |
| | 39 | 211 | | if (lengthString != null) |
| | | 212 | | { |
| | 0 | 213 | | length = Convert.ToInt32(lengthString, CultureInfo.InvariantCulture); |
| | | 214 | | } |
| | | 215 | | else |
| | | 216 | | { |
| | 39 | 217 | | length = DefaultDerivedKeyLength; |
| | | 218 | | } |
| | | 219 | | } |
| | 39 | 220 | | string tokenType = ReadTokenType(reader); |
| | 39 | 221 | | string strId = reader.GetAttribute(CoreWCF.XD.UtilityDictionary.IdAttribute, CoreWCF.XD.UtilityDictionar |
| | 39 | 222 | | reader.ReadStartElement(CoreWCF.XD.SecurityJan2004Dictionary.SecurityTokenReference, NamespaceUri); |
| | 39 | 223 | | SecurityKeyIdentifierClause clause = null; |
| | 214 | 224 | | for (int i = 0; i < StrEntries.Count; ++i) |
| | | 225 | | { |
| | 107 | 226 | | if (StrEntries[i].CanReadClause(reader, tokenType)) |
| | | 227 | | { |
| | 39 | 228 | | clause = StrEntries[i].ReadClause(reader, nonce, length, tokenType); |
| | 39 | 229 | | break; |
| | | 230 | | } |
| | | 231 | | } |
| | | 232 | | |
| | 39 | 233 | | if (clause == null) |
| | | 234 | | { |
| | 0 | 235 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.CannotReadKe |
| | | 236 | | } |
| | | 237 | | |
| | 39 | 238 | | if (!string.IsNullOrEmpty(strId)) |
| | | 239 | | { |
| | 1 | 240 | | clause.Id = strId; |
| | | 241 | | } |
| | | 242 | | |
| | 39 | 243 | | reader.ReadEndElement(); |
| | 39 | 244 | | return clause; |
| | | 245 | | } |
| | | 246 | | |
| | | 247 | | public override bool SupportsCore(SecurityKeyIdentifierClause keyIdentifierClause) |
| | | 248 | | { |
| | 140 | 249 | | for (int i = 0; i < StrEntries.Count; ++i) |
| | | 250 | | { |
| | 70 | 251 | | if (StrEntries[i].SupportsCore(keyIdentifierClause)) |
| | | 252 | | { |
| | 20 | 253 | | return true; |
| | | 254 | | } |
| | | 255 | | } |
| | 0 | 256 | | return false; |
| | | 257 | | } |
| | | 258 | | |
| | | 259 | | public override void WriteKeyIdentifierClauseCore(XmlDictionaryWriter writer, SecurityKeyIdentifierClause ke |
| | | 260 | | { |
| | 0 | 261 | | for (int i = 0; i < StrEntries.Count; ++i) |
| | | 262 | | { |
| | 0 | 263 | | if (StrEntries[i].SupportsCore(keyIdentifierClause)) |
| | | 264 | | { |
| | 0 | 265 | | writer.WriteStartElement(CoreWCF.XD.SecurityJan2004Dictionary.Prefix.Value, CoreWCF.XD.SecurityJ |
| | 0 | 266 | | StrEntries[i].WriteContent(writer, keyIdentifierClause); |
| | 0 | 267 | | writer.WriteEndElement(); |
| | 0 | 268 | | return; |
| | | 269 | | } |
| | | 270 | | } |
| | | 271 | | |
| | 0 | 272 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Sta |
| | | 273 | | } |
| | | 274 | | } |
| | | 275 | | |
| | | 276 | | protected abstract class KeyIdentifierStrEntry : StrEntry |
| | | 277 | | { |
| | | 278 | | protected const string EncodingTypeValueBase64Binary = SecurityJan2004Strings.EncodingTypeValueBase64Binary; |
| | | 279 | | protected const string EncodingTypeValueHexBinary = SecurityJan2004Strings.EncodingTypeValueHexBinary; |
| | | 280 | | protected const string EncodingTypeValueText = SecurityJan2004Strings.EncodingTypeValueText; |
| | | 281 | | |
| | | 282 | | protected abstract Type ClauseType { get; } |
| | 0 | 283 | | protected virtual string DefaultEncodingType { get { return EncodingTypeValueBase64Binary; } } |
| | | 284 | | public abstract Type TokenType { get; } |
| | | 285 | | protected abstract string ValueTypeUri { get; } |
| | 0 | 286 | | protected bool EmitBspRequiredAttributes { get; } |
| | | 287 | | |
| | 390 | 288 | | protected KeyIdentifierStrEntry(bool emitBspRequiredAttributes) |
| | | 289 | | { |
| | 390 | 290 | | EmitBspRequiredAttributes = emitBspRequiredAttributes; |
| | 390 | 291 | | } |
| | | 292 | | |
| | | 293 | | public override bool CanReadClause(XmlDictionaryReader reader, string tokenType) |
| | | 294 | | { |
| | 44 | 295 | | if (reader.IsStartElement(CoreWCF.XD.SecurityJan2004Dictionary.KeyIdentifier, CoreWCF.XD.SecurityJan2004 |
| | | 296 | | { |
| | 14 | 297 | | string valueType = reader.GetAttribute(CoreWCF.XD.SecurityJan2004Dictionary.ValueType, null); |
| | 14 | 298 | | return (ValueTypeUri == valueType); |
| | | 299 | | } |
| | 30 | 300 | | return false; |
| | | 301 | | } |
| | | 302 | | |
| | | 303 | | protected abstract SecurityKeyIdentifierClause CreateClause(byte[] bytes, byte[] derivationNonce, int deriva |
| | | 304 | | |
| | | 305 | | public override Type GetTokenType(SecurityKeyIdentifierClause clause) |
| | | 306 | | { |
| | 0 | 307 | | return TokenType; |
| | | 308 | | } |
| | | 309 | | |
| | | 310 | | public override SecurityKeyIdentifierClause ReadClause(XmlDictionaryReader reader, byte[] derivationNonce, i |
| | | 311 | | { |
| | 6 | 312 | | string encodingType = reader.GetAttribute(CoreWCF.XD.SecurityJan2004Dictionary.EncodingType, null); |
| | 6 | 313 | | if (encodingType == null) |
| | | 314 | | { |
| | 0 | 315 | | encodingType = DefaultEncodingType; |
| | | 316 | | } |
| | | 317 | | |
| | 6 | 318 | | reader.ReadStartElement(); |
| | | 319 | | |
| | | 320 | | byte[] bytes; |
| | 6 | 321 | | if (encodingType == EncodingTypeValueBase64Binary) |
| | | 322 | | { |
| | 6 | 323 | | bytes = reader.ReadContentAsBase64(); |
| | | 324 | | } |
| | 0 | 325 | | else if (encodingType == EncodingTypeValueHexBinary) |
| | | 326 | | { |
| | 0 | 327 | | bytes = HexBinary.Parse(reader.ReadContentAsString()).Value; |
| | | 328 | | } |
| | 0 | 329 | | else if (encodingType == EncodingTypeValueText) |
| | | 330 | | { |
| | 0 | 331 | | bytes = new UTF8Encoding().GetBytes(reader.ReadContentAsString()); |
| | | 332 | | } |
| | | 333 | | else |
| | | 334 | | { |
| | 0 | 335 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new Exception("UnknownEncodingInKeyIdentif |
| | | 336 | | } |
| | | 337 | | |
| | 6 | 338 | | reader.ReadEndElement(); |
| | | 339 | | |
| | 6 | 340 | | return CreateClause(bytes, derivationNonce, derivationLength); |
| | | 341 | | } |
| | | 342 | | |
| | | 343 | | public override bool SupportsCore(SecurityKeyIdentifierClause clause) |
| | | 344 | | { |
| | 60 | 345 | | return ClauseType.IsAssignableFrom(clause.GetType()); |
| | | 346 | | } |
| | | 347 | | |
| | | 348 | | public override void WriteContent(XmlDictionaryWriter writer, SecurityKeyIdentifierClause clause) |
| | | 349 | | { |
| | 0 | 350 | | writer.WriteStartElement(CoreWCF.XD.SecurityJan2004Dictionary.Prefix.Value, CoreWCF.XD.SecurityJan2004Di |
| | 0 | 351 | | writer.WriteAttributeString(CoreWCF.XD.SecurityJan2004Dictionary.ValueType, null, ValueTypeUri); |
| | 0 | 352 | | if (EmitBspRequiredAttributes) |
| | | 353 | | { |
| | | 354 | | // Emit the encodingType attribute. |
| | 0 | 355 | | writer.WriteAttributeString(CoreWCF.XD.SecurityJan2004Dictionary.EncodingType, null, DefaultEncoding |
| | | 356 | | } |
| | 0 | 357 | | string encoding = DefaultEncodingType; |
| | 0 | 358 | | BinaryKeyIdentifierClause binaryClause = clause as BinaryKeyIdentifierClause; |
| | | 359 | | |
| | 0 | 360 | | byte[] keyIdentifier = binaryClause.GetBuffer(); |
| | 0 | 361 | | if (encoding == EncodingTypeValueBase64Binary) |
| | | 362 | | { |
| | 0 | 363 | | writer.WriteBase64(keyIdentifier, 0, keyIdentifier.Length); |
| | | 364 | | } |
| | 0 | 365 | | else if (encoding == EncodingTypeValueHexBinary) |
| | | 366 | | { |
| | 0 | 367 | | writer.WriteBinHex(keyIdentifier, 0, keyIdentifier.Length); |
| | | 368 | | } |
| | 0 | 369 | | else if (encoding == EncodingTypeValueText) |
| | | 370 | | { |
| | 0 | 371 | | writer.WriteString(new UTF8Encoding().GetString(keyIdentifier, 0, keyIdentifier.Length)); |
| | | 372 | | } |
| | 0 | 373 | | writer.WriteEndElement(); |
| | 0 | 374 | | } |
| | | 375 | | } |
| | | 376 | | |
| | | 377 | | protected class X509SkiStrEntry : KeyIdentifierStrEntry |
| | | 378 | | { |
| | 20 | 379 | | protected override Type ClauseType { get { return typeof(X509SubjectKeyIdentifierClause); } } |
| | 0 | 380 | | public override Type TokenType { get { return typeof(X509SecurityToken); } } |
| | 6 | 381 | | protected override string ValueTypeUri { get { return SecurityJan2004Strings.X509SKIValueType; } } |
| | | 382 | | |
| | | 383 | | public X509SkiStrEntry(bool emitBspRequiredAttributes) |
| | 134 | 384 | | : base(emitBspRequiredAttributes) |
| | | 385 | | { |
| | 134 | 386 | | } |
| | | 387 | | |
| | | 388 | | protected override SecurityKeyIdentifierClause CreateClause(byte[] bytes, byte[] derivationNonce, int deriva |
| | | 389 | | { |
| | 2 | 390 | | return new X509SubjectKeyIdentifierClause(bytes); |
| | | 391 | | } |
| | | 392 | | public override string GetTokenTypeUri() |
| | | 393 | | { |
| | 0 | 394 | | return SecurityJan2004Strings.X509TokenType; |
| | | 395 | | } |
| | | 396 | | } |
| | | 397 | | |
| | | 398 | | protected class LocalReferenceStrEntry : StrEntry |
| | | 399 | | { |
| | | 400 | | private readonly bool _emitBspRequiredAttributes; |
| | | 401 | | private readonly KeyInfoSerializer _tokenSerializer; |
| | | 402 | | |
| | 134 | 403 | | public LocalReferenceStrEntry(bool emitBspRequiredAttributes, KeyInfoSerializer tokenSerializer) |
| | | 404 | | { |
| | 134 | 405 | | _emitBspRequiredAttributes = emitBspRequiredAttributes; |
| | 134 | 406 | | _tokenSerializer = tokenSerializer; |
| | 134 | 407 | | } |
| | | 408 | | |
| | | 409 | | public override Type GetTokenType(SecurityKeyIdentifierClause clause) |
| | | 410 | | { |
| | 16 | 411 | | LocalIdKeyIdentifierClause localClause = clause as LocalIdKeyIdentifierClause; |
| | 16 | 412 | | return localClause.OwnerType; |
| | | 413 | | } |
| | | 414 | | |
| | | 415 | | public string GetLocalTokenTypeUri(SecurityKeyIdentifierClause clause) |
| | | 416 | | { |
| | 16 | 417 | | Type tokenType = GetTokenType(clause); |
| | 16 | 418 | | return _tokenSerializer.GetTokenTypeUri(tokenType); |
| | | 419 | | } |
| | | 420 | | public override string GetTokenTypeUri() |
| | | 421 | | { |
| | 0 | 422 | | return null; |
| | | 423 | | } |
| | | 424 | | |
| | | 425 | | public override bool CanReadClause(XmlDictionaryReader reader, string tokenType) |
| | | 426 | | { |
| | 39 | 427 | | if (reader.IsStartElement(CoreWCF.XD.SecurityJan2004Dictionary.Reference, CoreWCF.XD.SecurityJan2004Dict |
| | | 428 | | { |
| | 33 | 429 | | string uri = reader.GetAttribute(CoreWCF.XD.SecurityJan2004Dictionary.URI, null); |
| | 33 | 430 | | if (uri != null && uri.Length > 0 && uri[0] == '#') |
| | | 431 | | { |
| | 23 | 432 | | return true; |
| | | 433 | | } |
| | | 434 | | } |
| | 16 | 435 | | return false; |
| | | 436 | | } |
| | | 437 | | |
| | | 438 | | public override SecurityKeyIdentifierClause ReadClause(XmlDictionaryReader reader, byte[] derivationNonce, i |
| | | 439 | | { |
| | 23 | 440 | | string uri = reader.GetAttribute(CoreWCF.XD.SecurityJan2004Dictionary.URI, null); |
| | 23 | 441 | | string tokenTypeUri = reader.GetAttribute(CoreWCF.XD.SecurityJan2004Dictionary.ValueType, null); |
| | 23 | 442 | | Type[] tokenTypes = null; |
| | 23 | 443 | | if (tokenTypeUri != null) |
| | | 444 | | { |
| | 19 | 445 | | tokenTypes = _tokenSerializer.GetTokenTypes(tokenTypeUri); |
| | | 446 | | } |
| | 23 | 447 | | SecurityKeyIdentifierClause clause = new LocalIdKeyIdentifierClause(uri.Substring(1), derivationNonce, d |
| | 23 | 448 | | if (reader.IsEmptyElement) |
| | | 449 | | { |
| | 23 | 450 | | reader.Read(); |
| | | 451 | | } |
| | | 452 | | else |
| | | 453 | | { |
| | 0 | 454 | | reader.ReadStartElement(); |
| | 0 | 455 | | reader.ReadEndElement(); |
| | | 456 | | } |
| | 0 | 457 | | return clause; |
| | | 458 | | } |
| | | 459 | | |
| | | 460 | | public override bool SupportsCore(SecurityKeyIdentifierClause clause) |
| | | 461 | | { |
| | 40 | 462 | | return clause is LocalIdKeyIdentifierClause; |
| | | 463 | | } |
| | | 464 | | |
| | | 465 | | public override void WriteContent(XmlDictionaryWriter writer, SecurityKeyIdentifierClause clause) |
| | | 466 | | { |
| | 10 | 467 | | LocalIdKeyIdentifierClause localIdClause = clause as LocalIdKeyIdentifierClause; |
| | 10 | 468 | | writer.WriteStartElement(CoreWCF.XD.SecurityJan2004Dictionary.Prefix.Value, CoreWCF.XD.SecurityJan2004Di |
| | 10 | 469 | | if (_emitBspRequiredAttributes) |
| | | 470 | | { |
| | 8 | 471 | | string tokenTypeUri = GetLocalTokenTypeUri(localIdClause); |
| | 8 | 472 | | if (tokenTypeUri != null) |
| | | 473 | | { |
| | 8 | 474 | | writer.WriteAttributeString(CoreWCF.XD.SecurityJan2004Dictionary.ValueType, null, tokenTypeUri); |
| | | 475 | | } |
| | | 476 | | } |
| | 10 | 477 | | writer.WriteAttributeString(CoreWCF.XD.SecurityJan2004Dictionary.URI, null, "#" + localIdClause.LocalId) |
| | 10 | 478 | | writer.WriteEndElement(); |
| | 10 | 479 | | } |
| | | 480 | | } |
| | | 481 | | |
| | | 482 | | protected class X509IssuerSerialStrEntry : StrEntry |
| | | 483 | | { |
| | | 484 | | public override Type GetTokenType(SecurityKeyIdentifierClause clause) |
| | | 485 | | { |
| | 0 | 486 | | return typeof(X509SecurityToken); |
| | | 487 | | } |
| | | 488 | | |
| | | 489 | | public override bool CanReadClause(XmlDictionaryReader reader, string tokenType) |
| | | 490 | | { |
| | 14 | 491 | | return reader.IsStartElement(CoreWCF.XD.XmlSignatureDictionary.X509Data, CoreWCF.XD.XmlSignatureDictiona |
| | | 492 | | } |
| | | 493 | | public override string GetTokenTypeUri() |
| | | 494 | | { |
| | 0 | 495 | | return SecurityJan2004Strings.X509TokenType; |
| | | 496 | | } |
| | | 497 | | |
| | | 498 | | public override SecurityKeyIdentifierClause ReadClause(XmlDictionaryReader reader, byte[] derivationNonce, i |
| | | 499 | | { |
| | 0 | 500 | | reader.ReadStartElement(CoreWCF.XD.XmlSignatureDictionary.X509Data, CoreWCF.XD.XmlSignatureDictionary.Na |
| | 0 | 501 | | reader.ReadStartElement(CoreWCF.XD.XmlSignatureDictionary.X509IssuerSerial, CoreWCF.XD.XmlSignatureDicti |
| | 0 | 502 | | reader.ReadStartElement(CoreWCF.XD.XmlSignatureDictionary.X509IssuerName, CoreWCF.XD.XmlSignatureDiction |
| | 0 | 503 | | string issuerName = reader.ReadContentAsString(); |
| | 0 | 504 | | reader.ReadEndElement(); |
| | 0 | 505 | | reader.ReadStartElement(CoreWCF.XD.XmlSignatureDictionary.X509SerialNumber, CoreWCF.XD.XmlSignatureDicti |
| | 0 | 506 | | string serialNumber = reader.ReadContentAsString(); |
| | 0 | 507 | | reader.ReadEndElement(); |
| | 0 | 508 | | reader.ReadEndElement(); |
| | 0 | 509 | | reader.ReadEndElement(); |
| | | 510 | | |
| | 0 | 511 | | return new X509IssuerSerialKeyIdentifierClause(issuerName, serialNumber); |
| | | 512 | | } |
| | | 513 | | |
| | | 514 | | public override bool SupportsCore(SecurityKeyIdentifierClause clause) |
| | | 515 | | { |
| | 20 | 516 | | return clause is X509IssuerSerialKeyIdentifierClause; |
| | | 517 | | } |
| | | 518 | | |
| | | 519 | | public override void WriteContent(XmlDictionaryWriter writer, SecurityKeyIdentifierClause clause) |
| | | 520 | | { |
| | 0 | 521 | | X509IssuerSerialKeyIdentifierClause issuerClause = clause as X509IssuerSerialKeyIdentifierClause; |
| | 0 | 522 | | writer.WriteStartElement(CoreWCF.XD.XmlSignatureDictionary.Prefix.Value, CoreWCF.XD.XmlSignatureDictiona |
| | 0 | 523 | | writer.WriteStartElement(CoreWCF.XD.XmlSignatureDictionary.Prefix.Value, CoreWCF.XD.XmlSignatureDictiona |
| | 0 | 524 | | writer.WriteElementString(CoreWCF.XD.XmlSignatureDictionary.Prefix.Value, CoreWCF.XD.XmlSignatureDiction |
| | 0 | 525 | | writer.WriteElementString(CoreWCF.XD.XmlSignatureDictionary.Prefix.Value, CoreWCF.XD.XmlSignatureDiction |
| | 0 | 526 | | writer.WriteEndElement(); |
| | 0 | 527 | | writer.WriteEndElement(); |
| | 0 | 528 | | } |
| | | 529 | | } |
| | | 530 | | |
| | | 531 | | public class IdManager : SignatureTargetIdManager |
| | | 532 | | { |
| | 0 | 533 | | internal static readonly XmlDictionaryString ElementName = CoreWCF.XD.XmlEncryptionDictionary.EncryptedData; |
| | | 534 | | |
| | 0 | 535 | | private IdManager() |
| | | 536 | | { |
| | 0 | 537 | | } |
| | | 538 | | |
| | | 539 | | public override string DefaultIdNamespacePrefix |
| | | 540 | | { |
| | 0 | 541 | | get { return UtilityStrings.Prefix; } |
| | | 542 | | } |
| | | 543 | | |
| | | 544 | | public override string DefaultIdNamespaceUri |
| | | 545 | | { |
| | 0 | 546 | | get { return UtilityStrings.Namespace; } |
| | | 547 | | } |
| | | 548 | | |
| | 0 | 549 | | internal static IdManager Instance { get; } = new IdManager(); |
| | | 550 | | |
| | | 551 | | public override string ExtractId(XmlDictionaryReader reader) |
| | | 552 | | { |
| | 0 | 553 | | if (reader == null) |
| | | 554 | | { |
| | 0 | 555 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader)); |
| | | 556 | | } |
| | | 557 | | |
| | 0 | 558 | | if (reader.IsStartElement(ElementName, CoreWCF.XD.XmlEncryptionDictionary.Namespace)) |
| | | 559 | | { |
| | 0 | 560 | | return reader.GetAttribute(CoreWCF.XD.XmlEncryptionDictionary.Id, null); |
| | | 561 | | } |
| | | 562 | | else |
| | | 563 | | { |
| | 0 | 564 | | return reader.GetAttribute(CoreWCF.XD.UtilityDictionary.IdAttribute, CoreWCF.XD.UtilityDictionary.Na |
| | | 565 | | } |
| | | 566 | | } |
| | | 567 | | |
| | | 568 | | public override void WriteIdAttribute(XmlDictionaryWriter writer, string id) |
| | | 569 | | { |
| | 0 | 570 | | if (writer == null) |
| | | 571 | | { |
| | 0 | 572 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(writer)); |
| | | 573 | | } |
| | | 574 | | |
| | 0 | 575 | | writer.WriteAttributeString(CoreWCF.XD.UtilityDictionary.Prefix.Value, CoreWCF.XD.UtilityDictionary.IdAt |
| | 0 | 576 | | } |
| | | 577 | | } |
| | | 578 | | } |
| | | 579 | | } |