/// <summary>
        /// Constructor
        /// </summary>
        /// <param name="settings">Set of options to use</param>
        /// <param name="collaborationProtocolRegistry">Reference to the collaboration protocol registry</param>
        /// <param name="addressRegistry">Reference to the address registry</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        protected MessagingCore(
            MessagingSettings settings,
            ICollaborationProtocolRegistry collaborationProtocolRegistry,
            IAddressRegistry addressRegistry)
        {
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }
            if (collaborationProtocolRegistry == null)
            {
                throw new ArgumentNullException(nameof(collaborationProtocolRegistry));
            }
            if (addressRegistry == null)
            {
                throw new ArgumentNullException(nameof(addressRegistry));
            }

            Settings = settings;
            CollaborationProtocolRegistry = collaborationProtocolRegistry;
            AddressRegistry = addressRegistry;

            DefaultCertificateValidator = new CertificateValidator();
            DefaultMessageProtection    = new SignThenEncryptMessageProtection();
            ServiceBus = new ServiceBusCore(this);

            Settings.Validate();
        }
Beispiel #2
0
        public void Protect_Encryption_ArgumentNullException()
        {
            MemoryStream contentStream = new MemoryStream(Encoding.UTF8.GetBytes(_content.ToString()));

            var partyAProtection = new SignThenEncryptMessageProtection(TestCertificates.CounterpartyPrivateSigntature, TestCertificates.CounterpartyPrivateEncryption);

            partyAProtection.Protect(contentStream, null);
        }
Beispiel #3
0
        public void Protect_And_Unprotect_WrongEncryptionCertificate()
        {
            MemoryStream contentStream = new MemoryStream(Encoding.UTF8.GetBytes(_content.ToString()));

            var partyAProtection = new SignThenEncryptMessageProtection(TestCertificates.CounterpartyPrivateSigntature, TestCertificates.CounterpartyPrivateEncryption);
            // Random encryption certificate -> TestCertificates.CounterpartyPublicEncryption
            var stream = partyAProtection.Protect(contentStream, TestCertificates.CounterpartyPublicEncryption);

            var partyBProtection = new SignThenEncryptMessageProtection(TestCertificates.HelsenorgePrivateSigntature, TestCertificates.HelsenorgePrivateEncryption);
            var result           = partyBProtection.Unprotect(stream, TestCertificates.CounterpartyPublicSignature);
        }
Beispiel #4
0
        public void Unprotect_Signature_MissingPublicKeySignatureCertificate()
        {
            MemoryStream contentStream = new MemoryStream(Encoding.UTF8.GetBytes(_content.ToString()));

            var partyAProtection = new SignThenEncryptMessageProtection(TestCertificates.CounterpartyPrivateSigntature, TestCertificates.CounterpartyPrivateEncryption);
            var stream           = partyAProtection.Protect(contentStream, TestCertificates.HelsenorgePublicEncryption);

            var partyBProtection = new SignThenEncryptMessageProtection(TestCertificates.HelsenorgePrivateSigntature, TestCertificates.HelsenorgePrivateEncryption);
            var result           = partyBProtection.Unprotect(stream, null);

            Assert.AreEqual(_content.ToString(), result.ToXDocument().ToString());
        }
