**** Merged from MCS ****
[mono-project.git] / mcs / class / corlib / System.Security.Policy / StrongNameMembershipCondition.cs
blobfd2e67b2f66fd4bb8d357d2edb382099188e71b9
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 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.Globalization;
32 using System.Security.Permissions;
34 namespace System.Security.Policy {
36 [Serializable]
37 public sealed class StrongNameMembershipCondition : IMembershipCondition, IConstantMembershipCondition {
39 private readonly int version = 1;
41 private StrongNamePublicKeyBlob blob;
42 private string name;
43 private Version assemblyVersion;
45 public StrongNameMembershipCondition (StrongNamePublicKeyBlob blob, string name, Version version)
47 if (blob == null)
48 throw new ArgumentNullException ("blob");
50 this.blob = blob;
51 this.name = name;
52 assemblyVersion = version;
55 // for PolicyLevel (to avoid validation duplication)
56 internal StrongNameMembershipCondition (SecurityElement e)
58 FromXml (e);
61 // so System.Activator.CreateInstance can create an instance...
62 internal StrongNameMembershipCondition ()
66 // properties
68 public string Name {
69 get { return name; }
70 set { name = value; }
73 public Version Version {
74 get { return assemblyVersion; }
75 set { assemblyVersion = value; }
78 public StrongNamePublicKeyBlob PublicKey {
79 get { return blob; }
80 set {
81 if (value == null)
82 throw new ArgumentNullException (
83 Locale.GetText ("The argument is null."));
84 blob = value;
88 public bool Check (Evidence evidence)
90 if (evidence == null)
91 return false;
93 foreach (object o in evidence) {
94 if (o is StrongName) {
95 StrongName sn = (o as StrongName);
96 /* ??? partial match ??? */
97 if (sn.PublicKey.Equals (blob) && (sn.Name == name) && (sn.Version.Equals (assemblyVersion)))
98 return true;
101 return false;
104 public IMembershipCondition Copy ()
106 return new StrongNameMembershipCondition (blob, name, assemblyVersion);
109 public override bool Equals (object o)
111 if (o is StrongNameMembershipCondition == false)
112 return false;
113 else {
114 StrongNameMembershipCondition snmc = (StrongNameMembershipCondition) o;
115 return (snmc.Name == Name && snmc.Version == Version && snmc.PublicKey == PublicKey);
119 public override int GetHashCode ()
121 return blob.GetHashCode ();
124 public void FromXml (SecurityElement e)
126 FromXml (e, null);
129 public void FromXml (SecurityElement e, PolicyLevel level)
131 MembershipConditionHelper.CheckSecurityElement (e, "e", version, version);
133 blob = StrongNamePublicKeyBlob.FromString (e.Attribute ("PublicKeyBlob"));
134 name = e.Attribute ("Name");
135 string v = (string) e.Attribute ("AssemblyVersion");
136 if (v == null)
137 assemblyVersion = new Version ();
138 else
139 assemblyVersion = new Version (v);
142 public override string ToString ()
144 // ??? missing informations ???
145 return String.Format ( "Strong Name - {0} name = {1} version {2}",
146 blob, name, assemblyVersion);
149 public SecurityElement ToXml ()
151 return ToXml (null);
154 public SecurityElement ToXml (PolicyLevel level)
156 // PolicyLevel isn't used as there's no need to resolve NamedPermissionSet references
157 SecurityElement se = MembershipConditionHelper.Element (typeof (StrongNameMembershipCondition), version);
159 if (blob != null)
160 se.AddAttribute ("PublicKeyBlob", blob.ToString ());
161 if (name != null)
162 se.AddAttribute ("Name", name);
163 if (assemblyVersion != null) {
164 string v = assemblyVersion.ToString ();
165 if (v != "0.0")
166 se.AddAttribute ("AssemblyVersion", v);
168 return se;