openssl_srtp.test 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #!/bin/sh
  2. # Test WolfSSL/OpenSSL srtp interoperability
  3. #
  4. # TODO: add OpenSSL client with WolfSSL server
  5. set -e
  6. if ! test -n "$WOLFSSL_OPENSSL_TEST"; then
  7. echo "WOLFSSL_OPENSSL_TEST NOT set, won't run"
  8. exit 0
  9. fi
  10. OPENSSL=${OPENSSL:="openssl"}
  11. WOLFSSL_CLIENT=${WOLFSSL_CLIENT:="./examples/client/client"}
  12. # need a unique port since may run the same time as testsuite
  13. generate_port() {
  14. port=$(($(od -An -N2 /dev/random) % (65535-49512) + 49512))
  15. }
  16. # get size of key material based on the profile
  17. # $1 srtp profile
  18. get_key_material_size() {
  19. case "$1" in
  20. "SRTP_AES128_CM_SHA1_80")
  21. ekm_size=60 ;;
  22. "SRTP_AES128_CM_SHA1_32")
  23. ekm_size=60 ;;
  24. "SRTP_NULL_SHA1_80")
  25. ekm_size=28 ;;
  26. "SRTP_NULL_SHA1_32")
  27. ekm_size=27 ;;
  28. "SRTP_AEAD_AES_128_GCM")
  29. ekm_size=56;;
  30. "SRTP_AEAD_AES_256_GCM")
  31. ekm_size=88;;
  32. *)
  33. echo "SRTP profile $1 unsupported"
  34. exit 1
  35. esac
  36. }
  37. # Start an OpenSSL server dtls with srtp
  38. # $1: dtsl version [1.0, 1.2]
  39. # $2: srtp profile string
  40. start_openssl_server() {
  41. generate_port
  42. server_port=$port
  43. srtp_profile=$2
  44. if [ "$1" = "1.0" ]; then
  45. dtls_version=dtls1
  46. elif [ "$1" = "1.2" ]; then
  47. dtls_version=dtls1_2
  48. fi
  49. get_key_material_size "$srtp_profile"
  50. server_output_file=/tmp/openssl_srtp_out
  51. # hackish but OpenSSL doesn't work if input is feeded before handshaking and
  52. # the wolfSSL client needs a reply to stop
  53. (sleep 1;echo -n "I hear you fa shizzle...") | \
  54. ${OPENSSL} s_server \
  55. -${dtls_version} \
  56. -port ${server_port} \
  57. -debug \
  58. -use_srtp ${srtp_profile} \
  59. -keymatexport EXTRACTOR-dtls_srtp \
  60. -keymatexportlen $ekm_size \
  61. -cert ./certs/server-cert.pem \
  62. -key ./certs/server-key.pem >$server_output_file &
  63. # make sure the server is up
  64. sleep 0.1
  65. }
  66. # Start an wolfssl client dtls with srtp
  67. # $1: dtsl version [1.0, 1.2]
  68. # $2: srtp profile string
  69. start_wolfssl_client() {
  70. srtp_profile=$2
  71. if [ "$1" = "1.0" ]; then
  72. dtls_version=2
  73. elif [ "$1" = "1.2" ]; then
  74. dtls_version=3
  75. fi
  76. client_output_file=/tmp/wolfssl_srtp_out
  77. ${WOLFSSL_CLIENT} -u\
  78. -x \
  79. -v${dtls_version} \
  80. --srtp ${srtp_profile} \
  81. -p${server_port} >$client_output_file
  82. }
  83. # $1 openssl file
  84. # $2 wolfssl file
  85. check_ekm() {
  86. openssl_ekm=$(cat "$1" | grep "Keying material: " | cut -d ':' -f 2)
  87. echo "OPENSSL EKM: $openssl_ekm"
  88. wolfssl_ekm=$(cat "$2" | grep "DTLS SRTP: Exported key material: " | cut -d ':' -f 3)
  89. echo "WOLFSSL EKM: $wolfssl_ekm"
  90. if [ "$openssl_ekm" = "$wolfssl_ekm" ];then
  91. check_ret=0
  92. else
  93. check_ret=1
  94. fi
  95. }
  96. # $1 dtsl version
  97. # $2 srtp profile
  98. check_dtls_srtp() {
  99. start_openssl_server $1 $2
  100. start_wolfssl_client $1 $2
  101. check_ekm $server_output_file $client_output_file
  102. echo -n "check dtls $1 $2... "
  103. if [ $check_ret -ne 0 ];then
  104. echo "failed"
  105. exit -1
  106. else
  107. echo "ok"
  108. fi
  109. }
  110. # SRTP_NULL_SHA1_80" and SRTP_NULL_SHA1_32 aren't supported by OpenSSL
  111. PROFILES="SRTP_AES128_CM_SHA1_80 \
  112. SRTP_AES128_CM_SHA1_32 \
  113. SRTP_AEAD_AES_128_GCM \
  114. SRTP_AEAD_AES_256_GCM"
  115. for DTLS in 1.0 1.2;do
  116. for SRTP_PROF in $PROFILES;do
  117. check_dtls_srtp $DTLS $SRTP_PROF
  118. done
  119. done