2010-05-27 Jb Evain <jbevain@novell.com>
[mcs.git] / class / corlib / System.Security.Policy / StrongNameMembershipCondition.cs
blob7b29b47231f88255067ef4b92183e94c1b6566cc
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)
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.
31 using System.Collections;
32 using System.Globalization;
33 using System.Security.Permissions;
34 using System.Runtime.InteropServices;
35 using System.Text;
37 namespace System.Security.Policy {
39 [Serializable]
40 [ComVisible (true)]
41 public sealed class StrongNameMembershipCondition : IMembershipCondition, IConstantMembershipCondition {
43 private readonly int version = 1;
45 private StrongNamePublicKeyBlob blob;
46 private string name;
47 private Version assemblyVersion;
49 public StrongNameMembershipCondition (StrongNamePublicKeyBlob blob, string name, Version version)
51 if (blob == null)
52 throw new ArgumentNullException ("blob");
54 this.blob = blob;
55 this.name = name;
56 if (version != null)
57 assemblyVersion = (Version) version.Clone ();
60 // for PolicyLevel (to avoid validation duplication)
61 internal StrongNameMembershipCondition (SecurityElement e)
63 FromXml (e);
66 // so System.Activator.CreateInstance can create an instance...
67 internal StrongNameMembershipCondition ()
71 // properties
73 public string Name {
74 get { return name; }
75 set { name = value; }
78 public Version Version {
79 get { return assemblyVersion; }
80 set { assemblyVersion = value; }
83 public StrongNamePublicKeyBlob PublicKey {
84 get { return blob; }
85 set {
86 if (value == null)
87 throw new ArgumentNullException ("PublicKey");
88 blob = value;
92 public bool Check (Evidence evidence)
94 if (evidence == null)
95 return false;
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;
110 return false;
113 public IMembershipCondition Copy ()
115 return new StrongNameMembershipCondition (blob, name, assemblyVersion);
118 public override bool Equals (object o)
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);
132 public override int GetHashCode ()
134 // name and version aren't part of the calculation
135 return blob.GetHashCode ();
138 public void FromXml (SecurityElement e)
140 FromXml (e, null);
143 public void FromXml (SecurityElement e, PolicyLevel level)
145 MembershipConditionHelper.CheckSecurityElement (e, "e", version, version);
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);
156 public override string ToString ()
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 ();
167 public SecurityElement ToXml ()
169 return ToXml (null);
172 public SecurityElement ToXml (PolicyLevel level)
174 // PolicyLevel isn't used as there's no need to resolve NamedPermissionSet references
175 SecurityElement se = MembershipConditionHelper.Element (typeof (StrongNameMembershipCondition), version);
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);
186 return se;