Browse code

secsipid: add function to build identity value and store in pv

- new function secsipid_build_identity(...)
- if successful, identity value is available in $secsipid(val)

Daniel-Constantin Mierla authored on 17/06/2021 13:40:16
Showing 1 changed files
... ...
@@ -55,13 +55,25 @@ static int w_secsipid_check_identity(sip_msg_t *msg, char *pkeypath, char *str2)
55 55
 static int w_secsipid_check_identity_pubkey(sip_msg_t *msg, char *pkeyval, char *str2);
56 56
 static int w_secsipid_add_identity(sip_msg_t *msg, char *porigtn, char *pdesttn,
57 57
 			char *pattest, char *porigid, char *px5u, char *pkeypath);
58
+static int w_secsipid_build_identity(sip_msg_t *msg, char *porigtn, char *pdesttn,
59
+			char *pattest, char *porigid, char *px5u, char *pkeypath);
58 60
 static int w_secsipid_get_url(sip_msg_t *msg, char *purl, char *pout);
59 61
 
60 62
 static int secsipid_libopt_param(modparam_t type, void *val);
61 63
 
64
+static int pv_get_secsipid(sip_msg_t *msg, pv_param_t *param, pv_value_t *res);
65
+static int pv_parse_secsipid_name(pv_spec_p sp, str *in);
66
+
62 67
 static str_list_t *secsipid_libopt_list = NULL;
63 68
 static int secsipid_libopt_list_used = 0;
64 69
 
70
+typedef struct secsipid_data {
71
+	str value;
72
+	int ret;
73
+} secsipid_data_t;
74
+
75
+static secsipid_data_t _secsipid_data = {0};
76
+
65 77
 secsipid_papi_t _secsipid_papi = {0};
66 78
 
67 79
 /* clang-format off */
... ...
@@ -72,6 +84,8 @@ static cmd_export_t cmds[]={
72 84
 		fixup_spve_null, fixup_free_spve_null, ANY_ROUTE},
73 85
 	{"secsipid_add_identity", (cmd_function)w_secsipid_add_identity, 6,
74 86
 		fixup_spve_all, fixup_free_spve_all, ANY_ROUTE},
87
+	{"secsipid_build_identity", (cmd_function)w_secsipid_build_identity, 6,
88
+		fixup_spve_all, fixup_free_spve_all, ANY_ROUTE},
75 89
 	{"secsipid_get_url", (cmd_function)w_secsipid_get_url, 2,
76 90
 		fixup_spve_pvar, fixup_free_spve_pvar, ANY_ROUTE},
77 91
 	{0, 0, 0, 0, 0, 0}
... ...
@@ -89,13 +103,20 @@ static param_export_t params[]={
89 103
 	{0, 0, 0}
90 104
 };
91 105
 
