transport_srtp.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #ifndef __PJMEDIA_TRANSPORT_SRTP_H__
  20. #define __PJMEDIA_TRANSPORT_SRTP_H__
  21. /**
  22. * @file transport_srtp.h
  23. * @brief Secure RTP (SRTP) transport.
  24. */
  25. #include <pjmedia/transport.h>
  26. /**
  27. * @defgroup PJMEDIA_TRANSPORT_SRTP Secure RTP (SRTP) Media Transport
  28. * @ingroup PJMEDIA_TRANSPORT
  29. * @brief Media transport adapter to add SRTP feature to existing transports
  30. * @{
  31. *
  32. * This module implements SRTP as described by RFC 3711, using RFC 4568 as
  33. * key exchange method. It implements \ref PJMEDIA_TRANSPORT to integrate
  34. * with the rest of PJMEDIA framework.
  35. *
  36. * As we know, media transport is separated from the stream object (which
  37. * does the encoding/decoding of PCM frames, (de)packetization of RTP/RTCP
  38. * packets, and de-jitter buffering). The connection between stream and media
  39. * transport is established when the stream is created (we need to specify
  40. * media transport during stream creation), and the interconnection can be
  41. * depicted from the diagram below:
  42. *
  43. \img{pjmedia/docs/media-transport.PNG}
  44. * I think the diagram above is self-explanatory.
  45. *
  46. * SRTP functionality is implemented as some kind of "adapter", which is
  47. * plugged between the stream and the actual media transport that does
  48. * sending/receiving RTP/RTCP packets. When SRTP is used, the interconnection
  49. * between stream and transport is like the diagram below:
  50. *
  51. \img{pjmedia/docs/media-srtp-transport.PNG}
  52. * So to stream, the SRTP transport behaves as if it is a media transport
  53. * (because it is a media transport), and to the media transport it behaves
  54. * as if it is a stream. The SRTP object then forwards RTP packets back and
  55. * forth between stream and the actual transport, encrypting/decrypting
  56. * the RTP/RTCP packets as necessary.
  57. *
  58. * The neat thing about this design is the SRTP "adapter" then can be used
  59. * to encrypt any kind of media transports. We currently have UDP and ICE
  60. * media transports that can benefit SRTP, and we could add SRTP to any
  61. * media transports that will be added in the future.
  62. */
  63. PJ_BEGIN_DECL
  64. /**
  65. * Crypto option.
  66. */
  67. typedef enum pjmedia_srtp_crypto_option
  68. {
  69. /** When this flag is specified, encryption will be disabled. */
  70. PJMEDIA_SRTP_NO_ENCRYPTION = 1,
  71. /** When this flag is specified, authentication will be disabled. */
  72. PJMEDIA_SRTP_NO_AUTHENTICATION = 2
  73. } pjmedia_srtp_crypto_option;
  74. /**
  75. * This structure describes an individual crypto setting.
  76. */
  77. typedef struct pjmedia_srtp_crypto
  78. {
  79. /** Optional key. If empty, a random key will be autogenerated. */
  80. pj_str_t key;
  81. /** Crypto name. */
  82. pj_str_t name;
  83. /** Flags, bitmask from #pjmedia_srtp_crypto_option */
  84. unsigned flags;
  85. } pjmedia_srtp_crypto;
  86. /**
  87. * This enumeration specifies the behavior of the SRTP transport regarding
  88. * media security offer and answer.
  89. */
  90. typedef enum pjmedia_srtp_use
  91. {
  92. /**
  93. * When this flag is specified, SRTP will be disabled, and the transport
  94. * will reject RTP/SAVP offer.
  95. */
  96. PJMEDIA_SRTP_DISABLED,
  97. /**
  98. * When this flag is specified, SRTP setting is unknown. This is to set
  99. * the initial remote's SRTP usage. It will be set later after remote's
  100. * policy in the SDP is received.
  101. */
  102. PJMEDIA_SRTP_UNKNOWN = PJMEDIA_SRTP_DISABLED,
  103. /**
  104. * When this flag is specified, SRTP will be advertised as optional and
  105. * incoming SRTP offer will be accepted.
  106. */
  107. PJMEDIA_SRTP_OPTIONAL,
  108. /**
  109. * When this flag is specified, the transport will require that RTP/SAVP
  110. * media shall be used.
  111. */
  112. PJMEDIA_SRTP_MANDATORY
  113. } pjmedia_srtp_use;
  114. /**
  115. * This enumeration specifies SRTP keying methods.
  116. */
  117. typedef enum pjmedia_srtp_keying_method
  118. {
  119. /**
  120. * Session Description (SDES).
  121. */
  122. PJMEDIA_SRTP_KEYING_SDES,
  123. /**
  124. * DTLS-SRTP.
  125. */
  126. PJMEDIA_SRTP_KEYING_DTLS_SRTP,
  127. /**
  128. * Number of keying method.
  129. */
  130. PJMEDIA_SRTP_KEYINGS_COUNT
  131. } pjmedia_srtp_keying_method;
  132. /**
  133. * Structure containing callbacks to receive SRTP notifications.
  134. */
  135. typedef struct pjmedia_srtp_cb
  136. {
  137. /**
  138. * This callback will be called when SRTP negotiation completes. This
  139. * callback will be invoked when the negotiation is done outside of
  140. * the SDP signalling, such as in DTLS-SRTP.
  141. *
  142. * @param tp PJMEDIA SRTP transport.
  143. * @param status Operation status.
  144. */
  145. void (*on_srtp_nego_complete)(pjmedia_transport *tp,
  146. pj_status_t status);
  147. } pjmedia_srtp_cb;
  148. /**
  149. * RTP sequence rollover counter settings.
  150. */
  151. typedef struct pjmedia_srtp_roc
  152. {
  153. /**
  154. * The synchronization source.
  155. */
  156. pj_uint32_t ssrc;
  157. /**
  158. * The rollover counter.
  159. */
  160. pj_uint32_t roc;
  161. } pjmedia_srtp_roc;
  162. /**
  163. * Settings to be given when creating SRTP transport. Application should call
  164. * #pjmedia_srtp_setting_default() to initialize this structure with its
  165. * default values.
  166. */
  167. typedef struct pjmedia_srtp_setting
  168. {
  169. /**
  170. * Specify the usage policy. Default is PJMEDIA_SRTP_OPTIONAL.
  171. */
  172. pjmedia_srtp_use use;
  173. /**
  174. * Specify whether the SRTP transport should close the member transport
  175. * when it is destroyed. Default: PJ_TRUE.
  176. */
  177. pj_bool_t close_member_tp;
  178. /**
  179. * Specify the number of crypto suite settings. If set to zero, all
  180. * available cryptos will be enabled. Default: zero.
  181. */
  182. unsigned crypto_count;
  183. /**
  184. * Specify individual crypto suite setting and its priority order.
  185. *
  186. * Notes for DTLS-SRTP keying:
  187. * - Currently only supports these cryptos: AES_CM_128_HMAC_SHA1_80,
  188. * AES_CM_128_HMAC_SHA1_32, AEAD_AES_256_GCM, and AEAD_AES_128_GCM.
  189. * - SRTP key is not configurable.
  190. */
  191. pjmedia_srtp_crypto crypto[PJMEDIA_SRTP_MAX_CRYPTOS];
  192. /**
  193. * Specify the number of enabled keying methods. If set to zero, all
  194. * keyings will be enabled. Maximum value is PJMEDIA_SRTP_MAX_KEYINGS.
  195. *
  196. * Default is zero (all keyings are enabled with priority order:
  197. * SDES, DTLS-SRTP).
  198. */
  199. unsigned keying_count;
  200. /**
  201. * Specify enabled keying methods and its priority order. Keying method
  202. * with higher priority will be given earlier chance to process the SDP,
  203. * for example as currently only one keying is supported in the SDP offer,
  204. * keying with first priority will be likely used in the SDP offer.
  205. */
  206. pjmedia_srtp_keying_method keying[PJMEDIA_SRTP_KEYINGS_COUNT];
  207. /**
  208. * RTP sequence rollover counter initialization value for incoming
  209. * direction. This is useful to maintain ROC after media transport
  210. * recreation such as in IP change scenario.
  211. */
  212. pjmedia_srtp_roc rx_roc;
  213. /**
  214. * The previous value of RTP sequence rollover counter. This is
  215. * useful in situations when we expect the remote to reset/maintain
  216. * ROC but for some reason, they don't. Thus, when we encounter
  217. * SRTP packet unprotect failure during probation, we will retry to
  218. * unprotect with this ROC value as well.
  219. * Set prev_rx_roc.ssrc to 0 to disable this feature.
  220. */
  221. pjmedia_srtp_roc prev_rx_roc;
  222. /**
  223. * RTP sequence rollover counter initialization value for outgoing
  224. * direction. This is useful to maintain ROC after media transport
  225. * recreation such as in IP change scenario.
  226. */
  227. pjmedia_srtp_roc tx_roc;
  228. /**
  229. * Specify SRTP callback.
  230. */
  231. pjmedia_srtp_cb cb;
  232. /**
  233. * Specify SRTP transport user data.
  234. */
  235. void *user_data;
  236. } pjmedia_srtp_setting;
  237. /**
  238. * This structure specifies SRTP transport specific info. This will fit
  239. * into \a buffer field of pjmedia_transport_specific_info.
  240. */
  241. typedef struct pjmedia_srtp_info
  242. {
  243. /**
  244. * Specify whether the SRTP transport is active for SRTP session.
  245. */
  246. pj_bool_t active;
  247. /**
  248. * Specify the policy used by the SRTP session for receive direction.
  249. */
  250. pjmedia_srtp_crypto rx_policy;
  251. /**
  252. * Specify the policy used by the SRTP session for transmit direction.
  253. */
  254. pjmedia_srtp_crypto tx_policy;
  255. /**
  256. * Specify the usage policy.
  257. */
  258. pjmedia_srtp_use use;
  259. /**
  260. * Specify the peer's usage policy.
  261. */
  262. pjmedia_srtp_use peer_use;
  263. /**
  264. * RTP sequence rollover counter info for incoming direction.
  265. */
  266. pjmedia_srtp_roc rx_roc;
  267. /**
  268. * RTP sequence rollover counter info for outgoing direction.
  269. */
  270. pjmedia_srtp_roc tx_roc;
  271. } pjmedia_srtp_info;
  272. /**
  273. * This structure specifies DTLS-SRTP negotiation parameters.
  274. */
  275. typedef struct pjmedia_srtp_dtls_nego_param
  276. {
  277. /**
  278. * Fingerprint of remote certificate, should be formatted as
  279. * "SHA-256/1 XX:XX:XX...". If this is not set, fingerprint verification
  280. * will not be performed.
  281. */
  282. pj_str_t rem_fingerprint;
  283. /**
  284. * Remote address and port.
  285. */
  286. pj_sockaddr rem_addr;
  287. /**
  288. * Remote RTCP address and port.
  289. */
  290. pj_sockaddr rem_rtcp;
  291. /**
  292. * Set to PJ_TRUE if our role is active. Active role will initiates
  293. * the DTLS negotiation. Passive role will wait for incoming DTLS
  294. * negotiation packet.
  295. */
  296. pj_bool_t is_role_active;
  297. } pjmedia_srtp_dtls_nego_param;
  298. /**
  299. * Initialize SRTP library. This function should be called before
  300. * any SRTP functions, however calling #pjmedia_transport_srtp_create()
  301. * will also invoke this function. This function will also register SRTP
  302. * library deinitialization to #pj_atexit(), so the deinitialization
  303. * of SRTP library will be performed automatically by PJLIB destructor.
  304. *
  305. * @param endpt The media endpoint instance.
  306. *
  307. * @return PJ_SUCCESS on success.
  308. */
  309. PJ_DECL(pj_status_t) pjmedia_srtp_init_lib(pjmedia_endpt *endpt);
  310. /**
  311. * Initialize SRTP setting with its default values.
  312. *
  313. * @param opt SRTP setting to be initialized.
  314. */
  315. PJ_DECL(void) pjmedia_srtp_setting_default(pjmedia_srtp_setting *opt);
  316. /**
  317. * Enumerate available SRTP crypto name.
  318. *
  319. * @param count On input, specifies the maximum length of crypto
  320. * array. On output, the number of available crypto
  321. * initialized by this function.
  322. * @param crypto The SRTP crypto array output.
  323. *
  324. * @return PJ_SUCCESS on success.
  325. */
  326. PJ_DECL(pj_status_t) pjmedia_srtp_enum_crypto(unsigned *count,
  327. pjmedia_srtp_crypto crypto[]);
  328. /**
  329. * Enumerate available SRTP keying methods.
  330. *
  331. * @param count On input, specifies the maximum length of keying method
  332. * array. On output, the number of available keying method
  333. * initialized by this function.
  334. * @param keying The SRTP keying method array output.
  335. *
  336. * @return PJ_SUCCESS on success.
  337. */
  338. PJ_DECL(pj_status_t) pjmedia_srtp_enum_keying(unsigned *count,
  339. pjmedia_srtp_keying_method keying[]);
  340. /**
  341. * Create an SRTP media transport.
  342. *
  343. * @param endpt The media endpoint instance.
  344. * @param tp The actual media transport to send and receive
  345. * RTP/RTCP packets. This media transport will be
  346. * kept as member transport of this SRTP instance.
  347. * @param opt Optional settings. If NULL is given, default
  348. * settings will be used.
  349. * @param p_tp Pointer to receive the transport SRTP instance.
  350. *
  351. * @return PJ_SUCCESS on success.
  352. */
  353. PJ_DECL(pj_status_t) pjmedia_transport_srtp_create(
  354. pjmedia_endpt *endpt,
  355. pjmedia_transport *tp,
  356. const pjmedia_srtp_setting *opt,
  357. pjmedia_transport **p_tp);
  358. /**
  359. * Get current SRTP media transport setting.
  360. *
  361. * @param srtp The SRTP transport.
  362. * @param opt Structure to receive the SRTP setting
  363. *
  364. * @return PJ_SUCCESS on success.
  365. */
  366. PJ_DECL(pj_status_t) pjmedia_transport_srtp_get_setting(
  367. pjmedia_transport *srtp,
  368. pjmedia_srtp_setting *opt);
  369. /**
  370. * Modify SRTP media transport setting.
  371. *
  372. * @param srtp The SRTP transport.
  373. * @param opt New setting
  374. *
  375. * @return PJ_SUCCESS on success.
  376. */
  377. PJ_DECL(pj_status_t) pjmedia_transport_srtp_modify_setting(
  378. pjmedia_transport *srtp,
  379. const pjmedia_srtp_setting *opt);
  380. /**
  381. * Get fingerprint of local DTLS-SRTP certificate.
  382. *
  383. * @param srtp The SRTP transport.
  384. * @param hash Fingerprint hash algorithm, currently valid values are
  385. * "SHA-256" and "SHA-1".
  386. * @param buf Buffer for fingerprint output. The output will be
  387. * formatted as "SHA-256/1 XX:XX:XX..." and null terminated.
  388. * @param len On input, the size of the buffer.
  389. * On output, the length of the fingerprint.
  390. *
  391. * @return PJ_SUCCESS on success.
  392. */
  393. PJ_DECL(pj_status_t) pjmedia_transport_srtp_dtls_get_fingerprint(
  394. pjmedia_transport *srtp,
  395. const char *hash,
  396. char *buf, pj_size_t *len);
  397. /**
  398. * Manually start DTLS-SRTP negotiation with the given parameters. Application
  399. * only needs to call this function when the SRTP transport is used without
  400. * SDP offer/answer. When SDP offer/answer framework is used, the DTLS-SRTP
  401. * negotiation will be handled by pjmedia_transport_media_create(),
  402. * pjmedia_transport_media_start(), pjmedia_transport_media_encode_sdp(), and
  403. * pjmedia_transport_media_stop().
  404. *
  405. * When the negotiation completes, application will be notified via SRTP
  406. * callback on_srtp_nego_complete(), if set. If the negotiation is successful,
  407. * SRTP will be automatically started.
  408. *
  409. * Note that if the SRTP member transport is an ICE transport, application
  410. * should only call this function after ICE negotiation is completed
  411. * successfully.
  412. *
  413. * @param srtp The SRTP transport.
  414. * @param param DTLS-SRTP nego parameter.
  415. *
  416. * @return PJ_SUCCESS on success.
  417. */
  418. PJ_DECL(pj_status_t) pjmedia_transport_srtp_dtls_start_nego(
  419. pjmedia_transport *srtp,
  420. const pjmedia_srtp_dtls_nego_param *param);
  421. /**
  422. * Manually start SRTP session with the given parameters. Application only
  423. * needs to call this function when the SRTP transport is used without SDP
  424. * offer/answer. When SDP offer/answer framework is used, the SRTP transport
  425. * will be started/stopped by #pjmedia_transport_media_start() and
  426. * #pjmedia_transport_media_stop() respectively.
  427. *
  428. * Please note that even if an RTP stream is only one direction, application
  429. * will still need to provide both crypto suites, because it is needed by
  430. * RTCP.
  431. * If application specifies the crypto keys, the keys for transmit and receive
  432. * direction MUST be different.
  433. *
  434. * @param srtp The SRTP transport.
  435. * @param tx Crypto suite setting for transmit direction.
  436. * @param rx Crypto suite setting for receive direction.
  437. *
  438. * @return PJ_SUCCESS on success.
  439. */
  440. PJ_DECL(pj_status_t) pjmedia_transport_srtp_start(
  441. pjmedia_transport *srtp,
  442. const pjmedia_srtp_crypto *tx,
  443. const pjmedia_srtp_crypto *rx);
  444. /**
  445. * Stop SRTP session.
  446. *
  447. * @param srtp The SRTP media transport.
  448. *
  449. * @return PJ_SUCCESS on success.
  450. *
  451. * @see #pjmedia_transport_srtp_start()
  452. */
  453. PJ_DECL(pj_status_t) pjmedia_transport_srtp_stop(pjmedia_transport *srtp);
  454. /**
  455. * This is a utility function to decrypt SRTP packet using SRTP transport.
  456. * This function is not part of SRTP transport's API, but it can be used
  457. * to decrypt SRTP packets from non-network (for example, from a saved file)
  458. * without having to use the transport framework. See pcaputil.c in the
  459. * samples collection on how to use this function.
  460. *
  461. * @param tp The SRTP transport.
  462. * @param is_rtp Set to non-zero if the packet is SRTP, otherwise set
  463. * to zero if the packet is SRTCP.
  464. * @param pkt On input, it contains SRTP or SRTCP packet. On
  465. * output, it contains the decrypted RTP/RTCP packet.
  466. * @param pkt_len On input, specify the length of the buffer. On
  467. * output, it will be filled with the actual length
  468. * of decrypted packet.
  469. *
  470. * @return PJ_SUCCESS on success.
  471. */
  472. PJ_DECL(pj_status_t) pjmedia_transport_srtp_decrypt_pkt(pjmedia_transport *tp,
  473. pj_bool_t is_rtp,
  474. void *pkt,
  475. int *pkt_len);
  476. /**
  477. * Query member transport of SRTP.
  478. *
  479. * @param srtp The SRTP media transport.
  480. *
  481. * @return member media transport.
  482. */
  483. PJ_DECL(pjmedia_transport*) pjmedia_transport_srtp_get_member(
  484. pjmedia_transport *srtp);
  485. PJ_END_DECL
  486. /**
  487. * @}
  488. */
  489. #endif /* __PJMEDIA_TRANSPORT_SRTP_H__ */