ceo_of_programming

ceo_of_programming

Plug_cowboy and https: self-signed certificate is rejected by browsers and curl

Hi! My self-signed certificate is rejected by browsers and curl.

The problem:

On curl I get the error: curl: (52) Empty reply from server from curl.
Firefox warns me that the certificate is self-signed, I click to continue and it never gets a response. Chromium gives similar results.

On the terminal I sometimes get:

[info]  ['TLS', 32, 'server', 58, 32, 73, 110, 32, 115, 116, 97, 11
6, 101, 32, 'abbreviated', 32, 'received CLIENT ALERT: Fatal - Certificate Unkno
wn', 10]  

What does work:

Everything works fine with plain http. When using the cowboy interface directly the results are the same, plain http works, https doesn’t. I was able to successfully setup self-signed certificates with Nginx, they were not the ones generated by mix x509.gen.selfsigned (see below) but they also did not work with plug_cowboy or cowboy.
I think the problem might be that I’m not passing a cacertfile parameter (for a self-signed certificate authority that signs my certificate), but I don’t know how to generate that.

The setup:

All on a single machine, with a fresh mix project created only for this.

I generated a self-signed certificate with: mix x509.gen.selfsigned, it was put in the default location: priv/cert/. It appears that the default hostname it uses is localhostbut I tried passing a specific name too, only to get the same result.

I’m on Debian 10 amd64;

Erlang/OTP 21 [erts-10.2.4] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1]

Interactive Elixir (1.7.4) - press Ctrl+C to exit (type h() ENTER for help)

My plug is in lib/https_tests.ex:

defmodule HttpsTests do
  import Plug.Conn

  def init(options) do
    # initialize options
    options
  end

  def call(conn, _opts) do
    conn
    |> put_resp_content_type("text/plain")
    |> send_resp(200, "Hello world")
  end
end

My lib/https_tests/application.ex:

defmodule HttpsTests.Application do
  # See https://hexdocs.pm/elixir/Application.html
  # for more information on OTP Applications
  @moduledoc false

  use Application

  def start(_type, _args) do
    # List all child processes to be supervised
    children = [
      {Plug.Cowboy, scheme: :https, plug: HttpsTests, 
       options: [
	 port: 4001,
	 cipher_suite: :strong,
	 certfile: "priv/cert/selfsigned.pem",
	 keyfile: "priv/cert/selfsigned_key.pem",
	 otp_app: :https_tests
       ]}
    ]

    # See https://hexdocs.pm/elixir/Supervisor.html
    # for other strategies and supported options
    opts = [strategy: :one_for_one, name: HttpsTests.Supervisor]
    Supervisor.start_link(children, opts)
  end
end

Thanks!

Marked As Solved

ceo_of_programming

ceo_of_programming

It works on OTP 23! It works with the x509 cert and the openssl one.
Thank you very much, both of you!

@voltone I had to use the certificate generated on the other machine, x509 does not work on OTP 23. I get:

$ mix x509.gen.selfsigned
==> x509
Compiling 22 files (.ex)

== Compilation error in file lib/x509/asn1.ex ==
** (RuntimeError) error parsing file /usr/lib/erlang/lib/public_key-1.9.1/include/OTP-PUB-KEY.hrl, got: {:error, :enoent}                                                         
    (elixir 1.10.3) lib/record/extractor.ex:84: Record.Extractor.read_file/2
    (elixir 1.10.3) lib/record/extractor.ex:50: Record.Extractor.extract_record/2
    lib/x509/asn1.ex:70: anonymous fn/1 in :elixir_compiler_5.__MODULE__/1
    (elixir 1.10.3) lib/enum.ex:783: Enum."-each/2-lists^foreach/1-0-"/2
    (elixir 1.10.3) lib/enum.ex:783: Enum.each/2
    lib/x509/asn1.ex:57: (module)
could not compile dependency :x509, "mix compile" failed. You can recompile this dependency with "mix deps.compile x509", update it with "mix deps.update x509" or clean it with "mix deps.clean x509"

Do you want me to open a bug report or is that a known issue?

EDIT: Everything works when using cowboy directly too.

Also Liked

ohmree

ohmree

I’m getting the exact same error in a newly-generated phoenix live-view app using pow and pow_assent with mint and castore dependencies added for HTTP/2 and SSL support.

The app was properly configured using the generators from phoenix, pow and pow_assent and everything works as expected without SSL.

I’ve also tried making pow_assent use :httpc instead of mint and added the recommended :certifi and :ssl_verify_fun dependencies from the readme to no avail - I get the same error.

I’m on arch linux with OTP 23.

Where Next?

Popular in Questions Top

gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
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
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New

We're in Beta

About us Mission Statement