tsip_parser_header_Security_Verify.rl 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Copyright (C) 2010-2011 Mamadou Diop.
  3. *
  4. * Contact: Mamadou Diop <diopmamadou(at)doubango[dot]org>
  5. *
  6. * This file is part of Open Source Doubango Framework.
  7. *
  8. * DOUBANGO is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * DOUBANGO is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with DOUBANGO.
  20. *
  21. */
  22. /**@file tsip_header_Security_Verify.c
  23. * @brief SIP Security-Verify header as per RFC 3329.
  24. *
  25. * @author Mamadou Diop <diopmamadou(at)doubango[dot]org>
  26. *
  27. */
  28. #include "tinysip/headers/tsip_header_Security_Verify.h"
  29. #include "tinysip/parsers/tsip_parser_uri.h"
  30. #include "tsk_debug.h"
  31. #include "tsk_memory.h"
  32. #include "tsk_time.h"
  33. #include <string.h>
  34. /***********************************
  35. * Ragel state machine.
  36. */
  37. %%{
  38. machine tsip_machine_parser_header_Security_Verify;
  39. # Includes
  40. include tsip_machine_utils "./ragel/tsip_machine_utils.rl";
  41. action tag{
  42. tag_start = p;
  43. }
  44. action create_securityverify{
  45. if(!curr_securityverify){
  46. curr_securityverify = tsip_header_Security_Verify_create_null();
  47. }
  48. }
  49. action add_securityverify{
  50. if(curr_securityverify){
  51. tsk_list_push_back_data(hdr_securityverifies, ((void**) &curr_securityverify));
  52. }
  53. }
  54. action parse_mech{
  55. if(curr_securityverify){
  56. TSK_PARSER_SET_STRING(curr_securityverify->mech);
  57. }
  58. }
  59. action parse_port_s{
  60. if(curr_securityverify){
  61. TSK_PARSER_SET_INT(curr_securityverify->port_s);
  62. }
  63. }
  64. action parse_port_c{
  65. if(curr_securityverify){
  66. TSK_PARSER_SET_INT(curr_securityverify->port_c);
  67. }
  68. }
  69. action parse_spi_s{
  70. if(curr_securityverify){
  71. TSK_PARSER_SET_UINT(curr_securityverify->spi_s);
  72. }
  73. }
  74. action parse_spi_c{
  75. if(curr_securityverify){
  76. TSK_PARSER_SET_UINT(curr_securityverify->spi_c);
  77. }
  78. }
  79. action parse_ealg{
  80. if(curr_securityverify){
  81. TSK_PARSER_SET_STRING(curr_securityverify->ealg);
  82. }
  83. }
  84. action parse_alg{
  85. if(curr_securityverify){
  86. TSK_PARSER_SET_STRING(curr_securityverify->alg);
  87. }
  88. }
  89. action parse_prot{
  90. if(curr_securityverify){
  91. TSK_PARSER_SET_STRING(curr_securityverify->prot);
  92. }
  93. }
  94. action parse_preference{
  95. if(curr_securityverify){
  96. TSK_PARSER_SET_DOUBLE(curr_securityverify->q);
  97. }
  98. }
  99. action parse_param{
  100. if(curr_securityverify){
  101. TSK_PARSER_ADD_PARAM(TSIP_HEADER_PARAMS(curr_securityverify));
  102. }
  103. }
  104. action eob{
  105. }
  106. mech_extension = generic_param>tag %parse_param;
  107. port_s = "port-s"i EQUAL DIGIT+>tag %parse_port_s;
  108. port_c = "port-c"i EQUAL DIGIT+>tag %parse_port_c;
  109. spi_s = "spi-s"i EQUAL DIGIT+>tag %parse_spi_s;
  110. spi_c = "spi-c"i EQUAL DIGIT+>tag %parse_spi_c;
  111. ealg = "ealg"i EQUAL token>tag %parse_ealg;
  112. alg = "alg"i EQUAL token>tag %parse_alg;
  113. prot = "prot"i EQUAL token>tag %parse_prot;
  114. preference = "q"i EQUAL qvalue>tag %parse_preference;
  115. mech_parameters = (preference | prot | alg | ealg | spi_c | spi_s | port_c | port_s) @1 | mech_extension @0;
  116. mechanism_name = token>tag %parse_mech;
  117. sec_mechanism = (mechanism_name ( SEMI mech_parameters )*) >create_securityverify %add_securityverify;
  118. Security_Verify = "Security-Verify"i HCOLON sec_mechanism ( COMMA sec_mechanism )*;
  119. # Entry point
  120. main := Security_Verify :>CRLF @eob;
  121. }%%
  122. tsip_header_Security_Verify_t* tsip_header_Security_Verify_create()
  123. {
  124. return tsk_object_new(TSIP_HEADER_SECURITY_VERIFY_VA_ARGS());
  125. }
  126. tsip_header_Security_Verify_t* tsip_header_Security_Verify_create_null()
  127. {
  128. return tsip_header_Security_Verify_create();
  129. }
  130. int tsip_header_Security_Verify_serialize(const tsip_header_t* header, tsk_buffer_t* output)
  131. {
  132. if(header){
  133. const tsip_header_Security_Verify_t *Security_Verify = (const tsip_header_Security_Verify_t *)header;
  134. int ret = 0;
  135. // ipsec-3gpp; alg=hmac-md5-96; ealg=des-ede3-cbc; spi-c=1111; spi-s=2222; port-c=5062; port-s=5064
  136. if(tsk_striequals(Security_Verify->mech, "ipsec-3gpp")){
  137. ret = tsk_buffer_append_2(output, "%s%s%s%s%s%s%s;spi-c=%u;spi-s=%u;port-c=%u;port-s=%u",
  138. Security_Verify->mech,
  139. Security_Verify->alg ? ";alg=" : "",
  140. Security_Verify->alg ? Security_Verify->alg : "",
  141. Security_Verify->ealg ? ";ealg=" : "",
  142. Security_Verify->ealg ? Security_Verify->ealg : "",
  143. Security_Verify->prot ? ";prot=" : "",
  144. Security_Verify->prot ? Security_Verify->prot : "",
  145. Security_Verify->spi_c,
  146. Security_Verify->spi_s,
  147. Security_Verify->port_c,
  148. Security_Verify->port_s
  149. );
  150. }
  151. else if(Security_Verify->mech){
  152. tsk_buffer_append(output, Security_Verify->mech, tsk_strlen(Security_Verify->mech));
  153. }
  154. if(Security_Verify->q >= 0){
  155. /* qvalue = ("0" [ "." 0*3DIGIT ] ) / ( "1" [ "." 0*3("0") ] ) */
  156. tsk_buffer_append_2(output, ";q=%1.3f", Security_Verify->q);
  157. }
  158. return ret;
  159. }
  160. return -1;
  161. }
  162. tsip_header_Security_Verifies_L_t *tsip_header_Security_Verify_parse(const char *data, tsk_size_t size)
  163. {
  164. int cs = 0;
  165. const char *p = data;
  166. const char *pe = p + size;
  167. const char *eof = pe;
  168. tsip_header_Security_Verifies_L_t *hdr_securityverifies = tsk_list_create();
  169. const char *tag_start = tsk_null;
  170. tsip_header_Security_Verify_t *curr_securityverify = tsk_null;
  171. %%write data;
  172. (void)(eof);
  173. (void)(tsip_machine_parser_header_Security_Verify_first_final);
  174. (void)(tsip_machine_parser_header_Security_Verify_error);
  175. (void)(tsip_machine_parser_header_Security_Verify_en_main);
  176. %%write init;
  177. %%write exec;
  178. if( cs < %%{ write first_final; }%% ){
  179. TSK_DEBUG_ERROR("Failed to parse 'Security-Verify' header.");
  180. TSK_OBJECT_SAFE_FREE(curr_securityverify);
  181. TSK_OBJECT_SAFE_FREE(hdr_securityverifies);
  182. }
  183. return hdr_securityverifies;
  184. }
  185. //========================================================
  186. // Security_Verify header object definition
  187. //
  188. static tsk_object_t* tsip_header_Security_Verify_ctor(tsk_object_t *self, va_list * app)
  189. {
  190. tsip_header_Security_Verify_t *Security_Verify = self;
  191. if(Security_Verify){
  192. TSIP_HEADER(Security_Verify)->type = tsip_htype_Security_Verify;
  193. TSIP_HEADER(Security_Verify)->serialize = tsip_header_Security_Verify_serialize;
  194. Security_Verify->q = -1;
  195. }
  196. else{
  197. TSK_DEBUG_ERROR("Failed to create new Security_Verify header.");
  198. }
  199. return self;
  200. }
  201. static tsk_object_t* tsip_header_Security_Verify_dtor(tsk_object_t *self)
  202. {
  203. tsip_header_Security_Verify_t *Security_Verify = self;
  204. if(Security_Verify){
  205. TSK_FREE(Security_Verify->mech);
  206. TSK_FREE(Security_Verify->alg);
  207. TSK_FREE(Security_Verify->prot);
  208. TSK_FREE(Security_Verify->mod);
  209. TSK_FREE(Security_Verify->ealg);
  210. TSK_OBJECT_SAFE_FREE(TSIP_HEADER_PARAMS(Security_Verify));
  211. }
  212. else{
  213. TSK_DEBUG_ERROR("Null Security_Verify header.");
  214. }
  215. return self;
  216. }
  217. static const tsk_object_def_t tsip_header_Security_Verify_def_s =
  218. {
  219. sizeof(tsip_header_Security_Verify_t),
  220. tsip_header_Security_Verify_ctor,
  221. tsip_header_Security_Verify_dtor,
  222. tsk_null
  223. };
  224. const tsk_object_def_t *tsip_header_Security_Verify_def_t = &tsip_header_Security_Verify_def_s;