Upgraded phpseclib to version 0.3.6

remotes/origin/ldap_group_count
Vincent Petry 11 years ago
parent 4cc70ec39d
commit 3f9beb8c6b
  1. 2
      apps/files_external/3rdparty/phpseclib/LICENSE
  2. 4
      apps/files_external/3rdparty/phpseclib/README.md
  3. 25
      apps/files_external/3rdparty/phpseclib/composer.json
  4. 95
      apps/files_external/3rdparty/phpseclib/composer.lock
  5. 11
      apps/files_external/3rdparty/phpseclib/phpseclib/Crypt/AES.php
  6. 11
      apps/files_external/3rdparty/phpseclib/phpseclib/Crypt/Base.php
  7. 14
      apps/files_external/3rdparty/phpseclib/phpseclib/Crypt/Blowfish.php
  8. 21
      apps/files_external/3rdparty/phpseclib/phpseclib/Crypt/DES.php
  9. 8
      apps/files_external/3rdparty/phpseclib/phpseclib/Crypt/Hash.php
  10. 11
      apps/files_external/3rdparty/phpseclib/phpseclib/Crypt/RC2.php
  11. 12
      apps/files_external/3rdparty/phpseclib/phpseclib/Crypt/RC4.php
  12. 71
      apps/files_external/3rdparty/phpseclib/phpseclib/Crypt/RSA.php
  13. 7
      apps/files_external/3rdparty/phpseclib/phpseclib/Crypt/Random.php
  14. 11
      apps/files_external/3rdparty/phpseclib/phpseclib/Crypt/Rijndael.php
  15. 11
      apps/files_external/3rdparty/phpseclib/phpseclib/Crypt/TripleDES.php
  16. 11
      apps/files_external/3rdparty/phpseclib/phpseclib/Crypt/Twofish.php
  17. 6
      apps/files_external/3rdparty/phpseclib/phpseclib/File/ANSI.php
  18. 63
      apps/files_external/3rdparty/phpseclib/phpseclib/File/ASN1.php
  19. 192
      apps/files_external/3rdparty/phpseclib/phpseclib/File/X509.php
  20. 8
      apps/files_external/3rdparty/phpseclib/phpseclib/Math/BigInteger.php
  21. 10
      apps/files_external/3rdparty/phpseclib/phpseclib/Net/SCP.php
  22. 41
      apps/files_external/3rdparty/phpseclib/phpseclib/Net/SFTP.php
  23. 12
      apps/files_external/3rdparty/phpseclib/phpseclib/Net/SFTP/Stream.php
  24. 86
      apps/files_external/3rdparty/phpseclib/phpseclib/Net/SSH1.php
  25. 236
      apps/files_external/3rdparty/phpseclib/phpseclib/Net/SSH2.php
  26. 315
      apps/files_external/3rdparty/phpseclib/phpseclib/System/SSH_Agent.php

@ -1,4 +1,4 @@
Copyright 2007-2012 TerraFrost and other contributors
Copyright 2007-2013 TerraFrost and other contributors
http://phpseclib.sourceforge.net/
Permission is hereby granted, free of charge, to any person obtaining

