private PermissionSet ParsePermissionSet(Parser parser)
 {
     SecurityElement topElement = parser.GetTopElement();
     PermissionSet set = new PermissionSet(PermissionState.None);
     set.FromXml(topElement);
     return set;
 }
Example #2
0
        // We can provide a default implementation of FromXmlString because we require 
        // every DSA implementation to implement ImportParameters
        // All we have to do here is parse the XML. 
 
        public override void FromXmlString(String xmlString) {
            if (xmlString == null) throw new ArgumentNullException("xmlString"); 
            Contract.EndContractBlock();
            DSAParameters dsaParams = new DSAParameters();
            Parser p = new Parser(xmlString);
            SecurityElement topElement = p.GetTopElement(); 

            // P is always present 
            String pString = topElement.SearchForTextOfLocalName("P"); 
            if (pString == null) {
                throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString","DSA","P")); 
            }
            dsaParams.P = Convert.FromBase64String(Utils.DiscardWhiteSpaces(pString));

            // Q is always present 
            String qString = topElement.SearchForTextOfLocalName("Q");
            if (qString == null) { 
                throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString","DSA","Q")); 
            }
            dsaParams.Q = Convert.FromBase64String(Utils.DiscardWhiteSpaces(qString)); 

            // G is always present
            String gString = topElement.SearchForTextOfLocalName("G");
            if (gString == null) { 
                throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString","DSA","G"));
            } 
            dsaParams.G = Convert.FromBase64String(Utils.DiscardWhiteSpaces(gString)); 

            // Y is always present 
            String yString = topElement.SearchForTextOfLocalName("Y");
            if (yString == null) {
                throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString","DSA","Y"));
            } 
            dsaParams.Y = Convert.FromBase64String(Utils.DiscardWhiteSpaces(yString));
 
            // J is optional 
            String jString = topElement.SearchForTextOfLocalName("J");
            if (jString != null) dsaParams.J = Convert.FromBase64String(Utils.DiscardWhiteSpaces(jString)); 

            // X is optional -- private key if present
            String xString = topElement.SearchForTextOfLocalName("X");
            if (xString != null) dsaParams.X = Convert.FromBase64String(Utils.DiscardWhiteSpaces(xString)); 

            // Seed and PgenCounter are optional as a unit -- both present or both absent 
            String seedString = topElement.SearchForTextOfLocalName("Seed"); 
            String pgenCounterString = topElement.SearchForTextOfLocalName("PgenCounter");
            if ((seedString != null) && (pgenCounterString != null)) { 
                dsaParams.Seed = Convert.FromBase64String(Utils.DiscardWhiteSpaces(seedString));
                dsaParams.Counter = Utils.ConvertByteArrayToInt(Convert.FromBase64String(Utils.DiscardWhiteSpaces(pgenCounterString)));
            } else if ((seedString != null) || (pgenCounterString != null)) {
                if (seedString == null) { 
                    throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString","DSA","Seed"));
                } else { 
                    throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString","DSA","PgenCounter")); 
                }
            } 

            ImportParameters(dsaParams);
        }
 public override void FromXmlString(string xmlString)
 {
     if (xmlString == null)
     {
         throw new ArgumentNullException("xmlString");
     }
     DSAParameters parameters = new DSAParameters();
     SecurityElement topElement = new Parser(xmlString).GetTopElement();
     string inputBuffer = topElement.SearchForTextOfLocalName("P");
     if (inputBuffer == null)
     {
         throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", new object[] { "DSA", "P" }));
     }
     parameters.P = Convert.FromBase64String(Utils.DiscardWhiteSpaces(inputBuffer));
     string str2 = topElement.SearchForTextOfLocalName("Q");
     if (str2 == null)
     {
         throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", new object[] { "DSA", "Q" }));
     }
     parameters.Q = Convert.FromBase64String(Utils.DiscardWhiteSpaces(str2));
     string str3 = topElement.SearchForTextOfLocalName("G");
     if (str3 == null)
     {
         throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", new object[] { "DSA", "G" }));
     }
     parameters.G = Convert.FromBase64String(Utils.DiscardWhiteSpaces(str3));
     string str4 = topElement.SearchForTextOfLocalName("Y");
     if (str4 == null)
     {
         throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", new object[] { "DSA", "Y" }));
     }
     parameters.Y = Convert.FromBase64String(Utils.DiscardWhiteSpaces(str4));
     string str5 = topElement.SearchForTextOfLocalName("J");
     if (str5 != null)
     {
         parameters.J = Convert.FromBase64String(Utils.DiscardWhiteSpaces(str5));
     }
     string str6 = topElement.SearchForTextOfLocalName("X");
     if (str6 != null)
     {
         parameters.X = Convert.FromBase64String(Utils.DiscardWhiteSpaces(str6));
     }
     string str7 = topElement.SearchForTextOfLocalName("Seed");
     string str8 = topElement.SearchForTextOfLocalName("PgenCounter");
     if ((str7 != null) && (str8 != null))
     {
         parameters.Seed = Convert.FromBase64String(Utils.DiscardWhiteSpaces(str7));
         parameters.Counter = Utils.ConvertByteArrayToInt(Convert.FromBase64String(Utils.DiscardWhiteSpaces(str8)));
     }
     else if ((str7 != null) || (str8 != null))
     {
         if (str7 == null)
         {
             throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", new object[] { "DSA", "Seed" }));
         }
         throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", new object[] { "DSA", "PgenCounter" }));
     }
     this.ImportParameters(parameters);
 }
 public override void FromXmlString(string xmlString)
 {
     if (xmlString == null)
     {
         throw new ArgumentNullException("xmlString");
     }
     RSAParameters parameters = new RSAParameters();
     SecurityElement topElement = new Parser(xmlString).GetTopElement();
     string inputBuffer = topElement.SearchForTextOfLocalName("Modulus");
     if (inputBuffer == null)
     {
         throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", new object[] { "RSA", "Modulus" }));
     }
     parameters.Modulus = Convert.FromBase64String(Utils.DiscardWhiteSpaces(inputBuffer));
     string str2 = topElement.SearchForTextOfLocalName("Exponent");
     if (str2 == null)
     {
         throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", new object[] { "RSA", "Exponent" }));
     }
     parameters.Exponent = Convert.FromBase64String(Utils.DiscardWhiteSpaces(str2));
     string str3 = topElement.SearchForTextOfLocalName("P");
     if (str3 != null)
     {
         parameters.P = Convert.FromBase64String(Utils.DiscardWhiteSpaces(str3));
     }
     string str4 = topElement.SearchForTextOfLocalName("Q");
     if (str4 != null)
     {
         parameters.Q = Convert.FromBase64String(Utils.DiscardWhiteSpaces(str4));
     }
     string str5 = topElement.SearchForTextOfLocalName("DP");
     if (str5 != null)
     {
         parameters.DP = Convert.FromBase64String(Utils.DiscardWhiteSpaces(str5));
     }
     string str6 = topElement.SearchForTextOfLocalName("DQ");
     if (str6 != null)
     {
         parameters.DQ = Convert.FromBase64String(Utils.DiscardWhiteSpaces(str6));
     }
     string str7 = topElement.SearchForTextOfLocalName("InverseQ");
     if (str7 != null)
     {
         parameters.InverseQ = Convert.FromBase64String(Utils.DiscardWhiteSpaces(str7));
     }
     string str8 = topElement.SearchForTextOfLocalName("D");
     if (str8 != null)
     {
         parameters.D = Convert.FromBase64String(Utils.DiscardWhiteSpaces(str8));
     }
     this.ImportParameters(parameters);
 }
