Exemple #1
0
        public void CreateSignatureWithoutSpecifyingDigest(EnvelopedSignatureTheoryData theoryData)
        {
            var context = TestUtilities.WriteHeader($"{this}.CreateSignatureWithoutSpecifyingDigest", theoryData);

            try
            {
                using (var buffer = new MemoryStream())
                {
                    var writer = new EnvelopedSignatureWriter(XmlWriter.Create(buffer), theoryData.SigningCredentials, theoryData.ReferenceId);
                    writer.WriteStartElement("EntityDescriptor", "urn:oasis:names:tc:SAML:2.0:metadata");
                    writer.WriteAttributeString("entityID", "issuer");
                    writer.WriteEndElement();

                    // read and verify signatures
                    EnvelopedSignatureReader envelopedReader = new EnvelopedSignatureReader(XmlUtilities.CreateDictionaryReader(Encoding.UTF8.GetString(buffer.ToArray())));
                    while (envelopedReader.Read())
                    {
                        ;
                    }

                    envelopedReader.Signature.Verify(theoryData.SigningCredentials.Key, theoryData.SigningCredentials.Key.CryptoProviderFactory);
                    theoryData.ExpectedException.ProcessNoException(context);
                }
            }
            catch (Exception ex)
            {
                theoryData.ExpectedException.ProcessException(ex, context);
            }

            TestUtilities.AssertFailIfErrors(context);
        }
Exemple #2
0
        public void RoundTripSaml2(EnvelopedSignatureTheoryData theoryData)
        {
            var context = TestUtilities.WriteHeader($"{this}.RoundTripSaml2", theoryData);

            try
            {
                var serializer    = new Saml2Serializer();
                var samlAssertion = serializer.ReadAssertion(XmlUtilities.CreateDictionaryReader(theoryData.Xml));
                var stream        = new MemoryStream();
                var writer        = XmlDictionaryWriter.CreateTextWriter(stream);
                samlAssertion.SigningCredentials = theoryData.SigningCredentials;
                serializer.WriteAssertion(writer, samlAssertion);
                writer.Flush();
                var xml = Encoding.UTF8.GetString(stream.ToArray());
                samlAssertion.SigningCredentials = null;
                var samlAssertion2 = serializer.ReadAssertion(XmlUtilities.CreateDictionaryReader(xml));
                samlAssertion2.Signature.Verify(theoryData.SigningCredentials.Key, theoryData.CryptoProviderFactory);
                IdentityComparer.AreEqual(samlAssertion, samlAssertion2, context);
            }
            catch (Exception ex)
            {
                theoryData.ExpectedException.ProcessException(ex, context);
            }

            TestUtilities.AssertFailIfErrors(context);
        }
Exemple #3
0
        public void RoundTripWsMetadata(EnvelopedSignatureTheoryData theoryData)
        {
            var context = TestUtilities.WriteHeader($"{this}.RoundTripWsMetadata", theoryData);

            try
            {
                var settings = new XmlWriterSettings
                {
                    Encoding = new UTF8Encoding(false)
                };

                var buffer = new MemoryStream();
                var esw    = new EnvelopedSignatureWriter(XmlWriter.Create(buffer, settings), theoryData.SigningCredentials, theoryData.ReferenceId);

                theoryData.Action.DynamicInvoke(esw);

                var metadata      = Encoding.UTF8.GetString(buffer.ToArray());
                var configuration = new WsFederationConfiguration();
                var reader        = XmlReader.Create(new StringReader(metadata));
                configuration = new WsFederationMetadataSerializer().ReadMetadata(reader);
                configuration.Signature.Verify(theoryData.SigningCredentials.Key, theoryData.SigningCredentials.Key.CryptoProviderFactory);
                theoryData.ExpectedException.ProcessNoException(context);
            }
            catch (Exception ex)
            {
                theoryData.ExpectedException.ProcessException(ex, context);
            }

            TestUtilities.AssertFailIfErrors(context);
        }
