示例#1
0
文件: SSH1.php 项目: huang-sh/yaf.app
 /**
  * Default Constructor.
  *
  * Connects to an SSHv1 server
  *
  * @param String $host
  * @param optional Integer $port
  * @param optional Integer $timeout
  * @param optional Integer $cipher
  * @return Net_SSH1
  * @access public
  */
 function __construct($host, $port = 22, $timeout = 10, $cipher = NET_SSH1_CIPHER_3DES)
 {
     // Include Crypt_Random
     // the class_exists() will only be called if the crypt_random_string function hasn't been defined and
     // will trigger a call to __autoload() if you're wanting to auto-load classes
     // call function_exists() a second time to stop the require_once from being called outside
     // of the auto loader
     if (!function_exists('crypt_random_string') && !class_exists('Crypt_Random') && !function_exists('crypt_random_string')) {
         require_once 'Crypt/Random.php';
     }
     $this->protocol_flags = array(1 => 'NET_SSH1_MSG_DISCONNECT', 2 => 'NET_SSH1_SMSG_PUBLIC_KEY', 3 => 'NET_SSH1_CMSG_SESSION_KEY', 4 => 'NET_SSH1_CMSG_USER', 9 => 'NET_SSH1_CMSG_AUTH_PASSWORD', 10 => 'NET_SSH1_CMSG_REQUEST_PTY', 12 => 'NET_SSH1_CMSG_EXEC_SHELL', 13 => 'NET_SSH1_CMSG_EXEC_CMD', 14 => 'NET_SSH1_SMSG_SUCCESS', 15 => 'NET_SSH1_SMSG_FAILURE', 16 => 'NET_SSH1_CMSG_STDIN_DATA', 17 => 'NET_SSH1_SMSG_STDOUT_DATA', 18 => 'NET_SSH1_SMSG_STDERR_DATA', 19 => 'NET_SSH1_CMSG_EOF', 20 => 'NET_SSH1_SMSG_EXITSTATUS', 33 => 'NET_SSH1_CMSG_EXIT_CONFIRMATION');
     $this->_define_array($this->protocol_flags);
     $this->fsock = @fsockopen($host, $port, $errno, $errstr, $timeout);
     if (!$this->fsock) {
         user_error(rtrim("Cannot connect to {$host}. Error {$errno}. {$errstr}"));
         return;
     }
     $this->server_identification = $init_line = fgets($this->fsock, 255);
     if (defined('NET_SSH1_LOGGING')) {
         $this->_append_log('<-', $this->server_identification);
         $this->_append_log('->', $this->identifier . "\r\n");
     }
     if (!preg_match('#SSH-([0-9\\.]+)-(.+)#', $init_line, $parts)) {
         user_error('Can only connect to SSH servers');
         return;
     }
     if ($parts[1][0] != 1) {
         user_error("Cannot connect to SSH {$parts['1']} servers");
         return;
     }
     fputs($this->fsock, $this->identifier . "\r\n");
     $response = $this->_get_binary_packet();
     if ($response[NET_SSH1_RESPONSE_TYPE] != NET_SSH1_SMSG_PUBLIC_KEY) {
         user_error('Expected SSH_SMSG_PUBLIC_KEY');
         return;
     }
     $anti_spoofing_cookie = $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 8);
     $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 4);
     $temp = unpack('nlen', $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 2));
     $server_key_public_exponent = new BigInteger($this->_string_shift($response[NET_SSH1_RESPONSE_DATA], ceil($temp['len'] / 8)), 256);
     $this->server_key_public_exponent = $server_key_public_exponent;
     $temp = unpack('nlen', $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 2));
     $server_key_public_modulus = new BigInteger($this->_string_shift($response[NET_SSH1_RESPONSE_DATA], ceil($temp['len'] / 8)), 256);
     $this->server_key_public_modulus = $server_key_public_modulus;
     $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 4);
     $temp = unpack('nlen', $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 2));
     $host_key_public_exponent = new BigInteger($this->_string_shift($response[NET_SSH1_RESPONSE_DATA], ceil($temp['len'] / 8)), 256);
     $this->host_key_public_exponent = $host_key_public_exponent;
     $temp = unpack('nlen', $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 2));
     $host_key_public_modulus = new BigInteger($this->_string_shift($response[NET_SSH1_RESPONSE_DATA], ceil($temp['len'] / 8)), 256);
     $this->host_key_public_modulus = $host_key_public_modulus;
     $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 4);
     // get a list of the supported ciphers
     extract(unpack('Nsupported_ciphers_mask', $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 4)));
     foreach ($this->supported_ciphers as $mask => $name) {
         if (($supported_ciphers_mask & 1 << $mask) == 0) {
             unset($this->supported_ciphers[$mask]);
         }
     }
     // get a list of the supported authentications
     extract(unpack('Nsupported_authentications_mask', $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 4)));
     foreach ($this->supported_authentications as $mask => $name) {
         if (($supported_authentications_mask & 1 << $mask) == 0) {
             unset($this->supported_authentications[$mask]);
         }
     }
     $session_id = pack('H*', md5($host_key_public_modulus->toBytes() . $server_key_public_modulus->toBytes() . $anti_spoofing_cookie));
     $session_key = crypt_random_string(32);
     $double_encrypted_session_key = $session_key ^ str_pad($session_id, 32, chr(0));
     if ($server_key_public_modulus->compare($host_key_public_modulus) < 0) {
         $double_encrypted_session_key = $this->_rsa_crypt($double_encrypted_session_key, array($server_key_public_exponent, $server_key_public_modulus));
         $double_encrypted_session_key = $this->_rsa_crypt($double_encrypted_session_key, array($host_key_public_exponent, $host_key_public_modulus));
     } else {
         $double_encrypted_session_key = $this->_rsa_crypt($double_encrypted_session_key, array($host_key_public_exponent, $host_key_public_modulus));
         $double_encrypted_session_key = $this->_rsa_crypt($double_encrypted_session_key, array($server_key_public_exponent, $server_key_public_modulus));
     }
     $cipher = isset($this->supported_ciphers[$cipher]) ? $cipher : NET_SSH1_CIPHER_3DES;
     $data = pack('C2a*na*N', NET_SSH1_CMSG_SESSION_KEY, $cipher, $anti_spoofing_cookie, 8 * strlen($double_encrypted_session_key), $double_encrypted_session_key, 0);
     if (!$this->_send_binary_packet($data)) {
         user_error('Error sending SSH_CMSG_SESSION_KEY');
         return;
     }
     switch ($cipher) {
         //case NET_SSH1_CIPHER_NONE:
         //    $this->crypto = new Crypt_Null();
         //    break;
         case NET_SSH1_CIPHER_DES:
             $this->crypto = new DES();
             $this->crypto->disablePadding();
             $this->crypto->enableContinuousBuffer();
             $this->crypto->setKey(substr($session_key, 0, 8));
             break;
         case NET_SSH1_CIPHER_3DES:
             $this->crypto = new TripleDES(CRYPT_DES_MODE_3CBC);
             $this->crypto->disablePadding();
             $this->crypto->enableContinuousBuffer();
             $this->crypto->setKey(substr($session_key, 0, 24));
             break;
             //case NET_SSH1_CIPHER_RC4:
             //    if (!class_exists('Crypt_RC4')) {
             //        require_once('Crypt/RC4.php');
             //    }
             //    $this->crypto = new Crypt_RC4();
             //    $this->crypto->enableContinuousBuffer();
             //    $this->crypto->setKey(substr($session_key, 0,  16));
             //    break;
     }
     $response = $this->_get_binary_packet();
     if ($response[NET_SSH1_RESPONSE_TYPE] != NET_SSH1_SMSG_SUCCESS) {
         user_error('Expected SSH_SMSG_SUCCESS');
         return;
     }
     $this->bitmap = NET_SSH1_MASK_CONSTRUCTOR;
 }