Example #5
0
        // Import/export functions

        // We can provide a default implementation of FromXmlString because we require 
        // every RSA implementation to implement ImportParameters
        // All we have to do here is parse the XML.

        public override void FromXmlString(String xmlString) {
            if (xmlString == null) throw new ArgumentNullException("xmlString");
            Contract.EndContractBlock();
            RSAParameters rsaParams = new RSAParameters();
            Parser p = new Parser(xmlString);
            SecurityElement topElement = p.GetTopElement();

            // Modulus is always present
            String modulusString = topElement.SearchForTextOfLocalName("Modulus");
            if (modulusString == null) {
                throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString","RSA","Modulus"));
            }
            rsaParams.Modulus = Convert.FromBase64String(Utils.DiscardWhiteSpaces(modulusString));

            // Exponent is always present
            String exponentString = topElement.SearchForTextOfLocalName("Exponent");
            if (exponentString == null) {
                throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString","RSA","Exponent"));
            }
            rsaParams.Exponent = Convert.FromBase64String(Utils.DiscardWhiteSpaces(exponentString));

            // P is optional
            String pString = topElement.SearchForTextOfLocalName("P");
            if (pString != null) rsaParams.P = Convert.FromBase64String(Utils.DiscardWhiteSpaces(pString));

            // Q is optional
            String qString = topElement.SearchForTextOfLocalName("Q");
            if (qString != null) rsaParams.Q = Convert.FromBase64String(Utils.DiscardWhiteSpaces(qString));

            // DP is optional
            String dpString = topElement.SearchForTextOfLocalName("DP");
            if (dpString != null) rsaParams.DP = Convert.FromBase64String(Utils.DiscardWhiteSpaces(dpString));

            // DQ is optional
            String dqString = topElement.SearchForTextOfLocalName("DQ");
            if (dqString != null) rsaParams.DQ = Convert.FromBase64String(Utils.DiscardWhiteSpaces(dqString));

            // InverseQ is optional
            String inverseQString = topElement.SearchForTextOfLocalName("InverseQ");
            if (inverseQString != null) rsaParams.InverseQ = Convert.FromBase64String(Utils.DiscardWhiteSpaces(inverseQString));

            // D is optional
            String dString = topElement.SearchForTextOfLocalName("D");
            if (dString != null) rsaParams.D = Convert.FromBase64String(Utils.DiscardWhiteSpaces(dString));

            ImportParameters(rsaParams);
        }
        [System.Security.SecurityCritical]  // auto-generated
        private static Object Deserialize(byte[] blob)
        {
            if (blob == null)
                return null;

            if (blob[0] == 0)
            {
                Parser parser = new Parser( blob, Tokenizer.ByteTokenEncoding.UTF8Tokens, 1 );
                SecurityElement root = parser.GetTopElement();
                if (root.Tag.Equals( "IPermission" ) || root.Tag.Equals( "Permission" ))
                {
                    IPermission ip = System.Security.Util.XMLUtil.CreatePermission( root, PermissionState.None, false );

                    if (ip == null)
                    {
                        return null;
                    }

                    ip.FromXml( root );

                    return ip;
                }
                else if (root.Tag.Equals( "PermissionSet" ))
                {
                    PermissionSet permissionSet = new PermissionSet();

                    permissionSet.FromXml( root, false, false );

                    return permissionSet;
                }
                else if (root.Tag.Equals( "PermissionToken" ))
                {
                    PermissionToken pToken = new PermissionToken();

                    pToken.FromXml( root );

                    return pToken;
                }
                else
                {
                    return null;
                }

            }
            else
            {
                Object obj = null;
                using(MemoryStream stream = new MemoryStream( blob, 1, blob.Length - 1 )) {
                    obj = CrossAppDomainSerializer.DeserializeObject(stream);
                }

                Contract.Assert( !(obj is IPermission), "IPermission should be xml deserialized" );
                Contract.Assert( !(obj is PermissionSet), "PermissionSet should be xml deserialized" );

                return obj;
            }
        }
