]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/lib/magicenvelope.php
bbd4ce17a24f69e172f1fa5c14ffaa21d9a6d819
[quix0rs-gnu-social.git] / plugins / OStatus / lib / magicenvelope.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * A sample module to show best practices for StatusNet plugins
7  *
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @package   StatusNet
24  * @author    James Walker <james@status.net>
25  * @copyright 2010 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
27  * @link      http://status.net/
28  */
29
30 class MagicEnvelope
31 {
32     const ENCODING = 'base64url';
33
34     const NS = 'http://salmon-protocol.org/ns/magic-env';
35
36     private function normalizeUser($user_id)
37     {
38         if (substr($user_id, 0, 5) == 'http:' ||
39             substr($user_id, 0, 6) == 'https:' ||
40             substr($user_id, 0, 5) == 'acct:') {
41             return $user_id;
42         }
43
44         if (strpos($user_id, '@') !== FALSE) {
45             return 'acct:' . $user_id;
46         }
47
48         return 'http://' . $user_id;
49     }
50
51     public function getKeyPair($signer_uri)
52     {
53         $disco = new Discovery();
54
55         try {
56             $xrd = $disco->lookup($signer_uri);
57         } catch (Exception $e) {
58             return false;
59         }
60         if ($xrd->links) {
61             if ($link = Discovery::getService($xrd->links, Magicsig::PUBLICKEYREL)) {
62                 $keypair = false;
63                 $parts = explode(',', $link['href']);
64                 if (count($parts) == 2) {
65                     $keypair = $parts[1];
66                 } else {
67                     // Backwards compatibility check for separator bug in 0.9.0
68                     $parts = explode(';', $link['href']);
69                     if (count($parts) == 2) {
70                         $keypair = $parts[1];
71                     }
72                 }
73
74                 if ($keypair) {
75                     return $keypair;
76                 }
77             }
78         }
79         throw new Exception('Unable to locate signer public key.');
80     }
81
82
83     public function signMessage($text, $mimetype, $keypair)
84     {
85         $signature_alg = Magicsig::fromString($keypair);
86         $armored_text = Magicsig::base64_url_encode($text);
87
88         return array(
89             'data' => $armored_text,
90             'encoding' => MagicEnvelope::ENCODING,
91             'data_type' => $mimetype,
92             'sig' => $signature_alg->sign($armored_text),
93             'alg' => $signature_alg->getName()
94         );
95
96     }
97
98     public function toXML($env) {
99         $xs = new XMLStringer();
100         $xs->startXML();
101         $xs->elementStart('me:env', array('xmlns:me' => MagicEnvelope::NS));
102         $xs->element('me:data', array('type' => $env['data_type']), $env['data']);
103         $xs->element('me:encoding', null, $env['encoding']);
104         $xs->element('me:alg', null, $env['alg']);
105         $xs->element('me:sig', null, $env['sig']);
106         $xs->elementEnd('me:env');
107
108         $string =  $xs->getString();
109         common_debug($string);
110         return $string;
111     }
112
113
114     public function unfold($env)
115     {
116         $dom = new DOMDocument();
117         $dom->loadXML(Magicsig::base64_url_decode($env['data']));
118
119         if ($dom->documentElement->tagName != 'entry') {
120             return false;
121         }
122
123         $prov = $dom->createElementNS(MagicEnvelope::NS, 'me:provenance');
124         $prov->setAttribute('xmlns:me', MagicEnvelope::NS);
125         $data = $dom->createElementNS(MagicEnvelope::NS, 'me:data', $env['data']);
126         $data->setAttribute('type', $env['data_type']);
127         $prov->appendChild($data);
128         $enc = $dom->createElementNS(MagicEnvelope::NS, 'me:encoding', $env['encoding']);
129         $prov->appendChild($enc);
130         $alg = $dom->createElementNS(MagicEnvelope::NS, 'me:alg', $env['alg']);
131         $prov->appendChild($alg);
132         $sig = $dom->createElementNS(MagicEnvelope::NS, 'me:sig', $env['sig']);
133         $prov->appendChild($sig);
134
135         $dom->documentElement->appendChild($prov);
136
137         return $dom->saveXML();
138     }
139
140     public function getAuthor($text) {
141         $doc = new DOMDocument();
142         if (!$doc->loadXML($text)) {
143             return FALSE;
144         }
145
146         if ($doc->documentElement->tagName == 'entry') {
147             $authors = $doc->documentElement->getElementsByTagName('author');
148             foreach ($authors as $author) {
149                 $uris = $author->getElementsByTagName('uri');
150                 foreach ($uris as $uri) {
151                     return $this->normalizeUser($uri->nodeValue);
152                 }
153             }
154         }
155     }
156
157     public function checkAuthor($text, $signer_uri)
158     {
159         return ($this->getAuthor($text) == $signer_uri);
160     }
161
162     public function verify($env)
163     {
164         if ($env['alg'] != 'RSA-SHA256') {
165             common_log(LOG_DEBUG, "Salmon error: bad algorithm");
166             return false;
167         }
168
169         if ($env['encoding'] != MagicEnvelope::ENCODING) {
170             common_log(LOG_DEBUG, "Salmon error: bad encoding");
171             return false;
172         }
173
174         $text = Magicsig::base64_url_decode($env['data']);
175         $signer_uri = $this->getAuthor($text);
176
177         try {
178             $keypair = $this->getKeyPair($signer_uri);
179         } catch (Exception $e) {
180             common_log(LOG_DEBUG, "Salmon error: ".$e->getMessage());
181             return false;
182         }
183
184         $verifier = Magicsig::fromString($keypair);
185
186         if (!$verifier) {
187             common_log(LOG_DEBUG, "Salmon error: unable to parse keypair");
188             return false;
189         }
190
191         return $verifier->verify($env['data'], $env['sig']);
192     }
193
194     public function parse($text)
195     {
196         $dom = DOMDocument::loadXML($text);
197         return $this->fromDom($dom);
198     }
199
200     public function fromDom($dom)
201     {
202         $env_element = $dom->getElementsByTagNameNS(MagicEnvelope::NS, 'env')->item(0);
203         if (!$env_element) {
204             $env_element = $dom->getElementsByTagNameNS(MagicEnvelope::NS, 'provenance')->item(0);
205         }
206
207         if (!$env_element) {
208             return false;
209         }
210
211         $data_element = $env_element->getElementsByTagNameNS(MagicEnvelope::NS, 'data')->item(0);
212         $sig_element = $env_element->getElementsByTagNameNS(MagicEnvelope::NS, 'sig')->item(0);
213         return array(
214             'data' => preg_replace('/\s/', '', $data_element->nodeValue),
215             'data_type' => $data_element->getAttribute('type'),
216             'encoding' => $env_element->getElementsByTagNameNS(MagicEnvelope::NS, 'encoding')->item(0)->nodeValue,
217             'alg' => $env_element->getElementsByTagNameNS(MagicEnvelope::NS, 'alg')->item(0)->nodeValue,
218             'sig' => preg_replace('/\s/', '', $sig_element->nodeValue),
219         );
220     }
221
222 }