2018-12-23 20:04:54 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2018-12-31 15:41:47 +00:00
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 20:04:54 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2017-09-18 09:39:57 +00:00
|
|
|
# https://tools.ietf.org/html/draft-cavage-http-signatures-08
|
|
|
|
defmodule Pleroma.Web.HTTPSignatures do
|
2017-12-12 09:17:21 +00:00
|
|
|
alias Pleroma.User
|
2019-02-09 15:16:26 +00:00
|
|
|
alias Pleroma.Web.ActivityPub.ActivityPub
|
|
|
|
alias Pleroma.Web.ActivityPub.Utils
|
2019-02-06 19:20:02 +00:00
|
|
|
|
2018-02-24 16:36:26 +00:00
|
|
|
require Logger
|
2017-12-12 09:17:21 +00:00
|
|
|
|
2017-09-18 09:39:57 +00:00
|
|
|
def split_signature(sig) do
|
2017-09-18 16:10:21 +00:00
|
|
|
default = %{"headers" => "date"}
|
2017-09-18 09:39:57 +00:00
|
|
|
|
2018-03-11 13:37:23 +00:00
|
|
|
sig =
|
|
|
|
sig
|
|
|
|
|> 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 16:10:21 +00:00
|
|
|
|
|
|
|
Map.put(sig, "headers", String.split(sig["headers"], ~r/\s/))
|
2017-09-18 09:39:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def validate(headers, signature, public_key) do
|
|
|
|
sigstring = build_signing_string(headers, signature["headers"])
|
2018-03-11 13:37:23 +00:00
|
|
|
Logger.debug("Signature: #{signature["signature"]}")
|
|
|
|
Logger.debug("Sigstring: #{sigstring}")
|
2017-09-18 09:39:57 +00:00
|
|
|
{:ok, sig} = Base.decode64(signature["signature"])
|
2017-12-12 09:17:21 +00: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.
|
2018-05-26 11:52:05 +00:00
|
|
|
with actor_id <- Utils.get_ap_id(conn.params["actor"]),
|
2017-12-12 09:17:21 +00:00
|
|
|
{:ok, public_key} <- User.get_public_key_for_ap_id(actor_id) do
|
2018-02-11 19:43:33 +00:00
|
|
|
if validate_conn(conn, public_key) do
|
|
|
|
true
|
|
|
|
else
|
2018-03-19 17:31:58 +00:00
|
|
|
Logger.debug("Could not validate, re-fetching user and trying one more time")
|
2018-02-11 19:43:33 +00:00
|
|
|
# Fetch user anew and try one more time
|
2018-05-26 11:52:05 +00:00
|
|
|
with actor_id <- Utils.get_ap_id(conn.params["actor"]),
|
2018-02-11 19:43:33 +00:00
|
|
|
{: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 09:17:21 +00:00
|
|
|
else
|
2018-05-04 20:59:01 +00:00
|
|
|
_e ->
|
2018-02-24 16:36:26 +00:00
|
|
|
Logger.debug("Could not public key!")
|
2018-03-11 13:47:37 +00:00
|
|
|
false
|
2017-12-12 09:17:21 +00:00
|
|
|
end
|
2017-09-18 09:39:57 +00:00
|
|
|
end
|
|
|
|
|
2017-12-11 09:37:40 +00: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 09:39:57 +00:00
|
|
|
def build_signing_string(headers, used_headers) do
|
|
|
|
used_headers
|
2018-03-11 13:37:23 +00:00
|
|
|
|> Enum.map(fn header -> "#{header}: #{headers[header]}" end)
|
2017-09-18 09:39:57 +00:00
|
|
|
|> Enum.join("\n")
|
|
|
|
end
|
2018-02-11 19:43:33 +00:00
|
|
|
|
|
|
|
def sign(user, headers) do
|
2018-11-27 16:42:45 +00:00
|
|
|
with {:ok, %{info: %{keys: keys}}} <- Pleroma.Web.WebFinger.ensure_keys_present(user),
|
2018-02-11 19:43:33 +00:00
|
|
|
{:ok, private_key, _} = Pleroma.Web.Salmon.keys_from_pem(keys) do
|
|
|
|
sigstring = build_signing_string(headers, Map.keys(headers))
|
2018-03-11 13:37:23 +00:00
|
|
|
|
|
|
|
signature =
|
|
|
|
:public_key.sign(sigstring, :sha256, private_key)
|
|
|
|
|> Base.encode64()
|
2018-02-11 19:43:33 +00:00
|
|
|
|
|
|
|
[
|
|
|
|
keyId: user.ap_id <> "#main-key",
|
|
|
|
algorithm: "rsa-sha256",
|
|
|
|
headers: Map.keys(headers) |> Enum.join(" "),
|
|
|
|
signature: signature
|
|
|
|
]
|
2018-03-11 13:37:23 +00:00
|
|
|
|> Enum.map(fn {k, v} -> "#{k}=\"#{v}\"" end)
|
2018-02-11 19:43:33 +00:00
|
|
|
|> Enum.join(",")
|
|
|
|
end
|
|
|
|
end
|
2017-09-18 09:39:57 +00:00
|
|
|
end
|