]> git.mxchange.org Git - friendica.git/blob - src/Util/LDSignature.php
Catch exceptions for Worker::AddContact()
[friendica.git] / src / Util / LDSignature.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Util;
23
24 use Friendica\Core\Logger;
25 use Friendica\Model\APContact;
26
27 /**
28  * Implements JSON-LD signatures
29  *
30  * Ported from Osada: https://framagit.org/macgirvin/osada
31  */
32 class LDSignature
33 {
34         public static function isSigned($data)
35         {
36                 return !empty($data['signature']);
37         }
38
39         public static function getSigner($data)
40         {
41                 if (!self::isSigned($data)) {
42                         return false;
43                 }
44
45                 $actor = JsonLD::fetchElement($data, 'actor', 'id');
46                 if (empty($actor) || !is_string($actor)) {
47                         return false;
48                 }
49
50                 $profile = APContact::getByURL($actor);
51                 if (empty($profile['pubkey'])) {
52                         return false;
53                 }
54                 $pubkey = $profile['pubkey'];
55
56                 $ohash = self::hash(self::signableOptions($data['signature']));
57                 $dhash = self::hash(self::signableData($data));
58
59                 $x = Crypto::rsaVerify($ohash . $dhash, base64_decode($data['signature']['signatureValue']), $pubkey);
60                 Logger::notice('LD-verify', ['verified' => (int)$x, 'actor' => $profile['url']]);
61
62                 if (empty($x)) {
63                         return false;
64                 } else {
65                         return $actor;
66                 }
67         }
68
69         public static function sign($data, $owner)
70         {
71                 $options = [
72                         'type' => 'RsaSignature2017',
73                         'nonce' => Strings::getRandomHex(64),
74                         'creator' => $owner['url'] . '#main-key',
75                         'created' => DateTimeFormat::utcNow(DateTimeFormat::ATOM)
76                 ];
77
78                 $ohash = self::hash(self::signableOptions($options));
79                 $dhash = self::hash(self::signableData($data));
80                 $options['signatureValue'] = base64_encode(Crypto::rsaSign($ohash . $dhash, $owner['uprvkey']));
81
82                 return array_merge($data, ['signature' => $options]);
83         }
84
85         private static function signableData($data)
86         {
87                 unset($data['signature']);
88                 return $data;
89         }
90
91         private static function signableOptions($options)
92         {
93                 $newopts = ['@context' => 'https://w3id.org/identity/v1'];
94
95                 unset($options['type']);
96                 unset($options['id']);
97                 unset($options['signatureValue']);
98
99                 return array_merge($newopts, $options);
100         }
101
102         private static function hash($obj)
103         {
104                 return hash('sha256', JsonLD::normalize($obj));
105         }
106 }