shithub: riscv

Download patch

ref: 6386a0391a11fd3c5216dfe1478fda08ae8bccbc
parent: 486523bbc57b111d198432d2416e598c6d5c2d63
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Mon Feb 6 16:40:42 EST 2017

libsec: handle signed asn.1 bigint to mpint conversion for x509

--- a/sys/src/libsec/port/x509.c
+++ b/sys/src/libsec/port/x509.c
@@ -2129,8 +2129,12 @@
 
 	if(is_int(e, &v))
 		return itomp(v, nil);
-	if(is_bigint(e, &b))
-		return betomp(b->data, b->len, nil);
+	if(is_bigint(e, &b)){
+		mpint *s = betomp(b->data, b->len, nil);
+		if(b->len > 0 && (b->data[0] & 0x80) != 0)
+			mpxtend(s, b->len*8, s);
+		return s;
+	}
 	return nil;
 }
 
@@ -2466,7 +2470,15 @@
 	e.tag.num = INTEGER;
 	e.val.tag = VBigInt;
 	e.val.u.bigintval = newbytes((mpsignif(p)+8)/8);
-	mptober(p, e.val.u.bigintval->data, e.val.u.bigintval->len);
+	if(p->sign < 0){
+		mpint *s = mpnew(e.val.u.bigintval->len*8+1);
+		mpleft(mpone, e.val.u.bigintval->len*8, s);
+		mpadd(p, s, s);
+		mptober(s, e.val.u.bigintval->data, e.val.u.bigintval->len);
+		mpfree(s);
+	} else {
+		mptober(p, e.val.u.bigintval->data, e.val.u.bigintval->len);
+	}
 	return e;
 }