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
34 const PUBLICKEYREL = 'magic-public-key';
36 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 $obj = Magicsig::fromString($obj->keypair);
56 // Double check keys: Crypt_RSA did not
57 // consistently generate good keypairs.
58 // We've also moved to 1024 bit keys.
59 if (strlen($obj->publicKey->modulus->toBits()) != 1024) {
72 'user_id' => DB_DATAOBJECT_INT,
73 'keypair' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
74 'alg' => DB_DATAOBJECT_STR
78 static function schemaDef()
80 return array(new ColumnDef('user_id', 'integer',
82 new ColumnDef('keypair', 'text',
84 new ColumnDef('alg', 'varchar',
90 return array_keys($this->keyTypes());
95 return array('user_id' => 'K');
98 function sequenceKey() {
99 return array(false, false, false);
104 $this->keypair = $this->toString();
106 return parent::insert();
109 public function generate($user_id)
111 $rsa = new Crypt_RSA();
113 $keypair = $rsa->createKey();
115 $rsa->loadKey($keypair['privatekey']);
117 $this->privateKey = new Crypt_RSA();
118 $this->privateKey->loadKey($keypair['privatekey']);
120 $this->publicKey = new Crypt_RSA();
121 $this->publicKey->loadKey($keypair['publickey']);
123 $this->user_id = $user_id;
127 public function toString($full_pair = true)
129 $mod = Magicsig::base64_url_encode($this->publicKey->modulus->toBytes());
130 $exp = Magicsig::base64_url_encode($this->publicKey->exponent->toBytes());
132 if ($full_pair && $this->privateKey->exponent->toBytes()) {
133 $private_exp = '.' . Magicsig::base64_url_encode($this->privateKey->exponent->toBytes());
136 return 'RSA.' . $mod . '.' . $exp . $private_exp;
139 public static function fromString($text)
141 $magic_sig = new Magicsig();
144 $text = preg_replace('/\s+/', '', $text);
147 if (!preg_match('/RSA\.([^\.]+)\.([^\.]+)(.([^\.]+))?/', $text, $matches)) {
153 if (!empty($matches[4])) {
154 $private_exp = $matches[4];
156 $private_exp = false;
159 $magic_sig->loadKey($mod, $exp, 'public');
161 $magic_sig->loadKey($mod, $private_exp, 'private');
167 public function loadKey($mod, $exp, $type = 'public')
169 common_log(LOG_DEBUG, "Adding ".$type." key: (".$mod .', '. $exp .")");
171 $rsa = new Crypt_RSA();
172 $rsa->signatureMode = CRYPT_RSA_SIGNATURE_PKCS1;
173 $rsa->setHash('sha256');
174 $rsa->modulus = new Math_BigInteger(Magicsig::base64_url_decode($mod), 256);
175 $rsa->k = strlen($rsa->modulus->toBytes());
176 $rsa->exponent = new Math_BigInteger(Magicsig::base64_url_decode($exp), 256);
178 if ($type == 'private') {
179 $this->privateKey = $rsa;
181 $this->publicKey = $rsa;
185 public function getName()
190 public function getHash()
192 switch ($this->alg) {
199 public function sign($bytes)
201 $sig = $this->privateKey->sign($bytes);
202 return Magicsig::base64_url_encode($sig);
205 public function verify($signed_bytes, $signature)
207 $signature = Magicsig::base64_url_decode($signature);
208 return $this->publicKey->verify($signed_bytes, $signature);
212 public static function base64_url_encode($input)
214 return strtr(base64_encode($input), '+/', '-_');
217 public static function base64_url_decode($input)
219 return base64_decode(strtr($input, '-_', '+/'));