Beispiel #5
0
        public void Protect_And_Unprotect_WrongSigningCertificate()
        {
            const string wrongCertificateBase64 = "MIIE3jCCA8agAwIBAgILCE2BUrKlJGrOxOgwDQYJKoZIhvcNAQELBQAwSzELMAkGA1UEBhMCTk8xHTAbBgNVBAoMFEJ1eXBhc3MgQVMtOTgzMTYzMzI3MR0wGwYDVQQDDBRCdXlwYXNzIENsYXNzIDMgQ0EgMzAeFw0xNjAxMTgwOTA3NTZaFw0xOTAxMTgyMjU5MDBaMHIxCzAJBgNVBAYTAk5PMRswGQYDVQQKDBJOT1JTSyBIRUxTRU5FVFQgU0YxFTATBgNVBAsMDFRFU1RTRU5URVJFVDEbMBkGA1UEAwwSTk9SU0sgSEVMU0VORVRUIFNGMRIwEAYDVQQFEwk5OTQ1OTg3NTkwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCZ34VMBCzmHwmvMWwq0YhtNaEz19PxcEq3ImbCLWZx0zIf2hp8ZSDQy23KpgTumrTebeXEW5b1ig4THXizKzDtwirV5ssO441U7hvTXr+Bm1GYpRc1Q0vzZbKg41Nje5cq+kAovq3H8nnJ3csdjFS5QWKKz1hyUL9V6mZiR1eMVLWbOL2gBR6rjB0OgpoXtF9wmb2Z9So+srAyqnpRy9xBumBFdqvx3+8iZp8G9FH0TPgzeEPreLX5tdKZL0J/Z7+zWXqCx+Fu1PoKMkdw+aYJCVtUJPRXY1t4BpLKO0h6yXf7Rpky+sUQcJmKyagOBPZr9mqqjycYQg6JPSkcTo+XAgMBAAGjggGaMIIBljAJBgNVHRMEAjAAMB8GA1UdIwQYMBaAFMzD+Ae3nG16TvWnKx0F+bNHHJHRMB0GA1UdDgQWBBRpioossQ08OgpOuAl6/58qpAkvajAOBgNVHQ8BAf8EBAMCBkAwFQYDVR0gBA4wDDAKBghghEIBGgEDAjCBpQYDVR0fBIGdMIGaMC+gLaArhilodHRwOi8vY3JsLmJ1eXBhc3Mubm8vY3JsL0JQQ2xhc3MzQ0EzLmNybDBnoGWgY4ZhbGRhcDovL2xkYXAuYnV5cGFzcy5uby9kYz1CdXlwYXNzLGRjPU5PLENOPUJ1eXBhc3MlMjBDbGFzcyUyMDMlMjBDQSUyMDM/Y2VydGlmaWNhdGVSZXZvY2F0aW9uTGlzdDB6BggrBgEFBQcBAQRuMGwwMwYIKwYBBQUHMAGGJ2h0dHA6Ly9vY3NwLmJ1eXBhc3Mubm8vb2NzcC9CUENsYXNzM0NBMzA1BggrBgEFBQcwAoYpaHR0cDovL2NydC5idXlwYXNzLm5vL2NydC9CUENsYXNzM0NBMy5jZXIwDQYJKoZIhvcNAQELBQADggEBALPuCmA93Mi9NZFUFOaQz3PasTFLeLmtSXtt4Qp0TVtJuhqrlDeWYXDCsffMQoCAZXE3569/hdEgHPBVALo8xKS9vdwZR5SgIF+IivsEdC4ZYsq8C5VX4qq2WxW7yHNy3GYU8RBdOaztTfUliv7uaAeooP6EOPa6m+R+dgGfGnb5rM8NRyGgcAKDvC1YUFwdWaIgqO0gBB6WnSkhkyk0iX4tksUkbemQFcyMi2XDog6IFpkYt85MvfBklwjjufCiIcpkzHmuZCcYSLdwqi40Cz4QM5FE8zQYJJLco35A7NVW3MusyFImTleOlL10NH3XnqeLM8loa1Ph7YPl0SpiSjY=";
            var          wrongCertificate       = new X509Certificate2(Convert.FromBase64String(wrongCertificateBase64));
            MemoryStream contentStream          = new MemoryStream(Encoding.UTF8.GetBytes(_content.ToString()));

            var partyAProtection = new SignThenEncryptMessageProtection(TestCertificates.CounterpartyPrivateSigntature, TestCertificates.CounterpartyPrivateEncryption);
            var stream           = partyAProtection.Protect(
                contentStream,
                TestCertificates.HelsenorgePublicEncryption);

            var partyBProtection = new SignThenEncryptMessageProtection(TestCertificates.HelsenorgePrivateSigntature, TestCertificates.HelsenorgePrivateEncryption);
            var result           = partyBProtection.Unprotect(stream, wrongCertificate);
        }
