3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * A sample module to show best practices for StatusNet plugins
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.
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.
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/>.
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/
32 const ENCODING = 'base64url';
34 const NS = 'http://salmon-protocol.org/ns/magic-env';
36 private function normalizeUser($user_id)
38 if (substr($user_id, 0, 5) == 'http:' ||
39 substr($user_id, 0, 6) == 'https:' ||
40 substr($user_id, 0, 5) == 'acct:') {
44 if (strpos($user_id, '@') !== FALSE) {
45 return 'acct:' . $user_id;
48 return 'http://' . $user_id;
51 public function getKeyPair($signer_uri)
53 $disco = new Discovery();
56 $xrd = $disco->lookup($signer_uri);
57 } catch (Exception $e) {
61 if ($link = Discovery::getService($xrd->links, Magicsig::PUBLICKEYREL)) {
63 $parts = explode(',', $link['href']);
64 if (count($parts) == 2) {
67 // Backwards compatibility check for separator bug in 0.9.0
68 $parts = explode(';', $link['href']);
69 if (count($parts) == 2) {
80 throw new Exception(_m('Unable to locate signer public key.'));
84 public function signMessage($text, $mimetype, $keypair)
86 $signature_alg = Magicsig::fromString($keypair);
87 $armored_text = Magicsig::base64_url_encode($text);
90 'data' => $armored_text,
91 'encoding' => MagicEnvelope::ENCODING,
92 'data_type' => $mimetype,
93 'sig' => $signature_alg->sign($armored_text),
94 'alg' => $signature_alg->getName()
98 public function toXML($env) {
99 $xs = new XMLStringer();
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');
108 $string = $xs->getString();
109 common_debug($string);
113 public function unfold($env)
115 $dom = new DOMDocument();
116 $dom->loadXML(Magicsig::base64_url_decode($env['data']));
118 if ($dom->documentElement->tagName != 'entry') {
122 $prov = $dom->createElementNS(MagicEnvelope::NS, 'me:provenance');
123 $prov->setAttribute('xmlns:me', MagicEnvelope::NS);
124 $data = $dom->createElementNS(MagicEnvelope::NS, 'me:data', $env['data']);
125 $data->setAttribute('type', $env['data_type']);
126 $prov->appendChild($data);
127 $enc = $dom->createElementNS(MagicEnvelope::NS, 'me:encoding', $env['encoding']);
128 $prov->appendChild($enc);
129 $alg = $dom->createElementNS(MagicEnvelope::NS, 'me:alg', $env['alg']);
130 $prov->appendChild($alg);
131 $sig = $dom->createElementNS(MagicEnvelope::NS, 'me:sig', $env['sig']);
132 $prov->appendChild($sig);
134 $dom->documentElement->appendChild($prov);
136 return $dom->saveXML();
139 public function getAuthor($text) {
140 $doc = new DOMDocument();
141 if (!$doc->loadXML($text)) {
145 if ($doc->documentElement->tagName == 'entry') {
146 $authors = $doc->documentElement->getElementsByTagName('author');
147 foreach ($authors as $author) {
148 $uris = $author->getElementsByTagName('uri');
149 foreach ($uris as $uri) {
150 return $this->normalizeUser($uri->nodeValue);
156 public function checkAuthor($text, $signer_uri)
158 return ($this->getAuthor($text) == $signer_uri);
161 public function verify($env)
163 if ($env['alg'] != 'RSA-SHA256') {
164 common_log(LOG_DEBUG, "Salmon error: bad algorithm");
168 if ($env['encoding'] != MagicEnvelope::ENCODING) {
169 common_log(LOG_DEBUG, "Salmon error: bad encoding");
173 $text = Magicsig::base64_url_decode($env['data']);
174 $signer_uri = $this->getAuthor($text);
177 $keypair = $this->getKeyPair($signer_uri);
178 } catch (Exception $e) {
179 common_log(LOG_DEBUG, "Salmon error: ".$e->getMessage());
183 $verifier = Magicsig::fromString($keypair);
186 common_log(LOG_DEBUG, "Salmon error: unable to parse keypair");
190 return $verifier->verify($env['data'], $env['sig']);
193 public function parse($text)
195 $dom = DOMDocument::loadXML($text);
196 return $this->fromDom($dom);
199 public function fromDom($dom)
201 $env_element = $dom->getElementsByTagNameNS(MagicEnvelope::NS, 'env')->item(0);
203 $env_element = $dom->getElementsByTagNameNS(MagicEnvelope::NS, 'provenance')->item(0);
210 $data_element = $env_element->getElementsByTagNameNS(MagicEnvelope::NS, 'data')->item(0);
211 $sig_element = $env_element->getElementsByTagNameNS(MagicEnvelope::NS, 'sig')->item(0);
213 'data' => preg_replace('/\s/', '', $data_element->nodeValue),
214 'data_type' => $data_element->getAttribute('type'),
215 'encoding' => $env_element->getElementsByTagNameNS(MagicEnvelope::NS, 'encoding')->item(0)->nodeValue,
216 'alg' => $env_element->getElementsByTagNameNS(MagicEnvelope::NS, 'alg')->item(0)->nodeValue,
217 'sig' => preg_replace('/\s/', '', $sig_element->nodeValue),