Example #7
0
 private static object Deserialize(byte[] blob)
 {
     if (blob == null)
     {
         return null;
     }
     if (blob[0] == 0)
     {
         SecurityElement topElement = new Parser(blob, Tokenizer.ByteTokenEncoding.UTF8Tokens, 1).GetTopElement();
         if (topElement.Tag.Equals("IPermission") || topElement.Tag.Equals("Permission"))
         {
             IPermission permission = XMLUtil.CreatePermission(topElement, PermissionState.None, false);
             if (permission == null)
             {
                 return null;
             }
             permission.FromXml(topElement);
             return permission;
         }
         if (topElement.Tag.Equals("PermissionSet"))
         {
             PermissionSet set = new PermissionSet();
             set.FromXml(topElement, false, false);
             return set;
         }
         if (topElement.Tag.Equals("PermissionToken"))
         {
             PermissionToken token = new PermissionToken();
             token.FromXml(topElement);
             return token;
         }
         return null;
     }
     using (MemoryStream stream = new MemoryStream(blob, 1, blob.Length - 1))
     {
         return CrossAppDomainSerializer.DeserializeObject(stream);
     }
 }
        private PermissionSet ParsePermissionSet(Parser parser)
        {
            SecurityElement e = parser.GetTopElement();
            PermissionSet permSet = new PermissionSet( PermissionState.None );
            permSet.FromXml( e );

            return permSet;
        }
        private static PolicyLevel LoadPolicyLevelFromStringHelper (string str, string path, PolicyLevelType type)
        {
            if (str == null)
                throw new ArgumentNullException( "str" );
            Contract.EndContractBlock();

            PolicyLevel level = new PolicyLevel(type, path);

            Parser parser = new Parser( str );
            SecurityElement elRoot = parser.GetTopElement();
            if (elRoot == null)
                throw new ArgumentException( String.Format( CultureInfo.CurrentCulture, Environment.GetResourceString( "Policy_BadXml" ), "configuration" ) );

            SecurityElement elMscorlib = elRoot.SearchForChildByTag( "mscorlib" );
            if (elMscorlib == null)
                throw new ArgumentException( String.Format( CultureInfo.CurrentCulture, Environment.GetResourceString( "Policy_BadXml" ), "mscorlib" ) );

            SecurityElement elSecurity = elMscorlib.SearchForChildByTag( "security" );
            if (elSecurity == null)
                throw new ArgumentException( String.Format( CultureInfo.CurrentCulture, Environment.GetResourceString( "Policy_BadXml" ), "security" ) );

            SecurityElement elPolicy = elSecurity.SearchForChildByTag( "policy" );
            if (elPolicy == null)
                throw new ArgumentException( String.Format( CultureInfo.CurrentCulture, Environment.GetResourceString( "Policy_BadXml" ), "policy" ) );

            SecurityElement elPolicyLevel = elPolicy.SearchForChildByTag( "PolicyLevel" );
            if (elPolicyLevel != null)
                level.FromXml( elPolicyLevel );
            else
                throw new ArgumentException( String.Format( CultureInfo.CurrentCulture, Environment.GetResourceString( "Policy_BadXml" ), "PolicyLevel" ) );

            return level;
        }
        private static SecurityElement GetLocalIntranetElement() {
            string[] repStrs = new string[s_LocalIntranetPolicySearchStrings.Length];

            repStrs[0] = ThisAssembly.Version;
            repStrs[1] = Environment.GetResourceString("Policy_PS_LocalIntranet");

            SecurityElement intranetSet = new Parser(s_localIntranetPermissionSet, s_LocalIntranetPolicySearchStrings, repStrs).GetTopElement();
            AppendRegistryInfoToNamedPermissionSet(intranetSet, 1);
            return intranetSet;
        }
