Merge pull request #733 from amoiseev-softheme/bugfix/monofix
[mono.git] / mcs / class / corlib / System.Security.Permissions / PublisherIdentityPermission.cs
index c0cff298237bc7fe99d0cc0276a019cf42cb093d..924c3a57dae20bcf722dfc069873700e25446a2d 100644 (file)
@@ -5,11 +5,7 @@
 //     Sebastien Pouliot  <sebastien@ximian.com>
 //
 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
-// (C) 2004 Novell (http://www.novell.com)
-//
-
-//
-// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-using System;
+using System.Runtime.InteropServices;
 using System.Security.Cryptography.X509Certificates;
 
 using Mono.Security.Cryptography;
 
 namespace System.Security.Permissions {
 
+       [ComVisible (true)]
        [Serializable]
        public sealed class PublisherIdentityPermission : CodeAccessPermission, IBuiltInPermission {
-       
+
+               private const int version = 1;
+
                private X509Certificate x509;
        
                public PublisherIdentityPermission (PermissionState state) 
                {
-                       switch (state) {
-                               case PermissionState.None:
-                                       break;
-                               case PermissionState.Unrestricted:
-                                       throw new ArgumentException ("Invalid state for identity permission", "state");
-                               default:
-                                       throw new ArgumentException ("Unknown state", "state");
-                       }
+                       // false == do not allow Unrestricted for Identity Permissions
+                       CheckPermissionState (state, false);
                }
        
                public PublisherIdentityPermission (X509Certificate certificate) 
                {
+                       // reuse validation by the Certificate property
                        Certificate = certificate;
                }
        
@@ -64,7 +58,7 @@ namespace System.Security.Permissions {
                        get { return x509; }
                        set { 
                                if (value == null)
-                                       throw new ArgumentNullException ("value");
+                                       throw new ArgumentNullException ("X509Certificate");
                                x509 = value; 
                        }
                }
@@ -79,14 +73,10 @@ namespace System.Security.Permissions {
 
                public override void FromXml (SecurityElement esd) 
                {
-                       if (esd == null)
-                               throw new ArgumentNullException ("esd");
-                       if (esd.Tag != "IPermission")
-                               throw new ArgumentException ("not IPermission");
-                       if (!(esd.Attributes ["class"] as string).StartsWith ("System.Security.Permissions.PublisherIdentityPermission"))
-                               throw new ArgumentException ("not PublisherIdentityPermission");
-                       if ((esd.Attributes ["version"] as string) != "1")
-                               throw new ArgumentException ("wrong version");
+                       // General validation in CodeAccessPermission
+                       CheckSecurityElement (esd, "esd", version, version);
+                       // Note: we do not (yet) care about the return value 
+                       // as we only accept version 1 (min/max values)
 
                        string cert = (esd.Attributes ["X509v3Certificate"] as string);
                        if (cert != null) {
@@ -97,39 +87,33 @@ namespace System.Security.Permissions {
        
                public override IPermission Intersect (IPermission target) 
                {
-                       if (target == null)
+                       PublisherIdentityPermission pip = Cast (target);
+                       if (pip == null)
                                return null;
-                       if (! (target is PublisherIdentityPermission))
-                               throw new ArgumentException ("wrong type");
-
-                       PublisherIdentityPermission o = (PublisherIdentityPermission) target;
 
-                       if ((x509 != null) && (o.x509 != null)) {
-                               if (x509.GetRawCertDataString () == o.x509.GetRawCertDataString ())
-                                       return new PublisherIdentityPermission (o.x509);
+                       if ((x509 != null) && (pip.x509 != null)) {
+                               if (x509.GetRawCertDataString () == pip.x509.GetRawCertDataString ())
+                                       return new PublisherIdentityPermission (pip.x509);
                        }
                        return null;
                }
        
                public override bool IsSubsetOf (IPermission target) 
                {
-                       if (target == null)
+                       PublisherIdentityPermission pip = Cast (target);
+                       if (pip == null)
                                return false;
 
-                       if (! (target is PublisherIdentityPermission))
-                               throw new ArgumentException ("wrong type");
-
-                       PublisherIdentityPermission o = (PublisherIdentityPermission) target;
                        if (x509 == null)
                                return true;
-                       if (o.x509 == null)
+                       if (pip.x509 == null)
                                return false;
-                       return (x509.GetRawCertDataString () == o.x509.GetRawCertDataString ());
+                       return (x509.GetRawCertDataString () == pip.x509.GetRawCertDataString ());
                }
        
                public override SecurityElement ToXml () 
                {
-                       SecurityElement se = Element (this, 1);
+                       SecurityElement se = Element (version);
                        if (x509 != null)
                                se.AddAttribute ("X509v3Certificate", x509.GetRawCertDataString ());
                        return se;
@@ -137,20 +121,17 @@ namespace System.Security.Permissions {
        
                public override IPermission Union (IPermission target) 
                {
-                       if (target == null)
+                       PublisherIdentityPermission pip = Cast (target);
+                       if (pip == null)
                                return Copy ();
-                       if (! (target is PublisherIdentityPermission))
-                               throw new ArgumentException ("wrong type");
-
-                       PublisherIdentityPermission o = (PublisherIdentityPermission) target;
 
-                       if ((x509 != null) && (o.x509 != null)) {
-                               if (x509.GetRawCertDataString () == o.x509.GetRawCertDataString ())
+                       if ((x509 != null) && (pip.x509 != null)) {
+                               if (x509.GetRawCertDataString () == pip.x509.GetRawCertDataString ())
                                        return new PublisherIdentityPermission (x509); // any cert would do
                        }
-                       else if ((x509 == null) && (o.x509 != null))
-                               return new PublisherIdentityPermission (o.x509);
-                       else if ((x509 != null) && (o.x509 == null))
+                       else if ((x509 == null) && (pip.x509 != null))
+                               return new PublisherIdentityPermission (pip.x509);
+                       else if ((x509 != null) && (pip.x509 == null))
                                return new PublisherIdentityPermission (x509);
                        return null;
                }
@@ -158,7 +139,22 @@ namespace System.Security.Permissions {
                // IBuiltInPermission
                int IBuiltInPermission.GetTokenIndex ()
                {
-                       return 9;
+                       return (int) BuiltInToken.PublisherIdentity;
+               }
+
+               // helpers
+
+               private PublisherIdentityPermission Cast (IPermission target)
+               {
+                       if (target == null)
+                               return null;
+
+                       PublisherIdentityPermission pip = (target as PublisherIdentityPermission);
+                       if (pip == null) {
+                               ThrowInvalidPermission (target, typeof (PublisherIdentityPermission));
+                       }
+
+                       return pip;
                }
-       } 
+       }
 }