/usr/share/php/Horde/Secret.php is in php-horde-secret 2.0.6-3ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | <?php
/**
* Provides an API for encrypting and decrypting small pieces of data with the
* use of a shared key stored in a cookie.
*
* Copyright 1999-2016 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @author Chuck Hagenbuch <chuck@horde.org>
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @license http://www.horde.org/licenses/lgpl21 LGPL
* @package Secret
*/
class Horde_Secret
{
/** Generic, default keyname. */
const DEFAULT_KEY = 'generic';
/**
* Configuration parameters.
*
* @var array
*/
protected $_params = array(
'cookie_domain' => '',
'cookie_path' => '',
'cookie_ssl' => false,
'session_name' => 'horde_secret'
);
/**
* Cipher cache.
*
* @var array
*/
protected $_cipherCache = array();
/**
* Key cache.
*
* @var array
*/
protected $_keyCache = array();
/**
* Constructor.
*
* @param array $params Configuration parameters:
* - cookie_domain: (string) The cookie domain.
* - cookie_path: (string) The cookie path.
* - cookie_ssl: (boolean) Only transmit cookie securely?
* - session_name: (string) The cookie session name.
*/
public function __construct($params = array())
{
$this->_params = array_merge($this->_params, $params);
}
/**
* Take a small piece of data and encrypt it with a key.
*
* @param string $key The key to use for encryption.
* @param string $message The plaintext message.
*
* @return string The ciphertext message.
* @throws Horde_Secret_Exception
*/
public function write($key, $message)
{
$message = strval($message);
return (strlen($key) && strlen($message))
? $this->_getCipherOb($key)->encrypt($message)
: '';
}
/**
* Decrypt a message encrypted with write().
*
* @param string $key The key to use for decryption.
* @param string $message The ciphertext message.
*
* @return string The plaintext message.
* @throws Horde_Secret_Exception
*/
public function read($key, $ciphertext)
{
$ciphertext = strval($ciphertext);
return (strlen($key) && strlen($ciphertext))
? $this->_getCipherOb($key)->decrypt($ciphertext)
: '';
}
/**
* Returns the cached crypt object.
*
* @param string $key The key to use for [de|en]cryption. Only the first
* 56 bytes of this string is used.
*
* @return Horde_Crypt_Blowfish The crypt object.
* @throws Horde_Secret_Exception
*/
protected function _getCipherOb($key)
{
if (!is_string($key)) {
throw new Horde_Secret_Exception('Key must be a string', Horde_Secret_Exception::KEY_NOT_STRING);
}
if (!strlen($key)) {
throw new Horde_Secret_Exception('Key must be non-zero.', Horde_Secret_Exception::KEY_ZERO_LENGTH);
}
$key = substr($key, 0, 56);
$idx = hash('md5', $key);
if (!isset($this->_cipherCache[$idx])) {
$this->_cipherCache[$idx] = new Horde_Crypt_Blowfish($key);
}
return $this->_cipherCache[$idx];
}
/**
* Generate a secret key (for encryption), either using a random
* string and storing it in a cookie if the user has cookies
* enabled, or munging some known values if they don't.
*
* @param string $keyname The name of the key to set.
*
* @return string The secret key that has been generated.
*/
public function setKey($keyname = self::DEFAULT_KEY)
{
$set = true;
if (isset($_COOKIE[$this->_params['session_name']])) {
if (isset($_COOKIE[$keyname . '_key'])) {
$key = $_COOKIE[$keyname . '_key'];
$set = false;
} else {
$key = $_COOKIE[$keyname . '_key'] = strval(new Horde_Support_Randomid());
}
} else {
$key = session_id();
}
if ($set) {
$this->_setCookie($keyname, $key);
}
return $key;
}
/**
* Return a secret key, either from a cookie, or if the cookie
* isn't there, assume we are using a munged version of a known
* base value.
*
* @param string $keyname The name of the key to get.
*
* @return string The secret key.
*/
public function getKey($keyname = self::DEFAULT_KEY)
{
if (!isset($this->_keyCache[$keyname])) {
if (isset($_COOKIE[$keyname . '_key'])) {
$key = $_COOKIE[$keyname . '_key'];
} else {
$key = session_id();
$this->_setCookie($keyname, $key);
}
$this->_keyCache[$keyname] = $key;
}
return $this->_keyCache[$keyname];
}
/**
* Clears a secret key entry from the current cookie.
*
* @param string $keyname The name of the key to clear.
*
* @return boolean True if key existed, false if not.
*/
public function clearKey($keyname = self::DEFAULT_KEY)
{
if (isset($_COOKIE[$this->_params['session_name']]) &&
isset($_COOKIE[$keyname . '_key'])) {
$this->_setCookie($keyname, false);
return true;
}
return false;
}
/**
* Sets the cookie with the given keyname/key.
*
* @param string $keyname The name of the key to set.
* @param string $key The key to use for encryption.
*/
protected function _setCookie($keyname, $key)
{
@setcookie(
$keyname . '_key',
$key,
0,
$this->_params['cookie_path'],
$this->_params['cookie_domain'],
$this->_params['cookie_ssl'],
true
);
if ($key === false) {
unset($_COOKIE[$keyname . '_key'], $this->_keyCache[$keyname]);
} else {
$_COOKIE[$keyname . '_key'] = $this->_keyCache[$keyname] = $key;
}
}
}
|