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 15... Line 15...
15
 *
15
 *
16
 * Analogous to ssh-keygen's pkcs8 format (as specified by -m). Although PKCS8
16
 * Analogous to ssh-keygen's pkcs8 format (as specified by -m). Although PKCS8
17
 * is specific to private keys it's basically creating a DER-encoded wrapper
17
 * is specific to private keys it's basically creating a DER-encoded wrapper
18
 * for keys. This just extends that same concept to public keys (much like ssh-keygen)
18
 * for keys. This just extends that same concept to public keys (much like ssh-keygen)
19
 *
19
 *
-
 
20
 * @category  Crypt
-
 
21
 * @package   Common
20
 * @author    Jim Wigginton <terrafrost@php.net>
22
 * @author    Jim Wigginton <terrafrost@php.net>
21
 * @copyright 2015 Jim Wigginton
23
 * @copyright 2015 Jim Wigginton
22
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
24
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
23
 * @link      http://phpseclib.sourceforge.net
25
 * @link      http://phpseclib.sourceforge.net
24
 */
26
 */
Line 39... Line 41...
39
use phpseclib3\File\ASN1\Maps;
41
use phpseclib3\File\ASN1\Maps;
40
 
42
 
41
/**
43
/**
42
 * PKCS#8 Formatted Key Handler
44
 * PKCS#8 Formatted Key Handler
43
 *
45
 *
-
 
46
 * @package Common
44
 * @author  Jim Wigginton <terrafrost@php.net>
47
 * @author  Jim Wigginton <terrafrost@php.net>
-
 
48
 * @access  public
45
 */
49
 */
