Skip to content
Snippets Groups Projects
Commit 99c32f36 authored by Peter Rotich's avatar Peter Rotich
Browse files

Move getCipher to base class

Add cipher validator - checks to make sure the cipher is valid & available.
parent 69440c8b
Branches
Tags
No related merge requests found
...@@ -201,6 +201,8 @@ class CryptoAlgo { ...@@ -201,6 +201,8 @@ class CryptoAlgo {
var $tag_number; var $tag_number;
var $ciphers = null;
function CryptoAlgo($tag) { function CryptoAlgo($tag) {
$this->tag_number = $tag; $this->tag_number = $tag;
} }
...@@ -209,6 +211,30 @@ class CryptoAlgo { ...@@ -209,6 +211,30 @@ class CryptoAlgo {
return $this->tag_number; return $this->tag_number;
} }
function getCipher($cid, $callback=null) {
if(!$this->ciphers)
return null;
$cipher = null;
if($cid)
$cipher = isset($this->ciphers[$cid]) ? $this->ciphers[$cid] : null;
elseif($this->ciphers) { // search best available.
foreach($this->ciphers as $k => $c) {
if(!$callback
|| (is_callable($callback)
&& call_user_func($callback, $c))) {
$cid = $k;
$cipher = $c;
break;
}
}
}
return $cipher ?
array_merge($cipher, array('cid' => $cid)) : null;
}
function getMasterKey() { function getMasterKey() {
return $this->master_key; return $this->master_key;
} }
...@@ -248,6 +274,8 @@ class CryptoAlgo { ...@@ -248,6 +274,8 @@ class CryptoAlgo {
*/ */
/* abstract */ /* abstract */
function exists() { return false; } function exists() { return false; }
} }
...@@ -276,25 +304,17 @@ Class CryptoMcrypt extends CryptoAlgo { ...@@ -276,25 +304,17 @@ Class CryptoMcrypt extends CryptoAlgo {
); );
function getCipher($cid=null) { function getCipher($cid=null) {
return parent::getCipher($cid, array($this, '_checkCipher'));
}
$cipher = null; function _checkCipher($c) {
if($cid)
$cipher = isset($this->ciphers[$cid]) ? $this->ciphers[$cid] : null;
elseif($this->ciphers) { // search best available.
foreach($this->ciphers as $k => $c) {
if($c['name']
&& $c['mode']
&& mcrypt_module_open($c['name'], '', $c['mode'], '')) {
$cid = $k;
$cipher = $c;
break;
}
}
}
return $cipher ? return ($c
array_merge($cipher, array('cid' => $cid)) : null; && $c['name']
&& $c['mode']
&& $this->exists()
&& mcrypt_module_open($c['name'], '', $c['mode'], '')
);
} }
/** /**
...@@ -385,7 +405,8 @@ Class CryptoMcrypt extends CryptoAlgo { ...@@ -385,7 +405,8 @@ Class CryptoMcrypt extends CryptoAlgo {
} }
function exists() { function exists() {
return extension_loaded('mcrypt'); return (extension_loaded('mcrypt')
&& function_exists('mcrypt_module_open'));
} }
} }
...@@ -417,22 +438,16 @@ class CryptoOpenSSL extends CryptoAlgo { ...@@ -417,22 +438,16 @@ class CryptoOpenSSL extends CryptoAlgo {
} }
function getCipher($cid) { function getCipher($cid) {
return parent::getCipher($cid, array($this, '_checkCipher'));
}
$cipher = null; function _checkCipher($c) {
if($cid)
$cipher = isset($this->ciphers[$cid]) ? $this->ciphers[$cid] : null;
elseif($this->ciphers) { // search best available.
foreach($this->ciphers as $k => $c) {
if($c['method'] && openssl_cipher_iv_length($c['method'])) {
$cid = $k;
$cipher = $c;
break;
}
}
}
return $cipher ? return ($c
array_merge($cipher, array('cid' => $cid)) : null; && $c['method']
&& $this->exists()
&& openssl_cipher_iv_length($c['method'])
);
} }
/** /**
...@@ -515,7 +530,7 @@ define('CRYPTO_CIPHER_PHPSECLIB_AES_CBC', 1); ...@@ -515,7 +530,7 @@ define('CRYPTO_CIPHER_PHPSECLIB_AES_CBC', 1);
class CryptoPHPSecLib extends CryptoAlgo { class CryptoPHPSecLib extends CryptoAlgo {
var $ciphers = array( //Replace with interface class var $ciphers = array(
CRYPTO_CIPHER_PHPSECLIB_AES_CBC => array( CRYPTO_CIPHER_PHPSECLIB_AES_CBC => array(
'mode' => CRYPT_AES_MODE_CBC, 'mode' => CRYPT_AES_MODE_CBC,
'ivlen' => 16, 'ivlen' => 16,
...@@ -523,36 +538,31 @@ class CryptoPHPSecLib extends CryptoAlgo { ...@@ -523,36 +538,31 @@ class CryptoPHPSecLib extends CryptoAlgo {
), ),
); );
//TODO: Will be replaced by interface cryto class.. with default/preset
// ivlen + extends PHPSecLib crypto classes.
function getCipher($cid) {
if ($cid)
$cipher = $this->ciphers[$cid];
elseif($this->ciphers) {
foreach($this->ciphers as $k => $c) {
if($c['class'] && class_exists($c['class'])) {
$cid = $k;
$cipher = $c;
break;
}
}
}
return $cipher ?
array_merge($cipher, array('cid' => $cid)) : null;
}
function getCrypto($cid) { function getCrypto($cid) {
if(!$cid if(!$cid
|| !($c=$this->getCipher($cid)) || !($c=$this->getCipher($cid))
|| !($class=$c['class']) || !$this->_checkCipher($c))
|| !class_exists($class))
return null; return null;
$class = $c['class'];
return new $class($c['mode']); return new $class($c['mode']);
} }
function getCipher($cid) {
return parent::getCipher($cid, array($this, '_checkCipher'));
}
function _checkCipher($c) {
return ($c
&& $c['mode']
&& $c['ivlen']
&& $c['class']
&& class_exists($c['class']));
}
function encrypt($text, $cid=0) { function encrypt($text, $cid=0) {
if(!$this->exists() if(!$this->exists()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment