Beispiel #1
0
        public void FromHex()
        {
            Assert.IsNull(CryptoConvert.FromHex(null), "FromHex(null)");
            string result = BitConverter.ToString(CryptoConvert.FromHex("0123456789aBcDeF"));

            Assert.AreEqual("01-23-45-67-89-AB-CD-EF", result, "0123456789abcdef");
        }
Beispiel #2
0
        public override IPermission CreatePermission()
        {
            if (this.Unrestricted)
            {
                return(new PublisherIdentityPermission(PermissionState.Unrestricted));
            }

            SSCX.X509Certificate x509 = null;
            if (x509data != null)
            {
                byte[] rawcert = CryptoConvert.FromHex(x509data);
                x509 = new SSCX.X509Certificate(rawcert);
                return(new PublisherIdentityPermission(x509));
            }
            if (certFile != null)
            {
                x509 = SSCX.X509Certificate.CreateFromCertFile(certFile);
                return(new PublisherIdentityPermission(x509));
            }
            if (signedFile != null)
            {
                x509 = SSCX.X509Certificate.CreateFromSignedFile(signedFile);
                return(new PublisherIdentityPermission(x509));
            }
            return(new PublisherIdentityPermission(PermissionState.None));
        }
        /// <summary>Reconstructs an <see cref="T:System.Security.Policy.ApplicationTrust" /> object with a given state from an XML encoding.</summary>
        /// <param name="element">The XML encoding to use to reconstruct the <see cref="T:System.Security.Policy.ApplicationTrust" /> object. </param>
        /// <exception cref="T:System.ArgumentNullException">
        ///   <paramref name="element" /> is null. </exception>
        /// <exception cref="T:System.ArgumentException">The XML encoding used for <paramref name="element" /> is invalid.</exception>
        /// <PermissionSet>
        ///   <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
        /// </PermissionSet>
        public void FromXml(SecurityElement element)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }
            if (element.Tag != "ApplicationTrust")
            {
                throw new ArgumentException("element");
            }
            string text = element.Attribute("FullName");

            if (text != null)
            {
                this._appid = new ApplicationIdentity(text);
            }
            else
            {
                this._appid = null;
            }
            this._defaultPolicy = null;
            SecurityElement securityElement = element.SearchForChildByTag("DefaultGrant");

            if (securityElement != null)
            {
                for (int i = 0; i < securityElement.Children.Count; i++)
                {
                    SecurityElement securityElement2 = securityElement.Children[i] as SecurityElement;
                    if (securityElement2.Tag == "PolicyStatement")
                    {
                        this.DefaultGrantSet.FromXml(securityElement2, null);
                        break;
                    }
                }
            }
            if (!bool.TryParse(element.Attribute("TrustedToRun"), out this._trustrun))
            {
                this._trustrun = false;
            }
            if (!bool.TryParse(element.Attribute("Persist"), out this._persist))
            {
                this._persist = false;
            }
            this._xtranfo = null;
            SecurityElement securityElement3 = element.SearchForChildByTag("ExtraInfo");

            if (securityElement3 != null)
            {
                text = securityElement3.Attribute("Data");
                if (text != null)
                {
                    byte[] buffer = CryptoConvert.FromHex(text);
                    using (MemoryStream memoryStream = new MemoryStream(buffer))
                    {
                        BinaryFormatter binaryFormatter = new BinaryFormatter();
                        this._xtranfo = binaryFormatter.Deserialize(memoryStream);
                    }
                }
            }
        }
        public void FromHex()
        {
            AssertNull("FromHex(null)", CryptoConvert.FromHex(null));
            string result = BitConverter.ToString(CryptoConvert.FromHex("0123456789aBcDeF"));

            AssertEquals("0123456789abcdef", "01-23-45-67-89-AB-CD-EF", result);
        }
Beispiel #5
0
        /// <summary>Reconstructs a security object with a specified state from an XML encoding.</summary>
        /// <param name="e">The XML encoding to use to reconstruct the security object. </param>
        /// <param name="level">The policy level context, used to resolve named permission set references. </param>
        /// <exception cref="T:System.ArgumentNullException">The <paramref name="e" /> parameter is null. </exception>
        /// <exception cref="T:System.ArgumentException">The <paramref name="e" /> parameter is not a valid membership condition element. </exception>
        public void FromXml(SecurityElement e, PolicyLevel level)
        {
            MembershipConditionHelper.CheckSecurityElement(e, "e", this.version, this.version);
            this.hash_value = CryptoConvert.FromHex(e.Attribute("HashValue"));
            string text = e.Attribute("HashAlgorithm");

            this.hash_algorithm = ((text != null) ? HashAlgorithm.Create(text) : null);
        }