Example #11
0
        private void Load( bool quickCacheOk )
        {
            // Load the appropriate security.cfg file from disk, parse it into
            // SecurityElements and pass it to the FromXml method.
            // Note: we do all sorts of lazy parsing and creation of objects
            // even after we supposedly "load" everything using this method.
        
            // Note: we could have given back some cache results before doing
            // a load, but the load may decide that the file is incorrectly
            // formatted, reset the policy level and turn caching off.  This
            // should be ok since we will only have a valid cache file when
            // the config file has not changed and is known to be correctly
            // formatted.                                                           
        
            Parser parser;
            byte[] fileArray = Config.GetData( m_configId );
            SecurityElement elRoot;
            Exception exception = null;
            bool noConfig = false;

            if (fileArray == null)
            {
#if _DEBUG
                LoadError( String.Format( Environment.GetResourceString( "Error_NoConfigFile" ), m_label ) );
#endif
                noConfig = true;
                goto SETDEFAULT;
            }
            else
            {
                MemoryStream stream = new MemoryStream( fileArray );
                StreamReader reader = new StreamReader( stream );

                try
                {
                    parser = new Parser( reader );
                }
                catch (Exception ex)
                {
                    String message;

                    if (ex.Message != null && !ex.Message.Equals( "" ))
                    {
                        message = ex.Message;
                    }
                    else
                    {
                        message = ex.GetType().AssemblyQualifiedName;
                    }

                    exception = LoadError( String.Format( Environment.GetResourceString( "Error_SecurityPolicyFileParseEx" ), m_label, message ) );
                    goto SETDEFAULT;
                }
            }

            elRoot = parser.GetTopElement();
            
            if (elRoot == null)
            {
                exception = LoadError( String.Format( Environment.GetResourceString( "Error_SecurityPolicyFileParse" ), m_label ) );
                goto SETDEFAULT;
            }

            SecurityElement elMscorlib = elRoot.SearchForChildByTag( "mscorlib" );

            if (elMscorlib == null)
            {
                exception = LoadError( String.Format( Environment.GetResourceString( "Error_SecurityPolicyFileParse" ), m_label ) );
                goto SETDEFAULT;
            }
            
            SecurityElement elSecurity = elMscorlib.SearchForChildByTag( "security" );
            
            if (elSecurity == null)
            {
                exception = LoadError( String.Format( Environment.GetResourceString( "Error_SecurityPolicyFileParse" ), m_label ) );
                goto SETDEFAULT;
            }
            
            SecurityElement elPolicy = elSecurity.SearchForChildByTag( "policy" );

            if (elPolicy == null)
            {
                exception = LoadError( String.Format( Environment.GetResourceString( "Error_SecurityPolicyFileParse" ), m_label ) );
                goto SETDEFAULT;
            }

            SecurityElement elPolicyLevel = elPolicy.SearchForChildByTag( "PolicyLevel" );
            
            if (elPolicyLevel != null)
            {
                try
                {
                    this.FromXml( elPolicyLevel );
                }
                catch (Exception)
                {
                    exception = LoadError( String.Format( Environment.GetResourceString( "Error_SecurityPolicyFileParse" ), m_label ) );
                    goto SETDEFAULT;
                }
            }
            else
            {
                exception = LoadError( String.Format( Environment.GetResourceString( "Error_SecurityPolicyFileParse" ), m_label ) );
                goto SETDEFAULT;
            }

            this.m_encoding = parser.GetEncoding();

            // Here we'll queue a work item to generate the quick cache if we've
            // discovered that we need to do that.  The reason we do this on another
            // thread is to avoid the recursive load situation that can come up if
            // we are currently loading an assembly that is referenced in policy.
            // We have the additional caveat that if we try this during a prejit
            // we end up making all these unnecessary references to the assemblies
            // referenced in policy, and therefore if we are in the compilation domain
            // we'll just not generate the cache.

            m_loaded = true;

            if (m_generateQuickCacheOnLoad && !Config.IsCompilationDomain() && SecurityManager.SecurityEnabled)
            {
                Monitor.Enter( SecurityManager.GetCodeAccessSecurityEngine() );

                if (s_waitingLevel == null)
                {
                    s_waitingLevel = this;
                    s_waitingThread = Thread.CurrentThread;
                    ThreadPool.UnsafeQueueUserWorkItem( new WaitCallback( GenerateQuickCache ), this );
                }   

                Monitor.Exit( SecurityManager.GetCodeAccessSecurityEngine() );
            }

            return;

        SETDEFAULT:
            bool caching = this.m_caching;

            Reset();

            if (caching)
            {
                this.m_caching = true;
                if (m_useDefaultCodeGroupsOnReset)
                    Config.SetQuickCache( this.m_configId, PolicyLevelData.s_quickCacheDefaultCodeGroups );
                else
                    Config.SetQuickCache( this.m_configId, PolicyLevelData.s_quickCacheUnrestricted );
            }

            m_loaded = true;

            if (noConfig && !Config.IsCompilationDomain() && SecurityManager.SecurityEnabled)
            {
                Monitor.Enter( SecurityManager.GetCodeAccessSecurityEngine() );

                if (s_waitingLevel == null)
                {
                    s_waitingLevel = this;
                    s_waitingThread = Thread.CurrentThread;
                    ThreadPool.UnsafeQueueUserWorkItem( new WaitCallback( GenerateConfigFile ), this );
                }

                Monitor.Exit( SecurityManager.GetCodeAccessSecurityEngine() );
            }

            if (exception != null)
                throw exception;
        }
