Welcome to mirror list, hosted at ThFree Co, Russian Federation.

publishermembershipcondition.cs « policy « security « system « mscorlib « referencesource « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c3da15b832c0015ddac385cb98083d41af071c14 (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
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
// ==++==
// 
//   Copyright (c) Microsoft Corporation.  All rights reserved.
// 
// ==--==
//  PublisherMembershipCondition.cs
// 
// <OWNER>Microsoft</OWNER>
//
//  Implementation of membership condition for X509 certificate based publishers
//

namespace System.Security.Policy {
    using System;
    using System.Collections;
    using System.Globalization;
    using System.Security;
    using System.Security.Cryptography.X509Certificates;
    using System.Security.Policy;
    using System.Diagnostics.Contracts;

    [Serializable]
    [System.Runtime.InteropServices.ComVisible(true)]
    sealed public class PublisherMembershipCondition : IMembershipCondition, IConstantMembershipCondition, IReportMatchMembershipCondition
    {
        //------------------------------------------------------
        //
        // PRIVATE STATE DATA
        //
        //------------------------------------------------------

        private X509Certificate m_certificate;
        private SecurityElement m_element;

        //------------------------------------------------------
        //
        // PUBLIC CONSTRUCTORS
        //
        //------------------------------------------------------

        internal PublisherMembershipCondition()
        {
            m_element = null;
            m_certificate = null;
        }

        public PublisherMembershipCondition( X509Certificate certificate )
        {
            CheckCertificate( certificate );
            m_certificate = new X509Certificate( certificate );
        }

        private static void CheckCertificate( X509Certificate certificate )
        {
            if (certificate == null)
            {
                throw new ArgumentNullException( "certificate" );
            }
            Contract.EndContractBlock();
        }

        //------------------------------------------------------
        //
        // PUBLIC ACCESSOR METHODS
        //
        //------------------------------------------------------

        public X509Certificate Certificate
        {
            set
            {
                CheckCertificate( value );
                m_certificate = new X509Certificate( value );
            }

            get
            {
                if (m_certificate == null && m_element != null)
                    ParseCertificate();

                if (m_certificate != null)
                    return new X509Certificate( m_certificate );
                else
                    return null;
            }
        }

        public override String ToString()
        {
            if (m_certificate == null && m_element != null)
                ParseCertificate();

            if (m_certificate == null)
                return Environment.GetResourceString( "Publisher_ToString" );
            else
            {
                String name = m_certificate.Subject;
                if (name != null)
                    return String.Format( CultureInfo.CurrentCulture, Environment.GetResourceString( "Publisher_ToStringArg" ), System.Security.Util.Hex.EncodeHexString( m_certificate.GetPublicKey() ) );
                else
                    return Environment.GetResourceString( "Publisher_ToString" );
            }
        }

        //------------------------------------------------------
        //
        // IMEMBERSHIPCONDITION IMPLEMENTATION
        //
        //------------------------------------------------------

        public bool Check( Evidence evidence )
        {
            object usedEvidence = null;
            return (this as IReportMatchMembershipCondition).Check(evidence, out usedEvidence);
        }

        bool IReportMatchMembershipCondition.Check(Evidence evidence, out object usedEvidence)
        {
            usedEvidence = null;

            if (evidence == null)
                return false;

            Publisher publisher = evidence.GetHostEvidence<Publisher>();
            if (publisher != null)
            {
                if (m_certificate == null && m_element != null)
                    ParseCertificate();

                // We can't just compare certs directly here because Publisher equality
                // depends only on the keys inside the certs.
                if (publisher.Equals(new Publisher(m_certificate)))
                {
                    usedEvidence = publisher;
                    return true;
                }
            }

            return false;
        }

        public IMembershipCondition Copy()
        {
            if (m_certificate == null && m_element != null)
                ParseCertificate();

            return new PublisherMembershipCondition( m_certificate );
        }

        public SecurityElement ToXml()
        {
            return ToXml( null );
        }

        public void FromXml( SecurityElement e )
        {
            FromXml( e, null );
        }

        public SecurityElement ToXml( PolicyLevel level )
        {
            if (m_certificate == null && m_element != null)
                ParseCertificate();

            SecurityElement root = new SecurityElement( "IMembershipCondition" );
            System.Security.Util.XMLUtil.AddClassAttribute( root, this.GetType(), "System.Security.Policy.PublisherMembershipCondition" );
            // If you hit this assert then most likely you are trying to change the name of this class. 
            // This is ok as long as you change the hard coded string above and change the assert below.
            Contract.Assert( this.GetType().FullName.Equals( "System.Security.Policy.PublisherMembershipCondition" ), "Class name changed!" );

            root.AddAttribute( "version", "1" );
            if (m_certificate != null)
                root.AddAttribute( "X509Certificate", m_certificate.GetRawCertDataString() );

            return root;
        }

        public void FromXml( SecurityElement e, PolicyLevel level )
        {
            if (e == null)
                throw new ArgumentNullException("e");

            if (!e.Tag.Equals( "IMembershipCondition" ))
            {
                throw new ArgumentException( Environment.GetResourceString( "Argument_MembershipConditionElement" ) );
            }
            Contract.EndContractBlock();

            lock (this)
            {
                m_element = e;
                m_certificate = null;
            }
        }

        private void ParseCertificate()
        {
            lock (this)
            {
                if (m_element == null)
                    return;

                String elCert = m_element.Attribute( "X509Certificate" );
                m_certificate = elCert == null ? null : new X509Certificate( System.Security.Util.Hex.DecodeHexString( elCert ) );
                CheckCertificate( m_certificate );
                m_element = null;
            }
        }

        public override bool Equals( Object o )
        {
            PublisherMembershipCondition that = (o as PublisherMembershipCondition);
            if (that != null)
            {
                if (this.m_certificate == null && this.m_element != null)
                    this.ParseCertificate();
                if (that.m_certificate == null && that.m_element != null)
                    that.ParseCertificate();

                if ( Publisher.PublicKeyEquals( this.m_certificate, that.m_certificate ))
                    return true;
            }
            return false;
        }

        public override int GetHashCode()
        {
            if (m_certificate == null && m_element != null)
                ParseCertificate();

            if (m_certificate != null)
                return m_certificate.GetHashCode();
            else
                return typeof( PublisherMembershipCondition ).GetHashCode();
        }
    }
}