| | | 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 | | namespace CoreWCF.IdentityModel.Security |
| | | 5 | | { |
| | | 6 | | using System; |
| | | 7 | | using System.Collections.Generic; |
| | | 8 | | using System.Xml; |
| | | 9 | | using CoreWCF.IdentityModel; |
| | | 10 | | using CoreWCF.IdentityModel.Selectors; |
| | | 11 | | using CoreWCF.IdentityModel.Tokens; |
| | | 12 | | using CoreWCF.Security; |
| | | 13 | | using CoreWCF.Security.Tokens; |
| | | 14 | | using KeyIdentifierClauseEntry = CoreWCF.IdentityModel.Selectors.SecurityTokenSerializer.KeyIdentifierClauseEntry; |
| | | 15 | | |
| | | 16 | | internal class WSTrust : SecurityTokenSerializer.SerializerEntries |
| | | 17 | | { |
| | | 18 | | private readonly KeyInfoSerializer _securityTokenSerializer; |
| | | 19 | | |
| | 134 | 20 | | public WSTrust(KeyInfoSerializer securityTokenSerializer, TrustDictionary serializerDictionary) |
| | | 21 | | { |
| | 134 | 22 | | _securityTokenSerializer = securityTokenSerializer; |
| | 134 | 23 | | SerializerDictionary = serializerDictionary; |
| | 134 | 24 | | } |
| | | 25 | | |
| | 403 | 26 | | public TrustDictionary SerializerDictionary { get; } |
| | | 27 | | |
| | | 28 | | public override void PopulateTokenEntries(IList<SecurityTokenSerializer.TokenEntry> tokenEntryList) |
| | | 29 | | { |
| | 134 | 30 | | tokenEntryList.Add(new BinarySecretTokenEntry(this)); |
| | 134 | 31 | | } |
| | | 32 | | |
| | | 33 | | public override void PopulateKeyIdentifierClauseEntries(IList<SecurityTokenSerializer.KeyIdentifierClauseEntry> |
| | | 34 | | { |
| | 134 | 35 | | keyIdentifierClauseEntries.Add(new BinarySecretClauseEntry(this)); |
| | 134 | 36 | | keyIdentifierClauseEntries.Add(new GenericXmlSecurityKeyIdentifierClauseEntry(this)); |
| | 134 | 37 | | } |
| | | 38 | | |
| | | 39 | | private class BinarySecretTokenEntry : SecurityTokenSerializer.TokenEntry |
| | | 40 | | { |
| | | 41 | | private readonly WSTrust _parent; |
| | | 42 | | |
| | 134 | 43 | | public BinarySecretTokenEntry(WSTrust parent) |
| | | 44 | | { |
| | 134 | 45 | | _parent = parent; |
| | 134 | 46 | | } |
| | | 47 | | |
| | 0 | 48 | | protected override XmlDictionaryString LocalName { get { return _parent.SerializerDictionary.BinarySecret; } |
| | 0 | 49 | | protected override XmlDictionaryString NamespaceUri { get { return _parent.SerializerDictionary.Namespace; } |
| | 8 | 50 | | protected override Type[] GetTokenTypesCore() { return new Type[] { typeof(BinarySecretSecurityToken) }; } |
| | 19 | 51 | | public override string TokenTypeUri { get { return null; } } |
| | 0 | 52 | | protected override string ValueTypeUri { get { return null; } } |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | internal class BinarySecretClauseEntry : KeyIdentifierClauseEntry |
| | | 56 | | { |
| | | 57 | | private readonly WSTrust _parent; |
| | | 58 | | private readonly TrustDictionary _otherDictionary = null; |
| | | 59 | | |
| | 134 | 60 | | public BinarySecretClauseEntry(WSTrust parent) |
| | | 61 | | { |
| | 134 | 62 | | _parent = parent; |
| | | 63 | | |
| | 134 | 64 | | _otherDictionary = null; |
| | | 65 | | |
| | 134 | 66 | | if (parent.SerializerDictionary is TrustDec2005Dictionary) |
| | | 67 | | { |
| | 134 | 68 | | _otherDictionary = parent._securityTokenSerializer.DictionaryManager.TrustFeb2005Dictionary; |
| | | 69 | | } |
| | | 70 | | |
| | 134 | 71 | | if (parent.SerializerDictionary is TrustFeb2005Dictionary) |
| | | 72 | | { |
| | 0 | 73 | | _otherDictionary = parent._securityTokenSerializer.DictionaryManager.TrustDec2005Dictionary; |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | // always set it, so we don't have to worry about null |
| | 134 | 77 | | if (_otherDictionary == null) |
| | | 78 | | { |
| | 0 | 79 | | _otherDictionary = _parent.SerializerDictionary; |
| | | 80 | | } |
| | 134 | 81 | | } |
| | | 82 | | |
| | | 83 | | protected override XmlDictionaryString LocalName |
| | | 84 | | { |
| | 90 | 85 | | get { return _parent.SerializerDictionary.BinarySecret; } |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | protected override XmlDictionaryString NamespaceUri |
| | | 89 | | { |
| | 45 | 90 | | get { return _parent.SerializerDictionary.Namespace; } |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | public override SecurityKeyIdentifierClause ReadKeyIdentifierClauseCore(XmlDictionaryReader reader) |
| | | 94 | | { |
| | 0 | 95 | | byte[] secret = reader.ReadElementContentAsBase64(); |
| | 0 | 96 | | return new BinarySecretKeyIdentifierClause(secret, false); |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | public override bool SupportsCore(SecurityKeyIdentifierClause keyIdentifierClause) |
| | | 100 | | { |
| | 20 | 101 | | return keyIdentifierClause is BinarySecretKeyIdentifierClause; |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | public override bool CanReadKeyIdentifierClauseCore(XmlDictionaryReader reader) |
| | | 105 | | { |
| | 45 | 106 | | return (reader.IsStartElement(LocalName, NamespaceUri) || reader.IsStartElement(LocalName, _otherDiction |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | public override void WriteKeyIdentifierClauseCore(XmlDictionaryWriter writer, SecurityKeyIdentifierClause ke |
| | | 110 | | { |
| | 0 | 111 | | BinarySecretKeyIdentifierClause skic = keyIdentifierClause as BinarySecretKeyIdentifierClause; |
| | 0 | 112 | | byte[] secret = skic.GetKeyBytes(); |
| | 0 | 113 | | writer.WriteStartElement(_parent.SerializerDictionary.Prefix.Value, _parent.SerializerDictionary.BinaryS |
| | 0 | 114 | | writer.WriteBase64(secret, 0, secret.Length); |
| | 0 | 115 | | writer.WriteEndElement(); |
| | 0 | 116 | | } |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | internal class GenericXmlSecurityKeyIdentifierClauseEntry : KeyIdentifierClauseEntry |
| | | 120 | | { |
| | | 121 | | private readonly WSTrust _parent; |
| | | 122 | | |
| | 134 | 123 | | public GenericXmlSecurityKeyIdentifierClauseEntry(WSTrust parent) |
| | | 124 | | { |
| | 134 | 125 | | _parent = parent; |
| | 134 | 126 | | } |
| | | 127 | | |
| | | 128 | | protected override XmlDictionaryString LocalName |
| | | 129 | | { |
| | 0 | 130 | | get { return null; } |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | protected override XmlDictionaryString NamespaceUri |
| | | 134 | | { |
| | 0 | 135 | | get { return null; } |
| | | 136 | | } |
| | | 137 | | |
| | | 138 | | public override bool CanReadKeyIdentifierClauseCore(XmlDictionaryReader reader) |
| | | 139 | | { |
| | 45 | 140 | | return false; |
| | | 141 | | } |
| | | 142 | | |
| | | 143 | | public override SecurityKeyIdentifierClause ReadKeyIdentifierClauseCore(XmlDictionaryReader reader) |
| | | 144 | | { |
| | 0 | 145 | | return null; |
| | | 146 | | } |
| | | 147 | | |
| | | 148 | | public override bool SupportsCore(SecurityKeyIdentifierClause keyIdentifierClause) |
| | | 149 | | { |
| | 20 | 150 | | return keyIdentifierClause is GenericXmlSecurityKeyIdentifierClause; |
| | | 151 | | } |
| | | 152 | | |
| | | 153 | | public override void WriteKeyIdentifierClauseCore(XmlDictionaryWriter writer, SecurityKeyIdentifierClause ke |
| | | 154 | | { |
| | 0 | 155 | | GenericXmlSecurityKeyIdentifierClause genericXmlSecurityKeyIdentifierClause = keyIdentifierClause as Gen |
| | 0 | 156 | | genericXmlSecurityKeyIdentifierClause.ReferenceXml.WriteTo(writer); |
| | 0 | 157 | | } |
| | | 158 | | } |
| | | 159 | | |
| | | 160 | | protected static bool CheckElement(XmlElement element, string name, string ns, out string value) |
| | | 161 | | { |
| | 0 | 162 | | value = null; |
| | 0 | 163 | | if (element.LocalName != name || element.NamespaceURI != ns) |
| | | 164 | | { |
| | 0 | 165 | | return false; |
| | | 166 | | } |
| | | 167 | | |
| | 0 | 168 | | if (element.FirstChild is XmlText) |
| | | 169 | | { |
| | 0 | 170 | | value = ((XmlText)element.FirstChild).Value; |
| | 0 | 171 | | return true; |
| | | 172 | | } |
| | 0 | 173 | | return false; |
| | | 174 | | } |
| | | 175 | | } |
| | | 176 | | } |