-
Notifications
You must be signed in to change notification settings - Fork 634
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 support for openssl PKCS#11 engine to openssl_posix.c #1780
Open
paulbartell
wants to merge
9
commits into
aws:main
Choose a base branch
from
paulbartell:paulbartell/openssl_libp11_support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9a9f027
Add experimental support for pkcs11 in openssl_posix.c using OpenSC l…
paulbartell 51586fb
Correct typo
paulbartell 3435e25
Fix some result code checks (rc == 1 for success rather than rc == 0)
paulbartell c9b198d
Add support for reading Root CA Certificates from a pkcs11 module.
paulbartell 397cec6
Add a draft README about using the openssl_posix transport with PKCS#11.
paulbartell 37c0613
Cleanup unused parameters
paulbartell f6fca1a
Update lexicon
paulbartell eb9bcfc
Correct formatting
paulbartell 3813275
Update lexicon and deduplicate
paulbartell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
# Install Prerequisites | ||
Install the prerequisites for using openssl with the libp11 pkcs11 engine: | ||
``` | ||
sudo apt install softhsm2 gnutls-bin openssl libengine-pkcs11-openssl libp11-kit0 libp11-kit-dev p11-kit p11-kit-modules | ||
``` | ||
|
||
[SoftHSM2](https://github.com/opendnssec/SoftHSMv2) is used as the pkcs11 module in this documentation. It should be replaced with the actual pkcs11 module you plan to use in production. | ||
|
||
If running softhsm on a linux system, be sure to add your user to the "softhsm" group and log-out/log-in if required by your distribution. | ||
``` | ||
sudo adduser $(whoami) softhsm | ||
``` | ||
|
||
# Initialize token and list objects | ||
List existing tokens: | ||
``` | ||
export GNUTLS_SO_PIN=0000 | ||
export GNUTLS_PIN=0000 | ||
p11tool --login --list-all | ||
``` | ||
|
||
## SoftHSM2 | ||
SoftHSM2 is used as an example PKCS#11 module for testing purposes. Depending on your device security model, it may or may not be suitable for use in produiction. | ||
|
||
|
||
Delete any existing tokens if desired: | ||
``` | ||
softhsm2-util --delete-token --token default | ||
``` | ||
Initalize a new token: | ||
``` | ||
softhsm2-util --init-token --free --label default --pin 0000 --so-pin 0000 | ||
``` | ||
|
||
Check that the new token is accessible: | ||
``` | ||
softhsm2-util --show-slots | ||
``` | ||
|
||
When using p11-kit-proxy, SoftHSM should be automatically detected by the openssl pkcs11 engine. | ||
|
||
## p11-kit-trust | ||
P11-Kit includes a Turst Policy module which exposes the trusted CA certificates installed on a system via PKCS#11. | ||
|
||
Note: By default, this module has the "disable-in: p11-kit-proxy" configuration option set. This will cause p11-kit-proxy to disregard this module altogether. If you plan to use PKCS#11 to access to the system trust store, you may want to disable this confgiuration option to allow access to both the system trust store and the PKCS11 module containing your private key. | ||
|
||
## Microchip CryptoAuthLib for ECC608 | ||
[cryptoauthlib](https://github.com/MicrochipTech/cryptoauthlib) | ||
|
||
## p11-kit-proxy | ||
On linux systems, it is quite common to use the [p11-kit-proxy](https://p11-glue.github.io/p11-glue/p11-kit.html) PKCS#11 module to arbitrate access to one or more other PKCS#11 modules from one or more threads. | ||
|
||
p11-kit and other p11-kit aware packages install default "module" configuration files in /usr/share/p11-kit/modules/. p11-kit also searches /etc/pkcs11/modules and ~/.config/pkcs11/modules for additional user-defined module configurations. | ||
|
||
The Openssl PKCS#11 module is configured to use p11-kit-proxy by default in most linux distributions. | ||
|
||
# Generate a new Key | ||
Generate a new public/private key pair if one has not been pre-provisioned in your hsm. | ||
ECC/ECDSA keys typically have a lower computational overhead, so are preferred on low-resource devices. | ||
Generate either an RSA or ECDSA key. | ||
|
||
## Delete any existing key objects | ||
``` | ||
yes | p11tool --login --delete "pkcs11:object=aws_iot_pk" | ||
``` | ||
## RSA | ||
``` | ||
p11tool --login --generate-rsa --bits 2048 --label "aws_iot_pk" pkcs11:token=default | ||
``` | ||
## ECC / ECDSA | ||
``` | ||
p11tool --login --generate-ecc --curve secp256r1 --label "aws_iot_pk" pkcs11:token=default | ||
``` | ||
|
||
# Generate and register a certificate | ||
A certificate can be generated in any of the three ways listed in the following sections. Choose the option that best fits your device provisioning strategy. | ||
|
||
## Self-Signed Certificate | ||
``` | ||
openssl req -new -x509 -subj '/CN=TestIotDevice01/' -days 30 -sha256 -engine pkcs11 -keyform engine -key "pkcs11:object=aws_iot_pk" -outform pem -out aws_iot_pk.crt -passin pass:0000 | ||
``` | ||
Review the contents of the self-signed certificate: | ||
``` | ||
openssl x509 -in aws_iot_pk.crt -noout -text | ||
``` | ||
Register the self-signed certificate with AWS IoT Core | ||
``` | ||
aws iot register-certificate-without-ca --status=ACTIVE --certificate-pem file://aws_iot_pk.crt | ||
``` | ||
## Certificate Signed by a private CA registered with IoT Core | ||
Follow the guides listed below to generate a new Certificate Authority and register that CA with AWS IoT Core. | ||
|
||
[Generate a new CA](https://docs.aws.amazon.com/iot/latest/developerguide/create-your-CA-cert.html) | ||
|
||
[Register your new CA with AWS IoT Core](https://docs.aws.amazon.com/iot/latest/developerguide/register-CA-cert.html) | ||
|
||
Generate a new CSR | ||
``` | ||
openssl req -new -subj '/CN=TestIotDevice01/' -sha256 -engine pkcs11 -keyform engine -key "pkcs11:object=aws_iot_pk" -outform pem -out aws_iot_pk.csr -passin pass:0000 | ||
``` | ||
View the contents of the generated CSR | ||
``` | ||
openssl x509 -in aws_iot_pk.csr -noout -text | ||
``` | ||
Issue a Certificate from you CA | ||
|
||
Follow the instructions for your particular CA managerment software or using something similar to the openssl command below: | ||
``` | ||
openssl x509 -req \ | ||
-in device_cert_csr_filename \ | ||
-CA root_CA_pem_filename \ | ||
-CAkey root_CA_key_filename \ | ||
-CAcreateserial \ | ||
-out device_cert_pem_filename \ | ||
-days 500 -sha256 | ||
``` | ||
|
||
## Certificate Signed by AWS IoT Core | ||
Generate a new CSR | ||
``` | ||
openssl req -new -subj '/CN=TestIotDevice01/' -sha256 -engine pkcs11 -keyform engine -key "pkcs11:object=aws_iot_pk" -outform pem -out aws_iot_pk.csr -passin pass:0000 | ||
``` | ||
View the contents of the generated CSR | ||
``` | ||
openssl x509 -in aws_iot_pk.csr -noout -text | ||
``` | ||
Submit the CSR to IoT Core, receive the new certificate and register it via the aws cli | ||
``` | ||
aws iot create-certificate-from-csr --set-as-active --certificate-signing-request file://aws_iot_pk.csr --certificate-pem-outfile aws_iot_pk.crt | ||
``` | ||
|
||
# Import certificate back into HSM | ||
Next, import the generated certificate back into your HSM / pkcs11 module: | ||
``` | ||
p11tool --login --load-certificate=aws_iot_pk.crt --write --label=aws_iot_pk "pkcs11:token=default" | ||
``` | ||
|
||
# Build the C-SDK demos: | ||
|
||
Use a cmake command similar to the one listed below to build the desired demos: | ||
|
||
``` | ||
cmake -B build2 -DBUILD_DEMOS=1 -DBUILD_TESTS=0 \ | ||
-DAWS_IOT_ENDPOINT="XXXXXXXXXXXXXX-ats.iot.XX-XXXX-X.amazonaws.com" \ | ||
-DCLIENT_CERT_PATH="pkcs11:token=default;object=aws_iot_pk" \ | ||
-DCLIENT_PRIVATE_KEY_PATH="pkcs11:token=default;object=aws_iot_pk?pin-value=0000" \ | ||
-DROOT_CA_CERT_PATH="pkcs11:token=System%20Trust;object=Amazon%20Root%20CA%201" \ | ||
-DBUILD_CLONE_SUBMODULES=0 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Steps are missing or its not clear how to add/get Amazon Root CA or private rootCA from system trust store.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They're automatically included by the OS.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See https://p11-glue.github.io/p11-glue/p11-kit/manual/trust-module.html for more information.
I'll add a note similar to:
You can confirm this with the follow cli command: