3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23 use Friendica\Core\Logger;
24 use Friendica\Core\Protocol;
25 use Friendica\Database\DBA;
27 use Friendica\Model\GServer;
28 use Friendica\Model\Post;
29 use Friendica\Protocol\ActivityNamespace;
30 use Friendica\Protocol\OStatus;
31 use Friendica\Protocol\Salmon;
32 use Friendica\Util\Crypto;
33 use Friendica\Util\Network;
34 use Friendica\Util\Strings;
36 function salmon_post(App $a, $xml = '') {
39 $xml = Network::postdata();
42 Logger::debug('new salmon ' . $xml);
44 $nick = trim(DI::args()->getArgv()[1] ?? '');
46 $importer = DBA::selectFirst('user', [], ['nickname' => $nick, 'account_expired' => false, 'account_removed' => false]);
47 if (! DBA::isResult($importer)) {
48 throw new \Friendica\Network\HTTPException\InternalServerErrorException();
53 $dom = simplexml_load_string($xml,'SimpleXMLElement',0, ActivityNamespace::SALMON_ME);
57 // figure out where in the DOM tree our data is hiding
58 if (!empty($dom->provenance->data))
59 $base = $dom->provenance;
60 elseif (!empty($dom->env->data))
62 elseif (!empty($dom->data))
66 Logger::notice('unable to locate salmon data in xml');
67 throw new \Friendica\Network\HTTPException\BadRequestException();
70 // Stash the signature away for now. We have to find their key or it won't be good for anything.
73 $signature = Strings::base64UrlDecode($base->sig);
77 // strip whitespace so our data element will return to one big base64 blob
78 $data = str_replace([" ","\t","\r","\n"],["","","",""],$base->data);
80 // stash away some other stuff for later
82 $type = $base->data[0]->attributes()->type[0];
83 $keyhash = $base->sig[0]->attributes()->keyhash[0] ?? '';
84 $encoding = $base->encoding;
87 // Salmon magic signatures have evolved and there is no way of knowing ahead of time which
88 // flavour we have. We'll try and verify it regardless.
90 $stnet_signed_data = $data;
92 $signed_data = $data . '.' . Strings::base64UrlEncode($type) . '.' . Strings::base64UrlEncode($encoding) . '.' . Strings::base64UrlEncode($alg);
94 $compliant_format = str_replace('=', '', $signed_data);
98 $data = Strings::base64UrlDecode($data);
100 $author = OStatus::salmonAuthor($data, $importer);
101 $author_link = $author["author-link"];
104 Logger::notice('Could not retrieve author URI.');
105 throw new \Friendica\Network\HTTPException\BadRequestException();
108 // Once we have the author URI, go to the web and try to find their public key
110 Logger::notice('Fetching key for ' . $author_link);
112 $key = Salmon::getKey($author_link, $keyhash);
115 Logger::notice('Could not retrieve author key.');
116 throw new \Friendica\Network\HTTPException\BadRequestException();
119 $key_info = explode('.',$key);
121 $m = Strings::base64UrlDecode($key_info[1]);
122 $e = Strings::base64UrlDecode($key_info[2]);
124 Logger::info('key details', ['info' => $key_info]);
126 $pubkey = Crypto::meToPem($m, $e);
128 // We should have everything we need now. Let's see if it verifies.
130 // Try GNU Social format
131 $verify = Crypto::rsaVerify($signed_data, $signature, $pubkey);
135 Logger::notice('message did not verify using protocol. Trying compliant format.');
136 $verify = Crypto::rsaVerify($compliant_format, $signature, $pubkey);
141 Logger::notice('message did not verify using padding. Trying old statusnet format.');
142 $verify = Crypto::rsaVerify($stnet_signed_data, $signature, $pubkey);
147 Logger::notice('Message did not verify. Discarding.');
148 throw new \Friendica\Network\HTTPException\BadRequestException();
151 Logger::notice('Message verified with mode '.$mode);
156 * If we reached this point, the message is good. Now let's figure out if the author is allowed to send us stuff.
160 $contact = DBA::selectFirst('contact', [], ["`network` IN (?, ?) AND (`nurl` = ? OR `alias` = ? OR `alias` = ?) AND `uid` = ?",
161 Protocol::OSTATUS, Protocol::DFRN, Strings::normaliseLink($author_link), $author_link, Strings::normaliseLink($author_link), $importer['uid']]);
163 if (!empty($contact['gsid'])) {
164 GServer::setProtocol($contact['gsid'], Post\DeliveryData::OSTATUS);
167 // Have we ignored the person?
168 // If so we can not accept this post.
170 if (!empty($contact['blocked'])) {
171 Logger::notice('Ignoring this author.');
172 throw new \Friendica\Network\HTTPException\AcceptedException();
175 // Placeholder for hub discovery.
178 $contact = $contact ?: [];
180 OStatus::import($data, $importer, $contact, $hub);
182 throw new \Friendica\Network\HTTPException\OKException();