samfrench

samfrench

Cowboy mTLS allowing us to verify client certificates

We are using Cowboy with a HTTPS listener and would like to do mTLS. We have a similar setup using nginx in another application and specify a certificate file (ssl_client_certificate) which allows us to verify client certificates. We also specify another certificate file (ssl_certificate) to allow clients to verify the server.

Is it possible in Cowboy to specify these two certificates? We can have one for the clients to verify the server but not too sure on how we can verify client certificates. I only see one “certfile” property in the configuration.

Our configuration is similar to this:

[
  certfile: "/path/to/cert.crt",
  keyfile: "/path/to/key.key",
  cacertfile: "/path/to/cacert.crt",
  verify: :verify_peer,
  depth: 3
]

We would like to be able to verify client certificates while allowing the server to present a different certificate to clients for them to trust the server.

There might be documentation for how we can do this but I have not seen anything specifically for this setup or comparable to nginx. Any help with how we can achieve this is appreciated.

Marked As Solved

samfrench

samfrench

The solution that worked is here:

defmodule Router.Https do

  def config() do
    [
      certfile: "cert_file",
      keyfile: "key_file",
      cacertfile: "cacert_file",
      cacerts: cacerts(),
      verify: :verify_peer,
      partial_chain: &partial_chain(cacerts(), &1),
      customize_hostname_check: [
        match_fun: :public_key.pkix_verify_hostname_match_fun(:https)
      ],
      secure_renegotiate: true,
      reuse_sessions: false,
      fail_if_no_peer_cert: true
    ]
  end

  defp cacerts() do
    "ca_client_certs_file" |> File.read() |> X509.Certificate.from_pem!() |> X509.Certificate.to_der()
  end

  def partial_chain(cacerts, certs) do
    certs = Enum.map(certs, &{&1, :public_key.pkix_decode_cert(&1, :otp)})
    cacerts = Enum.map(cacerts, &:public_key.pkix_decode_cert(&1, :otp))

    trusted =
      Enum.find_value(certs, fn {der, cert} ->
        trusted? =
          Enum.find(cacerts, fn cacert ->
            extract_public_key_info(cacert) == extract_public_key_info(cert)
          end)

        if trusted?, do: der
      end)

    if trusted do
      {:trusted_ca, trusted}
    else
      :unknown_ca
    end
  end

  defp extract_public_key_info(cert) do
    cert
    |> X509.Certificate.subject()
  end
end

Also Liked

voltone

voltone

Keep in mind that the cacerts and cacertfile options serve two roles when doing mTLS: they are used to specify the trust store used when verifying the other party’s certificate, and also to look up any intermediates that may need to be included in the ‘chain’ that is sent for the local party’s certificate.

So on a server, the CA certs would have to include the server certificate’s intermediates and the trusted CA that issued the client certs. And on the client, CA certs should include the usual CA trust store (or the specific one that issued the server’s cert, if you want to pin it that way) and any intermediates that should be sent with the client certificate.

In recent OTP versions you can, as an alternative approach, set certs to a list containing the local endpoint’s certificate and intermediates, and in that case cacerts would only have to contain the trust store.

moogle19

moogle19

You should take a look at Erlang -- ssl
When you specify cacertfile / cacerts on the server-side, these are normally used to verify the client certificate.

LostKobrakai

LostKobrakai

That’s the documentation for ranch, which is the library cowboy uses underneight.
https://ninenines.eu/docs/en/ranch/2.1/guide/ssl_auth/

tj0

tj0

Can you post your solution for future generations?

edit: relevant xkcd - xkcd: Wisdom of the Ancients

Where Next?

Popular in Questions Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

We're in Beta

About us Mission Statement