[securemail] Update Composer dependencies
[friendica-addons.git/.git] / securemail / vendor / phpseclib / phpseclib / phpseclib / System / SSH / Agent / Identity.php
1 <?php
2 /**
3  * Pure-PHP ssh-agent client.
4  *
5  * PHP version 5
6  *
7  * @category  System
8  * @package   SSH\Agent
9  * @author    Jim Wigginton <terrafrost@php.net>
10  * @copyright 2009 Jim Wigginton
11  * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
12  * @link      http://phpseclib.sourceforge.net
13  * @internal  See http://api.libssh.org/rfc/PROTOCOL.agent
14  */
15
16 namespace phpseclib\System\SSH\Agent;
17
18 use phpseclib\System\SSH\Agent;
19
20 /**
21  * Pure-PHP ssh-agent client identity object
22  *
23  * Instantiation should only be performed by \phpseclib\System\SSH\Agent class.
24  * This could be thought of as implementing an interface that phpseclib\Crypt\RSA
25  * implements. ie. maybe a Net_SSH_Auth_PublicKey interface or something.
26  * The methods in this interface would be getPublicKey and sign since those are the
27  * methods phpseclib looks for to perform public key authentication.
28  *
29  * @package SSH\Agent
30  * @author  Jim Wigginton <terrafrost@php.net>
31  * @access  internal
32  */
33 class Identity
34 {
35     /**
36      * Key Object
37      *
38      * @var \phpseclib\Crypt\RSA
39      * @access private
40      * @see self::getPublicKey()
41      */
42     var $key;
43
44     /**
45      * Key Blob
46      *
47      * @var string
48      * @access private
49      * @see self::sign()
50      */
51     var $key_blob;
52
53     /**
54      * Socket Resource
55      *
56      * @var resource
57      * @access private
58      * @see self::sign()
59      */
60     var $fsock;
61
62     /**
63      * Default Constructor.
64      *
65      * @param resource $fsock
66      * @return \phpseclib\System\SSH\Agent\Identity
67      * @access private
68      */
69     function __construct($fsock)
70     {
71         $this->fsock = $fsock;
72     }
73
74     /**
75      * Set Public Key
76      *
77      * Called by \phpseclib\System\SSH\Agent::requestIdentities()
78      *
79      * @param \phpseclib\Crypt\RSA $key
80      * @access private
81      */
82     function setPublicKey($key)
83     {
84         $this->key = $key;
85         $this->key->setPublicKey();
86     }
87
88     /**
89      * Set Public Key
90      *
91      * Called by \phpseclib\System\SSH\Agent::requestIdentities(). The key blob could be extracted from $this->key
92      * but this saves a small amount of computation.
93      *
94      * @param string $key_blob
95      * @access private
96      */
97     function setPublicKeyBlob($key_blob)
98     {
99         $this->key_blob = $key_blob;
100     }
101
102     /**
103      * Get Public Key
104      *
105      * Wrapper for $this->key->getPublicKey()
106      *
107      * @param int $format optional
108      * @return mixed
109      * @access public
110      */
111     function getPublicKey($format = null)
112     {
113         return !isset($format) ? $this->key->getPublicKey() : $this->key->getPublicKey($format);
114     }
115
116     /**
117      * Set Signature Mode
118      *
119      * Doesn't do anything as ssh-agent doesn't let you pick and choose the signature mode. ie.
120      * ssh-agent's only supported mode is \phpseclib\Crypt\RSA::SIGNATURE_PKCS1
121      *
122      * @param int $mode
123      * @access public
124      */
125     function setSignatureMode($mode)
126     {
127     }
128
129     /**
130      * Create a signature
131      *
132      * See "2.6.2 Protocol 2 private key signature request"
133      *
134      * @param string $message
135      * @return string
136      * @access public
137      */
138     function sign($message)
139     {
140         // the last parameter (currently 0) is for flags and ssh-agent only defines one flag (for ssh-dss): SSH_AGENT_OLD_SIGNATURE
141         $packet = pack('CNa*Na*N', Agent::SSH_AGENTC_SIGN_REQUEST, strlen($this->key_blob), $this->key_blob, strlen($message), $message, 0);
142         $packet = pack('Na*', strlen($packet), $packet);
143         if (strlen($packet) != fputs($this->fsock, $packet)) {
144             user_error('Connection closed during signing');
145         }
146
147         $length = current(unpack('N', fread($this->fsock, 4)));
148         $type = ord(fread($this->fsock, 1));
149         if ($type != Agent::SSH_AGENT_SIGN_RESPONSE) {
150             user_error('Unable to retrieve signature');
151         }
152
153         $signature_blob = fread($this->fsock, $length - 1);
154         // the only other signature format defined - ssh-dss - is the same length as ssh-rsa
155         // the + 12 is for the other various SSH added length fields
156         return substr($signature_blob, strlen('ssh-rsa') + 12);
157     }
158 }