@ -6,11 +6,11 @@ MIT-licensed pure-PHP implementations of an arbitrary-precision integer
arithmetic library, fully PKCS#1 (v2.1) compliant RSA, DES, 3DES, RC4, Rijndael,
AES, Blowfish, Twofish, SSH-1, SSH-2, SFTP, and X.509
* [Download (0.3.5)](http://sourceforge.net/projects/phpseclib/files/phpseclib0.3.5.zip/download)
* [Download (0.3.6)](http://sourceforge.net/projects/phpseclib/files/phpseclib0.3.6.zip/download)
* [Browse Git](https://github.com/phpseclib/phpseclib)
* [Documentation](http://phpseclib.sourceforge.net/)
* [Support](http://www.frostjedi.com/phpbb/viewforum.php?f=46)
* [Code Coverage Report](http://phpseclib.bantux.org/code_coverage/latest/)
* [Code Coverage Report](http://phpseclib.bantux.org/code_coverage/master/latest/)
<img src="http://phpseclib.sourceforge.net/pear-icon.png" alt="PEAR Channel" width="16" height="16">
PEAR Channel: [phpseclib.sourceforge.net](http://phpseclib.sourceforge.net/pear.htm)

@ -11,6 +11,8 @@
"signing",
"rsa",
"aes",
"blowfish",
"twofish",
"ssh",
"sftp",
"x509",
@ -25,16 +27,34 @@
{
"name": "Jim Wigginton",
"email": "terrafrost@php.net",
"role": "Lead Developer"
},
{
"name": "Patrick Monnerat",
"email": "pm@datasphere.ch",
"role": "Developer"
},
{
"name": "Andreas Fischer",
"email": "bantu@phpbb.com",
"role": "Developer"
},
{
"name": "Hans-Jürgen Petrich",
"email": "petrich@tronic-media.com",
"role": "Developer"
}
],
"require": {
"php": ">=5.0.0"
},
"require-dev": {
"squizlabs/php_codesniffer": "1.*"
},
"suggest": {
"ext-mcrypt": "Install the Mcrypt extension in order to speed up a wide variety of cryptographic operations.",
"ext-gmp": "Install the GMP (GNU Multiple Precision) extension in order to speed up arbitrary precision integer arithmetic operations.",
"pear-pear/PHP_Compat": "Install PHP_Compat to get phpseclib working on PHP >= 4.3.3."
"pear-pear/PHP_Compat": "Install PHP_Compat to get phpseclib working on PHP < 4.3.3."
},
"include-path": ["phpseclib/"],
"autoload": {
@ -42,7 +62,8 @@
"Crypt": "phpseclib/",
"File": "phpseclib/",
"Math": "phpseclib/",
"Net": "phpseclib/"
"Net": "phpseclib/",
"System": "phpseclib/"
},
"files": [
"phpseclib/Crypt/Random.php"

@ -0,0 +1,95 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file"
],
"hash": "4975b8cde04a99d6e78e108753845af6",
"packages": [
],
"packages-dev": [
{
"name": "squizlabs/php_codesniffer",
"version": "1.5.2",
"source": {
"type": "git",
"url": "https://github.com/squizlabs/PHP_CodeSniffer.git",
"reference": "a76a39b317ce8106abe6264daa505e24e1731860"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/a76a39b317ce8106abe6264daa505e24e1731860",
"reference": "a76a39b317ce8106abe6264daa505e24e1731860",
"shasum": ""
},
"require": {
"ext-tokenizer": "*",
"php": ">=5.1.2"
},
"suggest": {
"phpunit/php-timer": "dev-master"
},
"bin": [
"scripts/phpcs"
],
"type": "library",
"autoload": {
"classmap": [
"CodeSniffer.php",
"CodeSniffer/CLI.php",
"CodeSniffer/Exception.php",
"CodeSniffer/File.php",
"CodeSniffer/Report.php",
"CodeSniffer/Reporting.php",
"CodeSniffer/Sniff.php",
"CodeSniffer/Tokens.php",
"CodeSniffer/Reports/",
"CodeSniffer/CommentParser/",
"CodeSniffer/Tokenizers/",
"CodeSniffer/DocGenerators/",
"CodeSniffer/Standards/AbstractPatternSniff.php",
"CodeSniffer/Standards/AbstractScopeSniff.php",
"CodeSniffer/Standards/AbstractVariableSniff.php",
"CodeSniffer/Standards/IncorrectPatternException.php",
"CodeSniffer/Standards/Generic/Sniffs/",
"CodeSniffer/Standards/MySource/Sniffs/",
"CodeSniffer/Standards/PEAR/Sniffs/",
"CodeSniffer/Standards/PSR1/Sniffs/",
"CodeSniffer/Standards/PSR2/Sniffs/",
"CodeSniffer/Standards/Squiz/Sniffs/",
"CodeSniffer/Standards/Zend/Sniffs/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Greg Sherwood",
"role": "lead"
}
],
"description": "PHP_CodeSniffer tokenises PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.",
"homepage": "http://www.squizlabs.com/php-codesniffer",
"keywords": [
"phpcs",
"standards"
],
"time": "2014-02-04 23:49:58"
}
],
"aliases": [
],
"minimum-stability": "stable",
"stability-flags": [
],
"platform": {
"php": ">=5.0.0"
},
"platform-dev": [
]
}

@ -1,5 +1,4 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Pure-PHP implementation of AES.
@ -66,7 +65,7 @@
* Include Crypt_Rijndael
*/
if (!class_exists('Crypt_Rijndael')) {
require_once('Rijndael.php');
include_once 'Rijndael.php';
}
/**#@+
@ -125,12 +124,13 @@ define('CRYPT_AES_MODE_MCRYPT', CRYPT_MODE_MCRYPT);
/**
* Pure-PHP implementation of AES.
*
* @package Crypt_AES
* @author Jim Wigginton <terrafrost@php.net>
* @version 0.1.0
* @access public
* @package Crypt_AES
*/
class Crypt_AES extends Crypt_Rijndael {
class Crypt_AES extends Crypt_Rijndael
{
/**
* The namespace used by the cipher for its constants.
*
@ -183,6 +183,3 @@ class Crypt_AES extends Crypt_Rijndael {
return;
}
}
// vim: ts=4:sw=4:et:
// vim6: fdl=1:

@ -1,5 +1,4 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Base Class for all Crypt_* cipher classes
@ -115,13 +114,14 @@ define('CRYPT_MODE_MCRYPT', 2);
/**
* Base Class for all Crypt_* cipher classes
*
* @package Crypt_Base
* @author Jim Wigginton <terrafrost@php.net>
* @author Hans-Juergen Petrich <petrich@tronic-media.com>
* @version 1.0.0
* @access public
* @package Crypt_Base
*/
class Crypt_Base {
class Crypt_Base
{
/**
* The Encryption Mode
*
@ -582,7 +582,7 @@ class Crypt_Base {
case !function_exists('hash_algos'):
case !in_array($hash, hash_algos()):
if (!class_exists('Crypt_Hash')) {
require_once('Crypt/Hash.php');
include_once 'Crypt/Hash.php';
}
$i = 1;
while (strlen($key) < $dkLen) {
@ -1984,6 +1984,3 @@ class Crypt_Base {
return $functions;
}
}
// vim: ts=4:sw=4:et:
// vim6: fdl=1:

@ -1,5 +1,4 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Pure-PHP implementation of Blowfish.
@ -61,7 +60,7 @@
* Base cipher class
*/
if (!class_exists('Crypt_Base')) {
require_once('Base.php');
include_once 'Base.php';
}
/**#@+
@ -120,13 +119,14 @@ define('CRYPT_BLOWFISH_MODE_MCRYPT', CRYPT_MODE_MCRYPT);
/**
* Pure-PHP implementation of Blowfish.
*
* @package Crypt_Blowfish
* @author Jim Wigginton <terrafrost@php.net>
* @author Hans-Juergen Petrich <petrich@tronic-media.com>
* @version 1.0
* @access public
* @package Crypt_Blowfish
*/
class Crypt_Blowfish extends Crypt_Base {
class Crypt_Blowfish extends Crypt_Base
{
/**
* Block Length of the cipher
*
@ -418,8 +418,7 @@ class Crypt_Blowfish extends Crypt_Base {
if (!$keylength) {
$key = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
}
elseif ($keylength > 56) {
} elseif ($keylength > 56) {
$key = substr($key, 0, 56);
}
@ -673,6 +672,3 @@ class Crypt_Blowfish extends Crypt_Base {
$this->inline_crypt = $lambda_functions[$code_hash];
}
}
// vim: ts=4:sw=4:et:
// vim6: fdl=1:

@ -1,5 +1,4 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Pure-PHP implementation of DES.
@ -65,7 +64,7 @@
* Base cipher class
*/
if (!class_exists('Crypt_Base')) {
require_once('Base.php');
include_once 'Base.php';
}
/**#@+
@ -139,12 +138,13 @@ define('CRYPT_DES_MODE_MCRYPT', CRYPT_MODE_MCRYPT);
/**
* Pure-PHP implementation of DES.
*
* @package Crypt_DES
* @author Jim Wigginton <terrafrost@php.net>
* @version 0.1.0
* @access public
* @package Crypt_DES
*/
class Crypt_DES extends Crypt_Base {
class Crypt_DES extends Crypt_Base
{
/**
* Block Length of the cipher
*
@ -1336,12 +1336,14 @@ class Crypt_DES extends Crypt_Base {
$pc2mapd3[($d >> 8) & 0xFF] | $pc2mapd4[ $d & 0xFF];
// Reorder: odd bytes/even bytes. Push the result in key schedule.
$keys[$des_round][CRYPT_DES_ENCRYPT][ ] =
$keys[$des_round][CRYPT_DES_DECRYPT][$ki - 1] = ( $cp & 0xFF000000) | (($cp << 8) & 0x00FF0000) |
$val1 = ( $cp & 0xFF000000) | (($cp << 8) & 0x00FF0000) |
(($dp >> 16) & 0x0000FF00) | (($dp >> 8) & 0x000000FF);
$keys[$des_round][CRYPT_DES_ENCRYPT][ ] =
$keys[$des_round][CRYPT_DES_DECRYPT][$ki ] = (($cp << 8) & 0xFF000000) | (($cp << 16) & 0x00FF0000) |
$val2 = (($cp << 8) & 0xFF000000) | (($cp << 16) & 0x00FF0000) |
(($dp >> 8) & 0x0000FF00) | ( $dp & 0x000000FF);
$keys[$des_round][CRYPT_DES_ENCRYPT][ ] = $val1;
$keys[$des_round][CRYPT_DES_DECRYPT][$ki - 1] = $val1;
$keys[$des_round][CRYPT_DES_ENCRYPT][ ] = $val2;
$keys[$des_round][CRYPT_DES_DECRYPT][$ki ] = $val2;
}
}
@ -1531,6 +1533,3 @@ class Crypt_DES extends Crypt_Base {
$this->inline_crypt = $lambda_functions[$code_hash];
}
}
// vim: ts=4:sw=4:et:
// vim6: fdl=1:

@ -1,5 +1,4 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Pure-PHP implementations of keyed-hash message authentication codes (HMACs) and various cryptographic hashing functions.
@ -76,12 +75,13 @@ define('CRYPT_HASH_MODE_HASH', 3);
/**
* Pure-PHP implementations of keyed-hash message authentication codes (HMACs) and various cryptographic hashing functions.
*
* @package Crypt_Hash
* @author Jim Wigginton <terrafrost@php.net>
* @version 0.1.0
* @access public
* @package Crypt_Hash
*/
class Crypt_Hash {
class Crypt_Hash
{
/**
* Hash Parameter
*
@ -581,7 +581,7 @@ class Crypt_Hash {
function _sha512($m)
{
if (!class_exists('Math_BigInteger')) {
require_once('Math/BigInteger.php');
include_once 'Math/BigInteger.php';
}
static $init384, $init512, $k;

@ -1,5 +1,4 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Pure-PHP implementation of RC2.
@ -58,7 +57,7 @@
* Base cipher class
*/
if (!class_exists('Crypt_Base')) {
require_once('Base.php');
include_once 'Base.php';
}
/**#@+
@ -117,11 +116,12 @@ define('CRYPT_RC2_MODE_MCRYPT', CRYPT_MODE_MCRYPT);
/**
* Pure-PHP implementation of RC2.
*
* @package Crypt_RC2
* @version 0.1.1
* @access public
* @package Crypt_RC2
*/
class Crypt_RC2 extends Crypt_Base {
class Crypt_RC2 extends Crypt_Base
{
/**
* Block Length of the cipher
*
@ -651,6 +651,3 @@ class Crypt_RC2 extends Crypt_Base {
$this->inline_crypt = $lambda_functions[$code_hash];
}
}
// vim: ts=4:sw=4:et:
// vim6: fdl=1:

@ -1,5 +1,4 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Pure-PHP implementation of RC4.
@ -67,7 +66,7 @@
* Base cipher class
*/
if (!class_exists('Crypt_Base')) {
require_once('Base.php');
include_once 'Base.php';
}
/**#@+
@ -95,12 +94,13 @@ define('CRYPT_RC4_DECRYPT', 1);
/**
* Pure-PHP implementation of RC4.
*
* @package Crypt_RC4
* @author Jim Wigginton <terrafrost@php.net>
* @version 0.1.0
* @access public
* @package Crypt_RC4
*/
class Crypt_RC4 extends Crypt_Base {
class Crypt_RC4 extends Crypt_Base
{
/**
* Block Length of the cipher
*
@ -132,7 +132,6 @@ class Crypt_RC4 extends Crypt_Base {
*/
var $const_namespace = 'RC4';
/**
* The mcrypt specific name of the cipher
*
@ -332,6 +331,3 @@ class Crypt_RC4 extends Crypt_Base {
return $text;
}
}
// vim: ts=4:sw=4:et:
// vim6: fdl=1:

@ -1,5 +1,4 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Pure-PHP PKCS#1 (v2.1) compliant implementation of RSA.
@ -73,17 +72,17 @@
*/
// 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
// call function_exists() a second time to stop the include_once from being called outside
// of the auto loader
if (!function_exists('crypt_random_string')) {
require_once('Random.php');
include_once 'Random.php';
}
/**
* Include Crypt_Hash
*/
if (!class_exists('Crypt_Hash')) {
require_once('Hash.php');
include_once 'Hash.php';
}
/**#@+
@ -240,12 +239,13 @@ define('CRYPT_RSA_PUBLIC_FORMAT_PKCS1', 7);
/**
* Pure-PHP PKCS#1 compliant implementation of RSA.
*
* @package Crypt_RSA
* @author Jim Wigginton <terrafrost@php.net>
* @version 0.1.0
* @access public
* @package Crypt_RSA
*/
class Crypt_RSA {
class Crypt_RSA
{
/**
* Precomputed Zero
*
@ -433,7 +433,7 @@ class Crypt_RSA {
/**
* OpenSSL configuration file name.
*
* Set to NULL to use system configuration file.
* Set to null to use system configuration file.
* @see Crypt_RSA::createKey()
* @var Mixed
* @Access public
@ -461,7 +461,7 @@ class Crypt_RSA {
function Crypt_RSA()
{
if (!class_exists('Math_BigInteger')) {
require_once('Math/BigInteger.php');
include_once 'Math/BigInteger.php';
}
$this->configFile = CRYPT_RSA_OPENSSL_CONFIG;
@ -555,7 +555,7 @@ class Crypt_RSA {
$config['config'] = $this->configFile;
}
$rsa = openssl_pkey_new(array('private_key_bits' => $bits) + $config);
openssl_pkey_export($rsa, $privatekey, NULL, $config);
openssl_pkey_export($rsa, $privatekey, null, $config);
$publickey = openssl_pkey_get_details($rsa);
$publickey = $publickey['key'];
@ -773,7 +773,7 @@ class Crypt_RSA {
$private.= crypt_random_string(16 - (strlen($private) & 15));
$source.= pack('Na*', strlen($private), $private);
if (!class_exists('Crypt_AES')) {
require_once('Crypt/AES.php');
include_once 'Crypt/AES.php';
}
$sequence = 0;
$symkey = '';
@ -794,7 +794,7 @@ class Crypt_RSA {
$key.= 'Private-Lines: ' . ((strlen($private) + 63) >> 6) . "\r\n";
$key.= chunk_split($private, 64);
if (!class_exists('Crypt_Hash')) {
require_once('Crypt/Hash.php');
include_once 'Crypt/Hash.php';
}
$hash = new Crypt_Hash('sha1');
$hash->setKey(pack('H*', sha1($hashkey)));
@ -834,7 +834,7 @@ class Crypt_RSA {
$symkey = pack('H*', md5($this->password . $iv)); // symkey is short for symmetric key
$symkey.= substr(pack('H*', md5($symkey . $this->password . $iv)), 0, 8);
if (!class_exists('Crypt_TripleDES')) {
require_once('Crypt/TripleDES.php');
include_once 'Crypt/TripleDES.php';
}
$des = new Crypt_TripleDES();
$des->setKey($symkey);
@ -992,41 +992,42 @@ class Crypt_RSA {
$iv = pack('H*', trim($matches[2]));
$symkey = pack('H*', md5($this->password . substr($iv, 0, 8))); // symkey is short for symmetric key
$symkey.= pack('H*', md5($symkey . $this->password . substr($iv, 0, 8)));
$ciphertext = preg_replace('#.+(\r|\n|\r\n)\1|[\r\n]|-.+-| #s', '', $key);
$ciphertext = preg_match('#^[a-zA-Z\d/+]*={0,2}$#', $ciphertext) ? base64_decode($ciphertext) : false;
// remove the Proc-Type / DEK-Info sections as they're no longer needed
$key = preg_replace('#^(?:Proc-Type|DEK-Info): .*#m', '', $key);
$ciphertext = $this->_extractBER($key);
if ($ciphertext === false) {
$ciphertext = $key;
}
switch ($matches[1]) {
case 'AES-256-CBC':
if (!class_exists('Crypt_AES')) {
require_once('Crypt/AES.php');
include_once 'Crypt/AES.php';
}
$crypto = new Crypt_AES();
break;
case 'AES-128-CBC':
if (!class_exists('Crypt_AES')) {
require_once('Crypt/AES.php');
include_once 'Crypt/AES.php';
}
$symkey = substr($symkey, 0, 16);
$crypto = new Crypt_AES();
break;
case 'DES-EDE3-CFB':
if (!class_exists('Crypt_TripleDES')) {
require_once('Crypt/TripleDES.php');
include_once 'Crypt/TripleDES.php';
}
$crypto = new Crypt_TripleDES(CRYPT_DES_MODE_CFB);
break;
case 'DES-EDE3-CBC':
if (!class_exists('Crypt_TripleDES')) {
require_once('Crypt/TripleDES.php');
include_once 'Crypt/TripleDES.php';
}
$symkey = substr($symkey, 0, 24);
$crypto = new Crypt_TripleDES();
break;
case 'DES-CBC':
if (!class_exists('Crypt_DES')) {
require_once('Crypt/DES.php');
include_once 'Crypt/DES.php';
}
$crypto = new Crypt_DES();
break;
@ -1037,8 +1038,7 @@ class Crypt_RSA {
$crypto->setIV($iv);
$decoded = $crypto->decrypt($ciphertext);
} else {
$decoded = preg_replace('#-.+-|[\r\n]| #', '', $key);
$decoded = preg_match('#^[a-zA-Z\d/+]*={0,2}$#', $decoded) ? base64_decode($decoded) : false;
$decoded = $this->_extractBER($key);
}
if ($decoded !== false) {
@ -1240,7 +1240,7 @@ class Crypt_RSA {
switch ($encryption) {
case 'aes256-cbc':
if (!class_exists('Crypt_AES')) {
require_once('Crypt/AES.php');
include_once 'Crypt/AES.php';
}
$symkey = '';
$sequence = 0;
@ -2781,4 +2781,31 @@ class Crypt_RSA {
return $this->_rsassa_pss_verify($message, $signature);
}
}
/**
* Extract raw BER from Base64 encoding
*
* @access private
* @param String $str
* @return String
*/
function _extractBER($str)
{
/* X.509 certs are assumed to be base64 encoded but sometimes they'll have additional things in them
* above and beyond the ceritificate.
* ie. some may have the following preceding the -----BEGIN CERTIFICATE----- line:
*
* Bag Attributes
* localKeyID: 01 00 00 00
* subject=/O=organization/OU=org unit/CN=common name
* issuer=/O=organization/CN=common name
*/
$temp = preg_replace('#.*?^-+[^-]+-+#ms', '', $str, 1);
// remove the -----BEGIN CERTIFICATE----- and -----END CERTIFICATE----- stuff
$temp = preg_replace('#-+[^-]+-+#', '', $temp);
// remove new lines
$temp = str_replace(array("\r", "\n", ' '), '', $temp);
$temp = preg_match('#^[a-zA-Z\d/+]*={0,2}$#', $temp) ? base64_decode($temp) : false;
return $temp != false ? $temp : $str;
}
}

@ -1,5 +1,4 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Random Number Generator
@ -132,9 +131,7 @@ function crypt_random_string($length)
$old_session_id = session_id();
$old_use_cookies = ini_get('session.use_cookies');
$old_session_cache_limiter = session_cache_limiter();
if (isset($_SESSION)) {
$_OLD_SESSION = $_SESSION;
}
$_OLD_SESSION = isset($_SESSION) ? $_SESSION : false;
if ($old_session_id != '') {
session_write_close();
}
@ -167,7 +164,7 @@ function crypt_random_string($length)
ini_set('session.use_cookies', $old_use_cookies);
session_cache_limiter($old_session_cache_limiter);
} else {
if (isset($_OLD_SESSION)) {
if ($_OLD_SESSION !== false) {
$_SESSION = $_OLD_SESSION;
unset($_OLD_SESSION);
} else {

@ -1,5 +1,4 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Pure-PHP implementation of Rijndael.
@ -77,7 +76,7 @@
* Base cipher class
*/
if (!class_exists('Crypt_Base')) {
require_once('Base.php');
include_once 'Base.php';
}
/**#@+
@ -136,12 +135,13 @@ define('CRYPT_RIJNDAEL_MODE_MCRYPT', CRYPT_MODE_MCRYPT);
/**
* Pure-PHP implementation of Rijndael.
*
* @package Crypt_Rijndael
* @author Jim Wigginton <terrafrost@php.net>
* @version 0.1.0
* @access public
* @package Crypt_Rijndael
*/
class Crypt_Rijndael extends Crypt_Base {
class Crypt_Rijndael extends Crypt_Base
{
/**
* The default password key_size used by setPassword()
*
@ -1369,6 +1369,3 @@ class Crypt_Rijndael extends Crypt_Base {
$this->inline_crypt = $lambda_functions[$code_hash];
}
}
// vim: ts=4:sw=4:et:
// vim6: fdl=1:

@ -1,5 +1,4 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Pure-PHP implementation of Triple DES.
@ -57,7 +56,7 @@
* Include Crypt_DES
*/
if (!class_exists('Crypt_DES')) {
require_once('DES.php');
include_once 'DES.php';
}
/**
@ -77,12 +76,13 @@ define('CRYPT_DES_MODE_CBC3', CRYPT_DES_MODE_CBC);
/**
* Pure-PHP implementation of Triple DES.
*
* @package Crypt_TripleDES
* @author Jim Wigginton <terrafrost@php.net>
* @version 0.1.0
* @access public
* @package Crypt_TripleDES
*/
class Crypt_TripleDES extends Crypt_DES {
class Crypt_TripleDES extends Crypt_DES
{
/**
* The default password key_size used by setPassword()
*
@ -417,6 +417,3 @@ class Crypt_TripleDES extends Crypt_DES {
parent::_setupKey();
}
}
// vim: ts=4:sw=4:et:
// vim6: fdl=1:

@ -1,5 +1,4 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Pure-PHP implementation of Twofish.
@ -61,7 +60,7 @@
* Base cipher class
*/
if (!class_exists('Crypt_Base')) {
require_once('Base.php');
include_once 'Base.php';
}
/**#@+
@ -120,13 +119,14 @@ define('CRYPT_TWOFISH_MODE_MCRYPT', CRYPT_MODE_MCRYPT);
/**
* Pure-PHP implementation of Twofish.
*
* @package Crypt_Twofish
* @author Jim Wigginton <terrafrost@php.net>
* @author Hans-Juergen Petrich <petrich@tronic-media.com>
* @version 1.0
* @access public
* @package Crypt_Twofish
*/
class Crypt_Twofish extends Crypt_Base {
class Crypt_Twofish extends Crypt_Base
{
/**
* The namespace used by the cipher for its constants.
*
@ -919,6 +919,3 @@ class Crypt_Twofish extends Crypt_Base {
$this->inline_crypt = $lambda_functions[$code_hash];
}
}
// vim: ts=4:sw=4:et:
// vim6: fdl=1:

@ -1,5 +1,4 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Pure-PHP ANSI Decoder
@ -40,12 +39,13 @@
/**
* Pure-PHP ANSI Decoder
*
* @package File_ANSI
* @author Jim Wigginton <terrafrost@php.net>
* @version 0.3.0
* @access public
* @package File_ANSI
*/
class File_ANSI {
class File_ANSI
{
/**
* Max Width
*

@ -1,5 +1,4 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Pure-PHP ASN.1 Parser
@ -111,12 +110,13 @@ define('FILE_ASN1_TYPE_ANY', -2);
*
* Bypass normal encoding rules in File_ASN1::encodeDER()
*
* @package File_ASN1
* @author Jim Wigginton <terrafrost@php.net>
* @version 0.3.0
* @access public
* @package File_ASN1
*/
class File_ASN1_Element {
class File_ASN1_Element
{
/**
* Raw element value
*
@ -141,12 +141,13 @@ class File_ASN1_Element {
/**
* Pure-PHP ASN.1 Parser
*
* @package File_ASN1
* @author Jim Wigginton <terrafrost@php.net>
* @version 0.3.0
* @access public
* @package File_ASN1
*/
class File_ASN1 {
class File_ASN1
{
/**
* ASN.1 object identifier
*
@ -252,7 +253,7 @@ class File_ASN1 {
if (!$static_init) {
$static_init = true;
if (!class_exists('Math_BigInteger')) {
require_once('Math/BigInteger.php');
include_once 'Math/BigInteger.php';
}
}
}
@ -545,13 +546,13 @@ class File_ASN1 {
return array($key => $value);
}
}
return NULL;
return null;
case isset($mapping['implicit']):
case isset($mapping['explicit']):
case $decoded['type'] == $mapping['type']:
break;
default:
return NULL;
return null;
}
if (isset($mapping['implicit'])) {
@ -566,8 +567,8 @@ class File_ASN1 {
if (isset($mapping['min']) && isset($mapping['max'])) {
$child = $mapping['children'];
foreach ($decoded['content'] as $content) {
if (($map[] = $this->asn1map($content, $child, $special)) === NULL) {
return NULL;
if (($map[] = $this->asn1map($content, $child, $special)) === null) {
return null;
}
}
@ -585,15 +586,14 @@ class File_ASN1 {
if ($child['type'] != FILE_ASN1_TYPE_CHOICE) {
// Get the mapping and input class & constant.
$childClass = $tempClass = FILE_ASN1_CLASS_UNIVERSAL;
$constant = NULL;
$constant = null;
if (isset($temp['constant'])) {
$tempClass = isset($temp['class']) ? $temp['class'] : FILE_ASN1_CLASS_CONTEXT_SPECIFIC;
}
if (isset($child['class'])) {
$childClass = $child['class'];
$constant = $child['cast'];
}
elseif (isset($child['constant'])) {
} elseif (isset($child['constant'])) {
$childClass = FILE_ASN1_CLASS_CONTEXT_SPECIFIC;
$constant = $child['constant'];
}
@ -611,7 +611,7 @@ class File_ASN1 {
if ($maymatch) {
// Attempt submapping.
$candidate = $this->asn1map($temp, $child, $special);
$maymatch = $candidate !== NULL;
$maymatch = $candidate !== null;
}
if ($maymatch) {
@ -624,12 +624,12 @@ class File_ASN1 {
} elseif (isset($child['default'])) {
$map[$key] = $child['default']; // Use default.
} elseif (!isset($child['optional'])) {
return NULL; // Syntax error.
return null; // Syntax error.
}
}
// Fail mapping if all input items have not been consumed.
return $i < $n? NULL: $map;
return $i < $n? null: $map;
// the main diff between sets and sequences is the encapsulation of the foreach in another for loop
case FILE_ASN1_TYPE_SET:
@ -639,8 +639,8 @@ class File_ASN1 {
if (isset($mapping['min']) && isset($mapping['max'])) {
$child = $mapping['children'];
foreach ($decoded['content'] as $content) {
if (($map[] = $this->asn1map($content, $child, $special)) === NULL) {
return NULL;
if (($map[] = $this->asn1map($content, $child, $special)) === null) {
return null;
}
}
@ -661,12 +661,11 @@ class File_ASN1 {
$maymatch = true;
if ($child['type'] != FILE_ASN1_TYPE_CHOICE) {
$childClass = FILE_ASN1_CLASS_UNIVERSAL;
$constant = NULL;
$constant = null;
if (isset($child['class'])) {
$childClass = $child['class'];
$constant = $child['cast'];
}
elseif (isset($child['constant'])) {
} elseif (isset($child['constant'])) {
$childClass = FILE_ASN1_CLASS_CONTEXT_SPECIFIC;
$constant = $child['constant'];
}
@ -683,7 +682,7 @@ class File_ASN1 {
if ($maymatch) {
// Attempt submapping.
$candidate = $this->asn1map($temp, $child, $special);
$maymatch = $candidate !== NULL;
$maymatch = $candidate !== null;
}
if (!$maymatch) {
@ -704,7 +703,7 @@ class File_ASN1 {
if (isset($child['default'])) {
$map[$key] = $child['default'];
} elseif (!isset($child['optional'])) {
return NULL;
return null;
}
}
}
@ -797,7 +796,7 @@ class File_ASN1 {
function encodeDER($source, $mapping, $special = array())
{
$this->location = array();
return $this->_encode_der($source, $mapping, NULL, $special);
return $this->_encode_der($source, $mapping, null, $special);
}
/**
@ -819,7 +818,7 @@ class File_ASN1 {
* @return String
* @access private
*/
function _encode_der($source, $mapping, $idx = NULL, $special = array())
function _encode_der($source, $mapping, $idx = null, $special = array())
{
if (is_object($source) && strtolower(get_class($source)) == 'file_asn1_element') {
return $source->element;
@ -850,7 +849,7 @@ class File_ASN1 {
$child = $mapping['children'];
foreach ($source as $content) {
$temp = $this->_encode_der($content, $child, NULL, $special);
$temp = $this->_encode_der($content, $child, null, $special);
if ($temp === false) {
return false;
}
@ -1030,19 +1029,19 @@ class File_ASN1 {
switch (true) {
case !isset($source):
return $this->_encode_der(NULL, array('type' => FILE_ASN1_TYPE_NULL) + $mapping, NULL, $special);
return $this->_encode_der(null, array('type' => FILE_ASN1_TYPE_NULL) + $mapping, null, $special);
case is_int($source):
case is_object($source) && strtolower(get_class($source)) == 'math_biginteger':
return $this->_encode_der($source, array('type' => FILE_ASN1_TYPE_INTEGER) + $mapping, NULL, $special);
return $this->_encode_der($source, array('type' => FILE_ASN1_TYPE_INTEGER) + $mapping, null, $special);
case is_float($source):
return $this->_encode_der($source, array('type' => FILE_ASN1_TYPE_REAL) + $mapping, NULL, $special);
return $this->_encode_der($source, array('type' => FILE_ASN1_TYPE_REAL) + $mapping, null, $special);
case is_bool($source):
return $this->_encode_der($source, array('type' => FILE_ASN1_TYPE_BOOLEAN) + $mapping, NULL, $special);
return $this->_encode_der($source, array('type' => FILE_ASN1_TYPE_BOOLEAN) + $mapping, null, $special);
case is_array($source) && count($source) == 1:
$typename = implode('', array_keys($source));
$outtype = array_search($typename, $this->ANYmap, true);
if ($outtype !== false) {
return $this->_encode_der($source[$typename], array('type' => $outtype) + $mapping, NULL, $special);
return $this->_encode_der($source[$typename], array('type' => $outtype) + $mapping, null, $special);
}
}
@ -1058,7 +1057,7 @@ class File_ASN1 {
user_error('No filters defined for ' . implode('/', $loc));
return false;
}
return $this->_encode_der($source, $filters + $mapping, NULL, $special);
return $this->_encode_der($source, $filters + $mapping, null, $special);
case FILE_ASN1_TYPE_NULL:
$value = '';
break;

@ -1,5 +1,4 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Pure-PHP X.509 Parser
@ -47,7 +46,7 @@
* Include File_ASN1
*/
if (!class_exists('File_ASN1')) {
require_once('ASN1.php');
include_once 'ASN1.php';
}
/**
@ -124,12 +123,13 @@ define('FILE_X509_ATTR_REPLACE', -3); // Clear first, then add a value.
/**
* Pure-PHP X.509 Parser
*
* @package File_X509
* @author Jim Wigginton <terrafrost@php.net>
* @version 0.3.1
* @access public
* @package File_X509
*/
class File_X509 {
class File_X509
{
/**
* ASN.1 syntax for X.509 certificates
*
@ -307,7 +307,7 @@ class File_X509 {
function File_X509()
{
if (!class_exists('Math_BigInteger')) {
require_once('Math/BigInteger.php');
include_once 'Math/BigInteger.php';
}
// Explicitly Tagged Module, 1988 Syntax
@ -1431,7 +1431,7 @@ class File_X509 {
$this->currentCert = $cert;
$currentKeyIdentifier = $this->getExtension('id-ce-subjectKeyIdentifier');
$this->currentKeyIdentifier = is_string($currentKeyIdentifier) ? $currentKeyIdentifier : NULL;
$this->currentKeyIdentifier = is_string($currentKeyIdentifier) ? $currentKeyIdentifier : null;
unset($this->signatureSubject);
@ -1469,7 +1469,7 @@ class File_X509 {
$this->dn = $x509['tbsCertificate']['subject'];
$currentKeyIdentifier = $this->getExtension('id-ce-subjectKeyIdentifier');
$this->currentKeyIdentifier = is_string($currentKeyIdentifier) ? $currentKeyIdentifier : NULL;
$this->currentKeyIdentifier = is_string($currentKeyIdentifier) ? $currentKeyIdentifier : null;
return $x509;
}
@ -1496,33 +1496,33 @@ class File_X509 {
default:
switch ($algorithm) {
case 'rsaEncryption':
$cert['tbsCertificate']['subjectPublicKeyInfo']['subjectPublicKey'] =
base64_encode("\0" . base64_decode(preg_replace('#-.+-|[\r\n]#', '', $cert['tbsCertificate']['subjectPublicKeyInfo']['subjectPublicKey'])));
$cert['tbsCertificate']['subjectPublicKeyInfo']['subjectPublicKey']
= base64_encode("\0" . base64_decode(preg_replace('#-.+-|[\r\n]#', '', $cert['tbsCertificate']['subjectPublicKeyInfo']['subjectPublicKey'])));
}
}
$asn1 = new File_ASN1();
$asn1->loadOIDs($this->oids);
$filters = array();
$filters['tbsCertificate']['signature']['parameters'] =
$filters['tbsCertificate']['signature']['issuer']['rdnSequence']['value'] =
$filters['tbsCertificate']['issuer']['rdnSequence']['value'] =
$filters['tbsCertificate']['subject']['rdnSequence']['value'] =
$filters['tbsCertificate']['subjectPublicKeyInfo']['algorithm']['parameters'] =
$filters['signatureAlgorithm']['parameters'] =
$filters['authorityCertIssuer']['directoryName']['rdnSequence']['value'] =
//$filters['policyQualifiers']['qualifier'] =
$filters['distributionPoint']['fullName']['directoryName']['rdnSequence']['value'] =
$filters['directoryName']['rdnSequence']['value'] =
array('type' => FILE_ASN1_TYPE_UTF8_STRING);
$type_utf8_string = array('type' => FILE_ASN1_TYPE_UTF8_STRING);
$filters['tbsCertificate']['signature']['parameters'] = $type_utf8_string;
$filters['tbsCertificate']['signature']['issuer']['rdnSequence']['value'] = $type_utf8_string;
$filters['tbsCertificate']['issuer']['rdnSequence']['value'] = $type_utf8_string;
$filters['tbsCertificate']['subject']['rdnSequence']['value'] = $type_utf8_string;
$filters['tbsCertificate']['subjectPublicKeyInfo']['algorithm']['parameters'] = $type_utf8_string;
$filters['signatureAlgorithm']['parameters'] = $type_utf8_string;
$filters['authorityCertIssuer']['directoryName']['rdnSequence']['value'] = $type_utf8_string;
//$filters['policyQualifiers']['qualifier'] = $type_utf8_string;
$filters['distributionPoint']['fullName']['directoryName']['rdnSequence']['value'] = $type_utf8_string;
$filters['directoryName']['rdnSequence']['value'] = $type_utf8_string;
/* in the case of policyQualifiers/qualifier, the type has to be FILE_ASN1_TYPE_IA5_STRING.
FILE_ASN1_TYPE_PRINTABLE_STRING will cause OpenSSL's X.509 parser to spit out random
characters.
*/
$filters['policyQualifiers']['qualifier'] =
array('type' => FILE_ASN1_TYPE_IA5_STRING);
$filters['policyQualifiers']['qualifier']
= array('type' => FILE_ASN1_TYPE_IA5_STRING);
$asn1->loadFilters($filters);
@ -1715,8 +1715,7 @@ class File_X509 {
if ($map === false) {
user_error($id . ' is not a currently supported attribute', E_USER_NOTICE);
unset($attributes[$i]);
}
elseif (is_array($attributes[$i]['value'])) {
} elseif (is_array($attributes[$i]['value'])) {
$values = &$attributes[$i]['value'];
for ($j = 0; $j < count($values); $j++) {
switch ($id) {
@ -1963,7 +1962,7 @@ class File_X509 {
* @param Integer $date optional
* @access public
*/
function validateDate($date = NULL)
function validateDate($date = null)
{
if (!is_array($this->currentCert) || !isset($this->currentCert['tbsCertificate'])) {
return false;
@ -1992,7 +1991,7 @@ class File_X509 {
* Validate a signature
*
* Works on X.509 certs, CSR's and CRL's.
* Returns true if the signature is verified, false if it is not correct or NULL on error
* Returns true if the signature is verified, false if it is not correct or null on error
*
* By default returns false for self-signed certs. Call validateSignature(false) to make this support
* self-signed.
@ -2006,7 +2005,7 @@ class File_X509 {
function validateSignature($caonly = true)
{
if (!is_array($this->currentCert) || !isset($this->signatureSubject)) {
return NULL;
return null;
}
/* TODO:
@ -2107,7 +2106,7 @@ class File_X509 {
/**
* Validates a signature
*
* Returns true if the signature is verified, false if it is not correct or NULL on error
* Returns true if the signature is verified, false if it is not correct or null on error
*
* @param String $publicKeyAlgorithm
* @param String $publicKey
@ -2122,7 +2121,7 @@ class File_X509 {
switch ($publicKeyAlgorithm) {
case 'rsaEncryption':
if (!class_exists('Crypt_RSA')) {
require_once('Crypt/RSA.php');
include_once 'Crypt/RSA.php';
}
$rsa = new Crypt_RSA();
$rsa->loadKey($publicKey);
@ -2142,11 +2141,11 @@ class File_X509 {
}
break;
default:
return NULL;
return null;
}
break;
default:
return NULL;
return null;
}
return true;
@ -2367,7 +2366,7 @@ class File_X509 {
* @return Mixed
* @access public
*/
function getDNProp($propName, $dn = NULL, $withType = false)
function getDNProp($propName, $dn = null, $withType = false)
{
if (!isset($dn)) {
$dn = $this->dn;
@ -2421,7 +2420,7 @@ class File_X509 {
function setDN($dn, $merge = false, $type = 'utf8String')
{
if (!$merge) {
$this->dn = NULL;
$this->dn = null;
}
if (is_array($dn)) {
@ -2460,7 +2459,7 @@ class File_X509 {
* @access public
* @return Boolean
*/
function getDN($format = FILE_X509_DN_ARRAY, $dn = NULL)
function getDN($format = FILE_X509_DN_ARRAY, $dn = null)
{
if (!isset($dn)) {
$dn = isset($this->currentCert['tbsCertList']) ? $this->currentCert['tbsCertList']['issuer'] : $this->dn;
@ -2524,7 +2523,7 @@ class File_X509 {
case FILE_X509_DN_HASH:
$dn = $this->getDN(FILE_X509_DN_CANON, $dn);
if (!class_exists('Crypt_Hash')) {
require_once('Crypt/Hash.php');
include_once 'Crypt/Hash.php';
}
$hash = new Crypt_Hash('sha1');
$hash = $hash->hash($dn);
@ -2676,7 +2675,7 @@ class File_X509 {
{
switch (true) {
case !empty($this->dn):
return $this->getDNProp($propName, NULL, $withType);
return $this->getDNProp($propName, null, $withType);
case !isset($this->currentCert) || !is_array($this->currentCert):
break;
case isset($this->currentCert['tbsCertificate']):
@ -2792,7 +2791,7 @@ class File_X509 {
switch ($keyinfo['algorithm']['algorithm']) {
case 'rsaEncryption':
if (!class_exists('Crypt_RSA')) {
require_once('Crypt/RSA.php');
include_once 'Crypt/RSA.php';
}
$publicKey = new Crypt_RSA();
$publicKey->loadKey($key);
@ -2865,17 +2864,17 @@ class File_X509 {
switch ($algorithm) {
case 'rsaEncryption':
if (!class_exists('Crypt_RSA')) {
require_once('Crypt/RSA.php');
include_once 'Crypt/RSA.php';
}
$this->publicKey = new Crypt_RSA();
$this->publicKey->loadKey($key);
$this->publicKey->setPublicKey();
break;
default:
$this->publicKey = NULL;
$this->publicKey = null;
}
$this->currentKeyIdentifier = NULL;
$this->currentKeyIdentifier = null;
$this->currentCert = $csr;
return $csr;
@ -2902,8 +2901,8 @@ class File_X509 {
default:
switch ($algorithm) {
case 'rsaEncryption':
$csr['certificationRequestInfo']['subjectPKInfo']['subjectPublicKey'] =
base64_encode("\0" . base64_decode(preg_replace('#-.+-|[\r\n]#', '', $csr['certificationRequestInfo']['subjectPKInfo']['subjectPublicKey'])));
$csr['certificationRequestInfo']['subjectPKInfo']['subjectPublicKey']
= base64_encode("\0" . base64_decode(preg_replace('#-.+-|[\r\n]#', '', $csr['certificationRequestInfo']['subjectPKInfo']['subjectPublicKey'])));
}
}
@ -2912,8 +2911,8 @@ class File_X509 {
$asn1->loadOIDs($this->oids);
$filters = array();
$filters['certificationRequestInfo']['subject']['rdnSequence']['value'] =
array('type' => FILE_ASN1_TYPE_UTF8_STRING);
$filters['certificationRequestInfo']['subject']['rdnSequence']['value']
= array('type' => FILE_ASN1_TYPE_UTF8_STRING);
$asn1->loadFilters($filters);
@ -2990,17 +2989,17 @@ class File_X509 {
switch ($algorithm) {
case 'rsaEncryption':
if (!class_exists('Crypt_RSA')) {
require_once('Crypt/RSA.php');
include_once 'Crypt/RSA.php';
}
$this->publicKey = new Crypt_RSA();
$this->publicKey->loadKey($key);
$this->publicKey->setPublicKey();
break;
default:
$this->publicKey = NULL;
$this->publicKey = null;
}
$this->currentKeyIdentifier = NULL;
$this->currentKeyIdentifier = null;
$this->currentCert = $spkac;
return $spkac;
@ -3055,7 +3054,7 @@ class File_X509 {
}
}
$this->currentKeyIdentifier = NULL;
$this->currentKeyIdentifier = null;
$this->currentCert = $crl;
return $crl;
@ -3080,19 +3079,21 @@ class File_X509 {
$asn1->loadOIDs($this->oids);
$filters = array();
$filters['tbsCertList']['issuer']['rdnSequence']['value'] =
$filters['tbsCertList']['signature']['parameters'] =
$filters['signatureAlgorithm']['parameters'] =
array('type' => FILE_ASN1_TYPE_UTF8_STRING);
$filters['tbsCertList']['issuer']['rdnSequence']['value']
= array('type' => FILE_ASN1_TYPE_UTF8_STRING);
$filters['tbsCertList']['signature']['parameters']
= array('type' => FILE_ASN1_TYPE_UTF8_STRING);
$filters['signatureAlgorithm']['parameters']
= array('type' => FILE_ASN1_TYPE_UTF8_STRING);
if (empty($crl['tbsCertList']['signature']['parameters'])) {
$filters['tbsCertList']['signature']['parameters'] =
array('type' => FILE_ASN1_TYPE_NULL);
$filters['tbsCertList']['signature']['parameters']
= array('type' => FILE_ASN1_TYPE_NULL);
}
if (empty($crl['signatureAlgorithm']['parameters'])) {
$filters['signatureAlgorithm']['parameters'] =
array('type' => FILE_ASN1_TYPE_NULL);
$filters['signatureAlgorithm']['parameters']
= array('type' => FILE_ASN1_TYPE_NULL);
}
$asn1->loadFilters($filters);
@ -3139,14 +3140,14 @@ class File_X509 {
return false;
}
$currentCert = isset($this->currentCert) ? $this->currentCert : NULL;
$signatureSubject = isset($this->signatureSubject) ? $this->signatureSubject: NULL;
$currentCert = isset($this->currentCert) ? $this->currentCert : null;
$signatureSubject = isset($this->signatureSubject) ? $this->signatureSubject: null;
if (isset($subject->currentCert) && is_array($subject->currentCert) && isset($subject->currentCert['tbsCertificate'])) {
$this->currentCert = $subject->currentCert;
$this->currentCert['tbsCertificate']['signature']['algorithm'] =
$this->currentCert['signatureAlgorithm']['algorithm'] =
$signatureAlgorithm;
$this->currentCert['tbsCertificate']['signature']['algorithm'] = $signatureAlgorithm;
$this->currentCert['signatureAlgorithm']['algorithm'] = $signatureAlgorithm;
if (!empty($this->startDate)) {
$this->currentCert['tbsCertificate']['validity']['notBefore']['generalTime'] = $this->startDate;
unset($this->currentCert['tbsCertificate']['validity']['notBefore']['utcTime']);
@ -3312,12 +3313,11 @@ class File_X509 {
}
$this->publicKey = $origPublicKey;
$currentCert = isset($this->currentCert) ? $this->currentCert : NULL;
$signatureSubject = isset($this->signatureSubject) ? $this->signatureSubject: NULL;
$currentCert = isset($this->currentCert) ? $this->currentCert : null;
$signatureSubject = isset($this->signatureSubject) ? $this->signatureSubject: null;
if (isset($this->currentCert) && is_array($this->currentCert) && isset($this->currentCert['certificationRequestInfo'])) {
$this->currentCert['signatureAlgorithm']['algorithm'] =
$signatureAlgorithm;
$this->currentCert['signatureAlgorithm']['algorithm'] = $signatureAlgorithm;
if (!empty($this->dn)) {
$this->currentCert['certificationRequestInfo']['subject'] = $this->dn;
}
@ -3366,8 +3366,8 @@ class File_X509 {
return false;
}
$currentCert = isset($this->currentCert) ? $this->currentCert : NULL;
$signatureSubject = isset($this->signatureSubject) ? $this->signatureSubject : NULL;
$currentCert = isset($this->currentCert) ? $this->currentCert : null;
$signatureSubject = isset($this->signatureSubject) ? $this->signatureSubject : null;
$thisUpdate = !empty($this->startDate) ? $this->startDate : @date('D, d M y H:i:s O');
if (isset($crl->currentCert) && is_array($crl->currentCert) && isset($crl->currentCert['tbsCertList'])) {
@ -3400,10 +3400,9 @@ class File_X509 {
if (!empty($this->serialNumber)) {
$crlNumber = $this->serialNumber;
}
else {
} else {
$crlNumber = $this->getExtension('id-ce-cRLNumber');
$crlNumber = $crlNumber !== false ? $crlNumber->add(new Math_BigInteger(1)) : NULL;
$crlNumber = $crlNumber !== false ? $crlNumber->add(new Math_BigInteger(1)) : null;
}
$this->removeExtension('id-ce-authorityKeyIdentifier');
@ -3414,8 +3413,7 @@ class File_X509 {
if (!$version) {
if (!empty($tbsCertList['crlExtensions'])) {
$version = 1; // v2.
}
elseif (!empty($tbsCertList['revokedCertificates'])) {
} elseif (!empty($tbsCertList['revokedCertificates'])) {
foreach ($tbsCertList['revokedCertificates'] as $cert) {
if (!empty($cert['crlEntryExtensions'])) {
$version = 1; // v2.
@ -3613,7 +3611,7 @@ class File_X509 {
* @access private
* @return array ref or false
*/
function &_extensions(&$root, $path = NULL, $create = false)
function &_extensions(&$root, $path = null, $create = false)
{
if (!isset($root)) {
$root = $this->currentCert;
@ -3667,7 +3665,7 @@ class File_X509 {
* @access private
* @return Boolean
*/
function _removeExtension($id, $path = NULL)
function _removeExtension($id, $path = null)
{
$extensions = &$this->_extensions($this->currentCert, $path);
@ -3698,7 +3696,7 @@ class File_X509 {
* @access private
* @return Mixed
*/
function _getExtension($id, $cert = NULL, $path = NULL)
function _getExtension($id, $cert = null, $path = null)
{
$extensions = $this->_extensions($cert, $path);
@ -3723,7 +3721,7 @@ class File_X509 {
* @access private
* @return Array
*/
function _getExtensions($cert = NULL, $path = NULL)
function _getExtensions($cert = null, $path = null)
{
$exts = $this->_extensions($cert, $path);
$extensions = array();
@ -3748,7 +3746,7 @@ class File_X509 {
* @access private
* @return Boolean
*/
function _setExtension($id, $value, $critical = false, $replace = true, $path = NULL)
function _setExtension($id, $value, $critical = false, $replace = true, $path = null)
{
$extensions = &$this->_extensions($this->currentCert, $path, true);
@ -3795,7 +3793,7 @@ class File_X509 {
* @access public
* @return Mixed
*/
function getExtension($id, $cert = NULL)
function getExtension($id, $cert = null)
{
return $this->_getExtension($id, $cert);
}
@ -3807,7 +3805,7 @@ class File_X509 {
* @access public
* @return Array
*/
function getExtensions($cert = NULL)
function getExtensions($cert = null)
{
return $this->_getExtensions($cert);
}
@ -3886,7 +3884,7 @@ class File_X509 {
* @access public
* @return Mixed
*/
function getAttribute($id, $disposition = FILE_X509_ATTR_ALL, $csr = NULL)
function getAttribute($id, $disposition = FILE_X509_ATTR_ALL, $csr = null)
{
if (empty($csr)) {
$csr = $this->currentCert;
@ -3926,7 +3924,7 @@ class File_X509 {
* @access public
* @return Array
*/
function getAttributes($csr = NULL)
function getAttributes($csr = null)
{
if (empty($csr)) {
$csr = $this->currentCert;
@ -4035,7 +4033,7 @@ class File_X509 {
* @access public
* @return String binary key identifier
*/
function computeKeyIdentifier($key = NULL, $method = 1)
function computeKeyIdentifier($key = null, $method = 1)
{
if (is_null($key)) {
$key = $this;
@ -4064,7 +4062,7 @@ class File_X509 {
$raw = base64_decode($raw);
// If the key is private, compute identifier from its corresponding public key.
if (!class_exists('Crypt_RSA')) {
require_once('Crypt/RSA.php');
include_once 'Crypt/RSA.php';
}
$key = new Crypt_RSA();
if (!$key->loadKey($raw)) {
@ -4096,7 +4094,7 @@ class File_X509 {
// Now we have the key string: compute its sha-1 sum.
if (!class_exists('Crypt_Hash')) {
require_once('Crypt/Hash.php');
include_once 'Crypt/Hash.php';
}
$hash = new Crypt_Hash('sha1');
$hash = $hash->hash($key);
@ -4228,7 +4226,7 @@ class File_X509 {
* @access public
* @return Boolean
*/
function revoke($serial, $date = NULL)
function revoke($serial, $date = null)
{
if (isset($this->currentCert['tbsCertList'])) {
if (is_array($rclist = &$this->_subArray($this->currentCert, 'tbsCertList/revokedCertificates', true))) {
@ -4293,7 +4291,7 @@ class File_X509 {
* @access public
* @return array
*/
function listRevoked($crl = NULL)
function listRevoked($crl = null)
{
if (!isset($crl)) {
$crl = $this->currentCert;
@ -4344,7 +4342,7 @@ class File_X509 {
* @access public
* @return Mixed
*/
function getRevokedCertificateExtension($serial, $id, $crl = NULL)
function getRevokedCertificateExtension($serial, $id, $crl = null)
{
if (!isset($crl)) {
$crl = $this->currentCert;
@ -4367,7 +4365,7 @@ class File_X509 {
* @access public
* @return Array
*/
function getRevokedCertificateExtensions($serial, $crl = NULL)
function getRevokedCertificateExtensions($serial, $crl = null)
{
if (!isset($crl)) {
$crl = $this->currentCert;
@ -4415,14 +4413,14 @@ class File_X509 {
*/
function _extractBER($str)
{
/*
X.509 certs are assumed to be base64 encoded but sometimes they'll have additional things in them above and beyond the ceritificate. ie.
some may have the following preceding the -----BEGIN CERTIFICATE----- line:
Bag Attributes
localKeyID: 01 00 00 00
subject=/O=organization/OU=org unit/CN=common name
issuer=/O=organization/CN=common name
/* X.509 certs are assumed to be base64 encoded but sometimes they'll have additional things in them
* above and beyond the ceritificate.
* ie. some may have the following preceding the -----BEGIN CERTIFICATE----- line:
*
* Bag Attributes
* localKeyID: 01 00 00 00
* subject=/O=organization/OU=org unit/CN=common name
* issuer=/O=organization/CN=common name
*/
$temp = preg_replace('#.*?^-+[^-]+-+#ms', '', $str, 1);
// remove the -----BEGIN CERTIFICATE----- and -----END CERTIFICATE----- stuff

@ -1,5 +1,4 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Pure-PHP arbitrary precision integer arithmetic library.
@ -174,12 +173,13 @@ define('MATH_BIGINTEGER_KARATSUBA_CUTOFF', 25);
* Pure-PHP arbitrary precision integer arithmetic library. Supports base-2, base-10, base-16, and base-256
* numbers.
*
* @package Math_BigInteger
* @author Jim Wigginton <terrafrost@php.net>
* @version 1.0.0RC4
* @access public
* @package Math_BigInteger
*/
class Math_BigInteger {
class Math_BigInteger
{
/**
* Holds the BigInteger's value.
*
@ -3299,6 +3299,7 @@ class Math_BigInteger {
if (!$t) {
// see HAC 4.49 "Note (controlling the error probability)"
// @codingStandardsIgnoreStart
if ($length >= 163) { $t = 2; } // floor(1300 / 8)
else if ($length >= 106) { $t = 3; } // floor( 850 / 8)
else if ($length >= 81 ) { $t = 4; } // floor( 650 / 8)
@ -3311,6 +3312,7 @@ class Math_BigInteger {
else if ($length >= 25 ) { $t = 15; } // floor( 200 / 8)
else if ($length >= 18 ) { $t = 18; } // floor( 150 / 8)
else { $t = 27; }
// @codingStandardsIgnoreEnd
}
// ie. gmp_testbit($this, 0)

@ -1,5 +1,4 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Pure-PHP implementation of SCP.
@ -82,12 +81,13 @@ define('NET_SCP_SSH2', 2);
/**
* Pure-PHP implementations of SCP.
*
* @package Net_SCP
* @author Jim Wigginton <terrafrost@php.net>
* @version 0.1.0
* @access public
* @package Net_SCP
*/
class Net_SCP {
class Net_SCP
{
/**
* SSH Object
*
@ -181,7 +181,7 @@ class Net_SCP {
}
if ($this->mode == NET_SCP_SSH2) {
$this->packet_size = $this->ssh->packet_size_client_to_server[NET_SSH2_CHANNEL_EXEC];
$this->packet_size = $this->ssh->packet_size_client_to_server[NET_SSH2_CHANNEL_EXEC] - 4;
}
$remote_file = basename($remote_file);
@ -353,7 +353,7 @@ class Net_SCP {
{
switch ($this->mode) {
case NET_SCP_SSH2:
$this->ssh->_close_channel(NET_SSH2_CHANNEL_EXEC);
$this->ssh->_close_channel(NET_SSH2_CHANNEL_EXEC, true);
break;
case NET_SCP_SSH1:
$this->ssh->disconnect();

@ -1,5 +1,4 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Pure-PHP implementation of SFTP.
@ -58,7 +57,7 @@
* Include Net_SSH2
*/
if (!class_exists('Net_SSH2')) {
require_once('SSH2.php');
include_once 'SSH2.php';
}
/**#@+
@ -116,12 +115,13 @@ define('NET_SFTP_RESUME_START', 8);
/**
* Pure-PHP implementations of SFTP.
*
* @package Net_SFTP
* @author Jim Wigginton <terrafrost@php.net>
* @version 0.1.0
* @access public
* @package Net_SFTP
*/
class Net_SFTP extends Net_SSH2 {
class Net_SFTP extends Net_SSH2
{
/**
* Packet Types
*
@ -402,7 +402,7 @@ class Net_SFTP extends Net_SSH2 {
function login($username)
{
$args = func_get_args();
if (!call_user_func_array(array('Net_SSH2', 'login'), $args)) {
if (!call_user_func_array(array(&$this, '_login'), $args)) {
return false;
}
@ -635,7 +635,11 @@ class Net_SFTP extends Net_SSH2 {
return false;
}
if ($dir[strlen($dir) - 1] != '/') {
// assume current dir if $dir is empty
if ($dir === '') {
$dir = './';
// suffix a slash if needed
} elseif ($dir[strlen($dir) - 1] != '/') {
$dir.= '/';
}
@ -1038,7 +1042,7 @@ class Net_SFTP extends Net_SSH2 {
*/
function truncate($filename, $new_size)
{
$attr = pack('N3', NET_SFTP_ATTR_SIZE, $new_size / 0x100000000, $new_size);
$attr = pack('N3', NET_SFTP_ATTR_SIZE, $new_size / 4294967296, $new_size); // 4294967296 == 0x100000000 == 1<<32
return $this->_setstat($filename, $attr, false);
}
@ -1054,7 +1058,7 @@ class Net_SFTP extends Net_SSH2 {
* @return Boolean
* @access public
*/
function touch($filename, $time = NULL, $atime = NULL)
function touch($filename, $time = null, $atime = null)
{
if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {
return false;
@ -1097,7 +1101,7 @@ class Net_SFTP extends Net_SSH2 {
/**
* Changes file or directory owner
*
* Returns TRUE on success or FALSE on error.
* Returns true on success or false on error.
*
* @param String $filename
* @param Integer $uid
@ -1117,7 +1121,7 @@ class Net_SFTP extends Net_SSH2 {
/**
* Changes file or directory group
*
* Returns TRUE on success or FALSE on error.
* Returns true on success or false on error.
*
* @param String $filename
* @param Integer $gid
@ -1135,8 +1139,8 @@ class Net_SFTP extends Net_SSH2 {
/**
* Set permissions on a file.
*
* Returns the new file permissions on success or FALSE on error.
* If $recursive is true than this just returns TRUE or FALSE.
* Returns the new file permissions on success or false on error.
* If $recursive is true than this just returns true or false.
*
* @param Integer $mode
* @param String $filename
@ -1502,8 +1506,6 @@ class Net_SFTP extends Net_SSH2 {
return false;
}
$initialize = true;
// http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.2.3
if ($mode & NET_SFTP_LOCAL_FILE) {
if (!is_file($data)) {
@ -1537,7 +1539,7 @@ class Net_SFTP extends Net_SSH2 {
while ($sent < $size) {
$temp = $mode & NET_SFTP_LOCAL_FILE ? fread($fp, $sftp_packet_size) : substr($data, $sent, $sftp_packet_size);
$subtemp = $offset + $sent;
$packet = pack('Na*N3a*', strlen($handle), $handle, $subtemp / 0x100000000, $subtemp, strlen($temp), $temp);
$packet = pack('Na*N3a*', strlen($handle), $handle, $subtemp / 4294967296, $subtemp, strlen($temp), $temp);
if (!$this->_send_sftp_packet(NET_SFTP_WRITE, $packet)) {
fclose($fp);
return false;
@ -1683,9 +1685,10 @@ class Net_SFTP extends Net_SSH2 {
$content = '';
}
$start = $offset;
$size = $this->max_sftp_packet < $length || $length < 0 ? $this->max_sftp_packet : $length;
while (true) {
$packet = pack('Na*N3', strlen($handle), $handle, $offset / 0x100000000, $offset, $size);
$packet = pack('Na*N3', strlen($handle), $handle, $offset / 4294967296, $offset, $size);
if (!$this->_send_sftp_packet(NET_SFTP_READ, $packet)) {
if ($local_file !== false) {
fclose($fp);
@ -1716,12 +1719,12 @@ class Net_SFTP extends Net_SSH2 {
return false;
}
if ($length > 0 && $length <= $offset - $size) {
if ($length > 0 && $length <= $offset - $start) {
break;
}
}
if ($length > 0 && $length <= $offset - $size) {
if ($length > 0 && $length <= $offset - $start) {
if ($local_file === false) {
$content = substr($content, 0, $length);
} else {
@ -1923,7 +1926,7 @@ class Net_SFTP extends Net_SSH2 {
// (0xFFFFFFFF bytes), anyway. as such, we'll just represent all file sizes that are bigger than
// 4GB as being 4GB.
extract(unpack('Nupper/Nsize', $this->_string_shift($response, 8)));
$attr['size'] = $upper ? 0x100000000 * $upper : 0;
$attr['size'] = $upper ? 4294967296 * $upper : 0;
$attr['size']+= $size < 0 ? ($size & 0x7FFFFFFF) + 0x80000000 : $size;
break;
case NET_SFTP_ATTR_UIDGID: // 0x00000002 (SFTPv3 only)

@ -1,5 +1,4 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* SFTP Stream Wrapper
@ -37,12 +36,13 @@
/**
* SFTP Stream Wrapper
*
* @package Net_SFTP_Stream
* @author Jim Wigginton <terrafrost@php.net>
* @version 0.3.2
* @access public
* @package Net_SFTP_Stream
*/
class Net_SFTP_Stream {
class Net_SFTP_Stream
{
/**
* SFTP instances
*
@ -134,8 +134,12 @@ class Net_SFTP_Stream {
*/
function Net_SFTP_Stream()
{
if (defined('NET_SFTP_STREAM_LOGGING')) {
echo "__construct()\r\n";
}
if (!class_exists('Net_SFTP')) {
require_once('Net/SFTP.php');
include_once 'Net/SFTP.php';
}
}

@ -1,5 +1,4 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Pure-PHP implementation of SSHv1.
@ -202,11 +201,11 @@ define('NET_SSH1_LOG_COMPLEX', 2);
/**
* Outputs the content real-time
*/
define('NET_SSH2_LOG_REALTIME', 3);
define('NET_SSH1_LOG_REALTIME', 3);
/**
* Dumps the content real-time to a file
*/
define('NET_SSH2_LOG_REALTIME_FILE', 4);
define('NET_SSH1_LOG_REALTIME_FILE', 4);
/**#@-*/
/**#@+
@ -226,12 +225,13 @@ define('NET_SSH1_READ_REGEX', 2);
/**
* Pure-PHP implementation of SSHv1.
*
* @package Net_SSH1
* @author Jim Wigginton <terrafrost@php.net>
* @version 0.1.0
* @access public
* @package Net_SSH1
*/
class Net_SSH1 {
class Net_SSH1
{
/**
* The SSH identifier
*
@ -429,11 +429,35 @@ class Net_SSH1 {
/**
* Current Timeout
*
* @see Net_SSH2::_get_channel_packet()
* @see Net_SSH1::_get_channel_packet()
* @access private
*/
var $curTimeout;
/**
* Log Boundary
*
* @see Net_SSH1::_format_log
* @access private
*/
var $log_boundary = ':';
/**
* Log Long Width
*
* @see Net_SSH1::_format_log
* @access private
*/
var $log_long_width = 65;
/**
* Log Short Width
*
* @see Net_SSH1::_format_log
* @access private
*/
var $log_short_width = 16;
/**
* Default Constructor.
*
@ -449,16 +473,16 @@ class Net_SSH1 {
function Net_SSH1($host, $port = 22, $timeout = 10, $cipher = NET_SSH1_CIPHER_3DES)
{
if (!class_exists('Math_BigInteger')) {
require_once('Math/BigInteger.php');
include_once 'Math/BigInteger.php';
}
// 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
// call function_exists() a second time to stop the include_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');
include_once 'Crypt/Random.php';
}
$this->protocol_flags = array(
@ -603,7 +627,7 @@ class Net_SSH1 {
// break;
case NET_SSH1_CIPHER_DES:
if (!class_exists('Crypt_DES')) {
require_once('Crypt/DES.php');
include_once 'Crypt/DES.php';
}
$this->crypto = new Crypt_DES();
$this->crypto->disablePadding();
@ -612,7 +636,7 @@ class Net_SSH1 {
break;
case NET_SSH1_CIPHER_3DES:
if (!class_exists('Crypt_TripleDES')) {
require_once('Crypt/TripleDES.php');
include_once 'Crypt/TripleDES.php';
}
$this->crypto = new Crypt_TripleDES(CRYPT_DES_MODE_3CBC);
$this->crypto->disablePadding();
@ -621,7 +645,7 @@ class Net_SSH1 {
break;
//case NET_SSH1_CIPHER_RC4:
// if (!class_exists('Crypt_RC4')) {
// require_once('Crypt/RC4.php');
// include_once('Crypt/RC4.php');
// }
// $this->crypto = new Crypt_RC4();
// $this->crypto->enableContinuousBuffer();
@ -1017,7 +1041,7 @@ class Net_SSH1 {
if ($this->curTimeout) {
$read = array($this->fsock);
$write = $except = NULL;
$write = $except = null;
$start = strtok(microtime(), ' ') + strtok(''); // http://php.net/microtime#61838
$sec = floor($this->curTimeout);
@ -1253,7 +1277,7 @@ class Net_SSH1 {
{
/*
if (!class_exists('Crypt_RSA')) {
require_once('Crypt/RSA.php');
include_once 'Crypt/RSA.php';
}
$rsa = new Crypt_RSA();
@ -1318,7 +1342,7 @@ class Net_SSH1 {
/**
* Returns a log of the packets that have been sent and received.
*
* Returns a string if NET_SSH2_LOGGING == NET_SSH2_LOG_COMPLEX, an array if NET_SSH2_LOGGING == NET_SSH2_LOG_SIMPLE and false if !defined('NET_SSH2_LOGGING')
* Returns a string if NET_SSH1_LOGGING == NET_SSH1_LOG_COMPLEX, an array if NET_SSH1_LOGGING == NET_SSH1_LOG_SIMPLE and false if !defined('NET_SSH1_LOGGING')
*
* @access public
* @return String or Array
@ -1351,8 +1375,6 @@ class Net_SSH1 {
*/
function _format_log($message_log, $message_number_log)
{
static $boundary = ':', $long_width = 65, $short_width = 16;
$output = '';
for ($i = 0; $i < count($message_log); $i++) {
$output.= $message_number_log[$i] . "\r\n";
@ -1362,19 +1384,13 @@ class Net_SSH1 {
if (strlen($current_log)) {
$output.= str_pad(dechex($j), 7, '0', STR_PAD_LEFT) . '0 ';
}
$fragment = $this->_string_shift($current_log, $short_width);
$hex = substr(
preg_replace(
'#(.)#es',
'"' . $boundary . '" . str_pad(dechex(ord(substr("\\1", -1))), 2, "0", STR_PAD_LEFT)',
$fragment),
strlen($boundary)
);
$fragment = $this->_string_shift($current_log, $this->log_short_width);
$hex = substr(preg_replace_callback('#.#s', array($this, '_format_log_helper'), $fragment), strlen($this->log_boundary));
// replace non ASCII printable characters with dots
// http://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters
// also replace < with a . since < messes up the output on web browsers
$raw = preg_replace('#[^\x20-\x7E]|<#', '.', $fragment);
$output.= str_pad($hex, $long_width - $short_width, ' ') . $raw . "\r\n";
$output.= str_pad($hex, $this->log_long_width - $this->log_short_width, ' ') . $raw . "\r\n";
$j++;
} while (strlen($current_log));
$output.= "\r\n";
@ -1383,6 +1399,20 @@ class Net_SSH1 {
return $output;
}
/**
* Helper function for _format_log
*
* For use with preg_replace_callback()
*
* @param Array $matches
* @access private
* @return String
*/
function _format_log_helper($matches)
{
return $this->log_boundary . str_pad(dechex(ord($matches[0])), 2, '0', STR_PAD_LEFT);
}
/**
* Return the server key public exponent
*
@ -1507,7 +1537,7 @@ class Net_SSH1 {
$this->_string_shift($message);
$this->log_size+= strlen($message);
$this->message_log[] = $message;
while ($this->log_size > NET_SSH2_LOG_MAX_SIZE) {
while ($this->log_size > NET_SSH1_LOG_MAX_SIZE) {
$this->log_size-= strlen(array_shift($this->message_log));
array_shift($this->protocol_flags_log);
}
@ -1527,7 +1557,7 @@ class Net_SSH1 {
case NET_SSH1_LOG_REALTIME_FILE:
if (!isset($this->realtime_log_file)) {
// PHP doesn't seem to like using constants in fopen()
$filename = NET_SSH2_LOG_REALTIME_FILE;
$filename = NET_SSH1_LOG_REALTIME_FILE;
$fp = fopen($filename, 'w');
$this->realtime_log_file = $fp;
}

@ -1,5 +1,4 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Pure-PHP implementation of SSHv2.
@ -144,19 +143,20 @@ define('NET_SSH2_LOG_MAX_SIZE', 1024 * 1024);
/**
* Pure-PHP implementation of SSHv2.
*
* @package Net_SSH2
* @author Jim Wigginton <terrafrost@php.net>
* @version 0.1.0
* @access public
* @package Net_SSH2
*/
class Net_SSH2 {
class Net_SSH2
{
/**
* The SSH identifier
*
* @var String
* @access private
*/
var $identifier = 'SSH-2.0-phpseclib_0.3';
var $identifier;
/**
* The Socket Object
@ -747,6 +747,30 @@ class Net_SSH2 {
*/
var $is_timeout = false;
/**
* Log Boundary
*
* @see Net_SSH2::_format_log
* @access private
*/
var $log_boundary = ':';
/**
* Log Long Width
*
* @see Net_SSH2::_format_log
* @access private
*/
var $log_long_width = 65;
/**
* Log Short Width
*
* @see Net_SSH2::_format_log
* @access private
*/
var $log_short_width = 16;
/**
* Default Constructor.
*
@ -763,15 +787,15 @@ class Net_SSH2 {
// Include Math_BigInteger
// Used to do Diffie-Hellman key exchange and DSA/RSA signature verification.
if (!class_exists('Math_BigInteger')) {
require_once('Math/BigInteger.php');
include_once 'Math/BigInteger.php';
}
if (!function_exists('crypt_random_string')) {
require_once('Crypt/Random.php');
include_once 'Crypt/Random.php';
}
if (!class_exists('Crypt_Hash')) {
require_once('Crypt/Hash.php');
include_once 'Crypt/Hash.php';
}
$this->last_packet = strtok(microtime(), ' ') + strtok(''); // == microtime(true) in PHP5
@ -861,7 +885,7 @@ class Net_SSH2 {
}
$read = array($this->fsock);
$write = $except = NULL;
$write = $except = null;
$sec = floor($timeout);
$usec = 1000000 * ($timeout - $sec);
@ -895,19 +919,7 @@ class Net_SSH2 {
return false;
}
$ext = array();
if (extension_loaded('mcrypt')) {
$ext[] = 'mcrypt';
}
if (extension_loaded('gmp')) {
$ext[] = 'gmp';
} else if (extension_loaded('bcmath')) {
$ext[] = 'bcmath';
}
if (!empty($ext)) {
$this->identifier.= ' (' . implode(', ', $ext) . ')';
}
$this->identifier = $this->_generate_identifier();
if (defined('NET_SSH2_LOGGING')) {
$this->_append_log('<-', $extra . $temp);
@ -944,6 +956,36 @@ class Net_SSH2 {
$this->bitmap = NET_SSH2_MASK_CONSTRUCTOR;
}
/**
* Generates the SSH identifier
*
* You should overwrite this method in your own class if you want to use another identifier
*
* @access protected
* @return String
*/
function _generate_identifier()
{
$identifier = 'SSH-2.0-phpseclib_0.3';
$ext = array();
if (extension_loaded('mcrypt')) {
$ext[] = 'mcrypt';
}
if (extension_loaded('gmp')) {
$ext[] = 'gmp';
} elseif (extension_loaded('bcmath')) {
$ext[] = 'bcmath';
}
if (!empty($ext)) {
$identifier .= ' (' . implode(', ', $ext) . ')';
}
return $identifier;
}
/**
* Key Exchange
*
@ -1260,7 +1302,8 @@ class Net_SSH2 {
-- http://tools.ietf.org/html/rfc4419#section-6.2 */
$one = new Math_BigInteger(1);
$keyLength = min($keyLength, $kexHash->getLength());
$max = $one->bitwise_leftShift(16 * $keyLength)->subtract($one); // 2 * 8 * $keyLength
$max = $one->bitwise_leftShift(16 * $keyLength); // 2 * 8 * $keyLength
$max = $max->subtract($one);
$x = $one->random($one, $max);
$e = $g->modPow($x, $prime);
@ -1353,14 +1396,14 @@ class Net_SSH2 {
switch ($encrypt) {
case '3des-cbc':
if (!class_exists('Crypt_TripleDES')) {
require_once('Crypt/TripleDES.php');
include_once 'Crypt/TripleDES.php';
}
$this->encrypt = new Crypt_TripleDES();
// $this->encrypt_block_size = 64 / 8 == the default
break;
case '3des-ctr':
if (!class_exists('Crypt_TripleDES')) {
require_once('Crypt/TripleDES.php');
include_once 'Crypt/TripleDES.php';
}
$this->encrypt = new Crypt_TripleDES(CRYPT_DES_MODE_CTR);
// $this->encrypt_block_size = 64 / 8 == the default
@ -1369,7 +1412,7 @@ class Net_SSH2 {
case 'aes192-cbc':
case 'aes128-cbc':
if (!class_exists('Crypt_Rijndael')) {
require_once('Crypt/Rijndael.php');
include_once 'Crypt/Rijndael.php';
}
$this->encrypt = new Crypt_Rijndael();
$this->encrypt_block_size = 16; // eg. 128 / 8
@ -1378,21 +1421,21 @@ class Net_SSH2 {
case 'aes192-ctr':
case 'aes128-ctr':
if (!class_exists('Crypt_Rijndael')) {
require_once('Crypt/Rijndael.php');
include_once 'Crypt/Rijndael.php';
}
$this->encrypt = new Crypt_Rijndael(CRYPT_RIJNDAEL_MODE_CTR);
$this->encrypt_block_size = 16; // eg. 128 / 8
break;
case 'blowfish-cbc':
if (!class_exists('Crypt_Blowfish')) {
require_once('Crypt/Blowfish.php');
include_once 'Crypt/Blowfish.php';
}
$this->encrypt = new Crypt_Blowfish();
$this->encrypt_block_size = 8;
break;
case 'blowfish-ctr':
if (!class_exists('Crypt_Blowfish')) {
require_once('Crypt/Blowfish.php');
include_once 'Crypt/Blowfish.php';
}
$this->encrypt = new Crypt_Blowfish(CRYPT_BLOWFISH_MODE_CTR);
$this->encrypt_block_size = 8;
@ -1402,7 +1445,7 @@ class Net_SSH2 {
case 'twofish256-cbc':
case 'twofish-cbc':
if (!class_exists('Crypt_Twofish')) {
require_once('Crypt/Twofish.php');
include_once 'Crypt/Twofish.php';
}
$this->encrypt = new Crypt_Twofish();
$this->encrypt_block_size = 16;
@ -1411,7 +1454,7 @@ class Net_SSH2 {
case 'twofish192-ctr':
case 'twofish256-ctr':
if (!class_exists('Crypt_Twofish')) {
require_once('Crypt/Twofish.php');
include_once 'Crypt/Twofish.php';
}
$this->encrypt = new Crypt_Twofish(CRYPT_TWOFISH_MODE_CTR);
$this->encrypt_block_size = 16;
@ -1420,7 +1463,7 @@ class Net_SSH2 {
case 'arcfour128':
case 'arcfour256':
if (!class_exists('Crypt_RC4')) {
require_once('Crypt/RC4.php');
include_once 'Crypt/RC4.php';
}
$this->encrypt = new Crypt_RC4();
break;
@ -1431,13 +1474,13 @@ class Net_SSH2 {
switch ($decrypt) {
case '3des-cbc':
if (!class_exists('Crypt_TripleDES')) {
require_once('Crypt/TripleDES.php');
include_once 'Crypt/TripleDES.php';
}
$this->decrypt = new Crypt_TripleDES();
break;
case '3des-ctr':
if (!class_exists('Crypt_TripleDES')) {
require_once('Crypt/TripleDES.php');
include_once 'Crypt/TripleDES.php';
}
$this->decrypt = new Crypt_TripleDES(CRYPT_DES_MODE_CTR);
break;
@ -1445,7 +1488,7 @@ class Net_SSH2 {
case 'aes192-cbc':
case 'aes128-cbc':
if (!class_exists('Crypt_Rijndael')) {
require_once('Crypt/Rijndael.php');
include_once 'Crypt/Rijndael.php';
}
$this->decrypt = new Crypt_Rijndael();
$this->decrypt_block_size = 16;
@ -1454,21 +1497,21 @@ class Net_SSH2 {
case 'aes192-ctr':
case 'aes128-ctr':
if (!class_exists('Crypt_Rijndael')) {
require_once('Crypt/Rijndael.php');
include_once 'Crypt/Rijndael.php';
}
$this->decrypt = new Crypt_Rijndael(CRYPT_RIJNDAEL_MODE_CTR);
$this->decrypt_block_size = 16;
break;
case 'blowfish-cbc':
if (!class_exists('Crypt_Blowfish')) {
require_once('Crypt/Blowfish.php');
include_once 'Crypt/Blowfish.php';
}
$this->decrypt = new Crypt_Blowfish();
$this->decrypt_block_size = 8;
break;
case 'blowfish-ctr':
if (!class_exists('Crypt_Blowfish')) {
require_once('Crypt/Blowfish.php');
include_once 'Crypt/Blowfish.php';
}
$this->decrypt = new Crypt_Blowfish(CRYPT_BLOWFISH_MODE_CTR);
$this->decrypt_block_size = 8;
@ -1478,7 +1521,7 @@ class Net_SSH2 {
case 'twofish256-cbc':
case 'twofish-cbc':
if (!class_exists('Crypt_Twofish')) {
require_once('Crypt/Twofish.php');
include_once 'Crypt/Twofish.php';
}
$this->decrypt = new Crypt_Twofish();
$this->decrypt_block_size = 16;
@ -1487,7 +1530,7 @@ class Net_SSH2 {
case 'twofish192-ctr':
case 'twofish256-ctr':
if (!class_exists('Crypt_Twofish')) {
require_once('Crypt/Twofish.php');
include_once 'Crypt/Twofish.php';
}
$this->decrypt = new Crypt_Twofish(CRYPT_TWOFISH_MODE_CTR);
$this->decrypt_block_size = 16;
@ -1496,7 +1539,7 @@ class Net_SSH2 {
case 'arcfour128':
case 'arcfour256':
if (!class_exists('Crypt_RC4')) {
require_once('Crypt/RC4.php');
include_once 'Crypt/RC4.php';
}
$this->decrypt = new Crypt_RC4();
break;
@ -1647,10 +1690,26 @@ class Net_SSH2 {
* @param Mixed $password
* @param Mixed $...
* @return Boolean
* @see _login_helper
* @see _login
* @access public
*/
function login($username)
{
$args = func_get_args();
return call_user_func_array(array(&$this, '_login'), $args);
}
/**
* Login Helper
*
* @param String $username
* @param Mixed $password
* @param Mixed $...
* @return Boolean
* @see _login_helper
* @access private
*/
function _login($username)
{
$args = array_slice(func_get_args(), 1);
if (empty($args)) {
@ -1710,8 +1769,13 @@ class Net_SSH2 {
}
// although PHP5's get_class() preserves the case, PHP4's does not
if (is_object($password) && strtolower(get_class($password)) == 'crypt_rsa') {
if (is_object($password)) {
switch (strtolower(get_class($password))) {
case 'crypt_rsa':
return $this->_privatekey_login($username, $password);
case 'system_ssh_agent':
return $this->_ssh_agent_login($username, $password);
}
}
if (is_array($password)) {
@ -1757,7 +1821,7 @@ class Net_SSH2 {
// remove the username and password from the logged packet
if (!defined('NET_SSH2_LOGGING')) {
$logged = NULL;
$logged = null;
} else {
$logged = pack('CNa*Na*Na*CNa*',
NET_SSH2_MSG_USERAUTH_REQUEST, strlen('username'), 'username', strlen('ssh-connection'), 'ssh-connection',
@ -1948,6 +2012,26 @@ class Net_SSH2 {
return false;
}
/**
* Login with an ssh-agent provided key
*
* @param String $username
* @param System_SSH_Agent $agent
* @return Boolean
* @access private
*/
function _ssh_agent_login($username, $agent)
{
$keys = $agent->requestIdentities();
foreach ($keys as $key) {
if ($this->_privatekey_login($username, $key)) {
return true;
}
}
return false;
}
/**
* Login with an RSA private key
*
@ -2075,7 +2159,7 @@ class Net_SSH2 {
* @return String
* @access public
*/
function exec($command, $callback = NULL)
function exec($command, $callback = null)
{
$this->curTimeout = $this->timeout;
$this->is_timeout = false;
@ -2349,7 +2433,6 @@ class Net_SSH2 {
return false;
}
$channel = $this->in_request_pty_exec ? NET_SSH2_CHANNEL_EXEC : NET_SSH2_CHANNEL_SHELL;
return $this->_send_channel_packet($this->_get_interactive_channel(), $cmd);
}
@ -2430,8 +2513,7 @@ class Net_SSH2 {
*/
function reset()
{
$channel = $this->in_request_pty_exec ? NET_SSH2_CHANNEL_EXEC : NET_SSH2_CHANNEL_SHELL;
$this->_close_channel($channel);
$this->_close_channel($this->_get_interactive_channel());
}
/**
@ -2724,7 +2806,7 @@ class Net_SSH2 {
}
$read = array($this->fsock);
$write = $except = NULL;
$write = $except = null;
$start = strtok(microtime(), ' ') + strtok(''); // http://php.net/microtime#61838
$sec = floor($this->curTimeout);
@ -2861,12 +2943,9 @@ class Net_SSH2 {
case 'exit-status':
extract(unpack('Cfalse/Nexit_status', $this->_string_shift($response, 5)));
$this->exit_status = $exit_status;
// "The channel needs to be closed with SSH_MSG_CHANNEL_CLOSE after this message."
// -- http://tools.ietf.org/html/rfc4254#section-6.10
$this->_send_binary_packet(pack('CN', NET_SSH2_MSG_CHANNEL_EOF, $this->server_channels[$client_channel]));
$this->_send_binary_packet(pack('CN', NET_SSH2_MSG_CHANNEL_CLOSE, $this->server_channels[$channel]));
$this->channel_status[$channel] = NET_SSH2_MSG_CHANNEL_EOF;
// "The client MAY ignore these messages."
// -- http://tools.ietf.org/html/rfc4254#section-6.10
break;
default:
@ -2907,7 +2986,7 @@ class Net_SSH2 {
* @return Boolean
* @access private
*/
function _send_binary_packet($data, $logged = NULL)
function _send_binary_packet($data, $logged = null)
{
if (!is_resource($this->fsock) || feof($this->fsock)) {
user_error('Connection closed prematurely');
@ -3103,16 +3182,19 @@ class Net_SSH2 {
* for SCP more than anything.
*
* @param Integer $client_channel
* @param Boolean $want_reply
* @return Boolean
* @access private
*/
function _close_channel($client_channel)
function _close_channel($client_channel, $want_reply = false)
{
// see http://tools.ietf.org/html/rfc4254#section-5.3
$this->_send_binary_packet(pack('CN', NET_SSH2_MSG_CHANNEL_EOF, $this->server_channels[$client_channel]));
if (!$want_reply) {
$this->_send_binary_packet(pack('CN', NET_SSH2_MSG_CHANNEL_CLOSE, $this->server_channels[$client_channel]));
}
$this->channel_status[$client_channel] = NET_SSH2_MSG_CHANNEL_CLOSE;
@ -3120,6 +3202,10 @@ class Net_SSH2 {
while (!is_bool($this->_get_channel_packet($client_channel)));
if ($want_reply) {
$this->_send_binary_packet(pack('CN', NET_SSH2_MSG_CHANNEL_CLOSE, $this->server_channels[$client_channel]));
}
if ($this->bitmap & NET_SSH2_MASK_SHELL) {
$this->bitmap&= ~NET_SSH2_MASK_SHELL;
}
@ -3220,8 +3306,6 @@ class Net_SSH2 {
*/
function _format_log($message_log, $message_number_log)
{
static $boundary = ':', $long_width = 65, $short_width = 16;
$output = '';
for ($i = 0; $i < count($message_log); $i++) {
$output.= $message_number_log[$i] . "\r\n";
@ -3231,19 +3315,13 @@ class Net_SSH2 {
if (strlen($current_log)) {
$output.= str_pad(dechex($j), 7, '0', STR_PAD_LEFT) . '0 ';
}
$fragment = $this->_string_shift($current_log, $short_width);
$hex = substr(
preg_replace(
'#(.)#es',
'"' . $boundary . '" . str_pad(dechex(ord(substr("\\1", -1))), 2, "0", STR_PAD_LEFT)',
$fragment),
strlen($boundary)
);
$fragment = $this->_string_shift($current_log, $this->log_short_width);
$hex = substr(preg_replace_callback('#.#s', array($this, '_format_log_helper'), $fragment), strlen($this->log_boundary));
// replace non ASCII printable characters with dots
// http://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters
// also replace < with a . since < messes up the output on web browsers
$raw = preg_replace('#[^\x20-\x7E]|<#', '.', $fragment);
$output.= str_pad($hex, $long_width - $short_width, ' ') . $raw . "\r\n";
$output.= str_pad($hex, $this->log_long_width - $this->log_short_width, ' ') . $raw . "\r\n";
$j++;
} while (strlen($current_log));
$output.= "\r\n";
@ -3252,6 +3330,20 @@ class Net_SSH2 {
return $output;
}
/**
* Helper function for _format_log
*
* For use with preg_replace_callback()
*
* @param Array $matches
* @access private
* @return String
*/
function _format_log_helper($matches)
{
return $this->log_boundary . str_pad(dechex(ord($matches[0])), 2, '0', STR_PAD_LEFT);
}
/**
* Returns all errors
*
@ -3505,7 +3597,7 @@ class Net_SSH2 {
$signature = $this->_string_shift($signature, $temp['length']);
if (!class_exists('Crypt_RSA')) {
require_once('Crypt/RSA.php');
include_once 'Crypt/RSA.php';
}
$rsa = new Crypt_RSA();
@ -3572,7 +3664,15 @@ class Net_SSH2 {
*/
function _is_includable($suffix)
{
foreach (explode(PATH_SEPARATOR, get_include_path()) as $prefix) {
// stream_resolve_include_path was introduced in PHP 5.3.2
if (function_exists('stream_resolve_include_path')) {
return stream_resolve_include_path($suffix) !== false;
}
$paths = PATH_SEPARATOR == ':' ?
preg_split('#(?<!phar):#', get_include_path()) :
explode(PATH_SEPARATOR, get_include_path());
foreach ($paths as $prefix) {
$ds = substr($prefix, -1) == DIRECTORY_SEPARATOR ? '' : DIRECTORY_SEPARATOR;
$file = $prefix . $ds . $suffix;

@ -0,0 +1,315 @@
<?php
/**
* Pure-PHP ssh-agent client.
*
* PHP versions 4 and 5
*
* Here are some examples of how to use this library:
* <code>
* <?php
* include('System/SSH_Agent.php');
* include('Net/SSH2.php');
*
* $agent = new System_SSH_Agent();
*
* $ssh = new Net_SSH2('www.domain.tld');
* if (!$ssh->login('username', $agent)) {
* exit('Login Failed');
* }
*
* echo $ssh->exec('pwd');
* echo $ssh->exec('ls -la');
* ?>
* </code>
*
* LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @category System
* @package System_SSH_Agent
* @author Jim Wigginton <terrafrost@php.net>
* @copyright MMXIV Jim Wigginton
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link http://phpseclib.sourceforge.net
* @internal See http://api.libssh.org/rfc/PROTOCOL.agent
*/
/**#@+
* Message numbers
*
* @access private
*/
// to request SSH1 keys you have to use SSH_AGENTC_REQUEST_RSA_IDENTITIES (1)
define('SYSTEM_SSH_AGENTC_REQUEST_IDENTITIES', 11);
// this is the SSH2 response; the SSH1 response is SSH_AGENT_RSA_IDENTITIES_ANSWER (2).
define('SYSTEM_SSH_AGENT_IDENTITIES_ANSWER', 12);
define('SYSTEM_SSH_AGENT_FAILURE', 5);
// the SSH1 request is SSH_AGENTC_RSA_CHALLENGE (3)
define('SYSTEM_SSH_AGENTC_SIGN_REQUEST', 13);
// the SSH1 response is SSH_AGENT_RSA_RESPONSE (4)
define('SYSTEM_SSH_AGENT_SIGN_RESPONSE', 14);
/**#@-*/
/**
* Pure-PHP ssh-agent client identity object
*
* Instantiation should only be performed by System_SSH_Agent class.
* This could be thought of as implementing an interface that Crypt_RSA
* implements. ie. maybe a Net_SSH_Auth_PublicKey interface or something.
* The methods in this interface would be getPublicKey, setSignatureMode
* and sign since those are the methods phpseclib looks for to perform
* public key authentication.
*
* @package System_SSH_Agent
* @author Jim Wigginton <terrafrost@php.net>
* @version 0.1.0
* @access internal
*/
class System_SSH_Agent_Identity
{
/**
* Key Object
*
* @var Crypt_RSA
* @access private
* @see System_SSH_Agent_Identity::getPublicKey()
*/
var $key;
/**
* Key Blob
*
* @var String
* @access private
* @see System_SSH_Agent_Identity::sign()
*/
var $key_blob;
/**
* Socket Resource
*
* @var Resource
* @access private
* @see System_SSH_Agent_Identity::sign()
*/
var $fsock;
/**
* Default Constructor.
*
* @param Resource $fsock
* @return System_SSH_Agent_Identity
* @access private
*/
function System_SSH_Agent_Identity($fsock)
{
$this->fsock = $fsock;
}
/**
* Set Public Key
*
* Called by System_SSH_Agent::requestIdentities()
*
* @param Crypt_RSA $key
* @access private
*/
function setPublicKey($key)
{
$this->key = $key;
$this->key->setPublicKey();
}
/**
* Set Public Key
*
* Called by System_SSH_Agent::requestIdentities(). The key blob could be extracted from $this->key
* but this saves a small amount of computation.
*
* @param String $key_blob
* @access private
*/
function setPublicKeyBlob($key_blob)
{
$this->key_blob = $key_blob;
}
/**
* Get Public Key
*
* Wrapper for $this->key->getPublicKey()
*
* @param Integer $format optional
* @return Mixed
* @access public
*/
function getPublicKey($format = null)
{
return !isset($format) ? $this->key->getPublicKey() : $this->key->getPublicKey($format);
}
/**
* Set Signature Mode
*
* Doesn't do anything as ssh-agent doesn't let you pick and choose the signature mode. ie.
* ssh-agent's only supported mode is CRYPT_RSA_SIGNATURE_PKCS1
*
* @param Integer $mode
* @access public
*/
function setSignatureMode($mode)
{
}
/**
* Create a signature
*
* See "2.6.2 Protocol 2 private key signature request"
*
* @param String $message
* @return String
* @access public
*/
function sign($message)
{
// the last parameter (currently 0) is for flags and ssh-agent only defines one flag (for ssh-dss): SSH_AGENT_OLD_SIGNATURE
$packet = pack('CNa*Na*N', SYSTEM_SSH_AGENTC_SIGN_REQUEST, strlen($this->key_blob), $this->key_blob, strlen($message), $message, 0);
$packet = pack('Na*', strlen($packet), $packet);
if (strlen($packet) != fputs($this->fsock, $packet)) {
user_error('Connection closed during signing');
}
$length = current(unpack('N', fread($this->fsock, 4)));
$type = ord(fread($this->fsock, 1));
if ($type != SYSTEM_SSH_AGENT_SIGN_RESPONSE) {
user_error('Unable to retreive signature');
}
$signature_blob = fread($this->fsock, $length - 1);
// the only other signature format defined - ssh-dss - is the same length as ssh-rsa
// the + 12 is for the other various SSH added length fields
return substr($signature_blob, strlen('ssh-rsa') + 12);
}
}
/**
* Pure-PHP ssh-agent client identity factory
*
* requestIdentities() method pumps out System_SSH_Agent_Identity objects
*
* @package System_SSH_Agent
* @author Jim Wigginton <terrafrost@php.net>
* @version 0.1.0
* @access internal
*/
class System_SSH_Agent
{
/**
* Socket Resource
*
* @var Resource
* @access private
*/
var $fsock;
/**
* Default Constructor
*
* @return System_SSH_Agent
* @access public
*/
function System_SSH_Agent()
{
switch (true) {
case isset($_SERVER['SSH_AUTH_SOCK']):
$address = $_SERVER['SSH_AUTH_SOCK'];
break;
case isset($_ENV['SSH_AUTH_SOCK']):
$address = $_ENV['SSH_AUTH_SOCK'];
break;
default:
user_error('SSH_AUTH_SOCK not found');
return false;
}
$this->fsock = fsockopen('unix://' . $address, 0, $errno, $errstr);
if (!$this->fsock) {
user_error("Unable to connect to ssh-agent (Error $errno: $errstr)");
}
}
/**
* Request Identities
*
* See "2.5.2 Requesting a list of protocol 2 keys"
* Returns an array containing zero or more System_SSH_Agent_Identity objects
*
* @return Array
* @access public
*/
function requestIdentities()
{
if (!$this->fsock) {
return array();
}
$packet = pack('NC', 1, SYSTEM_SSH_AGENTC_REQUEST_IDENTITIES);
if (strlen($packet) != fputs($this->fsock, $packet)) {
user_error('Connection closed while requesting identities');
}
$length = current(unpack('N', fread($this->fsock, 4)));
$type = ord(fread($this->fsock, 1));
if ($type != SYSTEM_SSH_AGENT_IDENTITIES_ANSWER) {
user_error('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);
$length = current(unpack('N', fread($this->fsock, 4)));
$key_comment = 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':
if (!class_exists('Crypt_RSA')) {
include_once 'Crypt/RSA.php';
}
$key = new Crypt_RSA();
$key->loadKey('ssh-rsa ' . base64_encode($key_blob) . ' ' . $key_comment);
break;
case 'ssh-dss':
// not currently supported
break;
}
// resources are passed by reference by default
if (isset($key)) {
$identity = new System_SSH_Agent_Identity($this->fsock);
$identity->setPublicKey($key);
$identity->setPublicKeyBlob($key_blob);
$identities[] = $identity;
unset($key);
}
}
return $identities;
}
}
Loading…
Cancel
Save