summaryrefslogtreecommitdiffstats
path: root/src/main.lib/Plugins/StorePlugins/PfxFile/PfxFile.cs
diff options
context:
space:
mode:
authorWouter Tinus <wouter.tinus@gmail.com>2020-05-31 14:27:44 +0200
committerWouter Tinus <wouter.tinus@gmail.com>2020-05-31 14:27:44 +0200
commitacfb3caf686e9d8ebfffc26bc63c1c3a01d59bcb (patch)
treeb890f3b3b4d0a0e16103d6a7c190c223660cf1a6 /src/main.lib/Plugins/StorePlugins/PfxFile/PfxFile.cs
parent6252f0c1519e96161ec369efaa95b6a12eba06c9 (diff)
downloadletsencrypt-win-simple-acfb3caf686e9d8ebfffc26bc63c1c3a01d59bcb.zip
letsencrypt-win-simple-acfb3caf686e9d8ebfffc26bc63c1c3a01d59bcb.tar.gz
letsencrypt-win-simple-acfb3caf686e9d8ebfffc26bc63c1c3a01d59bcb.tar.bz2
Add pfx storev2.1.8
Diffstat (limited to 'src/main.lib/Plugins/StorePlugins/PfxFile/PfxFile.cs')
-rw-r--r--src/main.lib/Plugins/StorePlugins/PfxFile/PfxFile.cs73
1 files changed, 73 insertions, 0 deletions
diff --git a/src/main.lib/Plugins/StorePlugins/PfxFile/PfxFile.cs b/src/main.lib/Plugins/StorePlugins/PfxFile/PfxFile.cs
new file mode 100644
index 0000000..8636eca
--- /dev/null
+++ b/src/main.lib/Plugins/StorePlugins/PfxFile/PfxFile.cs
@@ -0,0 +1,73 @@
+using PKISharp.WACS.DomainObjects;
+using PKISharp.WACS.Extensions;
+using PKISharp.WACS.Plugins.Interfaces;
+using PKISharp.WACS.Services;
+using System;
+using System.IO;
+using System.Security.Cryptography.X509Certificates;
+using System.Threading.Tasks;
+
+namespace PKISharp.WACS.Plugins.StorePlugins
+{
+ internal class PfxFile : IStorePlugin
+ {
+ private readonly ILogService _log;
+ private readonly string _path;
+ private readonly string? _password;
+
+ public PfxFile(ILogService log, ISettingsService settings, PfxFileOptions options)
+ {
+ _log = log;
+
+ _password = !string.IsNullOrWhiteSpace(options.PfxPassword?.Value) ?
+ options.PfxPassword.Value :
+ settings.Store.PfxFile?.DefaultPassword;
+
+ var path = !string.IsNullOrWhiteSpace(options.Path) ?
+ options.Path :
+ settings.Store.PfxFile?.DefaultPath;
+
+ if (path != null && path.ValidPath(log))
+ {
+ _path = path;
+ _log.Debug("Using pfx file path: {_path}", _path);
+ }
+ else
+ {
+ throw new Exception($"Specified pfx file path {path} is not valid.");
+ }
+ }
+
+ private string PathForIdentifier(string identifier) => Path.Combine(_path, $"{identifier.Replace("*", "_")}.pfx");
+
+ public Task Save(CertificateInfo input)
+ {
+ _log.Information("Copying certificate to the pfx folder");
+ var dest = PathForIdentifier(input.CommonName);
+ try
+ {
+ var collection = new X509Certificate2Collection
+ {
+ input.Certificate
+ };
+ collection.AddRange(input.Chain.ToArray());
+ File.WriteAllBytes(dest, collection.Export(X509ContentType.Pfx, _password));
+ }
+ catch (Exception ex)
+ {
+ _log.Error(ex, "Error copying certificate to pfx path");
+ }
+ input.StoreInfo.Add(GetType(),
+ new StoreInfo()
+ {
+ Name = PfxFileOptions.PluginName,
+ Path = _path
+ });
+ return Task.CompletedTask;
+ }
+
+ public Task Delete(CertificateInfo input) => Task.CompletedTask;
+
+ (bool, string?) IPlugin.Disabled => (false, null);
+ }
+}