Skip to content

Commit

Permalink
X.509: Fix leap year handling again
Browse files Browse the repository at this point in the history
There are still a couple of minor issues in the X.509 leap year handling:

 (1) To avoid doing a modulus-by-400 in addition to a modulus-by-100 when
     determining whether the year is a leap year or not, I divided the year
     by 100 after doing the modulus-by-100, thereby letting the compiler do
     one instruction for both, and then did a modulus-by-4.

     Unfortunately, I then passed the now-modified year value to mktime64()
     to construct a time value.

     Since this isn't a fast path and since mktime64() does a bunch of
     divisions, just condense down to "% 400".  It's also easier to read.

 (2) The default month length for any February where the year doesn't
     divide by four exactly is obtained from the month_length[] array where
     the value is 29, not 28.

     This is fixed by altering the table.

Reported-by: Rudolf Polzer <[email protected]>
Signed-off-by: David Howells <[email protected]>
Acked-by: David Woodhouse <[email protected]>
Acked-by: Arnd Bergmann <[email protected]>
cc: [email protected]
  • Loading branch information
dhowells committed Feb 29, 2016
1 parent 06aae59 commit ac4cbed
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions crypto/asymmetric_keys/x509_cert_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ int x509_decode_time(time64_t *_t, size_t hdrlen,
unsigned char tag,
const unsigned char *value, size_t vlen)
{
static const unsigned char month_lengths[] = { 31, 29, 31, 30, 31, 30,
static const unsigned char month_lengths[] = { 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31 };
const unsigned char *p = value;
unsigned year, mon, day, hour, min, sec, mon_len;
Expand Down Expand Up @@ -517,9 +517,9 @@ int x509_decode_time(time64_t *_t, size_t hdrlen,
if (year % 4 == 0) {
mon_len = 29;
if (year % 100 == 0) {
year /= 100;
if (year % 4 != 0)
mon_len = 28;
mon_len = 28;
if (year % 400 == 0)
mon_len = 29;
}
}
}
Expand Down

0 comments on commit ac4cbed

Please sign in to comment.