示例#2
0
文件: Hash.php 项目: huang-sh/yaf.app
 /**
  * Pure-PHP implementation of SHA384 and SHA512
  *
  * @access private
  * @param String $m
  */
 function _sha512($m)
 {
     if (!class_exists('Math_BigInteger')) {
         require_once 'Math/BigInteger.php';
     }
     static $init384, $init512, $k;
     if (!isset($k)) {
         // Initialize variables
         $init384 = array('cbbb9d5dc1059ed8', '629a292a367cd507', '9159015a3070dd17', '152fecd8f70e5939', '67332667ffc00b31', '8eb44a8768581511', 'db0c2e0d64f98fa7', '47b5481dbefa4fa4');
         $init512 = array('6a09e667f3bcc908', 'bb67ae8584caa73b', '3c6ef372fe94f82b', 'a54ff53a5f1d36f1', '510e527fade682d1', '9b05688c2b3e6c1f', '1f83d9abfb41bd6b', '5be0cd19137e2179');
         for ($i = 0; $i < 8; $i++) {
             $init384[$i] = new BigInteger($init384[$i], 16);
             $init384[$i]->setPrecision(64);
             $init512[$i] = new BigInteger($init512[$i], 16);
             $init512[$i]->setPrecision(64);
         }
         // Initialize table of round constants
         // (first 64 bits of the fractional parts of the cube roots of the first 80 primes 2..409)
         $k = array('428a2f98d728ae22', '7137449123ef65cd', 'b5c0fbcfec4d3b2f', 'e9b5dba58189dbbc', '3956c25bf348b538', '59f111f1b605d019', '923f82a4af194f9b', 'ab1c5ed5da6d8118', 'd807aa98a3030242', '12835b0145706fbe', '243185be4ee4b28c', '550c7dc3d5ffb4e2', '72be5d74f27b896f', '80deb1fe3b1696b1', '9bdc06a725c71235', 'c19bf174cf692694', 'e49b69c19ef14ad2', 'efbe4786384f25e3', '0fc19dc68b8cd5b5', '240ca1cc77ac9c65', '2de92c6f592b0275', '4a7484aa6ea6e483', '5cb0a9dcbd41fbd4', '76f988da831153b5', '983e5152ee66dfab', 'a831c66d2db43210', 'b00327c898fb213f', 'bf597fc7beef0ee4', 'c6e00bf33da88fc2', 'd5a79147930aa725', '06ca6351e003826f', '142929670a0e6e70', '27b70a8546d22ffc', '2e1b21385c26c926', '4d2c6dfc5ac42aed', '53380d139d95b3df', '650a73548baf63de', '766a0abb3c77b2a8', '81c2c92e47edaee6', '92722c851482353b', 'a2bfe8a14cf10364', 'a81a664bbc423001', 'c24b8b70d0f89791', 'c76c51a30654be30', 'd192e819d6ef5218', 'd69906245565a910', 'f40e35855771202a', '106aa07032bbd1b8', '19a4c116b8d2d0c8', '1e376c085141ab53', '2748774cdf8eeb99', '34b0bcb5e19b48a8', '391c0cb3c5c95a63', '4ed8aa4ae3418acb', '5b9cca4f7763e373', '682e6ff3d6b2b8a3', '748f82ee5defb2fc', '78a5636f43172f60', '84c87814a1f0ab72', '8cc702081a6439ec', '90befffa23631e28', 'a4506cebde82bde9', 'bef9a3f7b2c67915', 'c67178f2e372532b', 'ca273eceea26619c', 'd186b8c721c0c207', 'eada7dd6cde0eb1e', 'f57d4f7fee6ed178', '06f067aa72176fba', '0a637dc5a2c898a6', '113f9804bef90dae', '1b710b35131c471b', '28db77f523047d84', '32caab7b40c72493', '3c9ebe0a15c9bebc', '431d67c49c100d4c', '4cc5d4becb3e42b6', '597f299cfc657e2a', '5fcb6fab3ad6faec', '6c44198c4a475817');
         for ($i = 0; $i < 80; $i++) {
             $k[$i] = new BigInteger($k[$i], 16);
         }
     }
     $hash = $this->l == 48 ? $init384 : $init512;
     // Pre-processing
     $length = strlen($m);
     // to round to nearest 112 mod 128, we'll add 128 - (length + (128 - 112)) % 128
     $m .= str_repeat(chr(0), 128 - ($length + 16 & 0x7f));
     $m[$length] = chr(0x80);
     // we don't support hashing strings 512MB long
     $m .= pack('N4', 0, 0, 0, $length << 3);
     // Process the message in successive 1024-bit chunks
     $chunks = str_split($m, 128);
     foreach ($chunks as $chunk) {
         $w = array();
         for ($i = 0; $i < 16; $i++) {
             $temp = new BigInteger($this->_string_shift($chunk, 8), 256);
             $temp->setPrecision(64);
             $w[] = $temp;
         }
         // Extend the sixteen 32-bit words into eighty 32-bit words
         for ($i = 16; $i < 80; $i++) {
             $temp = array($w[$i - 15]->bitwise_rightRotate(1), $w[$i - 15]->bitwise_rightRotate(8), $w[$i - 15]->bitwise_rightShift(7));
             $s0 = $temp[0]->bitwise_xor($temp[1]);
             $s0 = $s0->bitwise_xor($temp[2]);
             $temp = array($w[$i - 2]->bitwise_rightRotate(19), $w[$i - 2]->bitwise_rightRotate(61), $w[$i - 2]->bitwise_rightShift(6));
             $s1 = $temp[0]->bitwise_xor($temp[1]);
             $s1 = $s1->bitwise_xor($temp[2]);
             $w[$i] = $w[$i - 16]->copy();
             $w[$i] = $w[$i]->add($s0);
             $w[$i] = $w[$i]->add($w[$i - 7]);
             $w[$i] = $w[$i]->add($s1);
         }
         // Initialize hash value for this chunk
         $a = $hash[0]->copy();
         $b = $hash[1]->copy();
         $c = $hash[2]->copy();
         $d = $hash[3]->copy();
         $e = $hash[4]->copy();
         $f = $hash[5]->copy();
         $g = $hash[6]->copy();
         $h = $hash[7]->copy();
         // Main loop
         for ($i = 0; $i < 80; $i++) {
             $temp = array($a->bitwise_rightRotate(28), $a->bitwise_rightRotate(34), $a->bitwise_rightRotate(39));
             $s0 = $temp[0]->bitwise_xor($temp[1]);
             $s0 = $s0->bitwise_xor($temp[2]);
             $temp = array($a->bitwise_and($b), $a->bitwise_and($c), $b->bitwise_and($c));
             $maj = $temp[0]->bitwise_xor($temp[1]);
             $maj = $maj->bitwise_xor($temp[2]);
             $t2 = $s0->add($maj);
             $temp = array($e->bitwise_rightRotate(14), $e->bitwise_rightRotate(18), $e->bitwise_rightRotate(41));
             $s1 = $temp[0]->bitwise_xor($temp[1]);
             $s1 = $s1->bitwise_xor($temp[2]);
             $temp = array($e->bitwise_and($f), $g->bitwise_and($e->bitwise_not()));
             $ch = $temp[0]->bitwise_xor($temp[1]);
             $t1 = $h->add($s1);
             $t1 = $t1->add($ch);
             $t1 = $t1->add($k[$i]);
             $t1 = $t1->add($w[$i]);
             $h = $g->copy();
             $g = $f->copy();
             $f = $e->copy();
             $e = $d->add($t1);
             $d = $c->copy();
             $c = $b->copy();
             $b = $a->copy();
             $a = $t1->add($t2);
         }
         // Add this chunk's hash to result so far
         $hash = array($hash[0]->add($a), $hash[1]->add($b), $hash[2]->add($c), $hash[3]->add($d), $hash[4]->add($e), $hash[5]->add($f), $hash[6]->add($g), $hash[7]->add($h));
     }
     // Produce the final hash value (big-endian)
     // (Crypt_Hash::hash() trims the output for hashes but not for HMACs.  as such, we trim the output here)
     $temp = $hash[0]->toBytes() . $hash[1]->toBytes() . $hash[2]->toBytes() . $hash[3]->toBytes() . $hash[4]->toBytes() . $hash[5]->toBytes();
     if ($this->l != 48) {
         $temp .= $hash[6]->toBytes() . $hash[7]->toBytes();
     }
     return $temp;
 }
示例#3
0
文件: X509.php 项目: huang-sh/yaf.app
 /**
  * Get the index of a revoked certificate.
  *
  * @param array $rclist
  * @param String $serial
  * @param Boolean $create optional
  * @access private
  * @return Integer or false
  */
 function _revokedCertificate(&$rclist, $serial, $create = false)
 {
     $serial = new BigInteger($serial);
     foreach ($rclist as $i => $rc) {
         if (!$serial->compare($rc['userCertificate'])) {
             return $i;
         }
     }
     if (!$create) {
         return false;
     }
     $i = count($rclist);
     $rclist[] = array('userCertificate' => $serial, 'revocationDate' => array('generalTime' => @date('D, d M y H:i:s O')));
     return $i;
 }