Browse Source

[tls] Parse X.509 validity times into seconds since the Epoch

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 12 years ago
parent
commit
0610bcb1d2
2 changed files with 17 additions and 24 deletions
  1. 14
    12
      src/crypto/x509.c
  2. 3
    12
      src/include/ipxe/x509.h

+ 14
- 12
src/crypto/x509.c View File

20
 
20
 
21
 #include <string.h>
21
 #include <string.h>
22
 #include <ctype.h>
22
 #include <ctype.h>
23
+#include <time.h>
23
 #include <errno.h>
24
 #include <errno.h>
24
 #include <assert.h>
25
 #include <assert.h>
25
 #include <ipxe/asn1.h>
26
 #include <ipxe/asn1.h>
331
 		} __attribute__ (( packed )) named;
332
 		} __attribute__ (( packed )) named;
332
 		uint8_t raw[7];
333
 		uint8_t raw[7];
333
 	} pairs;
334
 	} pairs;
335
+	struct tm tm;
334
 	const uint8_t *data;
336
 	const uint8_t *data;
335
 	size_t remaining;
337
 	size_t remaining;
336
 	unsigned int tens;
338
 	unsigned int tens;
395
 	}
397
 	}
396
 
398
 
397
 	/* Fill in time */
399
 	/* Fill in time */
398
-	time->year = ( ( pairs.named.century * 100 ) + pairs.named.year );
399
-	time->month = pairs.named.month;
400
-	time->day = pairs.named.day;
401
-	time->hour = pairs.named.hour;
402
-	time->minute = pairs.named.minute;
403
-	time->second = pairs.named.second;
400
+	tm.tm_year = ( ( ( pairs.named.century - 19 ) * 100 ) +
401
+		       pairs.named.year );
402
+	tm.tm_mon = ( pairs.named.month - 1 );
403
+	tm.tm_mday = pairs.named.day;
404
+	tm.tm_hour = pairs.named.hour;
405
+	tm.tm_min = pairs.named.minute;
406
+	tm.tm_sec = pairs.named.second;
407
+
408
+	/* Convert to seconds since the Epoch */
409
+	time->time = mktime ( &tm );
404
 
410
 
405
 	return 0;
411
 	return 0;
406
 }
412
 }
492
 	/* Parse notBefore */
498
 	/* Parse notBefore */
493
 	if ( ( rc = x509_parse_time ( cert, not_before, &cursor ) ) != 0 )
499
 	if ( ( rc = x509_parse_time ( cert, not_before, &cursor ) ) != 0 )
494
 		return rc;
500
 		return rc;
495
-	DBGC ( cert, "X509 %p valid from %04d-%02d-%02d %02d:%02d:%02d\n",
496
-	       cert, not_before->year, not_before->month, not_before->day,
497
-	       not_before->hour, not_before->minute, not_before->second );
501
+	DBGC ( cert, "X509 %p valid from time %lld\n", cert, not_before->time );
498
 	asn1_skip_any ( &cursor );
502
 	asn1_skip_any ( &cursor );
499
 
503
 
500
 	/* Parse notAfter */
504
 	/* Parse notAfter */
501
 	if ( ( rc = x509_parse_time ( cert, not_after, &cursor ) ) != 0 )
505
 	if ( ( rc = x509_parse_time ( cert, not_after, &cursor ) ) != 0 )
502
 		return rc;
506
 		return rc;
503
-	DBGC ( cert, "X509 %p valid until %04d-%02d-%02d %02d:%02d:%02d\n",
504
-	       cert, not_after->year, not_after->month, not_after->day,
505
-	       not_after->hour, not_after->minute, not_after->second );
507
+	DBGC ( cert, "X509 %p valid until time %lld\n", cert, not_after->time );
506
 
508
 
507
 	return 0;
509
 	return 0;
508
 }
510
 }

+ 3
- 12
src/include/ipxe/x509.h View File

11
 
11
 
12
 #include <stdint.h>
12
 #include <stdint.h>
13
 #include <stddef.h>
13
 #include <stddef.h>
14
+#include <time.h>
14
 #include <ipxe/asn1.h>
15
 #include <ipxe/asn1.h>
15
 
16
 
16
 /** ASN.1 OID for joint-iso-itu-t(2) ds(5) attributeType(4) */
17
 /** ASN.1 OID for joint-iso-itu-t(2) ds(5) attributeType(4) */
70
 
71
 
71
 /** An X.509 time */
72
 /** An X.509 time */
72
 struct x509_time {
73
 struct x509_time {
73
-	/** Year */
74
-	uint16_t year;
75
-	/** Month */
76
-	uint8_t month;
77
-	/** Day */
78
-	uint8_t day;
79
-	/** Hour */
80
-	uint8_t hour;
81
-	/** Minute */
82
-	uint8_t minute;
83
-	/** Second */
84
-	uint8_t second;
74
+	/** Seconds since the Epoch */
75
+	time_t time;
85
 };
76
 };
86
 
77
 
87
 /** An X.509 certificate validity period */
78
 /** An X.509 certificate validity period */

Loading…
Cancel
Save