Beispiel #6
0
        public void Protect_And_Unprotect_UsingLegacy_OK()
        {
            MemoryStream contentStream = new MemoryStream(Encoding.UTF8.GetBytes(_content.ToString()));

            var partyAProtection = new SignThenEncryptMessageProtection(TestCertificates.CounterpartyPrivateSigntature, TestCertificates.CounterpartyPrivateEncryption);
            var stream           = partyAProtection.Protect(contentStream, TestCertificates.HelsenorgePublicEncryption);

            var partyBProtection = new SignThenEncryptMessageProtection(
                TestCertificates.HelsenorgePrivateSigntature,
                TestCertificates.HelsenorgePrivateEncryption,
                TestCertificates.HelsenorgePrivateEncryption);  // Legacy certificate
            var result = partyBProtection.Unprotect(stream, TestCertificates.CounterpartyPublicSignature);

            Assert.AreEqual(_content.ToString(), result.ToXDocument().ToString());
        }
        public void Asynchronous_Receive_CertificateSignError()
        {
            Exception receiveException = null;

            var partyAProtection = new SignThenEncryptMessageProtection(TestCertificates.CounterpartyPrivateSigntature, TestCertificates.CounterpartyPrivateEncryption);

            Client = new MessagingClient(Settings, CollaborationRegistry, AddressRegistry, CertificateStore, CertificateValidator, partyAProtection);
            Client.ServiceBus.RegisterAlternateMessagingFactory(MockFactory);

            var partyBProtection = new SignThenEncryptMessageProtection(TestCertificates.HelsenorgePrivateSigntature, TestCertificates.HelsenorgePrivateEncryption);

            Server = new MessagingServer(Settings, Logger, LoggerFactory, CollaborationRegistry, AddressRegistry, CertificateStore, CertificateValidator, partyBProtection);
            Server.ServiceBus.RegisterAlternateMessagingFactory(MockFactory);

            CollaborationRegistry.SetupFindAgreementForCounterparty(i =>
            {
                var file = Path.Combine("Files", $"CPA_{i}_ChangedSignedCertificate.xml");
                return(File.Exists(file) == false ? null : File.ReadAllText(file));
            });

            RunAsynchronousReceive(
                postValidation: () => {
                Assert.IsTrue(_startingCalled);
                Assert.IsFalse(_receivedCalled);
                Assert.IsTrue(_completedCalled);
                var error = MockLoggerProvider.FindEntry(EventIds.RemoteCertificate);
                Assert.IsTrue(error.Message
                              .Contains($"{TestCertificates.HelsenorgePrivateSigntature.Thumbprint}"));
                Assert.IsTrue(error.Message
                              .Contains($"{TestCertificates.HelsenorgePrivateSigntature.NotBefore}"));
                var signingException = receiveException as CertificateException;
                Assert.IsNotNull(signingException);
                Assert.IsNotNull(signingException.Payload);
            },
                wait: () => _completedCalled,
                received: (m) => { },
                messageModification: (m) => { },
                handledException: ((m, e) =>
            {
                Server.Stop(TimeSpan.FromSeconds(10));
                _handledExceptionCalled = true;
                _completedCalled = true;
                receiveException = e;
            }),
                messageProtected: true);
        }
Beispiel #8
0
        private MockMessage CreateAsynchronousMessageProtected()
        {
            var signing   = new SignThenEncryptMessageProtection();
            var messageId = Guid.NewGuid().ToString("D");
            var path      = Path.Combine("Files", "Helsenorge_Message.xml");
            var file      = File.Exists(path) ? new XDocument(XElement.Load(path)) : null;
            var protect   = signing.Protect(file ?? GenericMessage, TestCertificates.HelsenorgePublicEncryption,
                                            TestCertificates.HelsenorgePrivateSigntature);

            return(new MockMessage(protect)
            {
                MessageFunction = "DIALOG_INNBYGGER_EKONTAKT",
                ApplicationTimestamp = DateTime.Now,
                ContentType = ContentType.SignedAndEnveloped,
                MessageId = messageId,
                CorrelationId = messageId,
                FromHerId = MockFactory.OtherHerId,
                ToHerId = MockFactory.HelsenorgeHerId,
                ScheduledEnqueueTimeUtc = DateTime.UtcNow,
                TimeToLive = TimeSpan.FromSeconds(15),
                ReplyTo = MockFactory.OtherParty.Asynchronous.Name,
                Queue = MockFactory.Helsenorge.Asynchronous.Messages,
            });
        }
Beispiel #9
0
        public void Protect_Data_ArgumentNullException()
        {
            var partyAProtection = new SignThenEncryptMessageProtection(TestCertificates.CounterpartyPrivateSigntature, TestCertificates.CounterpartyPrivateEncryption);

            partyAProtection.Protect(null, TestCertificates.HelsenorgePublicEncryption);
        }
Beispiel #10
0
 public void Setup()
 {
     _content    = new XDocument(new XElement("SomeDummyXml"));
     _protection = new SignThenEncryptMessageProtection();
 }