Return the time in seconds since the Epoch, given the timestring representing the "notBefore" or "notAfter" date from a certificate in ``"%b %d %H:%M:%S %Y %Z"`` strptime format (C locale). "notBefore" or "notAfter" dates must use UTC (RFC 5280). Month is one of: Jan Feb Mar Apr Ma
(cert_time)
| 1499 | # some utility functions |
| 1500 | |
| 1501 | def cert_time_to_seconds(cert_time): |
| 1502 | """Return the time in seconds since the Epoch, given the timestring |
| 1503 | representing the "notBefore" or "notAfter" date from a certificate |
| 1504 | in ``"%b %d %H:%M:%S %Y %Z"`` strptime format (C locale). |
| 1505 | |
| 1506 | "notBefore" or "notAfter" dates must use UTC (RFC 5280). |
| 1507 | |
| 1508 | Month is one of: Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec |
| 1509 | UTC should be specified as GMT (see ASN1_TIME_print()) |
| 1510 | """ |
| 1511 | from time import strptime |
| 1512 | from calendar import timegm |
| 1513 | |
| 1514 | months = ( |
| 1515 | "Jan","Feb","Mar","Apr","May","Jun", |
| 1516 | "Jul","Aug","Sep","Oct","Nov","Dec" |
| 1517 | ) |
| 1518 | time_format = ' %d %H:%M:%S %Y GMT' # NOTE: no month, fixed GMT |
| 1519 | try: |
| 1520 | month_number = months.index(cert_time[:3].title()) + 1 |
| 1521 | except ValueError: |
| 1522 | raise ValueError('time data %r does not match ' |
| 1523 | 'format "%%b%s"' % (cert_time, time_format)) |
| 1524 | else: |
| 1525 | # found valid month |
| 1526 | tt = strptime(cert_time[3:], time_format) |
| 1527 | # return an integer, the previous mktime()-based implementation |
| 1528 | # returned a float (fractional seconds are always zero here). |
| 1529 | return timegm((tt[0], month_number) + tt[2:6]) |
| 1530 | |
| 1531 | PEM_HEADER = "-----BEGIN CERTIFICATE-----" |
| 1532 | PEM_FOOTER = "-----END CERTIFICATE-----" |