@@ -31,12 +31,14 @@ static LTC_INLINE int s_char_to_int(unsigned char x)
3131#endif
3232
3333#define DECODE_V (y , max ) do {\
34+ if (x + 2 > declen) return CRYPT_INVALID_PACKET; \
3435 y = s_char_to_int(buf[x])*10 + s_char_to_int(buf[x+1]); \
3536 if (y >= max) return CRYPT_INVALID_PACKET; \
3637 x += 2; \
3738} while(0)
3839
3940#define DECODE_V4 (y , max ) do {\
41+ if (x + 4 > declen) return CRYPT_INVALID_PACKET; \
4042 y = s_char_to_int(buf[x])*1000 + s_char_to_int(buf[x+1])*100 + s_char_to_int(buf[x+2])*10 + s_char_to_int(buf[x+3]); \
4143 if (y >= max) return CRYPT_INVALID_PACKET; \
4244 x += 4; \
@@ -52,8 +54,8 @@ static LTC_INLINE int s_char_to_int(unsigned char x)
5254int der_decode_generalizedtime (const unsigned char * in , unsigned long * inlen ,
5355 ltc_generalizedtime * out )
5456{
55- unsigned char buf [32 ];
56- unsigned long x ;
57+ unsigned char buf [32 ] = { 0 }; /* initialize as all zeroes */
58+ unsigned long x , declen ;
5759 int y ;
5860
5961 LTC_ARGCHK (in != NULL );
@@ -78,9 +80,10 @@ int der_decode_generalizedtime(const unsigned char *in, unsigned long *inlen,
7880 }
7981 buf [x ] = y ;
8082 }
83+ declen = x ; /* octets decoded into buf - the parsing below must not read past them */
8184 * inlen = 2 + x ;
8285
83- if (x < 15 ) {
86+ if (declen < 15 ) {
8487 return CRYPT_INVALID_PACKET ;
8588 }
8689
@@ -107,14 +110,16 @@ YYYYMMDDhhmmss.fs-hh'mm'
107110 out -> fs = 0 ;
108111
109112 /* now is it Z or . */
113+ if (x >= declen ) {
114+ return CRYPT_INVALID_PACKET ;
115+ }
110116 if (buf [x ] == 'Z' ) {
111117 return CRYPT_OK ;
112118 }
113119 if (buf [x ] == '.' ) {
114120 x ++ ;
115- while (buf [x ] >= '0' && buf [x ] <= '9' ) {
121+ while (x < declen && buf [x ] >= '0' && buf [x ] <= '9' ) {
116122 unsigned fs = out -> fs ;
117- if (x >= sizeof (buf )) return CRYPT_INVALID_PACKET ;
118123 out -> fs *= 10 ;
119124 out -> fs += s_char_to_int (buf [x ]);
120125 if (fs > out -> fs ) return CRYPT_OVERFLOW ;
@@ -123,6 +128,9 @@ YYYYMMDDhhmmss.fs-hh'mm'
123128 }
124129
125130 /* now is it Z, +, - */
131+ if (x >= declen ) {
132+ return CRYPT_INVALID_PACKET ;
133+ }
126134 if (buf [x ] == 'Z' ) {
127135 return CRYPT_OK ;
128136 }
0 commit comments