Skip to content

Commit

Permalink
Compile in CA Certificates
Browse files Browse the repository at this point in the history
  • Loading branch information
mobileoverlord committed Dec 14, 2018
1 parent 419f8b9 commit 1a179d4
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions lib/nerves_hub/certificate.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@ defmodule NervesHub.Certificate do
@public_keys Application.get_env(:nerves_hub, :public_keys, [])
|> NervesHubCLI.public_keys()

ca_cert_path =
Application.get_env(:nerves_hub_core, :ca_certs) || System.get_env("NERVES_HUB_CA_CERTS") ||
:code.priv_dir(:nerves_hub_core)
|> to_string()
|> Path.join("ca_certs")

ca_certs =
ca_cert_path
|> File.ls!()
|> Enum.map(&File.read!(Path.join(ca_cert_path, &1)))
|> Enum.map(fn
<<"-----BEGIN", _rest::binary>> = cert ->
[{_, cert, _}] = :public_key.pem_decode(cert)
cert
_ -> ""
end)

@ca_certs ca_certs

def pem_to_der(<<"-----BEGIN", _rest::binary>> = cert) do
[{_, cert, _}] = :public_key.pem_decode(cert)
cert
Expand All @@ -11,19 +30,7 @@ defmodule NervesHub.Certificate do
def pem_to_der(""), do: ""

def ca_certs do
ca_cert_path =
if cert_path = Application.get_env(:nerves_hub, :ca_certs) do
cert_path
else
:code.priv_dir(:nerves_hub)
|> to_string()
|> Path.join("ca_certs")
end

ca_cert_path
|> File.ls!()
|> Enum.map(&File.read!(Path.join(ca_cert_path, &1)))
|> Enum.map(&pem_to_der/1)
@ca_certs
end

def public_keys do
Expand Down

0 comments on commit 1a179d4

Please sign in to comment.