diff --git a/lib/chronoscope/nts/key_establishment.ex b/lib/chronoscope/nts/key_establishment.ex index 0679aaa..fc00b51 100644 --- a/lib/chronoscope/nts/key_establishment.ex +++ b/lib/chronoscope/nts/key_establishment.ex @@ -5,6 +5,13 @@ defmodule Chronoscope.NTS.KeyEstablishment do @aead_algorithm_negotiation <<0x80, 0x04, 0x00, 0x04, 0x00, 0x1E, 0x00, 0x0F>> @end_of_message <<0x80, 0x00, 0x00, 0x00>> + @aead_alogorithms %{ + 15 => "AEAD_AES_SIV_CMAC_256", + 30 => "AEAD_AES_128_GCM_SIV" + } + + @next_protocols %{0 => "NTPv4"} + def request() do @next_protocol_negotiation <> @aead_algorithm_negotiation <> @end_of_message end @@ -27,7 +34,7 @@ defmodule Chronoscope.NTS.KeyEstablishment do do_parse_response( remaining, - Map.put(acc, :next_protocols, parse_next_protocol(next_protocols)) + Map.put(acc, :next_protocols, parse_next_protocol_list(next_protocols)) ) end @@ -38,7 +45,7 @@ defmodule Chronoscope.NTS.KeyEstablishment do do_parse_response( remaining, - Map.put(acc, :aead_algorithms, parse_aead_algorithm(aead_algorithms)) + Map.put(acc, :aead_algorithms, parse_aead_algorithm_list(aead_algorithms)) ) end @@ -62,24 +69,32 @@ defmodule Chronoscope.NTS.KeyEstablishment do do_parse_response(remaining, acc) end - defp parse_aead_algorithm([0x00, 0x0F]) do - "AEAD_AES_SIV_CMAC_256" + defp parse_aead_algorithm_list(aead_algorithms) do + do_parse_aead_algorithm_list(aead_algorithms, []) end - defp parse_aead_algorithm([0x00, 0x1E]) do - "AEAD_AES_128_GCM_SIV" + defp do_parse_aead_algorithm_list([], acc) do + acc end - defp parse_aead_algorithm(_aead_algorithm) do - "UNKNOWN" + defp do_parse_aead_algorithm_list([high, low | rest], acc) do + @aead_alogorithms + |> Map.get(combine_octets(high, low), "UNKNOWN") + |> then(&do_parse_aead_algorithm_list(rest, [&1 | acc])) end - defp parse_next_protocol([0x00, 0x00]) do - "NTPv4" + defp parse_next_protocol_list(next_protocols) do + do_parse_next_protocol_list(next_protocols, []) end - defp parse_next_protocol(_next_protocol) do - "UNASSIGNED" + defp do_parse_next_protocol_list([], acc) do + acc + end + + defp do_parse_next_protocol_list([high, low | rest], acc) do + @next_protocols + |> Map.get(combine_octets(high, low), "UNASSIGNED") + |> then(&do_parse_aead_algorithm_list(rest, [&1 | acc])) end # todo parse server/port information diff --git a/lib/chronoscope_web/controllers/api/nts/key_establishment_controller.ex b/lib/chronoscope_web/controllers/api/v1/nts/key_establishment_controller.ex similarity index 90% rename from lib/chronoscope_web/controllers/api/nts/key_establishment_controller.ex rename to lib/chronoscope_web/controllers/api/v1/nts/key_establishment_controller.ex index 487ecf4..679d8ec 100644 --- a/lib/chronoscope_web/controllers/api/nts/key_establishment_controller.ex +++ b/lib/chronoscope_web/controllers/api/v1/nts/key_establishment_controller.ex @@ -1,4 +1,4 @@ -defmodule ChronoscopeWeb.API.NTS.KeyEstablishmentController do +defmodule ChronoscopeWeb.API.V1.NTS.KeyEstablishmentController do use ChronoscopeWeb, :controller require Logger diff --git a/lib/chronoscope_web/router.ex b/lib/chronoscope_web/router.ex index 03d181f..9898051 100644 --- a/lib/chronoscope_web/router.ex +++ b/lib/chronoscope_web/router.ex @@ -20,10 +20,10 @@ defmodule ChronoscopeWeb.Router do get "/", PageController, :home end - scope "/api", ChronoscopeWeb.API do + scope "/api/v1/nts", ChronoscopeWeb.API.V1.NTS do pipe_through :api - get "/key-exchange", NTS.KeyEstablishmentController, :get + get "/key-exchange", KeyEstablishmentController, :get end # Enable LiveDashboard and Swoosh mailbox preview in development