Browse code

Merge e7839bece62dd400154c1506f3560ed75cf46ed0 into ecbe90524165cf2fd8081ec5fe81d674f7a3334a

space88man authored on 30/06/2022 09:13:47 • GitHub committed on 30/06/2022 09:13:47
Showing 1 changed files
... ...
@@ -630,24 +630,33 @@ static int pv_validity(sip_msg_t* msg, pv_param_t* param, pv_value_t* res)
630 630
 }
631 631
 
632 632
 
633
-static int get_sn(str* res, int* ires, int local, sip_msg_t* msg)
633
+static int get_sn(str* res, int local, sip_msg_t* msg)
634 634
 {
635
-	static char buf[INT2STR_MAX_LEN];
635
+	static char buf[80]; // handle 256-bits log(2^256,10)
636 636
 	X509* cert;
637 637
 	struct tcp_connection* c;
638 638
 	char* sn;
639
-	int num;
639
+	BIGNUM* bn;
640 640
 
641 641
 	if (get_cert(&cert, &c, msg, local) < 0) return -1;
642 642
 
643
-	num = ASN1_INTEGER_get(X509_get_serialNumber(cert));
644
-	sn = int2str(num, &res->len);
643
+	if (!(bn = BN_new())) return -1;
644
+	if (!ASN1_INTEGER_to_BN(X509_get_serialNumber(cert), bn)) goto error;
645
+	if (!(sn = BN_bn2dec(bn))) goto error;
646
+
647
+	res->len = strlen(sn);
645 648
 	memcpy(buf, sn, res->len);
646 649
 	res->s = buf;
647
-	if (ires) *ires = num;
650
+
648 651
 	if (!local) X509_free(cert);
649 652
 	tcpconn_put(c);
653
+
654
+	BN_free(bn);
655
+	OPENSSL_free(sn);
650 656
 	return 0;
657
+error:
658
+	BN_free(bn);
659
+	return -1;
651 660
 }
652 661
 
653 662
 static int sel_sn(str* res, select_t* s, sip_msg_t* msg)
... ...
@@ -662,7 +671,7 @@ static int sel_sn(str* res, select_t* s, sip_msg_t* msg)
662 671
 		return -1;
663 672
 	}
664 673
 
665
-	return get_sn(res, NULL, local, msg);
674
+	return get_sn(res, local, msg);
666 675
 }
667 676
 
668 677
 
... ...
@@ -678,12 +687,13 @@ static int pv_sn(sip_msg_t* msg, pv_param_t* param, pv_value_t* res)
678 687
 		BUG("could not determine certificate\n");
679 688
 		return pv_get_null(msg, param, res);
680 689
 	}
681
-	
682
-	if (get_sn(&res->rs, &res->ri, local, msg) < 0) {
690
+
691
+	// serial no can be > 2^64 cannot store in res->ri
692
+	if (get_sn(&res->rs, local, msg) < 0) {
683 693
 		return pv_get_null(msg, param, res);
684 694
 	}
685 695
 	
686
-	res->flags = PV_VAL_STR | PV_VAL_INT;
696
+	res->flags = PV_VAL_STR;
687 697
 	return 0;
688 698
 }
689 699