f7190c9
diff -up openssl-1.0.1e/crypto/x509/x509_vfy.c.oob-read openssl-1.0.1e/crypto/x509/x509_vfy.c
f7190c9
--- openssl-1.0.1e/crypto/x509/x509_vfy.c.oob-read	2015-05-25 12:03:41.000000000 +0200
f7190c9
+++ openssl-1.0.1e/crypto/x509/x509_vfy.c	2015-06-09 15:01:51.688640453 +0200
f7190c9
@@ -1702,49 +1702,92 @@ int X509_cmp_time(const ASN1_TIME *ctm,
f7190c9
 	ASN1_TIME atm;
f7190c9
 	long offset;
f7190c9
 	char buff1[24],buff2[24],*p;
f7190c9
-	int i,j;
f7190c9
+	int i, j, remaining;
f7190c9
 
f7190c9
 	p=buff1;
f7190c9
-	i=ctm->length;
f7190c9
+	remaining=ctm->length;
f7190c9
 	str=(char *)ctm->data;
f7190c9
+	/*
f7190c9
+	 * Note that the following (historical) code allows much more slack in the
f7190c9
+	 * time format than RFC5280. In RFC5280, the representation is fixed:
f7190c9
+	 * UTCTime: YYMMDDHHMMSSZ
f7190c9
+	 * GeneralizedTime: YYYYMMDDHHMMSSZ
f7190c9
+	 */
f7190c9
 	if (ctm->type == V_ASN1_UTCTIME)
f7190c9
 		{
f7190c9
-		if ((i < 11) || (i > 17)) return 0;
f7190c9
+		/* YYMMDDHHMM[SS]Z or YYMMDDHHMM[SS](+-)hhmm */
f7190c9
+		int min_length = sizeof("YYMMDDHHMMZ") - 1;
f7190c9
+		int max_length = sizeof("YYMMDDHHMMSS+hhmm") - 1;
f7190c9
+		if (remaining < min_length || remaining > max_length)
f7190c9
+			return 0;
f7190c9
 		memcpy(p,str,10);
f7190c9
 		p+=10;
f7190c9
 		str+=10;
f7190c9
+		remaining -= 10;
f7190c9
 		}
f7190c9
 	else
f7190c9
 		{
f7190c9
-		if (i < 13) return 0;
f7190c9
+		/* YYYYMMDDHHMM[SS[.fff]]Z or YYYYMMDDHHMM[SS[.f[f[f]]]](+-)hhmm */
f7190c9
+		int min_length = sizeof("YYYYMMDDHHMMZ") - 1;
f7190c9
+		int max_length = sizeof("YYYYMMDDHHMMSS.fff+hhmm") - 1;
f7190c9
+		if (remaining < min_length || remaining > max_length)
f7190c9
+			return 0;
f7190c9
 		memcpy(p,str,12);
f7190c9
 		p+=12;
f7190c9
 		str+=12;
f7190c9
+		remaining -= 12;
f7190c9
 		}
f7190c9
 
f7190c9
 	if ((*str == 'Z') || (*str == '-') || (*str == '+'))
f7190c9
 		{ *(p++)='0'; *(p++)='0'; }
f7190c9
 	else
f7190c9
 		{ 
f7190c9
+		/* SS (seconds) */
f7190c9
+		if (remaining < 2)
f7190c9
+			return 0;
f7190c9
 		*(p++)= *(str++);
f7190c9
 		*(p++)= *(str++);
f7190c9
-		/* Skip any fractional seconds... */
f7190c9
-		if (*str == '.')
f7190c9
+		remaining -= 2;
f7190c9
+		/*
f7190c9
+		 * Skip any (up to three) fractional seconds...
f7190c9
+		 * TODO(emilia): in RFC5280, fractional seconds are forbidden.
f7190c9
+		 * Can we just kill them altogether?
f7190c9
+		 */
f7190c9
+		if (remaining && *str == '.')
f7190c9
 			{
f7190c9
 			str++;
f7190c9
-			while ((*str >= '0') && (*str <= '9')) str++;
f7190c9
+			remaining--;
f7190c9
+			for (i = 0; i < 3 && remaining; i++, str++, remaining--)
f7190c9
+				{
f7190c9
+				if (*str < '0' || *str > '9')
f7190c9
+					break;
f7190c9
+				}
f7190c9
 			}
f7190c9
 		
f7190c9
 		}
f7190c9
 	*(p++)='Z';
f7190c9
 	*(p++)='\0';
f7190c9
 
f7190c9
+	/* We now need either a terminating 'Z' or an offset. */
f7190c9
+	if (!remaining)
f7190c9
+		return 0;
f7190c9
 	if (*str == 'Z')
f7190c9
+		{
f7190c9
+		if (remaining != 1)
f7190c9
+			return 0;
f7190c9
 		offset=0;
f7190c9
+		}
f7190c9
 	else
f7190c9
 		{
f7190c9
+	        /* (+-)HHMM */
f7190c9
 		if ((*str != '+') && (*str != '-'))
f7190c9
 			return 0;
f7190c9
+		/* Historical behaviour: the (+-)hhmm offset is forbidden in RFC5280. */
f7190c9
+		if (remaining != 5)
f7190c9
+			return 0;
f7190c9
+		if (str[1] < '0' || str[1] > '9' || str[2] < '0' || str[2] > '9' ||
f7190c9
+			str[3] < '0' || str[3] > '9' || str[4] < '0' || str[4] > '9')
f7190c9
+			return 0;
f7190c9
 		offset=((str[1]-'0')*10+(str[2]-'0'))*60;
f7190c9
 		offset+=(str[3]-'0')*10+(str[4]-'0');
f7190c9
 		if (*str == '-')