summaryrefslogtreecommitdiff
path: root/src/mscorlib/src/System/Security/Policy/EvidenceTypeDescriptor.cs
blob: bccf39218bd984656fd3f4183f77599c9a2ff37f (plain)
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Runtime.Serialization;

namespace System.Security.Policy
{
    /// <summary>
    ///     Descriptor stored in the Evidence collection to detail the information we have about a type of
    ///     evidence. This descriptor also stores any evidence that's been generated of the specific type.
    /// </summary>
    [Serializable]
    internal sealed class EvidenceTypeDescriptor
    {
        [NonSerialized]
        private bool m_hostCanGenerate;

        [NonSerialized]
        private bool m_generated;

        private EvidenceBase m_hostEvidence;
        private EvidenceBase m_assemblyEvidence;

        // EvidenceTypeDescriptors are stored in Evidence indexed by the type they describe, so this
        // information is redundant.  We keep it around in checked builds to help debugging, but we can drop
        // it from retial builds.
#if _DEBUG
        [NonSerialized]
        private Type m_evidenceType;
#endif // _DEBUG

        public EvidenceTypeDescriptor()
        {
        }

        /// <summary>
        ///     Make a deep copy of a type descriptor
        /// </summary>
        private EvidenceTypeDescriptor(EvidenceTypeDescriptor descriptor)
        {
            Contract.Assert(descriptor != null);

            m_hostCanGenerate = descriptor.m_hostCanGenerate;

            if (descriptor.m_assemblyEvidence != null)
            {
                m_assemblyEvidence = descriptor.m_assemblyEvidence.Clone() as EvidenceBase;
            }
            if (descriptor.m_hostEvidence != null)
            {
                m_hostEvidence = descriptor.m_hostEvidence.Clone() as EvidenceBase;
            }

#if _DEBUG
            m_evidenceType = descriptor.m_evidenceType;
#endif // _DEBUG
        }

        /// <summary>
        ///     Evidence of this type supplied by the assembly
        /// </summary>
        public EvidenceBase AssemblyEvidence
        {
            get { return m_assemblyEvidence; }

            set
            {
                Contract.Assert(value != null);
#if _DEBUG
                Contract.Assert(CheckEvidenceType(value), "Incorrect type of AssemblyEvidence set");
#endif
                m_assemblyEvidence = value;
            }
        }

        /// <summary>
        ///     Flag indicating that we've already attempted to generate this type of evidence
        /// </summary>
        public bool Generated
        {
            get { return m_generated; }

            set
            {
                Contract.Assert(value, "Attempt to clear the Generated flag");
                m_generated = value;
            }
        }

        /// <summary>
        ///     Has the HostSecurityManager has told us that it can potentially generate evidence of this type
        /// </summary>
        public bool HostCanGenerate
        {
            get { return m_hostCanGenerate; }

            set
            {
                Contract.Assert(value, "Attempt to clear HostCanGenerate flag");
                m_hostCanGenerate = value;
            }
        }

        /// <summary>
        ///     Evidence of this type supplied by the CLR or the host
        /// </summary>
        public EvidenceBase HostEvidence
        {
            get { return m_hostEvidence; }

            set
            {
                Contract.Assert(value != null);
#if _DEBUG
                Contract.Assert(CheckEvidenceType(value), "Incorrect type of HostEvidence set");
#endif
                m_hostEvidence = value;
            }
        }

#if _DEBUG
        /// <summary>
        ///     Verify that evidence being stored in this descriptor is of the correct type
        /// </summary>
        private bool CheckEvidenceType(EvidenceBase evidence)
        {
            Contract.Assert(evidence != null);

            ILegacyEvidenceAdapter legacyAdapter = evidence as ILegacyEvidenceAdapter;
            Type storedType = legacyAdapter == null ? evidence.GetType() : legacyAdapter.EvidenceType;

            return m_evidenceType == null || m_evidenceType.IsAssignableFrom(storedType);
        }
#endif // _DEBUG

        /// <summary>
        ///     Make a deep copy of this descriptor
        /// </summary>
        public EvidenceTypeDescriptor Clone()
        {
            return new EvidenceTypeDescriptor(this);
        }

#if _DEBUG
        /// <summary>
        ///     Set the type that this evidence descriptor refers to.
        /// </summary>
        internal void SetEvidenceType(Type evidenceType)
        {
            Contract.Assert(evidenceType != null);
            Contract.Assert(m_evidenceType == null, "Attempt to reset evidence type");

            m_evidenceType = evidenceType;
        }
#endif // _DEBUG
    }
}