Browse code

jwt: renamed parameter to indicate is a path to file

Daniel-Constantin Mierla authored on 23/11/2022 09:32:40
Showing 1 changed files
... ...
@@ -44,7 +44,7 @@ static void mod_destroy(void);
44 44
 
45 45
 static int w_jwt_generate_4(sip_msg_t* msg, char* pkey, char* palg, char* pclaims, char* pheaders);
46 46
 static int w_jwt_generate_3(sip_msg_t* msg, char* pkey, char* palg, char* pclaims);
47
-static int w_jwt_verify(sip_msg_t* msg, char* pkey, char* palg, char* pclaims,
47
+static int w_jwt_verify(sip_msg_t* msg, char* pkeypath, char* palg, char* pclaims,
48 48
 		char *pjwtval);
49 49
 
50 50
 static int _jwt_key_mode = 0;
... ...
@@ -418,16 +418,13 @@ static int w_jwt_generate_4(sip_msg_t* msg, char* pkey, char* palg, char* pclaim
418 418
 /**
419 419
  *
420 420
  */
421
-static int ki_jwt_verify(sip_msg_t* msg, str *key, str *alg, str *claims,
421
+static int ki_jwt_verify_key(sip_msg_t* msg, str *key, str *alg, str *claims,
422 422
 		str *jwtval)
423 423
 {
424 424
 	str dupclaims = STR_NULL;
425 425
 	jwt_alg_t valg = JWT_ALG_NONE;
426 426
 	str kdata = STR_NULL;
427 427
 	time_t iat;
428
-	FILE *fpk = NULL;
429
-	unsigned char keybuf[10240];
430
-	size_t keybuf_len = 0;
431 428
 	param_t* params_list = NULL;
432 429
 	param_hooks_t phooks;
433 430
 	param_t *pit = NULL;
... ...
@@ -446,6 +443,8 @@ static int ki_jwt_verify(sip_msg_t* msg, str *key, str *alg, str *claims,
446 443
 
447 444
 	_jwt_verify_status = 0;
448 445
 
446
+	kdata = *key;
447
+	trim(&kdata);
449 448
 	valg = jwt_str_alg(alg->s);
450 449
 	if (valg == JWT_ALG_INVAL) {
451 450
 		LM_ERR("not supported algorithm: %s\n", alg->s);
... ...
@@ -455,25 +454,6 @@ static int ki_jwt_verify(sip_msg_t* msg, str *key, str *alg, str *claims,
455 454
 		LM_ERR("failed to duplicate claims\n");
456 455
 		return -1;
457 456
 	}
458
-	jwt_fcache_get(key, &kdata);
459
-	if(kdata.s==NULL) {
460
-		fpk= fopen(key->s, "r");
461
-		if(fpk==NULL) {
462
-			LM_ERR("failed to read key file: %s\n", key->s);
463
-			goto error;
464
-		}
465
-		keybuf_len = fread(keybuf, 1, sizeof(keybuf), fpk);
466
-		fclose(fpk);
467
-		if(keybuf_len==0) {
468
-			LM_ERR("unable to read key file content: %s\n", key->s);
469
-			goto error;
470
-		}
471
-		kdata.s = (char*)keybuf;
472
-		kdata.len = (int)keybuf_len;
473
-		trim(&kdata);
474
-		kdata.s[keybuf_len] = '\0';
475
-		jwt_fcache_add(key, &kdata);
476
-	}
477 457
 	sparams = dupclaims;
478 458
 	if(sparams.s[sparams.len-1]==';') {
479 459
 		sparams.len--;
... ...
@@ -548,15 +528,55 @@ error:
548 528
 /**
549 529
  *
550 530
  */
551
-static int w_jwt_verify(sip_msg_t* msg, char* pkey, char* palg, char* pclaims,
531
+static int ki_jwt_verify(sip_msg_t* msg, str *keypath, str *alg, str *claims,
532
+		str *jwtval)
533
+{
534
+	str kdata = STR_NULL;
535
+	FILE *fpk = NULL;
536
+	unsigned char keybuf[10240];
537
+	size_t keybuf_len = 0;
538
+
539
+	if(keypath==NULL || keypath->s==NULL || alg==NULL || alg->s==NULL
540
+			|| claims==NULL || claims->s==NULL || claims->len<=0
541
+			|| jwtval==NULL || jwtval->s==NULL || jwtval->len<=0) {
542
+		LM_ERR("invalid parameters\n");
543
+		return -1;
544
+	}
545
+
546
+	jwt_fcache_get(keypath, &kdata);
547
+	if(kdata.s==NULL) {
548
+		fpk= fopen(keypath->s, "r");
549
+		if(fpk==NULL) {
550
+			LM_ERR("failed to read key file: %s\n", keypath->s);
551
+			return -1;
552
+		}
553
+		keybuf_len = fread(keybuf, 1, sizeof(keybuf), fpk);
554
+		fclose(fpk);
555
+		if(keybuf_len==0) {
556
+			LM_ERR("unable to read key file content: %s\n", keypath->s);
557
+			return -1;
558
+		}
559
+		kdata.s = (char*)keybuf;
560
+		kdata.len = (int)keybuf_len;
561
+		trim(&kdata);
562
+		kdata.s[keybuf_len] = '\0';
563
+		jwt_fcache_add(keypath, &kdata);
564
+	}
565
+	return ki_jwt_verify_key(msg, &kdata, alg, claims, jwtval);
566
+}
567
+
568
+/**
569
+ *
570
+ */
571
+static int w_jwt_verify(sip_msg_t* msg, char* pkeypath, char* palg, char* pclaims,
552 572
 		char *pjwtval)
553 573
 {
554
-	str skey = STR_NULL;
574
+	str skeypath = STR_NULL;
555 575
 	str salg = STR_NULL;
556 576
 	str sclaims = STR_NULL;
557 577
 	str sjwtval = STR_NULL;
558 578
 
559
-	if (fixup_get_svalue(msg, (gparam_t*)pkey, &skey) != 0) {
579
+	if (fixup_get_svalue(msg, (gparam_t*)pkeypath, &skeypath) != 0) {
560 580
 		LM_ERR("cannot get path to the key file\n");
561 581
 		return -1;
562 582
 	}
... ...
@@ -573,7 +593,7 @@ static int w_jwt_verify(sip_msg_t* msg, char* pkey, char* palg, char* pclaims,
573 593
 		return -1;
574 594
 	}
575 595
 
576
-	return ki_jwt_verify(msg, &skey, &salg, &sclaims, &sjwtval);
596
+	return ki_jwt_verify(msg, &skeypath, &salg, &sclaims, &sjwtval);
577 597
 }
578 598
 
579 599
 /**