Example #12
0
        private PolicyStatement CheckCache( int count, char[] serializedEvidence, out bool xmlError )
        {
            BCLDebug.Assert( m_configId != ConfigId.None, "PolicyLevels must have a valid config id to check the cache" );
        
            char[] cachedValue;
            PolicyStatement cachedSet = null;
            
            xmlError = false;
 
            if (!Config.GetCacheEntry( m_configId, count, serializedEvidence, out cachedValue ))
            {
                return null;
            }
            else
            {
                BCLDebug.Assert( cachedValue != null, "GetCacheData returned success but cached value is null" );
                                          
                cachedSet = new PolicyStatement();
                Parser parser = null;
                try
                {
                    parser = new Parser( cachedValue );
                    cachedSet.FromXml( parser.GetTopElement() );
                    return cachedSet;
                }
                catch (XmlSyntaxException)
                {
                    BCLDebug.Assert( false, "XmlSyntaxException in CheckCache" );
                    xmlError = true;
                    return null;
                }
            }
        }
Example #13
0
        /// <include file='doc\PermissionAttributes.uex' path='docs/doc[@for="PermissionSetAttribute.CreatePermissionSet"]/*' />
        public PermissionSet CreatePermissionSet()
        {
            if (m_unrestricted)
            {
                return new PermissionSet( PermissionState.Unrestricted );
            }
            if (m_name != null)
            {
                return PolicyLevel.GetBuiltInSet( m_name );
            }
            else if (m_file != null || m_xml != null)
            {
                Parser parser = null;
                
                if (m_file != null)
                {
                    Encoding[] encodings = new Encoding[] { null, Encoding.UTF8, Encoding.ASCII, Encoding.Unicode };

                    bool success = false;
                    Exception exception = null;
                    FileStream f = new FileStream( m_file, FileMode.Open, FileAccess.Read );

                    for (int i = 0; !success && i < encodings.Length; ++i)
                    {

                        try
                        {
                            f.Position = 0;

                            StreamReader reader;
                            if (encodings[i] != null)
                                reader = new StreamReader( f, encodings[i] );
                            else
                                reader = new StreamReader( f );

                            parser = new Parser( reader );
                            success = true;
                        }
                        catch (Exception e1)
                        {
                            if (exception == null)
                                exception = e1;
                        }
                    }

                    if (!success && exception != null)
                        throw exception;
                }
                else
                {
                    // Since we have a unicode string, we have to copy the bytes
                    // into a byte array and create a data reader.
                   
                    parser = new Parser( m_xml.ToCharArray() );
                }
            
                SecurityElement e = parser.GetTopElement();

                // Note: we can just assume this is a regular permission set because
                // the name and description are not used in declarative security.

                PermissionSet permSet = new PermissionSet( PermissionState.None );

                permSet.FromXml( e );

                return permSet;
            }
            else
            {
                return new PermissionSet( PermissionState.None );
            }
        }
 private static PolicyLevel LoadPolicyLevelFromStringHelper(string str, string path, PolicyLevelType type)
 {
     if (str == null)
     {
         throw new ArgumentNullException("str");
     }
     PolicyLevel level = new PolicyLevel(type, path);
     SecurityElement topElement = new Parser(str).GetTopElement();
     if (topElement == null)
     {
         throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Policy_BadXml"), new object[] { "configuration" }));
     }
     SecurityElement element2 = topElement.SearchForChildByTag("mscorlib");
     if (element2 == null)
     {
         throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Policy_BadXml"), new object[] { "mscorlib" }));
     }
     SecurityElement element3 = element2.SearchForChildByTag("security");
     if (element3 == null)
     {
         throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Policy_BadXml"), new object[] { "security" }));
     }
     SecurityElement element4 = element3.SearchForChildByTag("policy");
     if (element4 == null)
     {
         throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Policy_BadXml"), new object[] { "policy" }));
     }
     SecurityElement e = element4.SearchForChildByTag("PolicyLevel");
     if (e == null)
     {
         throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Policy_BadXml"), new object[] { "PolicyLevel" }));
     }
     level.FromXml(e);
     return level;
 }
