blob: e86834b2f899b049b2944132b95feaaed1f5d435 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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 async 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());
await File.WriteAllBytesAsync(dest, collection.Export(X509ContentType.Pfx, _password));
}
catch (Exception ex)
{
_log.Error(ex, "Error copying certificate to pfx path");
}
input.StoreInfo.TryAdd(
GetType(),
new StoreInfo()
{
Name = PfxFileOptions.PluginName,
Path = _path
});
}
public Task Delete(CertificateInfo input) => Task.CompletedTask;
(bool, string?) IPlugin.Disabled => (false, null);
}
}
|