2017-09-18 11:39:57 +02:00
|
|
|
# https://tools.ietf.org/html/draft-cavage-http-signatures-08
|
|
|
|
defmodule Pleroma.Web.HTTPSignatures do
|
2017-12-12 10:17:21 +01:00
|
|
|
alias Pleroma.User
|
2018-02-11 20:43:33 +01:00
|
|
|
alias Pleroma.Web.ActivityPub.ActivityPub
|
2017-12-12 10:17:21 +01:00
|
|
|
|
2017-09-18 11:39:57 +02:00
|
|
|
def split_signature(sig) do
|
2017-09-18 18:10:21 +02:00
|
|
|
default = %{"headers" => "date"}
|
2017-09-18 11:39:57 +02:00
|
|
|
|
2017-09-18 18:10:21 +02:00
|
|
|
sig = sig
|
2017-09-18 11:39:57 +02:00
|
|
|
|> String.trim()
|
|
|
|
|> String.split(",")
|
|
|
|
|> Enum.reduce(default, fn(part, acc) ->
|
|
|
|
[key | rest] = String.split(part, "=")
|
|
|
|
value = Enum.join(rest, "=")
|
|
|
|
Map.put(acc, key, String.trim(value, "\""))
|
|
|
|
end)
|
2017-09-18 18:10:21 +02:00
|
|
|
|
|
|
|
Map.put(sig, "headers", String.split(sig["headers"], ~r/\s/))
|
2017-09-18 11:39:57 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def validate(headers, signature, public_key) do
|
|
|
|
sigstring = build_signing_string(headers, signature["headers"])
|
|
|
|
{:ok, sig} = Base.decode64(signature["signature"])
|
2017-12-12 10:17:21 +01:00
|
|
|
:public_key.verify(sigstring, :sha256, sig, public_key)
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_conn(conn) do
|
|
|
|
# TODO: How to get the right key and see if it is actually valid for that request.
|
|
|
|
# For now, fetch the key for the actor.
|
|
|
|
with actor_id <- conn.params["actor"],
|
|
|
|
{:ok, public_key} <- User.get_public_key_for_ap_id(actor_id) do
|
2018-02-11 20:43:33 +01:00
|
|
|
if validate_conn(conn, public_key) do
|
|
|
|
true
|
|
|
|
else
|
|
|
|
# Fetch user anew and try one more time
|
|
|
|
with actor_id <- conn.params["actor"],
|
|
|
|
{:ok, _user} <- ActivityPub.make_user_from_ap_id(actor_id),
|
|
|
|
{:ok, public_key} <- User.get_public_key_for_ap_id(actor_id) do
|
|
|
|
validate_conn(conn, public_key)
|
|
|
|
end
|
|
|
|
end
|
2017-12-12 10:17:21 +01:00
|
|
|
else
|
|
|
|
_ -> false
|
|
|
|
end
|
2017-09-18 11:39:57 +02:00
|
|
|
end
|
|
|
|
|
2017-12-11 10:37:40 +01:00
|
|
|
def validate_conn(conn, public_key) do
|
|
|
|
headers = Enum.into(conn.req_headers, %{})
|
|
|
|
signature = split_signature(headers["signature"])
|
|
|
|
validate(headers, signature, public_key)
|
|
|
|
end
|
|
|
|
|
2017-09-18 11:39:57 +02:00
|
|
|
def build_signing_string(headers, used_headers) do
|
|
|
|
used_headers
|
|
|
|
|> Enum.map(fn (header) -> "#{header}: #{headers[header]}" end)
|
|
|
|
|> Enum.join("\n")
|
|
|
|
end
|
2018-02-11 20:43:33 +01:00
|
|
|
|
|
|
|
def sign(user, headers) do
|
|
|
|
with {:ok, %{info: %{"keys" => keys}}} <- Pleroma.Web.WebFinger.ensure_keys_present(user),
|
|
|
|
{:ok, private_key, _} = Pleroma.Web.Salmon.keys_from_pem(keys) do
|
|
|
|
sigstring = build_signing_string(headers, Map.keys(headers))
|
|
|
|
signature = :public_key.sign(sigstring, :sha256, private_key)
|
|
|
|
|> Base.encode64()
|
|
|
|
|
|
|
|
[
|
|
|
|
keyId: user.ap_id <> "#main-key",
|
|
|
|
algorithm: "rsa-sha256",
|
|
|
|
headers: Map.keys(headers) |> Enum.join(" "),
|
|
|
|
signature: signature
|
|
|
|
]
|
|
|
|
|> Enum.map(fn({k, v}) -> "#{k}=\"#{v}\"" end)
|
|
|
|
|> Enum.join(",")
|
|
|
|
end
|
|
|
|
end
|
2017-09-18 11:39:57 +02:00
|
|
|
end
|