File: SecurityTraceRecordHelper.cs

package info (click to toggle)
mono 4.6.2.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (239 lines) | stat: -rw-r--r-- 10,642 bytes parent folder | download | duplicates (7)
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239

namespace System.IdentityModel.Diagnostics
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IdentityModel.Diagnostics;
    using System.Diagnostics;
    using System.Runtime.Diagnostics;
    using System.Security.Authentication.ExtendedProtection;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.ServiceModel.Diagnostics;
    using System.Xml;
    using System.IdentityModel.Tokens;
    using System.Security.Cryptography;

    class SecurityTraceRecord : TraceRecord
    {
        String traceName;
        internal SecurityTraceRecord(String traceName)
        {
            if (string.IsNullOrEmpty(traceName))
                this.traceName = "Empty";
            else
                this.traceName = traceName;
        }

        internal override string EventId { get { return BuildEventId(traceName); } }
    }

    internal static class SecurityTraceRecordHelper
    {
        internal static void TraceServiceNameBindingOnServer(string serviceBindingNameSentByClient, string defaultServiceBindingNameOfServer, ServiceNameCollection serviceNameCollectionConfiguredOnServer)
        {
            TraceUtility.TraceEvent(TraceEventType.Information, TraceCode.ServiceBindingCheck, SR.GetString(SR.TraceCodeServiceBindingCheck), new ServiceBindingNameTraceRecord(serviceBindingNameSentByClient, defaultServiceBindingNameOfServer, serviceNameCollectionConfiguredOnServer), null, null);
        }

        internal static void TraceChannelBindingInformation(ExtendedProtectionPolicyHelper policyHelper, bool isServer, ChannelBinding channelBinding)
        {
            TraceUtility.TraceEvent(TraceEventType.Information, TraceCode.ChannelBindingCheck, SR.GetString(SR.TraceCodeChannelBindingCheck), new ChannelBindingNameTraceRecord(policyHelper, isServer, channelBinding), null, null);
        }

        class ServiceBindingNameTraceRecord : SecurityTraceRecord
        {
            string serviceBindingNameSentByClient;
            string defaultServiceBindingNameOfServer;
            ServiceNameCollection serviceNameCollectionConfiguredOnServer;

            public ServiceBindingNameTraceRecord(string serviceBindingNameSentByClient, string defaultServiceBindingNameOfServer, ServiceNameCollection serviceNameCollectionConfiguredOnServer)
                : base("ServiceBindingCheckAfterSpNego")
            {
                this.serviceBindingNameSentByClient = serviceBindingNameSentByClient;
                this.defaultServiceBindingNameOfServer = defaultServiceBindingNameOfServer;
                this.serviceNameCollectionConfiguredOnServer = serviceNameCollectionConfiguredOnServer;
            }

            internal override void WriteTo(XmlWriter xml)
            {
                if (xml == null)
                    return;

                xml.WriteComment(SR.GetString(SR.ServiceNameFromClient));
                xml.WriteElementString("ServiceName", this.serviceBindingNameSentByClient);

                xml.WriteComment(SR.GetString(SR.ServiceNameOnService));
                xml.WriteStartElement("ServiceNameCollection");
                if (this.serviceNameCollectionConfiguredOnServer == null || this.serviceNameCollectionConfiguredOnServer.Count < 1)
                {
                    xml.WriteElementString("ServiceName", this.defaultServiceBindingNameOfServer);
                }
                else
                {
                    foreach (string serviceName in this.serviceNameCollectionConfiguredOnServer)
                    {
                        xml.WriteElementString("ServiceName", serviceName);
                    }
                }

                xml.WriteFullEndElement();

            }
        }

        class ChannelBindingNameTraceRecord : SecurityTraceRecord
        {
            ExtendedProtectionPolicyHelper policyHelper;
            bool isServer;
            bool channelBindingUsed;
            ChannelBinding channelBinding;

            public ChannelBindingNameTraceRecord(ExtendedProtectionPolicyHelper policyHelper, bool isServer, ChannelBinding channelBinding)
                : base("SpNegoChannelBindingInformation")
            {
                this.policyHelper = policyHelper;
                this.isServer = isServer;
                this.channelBindingUsed = false;
                this.channelBinding = channelBinding;
            }

            internal override void WriteTo(XmlWriter xml)
            {
                if (xml == null)
                    return;
                if (this.policyHelper != null)
                {
                    xml.WriteElementString("PolicyEnforcement", this.policyHelper.PolicyEnforcement.ToString());
                    xml.WriteElementString("ProtectionScenario", this.policyHelper.ProtectionScenario.ToString());

                    xml.WriteStartElement("ServiceNameCollection");

                    if (this.policyHelper.ServiceNameCollection != null && this.policyHelper.ServiceNameCollection.Count > 0)
                    {
                        foreach (string serviceName in this.policyHelper.ServiceNameCollection)
                        {
                            xml.WriteElementString("ServiceName", serviceName);
                        }
                    }

                    xml.WriteFullEndElement();

                    if (this.isServer)
                    {
                        this.channelBindingUsed = this.policyHelper.ShouldAddChannelBindingToASC();
                    }
                    else
                    {
                        this.channelBindingUsed = this.policyHelper.ChannelBinding != null;
                    }

                    xml.WriteElementString("ChannelBindingUsed", this.channelBindingUsed.ToString());

                    if (this.channelBinding != null && this.policyHelper.PolicyEnforcement != PolicyEnforcement.Never && this.channelBindingUsed == true)
                    {
                        ExtendedProtectionPolicy extendedProtection = new ExtendedProtectionPolicy(policyHelper.PolicyEnforcement, channelBinding);
                        xml.WriteElementString("ChannelBindingData", GetBase64EncodedChannelBindingData(extendedProtection));
                    }
                }
                else
                {
                    // This is the case for KerberosRequestorSecurityToken where policyHelper is null.
                    if (this.channelBinding != null)
                    {
                        xml.WriteElementString("ChannelBindingUsed", "true");

                        // We do not know the PolicyEnforcement value here on the client side and we can not pass Never 
                        //as ExtendedProtectionPolicy constructor would throw on PolicyEnforcement.Never
                        ExtendedProtectionPolicy extendedProtection = new ExtendedProtectionPolicy(PolicyEnforcement.WhenSupported, channelBinding);
                        xml.WriteElementString("ChannelBindingData", GetBase64EncodedChannelBindingData(extendedProtection));
                    }
                    else
                    {
                        xml.WriteElementString("ChannelBindingUsed", "false");
                        xml.WriteElementString("ChannelBindingData", null);
                    }
                }

            }

            internal string GetBase64EncodedChannelBindingData(ExtendedProtectionPolicy extendedProtectionPolicy)
            {
                MemoryStream ms = new MemoryStream();
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(ms, extendedProtectionPolicy);
                byte[] channelBindingData = ms.GetBuffer();
                return Convert.ToBase64String(channelBindingData, Base64FormattingOptions.None);
            }
        }

        /// <summary>
        /// Used to serialize a token to the trace.  Used in multiple places.
        /// </summary>
        internal class TokenTraceRecord : SecurityTraceRecord
        {
            const string ElementName = "TokenTraceRecord";

            SecurityToken _securityToken;

            public TokenTraceRecord(SecurityToken securityToken)
                : base(ElementName)
            {
                _securityToken = securityToken;
            }

            void WriteSessionToken(XmlWriter writer, SessionSecurityToken sessionToken)
            {
                SessionSecurityTokenHandler ssth = GetOrCreateSessionSecurityTokenHandler();

                XmlDictionaryWriter dictionaryWriter = XmlDictionaryWriter.CreateDictionaryWriter(writer);
                ssth.WriteToken(dictionaryWriter, sessionToken);
            }

            private static SessionSecurityTokenHandler GetOrCreateSessionSecurityTokenHandler()
            {
                SecurityTokenHandlerCollection defaultHandlers = SecurityTokenHandlerCollection.CreateDefaultSecurityTokenHandlerCollection();
                SessionSecurityTokenHandler ssth = defaultHandlers[typeof(SessionSecurityToken)] as SessionSecurityTokenHandler;

                if (ssth == null)
                {
                    ssth = new SessionSecurityTokenHandler();
                    defaultHandlers.AddOrReplace(ssth);
                }

                return ssth;
            }

            internal override void WriteTo(XmlWriter writer)
            {
                writer.WriteStartElement(ElementName);
                writer.WriteAttributeString(DiagnosticStrings.NamespaceTag, EventId);

                writer.WriteStartElement("SecurityToken");
                writer.WriteAttributeString("Type", _securityToken.GetType().ToString());

                if (_securityToken is SessionSecurityToken)
                {
                    WriteSessionToken(writer, _securityToken as SessionSecurityToken);
                }
                else
                {
                    SecurityTokenHandlerCollection sthc = SecurityTokenHandlerCollection.CreateDefaultSecurityTokenHandlerCollection();
                    if (sthc.CanWriteToken(_securityToken))
                    {
                        {
                            sthc.WriteToken(writer, _securityToken);
                        }
                    }
                    else
                    {
                        writer.WriteElementString("Warning", SR.GetString(SR.TraceUnableToWriteToken, _securityToken.GetType().ToString()));
                    }
                }

                writer.WriteEndElement();
            }
        }
    }
}