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 support for loading certificate chains from configuration. #24935

Closed
wants to merge 2 commits into from

Conversation

davidfowl
Copy link
Member

@davidfowl davidfowl commented Aug 15, 2020

  • Adds a ServerCertificateIntermediates property to HttpsConnectionAdapterOptions
  • Adds a Chain property to configuration. This only supports PEM certificates.

TODO: Add tests

Fixes #21513
Contributes to #21512

@ghost ghost added the area-servers label Aug 15, 2020
@davidfowl davidfowl added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Aug 15, 2020
Copy link
Member

@bartonjs bartonjs left a comment

Choose a reason for hiding this comment

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

Looks good aside from the one confusing line

/// <summary>
/// Specifies the intermediate certificates in the chain.
/// </summary>
public X509Certificate2Collection ServerCertificateIntermediates { get; set; }
Copy link
Member Author

@davidfowl davidfowl Aug 17, 2020

Choose a reason for hiding this comment

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

The weird thing about this property is that it should really be set with the ServerCertificate in an atomic manner. If the cert is set, then the intermediates should be set as well (null is valid)

Copy link
Member

Choose a reason for hiding this comment

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

Agreed. The combined type should probably be defined in the runtime.

Copy link
Member

Choose a reason for hiding this comment

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

Should we add a SslStreamCertificateContext property instead of this X509Certificate2Collection?

@davidfowl
Copy link
Member Author

@bartonjs if we wanted to support resolving the chain for PKCS #12 files (pfx) would I call Import on the pfx itself?

@bartonjs
Copy link
Member

if we wanted to support resolving the chain for PKCS #12 files (pfx) would I call Import on the pfx itself?

X509Certificate2Collection coll = new X509Certificate2Collection();
coll.Import(pfxPath, pfxPwd, flags);

If there's exactly one cert that returns true for HasPrivateKey, you can hold its reference as the target cert and remove it from the collection (removing it isn't strictly required, I guess...).

Base automatically changed from davidfowl/cert-preload to master August 17, 2020 19:20
}

var cert = new X509Certificate2();
var cert = TestResources.GetTestCertificate();
Copy link
Member

Choose a reason for hiding this comment

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

This gets called a lot. Maybe we should store this certificate in a static/fixture. We do that in some other test classes that need a lot of valid X509Certificate2s.

@@ -346,6 +346,8 @@ internal class SniConfig

// "CertificateName": {
// "Path": "testCert.pfx",
// "KeyPath": "",
// "ChainPath": "",
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Use fake .pem files to show the expected file type for KeyPath/ChainPath.

// This might be do blocking IO but it'll resolve the certificate chain up front before any connections are
// made to the server
sslOptions.ServerCertificate = serverCert;
sslOptions.ServerCertificateContext = SslStreamCertificateContext.Create(serverCert, intermediates);
Copy link
Member

Choose a reason for hiding this comment

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

It might be more straightforward to just have developers call this themselves by putting a SslStreamCertificateContext on HttpsConnectionAdapterOptions and preferring it to ServerCertificate similar to the way we treat ServerCertificateSelector.

Copy link
Member Author

Choose a reason for hiding this comment

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

Wherever we load the cert, we're going to be using the context. The callback happens to late. It needs to happen at startup.

Copy link
Member

Choose a reason for hiding this comment

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

HttpsConnectionAdapterOptions.ServerCertificateContext or whatever-we-call-it would be set at startup though.

Copy link
Member Author

Choose a reason for hiding this comment

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

What does that have to do with the SNI selector? Kestrel should always resolve this at startup.

Copy link
Member

Choose a reason for hiding this comment

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

It's just a comparison to point out that even though having both SslStreamCertificateContext and ServerCertificate on HttpsConnectionAdapterOptions would be redundant, ServerCertificateSelector and ServerCertificate are already redundant, so this isn't a new problem if we decide to add SslStreamCertificateContext.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't understand what this has to do with SNI. Exposing SslStreamCertificateContext on HttpsConnectionAdapterOptions is fine and I have no pushback but it has nothing to do with this code path...

@davidfowl davidfowl added api-ready-for-review API is ready for formal API review - https://github.com/dotnet/apireviews and removed api-suggestion Early API idea and discussion, it is NOT ready for implementation labels Aug 24, 2020
@ghost
Copy link

ghost commented Aug 24, 2020

Thank you for submitting this for API review. This will be reviewed by @dotnet/aspnet-api-review at the next meeting of the ASP.NET Core API Review group. Please ensure you take a look at the API review process documentation and ensure that:

  • The PR contains changes to the reference-assembly that describe the API change. Or, you have included a snippet of reference-assembly-style code that illustrates the API change.
  • The PR describes the impact to users, both positive (useful new APIs) and negative (breaking changes).
  • Someone is assigned to "champion" this change in the meeting, and they understand the impact and design of the change.

@davidfowl davidfowl changed the base branch from master to release/5.0 August 24, 2020 16:31
@davidfowl davidfowl added api-suggestion Early API idea and discussion, it is NOT ready for implementation and removed api-ready-for-review API is ready for formal API review - https://github.com/dotnet/apireviews labels Aug 24, 2020
Copy link
Member

@javiercn javiercn left a comment

Choose a reason for hiding this comment

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

This looks good!

There's missing tests and stuff, but other than that the changes are fine I think!

@Pilchie
Copy link
Member

Pilchie commented Aug 31, 2020

👀

@davidfowl davidfowl changed the base branch from release/5.0 to release/5.0-rc2 September 12, 2020 06:20
@dougbu
Copy link
Member

dougbu commented Nov 9, 2020

@davidfowl this PR is blocking me deleting the old release/5.0-rc2 branch. Could you please close it❔

@davidfowl
Copy link
Member Author

As long as you don't delete it

@dougbu
Copy link
Member

dougbu commented Nov 9, 2020

As long as you don't delete it

If you need the branch, probably safest to rebase it before I burn the release/5.0-rc2 branch. I don't think GitHub removes branches when left dangling after a PR is closed but the PR does protect the branch a bit.

@dougbu
Copy link
Member

dougbu commented Nov 13, 2020

🆗 @davidfowl I'll do the rebase to unblock myself. Should this PR target master now❔

@davidfowl
Copy link
Member Author

Yep

- Adds a ServerCertificateIntermediates property to HttpsConnectionAdapterOptions
- Adds a Chain property to configuration. This only supports PEM certificates.
- Import intermediates if chain path specified
@dougbu dougbu force-pushed the davidfowl/cert-chain-support branch from 567dee9 to 124b365 Compare November 13, 2020 20:50
@dougbu dougbu changed the base branch from release/5.0-rc2 to master November 13, 2020 20:50
@dougbu
Copy link
Member

dougbu commented Nov 13, 2020

Done

Base automatically changed from master to main January 22, 2021 01:32
@davidfowl davidfowl closed this Mar 12, 2021
@dougbu dougbu deleted the davidfowl/cert-chain-support branch August 21, 2021 22:36
@amcasey amcasey added area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions and removed area-runtime labels Jun 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions
Projects
None yet
7 participants