📄 inet_msg_unpack.c
字号:
* opaque = "opaque" "=" quoted-string
* stale = "stale" "=" ( "true" | "false" )
* algorithm = "algorithm" "=" ( "MD5" | "MD5-sess" | token )
* qop-options = "qop" "=" <"> 1#qop-value <">
* qop-value = "auth" | "auth-int" | token
* PARAMETERS
* mem_func [IN] Memory function
* text [IN] String of field value
* RETURNS
* inet_content_type_struct pointer
*****************************************************************************/
kal_uint32 inet_msg_unpack_authenticate(inet_mem_func_struct *mem_func, kal_char *text)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
kal_char *s;
inet_result_enum ret = INET_RESULT_OK;
inet_authenticate_struct *authenticate = NULL;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
if (mem_func == NULL || text == NULL)
{
return 0;
}
if (NULL == (s = strchr(text, ' ')))
{
goto exit_due_to_unpack_err;
}
*s = '\0';
s++;
authenticate = (inet_authenticate_struct*) INET_MEM_ALLOC(sizeof(inet_authenticate_struct));
if (authenticate == NULL)
{
return INET_RESULT_NO_MEMORY;
}
if (strcmp(text, "Digest") == 0)
{
kal_char *key[7];
/* key[0]=domain; key[1]=realm; key[2]=nonce; key[3]=opaque; key[4]=stale; key[5]=algorithm; key[6]=qop; */
kal_mem_set(key, 0x00, 28);
authenticate->auth_type = INET_AUTH_DIGEST;
text = s;
/* domain */
if (NULL != (s = strstr(text, "domain=\"")))
{
s += 8; /* skip domain=" */
key[0] = s;
}
/* realm */
if (NULL != (s = strstr(text, "realm=\"")))
{
s += 7; /* skip realm=" */
key[1] = s;
}
/* nonce */
if (NULL != (s = strstr(text, "nonce=\"")))
{
s += 7; /* skip nonce=" */
key[2] = s;
}
/* opaque */
if (NULL != (s = strstr(text, "opaque=\"")))
{
s += 8; /* skip opaque=" */
key[3] = s;
}
/* stale */
if (NULL != (s = strstr(text, "stale=")))
{
s += 6; /* skip stale= */
key[4] = s;
}
else
{
authenticate->stale = 0;
}
/* algorithm */
if (NULL != (s = strstr(text, "algorithm=")))
{
s += 10; /* skip algorithm=" */
key[5] = s;
}
/* qop */
if (NULL != (s = strstr(text, "qop=\"")))
{
s += 5; /* skip qop=" */
key[6] = s;
}
/* domain */
if (key[0])
{
kal_char *end;
if (NULL == (end = strchr(key[0], '\"')))
{
goto exit_due_to_unpack_err;
}
*end = '\0';
if (NULL ==
(authenticate->domains = (inet_str_list_struct*) inet_msg_unpack_space_string_list(mem_func, key[0])))
{
goto exit_due_to_no_mem;
}
}
/* realm */
if (key[1])
{
kal_char *end;
if (NULL == (end = strchr(key[1], '\"')))
{
goto exit_due_to_unpack_err;
}
*end = '\0';
if (NULL == (authenticate->realm = inet_msg_strdup(mem_func, key[1])))
{
goto exit_due_to_no_mem;
}
}
/* nonce */
if (key[2])
{
kal_char *end;
if (NULL == (end = strchr(key[2], '\"')))
{
goto exit_due_to_unpack_err;
}
*end = '\0';
if (NULL == (authenticate->nonce = inet_msg_strdup(mem_func, key[2])))
{
goto exit_due_to_no_mem;
}
}
/* opaque */
if (key[3])
{
kal_char *end;
if (NULL == (end = strchr(key[3], '\"')))
{
goto exit_due_to_unpack_err;
}
*end = '\0';
if (NULL == (authenticate->opaque = inet_msg_strdup(mem_func, key[3])))
{
goto exit_due_to_no_mem;
}
}
/* stale */
if (key[4])
{
if (*key[4] == 't')
{
authenticate->stale = 1;
}
else
{
authenticate->stale = 0;
}
}
else
{
authenticate->stale = 2;
}
/* algorithm */
if (key[5])
{
kal_uint32 token_len = 0;
inet_msg_get_token(key[5], strlen(key[5]), &token_len);
if (token_len != 0)
{
*(key[5] + token_len) = '\0';
if (inet_msg_compare_cstring(key[5], "MD5") == 0)
{
authenticate->algorithm.int_value = INET_AUTH_ALGO_MD5;
}
else if (inet_msg_compare_cstring(key[5], "MD5-sess") == 0)
{
authenticate->algorithm.int_value = INET_AUTH_ALGO_MD5_SESS;
}
else
{
authenticate->algorithm.int_value = INET_AUTH_ALGO_UNSUPPORT;
authenticate->algorithm.str_value = inet_msg_strdup(mem_func, key[5]);
if (NULL == authenticate->algorithm.str_value)
{
goto exit_due_to_no_mem;
}
}
}
}
else
{
authenticate->algorithm.int_value = INET_AUTH_ALGO_MD5;
}
/* qop */
if (key[6])
{
authenticate->qop = (inet_int_str_list_struct*) inet_msg_unpack_auth_message_qop(mem_func, key[6]);
}
}
else if (strcmp(text, "Basic") == 0)
{
authenticate->auth_type = INET_AUTH_BASIC;
/* Retrieve realm value */
if (s == NULL)
{
goto exit_due_to_unpack_err;
}
else
{
if ((s = strchr(s, '=')) != NULL)
{
*s++ = '\0';
}
s = inet_msg_mime_quote(s);
authenticate->realm = inet_msg_strdup(mem_func, s);
}
if (authenticate->realm == NULL)
{
goto exit_due_to_no_mem;
}
}
else
{
goto exit_due_to_unpack_err;
}
return (kal_uint32) authenticate;
exit_due_to_no_mem:
ret = 0;
exit_due_to_unpack_err:
ret = 0;
if (authenticate != NULL)
{
inet_authenticate_struct_free_fn(mem_func, authenticate);
}
return ret;
}
/*****************************************************************************
* FUNCTION
* inet_msg_unpack_authentication_info
* DESCRIPTION
* This function unpacks authentication-info header.
* GRAMMER
* AuthenticationInfo = "Authentication-Info" ":" auth-info
* auth-info = 1#(nextnonce | [ message-qop ]
* | [ response-auth ] | [ cnonce ]
* | [nonce-count] )
* nextnonce = "nextnonce" "=" nonce-value
* response-auth = "rspauth" "=" response-digest
* response-digest = <"> *LHEX <">
* PARAMETERS
* mem_func [IN] Memory function
* text [IN] String of field value
* RETURNS
* inet_addr_list_struct pointer
*****************************************************************************/
kal_uint32 inet_msg_unpack_authentication_info(inet_mem_func_struct *mem_func, kal_char *text)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
kal_char *value;
kal_char *next_token;
inet_authentication_info_struct *authentication_info = NULL;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
if (mem_func == NULL || text == NULL)
{
return 0;
}
authentication_info = (inet_authentication_info_struct*) INET_MEM_ALLOC(sizeof(inet_authentication_info_struct));
if (authentication_info == NULL)
{
goto exit_err;
}
do
{
next_token = inet_msg_get_next_token(text);
inet_msg_skipcfws(&text);
if ((value = strchr(text, '=')) != NULL)
{
*value++ = '\0';
if (strcmp(text, "nextnonce") == 0)
{
if ((authentication_info->next_nonce = inet_msg_strdup(mem_func, value)) == NULL)
{
goto exit_err;
}
}
else if (strcmp(text, "rspauth") == 0)
{
value = inet_msg_mime_quote(value);
if ((authentication_info->rspauth = inet_msg_strdup(mem_func, value)) == NULL)
{
goto exit_err;
}
}
else if (strcmp(text, "cnonce") == 0)
{
if ((authentication_info->cnonce = inet_msg_strdup(mem_func, value)) == NULL)
{
goto exit_err;
}
}
else if (strcmp(text, "nc") == 0)
{
authentication_info->nc = atoi(value);
}
else if (strcmp(text, "qop") == 0)
{
if ((authentication_info->qop =
(inet_int_str_list_struct*) inet_msg_unpack_auth_message_qop(mem_func, value)) == NULL)
{
goto exit_err;
}
}
}
else
{
/* abnormal value */
goto exit_err;
}
text = next_token;
} while (text);
return (kal_uint32) authentication_info;
exit_err:
if (authentication_info)
{
inet_authentication_info_struct_free_fn(mem_func, authentication_info);
}
return 0;
}
/*****************************************************************************
* FUNCTION
* inet_msg_unpack_authorization
* DESCRIPTION
*
* PARAMETERS
* mem_func [?]
* text [?]
* RETURNS
*
*****************************************************************************/
kal_uint32 inet_msg_unpack_authorization(inet_mem_func_struct *mem_func, kal_char *text)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
kal_char *value;
kal_char *next_token;
inet_authorization_struct *authorization = NULL;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
/* Authorization: Digest username="test1@mtk.com.tw",realm="mtk.com.tw",nonce="77e733713a546dd0026203f9h3d049b0",uri="sip:mtk.com.tw",response="c89fd3f8eb66b6bfff928e385407d590",algorithm=MD5,cnonce="482329",opaque="e619a86fbfdf9ab5b98590bf7f709c63",qop=auth,nc=00000001 */
if (mem_func == NULL || text == NULL)
{
return 0;
}
authorization = (inet_authorization_struct*) INET_MEM_ALLOC(sizeof(inet_authorization_struct));
if (authorization == NULL)
{
goto exit_err;
}
if ((value = strchr(text, ' ')) != NULL)
{
*value++ = '\0';
if (strcmp(text, "Digest") == 0)
{
authorization->auth_type = INET_AUTH_DIGEST;
}
else if (strcmp(text, "Basic") == 0)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -