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

[8.0] Add support for trusting dev certs on linux #57108

Merged
merged 5 commits into from
Aug 8, 2024

Conversation

amcasey
Copy link
Member

@amcasey amcasey commented Jul 31, 2024

Add support for trusting dev certs on linux

Make dotnet dev-certs https trust work on (select distros of) Linux.

Description

Backport of #56701, #57014, #56582, and #56985 to make the functionality available in Aspire, which targets 8.0.

There's no consistent way to do this that works for all clients on all Linux distros, but this approach gives us pretty good coverage. In particular, we aim to support .NET (esp HttpClient), Chromium, and Firefox on Ubuntu- and Fedora-based distros.

Certificate trust is applied per-user, which is simpler and preferable for security reasons, but comes with the notable downside that the process can't be completed within the tool - the user has to update an environment variable, probably in their user profile. In particular, OpenSSL consumes the SSL_CERT_DIR environment variable to determine where it should look for trusted certificates.

We break establishing trust into two categories: OpenSSL, which backs .NET, and NSS databases (henceforth, nssdb), which backs browsers.

To establish trust in OpenSSL, we put the certificate in ~/.dotnet/corefx/cryptography/trusted, run a simplified version of OpenSSL's c_rehash tool on the directory, and ask the user to update SSL_CERT_DIR.

To establish trust in dotnet, we put the certificate in the My/Root certificate store.

To establish trust in nssdb, we search the home directory for Firefox profiles and ~/.pki/nssdb. For each one found, we add an entry to the nssdb therein.

Each of these locations (the trusted certificate folder and the list of nssdbs) can be overridden with an environment variable.

This large number of steps introduces a problem that doesn't exist on Windows or macOS - the dev cert can end up trusted by some clients but not by others. This change introduces a TrustLevel concept so that we can produce clearer output when this happens.

The only non-bundled tools required to update certificate trust are openssl (the CLI) and certutil. sudo is not required, since all changes are within the user's home directory.

Customer Impact

Without a trusted dev cert, you can't use TLS in local development on Linux. This has proven to be particularly noticeable when building Aspire apps.

Available workarounds include using a community tool or installing aspnetcore 9.0 and using the tool from there.

Regression?

  • Yes
  • No

This is new functionality.

Risk

  • High
  • Medium
  • Low

The scenario has never worked, so there's little room for regression.

Verification

  • Manual (required)
  • Automated

Tested HttpClient, curl, Chrome, and Firefox on Ubuntu 22.04.

Packaging changes reviewed?

  • Yes
  • No
  • N/A

amcasey added 5 commits July 30, 2024 17:46
* Search for trusted certificates consistently on Windows

1. Don't use thumbprint so we don't get flagged for using SHA-1
2. Make TrustCertificateCore and RemoveCertificateFromTrustedRoots consistent

* Add a note about our usage of Thumbprint on macOS

* Clean up assumptions about root store

* FindBySubjectName expects a string

* Search by serial number to avoid having to parse subject name

* Fix typo

Co-authored-by: Martin Costello <[email protected]>

* Call DisposeCertificates more consistently

---------

Co-authored-by: Martin Costello <[email protected]>
(cherry picked from commit ed7ea40)
* Make dev-certs import consistent with kestrel

Kestrel checks the subject name and our magic extension - import was only checking the extension.  They can't easily share a method because import has a test hook.

(cherry picked from commit 06155c0)
* Add support for trusting dev certs on linux

There's no consistent way to do this that works for all clients on all Linux distros, but this approach gives us pretty good coverage.  In particular, we aim to support .NET (esp HttpClient), Chromium, and Firefox on Ubuntu- and Fedora-based distros.

Certificate trust is applied per-user, which is simpler and preferable for security reasons, but comes with the notable downside that the process can't be completed within the tool - the user has to update an environment variable, probably in their user profile.  In particular, OpenSSL consumes the `SSL_CERT_DIR` environment variable to determine where it should look for trusted certificates.

We break establishing trust into two categories: OpenSSL, which backs .NET, and NSS databases (henceforth, nssdb), which backs browsers.

To establish trust in OpenSSL, we put the certificate in `~/.dotnet/corefx/cryptography/trusted`, run a simplified version of OpenSSL's `c_rehash` tool on the directory, and ask the user to update `SSL_CERT_DIR`.

To establish trust in nssdb, we search the home directory for Firefox profiles and `~/.pki/nssdb`.  For each one found, we add an entry to the nssdb therein.

Each of these locations (the trusted certificate folder and the list of nssdbs) can be overridden with an environment variable.

This large number of steps introduces a problem that doesn't exist on Windows or macOS - the dev cert can end up trusted by some clients but not by others.  This change introduces a `TrustLevel` concept so that we can produce clearer output when this happens.

The only non-bundled tools required to update certificate trust are `openssl` (the CLI) and `certutil`.  `sudo` is not required, since all changes are within the user's home directory.

* Also trust certificates in the Current User/Root store

A belt-and-suspenders approach for dotnet trust (i.e. in addition to OpenSSL trust) that has the notable advantage of not requiring any environment variables.

* Clarify the mac-specific comments in GetDevelopmentCertificateFromStore

(cherry picked from commit 27ae082)
* Create directories with secure permissions

If we're creating it, make it 700.  If it already exists, warn if it's not 700.

* Don't create a directory specified by the user

(cherry picked from commit 1470e00)
@dotnet-issue-labeler dotnet-issue-labeler bot added the area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions label Jul 31, 2024
@dotnet-policy-service dotnet-policy-service bot added this to the 8.0.x milestone Jul 31, 2024
@amcasey
Copy link
Member Author

amcasey commented Jul 31, 2024

