]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/lib/magicenvelope.php
Merge branch 'testing' of git@gitorious.org:statusnet/mainline into testing
[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, $signer_uri)
71     {
72         $signer_uri = $this->normalizeUser($signer_uri);
73
74         if (!$this->checkAuthor($text, $signer_uri)) {
75             throw new Exception("Unable to determine entry author.");
76         }
77
78         $keypair = $this->getKeyPair($signer_uri);
79         if (!$keypair) {
80             throw new Exception("Unable to retrive keypair for ". $signer_uri);
81         }
82         $signature_alg = Magicsig::fromString($keypair);
83         $armored_text = base64_encode($text);
84
85         return array(
86             'data' => $armored_text,
87             'encoding' => MagicEnvelope::ENCODING,
88             'data_type' => $mimetype,
89             'sig' => $signature_alg->sign($armored_text),
90             'alg' => $signature_alg->getName()
91         );
92             
93             
94     }
95
96     public function unfold($env)
97     {
98         $dom = new DOMDocument();
99         $dom->loadXML(base64_decode($env['data']));
100
101         if ($dom->documentElement->tagName != 'entry') {
102             return false;
103         }
104
105         $prov = $dom->createElementNS(MagicEnvelope::NS, 'me:provenance');
106         $prov->setAttribute('xmlns:me', MagicEnvelope::NS);
107         $data = $dom->createElementNS(MagicEnvelope::NS, 'me:data', $env['data']);
108         $data->setAttribute('type', $env['data_type']);
109         $prov->appendChild($data);
110         $enc = $dom->createElementNS(MagicEnvelope::NS, 'me:encoding', $env['encoding']);
111         $prov->appendChild($enc);
112         $alg = $dom->createElementNS(MagicEnvelope::NS, 'me:alg', $env['alg']);
113         $prov->appendChild($alg);
114         $sig = $dom->createElementNS(MagicEnvelope::NS, 'me:sig', $env['sig']);
115         $prov->appendChild($sig);
116
117         $dom->documentElement->appendChild($prov);
118
119         return $dom->saveXML();
120     }
121     
122     public function getAuthor($text) {
123         $doc = new DOMDocument();
124         if (!$doc->loadXML($text)) {
125             return FALSE;
126         }
127
128         if ($doc->documentElement->tagName == 'entry') {
129             $authors = $doc->documentElement->getElementsByTagName('author');
130             foreach ($authors as $author) {
131                 $uris = $author->getElementsByTagName('uri');
132                 foreach ($uris as $uri) {
133                     return $this->normalizeUser($uri->nodeValue);
134                 }
135             }
136         }
137     }
138     
139     public function checkAuthor($text, $signer_uri)
140     {
141         return ($this->getAuthor($text) == $signer_uri);
142     }
143     
144     public function verify($env)
145     {
146         if ($env['alg'] != 'RSA-SHA256') {
147             return false;
148         }
149
150         if ($env['encoding'] != MagicEnvelope::ENCODING) {
151             return false;
152         }
153
154         $text = base64_decode($env['data']);
155         $signer_uri = $this->getAuthor($text);
156
157         $verifier = Magicsig::fromString($this->getKeyPair($signer_uri));
158
159         return $verifier->verify($env['data'], $env['sig']);
160     }
161
162     public function parse($text)
163     {
164         $dom = DOMDocument::loadXML($text);
165         return $this->fromDom($dom);
166     }
167
168     public function fromDom($dom)
169     {
170         if ($dom->documentElement->tagName == 'entry') {
171             $env_element = $dom->getElementsByTagNameNS(MagicEnvelope::NS, 'provenance')->item(0);
172         } else if ($dom->documentElement->tagName == 'me:env') {
173             $env_element = $dom->documentElement;
174         } else {
175             return false;
176         }
177
178         $data_element = $env_element->getElementsByTagNameNS(MagicEnvelope::NS, 'data')->item(0);
179         
180         return array(
181             'data' => trim($data_element->nodeValue),
182             'data_type' => $data_element->getAttribute('type'),
183             'encoding' => $env_element->getElementsByTagNameNS(MagicEnvelope::NS, 'encoding')->item(0)->nodeValue,
184             'alg' => $env_element->getElementsByTagNameNS(MagicEnvelope::NS, 'alg')->item(0)->nodeValue,
185             'sig' => $env_element->getElementsByTagNameNS(MagicEnvelope::NS, 'sig')->item(0)->nodeValue,
186         );
187     }
188
189 }