|
7 | 7 |
|
8 | 8 | using System; |
9 | 9 | using System.Collections.Generic; |
10 | | -using System.Security.Cryptography.X509Certificates; |
11 | 10 |
|
12 | 11 | using Keyfactor.Logging; |
13 | 12 | using Keyfactor.Orchestrators.Extensions; |
14 | 13 | using Keyfactor.Orchestrators.Common.Enums; |
15 | | -using Keyfactor.PKI.PEM; |
16 | 14 |
|
17 | 15 | using Microsoft.Extensions.Logging; |
18 | 16 |
|
19 | | -using Newtonsoft.Json; |
20 | | -using System.Security.Cryptography; |
21 | | - |
22 | 17 | namespace Keyfactor.Extensions.Orchestrator.RemoteFile |
23 | 18 | { |
24 | 19 | public abstract class ReenrollmentBase : RemoteFileJobTypeBase |
@@ -46,74 +41,74 @@ public JobResult ProcessJobToDo(ReenrollmentJobConfiguration config, SubmitReenr |
46 | 41 | { |
47 | 42 | ILogger logger = LogHandler.GetClassLogger(this.GetType()); |
48 | 43 |
|
49 | | - ICertificateStoreSerializer certificateStoreSerializer = GetCertificateStoreSerializer(config.CertificateStoreDetails.Properties); |
50 | | - |
51 | | - try |
52 | | - { |
53 | | - SetJobProperties(config, config.CertificateStoreDetails, logger); |
54 | | - |
55 | | - string alias = "abcd"; |
56 | | - string sans = "reenroll2.Keyfactor.com&reenroll1.keyfactor.com&reenroll3.Keyfactor.com"; |
57 | | - bool overwrite = true; |
58 | | - |
59 | | - // validate parameters |
60 | | - string KeyTypes = string.Join(",", Enum.GetNames(typeof(SupportedKeyTypeEnum))); |
61 | | - if (!Enum.TryParse(KeyType.ToUpper(), out SupportedKeyTypeEnum KeyTypeEnum)) |
62 | | - { |
63 | | - throw new RemoteFileException($"Unsupported KeyType value {KeyType}. Supported types are {KeyTypes}."); |
64 | | - } |
65 | | - |
66 | | - ApplicationSettings.FileTransferProtocolEnum fileTransferProtocol = ApplicationSettings.FileTransferProtocol; |
67 | | - |
68 | | - certificateStore = new RemoteCertificateStore(config.CertificateStoreDetails.ClientMachine, UserName, UserPassword, config.CertificateStoreDetails.StorePath, StorePassword, fileTransferProtocol, SSHPort, IncludePortInSPN); |
69 | | - certificateStore.Initialize(SudoImpersonatedUser, UseShellCommands); |
70 | | - |
71 | | - PathFile storePathFile = RemoteCertificateStore.SplitStorePathFile(config.CertificateStoreDetails.StorePath); |
72 | | - |
73 | | - if (!certificateStore.DoesStoreExist()) |
74 | | - { |
75 | | - throw new RemoteFileException($"Certificate store {config.CertificateStoreDetails.StorePath} does not exist on server {config.CertificateStoreDetails.ClientMachine}."); |
76 | | - } |
77 | | - |
78 | | - // generate CSR and call back to enroll certificate |
79 | | - string csr = string.Empty; |
80 | | - string pemPrivateKey = string.Empty; |
81 | | - if (CreateCSROnDevice) |
82 | | - { |
83 | | - csr = certificateStore.GenerateCSROnDevice(SubjectText, KeyTypeEnum, KeySize, new List<string>(sans.Split('&', StringSplitOptions.RemoveEmptyEntries)), out pemPrivateKey); |
84 | | - } |
85 | | - else |
86 | | - { |
87 | | - csr = certificateStore.GenerateCSR(SubjectText, KeyTypeEnum, KeySize, new List<string>(sans.Split('&', StringSplitOptions.RemoveEmptyEntries))); |
88 | | - } |
89 | | - |
90 | | - X509Certificate2 cert = submitReenrollment.Invoke(csr); |
91 | | - if (cert == null || String.IsNullOrEmpty(pemPrivateKey)) |
92 | | - throw new RemoteFileException("Enrollment of CSR failed. Please check Keyfactor Command logs for more information on potential enrollment errors."); |
93 | | - |
94 | | - AsymmetricAlgorithm alg = KeyTypeEnum == SupportedKeyTypeEnum.RSA ? RSA.Create() : ECDsa.Create(); |
95 | | - alg.ImportEncryptedPkcs8PrivateKey(string.Empty, Keyfactor.PKI.PEM.PemUtilities.PEMToDER(pemPrivateKey), out _); |
96 | | - cert = KeyTypeEnum == SupportedKeyTypeEnum.RSA ? cert.CopyWithPrivateKey((RSA)alg) : cert.CopyWithPrivateKey((ECDsa)alg); |
97 | | - |
98 | | - // save certificate |
99 | | - certificateStore.LoadCertificateStore(certificateStoreSerializer, false); |
100 | | - certificateStore.AddCertificate((alias ?? cert.Thumbprint), Convert.ToBase64String(cert.Export(X509ContentType.Pfx)), overwrite, null, RemoveRootCertificate); |
101 | | - certificateStore.SaveCertificateStore(certificateStoreSerializer.SerializeRemoteCertificateStore(certificateStore.GetCertificateStore(), storePathFile.Path, storePathFile.File, StorePassword, certificateStore.RemoteHandler)); |
102 | | - |
103 | | - logger.LogDebug($"END add Operation for {config.CertificateStoreDetails.StorePath} on {config.CertificateStoreDetails.ClientMachine}."); |
104 | | - } |
105 | | - |
106 | | - catch (Exception ex) |
107 | | - { |
108 | | - string errorMessage = $"Exception for {config.Capability}: {RemoteFileException.FlattenExceptionMessages(ex, string.Empty)} for job id {config.JobId}"; |
109 | | - logger.LogError(errorMessage); |
110 | | - return new JobResult() { Result = OrchestratorJobStatusJobResult.Failure, JobHistoryId = config.JobHistoryId, FailureMessage = $"Site {config.CertificateStoreDetails.StorePath} on server {config.CertificateStoreDetails.ClientMachine}: {errorMessage}" }; |
111 | | - } |
112 | | - finally |
113 | | - { |
114 | | - if (certificateStore.RemoteHandler != null) |
115 | | - certificateStore.Terminate(); |
116 | | - } |
| 44 | + //ICertificateStoreSerializer certificateStoreSerializer = GetCertificateStoreSerializer(config.CertificateStoreDetails.Properties); |
| 45 | + |
| 46 | + //try |
| 47 | + //{ |
| 48 | + // SetJobProperties(config, config.CertificateStoreDetails, logger); |
| 49 | + |
| 50 | + // string alias = "abcd"; |
| 51 | + // string sans = "reenroll2.Keyfactor.com&reenroll1.keyfactor.com&reenroll3.Keyfactor.com"; |
| 52 | + // bool overwrite = true; |
| 53 | + |
| 54 | + // // validate parameters |
| 55 | + // string KeyTypes = string.Join(",", Enum.GetNames(typeof(SupportedKeyTypeEnum))); |
| 56 | + // if (!Enum.TryParse(KeyType.ToUpper(), out SupportedKeyTypeEnum KeyTypeEnum)) |
| 57 | + // { |
| 58 | + // throw new RemoteFileException($"Unsupported KeyType value {KeyType}. Supported types are {KeyTypes}."); |
| 59 | + // } |
| 60 | + |
| 61 | + // ApplicationSettings.FileTransferProtocolEnum fileTransferProtocol = ApplicationSettings.FileTransferProtocol; |
| 62 | + |
| 63 | + // certificateStore = new RemoteCertificateStore(config.CertificateStoreDetails.ClientMachine, UserName, UserPassword, config.CertificateStoreDetails.StorePath, StorePassword, fileTransferProtocol, SSHPort, IncludePortInSPN); |
| 64 | + // certificateStore.Initialize(SudoImpersonatedUser, UseShellCommands); |
| 65 | + |
| 66 | + // PathFile storePathFile = RemoteCertificateStore.SplitStorePathFile(config.CertificateStoreDetails.StorePath); |
| 67 | + |
| 68 | + // if (!certificateStore.DoesStoreExist()) |
| 69 | + // { |
| 70 | + // throw new RemoteFileException($"Certificate store {config.CertificateStoreDetails.StorePath} does not exist on server {config.CertificateStoreDetails.ClientMachine}."); |
| 71 | + // } |
| 72 | + |
| 73 | + // // generate CSR and call back to enroll certificate |
| 74 | + // string csr = string.Empty; |
| 75 | + // string pemPrivateKey = string.Empty; |
| 76 | + // if (CreateCSROnDevice) |
| 77 | + // { |
| 78 | + // csr = certificateStore.GenerateCSROnDevice(SubjectText, KeyTypeEnum, KeySize, new List<string>(sans.Split('&', StringSplitOptions.RemoveEmptyEntries)), out pemPrivateKey); |
| 79 | + // } |
| 80 | + // else |
| 81 | + // { |
| 82 | + // csr = certificateStore.GenerateCSR(SubjectText, KeyTypeEnum, KeySize, new List<string>(sans.Split('&', StringSplitOptions.RemoveEmptyEntries))); |
| 83 | + // } |
| 84 | + |
| 85 | + // X509Certificate2 cert = submitReenrollment.Invoke(csr); |
| 86 | + // if (cert == null || String.IsNullOrEmpty(pemPrivateKey)) |
| 87 | + // throw new RemoteFileException("Enrollment of CSR failed. Please check Keyfactor Command logs for more information on potential enrollment errors."); |
| 88 | + |
| 89 | + // AsymmetricAlgorithm alg = KeyTypeEnum == SupportedKeyTypeEnum.RSA ? RSA.Create() : ECDsa.Create(); |
| 90 | + // alg.ImportEncryptedPkcs8PrivateKey(string.Empty, Keyfactor.PKI.PEM.PemUtilities.PEMToDER(pemPrivateKey), out _); |
| 91 | + // cert = KeyTypeEnum == SupportedKeyTypeEnum.RSA ? cert.CopyWithPrivateKey((RSA)alg) : cert.CopyWithPrivateKey((ECDsa)alg); |
| 92 | + |
| 93 | + // // save certificate |
| 94 | + // certificateStore.LoadCertificateStore(certificateStoreSerializer, false); |
| 95 | + // certificateStore.AddCertificate((alias ?? cert.Thumbprint), Convert.ToBase64String(cert.Export(X509ContentType.Pfx)), overwrite, null, RemoveRootCertificate); |
| 96 | + // certificateStore.SaveCertificateStore(certificateStoreSerializer.SerializeRemoteCertificateStore(certificateStore.GetCertificateStore(), storePathFile.Path, storePathFile.File, StorePassword, certificateStore.RemoteHandler)); |
| 97 | + |
| 98 | + // logger.LogDebug($"END add Operation for {config.CertificateStoreDetails.StorePath} on {config.CertificateStoreDetails.ClientMachine}."); |
| 99 | + //} |
| 100 | + |
| 101 | + //catch (Exception ex) |
| 102 | + //{ |
| 103 | + // string errorMessage = $"Exception for {config.Capability}: {RemoteFileException.FlattenExceptionMessages(ex, string.Empty)} for job id {config.JobId}"; |
| 104 | + // logger.LogError(errorMessage); |
| 105 | + // return new JobResult() { Result = OrchestratorJobStatusJobResult.Failure, JobHistoryId = config.JobHistoryId, FailureMessage = $"Site {config.CertificateStoreDetails.StorePath} on server {config.CertificateStoreDetails.ClientMachine}: {errorMessage}" }; |
| 106 | + //} |
| 107 | + //finally |
| 108 | + //{ |
| 109 | + // if (certificateStore.RemoteHandler != null) |
| 110 | + // certificateStore.Terminate(); |
| 111 | + //} |
117 | 112 |
|
118 | 113 | logger.LogDebug($"...End {config.Capability} job for job id {config.JobId}"); |
119 | 114 | return new JobResult() { Result = OrchestratorJobStatusJobResult.Success, JobHistoryId = config.JobHistoryId }; |
|
0 commit comments