-
Notifications
You must be signed in to change notification settings - Fork 14
/
certificate.ex
44 lines (36 loc) · 1.13 KB
/
certificate.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
defmodule NervesHub.Certificate do
# Get the fwup public keys from the app environment. Support the
# old name of `:public_keys` for now.
@public_keys Application.get_env(
:nerves_hub,
:fwup_public_keys,
Application.get_env(:nerves_hub, :public_keys, [])
)
|> NervesHubCLI.resolve_fwup_public_keys()
ca_cert_path =
System.get_env("NERVES_HUB_CA_CERTS") || Application.get_env(:nerves_hub, :ca_certs) ||
Application.app_dir(:nerves_hub, ["priv", "ca_certs"])
ca_certs =
ca_cert_path
|> File.ls!()
|> Enum.map(&File.read!(Path.join(ca_cert_path, &1)))
|> Enum.map(&X509.Certificate.to_der(X509.Certificate.from_pem!(&1)))
@ca_certs ca_certs
def pem_to_der(nil), do: <<>>
def pem_to_der(cert) do
case X509.Certificate.from_pem(cert) do
{:error, :not_found} -> <<>>
{:ok, decoded} -> X509.Certificate.to_der(decoded)
end
end
def ca_certs do
@ca_certs
end
@deprecated "Use fwup_public_keys/0 instead"
def public_keys do
fwup_public_keys()
end
def fwup_public_keys do
@public_keys
end
end