MQTTnet icon indicating copy to clipboard operation
MQTTnet copied to clipboard

TLS with ASP.Net Core 2.1

Open MattComb opened this issue 5 years ago • 31 comments

Would it be possible to extend the documentation for ASP.Net Core 2.1 on how to add an encrypted endpoint. I have tried the approaches in the document but does not seem to work with the ASP.Net Core approach.

How can I add an mqtts endpoint listening on 8883?

MattComb avatar Oct 30 '18 07:10 MattComb

I do it as follows on ASP.NET Core 2.1 and it works. I can add this in formation to the wiki if @chkr1011 agrees.

var x509 = new X509Certificate2(Configuration["Mqtt:Certificate"], "");

services.AddHostedMqttServerWithServices(builder => {
    builder
        .WithDefaultEndpoint()
        .WithEncryptedEndpoint()
        .WithEncryptionCertificate(x509.Export(X509ContentType.Pfx));
});

Note that I use a pfx certificate. The way of loading/exporting the certificate showed in the examples never worked for me (and I have still to understand why).

Also, "Mqtt:Certificate" is the configuration key that contains the full path to the pfx certificate file (X509, public key and private key). This isn't optimal, it would be better to access it using a file provider. If you have the certificate and the private key in different files you can always use openssl to join them in a pfx.

fogzot avatar Oct 30 '18 08:10 fogzot

This does not work for me, here is my Configure Services stuff.

       services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));

        var mqttServerOptions = new MqttServerOptionsBuilder()
            .WithConnectionValidator(c =>
                {
                   // some stuff that works
                }
            )
            .WithSubscriptionInterceptor(context =>
                {
                   // some stuff that works
                }
            )
            .WithApplicationMessageInterceptor(context =>
                {
                    // some stuff that works
                }
            )
            .Build();

        services.AddHostedMqttServer(mqttServerOptions);

        //supposed to add tcp support but actually next line does ?!?
        services.AddMqttConnectionHandler();

        //does this add tcp
        services.AddMqttTcpServerAdapter();

        // trying to add tls but doesn't open on 8883
        var x509 = new X509Certificate(@"some cert path here", "password");
        services.AddHostedMqttServerWithServices(builder => {
            builder
                .WithEncryptedEndpoint()
                .WithEncryptedEndpointPort(8883)
                .WithEncryptionCertificate(x509.Export(X509ContentType.Cert));
        });

        services.AddMqttWebSocketServerAdapter();

MattComb avatar Oct 30 '18 13:10 MattComb

Currently I'm able to achieve ws, wss, mqtt but not mqtts (using tcp) It feels like I'm missing an option to add tls to the tcp ?

MattComb avatar Oct 30 '18 13:10 MattComb

The call to services.AddMqttTcpServerAdapter() should be enough.

fogzot avatar Oct 30 '18 15:10 fogzot

unfortunately it is not working

MattComb avatar Oct 30 '18 21:10 MattComb

services.AddMqttTcpServerAdapter(); is the old api that works on .net sockets

services.AddMqttConnectionHandler(); is the new api for AspnetCore.Connections.Abstractions but it doesnt support tls yet

JanEggers avatar Nov 02 '18 10:11 JanEggers

Your other option, dependent on how you are deploying, may be to terminate TLS with HAProxy or Nginx, then just have an unecrypted connection from there to the backend service be unencrypted.

dealproc avatar Dec 27 '18 08:12 dealproc

When will this be available? I need this as well.

SeppPenner avatar Apr 25 '19 08:04 SeppPenner

this depends on

https://github.com/aspnet/AspNetCore/issues/4623

JanEggers avatar Apr 25 '19 08:04 JanEggers

@JanEggers: Thank you for the information. Hopefully, NetCore 3.0 (Initial release) will be there in August or so...

SeppPenner avatar Apr 25 '19 08:04 SeppPenner

@JanEggers The issue from AspNetCore is fixed now. Just for information.

SeppPenner avatar Jun 25 '19 08:06 SeppPenner

@SeppPenner I will wait some more until 3.0 is released. and im not sure if we can update because 3.0 does not have .net framework support.

JanEggers avatar Jun 26 '19 12:06 JanEggers

@JanEggers Yeah, that's a good idea.

SeppPenner avatar Jun 26 '19 14:06 SeppPenner

I do it as follows on ASP.NET Core 2.1 and it works. I can add this in formation to the wiki if @chkr1011 agrees.

