Skip to content

Commit

Permalink
[stir_shaken] fix using the right Identity hdr...
Browse files Browse the repository at this point in the history
Iterate all present Identity hdrs and pick the one with passport type "shaken".
This allows the usage of Identity hdrs for both Stir-Shaken and Rich Call Data
  • Loading branch information
bogdan-iancu committed Apr 18, 2024
1 parent b025650 commit c25ffda
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 62 deletions.
115 changes: 53 additions & 62 deletions modules/stir_shaken/stir_shaken.c
Original file line number Diff line number Diff line change
Expand Up @@ -1842,17 +1842,34 @@ static int verify_signature(X509 *cert,
return rc;
}

static int get_parsed_identity(struct hdr_field *identity_hdr,
static int get_parsed_identity(struct sip_msg *msg,
struct parsed_identity **parsed)
{
int rc = 0;
struct hdr_field *identity_hdr;
int rc;

*parsed = parsed_ctx_get();
if (*parsed == NULL) {
if (!current_processing_ctx) {
LM_ERR("no processing ctx found!\n");
return -1;
}
if (*parsed)
/* we are lucky, the parsing was already done */
return 0;

if (!current_processing_ctx) {
LM_ERR("no processing ctx found!\n");
return -1;
}

/* look for the Identity hdr */
if (parse_headers(msg, HDR_EOH_F, 0) < 0) {
LM_ERR("Failed to parse headers\n");
return -1;
}

if (!(identity_hdr = get_header_by_static_name(msg, "Identity"))) {
LM_INFO("No Identity header found\n");
return -1;
}

do {

*parsed = pkg_malloc(sizeof **parsed);
if (*parsed == NULL) {
Expand All @@ -1862,13 +1879,34 @@ static int get_parsed_identity(struct hdr_field *identity_hdr,
memset(*parsed, 0, sizeof **parsed);

rc = parse_identity_hf(&identity_hdr->body, *parsed);
if (rc == 0)
parsed_ctx_set(*parsed);
else
if (rc < 0) {
pkg_free(*parsed);
}
*parsed = NULL;
} else {
/* check the pss type to be "shaken" */
if (str_strcmp(&(*parsed)->ppt_hdr_param, const_str(PPORT_HDR_PPT_VAL))) {
LM_INFO("Unsupported 'ppt' extension\n");
parsed_ctx_free(*parsed);
*parsed = NULL;
rc = -4; // invalid format
}
}

return rc;
if (*parsed==NULL) {
/* let's check other Identity hdr, if present */
identity_hdr = get_next_header_by_static_name ( identity_hdr,
"Identity");
if (identity_hdr==NULL) {
LM_INFO("No valid Identity header found\n");
return rc;
}
}

}while(*parsed==NULL);

parsed_ctx_set(*parsed);

return 0;
}

static int set_err_resp_vars(struct sip_msg *msg, pv_spec_t *err_code_var,
Expand Down Expand Up @@ -1903,7 +1941,6 @@ static int w_stir_verify(struct sip_msg *msg, str *cert_buf,
pv_spec_t *err_code_var, pv_spec_t *err_reason_var,
str *orig_tn_p, str *dest_tn_p)
{
struct hdr_field *identity_hdr;
str orig_tn, dest_tn, pport_orig_tn, pport_dest_tn;
time_t now, date_ts, iat_ts;
struct hdr_field *date_hf = NULL;
Expand All @@ -1913,19 +1950,6 @@ static int w_stir_verify(struct sip_msg *msg, str *cert_buf,
int rc, err_code, orig_log_lev = L_ERR, dest_log_lev = L_ERR;
char *err_reason;

/* looking for 'Identity' and 'Date' */
if (parse_headers(msg, HDR_EOH_F, 0) < 0) {
LM_ERR("Failed to parse headers\n");
SET_VERIFY_ERR_VARS(IERROR_CODE, IERROR_REASON);
return -1;
}

if (!(identity_hdr = get_header_by_static_name(msg, "Identity"))) {
LM_NOTICE("No Identity header found\n");
SET_VERIFY_ERR_VARS(USE_IDENTITY_CODE, USE_IDENTITY_REASON);
return -2;
}

if (!orig_tn_p) {
err_code = BADREQ_CODE;
err_reason = BADREQ_ORIG_REASON;
Expand Down Expand Up @@ -1979,7 +2003,7 @@ static int w_stir_verify(struct sip_msg *msg, str *cert_buf,
return -3;
}

if ((rc = get_parsed_identity(identity_hdr, &parsed)) < 0) {
if ((rc = get_parsed_identity( msg, &parsed)) < 0) {
if (rc == -1) {
LM_ERR("Failed to parse identity header\n");
SET_VERIFY_ERR_VARS(IERROR_CODE, IERROR_REASON);
Expand All @@ -1991,13 +2015,6 @@ static int w_stir_verify(struct sip_msg *msg, str *cert_buf,
return rc;
}

if (str_strcmp(&parsed->ppt_hdr_param, const_str(PPORT_HDR_PPT_VAL))) {
LM_NOTICE("Unsupported 'ppt' extension: %.*s\n",
parsed->ppt_hdr_param.len, parsed->ppt_hdr_param.s);
SET_VERIFY_ERR_VARS(INVALID_IDENTITY_CODE, INVALID_IDENTITY_REASON);
rc = -5;
goto error;
}
if (parsed->alg_hdr_param.s &&
str_strcmp(&parsed->alg_hdr_param, const_str(PPORT_HDR_ALG_VAL))) {
LM_NOTICE("Unsupported 'alg': %.*s\n",
Expand Down Expand Up @@ -2149,21 +2166,10 @@ static int w_stir_verify(struct sip_msg *msg, str *cert_buf,

static int w_stir_check(struct sip_msg *msg)
{
struct hdr_field *identity_hdr;
struct parsed_identity *parsed;
int rc;

if (parse_headers(msg, HDR_EOH_F, 0) < 0) {
LM_ERR("Failed to parse headers\n");
return -1;
}

if (!(identity_hdr = get_header_by_static_name(msg, "Identity"))) {
LM_INFO("No Identity header found\n");
return -2;
}

if ((rc = get_parsed_identity(identity_hdr, &parsed)) < 0) {
if ((rc = get_parsed_identity( msg, &parsed)) < 0) {
if (rc == -1) {
LM_ERR("Failed to parse identity header\n");
return -1;
Expand All @@ -2173,10 +2179,6 @@ static int w_stir_check(struct sip_msg *msg)
}
}

if (str_strcmp(&parsed->ppt_hdr_param, const_str(PPORT_HDR_PPT_VAL))) {
LM_INFO("Unsupported 'ppt' extension\n");
return -4;
}
if (parsed->alg_hdr_param.s &&
str_strcmp(&parsed->alg_hdr_param, const_str(PPORT_HDR_ALG_VAL))) {
LM_INFO("Unsupported 'alg'\n");
Expand Down Expand Up @@ -2251,21 +2253,10 @@ int pv_parse_identity_name(pv_spec_p sp, const str *in)

int pv_get_identity(struct sip_msg *msg, pv_param_t *param, pv_value_t *res)
{
struct hdr_field *identity_hdr;
struct parsed_identity *parsed;
int rc;

if (parse_headers(msg, HDR_EOH_F, 0) < 0) {
LM_ERR("Failed to parse headers\n");
return pv_get_null(msg, param, res);
}

if (!(identity_hdr = get_header_by_static_name(msg, "Identity"))) {
LM_INFO("No Identity header found\n");
return pv_get_null(msg, param, res);
}

if ((rc = get_parsed_identity(identity_hdr, &parsed)) < 0) {
if ((rc = get_parsed_identity( msg, &parsed)) < 0) {
if (rc == -1)
LM_ERR("Failed to parse identity header\n");
else
Expand Down
17 changes: 17 additions & 0 deletions parser/msg_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,23 @@ inline static struct hdr_field *get_header_by_name( struct sip_msg *msg,
}


#define get_next_header_by_static_name(_hdr, _name) \
get_next_header_by_name(_hdr, _name, sizeof(_name)-1)
inline static struct hdr_field *get_next_header_by_name(
struct hdr_field *first, char *s, unsigned int len)
{
struct hdr_field *hdr;

for( hdr=first->next ; hdr ; hdr=hdr->next ) {
if(len==hdr->name.len && strncasecmp(hdr->name.s,s,len)==0)
return hdr;
}
return NULL;
}




/*
* Make a private copy of the string and assign it to new_uri (new RURI)
*/
Expand Down

0 comments on commit c25ffda

Please sign in to comment.