]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/lib/magicenvelope.php
Merge branch '0.9.x' of git@gitorious.org:statusnet/mainline into 0.9.x
[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                 list($type, $keypair) = explode(';', $link['href']);
63                 return $keypair;
64             }
65         }
66         throw new Exception('Unable to locate signer public key');
67     }
68
69
70     public function signMessage($text, $mimetype, $keypair)
71     {
72         $signature_alg = Magicsig::fromString($keypair);
73         $armored_text = base64_encode($text);
74
75         return array(
76             'data' => $armored_text,
77             'encoding' => MagicEnvelope::ENCODING,
78             'data_type' => $mimetype,
79             'sig' => $signature_alg->sign($armored_text),
80             'alg' => $signature_alg->getName()
81         );
82             
83             
84     }
85
86     public function toXML($env) {
87         $dom = new DOMDocument();
88
89         $envelope = $dom->createElementNS(MagicEnvelope::NS, 'me:env');
90         $envelope->setAttribute('xmlns:me', MagicEnvelope::NS);
91         $data = $dom->createElementNS(MagicEnvelope::NS, 'me:data', $env['data']);
92         $data->setAttribute('type', $env['data_type']);
93         $envelope->appendChild($data);
94         $enc = $dom->createElementNS(MagicEnvelope::NS, 'me:encoding', $env['encoding']);
95         $envelope->appendChild($enc);
96         $alg = $dom->createElementNS(MagicEnvelope::NS, 'me:alg', $env['alg']);
97         $envelope->appendChild($alg);
98         $sig = $dom->createElementNS(MagicEnvelope::NS, 'me:sig', $env['sig']);
99         $envelope->appendChild($sig);
100
101         $dom->appendChild($envelope);
102         
103         
104         return $dom->saveXML();
105     }
106
107     
108     public function unfold($env)
109     {
110         $dom = new DOMDocument();
111         $dom->loadXML(base64_decode($env['data']));
112
113         if ($dom->documentElement->tagName != 'entry') {
114             return false;
115         }
116
117         $prov = $dom->createElementNS(MagicEnvelope::NS, 'me:provenance');
118         $prov->setAttribute('xmlns:me', MagicEnvelope::NS);
119         $data = $dom->createElementNS(MagicEnvelope::NS, 'me:data', $env['data']);
120         $data->setAttribute('type', $env['data_type']);
121         $prov->appendChild($data);
122         $enc = $dom->createElementNS(MagicEnvelope::NS, 'me:encoding', $env['encoding']);
123         $prov->appendChild($enc);
124         $alg = $dom->createElementNS(MagicEnvelope::NS, 'me:alg', $env['alg']);
125         $prov->appendChild($alg);
126         $sig = $dom->createElementNS(MagicEnvelope::NS, 'me:sig', $env['sig']);
127         $prov->appendChild($sig);
128
129         $dom->documentElement->appendChild($prov);
130
131         return $dom->saveXML();
132     }
133     
134     public function getAuthor($text) {
135         $doc = new DOMDocument();
136         if (!$doc->loadXML($text)) {
137             return FALSE;
138         }
139
140         if ($doc->documentElement->tagName == 'entry') {
141             $authors = $doc->documentElement->getElementsByTagName('author');
142             foreach ($authors as $author) {
143                 $uris = $author->getElementsByTagName('uri');
144                 foreach ($uris as $uri) {
145                     return $this->normalizeUser($uri->nodeValue);
146                 }
147             }
148         }
149     }
150     
151     public function checkAuthor($text, $signer_uri)
152     {
153         return ($this->getAuthor($text) == $signer_uri);
154     }
155     
156     public function verify($env)
157     {
158         if ($env['alg'] != 'RSA-SHA256') {
159             common_log(LOG_DEBUG, "Salmon error: bad algorithm");
160             return false;
161         }
162
163         if ($env['encoding'] != MagicEnvelope::ENCODING) {
164             common_log(LOG_DEBUG, "Salmon error: bad encoding");
165             return false;
166         }
167
168         $text = base64_decode($env['data']);
169         $signer_uri = $this->getAuthor($text);
170
171         try {
172             $keypair = $this->getKeyPair($signer_uri);
173         } catch (Exception $e) {
174             common_log(LOG_DEBUG, "Salmon error: ".$e->getMessage());
175             return false;
176         }
177         
178         $verifier = Magicsig::fromString($keypair);
179
180         if (!$verifier) {
181             common_log(LOG_DEBUG, "Salmon error: unable to parse keypair");
182             return false;
183         }
184         
185         return $verifier->verify($env['data'], $env['sig']);
186     }
187
188     public function parse($text)
189     {
190         $dom = DOMDocument::loadXML($text);
191         return $this->fromDom($dom);
192     }
193
194     public function fromDom($dom)
195     {
196         if ($dom->documentElement->tagName == 'entry') {
197             $env_element = $dom->getElementsByTagNameNS(MagicEnvelope::NS, 'provenance')->item(0);
198         } else if ($dom->documentElement->tagName == 'me:env') {
199             $env_element = $dom->documentElement;
200         } else {
201             return false;
202         }
203
204         $data_element = $env_element->getElementsByTagNameNS(MagicEnvelope::NS, 'data')->item(0);
205         
206         return array(
207             'data' => trim($data_element->nodeValue),
208             'data_type' => $data_element->getAttribute('type'),
209             'encoding' => $env_element->getElementsByTagNameNS(MagicEnvelope::NS, 'encoding')->item(0)->nodeValue,
210             'alg' => $env_element->getElementsByTagNameNS(MagicEnvelope::NS, 'alg')->item(0)->nodeValue,
211             'sig' => $env_element->getElementsByTagNameNS(MagicEnvelope::NS, 'sig')->item(0)->nodeValue,
212         );
213     }
214
215 }