]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/classes/Magicsig.php
82b6017ded04a5e0cc7fb8f0d477a25e5a8bc842
[quix0rs-gnu-social.git] / plugins / OStatus / classes / Magicsig.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 'Crypt/RSA.php';
31
32 class Magicsig extends Memcached_DataObject
33 {
34
35     const PUBLICKEYREL = 'magic-public-key';
36     
37     public $__table = 'magicsig';
38
39     public $user_id;
40     public $keypair;
41     public $alg;
42     
43     private $publicKey;
44     private $privateKey;
45     
46     public function __construct($alg = 'RSA-SHA256')
47     {
48         $this->alg = $alg;
49     }
50     
51     public /*static*/ function staticGet($k, $v=null)
52     {
53         $obj =  parent::staticGet(__CLASS__, $k, $v);
54         if (!empty($obj)) {
55             return Magicsig::fromString($obj->keypair);
56         }
57
58         return $obj;
59     }
60
61
62     function table()
63     {
64         return array(
65             'user_id' => DB_DATAOBJECT_INT,
66             'keypair' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
67             'alg'     => DB_DATAOBJECT_STR
68         );
69     }
70
71     static function schemaDef()
72     {
73         return array(new ColumnDef('user_id', 'integer',
74                                    null, false, 'PRI'),
75                      new ColumnDef('keypair', 'varchar',
76                                    255, false),
77                      new ColumnDef('alg', 'varchar',
78                                    64, false));
79     }
80
81
82     function keys()
83     {
84         return array_keys($this->keyTypes());
85     }
86
87     function keyTypes()
88     {
89         return array('user_id' => 'K');
90     }
91
92     function sequenceKey() {
93         return array(false, false, false);
94     }
95
96     function insert()
97     {
98         $this->keypair = $this->toString();
99
100         return parent::insert();
101     }
102
103     public function generate($user_id, $key_length = 512)
104     {
105         $rsa = new Crypt_RSA();
106
107         extract($rsa->createKey());
108
109         $rsa->loadKey($privatekey);
110
111         $this->privateKey = $rsa;
112
113         $this->publicKey = new Crypt_RSA();
114         $this->publicKey->loadKey($publickey);
115         
116         $this->user_id = $user_id;
117         $this->insert();
118     }
119
120
121     public function toString($full_pair = true)
122     {
123         $mod = base64_url_encode($this->publicKey->modulus->toBytes());
124         $exp = base64_url_encode($this->publicKey->exponent->toBytes());
125         $private_exp = '';
126         if ($full_pair && $this->privateKey->exponent->toBytes()) {
127             $private_exp = '.' . base64_url_encode($this->privateKey->exponent->toBytes());
128         }
129
130         return 'RSA.' . $mod . '.' . $exp . $private_exp; 
131     }
132     
133     public static function fromString($text)
134     {
135         $magic_sig = new Magicsig();
136         
137         // remove whitespace
138         $text = preg_replace('/\s+/', '', $text);
139
140         // parse components
141         if (!preg_match('/RSA\.([^\.]+)\.([^\.]+)(.([^\.]+))?/', $text, $matches)) {
142             return false;
143         }
144         
145         $mod = $matches[1];
146         $exp = $matches[2];
147         if (!empty($matches[4])) {
148             $private_exp = $matches[4];
149         } else {
150             $private_exp = false;
151         }
152
153         $magic_sig->loadKey($mod, $exp, 'public');
154         if ($private_exp) {
155             $magic_sig->loadKey($mod, $private_exp, 'private');
156         }
157
158         return $magic_sig;
159     }
160
161     public function loadKey($mod, $exp, $type = 'public')
162     {
163         common_log(LOG_DEBUG, "Adding ".$type." key: (".$mod .', '. $exp .")");
164
165         $rsa = new Crypt_RSA();
166         $rsa->signatureMode = CRYPT_RSA_SIGNATURE_PKCS1;
167         $rsa->setHash('sha256');
168         $rsa->modulus = new Math_BigInteger(base64_url_decode($mod), 256);
169         $rsa->k = strlen($rsa->modulus->toBytes());
170         $rsa->exponent = new Math_BigInteger(base64_url_decode($exp), 256);
171
172         if ($type == 'private') {
173             $this->privateKey = $rsa;
174         } else {
175             $this->publicKey = $rsa;
176         }
177     }
178     
179     public function getName()
180     {
181         return $this->alg;
182     }
183
184     public function getHash()
185     {
186         switch ($this->alg) {
187
188         case 'RSA-SHA256':
189             return 'magicsig_sha256';
190         }
191
192     }
193     
194     public function sign($bytes)
195     {
196         $sig = $this->privateKey->sign($bytes);
197         return base64_url_encode($sig);
198     }
199
200     public function verify($signed_bytes, $signature)
201     {
202         $signature = base64_url_decode($signature);
203         return $this->publicKey->verify($signed_bytes, $signature);
204     }
205         
206 }
207
208 function base64_url_encode($input)
209 {
210     return strtr(base64_encode($input), '+/', '-_');
211 }
212
213 function base64_url_decode($input)
214 {
215     return base64_decode(strtr($input, '-_', '+/'));
216 }