Skip to content

feat: tedge cert renew c8y using HSM#3677

Merged
Bravo555 merged 2 commits intothin-edge:mainfrom
Bravo555:feat/cert-renew-pkcs11
Jul 2, 2025
Merged

feat: tedge cert renew c8y using HSM#3677
Bravo555 merged 2 commits intothin-edge:mainfrom
Bravo555:feat/cert-renew-pkcs11

Conversation

@Bravo555
Copy link
Copy Markdown
Member

@Bravo555 Bravo555 commented Jun 11, 2025

TODO

  • cleanup
  • consider using a pubkey derived from privkey (more complexity than reusing the public key in the certificate, different per cryptosystem, need to add some dependencies - reusing cert pubkey is simpler)

Proposed changes

This PR introduces support for tedge cert renew c8y when the private key is located on the HSM.

To generate a CSR, one needs to put the public key in the CSR (it will become a part of the certificate (SPKI)) and to sign the certificate using the private key to prove the ownership of it. Because the public key can be derived from the private key, and because we need to sign anyway, until this PR we just read the private key to do both these things.

Now, with an HSM, we can't read the private key, so instead for CSR creation with HSM being enabled, we read the public key from the currently used certificate and sign using the TedgeP11Client.

It's also perhaps possible to derive the public key from the public attributes of the private key in the HSM (that's what gnutls seems to be doing), so that would be an alternative approach to reusing the pubkey from the certificate that needs to be considered as well.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Improvement (general improvements like code refactoring that doesn't explicitly fix a bug or add any new functionality)
  • Documentation Update (if none of the other choices apply)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Paste Link to the issue

#3664

Checklist

  • I have read the CONTRIBUTING doc
  • I have signed the CLA (in all commits with git commit -s. You can activate automatic signing by running just prepare-dev once)
  • I ran just format as mentioned in CODING_GUIDELINES
  • I used just check as mentioned in CODING_GUIDELINES
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Further comments

@codecov
Copy link
Copy Markdown

codecov bot commented Jun 11, 2025

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Jun 11, 2025

Robot Results

✅ Passed ❌ Failed ⏭️ Skipped Total Pass % ⏱️ Duration
653 0 3 653 100 1h49m40.426192s

@reubenmiller reubenmiller added the theme:hsm Hardware Security Module related topics label Jun 12, 2025
@Bravo555 Bravo555 force-pushed the feat/cert-renew-pkcs11 branch from 5599cbc to 6636118 Compare June 13, 2025 08:18
@Bravo555 Bravo555 had a problem deploying to Test Pull Request June 13, 2025 08:18 — with GitHub Actions Failure
@Bravo555 Bravo555 force-pushed the feat/cert-renew-pkcs11 branch from 6636118 to 1bcdda9 Compare June 13, 2025 08:29
@Bravo555 Bravo555 temporarily deployed to Test Pull Request June 13, 2025 08:29 — with GitHub Actions Inactive
@Bravo555 Bravo555 had a problem deploying to Test Pull Request June 13, 2025 17:19 — with GitHub Actions Failure
@Bravo555 Bravo555 force-pushed the feat/cert-renew-pkcs11 branch from c1055c2 to 1bcdda9 Compare June 16, 2025 08:16
@Bravo555 Bravo555 temporarily deployed to Test Pull Request June 16, 2025 08:16 — with GitHub Actions Inactive
@Bravo555 Bravo555 force-pushed the feat/cert-renew-pkcs11 branch from 1bcdda9 to 9962540 Compare June 16, 2025 08:44
@Bravo555 Bravo555 temporarily deployed to Test Pull Request June 16, 2025 08:44 — with GitHub Actions Inactive
@Bravo555 Bravo555 marked this pull request as ready for review June 16, 2025 08:57
@reubenmiller
Copy link
Copy Markdown
Contributor

@Bravo555 The feature works nicely. The only open question would be if the errors messages could be a bit more informative when the tedge-p11-server.socket is not reachable. I checked to see what would happen if I just disabled both the tedge-p11-server and socket and only got the following output:

# tedge cert renew c8y
Error: failed to renew the device certificate via Cumulocity HTTP proxy http://127.0.0.1:8001

Caused by:
    0: Cryptography related error
    1: Remote key error

The above error doesn't tell anything about the tedge-p11-server not being reachable, which socket is being used, etc, so it would definitely help people debug a bit of the error message included more context.

@Bravo555 Bravo555 force-pushed the feat/cert-renew-pkcs11 branch from 9962540 to abfecbd Compare June 17, 2025 18:01
@Bravo555 Bravo555 had a problem deploying to Test Pull Request June 17, 2025 18:01 — with GitHub Actions Failure
@Bravo555 Bravo555 force-pushed the feat/cert-renew-pkcs11 branch from abfecbd to 8b43a28 Compare June 17, 2025 18:04
@Bravo555 Bravo555 temporarily deployed to Test Pull Request June 17, 2025 18:04 — with GitHub Actions Inactive
@Bravo555 Bravo555 force-pushed the feat/cert-renew-pkcs11 branch from 55ca816 to 18c056b Compare June 24, 2025 15:31
@Bravo555 Bravo555 temporarily deployed to Test Pull Request June 24, 2025 15:31 — with GitHub Actions Inactive
@Bravo555 Bravo555 force-pushed the feat/cert-renew-pkcs11 branch from 18c056b to 3feecbb Compare June 24, 2025 15:50
@Bravo555 Bravo555 had a problem deploying to Test Pull Request June 24, 2025 15:50 — with GitHub Actions Failure
@Bravo555 Bravo555 force-pushed the feat/cert-renew-pkcs11 branch from 3feecbb to c81a5bf Compare June 24, 2025 16:13
@Bravo555 Bravo555 temporarily deployed to Test Pull Request June 24, 2025 16:13 — with GitHub Actions Inactive
@Bravo555 Bravo555 marked this pull request as ready for review June 25, 2025 08:28
@Bravo555 Bravo555 requested a review from reubenmiller June 25, 2025 08:59
Instead of returning a rustls::SigningKey, which can't immediately be
used for signing, make tedge_p11_server::signing_key return an object on
which one can call .sign() immediately.

rustls::SigningKey first requires calling `choose_scheme` to obtain a
signer that could be used for signing. So far
`tedge_p11_server::signing_key` was called by only one caller which
wanted a rustls::SigningKey, but now for CSR we want to call `.sign()`.

There were also some changes in the pkcs11 module to remove some
unnecessary structs that in practice were really duplicates - instead of
having a separate `Pkcs11SigningKey` and `Pkcs11Signer` structs, just
have `Pkcs11Signer` impl both `SigningKey` and `Signer`.

Signed-off-by: Marcel Guzik <marcel.guzik@cumulocity.com>
@Bravo555 Bravo555 force-pushed the feat/cert-renew-pkcs11 branch from c81a5bf to 09fb381 Compare June 30, 2025 11:33
@Bravo555 Bravo555 temporarily deployed to Test Pull Request June 30, 2025 11:33 — with GitHub Actions Inactive
@Bravo555 Bravo555 temporarily deployed to Test Pull Request July 1, 2025 14:06 — with GitHub Actions Inactive
@Bravo555
Copy link
Copy Markdown
Member Author

Bravo555 commented Jul 1, 2025

@reubenmiller @didier-wenzek Change made in #3706 to start with a cert signed by C8y CA made the test stop testing if renewing a self-signed certificate actually works, so I changed it back to upload a self-signed cert when testing renewal.

Copy link
Copy Markdown
Contributor

@didier-wenzek didier-wenzek left a comment

Choose a reason for hiding this comment

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

Approved

@@ -7,6 +7,7 @@ Documentation Test thin-edge.io MQTT client authentication using a Hardwar

# it would be good to explain here why we use the tedge-p11-server exclusively and not the module mode
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It might be good to resolve this todo.

let current_cert = self
.current_cert
.clone()
.expect("Need an existing cert when using an HSM");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A bit surprising, but okay as a temporary solution.

This commit introduces support for tedge cert renew c8y when the private
key is located on the HSM.

To generate a CSR, one needs to put the public key in the CSR (it will
become a part of the certificate (SPKI)) and to sign the certificate
using the private key to prove the ownership of it. Because the public
key can be derived from the private key, and because we need to sign
anyway, until this PR we just read the private key to do both these
things.

Now, with an HSM, we can't read the private key, so instead for CSR
creation with HSM being enabled, we read the public key from the
currently used certificate and sign using the TedgeP11Client.

Signed-off-by: Marcel Guzik <marcel.guzik@cumulocity.com>
@Bravo555 Bravo555 force-pushed the feat/cert-renew-pkcs11 branch from 0b97869 to b5222c8 Compare July 2, 2025 07:13
@Bravo555 Bravo555 temporarily deployed to Test Pull Request July 2, 2025 07:13 — with GitHub Actions Inactive
@Bravo555 Bravo555 added this pull request to the merge queue Jul 2, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jul 2, 2025
@Bravo555 Bravo555 added this pull request to the merge queue Jul 2, 2025
Merged via the queue into thin-edge:main with commit 2b0e41f Jul 2, 2025
51 of 52 checks passed
@Bravo555 Bravo555 deleted the feat/cert-renew-pkcs11 branch July 2, 2025 10:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

theme:hsm Hardware Security Module related topics

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Extend tedge-p11-server to support creating a Certificate Signing Request

3 participants