nicksanders11

nicksanders11

Get SSL expiry date from an http request using Mint / Finch / Mojito

Hi,

Does anyone know how to access the SSL certificate to get the expiry date from an http request using Mint / Finch / Mojito

Marked As Solved

voltone

voltone

Finch and Mojito do not expose the underlying connections at all, while Mint wraps them in its own, opaque structs. If you don’t mind accessing private, undocumented fields in Mint’s structs, then you can get the certificate from the socket like this:

with {:ok, conn} <- Mint.HTTP.connect(:https, "blog.voltone.net", 443),
     {:ok, der} <- :ssl.peercert(conn.socket) do
  :public_key.pkix_decode_cert(der)
end

You can find the ‘not before’ and ‘not after’ timestamps in the :Validity record.

Also Liked

voltone

voltone

I needed to add :plain to pkix_decode_cert

Ah, right, sorry. I would use :otp myself, it makes it easier to work with extensions and RDNs. But it makes no difference for Validity.

If you are only interested in the certificate, and not making any HTTP request over the resulting connection, you may want to consider skipping the HTTP client altogether and just use :ssl.connect/3 (though that does not take into account proxies).

Finally, I just want to (shamelessly) plug my x509 package. With it you can do:

with {:ok, sock} <- :ssl.connect('blog.voltone.net', 443, []),
     {:ok, der} <- :ssl.peercert(sock),
     :ssl.close(sock),
     {:ok, cert} <- X509.Certificate.from_der(der),
     {:Validity, not_before, not_after} <- X509.Certificate.validity(cert) do
  {:ok, X509.DateTime.to_datetime(not_before), X509.DateTime.to_datetime(not_after)}
end
nicksanders11

nicksanders11

Brilliant thank you

I needed to add :plain to pkix_decode_cert

The code I ended up with is below in case it helps anyone else

def check_ssl(url) do
    uri = URI.parse(url)

    cert =
      with {:ok, conn} <- Mint.HTTP.connect(:https, uri.host, uri.port),
           {:ok, der} <- :ssl.peercert(conn.socket) do
        :public_key.pkix_decode_cert(der, :plain)
      end

    validity =
      cert
      |> elem(1)
      |> elem(5)

    case validity do
      {:Validity, {:utcTime, validFrom}, {:utcTime, validTo}} ->
        {:ok, from_cert_time(validFrom), from_cert_time(validTo)}

      x ->
        {:error, x}
    end
  end

  def from_cert_time(time) when is_list(time), do: List.to_string(time) |> from_cert_time()

  def from_cert_time(<<year::binary-size(2), month::binary-size(2), day::binary-size(2), _time::binary-size(6), "Z">>) do
    Date.new!(String.to_integer("20#{year}"), String.to_integer(month), String.to_integer(day))
  end

  def from_cert_time(<<year::binary-size(4), month::binary-size(2), day::binary-size(2), _time::binary-size(6), "Z">>) do
    Date.new!(String.to_integer(year), String.to_integer(month), String.to_integer(day))
  end

Where Next?

Popular in Questions Top

bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
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
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
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
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
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New

Other popular topics Top

William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
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
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
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

We're in Beta

About us Mission Statement