2019-07-30 16:40:59 +02: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 02:14:42 +01:00
|
|
|
defmodule Mobilizon.Federation.HTTPSignatures.Signature do
|
2019-07-30 16:40:59 +02:00
|
|
|
@moduledoc """
|
|
|
|
Adapter for the `HTTPSignatures` lib that handles signing and providing public keys to verify HTTPSignatures
|
|
|
|
"""
|
2019-09-09 00:52:49 +02:00
|
|
|
|
2019-07-30 16:40:59 +02:00
|
|
|
@behaviour HTTPSignatures.Adapter
|
|
|
|
|
|
|
|
alias Mobilizon.Actors.Actor
|
2021-04-22 12:17:56 +02:00
|
|
|
alias Mobilizon.Federation.ActivityPub.Actor, as: ActivityPubActor
|
2019-09-09 00:52:49 +02:00
|
|
|
|
2019-07-30 16:40:59 +02:00
|
|
|
require Logger
|
|
|
|
|
2019-12-03 11:29:51 +01:00
|
|
|
@spec key_id_to_actor_url(String.t()) :: String.t()
|
2019-07-30 16:40:59 +02:00
|
|
|
def key_id_to_actor_url(key_id) do
|
2021-09-10 11:27:59 +02:00
|
|
|
%URI{path: path} =
|
2019-09-09 09:35:50 +02:00
|
|
|
uri =
|
2019-09-09 00:52:49 +02:00
|
|
|
key_id
|
|
|
|
|> URI.parse()
|
2019-07-30 16:40:59 +02:00
|
|
|
|> Map.put(:fragment, nil)
|
|
|
|
|
|
|
|
uri =
|
2019-09-09 09:35:50 +02:00
|
|
|
if is_nil(path) do
|
2019-07-30 16:40:59 +02:00
|
|
|
uri
|
2019-09-09 09:35:50 +02:00
|
|
|
else
|
2021-09-10 11:27:59 +02:00
|
|
|
%URI{uri | path: String.trim_trailing(path, "/publickey")}
|
2019-07-30 16:40:59 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
URI.to_string(uri)
|
|
|
|
end
|
|
|
|
|
2019-09-09 00:52:49 +02: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 11:29:51 +01:00
|
|
|
# Gets a public key for a given ActivityPub actor ID (url).
|
2019-09-09 00:52:49 +02:00
|
|
|
@spec get_public_key_for_url(String.t()) ::
|
2021-04-21 18:57:23 +02:00
|
|
|
{:ok, String.t()}
|
2021-11-16 15:47:53 +01:00
|
|
|
| {:error, :actor_not_found | :pem_decode_error}
|
2019-12-03 11:29:51 +01:00
|
|
|
defp get_public_key_for_url(url) do
|
2021-11-29 10:29:26 +01:00
|
|
|
with {:ok, %Actor{} = actor} <-
|
|
|
|
ActivityPubActor.get_or_fetch_actor_by_url(url, ignore_sign_object_fetches: true) do
|
2021-11-19 17:40:42 +01:00
|
|
|
get_actor_public_key(actor)
|
2021-11-16 15:47:53 +01:00
|
|
|
end
|
|
|
|
end
|
2021-04-21 18:57:23 +02:00
|
|
|
|
2021-11-16 15:47:53 +01: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 18:57:23 +02:00
|
|
|
|
2021-11-16 15:47:53 +01:00
|
|
|
{:error, :pem_decode_error} ->
|
|
|
|
Logger.error("Error while decoding PEM")
|
2019-09-09 00:52:49 +02:00
|
|
|
|
2021-11-16 15:47:53 +01:00
|
|
|
{:error, :pem_decode_error}
|
2019-09-09 00:52:49 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-10 11:27:59 +02:00
|
|
|
@spec fetch_public_key(Plug.Conn.t()) ::
|
|
|
|
{:ok, String.t()}
|
2021-11-16 15:47:53 +01:00
|
|
|
| {:error, :actor_not_found | :pem_decode_error | :no_signature_in_conn}
|
2019-07-30 16:40:59 +02:00
|
|
|
def fetch_public_key(conn) do
|
2021-09-30 15:50:52 +02: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 16:40:59 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-10 11:27:59 +02:00
|
|
|
@spec refetch_public_key(Plug.Conn.t()) ::
|
|
|
|
{:ok, String.t()}
|
2021-09-24 16:46:42 +02:00
|
|
|
| {:error, :actor_fetch_error | :actor_not_fetchable | :pem_decode_error,
|
|
|
|
:actor_is_local}
|
2019-07-30 16:40:59 +02:00
|
|
|
def refetch_public_key(conn) do
|
2021-09-30 15:50:52 +02:00
|
|
|
%{"keyId" => kid} = HTTPSignatures.signature_for_conn(conn)
|
2021-11-16 15:47:53 +01:00
|
|
|
actor_url = key_id_to_actor_url(kid)
|
|
|
|
Logger.debug("Refetching public key for #{actor_url}")
|
2021-09-30 15:50:52 +02:00
|
|
|
|
2021-11-16 15:46:23 +01: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 16:40:59 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec sign(Actor.t(), map()) :: String.t() | {:error, :pem_decode_error} | no_return
|
2020-07-30 17:57:32 +02:00
|
|
|
def sign(%Actor{domain: domain, keys: keys} = actor, headers) when is_nil(domain) do
|
|
|
|
Logger.debug("Signing a payload on behalf of #{actor.url}")
|
2019-07-30 16:40:59 +02:00
|
|
|
Logger.debug("headers")
|
|
|
|
Logger.debug(inspect(headers))
|
|
|
|
|
2021-11-13 18:45:01 +01:00
|
|
|
case prepare_public_key(keys) do
|
|
|
|
{:ok, key} ->
|
|
|
|
HTTPSignatures.sign(key, actor.url <> "#main-key", headers)
|
|
|
|
|
|
|
|
{:error, :pem_decode_error} ->
|
|
|
|
raise ArgumentError, message: "Failed to prepare public keys for #{actor.url}"
|
2019-07-30 16:40:59 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-30 17:57:32 +02: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 11:27:59 +02:00
|
|
|
@spec generate_date_header :: String.t()
|
2019-12-03 11:29:51 +01:00
|
|
|
def generate_date_header, do: generate_date_header(NaiveDateTime.utc_now())
|
2019-07-30 16:40:59 +02:00
|
|
|
|
2021-09-30 15:50:52 +02:00
|
|
|
@spec generate_date_header(NaiveDateTime.t()) :: String.t()
|
2019-12-03 11:29:51 +01:00
|
|
|
def generate_date_header(%NaiveDateTime{} = date) do
|
2021-11-16 15:47:53 +01:00
|
|
|
# We make sure the format is correct
|
|
|
|
# TODO: Remove Timex, as this is the only usage (with parsing)
|
2021-11-13 18:45:01 +01:00
|
|
|
Timex.lformat!(date, "{WDshort}, {0D} {Mshort} {YYYY} {h24}:{m}:{s} GMT", "en")
|
2019-07-30 16:40:59 +02:00
|
|
|
end
|
|
|
|
|
2021-09-10 11:27:59 +02:00
|
|
|
@spec generate_request_target(String.t(), String.t()) :: String.t()
|
2019-07-30 16:40:59 +02:00
|
|
|
def generate_request_target(method, path), do: "#{method} #{path}"
|
|
|
|
|
2021-09-10 11:27:59 +02:00
|
|
|
@spec build_digest(String.t()) :: String.t()
|
2019-07-30 16:40:59 +02:00
|
|
|
def build_digest(body) do
|
2020-01-22 02:14:42 +01:00
|
|
|
"SHA-256=#{:sha256 |> :crypto.hash(body) |> Base.encode64()}"
|
2019-07-30 16:40:59 +02:00
|
|
|
end
|
|
|
|
end
|