3 * @copyright Copyright (C) 2010-2021, 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\Contact;
28 use Friendica\Model\GServer;
29 use Friendica\Model\Post;
30 use Friendica\Protocol\ActivityNamespace;
31 use Friendica\Protocol\OStatus;
32 use Friendica\Protocol\Salmon;
33 use Friendica\Util\Crypto;
34 use Friendica\Util\Network;
35 use Friendica\Util\Strings;
37 function salmon_post(App $a, $xml = '') {
40 $xml = Network::postdata();
43 Logger::debug('new salmon ' . $xml);
45 $nick = ((DI::args()->getArgc() > 1) ? Strings::escapeTags(trim(DI::args()->getArgv()[1])) : '');
47 $importer = DBA::selectFirst('user', [], ['nickname' => $nick, 'account_expired' => false, 'account_removed' => false]);
48 if (! DBA::isResult($importer)) {
49 throw new \Friendica\Network\HTTPException\InternalServerErrorException();
54 $dom = simplexml_load_string($xml,'SimpleXMLElement',0, ActivityNamespace::SALMON_ME);
58 // figure out where in the DOM tree our data is hiding
59 if (!empty($dom->provenance->data))
60 $base = $dom->provenance;
61 elseif (!empty($dom->env->data))
63 elseif (!empty($dom->data))
67 Logger::notice('unable to locate salmon data in xml');
68 throw new \Friendica\Network\HTTPException\BadRequestException();
71 // Stash the signature away for now. We have to find their key or it won't be good for anything.
74 $signature = Strings::base64UrlDecode($base->sig);
78 // strip whitespace so our data element will return to one big base64 blob
79 $data = str_replace([" ","\t","\r","\n"],["","","",""],$base->data);
81 // stash away some other stuff for later
83 $type = $base->data[0]->attributes()->type[0];
84 $keyhash = $base->sig[0]->attributes()->keyhash[0] ?? '';
85 $encoding = $base->encoding;
88 // Salmon magic signatures have evolved and there is no way of knowing ahead of time which
89 // flavour we have. We'll try and verify it regardless.
91 $stnet_signed_data = $data;
93 $signed_data = $data . '.' . Strings::base64UrlEncode($type) . '.' . Strings::base64UrlEncode($encoding) . '.' . Strings::base64UrlEncode($alg);
95 $compliant_format = str_replace('=', '', $signed_data);
99 $data = Strings::base64UrlDecode($data);
101 $author = OStatus::salmonAuthor($data, $importer);
102 $author_link = $author["author-link"];
105 Logger::notice('Could not retrieve author URI.');
106 throw new \Friendica\Network\HTTPException\BadRequestException();
109 // Once we have the author URI, go to the web and try to find their public key
111 Logger::notice('Fetching key for ' . $author_link);
113 $key = Salmon::getKey($author_link, $keyhash);
116 Logger::notice('Could not retrieve author key.');
117 throw new \Friendica\Network\HTTPException\BadRequestException();
120 $key_info = explode('.',$key);
122 $m = Strings::base64UrlDecode($key_info[1]);
123 $e = Strings::base64UrlDecode($key_info[2]);
125 Logger::info('key details', ['info' => $key_info]);
127 $pubkey = Crypto::meToPem($m, $e);
129 // We should have everything we need now. Let's see if it verifies.
131 // Try GNU Social format
132 $verify = Crypto::rsaVerify($signed_data, $signature, $pubkey);
136 Logger::notice('message did not verify using protocol. Trying compliant format.');
137 $verify = Crypto::rsaVerify($compliant_format, $signature, $pubkey);
142 Logger::notice('message did not verify using padding. Trying old statusnet format.');
143 $verify = Crypto::rsaVerify($stnet_signed_data, $signature, $pubkey);
148 Logger::notice('Message did not verify. Discarding.');
149 throw new \Friendica\Network\HTTPException\BadRequestException();
152 Logger::notice('Message verified with mode '.$mode);
157 * If we reached this point, the message is good. Now let's figure out if the author is allowed to send us stuff.
161 $contact = DBA::selectFirst('contact', ["`network` IN (?, ?) AND (`nurl` = ? OR `alias` = ? OR `alias` = ?) AND `uid` = ?",
162 Protocol::OSTATUS, Protocol::DFRN, Strings::normaliseLink($author_link), $author_link, Strings::normaliseLink($author_link), $importer['uid']]);
164 if (!empty($contact['gsid'])) {
165 GServer::setProtocol($contact['gsid'], Post\DeliveryData::OSTATUS);
168 // Have we ignored the person?
169 // If so we can not accept this post.
171 if (!empty($contact['blocked'])) {
172 Logger::notice('Ignoring this author.');
173 throw new \Friendica\Network\HTTPException\AcceptedException();
176 // Placeholder for hub discovery.
179 $contact = $contact ?: [];
181 OStatus::import($data, $importer, $contact, $hub);
183 throw new \Friendica\Network\HTTPException\OKException();