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)
| 1448 | # some utility functions |
| 1449 | |
| 1450 | def cert_time_to_seconds(cert_time): |
| 1451 | """Return the time in seconds since the Epoch, given the timestring |
| 1452 | representing the "notBefore" or "notAfter" date from a certificate |
| 1453 | in ``"%b %d %H:%M:%S %Y %Z"`` strptime format (C locale). |
| 1454 | |
| 1455 | "notBefore" or "notAfter" dates must use UTC (RFC 5280). |
| 1456 | |
| 1457 | Month is one of: Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec |
| 1458 | UTC should be specified as GMT (see ASN1_TIME_print()) |
| 1459 | """ |
| 1460 | from time import strptime |
| 1461 | from calendar import timegm |
| 1462 | |
| 1463 | months = ( |
| 1464 | "Jan","Feb","Mar","Apr","May","Jun", |
| 1465 | "Jul","Aug","Sep","Oct","Nov","Dec" |
| 1466 | ) |
| 1467 | time_format = ' %d %H:%M:%S %Y GMT' # NOTE: no month, fixed GMT |
| 1468 | try: |
| 1469 | month_number = months.index(cert_time[:3].title()) + 1 |
| 1470 | except ValueError: |
| 1471 | raise ValueError('time data %r does not match ' |
| 1472 | 'format "%%b%s"' % (cert_time, time_format)) |
| 1473 | else: |
| 1474 | # found valid month |
| 1475 | tt = strptime(cert_time[3:], time_format) |
| 1476 | # return an integer, the previous mktime()-based implementation |
| 1477 | # returned a float (fractional seconds are always zero here). |
| 1478 | return timegm((tt[0], month_number) + tt[2:6]) |
| 1479 | |
| 1480 | PEM_HEADER = "-----BEGIN CERTIFICATE-----" |
| 1481 | PEM_FOOTER = "-----END CERTIFICATE-----" |