]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/lib/magicenvelope.php
generate keypairs for users, and put them in the XRD for discovery
[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 require_once 'magicsig.php';
31
32 class MagicEnvelope
33 {
34     const ENCODING = 'base64url';
35
36     const NS = 'http://salmon-protocol.org/ns/magic-env';
37     
38     private function normalizeUser($user_id)
39     {
40         if (substr($user_id, 0, 5) == 'http:' ||
41             substr($user_id, 0, 6) == 'https:' ||
42             substr($user_id, 0, 5) == 'acct:') {
43             return $user_id;
44         }
45
46         if (strpos($user_id, '@') !== FALSE) {
47             return 'acct:' . $user_id;
48         }
49
50         return 'http://' . $user_id;
51     }
52
53     public function getKeyPair($signer_uri)
54     {
55         return 'RSA.79_L2gq-TD72Nsb5yGS0r9stLLpJZF5AHXyxzWmQmlqKl276LEJEs8CppcerLcR90MbYQUwt-SX9slx40Yq3vA==.AQAB.AR-jo5KMfSISmDAT2iMs2_vNFgWRjl5rbJVvA0SpGIEWyPdCGxlPtCbTexp8-0ZEIe8a4SyjatBECH5hxgMTpw==';
56     }
57
58
59     public function signMessage($text, $mimetype, $signer_uri)
60     {
61         $signer_uri = $this->normalizeUser($signer_uri);
62
63         if (!$this->checkAuthor($text, $signer_uri)) {
64             return false;
65         }
66
67         $signature_alg = new MagicsigRsaSha256($this->getKeyPair($signer_uri));
68         $armored_text = base64_encode($text);
69
70         return array(
71             'data' => $armored_text,
72             'encoding' => MagicEnvelope::ENCODING,
73             'data_type' => $mimetype,
74             'sig' => $signature_alg->sign($armored_text),
75             'alg' => $signature_alg->getName()
76         );
77             
78             
79     }
80
81     public function unfold($env)
82     {
83         $dom = new DOMDocument();
84         $dom->loadXML(base64_decode($env['data']));
85
86         if ($dom->documentElement->tagName != 'entry') {
87             return false;
88         }
89
90         $prov = $dom->createElementNS(MagicEnvelope::NS, 'me:provenance');
91         $prov->setAttribute('xmlns:me', MagicEnvelope::NS);
92         $data = $dom->createElementNS(MagicEnvelope::NS, 'me:data', $env['data']);
93         $data->setAttribute('type', $env['data_type']);
94         $prov->appendChild($data);
95         $enc = $dom->createElementNS(MagicEnvelope::NS, 'me:encoding', $env['encoding']);
96         $prov->appendChild($enc);
97         $alg = $dom->createElementNS(MagicEnvelope::NS, 'me:alg', $env['alg']);
98         $prov->appendChild($alg);
99         $sig = $dom->createElementNS(MagicEnvelope::NS, 'me:sig', $env['sig']);
100         $prov->appendChild($sig);
101
102         $dom->documentElement->appendChild($prov);
103
104         return $dom->saveXML();
105     }
106     
107     public function getAuthor($text) {
108         $doc = new DOMDocument();
109         if (!$doc->loadXML($text)) {
110             return FALSE;
111         }
112
113         if ($doc->documentElement->tagName == 'entry') {
114             $authors = $doc->documentElement->getElementsByTagName('author');
115             foreach ($authors as $author) {
116                 $uris = $author->getElementsByTagName('uri');
117                 foreach ($uris as $uri) {
118                     return $this->normalizeUser($uri->nodeValue);
119                 }
120             }
121         }
122     }
123     
124     public function checkAuthor($text, $signer_uri)
125     {
126         return ($this->getAuthor($text) == $signer_uri);
127     }
128     
129     public function verify($env)
130     {
131         if ($env['alg'] != 'RSA-SHA256') {
132             return false;
133         }
134
135         if ($env['encoding'] != MagicEnvelope::ENCODING) {
136             return false;
137         }
138
139         $text = base64_decode($env['data']);
140         $signer_uri = $this->getAuthor($text);
141
142         $verifier = new MagicsigRsaSha256($this->getKeyPair($signer_uri));
143
144         return $verifier->verify($env['data'], $env['sig']);
145     }
146
147     public function parse($text)
148     {
149         $dom = DOMDocument::loadXML($text);
150         return $this->fromDom($dom);
151     }
152
153     public function fromDom($dom)
154     {
155         if ($dom->documentElement->tagName == 'entry') {
156             $env_element = $dom->getElementsByTagNameNS(MagicEnvelope::NS, 'provenance')->item(0);
157         } else if ($dom->documentElement->tagName == 'me:env') {
158             $env_element = $dom->documentElement;
159         } else {
160             return false;
161         }
162
163         $data_element = $env_element->getElementsByTagNameNS(MagicEnvelope::NS, 'data')->item(0);
164         
165         return array(
166             'data' => trim($data_element->nodeValue),
167             'data_type' => $data_element->getAttribute('type'),
168             'encoding' => $env_element->getElementsByTagNameNS(MagicEnvelope::NS, 'encoding')->item(0)->nodeValue,
169             'alg' => $env_element->getElementsByTagNameNS(MagicEnvelope::NS, 'alg')->item(0)->nodeValue,
170             'sig' => $env_element->getElementsByTagNameNS(MagicEnvelope::NS, 'sig')->item(0)->nodeValue,
171         );
172     }
173
174 }