This repository was archived by the owner on Nov 21, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
Support more certificate loading scenarios (#69) #85
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Security.Cryptography.X509Certificates; | ||
|
||
namespace Microsoft.AspNetCore | ||
{ | ||
internal class CertificateFileLoader : ICertificateFileLoader | ||
{ | ||
public X509Certificate2 Load(string path, string password, X509KeyStorageFlags flags) | ||
{ | ||
return new X509Certificate2(path, password, flags); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Linq; | ||
using System.Security.Cryptography.X509Certificates; | ||
|
||
namespace Microsoft.AspNetCore | ||
{ | ||
internal class CertificateStoreLoader : ICertificateStoreLoader | ||
{ | ||
public X509Certificate2 Load(string subject, string storeName, StoreLocation storeLocation, bool validOnly) | ||
{ | ||
using (var store = new X509Store(storeName, storeLocation)) | ||
{ | ||
X509Certificate2Collection storeCertificates = null; | ||
X509Certificate2Collection foundCertificates = null; | ||
X509Certificate2 foundCertificate = null; | ||
|
||
try | ||
{ | ||
store.Open(OpenFlags.ReadOnly); | ||
storeCertificates = store.Certificates; | ||
foundCertificates = storeCertificates.Find(X509FindType.FindBySubjectDistinguishedName, subject, validOnly); | ||
foundCertificate = foundCertificates | ||
.OfType<X509Certificate2>() | ||
.OrderByDescending(certificate => certificate.NotAfter) | ||
.FirstOrDefault(); | ||
|
||
return foundCertificate; | ||
} | ||
finally | ||
{ | ||
if (foundCertificate != null) | ||
{ | ||
storeCertificates.Remove(foundCertificate); | ||
foundCertificates.Remove(foundCertificate); | ||
} | ||
|
||
DisposeCertificates(storeCertificates); | ||
DisposeCertificates(foundCertificates); | ||
} | ||
} | ||
} | ||
|
||
private void DisposeCertificates(X509Certificate2Collection certificates) | ||
{ | ||
if (certificates != null) | ||
{ | ||
foreach (var certificate in certificates) | ||
{ | ||
certificate.Dispose(); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Security.Cryptography.X509Certificates; | ||
|
||
namespace Microsoft.AspNetCore | ||
{ | ||
internal interface ICertificateFileLoader | ||
{ | ||
X509Certificate2 Load(string path, string password, X509KeyStorageFlags flags); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Security.Cryptography.X509Certificates; | ||
|
||
namespace Microsoft.AspNetCore | ||
{ | ||
internal interface ICertificateStoreLoader | ||
{ | ||
X509Certificate2 Load(string subject, string storeName, StoreLocation storeLocation, bool validOnly); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@danroth27 Do you want to keep the keys when loading from e.g.
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By "keys" do you mean the certificate name used in config? Yeah, I think it would be good to provide those.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But not required.