Пример #1
0
 public SignatureDescription(SecurityElement el) {
     if (el == null) throw new ArgumentNullException("el");
     Contract.EndContractBlock();
     _strKey = el.SearchForTextOfTag("Key");
     _strDigest = el.SearchForTextOfTag("Digest");
     _strFormatter = el.SearchForTextOfTag("Formatter");
     _strDeformatter = el.SearchForTextOfTag("Deformatter");
 }
Пример #2
0
 public SignatureDescription(SecurityElement el)
 {
     if (el == null)
         throw new ArgumentNullException(nameof(el));
     KeyAlgorithm = el.SearchForTextOfTag("Key");
     DigestAlgorithm = el.SearchForTextOfTag("Digest");
     FormatterAlgorithm = el.SearchForTextOfTag("Formatter");
     DeformatterAlgorithm = el.SearchForTextOfTag("Deformatter");
 }
Пример #3
0
 public SignatureDescription(SecurityElement el)
 {
     if (el == null)
     {
         throw new ArgumentNullException(nameof(el));
     }
     KeyAlgorithm         = el.SearchForTextOfTag("Key");
     DigestAlgorithm      = el.SearchForTextOfTag("Digest");
     FormatterAlgorithm   = el.SearchForTextOfTag("Formatter");
     DeformatterAlgorithm = el.SearchForTextOfTag("Deformatter");
 }
 public void SearchForTextOfTag_Tag_Null()
 {
     try {
         elem.SearchForTextOfTag(null);
         Assert.Fail("#1");
     } catch (ArgumentNullException ex) {
         Assert.AreEqual(typeof(ArgumentNullException), ex.GetType(), "#2");
         Assert.IsNull(ex.InnerException, "#3");
         Assert.IsNotNull(ex.Message, "#4");
         Assert.IsNotNull(ex.ParamName, "#5");
         Assert.AreEqual("tag", ex.ParamName, "#6");
     }
 }
Пример #5
0
        public void SearchForTextOfTag()
        {
            SecurityElement elem = CreateElement();
            string          s    = elem.SearchForTextOfTag("ENDPOINT");

            Assert.Equal("some text", s);
        }
    // Add a child element to the specified security element.
    private static SecurityElement AddChildElement(
        SecurityElement parentElement,
        string tagName,
        string tagText)
    {
        if (parentElement != null)
        {
            // Ensure that the tag text is in valid XML format.
            //<Snippet9>
            if (!SecurityElement.IsValidText(tagText))
            //</Snippet9>
            {
                // Replace invalid text with valid XML text
                // to enforce proper XML formatting.
                //<Snippet19>
                tagText = SecurityElement.Escape(tagText);
                //</Snippet19>
            }

            // Determine whether the tag is in valid XML format.
            //<Snippet10>
            if (SecurityElement.IsValidTag(tagName))
            //</Snippet10>
            {
                //<Snippet24>
                SecurityElement childElement;
                childElement = parentElement.SearchForChildByTag(tagName);
                //</Snippet24>

                if (childElement != null)
                {
                    //<Snippet25>
                    String elementText;
                    elementText = parentElement.SearchForTextOfTag(tagName);
                    //</Snippet25>

                    if (!elementText.Equals(tagText))
                    {
                        // Add child element to the parent security element.
                        parentElement.AddChild(
                            new SecurityElement(tagName, tagText));
                    }
                }
                else
                {
                    // Add child element to the parent security element.
                    parentElement.AddChild(
                        new SecurityElement(tagName, tagText));
                }
            }
        }
        return(parentElement);
    }
    // Delete the specified security element if the current time is past
    // the time stored in the destroytime tag.
    private static SecurityElement DestroyTree(SecurityElement xmlElement)
    {
        SecurityElement localXmlElement = xmlElement;
        SecurityElement destroyElement  =
            localXmlElement.SearchForChildByTag("destroytime");

        // Verify that a destroytime tag exists.
        //<Snippet17>
        if (localXmlElement.SearchForChildByTag("destroytime") != null)
        //</Snippet17>
        {
            // Retrieve the destroytime text to get the time
            // the tree can be destroyed.
            //<Snippet18>
            string storedDestroyTime =
                localXmlElement.SearchForTextOfTag("destroytime");
            //</Snippet18>

            DateTime destroyTime = DateTime.Parse(storedDestroyTime);
            if (DateTime.Now > destroyTime)
            {
                localXmlElement = null;
                Console.WriteLine("The XML security tree has been deleted.");
            }
        }

        // Verify that xmlElement is of type SecurityElement.
        //<Snippet21>
        if (xmlElement.GetType().Equals(
                typeof(System.Security.SecurityElement)))
        //</Snippet21>
        {
            // Determine whether the localXmlElement object
            // differs from xmlElement.
            //<Snippet20>
            if (xmlElement.Equals(localXmlElement))
            //</Snippet20>
            {
                // Verify that the tags, attributes and children of the
                // two security elements are identical.
                //<Snippet22>
                if (xmlElement.Equal(localXmlElement))
                //</Snippet22>
                {
                    // Return the original security element.
                    return(xmlElement);
                }
            }
        }

        // Return the modified security element.
        return(localXmlElement);
    }
        public void SearchForTextOfTag_Tag_Null()
        {
            SecurityElement elem = CreateElement();

            try
            {
                elem.SearchForTextOfTag(null);
                Assert.False(true);
            }
            catch (ArgumentNullException ex)
            {
                Assert.Equal(typeof(ArgumentNullException), ex.GetType());
                Assert.Null(ex.InnerException);
                Assert.NotNull(ex.Message);
                Assert.NotNull(ex.ParamName);
                Assert.Equal("tag", ex.ParamName);
            }
        }
