diff --git a/DataPower/DataPower.csproj b/DataPower/DataPower.csproj index 945df36..f018c40 100644 --- a/DataPower/DataPower.csproj +++ b/DataPower/DataPower.csproj @@ -14,6 +14,7 @@ + diff --git a/DataPower/RequestManager.cs b/DataPower/RequestManager.cs index 64af4f3..5f8828e 100644 --- a/DataPower/RequestManager.cs +++ b/DataPower/RequestManager.cs @@ -1028,29 +1028,38 @@ public JobResult GetPublicCerts(InventoryJobConfiguration config, DataPowerClien _logger.LogTrace($"Cert Detail Response: {JsonConvert.SerializeObject(viewCertResponse)}"); _logger.LogTrace($"Add to List: {pc.Name}"); - var pem = Convert.FromBase64String(viewCertResponse.File); + var pemString = Utility.GetPemFromResponse(pem); var cert = new X509Certificate2(pem); _logger.LogTrace($"Created X509Certificate2: {cert.SerialNumber} : {cert.Subject}"); - if (intCount < intMax) + if (pemString.Contains("BEGIN CERTIFICATE")) + { + _logger.LogTrace("Valid Pem File Adding to KF"); + + if (intCount < intMax) + { + if (!blackList.Contains(pc.Name) && cert.Thumbprint != null) + inventoryItems.Add( + new CurrentInventoryItem + { + Alias = pc.Name, + Certificates = new[] { pemString }, + ItemStatus = OrchestratorInventoryItemStatus.Unknown, + PrivateKeyEntry = false, + UseChainLevel = true + }); + + intCount++; + + _logger.LogTrace($"Inv-Certs: {pc.Name}"); + _logger.LogTrace($"Certificates: {viewCertResponse.File}"); + } + } + else { - if (!blackList.Contains(pc.Name) && cert.Thumbprint != null) - inventoryItems.Add( - new CurrentInventoryItem - { - Alias = pc.Name, - Certificates = new[] {Encoding.Default.GetString(pem)}, - ItemStatus = OrchestratorInventoryItemStatus.Unknown, - PrivateKeyEntry = true, - UseChainLevel = false - }); - - intCount++; - - _logger.LogTrace($"Inv-Certs: {pc.Name}"); - _logger.LogTrace($"Certificates: {viewCertResponse.File}"); + _logger.LogTrace("Not a valid Pem File, Skipping the Add to Keyfactor..."); } } catch (Exception ex) diff --git a/DataPower/Utility.cs b/DataPower/Utility.cs index 950caa9..f15873e 100644 --- a/DataPower/Utility.cs +++ b/DataPower/Utility.cs @@ -1,7 +1,10 @@ using System; +using System.Security.Cryptography.X509Certificates; +using System.Text; using Keyfactor.Extensions.Orchestrator.DataPower.Models.SupportingObjects; using Keyfactor.Logging; using Keyfactor.Orchestrators.Extensions; +using Keyfactor.PKI.PEM; using Microsoft.Extensions.Logging; using Newtonsoft.Json; @@ -166,5 +169,37 @@ public static string ReplaceFirstOccurrence(string source, string find, string r throw; } } + + public static string GetPemFromResponse(byte[] pem) + { + + string pemString; + try + { + pemString = PemUtilities.DERToPEM(pem, PemUtilities.PemObjectType.Certificate); + var ba = Encoding.ASCII.GetBytes(pemString); + var _ = new X509Certificate2(ba); + } + catch (Exception) + { + pemString = String.Empty; + } + + if (pemString.Length == 0) + { + try + { + pemString = Encoding.UTF8.GetString(pem); + var ba = Encoding.ASCII.GetBytes(pemString); + var _ = new X509Certificate2(ba); + } + catch (Exception) + { + pemString = String.Empty; + } + } + + return pemString; + } } } \ No newline at end of file