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