zrtp.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. /*
  2. * libZRTP SDK library, implements the ZRTP secure VoIP protocol.
  3. * Copyright (c) 2006-2009 Philip R. Zimmermann. All rights reserved.
  4. * Contact: http://philzimmermann.com
  5. * For licensing and other legal details, see the file zrtp_legal.c.
  6. *
  7. * Viktor Krykun <v.krikun at zfoneproject.com>
  8. */
  9. /**
  10. * \file zrtp.h
  11. * \brief Defines basic libzrtp functions and data types
  12. */
  13. #ifndef __ZRTP_H__
  14. #define __ZRTP_H__
  15. #include "zrtp_config.h"
  16. #include "zrtp_base.h"
  17. #include "zrtp_error.h"
  18. #include "zrtp_types.h"
  19. #include "zrtp_protocol.h"
  20. #include "zrtp_engine.h"
  21. #include "zrtp_crypto.h"
  22. #include "zrtp_iface.h"
  23. #include "zrtp_iface_system.h"
  24. #include "zrtp_iface_scheduler.h"
  25. #include "zrtp_list.h"
  26. #include "zrtp_legal.h"
  27. #include "zrtp_log.h"
  28. #include "zrtp_srtp.h"
  29. #include "zrtp_srtp_builtin.h"
  30. #include "zrtp_string.h"
  31. #include "zrtp_pbx.h"
  32. #include "zrtp_legal.h"
  33. #include "zrtp_version.h"
  34. #include "zrtp_iface_cache.h"
  35. #include "zrtp_ec.h"
  36. /**
  37. * \defgroup zrtp_api API
  38. *
  39. * In this section the basic functions for using the library are defined. They include
  40. * initialization and deinitialization functions, functions for session and stream management and
  41. * functions for RTP traffic management.
  42. *
  43. * In most cases this section is all you need to start working with libzrtp. The typical simplified
  44. * order of operations in using libzrtp is the following:
  45. * -# library configuration
  46. * -# library initialization;
  47. * -# ZRTP session creation and configuration;
  48. * -# ZRTP stream attaching and Protocol initiation;
  49. * -# RTP stream processing;
  50. * -# ZRTP protocol stopping and releasing resources.
  51. * For each of these actions there is a set of corresponding functions.
  52. * \sa
  53. * - \ref howto
  54. * - \ref XXX_GUIDE
  55. */
  56. /*======================================================================*/
  57. /* Public ZRTP libzrtp datatypes */
  58. /*======================================================================*/
  59. /**
  60. * \defgroup zrtp_types Types and Definitions
  61. * \ingroup zrtp_api
  62. * The data types used in libzrtp are defined in this section
  63. * \{
  64. *
  65. */
  66. /**
  67. * \typedef typedef uint32_t zrtp_id_t;
  68. * \brief libzrtp general identifier used to debug connections management.
  69. * \ingroup zrtp_main_init
  70. */
  71. /** Length of "zrtp-hash-value", RFC 6189 sec 8. @sa zrtp_signaling_hash_get(); */
  72. #define ZRTP_SIGN_ZRTP_HASH_LENGTH (ZRTP_MESSAGE_HASH_SIZE*2)
  73. /**
  74. * \brief Enumeration for ZRTP Licensing modes
  75. * \ingroup zrtp_main_init
  76. *
  77. * A ZRTP endpoint that is Passive will never send a Commit message, which means that it cannot be
  78. * the initiator in the ZRTP exchange. Since at least one of the two parties must be the initiator,
  79. * two Passive endpoints cannot make a secure connection. However, a non-Passive ZRTP endpoint can
  80. * send a Commit message, enabling it to act as the initiator in a ZRTP exchange. This allows it to
  81. * make a secure connection to a Passive endpoint, or to another non-Passive endpoint.
  82. *
  83. * In addition, a Passive ZRTP endpoint declares that it is Passive by setting the passive flag in
  84. * the Hello message, which means the other party will recognize it as Passive. This allows for a
  85. * Passive mode and two forms of Active mode-- Active, or Unlimited. These three possible behaviors
  86. * for a ZRTP endpoint are defined as:
  87. * - \b Passive: Never send a Commit message, and thus can never be the initiator.
  88. * - \b Active: Will send a Commit message, but only to non-Passive ZRTP partners.
  89. * - \b Unlimited: Will send a Commit message to any ZRTP partner, Passive or non-Passive.
  90. *
  91. * This can be used to provide three classes of service, which can be licensed t different price
  92. * points. Passive can be used in freeware for widest possible deployment, Active can be used in
  93. * discount products that can only talk to non-freeware, and Unlimited can be used in full-price
  94. * products that will benefit from the network effect of widely deployed Passive freeware.
  95. */
  96. typedef enum zrtp_license_mode_t
  97. {
  98. /** @brief Never send a Commit message, and thus can never be the initiator. */
  99. ZRTP_LICENSE_MODE_PASSIVE = 0,
  100. /** @brief Will initiate ZRTP exchange, but only to non-Passive ZRTP partners. */
  101. ZRTP_LICENSE_MODE_ACTIVE,
  102. /** @brief Will send a Commit message to any ZRTP partner, Passive or non-Passive. */
  103. ZRTP_LICENSE_MODE_UNLIMITED
  104. } zrtp_license_mode_t;
  105. /**
  106. * @brief Enumeration to define Signaling initiator/responder roles.
  107. *
  108. * Used by libzrtp to optimize some internal processes and protocol handshake.
  109. *
  110. * @sas zrtp_stream_start().
  111. */
  112. typedef enum zrtp_signaling_role_t
  113. {
  114. /** @brief Unknown Signaling role, should be used when the app can't determine the role. */
  115. ZRTP_SIGNALING_ROLE_UNKNOWN = 0,
  116. /** @brief Signaling Initiator. */
  117. ZRTP_SIGNALING_ROLE_INITIATOR,
  118. /** @brief Signaling Responder. */
  119. ZRTP_SIGNALING_ROLE_RESPONDER,
  120. ZRTP_SIGNALING_ROLE_COUNT
  121. } zrtp_signaling_role_t;
  122. /** @brief 12-byte ZID for unique ZRTP endpoint identification. */
  123. typedef unsigned char zrtp_zid_t[12];
  124. /** \brief 16-byte ID for ZRTP endpoint's software identification. */
  125. typedef char zrtp_client_id_t[16];
  126. /**
  127. * @brief ZRTP global configuration options
  128. * @ingroup zrtp_main_init
  129. * @warning Use \ref zrtp_config_defaults() before start configuring this structure.
  130. */
  131. typedef struct zrtp_config_t
  132. {
  133. /** @brief Symbolic client identifier */
  134. zrtp_client_id_t client_id;
  135. /** @brief libzrtp license mode defined protocol behavior */
  136. zrtp_license_mode_t lic_mode;
  137. /** @brief Set this flag to 1 if you product is MiTM box */
  138. uint8_t is_mitm;
  139. /** @brief Set of interfaces required to operate with libzrtp */
  140. zrtp_callback_t cb;
  141. /** @brief Path to zrtp cache file (set if you use built-in realization) */
  142. zrtp_string256_t def_cache_path;
  143. /**
  144. * @brief Flush the cache automatically
  145. * Set to 1 if you want libzrtp to flush the cache to the persistent storage
  146. * right after it is modified. If cache_auto_store is 0, libzrtp will flush
  147. * the cache on going down only and the app is responsible for storing the
  148. * cache in unexpected situations. Enabled by default.
  149. *
  150. * @sa zrtp_def_cache_store()
  151. */
  152. unsigned cache_auto_store;
  153. } zrtp_config_t;
  154. /**
  155. * \brief zrtp stream information structure
  156. * \ingroup zrtp_main_management
  157. *
  158. * libzrtp, since v0.80 takes data encapsulating approach and hides all private data inside
  159. * zrtp_stream_t structure. Developers shouldn't access them directly. \ref zrtp_stream_get() should
  160. * be used instead to fill zrtp_stream_info_t structure. zrtp_stream_info_t contains all needed
  161. * information in safe and easy to use form.
  162. */
  163. struct zrtp_stream_info_t
  164. {
  165. /** \brief Stream unique identifier for debug purposes */
  166. zrtp_id_t id;
  167. /** \brief Pointer to the parent zrtp session */
  168. zrtp_session_t* session;
  169. /** \brief Stream mode. Defines libzrtp behavior related to specified contexts. */
  170. zrtp_stream_mode_t mode;
  171. /** \brief Defines ZRTP Trusted mitm mode for the current session. */
  172. zrtp_mitm_mode_t mitm_mode;
  173. /** \brief Reflects current state of ZRTP protocol */
  174. zrtp_state_t state;
  175. /**
  176. * \brief Last protocol error code
  177. *
  178. * Available for reading in ERROR state on zrtp_security_event_t#ZRTP_EVENT_PROTOCOL_ERROR.
  179. */
  180. zrtp_protocol_error_t last_error;
  181. /**
  182. * \brief Remote passive flag
  183. *
  184. * This flag shows when remote side is "passive" (has license mode PASSIVE) available in CLEAR
  185. * state and later.
  186. */
  187. uint8_t peer_passive;
  188. /**
  189. * \brief Allowclear flag.
  190. *
  191. * Current value of "allowclear" option exchanged during ZRTP negotiation. Available in SECURE
  192. * state.
  193. */
  194. uint8_t res_allowclear;
  195. /**
  196. * \brief Peer disclose bit flag
  197. *
  198. * Indicates the ability of the remote side to disclose its session key. Specifies that the
  199. * remote side allows call monitoring. If this flag is set, the end user must be informed. It
  200. * can be read in the SECURE state.
  201. */
  202. uint8_t peer_disclose;
  203. /**
  204. * \brief Defines that remote party is ZRTP MiTM endpoint
  205. *
  206. * Enabled by (Asterisk PBX, UMLab SIP Firewall or etc.) Available for reading in CLEAR state
  207. * ande later.
  208. */
  209. uint8_t peer_mitm;
  210. };
  211. /**
  212. * \brief zrtp session information structure
  213. * \ingroup zrtp_main_management
  214. * libzrtp, since v0.80 takes data incapsulating approach and hides all private date inside
  215. * zrtp_session_t structure. Developers shouldn't access them directly. \ref zrtp_session_get()
  216. * should be used instead to fill zrtp_session_info_t structure. zrtp_session_info_t contains all
  217. * needed information in safe and easy to use form.
  218. */
  219. struct zrtp_session_info_t
  220. {
  221. /** \brief Session unique identifier for debug purposes */
  222. zrtp_id_t id;
  223. /**
  224. * \brief Local ZID
  225. *
  226. The unique 12-characters string that identifies the local ZRTP endpoint.This ID allows remote
  227. * peers to recognize this ZRTP endpoint.
  228. */
  229. zrtp_string16_t zid;
  230. /**
  231. * \brief Remote ZID
  232. *
  233. * Extracted from the Hello packet of the very first ZRTP stream. Uniquely identifies the remote
  234. * ZRTP peer.
  235. */
  236. zrtp_string16_t peer_zid;
  237. /** \brief Character name identified remote ZRTP endpoint.*/
  238. zrtp_string16_t peer_clientid;
  239. /** \brief ZRTP Protocol version supported by the remote endpoint. */
  240. zrtp_string16_t peer_version;
  241. /**
  242. * \brief Indicates that SAS related data is available for reading.
  243. * \note
  244. * As SAS is computed in SECURE state only, it may contain unknown values in other states. Check
  245. * sas_is_ready before displaying SAS to the user.
  246. */
  247. uint8_t sas_is_ready;
  248. /** \brief First Short Authentication String */
  249. zrtp_string16_t sas1;
  250. /**
  251. * \brief Second Short Authentication string.
  252. * \note
  253. * Second SAS is available for \c base256 authentication only (\c sas_is_base256 is set). In
  254. * other case, \c sas1 contains \c base32 value and \c sas2 is empty.
  255. */
  256. zrtp_string16_t sas2;
  257. /** \brief Binary SAS digest (ZRTP_SAS_DIGEST_LENGTH bytes) */
  258. zrtp_string32_t sasbin;
  259. /**
  260. * \brief Bit-map to summarize shared secrets "Cached" flags.
  261. *
  262. * 1 at appropriate bit means that the secrets was found in the cache and restored successfully.
  263. * Value equal to 0 indicates that secret for the remote endpoint was not found in the cache
  264. * and was generated randomly.
  265. * Use ZRTP_BIT_RS1, ZRTP_BIT_RS2, ZRTP_BIT_AUX and ZRTP_BIT_PBX bit-masks to get "cached" value
  266. * for the appropriate secret.
  267. */
  268. uint32_t cached_flags;
  269. /**
  270. * \brief Bit-map to summarize shared secrets "Matched" flags.
  271. *
  272. * 1 at appropriate bit means that the secret, locally computed by your ZRTP endpoint is equal
  273. * to the secret, received from the remote endpoint. Secrets may not match if one of the
  274. * endpoints doesn't use cache of the shared secrets, if the cache was deleted or in case of
  275. * an attack.
  276. * Use ZRTP_BIT_RS1, ZRTP_BIT_RS2, ZRTP_BIT_AUX and ZRTP_BIT_PBX bit-masks to get "cached" value
  277. * for the appropriate secret.
  278. */
  279. uint32_t matches_flags;
  280. /**
  281. * \brief Bit-map to summarize shared secrets "Wrong" flags.
  282. *
  283. * 1 at appropriate bit means that the secret was restored from the cache, but doesn't match
  284. * to the remote endpoint's secret. Such situation may happen if the remote endpoint lost cache
  285. * or in case of attach.
  286. * Use ZRTP_BIT_RS1, ZRTP_BIT_RS2, ZRTP_BIT_AUX and ZRTP_BIT_PBX bit-masks to get "cached" value
  287. * for the appropriate secret.
  288. */
  289. uint32_t wrongs_flags;
  290. /**
  291. * \brief SAS Verification flag.
  292. *
  293. * The SAS Verified flag (V) is set based on the user indicating that SAS comparison has been
  294. * successfully performed. Each party sends the SAS Verified flag from the previous session in
  295. * the Confirm message of the current session.
  296. * \sa
  297. * - ZRTP RFC section. "7.1. SAS Verified Flag" for more information about Verification Flag.
  298. * - zrtp_verified_set()
  299. */
  300. uint32_t sas_is_verified;
  301. /** \brief Indicates base256 SAS encoding */
  302. uint8_t sas_is_base256;
  303. /**
  304. * \brief actual lifetime of the secrets
  305. *
  306. * This variable contains the interval for retaining secrets within an established session. In
  307. * accordance with ZRTP RFC this value is calculated as the minimal of local and remote TTLs
  308. * after confirmation. Value is given in seconds and can be read in the SECURE state.
  309. */
  310. uint32_t secrets_ttl;
  311. /** \brief Hash crypto component name used in ZRTP calculations. */
  312. zrtp_string32_t hash_name;
  313. /** \brief Cipher crypto component name used in ZRTP encryption. */
  314. zrtp_string32_t cipher_name;
  315. /** \brief SRTP Authentication crypto component name used in ZRTP exchange. */
  316. zrtp_string32_t auth_name;
  317. /** \brief SAS scheme crypto component name used in ZRTP exchange. */
  318. zrtp_string32_t sas_name;
  319. /** \brief Publik Key Exchange name used in ZRTP exchange. */
  320. zrtp_string32_t pk_name;
  321. };
  322. /* \} */
  323. /*======================================================================*/
  324. /* libzrtp Public API: Streams management */
  325. /*======================================================================*/
  326. #if defined(__cplusplus)
  327. extern "C"
  328. {
  329. #endif
  330. /**
  331. * \defgroup zrtp_main_init Initalization and Configuration
  332. * \ingroup zrtp_api
  333. * \{
  334. */
  335. /**
  336. * \brief Initializes libzrtp global config
  337. *
  338. * zrtp_config_defaults() prepares all fields of zrtp_config_t for further usage in zrtp_init().
  339. * This function allocates all necessary resources and initialize zrtp_config_t#cb with default
  340. * implementations.
  341. *
  342. * \param config - libzrtp config for initialization.
  343. * \warning this function must be used before start operating with the config.
  344. */
  345. void zrtp_config_defaults(zrtp_config_t* config);
  346. /**
  347. * \brief Initializing libzrtp
  348. *
  349. * This function initializes the library and all its components. zrtp_init() initialize global data
  350. * for all sessions and streams. Fields of the global zrtp context are initialized automatically and
  351. * shouldn't be modified. For correct memory management, global context should be released by
  352. * calling zrtp_down().
  353. *
  354. * \param config - libzrtp inital parameters
  355. * \param zrtp - out parameter, pointer to allocated zrtp global context structure;
  356. * \warning this function \b must be called before any operation with libzrtp.
  357. * \return
  358. * - zrtp_status_ok in successfully initialized or one of zrtp status errors in other case.
  359. * \sa zrtp_down()
  360. */
  361. zrtp_status_t zrtp_init(zrtp_config_t* config, zrtp_global_t** zrtp);
  362. /*!
  363. * \brief Shutting down the library
  364. *
  365. * Frees all allocated structures and resources. This function \b must be called at the end of use
  366. * to stop libzrtp correctly. zrtp_down() doesn't stop in-progress ZRTP streams. To avoid mistakes,
  367. * close all sessions before library deinitialization.
  368. *
  369. * \param zrtp - global ZRTP context previously allocated by zrtp_init();
  370. * \return
  371. * - zrtp_status_ok if successfully shut down;
  372. * - zrtp_status_fail if an error occurred.
  373. * \sa zrtp_init()
  374. */
  375. zrtp_status_t zrtp_down(zrtp_global_t* zrtp);
  376. /* \} */
  377. /**
  378. * \defgroup zrtp_main_management ZRTP Connections
  379. * \ingroup zrtp_api
  380. * \{
  381. */
  382. /**
  383. * \brief ZRTP Session Initialization.
  384. *
  385. * This function allocates and initializes the internal session context data. The given context is
  386. * associated with the specified ZRTP identifier. Only after initialization does the session contain
  387. * ZRTP_MAX_STREAMS_PER_SESSION streams ready to be used.
  388. *
  389. * After successfully initialization, configuration will be done according to the relevant profile
  390. * \c profile. Profile will be applyed to every stream allocated within this session. Before using
  391. * the profile, call zrtp_profile_check() function to make sure that the profile you are applying
  392. * is correct.
  393. *
  394. * \warning Don't call zrtp_session_init() in parallel with other operations on this session.
  395. * \param zrtp - global libzrtp context;
  396. * \param profile - the session configuration profile. If value of this parameter is NULL, default
  397. * profile will be used. NULL profile usage is equivalent to calling zrtp_profile_defaults().
  398. * \param zid - ZRTP peer identificator.
  399. * \param role - identifies if the endpoint was the signaling initiator of the call. Used to
  400. * provide Passive Mode options to the developer. If your application doesn't control signaling
  401. * or you don't want to support Passive Mode features - set it to ZRTP_SIGNALING_ROLE_UNKNOWN.
  402. * \param session - allocated session structure.
  403. * \return
  404. * - zrtp_status_ok if initialization is successful;
  405. * - zrtp_status_fail if an error occurs.
  406. * \sa zrtp_session_down()
  407. */
  408. zrtp_status_t zrtp_session_init( zrtp_global_t* zrtp,
  409. zrtp_profile_t* profile,
  410. zrtp_zid_t zid,
  411. zrtp_signaling_role_t role,
  412. zrtp_session_t **session);
  413. /**
  414. * \brief ZRTP Session context deinitialization
  415. *
  416. * This function releases all resources allocated for internal context operations by zrtp_init().
  417. *
  418. * \warning Don't call zrtp_session_init() in parallel with other operations on this session.
  419. * \param session - session for deinitialization.
  420. * \sa zrtp_session_init()
  421. */
  422. void zrtp_session_down(zrtp_session_t *session);
  423. /**
  424. * \brief Obtain information about ZRTP session
  425. *
  426. * Function initialize and fills all fields of zrtp_session_info_t structure according to
  427. * the current state of ZRTP session.
  428. *
  429. * \param session - zrtp session which parameters should be extracted;
  430. * \param info - out structure to be initialized.
  431. * \return
  432. * - zrtp_status_ok in case of success.
  433. * - zrtp_status_fail if an error occurs.
  434. */
  435. zrtp_status_t zrtp_session_get(zrtp_session_t *session, zrtp_session_info_t *info);
  436. /**
  437. * \brief Allow user to associate some data with current zrtp session.
  438. * \param session - zrtp session to attach data to.
  439. * \param udata - pointer to the user-data context.
  440. * \sa zrtp_session_get_userdata()
  441. */
  442. void zrtp_session_set_userdata(zrtp_session_t *session, void* udata);
  443. /**
  444. * \brief Return user data associated with the zrtp session
  445. * \param session - zrtp session to extract user data.
  446. * \return
  447. * - pointer to the user-data context previously set by zrtp_session_set_userdata().
  448. * - NULL if the user data unavailable.
  449. * \sa zrtp_session_set_userdata()
  450. */
  451. void* zrtp_session_get_userdata(zrtp_session_t *session);
  452. /**
  453. * \brief Attaching a new stream to the session
  454. *
  455. * This function call initializes a ZRTP stream and prepares it for use within the specified
  456. * session. The maximum number of streams for one session is defined by the
  457. * ZRTP_MAX_STREAMS_PER_SESSION variable. All newly created streams are equivalent and have
  458. * ZRTP_STREAM_MODE_CLEAR mode and ZRTP_ACTIVE state. Only after attaching a stream, ZRTP protocol
  459. * can be initiated.
  460. *
  461. * \param session - the ZRTP session within which a new stream is to be
  462. * \param stream - out parameter, attached stream will be stored there
  463. * \return
  464. * - zrtp_status_ok if stream was attached successfully
  465. * - one of zrtp_status_t errors in case of failure
  466. * \sa zrtp_stream_start() zrtp_stream_stop()
  467. */
  468. zrtp_status_t zrtp_stream_attach(zrtp_session_t *session, zrtp_stream_t** stream);
  469. /**
  470. * \brief Starting a ZRTP stream
  471. *
  472. * ZRTP stream setup is initiated by calling this function. Exchange of command packets begins
  473. * immediately according to protocol. If the option "autosecure" is on, calling this function is the
  474. * only requirement for setting up the ZRTP connection within a stream. If "autosecure" mode is not
  475. * available, calling this function activates only connection within a ZRTP stream. A connection can
  476. * be established manually later by calling zrtp_stream_secure().
  477. *
  478. * Setup of the stream/connection takes a certain interval of time. This function just initiates
  479. * this process. The system of callbacks informs the user about the progress of libzrtp protocol.
  480. *
  481. * \param stream - ZRTP stream to be started.
  482. * \param ssrc - ssrc which will be used in ZRTP protocol messages. It should match with ssrc of
  483. * appropriate RTP stream which will be encrypted by this ZRTP stream.
  484. * \return
  485. * - zrtp_status_ok in case of success;
  486. * - one of zrtp_status_t errors in case of failure
  487. * \sa
  488. * - \ref XXX_GUIDE_CB \ref XXX_GUIDE_MANAGEMENT
  489. * - zrtp_stream_stop() zrtp_stream_secure() zrtp_stream_clear()
  490. */
  491. zrtp_status_t zrtp_stream_start(zrtp_stream_t* stream,
  492. uint32_t ssrc);
  493. /**
  494. * \brief ZRTP protocol stopping
  495. *
  496. * This function stops all protocol operations for the specified stream, releases resources
  497. * allocated on the zrtp_stream_start() and prepares the stream structure for the next use.
  498. *
  499. * This function will stop the protocol at any stage: all delayed tasks are canceled, and the
  500. * protocol packet exchange and encryption is stopped. After this function call it is necessary to
  501. * stop processing traffic using the zrtp_process_xxx() function.
  502. *
  503. * \param stream - the stream being shutdown.
  504. * \return
  505. * - zrtp_status_ok in case of success;
  506. * - one of zrtp_status_t errors in case of failure
  507. * \sa
  508. * - \ref XXX_GUIDE_CB \ref XXX_GUIDE_MANAGEMENT
  509. * - zrtp_stream_start() zrtp_stream_secure() zrtp_stream_clear()
  510. */
  511. zrtp_status_t zrtp_stream_stop(zrtp_stream_t* stream);
  512. /*!
  513. * \brief Initiating an interruption of the secure connection
  514. *
  515. * This function initiates the shutting down of the ZRTP connection within a stream. In other words,
  516. * after successfully switching to secure mode (\ref XXX SECURE state, fig. 1.5), calling this
  517. * function begins the exchange of packets switching back to insecure (CLEAR) mode.
  518. *
  519. * This function can only be implemented from the SECURE state. Attempt to call this function from
  520. * any other state will end in failure. The client application is informed about protocol
  521. * progress through a system of callbacks.
  522. *
  523. * \param stream - ZRTP stream .
  524. * \return
  525. * - zrtp_status_ok - if shutting down the connection is started successfully.
  526. * - zrtp_status_fail - if shutting down the connection is initiated from an incorrect state.
  527. * \sa
  528. * - \ref XXX_GUIDE_CB \ref XXX_GUIDE_MANAGEMENT
  529. * - zrtp_stream_start() zrtp_stream_secure() zrtp_stream_clear()
  530. */
  531. zrtp_status_t zrtp_stream_clear(zrtp_stream_t *stream);
  532. /**
  533. * \brief Initiating a secure connection setup
  534. *
  535. * The function initiates a ZRTP connection setup within a stream. In other words, after the
  536. * protocol has started and Discovery phase have been successfully accomplished, calling this
  537. * function will begin the exchange of packets for switching to SECURE mode.
  538. *
  539. * This function can be successfully performed only from the CLEAR state (\ref XXX Figure 1.6).
  540. * Attempting to call this function from any other state will result in failure. The client
  541. * application is informed about protocol progress through a system of callbacks.
  542. *
  543. * \param stream - ZRTP stream to be secured.
  544. * \return
  545. * - zrtp_status_ok - if switching to secure mode started successfully.
  546. * - zrtp_status_fail - if switching to secure mode is initiated from a state other than CLEAR.
  547. * \sa
  548. * - \ref XXX_GUIDE_CB \ref XXX_GUIDE_MANAGEMENT.
  549. * - zrtp_stream_start() zrtp_stream_clear().
  550. */
  551. zrtp_status_t zrtp_stream_secure(zrtp_stream_t *stream);
  552. /**
  553. * \brief Obtain information about zrtp stream
  554. *
  555. * Function initialize and fills all fields of zrtp_stream_info_t structure accordint to
  556. * current state of zrtp stream.
  557. *
  558. * \param stream - zrtp stream which parameters should be extracted
  559. * \param info - out structure to be initialized
  560. * \return
  561. * - zrtp_status_ok in case of success.
  562. * - zrtp_status_fail if an error occurs.
  563. */
  564. zrtp_status_t zrtp_stream_get(zrtp_stream_t *stream, zrtp_stream_info_t *info);
  565. /**
  566. * @brief Allow user to associate some data with zrtp stream.
  567. * @param stream - zrtp stream to attach data to.
  568. * @param udata - pointer to the user-data context.
  569. * @sa zrtp_stream_get_userdata()
  570. */
  571. void zrtp_stream_set_userdata(zrtp_stream_t *stream, void* udata);
  572. /**
  573. * \brief Return user data associated with the zrtp stream
  574. * \return
  575. * - pointer to the user-data context previously set by zrtp_stream_set_userdata()
  576. * - NULL if user data unavailable;
  577. * \sa zrtp_stream_set_userdata()
  578. */
  579. void* zrtp_stream_get_userdata(const zrtp_stream_t *stream);
  580. /* \} */
  581. /*======================================================================*/
  582. /* libzrtp Public API: Encryption */
  583. /*======================================================================*/
  584. /**
  585. * \defgroup zrtp_main_proto Traffic Processing
  586. * \ingroup zrtp_api
  587. * \{
  588. */
  589. /**
  590. * \brief Processing outgoing RTP packets
  591. *
  592. * This is the main function for processing outgoing RTP packets. As soon as the protocol is
  593. * started, each outgoing RTP packet (not encrypted) has to go through this function.
  594. *
  595. * It performs different actions depending on the connection state and packet type:
  596. * - In setup ZRTP connection mode, it encrypts outgoing RTP packets. The packet is encrypted right
  597. * in the transferred buffer;
  598. * - Protects codec and data privacy by deleting certain packets from the stream. In this case the
  599. * body and the length of the packet remain unchanged.
  600. *
  601. * \param stream - ZRTP stream to process RTP packet;
  602. * \param packet - buffer storing the RTP packet for encryption. After processing, the encrypted
  603. * packet is stored in the same buffer.
  604. * \param length - the length of the buffered packet. After processing, the length of encrypted
  605. * packet is stored here.
  606. * \warning During encryption, the data length increases in comparison to the source data. Because
  607. * the function uses the same buffer both for incoming and resulting values, the length of the
  608. * buffer must be larger than size of source packet.
  609. * \return
  610. * - zrtp_status_ok if encryption is successful. The packet should be sent to the recipient.
  611. * - zrtp_status_fail if there was an error during encryption. The packet should be rejected.
  612. * - zrtp_status_drop if there was interference in the VoIP client codec protection mechanism. The
  613. * packet should be rejected.
  614. * \sa zrtp_process_srtp() zrtp_process_rtcp() zrtp_process_srtcp()
  615. */
  616. zrtp_status_t zrtp_process_rtp( zrtp_stream_t *stream,
  617. char* packet,
  618. unsigned int* length);
  619. /**
  620. * \brief Processing incoming RTP packets
  621. *
  622. * This is the main function for incoming RTP packets processing. It is an analogue of
  623. * zrtp_process_rtp() but for an incoming stream. After the protocol is started, each (encrypted)
  624. * incoming RTP packet has to go through this function.
  625. *
  626. * It performs different actions depending on the connection state and packet type:
  627. * - during setup/interruption of ZRTP connection, processes incoming protocol packets. The body
  628. * and length of the packet remain unchanged;
  629. * - in setup ZRTP connection mode, decrypts incoming RTP packet. The packet is decrypted right in
  630. * the transferred buffer;
  631. * - protects codec and data privacy by deleting certain packets from the stream. In this case the
  632. * body and the length of the packet remain unchanged.
  633. *
  634. * \param stream - ZRTP stream for processing
  635. * \param packet - buffer storing the packet for decrypting. After processing, the decrypted packet
  636. * is stored in the same buffer;
  637. * \param length - the length of the buffered packet. After processing, the length of decrypted
  638. * packet is stored here;
  639. * \return
  640. * - zrtp_status_ok if decrypting is successful. Such a packet should be sent to the recipient;
  641. * - zrtp_status_fail if an error occurred during decrypting or command packet processing. The
  642. * packet should be rejected;
  643. * - zrtp_status_drop if the command packet processing is successful or if there was interference
  644. * in the VoIP client codec protection mechanism. The packet should be rejected in either case;
  645. * \sa zrtp_process_rtp() zrtp_process_rtcp() zrtp_process_srtcp()
  646. */
  647. zrtp_status_t zrtp_process_srtp( zrtp_stream_t *stream,
  648. char* packet,
  649. unsigned int* length);
  650. /*!
  651. * \brief Processing outgoing RTCP packets
  652. *
  653. * This is the main function for processing outgoing RTCP packets. The function behavior is similar
  654. * to that of zrtp_process_rtp():
  655. * - In SECURE mode, encrypts outgoing RTCP packets. The packet is encrypted right in the
  656. * transferred buffer. The length of encrypted packet is returned in the \c length variable;
  657. * - protects codec and data privacy by deleting certain packets from the stream. In this case the
  658. * body and the length of the packet remain unchanged.
  659. *
  660. * \param stream - ZRTP session for processing;
  661. * \param packet - buffer storing RTCP packet;
  662. * \param length - length of the buffered packet.
  663. * \return
  664. * - zrtp_status_ok if encryption is successful. The packet should be sent to the recipient.
  665. * - zrtp_status_fail if there was an error during encryption. The packet should be rejected.
  666. * - zrtp_status_drop if there was interference in the VoIP client codec protection mechanism. The
  667. * packet should be rejected.
  668. * \sa zrtp_process_srtp() zrtp_process_rtp() zrtp_process_srtcp()
  669. */
  670. zrtp_status_t zrtp_process_rtcp( zrtp_stream_t *stream,
  671. char* packet,
  672. unsigned int* length);
  673. /**
  674. * \brief Processing incoming RTCP packets
  675. *
  676. * This is the main function for processing incoming RTCP packets. The function behavior is similar
  677. * to that of zrtp_process_srtp():
  678. * - In SECURE mode, decrypts incoming RTCP packets. The packet is decrypted right in the
  679. * transferred buffer. The length of the encrypted packet is returned in the \c length variable;
  680. * - In transition states, drops all incoming RTCP traffic. In this case the body and the length of
  681. * the packet remain unchanged.
  682. *
  683. * \param stream - ZRTP stream for processing;
  684. * \param packet - buffer storing the RTCP packet;
  685. * \param length - length of the buffered packet.
  686. * \return
  687. * - zrtp_status_ok if decrypting is successful. Such a packet should be sent to the recipient;
  688. * - zrtp_status_drop if the command packet processing is successful or if there was interference
  689. * in the VoIP client codec protection mechanism. The packet should be rejected in either case;
  690. * - zrtp_status_fail if there was an error during encryption. The packet should be rejected.
  691. * \sa zrtp_process_srtp() zrtp_process_rtp() zrtp_process_rtcp()
  692. */
  693. zrtp_status_t zrtp_process_srtcp( zrtp_stream_t *stream,
  694. char* packet,
  695. unsigned int* length);
  696. /* \} */
  697. /**
  698. * \defgroup zrtp_main_utils Utilities
  699. * \ingroup zrtp_api
  700. * \{
  701. */
  702. /**
  703. * \brief Specifies the hash of the peer Hello message for verification.
  704. *
  705. * In accordance with the ZRTP RFC sec. 9, this protocol can prevent DOS attacks by verification of
  706. * the Hello message hash sent through the signaling protocol.
  707. *
  708. * This function allows the user to specify the Hello hash for verification. If after the
  709. * discovering phase the Hello hashes don't match, libzrtp raises the
  710. * zrtp_event_t#ZRTP_EVENT_WRONG_SIGNALING_HASH event. This function should only be called before
  711. * starting the protocol from the ZRTP_STATE_ACTIVE state.
  712. *
  713. * \param stream - stream for operating with;
  714. * \param hash_buff - signaling hash buffer. Function accepts string, not a binary value!;
  715. * \param hash_buff_length - signaling hash length in bytes, must be ZRTP_SIGN_ZRTP_HASH_LENGTH bytes;
  716. * \return:
  717. * - zrtp_status_ok if the operation finished successfully
  718. * - one of the errors otherwise
  719. * \sa
  720. * - ZRTP RFC. sec 8;
  721. * - zrtp_signaling_hash_get()
  722. */
  723. zrtp_status_t zrtp_signaling_hash_set( zrtp_stream_t* stream,
  724. const char *hash_buff,
  725. uint32_t hash_buff_length);
  726. /**
  727. * \brief Returns the hash of the Hello message to be transferred in signaling.
  728. *
  729. * To prevent DOS attacks, the hash of the Hello message may be sent through signaling.
  730. * zrtp_signaling_hash_get() may be called after attaching the stream to receive the value of this
  731. * hash.
  732. *
  733. * \param stream - stream for operating with
  734. * \param hash_buff - buffer for storing signaling hash. Function returns already parsed hex string.
  735. * String is null-terminated. Buffer must be at least ZRTP_SIGN_ZRTP_HASH_LENGTH bytes length.
  736. * \param hash_buff_length - buffer length in bytes, non less than ZRTP_SIGN_ZRTP_HASH_LENGTH bytes.
  737. * \return:
  738. * - zrtp_status_ok if the operation finished successfully
  739. * - one of the errors otherwise
  740. * \sa
  741. * - ZRTP RFC. sec 8;
  742. * - zrtp_signaling_hash_set()
  743. */
  744. zrtp_status_t zrtp_signaling_hash_get(zrtp_stream_t* stream,
  745. char* hash_buff,
  746. uint32_t hash_buff_length);
  747. /**
  748. * \brief Changing the value of the secret's verification flag
  749. *
  750. * This function is used to change (set, unset) the secret's verification flag. zrtp_verified_set()
  751. * changes the relevant internal data and stores a flag in the cache.
  752. * \note
  753. * Special synchronization mechanisms are provided to protect the cache from race conditions. Don't
  754. * change the verified flag directly in the cache - use this function.
  755. *
  756. * \param zrtp - zrtp global data;
  757. * \param zid1 - ZID of the first party;
  758. * \param zid2 - ZID of the second party;
  759. * \param verified - Boolean value of the verified flag.
  760. * \return
  761. * - zrtp_status_ok - if successful;
  762. * - one of zrtp_status_t errors if fails.
  763. */
  764. zrtp_status_t zrtp_verified_set( zrtp_global_t *zrtp,
  765. zrtp_string16_t *zid1,
  766. zrtp_string16_t *zid2,
  767. uint8_t verified);
  768. /**
  769. * \brief Verifying the ZRTP profile
  770. *
  771. * zrtp_profile_check() checks the correctness of the values in the profile. The following checks
  772. * are performed:
  773. * - the number of components in each group does not exceed ZRTP_MAX_COMP_COUNT;
  774. * - the components declared are supported by the library kernel.
  775. * - presence of the set of obligatory components defined by ZRTP RFC.
  776. *
  777. * \param profile - ZRTP profile for validation;
  778. * \param zrtp - global ZRTP context.
  779. * \return
  780. * - zrtp_status_ok - if profile passed all available tests;
  781. * - one of ZRTP errors - if there are mistakes in the profile. See debug logging for additional
  782. * information.
  783. */
  784. zrtp_status_t zrtp_profile_check(const zrtp_profile_t* profile, zrtp_global_t* zrtp);
  785. /**
  786. * \brief Configure the default ZRTP profile
  787. *
  788. * These options are used:
  789. * \code
  790. * "active" is enabled;
  791. * "allowclear" is disabled by default and enabled for Zfone only;
  792. * "autosecure" is enabled;
  793. * "disclose_bit" is disabled;
  794. * cache_ttl = ZRTP_CACHE_DEFAULT_TTL defined by ZRTP RFC;
  795. *
  796. * [sas_schemes] = ZRTP_SAS_BASE256, ZRTP_SAS_BASE32;
  797. * [cipher_types] = ZRTP_CIPHER_AES128;
  798. * [pk_schemes] = ZRTP_PKTYPE_DH3072;
  799. * [auth_tag_lens] = ZRTP_ATL_HS32;
  800. * [hash_schemes] = ZRTP_HASH_SHA256;
  801. * \endcode
  802. *
  803. * \param profile - ZRTP stream profile for filling;
  804. * \param zrtp - libzrtp global context.
  805. */
  806. void zrtp_profile_defaults(zrtp_profile_t* profile, zrtp_global_t* zrtp);
  807. /**
  808. * \brief Search for a component in the profile by ID
  809. *
  810. * The utility function returning the position of an element of the specified type in the profile.
  811. * Used by libZRTP kernel and for external use.
  812. *
  813. * \param profile - ZRTP profile;
  814. * \param type - sought component type;
  815. * \param id - sought component ID.
  816. * \return
  817. * - component position - if component was found;
  818. * -1 - if the component with the specified ID can't be found in profile.
  819. */
  820. int zrtp_profile_find(const zrtp_profile_t* profile, zrtp_crypto_comp_t type, uint8_t id);
  821. /* \} */
  822. /**
  823. * \defgroup zrtp_main_rng Random Number Generation
  824. * \ingroup zrtp_api
  825. * \{
  826. * The generation of cryptographic key material is a highly sensitive process. To do this, you need
  827. * high entropy random numbers that an attacker cannot predict. This section \ref rng gives basic
  828. * knowliges andbot the RNG and it's implementation in libzrtp.
  829. * \warning
  830. * \ref rng \c MUST be read by every developer using libzrtp.
  831. */
  832. /**
  833. * \brief Entropy accumulation routine
  834. *
  835. * The random number generation scheme is described in detail in chapter \ref XXX. This function
  836. * gets \c length bytes of entropy from \c buffer and hashes it into the special storage. This
  837. * function should be called periodically from the user's space to increase entropy quality.
  838. * \warning
  839. * RNG is a very important and sensitive component of the crypto-system. Please, pay attention to
  840. * \ref rng.
  841. * \param zrtp - libzrtp global context;
  842. * \param buffer - pointer to the buffer with entropy for accumulating;
  843. * \param length - entropy size in bytes.
  844. * \return: number of hashed bytes.
  845. */
  846. int zrtp_entropy_add(zrtp_global_t* zrtp, const unsigned char *buffer, uint32_t length);
  847. /**
  848. * \brief Random string generation
  849. *
  850. * zrtp_randstr() generates \c length bytes of "random" data. We say "random" because the
  851. * "randomness" of the generated sequence depends on the quality of the entropy passed to
  852. * zrtp_entropy_add(). If the user provides "good" entropy, zrtp_randstr() generates sufficiently
  853. * "random" data.
  854. *
  855. * \param zrtp - libzrtp global context;
  856. * \param buffer - buffer into which random data will be generated;
  857. * \param length - length of required sequence in bytes.
  858. * \return
  859. * - length of generated sequence in bytes or -1 in case of error
  860. * \sa \ref rng
  861. */
  862. int zrtp_randstr(zrtp_global_t* zrtp, unsigned char *buffer, uint32_t length);
  863. int zrtp_randstr2(unsigned char *buffer, uint32_t length);
  864. /* \} */
  865. #if defined(__cplusplus)
  866. }
  867. #endif
  868. #endif /* __ZRTP_H__ */