Merge pull request #5714 from alexischr/update_bockbuild
[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         [ComVisible (true)]
41         public sealed class StrongNameMembershipCondition : IMembershipCondition, IConstantMembershipCondition {
42
43                 private readonly int version = 1;
44
45                 private StrongNamePublicKeyBlob blob;
46                 private string name;
47                 private Version assemblyVersion;
48                 
49                 public StrongNameMembershipCondition (StrongNamePublicKeyBlob blob, string name, Version version)
50                 {
51                         if (blob == null)
52                                 throw new ArgumentNullException ("blob");
53
54                         this.blob = blob;
55                         this.name = name;
56                         if (version != null)
57                                 assemblyVersion = (Version) version.Clone ();
58                 }
59
60                 // for PolicyLevel (to avoid validation duplication)
61                 internal StrongNameMembershipCondition (SecurityElement e)
62                 {
63                         FromXml (e);
64                 }
65
66                 // so System.Activator.CreateInstance can create an instance...
67                 internal StrongNameMembershipCondition ()
68                 {
69                 }
70
71                 // properties
72
73                 public string Name {
74                         get { return name; }
75                         set { name = value; }
76                 }
77
78                 public Version Version {
79                         get { return assemblyVersion; }
80                         set { assemblyVersion = value; }
81                 }
82
83                 public StrongNamePublicKeyBlob PublicKey {
84                         get { return blob; }
85                         set {
86                                 if (value == null)
87                                         throw new ArgumentNullException ("PublicKey");
88                                 blob = value;
89                         }
90                 }
91
92                 public bool Check (Evidence evidence)
93                 {
94                         if (evidence == null)
95                                 return false;
96
97                         IEnumerator e = evidence.GetHostEnumerator ();
98                         while (e.MoveNext ()) {
99                                 StrongName sn = (e.Current as StrongName);
100                                 if (sn != null) {
101                                         if (!sn.PublicKey.Equals (blob))
102                                                 return false;
103                                         if ((name != null) && (name != sn.Name))
104                                                 return false;
105                                         if ((assemblyVersion != null) && !assemblyVersion.Equals (sn.Version))
106                                                 return false;
107                                         return true;
108                                 }
109                         }
110                         return false;
111                 }
112
113                 public IMembershipCondition Copy ()
114                 {
115                         return new StrongNameMembershipCondition (blob, name, assemblyVersion);
116                 }
117
118                 public override bool Equals (object o)
119                 {
120                         StrongNameMembershipCondition snmc = (o as StrongNameMembershipCondition);
121                         if (snmc == null)
122                                 return false;
123                         if (!snmc.PublicKey.Equals (PublicKey))
124                                 return false;
125                         if (name != snmc.Name)
126                                 return false;
127                         if (assemblyVersion != null)
128                                 return assemblyVersion.Equals (snmc.Version);
129                         return (snmc.Version == null);
130                 }
131
132                 public override int GetHashCode ()
133                 {
134                         // name and version aren't part of the calculation
135                         return blob.GetHashCode ();
136                 }
137
138                 public void FromXml (SecurityElement e)
139                 {
140                         FromXml (e, null);
141                 }
142
143                 public void FromXml (SecurityElement e, PolicyLevel level)
144                 {
145                         MembershipConditionHelper.CheckSecurityElement (e, "e", version, version);
146
147                         blob = StrongNamePublicKeyBlob.FromString (e.Attribute ("PublicKeyBlob"));
148                         name = e.Attribute ("Name");
149                         string v = (string) e.Attribute ("AssemblyVersion");
150                         if (v == null)
151                                 assemblyVersion = null;
152                         else
153                                 assemblyVersion = new Version (v);
154                 }
155
156                 public override string ToString ()
157                 {
158                         StringBuilder sb = new StringBuilder ("StrongName - ");
159                         sb.Append (blob);
160                         if (name != null)
161                                 sb.AppendFormat (" name = {0}", name);
162                         if (assemblyVersion != null)
163                                 sb.AppendFormat (" version = {0}", assemblyVersion);
164                         return sb.ToString ();
165                 }
166
167                 public SecurityElement ToXml ()
168                 {
169                         return ToXml (null);
170                 }
171
172                 public SecurityElement ToXml (PolicyLevel level)
173                 {
174                         // PolicyLevel isn't used as there's no need to resolve NamedPermissionSet references
175                         SecurityElement se = MembershipConditionHelper.Element (typeof (StrongNameMembershipCondition), version);
176
177                         if (blob != null)
178                                 se.AddAttribute ("PublicKeyBlob", blob.ToString ());
179                         if (name != null)
180                                 se.AddAttribute ("Name", name);
181                         if (assemblyVersion != null) {
182                                 string v = assemblyVersion.ToString ();
183                                 if (v != "0.0")
184                                         se.AddAttribute ("AssemblyVersion", v);
185                         }
186                         return se;
187                 }
188         }
189 }