File: DigestTraceRecordHelper.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 (146 lines) | stat: -rw-r--r-- 5,227 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
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//------------------------------------------------------------

namespace System.IdentityModel.Diagnostics
{
    using System;
    using System.Diagnostics;
    using System.Globalization;
    using System.IO;
    using System.Runtime.Diagnostics;
    using System.Security.Cryptography;
    using System.ServiceModel.Diagnostics;
    using System.Xml;
    
    class DigestTraceRecord : TraceRecord
    {
        MemoryStream _logStream;
        HashAlgorithm _hash;
        string _traceName;

        const string Empty = "Empty";

        const string CanonicalElementString = "CanonicalElementString";
        const string CanonicalElementStringLength = "CanonicalElementStringLength";
        
        const string CanonicalOctets = "CanonicalOctets";
        const string CanonicalOctetsLength = "CanonicalOctetsLength";

        const string CanonicalOctetsHash = "CanonicalOctetsHash";
        const string CanonicalOctetsHashLength = "CanonicalOctetsHashLength";

        const string Key = "Key";
        const string Length = "Length";
        const string FirstByte = "FirstByte";
        const string LastByte = "LastByte";

        internal DigestTraceRecord(string traceName, MemoryStream logStream, HashAlgorithm hash)
        {   
            if (string.IsNullOrEmpty(traceName))
                _traceName = Empty;
            else
                _traceName = traceName;

            _logStream = logStream;
            _hash = hash;
        }

        internal override string EventId
        {
            get
            {
                return TraceRecord.EventIdBase + _traceName + TraceRecord.NamespaceSuffix;
            }
        }

        internal override void WriteTo(XmlWriter writer)
        {
            
            base.WriteTo(writer);

            //
            // canonical element string
            //
            byte[] contentBuffer = _logStream.GetBuffer();
            string contentAsString = System.Text.Encoding.UTF8.GetString(contentBuffer, 0, (int)_logStream.Length);


            writer.WriteElementString(CanonicalElementStringLength, contentAsString.Length.ToString(CultureInfo.InvariantCulture));
            writer.WriteComment(CanonicalElementString + ":" + contentAsString);

            //
            // canonical element base64 format
            //
            writer.WriteElementString(CanonicalOctetsLength, contentBuffer.Length.ToString(CultureInfo.InvariantCulture));
            writer.WriteElementString(CanonicalOctets, Convert.ToBase64String(contentBuffer));

            //
            // Hash
            //
            writer.WriteElementString(CanonicalOctetsHashLength, _hash.Hash.Length.ToString(CultureInfo.InvariantCulture));
            writer.WriteElementString(CanonicalOctetsHash, Convert.ToBase64String(_hash.Hash));

            //
            // Key: this will only be printed out for the symmetric key case
            //
            if (_hash is KeyedHashAlgorithm)
            {
                KeyedHashAlgorithm keyedHash = _hash as KeyedHashAlgorithm;
                byte[] key = keyedHash.Key;

                writer.WriteStartElement(Key); // start the key element

                writer.WriteElementString(Length, key.Length.ToString(CultureInfo.InvariantCulture)); // key length
                writer.WriteElementString(FirstByte, key[0].ToString(CultureInfo.InvariantCulture)); // first byte of the key
                writer.WriteElementString(LastByte, key[key.Length - 1].ToString(CultureInfo.InvariantCulture)); // last byte of the key

                writer.WriteEndElement();  // close the key element
            }
        }
    }

    internal static class DigestTraceRecordHelper
    {
        const string DigestTrace = "DigestTrace";
        static bool _shouldTraceDigest = false;
        static bool _initialized = false;

        internal static bool ShouldTraceDigest
        {
            get
            {
                if (!_initialized)
                    InitializeShouldTraceDigest();

                return _shouldTraceDigest;
            }
        }

        static void InitializeShouldTraceDigest()
        {
            //
            // Log the digest only if
            // 1.Users ask for verbose AND
            // 2.Users are fine with logging Pii ( private identity information )
            //
            if (DiagnosticUtility.DiagnosticTrace != null &&
                DiagnosticUtility.DiagnosticTrace.TraceSource != null &&
                DiagnosticUtility.DiagnosticTrace.ShouldLogPii &&
                DiagnosticUtility.ShouldTraceVerbose)
            {
                _shouldTraceDigest = true;
            }

            _initialized = true;
        }

        internal static void TraceDigest(MemoryStream logStream, HashAlgorithm hash)
        {
            if (DigestTraceRecordHelper.ShouldTraceDigest)
            {
                TraceUtility.TraceEvent(TraceEventType.Verbose, TraceCode.IdentityModel, SR.GetString(SR.TraceCodeIdentityModel), new DigestTraceRecord(DigestTrace, logStream, hash), null, null);
            }
        }
    }
}