2 // StrongNameIdentityPermission.cs: Strong Name Identity Permission
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
30 using System.Globalization;
32 namespace System.Security.Permissions {
35 public sealed class StrongNameIdentityPermission : CodeAccessPermission, IBuiltInPermission {
37 private const int version = 1;
38 static private Version defaultVersion = new Version (0, 0);
40 private StrongNamePublicKeyBlob publickey;
42 private Version assemblyVersion;
44 public StrongNameIdentityPermission (PermissionState state)
46 // false == do not allow Unrestricted for Identity Permissions
47 CheckPermissionState (state, false);
50 assemblyVersion = (Version) defaultVersion.Clone ();
53 public StrongNameIdentityPermission (StrongNamePublicKeyBlob blob, string name, Version version)
56 throw new ArgumentNullException ("blob");
60 assemblyVersion = version;
67 if ((value != null) && (value.Length == 0))
68 throw new ArgumentException ("name");
74 public StrongNamePublicKeyBlob PublicKey {
75 get { return publickey; }
78 throw new ArgumentNullException ("value");
83 public Version Version {
84 get { return assemblyVersion; }
85 set { assemblyVersion = value; }
88 public override IPermission Copy ()
91 return new StrongNameIdentityPermission (PermissionState.None);
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
98 public override void FromXml (SecurityElement e)
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)
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);
111 public override IPermission Intersect (IPermission target)
113 StrongNameIdentityPermission snip = (target as StrongNameIdentityPermission);
117 if (IsEmpty () || snip.IsEmpty ())
118 return new StrongNameIdentityPermission (PermissionState.None);
120 if (name != snip.name)
122 if (!assemblyVersion.Equals (snip.assemblyVersion))
124 if (!publickey.Equals (snip.publickey))
130 public override bool IsSubsetOf (IPermission target)
132 StrongNameIdentityPermission snip = Cast (target);
136 if (((name != null) && (name.Length > 0)) && (name != snip.Name))
138 if ((assemblyVersion != null) && !assemblyVersion.Equals (snip.assemblyVersion))
140 return publickey.Equals (snip.publickey);
143 public override SecurityElement ToXml ()
145 SecurityElement se = Element (version);
146 if (publickey != null)
147 se.AddAttribute ("PublicKeyBlob", publickey.ToString ());
149 se.AddAttribute ("Name", name);
150 if (assemblyVersion != null)
151 se.AddAttribute ("AssemblyVersion", assemblyVersion.ToString ());
155 public override IPermission Union (IPermission target)
157 StrongNameIdentityPermission snip = Cast (target);
158 if ((snip == null) || snip.IsEmpty ())
161 if (!publickey.Equals (snip.publickey)) {
163 string msg = Locale.GetText ("Permissions have different public keys.");
164 throw new ArgumentException (msg, "target");
171 if ((n == null) || (n.Length == 0)) {
174 else if ((snip.name != null) && (snip.name.Length > 0) && (n != snip.name)) {
176 string msg = String.Format (Locale.GetText ("Name mismatch: '{0}' versus '{1}'"), n, snip.Name);
177 throw new ArgumentException (msg, "target");
183 Version v = assemblyVersion;
185 v = snip.assemblyVersion;
187 else if ((snip.assemblyVersion != null) && (v != snip.assemblyVersion)) {
189 string msg = String.Format (Locale.GetText ("Version mismatch: '{0}' versus '{1}'"), v, snip.assemblyVersion);
190 throw new ArgumentException (msg, "target");
196 return new StrongNameIdentityPermission (publickey, n, v);
199 // IBuiltInPermission
200 int IBuiltInPermission.GetTokenIndex ()
202 return (int) BuiltInToken.StrongNameIdentity;
207 private bool IsEmpty ()
209 if (publickey != null)
211 if ((name != null) && (name.Length > 0))
213 return ((assemblyVersion == null) || defaultVersion.Equals (assemblyVersion));
216 private StrongNameIdentityPermission Cast (IPermission target)
221 StrongNameIdentityPermission snip = (target as StrongNameIdentityPermission);
223 ThrowInvalidPermission (target, typeof (StrongNameIdentityPermission));