usm_v3.h 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  1. /*_############################################################################
  2. _##
  3. _## usm_v3.h
  4. _##
  5. _## SNMP++v3.2.23
  6. _## -----------------------------------------------
  7. _## Copyright (c) 2001-2007 Jochen Katz, Frank Fock
  8. _##
  9. _## This software is based on SNMP++2.6 from Hewlett Packard:
  10. _##
  11. _## Copyright (c) 1996
  12. _## Hewlett-Packard Company
  13. _##
  14. _## ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
  15. _## Permission to use, copy, modify, distribute and/or sell this software
  16. _## and/or its documentation is hereby granted without fee. User agrees
  17. _## to display the above copyright notice and this license notice in all
  18. _## copies of the software and any documentation of the software. User
  19. _## agrees to assume all liability for the use of the software;
  20. _## Hewlett-Packard and Jochen Katz make no representations about the
  21. _## suitability of this software for any purpose. It is provided
  22. _## "AS-IS" without warranty of any kind, either express or implied. User
  23. _## hereby grants a royalty-free license to any and all derivatives based
  24. _## upon this software code base.
  25. _##
  26. _## Stuttgart, Germany, Sun Nov 11 15:10:59 CET 2007
  27. _##
  28. _##########################################################################*/
  29. // $Id: usm_v3.h 287 2007-03-22 22:37:09Z katz $
  30. #ifndef _USM_V3
  31. #define _USM_V3
  32. #include "snmp_pp/config_snmp_pp.h"
  33. #ifdef _SNMPv3
  34. #include "snmp_pp/smi.h"
  35. #include "snmp_pp/octet.h"
  36. #include "snmp_pp/address.h"
  37. #ifdef SNMP_PP_NAMESPACE
  38. namespace Snmp_pp {
  39. #endif
  40. #define MAXUINT32 4294967295u
  41. // the maximum allowed length of the username
  42. #define MAXLEN_USMUSERNAME 32
  43. #define MAXLEN_USMSECURITYNAME MAXLEN_USMUSERNAME
  44. #define SNMPv3_AUTHFLAG 0x01
  45. #define SNMPv3_PRIVFLAG 0x02
  46. #define SNMPv3_REPORTABLEFLAG 0x04
  47. #define NOKEY 0
  48. #define AUTHKEY 1
  49. #define PRIVKEY 2
  50. #define OWNAUTHKEY 3
  51. #define OWNPRIVKEY 4
  52. /** @name SecurityLevels
  53. *
  54. * When sending a SNMPv3 message, one of these security levels can be
  55. * set on the Pdu object.
  56. */
  57. //@{
  58. #define SNMP_SECURITY_LEVEL_NOAUTH_NOPRIV 1 ///< noAuthNoPriv
  59. #define SNMP_SECURITY_LEVEL_AUTH_NOPRIV 2 ///< authNoPriv
  60. #define SNMP_SECURITY_LEVEL_AUTH_PRIV 3 ///< authPriv
  61. //@}
  62. /** @name AuthProtocols
  63. *
  64. * Each user of the USM must use one authentication protocol (which
  65. * may be none.
  66. */
  67. //@{
  68. #define SNMP_AUTHPROTOCOL_NONE 1 ///< None
  69. #define SNMP_AUTHPROTOCOL_HMACMD5 2 ///< HMAC-MD5
  70. #define SNMP_AUTHPROTOCOL_HMACSHA 3 ///< HMAC-SHA
  71. //@}
  72. /** @name PrivProtocols
  73. *
  74. * Each user of the USM must use one privacy protocol (which may be
  75. * none.
  76. */
  77. //@{
  78. #define SNMP_PRIVPROTOCOL_NONE 1 ///< None
  79. #define SNMP_PRIVPROTOCOL_DES 2 ///< DES
  80. #define SNMP_PRIVPROTOCOL_AES128 4 ///< AES128 (RFC 3826)
  81. #define SNMP_PRIVPROTOCOL_IDEA 9 ///< IDEA (non standard)
  82. #define SNMP_PRIVPROTOCOL_AES192 20 ///< AES192 (non standard)
  83. #define SNMP_PRIVPROTOCOL_AES256 21 ///< AES256 (non standard)
  84. #define SNMP_PRIVPROTOCOL_3DESEDE 3 ///< 3DES (expired draft standard)
  85. //@}
  86. /** @name USM-ErrorCodes
  87. *
  88. * Each method of the class USM may return one of the following
  89. * error codes.
  90. */
  91. //@{
  92. #define SNMPv3_USM_OK 1400
  93. #define SNMPv3_USM_ERROR 1401
  94. #define SNMPv3_USM_ERROR_CONFIGFILE 1402
  95. #define SNMPv3_USM_UNSUPPORTED_SECURITY_LEVEL 1403
  96. #define SNMPv3_USM_UNKNOWN_SECURITY_NAME 1404
  97. #define SNMPv3_USM_ENCRYPTION_ERROR 1405
  98. #define SNMPv3_USM_DECRYPTION_ERROR 1406
  99. #define SNMPv3_USM_AUTHENTICATION_ERROR 1407
  100. #define SNMPv3_USM_AUTHENTICATION_FAILURE 1408
  101. #define SNMPv3_USM_PARSE_ERROR 1409
  102. #define SNMPv3_USM_UNKNOWN_ENGINEID 1410
  103. #define SNMPv3_USM_NOT_IN_TIME_WINDOW 1411
  104. #define SNMPv3_USM_UNSUPPORTED_AUTHPROTOCOL 1412
  105. #define SNMPv3_USM_UNSUPPORTED_PRIVPROTOCOL 1413
  106. #define SNMPv3_USM_ADDRESS_ERROR 1414
  107. #define SNMPv3_USM_FILECREATE_ERROR 1415
  108. #define SNMPv3_USM_FILEOPEN_ERROR 1416
  109. #define SNMPv3_USM_FILERENAME_ERROR 1417
  110. #define SNMPv3_USM_FILEDELETE_ERROR 1418
  111. #define SNMPv3_USM_FILEWRITE_ERROR 1419
  112. #define SNMPv3_USM_FILEREAD_ERROR 1420
  113. //@}
  114. /** @name Statistics on error codes. */
  115. //@{
  116. #define SNMPv3_USM_MAX_ERROR SNMPv3_USM_FILEREAD_ERROR
  117. #define SNMPv3_USM_MIN_ERROR SNMPv3_USM_OK
  118. #define SNMPv3_USM_ERRORCOUNT SNMPv3_USM_MAX_ERROR - SNMPv3_USM_MIN_ERROR
  119. //@}
  120. #define oidUsmStats "1.3.6.1.6.3.15.1.1"
  121. #define oidUsmStatsUnsupportedSecLevels "1.3.6.1.6.3.15.1.1.1.0"
  122. #define oidUsmStatsNotInTimeWindows "1.3.6.1.6.3.15.1.1.2.0"
  123. #define oidUsmStatsUnknownUserNames "1.3.6.1.6.3.15.1.1.3.0"
  124. #define oidUsmStatsUnknownEngineIDs "1.3.6.1.6.3.15.1.1.4.0"
  125. #define oidUsmStatsWrongDigests "1.3.6.1.6.3.15.1.1.5.0"
  126. #define oidUsmStatsDecryptionErrors "1.3.6.1.6.3.15.1.1.6.0"
  127. #define oidUsmUserTable "1.3.6.1.6.3.15.1.2.2"
  128. #define oidUsmUserEntry "1.3.6.1.6.3.15.1.2.2.1"
  129. #define oidUsmAuthProtocolBase "1.3.6.1.6.3.10.1.1"
  130. #define oidUsmNoAuthProtocol "1.3.6.1.6.3.10.1.1.1"
  131. #define oidUsmHMACMD5AuthProtocol "1.3.6.1.6.3.10.1.1.2"
  132. #define oidUsmHMACSHAAuthProtocol "1.3.6.1.6.3.10.1.1.3"
  133. #define oidUsmPrivProtocolBase "1.3.6.1.6.3.10.1.2"
  134. #define oidUsmNoPrivProtocol "1.3.6.1.6.3.10.1.2.1"
  135. #define oidUsmDESPrivProtocol "1.3.6.1.6.3.10.1.2.2"
  136. #define oidUsmIDEAPrivProtocol "1.3.6.1.6.3.10.1.2.9"
  137. #define oidUsmAES128PrivProtocol "1.3.6.1.6.3.10.1.2.4"
  138. #define oidUsmAES192PrivProtocol "1.3.6.1.6.3.10.1.2.20"
  139. #define oidUsmAES256PrivProtocol "1.3.6.1.6.3.10.1.2.21"
  140. #define oidUsm3DESEDEPrivProtocol "1.3.6.1.6.3.10.1.2.3"
  141. #define USM_KeyUpdate 1
  142. #define USM_PasswordUpdate 2
  143. #define USM_PasswordKeyUpdate 3
  144. #define USM_PasswordAllKeyUpdate 4
  145. class SnmpTarget;
  146. class Pdu;
  147. struct UsmKeyUpdate;
  148. struct UsmUserTableEntry {
  149. unsigned char *usmUserEngineID; long int usmUserEngineIDLength;
  150. unsigned char *usmUserName; long int usmUserNameLength;
  151. unsigned char *usmUserSecurityName; long int usmUserSecurityNameLength;
  152. long int usmUserAuthProtocol;
  153. unsigned char *usmUserAuthKey; long int usmUserAuthKeyLength;
  154. long int usmUserPrivProtocol;
  155. unsigned char *usmUserPrivKey; long int usmUserPrivKeyLength;
  156. };
  157. struct UsmUser {
  158. unsigned char *engineID; long int engineIDLength;
  159. unsigned char *usmUserName; long int usmUserNameLength;
  160. unsigned char *securityName; long int securityNameLength;
  161. long int authProtocol;
  162. unsigned char *authKey; long int authKeyLength;
  163. long int privProtocol;
  164. unsigned char *privKey; long int privKeyLength;
  165. };
  166. struct UsmUserNameTableEntry {
  167. OctetStr usmUserName;
  168. OctetStr usmUserSecurityName;
  169. long int usmUserAuthProtocol;
  170. long int usmUserPrivProtocol;
  171. unsigned char *authPassword; long int authPasswordLength;
  172. unsigned char *privPassword; long int privPasswordLength;
  173. };
  174. //-----------[ async methods callback ]-----------------------------------
  175. typedef void (*usm_add_user_callback)(const OctetStr &engine_id,
  176. const OctetStr &usm_user_name,
  177. const OctetStr &usm_user_security_name,
  178. const int auth_protocol,
  179. const OctetStr &auth_key,
  180. const int priv_protocol,
  181. const OctetStr &priv_key);
  182. struct SecurityStateReference;
  183. class AuthPriv;
  184. class USMTimeTable;
  185. class USMUserNameTable;
  186. class USMUserTable;
  187. class v3MP;
  188. /**
  189. * This is the class for the User Based Security Model.
  190. *
  191. * To add or delete users, the methods add_usm_user() and delete_usm_user()
  192. * should be used.
  193. *
  194. * USM distinguishes between userName and securityName. The following is
  195. * from section 2.1 of RFC3414:
  196. *
  197. * "userName: A string representing the name of the user.
  198. *
  199. * securityName: A human-readable string representing the user in a format
  200. * that is Security Model independent. There is a one-to-one relationship *
  201. * between userName and securityName."
  202. */
  203. class DLLOPT USM
  204. {
  205. friend class v3MP;
  206. public:
  207. /**
  208. * Create an instance of the USM.
  209. *
  210. * @param engine_boots - The new value for the snmpEngineBoots counter
  211. * @param engine_id - The local snmp engine id
  212. * @param v3_mp - Pointer to the parent v3MP object.
  213. * @param msg_id - OUT: The initial value for the msgID
  214. * @param result - OUT: construct status, should be SNMPv3_USM_OK
  215. */
  216. USM(unsigned int engine_boots, const OctetStr &engine_id, const v3MP *v3_mp,
  217. unsigned int *msg_id, int &result);
  218. /**
  219. * Destructor.
  220. */
  221. ~USM();
  222. /**
  223. * Enables the discovery mode of the USM, i.e. the USM accepts all messages
  224. * with unknown engine ids and adds these engine ids to its tables.
  225. */
  226. void set_discovery_mode() { discovery_mode = 1; };
  227. /**
  228. * Disables the discovery mode of the USM, i.e. the USM will not accept any
  229. * message with an unknown engine id.
  230. */
  231. void unset_discovery_mode() { discovery_mode = 0; };
  232. /**
  233. * Return TRUE if the USM discovery mode is enabled, FALSE else.
  234. */
  235. int is_discovery_enabled() const { return discovery_mode; };
  236. /**
  237. * Add a new user to the usmUserNameTable. If the User is already known
  238. * to the USM, the old entry is replaced.
  239. * The USM will compute a userName for the given securityName, which
  240. * will be the same as securityName (recommended).
  241. *
  242. * @param security_name - Unique securityName
  243. * @param auth_protocol - Possible values are:
  244. * SNMP_AUTHPROTOCOL_NONE,
  245. * SNMP_AUTHPROTOCOL_HMACMD5,
  246. * SNMP_AUTHPROTOCOL_HMACSHA
  247. * @param priv_protocol - Possible values are:
  248. * SNMP_PRIVPROTOCOL_NONE,
  249. * SNMP_PRIVPROTOCOL_DES,
  250. * SNMP_PRIVPROTOCOL_IDEA
  251. * @param auth_password - Secret password for authentication
  252. * @param priv_password - Secret password for privacy
  253. *
  254. * @return - SNMPv3_USM_OK or
  255. * SNMP_v3_USM_ERROR (memory error, not initialized)
  256. */
  257. int add_usm_user(const OctetStr& security_name,
  258. const long int auth_protocol,
  259. const long int priv_protocol,
  260. const OctetStr& auth_password,
  261. const OctetStr& priv_password);
  262. /**
  263. * Add a new user to the usmUserNameTable. If the userName is already known
  264. * to the USM, the old entry is replaced.
  265. *
  266. * It is not recommended to add users with userName != securityName.
  267. *
  268. * @param user_name - Unique userName
  269. * @param security_name - Unique securityName
  270. * @param auth_protocol - Possible values are:
  271. * SNMP_AUTHPROTOCOL_NONE,
  272. * SNMP_AUTHPROTOCOL_HMACMD5,
  273. * SNMP_AUTHPROTOCOL_HMACSHA
  274. * @param priv_protocol - Possible values are:
  275. * SNMP_PRIVPROTOCOL_NONE,
  276. * SNMP_PRIVPROTOCOL_DES,
  277. * SNMP_PRIVPROTOCOL_IDEA
  278. * @param auth_password - Secret password for authentication
  279. * @param priv_password - Secret password for privacy
  280. *
  281. * @return - SNMPv3_USM_OK or
  282. * SNMP_v3_USM_ERROR (memory error, not initialized)
  283. */
  284. int add_usm_user(const OctetStr& user_name,
  285. const OctetStr& security_name,
  286. const long int auth_protocol,
  287. const long int priv_protocol,
  288. const OctetStr& auth_password,
  289. const OctetStr& priv_password);
  290. /**
  291. * Add or replace a localized user in the USM table.
  292. *
  293. * This function uses build_localized_keys() to generate localized
  294. * keys for the given passwords. Then it calls add_localized_user()
  295. * to add/replace the localized entry for the user.
  296. *
  297. * The passwords are not stored, so no additonal engine id discovery
  298. * is possible.
  299. *
  300. * @param user_name - The name of the user (in the USM)
  301. * @param security_name - The securityName of the user, this name
  302. * is the same for all securityModels
  303. * @param auth_protocol - Possible values are:
  304. * SNMP_AUTHPROTOCOL_NONE,
  305. * SNMP_AUTHPROTOCOL_HMACMD5,
  306. * SNMP_AUTHPROTOCOL_HMACSHA,...
  307. * @param priv_protocol - Possible values are:
  308. * SNMP_PRIVPROTOCOL_NONE,
  309. * SNMP_PRIVPROTOCOL_DES,
  310. * SNMP_PRIVPROTOCOL_IDEA,...
  311. * @param auth_password - Secret password for authentication
  312. * @param priv_password - Secret password for privacy
  313. * @param engine_id - The engineID, the key was localized with
  314. *
  315. * @return - SNMPv3_USM_OK
  316. * SNMP_v3_USM_ERROR (not initialized, no memory)
  317. */
  318. int add_usm_user(const OctetStr& user_name,
  319. const OctetStr& security_name,
  320. const long int auth_protocol,
  321. const long int priv_protocol,
  322. const OctetStr& auth_password,
  323. const OctetStr& priv_password,
  324. const OctetStr& engine_id);
  325. int add_usm_user(const OctetStr& security_name,
  326. const long int auth_protocol,
  327. const long int priv_protocol,
  328. const OctetStr& auth_password,
  329. const OctetStr& priv_password,
  330. const OctetStr& engine_id)
  331. { return add_usm_user(security_name, security_name, auth_protocol,
  332. priv_protocol, auth_password, priv_password,
  333. engine_id); };
  334. /**
  335. * Delete all occurences of the user with the given security name
  336. * from the USM.
  337. *
  338. * @param security_name - the securityName of the user
  339. *
  340. * @return - SNMPv3_USM_OK, SNMPv3_USM_ERROR (not initialized)
  341. */
  342. int delete_usm_user(const OctetStr& security_name);
  343. /**
  344. * Save all localized users into a file.
  345. *
  346. * @param file - filename including path
  347. *
  348. * @return SNMPv3_USM_ERROR, SNMPv3_USM_FILECREATE_ERROR,
  349. * SNMPv3_USM_FILERENAME_ERROR or SNMPv3_USM_OK
  350. */
  351. int save_localized_users(const char *file);
  352. /**
  353. * Load localized users from a file.
  354. *
  355. * @param file - filename including path
  356. *
  357. * @return SNMPv3_USM_ERROR, SNMPv3_USM_FILEOPEN_ERROR,
  358. * SNMPv3_USM_FILEREAD_ERROR or SNMPv3_USM_OK
  359. */
  360. int load_localized_users(const char *file);
  361. /**
  362. * Save all users with their passwords into a file.
  363. *
  364. * @param file - filename including path
  365. *
  366. * @return SNMPv3_USM_ERROR, SNMPv3_USM_FILECREATE_ERROR,
  367. * SNMPv3_USM_FILERENAME_ERROR or SNMPv3_USM_OK
  368. */
  369. int save_users(const char *file);
  370. /**
  371. * Load users with their passwords from a file.
  372. *
  373. * @param file - filename including path
  374. *
  375. * @return SNMPv3_USM_ERROR, SNMPv3_USM_FILEOPEN_ERROR,
  376. * SNMPv3_USM_FILEREAD_ERROR or SNMPv3_USM_OK
  377. */
  378. int load_users(const char *file);
  379. /**
  380. * Add or replace a localized user in the USM table. Use this method
  381. * only, if you know what you are doing.
  382. *
  383. * @param engine_id - The engineID, the key was localized with
  384. * @param user_name - The name of the user (in the USM)
  385. * @param security_name - The securityName of the user, this name
  386. * is the same for all securityModels
  387. * @param auth_protocol - Possible values are:
  388. * SNMP_AUTHPROTOCOL_NONE,
  389. * SNMP_AUTHPROTOCOL_HMACMD5,
  390. * SNMP_AUTHPROTOCOL_HMACSHA,...
  391. * @param auth_key - The key used for authentications
  392. * @param priv_protocol - Possible values are:
  393. * SNMP_PRIVPROTOCOL_NONE,
  394. * SNMP_PRIVPROTOCOL_DES,
  395. * SNMP_PRIVPROTOCOL_IDEA,...
  396. * @param priv_key - The key used for privacy
  397. *
  398. * @return - SNMPv3_USM_OK
  399. * SNMP_v3_USM_ERROR (not initialized, no memory)
  400. */
  401. int add_localized_user(const OctetStr &engine_id,
  402. const OctetStr &user_name,
  403. const OctetStr &security_name,
  404. const long auth_protocol,
  405. const OctetStr &auth_key,
  406. const long priv_protocol,
  407. const OctetStr &priv_key);
  408. /**
  409. * Generate localized keys for the given params.
  410. *
  411. * The buffers for the keys should be of size SNMPv3_USM_MAX_KEY_LEN.
  412. *
  413. * @param engine_id -
  414. * @param auth_prot -
  415. * @param priv_prot -
  416. * @param auth_password -
  417. * @param auth_password_len -
  418. * @param priv_password -
  419. * @param priv_password_len -
  420. * @param auth_key - allocated space for the authentication key
  421. * @param auth_key_len - IN: length of the buffer, OUT: key length
  422. * @param priv_key - allocated space for the privacy key
  423. * @param priv_key_len - IN: length of the buffer, OUT: key length
  424. * @return SNMPv3_USM_OK, or USM error codes
  425. */
  426. int build_localized_keys(const OctetStr &engine_id,
  427. const int auth_prot,
  428. const int priv_prot,
  429. const unsigned char *auth_password,
  430. const unsigned int auth_password_len,
  431. const unsigned char *priv_password,
  432. const unsigned int priv_password_len,
  433. unsigned char *auth_key,
  434. unsigned int *auth_key_len,
  435. unsigned char *priv_key,
  436. unsigned int *priv_key_len);
  437. /**
  438. * Delete all localized entries of this user from the usmUserTable.
  439. *
  440. * @param user_name - The userName that should be deleted
  441. *
  442. * @return - SNMPv3_USM_ERROR (not initialized),
  443. * SNMPv3_USM_OK (user deleted or not in table)
  444. */
  445. int delete_localized_user(const OctetStr& user_name);
  446. /**
  447. * Delete the entry with the given userName and engineID
  448. * from the usmUserTable
  449. *
  450. * @param engine_id - The engineID
  451. * @param user_name - The userName that should be deleted
  452. *
  453. * @return - SNMPv3_USM_ERROR (not initialized),
  454. * SNMPv3_USM_OK (user deleted or not in table)
  455. */
  456. int delete_localized_user(const OctetStr& engine_id,
  457. const OctetStr& user_name);
  458. /**
  459. * Delete this engine id form all USM tables (users and engine time).
  460. *
  461. * @param engine_id - the engine id
  462. *
  463. * @return - SNMPv3_USM_ERROR (not initialized),
  464. * SNMPv3_USM_OK (user deleted or not in table)
  465. */
  466. int remove_engine_id(const OctetStr &engine_id);
  467. /**
  468. * Replace a localized key of the user and engineID in the
  469. * usmUserTable.
  470. *
  471. * @param user_name - The name of the user in the USM
  472. * @param user_name_len - The length of the user name
  473. * @param engine_id - Change the localized key for the SNMP
  474. * entity with this engine id
  475. * @param engine_id_len - The length of the engine id
  476. * @param new_key - The new key
  477. * @param new_key_len - The length of the new key
  478. * @param type_of_key - AUTHKEY, OWNAUTHKEY, PRIVKEY or OWNPRIVKEY
  479. *
  480. * @return - SNMPv3_USM_ERROR (no such entry or not initialized),
  481. * SNMPv3_USM_OK
  482. */
  483. int update_key(const unsigned char* user_name, const long user_name_len,
  484. const unsigned char* engine_id, const long engine_id_len,
  485. const unsigned char* new_key, const long new_key_len,
  486. const int type_of_key);
  487. /**
  488. * Search for a user with the given securityName and engineID
  489. * in the usmUserTable and return the entry. If no entry
  490. * could be found, the usmUserNameTable is searched for the given
  491. * securityName. If this table has an entry of this user, a
  492. * localized entry is generated, added to the usmUserTable and
  493. * returned to the caller.
  494. *
  495. * The caller has to call free_user() with the returned struct.
  496. *
  497. * @param engine_id -
  498. * @param security_name -
  499. *
  500. * @return - a pointer to the structure if an entry could be found
  501. * or was generated, NULL for all errors
  502. */
  503. struct UsmUser *get_user(const OctetStr &engine_id,
  504. const OctetStr &security_name);
  505. /**
  506. * Free the structure returned from get_user(OctetStr,OctetStr).
  507. */
  508. void free_user(struct UsmUser *&user);
  509. /**
  510. * Get the security name from a user name.
  511. *
  512. * @param user_name -
  513. * @param user_name_len -
  514. * @param security_name - Buffer for the securityName
  515. *
  516. * @return - SNMPv3_USM_ERROR (not initialized, not found, buffer too small),
  517. * SNMPv3_USM_OK
  518. */
  519. int get_security_name(const unsigned char *user_name,
  520. const long int user_name_len,
  521. OctetStr &security_name);
  522. /**
  523. * Get the user name from a security name.
  524. *
  525. * @param user_name - Buffer for the userName
  526. * @param user_name_len - Has to be set to the max length of the
  527. * buffer. Is set to the length of the found
  528. * securityName or to 0 if not found.
  529. * @param security_name -
  530. * @param security_name_len -
  531. *
  532. * @return - SNMPv3_USM_ERROR (not initialized, not found, buffer too small),
  533. * SNMPv3_USM_OK
  534. */
  535. int get_user_name(unsigned char *user_name,
  536. long int *user_name_len,
  537. const unsigned char *security_name,
  538. const long int security_name_len);
  539. /**
  540. * Prepare a key update in the USM. The following procedure is used: To
  541. * prepare the key update, this function adds the neccessary variable
  542. * bindings to the Pdu to do the key update on the target SNMP entity.
  543. * The Pdu has to be sent to the target. If the key update on the target
  544. * is successful, usmCommitKeyUpdate() has to be called to do the local key
  545. * update. On failure usmAbortKeyUpdate() has to be called to free
  546. * temporary ressources.
  547. *
  548. * @param securityName - The name of the user
  549. * @param target - A target to identify the SNMP entity on which the
  550. * key will be updated
  551. * @param newPassword - The new password for the user
  552. * @param pdu - A PDU into which this funktion adds the VBs needed
  553. * to change the keys on the target
  554. * @param type - Indicates how and which key should be chaned:
  555. * possilbe values are: AUTHKEY, PRIVKEY and
  556. * OWNAUTHKEY, OWNPRIVKEY.
  557. * @param status - The return status: SNMPv3_USM_OK or one of the
  558. * error codes
  559. *
  560. * @return - A structure, that is needed to commit/abort the key update.
  561. * If an error occurs, the return value is NULL
  562. */
  563. struct UsmKeyUpdate* key_update_prepare(const OctetStr& securityName,
  564. SnmpTarget& target,
  565. const OctetStr& newPassword,
  566. Pdu& pdu, int type,
  567. int &status,
  568. const OctetStr& oldpass = "",
  569. const OctetStr& oldengid= "",
  570. const OctetStr& newengid= "");
  571. /**
  572. * Abort the local key update.
  573. *
  574. * @param uku - The pointer returned by usmPrepareKeyUpdate()
  575. */
  576. void key_update_abort(struct UsmKeyUpdate *uku);
  577. /**
  578. * Commit the local key update.
  579. *
  580. * @param uku - The pointer returned by usmPrepareKeyUpdate()
  581. * @param update_type - One of USM_KeyUpdate, USM_PasswordKeyUpdate,
  582. * USM_PasswordAllKeyUpdate
  583. *
  584. * @return - SNMPv3_USM_ERROR, SNMPv3_USM_OK
  585. */
  586. int key_update_commit(struct UsmKeyUpdate *uku, int update_type);
  587. /**
  588. * Get a pointer to the AuthPriv object used by the USM.
  589. *
  590. */
  591. AuthPriv *get_auth_priv();
  592. /**
  593. * Return engineBoots and engineTime for a given engineID
  594. *
  595. * @param engine_id - The engineID of the SNMP entity
  596. * @param engine_boots - OUT: boot counter (0 if not found)
  597. * @param engine_time - OUT: engine time (0 if not found)
  598. *
  599. * @return - SNMPv3_USM_ERROR (not initialized),
  600. * SNMPv3_USM_OK (entry found, values are filled)
  601. * SNMPv3_USM_UNKNOWN_ENGINEID ( not found)
  602. */
  603. int get_time(const OctetStr &engine_id,
  604. long int *engine_boots, long int *engine_time);
  605. /**
  606. * Return engineBoots and engineTime of the local snmp entity
  607. *
  608. * @param engine_boots - OUT: boot counter (0 if not found)
  609. * @param engine_time - OUT: engine time (0 if not found)
  610. *
  611. * @return - SNMPv3_USM_ERROR (not initialized),
  612. * SNMPv3_USM_OK (entry found, values are filled)
  613. */
  614. int get_local_time(long int *engine_boots, long int *engine_time) const;
  615. /**
  616. * Return the local snmp engine id.
  617. */
  618. const OctetStr& get_local_engine_id() const { return local_snmp_engine_id; };
  619. /**
  620. * Get the number of received messages with an unsupported securityLevel
  621. *
  622. * @return - usmStatsUnsupportedSecLevels
  623. */
  624. unsigned long get_stats_unsupported_sec_levels() const
  625. { return usmStatsUnsupportedSecLevels; };
  626. /**
  627. * Get the number of received messages outside time window
  628. *
  629. * @return - usmStatsNotInTimeWindows
  630. */
  631. unsigned long get_stats_not_in_time_windows() const
  632. { return usmStatsNotInTimeWindows; };
  633. /**
  634. * Get the number of received messages with a unknown userName
  635. *
  636. * @return - usmStatsUnknownUserNames
  637. */
  638. unsigned long get_stats_unknown_user_names() const
  639. { return usmStatsUnknownUserNames; };
  640. /**
  641. * Get the number of received messages with a unknown engineID
  642. *
  643. * @return - usmStatsUnknownEngineIDs
  644. */
  645. unsigned long get_stats_unknown_engine_ids() const
  646. { return usmStatsUnknownEngineIDs; };
  647. /**
  648. * Get the number of received messages with a wrong digest
  649. *
  650. * @return - usmStatsWrongDigests
  651. */
  652. unsigned long get_stats_wrong_digests() const
  653. { return usmStatsWrongDigests; };
  654. /**
  655. * Get the number of received messages with decryption errors
  656. *
  657. * @return - usmStatsDecryptionErrors
  658. */
  659. unsigned long get_stats_decryption_errors() const
  660. { return usmStatsDecryptionErrors; };
  661. //@{
  662. /**
  663. * Increase the stats counter. Should only be used by agent++.
  664. */
  665. void inc_stats_unsupported_sec_levels();
  666. void inc_stats_not_in_time_windows();
  667. void inc_stats_unknown_user_names();
  668. void inc_stats_unknown_engine_ids();
  669. void inc_stats_wrong_digests();
  670. void inc_stats_decryption_errors();
  671. //@}
  672. /**
  673. * Lock the UsmUserNameTable for access through peek_first_user()
  674. * and peek_next_user().
  675. */
  676. void lock_user_name_table();
  677. /**
  678. * Get a const pointer to the first entry of the UsmUserNameTable.
  679. *
  680. * @note Use lock_user_name_table() and unlock_user_name_table()
  681. * for thread safety.
  682. */
  683. const UsmUserNameTableEntry *peek_first_user();
  684. /**
  685. * Get a const pointer to the next entry of the UsmUserNameTable.
  686. *
  687. * @note Use lock_user_name_table() and unlock_user_name_table()
  688. * for thread safety.
  689. */
  690. const UsmUserNameTableEntry *peek_next_user(const UsmUserNameTableEntry *e);
  691. /**
  692. * Unlock the UsmUserNameTable after access through peek_first_user()
  693. * and peek_next_user().
  694. */
  695. void unlock_user_name_table();
  696. /**
  697. * Lock the UsmUserTable for access through peek_first_luser()
  698. * and peek_next_luser().
  699. */
  700. void lock_user_table();
  701. /**
  702. * Get a const pointer to the first entry of the UsmUserTable.
  703. *
  704. * @note Use lock_user_table() and unlock_user_table()
  705. * for thread safety.
  706. */
  707. const UsmUserTableEntry *peek_first_luser();
  708. /**
  709. * Get a const pointer to the next entry of the UsmUserTable.
  710. *
  711. * @note Use lock_user_table() and unlock_user_table()
  712. * for thread safety.
  713. */
  714. const UsmUserTableEntry *peek_next_luser(const UsmUserTableEntry *e);
  715. /**
  716. * Unlock the UsmUserTable after access through peek_first_luser()
  717. * and peek_next_luser().
  718. */
  719. void unlock_user_table();
  720. /**
  721. * for v3MP:
  722. *
  723. * Delete the pointers within the structure and the structure
  724. * itself.
  725. *
  726. * @param ssr - The structure that should be deleted.
  727. */
  728. void delete_sec_state_reference(struct SecurityStateReference *ssr);
  729. /**
  730. * Protected (for agent++):
  731. *
  732. * Get the user at the specified position of the usmUserTable.
  733. *
  734. * The returned pointer must NOT be deleted!
  735. *
  736. * @note lock_user_table() and unlock_user_table() must be used
  737. * for thread synchronization.
  738. *
  739. * @param number - get the entry at position number (1...)
  740. *
  741. * @return - a pointer to the structure or NULL if number is out
  742. * of range
  743. */
  744. const struct UsmUserTableEntry *get_user(int number);
  745. /**
  746. * Get the properties of the specified user.
  747. *
  748. * The returned pointer must NOT be deleted!
  749. *
  750. * @note lock_user_table() and unlock_user_table() must be used
  751. * for thread synchronization.
  752. *
  753. * @param security_name - The security name of the user
  754. *
  755. * @return - a pointer to the structure or NULL if number is out
  756. * of range
  757. */
  758. const struct UsmUserNameTableEntry *get_user(const OctetStr &security_name);
  759. /**
  760. * Protected (for agent++):
  761. *
  762. * Get the number of elements in the usmUserTable
  763. *
  764. * @note lock_user_table() and unlock_user_table() must be used
  765. * for thread synchronization.
  766. *
  767. * @return - number of elements
  768. */
  769. int get_user_count() const;
  770. /**
  771. * Protected (for agent++)
  772. *
  773. * Register a callback function that is called if a new localized user
  774. * has been added to the usm user table
  775. */
  776. void add_user_added_callback(const usm_add_user_callback cb);
  777. protected:
  778. /**
  779. * Get a new security state reference (for v3MP).
  780. *
  781. * @return - A newly created security state reference.
  782. */
  783. struct SecurityStateReference *get_new_sec_state_reference();
  784. /**
  785. * Generate a complete message that is ready to send to the target.
  786. *
  787. * @param globalData - Buffer containing the serialized globalData,
  788. * ready to be copied into the wholeMsg
  789. * @param globalDataLength - The length of this buffer
  790. * @param maxMessageSize - The maximum message size
  791. * @param securityEngineID - The engineID of the authoritative SNMP entity
  792. * @param securityName - The name of the user
  793. * @param securityLevel - The security Level for this Message
  794. * @param scopedPDU - Buffer containing the serialized scopedPDU,
  795. * ready to be copied into the wholeMsg
  796. * @param scopedPDULength - The length of this Buffer
  797. * @param securityStateReference - The reference that was generated when
  798. * the request was parsed. For request, this
  799. * param has to be NULL. The reference
  800. * is deleted by this function.
  801. * @param wholeMsg - OUT: the buffer for the whole message
  802. * @param wholeMsgLength - IN: lenght of the buffer.
  803. * OUT: length of the generated message
  804. *
  805. * @return - SNMPv3_USM_OK on success. See snmperrs.h for the error codes
  806. * of the USM.
  807. */
  808. int generate_msg(
  809. unsigned char *globalData, // message header, admin data
  810. int globalDataLength,
  811. int maxMessageSize, // of the sending SNMP entity
  812. const OctetStr &securityEngineID,// authoritative SNMP entity
  813. const OctetStr &securityName, // on behalf of this principal
  814. int securityLevel, // Level of Security requested
  815. unsigned char *scopedPDU, // message (plaintext) payload
  816. int scopedPDULength,
  817. struct SecurityStateReference *securityStateReference,
  818. unsigned char *wholeMsg, // OUT complete generated message
  819. int *wholeMsgLength); // OUT length of generated message
  820. /**
  821. * Parse a received message.
  822. *
  823. * @param maxMessageSize - The maximum message size of the snding
  824. * SNMP entity.
  825. * @param securityParameters - The security parameters as received
  826. * @param securityParametersLength - The length of the security parameters
  827. * @param securityParametersPosition - The position of the security
  828. * parameters in the message
  829. * @param securityLevel - The securityLevel of the message
  830. * @param wholeMsg - The buffer with the whole message
  831. * @param wholeMsgLength - The length of the whole message
  832. * @param msgData - The buffer with the messageData
  833. * @param msgDataLength - The length of the messageData buffer
  834. * @param security_engine_id - OUT: the authoritative engineID
  835. * @param security_name - OUT: the name of the user
  836. * @param scopedPDU - OUT: buffer containing the scopedPDU
  837. * @param scopedPDULength - IN: length of the buffer
  838. * OUT: length of the scopedPDU
  839. * @param maxSizeResponseScopedPDU - OUT: maximum size for a scopedPDU in a
  840. * response message
  841. * @param securityStateReference - OUT: the securityStateReference
  842. * @param fromAddress - IN: Address of the sender
  843. *
  844. * @return - SNMPv3_USM_OK on success. See snmperrs.h for the error codes
  845. * of the USM.
  846. */
  847. int process_msg(
  848. int maxMessageSize, // of the sending SNMP entity
  849. unsigned char *securityParameters, // for the received message
  850. int securityParametersLength,
  851. int securityParametersPosition,
  852. long int securityLevel, // Level of Security
  853. unsigned char *wholeMsg, // as received on the wire
  854. int wholeMsgLength, // length as received on the wire
  855. unsigned char *msgData,
  856. int msgDataLength,
  857. OctetStr &security_engine_id, // authoritative SNMP entity
  858. OctetStr &security_name, //identification of the principal
  859. unsigned char *scopedPDU, // message (plaintext) payload
  860. int *scopedPDULength,
  861. long *maxSizeResponseScopedPDU,// maximum size of the Response PDU
  862. struct SecurityStateReference *securityStateReference,
  863. // reference to security state
  864. // information, needed for response
  865. const UdpAddress &fromAddress); // Address of the sender
  866. private:
  867. /**
  868. * Delete the pointers in the structure and set all values to 0/NULL.
  869. *
  870. * @param usp - The structure that should be deleted
  871. */
  872. void delete_sec_parameters( struct UsmSecurityParameters *usp);
  873. /**
  874. * Serialize the given values into the buffer according to the BER.
  875. *
  876. * UsmSecurityParameters ::=
  877. * SEQUENCE {
  878. * -- global User-based security parameters
  879. * msgAuthoritativeEngineID OCTET STRING (5..32)
  880. * msgAuthoritativeEngineBoots INTEGER (0..2147483647),
  881. * msgAuthoritativeEngineTime INTEGER (0..2147483647),
  882. * msgUserName OCTET STRING (SIZE(0..32)),
  883. * -- authentication protocol specific parameters
  884. * msgAuthenticationParameters OCTET STRING,
  885. * -- privacy protocol specific parameters
  886. * msgPrivacyParameters OCTET STRING
  887. * }
  888. *
  889. * @param outBuf - buffer for the serialized values
  890. * @param maxLength - before call: length of the buffer
  891. * after call: bytes left in the buffer
  892. * @param sp - the values to serialize
  893. * @param position - after call: points to the first byte of the
  894. * field for the authentication parameter
  895. *
  896. * @return - a pointer to the first free byte in the buffer,
  897. * NULL on error
  898. */
  899. unsigned char *build_sec_params(unsigned char *outBuf, int *maxLength,
  900. struct UsmSecurityParameters sp,
  901. int *position);
  902. /**
  903. * Serialize the given values acording to the BER into the
  904. * buffer. On success, the buffer contains a valid SNMPv3 message.
  905. *
  906. * @param outBuf - buffer for the serialized values
  907. * @param maxLength - before call: length of the buffer
  908. * after call: bytes left in the buffer
  909. * @param globalData - Buffer that contains the serialized globalData
  910. * @param globalDataLength - The length of this buffer
  911. * @param positionAuthPar - after call: points to the first byte of the
  912. * field for the authentication parameter
  913. * @param securityParameters - The security parameters
  914. * @param msgData - Buffer that contains the serialized msgData
  915. * @param msgDataLength - The length of this buffer
  916. *
  917. * @return - a pointer to the first free byte in the buffer,
  918. * NULL on error
  919. */
  920. unsigned char *build_whole_msg(
  921. unsigned char *outBuf, int *maxLength,
  922. unsigned char *globalData, long int globalDataLength,
  923. int *positionAuthPar,
  924. struct UsmSecurityParameters securityParameters,
  925. unsigned char *msgData, long int msgDataLength);
  926. /**
  927. * Delete the pointers in the structure
  928. *
  929. * @param user - The structure that should be deleted
  930. */
  931. inline void delete_user_ptr(struct UsmUser *user);
  932. private:
  933. OctetStr local_snmp_engine_id; ///< local snmp engine id
  934. const v3MP *v3mp; ///< Pointer to the v3MP that created this object
  935. // 0: don't accept messages from hosts with a unknown engine id
  936. int discovery_mode;
  937. // MIB Counters
  938. unsigned int usmStatsUnsupportedSecLevels;
  939. unsigned int usmStatsNotInTimeWindows;
  940. unsigned int usmStatsUnknownUserNames;
  941. unsigned int usmStatsUnknownEngineIDs;
  942. unsigned int usmStatsWrongDigests;
  943. unsigned int usmStatsDecryptionErrors;
  944. // the instance of AuthPriv
  945. AuthPriv *auth_priv;
  946. // this table contains time values of contacted snmp entities
  947. USMTimeTable *usm_time_table;
  948. // Users that are known but not localized to a engine ID
  949. USMUserNameTable *usm_user_name_table;
  950. // Table containing localized Users ready to use
  951. USMUserTable *usm_user_table;
  952. // Callback for agent++ to indicate new users in usm tables
  953. usm_add_user_callback usm_add_user_cb;
  954. };
  955. // only for compatibility do not use these values and functions:
  956. // =============================================================
  957. #define SecurityLevel_noAuthNoPriv SNMP_SECURITY_LEVEL_NOAUTH_NOPRIV
  958. #define SecurityLevel_authNoPriv SNMP_SECURITY_LEVEL_AUTH_NOPRIV
  959. #define SecurityLevel_authPriv SNMP_SECURITY_LEVEL_AUTH_PRIV
  960. #define SNMPv3_usmNoAuthProtocol SNMP_AUTHPROTOCOL_NONE
  961. #define SNMPv3_usmHMACMD5AuthProtocol SNMP_AUTHPROTOCOL_HMACMD5
  962. #define SNMPv3_usmHMACSHAAuthProtocol SNMP_AUTHPROTOCOL_HMACSHA
  963. #define SNMPv3_usmNoPrivProtocol SNMP_PRIVPROTOCOL_NONE
  964. #define SNMPv3_usmDESPrivProtocol SNMP_PRIVPROTOCOL_DES
  965. #define SNMPv3_usmIDEAPrivProtocol SNMP_PRIVPROTOCOL_IDEA
  966. #define SNMPv3_usmAES128PrivProtocol SNMP_PRIVPROTOCOL_AES128
  967. #define SNMPv3_usmAES192PrivProtocol SNMP_PRIVPROTOCOL_AES192
  968. #define SNMPv3_usmAES256PrivProtocol SNMP_PRIVPROTOCOL_AES256
  969. #ifdef SNMP_PP_NAMESPACE
  970. } // end of namespace Snmp_pp
  971. #endif
  972. #endif // _SNMPv3
  973. #endif