46
abstract class PKCS8 extends PKCS
50
abstract class PKCS8 extends PKCS
47
{
51
{
48
    /**
52
    /**
49
     * Default encryption algorithm
53
     * Default encryption algorithm
50
     *
54
     *
51
     * @var string
55
     * @var string
-
 
56
     * @access private
52
     */
57
     */
53
    private static $defaultEncryptionAlgorithm = 'id-PBES2';
58
    private static $defaultEncryptionAlgorithm = 'id-PBES2';
54
 
59
 
55
    /**
60
    /**
56
     * Default encryption scheme
61
     * Default encryption scheme
57
     *
62
     *
58
     * Only used when defaultEncryptionAlgorithm is id-PBES2
63
     * Only used when defaultEncryptionAlgorithm is id-PBES2
59
     *
64
     *
60
     * @var string
65
     * @var string
-
 
66
     * @access private
61
     */
67
     */
62
    private static $defaultEncryptionScheme = 'aes128-CBC-PAD';
68
    private static $defaultEncryptionScheme = 'aes128-CBC-PAD';
63
 
69
 
64
    /**
70
    /**
65
     * Default PRF
71
     * Default PRF
66
     *
72
     *
67
     * Only used when defaultEncryptionAlgorithm is id-PBES2
73
     * Only used when defaultEncryptionAlgorithm is id-PBES2
68
     *
74
     *
69
     * @var string
75
     * @var string
-
 
76
     * @access private
70
     */
77
     */
71
    private static $defaultPRF = 'id-hmacWithSHA256';
78
    private static $defaultPRF = 'id-hmacWithSHA256';
72
 
79
 
73
    /**
80
    /**
74
     * Default Iteration Count
81
     * Default Iteration Count
75
     *
82
     *
76
     * @var int
83
     * @var int
-
 
84
     * @access private
77
     */
85
     */
78
    private static $defaultIterationCount = 2048;
86
    private static $defaultIterationCount = 2048;
79
 
87
 
80
    /**
88
    /**
81
     * OIDs loaded
89
     * OIDs loaded
82
     *
90
     *
83
     * @var bool
91
     * @var bool
-
 
92
     * @access private
84
     */
93
     */
85
    private static $oidsLoaded = false;
94
    private static $oidsLoaded = false;
86
 
95
 
87
    /**
96
    /**
88
     * Sets the default encryption algorithm
97
     * Sets the default encryption algorithm
89
     *
98
     *
-
 
99
     * @access public
90
     * @param string $algo
100
     * @param string $algo
91
     */
101
     */
92
    public static function setEncryptionAlgorithm($algo)
102
    public static function setEncryptionAlgorithm($algo)
93
    {
103
    {
94
        self::$defaultEncryptionAlgorithm = $algo;
104
        self::$defaultEncryptionAlgorithm = $algo;
95
    }
105
    }
96
 
106
 
97
    /**
107
    /**
98
     * Sets the default encryption algorithm for PBES2
108
     * Sets the default encryption algorithm for PBES2
99
     *
109
     *
-
 
110
     * @access public
100
     * @param string $algo
111
     * @param string $algo
101
     */
112
     */
102
    public static function setEncryptionScheme($algo)
113
    public static function setEncryptionScheme($algo)
103
    {
114
    {
104
        self::$defaultEncryptionScheme = $algo;
115
        self::$defaultEncryptionScheme = $algo;
105
    }
116
    }
106
 
117
 
107
    /**
118
    /**
108
     * Sets the iteration count
119
     * Sets the iteration count
109
     *
120
     *
-
 
121
     * @access public
110
     * @param int $count
122
     * @param int $count
111
     */
123
     */
112
    public static function setIterationCount($count)
124
    public static function setIterationCount($count)
113
    {
125
    {
114
        self::$defaultIterationCount = $count;
126
        self::$defaultIterationCount = $count;
115
    }
127
    }
116
 
128
 
117
    /**
129
    /**
118
     * Sets the PRF for PBES2
130
     * Sets the PRF for PBES2
119
     *
131
     *
-
 
132
     * @access public
120
     * @param string $algo
133
     * @param string $algo
121
     */
134
     */
122
    public static function setPRF($algo)
135
    public static function setPRF($algo)
123
    {
136
    {
124
        self::$defaultPRF = $algo;
137
        self::$defaultPRF = $algo;
Line 126... Line 139...
126
 
139
 
127
    /**
140
    /**
128
     * Returns a SymmetricKey object based on a PBES1 $algo
141
     * Returns a SymmetricKey object based on a PBES1 $algo
129
     *
142
     *
130
     * @return \phpseclib3\Crypt\Common\SymmetricKey
143
     * @return \phpseclib3\Crypt\Common\SymmetricKey
-
 
144
     * @access public
131
     * @param string $algo
145
     * @param string $algo
132
     */
146
     */
133
    private static function getPBES1EncryptionObject($algo)
147
    private static function getPBES1EncryptionObject($algo)
134
    {
148
    {
135
        $algo = preg_match('#^pbeWith(?:MD2|MD5|SHA1|SHA)And(.*?)-CBC$#', $algo, $matches) ?
149
        $algo = preg_match('#^pbeWith(?:MD2|MD5|SHA1|SHA)And(.*?)-CBC$#', $algo, $matches) ?
Line 175... Line 189...
175
 
189
 
176
    /**
190
    /**
177
     * Returns a hash based on a PBES1 $algo
191
     * Returns a hash based on a PBES1 $algo
178
     *
192
     *
179
     * @return string
193
     * @return string
-
 
194
     * @access public
180
     * @param string $algo
195
     * @param string $algo
181
     */
196
     */
182
    private static function getPBES1Hash($algo)
197
    private static function getPBES1Hash($algo)
183
    {
198
    {
184
        if (preg_match('#^pbeWith(MD2|MD5|SHA1|SHA)And.*?-CBC$#', $algo, $matches)) {
199
        if (preg_match('#^pbeWith(MD2|MD5|SHA1|SHA)And.*?-CBC$#', $algo, $matches)) {
Line 190... Line 205...
190
 
205
 
191
    /**
206
    /**
192
     * Returns a KDF baesd on a PBES1 $algo
207
     * Returns a KDF baesd on a PBES1 $algo
193
     *
208
     *
194
     * @return string
209
     * @return string
-
 
210
     * @access public
195
     * @param string $algo
211
     * @param string $algo
196
     */
212
     */
197
    private static function getPBES1KDF($algo)
213
    private static function getPBES1KDF($algo)
198
    {
214
    {
199
        switch ($algo) {
215
        switch ($algo) {
Line 211... Line 227...
211
 
227
 
212
    /**
228
    /**
213
     * Returns a SymmetricKey object baesd on a PBES2 $algo
229
     * Returns a SymmetricKey object baesd on a PBES2 $algo
214
     *
230
     *
215
     * @return SymmetricKey
231
     * @return SymmetricKey
-
 
232
     * @access public
216
     * @param string $algo
233
     * @param string $algo
217
     */
234
     */
218
    private static function getPBES2EncryptionObject($algo)
235
    private static function getPBES2EncryptionObject($algo)
219
    {
236
    {
220
        switch ($algo) {
237
        switch ($algo) {
Line 245... Line 262...
245
    }
262
    }
246
 
263
 
247
    /**
264
    /**
248
     * Initialize static variables
265
     * Initialize static variables
249
     *
266
     *
-
 
267
     * @access private
250
     */
268
     */
251
    private static function initialize_static_variables()
269
    private static function initialize_static_variables()
252
    {
270
    {
253
        if (!isset(static::$childOIDsLoaded)) {
271
        if (!isset(static::$childOIDsLoaded)) {
254
            throw new InsufficientSetupException('This class should not be called directly');
272
            throw new InsufficientSetupException('This class should not be called directly');
Line 308... Line 326...
308
    }
326
    }
309
 
327
 
310
    /**
328
    /**
311
     * Break a public or private key down into its constituent components
329
     * Break a public or private key down into its constituent components
312
     *
330
     *
-
 
331
     * @access public
313
     * @param string $key
332
     * @param string $key
314
     * @param string $password optional
333
     * @param string $password optional
315
     * @return array
334
     * @return array
316
     */
335
     */
317
    protected static function load($key, $password = '')
336
    protected static function load($key, $password = '')
Line 484... Line 503...
484
    }
503
    }
485
 
504
 
486
    /**
505
    /**
487
     * Wrap a private key appropriately
506
     * Wrap a private key appropriately
488
     *
507
     *
-
 
508
     * @access public
489
     * @param string $key
509
     * @param string $key
490
     * @param string $attr
510
     * @param string $attr
491
     * @param mixed $params
511
     * @param mixed $params
492
     * @param string $password
512
     * @param string $password
493
     * @param string $oid optional
513
     * @param string $oid optional
Line 597... Line 617...
597
    }
617
    }
598
 
618
 
599
    /**
619
    /**
600
     * Wrap a public key appropriately
620
     * Wrap a public key appropriately
601
     *
621
     *
-
 
622
     * @access public
602
     * @param string $key
623
     * @param string $key
603
     * @param mixed $params
624
     * @param mixed $params
604
     * @param string $oid
625
     * @param string $oid
605
     * @return string
626
     * @return string
606
     */
627
     */