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.'));
87 * @param <type> $mimetype
88 * @param <type> $keypair
89 * @return array: associative array of envelope properties
90 * @fixme it might be easier to work with storing envelope data these in the object instead of passing arrays around
92 public function signMessage($text, $mimetype, $keypair)
94 $signature_alg = Magicsig::fromString($keypair);
95 $armored_text = Magicsig::base64_url_encode($text);
98 'data' => $armored_text,
99 'encoding' => MagicEnvelope::ENCODING,
100 'data_type' => $mimetype,
101 'sig' => $signature_alg->sign($armored_text),
102 'alg' => $signature_alg->getName()
107 * Create an <me:env> XML representation of the envelope.
109 * @param array $env associative array with envelope data
110 * @return string representation of XML document
111 * @fixme it might be easier to work with storing envelope data these in the object instead of passing arrays around
113 public function toXML($env) {
114 $xs = new XMLStringer();
116 $xs->elementStart('me:env', array('xmlns:me' => MagicEnvelope::NS));
117 $xs->element('me:data', array('type' => $env['data_type']), $env['data']);
118 $xs->element('me:encoding', null, $env['encoding']);
119 $xs->element('me:alg', null, $env['alg']);
120 $xs->element('me:sig', null, $env['sig']);
121 $xs->elementEnd('me:env');
123 $string = $xs->getString();
124 common_debug($string);
129 * Extract the contained XML payload, and insert a copy of the envelope
130 * signature data as an <me:provenance> section.
132 * @param array $env associative array with envelope data
133 * @return string representation of modified XML document
135 * @fixme in case of XML parsing errors, this will spew to the error log or output
136 * @fixme it might be easier to work with storing envelope data these in the object instead of passing arrays around
138 public function unfold($env)
140 $dom = new DOMDocument();
141 $dom->loadXML(Magicsig::base64_url_decode($env['data']));
143 if ($dom->documentElement->tagName != 'entry') {
147 $prov = $dom->createElementNS(MagicEnvelope::NS, 'me:provenance');
148 $prov->setAttribute('xmlns:me', MagicEnvelope::NS);
149 $data = $dom->createElementNS(MagicEnvelope::NS, 'me:data', $env['data']);
150 $data->setAttribute('type', $env['data_type']);
151 $prov->appendChild($data);
152 $enc = $dom->createElementNS(MagicEnvelope::NS, 'me:encoding', $env['encoding']);
153 $prov->appendChild($enc);
154 $alg = $dom->createElementNS(MagicEnvelope::NS, 'me:alg', $env['alg']);
155 $prov->appendChild($alg);
156 $sig = $dom->createElementNS(MagicEnvelope::NS, 'me:sig', $env['sig']);
157 $prov->appendChild($sig);
159 $dom->documentElement->appendChild($prov);
161 return $dom->saveXML();
165 * Find the author URI referenced in the given Atom entry.
167 * @param string $text string containing Atom entry XML
168 * @return mixed URI string or false if XML parsing fails, or null if no author URI can be found
170 * @fixme XML parsing failures will spew to error logs/output
172 public function getAuthor($text) {
173 $doc = new DOMDocument();
174 if (!$doc->loadXML($text)) {
178 if ($doc->documentElement->tagName == 'entry') {
179 $authors = $doc->documentElement->getElementsByTagName('author');
180 foreach ($authors as $author) {
181 $uris = $author->getElementsByTagName('uri');
182 foreach ($uris as $uri) {
183 return $this->normalizeUser($uri->nodeValue);
190 * Check if the author in the Atom entry fragment claims to match
191 * the given identifier URI.
193 * @param string $text string containing Atom entry XML
194 * @param string $signer_uri
197 public function checkAuthor($text, $signer_uri)
199 return ($this->getAuthor($text) == $signer_uri);
203 * Attempt to verify cryptographic signing for parsed envelope data.
204 * Requires network access to retrieve public key referenced by the envelope signer.
206 * Details of failure conditions are dumped to output log and not exposed to caller.
208 * @param array $env array representation of magic envelope data, as returned from MagicEnvelope::parse()
211 * @fixme it might be easier to work with storing envelope data these in the object instead of passing arrays around
213 public function verify($env)
215 if ($env['alg'] != 'RSA-SHA256') {
216 common_log(LOG_DEBUG, "Salmon error: bad algorithm");
220 if ($env['encoding'] != MagicEnvelope::ENCODING) {
221 common_log(LOG_DEBUG, "Salmon error: bad encoding");
225 $text = Magicsig::base64_url_decode($env['data']);
226 $signer_uri = $this->getAuthor($text);
229 $keypair = $this->getKeyPair($signer_uri);
230 } catch (Exception $e) {
231 common_log(LOG_DEBUG, "Salmon error: ".$e->getMessage());
235 $verifier = Magicsig::fromString($keypair);
238 common_log(LOG_DEBUG, "Salmon error: unable to parse keypair");
242 return $verifier->verify($env['data'], $env['sig']);
246 * Extract envelope data from an XML document containing an <me:env> or <me:provenance> element.
248 * @param string XML source
249 * @return mixed associative array of envelope data, or false on unrecognized input
251 * @fixme it might be easier to work with storing envelope data these in the object instead of passing arrays around
252 * @fixme will spew errors to logs or output in case of XML parse errors
253 * @fixme may give fatal errors if some elements are missing or invalid XML
254 * @fixme calling DOMDocument::loadXML statically triggers warnings in strict mode
256 public function parse($text)
258 $dom = DOMDocument::loadXML($text);
259 return $this->fromDom($dom);
263 * Extract envelope data from an XML document containing an <me:env> or <me:provenance> element.
265 * @param DOMDocument $dom
266 * @return mixed associative array of envelope data, or false on unrecognized input
268 * @fixme it might be easier to work with storing envelope data these in the object instead of passing arrays around
269 * @fixme may give fatal errors if some elements are missing
271 public function fromDom($dom)
273 $env_element = $dom->getElementsByTagNameNS(MagicEnvelope::NS, 'env')->item(0);
275 $env_element = $dom->getElementsByTagNameNS(MagicEnvelope::NS, 'provenance')->item(0);
282 $data_element = $env_element->getElementsByTagNameNS(MagicEnvelope::NS, 'data')->item(0);
283 $sig_element = $env_element->getElementsByTagNameNS(MagicEnvelope::NS, 'sig')->item(0);
285 'data' => preg_replace('/\s/', '', $data_element->nodeValue),
286 'data_type' => $data_element->getAttribute('type'),
287 'encoding' => $env_element->getElementsByTagNameNS(MagicEnvelope::NS, 'encoding')->item(0)->nodeValue,
288 'alg' => $env_element->getElementsByTagNameNS(MagicEnvelope::NS, 'alg')->item(0)->nodeValue,
289 'sig' => preg_replace('/\s/', '', $sig_element->nodeValue),