Skip to content

Commit

Permalink
Merge pull request #6 from Keyfactor/crtsupport
Browse files Browse the repository at this point in the history
Crtsupport
  • Loading branch information
bhillkeyfactor authored Aug 23, 2022
2 parents 15b93cd + 8d90461 commit 89dba42
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 17 deletions.
1 change: 1 addition & 0 deletions DataPower/DataPower.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<ItemGroup>
<PackageReference Include="Keyfactor.Logging" Version="1.1.1" />
<PackageReference Include="Keyfactor.Orchestrators.IOrchestratorJobExtensions" Version="0.6.0" />
<PackageReference Include="Keyfactor.PKI" Version="4.6.1" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
<PackageReference Include="Portable.BouncyCastle" Version="1.9.0" />
<PackageReference Include="System.Management.Automation" Version="7.0.5" />
Expand Down
43 changes: 26 additions & 17 deletions DataPower/RequestManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
35 changes: 35 additions & 0 deletions DataPower/Utility.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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;
}
}
}

0 comments on commit 89dba42

Please sign in to comment.