2004-09-02 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / corlib / System.Security.Permissions / StrongNameIdentityPermission.cs
1 //
2 // StrongNameIdentityPermission.cs: Strong Name Identity Permission
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System.Globalization;
31
32 namespace System.Security.Permissions {
33
34         [Serializable]
35         public sealed class StrongNameIdentityPermission : CodeAccessPermission, IBuiltInPermission {
36         
37                 private const int version = 1;
38                 static private Version defaultVersion = new Version (0, 0);
39
40                 private StrongNamePublicKeyBlob publickey;
41                 private string name;
42                 private Version assemblyVersion;
43         
44                 public StrongNameIdentityPermission (PermissionState state) 
45                 {
46                         // false == do not allow Unrestricted for Identity Permissions
47                         CheckPermissionState (state, false);
48                         // default values
49                         name = String.Empty;
50                         assemblyVersion = (Version) defaultVersion.Clone ();
51                 }
52         
53                 public StrongNameIdentityPermission (StrongNamePublicKeyBlob blob, string name, Version version) 
54                 {
55                         if (blob == null)
56                                 throw new ArgumentNullException ("blob");
57
58                         Name = name;
59                         publickey = blob;
60                         assemblyVersion = version;
61                 }
62         
63                 public string Name { 
64                         get { return name; }
65                         set { 
66 #if NET_2_0
67                                 if ((value != null) && (value.Length == 0))
68                                         throw new ArgumentException ("name");
69 #endif
70                                 name = value;
71                         }
72                 }
73         
74                 public StrongNamePublicKeyBlob PublicKey { 
75                         get { return publickey; }
76                         set {
77                                 if (value == null)
78                                         throw new ArgumentNullException ("value");
79                                 publickey = value;
80                         }
81                 }
82         
83                 public Version Version { 
84                         get { return assemblyVersion; }
85                         set { assemblyVersion = value; }
86                 }
87         
88                 public override IPermission Copy () 
89                 {
90                         if (IsEmpty ())
91                                 return new StrongNameIdentityPermission (PermissionState.None);
92                         else
93                                 return new StrongNameIdentityPermission (publickey, name, assemblyVersion);
94                         // Note: this will throw an ArgumentException if Name is still equals to String.Empty
95                         // but MS implementation has the same bug/design issue
96                 }
97         
98                 public override void FromXml (SecurityElement e) 
99                 {
100                         // General validation in CodeAccessPermission
101                         CheckSecurityElement (e, "e", version, version);
102                         // Note: we do not (yet) care about the return value 
103                         // as we only accept version 1 (min/max values)
104
105                         name = e.Attribute ("Name");
106                         publickey = StrongNamePublicKeyBlob.FromString (e.Attribute ("PublicKeyBlob"));
107                         string v = e.Attribute ("AssemblyVersion");
108                         assemblyVersion = (v == null) ? null : new Version (v);
109                 }
110         
111                 public override IPermission Intersect (IPermission target) 
112                 {
113                         StrongNameIdentityPermission snip = (target as StrongNameIdentityPermission);
114                         if (snip == null)
115                                 return null;
116
117                         if (IsEmpty () || snip.IsEmpty ())
118                                 return new StrongNameIdentityPermission (PermissionState.None);
119
120                         if (name != snip.name)
121                                 return null;
122                         if (!assemblyVersion.Equals (snip.assemblyVersion))
123                                 return null;
124                         if (!publickey.Equals (snip.publickey))
125                                 return null;
126
127                         return Copy ();
128                 }
129         
130                 public override bool IsSubsetOf (IPermission target) 
131                 {
132                         StrongNameIdentityPermission snip = Cast (target);
133                         if (snip == null)
134                                 return IsEmpty ();
135
136                         if (((name != null) && (name.Length > 0)) && (name != snip.Name))
137                                 return false;
138                         if ((assemblyVersion != null) && !assemblyVersion.Equals (snip.assemblyVersion))
139                                 return false;
140                         return publickey.Equals (snip.publickey);
141                 }
142         
143                 public override SecurityElement ToXml () 
144                 {
145                         SecurityElement se = Element (version);
146                         if (publickey != null)
147                                 se.AddAttribute ("PublicKeyBlob", publickey.ToString ());
148                         if (name != null)
149                                 se.AddAttribute ("Name", name);
150                         if (assemblyVersion != null)
151                                 se.AddAttribute ("AssemblyVersion", assemblyVersion.ToString ());
152                         return se;
153                 }
154         
155                 public override IPermission Union (IPermission target) 
156                 {
157                         StrongNameIdentityPermission snip = Cast (target);
158                         if ((snip == null) || snip.IsEmpty ())
159                                 return Copy ();
160
161                         if (!publickey.Equals (snip.publickey)) {
162 #if NET_2_0
163                                 string msg = Locale.GetText ("Permissions have different public keys.");
164                                 throw new ArgumentException (msg, "target");
165 #else
166                                 return null;
167 #endif
168                         }
169
170                         string n = name;
171                         if ((n == null) || (n.Length == 0)) {
172                                 n = snip.name;
173                         }
174                         else if ((snip.name != null) && (snip.name.Length > 0) && (n != snip.name)) {
175 #if NET_2_0
176                                 string msg = String.Format (Locale.GetText ("Name mismatch: '{0}' versus '{1}'"), n, snip.Name);
177                                 throw new ArgumentException (msg, "target");
178 #else
179                                 return null;
180 #endif
181                         }
182
183                         Version v = assemblyVersion;
184                         if (v == null) {
185                                 v = snip.assemblyVersion;
186                         }
187                         else if ((snip.assemblyVersion != null) && (v != snip.assemblyVersion)) {
188 #if NET_2_0
189                                 string msg = String.Format (Locale.GetText ("Version mismatch: '{0}' versus '{1}'"), v, snip.assemblyVersion);
190                                 throw new ArgumentException (msg, "target");
191 #else
192                                 return null;
193 #endif
194                         }
195
196                         return new StrongNameIdentityPermission (publickey, n, v);
197                 }
198         
199                 // IBuiltInPermission
200                 int IBuiltInPermission.GetTokenIndex ()
201                 {
202                         return (int) BuiltInToken.StrongNameIdentity;
203                 }
204
205                 // helpers
206
207                 private bool IsEmpty ()
208                 {
209                         if (publickey != null)
210                                 return false;
211                         if ((name != null) && (name.Length > 0))
212                                 return false;
213                         return ((assemblyVersion == null) || defaultVersion.Equals (assemblyVersion));
214                 }
215
216                 private StrongNameIdentityPermission Cast (IPermission target)
217                 {
218                         if (target == null)
219                                 return null;
220
221                         StrongNameIdentityPermission snip = (target as StrongNameIdentityPermission);
222                         if (snip == null) {
223                                 ThrowInvalidPermission (target, typeof (StrongNameIdentityPermission));
224                         }
225
226                         return snip;
227                 }
228         } 
229 }