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

Add uri2pem.py tool to create pkcs11-provider PEM key files #363

Merged
merged 1 commit into from
Apr 4, 2024

Conversation

space88man
Copy link
Contributor

@space88man space88man commented Mar 20, 2024

Add a cli tool to create PEM files in PKCS#11 PROVIDER URI format

[Updated] Usage:

 usage: uri2pem.py [-h] [--bypass] [--verify] [--out OUTPUT_FILE] keyuri

positional arguments:
  keyuri             the PKCS#11 key URI to encode

options:
  -h, --help         show this help message and exit
  --bypass           skip basic URI checks
  --verify           verify PEM file with OpenSSL; requires --out to be specified
  --out OUTPUT_FILE  output to PEM file, otherwise to stdout

Example run:

OPENSSL_CONF=../../softoken/openssl.cnf python uri2pem.py  \
    --verify --out something.pem \
    'pkcs11:model=NSS%203;manufacturer=Mozilla...type=private'

===== OpenSSL pkey output =====
-----BEGIN RSA PUBLIC KEY-----
MIIBigKCAYEA3BE+a2ysvJMZuccg3QU51dabXNF/XAo5/iUP0UGHLtAYolLlkz2j
s1ZQL5D3FpgPQefIfhLBclkCjHBbtl7HxA3Zci/THoUYYYd+JnhNcNvTduz07vgC
NBsVeYPkrCLr5Yu92NkMKllObFju57K4NjC8i5oNarckt38l03UgPR7tjyBBmXnt
RS1ixBGasQCKo7FxqDQoIZ7alpzoolOioszSEwBQQ+J+4FwGYBo8MS0ksJXaXPAW
313OKR6JcWmDdzMGVgDVbCskX5Y/AEVbum2sdk2aIfUVWAqc3h4NXKAvtM0XjBty
QLvEksmAIYv+6J8vYtD2WpTNW5eW+FWSCAjlfHnTbjqDzUylmHF1AyR9R62EzCRQ
CiOXMOcriExWy8rJBsxI+ODtlnfVS4Qb0Ee58lO1KIHEVmYcBnl+7cAKgq0KqJQZ
dNxBIHp5ZKryUBmb+IJ6P/dwOHFFv7cvqAcOqL0dK5NsDDk9y1bZ+Gp1s5S3JSPL
oaKHnzKVO0mFAgMBAAE=
-----END RSA PUBLIC KEY-----
===== END =====
uri2pem.py: verification of private key PEM(something.pem) OK

@space88man space88man force-pushed the pem-tools branch 2 times, most recently from 1814982 to 0efe892 Compare March 20, 2024 02:30
@simo5
Copy link
Member

simo5 commented Mar 20, 2024

This allows to create anything, I think the tool should try to validate the URI by default (maybe by calling the openssl storeutl tool or something), and provide a bypass mode for those that "know what they are doing".

Also we'd need a test to ensure what this tool is producing actually working files, ie again use an openssl tool to import the file and see that pkcs11 provider was able to parse it.

@space88man
Copy link
Contributor Author

space88man commented Mar 20, 2024

This allows to create anything, I think the tool should try to validate the URI by default (maybe by calling the openssl storeutl tool or something), and provide a bypass mode for those that "know what they are doing".

Update:

  • basic string checks for sane URIs
  • option --out <filename> to write to PEM file
  • if run online (token is available): --out <filename> --verify attempts to load the PEM file using openssl
  • --bypass option to create PEM files with bogus URIs

Also we'd need a test to ensure what this tool is producing actually working files, ie again use an openssl tool to import the file and see that pkcs11 provider was able to parse it.

In tools/tests: add a few pytest tests that use the token in tests/tmp.softokn/tokens: requires that make check be run first. The test will write out the first private key it finds and then verifies that pkey can load the PEM file. The test enables pkcs11-module-encode-provider-uri-to-pem = true via tools/openssl-tools.cnf.

The tests work around a bug in asn1crypto that does not like # sign in PEM labels.

Successful test run should look like:

pip install pytest
# dnf install python3-pytest
cd tools/
pytest tests/
==== test session starts ====
platform linux -- Python 3.12.2, pytest-8.1.1, pluggy-1.4.0
rootdir: /openssl/pkcs11-provider/tools
collected 3 items                                                                                     

tests/test_softoken.py ...                                                                      [100%]

==== 3 passed in 0.18s ====

@space88man space88man force-pushed the pem-tools branch 4 times, most recently from d293ed1 to b72d1fe Compare March 20, 2024 23:36
@simo5 simo5 mentioned this pull request Apr 3, 2024
@simo5
Copy link
Member

simo5 commented Apr 3, 2024

@space88man Can you please fix the REUSE issues?
One needs an optout in .reuse/dep5
The other needs a (C) line in the py file, see any of the tests/ scripts for an example

@space88man space88man force-pushed the pem-tools branch 2 times, most recently from 9aa2ec6 to 39f10c2 Compare April 3, 2024 21:54
@space88man
Copy link
Contributor Author

@space88man Can you please fix the REUSE issues?

  • done!

tools/README.md Outdated Show resolved Hide resolved
Signed-off-by: S-P Chan <shihping.chan@gmail.com>
@simo5
Copy link
Member

simo5 commented Apr 4, 2024

Thanks @space88man, merging

@simo5 simo5 merged commit b8624f0 into latchset:main Apr 4, 2024
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants