tomazbracic

tomazbracic

Dear all,

I am building a nerves based firmware where I will be using mTLS. For that I build my own CA and generated all the certificates. For shipping data I will be using emqtt client.

I have successfully connected and I am able to send messages between client and server service which is as well built with elixir. In-between there is NGINX on cloud edge for TLS termination and MQTT broker where messages go to.

WANT TO DO:
gateway <---- mTLS port: 8883 ------> Nginx <---- port: 1883 -----> EMQX broker ↔ my cloud service

TESTING:
mqttx GUI app <---- mTLS port: 8883 ------> Nginx <---- port: 1883 -----> EMQX broker ↔ my cloud service

I can use certificates and test everything with my MQTTX app. It works and I can see client connected to broker.

If I try to use the same certificates later with my “want to do” setup, so in elixir I get full of reconnections in my logs and one of the error that bothers me is something around hostname

16:52:58.810 [notice] TLS :client: In state :wait_cert at ssl_handshake.erl:2182 generated CLIENT ALERT: Fatal - Handshake Failure
 - {:bad_cert, :hostname_check_failed}

16:52:58.810 [info] Starting MQTT Client

16:52:58.811 [info] Client ID set to: testko

16:52:58.811 [debug] Timer set to 5000 milliseconds

16:52:58.944 [notice] TLS :client: In state :wait_cert at ssl_handshake.erl:2182 generated CLIENT ALERT: Fatal - Handshake Failure
 - {:bad_cert, :hostname_check_failed}

16:52:58.944 [info] Starting MQTT Client

16:52:58.944 [info] Client ID set to: testko

though... I used the same certs in MQTTX app on the same dev laptop and was successfully connected. So I guess there is still configuration issue?

My configuration is

config :testko, :emqtt,
  host: "prefix.mydomain.com",
  port: 8883,
  clientid: "testko",
  clean_start: false,
  ssl: true,
  ssl_opts: [
    cacertfile: "certs/ca-chain.cert.pem",
    certfile: "certs/client3.cert.pem",
    keyfile: "certs/client3.key.pem",
    tls_versions: [:"tlsv1.2", :"tlsv1.3"],
    verify: :verify_peer,
    fail_if_no_peer_cert: true
  ],
  name: :emqtt,
  reconnect: true,
  reconnect_interval: 10000

I even tried puthing cacertfile, certfile and keyfile in single quotes. Nothing really works.

Can anyone see anything problematic in my configuration? Does anybody have emqtt client configured with mTLS and actually works?

I would really appreciate any help/feedback/direction/blog/tutorial/doc where this would be explained.

Thanks in advance!

Tomaz

Showing Posts 1 to 5

tomazbracic

tomazbracic OP

I tested the certificates with openssl tool as well. Based on that PKI part is ok. mTLS works. But how to configure this with emqtt is now a mistery to me.

LostKobrakai

LostKobrakai

As mentioned on slack many of those setting need to be charlists instead of binaries based on the specs on the emqtt repo and ones pointed to on OTP:

  • host
  • ssl_opts → all …_file ones

You can prefix the quoted values with ~c"…" to make them charlists.

clientid is speced as iodata, which is a supertype to binary, so that one is fine.

tomazbracic

tomazbracic OP

I did that as well.

config :testko, :emqtt,
  host: ~c"prefix.mydomain.com",
  port: 8883,
  clientid: "testko",
  clean_start: false,
  ssl: true,
  ssl_opts: [
    cacertfile: ~c"certs/ca-chain.cert.pem",
    certfile:
      ~c"certs/client-nemo_gw_3.cert.pem",
    keyfile:
      ~c"certs/client-nemo_gw_3.key.pem",
    tls_versions: [:"tlsv1.2", :"tlsv1.3"],
    verify: :verify_peer,
    log_level: :debug,
    server_name_indication: ~c"prefix.mydomain.com",
    # depth: 3,
    customize_hostname_check: [
      match_fun: :public_key.pkix_verify_hostname_match_fun(:https)
    ],
    fail_if_no_peer_cert: true
  ],
  name: :emqtt,
  reconnect: true,
  reconnect_interval: 10000

And this still don’t work. Though now I get bunch of messages like this - reconnecting

➜  testko git:(drugi) ✗ iex -S mix
Erlang/OTP 27 [erts-15.0.1] [source] [64-bit] [smp:10:10] [ds:10:10:10] [async-threads:1] [jit]


10:05:26.241 [info] Starting MQTT Supervisor

10:05:26.243 [info] Starting MQTT Client

10:05:26.254 [info] Client ID set to: testko

10:05:26.254 [debug] Timer set to 5000 milliseconds
Interactive Elixir (1.17.2) - press Ctrl+C to exit (type h() ENTER for help)

10:05:26.382 [info] Starting MQTT Client

10:05:26.382 [info] Client ID set to: testko

10:05:26.382 [debug] Timer set to 5000 milliseconds

10:05:26.442 [info] Starting MQTT Client

10:05:26.442 [info] Client ID set to: testko

10:05:26.442 [debug] Timer set to 5000 milliseconds

10:05:26.497 [info] Starting MQTT Client

10:05:26.497 [info] Client ID set to: testko

10:05:26.497 [debug] Timer set to 5000 milliseconds

10:05:26.553 [info] Starting MQTT Client

10:05:26.554 [info] Client ID set to: testko

10:05:26.554 [debug] Timer set to 5000 milliseconds

10:05:26.602 [info] Starting MQTT Client

10:05:26.602 [info] Client ID set to: testko

10:05:26.602 [debug] Timer set to 5000 milliseconds
jjcarstens

jjcarstens

Nerves Core Team

I have not used emqtt, but have 2 thoughts to try:

  • Use absolute paths instead of relative to rule out file structure inconsistency. Or even use the DER format there instead of files just to get it working
  • Many brokers only allow one connection at a time. When I see things seem to connect and then disconnect without much else, it’s usually because the same cert and/or client ID is already connected elsewhere and the broker ungracefully kicks the connection. This could be some test process already connect elsewhere (ie on host) or multiple processes trying to make the connection and getting in each others way

Outside of that, I’m unsure. I think we need the SSL debug logs here to maybe be useful. Or reach out to the library maintainers. There is nothing different starting it on server, host, or nerves device

tomazbracic

tomazbracic OP

This is now what works. I found the right configuration.

config :testko, :emqtt,
  host: "prefix.mydomain.com",
  port: 8883,
  clientid: "testko",
  clean_start: false,
  ssl: true,
  ssl_opts: [
    cacertfile: ~c"/root/certificates/ca-chain.cert.pem",
    certfile: ~c"/root/certificates/client-nemo_gw_1.cert.pem",
    keyfile: ~c"/root/certificates/client-nemo_gw_1.key.pem",
    tls_versions: [:"tlsv1.2", :"tlsv1.3"],
    verify: :verify_peer,
    server_name_indication: ~c"prefix.mydomain.com"
  ],
  name: :emqtt,
  reconnect: true,
  reconnect_interval: 10000

Thanks for the help.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews