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 3... Line 3...
3
/**
3
/**
4
 * Base Class for all asymmetric key ciphers
4
 * Base Class for all asymmetric key ciphers
5
 *
5
 *
6
 * PHP version 5
6
 * PHP version 5
7
 *
7
 *
-
 
8
 * @category  Crypt
-
 
9
 * @package   AsymmetricKey
8
 * @author    Jim Wigginton <terrafrost@php.net>
10
 * @author    Jim Wigginton <terrafrost@php.net>
9
 * @copyright 2016 Jim Wigginton
11
 * @copyright 2016 Jim Wigginton
10
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
12
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
11
 * @link      http://phpseclib.sourceforge.net
13
 * @link      http://phpseclib.sourceforge.net
12
 */
14
 */
Line 21... Line 23...
21
use phpseclib3\Math\BigInteger;
23
use phpseclib3\Math\BigInteger;
22
 
24
 
23
/**
25
/**
24
 * Base Class for all asymmetric cipher classes
26
 * Base Class for all asymmetric cipher classes
25
 *
27
 *
-
 
28
 * @package AsymmetricKey
26
 * @author  Jim Wigginton <terrafrost@php.net>
29
 * @author  Jim Wigginton <terrafrost@php.net>
27
 */
30
 */
28
abstract class AsymmetricKey
31
abstract class AsymmetricKey
29
{
32
{
30
    /**
33
    /**
31
     * Precomputed Zero
34
     * Precomputed Zero
32
     *
35
     *
33
     * @var \phpseclib3\Math\BigInteger
36
     * @var \phpseclib3\Math\BigInteger
-
 
37
     * @access private
34
     */
38
     */
35
    protected static $zero;
39
    protected static $zero;
36
 
40
 
37
    /**
41
    /**
38
     * Precomputed One
42
     * Precomputed One
39
     *
43
     *
40
     * @var \phpseclib3\Math\BigInteger
44
     * @var \phpseclib3\Math\BigInteger
-
 
45
     * @access private
41
     */
46
     */
42
    protected static $one;
47
    protected static $one;
43
 
48
 
44
    /**
49
    /**
45
     * Format of the loaded key
50
     * Format of the loaded key
46
     *
51
     *
47
     * @var string
52
     * @var string
-
 
53
     * @access private
48
     */
54
     */
49
    protected $format;
55
    protected $format;
50
 
56
 
51
    /**
57
    /**
52
     * Hash function
58
     * Hash function
53
     *
59
     *
54
     * @var \phpseclib3\Crypt\Hash
60
     * @var \phpseclib3\Crypt\Hash
-
 
61
     * @access private
55
     */
62
     */
56
    protected $hash;
63
    protected $hash;
57
 
64
 
58
    /**
65
    /**
59
     * HMAC function
66
     * HMAC function
60
     *
67
     *
61
     * @var \phpseclib3\Crypt\Hash
68
     * @var \phpseclib3\Crypt\Hash
-
 
69
     * @access private
62
     */
70
     */
63
    private $hmac;
71
    private $hmac;
64
 
72
 
65
    /**
73
    /**
66
     * Supported plugins (lower case)
74
     * Supported plugins (lower case)
67
     *
75
     *
68
     * @see self::initialize_static_variables()
76
     * @see self::initialize_static_variables()
69
     * @var array
77
     * @var array
-
 
78
     * @access private
70
     */
79
     */
71
    private static $plugins = [];
80
    private static $plugins = [];
72
 
81
 
73
    /**
82
    /**
74
     * Invisible plugins
83
     * Invisible plugins
75
     *
84
     *
76
     * @see self::initialize_static_variables()
85
     * @see self::initialize_static_variables()
77
     * @var array
86
     * @var array
-
 
87
     * @access private
78
     */
88
     */
79
    private static $invisiblePlugins = [];
89
    private static $invisiblePlugins = [];
80
 
90
 
81
    /**
91
    /**
82
     * Supported signature formats (lower case)
92
     * Supported signature formats (lower case)
83
     *
93
     *
84
     * @see self::initialize_static_variables()
94
     * @see self::initialize_static_variables()
85
     * @var array
95
     * @var array
-
 
96
     * @access private
86
     */
97
     */
87
    private static $signatureFormats = [];
98
    private static $signatureFormats = [];
88
 
99
 
89
    /**
100
    /**
90
     * Supported signature formats (original case)
101
     * Supported signature formats (original case)
91
     *
102
     *
92
     * @see self::initialize_static_variables()
103
     * @see self::initialize_static_variables()
93
     * @var array
104
     * @var array
-
 
105
     * @access private
94
     */
106
     */
95
    private static $signatureFileFormats = [];
107
    private static $signatureFileFormats = [];
96
 
108
 
97
    /**
109
    /**
98
     * Available Engines
110
     * Available Engines
99
     *
111
     *
100
     * @var boolean[]
112
     * @var boolean[]
-
 
113
     * @access private
101
     */
114
     */
102
    protected static $engines = [];
115
    protected static $engines = [];
103
 
116
 
104
    /**
117
    /**
105
     * Key Comment
118
     * Key Comment
106
     *
119
     *
107
     * @var null|string
120
     * @var null|string
-
 
121
     * @access private
108
     */
122
     */
109
    private $comment;
123
    private $comment;
110
 
124
 
111
    /**
125
    /**
112
     * @param string $type
126
     * @param string $type
Line 183... Line 197...
183
 
197
 
184
    /**
198
    /**
185
     * Loads a private key
199
     * Loads a private key
186
     *
200
     *
187
     * @return PrivateKey
201
     * @return PrivateKey
-
 
202
     * @access public
188
     * @param string|array $key
203
     * @param string|array $key
189
     * @param string $password optional
204
     * @param string $password optional
190
     */
205
     */
191
    public static function loadPrivateKey($key, $password = '')
206
    public static function loadPrivateKey($key, $password = '')
192
    {
207
    {
Line 199... Line 214...
199
 
214
 
200
    /**
215
    /**
201
     * Loads a public key
216
     * Loads a public key
202
     *
217
     *
203
     * @return PublicKey
218
     * @return PublicKey
-
 
219
     * @access public
204
     * @param string|array $key
220
     * @param string|array $key
205
     */
221
     */
206
    public static function loadPublicKey($key)
222
    public static function loadPublicKey($key)
207
    {
223
    {
208
        $key = self::load($key);
224
        $key = self::load($key);
Line 214... Line 230...
214
 
230
 
215
    /**
231
    /**
216
     * Loads parameters
232
     * Loads parameters
217
     *
233
     *
218
     * @return AsymmetricKey
234
     * @return AsymmetricKey
-
 
235
     * @access public
219
     * @param string|array $key
236
     * @param string|array $key
220
     */
237
     */
221
    public static function loadParameters($key)
238
    public static function loadParameters($key)
222
    {
239
    {
223
        $key = self::load($key);
240
        $key = self::load($key);
Line 261... Line 278...
261
 
278
 
262
    /**
279
    /**
263
     * Loads a private key
280
     * Loads a private key
264
     *
281
     *
265
     * @return PrivateKey
282
     * @return PrivateKey
-
 
283
     * @access public
266
     * @param string $type
284
     * @param string $type
267
     * @param string $key
285
     * @param string $key
268
     * @param string $password optional
286
     * @param string $password optional
269
     */
287
     */
270
    public static function loadPrivateKeyFormat($type, $key, $password = false)
288
    public static function loadPrivateKeyFormat($type, $key, $password = false)
Line 278... Line 296...
278
 
296
 
279
    /**
297
    /**
280
     * Loads a public key
298
     * Loads a public key
281
     *
299
     *
282
     * @return PublicKey
300
     * @return PublicKey
-
 
301
     * @access public
283
     * @param string $type
302
     * @param string $type
284
     * @param string $key
303
     * @param string $key
285
     */
304
     */
286
    public static function loadPublicKeyFormat($type, $key)
305
    public static function loadPublicKeyFormat($type, $key)
287
    {
306
    {
Line 294... Line 313...
294
 
313
 
295
    /**
314
    /**
296
     * Loads parameters
315
     * Loads parameters
297
     *
316
     *
298
     * @return AsymmetricKey
317
     * @return AsymmetricKey
-
 
318
     * @access public
299
     * @param string $type
319
     * @param string $type
300
     * @param string|array $key
320
     * @param string|array $key
301
     */
321
     */
302
    public static function loadParametersFormat($type, $key)
322
    public static function loadParametersFormat($type, $key)
303
    {
323
    {
Line 309... Line 329...
309
    }
329
    }
310
 
330
 
311
    /**
331
    /**
312
     * Validate Plugin
332
     * Validate Plugin
313
     *
333
     *
-
 
334
     * @access private
314
     * @param string $format
335
     * @param string $format
315
     * @param string $type
336
     * @param string $type
316
     * @param string $method optional
337
     * @param string $method optional
317
     * @return mixed
338
     * @return mixed
318
     */
339
     */
Line 331... Line 352...
331
    }
352
    }
332
 
353
 
333
    /**
354
    /**
334
     * Load Plugins
355
     * Load Plugins
335
     *
356
     *
-
 
357
     * @access private
336
     * @param string $format
358
     * @param string $format
337
     */
359
     */
338
    private static function loadPlugins($format)
360
    private static function loadPlugins($format)
339
    {
361
    {
340
        if (!isset(self::$plugins[static::ALGORITHM][$format])) {
362
        if (!isset(self::$plugins[static::ALGORITHM][$format])) {
Line 361... Line 383...
361
    }
383
    }
362
 
384
 
363
    /**
385
    /**
364
     * Returns a list of supported formats.
386
     * Returns a list of supported formats.
365
     *
387
     *
-
 
388
     * @access public
366
     * @return array
389
     * @return array
367
     */
390
     */
368
    public static function getSupportedKeyFormats()
391
    public static function getSupportedKeyFormats()
369
    {
392
    {
370
        self::initialize_static_variables();
393
        self::initialize_static_variables();
Line 378... Line 401...
378
     * The plugin needs to either already be loaded or be auto-loadable.
401
     * The plugin needs to either already be loaded or be auto-loadable.
379
     * Loading a plugin whose shortname overwrite an existing shortname will overwrite the old plugin.
402
     * Loading a plugin whose shortname overwrite an existing shortname will overwrite the old plugin.
380
     *
403
     *
381
     * @see self::load()
404
     * @see self::load()
382
     * @param string $fullname
405
     * @param string $fullname
-
 
406
     * @access public
383
     * @return bool
407
     * @return bool
384
     */
408
     */
385
    public static function addFileFormat($fullname)
409
    public static function addFileFormat($fullname)
386
    {
410
    {
387
        self::initialize_static_variables();
411
        self::initialize_static_variables();
Line 401... Line 425...
401
     *
425
     *
402
     * If the key that was loaded wasn't in a valid or if the key was auto-generated
426
     * If the key that was loaded wasn't in a valid or if the key was auto-generated
403
     * with RSA::createKey() then this will throw an exception.
427
     * with RSA::createKey() then this will throw an exception.
404
     *
428
     *
405
     * @see self::load()
429
     * @see self::load()
-
 
430
     * @access public
406
     * @return mixed
431
     * @return mixed
407
     */
432
     */
408
    public function getLoadedFormat()
433
    public function getLoadedFormat()
409
    {
434
    {
410
        if (empty($this->format)) {
435
        if (empty($this->format)) {
Line 418... Line 443...
418
    /**
443
    /**
419
     * Returns the key's comment
444
     * Returns the key's comment
420
     *
445
     *
421
     * Not all key formats support comments. If you want to set a comment use toString()
446
     * Not all key formats support comments. If you want to set a comment use toString()
422
     *
447
     *
-
 
448
     * @access public
423
     * @return null|string
449
     * @return null|string
424
     */
450
     */
425
    public function getComment()
451
    public function getComment()
426
    {
452
    {
427
        return $this->comment;
453
        return $this->comment;
428
    }
454
    }
429
 
455
 
430
    /**
456
    /**
431
     * Tests engine validity
457
     * Tests engine validity
432
     *
458
     *
-
 
459
     * @access public
433
     */
460
     */
434
    public static function useBestEngine()
461
    public static function useBestEngine()
435
    {
462
    {
436
        static::$engines = [
463
        static::$engines = [
437
            'PHP' => true,
464
            'PHP' => true,
Line 446... Line 473...
446
    }
473
    }
447
 
474
 
448
    /**
475
    /**
449
     * Flag to use internal engine only (useful for unit testing)
476
     * Flag to use internal engine only (useful for unit testing)
450
     *
477
     *
-
 
478
     * @access public
451
     */
479
     */
452
    public static function useInternalEngine()
480
    public static function useInternalEngine()
453
    {
481
    {
454
        static::$engines = [
482
        static::$engines = [
455
            'PHP' => true,
483
            'PHP' => true,
Line 469... Line 497...
469
    }
497
    }
470
 
498
 
471
    /**
499
    /**
472
     * Determines which hashing function should be used
500
     * Determines which hashing function should be used
473
     *
501
     *
-
 
502
     * @access public
474
     * @param string $hash
503
     * @param string $hash
475
     */
504
     */
476
    public function withHash($hash)
505
    public function withHash($hash)
477
    {
506
    {
478
        $new = clone $this;
507
        $new = clone $this;
Line 484... Line 513...
484
    }
513
    }
485
 
514
 
486
    /**
515
    /**
487
     * Returns the hash algorithm currently being used
516
     * Returns the hash algorithm currently being used
488
     *
517
     *
-
 
518
     * @access public
489
     */
519
     */
490
    public function getHash()
520
    public function getHash()
491
    {
521
    {
492
        return clone $this->hash;
522
        return clone $this->hash;
493
    }
523
    }
494
 
524
 
495
    /**
525
    /**
496
     * Compute the pseudorandom k for signature generation,
526
     * Compute the pseudorandom k for signature generation,
497
     * using the process specified for deterministic DSA.
527
     * using the process specified for deterministic DSA.
498
     *
528
     *
-
 
529
     * @access public
499
     * @param string $h1
530
     * @param string $h1
500
     * @return string
531
     * @return string
501
     */
532
     */
502
    protected function computek($h1)
533
    protected function computek($h1)
503
    {
534
    {
Line 538... Line 569...
538
    }
569
    }
539
 
570
 
540
    /**
571
    /**
541
     * Integer to Octet String
572
     * Integer to Octet String
542
     *
573
     *
-
 
574
     * @access private
543
     * @param \phpseclib3\Math\BigInteger $v
575
     * @param \phpseclib3\Math\BigInteger $v
544
     * @return string
576
     * @return string
545
     */
577
     */
546
    private function int2octets($v)
578
    private function int2octets($v)
547
    {
579
    {
Line 557... Line 589...
557
    }
589
    }
558
 
590
 
559
    /**
591
    /**
560
     * Bit String to Integer
592
     * Bit String to Integer
561
     *
593
     *
-
 
594
     * @access private
562
     * @param string $in
595
     * @param string $in
563
     * @return \phpseclib3\Math\BigInteger
596
     * @return \phpseclib3\Math\BigInteger
564
     */
597
     */
565
    protected function bits2int($in)
598
    protected function bits2int($in)
566
    {
599
    {
Line 574... Line 607...
574
    }
607
    }
575
 
608
 
576
    /**
609
    /**
577
     * Bit String to Octet String
610
     * Bit String to Octet String
578
     *
611
     *
-
 
612
     * @access private
579
     * @param string $in
613
     * @param string $in
580
     * @return string
614
     * @return string
581
     */
615
     */
582
    private function bits2octets($in)
616
    private function bits2octets($in)
583
    {
617
    {