| | | 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.Security.Tokens; |
| | | 8 | | using static CoreWCF.IdentityModel.Selectors.SecurityTokenSerializer; |
| | | 9 | | |
| | | 10 | | namespace CoreWCF.IdentityModel.Tokens |
| | | 11 | | { |
| | | 12 | | internal class WSSecurityXXX2005 : WSSecurityJan2004 |
| | | 13 | | { |
| | | 14 | | public WSSecurityXXX2005(KeyInfoSerializer securityTokenSerializer) |
| | 128 | 15 | | : base(securityTokenSerializer) |
| | | 16 | | { |
| | 128 | 17 | | } |
| | | 18 | | |
| | | 19 | | public override void PopulateStrEntries(IList<StrEntry> strEntries) |
| | | 20 | | { |
| | 128 | 21 | | PopulateJan2004StrEntries(strEntries); |
| | | 22 | | // strEntries.Add(new SamlDirectStrEntry()); |
| | 128 | 23 | | strEntries.Add(new X509ThumbprintStrEntry(SecurityTokenSerializer.EmitBspRequiredAttributes)); |
| | 128 | 24 | | strEntries.Add(new EncryptedKeyHashStrEntry(SecurityTokenSerializer.EmitBspRequiredAttributes)); |
| | 128 | 25 | | } |
| | | 26 | | |
| | | 27 | | public override void PopulateTokenEntries(IList<TokenEntry> tokenEntryList) |
| | | 28 | | { |
| | 128 | 29 | | PopulateJan2004TokenEntries(tokenEntryList); |
| | 128 | 30 | | tokenEntryList.Add(new WSSecurityXXX2005.WrappedKeyTokenEntry()); |
| | | 31 | | // tokenEntryList.Add(new WSSecurityXXX2005.SamlTokenEntry()); |
| | 128 | 32 | | } |
| | | 33 | | |
| | | 34 | | public override void PopulateKeyIdentifierClauseEntries(IList<KeyIdentifierClauseEntry> clauseEntries) |
| | | 35 | | { |
| | 128 | 36 | | List<StrEntry> strEntries = new List<StrEntry>(); |
| | 128 | 37 | | SecurityTokenSerializer.PopulateStrEntries(strEntries); |
| | 128 | 38 | | SecurityTokenReferenceXXX2005ClauseEntry strClause = new SecurityTokenReferenceXXX2005ClauseEntry(SecurityTo |
| | 128 | 39 | | clauseEntries.Add(strClause); |
| | 128 | 40 | | } |
| | | 41 | | |
| | | 42 | | private new class WrappedKeyTokenEntry : WSSecurityJan2004.WrappedKeyTokenEntry |
| | | 43 | | { |
| | 16 | 44 | | public override string TokenTypeUri { get { return SecurityXXX2005Strings.EncryptedKeyTokenType; } } |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | private class SecurityTokenReferenceXXX2005ClauseEntry : SecurityTokenReferenceJan2004ClauseEntry |
| | | 48 | | { |
| | | 49 | | public SecurityTokenReferenceXXX2005ClauseEntry(bool emitBspRequiredAttributes, IList<StrEntry> strEntries) |
| | 128 | 50 | | : base(emitBspRequiredAttributes, strEntries) |
| | | 51 | | { |
| | 128 | 52 | | } |
| | | 53 | | |
| | | 54 | | protected override string ReadTokenType(XmlDictionaryReader reader) |
| | | 55 | | { |
| | 39 | 56 | | return reader.GetAttribute(CoreWCF.XD.SecurityXXX2005Dictionary.TokenTypeAttribute, CoreWCF.XD.SecurityX |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | public override void WriteKeyIdentifierClauseCore(XmlDictionaryWriter writer, SecurityKeyIdentifierClause ke |
| | | 60 | | { |
| | 140 | 61 | | for (int i = 0; i < StrEntries.Count; ++i) |
| | | 62 | | { |
| | 70 | 63 | | if (StrEntries[i].SupportsCore(keyIdentifierClause)) |
| | | 64 | | { |
| | 20 | 65 | | writer.WriteStartElement(CoreWCF.XD.SecurityJan2004Dictionary.Prefix.Value, CoreWCF.XD.SecurityJ |
| | | 66 | | |
| | 20 | 67 | | string tokenTypeUri = GetTokenTypeUri(StrEntries[i], keyIdentifierClause); |
| | 20 | 68 | | if (tokenTypeUri != null) |
| | | 69 | | { |
| | 0 | 70 | | writer.WriteAttributeString(CoreWCF.XD.SecurityXXX2005Dictionary.Prefix.Value, CoreWCF.XD.Se |
| | | 71 | | } |
| | | 72 | | |
| | 20 | 73 | | StrEntries[i].WriteContent(writer, keyIdentifierClause); |
| | 20 | 74 | | writer.WriteEndElement(); |
| | 20 | 75 | | return; |
| | | 76 | | } |
| | | 77 | | } |
| | 0 | 78 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Sta |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | private string GetTokenTypeUri(StrEntry str, SecurityKeyIdentifierClause keyIdentifierClause) |
| | | 82 | | { |
| | 20 | 83 | | bool emitTokenType = EmitTokenType(str); |
| | 20 | 84 | | if (emitTokenType) |
| | | 85 | | { |
| | | 86 | | string tokenTypeUri; |
| | 8 | 87 | | if (str is LocalReferenceStrEntry) |
| | | 88 | | { |
| | 8 | 89 | | tokenTypeUri = (str as LocalReferenceStrEntry).GetLocalTokenTypeUri(keyIdentifierClause); |
| | | 90 | | // only emit token type for SAML,Kerberos and Encrypted References |
| | | 91 | | switch (tokenTypeUri) |
| | | 92 | | { |
| | | 93 | | case SecurityXXX2005Strings.Saml20TokenType: |
| | | 94 | | case SecurityXXX2005Strings.SamlTokenType: |
| | | 95 | | case SecurityXXX2005Strings.EncryptedKeyTokenType: |
| | | 96 | | case SecurityJan2004Strings.KerberosTokenTypeGSS: break; |
| | | 97 | | |
| | | 98 | | default: |
| | 8 | 99 | | tokenTypeUri = null; |
| | 8 | 100 | | break; |
| | | 101 | | } |
| | | 102 | | } |
| | | 103 | | else |
| | | 104 | | { |
| | 0 | 105 | | tokenTypeUri = str.GetTokenTypeUri(); |
| | | 106 | | } |
| | | 107 | | |
| | 8 | 108 | | return tokenTypeUri; |
| | | 109 | | } |
| | | 110 | | else |
| | | 111 | | { |
| | 12 | 112 | | return null; |
| | | 113 | | } |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | private bool EmitTokenType(StrEntry str) |
| | | 117 | | { |
| | 20 | 118 | | bool emitTokenType = false; |
| | | 119 | | // we emit tokentype always for SAML and Encrypted Key Tokens |
| | 20 | 120 | | if ( |
| | 20 | 121 | | //(str is SamlJan2004KeyIdentifierStrEntry) |
| | 20 | 122 | | //|| |
| | 20 | 123 | | (str is EncryptedKeyHashStrEntry) |
| | 20 | 124 | | // || (str is SamlDirectStrEntry) |
| | 20 | 125 | | ) |
| | | 126 | | { |
| | 0 | 127 | | emitTokenType = true; |
| | | 128 | | } |
| | 20 | 129 | | else if (EmitBspRequiredAttributes) |
| | | 130 | | { |
| | 16 | 131 | | if ( |
| | 16 | 132 | | //(str is KerberosHashStrEntry) |
| | 16 | 133 | | //|| |
| | 16 | 134 | | (str is LocalReferenceStrEntry)) |
| | | 135 | | { |
| | 8 | 136 | | emitTokenType = true; |
| | | 137 | | } |
| | | 138 | | } |
| | 20 | 139 | | return emitTokenType; |
| | | 140 | | } |
| | | 141 | | } |
| | | 142 | | |
| | | 143 | | private class EncryptedKeyHashStrEntry : WSSecurityJan2004.KeyIdentifierStrEntry |
| | | 144 | | { |
| | 20 | 145 | | protected override Type ClauseType { get { return typeof(EncryptedKeyHashIdentifierClause); } } |
| | 0 | 146 | | public override Type TokenType { get { return typeof(WrappedKeySecurityToken); } } |
| | 4 | 147 | | protected override string ValueTypeUri { get { return SecurityXXX2005Strings.EncryptedKeyHashValueType; } } |
| | | 148 | | |
| | | 149 | | public EncryptedKeyHashStrEntry(bool emitBspRequiredAttributes) |
| | 128 | 150 | | : base(emitBspRequiredAttributes) |
| | | 151 | | { |
| | 128 | 152 | | } |
| | | 153 | | |
| | | 154 | | public override bool CanReadClause(XmlDictionaryReader reader, string tokenType) |
| | | 155 | | { |
| | | 156 | | // Backward compatible with V1. Accept if missing. |
| | 14 | 157 | | if (tokenType != null && tokenType != SecurityXXX2005Strings.EncryptedKeyTokenType) |
| | | 158 | | { |
| | 0 | 159 | | return false; |
| | | 160 | | } |
| | 14 | 161 | | return base.CanReadClause(reader, tokenType); |
| | | 162 | | } |
| | | 163 | | |
| | | 164 | | protected override SecurityKeyIdentifierClause CreateClause(byte[] bytes, byte[] derivationNonce, int deriva |
| | | 165 | | { |
| | 4 | 166 | | return new EncryptedKeyHashIdentifierClause(bytes, true, derivationNonce, derivationLength); |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | public override string GetTokenTypeUri() |
| | | 170 | | { |
| | 0 | 171 | | return SecurityXXX2005Strings.EncryptedKeyTokenType; |
| | | 172 | | } |
| | | 173 | | } |
| | | 174 | | |
| | | 175 | | private class X509ThumbprintStrEntry : WSSecurityJan2004.KeyIdentifierStrEntry |
| | | 176 | | { |
| | 20 | 177 | | protected override Type ClauseType { get { return typeof(X509ThumbprintKeyIdentifierClause); } } |
| | 0 | 178 | | public override Type TokenType { get { return typeof(X509SecurityToken); } } |
| | 4 | 179 | | protected override string ValueTypeUri { get { return SecurityXXX2005Strings.ThumbprintSha1ValueType; } } |
| | | 180 | | |
| | | 181 | | public X509ThumbprintStrEntry(bool emitBspRequiredAttributes) |
| | 128 | 182 | | : base(emitBspRequiredAttributes) |
| | | 183 | | { |
| | 128 | 184 | | } |
| | | 185 | | |
| | | 186 | | protected override SecurityKeyIdentifierClause CreateClause(byte[] bytes, byte[] derivationNonce, int deriva |
| | | 187 | | { |
| | 0 | 188 | | return new X509ThumbprintKeyIdentifierClause(bytes); |
| | | 189 | | } |
| | | 190 | | public override string GetTokenTypeUri() |
| | | 191 | | { |
| | 0 | 192 | | return CoreWCF.XD.SecurityXXX2005Dictionary.ThumbprintSha1ValueType.Value; |
| | | 193 | | } |
| | | 194 | | } |
| | | 195 | | |
| | | 196 | | //class SamlDirectStrEntry : StrEntry |
| | | 197 | | //{ |
| | | 198 | | // public override bool CanReadClause(XmlDictionaryReader reader, string tokenType) |
| | | 199 | | // { |
| | | 200 | | // if (tokenType != CoreWCF.XD.SecurityXXX2005Dictionary.Saml20TokenType.Value) |
| | | 201 | | // { |
| | | 202 | | // return false; |
| | | 203 | | // } |
| | | 204 | | // return (reader.IsStartElement(CoreWCF.XD.SecurityJan2004Dictionary.Reference, CoreWCF.XD.SecurityJan20 |
| | | 205 | | // } |
| | | 206 | | |
| | | 207 | | // public override Type GetTokenType(SecurityKeyIdentifierClause clause) |
| | | 208 | | // { |
| | | 209 | | // return null; |
| | | 210 | | // } |
| | | 211 | | |
| | | 212 | | // public override string GetTokenTypeUri() |
| | | 213 | | // { |
| | | 214 | | // return CoreWCF.XD.SecurityXXX2005Dictionary.Saml20TokenType.Value; |
| | | 215 | | // } |
| | | 216 | | |
| | | 217 | | // public override SecurityKeyIdentifierClause ReadClause(XmlDictionaryReader reader, byte[] derivationNone, |
| | | 218 | | // { |
| | | 219 | | // string samlUri = reader.GetAttribute(CoreWCF.XD.SecurityJan2004Dictionary.URI, null); |
| | | 220 | | // if (reader.IsEmptyElement) |
| | | 221 | | // { |
| | | 222 | | // reader.Read(); |
| | | 223 | | // } |
| | | 224 | | // else |
| | | 225 | | // { |
| | | 226 | | // reader.ReadStartElement(); |
| | | 227 | | // reader.ReadEndElement(); |
| | | 228 | | // } |
| | | 229 | | // return new SamlAssertionDirectKeyIdentifierClause(samlUri, derivationNone, derivationLength); |
| | | 230 | | // } |
| | | 231 | | |
| | | 232 | | // public override bool SupportsCore(SecurityKeyIdentifierClause clause) |
| | | 233 | | // { |
| | | 234 | | // return typeof(SamlAssertionDirectKeyIdentifierClause).IsAssignableFrom(clause.GetType()); |
| | | 235 | | // } |
| | | 236 | | |
| | | 237 | | // public override void WriteContent(XmlDictionaryWriter writer, SecurityKeyIdentifierClause clause) |
| | | 238 | | // { |
| | | 239 | | // SamlAssertionDirectKeyIdentifierClause samlClause = clause as SamlAssertionDirectKeyIdentifierClause; |
| | | 240 | | // writer.WriteStartElement(CoreWCF.XD.SecurityJan2004Dictionary.Prefix.Value, CoreWCF.XD.SecurityJan2004 |
| | | 241 | | // writer.WriteAttributeString(CoreWCF.XD.SecurityJan2004Dictionary.URI, null, samlClause.SamlUri); |
| | | 242 | | // writer.WriteEndElement(); |
| | | 243 | | // } |
| | | 244 | | //} |
| | | 245 | | } |
| | | 246 | | } |