zrtp_sas_proxy.lua 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. -- zrtp_sas_proxy.lua
  2. --
  3. -- Copyright (c) 2011-2013 Travis Cross
  4. --
  5. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  6. -- of this software and associated documentation files (the "Software"), to deal
  7. -- in the Software without restriction, including without limitation the rights
  8. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. -- copies of the Software, and to permit persons to whom the Software is
  10. -- furnished to do so, subject to the following conditions:
  11. --
  12. -- The above copyright notice and this permission notice shall be included in
  13. -- all copies or substantial portions of the Software.
  14. --
  15. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. -- THE SOFTWARE.
  22. --
  23. --
  24. -- When we're acting as a ZRTP man-in-the-middle, proxy the SAS (Short
  25. -- Authentication String) from one leg of the call to the other.
  26. --
  27. -- This script should be called asynchonously with luarun. e.g.:
  28. --
  29. -- <action application="export" data="nolocal:api_on_answer=luarun zrtp_sas_proxy.lua ${uuid}"/>
  30. --
  31. aleg=argv[1]
  32. api=freeswitch.API()
  33. function log(level,msg) return freeswitch.consoleLog(level,"zrtp_sas: "..msg.."\n") end
  34. function sleep(sec) return freeswitch.msleep(sec*1000) end
  35. function ready() return api:execute("uuid_exists",aleg)=="true" end
  36. function getvar(uuid,var)
  37. local x=api:execute("uuid_getvar",uuid.." "..var)
  38. if x=="_undef_" then return nil end
  39. return x
  40. end
  41. function getvarp(uuid,var) return getvar(uuid,var)=="true" end
  42. function display(uuid,msg)
  43. local cidn=getvar(uuid,"caller_id_name")
  44. return api:execute("uuid_display",uuid.." "..msg.." "..cidn)
  45. end
  46. function mk_sas(sas1,sas2)
  47. if sas1 and sas2 then return sas1.." "..sas2
  48. else return sas1 or sas2 or "" end
  49. end
  50. function get_sas(uuid)
  51. return mk_sas(getvar(uuid,"zrtp_sas1_string_audio"),
  52. getvar(uuid,"zrtp_sas2_string"))
  53. end
  54. function log_sas(leg,uuid)
  55. return log("notice",leg..": "..uuid.." sas: "..get_sas(uuid))
  56. end
  57. function display_sas(to,from)
  58. return display(to," ("..get_sas(from)..")")
  59. end
  60. function get_bleg(aleg)
  61. local retries=15 bleg=nil
  62. while ready() do
  63. if retries<1 then return nil end
  64. local bleg=getvar(aleg,"signal_bond")
  65. if bleg then return bleg end
  66. log("debug","waiting for bleg uuid...")
  67. sleep(1)
  68. retries=retries-1
  69. end
  70. end
  71. function handle_sas(aleg,bleg)
  72. local retries=45 af=false bf=false
  73. while ready() do
  74. if retries<1 then return nil end
  75. if not af and getvarp(aleg,"zrtp_secure_media_confirmed_audio") then
  76. af=true
  77. log_sas("aleg",aleg)
  78. display_sas(bleg,aleg)
  79. end
  80. if not bf and getvarp(bleg,"zrtp_secure_media_confirmed_audio") then
  81. bf=true
  82. log_sas("bleg",bleg)
  83. display_sas(aleg,bleg)
  84. end
  85. if (af and bf) then break
  86. elseif af then log("debug","waiting on bleg zrtp...")
  87. elseif bf then log("debug","waiting on aleg zrtp...")
  88. else log("debug","waiting for zrtp...") end
  89. sleep(1)
  90. retries=retries-1
  91. end
  92. end
  93. if not (getvarp(aleg,"zrtp_passthru") or getvarp(aleg,"proxy_media")) then
  94. handle_sas(aleg,get_bleg(aleg))
  95. end