merged Sys.Web.Services 2.0 support in my branch:
[mono.git] / mcs / class / corlib / System.Security.Policy / StrongNameMembershipCondition.cs
1 //
2 // System.Security.Policy.StrongNameMembershipCondition.cs
3 //
4 // Author:
5 //      Duncan Mak (duncan@ximian.com)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // (C) 2003 Duncan Mak, Ximian Inc.
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.Collections;
32 using System.Globalization;
33 using System.Security.Permissions;
34 using System.Runtime.InteropServices;
35 using System.Text;
36
37 namespace System.Security.Policy {
38
39         [Serializable]
40 #if NET_2_0
41         [ComVisible (true)]
42 #endif
43         public sealed class StrongNameMembershipCondition : IMembershipCondition, IConstantMembershipCondition {
44
45                 private readonly int version = 1;
46
47                 private StrongNamePublicKeyBlob blob;
48                 private string name;
49                 private Version assemblyVersion;
50                 
51                 public StrongNameMembershipCondition (StrongNamePublicKeyBlob blob, string name, Version version)
52                 {
53                         if (blob == null)
54                                 throw new ArgumentNullException ("blob");
55
56                         this.blob = blob;
57                         this.name = name;
58                         if (version != null)
59                                 assemblyVersion = (Version) version.Clone ();
60                 }
61
62                 // for PolicyLevel (to avoid validation duplication)
63                 internal StrongNameMembershipCondition (SecurityElement e)
64                 {
65                         FromXml (e);
66                 }
67
68                 // so System.Activator.CreateInstance can create an instance...
69                 internal StrongNameMembershipCondition ()
70                 {
71                 }
72
73                 // properties
74
75                 public string Name {
76                         get { return name; }
77                         set { name = value; }
78                 }
79
80                 public Version Version {
81                         get { return assemblyVersion; }
82                         set { assemblyVersion = value; }
83                 }
84
85                 public StrongNamePublicKeyBlob PublicKey {
86                         get { return blob; }
87                         set {
88                                 if (value == null)
89                                         throw new ArgumentNullException ("PublicKey");
90                                 blob = value;
91                         }
92                 }
93
94                 public bool Check (Evidence evidence)
95                 {
96                         if (evidence == null)
97                                 return false;
98
99                         IEnumerator e = evidence.GetHostEnumerator ();
100                         while (e.MoveNext ()) {
101                                 StrongName sn = (e.Current as StrongName);
102                                 if (sn != null) {
103                                         if (!sn.PublicKey.Equals (blob))
104                                                 return false;
105                                         if ((name != null) && (name != sn.Name))
106                                                 return false;
107                                         if ((assemblyVersion != null) && !assemblyVersion.Equals (sn.Version))
108                                                 return false;
109                                         return true;
110                                 }
111                         }
112                         return false;
113                 }
114
115                 public IMembershipCondition Copy ()
116                 {
117                         return new StrongNameMembershipCondition (blob, name, assemblyVersion);
118                 }
119
120                 public override bool Equals (object o)
121                 {
122                         StrongNameMembershipCondition snmc = (o as StrongNameMembershipCondition);
123                         if (snmc == null)
124                                 return false;
125                         if (!snmc.PublicKey.Equals (PublicKey))
126                                 return false;
127                         if (name != snmc.Name)
128                                 return false;
129                         if (assemblyVersion != null)
130                                 return assemblyVersion.Equals (snmc.Version);
131                         return (snmc.Version == null);
132                 }
133
134                 public override int GetHashCode ()
135                 {
136                         // name and version aren't part of the calculation
137                         return blob.GetHashCode ();
138                 }
139
140                 public void FromXml (SecurityElement e)
141                 {
142                         FromXml (e, null);
143                 }
144
145                 public void FromXml (SecurityElement e, PolicyLevel level)
146                 {
147                         MembershipConditionHelper.CheckSecurityElement (e, "e", version, version);
148
149                         blob = StrongNamePublicKeyBlob.FromString (e.Attribute ("PublicKeyBlob"));
150                         name = e.Attribute ("Name");
151                         string v = (string) e.Attribute ("AssemblyVersion");
152                         if (v == null)
153                                 assemblyVersion = null;
154                         else
155                                 assemblyVersion = new Version (v);
156                 }
157
158                 public override string ToString ()
159                 {
160                         StringBuilder sb = new StringBuilder ("StrongName - ");
161                         sb.Append (blob);
162                         if (name != null)
163                                 sb.AppendFormat (" name = {0}", name);
164                         if (assemblyVersion != null)
165                                 sb.AppendFormat (" version = {0}", assemblyVersion);
166                         return sb.ToString ();
167                 }
168
169                 public SecurityElement ToXml ()
170                 {
171                         return ToXml (null);
172                 }
173
174                 public SecurityElement ToXml (PolicyLevel level)
175                 {
176                         // PolicyLevel isn't used as there's no need to resolve NamedPermissionSet references
177                         SecurityElement se = MembershipConditionHelper.Element (typeof (StrongNameMembershipCondition), version);
178
179                         if (blob != null)
180                                 se.AddAttribute ("PublicKeyBlob", blob.ToString ());
181                         if (name != null)
182                                 se.AddAttribute ("Name", name);
183                         if (assemblyVersion != null) {
184                                 string v = assemblyVersion.ToString ();
185                                 if (v != "0.0")
186                                         se.AddAttribute ("AssemblyVersion", v);
187                         }
188                         return se;
189                 }
190         }
191 }