Browse code

tls: cert serial number can exceed uint64

- GH #3168

S-P Chan authored on 29/06/2022 23:19:18
Showing 1 changed files
... ...
@@ -630,24 +630,34 @@ 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 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)) || 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
+	BN_free(bn);
660
+	return -1;
651 661
 }
652 662
 
653 663
 static int sel_sn(str* res, select_t* s, sip_msg_t* msg)
... ...
@@ -662,7 +672,7 @@ static int sel_sn(str* res, select_t* s, sip_msg_t* msg)
662 672
 		return -1;
663 673
 	}
664 674
 
665
-	return get_sn(res, NULL, local, msg);
675
+	return get_sn(res, local, msg);
666 676
 }
667 677
 
668 678
 
... ...
@@ -679,11 +689,11 @@ static int pv_sn(sip_msg_t* msg, pv_param_t* param, pv_value_t* res)
679 689
 		return pv_get_null(msg, param, res);
680 690
 	}
681 691
 	
682
-	if (get_sn(&res->rs, &res->ri, local, msg) < 0) {
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