Skip to content

Commit 06f492e

Browse files
author
Alexander Thurau
committed
Added support for password-protected certificates.
1 parent 280b888 commit 06f492e

File tree

5 files changed

+16
-5
lines changed

5 files changed

+16
-5
lines changed

ConfigCrypter.Console/Options/CommandlineOptions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ public class CommandlineOptions
1010
[Option('n', "name", Required = true, HelpText = "The subject name of the certificate (CN). This can only be used in Windows environments.", Group = "CertLocation")]
1111
public string CertSubjectName { get; set; }
1212

13+
[Option('s', "password", Required = false, HelpText = "Password of the certificate (if available).", Default = null)]
14+
public string CertificatePassword { get; set; }
15+
1316
[Option('k', "key", Required = true, HelpText = "The key to encrypt in the config file.")]
1417
public string Key { get; set; }
1518

ConfigCrypter.Console/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ private static ConfigFileCrypter CreateCrypter(CommandlineOptions options)
3030

3131
if (!string.IsNullOrEmpty(options.CertificatePath))
3232
{
33-
certLoader = new FilesystemCertificateLoader(options.CertificatePath);
33+
certLoader = new FilesystemCertificateLoader(options.CertificatePath, options.CertificatePassword);
3434
}
3535
else if (!string.IsNullOrEmpty(options.CertSubjectName))
3636
{

ConfigCrypter/CertificateLoaders/FilesystemCertificateLoader.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@ namespace DevAttic.ConfigCrypter.CertificateLoaders
88
public class FilesystemCertificateLoader : ICertificateLoader
99
{
1010
private readonly string _certificatePath;
11+
private readonly string _certificatePassword;
1112

1213
/// <summary>
1314
/// Creates an instance of the certificate loader.
1415
/// </summary>
1516
/// <param name="certificatePath">Fully qualified path to the certificate (.pfx file).</param>
16-
public FilesystemCertificateLoader(string certificatePath)
17+
/// <param name="certificatePassword">Password of the certificate, if available.</param>
18+
public FilesystemCertificateLoader(string certificatePath, string certificatePassword = null)
1719
{
1820
_certificatePath = certificatePath;
21+
_certificatePassword = certificatePassword;
1922
}
2023

2124
/// <summary>
@@ -24,7 +27,9 @@ public FilesystemCertificateLoader(string certificatePath)
2427
/// <returns>A X509Certificate2 instance.</returns>
2528
public X509Certificate2 LoadCertificate()
2629
{
27-
return new X509Certificate2(_certificatePath);
30+
return string.IsNullOrEmpty(_certificatePassword) ?
31+
new X509Certificate2(_certificatePath) :
32+
new X509Certificate2(_certificatePath, _certificatePassword);
2833
}
2934
}
3035
}

ConfigCrypter/ConfigProviders/Json/EncryptedJsonConfigSource.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ public class EncryptedJsonConfigSource : JsonConfigurationSource
2424
/// The subject name of the certificate (Issued for).
2525
/// </summary>
2626
public string CertificateSubjectName { get; set; }
27-
27+
/// <summary>
28+
/// The password of the certificate or null, if the certificate has no password.
29+
/// </summary>
30+
public string CertificatePassword { get; set; } = null;
2831
/// <summary>
2932
/// Factory function that is used to create an instance of the crypter.
3033
/// The default factory uses the RSACrypter and passes it the given certificate loader.

ConfigCrypter/Extensions/ConfigurationBuilderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ private static void InitializeCertificateLoader(EncryptedJsonConfigSource config
9797
{
9898
if (!string.IsNullOrEmpty(config.CertificatePath))
9999
{
100-
config.CertificateLoader = new FilesystemCertificateLoader(config.CertificatePath);
100+
config.CertificateLoader = new FilesystemCertificateLoader(config.CertificatePath, config.CertificatePassword);
101101
}
102102
else if (!string.IsNullOrEmpty(config.CertificateSubjectName))
103103
{

0 commit comments

Comments
 (0)