Exemplo n.º 1
0
        public override Task <object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
        {
            try
            {
                var body = base.ReadBodyFromStream(readStream, content);

                if (type == typeof(Bundle))
                {
                    if (XmlSignatureHelper.IsSigned(body))
                    {
                        if (!XmlSignatureHelper.VerifySignature(body))
                        {
                            throw Error.BadRequest("Digital signature in body failed verification");
                        }
                    }
                }

                if (typeof(Resource).IsAssignableFrom(type))
                {
                    Resource resource = _parser.Parse <Resource>(body);
                    return(Task.FromResult <object>(resource));
                }
                else
                {
                    throw Error.Internal("The type {0} expected by the controller can not be deserialized", type.Name);
                }
            }
            catch (FormatException exc)
            {
                throw Error.BadRequest("Body parsing failed: " + exc.Message);
            }
        }
Exemplo n.º 2
0
        public void TestSigning()
        {
            Bundle b = new Bundle();

            b.Title       = "Updates to resource 233";
            b.Id          = new Uri("urn:uuid:0d0dcca9-23b9-4149-8619-65002224c3");
            b.LastUpdated = new DateTimeOffset(2012, 11, 2, 14, 17, 21, TimeSpan.Zero);
            b.AuthorName  = "Ewout Kramer";

            ResourceEntry <Patient> p = new ResourceEntry <Patient>();

            p.Id            = new ResourceIdentity("http://test.com/fhir/Patient/233");
            p.Resource      = new Patient();
            p.Resource.Name = new List <HumanName> {
                HumanName.ForFamily("Kramer").WithGiven("Ewout")
            };
            b.Entries.Add(p);

            var certificate = getCertificate();

            var bundleData   = FhirSerializer.SerializeBundleToXmlBytes(b);
            var bundleXml    = Encoding.UTF8.GetString(bundleData);
            var bundleSigned = XmlSignatureHelper.Sign(bundleXml, certificate);

            _signedXml = bundleSigned;

            using (var response = postBundle(bundleSigned))
            {
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    TestResult.Fail("Server refused POSTing signed document at /");
                }
            }
        }
Exemplo n.º 3
0
        public void Sign(X509Certificate2 certificate)
        {
            string xml = GetDefinitionsXml();
            var    signatureElement = XmlSignatureHelper.Sign(xml, certificate);

            XmlSerializer xmlSerializer = new XmlSerializer(typeof(SignatureType));

            using (StringReader stringReader = new StringReader(signatureElement.OuterXml))
            {
                Signature = ((SignatureType)(xmlSerializer.Deserialize(System.Xml.XmlReader.Create(stringReader))));
            }
        }
Exemplo n.º 4
0
        public void TestSigning()
        {
            Bundle b = new Bundle();

            b.Title       = "Updates to resource 233";
            b.Id          = new Uri("urn:uuid:0d0dcca9-23b9-4149-8619-65002224c3");
            b.LastUpdated = new DateTimeOffset(2012, 11, 2, 14, 17, 21, TimeSpan.Zero);
            b.AuthorName  = "Ewout Kramer";

            ResourceEntry <Patient> p = new ResourceEntry <Patient>();

            p.Id            = new ResourceIdentity("http://test.com/fhir/Patient/233");
            p.Resource      = new Patient();
            p.Resource.Name = new List <HumanName> {
                HumanName.ForFamily("Kramer").WithGiven("Ewout")
            };
            b.Entries.Add(p);

            var myAssembly = typeof(TestXmlSignature).Assembly;
            var stream     = myAssembly.GetManifestResourceStream("Spark.Tests.spark.pfx");

            var data = new byte[stream.Length];

            stream.Read(data, 0, (int)stream.Length);
            var certificate = new X509Certificate2(data);

            var bundleData = FhirSerializer.SerializeBundleToXmlBytes(b);
            var bundleXml  = Encoding.UTF8.GetString(bundleData);

            var bundleSigned = XmlSignatureHelper.Sign(bundleXml, certificate);

            Assert.IsTrue(XmlSignatureHelper.IsSigned(bundleSigned));
            Assert.IsTrue(XmlSignatureHelper.VerifySignature(bundleSigned));

            var changedBundle = bundleSigned.Replace("<name>Ewout", "<name>Ewald");

            Assert.AreEqual(bundleSigned.Length, changedBundle.Length);

            Assert.IsFalse(XmlSignatureHelper.VerifySignature(changedBundle));
        }
        public override Task <object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
        {
            return(System.Threading.Tasks.Task.Factory.StartNew <object>(() =>
            {
                try
                {
                    var body = ReadBodyFromStream(readStream, content);

                    if (type == typeof(Bundle))
                    {
                        if (XmlSignatureHelper.IsSigned(body))
                        {
                            if (!XmlSignatureHelper.VerifySignature(body))
                            {
                                throw Error.BadRequest("Digital signature in body failed verification");
                            }
                        }
                    }

                    if (!typeof(Resource).IsAssignableFrom(type))
                    {
                        throw Error.Internal("The type {0} expected by the controller can not be deserialized",
                                             type.Name);
                    }

                    //var fhirparser = new FhirJsonParser();
                    //var resource = fhirparser.Parse(body, type);
                    var fhirXmlParser = new FhirXmlParser();
                    var resource = fhirXmlParser.Parse(body, type);
                    return resource;
                }
                catch (FormatException exc)
                {
                    throw Error.BadRequest("Body parsing failed: " + exc.Message);
                }
            }));
        }
Exemplo n.º 6
0
 public bool VerifySignature(IEnumerable <X509Certificate2> trustedCertificates)
 {
     return(XmlSignatureHelper.VerifySignature(GetDefinitionsXml(), trustedCertificates));
 }
Exemplo n.º 7
0
 public bool VerifySignature(out X509Certificate2 certificate)
 {
     return(XmlSignatureHelper.VerifySignature(GetDefinitionsXml(), out certificate));
 }