diff options
author | Sebastien Pouliot <sebastien@ximian.com> | 2004-08-17 23:48:22 +0400 |
---|---|---|
committer | Sebastien Pouliot <sebastien@ximian.com> | 2004-08-17 23:48:22 +0400 |
commit | 61baec6662e3a83530cc9618f674c05e672f59c2 (patch) | |
tree | 71c689df6d61af74f31b556cb38f163d6215543a /mcs/class/corlib/System.Security.Permissions | |
parent | 4d1527aff7ce39549daaa81e4fc279936f48286b (diff) |
2004-08-17 Sebastien Pouliot <sebastien@ximian.com>
* StrongNameIdentityPermission.cs: Update to use the common template.
Completed TODOs and now pass all unit tests.
* StrongNamePermissionAttribute.cs: Fixed public key convertion (as it
wasn't base64 encoded).
* StrongNamePublicKeyBlob.cs: Made static FromString() more robust.
svn path=/trunk/mcs/; revision=32459
Diffstat (limited to 'mcs/class/corlib/System.Security.Permissions')
4 files changed, 149 insertions, 58 deletions
diff --git a/mcs/class/corlib/System.Security.Permissions/ChangeLog b/mcs/class/corlib/System.Security.Permissions/ChangeLog index 98d2718cfb0..637df400003 100644 --- a/mcs/class/corlib/System.Security.Permissions/ChangeLog +++ b/mcs/class/corlib/System.Security.Permissions/ChangeLog @@ -1,5 +1,10 @@ 2004-08-17 Sebastien Pouliot <sebastien@ximian.com> + * StrongNameIdentityPermission.cs: Update to use the common template. + Completed TODOs and now pass all unit tests. + * StrongNamePermissionAttribute.cs: Fixed public key convertion (as it + wasn't base64 encoded). + * StrongNamePublicKeyBlob.cs: Made static FromString() more robust. * ZoneIdentityPermission.cs: Update to use the common template. Now pass all unit tests. diff --git a/mcs/class/corlib/System.Security.Permissions/StrongNameIdentityPermission.cs b/mcs/class/corlib/System.Security.Permissions/StrongNameIdentityPermission.cs index ad6f375a0ec..8422cdc371a 100644 --- a/mcs/class/corlib/System.Security.Permissions/StrongNameIdentityPermission.cs +++ b/mcs/class/corlib/System.Security.Permissions/StrongNameIdentityPermission.cs @@ -2,12 +2,9 @@ // StrongNameIdentityPermission.cs: Strong Name Identity Permission // // Author: -// Sebastien Pouliot (spouliot@motus.com) +// Sebastien Pouliot <sebastien@ximian.com> // // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com) -// - -// // Copyright (C) 2004 Novell, Inc (http://www.novell.com) // // Permission is hereby granted, free of charge, to any person obtaining @@ -30,42 +27,48 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // -using System; +using System.Globalization; namespace System.Security.Permissions { [Serializable] public sealed class StrongNameIdentityPermission : CodeAccessPermission, IBuiltInPermission { + private const int version = 1; + static private Version defaultVersion = new Version (0, 0); + private StrongNamePublicKeyBlob publickey; private string name; - private Version version; + private Version assemblyVersion; public StrongNameIdentityPermission (PermissionState state) { - if (state == PermissionState.Unrestricted) - throw new ArgumentException ("state"); + // false == do not allow Unrestricted for Identity Permissions + CheckPermissionState (state, false); + // default values name = String.Empty; - version = new Version (0, 0); + assemblyVersion = (Version) defaultVersion.Clone (); } public StrongNameIdentityPermission (StrongNamePublicKeyBlob blob, string name, Version version) { if (blob == null) throw new ArgumentNullException ("blob"); - if (name == null) - throw new ArgumentNullException ("name"); - if (version == null) - throw new ArgumentNullException ("version"); + if ((name != null) && (name.Length == 0)) + throw new ArgumentException ("name"); publickey = blob; this.name = name; - this.version = version; + assemblyVersion = version; } public string Name { get { return name; } - set { name = value; } + set { + if ((value != null) && (value.Length == 0)) + throw new ArgumentException ("name"); + name = value; + } } public StrongNamePublicKeyBlob PublicKey { @@ -78,51 +81,137 @@ namespace System.Security.Permissions { } public Version Version { - get { return version; } - set { version = value; } + get { return assemblyVersion; } + set { assemblyVersion = value; } } public override IPermission Copy () { - return new StrongNameIdentityPermission (publickey, name, version); + if (IsEmpty ()) + return new StrongNameIdentityPermission (PermissionState.None); + else + return new StrongNameIdentityPermission (publickey, name, assemblyVersion); + // Note: this will throw an ArgumentException if Name is still equals to String.Empty + // but MS implementation has the same bug/design issue } - [MonoTODO] public override void FromXml (SecurityElement e) { - if (e == null) - throw new ArgumentNullException ("e"); - throw new NotImplementedException (); + // General validation in CodeAccessPermission + CheckSecurityElement (e, "e", version, version); + // Note: we do not (yet) care about the return value + // as we only accept version 1 (min/max values) + + name = e.Attribute ("Name"); + publickey = StrongNamePublicKeyBlob.FromString (e.Attribute ("PublicKeyBlob")); + string v = e.Attribute ("AssemblyVersion"); + assemblyVersion = (v == null) ? null : new Version (v); } - [MonoTODO] public override IPermission Intersect (IPermission target) { - throw new NotImplementedException (); + StrongNameIdentityPermission snip = (target as StrongNameIdentityPermission); + if (snip == null) + return null; + + if (IsEmpty () || snip.IsEmpty ()) + return new StrongNameIdentityPermission (PermissionState.None); + + if (name != snip.name) + return null; + if (!assemblyVersion.Equals (snip.assemblyVersion)) + return null; + if (!publickey.Equals (snip.publickey)) + return null; + + return Copy (); } - [MonoTODO] public override bool IsSubsetOf (IPermission target) { - throw new NotImplementedException (); + StrongNameIdentityPermission snip = Cast (target); + if (snip == null) + return IsEmpty (); + + if (((name != null) && (name.Length > 0)) && (name != snip.Name)) + return false; + if ((assemblyVersion != null) && !assemblyVersion.Equals (snip.assemblyVersion)) + return false; + return publickey.Equals (snip.publickey); } - [MonoTODO] public override SecurityElement ToXml () { - throw new NotImplementedException (); + SecurityElement se = Element (version); + if (publickey != null) + se.AddAttribute ("PublicKeyBlob", publickey.ToString ()); + if (name != null) + se.AddAttribute ("Name", name); + if (assemblyVersion != null) + se.AddAttribute ("AssemblyVersion", assemblyVersion.ToString ()); + return se; } - [MonoTODO] public override IPermission Union (IPermission target) { - throw new NotImplementedException (); + StrongNameIdentityPermission snip = Cast (target); + if ((snip == null) || snip.IsEmpty ()) + return Copy (); + + if (!publickey.Equals (snip.publickey)) { + string msg = Locale.GetText ("Permissions have different public keys."); + throw new ArgumentException (msg, "target"); + } + + string n = name; + if ((n == null) || (n.Length == 0)) { + n = snip.name; + } + else if ((snip.name != null) && (snip.name.Length > 0) && (n != snip.name)) { + string msg = String.Format (Locale.GetText ("Name mismatch: '{0}' versus '{1}'"), n, snip.Name); + throw new ArgumentException (msg, "target"); + } + + Version v = assemblyVersion; + if (v == null) { + v = snip.assemblyVersion; + } + else if ((snip.assemblyVersion != null) && (v != snip.assemblyVersion)) { + string msg = String.Format (Locale.GetText ("Version mismatch: '{0}' versus '{1}'"), v, snip.assemblyVersion); + throw new ArgumentException (msg, "target"); + } + + return new StrongNameIdentityPermission (publickey, n, v); } // IBuiltInPermission int IBuiltInPermission.GetTokenIndex () { - return 11; + return (int) BuiltInToken.StrongNameIdentity; + } + + // helpers + + private bool IsEmpty () + { + if (publickey != null) + return false; + if ((name != null) && (name.Length > 0)) + return false; + return ((assemblyVersion == null) || defaultVersion.Equals (assemblyVersion)); + } + + private StrongNameIdentityPermission Cast (IPermission target) + { + if (target == null) + return null; + + StrongNameIdentityPermission snip = (target as StrongNameIdentityPermission); + if (snip == null) { + ThrowInvalidPermission (target, typeof (StrongNameIdentityPermission)); + } + + return snip; } } } diff --git a/mcs/class/corlib/System.Security.Permissions/StrongNamePermissionAttribute.cs b/mcs/class/corlib/System.Security.Permissions/StrongNamePermissionAttribute.cs index 27f240ed65f..dc5383de897 100644 --- a/mcs/class/corlib/System.Security.Permissions/StrongNamePermissionAttribute.cs +++ b/mcs/class/corlib/System.Security.Permissions/StrongNamePermissionAttribute.cs @@ -1,12 +1,11 @@ // // System.Security.Permissions.StrongNameIdentityPermissionAttribute.cs // -// Duncan Mak <duncan@ximian.com> +// Authors: +// Duncan Mak <duncan@ximian.com> +// Sebastien Pouliot <sebastien@ximian.com> // // (C) 2002 Ximian, Inc. http://www.ximian.com -// - -// // Copyright (C) 2004 Novell, Inc (http://www.novell.com) // // Permission is hereby granted, free of charge, to any person obtaining @@ -29,7 +28,7 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // -using System; +using System.Globalization; namespace System.Security.Permissions { @@ -45,23 +44,23 @@ namespace System.Security.Permissions { private string version; // Constructor - public StrongNameIdentityPermissionAttribute (SecurityAction action) : base (action) {} + public StrongNameIdentityPermissionAttribute (SecurityAction action) + : base (action) + { + } // Properties - public string Name - { + public string Name { get { return name; } set { name = value; } } - public string PublicKey - { + public string PublicKey { get { return key; } set { key = value; } } - public string Version - { + public string Version { get { return version; } set { version = value; } } @@ -69,27 +68,25 @@ namespace System.Security.Permissions { // Methods public override IPermission CreatePermission () { - if (this.Unrestricted) - throw new ArgumentException ("Unsupported PermissionState.Unrestricted"); + if (this.Unrestricted) { + throw new ArgumentException (Locale.GetText ( + "Unsupported PermissionState.Unrestricted")); + } StrongNameIdentityPermission perm = null; if ((name == null) && (key == null) && (version == null)) perm = new StrongNameIdentityPermission (PermissionState.None); else { - if (key == null) - throw new ArgumentException ("PublicKey is required"); + if (key == null) { + throw new ArgumentException (Locale.GetText ( + "PublicKey is required")); + } - byte[] keyblob = Convert.FromBase64String (key); - StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (keyblob); + StrongNamePublicKeyBlob blob = StrongNamePublicKeyBlob.FromString (key); Version v = null; if (version != null) v = new Version (version); - else - v = new Version (); - - if (name == null) - name = String.Empty; perm = new StrongNameIdentityPermission (blob, name, v); } diff --git a/mcs/class/corlib/System.Security.Permissions/StrongNamePublicKeyBlob.cs b/mcs/class/corlib/System.Security.Permissions/StrongNamePublicKeyBlob.cs index cd1c0f9bf3b..d6bbbda9362 100644 --- a/mcs/class/corlib/System.Security.Permissions/StrongNamePublicKeyBlob.cs +++ b/mcs/class/corlib/System.Security.Permissions/StrongNamePublicKeyBlob.cs @@ -2,12 +2,9 @@ // StrongNamePublicKeyBlob.cs: Strong Name Public Key Blob // // Author: -// Sebastien Pouliot (spouliot@motus.com) +// Sebastien Pouliot <sebastien@ximian.com> // // (C) 2002 Motus Technologies Inc. (http://www.motus.com) -// - -// // Copyright (C) 2004 Novell, Inc (http://www.novell.com) // // Permission is hereby granted, free of charge, to any person obtaining @@ -50,6 +47,9 @@ public sealed class StrongNamePublicKeyBlob { internal static StrongNamePublicKeyBlob FromString (string s) { + if ((s == null) || (s.Length == 0)) + return null; + int length = s.Length / 2; byte [] array = new byte [length]; |