Пример #9
0
        public void SearchForTextOfTag_Tag_Null()
        {
            SecurityElement elem = CreateElement();

            try
            {
                elem.SearchForTextOfTag(null);
                Assert.False(true);
            }
            catch (ArgumentNullException ex)
            {
                Assert.Equal(typeof(ArgumentNullException), ex.GetType());
                Assert.Null(ex.InnerException);
                if (!PlatformDetection.IsNetNative)  // .Net Native toolchain optimizes away exception messages and paramnames.
                {
                    Assert.NotNull(ex.Message);
                    Assert.NotNull(ex.ParamName);
                    Assert.Equal("tag", ex.ParamName);
                }
            }
        }
Пример #10
0
        public KuSigner(string xmlString)
        {
            if (string.IsNullOrEmpty(xmlString))
            {
                throw new ArgumentNullException("xmlString");
            }

            SecurityElement topElement = SecurityElement.FromString(xmlString);

            string publicString = topElement.SearchForTextOfTag("Public");

            if (string.IsNullOrEmpty(publicString))
            {
                throw new ArgumentException("Input string does not contain a valid encoding of the 'PublicKey' parameter.");
            }

            _publicKey = Convert.FromBase64String(publicString);

            string signatureLengthString = topElement.SearchForTextOfTag("SignatureLength");

            if (string.IsNullOrEmpty(signatureLengthString))
            {
                throw new ArgumentException("Input string does not contain a valid encoding of the 'SignatureLength' parameter.");
            }

            _signatureLength = int.Parse(signatureLengthString, NumberStyles.Integer, CultureInfo.InvariantCulture);
            _keyLength       = _publicKey.Length / _signatureLength;

            if (_keyLength <= 0)
            {
                throw new ArgumentOutOfRangeException("keyLength");
            }

            if (_signatureLength <= 0 || _signatureLength > HASH_LENGTH)
            {
                throw new ArgumentOutOfRangeException("signatureLength");
            }

            string privateString = topElement.SearchForTextOfTag("Private");

            if (string.IsNullOrEmpty(privateString))
            {
                return;
            }

            _privateKey = Convert.FromBase64String(privateString);

            if (_publicKey.Length != _privateKey.Length)
            {
                throw new ArgumentException("publicKey.Length != privateKey.Length");
            }

            if (0 == _publicKey.Length)
            {
                throw new ArgumentException("0 == publicKey.Length");
            }

            if (_publicKey.Length < _signatureLength)
            {
                throw new ArgumentException("publicKey.Length < signatureLength");
            }

            if (_publicKey.Length % _signatureLength != 0)
            {
                throw new ArgumentException("publicKey.Length % signatureLength != 0");
            }
        }