* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / corlib / System.Security.Policy / ApplicationTrust.cs
index 0ac43e51e2b921a01127c8e49124bdcac1f1ec63..24c74dc719a8e9cbe432d3019d7edcb1a0b7a2e4 100644 (file)
@@ -4,7 +4,7 @@
 // Author:
 //     Sebastien Pouliot  <sebastien@ximian.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
 
 #if NET_2_0
 
-using System;
-using System.Collections;
+using System.IO;
+using System.Runtime.InteropServices;
+using System.Runtime.Serialization.Formatters.Binary;
+using System.Security.Permissions;
+
+using Mono.Security.Cryptography;
 
 namespace System.Security.Policy {
 
-       public sealed class ApplicationTrust : ISecurityEncodable, ISecurityPolicyEncodable {
+       [ComVisible (true)]
+       public sealed class ApplicationTrust : ISecurityEncodable {
 
                private ApplicationIdentity _appid;
-               private IDictionary _grants;
                private PolicyStatement _defaultPolicy;
                private object _xtranfo;
                private bool _trustrun;
                private bool _persist;
 
-               [MonoTODO]
                public ApplicationTrust ()
                {
-                       _grants = (IDictionary) new Hashtable ();
                }
 
-               [MonoTODO]
                public ApplicationTrust (ApplicationIdentity applicationIdentity)
-                       : this ()
                {
+                       if (applicationIdentity == null)
+                               throw new ArgumentNullException ("applicationIdentity");
+                       _appid = applicationIdentity;
                }
 
                public ApplicationIdentity ApplicationIdentity {
                        get { return _appid; }
-               }
-
-               public IDictionary AssemblyGrants {
-                       get { return _grants; }
-                       set { _grants = value; }
+                       set {
+                               if (value == null)
+                                       throw new ArgumentNullException ("ApplicationIdentity");
+                               _appid = value;
+                       }
                }
 
                public PolicyStatement DefaultGrantSet {
-                       get { return _defaultPolicy; }
+                       get {
+                               if (_defaultPolicy == null)
+                                       _defaultPolicy = GetDefaultGrantSet ();
+
+                               return _defaultPolicy;
+                       }
                        set { _defaultPolicy = value; }
                }
 
@@ -83,48 +91,94 @@ namespace System.Security.Policy {
                        set { _persist = value; }
                }
 
-               [MonoTODO ("incomplete")]
-               public override bool Equals (object obj) 
-               {
-                       if (obj == null)
-                               return false;
-                       ApplicationTrust at = (obj as ApplicationTrust);
-                       if (at == null)
-                               return false;
-
-                       // TODO
-                       return false;
-               }
-
-               [MonoTODO ("incomplete")]
                public void FromXml (SecurityElement element) 
                {
                        if (element == null)
                                throw new ArgumentNullException ("element");
-                       throw new NotImplementedException ();
-               }
-
-               [MonoTODO ("incomplete - is PolicyLevel used ?")]
-               public void FromXml (SecurityElement element, PolicyLevel level) 
-               {
-                       FromXml (element);
-               }
 
-               public override int GetHashCode ()
-               {
-                       return base.GetHashCode ();
+                       if (element.Tag != "ApplicationTrust")
+                               throw new ArgumentException ("element");
+
+                       string s = element.Attribute ("FullName");
+                       if (s != null)
+                               _appid = new ApplicationIdentity (s);
+                       else
+                               _appid = null;
+
+                       _defaultPolicy = null;
+                       SecurityElement defaultGrant = element.SearchForChildByTag ("DefaultGrant");
+                       if (defaultGrant != null) {
+                               for (int i=0; i < defaultGrant.Children.Count; i++) {
+                                       SecurityElement se = (defaultGrant.Children [i] as SecurityElement);
+                                       if (se.Tag == "PolicyStatement") {
+                                               DefaultGrantSet.FromXml (se, null);
+                                               break;
+                                       }
+                               }
+                       }
+
+                       if (!Boolean.TryParse (element.Attribute ("TrustedToRun"), out _trustrun))
+                               _trustrun = false;
+
+                       if (!Boolean.TryParse (element.Attribute ("Persist"), out _persist))
+                               _persist = false;
+
+                       _xtranfo = null;
+                       SecurityElement xtra = element.SearchForChildByTag ("ExtraInfo");
+                       if (xtra != null) {
+                               s = xtra.Attribute ("Data");
+                               if (s != null) {
+                                       byte[] data = CryptoConvert.FromHex (s);
+                                       using (MemoryStream ms = new MemoryStream (data)) {
+                                               BinaryFormatter bf = new BinaryFormatter ();
+                                               _xtranfo = bf.Deserialize (ms);
+                                       }
+                               }
+                       }
                }
 
-               [MonoTODO ("incomplete")]
                public SecurityElement ToXml () 
                {
-                       throw new NotImplementedException ();
+                       SecurityElement se = new SecurityElement ("ApplicationTrust");
+                       se.AddAttribute ("version", "1");
+
+                       if (_appid != null) {
+                               se.AddAttribute ("FullName", _appid.FullName);
+                       }
+
+                       if (_trustrun) {
+                               se.AddAttribute ("TrustedToRun", "true");
+                       }
+
+                       if (_persist) {
+                               se.AddAttribute ("Persist", "true");
+                       }
+
+                       SecurityElement defaultGrant = new SecurityElement ("DefaultGrant");
+                       defaultGrant.AddChild (DefaultGrantSet.ToXml ());
+                       se.AddChild (defaultGrant);
+
+                       if (_xtranfo != null) {
+                               byte[] data = null;
+                               using (MemoryStream ms = new MemoryStream ()) {
+                                       BinaryFormatter bf = new BinaryFormatter ();
+                                       bf.Serialize (ms, _xtranfo);
+                                       data = ms.ToArray ();
+                               }
+                               SecurityElement xtra = new SecurityElement ("ExtraInfo");
+                               xtra.AddAttribute ("Data", CryptoConvert.ToHex (data));
+                               se.AddChild (xtra);
+                       }
+
+                       return se;
                }
 
-               [MonoTODO ("incomplete - is PolicyLevel used ?")]
-               public SecurityElement ToXml (PolicyLevel level) 
+               // internal stuff
+
+               private PolicyStatement GetDefaultGrantSet ()
                {
-                       return ToXml ();
+                       PermissionSet ps = new PermissionSet (PermissionState.None);
+                       return new PolicyStatement (ps);
                }
        }
 }