2019-07-30 14:40:59 +00:00
|
|
|
# Portions of this file are derived from Pleroma:
|
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
# Upstream: https://git.pleroma.social/pleroma/pleroma/blob/develop/lib/pleroma/signature.ex
|
|
|
|
|
2020-01-22 01:14:42 +00:00
|
|
|
defmodule Mobilizon.Federation.HTTPSignatures.Signature do
|
2019-07-30 14:40:59 +00:00
|
|
|
@moduledoc """
|
|
|
|
Adapter for the `HTTPSignatures` lib that handles signing and providing public keys to verify HTTPSignatures
|
|
|
|
"""
|
2019-09-08 22:52:49 +00:00
|
|
|
|
2019-07-30 14:40:59 +00:00
|
|
|
@behaviour HTTPSignatures.Adapter
|
|
|
|
|
|
|
|
alias Mobilizon.Actors.Actor
|
2021-04-22 10:17:56 +00:00
|
|
|
alias Mobilizon.Federation.ActivityPub.Actor, as: ActivityPubActor
|
2019-09-08 22:52:49 +00:00
|
|
|
|
2019-07-30 14:40:59 +00:00
|
|
|
require Logger
|
|
|
|
|
2019-12-03 10:29:51 +00:00
|
|
|
@spec key_id_to_actor_url(String.t()) :: String.t()
|
2019-07-30 14:40:59 +00:00
|
|
|
def key_id_to_actor_url(key_id) do
|
2021-09-10 09:27:59 +00:00
|
|
|
%URI{path: path} =
|
2019-09-09 07:35:50 +00:00
|
|
|
uri =
|
2019-09-08 22:52:49 +00:00
|
|
|
key_id
|
|
|
|
|> URI.parse()
|
2019-07-30 14:40:59 +00:00
|
|
|
|> Map.put(:fragment, nil)
|
|
|
|
|
|
|
|
uri =
|
2019-09-09 07:35:50 +00:00
|
|
|
if is_nil(path) do
|
2019-07-30 14:40:59 +00:00
|
|
|
uri
|
2019-09-09 07:35:50 +00:00
|
|
|
else
|
2021-09-10 09:27:59 +00:00
|
|
|
%URI{uri | path: String.trim_trailing(path, "/publickey")}
|
2019-07-30 14:40:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
URI.to_string(uri)
|
|
|
|
end
|
|
|
|
|
2019-09-08 22:52:49 +00:00
|
|
|
@doc """
|
|
|
|
Convert internal PEM encoded keys to public key format.
|
|
|
|
"""
|
|
|
|
@spec prepare_public_key(String.t()) :: {:ok, tuple} | {:error, :pem_decode_error}
|
|
|
|
def prepare_public_key(public_key_code) do
|
|
|
|
case :public_key.pem_decode(public_key_code) do
|
|
|
|
[public_key_entry] ->
|
|
|
|
{:ok, :public_key.pem_entry_decode(public_key_entry)}
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
{:error, :pem_decode_error}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-03 10:29:51 +00:00
|
|
|
# Gets a public key for a given ActivityPub actor ID (url).
|
2019-09-08 22:52:49 +00:00
|
|
|
@spec get_public_key_for_url(String.t()) ::
|
2021-04-21 16:57:23 +00:00
|
|
|
{:ok, String.t()}
|
2021-11-16 14:47:53 +00:00
|
|
|
| {:error, :actor_not_found | :pem_decode_error}
|
2019-12-03 10:29:51 +00:00
|
|
|
defp get_public_key_for_url(url) do
|
2021-11-29 09:29:26 +00:00
|
|
|
with {:ok, %Actor{} = actor} <-
|
|
|
|
ActivityPubActor.get_or_fetch_actor_by_url(url, ignore_sign_object_fetches: true) do
|
2021-11-19 16:40:42 +00:00
|
|
|
get_actor_public_key(actor)
|
2021-11-16 14:47:53 +00:00
|
|
|
end
|
|
|
|
end
|
2021-04-21 16:57:23 +00:00
|
|
|
|
2021-11-16 14:47:53 +00:00
|
|
|
@spec get_actor_public_key(Actor.t()) :: {:ok, String.t()} | {:error, :pem_decode_error}
|
|
|
|
defp get_actor_public_key(%Actor{keys: keys}) do
|
|
|
|
case prepare_public_key(keys) do
|
|
|
|
{:ok, public_key} ->
|
|
|
|
{:ok, public_key}
|
2021-04-21 16:57:23 +00:00
|
|
|
|
2021-11-16 14:47:53 +00:00
|
|
|
{:error, :pem_decode_error} ->
|
|
|
|
Logger.error("Error while decoding PEM")
|
2019-09-08 22:52:49 +00:00
|
|
|
|
2021-11-16 14:47:53 +00:00
|
|
|
{:error, :pem_decode_error}
|
2019-09-08 22:52:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-10 09:27:59 +00:00
|
|
|
@spec fetch_public_key(Plug.Conn.t()) ::
|
|
|
|
{:ok, String.t()}
|
2021-11-16 14:47:53 +00:00
|
|
|
| {:error, :actor_not_found | :pem_decode_error | :no_signature_in_conn}
|
2019-07-30 14:40:59 +00:00
|
|
|
def fetch_public_key(conn) do
|
2021-09-30 13:50:52 +00:00
|
|
|
case HTTPSignatures.signature_for_conn(conn) do
|
|
|
|
%{"keyId" => kid} ->
|
|
|
|
actor_id = key_id_to_actor_url(kid)
|
|
|
|
Logger.debug("Fetching public key for #{actor_id}")
|
|
|
|
get_public_key_for_url(actor_id)
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
{:error, :no_signature_in_conn}
|
2019-07-30 14:40:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-10 09:27:59 +00:00
|
|
|
@spec refetch_public_key(Plug.Conn.t()) ::
|
|
|
|
{:ok, String.t()}
|
2021-09-24 14:46:42 +00:00
|
|
|
| {:error, :actor_fetch_error | :actor_not_fetchable | :pem_decode_error,
|
|
|
|
:actor_is_local}
|
2019-07-30 14:40:59 +00:00
|
|
|
def refetch_public_key(conn) do
|
2021-09-30 13:50:52 +00:00
|
|
|
%{"keyId" => kid} = HTTPSignatures.signature_for_conn(conn)
|
2021-11-16 14:47:53 +00:00
|
|
|
actor_url = key_id_to_actor_url(kid)
|
|
|
|
Logger.debug("Refetching public key for #{actor_url}")
|
2021-09-30 13:50:52 +00:00
|
|
|
|
2022-04-20 07:44:26 +00:00
|
|
|
# In this specific case we don't sign object fetches because
|
|
|
|
# this would cause infinite recursion when servers both need
|
|
|
|
# to fetch each other's keys
|
2021-11-16 14:46:23 +00:00
|
|
|
with {:ok, %Actor{} = actor} <-
|
|
|
|
ActivityPubActor.make_actor_from_url(actor_url, ignore_sign_object_fetches: true) do
|
|
|
|
get_actor_public_key(actor)
|
2019-07-30 14:40:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-24 14:46:42 +00:00
|
|
|
@spec sign(Actor.t(), map()) :: String.t() | {:error, :pem_decode_error} | no_return
|
2022-03-29 09:33:09 +00:00
|
|
|
def sign(%Actor{domain: domain, keys: keys, url: url} = actor, headers) when is_nil(domain) do
|
|
|
|
Logger.debug("Signing a payload on behalf of #{url}")
|
2019-07-30 14:40:59 +00:00
|
|
|
Logger.debug("headers")
|
|
|
|
Logger.debug(inspect(headers))
|
|
|
|
|
2021-11-13 17:45:01 +00:00
|
|
|
case prepare_public_key(keys) do
|
|
|
|
{:ok, key} ->
|
2022-03-29 09:33:09 +00:00
|
|
|
if Application.get_env(:sentry, :dsn) != nil do
|
|
|
|
Sentry.Context.set_extra_context(%{"actor_url" => url})
|
|
|
|
end
|
|
|
|
|
|
|
|
HTTPSignatures.sign(key, url <> "#main-key", headers)
|
2021-11-13 17:45:01 +00:00
|
|
|
|
|
|
|
{:error, :pem_decode_error} ->
|
|
|
|
raise ArgumentError, message: "Failed to prepare public keys for #{actor.url}"
|
2019-07-30 14:40:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-30 15:57:32 +00:00
|
|
|
def sign(%Actor{url: url}, _) do
|
|
|
|
Logger.error("Can't do a signature on remote actor #{url}")
|
|
|
|
raise ArgumentError, message: "Can't do a signature on remote actor #{url}"
|
|
|
|
end
|
|
|
|
|
2021-09-10 09:27:59 +00:00
|
|
|
@spec generate_date_header :: String.t()
|
2019-12-03 10:29:51 +00:00
|
|
|
def generate_date_header, do: generate_date_header(NaiveDateTime.utc_now())
|
2019-07-30 14:40:59 +00:00
|
|
|
|
2021-09-30 13:50:52 +00:00
|
|
|
@spec generate_date_header(NaiveDateTime.t()) :: String.t()
|
2019-12-03 10:29:51 +00:00
|
|
|
def generate_date_header(%NaiveDateTime{} = date) do
|
2021-11-16 14:47:53 +00:00
|
|
|
# We make sure the format is correct
|
|
|
|
# TODO: Remove Timex, as this is the only usage (with parsing)
|
2021-11-13 17:45:01 +00:00
|
|
|
Timex.lformat!(date, "{WDshort}, {0D} {Mshort} {YYYY} {h24}:{m}:{s} GMT", "en")
|
2019-07-30 14:40:59 +00:00
|
|
|
end
|
|
|
|
|
2021-09-10 09:27:59 +00:00
|
|
|
@spec generate_request_target(String.t(), String.t()) :: String.t()
|
2019-07-30 14:40:59 +00:00
|
|
|
def generate_request_target(method, path), do: "#{method} #{path}"
|
|
|
|
|
2021-09-10 09:27:59 +00:00
|
|
|
@spec build_digest(String.t()) :: String.t()
|
2019-07-30 14:40:59 +00:00
|
|
|
def build_digest(body) do
|
2020-01-22 01:14:42 +00:00
|
|
|
"SHA-256=#{:sha256 |> :crypto.hash(body) |> Base.encode64()}"
|
2019-07-30 14:40:59 +00:00
|
|
|
end
|
|
|
|
end
|