Source for file rfc3414.php

Documentation is available at rfc3414.php

  1. <?php
  2. /**
  3. * phpsnmp - a PHP SNMP library
  4. *
  5. * Copyright (C) 2004 David Eder <david@eder,us>
  6. *
  7. * Based on snmp - a Python SNMP library
  8. * Copyright (C) 2003 Unicity Pty Ltd <libsnmp@unicity.com.au>
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. * @author David Eder <david@eder.us>
  25. * @copyright 2004 David Eder
  26. * @package phpSNMP
  27. * @subpackage rfc3414
  28. * @version .7
  29. */
  30.  
  31. /**
  32. */
  33.  
  34. require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'rfc1155.php');
  35. require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'rfc3411.php');
  36.  
  37. define('USM_AUTH_KEY_LEN', 12);
  38. define('USM_SALT_LEN', 8);
  39.  
  40. /**
  41. * User-based Security Model (USM)
  42. *
  43. * @package phpSNMP
  44. * @subpackage rfc3414
  45. */
  46. class rfc3414_USM extends rfc1155_Sequence
  47. {
  48. var $auth_password = '';
  49. var $priv_password = '';
  50. var $hash_function = 'md5';
  51. var $crypt_algorithm = 'des';
  52. var $crypt_mode = 'cbc';
  53.  
  54. /**
  55. * Constructor
  56. *
  57. * @param string $engine_id
  58. * @param integer $engine_boots
  59. * @param integer $engine_time
  60. * @param string $user
  61. * @param string $auth MD5 or SHA hash sum
  62. * @param string $priv DES salt
  63. */
  64. function rfc3414_USM($engine_id='', $engine_boots=0, $engine_time=0, $user='', $auth='', $priv='')
  65. {
  66. parent::rfc1155_Sequence();
  67. if(strlen($user) > 32)
  68. trigger_error('user must be at most 32 characters', E_USER_WARNING);
  69. $this->value = array(new rfc3411_EngineID($engine_id), new rfc1155_Integer($engine_boots),
  70. new rfc1155_Integer($engine_time), new rfc1155_OctetString($user),
  71. new rfc1155_OctetString($auth), new rfc1155_OctetString($priv));
  72. }
  73.  
  74. /**
  75. * Get/Set engine ID
  76. *
  77. * @param string $value
  78. * @return string
  79. */
  80. function engineID($value=NULL)
  81. {
  82. if(!is_null($value)) $this->value[0]->value = $value;
  83. return $this->value[0]->value;
  84. }
  85.  
  86. /**
  87. * Get/Set engine boots
  88. *
  89. * @param integer $value
  90. * @return integer
  91. */
  92. function engineBoots($value=NULL)
  93. {
  94. if(!is_null($value)) $this->value[1]->value = $value;
  95. return $this->value[1]->value;
  96. }
  97.  
  98. /**
  99. * Get/Set engine time
  100. *
  101. * @param integer $value
  102. * @return integer
  103. */
  104. function engineTime($value=NULL)
  105. {
  106. if(!is_null($value)) $this->value[2]->value = $value;
  107. return $this->value[2]->value;
  108. }
  109.  
  110. /**
  111. * Get/Set usm user
  112. *
  113. * @param string $value
  114. * @return string
  115. */
  116. function user($value=NULL)
  117. {
  118. if(!is_null($value)) $this->value[3]->value = $value;
  119. return $this->value[3]->value;
  120. }
  121.  
  122. /**
  123. * Get/Set auth parameters
  124. *
  125. * @param string $value
  126. * @return string
  127. */
  128. function auth($value=NULL)
  129. {
  130. if(!is_null($value)) $this->value[4]->value = substr($value, 0, USM_AUTH_KEY_LEN);
  131. return $this->value[4]->value;
  132. }
  133.  
  134. /**
  135. * Get/Set priv parameters
  136. *
  137. * @param string $value - a value of 'salt' generates a new priv parameter
  138. * @return string
  139. */
  140. function priv($value=NULL)
  141. {
  142. static $salt = NULL;
  143.  
  144. if(!is_null($value))
  145. {
  146. if($value == 'salt')
  147. {
  148. if(is_null($salt)) for($i = 0; $i < USM_SALT_LEN; $i++) $salt .= chr(rand(0, 255));
  149.  
  150. $i = USM_SALT_LEN - 1;
  151. while($i)
  152. {
  153. if($salt{$i} == chr(255))
  154. {
  155. $salt{$i} = chr(0);
  156. $i--;
  157. }
  158. else
  159. {
  160. $salt{$i} = chr(ord($salt{$i}) + 1);
  161. $i = 0;
  162. }
  163. }
  164. $this->value[5]->value = $salt;
  165. }
  166. else
  167. $this->value[5]->value = $value;
  168. }
  169. return $this->value[5]->value;
  170. }
  171.  
  172. /**
  173. * Decode Stream
  174. *
  175. * decode() an octet stream into a sequence of Asn1Objects
  176. *
  177. * @param string $stream
  178. * @return rfc3411_USM
  179. */
  180. function decode($stream)
  181. {
  182. $this->value = parent::decode($stream);
  183. if(count($this->value) != 1)
  184. trigger_error('Malformed Message: More than one object decoded.', E_USER_WARNING);
  185. $this->value = $this->value[0]->value;
  186. if(count($this->value) != 6)
  187. trigger_error('Malformed Message: Incorrect sequence length ' . count($this->value), E_USER_WARNING);
  188. return $this;
  189. }
  190.  
  191. /**
  192. * Generate a key
  193. *
  194. * @param string $password - 'auth' for auth_password, 'priv' for priv_password, anything else will be treated as a password
  195. * @return string key
  196. */
  197. function generate_key($password)
  198. {
  199. if($password == 'auth')
  200. $password = $this->auth_password;
  201. elseif($password == 'priv')
  202. $password = $this->priv_password;
  203.  
  204. $hashfn = $this->hash_function;
  205. $key = substr(str_repeat($password, ceil(1048576 / strlen($password))), 0, 1048576);
  206. $key = pack('H*', $hashfn($key));
  207. return pack('H*', $hashfn($key . $this->engineID() . $key));
  208. }
  209.  
  210. /**
  211. * Generate initialization vector for DES
  212. *
  213. * @param string $key
  214. * @return string iv
  215. */
  216. function generate_iv($key=NULL)
  217. {
  218. if(is_null($key)) $key = $this->generate_key('priv');
  219. $salt = $this->priv();
  220. return substr($key, strlen($key) - strlen($salt)) ^ $salt;
  221. }
  222.  
  223. /**
  224. * Encrypt using crypt_algorithm and crypt_mode
  225. *
  226. * @param string $data
  227. * @return string
  228. */
  229. function encrypt($data)
  230. {
  231. if(!(function_exists('mcrypt_module_open') && function_exists('mcrypt_generic')))
  232. {
  233. trigger_error('Mcrypt must be installed', E_USER_WARNING);
  234. return $data;
  235. }
  236. $key = $this->generate_key('priv');
  237. $this->priv('salt');
  238. $iv = $this->generate_iv($key);
  239. $td = mcrypt_module_open($this->crypt_algorithm, '', $this->crypt_mode, '');
  240. $ks = mcrypt_enc_get_key_size($td);
  241. $key = substr($key, 0, $ks);
  242. mcrypt_generic_init($td, $key, $iv);
  243. $ret = mcrypt_generic($td, $data);
  244. mcrypt_generic_deinit($td);
  245. mcrypt_module_close($td);
  246. return $ret;
  247. }
  248.  
  249. /**
  250. * Decrypt using crypt_algorithm and crypt_mode
  251. *
  252. * @param string $data
  253. * @return string
  254. */
  255. function decrypt($data)
  256. {
  257. if(!(function_exists('mcrypt_module_open') && function_exists('mdecrypt_generic')))
  258. {
  259. trigger_error('Mcrypt must be installed', E_USER_WARNING);
  260. return $data;
  261. }
  262. $key = $this->generate_key('priv');
  263. $iv = $this->generate_iv($key);
  264. $td = mcrypt_module_open($this->crypt_algorithm, '', $this->crypt_mode, '');
  265. $ks = mcrypt_enc_get_key_size($td);
  266. $key = substr($key, 0, $ks);
  267. mcrypt_generic_init($td, $key, $iv);
  268. $ret = mdecrypt_generic($td, $data);
  269. mcrypt_generic_deinit($td);
  270. mcrypt_module_close($td);
  271. return $ret;
  272. }
  273. }
  274. ?>

Documentation generated on Mon, 14 Nov 2005 17:55:46 -0700 by phpDocumentor 1.3.0RC3