]> git.mxchange.org Git - friendica.git/blob - src/Util/LDSignature.php
spelling: parameter
[friendica.git] / src / Util / LDSignature.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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         /**
35          * Checks if element 'signature' is found and not empty
36          *
37          * @param array $data
38          * @return bool
39          */
40         public static function isSigned(array $data): bool
41         {
42                 return !empty($data['signature']);
43         }
44
45         /**
46          * Returns actor (signer) from given data
47          *
48          * @param array $data
49          * @return mixed Returns actor or false on error
50          */
51         public static function getSigner(array $data)
52         {
53                 if (!self::isSigned($data)) {
54                         return false;
55                 }
56
57                 $actor = JsonLD::fetchElement($data, 'actor', 'id');
58                 if (empty($actor) || !is_string($actor)) {
59                         return false;
60                 }
61
62                 $profile = APContact::getByURL($actor);
63                 if (empty($profile['pubkey'])) {
64                         return false;
65                 }
66                 $pubkey = $profile['pubkey'];
67
68                 $ohash = self::hash(self::signableOptions($data['signature']));
69                 $dhash = self::hash(self::signableData($data));
70
71                 $x = Crypto::rsaVerify($ohash . $dhash, base64_decode($data['signature']['signatureValue']), $pubkey);
72                 Logger::info('LD-verify', ['verified' => (int)$x, 'actor' => $profile['url']]);
73
74                 if (empty($x)) {
75                         return false;
76                 } else {
77                         return $actor;
78                 }
79         }
80
81         /**
82          * Signs given data by owner's signature
83          *
84          * @param array $data Data to sign
85          * @param array $owner Owner information, like URL
86          * @return array Merged array of $data and signature
87          */
88         public static function sign(array $data, array $owner): array
89         {
90                 $options = [
91                         'type' => 'RsaSignature2017',
92                         'nonce' => Strings::getRandomHex(64),
93                         'creator' => $owner['url'] . '#main-key',
94                         'created' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
95                 ];
96
97                 $ohash = self::hash(self::signableOptions($options));
98                 $dhash = self::hash(self::signableData($data));
99                 $options['signatureValue'] = base64_encode(Crypto::rsaSign($ohash . $dhash, $owner['uprvkey']));
100
101                 return array_merge($data, ['signature' => $options]);
102         }
103
104         /**
105          * Removes element 'signature' from array
106          *
107          * @param array $data
108          * @return array With no element 'signature'
109          */
110         private static function signableData(array $data): array
111         {
112                 unset($data['signature']);
113                 return $data;
114         }
115
116         /**
117          * Removes some elements and adds '@context' to it
118          *
119          * @param array $options
120          * @return array With some removed elements and added '@context' element
121          */
122         private static function signableOptions(array $options): array
123         {
124                 $newopts = ['@context' => 'https://w3id.org/identity/v1'];
125
126                 unset($options['type']);
127                 unset($options['id']);
128                 unset($options['signatureValue']);
129
130                 return array_merge($newopts, $options);
131         }
132
133         /**
134          * Hashes normalized object
135          *
136          * @param ??? $obj
137          * @return string SHA256 hash
138          */
139         private static function hash($obj): string
140         {
141                 return hash('sha256', JsonLD::normalize($obj));
142         }
143 }