Skip to content

Commit

Permalink
pki: T4026: Only emit private keys when available
Browse files Browse the repository at this point in the history
* install_certificate() code path handles private_key=None &
  key_passphrase=None OK already
* file and console output paths will error trying to encode None as a key
* This is only an issue for a couple of the generate_*_sign() functions,
  where having a null private key is possible
  * Self-signing and CA creation always generate a private key
  * Certreqs will generate a private key if not already provided
* Do not prompt for a private key passphrase if we aren't giving back a
  private key
  • Loading branch information
talmakion committed Jun 16, 2024
1 parent 6129138 commit d2cf8ee
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/op_mode/pki.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,19 +426,24 @@ def generate_ca_certificate_sign(name, ca_name, install=False, file=False):
return None

cert = generate_certificate(cert_req, ca_cert, ca_private_key, is_ca=True, is_sub_ca=True)
passphrase = ask_passphrase()

passphrase = None
if private_key is not None:
passphrase = ask_passphrase()

if not install and not file:
print(encode_certificate(cert))
print(encode_private_key(private_key, passphrase=passphrase))
if private_key is not None:
print(encode_private_key(private_key, passphrase=passphrase))
return None

if install:
install_certificate(name, cert, private_key, key_type, key_passphrase=passphrase, is_ca=True)

if file:
write_file(f'{name}.pem', encode_certificate(cert))
write_file(f'{name}.key', encode_private_key(private_key, passphrase=passphrase))
if private_key is not None:
write_file(f'{name}.key', encode_private_key(private_key, passphrase=passphrase))

def generate_certificate_sign(name, ca_name, install=False, file=False):
ca_dict = get_config_ca_certificate(ca_name)
Expand Down Expand Up @@ -492,19 +497,24 @@ def generate_certificate_sign(name, ca_name, install=False, file=False):
return None

cert = generate_certificate(cert_req, ca_cert, ca_private_key, is_ca=False)
passphrase = ask_passphrase()

passphrase = None
if private_key is not None:
passphrase = ask_passphrase()

if not install and not file:
print(encode_certificate(cert))
print(encode_private_key(private_key, passphrase=passphrase))
if private_key is not None:
print(encode_private_key(private_key, passphrase=passphrase))
return None

if install:
install_certificate(name, cert, private_key, key_type, key_passphrase=passphrase, is_ca=False)

if file:
write_file(f'{name}.pem', encode_certificate(cert))
write_file(f'{name}.key', encode_private_key(private_key, passphrase=passphrase))
if private_key is not None:
write_file(f'{name}.key', encode_private_key(private_key, passphrase=passphrase))

def generate_certificate_selfsign(name, install=False, file=False):
private_key, key_type = generate_private_key()
Expand Down

0 comments on commit d2cf8ee

Please sign in to comment.