var x509 = new X509Certificate2(Configuration["Mqtt:Certificate"], "");

services.AddHostedMqttServerWithServices(builder => {
    builder
        .WithDefaultEndpoint()
        .WithEncryptedEndpoint()
        .WithEncryptionCertificate(x509.Export(X509ContentType.Pfx));
});

Note that I use a pfx certificate. The way of loading/exporting the certificate showed in the examples never worked for me (and I have still to understand why).

Also, "Mqtt:Certificate" is the configuration key that contains the full path to the pfx certificate file (X509, public key and private key). This isn't optimal, it would be better to access it using a file provider. If you have the certificate and the private key in different files you can always use openssl to join them in a pfx.

Hi, Could you please help me with setting up MQTTNet server/broker with both TCP and WS. TLS is not required right now. I tried with both ASP.NET Core 20 and 2.1, but dud not succeed.

nibirc avatar Mar 23 '20 03:03 nibirc

Currently I'm able to achieve ws, wss, mqtt but not mqtts (using tcp) It feels like I'm missing an option to add tls to the tcp ?

Dear Sir, can you please share your code?

nibirc avatar Mar 23 '20 06:03 nibirc

@JanEggers What is needed here? I would like to fix this issue and https://github.com/chkr1011/MQTTnet/issues/756 with one feature branch (Now that I have more time due to home office because of Corona :D).

SeppPenner avatar Mar 29 '20 15:03 SeppPenner

there is no official tls middleware yet so you could start with https://github.com/dotnet/orleans/blob/a6bf5940a515aea09b0143c58c54f1872f655203/src/Orleans.Connections.Security/Security/TlsServerConnectionMiddleware.cs

and create your own that is plugged in the pipeline.

JanEggers avatar Mar 30 '20 05:03 JanEggers

Well, I just need to start the server in the pipeline and allow to set options with the builder. Anything else? This shouldn't be so difficult, I would say.

SeppPenner avatar May 03 '20 16:05 SeppPenner

Turns out there a couple things that more complicated that might be expected:

  1. You cannot add any fields to the current MqttClientOptionsBuilderTlsParameters as this is not build as a string indexed dictionary.
  2. If you have any need to access information about the TLS channel, that is difficult as it does not seem to be propagated all the way back. I had to add some extra fields in to the base code to get the TLS Exporter in the validation function code.

jimsch avatar May 05 '20 01:05 jimsch

You cannot add any fields to the current MqttClientOptionsBuilderTlsParameters as this is not build as a string indexed dictionary.

I already expected that.

If you have any need to access information about the TLS channel, that is difficult as it does not seem to be propagated all the way back. I had to add some extra fields in to the base code to get the TLS Exporter in the validation function code.

Ok, good to know. I will check and see what I can do here.

SeppPenner avatar May 05 '20 08:05 SeppPenner

this is blocked by https://github.com/davidfowl/BedrockFramework/pull/52 and https://github.com/davidfowl/BedrockFramework/pull/56

JanEggers avatar May 05 '20 09:05 JanEggers

how's the progress

egops avatar Jan 28 '21 03:01 egops

@JanEggers @SeppPenner @chkr1011 Hi How its progress? Is blocked by any issue?

behroozbc avatar Oct 25 '21 21:10 behroozbc

I have no idea about the status...

SeppPenner avatar Oct 28 '21 19:10 SeppPenner

@behroozbc I actually did not look at the status of bedrock. @davidfowl maybe there will be some new bits to play with when .net 6 releases

JanEggers avatar Oct 29 '21 05:10 JanEggers

Closing this due to inactivity. If the issue is not solved or closing is a mistake please feel free to reopen it.

chkr1011 avatar Jan 27 '22 19:01 chkr1011

hi @chkr1011, this issue is not solved.

behroozbc avatar Jan 27 '22 21:01 behroozbc

hi @JanEggers , Is new update about the status of this issue after .net 6 was released?

behroozbc avatar Jan 27 '22 21:01 behroozbc

Hey, there. We wish to move from .NET 4.8 to .NET 6 and we use TLS inside our MQTTnet server. Is there any news ?

riccardogas avatar Apr 05 '22 15:04 riccardogas

@riccardogas it works just fine like before. but there is still no "optimized tls middleware for tcp connections" from the dotnet team. project bedrock seems to be stale

JanEggers avatar Apr 06 '22 14:04 JanEggers