]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/classes/Magicsig.php
moving magicsig.php to classes - to add storage
[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
33 {
34
35     public $keypair;
36     
37     public function __construct($init = null)
38     {
39         if (is_null($init)) {
40             $this->generate();
41         } else {
42             $this->fromString($init);
43         }
44     }
45
46
47     public function generate($key_length = 512)
48     {
49         $keypair = new Crypt_RSA_KeyPair($key_length);
50         $params['public_key'] = $keypair->getPublicKey();
51         $params['private_key'] = $keypair->getPrivateKey();
52
53         PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
54         $this->keypair = new Crypt_RSA($params);
55         PEAR::popErrorHandling();
56     }
57
58
59     public function toString($full_pair = true)
60     {
61         $public_key = $this->keypair->_public_key;
62         $private_key = $this->keypair->_private_key;
63
64         $mod = base64_url_encode($public_key->getModulus());
65         $exp = base64_url_encode($public_key->getExponent());
66         $private_exp = '';
67         if ($full_pair && $private_key->getExponent()) {
68             $private_exp = '.' . base64_url_encode($private_key->getExponent());
69         }
70
71         return 'RSA.' . $mod . '.' . $exp . $private_exp; 
72     }
73     
74     public function fromString($text)
75     {
76         PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
77
78         // remove whitespace
79         $text = preg_replace('/\s+/', '', $text);
80
81         // parse components
82         if (!preg_match('/RSA\.([^\.]+)\.([^\.]+)(.([^\.]+))?/', $text, $matches)) {
83             return false;
84         }
85         
86         $mod = base64_url_decode($matches[1]);
87         $exp = base64_url_decode($matches[2]);
88         if ($matches[4]) {
89             $private_exp = base64_url_decode($matches[4]);
90         }
91
92         $params['public_key'] = new Crypt_RSA_KEY($mod, $exp, 'public');
93         if ($params['public_key']->isError()) {
94             $error = $params['public_key']->getLastError();
95             print $error->getMessage();
96             exit;
97         }
98         if ($private_exp) {
99             $params['private_key'] = new Crypt_RSA_KEY($mod, $private_exp, 'private');
100             if ($params['private_key']->isError()) {
101                 $error = $params['private_key']->getLastError();
102                 print $error->getMessage();
103                 exit;
104             }
105         }
106
107         $this->keypair = new Crypt_RSA($params);
108         PEAR::popErrorHandling();
109     }
110
111     public function getName()
112     {
113         return 'RSA-SHA256';
114     }
115
116     public function sign($bytes)
117     {
118         $sig = $this->keypair->createSign($bytes, null, 'sha256');
119         if ($this->keypair->isError()) {
120             $error = $this->keypair->getLastError();
121             common_log(LOG_DEBUG, 'RSA Error: '. $error->getMessage());
122         }
123
124         return $sig;
125     }
126
127     public function verify($signed_bytes, $signature)
128     {
129         $result =  $this->keypair->validateSign($signed_bytes, $signature, null, 'sha256');
130         if ($this->keypair->isError()) {
131             $error = $this->keypair->getLastError();
132             //common_log(LOG_DEBUG, 'RSA Error: '. $error->getMessage());
133             print $error->getMessage();
134         }
135         return $result;
136     }
137         
138 }
139
140 // Define a sha256 function for hashing
141 // (Crypt_RSA should really be updated to use hash() )
142 function sha256($bytes)
143 {
144     return hash('sha256', $bytes);
145 }
146
147 function base64_url_encode($input)
148 {
149     return strtr(base64_encode($input), '+/', '-_');
150 }
151
152 function base64_url_decode($input)
153 {
154     return base64_decode(strtr($input, '-_', '+/'));
155 }