2018-12-24 12:34:45 +00:00
|
|
|
# Portions of this file are derived from Pleroma:
|
|
|
|
# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
2018-12-27 10:24:04 +00:00
|
|
|
# Upstream: https://git.pleroma.social/pleroma/pleroma/blob/develop/lib/pleroma/web/http_signatures/http_signatures.ex
|
2018-12-24 12:34:45 +00:00
|
|
|
|
2018-05-17 09:32:23 +00:00
|
|
|
# https://tools.ietf.org/html/draft-cavage-http-signatures-08
|
2018-10-11 15:37:39 +00:00
|
|
|
defmodule Mobilizon.Service.HTTPSignatures do
|
2018-06-14 16:15:27 +00:00
|
|
|
@moduledoc """
|
|
|
|
# HTTP Signatures
|
|
|
|
|
|
|
|
Generates and checks HTTP Signatures
|
|
|
|
"""
|
|
|
|
|
2018-10-11 15:37:39 +00:00
|
|
|
alias Mobilizon.Actors.Actor
|
|
|
|
alias Mobilizon.Service.ActivityPub
|
2018-11-12 22:30:47 +00:00
|
|
|
require Logger
|
2018-05-17 09:32:23 +00:00
|
|
|
|
|
|
|
def split_signature(sig) do
|
|
|
|
default = %{"headers" => "date"}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
Map.put(sig, "headers", String.split(sig["headers"], ~r/\s/))
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate(headers, signature, public_key) do
|
|
|
|
sigstring = build_signing_string(headers, signature["headers"])
|
2018-07-27 08:45:35 +00:00
|
|
|
|
|
|
|
Logger.debug(fn ->
|
2018-06-14 16:15:27 +00:00
|
|
|
"Signature: #{signature["signature"]}"
|
2018-07-27 08:45:35 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
Logger.debug(fn ->
|
2018-06-14 16:15:27 +00:00
|
|
|
"Sigstring: #{sigstring}"
|
2018-07-27 08:45:35 +00:00
|
|
|
end)
|
|
|
|
|
2018-05-17 09:32:23 +00:00
|
|
|
{:ok, sig} = Base.decode64(signature["signature"])
|
2018-05-18 07:56:21 +00:00
|
|
|
:public_key.verify(sigstring, :sha256, sig, public_key)
|
2018-05-17 09:32:23 +00:00
|
|
|
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-11-12 17:17:53 +00:00
|
|
|
with {:ok, public_key} <- conn.params["actor"] |> Actor.get_public_key_for_url() do
|
2018-05-18 07:56:21 +00:00
|
|
|
if validate_conn(conn, public_key) do
|
|
|
|
true
|
|
|
|
else
|
|
|
|
Logger.info("Could not validate request, re-fetching user and trying one more time")
|
2018-05-17 09:32:23 +00:00
|
|
|
# Fetch user anew and try one more time
|
|
|
|
with actor_id <- conn.params["actor"],
|
2018-05-18 07:56:21 +00:00
|
|
|
{:ok, _actor} <- ActivityPub.make_actor_from_url(actor_id),
|
2018-11-12 17:17:53 +00:00
|
|
|
{:ok, public_key} <- actor_id |> Actor.get_public_key_for_url() do
|
2018-07-27 08:45:35 +00:00
|
|
|
validate_conn(conn, public_key)
|
2018-05-17 09:32:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
e ->
|
2018-05-18 07:56:21 +00:00
|
|
|
Logger.debug("Could not found url for actor!")
|
2018-07-27 08:45:35 +00:00
|
|
|
Logger.debug(inspect(e))
|
2018-05-17 09:32:23 +00:00
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-18 07:56:21 +00:00
|
|
|
def validate_conn(conn, public_key) do
|
|
|
|
headers = Enum.into(conn.req_headers, %{})
|
2018-11-07 17:16:57 +00:00
|
|
|
host_without_port = String.split(headers["host"], ":") |> hd
|
2018-05-18 07:56:21 +00:00
|
|
|
headers = Map.put(headers, "host", host_without_port)
|
|
|
|
signature = split_signature(headers["signature"])
|
|
|
|
validate(headers, signature, public_key)
|
|
|
|
end
|
2018-05-17 09:32:23 +00:00
|
|
|
|
|
|
|
def build_signing_string(headers, used_headers) do
|
|
|
|
used_headers
|
|
|
|
|> Enum.map(fn header -> "#{header}: #{headers[header]}" end)
|
|
|
|
|> Enum.join("\n")
|
|
|
|
end
|
|
|
|
|
2018-06-14 15:25:55 +00:00
|
|
|
def sign(%Actor{} = actor, headers) do
|
2018-11-12 08:05:31 +00:00
|
|
|
with sigstring <- build_signing_string(headers, Map.keys(headers)),
|
2018-11-12 17:17:53 +00:00
|
|
|
{:ok, key} <- actor.keys |> Actor.prepare_public_key(),
|
2018-11-12 08:05:31 +00:00
|
|
|
signature <- sigstring |> :public_key.sign(:sha256, key) |> Base.encode64() do
|
|
|
|
[
|
|
|
|
keyId: actor.url <> "#main-key",
|
|
|
|
algorithm: "rsa-sha256",
|
|
|
|
headers: headers |> Map.keys() |> Enum.join(" "),
|
|
|
|
signature: signature
|
|
|
|
]
|
|
|
|
|> Enum.map(fn {k, v} -> "#{k}=\"#{v}\"" end)
|
|
|
|
|> Enum.join(",")
|
|
|
|
else
|
|
|
|
err ->
|
|
|
|
Logger.error("Unable to sign headers")
|
|
|
|
Logger.error(inspect(err))
|
2018-12-07 09:47:31 +00:00
|
|
|
nil
|
2018-11-12 08:05:31 +00:00
|
|
|
end
|
2018-05-17 09:32:23 +00:00
|
|
|
end
|
2018-12-07 09:47:31 +00:00
|
|
|
|
|
|
|
def generate_date_header(date \\ Timex.now("GMT")) do
|
|
|
|
with {:ok, date} <- Timex.format(date, "%a, %d %b %Y %H:%M:%S %Z", :strftime) do
|
|
|
|
date
|
|
|
|
else
|
|
|
|
{:error, err} ->
|
|
|
|
Logger.error("Unable to generate date header")
|
|
|
|
Logger.error(inspect(err))
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_request_target(method, path), do: "#{method} #{path}"
|
|
|
|
|
|
|
|
def build_digest(body) do
|
|
|
|
"SHA-256=" <> (:crypto.hash(:sha256, body) |> Base.encode64())
|
|
|
|
end
|
2018-05-17 09:32:23 +00:00
|
|
|
end
|