Example #15
0
        static public PolicyLevel LoadPolicyLevelFromString( String str, PolicyLevelType type )
        {
#if _DEBUG
            if (debug)
            {
                DEBUG_OUT( "Input string =" );
                DEBUG_OUT( str );
            }
#endif 

            if (str == null)
                throw new ArgumentNullException( "str" );

            String name = Enum.GetName( typeof( PolicyLevelType ), type );

            if (name == null)
                return null;

            Parser parser = new Parser( str );

            PolicyLevel level = new PolicyLevel( name, ConfigId.None, type == PolicyLevelType.Machine );

            SecurityElement elRoot = parser.GetTopElement();
            
            if (elRoot == null)
            {
                throw new ArgumentException( String.Format( Environment.GetResourceString( "Policy_BadXml" ), "configuration" ) );
            }
            
            SecurityElement elMscorlib = elRoot.SearchForChildByTag( "mscorlib" );
            
            if (elMscorlib == null)
            {
                throw new ArgumentException( String.Format( Environment.GetResourceString( "Policy_BadXml" ), "mscorlib" ) );
            }
            
            SecurityElement elSecurity = elMscorlib.SearchForChildByTag( "security" );

            if (elSecurity == null)
            {
                throw new ArgumentException( String.Format( Environment.GetResourceString( "Policy_BadXml" ), "security" ) );
            }

            SecurityElement elPolicy = elSecurity.SearchForChildByTag( "policy" );
                
            if (elPolicy == null)
            {
                throw new ArgumentException( String.Format( Environment.GetResourceString( "Policy_BadXml" ), "policy" ) );
            }

            SecurityElement elPolicyLevel = elPolicy.SearchForChildByTag( "PolicyLevel" );
            
            if (elPolicyLevel != null)
            {
                level.FromXml( elPolicyLevel );
            }
            else
            {
                throw new ArgumentException( String.Format( Environment.GetResourceString( "Policy_BadXml" ), "PolicyLevel" ) );
            }

            level.Loaded = true; 

            return level;
        }
        /// <include file='doc\PermissionRequestEvidence.uex' path='docs/doc[@for="PermissionRequestEvidence.char1"]/*' />
        /// <internalonly/>
        int IBuiltInEvidence.InitFromBuffer( char[] buffer, int position )
        {
            int numPermSets = BuiltInEvidenceHelper.GetIntFromCharArray(buffer, position);
            position += 2;

            int tempLength;
            for (int i = 0; i < numPermSets; i++)
            {
                char psKind = buffer[position++];

                tempLength = BuiltInEvidenceHelper.GetIntFromCharArray(buffer, position);
                position += 2;

                String tempStr = new String(buffer, position, tempLength);
                position += tempLength;
                Parser p = new Parser( tempStr );

                PermissionSet psTemp = new PermissionSet();
                psTemp.FromXml(p.GetTopElement());

                switch(psKind)
                {
                    case idRequest:
                        m_strRequest = tempStr;
                        m_request = psTemp;
                        break;

                    case idOptional:
                        m_strOptional = tempStr;
                        m_optional = psTemp;
                        break;

                    case idDenied:
                        m_strDenied = tempStr;
                        m_denied = psTemp;
                        break;

                    default:
                        throw new SerializationException(Environment.GetResourceString("Serialization_UnableToFixup"));
                }
            }

            return position;
        }