Skip to content

Commit e711dd8

Browse files
Fix for .crt files der encoded
1 parent 2f4340a commit e711dd8

2 files changed

Lines changed: 44 additions & 14 deletions

File tree

AnyAgent/CertManager.cs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
using System.Security.Cryptography.X509Certificates;
2222
using System.Text;
2323
using CSS.Common.Logging;
24-
using CSS.PKI.PEM;
2524
using DataPower.API.api;
2625
using DataPower.API.client;
2726
using Keyfactor.Platform.Extensions.Agents;
@@ -479,7 +478,7 @@ public AnyErrors AddPubCert(AnyJobConfigInfo addPubConfig, CertStoreInfo ci, Nam
479478

480479
private AnyErrors RemoveCertFromDomain(AnyJobConfigInfo removeConfig, CertStoreInfo ci, NamePrefix np)
481480
{
482-
var error = new AnyErrors {HasError = false};
481+
var error = new AnyErrors { HasError = false };
483482
Logger.Trace($"Entering RemoveCertStore for {removeConfig.Job.Alias} ");
484483
Logger.Trace(
485484
$"Entering RemoveCertStore for Domain: {ci.Domain} and Certificate Store: {ci.CertificateStore}");
@@ -536,7 +535,7 @@ private AnyErrors RemoveCertFromDomain(AnyJobConfigInfo removeConfig, CertStoreI
536535

537536
private AnyErrors RemoveFile(AnyJobConfigInfo removeConfig, CertStoreInfo ci, string filename)
538537
{
539-
var error = new AnyErrors {HasError = false};
538+
var error = new AnyErrors { HasError = false };
540539
Logger.Trace($"Entering RemoveFile for {removeConfig.Job.Alias} ");
541540
Logger.Trace($"Entering RemoveFile for Domain: {ci.Domain} and Certificate Store: {ci.CertificateStore}");
542541
var apiClient = new ApiClient(removeConfig.Server.Username, removeConfig.Server.Password,
@@ -729,14 +728,14 @@ private void ReplaceCryptoObject(CertStoreInfo ci, string cryptoCertObjectName,
729728
public InventoryResult GetPublicCerts(ApiClient apiClient)
730729
{
731730
var result = new InventoryResult();
732-
var error = new AnyErrors {HasError = false};
731+
var error = new AnyErrors { HasError = false };
733732

734733
Logger.Trace("GetPublicCerts");
735734
var viewCert = new ViewPublicCertificatesRequest();
736735
var viewCertificateCollection = apiClient.ViewPublicCertificates(viewCert);
737736

738737
var intCount = 0;
739-
char[] s = {','};
738+
char[] s = { ',' };
740739

741740

742741
var intMax = Convert.ToInt32(_appConfig.AppSettings.Settings["MaxInventoryCapacity"].Value);
@@ -758,24 +757,21 @@ public InventoryResult GetPublicCerts(ApiClient apiClient)
758757
Logger.Trace($"Add to List: {pc.Name}");
759758
var pem = Convert.FromBase64String(viewCertResponse.File);
760759

761-
var pemString = pc.Name.EndsWith(".crt") ? PemUtilities.DERToPEM(pem, PemUtilities.PemObjectType.Certificate) : Encoding.UTF8.GetString(pem);
760+
var pemString = Utility.GetPemFromResponse(pem);
762761

763762
Logger.Trace($"Pem File: {pemString}");
764763

765764
if (pemString.Contains("BEGIN CERTIFICATE"))
766765
{
767766
Logger.Trace("Valid Pem File Adding to KF");
768-
var cert = new X509Certificate2(pemString);
769-
var b64 = Convert.ToBase64String(cert.Export(X509ContentType.Cert));
770-
Logger.Trace($"Created X509Certificate2: {cert.SerialNumber} : {cert.Subject}");
771767

772768
if (intCount < intMax)
773769
{
774-
if (!blackList.Contains(pc.Name) && cert.Thumbprint != null)
770+
if (!blackList.Contains(pc.Name))
775771
inventoryItems.Add(
776772
new AgentCertStoreInventoryItem
777773
{
778-
Certificates = new[] {b64},
774+
Certificates = new[] { pemString },
779775
Alias = pc.Name,
780776
PrivateKeyEntry = false,
781777
ItemStatus = AgentInventoryItemStatus.Unknown,
@@ -810,7 +806,7 @@ public InventoryResult GetPublicCerts(ApiClient apiClient)
810806
public InventoryResult GetCerts(ApiClient apiClient)
811807
{
812808
var result = new InventoryResult();
813-
var error = new AnyErrors {HasError = false};
809+
var error = new AnyErrors { HasError = false };
814810

815811
Logger.Trace("GetCerts");
816812
var viewCert = new ViewCryptoCertificatesRequest(apiClient.Domain);

AnyAgent/utility.cs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
// limitations under the License.
1414

1515
using System;
16+
using System.Security.Cryptography.X509Certificates;
17+
using System.Text;
18+
using CSS.PKI.PEM;
1619
using DataPower.API.api;
1720
using Keyfactor.Platform.Extensions.Agents;
1821
using Newtonsoft.Json;
@@ -37,8 +40,8 @@ public static NamePrefix ParseStoreProperties(AnyJobConfigInfo config)
3740

3841
public static string Base64Encode(string plainText)
3942
{
40-
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
41-
return System.Convert.ToBase64String(plainTextBytes);
43+
var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
44+
return Convert.ToBase64String(plainTextBytes);
4245
}
4346

4447
public static CertStoreInfo ParseCertificateConfig(AnyJobConfigInfo config)
@@ -83,6 +86,37 @@ public static string ReplaceFirstOccurrence(string source, string find, string r
8386
return result;
8487
}
8588

89+
public static string GetPemFromResponse(byte[] pem)
90+
{
91+
92+
string pemString;
93+
try
94+
{
95+
pemString = PemUtilities.DERToPEM(pem, PemUtilities.PemObjectType.Certificate);
96+
var ba = Encoding.ASCII.GetBytes(pemString);
97+
var cert = new X509Certificate2(ba);
98+
}
99+
catch (Exception e)
100+
{
101+
pemString = String.Empty;
102+
}
103+
104+
if (pemString.Length == 0)
105+
{
106+
try
107+
{
108+
pemString = Encoding.UTF8.GetString(pem);
109+
var ba = Encoding.ASCII.GetBytes(pemString);
110+
var cert = new X509Certificate2(ba);
111+
}
112+
catch (Exception)
113+
{
114+
pemString = String.Empty;
115+
}
116+
}
117+
118+
return pemString;
119+
}
86120

87121
}
88122
}

0 commit comments

Comments
 (0)