zrtp_engine.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. #ifndef __ZRTP_ENGINE_H__
  10. #define __ZRTP_ENGINE_H__
  11. #include "zrtp_config.h"
  12. #include "zrtp_types.h"
  13. #include "zrtp_crypto.h"
  14. #if defined(__cplusplus)
  15. extern "C"
  16. {
  17. #endif
  18. /**
  19. * @defgroup engine_dev ZRTP Engine related types and functions
  20. * @ingroup zrtp_dev
  21. * \{
  22. */
  23. #define ZRTP_IS_STREAM_DH(stream) \
  24. (stream->mode == ZRTP_STREAM_MODE_DH)
  25. #define ZRTP_IS_STREAM_FAST(stream) \
  26. (stream->mode != ZRTP_STREAM_MODE_DH)
  27. #define ZRTP_IS_STREAM_MULT(stream) \
  28. (stream->mode == ZRTP_STREAM_MODE_MULT)
  29. #define ZRTP_IS_STREAM_PRESH(stream) \
  30. (stream->mode == ZRTP_STREAM_MODE_PRESHARED)
  31. /**
  32. * @brief Test Passive Rule N1
  33. * A passive endpoint never sends a Commit message. Semi-active endpoint does
  34. * not send a commit to a passive endpoint.
  35. * Return 1 if the tast have been passed successfully and 0 in other case.
  36. */
  37. #define ZRTP_PASSIVE1_TEST(stream) \
  38. ( (ZRTP_LICENSE_MODE_UNLIMITED == stream->zrtp->lic_mode) || \
  39. ((ZRTP_LICENSE_MODE_ACTIVE == stream->zrtp->lic_mode) && (!stream->messages.peer_hello.pasive)) )
  40. /**
  41. * @brief Test Passive Rule N2
  42. * A passive phone, if acting as a SIP initiator (meaning it initiated the call),
  43. * rejects all commit packets from everyone.
  44. * Return 1 if the tast have been passed successfully and 0 in other case
  45. */
  46. #define ZRTP_PASSIVE2_TEST(stream) \
  47. ( !((ZRTP_LICENSE_MODE_PASSIVE == stream->zrtp->lic_mode) && \
  48. (stream->session->signaling_role == ZRTP_SIGNALING_ROLE_INITIATOR)) )
  49. /**
  50. * @brief Test Passive Rule N3
  51. * A passive phone rejects all commit messages from a PBX.
  52. * Return 1 if the tast have been passed successfully and 0 in other case
  53. */
  54. #define ZRTP_PASSIVE3_TEST(stream) \
  55. ( !(!stream->zrtp->is_mitm && stream->peer_mitm_flag && \
  56. (ZRTP_LICENSE_MODE_PASSIVE == stream->zrtp->lic_mode)) )
  57. /*===========================================================================*/
  58. /* PROTOCOL Logic */
  59. /*===========================================================================*/
  60. /**
  61. * @brief Allocate ZRTP protocol structure
  62. * Allocates and initializes all necessary data according to the protocol mode.
  63. * Initializes required DH crypto context info and generates secret IDs.
  64. * @param stream - stream context in which protocol should be allocated;
  65. * @param is_initiator - defines protocol type (1 - initiator, 0 - responder).
  66. * @exception SOFTWARE exceptions.
  67. */
  68. zrtp_status_t _zrtp_protocol_init( zrtp_stream_t *stream,
  69. uint8_t is_initiator,
  70. zrtp_protocol_t **proto);
  71. /**
  72. * @brief Release protocol structure
  73. * Stops all replay tasks, clears all crypto sources and SRTP engine, and
  74. * releases memory. The protocol should be destroyed on: stream closing, or
  75. * switching to CLEAR or ERROR states.
  76. */
  77. void _zrtp_protocol_destroy(zrtp_protocol_t *proto);
  78. /**
  79. * @brief Encrypts RTP/RTCP media
  80. * After switching to Secure, the protocol structure is able to encrypt
  81. * media using the SRTP crypto-engine.
  82. * @param self - self-pointer to protocol instance;
  83. * @param packet - media packet for encryption;
  84. * @param is_rtp - defines type of media for encryption; value equal to 1
  85. * means RTP packet, 0 - RTCP.
  86. * @return
  87. * - zrtp_status_ok - if successfully encrypted;
  88. * - one of zrtp_status_t errors otherwise.
  89. */
  90. zrtp_status_t _zrtp_protocol_encrypt( zrtp_protocol_t *proto,
  91. zrtp_rtp_info_t *packet,
  92. uint8_t is_rtp);
  93. /**
  94. * @brief Decrypts RTP/RTCP media
  95. * After switching to Secure, the protocol structure is able to decrypt
  96. * media using the SRTP crypto-engine.
  97. * @param self - self-pointer to protocol instance;
  98. * @param packet - media packet for decryption;
  99. * @param is_rtp - defines type of media for decryption; value equal to 1
  100. * means RTP packet, 0 - RTCP.
  101. * @return
  102. * - zrtp_status_ok - if successfully decrypted;
  103. * - one of zrtp_status_t errors otherwise.
  104. */
  105. zrtp_status_t _zrtp_protocol_decrypt( zrtp_protocol_t *self,
  106. zrtp_rtp_info_t *packet,
  107. uint8_t is_rtp);
  108. /*===========================================================================*/
  109. /* CRTPTO Utilities */
  110. /*===========================================================================*/
  111. /**
  112. * ZRTP KDF function.
  113. * KDF(KI, Label, Context, L) = HMAC(KI, i | Label | 0x00 | Context | L). See
  114. * Section "4.5.1. The ZRTP Key Derivation Function" in ZRTP RFC for more info.
  115. * @param stream - used to obtain negotiated HMAC function and other parameters;
  116. * @param ki- secret key derivation key that is unknown to the wiretapper
  117. * (for example, s0);
  118. * @param label - string of nonzero octets that identifies the purpose for the
  119. * derived keying material;
  120. * @param context - includes ZIDi, ZIDr, and some optional nonce material;
  121. * @param length - needed digest length. (The output of the KDF is truncated to
  122. * the leftmost length bits);
  123. * @param digest - destination buffer.
  124. */
  125. zrtp_status_t _zrtp_kdf( zrtp_stream_t* stream,
  126. zrtp_stringn_t* ki,
  127. zrtp_stringn_t* label,
  128. zrtp_stringn_t* context,
  129. uint32_t length,
  130. zrtp_stringn_t* digest);
  131. /*!
  132. * \brief Allocate shared secret structure
  133. * This function allocates memory for a zrtp_shared_secret_t and initializes
  134. * the secret value using a zrtp_fill_shared_secret() function call. Used in
  135. * protocol allocating.
  136. * \param session - ZRTP session for access to global data.
  137. * \return
  138. * - allocated secrets - on success;
  139. * - NULL - if allocation fails.
  140. */
  141. zrtp_shared_secret_t *_zrtp_alloc_shared_secret(zrtp_session_t* session);
  142. /*!
  143. * \brief Restores secrets from the cache
  144. * Uploads retained secrets from the cache and initializes secret flags. If
  145. * the secret has expired (is_expired flag is set), its value will be randomly
  146. * regenerated. _zrtp_prepare_secrets() is called after the discovery phase on
  147. * the setting up the very first stream. After secrets are uploaded the
  148. * zrtp_secrets_t#_is_ready flag is enabled to prevent secrets from reinitialization
  149. * on setting up the next stream.
  150. * \param session - ZRTP session in which secrets should be restored.
  151. * - zrtp_status_ok - if secrets were restored successfully;
  152. * - one of zrtp_status_t errors in case of failure.
  153. */
  154. zrtp_status_t _zrtp_prepare_secrets(zrtp_session_t* session);
  155. /**
  156. * @brief Validate confirm chmac message.
  157. * In case of chmac failure it switches to Initiating Error state and generate
  158. * ZRTP_EVENT_WRONG_MESSAGE_HMAC security event.
  159. * @return
  160. * -1 - in case of error and 0 - on success.
  161. */
  162. int _zrtp_validate_message_hmac(zrtp_stream_t *stream, zrtp_msg_hdr_t* msg2check, char* hmackey);
  163. /**
  164. * @brief Computes preshared key using available secrets.
  165. * hash(len(rs1) | rs1 | len(auxsecret) | auxsecret | len(pbxsecret) | pbxsecret)
  166. * Result key stored in key variable, if key_id not NULL - hmac
  167. * of the preshared_key will be stored.
  168. * return
  169. * - zrtp_status_ok on success and one of libzrtp errors in case of failure
  170. */
  171. zrtp_status_t _zrtp_compute_preshared_key( zrtp_session_t *session,
  172. zrtp_stringn_t* rs1,
  173. zrtp_stringn_t* auxs,
  174. zrtp_stringn_t* pbxs,
  175. zrtp_stringn_t* key,
  176. zrtp_stringn_t* key_id);
  177. /** @brief Perform Key generation according to ZRTp RFC sec. 5.6 */
  178. zrtp_status_t _zrtp_set_public_value(zrtp_stream_t *stream, int is_initiator);
  179. /*===========================================================================*/
  180. /* PROTOCOL Utilites */
  181. /*===========================================================================*/
  182. /*!
  183. * \brief Check availability to start stream (DH or Preshared)
  184. * The ZRTP specification says that only one DH stream can be run at a time between
  185. * two ZRTP endpoints. So _zrtp_can_start_stream(DH) looks over all sessions
  186. * between two ZIDs and if any other stream is running it denies the start of
  187. * another DH stream in parallel. Although the ZRTP standard says that Preshared
  188. * or Multistream stream can't be run in parallel with DH streams between two
  189. * ZRTP endpoints. So _zrtp_can_start_stream(PRESH) looks over all sessions between
  190. * two ZIDs and if any other DH stream is running it denies the start of
  191. * Preshared/Multistream stream in parallel. All operations with sessions and
  192. * streams are protected by mutexes. Call this function every time before starting
  193. * "initiating secure" process. For internal use only.
  194. * \sa "break the tie schemes" internal document.
  195. * \param stream - ZRTP stream which going to be started;
  196. * \param conc - in this variable _zrtp_can_start_stream() returns pointer to the
  197. * concurrent DH stream if it's in progress. It's used in "breaking the tie"
  198. * scheme.
  199. * \param mode - stream mode.
  200. * \return
  201. * - 1 if stream can be started;
  202. * - 0 - if stream can't be started and should wait for concurrent stream
  203. * establishment.
  204. */
  205. int _zrtp_can_start_stream( zrtp_stream_t* stream,
  206. zrtp_stream_t** conc,
  207. zrtp_stream_mode_t mode);
  208. /** Return ZRTP Stream mode which should be used for current stream. */
  209. zrtp_stream_mode_t _zrtp_define_stream_mode(zrtp_stream_t* stream);
  210. /*!
  211. * \brief Chooses the best crypto component of the given type
  212. * Selects the crypto component according to the local initiator's profile and
  213. * the remote responder's Hello.
  214. * \param profile - local profile;
  215. * \param peer_hello - Hello packet, received from the remote peer;
  216. * \param type - type of the crypto component to be chosen.
  217. * \return:
  218. * - identifier of the chosen component (according to type);
  219. * - ZRTP_COMP_UNKN in case of error.
  220. */
  221. uint8_t _zrtp_choose_best_comp( zrtp_profile_t* profile,
  222. zrtp_packet_Hello_t* peer_hello,
  223. zrtp_crypto_comp_t type);
  224. /*!
  225. * \brief Computes replay timeouts
  226. * This function computes messages replays schedule. There are some recommended
  227. * values by ZRTP specification, but in some network environments values may be
  228. * sligh different
  229. */
  230. uint32_t _zrtp_get_timeout(uint32_t curr_timeout, zrtp_msg_type_t msg);
  231. /*!
  232. * \brief Terminates retransmission task
  233. * This function is a wrapper around zrtp_cancele_send_packet_later() which
  234. * unsets the zrtp_retry_task_t#_is_enabled flag to prevent the scheduler from
  235. * re-adding tasks after their termination.
  236. */
  237. void _zrtp_cancel_send_packet_later( zrtp_stream_t* stream,
  238. zrtp_msg_type_t type);
  239. /*!
  240. * \brief state switcher
  241. * This function changes stream state to \c state, makes a backup of the previous
  242. * state at zrtp_stream_t#_prev_state and prints debug information.
  243. * \warning Don't change the stream state directly. Use this function.
  244. * \param stream - ZRTP stream to be changed;
  245. * \param state - new state.
  246. */
  247. void _zrtp_change_state( zrtp_stream_t* stream, zrtp_state_t state);
  248. /*===========================================================================*/
  249. /* Shared STATE-MACHINE Routine */
  250. /*===========================================================================*/
  251. // TODO: clean this up
  252. zrtp_status_t _zrtp_machine_enter_pendingsecure(zrtp_stream_t* stream, zrtp_rtp_info_t* commit);
  253. zrtp_status_t _zrtp_machine_enter_initiatingsecure(zrtp_stream_t* stream);
  254. zrtp_status_t _zrtp_machine_enter_secure(zrtp_stream_t* stream);
  255. zrtp_status_t _zrtp_machine_enter_pendingclear(zrtp_stream_t* stream);
  256. zrtp_status_t _zrtp_machine_enter_initiatingerror( zrtp_stream_t *stream,
  257. zrtp_protocol_error_t code,
  258. uint8_t notif);
  259. zrtp_status_t _zrtp_machine_create_confirm(zrtp_stream_t *stream, zrtp_packet_Confirm_t* confirm);
  260. zrtp_status_t _zrtp_machine_process_confirm(zrtp_stream_t *stream, zrtp_packet_Confirm_t *confirm);
  261. zrtp_status_t _zrtp_machine_process_goclear(zrtp_stream_t* stream, zrtp_rtp_info_t* packet);
  262. zrtp_status_t _zrtp_machine_start_initiating_secure(zrtp_stream_t *stream);
  263. zrtp_statemachine_type_t _zrtp_machine_preparse_commit(zrtp_stream_t *stream, zrtp_rtp_info_t* packet);
  264. /*===========================================================================*/
  265. /* PARSERS */
  266. /*===========================================================================*/
  267. /*!
  268. * \brief Prepare RTP/ZRTP media packet for the further processing.
  269. * This function defines the packet type, parses SSRC and makes the sequence
  270. * number implicit. If it is a ZRTP message, packet length correctness and CRC
  271. * are checked as well.
  272. * \param stream - ZRTP stream associated with this packet;
  273. * \param packet - packet for preparing;
  274. * \param length - packet length;
  275. * \param info - resulting packet structure;
  276. * \param is_input - 1 - assumes incoming and 0 - outgoing packet direction.
  277. */
  278. zrtp_status_t _zrtp_packet_preparse( zrtp_stream_t* stream,
  279. char* packet,
  280. uint32_t *length,
  281. zrtp_rtp_info_t* info,
  282. uint8_t is_input);
  283. /*!
  284. * \brief Fills ZRTP message header and computes messages HMAC
  285. * _zrtp_packet_fill_msg_hdr() prepares a ZRTP message header for sending. It calculates
  286. * the total message length in 4-byte words and fills the message type block.
  287. * \param stream - stream within in the operation will be performed
  288. * \param type - ZRTP message type;
  289. * \param body_length - message body length (without header);
  290. * \param hdr - message ZRTP header
  291. * \return
  292. * - zrtp_status_ok - if success;
  293. * - zrtp_status_bad_param - if message \c type is unknown.
  294. */
  295. zrtp_status_t _zrtp_packet_fill_msg_hdr( zrtp_stream_t *stream,
  296. zrtp_msg_type_t type,
  297. uint16_t body_length,
  298. zrtp_msg_hdr_t *hdr);
  299. /**
  300. * @brief Sends ZRTP message onto the network
  301. * _zrtp_packet_send_message constructs a ZRTP header and prepares packet for sending,
  302. * computes CRC and injects the packet into the network using the interface
  303. * function zrtp_send_rtp().
  304. * @param ctx - ZRTP stream context;
  305. * @param type - packet type to construct primitive ZRTP messages;
  306. * @param message - ZRTP message for sending.
  307. * @return
  308. * - 0 - if sent successfully;
  309. * - -1 - if error.
  310. */
  311. int _zrtp_packet_send_message( zrtp_stream_t *stream,
  312. zrtp_msg_type_t type,
  313. const void *message);
  314. /** @brief Returns ZRTP message type by symbolic name in header. */
  315. zrtp_msg_type_t _zrtp_packet_get_type(ZRTP_UNALIGNED(zrtp_rtp_hdr_t)*hdr, uint32_t length);
  316. /**
  317. * @brief Insert CRC32 to ZRTP packets
  318. * This function computes the 32 bit ZRTP packet checksum according to RFC 3309.
  319. * As specified at ZRTP RFC, CRC32 is appended to the end of the extension for every ZRTP packet.
  320. * @param packet - zrtp packet wrapper structure.
  321. */
  322. void _zrtp_packet_insert_crc(char* packet, uint32_t length);
  323. /**
  324. * @brief Validate ZRTP packet CRC
  325. * @return
  326. * - 0 if correct CRC;
  327. * - -1 if CRC validation failed.
  328. */
  329. int8_t _zrtp_packet_validate_crc(const char* packet, uint32_t length);
  330. /* \} */
  331. #if defined(__cplusplus)
  332. }
  333. #endif
  334. #endif /* __ZRTP_ENGINE_H__ */