Browse Source

for asn1derlength: save a few bytes

for asn1dertag: catch too-large-value overflow
master
Felix von Leitner 8 years ago
parent
commit
6a4ceca9c0
  1. 2
      scan/scan_asn1derlength.c
  2. 22
      scan/scan_asn1dertag.c
  3. 1
      test/marshal.c

2
scan/scan_asn1derlength.c

@ -2,7 +2,7 @@
size_t scan_asn1derlengthvalue(const char* src,size_t len,unsigned long long* value) {
if (len==0 || len>=-(uintptr_t)src) return 0;
unsigned char i,c=*src;
unsigned int i,c=*src;
unsigned long long l;
if ((c&0x80)==0) {
*value=c&0x7f;

22
scan/scan_asn1dertag.c

@ -2,14 +2,30 @@
size_t scan_asn1dertag(const char* src,size_t len,unsigned long long* length) {
size_t n;
unsigned long long l=0;
for (n=0; n<len; ++n) {
unsigned long long l;
unsigned int bits=0;
if (len==0) return 0;
{
unsigned int k=src[0]&0x7f;
if (!(src[0]&0x80)) {
*length=k;
return 1;
}
if (!(l=k)) return 0; // non-minimal encoding
while (k) { // count bits in leading byte
++bits;
k>>=1;
}
bits=sizeof(l)*8-bits;
}
for (n=1; n<len; ++n) {
if (bits<7) return 0;
bits-=7;
l=(l<<7) | (src[n]&0x7f);
if (!(src[n]&0x80)) {
*length=l;
return n+1;
}
if (n==0 && !l) return 0; // DER says: must be encoded minimally
}
return 0;
}

1
test/marshal.c

@ -139,6 +139,7 @@ int main() {
ull=-1; assert(scan_asn1dertag("\x80\x42_",3,&ull)==0 && ull==-1); // non-minimal encoding
ull=-1; assert(scan_asn1dertag("\x80_",1,&ull)==0 && ull==-1); // incomplete sequence
ull=-1; assert(scan_asn1dertag("\x82\x80_",2,&ull)==0 && ull==-1); // incomplete sequence
ull=-1; assert(scan_asn1dertag("\xff\xff\xff\xff\xff\xff\xff\xff\xff\x7f",10,&ull)==0 && ull==-1); // value too large
ull=-1; assert(scan_asn1derlength("\x00_",2,&ull)==1 && ull==0);
ull=-1; assert(scan_asn1derlengthvalue("\x81\xc2_",3,&ull)==2 && ull==0xc2);

Loading…
Cancel
Save