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 FIPS TLS encrypted private key tests #281

Merged
merged 9 commits into from
Feb 21, 2025

Conversation

michel-laterman
Copy link
Contributor

What does this PR do?

Add a test to ensure that when if FIPS mode attempting to decrypt an encrypted private key will result in errors.ErrUnsuported. Change the ReadPEM method to return a joined error so that an encrypted block will return an error instead of just having the "no PEM blocks" error.

Why is it important?

FIPS functionality needs tests.

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • I have added tests that prove my fix is effective or that my feature works

Example test run

tlscommon|fips-test-private-key ⇒ go test -tags=requirefips -v -race ./...
=== RUN   TestFIPSCertificateAndKeys
=== RUN   TestFIPSCertificateAndKeys/embed
=== RUN   TestFIPSCertificateAndKeys/embed_small_key
=== RUN   TestFIPSCertificateAndKeys/embed_encrypted_PKCS#1_key
=== RUN   TestFIPSCertificateAndKeys/embed_PKCS#8_key
=== RUN   TestFIPSCertificateAndKeys/From_disk
--- PASS: TestFIPSCertificateAndKeys (0.01s)
    --- PASS: TestFIPSCertificateAndKeys/embed (0.00s)
    --- PASS: TestFIPSCertificateAndKeys/embed_small_key (0.00s)
    --- PASS: TestFIPSCertificateAndKeys/embed_encrypted_PKCS#1_key (0.00s)
    --- PASS: TestFIPSCertificateAndKeys/embed_PKCS#8_key (0.00s)
    --- PASS: TestFIPSCertificateAndKeys/From_disk (0.00s)
PASS
ok  	github.com/elastic/elastic-agent-libs/transport/tlscommon	1.274s

Related issues

Add a test to ensure that when if FIPS mode attempting to decrypt an
encrypted private key will result in errors.ErrUnsuported. Change the
ReadPEM method to return a joined error so that an encrypted block will
return an error instead of just having the "no PEM blocks" error.
@michel-laterman michel-laterman added enhancement New feature or request Team:Elastic-Agent-Control-Plane Label for the Agent Control Plane team labels Feb 18, 2025
@michel-laterman michel-laterman requested a review from a team as a code owner February 18, 2025 18:41
@michel-laterman michel-laterman requested review from rdner and khushijain21 and removed request for a team February 18, 2025 18:41
@michel-laterman michel-laterman requested review from belimawr, cmacknz and simitt and removed request for khushijain21 February 18, 2025 18:42
@pierrehilbert pierrehilbert added the Team:Elastic-Agent-Data-Plane Label for the Agent Data Plane team label Feb 19, 2025
Comment on lines 18 to 19
//go:build !requirefips

Copy link
Contributor

Choose a reason for hiding this comment

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

The only thing I didn't understand is why most of the tests won't run when FIPS is enabled.
Do they break with FIPS enabled?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Some of them break, but this is mostly so we can limit the requirefips tests to only test the different behaviour

Copy link
Contributor

Choose a reason for hiding this comment

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

@michel-laterman I don't think that we should generally exclude non-system tests from running when in fips mode. What we had discussed was to exclude the expensive system tests that might test certain specifics that aren't focused on FIPS relevant parts; instead we want to have some focused system tests for FIPS covering TLS, crypto, pgp,..

Would be worth looking into why these tests are failing rather than disabling them.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with @simitt, because FIPS changes some cryptographic libraries it is better to have the full suit of tests dealing with crypto running and only add build tags to the ones with different behaviour.

Also, IIRC the elastic-agent-libs CI is rather fast, so I don't see any issues with running the tests twice, one for each build.

Copy link
Contributor

@simitt simitt left a comment

Choose a reason for hiding this comment

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

@michel-laterman and I discussed to replace hardcoded test certs with generated ones as a follow up #282 (out of scope for currently planned work).

simitt
simitt previously requested changes Feb 21, 2025
Copy link
Contributor

@simitt simitt left a comment

Choose a reason for hiding this comment

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

@michel-laterman I created a PR against your branch to get rid of the hardcoded certificates michel-laterman#1. Please review and let me know if you are ok with merging this into your branch before moving on with this PR.

(fips): create test TLS certs and run max number of tests in fips mode
@michel-laterman michel-laterman linked an issue Feb 21, 2025 that may be closed by this pull request
Copy link
Contributor

@simitt simitt left a comment

Choose a reason for hiding this comment

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

Left a few nits

@simitt simitt dismissed their stale review February 21, 2025 16:48

re-reviewed

@elasticmachine
Copy link
Collaborator

💚 Build Succeeded

History

Copy link
Contributor

@belimawr belimawr left a comment

Choose a reason for hiding this comment

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

The code looks good, but there is a TODO that was left behind. It would be better to remove it before merging.

block, err = x509.EncryptPEMBlock(rand.Reader, "RSA PRIVATE KEY", x509.MarshalPKCS1PrivateKey(key), []byte(password), x509.PEMCipherAES256) //nolint:staticcheck // we need to support encrypted private keys
require.NoError(t, err)
case blockTypePKCS8Encrypted:
//TODO: this uses an elastic implementation of pkcs8 as the stdlib does not support password protected pkcs8
Copy link
Contributor

Choose a reason for hiding this comment

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

Why a TODO comment? Shouldn't it be just the comment explaining why we use our custom implementation?

Suggested change
//TODO: this uses an elastic implementation of pkcs8 as the stdlib does not support password protected pkcs8
// This uses an elastic implementation of pkcs8 as the stdlib does not support password protected pkcs8

@michel-laterman michel-laterman merged commit 0ab6544 into elastic:main Feb 21, 2025
5 checks passed
@michel-laterman michel-laterman deleted the fips-test-private-key branch February 21, 2025 17:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Team:Elastic-Agent-Control-Plane Label for the Agent Control Plane team Team:Elastic-Agent-Data-Plane Label for the Agent Data Plane team
Projects
None yet
5 participants