Beispiel #6
0
        public void FromXml(SecurityElement e, PolicyLevel level)
        {
            MembershipConditionHelper.CheckSecurityElement(e, "e", version, version);

            hash_value = CryptoConvert.FromHex(e.Attribute("HashValue"));

            string algorithm = e.Attribute("HashAlgorithm");

            hash_algorithm = (algorithm == null) ? null : HashAlgorithm.Create(algorithm);
        }
        /// <summary>Reconstructs a security object with a specified state from an XML encoding.</summary>
        /// <param name="e">The XML encoding to use to reconstruct the security object. </param>
        /// <param name="level">The <see cref="T:System.Security.Policy.PolicyLevel" /> context, used to resolve <see cref="T:System.Security.NamedPermissionSet" /> references. </param>
        /// <exception cref="T:System.ArgumentNullException">The <paramref name="e" /> parameter is null. </exception>
        /// <exception cref="T:System.ArgumentException">The <paramref name="e" /> parameter is not a valid membership condition element. </exception>
        public void FromXml(SecurityElement e, PolicyLevel level)
        {
            MembershipConditionHelper.CheckSecurityElement(e, "e", this.version, this.version);
            string text = e.Attribute("X509Certificate");

            if (text != null)
            {
                byte[] data = CryptoConvert.FromHex(text);
                this.x509 = new X509Certificate(data);
            }
        }
Beispiel #8
0
        /// <summary>Reconstructs a permission with a specified state from an XML encoding.</summary>
        /// <param name="esd">The XML encoding to use to reconstruct the permission. </param>
        /// <exception cref="T:System.ArgumentNullException">The <paramref name="esd" /> parameter is null. </exception>
        /// <exception cref="T:System.ArgumentException">The <paramref name="esd" /> parameter is not a valid permission element.-or- The <paramref name="esd" /> parameter's version number is not valid. </exception>
        /// <PermissionSet>
        ///   <IPermission class="System.Security.Permissions.KeyContainerPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="Create" />
        /// </PermissionSet>
        public override void FromXml(SecurityElement esd)
        {
            CodeAccessPermission.CheckSecurityElement(esd, "esd", 1, 1);
            string text = esd.Attributes["X509v3Certificate"] as string;

            if (text != null)
            {
                byte[] data = CryptoConvert.FromHex(text);
                this.x509 = new X509Certificate(data);
            }
        }
        public void FromXml(SecurityElement e, PolicyLevel level)
        {
            MembershipConditionHelper.CheckSecurityElement(e, "e", version, version);
            string cert = e.Attribute("X509Certificate");

            if (cert != null)
            {
                byte[] rawcert = CryptoConvert.FromHex(cert);
                x509 = new X509Certificate(rawcert);
            }
            // PolicyLevel isn't used as there's no need to resolve NamedPermissionSet references
        }
        public static byte[] GetMappedPublicKey(byte[] token)
        {
            if (StrongNameManager.mappings == null || token == null)
            {
                return(null);
            }
            string key  = CryptoConvert.ToHex(token);
            string text = (string)StrongNameManager.mappings[key];

            if (text == null)
            {
                return(null);
            }
            return(CryptoConvert.FromHex(text));
        }
Beispiel #11
0
        private static string ReadHex(string value, ref int pos)
        {
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append(value[pos++]);
            stringBuilder.Append(value[pos]);
            if (pos < value.Length - 4 && value[pos + 1] == '\\' && X501.IsHex(value[pos + 2]))
            {
                pos += 2;
                stringBuilder.Append(value[pos++]);
                stringBuilder.Append(value[pos]);
            }
            byte[] bytes = CryptoConvert.FromHex(stringBuilder.ToString());
            return(Encoding.UTF8.GetString(bytes));
        }
        public override void FromXml(SecurityElement esd)
        {
            // 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)
            {
                byte[] rawcert = CryptoConvert.FromHex(cert);
                x509 = new X509Certificate(rawcert);
            }
        }
Beispiel #13
0
        static public byte[] GetMappedPublicKey(byte[] token)
        {
            if ((mappings == null) || (token == null))
            {
                return(null);
            }

            string t  = CryptoConvert.ToHex(token);
            string pk = (string)mappings [t];

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

            return(CryptoConvert.FromHex(pk));
        }
Beispiel #14
0
        static string ReadHex(string value, ref int pos)
        {
            StringBuilder sb = new StringBuilder();

            // it is (at least an) 8 bits char
            sb.Append(value[pos++]);
            sb.Append(value[pos]);
            // look ahead for a 16 bits char
            if ((pos < value.Length - 4) && (value[pos + 1] == '\\') && IsHex(value[pos + 2]))
            {
                pos += 2;                 // pass last char and skip \
                sb.Append(value[pos++]);
                sb.Append(value[pos]);
            }
            byte[] data = CryptoConvert.FromHex(sb.ToString());
            return(Encoding.UTF8.GetString(data));
        }
