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