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