Beispiel #15
0
        public PermissionSet CreatePermissionSet()
        {
            PermissionSet pset = null;

#if !NET_2_1
            if (this.Unrestricted)
            {
                pset = new PermissionSet(PermissionState.Unrestricted);
            }
            else
            {
                pset = new PermissionSet(PermissionState.None);
                if (name != null)
                {
                    return(PolicyLevel.CreateAppDomainLevel().GetNamedPermissionSet(name));
                }
                else if (file != null)
                {
                    Encoding e = ((isUnicodeEncoded) ? System.Text.Encoding.Unicode : System.Text.Encoding.ASCII);
                    using (StreamReader sr = new StreamReader(file, e)) {
                        pset = CreateFromXml(sr.ReadToEnd());
                    }
                }
                else if (xml != null)
                {
                    pset = CreateFromXml(xml);
                }
#if NET_2_0
                else if (hex != null)
                {
                    // Unicode isn't supported
                    //Encoding e = ((isUnicodeEncoded) ? System.Text.Encoding.Unicode : System.Text.Encoding.ASCII);
                    Encoding e   = System.Text.Encoding.ASCII;
                    byte[]   bin = CryptoConvert.FromHex(hex);
                    pset = CreateFromXml(e.GetString(bin, 0, bin.Length));
                }
#endif
            }
#endif
            return(pset);
        }
Beispiel #16
0
        // -hash algo -hex hash
        // -hash algo -file assemblyname
        static IMembershipCondition ProcessHashMembership(string[] args, ref int i)
        {
            HashAlgorithm ha = HashAlgorithm.Create(args [++i]);

            byte [] value = null;
            switch (args [++i])
            {
            case "-hex":
                value = CryptoConvert.FromHex(args [++i]);
                break;

            case "-file":
                Hash hash = new Hash(GetAssembly(args [++i]));
                value = hash.GenerateHash(ha);
                break;

            default:
                return(null);
            }
            return(new HashMembershipCondition(ha, value));
        }
 /// <summary>Creates and returns a new instance of <see cref="T:System.Security.Permissions.PublisherIdentityPermission" />.</summary>
 /// <returns>A <see cref="T:System.Security.Permissions.PublisherIdentityPermission" /> that corresponds to this attribute.</returns>
 /// <PermissionSet>
 ///   <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
 ///   <IPermission class="System.Security.Permissions.KeyContainerPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="Create" />
 /// </PermissionSet>
 public override IPermission CreatePermission()
 {
     if (base.Unrestricted)
     {
         return(new PublisherIdentityPermission(PermissionState.Unrestricted));
     }
     if (this.x509data != null)
     {
         byte[]          data        = CryptoConvert.FromHex(this.x509data);
         X509Certificate certificate = new X509Certificate(data);
         return(new PublisherIdentityPermission(certificate));
     }
     if (this.certFile != null)
     {
         X509Certificate certificate = System.Security.Cryptography.X509Certificates.X509Certificate.CreateFromCertFile(this.certFile);
         return(new PublisherIdentityPermission(certificate));
     }
     if (this.signedFile != null)
     {
         X509Certificate certificate = System.Security.Cryptography.X509Certificates.X509Certificate.CreateFromSignedFile(this.signedFile);
         return(new PublisherIdentityPermission(certificate));
     }
     return(new PublisherIdentityPermission(PermissionState.None));
 }
Beispiel #18
0
        // -pub -cert certificate
        // -pub -file signedfile
        // -pub -hex rawdata
        static IMembershipCondition ProcessPublisherMembership(string[] args, ref int i)
        {
            X509Certificate cert = null;

            switch (args [++i])
            {
            case "-cert":
                cert = X509Certificate.CreateFromCertFile(args [++i]);
                break;

            case "-file":
                cert = X509Certificate.CreateFromSignedFile(args [++i]);
                break;

            case "-hex":
                byte[] raw = CryptoConvert.FromHex(args [++i]);
                cert = new X509Certificate(raw);
                break;

            default:
                return(null);
            }
            return(new PublisherMembershipCondition(cert));
        }
Beispiel #19
0
 public void ToHex()
 {
     Assert.IsNull(CryptoConvert.FromHex(null), "FromHex(null)");
     byte[] data = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
     Assert.AreEqual("0123456789ABCDEF", CryptoConvert.ToHex(data), "0123456789abcdef");
 }
Beispiel #20
0
 public void FromHex_NonMultipleOf2()
 {
     CryptoConvert.FromHex("abc");
 }
Beispiel #21
0
 public void FromHex_NonHexChars()
 {
     CryptoConvert.FromHex("abcdefgh");
 }
Beispiel #22
0
        public void FromXml(SecurityElement element)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }

            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);
                    }
                }
            }
        }