Subversion Repositories oidplus

Rev

Rev 846 | Rev 1042 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 846 Rev 874
Line 1... Line 1...
1
<?php
1
<?php
2
 
2
 
3
/**
3
/**
4
 * RSA Private Key
4
 * RSA Private Key
5
 *
5
 *
-
 
6
 * @category  Crypt
-
 
7
 * @package   RSA
6
 * @author    Jim Wigginton <terrafrost@php.net>
8
 * @author    Jim Wigginton <terrafrost@php.net>
7
 * @copyright 2015 Jim Wigginton
9
 * @copyright 2015 Jim Wigginton
8
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
10
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
9
 * @link      http://phpseclib.sourceforge.net
11
 * @link      http://phpseclib.sourceforge.net
10
 */
12
 */
Line 19... Line 21...
19
use phpseclib3\Math\BigInteger;
21
use phpseclib3\Math\BigInteger;
20
 
22
 
21
/**
23
/**
22
 * Raw RSA Key Handler
24
 * Raw RSA Key Handler
23
 *
25
 *
-
 
26
 * @package RSA
24
 * @author  Jim Wigginton <terrafrost@php.net>
27
 * @author  Jim Wigginton <terrafrost@php.net>
-
 
28
 * @access  public
25
 */
29
 */
26
class PrivateKey extends RSA implements Common\PrivateKey
30
class PrivateKey extends RSA implements Common\PrivateKey
27
{
31
{
28
    use Common\Traits\PasswordProtected;
32
    use Common\Traits\PasswordProtected;
29
 
33
 
30
    /**
34
    /**
31
     * Primes for Chinese Remainder Theorem (ie. p and q)
35
     * Primes for Chinese Remainder Theorem (ie. p and q)
32
     *
36
     *
33
     * @var array
37
     * @var array
-
 
38
     * @access private
34
     */
39
     */
35
    protected $primes;
40
    protected $primes;
36
 
41
 
37
    /**
42
    /**
38
     * Exponents for Chinese Remainder Theorem (ie. dP and dQ)
43
     * Exponents for Chinese Remainder Theorem (ie. dP and dQ)
39
     *
44
     *
40
     * @var array
45
     * @var array
-
 
46
     * @access private
41
     */
47
     */
42
    protected $exponents;
48
    protected $exponents;
43
 
49
 
44
    /**
50
    /**
45
     * Coefficients for Chinese Remainder Theorem (ie. qInv)
51
     * Coefficients for Chinese Remainder Theorem (ie. qInv)
46
     *
52
     *
47
     * @var array
53
     * @var array
-
 
54
     * @access private
48
     */
55
     */
49
    protected $coefficients;
56
    protected $coefficients;
50
 
57
 
51
    /**
58
    /**
52
     * Public Exponent
59
     * Public Exponent
53
     *
60
     *
54
     * @var mixed
61
     * @var mixed
-
 
62
     * @access private
55
     */
63
     */
56
    protected $publicExponent = false;
64
    protected $publicExponent = false;
57
 
65
 
58
    /**
66
    /**
59
     * RSADP
67
     * RSADP
60
     *
68
     *
61
     * See {@link http://tools.ietf.org/html/rfc3447#section-5.1.2 RFC3447#section-5.1.2}.
69
     * See {@link http://tools.ietf.org/html/rfc3447#section-5.1.2 RFC3447#section-5.1.2}.
62
     *
70
     *
-
 
71
     * @access private
63
     * @param \phpseclib3\Math\BigInteger $c
72
     * @param \phpseclib3\Math\BigInteger $c
64
     * @return bool|\phpseclib3\Math\BigInteger
73
     * @return bool|\phpseclib3\Math\BigInteger
65
     */
74
     */
66
    private function rsadp($c)
75
    private function rsadp($c)
67
    {
76
    {
Line 74... Line 83...
74
    /**
83
    /**
75
     * RSASP1
84
     * RSASP1
76
     *
85
     *
77
     * See {@link http://tools.ietf.org/html/rfc3447#section-5.2.1 RFC3447#section-5.2.1}.
86
     * See {@link http://tools.ietf.org/html/rfc3447#section-5.2.1 RFC3447#section-5.2.1}.
78
     *
87
     *
-
 
88
     * @access private
79
     * @param \phpseclib3\Math\BigInteger $m
89
     * @param \phpseclib3\Math\BigInteger $m
80
     * @return bool|\phpseclib3\Math\BigInteger
90
     * @return bool|\phpseclib3\Math\BigInteger
81
     */
91
     */
82
    private function rsasp1($m)
92
    private function rsasp1($m)
83
    {
93
    {
Line 169... Line 179...
169
     * Performs RSA Blinding
179
     * Performs RSA Blinding
170
     *
180
     *
171
     * Protects against timing attacks by employing RSA Blinding.
181
     * Protects against timing attacks by employing RSA Blinding.
172
     * Returns $x->modPow($this->exponents[$i], $this->primes[$i])
182
     * Returns $x->modPow($this->exponents[$i], $this->primes[$i])
173
     *
183
     *
-
 
184
     * @access private
174
     * @param \phpseclib3\Math\BigInteger $x
185
     * @param \phpseclib3\Math\BigInteger $x
175
     * @param \phpseclib3\Math\BigInteger $r
186
     * @param \phpseclib3\Math\BigInteger $r
176
     * @param int $i
187
     * @param int $i
177
     * @return \phpseclib3\Math\BigInteger
188
     * @return \phpseclib3\Math\BigInteger
178
     */
189
     */
Line 192... Line 203...
192
     * EMSA-PSS-ENCODE
203
     * EMSA-PSS-ENCODE
193
     *
204
     *
194
     * See {@link http://tools.ietf.org/html/rfc3447#section-9.1.1 RFC3447#section-9.1.1}.
205
     * See {@link http://tools.ietf.org/html/rfc3447#section-9.1.1 RFC3447#section-9.1.1}.
195
     *
206
     *
196
     * @return string
207
     * @return string
-
 
208
     * @access private
197
     * @param string $m
209
     * @param string $m
198
     * @throws \RuntimeException on encoding error
210
     * @throws \RuntimeException on encoding error
199
     * @param int $emBits
211
     * @param int $emBits
200
     */
212
     */
201
    private function emsa_pss_encode($m, $emBits)
213
    private function emsa_pss_encode($m, $emBits)
Line 227... Line 239...
227
    /**
239
    /**
228
     * RSASSA-PSS-SIGN
240
     * RSASSA-PSS-SIGN
229
     *
241
     *
230
     * See {@link http://tools.ietf.org/html/rfc3447#section-8.1.1 RFC3447#section-8.1.1}.
242
     * See {@link http://tools.ietf.org/html/rfc3447#section-8.1.1 RFC3447#section-8.1.1}.
231
     *
243
     *
-
 
244
     * @access private
232
     * @param string $m
245
     * @param string $m
233
     * @return bool|string
246
     * @return bool|string
234
     */
247
     */
235
    private function rsassa_pss_sign($m)
248
    private function rsassa_pss_sign($m)
236
    {
249
    {
Line 252... Line 265...
252
    /**
265
    /**
253
     * RSASSA-PKCS1-V1_5-SIGN
266
     * RSASSA-PKCS1-V1_5-SIGN
254
     *
267
     *
255
     * See {@link http://tools.ietf.org/html/rfc3447#section-8.2.1 RFC3447#section-8.2.1}.
268
     * See {@link http://tools.ietf.org/html/rfc3447#section-8.2.1 RFC3447#section-8.2.1}.
256
     *
269
     *
-
 
270
     * @access private
257
     * @param string $m
271
     * @param string $m
258
     * @throws \LengthException if the RSA modulus is too short
272
     * @throws \LengthException if the RSA modulus is too short
259
     * @return bool|string
273
     * @return bool|string
260
     */
274
     */
261
    private function rsassa_pkcs1_v1_5_sign($m)
275
    private function rsassa_pkcs1_v1_5_sign($m)
Line 283... Line 297...
283
 
297
 
284
    /**
298
    /**
285
     * Create a signature
299
     * Create a signature
286
     *
300
     *
287
     * @see self::verify()
301
     * @see self::verify()
-
 
302
     * @access public
288
     * @param string $message
303
     * @param string $message
289
     * @return string
304
     * @return string
290
     */
305
     */
291
    public function sign($message)
306
    public function sign($message)
292
    {
307
    {
Line 303... Line 318...
303
    /**
318
    /**
304
     * RSAES-PKCS1-V1_5-DECRYPT
319
     * RSAES-PKCS1-V1_5-DECRYPT
305
     *
320
     *
306
     * See {@link http://tools.ietf.org/html/rfc3447#section-7.2.2 RFC3447#section-7.2.2}.
321
     * See {@link http://tools.ietf.org/html/rfc3447#section-7.2.2 RFC3447#section-7.2.2}.
307
     *
322
     *
-
 
323
     * @access private
308
     * @param string $c
324
     * @param string $c
309
     * @return bool|string
325
     * @return bool|string
310
     */
326
     */
311
    private function rsaes_pkcs1_v1_5_decrypt($c)
327
    private function rsaes_pkcs1_v1_5_decrypt($c)
312
    {
328
    {
Line 352... Line 368...
352
     *    information about the encoded message EM.  Otherwise an opponent may
368
     *    information about the encoded message EM.  Otherwise an opponent may
353
     *    be able to obtain useful information about the decryption of the
369
     *    be able to obtain useful information about the decryption of the
354
     *    ciphertext C, leading to a chosen-ciphertext attack such as the one
370
     *    ciphertext C, leading to a chosen-ciphertext attack such as the one
355
     *    observed by Manger [36].
371
     *    observed by Manger [36].
356
     *
372
     *
-
 
373
     * @access private
357
     * @param string $c
374
     * @param string $c
358
     * @return bool|string
375
     * @return bool|string
359
     */
376
     */
360
    private function rsaes_oaep_decrypt($c)
377
    private function rsaes_oaep_decrypt($c)
361
    {
378
    {
Line 410... Line 427...
410
    /**
427
    /**
411
     * Raw Encryption / Decryption
428
     * Raw Encryption / Decryption
412
     *
429
     *
413
     * Doesn't use padding and is not recommended.
430
     * Doesn't use padding and is not recommended.
414
     *
431
     *
-
 
432
     * @access private
415
     * @param string $m
433
     * @param string $m
416
     * @return bool|string
434
     * @return bool|string
417
     * @throws \LengthException if strlen($m) > $this->k
435
     * @throws \LengthException if strlen($m) > $this->k
418
     */
436
     */
419
    private function raw_encrypt($m)
437
    private function raw_encrypt($m)
Line 429... Line 447...
429
 
447
 
430
    /**
448
    /**
431
     * Decryption
449
     * Decryption
432
     *
450
     *
433
     * @see self::encrypt()
451
     * @see self::encrypt()
-
 
452
     * @access public
434
     * @param string $ciphertext
453
     * @param string $ciphertext
435
     * @return bool|string
454
     * @return bool|string
436
     */
455
     */
437
    public function decrypt($ciphertext)
456
    public function decrypt($ciphertext)
438
    {
457
    {
Line 448... Line 467...
448
    }
467
    }
449
 
468
 
450
    /**
469
    /**
451
     * Returns the public key
470
     * Returns the public key
452
     *
471
     *
-
 
472
     * @access public
453
     * @return mixed
473
     * @return mixed
454
     */
474
     */
455
    public function getPublicKey()
475
    public function getPublicKey()
456
    {
476
    {
457
        $type = self::validatePlugin('Keys', 'PKCS8', 'savePublicKey');
477
        $type = self::validatePlugin('Keys', 'PKCS8', 'savePublicKey');