Skip to content

Commit

Permalink
Minor fixes to CRL update command
Browse files Browse the repository at this point in the history
  • Loading branch information
elonen committed Sep 18, 2024
1 parent 79692ed commit 1a7acfc
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions hsm_secrets/x509/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from copy import deepcopy
import re
import click
import datetime
from pathlib import Path
Expand Down Expand Up @@ -277,6 +278,8 @@ def update_crl(ctx: HsmSecretsCtx, crl_file: str, ca: str, out: str, validity: O
Example: '--remove 123456'.
Use '0x' prefix for serial numbers in hex.
Remove and add commands can be specified multiple times.
"""
ca_cert_def = ctx.conf.find_def(ca, HSMOpaqueObject)
Expand Down Expand Up @@ -319,7 +322,10 @@ def update_crl(ctx: HsmSecretsCtx, crl_file: str, ca: str, out: str, validity: O
else:
raise click.ClickException(f"Error: Invalid revocation info: {cert_info}")

if not serial.isdigit():
serial = serial.lower()
if serial.startswith('0x') or (re.match(r'^[0-9a-fA-F]+$', serial) and not serial.isdigit()):
serial = str(int(serial.lstrip('0x'), 16))
elif not serial.isdigit():
raise click.ClickException(f"Error: Invalid serial number: {serial}")
if not (date and date.count('-') == 2):
raise click.ClickException(f"Error: Invalid date format: {date} (use YYYY-MM-DD)")
Expand All @@ -342,14 +348,14 @@ def update_crl(ctx: HsmSecretsCtx, crl_file: str, ca: str, out: str, validity: O
if validity:
next_update = datetime.datetime.now(datetime.UTC) + datetime.timedelta(days=validity)
else:
if not existing_crl.next_update:
if not existing_crl.next_update_utc:
raise click.ClickException("Error: No validity period specified and no existing CRL next_update")
if last_update := existing_crl.last_update:
next_update = last_update + (existing_crl.next_update - last_update)
if last_update := existing_crl.next_update_utc:
next_update = last_update + (existing_crl.next_update_utc - last_update)
cli_info(f"Extending CRL validity to: {next_update} (same duration as previous)")
else:
cli_warn("Warning: Validity time not extended! No last_update in existing CRL, and no new validity period specified.")
next_update = existing_crl.next_update
next_update = existing_crl.next_update_utc

builder = builder.last_update(datetime.datetime.now(datetime.UTC))
builder = builder.next_update(next_update)
Expand Down Expand Up @@ -387,4 +393,4 @@ def show_crl(ctx: HsmSecretsCtx, crl_file: str):
cli_info("Revoked Certificates:")
for cert in crl:
reason = cert.extensions.get_extension_for_class(x509.CRLReason).value.reason
cli_info(f" - Serial: {cert.serial_number}, Revoked On: {cert.revocation_date}, Reason: {reason.value}")
cli_info(f" - Serial: 0x{cert.serial_number:x}, Revoked On: {cert.revocation_date_utc}, Reason: {reason.value}")

0 comments on commit 1a7acfc

Please sign in to comment.