Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using cert from nssdb in cert-export #4578

Merged
merged 2 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions base/common/python/pki/nssdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2009,6 +2009,8 @@ def get_cert_info(self, nickname, token=None):
cert = {}
cert['object'] = cert_obj

cert['data'] = self.get_cert(nickname=nickname, token=token, output_format='base64')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably can reuse the return value from get_cert() in line 2001, then convert it to base64 with convert_cert().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tried to use the cert_pem value but it is a binary string and the convert_cert() has some trouble to convert.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, no worries then, we can deal with it another time. Thanks!


cert['serial_number'] = cert_obj.serial_number

cert['issuer'] = pki.convert_x509_name_to_dn(cert_obj.issuer)
Expand Down
28 changes: 20 additions & 8 deletions base/server/python/pki/server/cli/cert.py
Original file line number Diff line number Diff line change
Expand Up @@ -959,11 +959,17 @@ def execute(self, argv):
logger.info('Exporting %s certificate into %s.', cert_id, cert_file)

cert_data = cert.get('data')
if cert_data is None:
logger.error('Unable to find certificate data for %s', cert_id)
sys.exit(1)
if cert_data:
cert_data = pki.nssdb.convert_cert(cert_data, 'base64', 'pem')
else:
crt_path = os.path.join(instance.conf_dir, 'conf', 'certs', cert_id + '.crt')
try:
with open(crt_path, 'r', encoding='utf-8') as f:
cert_data = ''.join(f.readlines())
except FileNotFoundError:
logger.error('Unable to find certificate data for %s', cert_id)
sys.exit(1)

cert_data = pki.nssdb.convert_cert(cert_data, 'base64', 'pem')
with open(cert_file, 'w', encoding='utf-8') as f:
f.write(cert_data)

Expand All @@ -972,11 +978,17 @@ def execute(self, argv):
logger.info('Exporting %s CSR into %s.', cert_id, csr_file)

cert_request = cert.get('request')
if cert_request is None:
logger.error('Unable to find certificate request for %s', cert_id)
sys.exit(1)
if cert_request:
csr_data = pki.nssdb.convert_csr(cert_request, 'base64', 'pem')
else:
csr_path = os.path.join(instance.conf_dir, 'conf', 'certs', cert_id + '.csr')
try:
with open(csr_path, 'r', encoding='utf-8') as f:
csr_data = ''.join(f.readlines())
except FileNotFoundError:
logger.error('Unable to find certificate request for %s', cert_id)
sys.exit(1)

csr_data = pki.nssdb.convert_csr(cert_request, 'base64', 'pem')
with open(csr_file, 'w', encoding='utf-8') as f:
f.write(csr_data)

Expand Down
Loading