Exemple #4
0
        public void Constructor(EnvelopedSignatureTheoryData theoryData)
        {
            TestUtilities.WriteHeader($"{this}.Constructor", theoryData);
            try
            {
                var envelopedReader = new EnvelopedSignatureReader(theoryData.XmlReader);
                while (envelopedReader.Read())
                {
                    ;
                }

                if (theoryData.ExpectSignature)
                {
                    if (envelopedReader.Signature == null)
                    {
                        Assert.False(true, "theoryData.ExpectSignature == true && envelopedReader.ExpectSignature == null");
                    }

                    envelopedReader.Signature.Verify(theoryData.SecurityKey, theoryData.SecurityKey.CryptoProviderFactory);
                }

                theoryData.ExpectedException.ProcessNoException();
            }
            catch (Exception ex)
            {
                theoryData.ExpectedException.ProcessException(ex);
            }
        }
Exemple #5
0
        public void ReadSignedXml(EnvelopedSignatureTheoryData theoryData)
        {
            TestUtilities.WriteHeader($"{this}.ReadSignedXml", theoryData);
            var context = new CompareContext($"{this}.ReadSignedXml : {theoryData.TestId}.");

            try
            {
                var envelopedReader = XmlUtilities.CreateEnvelopedSignatureReader(theoryData.Xml);
                while (envelopedReader.Read())
                {
                    ;
                }

                if (theoryData.ExpectSignature)
                {
                    if (envelopedReader.Signature == null)
                    {
                        Assert.False(true, "theoryData.ExpectSignature == true && envelopedReader.Signature == null");
                    }

                    envelopedReader.Signature.Verify(theoryData.SecurityKey, theoryData.CryptoProviderFactory);
                }

                theoryData.ExpectedException.ProcessNoException(context);
            }
            catch (Exception ex)
            {
                theoryData.ExpectedException.ProcessException(ex, context);
            }

            TestUtilities.AssertFailIfErrors(context);
        }
        public void ReadXmlElements(EnvelopedSignatureTheoryData theoryData)
        {
            TestUtilities.WriteHeader($"{this}.ReadXmlElements", theoryData);
            var context = new CompareContext($"{this}.ReadXmlElements : {theoryData.TestId}.");

            try
            {
                XmlReader reader = XmlUtilities.CreateDictionaryReader(theoryData.Xml);

                EnvelopedSignatureReader envelopedReader;
                if (theoryData.XmlElementReader == null)
                {
                    envelopedReader = new EnvelopedSignatureReader(reader);
                }
                else
                {
                    envelopedReader = new EnvelopedSignatureReader(reader, theoryData.XmlElementReader);
                }

                while (envelopedReader.Read())
                {
                    ;
                }

                if (theoryData.XmlElementReader != null)
                {
                    foreach (var item in theoryData.XmlElementReader.Items)
                    {
                        if (item is SamlSecurityToken samlToken)
                        {
                            samlToken.Assertion.Signature.Verify(theoryData.TokenSecurityKey);
                        }

                        if (item is Saml2SecurityToken saml2Token)
                        {
                            saml2Token.Assertion.Signature.Verify(theoryData.TokenSecurityKey);
                        }
                    }
                }

                if (envelopedReader.Signature != null)
                {
                    envelopedReader.Signature.Verify(theoryData.SecurityKey, theoryData.SecurityKey.CryptoProviderFactory);
                }
            }
            catch (Exception ex)
            {
                theoryData.ExpectedException.ProcessException(ex, context);
            }

            TestUtilities.AssertFailIfErrors(context);
        }
Exemple #7
0
 public void Constructor(EnvelopedSignatureTheoryData theoryData)
 {
     TestUtilities.WriteHeader($"{this}.Constructor", theoryData);
     try
     {
         var envelopedWriter = new EnvelopedSignatureWriter(theoryData.XmlWriter, theoryData.SigningCredentials, theoryData.ReferenceId, theoryData.InclusiveNamespacesPrefixList);
         theoryData.ExpectedException.ProcessNoException();
     }
     catch (Exception ex)
     {
         theoryData.ExpectedException.ProcessException(ex);
     }
 }