2005-06-05 Peter Bartok <pbartok@novell.com>
[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.Globalization;
32 using System.Security.Permissions;
33 using System.Runtime.InteropServices;
34
35 namespace System.Security.Policy {
36
37         [Serializable]
38 #if NET_2_0
39         [ComVisible (true)]
40 #endif
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                         assemblyVersion = version;
57                 }
58
59                 // for PolicyLevel (to avoid validation duplication)
60                 internal StrongNameMembershipCondition (SecurityElement e)
61                 {
62                         FromXml (e);
63                 }
64
65                 // so System.Activator.CreateInstance can create an instance...
66                 internal StrongNameMembershipCondition ()
67                 {
68                 }
69
70                 // properties
71
72                 public string Name {
73                         get { return name; }
74                         set { name = value; }
75                 }
76
77                 public Version Version {
78                         get { return assemblyVersion; }
79                         set { assemblyVersion = value; }
80                 }
81
82                 public StrongNamePublicKeyBlob PublicKey {
83                         get { return blob; }
84                         set {
85                                 if (value == null)
86                                         throw new ArgumentNullException (
87                                                 Locale.GetText ("The argument is null."));
88                                 blob = value;
89                         }
90                 }
91
92                 public bool Check (Evidence evidence)
93                 {
94                         if (evidence == null)
95                                 return false;
96
97                         foreach (object o in evidence) {
98                                 if (o is StrongName) {
99                                         StrongName sn = (o as StrongName);
100                                         /* ??? partial match ??? */
101                                         if (sn.PublicKey.Equals (blob) && (sn.Name == name) && (sn.Version.Equals (assemblyVersion)))
102                                                 return true;
103                                 }
104                         }
105                         return false;
106                 }
107
108                 public IMembershipCondition Copy ()
109                 {
110                         return new StrongNameMembershipCondition (blob, name, assemblyVersion);
111                 }
112
113                 public override bool Equals (object o)
114                 {        
115                         if (o is StrongNameMembershipCondition == false)
116                                 return false;
117                         else {
118                                 StrongNameMembershipCondition snmc = (StrongNameMembershipCondition) o;
119                                 return (snmc.Name == Name && snmc.Version == Version && snmc.PublicKey == PublicKey);
120                         }
121                 }
122
123                 public override int GetHashCode ()
124                 {
125                         return blob.GetHashCode ();
126                 }
127
128                 public void FromXml (SecurityElement e)
129                 {
130                         FromXml (e, null);
131                 }
132
133                 public void FromXml (SecurityElement e, PolicyLevel level)
134                 {
135                         MembershipConditionHelper.CheckSecurityElement (e, "e", version, version);
136
137                         blob = StrongNamePublicKeyBlob.FromString (e.Attribute ("PublicKeyBlob"));
138                         name = e.Attribute ("Name");
139                         string v = (string) e.Attribute ("AssemblyVersion");
140                         if (v == null)
141                                 assemblyVersion = new Version ();
142                         else
143                                 assemblyVersion = new Version (v);
144                 }
145
146                 public override string ToString ()
147                 {
148                         // ??? missing informations ???
149                         return String.Format ( "Strong Name - {0} name = {1} version {2}",
150                                         blob, name, assemblyVersion);
151                 }
152
153                 public SecurityElement ToXml ()
154                 {
155                         return ToXml (null);
156                 }
157
158                 public SecurityElement ToXml (PolicyLevel level)
159                 {
160                         // PolicyLevel isn't used as there's no need to resolve NamedPermissionSet references
161                         SecurityElement se = MembershipConditionHelper.Element (typeof (StrongNameMembershipCondition), version);
162
163                         if (blob != null)
164                                 se.AddAttribute ("PublicKeyBlob", blob.ToString ());
165                         if (name != null)
166                                 se.AddAttribute ("Name", name);
167                         if (assemblyVersion != null) {
168                                 string v = assemblyVersion.ToString ();
169                                 if (v != "0.0")
170                                         se.AddAttribute ("AssemblyVersion", v);
171                         }
172                         return se;
173                 }
174         }
175 }