How to sign TAs with an HSM?
There is one thing I really don't understand about the offline signing process for TAs using an HSM:
In the documentation (https://link.getmailspring.com/link/4CCC8610-79B9-4890-AF1F-7A0AA56F0C20(a)getmailspring.com/0?redirect=https%3A%2F%2Foptee.readthedocs.io%2Fen%2Flatest%2Fbuilding%2Ftrusted_applications.html%23signing-of-tas&recipient=b3AtdGVlQGxpc3RzLnRydXN0ZWRmaXJtd2FyZS5vcmc%3D) it says to generate a keypair with openssl and sign the TAs using the public key as ${TA_SIGN_KEY}. In point 4. the usage of an HSM is described, but since it is not possible to extract the private key from an HSM, I wonder how steps 3.-5. are even possible. Do you mix the previously generated RSA key with the one from the HSM? I cannot image that is as it should be. Can you please clarify this! Thank you
Sign the TAs using the private key I meant...
Quoted 4 lines · Jan Claußen
There is one thing I really don't understand about the offline signing process for TAs using an HSM:
In the documentation (https://link.getmailspring.com/link/756A14AB-7D5B-4EB4-B980-50EB714532CD(a)getmailspring.com/0?redirect=https%3A%2F%2Foptee.readthedocs.io%2Fen%2Flatest%2Fbuilding%2Ftrusted_applications.html%23signing-of-tas&recipient=b3AtdGVlQGxpc3RzLnRydXN0ZWRmaXJtd2FyZS5vcmc%3D) it says to generate a keypair with openssl and sign the TAs using the public key as ${TA_SIGN_KEY}. In point 4. the usage of an HSM is described, but since it is not possible to extract the private key from an HSM, I wonder how steps 3.-5. are even possible. Do you mix the previously generated RSA key with the one from the HSM? I cannot image that is as it should be. Can you please clarify this! Thank you
The example shell script for the pkcs11-tools does not work btw. This could be fixed!
I had no luck today with the HSM signing. Neither with the described solution using the pkcs11-tool or openssl with the pkcs11 engine. I also wonder if the header in the documentation will be the same in any case or if I have to prepend my own header. This part is not documented. Would be great if some could get back to me regarding this!
Quoted 4 lines
Am 15.11.2023 um 17:56 schrieb Jan Claußen <[email protected]>:
The example shell script for the pkcs11-tools does not work btw. This could be fixed!
I guess the pkcs11-tool is just an example for signing and I would have to come up with a way to calculate the digest and stitch everything back together myself, don’t I?
Quoted 8 lines
Am 15.11.2023 um 19:35 schrieb Jan Claußen <[email protected]>:
I had no luck today with the HSM signing. Neither with the described solution using the pkcs11-tool or openssl with the pkcs11 engine. I also wonder if the header in the documentation will be the same in any case or if I have to prepend my own header. This part is not documented. Would be great if some could get back to me regarding this!
Quoted 4 lines
Am 15.11.2023 um 17:56 schrieb Jan Claußen <[email protected]>:
The example shell script for the pkcs11-tools does not work btw. This could be fixed!
Hello Jan,
I believe the documentation at [1] has some issues and should be revised. Please refer to the output of:
sign_encrypt.py --help sign_encrypt.py digest --help sign_encrypt.py stitch --help
The script can be found in optee_os/scripts.
From the help text and my recollection if all this, here are some commands that should hopefully help (untested):
- To generate the digest (<UUID>.dig) you use "sign_encrypt.py digest":
$ sign_encrypt.py digest --uuid <UUID> --in path/to/<UUID>.stripped.elf --key path/to/<your-public-key>.pem --dig <UUID>.dig
- Then you sign the digest with the HSM using pkcs11-tool:
$ pkcs11-tool --id <your-key-id> -s --login -m RSA-PKCS-PSS --hash-algorithm SHA256 --mgf MGF1-SHA256 --input-file <UUID>.dig | base64
Quoted 1 line
<UUID>.sig
- Finally to create the signed TA you use "sign_encrypt.py stitch"
$ sign_encrypt.py stitch --uuid <UUID> --in path/to/<UUID>.stripped.elf --key <your-public-key>.pem --sig <UUID>.sig --out <UUID>.ta
Make sure OP-TEE is built with "make TA_PBLIC_KEY=<your-public-key>.pem" as documented in mk/config.mk.
[1]
https://optee.readthedocs.io/en/latest/building/trusted_applications.html#offline-signing-of-tas
HTH,
Signature · 1 line
Jerome
Quoted 19 lines · Jan Claußen
I guess the pkcs11-tool is just an example for signing and I would have to come up with a way to calculate the digest and stitch everything back together myself, don’t I?
Quoted 3 lines
Am 15.11.2023 um 19:35 schrieb Jan Claußen <[email protected]>:
I had no luck today with the HSM signing. Neither with the described
solution using the pkcs11-tool or openssl with the pkcs11 engine. I also wonder if the header in the documentation will be the same in any case or if I have to prepend my own header. This part is not documented. Would be great if some could get back to me regarding this!
Quoted 5 lines
Quoted 5 lines
Am 15.11.2023 um 17:56 schrieb Jan Claußen <[email protected]>:
The example shell script for the pkcs11-tools does not work btw. Thiscould be fixed!
Dear all,
The method suggested by Jérôme is OP-TEE native way to stitch a signature computed offline, possibly through a PKCS#11 token.
An alternate way was proposed in discussion thread [1]. We proposed to integrate use of python PKCS#11 support in OP-TEE's sign_encrypt.py script to automate the signing process using a PKCS#11 token. However, at the time there was demand for such support so I dropped it.
Please tell us if you see an interest in rebasing the proposed change [1] in OP-TEE OS mainline repository.
If so, we could take a bit of time to do this but as usual, feel free to create a P-R and propose the required changes if you rather have them to be reviewed (eventually merged) in a short time frame.
[1] https://github.com/OP-TEE/optee_os/issues/5284#issuecomment-1112173121
Regards,
Etienne
________________________________________
From: Jérôme Forissier via OP-TEE <[email protected]>
Sent: Thursday, November 16, 2023 12:21:25 AM
To: Jan Claußen
Cc: op-tee(a)lists.trustedfirmware.org
Subject: Re: How to sign TAs with an HSM?
Hello Jan,
I believe the documentation at [1] has some issues and should be revised. Please refer to the output of:
sign_encrypt.py --help sign_encrypt.py digest --help sign_encrypt.py stitch --help
The script can be found in optee_os/scripts.
From the help text and my recollection if all this, here are some commands that should hopefully help (untested):
- To generate the digest (<UUID>.dig) you use "sign_encrypt.py digest":
$ sign_encrypt.py digest --uuid <UUID> --in path/to/<UUID>.stripped.elf --key path/to/<your-public-key>.pem --dig <UUID>.dig
- Then you sign the digest with the HSM using pkcs11-tool:
$ pkcs11-tool --id <your-key-id> -s --login -m RSA-PKCS-PSS --hash-algorithm SHA256 --mgf MGF1-SHA256 --input-file <UUID>.dig | base64
Quoted 1 line
<UUID>.sig
- Finally to create the signed TA you use "sign_encrypt.py stitch"
$ sign_encrypt.py stitch --uuid <UUID> --in path/to/<UUID>.stripped.elf --key <your-public-key>.pem --sig <UUID>.sig --out <UUID>.ta
Make sure OP-TEE is built with "make TA_PBLIC_KEY=<your-public-key>.pem" as documented in mk/config.mk.
[1]
https://optee.readthedocs.io/en/latest/building/trusted_applications.html#offline-signing-of-tas
HTH,
--
Jerome
Quoted 19 lines · Jan Claußen
I guess the pkcs11-tool is just an example for signing and I would have to come up with a way to calculate the digest and stitch everything back together myself, don’t I?
Quoted 3 lines
Am 15.11.2023 um 19:35 schrieb Jan Claußen <[email protected]>:
I had no luck today with the HSM signing. Neither with the described
solution using the pkcs11-tool or openssl with the pkcs11 engine. I also wonder if the header in the documentation will be the same in any case or if I have to prepend my own header. This part is not documented. Would be great if some could get back to me regarding this!
Quoted 5 lines
Quoted 5 lines
Am 15.11.2023 um 17:56 schrieb Jan Claußen <[email protected]>:
The example shell script for the pkcs11-tools does not work btw. Thiscould be fixed!
Hi Etienne,
Quoted 5 lines · Etienne CARRIERE - foss
Dear all,
The method suggested by Jérôme is OP-TEE native way to stitch a signature computed offline, possibly through a PKCS#11 token.
An alternate way was proposed in discussion thread [1]. We proposed to integrate use of python PKCS#11 support in OP-TEE'ssign_encrypt.pyscript to automate the signing process using a PKCS#11 token.
I think using OpenSSL for signing TAs should be a better alternative. It has a good concept of provider/engine which has support for PKCS#11, TPM etc. And even we can directly input the test key in OP-TEE OS tree.
-Sumit
Quoted 74 lines
However, at the time there was demand for such support so I dropped it.
Please tell us if you see an interest in rebasing the proposed change [1] in OP-TEE OS mainline repository.
If so, we could take a bit of time to do this but as usual, feel free to create a P-R and propose the required changes if you rather have them to be reviewed (eventually merged) in a short time frame.[1] https://github.com/OP-TEE/optee_os/issues/5284#issuecomment-1112173121
Regards,
Etienne________________________________________
From: Jérôme Forissier via OP-TEE <[email protected]>
Sent: Thursday, November 16, 2023 12:21:25 AM
To: Jan Claußen
Cc: op-tee(a)lists.trustedfirmware.org
Subject: Re: How to sign TAs with an HSM?Hello Jan,
I believe the documentation at [1] has some issues and should be revised. Please refer to the output of:
sign_encrypt.py --help sign_encrypt.py digest --help sign_encrypt.py stitch --helpThe script can be found in optee_os/scripts.
From the help text and my recollection if all this, here are some commands that should hopefully help (untested):- To generate the digest (<UUID>.dig) you use "
sign_encrypt.pydigest":$ sign_encrypt.py digest --uuid <UUID> --in path/to/<UUID>.stripped.elf --key path/to/<your-public-key>.pem --dig <UUID>.dig- Then you sign the digest with the HSM using pkcs11-tool:
$ pkcs11-tool --id <your-key-id> -s --login -m RSA-PKCS-PSS --hash-algorithm SHA256 --mgf MGF1-SHA256 --input-file <UUID>.dig | base64Quoted 1 line
<UUID>.sig
- Finally to create the signed TA you use "
sign_encrypt.pystitch"$ sign_encrypt.py stitch --uuid <UUID> --in path/to/<UUID>.stripped.elf --key <your-public-key>.pem --sig <UUID>.sig --out <UUID>.taMake sure OP-TEE is built with "make TA_PBLIC_KEY=<your-public-key>.pem" as documented in
mk/config.mk.[1]
https://optee.readthedocs.io/en/latest/building/trusted_applications.html#offline-signing-of-tasHTH,
--
JeromeQuoted 19 lines · Jan Claußen
I guess the pkcs11-tool is just an example for signing and I would have to come up with a way to calculate the digest and stitch everything back together myself, don’t I?
Quoted 3 lines
Am 15.11.2023 um 19:35 schrieb Jan Claußen <[email protected]>:
I had no luck today with the HSM signing. Neither with the described
solution using the pkcs11-tool or openssl with the pkcs11 engine. I also wonder if the header in the documentation will be the same in any case or if I have to prepend my own header. This part is not documented. Would be great if some could get back to me regarding this!
Quoted 5 lines
Quoted 5 lines
Am 15.11.2023 um 17:56 schrieb Jan Claußen <[email protected]>:
The example shell script for the pkcs11-tools does not work btw. Thiscould be fixed!
@Jérome You solution worked well. Thank you!
@Étienne It works for me now, so actually there is no need for me. I also think that for the sake of transparency, it is letting the signing step be done by official tools like openssl or pkcs11-tool is the better solution! It would just be great if the documentation could be changed accordingly. Make clear that the pubkey must be used for the digest and stitch steps
Fix the pkcs11-tool command example
Maybe mention that the --ta-version flag must be set for some TAs e.g. oemcrypto. This has taken me quite a while to find out. Had to look at the make commands in Yocto.
Cheers,
Jan
Hi Jan,
Quoted 1 line
It works for me now..
This great news, glad to hear it's working for you!
Quoted 1 line
It would just be great if the documentation could be changed accordingly.
Agreed, and this is where it would be great if you could help out. ;). It would be helpful if you could take this on since community docs are found to be most useful when the actual users contribute. If you would be willing, here's a couple suggestions:
- First, post this as a docs issue here:
https://github.com/OP-TEE/optee_docs/issues
- And second, it would be awesome if you could create a Pull Request against that issue for the changes you'd recommend to the docs in that same repo.
Thanks in advance and feel free to reach out to me if you have any questions,
Don Harbin
TrustedFirmware Community Manager
don.harbin(a)linaro.org
Quoted 16 lines · Jan Claußen
@Jérome You solution worked well. Thank you!
@Étienne It works for me now, so actually there is no need for me. I also think that for the sake of transparency, it is letting the signing step be done by official tools like openssl or pkcs11-tool is the better solution! It would just be great if the documentation could be changed accordingly. Make clear that the pubkey must be used for the digest and stitch steps Fix the pkcs11-tool command example
Maybe mention that the --ta-version flag must be set for some TAs e.g. oemcrypto. This has taken me quite a while to find out. Had to look at the make commands in Yocto.
Cheers,
Jan
Done
https://github.com/OP-TEE/optee_docs/pull/223
Regards,
Jan
Quoted 40 lines · Don Harbin
Hi Jan,
Quoted 1 line
It works for me now..
This great news, glad to hear it's working for you!
Quoted 1 line
It would just be great if the documentation could be changed accordingly.
Agreed, and this is where it would be great if you could help out. ;). It would be helpful if you could take this on since community docs are found to be most useful when the actual users contribute. If you would be willing, here's a couple suggestions:
- First, post this as a docs issue here:
https://github.com/OP-TEE/optee_docs/issues
- And second, it would be awesome if you could create a Pull Request against that issue for the changes you'd recommend to the docs in that same repo.Thanks in advance and feel free to reach out to me if you have any questions,
Don Harbin
TrustedFirmware Community Manager
don.harbin(a)linaro.orgQuoted 16 lines · Jan Claußen
@Jérome You solution worked well. Thank you!
@Étienne It works for me now, so actually there is no need for me. I also think that for the sake of transparency, it is letting the signing step be done by official tools like openssl or pkcs11-tool is the better solution! It would just be great if the documentation could be changed accordingly. Make clear that the pubkey must be used for the digest and stitch steps Fix the pkcs11-tool command example
Maybe mention that the --ta-version flag must be set for some TAs e.g. oemcrypto. This has taken me quite a while to find out. Had to look at the make commands in Yocto.
Cheers,
Jan