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/
30 require_once 'Crypt/RSA.php';
32 class Magicsig extends Memcached_DataObject
35 const PUBLICKEYREL = 'magic-public-key';
37 public $__table = 'magicsig';
45 public function __construct($alg = 'RSA-SHA256')
50 public /*static*/ function staticGet($k, $v=null)
52 $obj = parent::staticGet(__CLASS__, $k, $v);
54 return Magicsig::fromString($obj->keypair);
63 'user_id' => DB_DATAOBJECT_INT,
64 'keypair' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
65 'alg' => DB_DATAOBJECT_STR
69 static function schemaDef()
71 return array(new ColumnDef('user_id', 'integer',
73 new ColumnDef('keypair', 'varchar',
75 new ColumnDef('alg', 'varchar',
81 return array_keys($this->keyTypes());
86 return array('user_id' => 'K');
89 function sequenceKey() {
90 return array(false, false, false);
95 $this->keypair = $this->toString();
97 return parent::insert();
100 public function generate($user_id, $key_length = 512)
102 PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
104 $keypair = new Crypt_RSA_KeyPair($key_length);
105 $params['public_key'] = $keypair->getPublicKey();
106 $params['private_key'] = $keypair->getPrivateKey();
108 $this->_rsa = new Crypt_RSA($params);
109 PEAR::popErrorHandling();
111 $this->user_id = $user_id;
115 public function toString($full_pair = true)
117 $public_key = $this->_rsa->_public_key;
118 $private_key = $this->_rsa->_private_key;
120 $mod = base64_url_encode($public_key->getModulus());
121 $exp = base64_url_encode($public_key->getExponent());
123 if ($full_pair && $private_key->getExponent()) {
124 $private_exp = '.' . base64_url_encode($private_key->getExponent());
127 return 'RSA.' . $mod . '.' . $exp . $private_exp;
130 public static function fromString($text)
132 PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
134 $magic_sig = new Magicsig();
137 $text = preg_replace('/\s+/', '', $text);
140 if (!preg_match('/RSA\.([^\.]+)\.([^\.]+)(.([^\.]+))?/', $text, $matches)) {
144 $mod = base64_url_decode($matches[1]);
145 $exp = base64_url_decode($matches[2]);
147 $private_exp = base64_url_decode($matches[4]);
150 $params['public_key'] = new Crypt_RSA_KEY($mod, $exp, 'public');
151 if ($params['public_key']->isError()) {
152 $error = $params['public_key']->getLastError();
153 common_log(LOG_DEBUG, 'RSA Error: '. $error->getMessage());
157 $params['private_key'] = new Crypt_RSA_KEY($mod, $private_exp, 'private');
158 if ($params['private_key']->isError()) {
159 $error = $params['private_key']->getLastError();
160 common_log(LOG_DEBUG, 'RSA Error: '. $error->getMessage());
165 $magic_sig->_rsa = new Crypt_RSA($params);
166 PEAR::popErrorHandling();
171 public function getName()
176 public function getHash()
178 switch ($this->alg) {
186 public function sign($bytes)
188 $sig = $this->_rsa->createSign($bytes, null, 'magicsig_sha256');
189 if ($this->_rsa->isError()) {
190 $error = $this->_rsa->getLastError();
191 common_log(LOG_DEBUG, 'RSA Error: '. $error->getMessage());
198 public function verify($signed_bytes, $signature)
200 $result = $this->_rsa->validateSign($signed_bytes, $signature, null, 'magicsig_sha256');
201 if ($this->_rsa->isError()) {
202 $error = $this->keypair->getLastError();
203 common_log(LOG_DEBUG, 'RSA Error: '. $error->getMessage());
211 // Define a sha256 function for hashing
212 // (Crypt_RSA should really be updated to use hash() )
213 function magicsig_sha256($bytes)
215 return hash('sha256', $bytes);
218 function base64_url_encode($input)
220 return strtr(base64_encode($input), '+/', '-_');
223 function base64_url_decode($input)
225 return base64_decode(strtr($input, '-_', '+/'));