phpseclib\System\SSH\Agent::requestIdentities PHP Method

requestIdentities() public method

See "2.5.2 Requesting a list of protocol 2 keys" Returns an array containing zero or more \phpseclib\System\SSH\Agent\Identity objects
public requestIdentities ( ) : array
return array
    function requestIdentities()
    {
        if (!$this->fsock) {
            return array();
        }
        $packet = pack('NC', 1, self::SSH_AGENTC_REQUEST_IDENTITIES);
        if (strlen($packet) != fputs($this->fsock, $packet)) {
            throw new \RuntimeException('Connection closed while requesting identities');
        }
        $length = current(unpack('N', fread($this->fsock, 4)));
        $type = ord(fread($this->fsock, 1));
        if ($type != self::SSH_AGENT_IDENTITIES_ANSWER) {
            throw new \RuntimeException('Unable to request identities');
        }
        $identities = array();
        $keyCount = current(unpack('N', fread($this->fsock, 4)));
        for ($i = 0; $i < $keyCount; $i++) {
            $length = current(unpack('N', fread($this->fsock, 4)));
            $key_blob = fread($this->fsock, $length);
            $key_str = 'ssh-rsa ' . Base64::encode($key_blob);
            $length = current(unpack('N', fread($this->fsock, 4)));
            if ($length) {
                $key_str .= ' ' . fread($this->fsock, $length);
            }
            $length = current(unpack('N', substr($key_blob, 0, 4)));
            $key_type = substr($key_blob, 4, $length);
            switch ($key_type) {
                case 'ssh-rsa':
                    $key = new RSA();
                    $key->load($key_str);
                    break;
                case 'ssh-dss':
                    // not currently supported
                    break;
            }
            // resources are passed by reference by default
            if (isset($key)) {
                $identity = new Identity($this->fsock);
                $identity->setPublicKey($key);
                $identity->setPublicKeyBlob($key_blob);
                $identities[] = $identity;
                unset($key);
            }
        }
        return $identities;
    }

Usage Example

Beispiel #1
0
 /**
  * Login with an ssh-agent provided key
  *
  * @param String $username
  * @param \phpseclib\System\SSH\Agent $agent
  * @return Boolean
  * @access private
  */
 function _ssh_agent_login($username, $agent)
 {
     $this->agent = $agent;
     $keys = $agent->requestIdentities();
     foreach ($keys as $key) {
         if ($this->_privatekey_login($username, $key)) {
             return true;
         }
     }
     return false;
 }