b1c2dc7fa96972cfc59de974098374cd5d1bd0df
[akkoma] / lib / pleroma / web / salmon / salmon.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Salmon do
6 @httpoison Application.get_env(:pleroma, :httpoison)
7
8 use Bitwise
9 alias Pleroma.Instances
10 alias Pleroma.Web.XML
11 alias Pleroma.Web.OStatus.ActivityRepresenter
12 alias Pleroma.User
13 require Logger
14
15 def decode(salmon) do
16 doc = XML.parse_document(salmon)
17
18 {:xmlObj, :string, data} = :xmerl_xpath.string('string(//me:data[1])', doc)
19 {:xmlObj, :string, sig} = :xmerl_xpath.string('string(//me:sig[1])', doc)
20 {:xmlObj, :string, alg} = :xmerl_xpath.string('string(//me:alg[1])', doc)
21 {:xmlObj, :string, encoding} = :xmerl_xpath.string('string(//me:encoding[1])', doc)
22 {:xmlObj, :string, type} = :xmerl_xpath.string('string(//me:data[1]/@type)', doc)
23
24 {:ok, data} = Base.url_decode64(to_string(data), ignore: :whitespace)
25 {:ok, sig} = Base.url_decode64(to_string(sig), ignore: :whitespace)
26 alg = to_string(alg)
27 encoding = to_string(encoding)
28 type = to_string(type)
29
30 [data, type, encoding, alg, sig]
31 end
32
33 def fetch_magic_key(salmon) do
34 with [data, _, _, _, _] <- decode(salmon),
35 doc <- XML.parse_document(data),
36 uri when not is_nil(uri) <- XML.string_from_xpath("/entry/author[1]/uri", doc),
37 {:ok, public_key} <- User.get_public_key_for_ap_id(uri),
38 magic_key <- encode_key(public_key) do
39 {:ok, magic_key}
40 end
41 end
42
43 def decode_and_validate(magickey, salmon) do
44 [data, type, encoding, alg, sig] = decode(salmon)
45
46 signed_text =
47 [data, type, encoding, alg]
48 |> Enum.map(&Base.url_encode64/1)
49 |> Enum.join(".")
50
51 key = decode_key(magickey)
52
53 verify = :public_key.verify(signed_text, :sha256, sig, key)
54
55 if verify do
56 {:ok, data}
57 else
58 :error
59 end
60 end
61
62 def decode_key("RSA." <> magickey) do
63 make_integer = fn bin ->
64 list = :erlang.binary_to_list(bin)
65 Enum.reduce(list, 0, fn el, acc -> acc <<< 8 ||| el end)
66 end
67
68 [modulus, exponent] =
69 magickey
70 |> String.split(".")
71 |> Enum.map(fn n -> Base.url_decode64!(n, padding: false) end)
72 |> Enum.map(make_integer)
73
74 {:RSAPublicKey, modulus, exponent}
75 end
76
77 def encode_key({:RSAPublicKey, modulus, exponent}) do
78 modulus_enc = :binary.encode_unsigned(modulus) |> Base.url_encode64()
79 exponent_enc = :binary.encode_unsigned(exponent) |> Base.url_encode64()
80
81 "RSA.#{modulus_enc}.#{exponent_enc}"
82 end
83
84 # Native generation of RSA keys is only available since OTP 20+ and in default build conditions
85 # We try at compile time to generate natively an RSA key otherwise we fallback on the old way.
86 try do
87 _ = :public_key.generate_key({:rsa, 2048, 65537})
88
89 def generate_rsa_pem do
90 key = :public_key.generate_key({:rsa, 2048, 65537})
91 entry = :public_key.pem_entry_encode(:RSAPrivateKey, key)
92 pem = :public_key.pem_encode([entry]) |> String.trim_trailing()
93 {:ok, pem}
94 end
95 rescue
96 _ ->
97 def generate_rsa_pem do
98 port = Port.open({:spawn, "openssl genrsa"}, [:binary])
99
100 {:ok, pem} =
101 receive do
102 {^port, {:data, pem}} -> {:ok, pem}
103 end
104
105 Port.close(port)
106
107 if Regex.match?(~r/RSA PRIVATE KEY/, pem) do
108 {:ok, pem}
109 else
110 :error
111 end
112 end
113 end
114
115 def keys_from_pem(pem) do
116 [private_key_code] = :public_key.pem_decode(pem)
117 private_key = :public_key.pem_entry_decode(private_key_code)
118 {:RSAPrivateKey, _, modulus, exponent, _, _, _, _, _, _, _} = private_key
119 public_key = {:RSAPublicKey, modulus, exponent}
120 {:ok, private_key, public_key}
121 end
122
123 def encode(private_key, doc) do
124 type = "application/atom+xml"
125 encoding = "base64url"
126 alg = "RSA-SHA256"
127
128 signed_text =
129 [doc, type, encoding, alg]
130 |> Enum.map(&Base.url_encode64/1)
131 |> Enum.join(".")
132
133 signature =
134 signed_text
135 |> :public_key.sign(:sha256, private_key)
136 |> to_string
137 |> Base.url_encode64()
138
139 doc_base64 =
140 doc
141 |> Base.url_encode64()
142
143 # Don't need proper xml building, these strings are safe to leave unescaped
144 salmon = """
145 <?xml version="1.0" encoding="UTF-8"?>
146 <me:env xmlns:me="http://salmon-protocol.org/ns/magic-env">
147 <me:data type="application/atom+xml">#{doc_base64}</me:data>
148 <me:encoding>#{encoding}</me:encoding>
149 <me:alg>#{alg}</me:alg>
150 <me:sig>#{signature}</me:sig>
151 </me:env>
152 """
153
154 {:ok, salmon}
155 end
156
157 def remote_users(%{data: %{"to" => to} = data}) do
158 to = to ++ (data["cc"] || [])
159
160 to
161 |> Enum.map(fn id -> User.get_cached_by_ap_id(id) end)
162 |> Enum.filter(fn user -> user && !user.local end)
163 end
164
165 @doc "Pushes an activity to remote account."
166 def send_to_user(%{recipient: %{info: %{salmon: salmon}}} = params),
167 do: send_to_user(Map.put(params, :recipient, salmon))
168
169 def send_to_user(%{recipient: url, feed: feed, poster: poster} = params) when is_binary(url) do
170 with {:ok, %{status: code}} when code in 200..299 <-
171 poster.(
172 url,
173 feed,
174 [{"Content-Type", "application/magic-envelope+xml"}]
175 ) do
176 if !Map.has_key?(params, :unreachable_since) || params[:unreachable_since],
177 do: Instances.set_reachable(url)
178
179 Logger.debug(fn -> "Pushed to #{url}, code #{code}" end)
180 :ok
181 else
182 e ->
183 unless params[:unreachable_since], do: Instances.set_reachable(url)
184 Logger.debug(fn -> "Pushing Salmon to #{url} failed, #{inspect(e)}" end)
185 :error
186 end
187 end
188
189 def send_to_user(_), do: :noop
190
191 @supported_activities [
192 "Create",
193 "Follow",
194 "Like",
195 "Announce",
196 "Undo",
197 "Delete"
198 ]
199
200 @doc """
201 Publishes an activity to remote accounts
202 """
203 @spec publish(User.t(), Pleroma.Activity.t(), Pleroma.HTTP.t()) :: none
204 def publish(user, activity, poster \\ &@httpoison.post/3)
205
206 def publish(%{info: %{keys: keys}} = user, %{data: %{"type" => type}} = activity, poster)
207 when type in @supported_activities do
208 feed = ActivityRepresenter.to_simple_form(activity, user, true)
209
210 if feed do
211 feed =
212 ActivityRepresenter.wrap_with_entry(feed)
213 |> :xmerl.export_simple(:xmerl_xml)
214 |> to_string
215
216 {:ok, private, _} = keys_from_pem(keys)
217 {:ok, feed} = encode(private, feed)
218
219 remote_users = remote_users(activity)
220
221 salmon_urls = Enum.map(remote_users, & &1.info.salmon)
222 reachable_urls_metadata = Instances.filter_reachable(salmon_urls)
223 reachable_urls = Map.keys(reachable_urls_metadata)
224
225 remote_users
226 |> Enum.filter(&(&1.info.salmon in reachable_urls))
227 |> Enum.each(fn remote_user ->
228 Logger.debug(fn -> "Sending Salmon to #{remote_user.ap_id}" end)
229
230 Pleroma.Web.Federator.enqueue(:publish_single_salmon, %{
231 recipient: remote_user,
232 feed: feed,
233 poster: poster,
234 unreachable_since: reachable_urls_metadata[remote_user.info.salmon]
235 })
236 end)
237 end
238 end
239
240 def publish(%{id: id}, _, _), do: Logger.debug(fn -> "Keys missing for user #{id}" end)
241 end