106
+static pv_export_t mod_pvs[] = {
107
+	{{"secsipid", (sizeof("secsipid")-1)}, PVT_OTHER, pv_get_secsipid, 0,
108
+		pv_parse_secsipid_name, 0, 0, 0},
109
+
110
+	{ {0, 0}, 0, 0, 0, 0, 0, 0, 0 }
111
+};
112
+
92 113
 struct module_exports exports = {
93 114
 	"secsipid",
94 115
 	DEFAULT_DLFLAGS, /* dlopen flags */
95 116
 	cmds,
96 117
 	params,
97 118
 	0,              /* exported RPC methods */
98
-	0,              /* exported pseudo-variables */
119
+	mod_pvs,        /* exported pseudo-variables */
99 120
 	0,              /* response function */
100 121
 	mod_init,       /* module initialization function */
101 122
 	child_init,     /* per child init function */
... ...
@@ -308,8 +329,8 @@ static int w_secsipid_check_identity_pubkey(sip_msg_t *msg, char *pkeyval, char
308 329
 /**
309 330
  *
310 331
  */
311
-static int ki_secsipid_add_identity(sip_msg_t *msg, str *origtn, str *desttn,
312
-			str *attest, str *origid, str *x5u, str *keypath)
332
+static int ki_secsipid_add_identity_mode(sip_msg_t *msg, str *origtn, str *desttn,
333
+			str *attest, str *origid, str *x5u, str *keypath, int mode)
313 334
 {
314 335
 	str ibody = STR_NULL;
315 336
 	str hdr = STR_NULL;
... ...
@@ -326,12 +347,25 @@ static int ki_secsipid_add_identity(sip_msg_t *msg, str *origtn, str *desttn,
326 347
 	ibody.len = _secsipid_papi.SecSIPIDGetIdentity(origtn->s, desttn->s,
327 348
 			attest->s, origid->s, x5u->s, keypath->s, &ibody.s);
328 349
 
350
+	if(mode==1) {
351
+		_secsipid_data.ret = ibody.len;
352
+	}
353
+
329 354
 	if(ibody.len<=0) {
330 355
 		LM_ERR("failed to get identity header body (%d)\n", ibody.len);
331 356
 		goto error;
332 357
 	}
333 358
 
334
-	LM_DBG("appending identity: %.*s\n", ibody.len, ibody.s);
359
+	LM_DBG("identity value: %.*s\n", ibody.len, ibody.s);
360
+
361
+	if(mode==1) {
362
+		if(_secsipid_data.value.s) {
363
+			free(_secsipid_data.value.s);
364
+		}
365
+		_secsipid_data.value = ibody;
366
+		return 1;
367
+	}
368
+
335 369
 	if (parse_headers(msg, HDR_EOH_F, 0) == -1) {
336 370
 		LM_ERR("error while parsing message\n");
337 371
 		goto error;
... ...
@@ -372,6 +406,16 @@ error:
372 406
 	return -1;
373 407
 }
374 408
 
409
+/**
410
+ *
411
+ */
412
+static int ki_secsipid_add_identity(sip_msg_t *msg, str *origtn, str *desttn,
413
+			str *attest, str *origid, str *x5u, str *keypath)
414
+{
415
+	return ki_secsipid_add_identity_mode(msg, origtn, desttn,
416
+			attest, origid, x5u, keypath, 0);
417
+}
418
+
375 419
 /**
376 420
  *
377 421
  */
... ...
@@ -410,14 +454,77 @@ static int w_secsipid_add_identity(sip_msg_t *msg, char *porigtn, char *pdesttn,
410 454
 		return -1;
411 455
 	}
412 456
 
413
-	return ki_secsipid_add_identity(msg, &origtn, &desttn,
414
-			&attest, &origid, &x5u, &keypath);
457
+	return ki_secsipid_add_identity_mode(msg, &origtn, &desttn,
458
+			&attest, &origid, &x5u, &keypath, 0);
459
+}
460
+
461
+/**
462
+ *
463
+ */
464
+static int ki_secsipid_build_identity(sip_msg_t *msg, str *origtn, str *desttn,
465
+			str *attest, str *origid, str *x5u, str *keypath)
466
+{
467
+	if(_secsipid_data.value.s) {
468
+		free(_secsipid_data.value.s);
469
+	}
470
+	memset(&_secsipid_data, 0, sizeof(secsipid_data_t));
471
+
472
+	return ki_secsipid_add_identity_mode(msg, origtn, desttn,
473
+			attest, origid, x5u, keypath, 1);
474
+}
475
+
476
+/**
477
+ *
478
+ */
479
+static int w_secsipid_build_identity(sip_msg_t *msg, char *porigtn, char *pdesttn,
480
+			char *pattest, char *porigid, char *px5u, char *pkeypath)
481
+{
482
+	str origtn = STR_NULL;
483
+	str desttn = STR_NULL;
484
+	str attest = STR_NULL;
485
+	str origid = STR_NULL;
486
+	str x5u = STR_NULL;
487
+	str keypath = STR_NULL;
488
+
489
+	if(_secsipid_data.value.s) {
490
+		free(_secsipid_data.value.s);
491
+	}
492
+	memset(&_secsipid_data, 0, sizeof(secsipid_data_t));
493
+
494
+	if(fixup_get_svalue(msg, (gparam_t*)porigtn, &origtn)<0) {
495
+		LM_ERR("failed to get origtn parameter\n");
496
+		return -1;
497
+	}
498
+	if(fixup_get_svalue(msg, (gparam_t*)pdesttn, &desttn)<0) {
499
+		LM_ERR("failed to get desttn parameter\n");
500
+		return -1;
501
+	}
502
+	if(fixup_get_svalue(msg, (gparam_t*)pattest, &attest)<0) {
503
+		LM_ERR("failed to get attest parameter\n");
504
+		return -1;
505
+	}
506
+	if(fixup_get_svalue(msg, (gparam_t*)porigid, &origid)<0) {
507
+		LM_ERR("failed to get origid parameter\n");
508
+		return -1;
509
+	}
510
+	if(fixup_get_svalue(msg, (gparam_t*)px5u, &x5u)<0) {
511
+		LM_ERR("failed to get x5u parameter\n");
512
+		return -1;
513
+	}
514
+	if(fixup_get_svalue(msg, (gparam_t*)pkeypath, &keypath)<0) {
515
+		LM_ERR("failed to get keypath parameter\n");
516
+		return -1;
517
+	}
518
+
519
+	return ki_secsipid_add_identity_mode(msg, &origtn, &desttn,
520
+			&attest, &origid, &x5u, &keypath, 1);
415 521
 }
416 522
 
417 523
 /**
418 524
  *
419 525
  */
420 526
 static str _secsipid_get_url_val = STR_NULL;
527
+
421 528
 /**
422 529
  *
423 530
  */
... ...
@@ -533,6 +640,55 @@ static int secsipid_libopt_param(modparam_t type, void *val)
533 640
 	return 0;
534 641
 }
535 642
 
643
+/**
644
+ *
645
+ */
646
+static int pv_get_secsipid(sip_msg_t *msg, pv_param_t *param, pv_value_t *res)
647
+{
648
+	switch(param->pvn.u.isname.name.n) {
649
+		case 0: /* value */
650
+			if(_secsipid_data.value.s==NULL || _secsipid_data.value.len<=0) {
651
+				return pv_get_null(msg, param, res);
652
+			}
653
+			return pv_get_strval(msg, param, res, &_secsipid_data.value);
654
+		case 1: /* ret code */
655
+			return pv_get_sintval(msg, param, res, _secsipid_data.ret);
656
+	}
657
+	return pv_get_null(msg, param, res);
658
+}
659
+
660
+/**
661
+ *
662
+ */
663
+static int pv_parse_secsipid_name(pv_spec_p sp, str *in)
664
+{
665
+	if(sp==NULL || in==NULL || in->len<=0)
666
+		return -1;
667
+
668
+	/* attributes not related to dst of reply get an id starting with 20 */
669
+	switch(in->len) {
670
+		case 3:
671
+			if(strncmp(in->s, "val", 3)==0)
672
+				sp->pvp.pvn.u.isname.name.n = 0;
673
+			else if(strncmp(in->s, "ret", 3)==0)
674
+				sp->pvp.pvn.u.isname.name.n = 1;
675
+			else goto error;
676
+		break;
677
+
678
+		default:
679
+			goto error;
680
+	}
681
+	sp->pvp.pvn.type = PV_NAME_INTSTR;
682
+	sp->pvp.pvn.u.isname.type = 0;
683
+
684
+	return 0;
685
+
686
+error:
687
+	LM_ERR("unknown PV secsipid key: %.*s\n", in->len, in->s);
688
+	return -1;
689
+
690
+}
691
+
536 692
 /**
537 693
  *
538 694
  */
... ...
@@ -543,15 +699,20 @@ static sr_kemi_t sr_kemi_secsipid_exports[] = {
543 699
 		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
544 700
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
545 701
 	},
702
+	{ str_init("secsipid"), str_init("secsipid_check_identity_pubkey"),
703
+		SR_KEMIP_INT, ki_secsipid_check_identity_pubkey,
704
+		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
705
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
706
+	},
546 707
 	{ str_init("secsipid"), str_init("secsipid_add_identity"),
547 708
 		SR_KEMIP_INT, ki_secsipid_add_identity,
548 709
 		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR,
549 710
 			SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR }
550 711
 	},
551
-	{ str_init("secsipid"), str_init("secsipid_check_identity_pubkey"),
552
-		SR_KEMIP_INT, ki_secsipid_check_identity_pubkey,
553
-		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
554
-			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
712
+	{ str_init("secsipid"), str_init("secsipid_build_identity"),
713
+		SR_KEMIP_INT, ki_secsipid_build_identity,
714
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR,
715
+			SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR }
555 716
 	},
556 717
 	{ str_init("secsipid"), str_init("secsipid_get_url"),
557 718
 		SR_KEMIP_XVAL, ki_secsipid_get_url,