#56701 and #57014 aren't required, but I think keeping the implementations consistent between 8.0 and 9.0 will make understanding and servicing them easier.

@amcasey amcasey changed the title Add support for trusting dev certs on linux [8.0] Add support for trusting dev certs on linux Jul 31, 2024
@amcasey amcasey added the Servicing-consider Shiproom approval is required for the issue label Jul 31, 2024
@adityamandaleeka adityamandaleeka added Servicing-approved Shiproom has approved the issue and removed Servicing-consider Shiproom approval is required for the issue labels Aug 1, 2024
@wtgodbe wtgodbe merged commit 32e5f4f into dotnet:release/8.0 Aug 8, 2024
25 checks passed
@dotnet-policy-service dotnet-policy-service bot modified the milestones: 8.0.x, 8.0.9 Aug 8, 2024
@rbhanda rbhanda modified the milestones: 8.0.9, 8.0.10 Oct 7, 2024
amcasey added a commit to amcasey/aspnetcore that referenced this pull request Oct 16, 2024
During a recent security review of the dev-certs tool, we observed that on export it would create a directory that was potentially world-readable (e.g. based on permissions inherited from the parent directory).  We decided it would be more appropriate to let users make the decision of who should have access to the directory.  Unfortunately, this removal of functionality broke some app authors' workflows.  When dev-certs is run directly, the `--verbose` output makes it clear what went wrong and what needs to happen, but the non-verbose output that appears when another tool does the export is less helpful.  This change introduces a new top-level error state for an export failure caused by a non-existent target directory to make it clearer how to fix broken workflows.

The behavior changed in dotnet#57108, which included a backport of dotnet#56985, and shipped in 8.0.10.

For dotnet#58330
github-actions bot pushed a commit that referenced this pull request Oct 16, 2024
During a recent security review of the dev-certs tool, we observed that on export it would create a directory that was potentially world-readable (e.g. based on permissions inherited from the parent directory).  We decided it would be more appropriate to let users make the decision of who should have access to the directory.  Unfortunately, this removal of functionality broke some app authors' workflows.  When dev-certs is run directly, the `--verbose` output makes it clear what went wrong and what needs to happen, but the non-verbose output that appears when another tool does the export is less helpful.  This change introduces a new top-level error state for an export failure caused by a non-existent target directory to make it clearer how to fix broken workflows.

The behavior changed in #57108, which included a backport of #56985, and shipped in 8.0.10.

For #58330
github-actions bot pushed a commit that referenced this pull request Oct 16, 2024
During a recent security review of the dev-certs tool, we observed that on export it would create a directory that was potentially world-readable (e.g. based on permissions inherited from the parent directory).  We decided it would be more appropriate to let users make the decision of who should have access to the directory.  Unfortunately, this removal of functionality broke some app authors' workflows.  When dev-certs is run directly, the `--verbose` output makes it clear what went wrong and what needs to happen, but the non-verbose output that appears when another tool does the export is less helpful.  This change introduces a new top-level error state for an export failure caused by a non-existent target directory to make it clearer how to fix broken workflows.

The behavior changed in #57108, which included a backport of #56985, and shipped in 8.0.10.

For #58330
wtgodbe pushed a commit that referenced this pull request Oct 16, 2024
* Improve dev-certs export error message

During a recent security review of the dev-certs tool, we observed that on export it would create a directory that was potentially world-readable (e.g. based on permissions inherited from the parent directory).  We decided it would be more appropriate to let users make the decision of who should have access to the directory.  Unfortunately, this removal of functionality broke some app authors' workflows.  When dev-certs is run directly, the `--verbose` output makes it clear what went wrong and what needs to happen, but the non-verbose output that appears when another tool does the export is less helpful.  This change introduces a new top-level error state for an export failure caused by a non-existent target directory to make it clearer how to fix broken workflows.

The behavior changed in #57108, which included a backport of #56985, and shipped in 8.0.10.

For #58330

* Improve error test
wtgodbe pushed a commit that referenced this pull request Oct 16, 2024
* Improve dev-certs export error message

During a recent security review of the dev-certs tool, we observed that on export it would create a directory that was potentially world-readable (e.g. based on permissions inherited from the parent directory).  We decided it would be more appropriate to let users make the decision of who should have access to the directory.  Unfortunately, this removal of functionality broke some app authors' workflows.  When dev-certs is run directly, the `--verbose` output makes it clear what went wrong and what needs to happen, but the non-verbose output that appears when another tool does the export is less helpful.  This change introduces a new top-level error state for an export failure caused by a non-existent target directory to make it clearer how to fix broken workflows.

The behavior changed in #57108, which included a backport of #56985, and shipped in 8.0.10.

For #58330

* Improve error text

---------

Co-authored-by: Andrew Casey <[email protected]>
amcasey added a commit that referenced this pull request Oct 23, 2024
* Improve dev-certs export error message

During a recent security review of the dev-certs tool, we observed that on export it would create a directory that was potentially world-readable (e.g. based on permissions inherited from the parent directory).  We decided it would be more appropriate to let users make the decision of who should have access to the directory.  Unfortunately, this removal of functionality broke some app authors' workflows.  When dev-certs is run directly, the `--verbose` output makes it clear what went wrong and what needs to happen, but the non-verbose output that appears when another tool does the export is less helpful.  This change introduces a new top-level error state for an export failure caused by a non-existent target directory to make it clearer how to fix broken workflows.

The behavior changed in #57108, which included a backport of #56985, and shipped in 8.0.10.

For #58330

* Improve error text

---------

Co-authored-by: Andrew Casey <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions Servicing-approved Shiproom has approved the issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants