Read aead and next protocol as lists

This commit is contained in:
Mike Cifelli 2024-03-16 17:50:51 -04:00
parent 0fa4d93f6e
commit ec308c1fd4
Signed by: mike
GPG Key ID: 6B08C6BE47D08E4C
3 changed files with 30 additions and 15 deletions

View File

@ -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

View File

@ -1,4 +1,4 @@
defmodule ChronoscopeWeb.API.NTS.KeyEstablishmentController do
defmodule ChronoscopeWeb.API.V1.NTS.KeyEstablishmentController do
use ChronoscopeWeb, :controller
require Logger

View File

@ -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