willc0de4food

willc0de4food

Sending / receiving TCP requests with SSL certificate

Hello,

I’m attempting to communicate with the Verisign EPP server over a TCP / SSL connection. This connection requires an SSL certificate, but I’m having trouble with the SSL certificate. I am inexperienced in working with anything like this, in the past all I’ve done is use Req to send various requests of different verbs. So has anyone written a library to make this easier? Or can anyone assist in the correct configuration of including an SSL certificate, sending a request and listening for a response? Here’s what I have so far, just shooting in the dark:

  def ssl_client() do
    host = Application.get_env(:appname, :epp_host) |> String.to_charlist()
    port = Application.get_env(:appname, :epp_port)
    cert = File.cwd!() <> "/ssl/cert.chain.pem"

    {:ok, connect_socket} =
      :ssl.connect(host, port, [verify: :verify_none, cacertfile: cert, active: true], :infinity)

    connect_socket
  end

  defp listen_ssl(socket) do
    case :ssl.recv(socket, 0) do
      {:ok, line} ->
        IO.puts(~s(Client got: "#{String.trim(line)}"))
        :ok = :ssl.close(socket)

      {:error, :closed} ->
        IO.puts("Server closed socket.")

      {:error, :enotconn} ->
        IO.puts("Server is not connected.")

      {:error, reason} ->
        IO.puts("Server errored with code: #{reason}")
    end
  end

  def send_ssl_request(line) do
    socket = ssl_client()
    :ssl.send(socket, line)
    listen_ssl(socket)
  end

The response that I get when I attempt to call send_ssl_request() is:
TLS :client: In state :connection received SERVER ALERT: Fatal - Bad Certificate

Thanks!

Marked As Solved

willc0de4food

willc0de4food

I finally found the right combination of options to get it working. What a pain! Somehow this ended up working:

  def ssl_start() do
    host = Application.get_env(:appname, :epp_host) |> String.to_charlist()
    port = Application.get_env(:appname, :epp_port)
    certs = File.cwd!() <> "/ssl/certs.pem"
    key = File.cwd!() <> "/ssl/key.pem"
    :public_key.cacerts_load(certs)

    opts = [
      cacerts: :public_key.cacerts_get(),
      verify: :verify_none,
      certfile: certs,
      keyfile: key
    ]

    :ssl.start()

    case :ssl.connect(host, port, opts, 5000) do
      {:ok, socket} ->
        socket

      {:error, err} ->
        dbg(err)
        nil
    end
  end

I received a file with 1 key & 3 certs. I tried separating each cert into it’s own file, but the working combination was to have the 3 certs in 1 file, and the key in another.

Also Liked

D4no0

D4no0

On what are you running this server? It might be possible that you are missing the client certificates on your deployed system, the symptom usually is that it works on dev envs but fails on deployed server.

To check that fast, you can try adding castore to your project and use the provided certs by castore with: CAStore.file_path()

muelthe

muelthe

A couple of things that have caught me out in the past, one being when using :public_key_cacerts_get() I didn’t have the certificate store configured correctly on my host machine.

Second, I’ve found that I also need to include the server_name_indication (SNI) in my ssl opts (some info here on SNI: https://www.cloudflare.com/learning/ssl/what-is-sni/).

Where Next?

Popular in Questions Top

Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&amp;query=perfume&amp;page=2, I would like to get: ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

Other popular topics Top

WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43657 311
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement