/libs/libzrtp/src/zrtp_srtp_dm.c

https://github.com/curriegrad2004/FreeSWITCH · C · 232 lines · 170 code · 36 blank · 26 comment · 28 complexity · 00c3672514594dc9168cf845bddc18f9 MD5 · raw file

  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. #if (defined(ZRTP_USE_EXTERN_SRTP) && (ZRTP_USE_EXTERN_SRTP == 1))
  10. /* exactly in this order (for winsock) */
  11. #include <srtp.h>
  12. #include "zrtp.h"
  13. struct zrtp_srtp_ctx
  14. {
  15. srtp_t outgoing_srtp;
  16. srtp_t incoming_srtp;
  17. };
  18. /*---------------------------------------------------------------------------*/
  19. void init_policy(crypto_policy_t *sp, zrtp_srtp_policy_t *zp)
  20. {
  21. //TODO: make incoming policy crypto algorithm check for David A. McGrew's implementation support
  22. /* there are no another appropriate ciphers in the David A. McGrew's implementation yet */
  23. sp->cipher_type = AES_128_ICM;
  24. sp->cipher_key_len = zp->cipher_key_len;
  25. sp->auth_type = HMAC_SHA1;
  26. sp->auth_key_len = zp->auth_key_len;
  27. sp->auth_tag_len = zp->auth_tag_len->tag_length ? zp->auth_tag_len->tag_length : 10;
  28. sp->sec_serv = sec_serv_conf_and_auth;
  29. }
  30. /*---------------------------------------------------------------------------*/
  31. zrtp_status_t create_srtp_stream( srtp_t *srtp_stream,
  32. zrtp_srtp_profile_t *profile,
  33. ssrc_type_t ssrc_type )
  34. {
  35. srtp_policy_t policy;
  36. uint8_t *tmp_key;
  37. init_policy(&policy.rtp, &profile->rtp_policy);
  38. init_policy(&policy.rtcp, &profile->rtcp_policy);
  39. policy.ssrc.type = ssrc_type;
  40. policy.ssrc.value = 0;
  41. /* David A. McGrew's implementation uses key and salt as whole buffer, so let's make it */
  42. tmp_key = (uint8_t*)zrtp_sys_alloc(profile->key.length + profile->salt.length);
  43. if(NULL == tmp_key){
  44. return zrtp_status_fail;
  45. }
  46. zrtp_memcpy(tmp_key, profile->key.buffer, profile->key.length);
  47. zrtp_memcpy(tmp_key+profile->key.length, profile->salt.buffer, profile->salt.length);
  48. policy.key = tmp_key;
  49. policy.next = NULL;
  50. /* add salt length to the key length of each policy */
  51. policy.rtp.cipher_key_len += 14;
  52. policy.rtcp.cipher_key_len += 14;
  53. if(err_status_ok != srtp_create(srtp_stream, &policy)){
  54. zrtp_sys_free(tmp_key);
  55. return zrtp_status_fail;
  56. }
  57. zrtp_sys_free(tmp_key);
  58. return zrtp_status_ok;
  59. }
  60. /*===========================================================================*/
  61. /* Public interface */
  62. /*===========================================================================*/
  63. /*---------------------------------------------------------------------------*/
  64. zrtp_status_t zrtp_srtp_init(zrtp_global_ctx_t *zrtp_global)
  65. {
  66. err_status_t s = srtp_init();
  67. return (err_status_ok == s) ? zrtp_status_ok : s;
  68. }
  69. zrtp_status_t zrtp_srtp_down( zrtp_global_ctx_t *zrtp_global )
  70. {
  71. return zrtp_status_ok;
  72. }
  73. /*---------------------------------------------------------------------------*/
  74. zrtp_srtp_ctx_t * zrtp_srtp_create( zrtp_srtp_global_t *srtp_global,
  75. zrtp_srtp_profile_t *inc_profile,
  76. zrtp_srtp_profile_t *out_profile)
  77. {
  78. zrtp_status_t res = zrtp_status_ok;
  79. zrtp_srtp_ctx_t *srtp_ctx = NULL;
  80. if(NULL == inc_profile || NULL == out_profile){
  81. return NULL;
  82. }
  83. do{
  84. srtp_policy_t *policy_head, *policy_next;
  85. srtp_ctx = zrtp_sys_alloc(sizeof(zrtp_srtp_ctx_t));
  86. if(NULL == srtp_ctx){
  87. break;
  88. }
  89. res = create_srtp_stream(&srtp_ctx->incoming_srtp, inc_profile, ssrc_any_inbound);
  90. if(zrtp_status_ok != res){
  91. zrtp_sys_free(srtp_ctx);
  92. srtp_ctx = NULL;
  93. break;
  94. }
  95. res = create_srtp_stream(&srtp_ctx->outgoing_srtp, out_profile, ssrc_any_outbound);
  96. if(zrtp_status_ok != res){
  97. srtp_dealloc(srtp_ctx->incoming_srtp);
  98. zrtp_sys_free(srtp_ctx);
  99. srtp_ctx = NULL;
  100. break;
  101. }
  102. }while(0);
  103. return srtp_ctx;
  104. }
  105. /*---------------------------------------------------------------------------*/
  106. zrtp_status_t zrtp_srtp_destroy( zrtp_srtp_global_t *zrtp_srtp_global,
  107. zrtp_srtp_ctx_t *srtp_ctx )
  108. {
  109. srtp_dealloc(srtp_ctx->incoming_srtp);
  110. srtp_dealloc(srtp_ctx->outgoing_srtp);
  111. zrtp_sys_free(srtp_ctx);
  112. return zrtp_status_ok;
  113. }
  114. /*---------------------------------------------------------------------------*/
  115. zrtp_status_t zrtp_srtp_protect( zrtp_srtp_global_t *srtp_global,
  116. zrtp_srtp_ctx_t *srtp_ctx,
  117. zrtp_rtp_info_t *packet)
  118. {
  119. err_status_t res;
  120. res = srtp_protect(srtp_ctx->outgoing_srtp, packet->packet, packet->length);
  121. if(err_status_ok != res){
  122. return zrtp_status_fail;
  123. }else{
  124. return zrtp_status_ok;
  125. }
  126. }
  127. /*---------------------------------------------------------------------------*/
  128. zrtp_status_t zrtp_srtp_unprotect( zrtp_srtp_global_t *srtp_global,
  129. zrtp_srtp_ctx_t *srtp_ctx,
  130. zrtp_rtp_info_t *packet)
  131. {
  132. err_status_t res;
  133. res = srtp_unprotect(srtp_ctx->incoming_srtp, packet->packet, packet->length);
  134. if(err_status_ok != res){
  135. return zrtp_status_fail;
  136. }else{
  137. return zrtp_status_ok;
  138. }
  139. }
  140. /*---------------------------------------------------------------------------*/
  141. zrtp_status_t zrtp_srtp_protect_rtcp( zrtp_srtp_global_t *srtp_global,
  142. zrtp_srtp_ctx_t *srtp_ctx,
  143. zrtp_rtp_info_t *packet)
  144. {
  145. err_status_t res;
  146. res = srtp_protect_rtcp(srtp_ctx->outgoing_srtp, packet->packet, packet->length);
  147. if(err_status_ok != res){
  148. return zrtp_status_fail;
  149. }else{
  150. return zrtp_status_ok;
  151. }
  152. }
  153. /*---------------------------------------------------------------------------*/
  154. zrtp_status_t zrtp_srtp_unprotect_rtcp( zrtp_srtp_global_t *srtp_global,
  155. zrtp_srtp_ctx_t *srtp_ctx,
  156. zrtp_rtp_info_t *packet)
  157. {
  158. err_status_t res;
  159. res = srtp_unprotect_rtcp(srtp_ctx->incoming_srtp, packet->packet, packet->length);
  160. if(err_status_ok != res){
  161. return zrtp_status_fail;
  162. }else{
  163. return zrtp_status_ok;
  164. }
  165. }
  166. /*----------------------------------------------------------------------------*/
  167. uint64_t make64(uint32_t high, uint32_t low)
  168. {
  169. uint64_t_ res;
  170. uint32_t *p = (uint32_t*)&res;
  171. #if ZRTP_BYTE_ORDER == ZBO_LITTLE_ENDIAN
  172. *p++ = low;
  173. *p = high;
  174. #else
  175. *p++ = high;
  176. *p = low;
  177. #endif
  178. return res;
  179. }
  180. uint32_t high32(uint64_t x)
  181. {
  182. uint32_t *p = &x;
  183. #if ZRTP_BYTE_ORDER == ZBO_LITTLE_ENDIAN
  184. p++;
  185. #endif
  186. return *p;
  187. }
  188. uint32_t low32(uint64_t x)
  189. {
  190. uint32_t *p = &x;
  191. #if ZRTP_BYTE_ORDER == ZBO_BIG_ENDIAN
  192. p++;
  193. #endif
  194. return *p;
  195. }
  196. #endif /*ZRTP_USE_EXTERN_SRTP*/