auth_priv.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. /*_############################################################################
  2. _##
  3. _## auth_priv.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: auth_priv.h 287 2007-03-22 22:37:09Z katz $
  30. #ifndef _AUTH_PRIV_
  31. #define _AUTH_PRIV_
  32. #include "snmp_pp/config_snmp_pp.h"
  33. #ifdef _SNMPv3
  34. #include "snmp_pp/usm_v3.h"
  35. #ifdef SNMP_PP_NAMESPACE
  36. namespace Snmp_pp {
  37. #endif
  38. #define SNMPv3_USM_MAX_KEY_LEN 32
  39. /* Accept Messages with auth/priv param fields up to this length */
  40. #define SNMPv3_AP_MAXLENGTH_AUTHPARAM 128
  41. #define SNMPv3_AP_MAXLENGTH_PRIVPARAM 128
  42. #define SNMPv3_AP_OUTPUT_LENGTH_MD5 16
  43. #define SNMPv3_AP_OUTPUT_LENGTH_SHA 20
  44. class OctetStr;
  45. /**
  46. * Abstract class for auth modules.
  47. *
  48. * This class has to be subclassed to add new authentication
  49. * protocols.
  50. *
  51. */
  52. class DLLOPT Auth
  53. {
  54. public:
  55. virtual ~Auth() {};
  56. /**
  57. * Generate the localized key for the given password and engine id.
  58. *
  59. * @param password - the password
  60. * @param password_len - the length of the password
  61. * @param engine_id - pointer to snmpEngineID
  62. * @param engine_id_len - length of snmpEngineID
  63. * @param key - pointer to an empty buffer that will be filled
  64. * with generated key
  65. * @param key_len - IN: length of the buffer
  66. * OUT: length of the key
  67. *
  68. * @return SNMPv3_USM_OK on success
  69. */
  70. virtual int password_to_key(const unsigned char *password,
  71. const unsigned int password_len,
  72. const unsigned char *engine_id,
  73. const unsigned int engine_id_len,
  74. unsigned char *key,
  75. unsigned int *key_len) = 0;
  76. /**
  77. * Generate a hash value for the given data.
  78. *
  79. * @param data - the data
  80. * @param data_len - the length of the data
  81. * @param digest - pointer to the generated digest
  82. *
  83. * @return SNMPv3_USM_OK on success
  84. */
  85. virtual int hash(const unsigned char *data,
  86. const unsigned int data_len,
  87. unsigned char *digest) const = 0;
  88. /**
  89. * Authenticate an outgoing message.
  90. *
  91. * This method fills the authentication parameters field of the
  92. * given message. The param auth_par_ptr is pointing inside the
  93. * message buffer and must be zeroed before the authentication value
  94. * is computed.
  95. *
  96. * @param key - pointer to the (fixed length) key
  97. * @param msg - pointer to the whole message
  98. * @param msg_len - the length of the message
  99. * @param auth_par_ptr - pointer to the auth field inside the msg buffer
  100. *
  101. * @return SNMPv3_USM_OK on success and
  102. * SNMPv3_USM_ERROR for unexpected errors.
  103. */
  104. virtual int auth_out_msg(const unsigned char *key,
  105. unsigned char *msg,
  106. const int msg_len,
  107. unsigned char *auth_par_ptr) = 0;
  108. /**
  109. * Authenticate an incoming message.
  110. *
  111. * This method checks if the value in the authentication parameters
  112. * field of the message is valid.
  113. *
  114. * The following procedure is used to verify the authenitcation value
  115. * - copy the authentication value to a temp buffer
  116. * - zero the auth field
  117. * - recalculate the authenthication value
  118. * - compare the two authentcation values
  119. * - write back the received authentication value if values differ
  120. *
  121. * @param key - pointer to the (fixed length) key
  122. * @param msg - pointer to the whole message
  123. * @param msg_len - the length of the message
  124. * @param auth_par_ptr - pointer to the auth field inside the msg buffer
  125. * @param auth_par_len - Length of the received auth field
  126. *
  127. * @return SNMPv3_USM_OK if the msg is valid,
  128. * SNMPv3_USM_AUTHENTICATION_FAILURE if not and
  129. * SNMPv3_USM_ERROR for unexpected errors.
  130. */
  131. virtual int auth_inc_msg(const unsigned char *key,
  132. unsigned char *msg,
  133. const int msg_len,
  134. unsigned char *auth_par_ptr,
  135. const int auth_par_len) = 0;
  136. /**
  137. * Get the unique id of the authentication protocol.
  138. */
  139. virtual int get_id() const = 0;
  140. /**
  141. * Get the unique identifier string of the authentication protocol.
  142. */
  143. virtual const char *get_id_string() const = 0;
  144. /**
  145. * Set the pointer to the salt that should be used.
  146. */
  147. virtual void set_salt(pp_uint64 *new_salt) { salt = new_salt; };
  148. /**
  149. * Get the maximum length that is needed for the
  150. * msgAuthenticationParameters field.
  151. */
  152. virtual int get_auth_params_len() const = 0;
  153. /**
  154. * Get length of a hash output.
  155. */
  156. virtual int get_hash_len() const = 0;
  157. protected:
  158. pp_uint64 *salt;
  159. };
  160. /**
  161. * Abstract class for priv modules
  162. *
  163. * This class has to be subclassed to add new privacy
  164. * protocols.
  165. *
  166. */
  167. class DLLOPT Priv
  168. {
  169. public:
  170. virtual ~Priv() {};
  171. /**
  172. * Encrypt the buffer with the given key.
  173. *
  174. * This method fills the privacy parameters field of the given
  175. * message.
  176. *
  177. * @param key - pointer to the encryption key
  178. * @param key_len - length of encryption key
  179. * @param buffer - pointer to the unencrypted buffer
  180. * @param buffer_len - length of the buffer
  181. * @param out_buffer - pointer to the buffer for the encryptet data
  182. * @param out_buffer_len - Input: Length of the output buffer.
  183. * Output: Bytes written
  184. * @param privacy_params - Buffer, where the privacy parameters
  185. * are written to.
  186. * @param privacy_params_len - Length of the privacy parameters buffer
  187. * @param engine_boots - The engine boots value for the message
  188. * @param engine_time - The engine time value for the message
  189. *
  190. * @return SNMPv3_USM_OK on success
  191. */
  192. virtual int encrypt(const unsigned char *key,
  193. const unsigned int key_len,
  194. const unsigned char *buffer,
  195. const unsigned int buffer_len,
  196. unsigned char *out_buffer,
  197. unsigned int *out_buffer_len,
  198. unsigned char *privacy_params,
  199. unsigned int *privacy_params_len,
  200. const unsigned long engine_boots,
  201. const unsigned long engine_time) = 0;
  202. /**
  203. * Decrypt the buffer with the given key.
  204. *
  205. * This method needs the privacy parameters field for the given
  206. * message.
  207. *
  208. * @param key - pointer to the (fixed length) dencryption key
  209. * @param key_len - length of encryption key
  210. * @param buffer - pointer to the encrypted buffer
  211. * @param buffer_len - length of the buffer
  212. * @param out_buffer - pointer to the buffer for the decryptet data
  213. * @param out_buffer_len - Input: Length of the output buffer.
  214. * Output: Bytes written
  215. * @param privacy_params - Buffer, where the privacy parameters
  216. * are read from.
  217. * @param privacy_params_len - Length of the privacy parameters buffer
  218. * @param engine_boots - The engine boots value for the message
  219. * @param engine_time - The engine time value for the message
  220. *
  221. * @return SNMPv3_USM_OK on success
  222. */
  223. virtual int decrypt(const unsigned char *key,
  224. const unsigned int key_len,
  225. const unsigned char *buffer,
  226. const unsigned int buffer_len,
  227. unsigned char *out_buffer,
  228. unsigned int *out_buffer_len,
  229. const unsigned char *privacy_params,
  230. const unsigned int privacy_params_len,
  231. const unsigned long engine_boots,
  232. const unsigned long engine_time) = 0;
  233. /**
  234. * Extend a localized key that is too short.
  235. *
  236. * Some privacy protocols require a key that is longer than the key
  237. * generated by the pasword to key algorithm of the authentication
  238. * protocol. This function extends a short key to the required length.
  239. *
  240. * @param password - the password
  241. * @param password_len - the length of the password
  242. * @param engine_id - pointer to snmpEngineID
  243. * @param engine_id_len - length of snmpEngineID
  244. * @param key - pointer to the short key that was generated
  245. * using Auth::password_to_key() function
  246. * @param key_len - IN: length of the short key
  247. * OUT: length of the extended key
  248. * @param max_key_len - Length of the key buffer
  249. * @param auth - Pointer of the authentication protocol that
  250. * should be used
  251. *
  252. * @return SNMPv3_USM_OK on success
  253. */
  254. virtual int extend_short_key(const unsigned char *password,
  255. const unsigned int password_len,
  256. const unsigned char *engine_id,
  257. const unsigned int engine_id_len,
  258. unsigned char *key,
  259. unsigned int *key_len,
  260. const unsigned int max_key_len,
  261. Auth *auth) = 0;
  262. /**
  263. * Get the uniqhe id of the privacy protocol.
  264. */
  265. virtual int get_id() const = 0;
  266. /**
  267. * Get the unique identifier string of the privacy protocol.
  268. */
  269. virtual const char *get_id_string() const = 0;
  270. /**
  271. * Set the pointer to the salt that should be used.
  272. */
  273. virtual void set_salt(pp_uint64 *new_salt) { salt = new_salt; };
  274. /**
  275. * Get the maximum length that is needed for the
  276. * msgPrivacyParameters field.
  277. */
  278. virtual int get_priv_params_len() const = 0;
  279. /**
  280. * Get the minimum key length needed for encryption and decryption.
  281. */
  282. virtual int get_min_key_len() const = 0;
  283. /**
  284. * Decrease a too long length to the right value.
  285. */
  286. virtual void fix_key_len(unsigned int &key_len) const = 0;
  287. protected:
  288. pp_uint64 *salt;
  289. };
  290. typedef Auth* AuthPtr;
  291. typedef Priv* PrivPtr;
  292. /**
  293. * Class that holds all authentication and privacy protocols
  294. * for a snmp entity.
  295. */
  296. class DLLOPT AuthPriv
  297. {
  298. public:
  299. /**
  300. * Default constructor, initializes random values
  301. */
  302. AuthPriv(int &construct_state);
  303. /**
  304. * Destructor, deletes all auth and priv protocol objets.
  305. */
  306. ~AuthPriv();
  307. /**
  308. * Add the default authentication protocols.
  309. *
  310. * The following authentication protocols are added:
  311. * - MD5
  312. * - SHA
  313. *
  314. * The following privacy protocols are added:
  315. * - DES
  316. * - AES128, AES196 and AES256 if libtomcrypt or OpenSSL is enabled
  317. * - IDEA if enabled
  318. *
  319. * @return SNMP_CLASS_SUCCESS or SNMP_CLASS_ERROR.
  320. */
  321. int add_default_modules();
  322. /**
  323. * Add a new authentication protocol.
  324. *
  325. * All added objects will be deleted in the destructor
  326. *
  327. * @param auth - Pointer to a new auth protocol object
  328. *
  329. * @return SNMP_CLASS_SUCCESS or SNMP_CLASS_ERROR
  330. */
  331. int add_auth(Auth *auth);
  332. /**
  333. * Delete a authentication protocol.
  334. *
  335. * @param auth_id - The id of the authentication protocol to remove
  336. *
  337. * @return SNMP_CLASS_SUCCESS or SNMP_CLASS_ERROR
  338. */
  339. int del_auth(const int auth_id);
  340. /**
  341. * Add a new privacy protocol.
  342. *
  343. * All added objects will be deleted in the destructor
  344. *
  345. * @param priv - Pointer to a new privacy protocol object
  346. *
  347. * @return SNMP_CLASS_SUCCESS or SNMP_CLASS_ERROR
  348. */
  349. int add_priv(Priv *priv);
  350. /**
  351. * Delete a privacy protocol.
  352. *
  353. * @param priv_id - The id of the privacy protocol to remove
  354. *
  355. * @return SNMP_CLASS_SUCCESS or SNMP_CLASS_ERROR
  356. */
  357. int del_priv(const int priv_id);
  358. /**
  359. * Call the password-to-key method of the specified authentication
  360. * protocol.
  361. */
  362. int password_to_key_auth(const int auth_prot,
  363. const unsigned char *password,
  364. const unsigned int password_len,
  365. const unsigned char *engine_id,
  366. const unsigned int engine_id_len,
  367. unsigned char *key,
  368. unsigned int *key_len);
  369. /**
  370. * Call the password-to-key method of the specified privacy
  371. * protocol.
  372. */
  373. int password_to_key_priv(const int auth_prot,
  374. const int priv_prot,
  375. const unsigned char *password,
  376. const unsigned int password_len,
  377. const unsigned char *engine_id,
  378. const unsigned int engine_id_len,
  379. unsigned char *key,
  380. unsigned int *key_len);
  381. /**
  382. * Get the keyChange value for the specified keys using the given
  383. * authentication protocol.
  384. */
  385. int get_keychange_value(const int auth_prot,
  386. const OctetStr& old_key,
  387. const OctetStr& new_key,
  388. OctetStr& keychange_value);
  389. /**
  390. * Get a pointer to a privacy protocol object.
  391. */
  392. Priv *get_priv(const int priv_prot);
  393. /**
  394. * Get a pointer to a authentication protocol object.
  395. */
  396. Auth *get_auth(const int auth_prot);
  397. /**
  398. * Get the unique id for the given auth protocol.
  399. *
  400. * @param string_id - The string returned by Auth::get_id_string()
  401. *
  402. * @return The id or -1
  403. */
  404. int get_auth_id(const char *string_id) const;
  405. /**
  406. * Get the unique id for the given priv protocol.
  407. *
  408. * @param string_id - The string returned by Priv::get_id_string()
  409. *
  410. * @return The id or -1
  411. */
  412. int get_priv_id(const char *string_id) const;
  413. /**
  414. * Encrypt a message.
  415. */
  416. int encrypt_msg(const int priv_prot,
  417. const unsigned char *key,
  418. const unsigned int key_len,
  419. const unsigned char *buffer,
  420. const unsigned int buffer_len,
  421. unsigned char *out_buffer,
  422. unsigned int *out_buffer_len,
  423. unsigned char *privacy_params,
  424. unsigned int *privacy_params_len,
  425. const unsigned long engine_boots,
  426. const unsigned long engine_time);
  427. /**
  428. * Decrypt a message.
  429. */
  430. int decrypt_msg(const int priv_prot,
  431. const unsigned char *key,
  432. const unsigned int key_len,
  433. const unsigned char *buffer,
  434. const unsigned int buffer_len,
  435. unsigned char *out_buffer,
  436. unsigned int *out_buffer_len,
  437. const unsigned char *privacy_params,
  438. const unsigned int privacy_params_len,
  439. const unsigned long engine_boots,
  440. const unsigned long engine_time);
  441. /**
  442. * Get the length of the authentication parameters field of the given
  443. * authentication protocol.
  444. */
  445. int get_auth_params_len(const int auth_prot);
  446. /**
  447. * Get the length of the privacy parameters field of the given
  448. * privacy protocol.
  449. */
  450. int get_priv_params_len(const int priv_prot);
  451. /**
  452. * Fill in the authentication field of an outgoing message
  453. */
  454. int auth_out_msg(const int auth_prot,
  455. const unsigned char *key,
  456. unsigned char *msg,
  457. const int msg_len,
  458. unsigned char *auth_par_ptr);
  459. /**
  460. * Check the authentication field of an incoming message
  461. */
  462. int auth_inc_msg(const int auth_prot,
  463. const unsigned char *key,
  464. unsigned char *msg,
  465. const int msg_len,
  466. unsigned char *auth_par_ptr,
  467. const int auth_par_len);
  468. private:
  469. AuthPtr *auth; ///< Array of pointers to Auth-objects
  470. PrivPtr *priv; ///< Array of pointers to Priv-objects
  471. int auth_size; ///< current size of the auth array
  472. int priv_size; ///< current size of the priv array
  473. pp_uint64 salt; ///< current salt value (64 bits)
  474. };
  475. /**
  476. * Authentication module using SHA.
  477. *
  478. * @see Auth
  479. */
  480. class DLLOPT AuthSHA: public Auth
  481. {
  482. public:
  483. int password_to_key(const unsigned char *password,
  484. const unsigned int password_len,
  485. const unsigned char *engine_id,
  486. const unsigned int engine_id_len,
  487. unsigned char *key,
  488. unsigned int *key_len);
  489. int hash(const unsigned char *data,
  490. const unsigned int data_len,
  491. unsigned char *digest) const;
  492. int auth_out_msg(const unsigned char *key,
  493. unsigned char *msg,
  494. const int msg_len,
  495. unsigned char *auth_par_ptr);
  496. int auth_inc_msg(const unsigned char *key,
  497. unsigned char *msg,
  498. const int msg_len,
  499. unsigned char *auth_par_ptr,
  500. const int auth_par_len);
  501. int get_id() const { return SNMP_AUTHPROTOCOL_HMACSHA; };
  502. const char *get_id_string() const { return "HMAC-SHA"; };
  503. int get_auth_params_len() const { return 12; };
  504. int get_hash_len() const { return SNMPv3_AP_OUTPUT_LENGTH_SHA;};
  505. };
  506. /**
  507. * Authentication module using MD5.
  508. *
  509. * @see Auth
  510. */
  511. class DLLOPT AuthMD5: public Auth
  512. {
  513. public:
  514. int password_to_key(const unsigned char *password,
  515. const unsigned int password_len,
  516. const unsigned char *engine_id,
  517. const unsigned int engine_id_len,
  518. unsigned char *key,
  519. unsigned int *key_len);
  520. int hash(const unsigned char *data,
  521. const unsigned int data_len,
  522. unsigned char *digest) const;
  523. int auth_out_msg(const unsigned char *key,
  524. unsigned char *msg,
  525. const int msg_len,
  526. unsigned char *auth_par_ptr);
  527. int auth_inc_msg(const unsigned char *key,
  528. unsigned char *msg,
  529. const int msg_len,
  530. unsigned char *auth_par_ptr,
  531. const int auth_par_len);
  532. int get_id() const { return SNMP_AUTHPROTOCOL_HMACMD5; };
  533. const char *get_id_string() const { return "HMAC-MD5"; };
  534. int get_auth_params_len() const { return 12; };
  535. int get_hash_len() const { return SNMPv3_AP_OUTPUT_LENGTH_MD5;};
  536. };
  537. /**
  538. * Encryption module using DES.
  539. *
  540. * @see Priv
  541. */
  542. class DLLOPT PrivDES: public Priv
  543. {
  544. public:
  545. #if defined(_USE_LIBTOMCRYPT) && !defined(_USE_OPENSSL)
  546. PrivDES();
  547. private:
  548. int cipher;
  549. public:
  550. #endif
  551. int encrypt(const unsigned char *key,
  552. const unsigned int key_len,
  553. const unsigned char *buffer,
  554. const unsigned int buffer_len,
  555. unsigned char *out_buffer,
  556. unsigned int *out_buffer_len,
  557. unsigned char *privacy_params,
  558. unsigned int *privacy_params_len,
  559. const unsigned long engine_boots,
  560. const unsigned long engine_time);
  561. int decrypt(const unsigned char *key,
  562. const unsigned int key_len,
  563. const unsigned char *buffer,
  564. const unsigned int buffer_len,
  565. unsigned char *out_buffer,
  566. unsigned int *out_buffer_len,
  567. const unsigned char *privacy_params,
  568. const unsigned int privacy_params_len,
  569. const unsigned long engine_boots,
  570. const unsigned long engine_time);
  571. int extend_short_key(const unsigned char *password,
  572. const unsigned int password_len,
  573. const unsigned char *engine_id,
  574. const unsigned int engine_id_len,
  575. unsigned char *key,
  576. unsigned int *key_len,
  577. const unsigned int max_key_len,
  578. Auth *auth)
  579. { return SNMPv3_USM_ERROR; /* not needed for DES! */ };
  580. int get_id() const { return SNMP_PRIVPROTOCOL_DES; };
  581. const char *get_id_string() const { return "DES"; };
  582. int get_priv_params_len() const { return 8; };
  583. int get_min_key_len() const { return 16; };
  584. void fix_key_len(unsigned int &key_len) const
  585. { key_len = (key_len >= 16 ? 16 : 0); };
  586. };
  587. #ifdef _USE_IDEA
  588. /**
  589. * Encryption module using IDEA.
  590. *
  591. * @see Priv
  592. */
  593. class DLLOPT PrivIDEA: public Priv
  594. {
  595. public:
  596. int encrypt(const unsigned char *key,
  597. const unsigned int key_len,
  598. const unsigned char *buffer,
  599. const unsigned int buffer_len,
  600. unsigned char *out_buffer,
  601. unsigned int *out_buffer_len,
  602. unsigned char *privacy_params,
  603. unsigned int *privacy_params_len,
  604. const unsigned long engine_boots,
  605. const unsigned long engine_time);
  606. int decrypt(const unsigned char *key,
  607. const unsigned int key_len,
  608. const unsigned char *buffer,
  609. const unsigned int buffer_len,
  610. unsigned char *out_buffer,
  611. unsigned int *out_buffer_len,
  612. const unsigned char *privacy_params,
  613. const unsigned int privacy_params_len,
  614. const unsigned long engine_boots,
  615. const unsigned long engine_time);
  616. int extend_short_key(const unsigned char *password,
  617. const unsigned int password_len,
  618. const unsigned char *engine_id,
  619. const unsigned int engine_id_len,
  620. unsigned char *key,
  621. unsigned int *key_len,
  622. const unsigned int max_key_len,
  623. Auth *auth)
  624. { return SNMPv3_USM_ERROR; /* not needed for IDEA! */ };
  625. int get_id() const { return SNMP_PRIVPROTOCOL_IDEA; };
  626. const char *get_id_string() const { return "IDEA"; };
  627. int get_priv_params_len() const { return 8; };
  628. int get_min_key_len() const { return 16; };
  629. void fix_key_len(unsigned int &key_len) const
  630. { key_len = (key_len >= 16 ? 16 : 0); };
  631. };
  632. #endif
  633. #if defined(_USE_LIBTOMCRYPT) || defined(_USE_OPENSSL)
  634. /**
  635. * Encryption module using AES (only available with libtomcrypt).
  636. *
  637. * @see Priv
  638. */
  639. class DLLOPT PrivAES: public Priv
  640. {
  641. public:
  642. PrivAES(const int aes_type_);
  643. int encrypt(const unsigned char *key,
  644. const unsigned int key_len,
  645. const unsigned char *buffer,
  646. const unsigned int buffer_len,
  647. unsigned char *out_buffer,
  648. unsigned int *out_buffer_len,
  649. unsigned char *privacy_params,
  650. unsigned int *privacy_params_len,
  651. const unsigned long engine_boots,
  652. const unsigned long engine_time);
  653. int decrypt(const unsigned char *key,
  654. const unsigned int key_len,
  655. const unsigned char *buffer,
  656. const unsigned int buffer_len,
  657. unsigned char *out_buffer,
  658. unsigned int *out_buffer_len,
  659. const unsigned char *privacy_params,
  660. const unsigned int privacy_params_len,
  661. const unsigned long engine_boots,
  662. const unsigned long engine_time);
  663. int extend_short_key(const unsigned char *password,
  664. const unsigned int password_len,
  665. const unsigned char *engine_id,
  666. const unsigned int engine_id_len,
  667. unsigned char *key,
  668. unsigned int *key_len,
  669. const unsigned int max_key_len,
  670. Auth *auth);
  671. int get_id() const { return aes_type; };
  672. const char *get_id_string() const;
  673. int get_priv_params_len() const { return 8; };
  674. int get_min_key_len() const { return key_bytes; };
  675. void fix_key_len(unsigned int &key_len) const
  676. { key_len = (key_len >= (unsigned)key_bytes ? key_bytes : 0); };
  677. private:
  678. int aes_type;
  679. int key_bytes;
  680. int rounds;
  681. #if defined(_USE_LIBTOMCRYPT) && !defined(_USE_OPENSSL)
  682. int cipher;
  683. #endif
  684. int need_byteswap;
  685. };
  686. #endif // _USE_LIBTOMCRYPT or _USE_OPENSSL
  687. #ifdef _USE_3DES_EDE
  688. /**
  689. * Encryption module using TripleDES-EDE KEY
  690. *
  691. *
  692. * @see Priv
  693. */
  694. #define TRIPLEDES_EDE_KEY_LEN 32
  695. class DLLOPT Priv3DES_EDE: public Priv
  696. {
  697. public:
  698. #if defined(_USE_LIBTOMCRYPT) && !defined(_USE_OPENSSL)
  699. Priv3DES_EDE();
  700. private:
  701. int cipher;
  702. public:
  703. #endif
  704. int encrypt(const unsigned char *key,
  705. const unsigned int key_len,
  706. const unsigned char *buffer,
  707. const unsigned int buffer_len,
  708. unsigned char *out_buffer,
  709. unsigned int *out_buffer_len,
  710. unsigned char *privacy_params,
  711. unsigned int *privacy_params_len,
  712. const unsigned long engine_boots,
  713. const unsigned long engine_time);
  714. int decrypt(const unsigned char *key,
  715. const unsigned int key_len,
  716. const unsigned char *buffer,
  717. const unsigned int buffer_len,
  718. unsigned char *out_buffer,
  719. unsigned int *out_buffer_len,
  720. const unsigned char *privacy_params,
  721. const unsigned int privacy_params_len,
  722. const unsigned long engine_boots,
  723. const unsigned long engine_time);
  724. int extend_short_key(const unsigned char *password,
  725. const unsigned int password_len,
  726. const unsigned char *engine_id,
  727. const unsigned int engine_id_len,
  728. unsigned char *key,
  729. unsigned int *key_len,
  730. const unsigned int max_key_len,
  731. Auth *auth);
  732. int get_id() const { return SNMP_PRIVPROTOCOL_3DESEDE; };
  733. const char *get_id_string() const { return "3DESEDE"; };
  734. int get_priv_params_len() const { return 8; };
  735. int get_min_key_len() const { return TRIPLEDES_EDE_KEY_LEN; };
  736. void fix_key_len(unsigned int &key_len) const
  737. { key_len = (key_len >= TRIPLEDES_EDE_KEY_LEN
  738. ? TRIPLEDES_EDE_KEY_LEN : 0); };
  739. #ifdef _TEST
  740. bool test();
  741. #endif
  742. };
  743. #endif // _USE_3DES_EDE
  744. #ifdef SNMP_PP_NAMESPACE
  745. } // end of namespace Snmp_pp
  746. #endif
  747. #endif // _SNMPv3
  748. #endif