fix padding for certs Reference: /n/atom/patch/applied/x509pad Date: Fri Mar 21 01:40:58 CET 2014 Signed-off-by: quanstro@quanstro.net --- /sys/src/libsec/port/x509.c Fri Mar 21 01:40:50 2014 +++ /sys/src/libsec/port/x509.c Fri Mar 21 01:40:52 2014 @@ -665,16 +665,15 @@ switch(elem.val.tag) { case VOctets: newans = catbytes(ans, elem.val.u.octetsval); + freevalfields(&elem.val); freebytes(ans); ans = newans; break; case VEOC: - if(length != -1) { - p = pold; - err = ASN_EINVAL; - } - goto cloop_done; + if(length == -1) + goto cloop_done; + /* no break */ default: p = pold; @@ -683,7 +682,10 @@ } } cloop_done: - ; + if(err != ASN_OK){ + freebytes(ans); + ans = nil; + } } *pp = p; *pbytes = ans; @@ -736,7 +738,9 @@ else lve = mkel(elem, lve); } - if(err == ASN_OK) { + if(err != ASN_OK) + freeelist(lve); + else { /* reverse back to original order */ while(lve != nil) { lveold = lve; @@ -977,8 +981,8 @@ memmove(p, bb->data, bb->len); p += bb->len; } - else - err = ASN_EINVAL; + else + err = ASN_EINVAL; break; case NULLTAG: @@ -1598,7 +1602,6 @@ static Ints7 oid_sha1WithRSAEncryption ={7, 1, 2, 840, 113549, 1, 1, 5 }; static Ints7 oid_sha1WithRSAEncryptionOiw ={6, 1, 3, 14, 3, 2, 29 }; static Ints7 oid_sha256WithRSAEncryption ={7, 1, 2, 840, 113549, 1, 1, 11 }; -static Ints7 oid_shaWithRSASignatureOiw ={6, 1, 3, 14, 3, 2, 15 }; static Ints7 oid_md5 ={6, 1, 2, 840, 113549, 2, 5, 0 }; static Ints *alg_oid_tab[NUMALGS+1] = { (Ints*)&oid_rsaEncryption, @@ -2036,25 +2039,31 @@ return nil; } -static mpint* -pkcs1pad(Bytes *b, mpint *modulus) +mpint* +pkcs1padbuf(uchar *buf, int len, mpint *modulus) { int n = (mpsignif(modulus)+7)/8; int pm1, i; uchar *p; mpint *mp; - pm1 = n - 1 - b->len; + pm1 = n - 1 - len; p = (uchar*)emalloc(n); p[0] = 0; p[1] = 1; for(i = 2; i < pm1; i++) p[i] = 0xFF; p[pm1] = 0; - memcpy(&p[pm1+1], b->data, b->len); + memcpy(&p[pm1+1], buf, len); mp = betomp(p, n, nil); free(p); return mp; +} + +static mpint* +pkcs1pad(Bytes *b, mpint *modulus) +{ + return pkcs1padbuf(b->data, b->len, modulus); } RSApriv*