Browse code

Merge 0538776ba16899f345061f1b8b68d77408ec0f90 into 444adb0b27d62fbd2af25e4f555b6aab5ca7bc96

space88man authored on 04/07/2022 08:20:54 • GitHub committed on 04/07/2022 08:20:54
Showing 1 changed files
... ...
@@ -630,24 +630,35 @@ 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-bit > log(2^256,10)
636 636
 	X509* cert;
637 637
 	struct tcp_connection* c;
638
-	char* sn;
639
-	int num;
638
+	char* sn = NULL;
639
+	BIGNUM* bn = NULL;
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())) goto error;
644
+	if (!ASN1_INTEGER_to_BN(X509_get_serialNumber(cert), bn)) goto error;
645
+	if (!(sn = BN_bn2dec(bn)) || strlen(sn) > 80) 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
+
658
+ error:
659
+	if (sn) OPENSSL_free(sn);
660
+	if (bn) BN_free(bn);
661
+	return -1;
651 662
 }
652 663
 
653 664
 static int sel_sn(str* res, select_t* s, sip_msg_t* msg)
... ...
@@ -662,7 +673,7 @@ static int sel_sn(str* res, select_t* s, sip_msg_t* msg)
662 673
 		return -1;
663 674
 	}
664 675
 
665
-	return get_sn(res, NULL, local, msg);
676
+	return get_sn(res, local, msg);
666 677
 }
667 678
 
668 679
 
... ...
@@ -679,11 +690,11 @@ static int pv_sn(sip_msg_t* msg, pv_param_t* param, pv_value_t* res)
679 690
 		return pv_get_null(msg, param, res);
680 691
 	}
681 692
 	
682
-	if (get_sn(&res->rs, &res->ri, local, msg) < 0) {
693
+	if (get_sn(&res->rs, local, msg) < 0) {
683 694
 		return pv_get_null(msg, param, res);
684 695
 	}
685 696
 	
686
-	res->flags = PV_VAL_STR | PV_VAL_INT;
697
+	res->flags = PV_VAL_STR;
687 698
 	return 0;
688 699
 }
689 700