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

publisher.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: b28e9c13bbb5d17586bbdfba275fd57bf07bf835 (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
// ==++==
// 
//   Copyright (c) Microsoft Corporation.  All rights reserved.
// 
// ==--==
// <OWNER>[....]</OWNER>
// 

//
//  Publisher.cs
//
//  Publisher is an IIdentity representing internet sites.
//

namespace System.Security.Policy {
    using System.Runtime.Remoting;
    using System;
    using System.IO;
    using System.Security.Util;
    using System.Collections;
    using PublisherIdentityPermission = System.Security.Permissions.PublisherIdentityPermission;
    using System.Security.Cryptography.X509Certificates;
    using System.Diagnostics.Contracts;

    [Serializable]
    [System.Runtime.InteropServices.ComVisible(true)]
    sealed public class Publisher : EvidenceBase, IIdentityPermissionFactory
    {
        private X509Certificate m_cert;

        public Publisher(X509Certificate cert)
        {
            if (cert == null)
                throw new ArgumentNullException("cert");
            Contract.EndContractBlock();

            m_cert = cert;
        }

        public IPermission CreateIdentityPermission( Evidence evidence )
        {
            return new PublisherIdentityPermission( m_cert );
        }

        // Two Publisher objects are equal if the public keys contained within their certificates
        // are equal.  The certs themselves may be different...

        public override bool Equals(Object o)
        {
            Publisher that = (o as Publisher);

            return (that != null && PublicKeyEquals( this.m_cert, that.m_cert ));
        }

        // Checks if two certificates have the same public key, keyalg, and keyparam.
        internal static bool PublicKeyEquals( X509Certificate cert1, X509Certificate cert2 )
        {
            if (cert1 == null)
            {
                return (cert2 == null);
            }
            else if (cert2 == null)
            {
                return false;
            }

            byte[] publicKey1 = cert1.GetPublicKey();
            String keyAlg1 = cert1.GetKeyAlgorithm();
            byte[] keyAlgParam1 = cert1.GetKeyAlgorithmParameters();
            byte[] publicKey2 = cert2.GetPublicKey();
            String keyAlg2 = cert2.GetKeyAlgorithm();
            byte[] keyAlgParam2 = cert2.GetKeyAlgorithmParameters();

            // Keys are most likely to be different of the three components,
            // so check them first

            int len = publicKey1.Length;
            if (len != publicKey2.Length) return(false);
            for (int i = 0; i < len; i++) {
                if (publicKey1[i] != publicKey2[i]) return(false);
            }
            if (!(keyAlg1.Equals(keyAlg2))) return(false);
            len = keyAlgParam1.Length;
            if (keyAlgParam2.Length != len) return(false);
            for (int i = 0; i < len; i++) {
                if (keyAlgParam1[i] != keyAlgParam2[i]) return(false);
            }

            return true;
        }

        public override int GetHashCode()
        {
            return m_cert.GetHashCode();
        }

        public X509Certificate Certificate
        {
            get { return new X509Certificate(m_cert); }
        }

        public override EvidenceBase Clone()
        {
            return new Publisher(m_cert);
        }

        public object Copy()
        {
            return Clone();
        }

        internal SecurityElement ToXml()
        {
            SecurityElement elem = new SecurityElement( "System.Security.Policy.Publisher" );
            // 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.Publisher" ), "Class name changed!" );

            elem.AddAttribute( "version", "1" );
            elem.AddChild( new SecurityElement( "X509v3Certificate", m_cert != null ? m_cert.GetRawCertDataString() : "" ) );
            return elem;
        }

        public override String ToString()
        {
            return ToXml().ToString();
        }

        // INormalizeForIsolatedStorage is not implemented for startup perf
        // equivalent to INormalizeForIsolatedStorage.Normalize()
        internal Object Normalize()
        {
            MemoryStream ms = new MemoryStream(m_cert.GetRawCertData());
            ms.Position = 0;
            return ms;
        }
    }
}